Advertisement
Guest User

Untitled

a guest
May 12th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 47.76 KB | None | 0 0
  1. Index: Control.cpp
  2. ===================================================================
  3. --- Control.cpp (revision 1003)
  4. +++ Control.cpp (working copy)
  5.  -163,7 +163,7 @@
  6.     return false;
  7.  }
  8.  
  9. -BOOL OOG_CreateCharacter(char* szCharacter, int type, bool hardcore, bool ladder)
  10. +BOOL OOG_CreateCharacter(char* szCharacter, int type, bool hardcore, bool ladder, bool expansion)
  11.  {
  12.     if(OOG_GetLocation() != OOG_CHAR_SELECT || strlen(szCharacter) > 15 || type > 6 || type < 0)
  13.         return FALSE;
  14.  -203,7 +203,7 @@
  15.         return FALSE; // something bad happened?
  16.  
  17.     // set the name
  18. -// ctrl = findControl();
  19. +   //ctrl = findControl();
  20.     // still need to find the name editbox, set the name, and click the various
  21.     // checkboxes and the ok button
  22.  
  23. Index: Control.h
  24. ===================================================================
  25. --- Control.h   (revision 1003)
  26. +++ Control.h   (working copy)
  27.  -6,7 +6,7 @@
  28.  Control* findControl(int Type, int LocaleID, int Disabled, int PosX, int PosY, int SizeX, int SizeY);
  29.  Control* findControl(int Type, char* Text, int Disabled, int PosX, int PosY, int SizeX, int SizeY);
  30.  bool clickControl(Control* pControl, int x = -1, int y = -1);
  31. -BOOL OOG_CreateCharacter(char* szCharacter, int type, bool hardcore, bool ladder);
  32. +BOOL OOG_CreateCharacter(char* szCharacter, int type, bool hardcore, bool ladder, bool expansion);
  33.  BOOL OOG_SelectCharacter(char * szCharacter);
  34.  BOOL OOG_SelectGateway(char * szGateway, size_t strSize);
  35.  void SetControlText(Control* pControl, const char* Text);
  36. Index: Core.cpp
  37. ===================================================================
  38. --- Core.cpp    (revision 1008)
  39. +++ Core.cpp    (working copy)
  40.  -18,12 +18,11 @@
  41.     if(str.length() < 1 || maxlen < 2)
  42.         return false;
  43.  
  44. -   size_t pos, len;
  45. +   size_t pos;
  46.     string tmp(str);
  47.  
  48.     while(tmp.length() > maxlen)
  49.     {
  50. -       len = tmp.length();
  51.         // maxlen-1 since std::string::npos indexes from 0
  52.         pos = tmp.find_last_of(delim, maxlen-1);
  53.         if(!pos || pos == string::npos)
  54.  -44,11 +43,9 @@
  55.         else
  56.             DebugBreak();
  57.     }
  58. -   if(!tmp.length())
  59. -       DebugBreak();
  60.  
  61. -   if(tmp.length())
  62. -       lst.push_back(tmp);
  63. +   ASSERT(tmp.length());
  64. +   lst.push_back(tmp);
  65.  
  66.     return true;
  67.  }
  68.  -68,42 +65,39 @@
  69.  
  70.     replace(str, str + len, REPLACE_CHAR, '%');
  71.  
  72. -   const uint maxlen = 98;
  73. -
  74.     // Break into lines through \n.
  75.     list<string> lines;
  76.     string temp;
  77.     stringstream ss(str);
  78. +
  79. +   delete[] str;
  80. +   str = NULL;
  81. +
  82. +   const uint maxlen = 98;
  83. +
  84.     while(getline(ss, temp))
  85.         SplitLines(temp, maxlen, ' ', lines);
  86.  
  87.     EnterCriticalSection(&Vars.cPrintSection);
  88. -   if(Vars.bUseGamePrint)
  89. +
  90. +   for(list<string>::iterator it = lines.begin(); it != lines.end(); ++it)
  91.     {
  92. -       if(ClientState() == ClientStateInGame)
  93. +       if(Vars.bUseGamePrint)
  94.         {
  95. -           // Convert and send every line.
  96. -           for(list<string>::iterator it = lines.begin(); it != lines.end(); ++it)
  97. +           if(ClientState() == ClientStateInGame)
  98.             {
  99. -               wchar_t * output = AnsiToUnicode(it->c_str());
  100. +               wchar_t *output = AnsiToUnicode(it->c_str());
  101.                 D2CLIENT_PrintGameString(output, 0);
  102.                 delete [] output;
  103.             }
  104. +           else if(ClientState() == ClientStateMenu && findControl(4, (char *)NULL, -1, 28, 410, 354, 298))
  105. +               D2MULTI_PrintChannelText((char* )it->c_str(), 0);
  106.         }
  107. -       else if(ClientState() == ClientStateMenu && findControl(4, (char *)NULL, -1, 28, 410, 354, 298))    
  108. -       {
  109. -           // TODO: Double check this function, make sure it is working as intended.
  110. -           for(list<string>::iterator it = lines.begin(); it != lines.end(); ++it)
  111. -               D2MULTI_PrintChannelText((char* )it->c_str(), 0);  
  112. -       }
  113. -   }
  114. -   for(list<string>::iterator it = lines.begin(); it != lines.end(); ++it)
  115. +       else
  116.             Console::AddLine(*it);
  117. +   }
  118.  
  119.     LeaveCriticalSection(&Vars.cPrintSection);
  120. -
  121. -   delete[] str;
  122. -   str = NULL;
  123.  }
  124.  
  125.  void __declspec(naked) __fastcall Say_ASM(DWORD dwPtr)
  126. Index: D2Handlers.cpp
  127. ===================================================================
  128. --- D2Handlers.cpp  (revision 1003)
  129. +++ D2Handlers.cpp  (working copy)
  130.  -36,8 +36,9 @@
  131.     ClientGameState oldState = ClientState();
  132.     while(Vars.bActive)
  133.     {
  134. +       // TODO look at this more
  135.         ClientGameState state = ClientState();
  136. -       if(oldState != state)
  137. +       if((state == ClientStateInGame || state == ClientStateMenu) && oldState != state)
  138.         {
  139.             // we switched from in game to out or something
  140.             JS_TriggerAllOperationCallbacks(ScriptEngine::GetRuntime());
  141.  -55,11 +56,11 @@
  142.                             (!IsTownLevel(GetPlayerArea()) &&
  143.                             (Vars.nChickenHP && Vars.nChickenHP >= GetUnitHP(D2CLIENT_GetPlayerUnit())) ||
  144.                             (Vars.nChickenMP && Vars.nChickenMP >= GetUnitMP(D2CLIENT_GetPlayerUnit()))))
  145. -                   D2CLIENT_ExitGame();
  146. +                       D2CLIENT_ExitGame();
  147.                 }
  148.                 else
  149.                 {
  150. -                   Sleep(1000);
  151. +                   //Sleep(1000);
  152.  
  153.                     Vars.dwGameTime = GetTickCount();
  154.                     D2CLIENT_InitInventory();
  155.  -81,11 +82,10 @@
  156.                 }
  157.                 break;
  158.             }
  159. -           case ClientStateBusy:
  160. -           case ClientStateNull:
  161. +           default:
  162.                 break;
  163.         }
  164. -       Sleep(50);
  165. +       Sleep(10);
  166.     }
  167.  
  168.     ScriptEngine::Shutdown();
  169.  -98,16 +98,14 @@
  170.     if(Vars.bDontCatchNextMsg)
  171.     {
  172.         Vars.bDontCatchNextMsg = FALSE;
  173. -       return NULL;
  174. +       return 0;
  175.     }
  176.  
  177.     char* szBuffer = UnicodeToAnsi(wMsg);
  178.     bool result = false;
  179.  
  180.     if(szBuffer[0] == '.')
  181. -   {
  182.         result = ProcessCommand(szBuffer+1, false);
  183. -   }
  184.  
  185.     delete[] szBuffer;
  186.  
  187.  -129,7 +127,7 @@
  188.         case 0xA7: return DelayedStateHandler(pPacket, dwSize);
  189.     }
  190.  
  191. -   return TRUE;
  192. +   return 1;
  193.  }
  194.  
  195.  LONG WINAPI GameEventHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  196.  -181,7 +179,7 @@
  197.         bool chatBoxOpen = gameState ? !!D2CLIENT_GetUIState(5) : false;
  198.         bool escMenuOpen = gameState ? !!D2CLIENT_GetUIState(9) : false;
  199.  
  200. -       if (altState && wParam == VK_F4)
  201. +       if(altState && wParam == VK_F4)
  202.             return CallNextHookEx(NULL, code, wParam, lParam);
  203.  
  204.         if(Vars.bBlockKeys)
  205.  -198,7 +196,7 @@
  206.  
  207.                 return CallNextHookEx(NULL, code, wParam, lParam);
  208.             }
  209. -       }          
  210. +       }
  211.         else if(wParam == VK_ESCAPE && Console::IsVisible())
  212.         {
  213.             if(isDown && !isRepeat && code == HC_ACTION )
  214.  -323,7 +321,7 @@
  215.         DrawLogo();
  216.         Console::Draw();
  217.     }
  218. -   Sleep(10);
  219. +   Sleep(1);
  220.  }
  221.  
  222.  void GameDrawOOG(void)
  223.  -365,26 +363,31 @@
  224.     PlayerAssignEvent(pPlayer->dwUnitId);
  225.  }
  226.  
  227. -BOOL __stdcall GameLoop(LPMSG lpMsg, HWND hWnd, UINT wMsgFIlterMin, UINT wMsgFilterMax, UINT wRemoveMsg)
  228. +BOOL __stdcall GameLoop(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg)
  229.  {
  230.     if(Vars.bGameLoopEntered)
  231. -   {
  232.         LeaveCriticalSection(&Vars.cGameLoopSection);
  233. -   }
  234. -   else Vars.bGameLoopEntered = true;
  235. +   else
  236. +       Vars.bGameLoopEntered = true;
  237.  
  238.     EnterCriticalSection(&Vars.cGameLoopSection);
  239.  
  240. -   return PeekMessage(lpMsg, hWnd, wMsgFIlterMin, wMsgFilterMax, wRemoveMsg);
  241. +   /*
  242. +    * Temporary hack needed to avoid issues with WaitForClientState when
  243. +    * entering Tyreals portal after killing baal for quest, I'm looking
  244. +    * into a solution.
  245. +    */
  246. +   //BOOL ret = PeekMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
  247. +   //Sleep(1);
  248. +   return PeekMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
  249.  }
  250.  
  251.  void GameLeave(void)
  252.  {
  253.     if(Vars.bGameLoopEntered)
  254. -   {
  255.         LeaveCriticalSection(&Vars.cGameLoopSection);
  256. -   }
  257. -   else Vars.bGameLoopEntered = true;
  258. +   else
  259. +       Vars.bGameLoopEntered = true;
  260.  
  261.     // Stop ingame scripts at this point ..
  262.     // otherwise we deadlock ...
  263. Index: D2Helpers.cpp
  264. ===================================================================
  265. --- D2Helpers.cpp   (revision 1005)
  266. +++ D2Helpers.cpp   (working copy)
  267.  -144,6 +144,22 @@
  268.  
  269.  ClientGameState ClientState(void)
  270.  {
  271. +
  272. +   // Temp debugging helper for spammage.
  273. +#if 0
  274. +   static DWORD tick = GetTickCount();
  275. +   static int times;
  276. +   if(GetTickCount() - tick <= 1000)
  277. +   {
  278. +       times++;
  279. +       if(times > 200)
  280. +       {
  281. +           times = 0;
  282. +           DebugBreak();
  283. +       }
  284. +   }
  285. +#endif
  286. +
  287.     if(*p_D2CLIENT_PlayerUnit && !(*p_D2WIN_FirstControl))
  288.     {
  289.         if((*p_D2CLIENT_PlayerUnit)->pInventory &&
  290.  -160,26 +176,39 @@
  291.     else if(!(*p_D2CLIENT_PlayerUnit) && *p_D2WIN_FirstControl)
  292.         return ClientStateMenu;
  293.     else if(!(*p_D2CLIENT_PlayerUnit) && !(*p_D2WIN_FirstControl))
  294. -       return ClientStateNull;
  295. +   {
  296. +       // Throttle since we need to wait anyways.
  297. +       Sleep(10);
  298. +       return ClientStateBusy;
  299. +   }
  300. +
  301.  //#ifdef DEBUG
  302.  // else
  303.  //     DebugBreak();
  304.  //#endif
  305. +
  306. +   // This should only happen on injection
  307. +   Sleep(50);
  308.     return ClientStateNull;
  309.  }
  310.  
  311. -bool WaitForGameReady(void)
  312. +bool WaitForClientState(ClientGameState WantedState)
  313.  {
  314.     DWORD start = GetTickCount();
  315.     do
  316.     {
  317.         switch(ClientState())
  318.         {
  319. -           case ClientStateNull: case ClientStateMenu: return false;
  320. -           case ClientStateInGame: return true;
  321. +           case ClientStateInGame:
  322. +               if(WantedState == ClientStateInGame)
  323. +                   return true;
  324. +           case ClientStateMenu:
  325. +               if(WantedState == ClientStateMenu)
  326. +                   return true;
  327.         }
  328.         Sleep(10);
  329. -   } while((Vars.dwGameTimeout == 0 ) || (Vars.dwGameTimeout > 0 && (GetTickCount() - start) < Vars.dwGameTimeout));
  330. +   } while(Vars.dwGameTimeout == 0 || (Vars.dwGameTimeout > 0 && (GetTickCount() - start) < Vars.dwGameTimeout));
  331. +
  332.     return false;
  333.  }
  334.  
  335.  -219,7 +248,8 @@
  336.  
  337.  INT GetSkill(WORD wSkillId)
  338.  {
  339. -   if(!D2CLIENT_GetPlayerUnit()) return 0;
  340. +   if(!D2CLIENT_GetPlayerUnit())
  341. +       return 0;
  342.  
  343.     for(Skill* pSkill = D2CLIENT_GetPlayerUnit()->pInfo->pFirstSkill; pSkill; pSkill = pSkill->pNextSkill)
  344.         if(pSkill->pSkillInfo->wSkillId == wSkillId)
  345. Index: D2Helpers.h
  346. ===================================================================
  347. --- D2Helpers.h (revision 1003)
  348. +++ D2Helpers.h (working copy)
  349.  -19,7 +19,7 @@
  350.  WORD   GetUnitY(UnitAny* pUnit);
  351.  ClientGameState ClientState(void);
  352.  bool   GameReady(void);
  353. -bool   WaitForGameReady(void);
  354. +bool WaitForClientState(ClientGameState WantedState = ClientStateInGame);
  355.  DWORD  GetPlayerArea(void);
  356.  void   ScreenSizeCheck(POINT* pPoint);
  357.  void   SendMouseClick(INT x, INT y, INT clicktype);
  358. Index: Helpers.cpp
  359. ===================================================================
  360. --- Helpers.cpp (revision 1009)
  361. +++ Helpers.cpp (working copy)
  362.  -67,17 +67,10 @@
  363.  
  364.  void InitSettings(void)
  365.  {
  366. -   char fname[_MAX_FNAME+MAX_PATH],
  367. -        scriptPath[_MAX_FNAME+MAX_PATH],
  368. -        debug[6],
  369. -        blockMinimize[6],
  370. -        quitOnHostile[6],
  371. -        quitOnError[6],
  372. -        maxGameTime[6],
  373. -        gameTimeout[6],
  374. -        startAtMenu[6],
  375. -        disableCache[6],
  376. -        memUsage[6],
  377. +   char fname[_MAX_FNAME+MAX_PATH],    scriptPath[_MAX_FNAME+MAX_PATH],
  378. +        debug[6],          blockMinimize[6],       quitOnHostile[6],
  379. +        quitOnError[6],    maxGameTime[6],         gameTimeout[6],
  380. +        startAtMenu[6],    disableCache[6],        memUsage[6],
  381.          gamePrint[6];
  382.  
  383.     sprintf_s(fname, sizeof(fname), "%sd2bs.ini", Vars.szPath);
  384.  -117,6 +110,9 @@
  385.     int i = 0;
  386.     while(!Vars.bActive)
  387.     {
  388. +       Sleep(50);
  389. +       i++;
  390. +
  391.         if(i >= 300)
  392.         {
  393.             MessageBox(0, "Failed to set hooks, exiting!", "D2BS", 0);
  394.  -151,8 +147,6 @@
  395.             if(ClientState() == ClientStateMenu && Vars.bStartAtMenu)
  396.                 clickControl(*p_D2WIN_FirstControl);
  397.         }
  398. -       Sleep(50);
  399. -       i++;
  400.     }
  401.     return true;
  402.  }
  403.  -162,7 +156,7 @@
  404.     return (ClientState() == ClientStateInGame ? "default.dbj" : ClientState() == ClientStateMenu ? "starter.dbj" : NULL);
  405.  }
  406.  
  407. -ScriptState GetStarterScriptState(void)
  408. +ScriptType GetStarterScriptType(void)
  409.  {
  410.     // the default return is InGame because that's the least harmful of the options
  411.     return (ClientState() == ClientStateInGame ? InGame : ClientState() == ClientStateMenu ? OutOfGame : InGame);
  412.  -174,11 +168,11 @@
  413.     return (script && CreateThread(0, 0, ScriptThread, script, 0, 0) != INVALID_HANDLE_VALUE);
  414.  }
  415.  
  416. -bool StartScript(const char* scriptname, ScriptState state)
  417. +bool StartScript(const char* scriptname, ScriptType scriptType)
  418.  {
  419.     char file[_MAX_FNAME+_MAX_PATH];
  420.     sprintf_s(file, _MAX_FNAME+_MAX_PATH, "%s\\%s", Vars.szScriptPath, scriptname);
  421. -   Script* script = ScriptEngine::CompileFile(file, state);
  422. +   Script* script = ScriptEngine::CompileFile(file, scriptType);
  423.     return (script && CreateThread(0, 0, ScriptThread, script, 0, 0) != INVALID_HANDLE_VALUE);
  424.  }
  425.  
  426.  -196,7 +190,7 @@
  427.     Sleep(500);
  428.  
  429.     const char* script = GetStarterScriptName();
  430. -   if(StartScript(script, GetStarterScriptState()))
  431. +   if(StartScript(script, GetStarterScriptType()))
  432.         Print("ÿc2D2BSÿc0 :: Started %s", script);
  433.     else
  434.         Print("ÿc2D2BSÿc0 :: Failed to start %s", script);
  435.  -216,7 +210,7 @@
  436.     if(_strcmpi(argv, "start") == 0)
  437.     {
  438.         const char* script = GetStarterScriptName();
  439. -       if(StartScript(script, GetStarterScriptState()))
  440. +       if(StartScript(script, GetStarterScriptType()))
  441.             Print("ÿc2D2BSÿc0 :: Started %s", script);
  442.         else
  443.             Print("ÿc2D2BSÿc0 :: Failed to start %s", script);
  444.  -239,7 +233,7 @@
  445.     else if(_strcmpi(argv, "load") == 0)
  446.     {
  447.         const char* script = command+5;
  448. -       if(StartScript(script, GetStarterScriptState()))
  449. +       if(StartScript(script, GetStarterScriptType()))
  450.             Print("ÿc2D2BSÿc0 :: Started %s", script);
  451.         else
  452.             Print("ÿc2D2BSÿc0 :: Failed to start %s", script);
  453.  -266,7 +260,7 @@
  454.  void GameJoined(void)
  455.  {
  456.     Print("ÿc2D2BSÿc0 :: Starting default.dbj");
  457. -   if(StartScript(GetStarterScriptName(), GetStarterScriptState()))
  458. +   if(StartScript(GetStarterScriptName(), GetStarterScriptType()))
  459.         Print("ÿc2D2BSÿc0 :: default.dbj running.");
  460.     else
  461.         Print("ÿc2D2BSÿc0 :: Failed to start default.dbj!");
  462.  -277,7 +271,7 @@
  463.     if(beginStarter)
  464.     {
  465.         Print("ÿc2D2BSÿc0 :: Starting starter.dbj");
  466. -       if(StartScript(GetStarterScriptName(), GetStarterScriptState()))
  467. +       if(StartScript(GetStarterScriptName(), GetStarterScriptType()))
  468.             Print("ÿc2D2BSÿc0 :: starter.dbj running.");
  469.         else
  470.             Print("ÿc2D2BSÿc0 :: Failed to start starter.dbj!");
  471. Index: Helpers.h
  472. ===================================================================
  473. --- Helpers.h   (revision 1003)
  474. +++ Helpers.h   (working copy)
  475.  -13,7 +13,7 @@
  476.  void InitSettings(void);
  477.  bool InitHooks(void);
  478.  bool ExecCommand(const char* command);
  479. -bool StartScript(const char* script, ScriptState state);
  480. +bool StartScript(const char* script, ScriptType scriptType);
  481.  void Reload(void);
  482.  bool ProcessCommand(const char* command, bool unprocessedIsCommand);
  483.  
  484. Index: JSArea.cpp
  485. ===================================================================
  486. --- JSArea.cpp  (revision 1003)
  487. +++ JSArea.cpp  (working copy)
  488.  -106,7 +106,7 @@
  489.  
  490.  JSAPI_FUNC(my_getArea)
  491.  {
  492. -   if(!WaitForGameReady())
  493. +   if(!WaitForClientState())
  494.         THROW_ERROR(cx, "Game not ready");
  495.  
  496.     int32 nArea = GetPlayerArea();
  497. Index: JSControl.cpp
  498. ===================================================================
  499. --- JSControl.cpp   (revision 1003)
  500. +++ JSControl.cpp   (working copy)
  501.  -143,9 +143,9 @@
  502.     if(ClientState() != ClientStateMenu)
  503.         return JS_TRUE;
  504.  
  505. -   ControlData *pData = ((ControlData*)JS_GetPrivate(cx, obj));
  506. +   ControlData *pData = (ControlData*)JS_GetPrivate(cx, obj);
  507.     if(!pData)
  508. -       return JS_TRUE;
  509. +       return JS_FALSE;
  510.  
  511.     Control* pControl = findControl(pData->dwType, (char *)NULL, -1, pData->dwX, pData->dwY, pData->dwSizeX, pData->dwSizeY);
  512.     if(pControl && pControl->pNext)
  513.  -168,7 +168,8 @@
  514.     {
  515.         JS_ClearScope(cx, obj);
  516.         if(JS_ValueToObject(cx, JSVAL_NULL, &obj) == JS_FALSE)
  517. -           return JS_TRUE;
  518. +           return JS_FALSE;
  519. +
  520.         *rval = JSVAL_FALSE;
  521.     }
  522.    
  523.  -180,7 +181,7 @@
  524.     if(ClientState() != ClientStateMenu)
  525.         return JS_TRUE;
  526.  
  527. -   ControlData *pData = ((ControlData*)JS_GetPrivate(cx, obj));
  528. +   ControlData *pData = (ControlData*)JS_GetPrivate(cx, obj);
  529.     if(!pData)
  530.         return JS_TRUE;
  531.  
  532.  -239,19 +240,28 @@
  533.     if(ClientState() != ClientStateMenu)
  534.         return JS_TRUE;
  535.  
  536. -   ControlData *pData = ((ControlData*)JS_GetPrivate(cx, obj));
  537. +   ControlData *pData = (ControlData*)JS_GetPrivate(cx, obj);
  538.     if(!pData)
  539.         return JS_TRUE;
  540.  
  541.     Control* pControl = findControl(pData->dwType, (char *)NULL, -1, pData->dwX, pData->dwY, pData->dwSizeX, pData->dwSizeY);
  542.     if(!pControl)
  543.     {
  544. -       *rval = INT_TO_JSVAL(0);
  545. +       JS_ClearScope(cx, obj);
  546. +       if(JS_ValueToObject(cx, JSVAL_NULL, &obj) == JS_FALSE)
  547. +           return JS_FALSE;
  548. +       *rval = JSVAL_FALSE;
  549.         return JS_TRUE;
  550.     }
  551.  
  552.     if(pControl->dwType != 4 || !pControl->pFirstText)
  553. +   {
  554. +       JS_ClearScope(cx, obj);
  555. +       if(JS_ValueToObject(cx, JSVAL_NULL, &obj) == JS_FALSE)
  556. +           return JS_FALSE;
  557. +       *rval = JSVAL_FALSE;
  558.         return JS_TRUE;
  559. +   }
  560.  
  561.     JSObject* pReturnArray = JS_NewArrayObject(cx, 0, NULL);
  562.     INT nArrayCount = 0;
  563.  -283,13 +293,19 @@
  564.     int32 nType = -1, nX = -1, nY = -1, nXSize = -1, nYSize = -1;
  565.     int32 *args[] = {&nType, &nX, &nY, &nXSize, &nYSize};
  566.  
  567. -   for(uintN i = 0; i < argc; i++)
  568. +   for(uintN i = 0; i < 5; i++)
  569.         if(JSVAL_IS_INT(argv[i]))
  570.             JS_ValueToECMAInt32(cx, argv[i], args[i]);
  571.  
  572.     Control* pControl = findControl(nType, (char*)NULL, -1, nX, nY, nXSize, nYSize);
  573.     if(!pControl)
  574. +   {
  575. +       JS_ClearScope(cx, obj);
  576. +       if(JS_ValueToObject(cx, JSVAL_NULL, &obj) == JS_FALSE)
  577. +           return JS_FALSE;
  578. +       *rval = JSVAL_FALSE;
  579.         return JS_TRUE;
  580. +   }
  581.  
  582.     ControlData* data = new ControlData;
  583.     data->dwType = nType;
  584. Index: JSCore.cpp
  585. ===================================================================
  586. --- JSCore.cpp  (revision 1003)
  587. +++ JSCore.cpp  (working copy)
  588.  -2,13 +2,13 @@
  589.  #include <windows.h>
  590.  #include <ddeml.h>
  591.  
  592. -//#include "js32.h"
  593. -//#include "Script.h"
  594. +#include "js32.h"
  595. +#include "Script.h"
  596.  #include "JSCore.h"
  597.  #include "Core.h"
  598.  #include "Helpers.h"
  599.  #include "dde.h"
  600. -//#include "ScriptEngine.h"
  601. +#include "ScriptEngine.h"
  602.  //#include "D2BS.h"
  603.  #include "Events.h"
  604.  #include "Console.h"
  605.  -60,21 +60,6 @@
  606.     else
  607.         JS_ReportWarning(cx, "delay(0) called, argument must be >= 1");
  608.  
  609. -/* if(argc == 1 && JSVAL_IS_INT(argv[0]))
  610. -   {
  611. -       int nDelay = JSVAL_TO_INT(argv[0]);
  612. -       if(nDelay)
  613. -       {
  614. -           jsrefcount depth = JS_SuspendRequest(cx);
  615. -
  616. -           Sleep(nDelay);
  617. -
  618. -           JS_ResumeRequest(cx, depth);
  619. -       }
  620. -       else
  621. -           JS_ReportWarning(cx, "delay(0) called, argument must be >= 1");
  622. -   }*/
  623. -
  624.     return JS_TRUE;
  625.  }
  626.  
  627.  -100,13 +85,15 @@
  628.     }
  629.  
  630.     char buf[_MAX_PATH+_MAX_FNAME];
  631. -   ScriptState scriptState = script->GetState();
  632. -   if(scriptState == Command)
  633. -       scriptState = (ClientState() == ClientStateInGame ? InGame : OutOfGame);
  634. +   ScriptType scriptType = script->GetScriptType();
  635. +   if(scriptType == Command)
  636. +       scriptType = ClientState() == ClientStateInGame ? InGame : OutOfGame;
  637.  
  638.     sprintf_s(buf, sizeof(buf), "%s\\%s", Vars.szScriptPath, file);
  639. +  
  640. +   _strlwr_s(buf, _MAX_PATH+_MAX_FNAME);
  641.     StringReplace(buf, '/', '\\', _MAX_PATH+_MAX_FNAME);
  642. -   Script* newScript = ScriptEngine::CompileFile(buf, scriptState);
  643. +   Script* newScript = ScriptEngine::CompileFile(buf, scriptType);
  644.     if(newScript)
  645.     {
  646.         CreateThread(0, 0, ScriptThread, newScript, 0, 0);
  647.  -119,38 +106,6 @@
  648.         *rval = JSVAL_FALSE;
  649.     }
  650.  
  651. -/* if(argc > 0 && JSVAL_IS_STRING(argv[0]))
  652. -   {
  653. -       Script* execScript = (Script*)JS_GetContextPrivate(cx);
  654. -       ScriptState scriptState = execScript->GetState();
  655. -       if(scriptState == Command)
  656. -           scriptState = (GameReady() ? InGame : OutOfGame);
  657. -
  658. -       char* lpszFileName = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
  659. -       if(!(lpszFileName && lpszFileName[0]))
  660. -           THROW_ERROR(cx, obj, "Could not convert or empty string");
  661. -       StringReplace(lpszFileName, '/', '\\');
  662. -       if(strlen(lpszFileName) < (_MAX_FNAME + _MAX_PATH - strlen(Vars.szScriptPath)))
  663. -       {
  664. -           char lpszBuf[_MAX_PATH+_MAX_FNAME];
  665. -           sprintf_s(lpszBuf, sizeof(lpszBuf), "%s\\%s", Vars.szScriptPath, lpszFileName);
  666. -           Script* script = ScriptEngine::CompileFile(lpszBuf, scriptState);
  667. -           if(script)
  668. -           {
  669. -               CreateThread(0, 0, ScriptThread, script, 0, 0);
  670. -               *rval = JSVAL_TRUE;
  671. -           }
  672. -           else
  673. -           {
  674. -               // TODO: Should this actually be there? No notification is bad, but do we want this? maybe throw an exception?
  675. -               Print("File \"%s\" not found.", lpszFileName);
  676. -               *rval = JSVAL_FALSE;
  677. -           }
  678. -       }
  679. -       else
  680. -           THROW_ERROR(cx, obj, "File name exceeds max name length");
  681. -   }*/
  682. -
  683.     return JS_TRUE;
  684.  }
  685.  
  686.  -177,26 +132,10 @@
  687.  
  688.     char buf[_MAX_PATH+_MAX_FNAME];
  689.     sprintf_s(buf, sizeof(buf), "%s\\libs\\%s", Vars.szScriptPath, file);
  690. +
  691.     if(_access(buf, 0) == 0)
  692.         *rval = BOOLEAN_TO_JSVAL(script->Include(buf));
  693.  
  694. -/* if(argc > 0 && JSVAL_IS_STRING(argv[0]))
  695. -   {
  696. -       Script* script = (Script*)JS_GetContextPrivate(cx);
  697. -       if(script)
  698. -       {
  699. -           char * lpszFileName = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
  700. -           if(lpszFileName && strlen(lpszFileName) <= _MAX_FNAME)
  701. -           {
  702. -               char lpszBuf[_MAX_PATH+_MAX_FNAME];
  703. -               sprintf_s(lpszBuf, sizeof(lpszBuf), "%s\\libs\\%s", Vars.szScriptPath, lpszFileName);
  704. -               StringReplace(lpszFileName, '/', '\\');
  705. -               if(_access(lpszBuf, 0) == 0)
  706. -                   *rval = BOOLEAN_TO_JSVAL(script->Include(lpszBuf));
  707. -           }
  708. -       }
  709. -   }*/
  710. -
  711.     return JS_TRUE;
  712.  }
  713.  
  714. Index: JSGame.cpp
  715. ===================================================================
  716. --- JSGame.cpp  (revision 1008)
  717. +++ JSGame.cpp  (working copy)
  718.  -70,7 +70,7 @@
  719.  
  720.  JSAPI_FUNC(my_clickMap)
  721.  { 
  722. -   if(!WaitForGameReady())
  723. +   if(!WaitForClientState())
  724.         THROW_ERROR(cx, "Game not ready");
  725.  
  726.     uint16 nClickType = 0, nShift = 0, nX = 0xFFFF, nY = 0xFFFF;
  727.  -120,7 +120,7 @@
  728.  
  729.  JSAPI_FUNC(my_acceptTrade)
  730.  { 
  731. -   if(!WaitForGameReady())
  732. +   if(!WaitForClientState())
  733.         THROW_ERROR(cx, "Game not ready");
  734.  
  735.     // TODO: Fix this nonsense.
  736.  -167,7 +167,7 @@
  737.  
  738.  JSAPI_FUNC(my_getPath)
  739.  { 
  740. -   if(!WaitForGameReady())
  741. +   if(!WaitForClientState())
  742.         THROW_ERROR(cx, "Game not ready");
  743.  
  744.     if(argc < 5)
  745.  -307,7 +307,7 @@
  746.  
  747.  JSAPI_FUNC(my_getCollision)
  748.  { 
  749. -   if(!WaitForGameReady())
  750. +   if(!WaitForClientState())
  751.         THROW_ERROR(cx, "Game not ready");
  752.  
  753.     if(argc < 3 || !JSVAL_IS_INT(argv[0]) || !JSVAL_IS_INT(argv[1]) || !JSVAL_IS_INT(argv[2]))
  754.  -340,7 +340,7 @@
  755.     CriticalMisc myMisc;
  756.     myMisc.EnterSection();
  757.  
  758. -   if(!WaitForGameReady())
  759. +   if(!WaitForClientState())
  760.         THROW_ERROR(cx, "Game not ready");
  761.  
  762.     if(*p_D2CLIENT_TransactionDialog != 0 || *p_D2CLIENT_TransactionDialogs != 0 || *p_D2CLIENT_TransactionDialogs_2 != 0)
  763.  -697,7 +697,7 @@
  764.  
  765.  JSAPI_FUNC(my_getDistance)
  766.  { 
  767. -   if(!WaitForGameReady())
  768. +   if(!WaitForClientState())
  769.         THROW_ERROR(cx, "Game not ready");
  770.  
  771.     // TODO: Add the type of distance to the api design
  772.  -785,7 +785,7 @@
  773.  
  774.  JSAPI_FUNC(my_gold)
  775.  {
  776. -   if(!WaitForGameReady())
  777. +   if(!WaitForClientState())
  778.         THROW_ERROR(cx, "Game not ready");
  779.  
  780.     jsint nGold = NULL;
  781.  -803,7 +803,7 @@
  782.  
  783.  JSAPI_FUNC(my_checkCollision)
  784.  { 
  785. -   if(!WaitForGameReady())
  786. +   if(!WaitForClientState())
  787.         THROW_ERROR(cx, "Game not ready");
  788.  
  789.     if(argc == 3 && JSVAL_IS_OBJECT(argv[0]) && JSVAL_IS_OBJECT(argv[1]) && JSVAL_IS_INT(argv[2]))
  790.  -830,7 +830,7 @@
  791.  
  792.  JSAPI_FUNC(my_getMercHP)
  793.  { 
  794. -   if(!WaitForGameReady())
  795. +   if(!WaitForClientState())
  796.         THROW_ERROR(cx, "Game not ready");
  797.  
  798.     if(D2CLIENT_GetPlayerUnit() && D2CLIENT_GetPlayerUnit()->pAct)
  799.  -950,7 +950,7 @@
  800.  
  801.  JSAPI_FUNC(my_getTradeInfo)
  802.  { 
  803. -   if(!WaitForGameReady())
  804. +   if(!WaitForClientState())
  805.         THROW_ERROR(cx, "Game not ready");
  806.  
  807.     if(argc < 1)
  808.  -986,7 +986,7 @@
  809.  
  810.  JSAPI_FUNC(my_getUIFlag)
  811.  {
  812. -   if(!WaitForGameReady())
  813. +   if(!WaitForClientState())
  814.         THROW_ERROR(cx, "Game not ready");
  815.  
  816.     if(argc < 1 || !JSVAL_IS_INT(argv[0]))
  817.  -1003,7 +1003,7 @@
  818.  
  819.  JSAPI_FUNC(my_getWaypoint)
  820.  { 
  821. -   if(!WaitForGameReady())
  822. +   if(!WaitForClientState())
  823.         THROW_ERROR(cx, "Game not ready");
  824.  
  825.     if(argc < 1 || !JSVAL_IS_INT(argv[0]))
  826.  -1084,7 +1084,7 @@
  827.  
  828.  JSAPI_FUNC(my_clickParty)
  829.  { 
  830. -   if(!WaitForGameReady())
  831. +   if(!WaitForClientState())
  832.         THROW_ERROR(cx, "Game not ready");
  833.  
  834.     *rval = JSVAL_FALSE;
  835.  -1188,7 +1188,7 @@
  836.  { 
  837.     *rval = JSVAL_FALSE;
  838.  
  839. -   if(!WaitForGameReady())
  840. +   if(!WaitForClientState())
  841.         THROW_ERROR(cx, "Game not ready");
  842.  
  843.     jsint nParameter = NULL;
  844.  -1221,7 +1221,7 @@
  845.  
  846.  JSAPI_FUNC(my_transmute)
  847.  {
  848. -   if(!WaitForGameReady())
  849. +   if(!WaitForClientState())
  850.         THROW_ERROR(cx, "Game not ready");
  851.  
  852.     D2CLIENT_Transmute();
  853.  -1231,7 +1231,7 @@
  854.  
  855.  JSAPI_FUNC(my_getPlayerFlag)
  856.  {
  857. -   if(!WaitForGameReady())
  858. +   if(!WaitForClientState())
  859.         THROW_ERROR(cx, "Game not ready");
  860.  
  861.     if(argc != 3 || !JSVAL_IS_NUMBER(argv[0]) || !JSVAL_IS_NUMBER(argv[1]) || !JSVAL_IS_NUMBER(argv[2]))
  862.  -1296,7 +1296,7 @@
  863.  
  864.  JSAPI_FUNC(my_submitItem)
  865.  {
  866. -   if(!WaitForGameReady())
  867. +   if(!WaitForClientState())
  868.         THROW_ERROR(cx, "Game not ready");
  869.  
  870.     if(UnitAny* pUnit = D2CLIENT_GetCursorItem())
  871.  -1312,7 +1312,7 @@
  872.  
  873.  JSAPI_FUNC(my_getInteractedNPC)
  874.  {
  875. -   if(!WaitForGameReady())
  876. +   if(!WaitForClientState())
  877.         THROW_ERROR(cx, "Game not ready");
  878.  
  879.     UnitAny* pNPC = D2CLIENT_GetCurrentInteractingNPC();
  880. Index: JSMenu.cpp
  881. ===================================================================
  882. --- JSMenu.cpp  (revision 1003)
  883. +++ JSMenu.cpp  (working copy)
  884.  -128,23 +128,23 @@
  885.                     break;
  886.                 }
  887.            
  888. -           case OOG_MAIN_MENU_CONNECTING:
  889. +           case OOG_MAIN_MENU_CONNECTING:
  890.             case OOG_CHARACTER_SELECT_PLEASE_WAIT:
  891. -           case OOG_PLEASE_WAIT:
  892. +           case OOG_PLEASE_WAIT:
  893.             case OOG_GATEWAY:
  894. -           case OOG_CHARACTER_SELECT_NO_CHARS:
  895. +           case OOG_CHARACTER_SELECT_NO_CHARS:
  896.             case OOG_NONE:
  897.                 timeout++;
  898.                 break;
  899. -           case OOG_LOBBY:
  900. -           case OOG_INLINE:
  901. -           case OOG_CHAT:
  902. +           case OOG_LOBBY:
  903. +           case OOG_INLINE:
  904. +           case OOG_CHAT:
  905.             case OOG_CREATE:
  906. -           case OOG_JOIN:
  907. -           case OOG_LADDER:
  908. -           case OOG_CHANNEL:
  909. -           case OOG_GAME_EXIST:
  910. -           case OOG_GAME_DOES_NOT_EXIST:  
  911. +           case OOG_JOIN:
  912. +           case OOG_LADDER:
  913. +           case OOG_CHANNEL:
  914. +           case OOG_GAME_EXIST:
  915. +           case OOG_GAME_DOES_NOT_EXIST:
  916.                 loginComplete=TRUE;
  917.                 break;
  918.             case OOG_UNABLE_TO_CONNECT:
  919.  -161,13 +161,13 @@
  920.                 break;
  921.             default:
  922.                 errorMsg = "Unhandled login location";
  923. -               break;             
  924. +               break;
  925.         }
  926.  
  927.         if(_strcmpi(errorMsg, ""))
  928.         {
  929.             Vars.bBlockKeys = Vars.bBlockMouse = FALSE;
  930. -           THROW_ERROR(cx, errorMsg);                     
  931. +           THROW_ERROR(cx, errorMsg);
  932.             break;
  933.         }
  934.  
  935.  -290,8 +290,8 @@
  936.     {
  937.         char settings[600] = "";
  938.         sprintf_s(settings, sizeof(settings),
  939. -                   "mode=%s\tgateway=%s\tusername=%s\tpassword=%s\tcharacter=%s\tspdifficulty=%d\t",
  940. -                   mode, gateway, username, password, charname, spdifficulty);
  941. +               "mode=%s\tgateway=%s\tusername=%s\tpassword=%s\tcharacter=%s\tspdifficulty=%d\t",
  942. +               mode, gateway, username, password, charname, spdifficulty);
  943.  
  944.         StringReplace(settings, '\t', '\0', 600);
  945.         WritePrivateProfileSection(*args[0], settings, file);
  946.  -317,10 +317,10 @@
  947.  
  948.     char* name = NULL;
  949.     int32 type = -1;
  950. -   JSBool hc = JS_FALSE, ladder = JS_FALSE;
  951. -   if(!JS_ConvertArguments(cx, argc, argv, "si/bb", &name, &type, &hc, &ladder))
  952. +   JSBool hardcore = JS_FALSE, ladder = JS_FALSE, expansion = JS_TRUE;
  953. +   if(!JS_ConvertArguments(cx, argc, argv, "si/bbb", &name, &type, &hardcore, &ladder, &expansion))
  954.         return JS_FALSE;
  955.  
  956. -   *rval = BOOLEAN_TO_JSVAL(!!OOG_CreateCharacter(name, type, !!hc, !!ladder));
  957. +   *rval = BOOLEAN_TO_JSVAL(!!OOG_CreateCharacter(name, type, !!hardcore, !!ladder, !!expansion));
  958.     return JS_TRUE;
  959. -}
  960. \ No newline at end of file
  961. +}
  962. Index: JSParty.cpp
  963. ===================================================================
  964. --- JSParty.cpp (revision 1003)
  965. +++ JSParty.cpp (working copy)
  966.  -52,7 +52,7 @@
  967.  
  968.  JSAPI_FUNC(party_getNext)
  969.  {
  970. -   if(!WaitForGameReady())
  971. +   if(!WaitForClientState())
  972.         THROW_ERROR(cx, "Game not ready");
  973.  
  974.     RosterUnit *pUnit = (RosterUnit*)JS_GetPrivate(cx, obj);
  975.  -82,7 +82,7 @@
  976.  
  977.  JSAPI_FUNC(my_getParty)
  978.  { 
  979. -   if(!WaitForGameReady())
  980. +   if(!WaitForClientState())
  981.         THROW_ERROR(cx, "Game not ready");
  982.  
  983.     RosterUnit* pUnit = *p_D2CLIENT_PlayerUnitList;
  984. Index: JSPresetUnit.cpp
  985. ===================================================================
  986. --- JSPresetUnit.cpp    (revision 1003)
  987. +++ JSPresetUnit.cpp    (working copy)
  988.  -55,7 +55,7 @@
  989.  
  990.  JSAPI_FUNC(my_getPresetUnits)
  991.  {
  992. -   if(!WaitForGameReady())
  993. +   if(!WaitForClientState())
  994.         THROW_ERROR(cx, "Game not ready");
  995.  
  996.     if(argc < 1)
  997.  -140,7 +140,7 @@
  998.  
  999.  JSAPI_FUNC(my_getPresetUnit)
  1000.  {
  1001. -   if(!WaitForGameReady())
  1002. +   if(!WaitForClientState())
  1003.         THROW_ERROR(cx, "Game not ready");
  1004.  
  1005.     if(argc < 1)
  1006. Index: JSRoom.cpp
  1007. ===================================================================
  1008. --- JSRoom.cpp  (revision 1003)
  1009. +++ JSRoom.cpp  (working copy)
  1010.  -390,7 +390,7 @@
  1011.  
  1012.  JSAPI_FUNC(my_getRoom)
  1013.  {
  1014. -   if(!WaitForGameReady())
  1015. +   if(!WaitForClientState())
  1016.         THROW_ERROR(cx, "Game not ready");
  1017.  
  1018.     CriticalRoom cRoom;
  1019. Index: JSScreenHook.cpp
  1020. ===================================================================
  1021. --- JSScreenHook.cpp    (revision 1003)
  1022. +++ JSScreenHook.cpp    (working copy)
  1023.  -168,7 +168,7 @@
  1024.  {
  1025.     Script* script = (Script*)JS_GetContextPrivate(cx);
  1026.  
  1027. -   ScreenhookState state = (script->GetState () == OutOfGame) ? OOG : IG;
  1028. +   ScreenhookState state = (script->GetScriptType() == OutOfGame) ? OOG : IG;
  1029.     uint x = 0, y = 0, x2 = 0, y2 = 0;
  1030.     ushort color = 0, opacity = 0;
  1031.     Align align = Left;
  1032.  -317,7 +317,7 @@
  1033.  {
  1034.     Script* script = (Script*)JS_GetContextPrivate(cx);
  1035.  
  1036. -   ScreenhookState state = (script->GetState () == OutOfGame) ? OOG : IG;
  1037. +   ScreenhookState state = (script->GetScriptType () == OutOfGame) ? OOG : IG;
  1038.     int x = 0, y = 0, x2 = 0, y2 = 0;
  1039.     ushort color = 0;
  1040.     bool automap = false;
  1041.  -449,7 +449,7 @@
  1042.  {
  1043.     Script* script = (Script*)JS_GetContextPrivate(cx);
  1044.  
  1045. -   ScreenhookState state = (script->GetState () == OutOfGame) ? OOG : IG;
  1046. +   ScreenhookState state = (script->GetScriptType () == OutOfGame) ? OOG : IG;
  1047.     uint x = 0, y = 0;
  1048.     ushort color = 0, font = 0;
  1049.     Align align = Left;
  1050.  -598,7 +598,7 @@
  1051.  {
  1052.     Script* script = (Script*)JS_GetContextPrivate(cx);
  1053.  
  1054. -   ScreenhookState state = (script->GetState () == OutOfGame) ? OOG : IG;
  1055. +   ScreenhookState state = (script->GetScriptType () == OutOfGame) ? OOG : IG;
  1056.     uint x = 0, y = 0;
  1057.     ushort color = 0;
  1058.     Align align = Left;
  1059. Index: JSScript.cpp
  1060. ===================================================================
  1061. --- JSScript.cpp    (revision 1003)
  1062. +++ JSScript.cpp    (working copy)
  1063.  -38,7 +38,7 @@
  1064.             }
  1065.             break;
  1066.         case SCRIPT_GAMETYPE:
  1067. -           *vp = script->GetState() == InGame ? INT_TO_JSVAL(0) : INT_TO_JSVAL(1);
  1068. +           *vp = script->GetScriptType() == InGame ? INT_TO_JSVAL(0) : INT_TO_JSVAL(1);
  1069.             break;
  1070.         case SCRIPT_RUNNING:
  1071.             *vp = BOOLEAN_TO_JSVAL(script->IsRunning());
  1072.  -133,8 +133,7 @@
  1073.     else if(argc == 1 && JSVAL_IS_STRING(argv[0]))
  1074.     {
  1075.         char* name = JS_GetStringBytes(JSVAL_TO_STRING(argv[0]));
  1076. -       if(name)
  1077. -           StringReplace(name, '/', '\\', strlen(name));
  1078. +
  1079.         FindHelper args = {0, name, NULL};
  1080.         ScriptEngine::ForEachScript(FindScriptByName, &args, 1);
  1081.         if(args.script != NULL)
  1082.  -161,7 +160,7 @@
  1083.     static uint pathlen = strlen(Vars.szScriptPath) + 1;
  1084.     const char* fname = script->GetFilename();
  1085.     // calculate the relative name from the filename
  1086. -   const char* relName = (strlen(fname) > pathlen ? fname + pathlen : fname);
  1087. +   const char* relName = strlen(fname) > pathlen ? fname + pathlen : fname;
  1088.     if(_strcmpi(relName, helper->name) == 0)
  1089.     {
  1090.         helper->script = script;
  1091. Index: JSUnit.cpp
  1092. ===================================================================
  1093. --- JSUnit.cpp  (revision 1005)
  1094. +++ JSUnit.cpp  (working copy)
  1095.  -608,7 +608,7 @@
  1096.  
  1097.  JSAPI_FUNC(unit_cancel)
  1098.  { 
  1099. -   if(!WaitForGameReady())
  1100. +   if(!WaitForClientState())
  1101.         THROW_ERROR(cx, "Game not ready");
  1102.  
  1103.     if(argc == 1 && JSVAL_IS_INT(argv[0]))
  1104.  -679,7 +679,7 @@
  1105.  
  1106.  JSAPI_FUNC(unit_interact)
  1107.  { 
  1108. -   if(!WaitForGameReady())
  1109. +   if(!WaitForClientState())
  1110.         THROW_ERROR(cx, "Game not ready");
  1111.  
  1112.     myUnit* lpUnit = (myUnit*)JS_GetPrivate(cx, obj);
  1113.  -696,7 +696,7 @@
  1114.  
  1115.     if(pUnit->dwType == UNIT_ITEM && pUnit->dwMode != ITEM_MODE_ON_GROUND && pUnit->dwMode != ITEM_MODE_BEING_DROPPED)
  1116.     {
  1117. -           INT nLocation = GetItemLocation(pUnit);                
  1118. +           INT nLocation = GetItemLocation(pUnit);
  1119.            
  1120.             BYTE aPacket[13] = {NULL};
  1121.  
  1122.  -738,7 +738,8 @@
  1123.     else
  1124.     {
  1125.         *rval = JSVAL_TRUE;
  1126. -       ClickMap(0, GetUnitX(pUnit), GetUnitY(pUnit), FALSE, pUnit);
  1127. +       //ClickMap(0, GetUnitX(pUnit), GetUnitY(pUnit), FALSE, pUnit);
  1128. +       ClickMap(0, 0xFFFF, 0xFFFF, FALSE, pUnit);
  1129.         //D2CLIENT_Interact(pUnit, 0x45);
  1130.     }
  1131.  
  1132.  -750,7 +751,7 @@
  1133.  
  1134.  JSAPI_FUNC(unit_getStat)
  1135.  { 
  1136. -   if(!WaitForGameReady())
  1137. +   if(!WaitForClientState())
  1138.         THROW_ERROR(cx, "Game not ready");
  1139.  
  1140.     myUnit* lpUnit = (myUnit*)JS_GetPrivate(cx, obj);
  1141.  -939,7 +940,7 @@
  1142.  
  1143.  JSAPI_FUNC(unit_getState)
  1144.  { 
  1145. -   if(!WaitForGameReady())
  1146. +   if(!WaitForClientState())
  1147.         THROW_ERROR(cx, "Game not ready");
  1148.  
  1149.     myUnit* lpUnit = (myUnit*)JS_GetPrivate(cx, obj);
  1150.  -970,7 +971,7 @@
  1151.  
  1152.  JSAPI_FUNC(item_getFlags)
  1153.  { 
  1154. -   if(!WaitForGameReady())
  1155. +   if(!WaitForClientState())
  1156.         THROW_ERROR(cx, "Game not ready");
  1157.  
  1158.     myUnit* lpUnit = (myUnit*)JS_GetPrivate(cx, obj);
  1159.  -990,7 +991,7 @@
  1160.  
  1161.  JSAPI_FUNC(item_getFlag)
  1162.  { 
  1163. -   if(!WaitForGameReady())
  1164. +   if(!WaitForClientState())
  1165.         THROW_ERROR(cx, "Game not ready");
  1166.  
  1167.     if(argc < 1 || !JSVAL_IS_INT(argv[0]))
  1168.  -1017,7 +1018,7 @@
  1169.  { 
  1170.     DEPRECATED;
  1171.  
  1172. -   if(!WaitForGameReady())
  1173. +   if(!WaitForClientState())
  1174.         THROW_ERROR(cx, "Game not ready");
  1175.  
  1176.     INT diff = D2CLIENT_GetDifficulty();
  1177.  -1066,7 +1067,7 @@
  1178.  
  1179.  JSAPI_FUNC(item_getItemCost)
  1180.  {
  1181. -   if(!WaitForGameReady())
  1182. +   if(!WaitForClientState())
  1183.         THROW_ERROR(cx, "Game not ready");
  1184.  
  1185.     jsint nMode;
  1186.  -1113,7 +1114,7 @@
  1187.  
  1188.  JSAPI_FUNC(unit_getItems)
  1189.  { 
  1190. -   if(!WaitForGameReady())
  1191. +   if(!WaitForClientState())
  1192.         THROW_ERROR(cx, "Game not ready");
  1193.  
  1194.     myUnit* lpUnit = (myUnit*)JS_GetPrivate(cx, obj);
  1195.  -1170,7 +1171,7 @@
  1196.  
  1197.  JSAPI_FUNC(unit_getSkill)
  1198.  {
  1199. -   if(!WaitForGameReady())
  1200. +   if(!WaitForClientState())
  1201.         THROW_ERROR(cx, "Game not ready");
  1202.  
  1203.     jsint nSkillId = NULL;
  1204.  -1295,7 +1296,7 @@
  1205.     CriticalMisc myMisc;
  1206.     myMisc.EnterSection();
  1207.  
  1208. -   if(!WaitForGameReady())
  1209. +   if(!WaitForClientState())
  1210.         THROW_ERROR(cx, "Game not ready");
  1211.  
  1212.     if(*p_D2CLIENT_TransactionDialog != 0 || *p_D2CLIENT_TransactionDialogs != 0 || *p_D2CLIENT_TransactionDialogs_2 != 0)
  1213.  -1389,7 +1390,7 @@
  1214.  
  1215.  JSAPI_FUNC(unit_getParent)
  1216.  { 
  1217. -   if(!WaitForGameReady())
  1218. +   if(!WaitForClientState())
  1219.         THROW_ERROR(cx, "Game not ready");
  1220.  
  1221.     myUnit* lpUnit = (myUnit*)JS_GetPrivate(cx, obj);
  1222.  -1468,7 +1469,7 @@
  1223.  // Works only on players sinces monsters _CANT_ have mercs!
  1224.  JSAPI_FUNC(unit_getMerc)
  1225.  { 
  1226. -   if(!WaitForGameReady())
  1227. +   if(!WaitForClientState())
  1228.         THROW_ERROR(cx, "Game not ready");
  1229.  
  1230.     myUnit* lpUnit = (myUnit*)JS_GetPrivate(cx, obj);
  1231.  -1557,7 +1558,7 @@
  1232.  // unit.setSkill( int skillId OR String skillName, int hand [, int itemGlobalId] );
  1233.  JSAPI_FUNC(unit_setskill)
  1234.  { 
  1235. -   if(!WaitForGameReady())
  1236. +   if(!WaitForClientState())
  1237.         THROW_ERROR(cx, "Game not ready");
  1238.  
  1239.     WORD nSkillId = (WORD)-1;
  1240.  -1587,7 +1588,7 @@
  1241.  
  1242.  JSAPI_FUNC(my_overhead)
  1243.  { 
  1244. -   if(!WaitForGameReady())
  1245. +   if(!WaitForClientState())
  1246.         THROW_ERROR(cx, "Game not ready");
  1247.  
  1248.     myUnit *pmyUnit = (myUnit*)JS_GetPrivate(cx, obj);
  1249.  -1620,7 +1621,7 @@
  1250.  
  1251.  JSAPI_FUNC(unit_getItem)
  1252.  { 
  1253. -   if(!WaitForGameReady())
  1254. +   if(!WaitForClientState())
  1255.         THROW_ERROR(cx, "Game not ready");
  1256.  
  1257.     myUnit *pmyUnit = (myUnit*)JS_GetPrivate(cx, obj);
  1258.  -1681,7 +1682,7 @@
  1259.  
  1260.  JSAPI_FUNC(unit_move)
  1261.  {
  1262. -   if(!WaitForGameReady())
  1263. +   if(!WaitForClientState())
  1264.         THROW_ERROR(cx, "Game not ready");
  1265.  
  1266.     myUnit *pmyUnit = (myUnit*)JS_GetPrivate(cx, obj);
  1267.  -1724,7 +1725,7 @@
  1268.  
  1269.  JSAPI_FUNC(unit_getEnchant)
  1270.  {
  1271. -   if(!WaitForGameReady())
  1272. +   if(!WaitForClientState())
  1273.         THROW_ERROR(cx, "Game not ready");
  1274.    
  1275.     if(argc < 1 || !JSVAL_IS_INT(argv[0]))
  1276.  -1756,7 +1757,7 @@
  1277.  
  1278.  JSAPI_FUNC(unit_getQuest)
  1279.  {
  1280. -   if(!WaitForGameReady())
  1281. +   if(!WaitForClientState())
  1282.         THROW_ERROR(cx, "Game not ready");
  1283.    
  1284.     if(argc < 2 || !JSVAL_IS_INT(argv[0]) || !JSVAL_IS_INT(argv[1]))
  1285.  -1772,7 +1773,7 @@
  1286.  
  1287.  JSAPI_FUNC(unit_getMinionCount)
  1288.  {
  1289. -   if(!WaitForGameReady())
  1290. +   if(!WaitForClientState())
  1291.         THROW_ERROR(cx, "Game not ready");
  1292.    
  1293.     if(argc < 1 || !JSVAL_IS_INT(argv[0]))
  1294.  -1799,7 +1800,7 @@
  1295.  
  1296.  JSAPI_FUNC(me_getRepairCost)
  1297.  {
  1298. -   if(!WaitForGameReady())
  1299. +   if(!WaitForClientState())
  1300.         THROW_ERROR(cx, "Game not ready");
  1301.  
  1302.     UnitAny* npc = D2CLIENT_GetCurrentInteractingNPC();
  1303. Index: Script.cpp
  1304. ===================================================================
  1305. --- Script.cpp  (revision 1006)
  1306. +++ Script.cpp  (working copy)
  1307.  -13,21 +13,21 @@
  1308.  
  1309.  using namespace std;
  1310.  
  1311. -Script::Script(const char* file, ScriptState state) :
  1312. +Script::Script(const char* file, ScriptType scriptType) :
  1313.             context(NULL), globalObject(NULL), scriptObject(NULL), script(NULL), execCount(0),
  1314. -           isAborted(false), isPaused(false), isReallyPaused(false), scriptState(state),
  1315. +           isAborted(false), isPaused(false), isReallyPaused(false), scriptType(scriptType),
  1316.             threadHandle(INVALID_HANDLE_VALUE), threadId(0)
  1317.  {
  1318.     InitializeCriticalSection(&lock);
  1319.     EnterCriticalSection(&lock);
  1320.  
  1321. -   if(scriptState == Command)
  1322. +   if(scriptType == Command)
  1323.     {
  1324.         fileName = string("Command Line");
  1325.     }
  1326.     else
  1327.     {
  1328. -       if(_access(file, 0) != 0)
  1329. +       if(!!_access(file, 0))
  1330.             throw std::exception("File not found");
  1331.  
  1332.         char* tmpName = _strdup(file);
  1333.  -35,34 +35,60 @@
  1334.             throw std::exception("Could not dup filename");
  1335.  
  1336.         _strlwr_s(tmpName, strlen(file)+1);
  1337. +
  1338.         fileName = string(tmpName);
  1339.         replace(fileName.begin(), fileName.end(), '/', '\\');
  1340. +
  1341.         free(tmpName);
  1342.     }
  1343. +
  1344.     try
  1345.     {
  1346.         context = JS_NewContext(ScriptEngine::GetRuntime(), 0x2000);
  1347.         if(!context)
  1348.             throw std::exception("Couldn't create the context");
  1349. +   }
  1350. +   catch(std::exception &)
  1351. +   {
  1352. +       LeaveCriticalSection(&lock);
  1353. +       throw;
  1354. +   }
  1355.  
  1356. +   try
  1357. +   {
  1358.         JS_SetContextPrivate(context, this);
  1359.         JS_BeginRequest(context);
  1360.  
  1361.         globalObject = JS_GetGlobalObject(context);
  1362.         jsval meVal = JSVAL_VOID;
  1363. -       if(JS_GetProperty(GetContext(), globalObject, "me", &meVal) != JS_FALSE)
  1364. +       if(JS_GetProperty(context, globalObject, "me", &meVal) != JS_FALSE)
  1365.         {
  1366.             JSObject* meObject = JSVAL_TO_OBJECT(meVal);
  1367. -           me = (myUnit*)JS_GetPrivate(GetContext(), meObject);
  1368. +           me = (myUnit*)JS_GetPrivate(context, meObject);
  1369.         }
  1370. +       else
  1371. +           throw std::exception("Couldn't get me object");
  1372.  
  1373. -       if(state == Command)
  1374. -           script = JS_CompileScript(context, globalObject, file, strlen(file), "Command Line", 1);
  1375. +       if(scriptType == Command)
  1376. +           script = JS_CompileScript(context, globalObject, fileName.c_str(), strlen(fileName.c_str()), "Command Line", 1);
  1377.         else
  1378.             script = JS_CompileFile(context, globalObject, fileName.c_str());
  1379. +
  1380.         if(!script)
  1381.             throw std::exception("Couldn't compile the script");
  1382. +   }
  1383. +   catch(std::exception &)
  1384. +   {
  1385.  
  1386. +       JS_EndRequest(context);
  1387. +       JS_DestroyContext(context);
  1388. +
  1389. +       LeaveCriticalSection(&lock);
  1390. +       throw;
  1391. +   }
  1392. +
  1393. +   try
  1394. +   {
  1395.         scriptObject = JS_NewScriptObject(context, script);
  1396.         if(!scriptObject)
  1397.             throw std::exception("Couldn't create the script object");
  1398.  -74,16 +100,19 @@
  1399.         JS_ClearContextThread(context);
  1400.  
  1401.         LeaveCriticalSection(&lock);
  1402. -   } catch(std::exception&) {
  1403. +   }
  1404. +   catch(std::exception&)
  1405. +   {
  1406.         if(scriptObject)
  1407. +       {
  1408.             JS_RemoveRoot(&scriptObject);
  1409. -       if(script && !scriptObject)
  1410. +       }
  1411. +       else
  1412.             JS_DestroyScript(context, script);
  1413. -       if(context)
  1414. -       {
  1415. -           JS_EndRequest(context);
  1416. -           JS_DestroyContext(context);
  1417. -       }
  1418. +
  1419. +       JS_EndRequest(context);
  1420. +       JS_DestroyContext(context);
  1421. +
  1422.         LeaveCriticalSection(&lock);
  1423.         throw;
  1424.     }
  1425.  -103,7 +132,8 @@
  1426.     JS_RemoveRoot(&globalObject);
  1427.     JS_RemoveRoot(&scriptObject);
  1428.  
  1429. -   JS_DestroyContextNoGC(context);
  1430. +   JS_EndRequest(context);
  1431. +   JS_DestroyContext(context);
  1432.  
  1433.     context = NULL;
  1434.     scriptObject = NULL;
  1435.  -111,8 +141,12 @@
  1436.     script = NULL;
  1437.  
  1438.     includes.clear();
  1439. -   if(threadHandle)
  1440. +   if(threadHandle != INVALID_HANDLE_VALUE)
  1441. +   {
  1442.         CloseHandle(threadHandle);
  1443. +       threadHandle = INVALID_HANDLE_VALUE;
  1444. +   }
  1445. +
  1446.     LeaveCriticalSection(&lock);
  1447.     DeleteCriticalSection(&lock);
  1448.  }
  1449.  -150,7 +184,7 @@
  1450.         JS_CallFunctionValue(GetContext(), globalObject, main, 0, NULL, &dummy);
  1451.     }
  1452.  
  1453. -   if(GetState() == Command)
  1454. +   if(GetScriptType() == Command)
  1455.     {
  1456.         // if we just processed a command, print the results of the command
  1457.         if(!JSVAL_IS_NULL(dummy) && !JSVAL_IS_VOID(dummy))
  1458.  -202,15 +236,13 @@
  1459.  
  1460.     EnterCriticalSection(&lock);
  1461.  
  1462. +   if(context && JS_IsRunning(context))
  1463. +       JS_TriggerOperationCallback(context);
  1464. +
  1465.     // tell everyone else that the script is aborted FIRST
  1466.     isAborted = true;
  1467.     isPaused = false;
  1468.     isReallyPaused = false;
  1469. -   if(GetState() != Command)
  1470. -   {
  1471. -       const char* displayName = fileName.c_str() + strlen(Vars.szScriptPath) + 1;
  1472. -       Print("Script %s ended", displayName);
  1473. -   }
  1474.  
  1475.     ClearAllEvents();
  1476.     Genhook::Clean(this);
  1477.  -225,21 +257,28 @@
  1478.         Sleep(10);
  1479.     }
  1480.  
  1481. -   if(threadHandle)
  1482. +   if(threadHandle != INVALID_HANDLE_VALUE)
  1483. +   {
  1484.         CloseHandle(threadHandle);
  1485. -   threadHandle = NULL;
  1486. +       threadHandle = INVALID_HANDLE_VALUE;
  1487. +   }
  1488. +
  1489. +   if(GetScriptType() != Command)
  1490. +       Print("Script %s ended", fileName.c_str() + strlen(Vars.szScriptPath) + 1);
  1491. +
  1492.     LeaveCriticalSection(&lock);
  1493.  }
  1494.  
  1495.  bool Script::IsIncluded(const char* file)
  1496.  {
  1497. -   uint count = 0;
  1498.     char* fname = _strdup(file);
  1499.     if(!fname)
  1500.         return false;
  1501.  
  1502.     _strlwr_s(fname, strlen(fname)+1);
  1503.     StringReplace(fname, '/', '\\', strlen(fname));
  1504. +
  1505. +   uint count = 0;
  1506.     count = includes.count(string(fname));
  1507.     free(fname);
  1508.  
  1509.  -256,9 +295,8 @@
  1510.     _strlwr_s(fname, strlen(fname)+1);
  1511.     StringReplace(fname, '/', '\\', strlen(fname));
  1512.     // ignore already included, 'in-progress' includes, and self-inclusion
  1513. -   if(!!includes.count(string(fname)) ||
  1514. -      !!inProgress.count(string(fname)) ||
  1515. -       (fileName == string(fname)))
  1516. +   string fName(fname);
  1517. +   if(!!includes.count(fName) || !!inProgress.count(fName) || fileName == fName)
  1518.     {
  1519.         LeaveCriticalSection(&lock);
  1520.         free(fname);
  1521.  -378,7 +416,7 @@
  1522.  void Script::ExecEvent(const char* evtName, const char* format, uintN argc, void* argv)
  1523.  {
  1524.     if((!IsAborted() && !IsPaused() && functions.count(evtName)) &&
  1525. -       IsRunning() && !(GetState() == InGame && ClientState() != ClientStateInGame))
  1526. +       IsRunning() && !(GetScriptType() == InGame && ClientState() != ClientStateInGame))
  1527.     {
  1528.         FunctionList funcs = functions[evtName];
  1529.         JSContext* cx = JS_NewContext(ScriptEngine::GetRuntime(), 8192);
  1530.  -403,7 +441,7 @@
  1531.  
  1532.  void Script::ExecEventAsync(const char* evtName, const char* format, ArgList* args)
  1533.  {
  1534. -   if((!IsAborted() && !IsPaused() && functions.count(evtName)))
  1535. +   if(!IsAborted() && !IsPaused() && functions.count(evtName))
  1536.     {
  1537.         Event* evt = new Event;
  1538.         evt->owner = this;
  1539.  -438,7 +476,7 @@
  1540.     if(!evt)
  1541.         return;
  1542.  
  1543. -   if(evt->owner->IsRunning() && !(evt->owner->GetState() == InGame && ClientState() != ClientStateInGame))
  1544. +   if(evt->owner->IsRunning() && !(evt->owner->GetScriptType() == InGame && ClientState() != ClientStateInGame))
  1545.     {
  1546.         JSContext* cx = JS_NewContext(ScriptEngine::GetRuntime(), 8192);
  1547.         JS_SetContextPrivate(cx, evt->owner);
  1548.  -452,12 +490,18 @@
  1549.         {
  1550.             switch(it->second)
  1551.             {
  1552. -               case String: argv[i] = STRING_TO_JSVAL((JSString*)it->first); break;
  1553. +               case String:
  1554. +                   argv[i] = STRING_TO_JSVAL((JSString*)it->first);
  1555. +                   break;
  1556.                 case SignedInt: case UnsignedInt: case Double: case UnsignedShort:
  1557.                     JS_NewNumberValue(cx, (jsdouble)it->first, &argv[i]);
  1558.                     break;
  1559. -               case Boolean: argv[i] = BOOLEAN_TO_JSVAL((JSBool)it->first); break;
  1560. -               case JSVal: argv[i] = (jsval)it->first; break;
  1561. +               case Boolean:
  1562. +                   argv[i] = BOOLEAN_TO_JSVAL((JSBool)it->first);
  1563. +                   break;
  1564. +               case JSVal:
  1565. +                   argv[i] = (jsval)it->first;
  1566. +                   break;
  1567.             }
  1568.         }
  1569.  
  1570.  -476,8 +520,12 @@
  1571.         {
  1572.             switch(it->second)
  1573.             {
  1574. -               case JSVal: JS_RemoveRoot((jsval*)&(it->first)); break;
  1575. -               case String: JS_RemoveRoot((JSString*)&(it->first)); break;
  1576. +               case JSVal:
  1577. +                   JS_RemoveRoot((jsval*)&(it->first));
  1578. +                   break;
  1579. +               case String:
  1580. +                   JS_RemoveRoot((JSString*)&(it->first));
  1581. +                   break;
  1582.             }
  1583.         }
  1584.  
  1585. Index: Script.h
  1586. ===================================================================
  1587. --- Script.h    (revision 1006)
  1588. +++ Script.h    (working copy)
  1589.  -10,7 +10,7 @@
  1590.  #include "AutoRoot.h"
  1591.  #include "JSUnit.h"
  1592.  
  1593. -enum ScriptState {
  1594. +enum ScriptType {
  1595.     InGame,
  1596.     OutOfGame,
  1597.     Command
  1598.  -34,8 +34,8 @@
  1599.  static JSClass global_obj = {
  1600.     "global", JSCLASS_GLOBAL_FLAGS,
  1601.     JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
  1602. -    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
  1603. -    JSCLASS_NO_OPTIONAL_MEMBERS
  1604. +   JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
  1605. +   JSCLASS_NO_OPTIONAL_MEMBERS
  1606.  };
  1607.  
  1608.  class Script;
  1609.  -60,12 +60,13 @@
  1610.  private:
  1611.     std::string fileName;
  1612.     int execCount;
  1613. -   ScriptState scriptState;
  1614. +   ScriptType scriptType;
  1615.     JSContext* context;
  1616.     JSScript* script;
  1617.     myUnit* me;
  1618.  
  1619.     JSObject *globalObject, *scriptObject;
  1620. +
  1621.     bool isLocked, isPaused, isReallyPaused, isAborted;
  1622.  
  1623.     IncludeList includes, inProgress;
  1624.  -74,7 +75,7 @@
  1625.     DWORD threadId;
  1626.     CRITICAL_SECTION lock;
  1627.  
  1628. -   Script(const char* file, ScriptState state);
  1629. +   Script(const char* file, ScriptType type);
  1630.     Script(const Script&);
  1631.     Script& operator=(const Script&);
  1632.     ~Script(void);
  1633.  -94,7 +95,7 @@
  1634.     JSContext* GetContext(void) { return context; }
  1635.     JSObject* GetGlobalObject(void) { return globalObject; }
  1636.     JSObject* GetScriptObject(void) { return scriptObject; }
  1637. -   ScriptState GetState(void) { return scriptState; }
  1638. +   ScriptType GetScriptType(void) { return scriptType; }
  1639.     int GetExecutionCount(void);
  1640.     DWORD GetThreadId(void);
  1641.     // UGLY HACK to fix up the player gid on game join for cached scripts/oog scripts
  1642. Index: ScriptEngine.cpp
  1643. ===================================================================
  1644. --- ScriptEngine.cpp    (revision 1005)
  1645. +++ ScriptEngine.cpp    (working copy)
  1646.  -27,7 +27,7 @@
  1647.  bool __fastcall StopScript(Script* script, void* argv, uint argc);
  1648.  bool __fastcall GCPauseScript(Script* script, void* argv, uint argc);
  1649.  
  1650. -Script* ScriptEngine::CompileFile(const char* file, ScriptState state, bool recompile)
  1651. +Script* ScriptEngine::CompileFile(const char* file, ScriptType scriptType, bool recompile)
  1652.  {
  1653.     if(GetState() != Running)
  1654.         return NULL;
  1655.  -52,7 +52,7 @@
  1656.                 return ret;
  1657.             }
  1658.         }
  1659. -       Script* script = new Script(fileName, state);
  1660. +       Script* script = new Script(fileName, scriptType);
  1661.         scripts[fileName] = script;
  1662.         LeaveCriticalSection(&lock);
  1663.         delete[] fileName;
  1664.  -354,7 +354,7 @@
  1665.  
  1666.  bool __fastcall StopIngameScript(Script* script, void*, uint)
  1667.  {
  1668. -   if(script->GetState() == InGame)
  1669. +   if(script->GetScriptType() == InGame)
  1670.         script->Stop(true);
  1671.     return true;
  1672.  }
  1673.  -390,12 +390,14 @@
  1674.     bool pause = script->IsPaused();
  1675.  
  1676.     if(pause)
  1677. +   {
  1678.         script->SetPauseState(true);
  1679. +       JS_MaybeGC(cx);
  1680. +   }
  1681. +
  1682.     while(script->IsPaused())
  1683. -   {
  1684.         Sleep(50);
  1685. -       JS_MaybeGC(cx);
  1686. -   }
  1687. +
  1688.     if(pause)
  1689.         script->SetPauseState(false);
  1690.  
  1691.  -403,7 +405,7 @@
  1692.  
  1693.     script->UpdatePlayerGid();
  1694.  
  1695. -   return !!!(JSBool)(script->IsAborted() || ((script->GetState() != OutOfGame) && !D2CLIENT_GetPlayerUnit()));
  1696. +   return !!!(JSBool)(script->IsAborted() || (script->GetScriptType() != OutOfGame && ClientState() != ClientStateInGame));
  1697.  }
  1698.  
  1699.  JSBool contextCallback(JSContext* cx, uintN contextOp)
  1700.  -486,14 +488,19 @@
  1701.  {
  1702.     while(ScriptEngine::GetState() != Stopped)
  1703.     {
  1704. -       EventHelper* helper = (EventHelper*)InterlockedPopEntrySList(&ScriptEngine::eventList);
  1705. -       if(helper)
  1706. +       while(QueryDepthSList(&ScriptEngine::eventList) > 0)
  1707.         {
  1708. -           // execute it on every script
  1709. -           ScriptEngine::ForEachScript(ExecEventOnScript, helper, 1);
  1710. -           delete helper;
  1711. +           EventHelper* helper = (EventHelper*)InterlockedPopEntrySList(&ScriptEngine::eventList);
  1712. +           if(helper)
  1713. +           {
  1714. +               // execute it on every script
  1715. +               ScriptEngine::ForEachScript(ExecEventOnScript, helper, 1);
  1716. +               delete helper;
  1717. +           }
  1718. +           else
  1719. +               DebugBreak();
  1720.         }
  1721. -       Sleep(10);
  1722. +       Sleep(1);
  1723.     }
  1724.  }
  1725.  
  1726. Index: ScriptEngine.h
  1727. ===================================================================
  1728. --- ScriptEngine.h  (revision 1003)
  1729. +++ ScriptEngine.h  (working copy)
  1730.  -49,7 +49,7 @@
  1731.  
  1732.     static void FlushCache(void);
  1733.  
  1734. -   static Script* CompileFile(const char* file, ScriptState state, bool recompile = false);
  1735. +   static Script* CompileFile(const char* file, ScriptType scriptType, bool recompile = false);
  1736.     static Script* CompileCommand(const char* command);
  1737.     static void DisposeScript(Script* script);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement