#include "sp_checkEntity.h" #include #include #include #include "modCheck.h" #include #include #include #include using namespace std; class spCheckLock { public: spCheckLock() { static bool isInit = false; if (!isInit) { InitializeCriticalSection(&cs_); isInit = true; } EnterCriticalSection(&cs_); } ~spCheckLock() { LeaveCriticalSection(&cs_); } private: spCheckLock(const spCheckLock&); spCheckLock& operator =(const spCheckLock&); private: static CRITICAL_SECTION cs_; }; CRITICAL_SECTION spCheckLock::cs_; class spModInfo { public: spModInfo(string name); bool operator == (const string name){ //全部转换成小写再进行比较,原因为发现ent中的实体名不一致 string dstCompareName = name, dstModName = m_name, dstAliasName = m_aliasName; transform(dstCompareName.begin(), dstCompareName.end(), dstCompareName.begin(), (int(*)(int))tolower); transform(dstModName.begin(), dstModName.end(), dstModName.begin(), (int(*)(int))tolower); transform(dstAliasName.begin(), dstAliasName.end(), dstAliasName.begin(), (int(*)(int))tolower); return (dstCompareName == dstModName) || (0 != dstAliasName.length() ? dstCompareName == dstAliasName : false); } bool operator == (const void *module){ return module == this->m_module; } void setspModule(void *module){ m_module = module; } void AddModThread(DWORD threadId){ toolkit_SetthreadGroup(threadId, m_name.c_str()); } bool findModThread(DWORD threadId){ return toolkit_checkThreadInName(threadId, m_name.c_str()); } //return the SpModule class object pointer void* getspModule(){ return m_module; } void setAliasName(const string name){ m_aliasName = name; } EntityGloabalResource* getEntityResource(){ return &m_entityResource; } const string getEntityName(){ return m_name; } private: //实体资源,实体关闭时需销毁 string m_name; string m_aliasName; void* m_module; void* m_pool; EntityGloabalResource m_entityResource; }; list g_moduleArr; spModInfo *g_singleModule = NULL; //for spshell.exe or group 0 entity spModInfo::spModInfo(string name) :m_name(name), m_module(NULL), m_aliasName("") { } spModInfo* findModuleInfo(string name) { if (NULL != g_singleModule) return g_singleModule; for (list::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++) { if (**i == name) return *i; } return NULL; } spModInfo* findThreadModuleInfo(DWORD threadId) { if (NULL != g_singleModule) { SetthreadGroup(GetCurrentThreadId(), g_singleModule->getEntityName().c_str()); return g_singleModule; } for (list::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++) { if ((*i)->findModThread(threadId)) return (*i); } return NULL; } bool CreateModuleInfo(const char *name) { if (NULL == name || NULL != findModuleInfo(name) || NULL != g_singleModule) return false; spCheckLock curLock; toolkit_CreateModuleInfo(name); spModInfo *curModInfo = new spModInfo(name); if (!strcmp(name, ENTITY_SINGLE_GROUPNAME)) g_singleModule = curModInfo; else g_moduleArr.push_back(curModInfo); return true; } bool SetSpModule(const char *name, void *module) { if (NULL == name || NULL == module) return false; spCheckLock curLock; spModInfo *curModInfo = findModuleInfo(name); if (NULL != curModInfo) { curModInfo->setspModule((void*)module); return true; } return false; } bool CleanModuleThread(const char *name) { if (NULL == name) return false; if (NULL != g_singleModule) return true; spCheckLock curLock; string modName = name; for (list::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++) { if (**i == modName) { //remove threads toolkit_TerminateThreadExcept(GetCurrentThreadId(), name); return true; } } return false; } bool DestoryModuleInfo(const char *name) { if (NULL == name) return false; if (NULL != g_singleModule) return true; spCheckLock curLock; string modName = name; for (list::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++) { if (**i == modName) { delete *i; g_moduleArr.erase(i); return true; } } return false; } bool SetthreadGroup(DWORD threadId, const char *name) { if (NULL == name) return false; spCheckLock curLock; spModInfo *curModInfo = findModuleInfo(name); if (NULL == curModInfo) return false; curModInfo->AddModThread(threadId); return true; } bool SetThreadGroupByResource(DWORD threadId, void *resource) { if (NULL == resource) return false; spCheckLock curLock; if (NULL != g_singleModule) { if (resource == g_singleModule->getEntityResource()->m_timer) { g_singleModule->AddModThread(threadId); return true; } else return false; } for (list::iterator i = g_moduleArr.begin(); i != g_moduleArr.end(); i++) { EntityGloabalResource *curResource = (*i)->getEntityResource(); if (NULL != curResource && curResource->m_timer == resource) { (*i)->AddModThread(threadId); return true; } } return false; } bool findThreadModule(DWORD threadId, void **module) { spCheckLock curLock; spModInfo *curModInfo = findThreadModuleInfo(threadId); if (NULL != curModInfo) { *module = curModInfo->getspModule(); return true; } return false; } void* findModuleByName(const char *name) { if (NULL == name) return false; spCheckLock curLock; return findModuleInfo(name); } bool setModuleAliasName(const char *aliasName) { spCheckLock curLock; spModInfo *curModInfo = findThreadModuleInfo(GetCurrentThreadId()); if (NULL != curModInfo) { curModInfo->setAliasName(aliasName); return true; } return false; } EntityGloabalResource* getEntityResource() { spCheckLock curLock; spModInfo *curModInfo = findThreadModuleInfo(GetCurrentThreadId()); if (NULL != curModInfo) return curModInfo->getEntityResource(); return NULL; }