sp_checkEntity.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "sp_checkEntity.h"
  2. #include <set>
  3. #include <string>
  4. #include <list>
  5. #include "modCheck.h"
  6. #include <algorithm>
  7. #include <iterator>
  8. #include <cctype>
  9. #include <windows.h>
  10. using namespace std;
  11. class spCheckLock
  12. {
  13. public:
  14. spCheckLock() {
  15. static bool isInit = false;
  16. if (!isInit)
  17. {
  18. InitializeCriticalSection(&cs_);
  19. isInit = true;
  20. }
  21. EnterCriticalSection(&cs_);
  22. }
  23. ~spCheckLock() {
  24. LeaveCriticalSection(&cs_);
  25. }
  26. private:
  27. spCheckLock(const spCheckLock&);
  28. spCheckLock& operator =(const spCheckLock&);
  29. private:
  30. static CRITICAL_SECTION cs_;
  31. };
  32. CRITICAL_SECTION spCheckLock::cs_;
  33. class spModInfo
  34. {
  35. public:
  36. spModInfo(string name);
  37. bool operator == (const string name){
  38. //全部转换成小写再进行比较,原因为发现ent中的实体名不一致
  39. string dstCompareName = name, dstModName = m_name, dstAliasName = m_aliasName;
  40. transform(dstCompareName.begin(), dstCompareName.end(), dstCompareName.begin(), (int(*)(int))tolower);
  41. transform(dstModName.begin(), dstModName.end(), dstModName.begin(), (int(*)(int))tolower);
  42. transform(dstAliasName.begin(), dstAliasName.end(), dstAliasName.begin(), (int(*)(int))tolower);
  43. return (dstCompareName == dstModName) || (0 != dstAliasName.length() ? dstCompareName == dstAliasName : false);
  44. }
  45. bool operator == (const void *module){
  46. return module == this->m_module;
  47. }
  48. void setspModule(void *module){
  49. m_module = module;
  50. }
  51. void AddModThread(DWORD threadId){
  52. toolkit_SetthreadGroup(threadId, m_name.c_str());
  53. }
  54. bool findModThread(DWORD threadId){
  55. return toolkit_checkThreadInName(threadId, m_name.c_str());
  56. }
  57. //return the SpModule class object pointer
  58. void* getspModule(){
  59. return m_module;
  60. }
  61. void setAliasName(const string name){
  62. m_aliasName = name;
  63. }
  64. EntityGloabalResource* getEntityResource(){
  65. return &m_entityResource;
  66. }
  67. const string getEntityName(){
  68. return m_name;
  69. }
  70. private:
  71. //实体资源,实体关闭时需销毁
  72. string m_name;
  73. string m_aliasName;
  74. void* m_module;
  75. void* m_pool;
  76. EntityGloabalResource m_entityResource;
  77. };
  78. list<spModInfo*> g_moduleArr;
  79. spModInfo *g_singleModule = NULL; //for spshell.exe or group 0 entity
  80. spModInfo::spModInfo(string name)
  81. :m_name(name),
  82. m_module(NULL),
  83. m_aliasName("")
  84. {
  85. }
  86. spModInfo* findModuleInfo(string name)
  87. {
  88. if (NULL != g_singleModule)
  89. return g_singleModule;
  90. for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
  91. {
  92. if (**i == name)
  93. return *i;
  94. }
  95. return NULL;
  96. }
  97. spModInfo* findThreadModuleInfo(DWORD threadId)
  98. {
  99. if (NULL != g_singleModule)
  100. {
  101. SetthreadGroup(GetCurrentThreadId(), g_singleModule->getEntityName().c_str());
  102. return g_singleModule;
  103. }
  104. for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
  105. {
  106. if ((*i)->findModThread(threadId))
  107. return (*i);
  108. }
  109. return NULL;
  110. }
  111. bool CreateModuleInfo(const char *name)
  112. {
  113. if (NULL == name || NULL != findModuleInfo(name) || NULL != g_singleModule)
  114. return false;
  115. spCheckLock curLock;
  116. toolkit_CreateModuleInfo(name);
  117. spModInfo *curModInfo = new spModInfo(name);
  118. if (!strcmp(name, ENTITY_SINGLE_GROUPNAME))
  119. g_singleModule = curModInfo;
  120. else
  121. g_moduleArr.push_back(curModInfo);
  122. return true;
  123. }
  124. bool SetSpModule(const char *name, void *module)
  125. {
  126. if (NULL == name || NULL == module)
  127. return false;
  128. spCheckLock curLock;
  129. spModInfo *curModInfo = findModuleInfo(name);
  130. if (NULL != curModInfo)
  131. {
  132. curModInfo->setspModule((void*)module);
  133. return true;
  134. }
  135. return false;
  136. }
  137. bool CleanModuleThread(const char *name)
  138. {
  139. if (NULL == name)
  140. return false;
  141. if (NULL != g_singleModule)
  142. return true;
  143. spCheckLock curLock;
  144. string modName = name;
  145. for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
  146. {
  147. if (**i == modName)
  148. {
  149. //remove threads
  150. toolkit_TerminateThreadExcept(GetCurrentThreadId(), name);
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. bool DestoryModuleInfo(const char *name)
  157. {
  158. if (NULL == name)
  159. return false;
  160. if (NULL != g_singleModule)
  161. return true;
  162. spCheckLock curLock;
  163. string modName = name;
  164. for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
  165. {
  166. if (**i == modName)
  167. {
  168. delete *i;
  169. g_moduleArr.erase(i);
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. bool SetthreadGroup(DWORD threadId, const char *name)
  176. {
  177. if (NULL == name)
  178. return false;
  179. spCheckLock curLock;
  180. spModInfo *curModInfo = findModuleInfo(name);
  181. if (NULL == curModInfo)
  182. return false;
  183. curModInfo->AddModThread(threadId);
  184. return true;
  185. }
  186. bool SetThreadGroupByResource(DWORD threadId, void *resource)
  187. {
  188. if (NULL == resource)
  189. return false;
  190. spCheckLock curLock;
  191. if (NULL != g_singleModule)
  192. {
  193. if (resource == g_singleModule->getEntityResource()->m_timer)
  194. {
  195. g_singleModule->AddModThread(threadId);
  196. return true;
  197. }
  198. else
  199. return false;
  200. }
  201. for (list<spModInfo*>::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++)
  202. {
  203. EntityGloabalResource *curResource = (*i)->getEntityResource();
  204. if (NULL != curResource && curResource->m_timer == resource)
  205. {
  206. (*i)->AddModThread(threadId);
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. bool findThreadModule(DWORD threadId, void **module)
  213. {
  214. spCheckLock curLock;
  215. spModInfo *curModInfo = findThreadModuleInfo(threadId);
  216. if (NULL != curModInfo)
  217. {
  218. *module = curModInfo->getspModule();
  219. return true;
  220. }
  221. return false;
  222. }
  223. void* findModuleByName(const char *name)
  224. {
  225. if (NULL == name)
  226. return false;
  227. spCheckLock curLock;
  228. return findModuleInfo(name);
  229. }
  230. bool setModuleAliasName(const char *aliasName)
  231. {
  232. spCheckLock curLock;
  233. spModInfo *curModInfo = findThreadModuleInfo(GetCurrentThreadId());
  234. if (NULL != curModInfo)
  235. {
  236. curModInfo->setAliasName(aliasName);
  237. return true;
  238. }
  239. return false;
  240. }
  241. EntityGloabalResource* getEntityResource()
  242. {
  243. spCheckLock curLock;
  244. spModInfo *curModInfo = findThreadModuleInfo(GetCurrentThreadId());
  245. if (NULL != curModInfo)
  246. return curModInfo->getEntityResource();
  247. return NULL;
  248. }