Guest User

Untitled

a guest
Jul 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1. Index: D2BS.cpp
  2. ===================================================================
  3. --- D2BS.cpp (revision 1689)
  4. +++ D2BS.cpp (working copy)
  5. @@ -18,7 +18,7 @@
  6. #endif
  7.  
  8. static HANDLE hD2Thread = INVALID_HANDLE_VALUE;
  9. -
  10. +static HANDLE hEventThread = INVALID_HANDLE_VALUE;
  11. BOOL WINAPI DllMain(HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
  12. {
  13. switch(dwReason)
  14. @@ -76,6 +76,7 @@
  15.  
  16. BOOL Startup(void)
  17. {
  18. + InitializeCriticalSection(&Vars.cEventSection);
  19. InitializeCriticalSection(&Vars.cRoomSection);
  20. InitializeCriticalSection(&Vars.cMiscSection);
  21. InitializeCriticalSection(&Vars.cScreenhookSection);
  22. @@ -103,7 +104,7 @@
  23.  
  24. if((hD2Thread = CreateThread(NULL, NULL, D2Thread, NULL, NULL, NULL)) == NULL)
  25. return FALSE;
  26. -
  27. + hEventThread = CreateThread(0, 0, EventThread, NULL, 0, 0);
  28. return TRUE;
  29. }
  30.  
  31. Index: D2BS.h
  32. ===================================================================
  33. --- D2BS.h (revision 1689)
  34. +++ D2BS.h (working copy)
  35. @@ -6,7 +6,7 @@
  36. #define XP_WIN
  37. #define JS_THREADSAFE
  38.  
  39. -#define D2BS_VERSION "1.4.1688"
  40. +#define D2BS_VERSION "1.4.1689"
  41.  
  42. #include <windows.h>
  43. #include <vector>
  44. @@ -78,7 +78,8 @@
  45.  
  46. std::map<unsigned __int32, CellFile*> mCachedCellFiles;
  47. std::vector<std::pair<DWORD, DWORD> > vUnitList;
  48. -
  49. + std::list<Event*> EventList;
  50. + CRITICAL_SECTION cEventSection;
  51. CRITICAL_SECTION cRoomSection;
  52. CRITICAL_SECTION cMiscSection;
  53. CRITICAL_SECTION cScreenhookSection;
  54. Index: D2Handlers.cpp
  55. ===================================================================
  56. --- D2Handlers.cpp (revision 1689)
  57. +++ D2Handlers.cpp (working copy)
  58. @@ -349,7 +349,7 @@
  59. break;
  60. case WM_MOUSEMOVE:
  61. // would be nice to enable these events but they bog down too much
  62. - //MouseMoveEvent(pt);
  63. + MouseMoveEvent(pt);
  64. //Genhook::ForEachVisibleHook(HoverHook, &helper, 1);
  65. break;
  66. }
  67. Index: Map/Diablo_II/ActMap.cpp
  68. ===================================================================
  69. --- Map/Diablo_II/ActMap.cpp (revision 1689)
  70. +++ Map/Diablo_II/ActMap.cpp (working copy)
  71. @@ -208,7 +208,8 @@
  72. return val;
  73. }
  74.  
  75. -void ActMap::CleanUp(void){
  76. +void ActMap::CleanUp(void) const
  77. +{
  78.  
  79. for(RoomList::iterator it = RoomsAdded.begin(); it != RoomsAdded.end(); it++)
  80. RemoveRoomData(*it);
  81. Index: Map/Diablo_II/ActMap.h
  82. ===================================================================
  83. --- Map/Diablo_II/ActMap.h (revision 1689)
  84. +++ Map/Diablo_II/ActMap.h (working copy)
  85. @@ -49,8 +49,7 @@
  86. class ActMap : public Map
  87. {
  88. public:
  89. - void ActMap::CleanUp(void);
  90. -
  91. +
  92. enum CollisionFlag {
  93. None = 0x0000,
  94. BlockWalk = 0x0001,
  95. @@ -114,7 +113,8 @@
  96. ~ActMap(void);
  97.  
  98. public:
  99. -
  100. + void CleanUp(void) const;
  101. +
  102. static ActMap* GetMap(Level* level);
  103. static void ClearCache(void);
  104. void Dump(const char* file, const PointList& points) const;
  105. Index: Map/Diablo_II/LevelMap.cpp
  106. ===================================================================
  107. --- Map/Diablo_II/LevelMap.cpp (revision 1689)
  108. +++ Map/Diablo_II/LevelMap.cpp (working copy)
  109. @@ -16,7 +16,10 @@
  110. cache[level->dwLevelNo] = map;
  111. return map;
  112. }
  113. -
  114. +void LevelMap::CleanUp(void) const
  115. +{
  116. + //Dummy Sub here because act map has it
  117. +}
  118. void LevelMap::ClearCache(void)
  119. {
  120. LevelMapList::iterator end = cache.end();
  121. Index: Map/Diablo_II/LevelMap.h
  122. ===================================================================
  123. --- Map/Diablo_II/LevelMap.h (revision 1689)
  124. +++ Map/Diablo_II/LevelMap.h (working copy)
  125. @@ -42,7 +42,7 @@
  126. class LevelMap : public Map
  127. {
  128. public:
  129. - void LevelMap::CleanUp(void);
  130. +
  131.  
  132. enum CollisionFlag {
  133. None = 0x0000,
  134. @@ -116,11 +116,11 @@
  135. ~LevelMap(void);
  136.  
  137. public:
  138. -
  139. +
  140. static LevelMap* GetMap(Level* level);
  141. static void ClearCache(void);
  142. void Dump(const char* file, const PointList& points) const;
  143. -
  144. + void CleanUp(void) const;
  145. Point AbsToRelative(const Point& point) const;
  146. Point RelativeToAbs(const Point& point) const;
  147.  
  148. Index: Map/Map.h
  149. ===================================================================
  150. --- Map/Map.h (revision 1689)
  151. +++ Map/Map.h (working copy)
  152. @@ -14,7 +14,7 @@
  153. {
  154. public:
  155. virtual void Dump(const char* file, const PointList& points) const = 0;
  156. -
  157. + virtual void CleanUp(void) const = 0;
  158. virtual int GetWidth(void) const = 0;
  159. virtual int GetHeight(void) const = 0;
  160.  
  161. Index: Map/Path/AStarPath.h
  162. ===================================================================
  163. --- Map/Path/AStarPath.h (revision 1689)
  164. +++ Map/Path/AStarPath.h (working copy)
  165. @@ -78,7 +78,7 @@
  166. alloc.construct(begin, Node(start, NULL, 0, estimate(map, start, end)));
  167. nodes.push_back(begin);
  168. open.push(begin);
  169. -
  170. + DWORD ticks = GetTickCount();
  171. while(!open.empty())
  172. {
  173. Node* current = open.top();
  174. @@ -91,7 +91,16 @@
  175. *result = current;
  176. return;
  177. }
  178. -
  179. + // this is to make d2 not completly freeze on long paths.
  180. + if (GetTickCount() - ticks > 500){
  181. + map->CleanUp();
  182. + LeaveCriticalSection(&Vars.cGameLoopSection);
  183. + InterlockedDecrement(&Vars.SectionCount);
  184. + Sleep(1);
  185. + InterlockedIncrement(&Vars.SectionCount);
  186. + EnterCriticalSection(&Vars.cGameLoopSection);
  187. + ticks=GetTickCount();
  188. + }
  189. bool result = closed.insert(current->point).second;
  190. assert(result == true);
  191. (void)(result); // shut up compiler about unused variable warning
  192. @@ -134,7 +143,7 @@
  193. {
  194. Node* result = NULL;
  195. Point start = _start, end = _end;
  196. -
  197. +
  198. // if we don't have a valid start and end, try mutating the points
  199. if(reducer->Reject(start, abs))
  200. reducer->MutatePoint(start, abs);
  201. Index: Script.cpp
  202. ===================================================================
  203. --- Script.cpp (revision 1689)
  204. +++ Script.cpp (working copy)
  205. @@ -433,10 +433,24 @@
  206. evt->argc = argc;
  207. evt->argv = argv;
  208. evt->object = globalObject;
  209. +
  210. + int timeout = 0;
  211. + while(! TryEnterCriticalSection(&Vars.cEventSection) )
  212. + {
  213. + timeout++ ;
  214. + if(timeout > 3)
  215. + {
  216. + HANDLE hThread;
  217. + hThread = CreateThread(0, 0, FuncThread, evt, 0, 0);
  218. + CloseHandle(hThread);
  219. + return;
  220. + }
  221. + Sleep(10);
  222. + }
  223.  
  224. - HANDLE hThread;
  225. - hThread = CreateThread(0, 0, FuncThread, evt, 0, 0);
  226. - CloseHandle(hThread);
  227. + Vars.EventList.push_front(evt);
  228. + LeaveCriticalSection(&Vars.cEventSection);
  229. +
  230. }
  231. bool Script::ExecEvent(char* evtName, uintN argc, AutoRoot** argv)
  232. {
  233. @@ -593,3 +607,90 @@
  234.  
  235. return block;
  236. }
  237. +DWORD WINAPI EventThread(LPVOID lpParam)
  238. +{
  239. +
  240. + while(Vars.bNeedShutdown)
  241. + {
  242. + Sleep(10);
  243. + while(Vars.EventList.size() > 0)
  244. + {
  245. + EnterCriticalSection(&Vars.cEventSection);
  246. + Event* evt = Vars.EventList.back();
  247. + Vars.EventList.pop_back();
  248. + LeaveCriticalSection(&Vars.cEventSection);
  249. +
  250. + JSContext* cx = JS_NewContext(ScriptEngine::GetRuntime(), 8192);
  251. + JS_SetContextPrivate(cx, evt->owner);
  252. + JS_BeginRequest(cx);
  253. +
  254. + callEventFunction(cx,evt); // call the first event
  255. + bool match = false; // vars to keep event list unlocked dont want the list locked while in event call
  256. + bool fullSearch = false;
  257. + while (!fullSearch)
  258. + {
  259. + match = false;
  260. + EnterCriticalSection(&Vars.cEventSection); // call any other events on the que with the same script
  261. + for(list<Event*>::iterator it = Vars.EventList.begin(); it != Vars.EventList.end(); it++)
  262. + {
  263. + if((*it)->owner->GetThreadId() == evt->owner->GetThreadId())
  264. + {
  265. + match = true;
  266. + evt=(*it);
  267. + Vars.EventList.erase(it);
  268. + break;
  269. + }
  270. + }
  271. + LeaveCriticalSection(&Vars.cEventSection);
  272. + if(match)
  273. + callEventFunction(cx,evt);
  274. + else
  275. + fullSearch = true;
  276. + }
  277. +
  278. + JS_DestroyContextNoGC(cx);
  279. + // we have to clean up the event
  280. + for(uintN i = 0; i < evt->argc; i++)
  281. + {
  282. + evt->argv[i]->Release();
  283. + if(evt->argv[i])
  284. + delete evt->argv[i];
  285. + }
  286. + if(evt->argv)
  287. + delete[] evt->argv;
  288. + delete evt;
  289. + }
  290. + }
  291. + return true;
  292. +}
  293. +bool callEventFunction(JSContext* cx ,Event* evt)
  294. +{
  295. + bool block =false;
  296. + if(evt->owner->IsRunning() && !(evt->owner->GetState() == InGame && ClientState() != ClientStateInGame))
  297. + {
  298. + jsval* args = new jsval[evt->argc];
  299. + for(uintN i = 0; i < evt->argc; i++)
  300. + {
  301. + args[i] = *evt->argv[i]->value();
  302. + if(JS_AddRoot(&args[i]) == JS_FALSE)
  303. + {
  304. + if(evt->argv)
  305. + delete[] evt->argv;
  306. + delete evt;
  307. + return false;
  308. + }
  309. + }
  310. + jsval rval = JSVAL_VOID;
  311. +
  312. + for(FunctionList::iterator it = evt->functions.begin(); it != evt->functions.end(); it++)
  313. + {
  314. + JS_CallFunctionValue(cx, evt->object, *(*it)->value(), evt->argc, args, &rval);
  315. + block |= (JSVAL_IS_BOOLEAN(rval) && JSVAL_TO_BOOLEAN(rval));
  316. + }
  317. +
  318. + for(uintN i = 0; i < evt->argc; i++)
  319. + JS_RemoveRoot(&args[i]);
  320. + delete[] args;
  321. + }
  322. +return block;
  323. +}
  324. Index: Script.h
  325. ===================================================================
  326. --- Script.h (revision 1689)
  327. +++ Script.h (working copy)
  328. @@ -107,3 +107,5 @@
  329. DWORD WINAPI RunCommandThread(void* data);
  330. DWORD WINAPI ScriptThread(void* data);
  331. DWORD WINAPI FuncThread(void* data);
  332. +DWORD WINAPI EventThread(LPVOID lpParam);
  333. +bool callEventFunction(JSContext* cx ,Event* evt);
  334. \ No newline at end of file
Add Comment
Please, Sign In to add comment