Skip to content

Commit

Permalink
Adding multi-loading, cachefix, sleep, from d2m to d2bs directly
Browse files Browse the repository at this point in the history
Rewrote some asm stubs so they are easier to understand for the future
  • Loading branch information
noah- committed Jun 24, 2017
1 parent 0ece7dc commit 11de04a
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 116 deletions.
6 changes: 5 additions & 1 deletion CommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ sLine CLine[] = {
{"-e0",0},
{"-e1",0},
{"-e2",0},
{"-title",0},
{"-mpq",0},
{"-profile", 0},
{"-handle", 0}
{"-handle", 0},
{"-multi", 0},
{"-sleepy", 0},
{"-cachefix",0}
};

DWORD ParseStringForText(LPSTR Source,LPSTR text)
Expand Down
4 changes: 2 additions & 2 deletions Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ BOOL OOG_SelectCharacter(char* szCharacter)
if(!szLine)
return FALSE;

char* cLine = strdup(szLine);
char* cCharacter = strdup(szCharacter);
char* cLine = _strdup(szLine);
char* cCharacter = _strdup(szCharacter);
StringToLower(cLine);
StringToLower(cCharacter);

Expand Down
126 changes: 80 additions & 46 deletions D2BS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Console.h"
#include "D2BS.h"
#include "D2Ptrs.h"
#include "CommandLine.h"

#ifdef _MSVC_DEBUG
#include "D2Loader.h"
Expand All @@ -21,54 +22,86 @@ static HANDLE hD2Thread = INVALID_HANDLE_VALUE;
static HANDLE hEventThread = INVALID_HANDLE_VALUE;
BOOL WINAPI DllMain(HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
{
switch(dwReason)
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
case DLL_PROCESS_ATTACH:
{
DisableThreadLibraryCalls(hDll);
if (lpReserved != NULL)
{
Vars.pModule = (Module*)lpReserved;

if (!Vars.pModule)
return FALSE;

strcpy_s(Vars.szPath, MAX_PATH, Vars.pModule->szPath);
Vars.bLoadedWithCGuard = TRUE;
}
else
{
DisableThreadLibraryCalls(hDll);
if(lpReserved != NULL)
{
Vars.pModule = (Module*)lpReserved;

if(!Vars.pModule)
return FALSE;

strcpy_s(Vars.szPath, MAX_PATH, Vars.pModule->szPath);
Vars.bLoadedWithCGuard = TRUE;
}
else
{
Vars.hModule = hDll;
GetModuleFileName(hDll, Vars.szPath, MAX_PATH);
PathRemoveFileSpec(Vars.szPath);
strcat_s(Vars.szPath, MAX_PATH, "\\");
Vars.bLoadedWithCGuard = FALSE;
}
Vars.hModule = hDll;
GetModuleFileName(hDll, Vars.szPath, MAX_PATH);
PathRemoveFileSpec(Vars.szPath);
strcat_s(Vars.szPath, MAX_PATH, "\\");
Vars.bLoadedWithCGuard = FALSE;
}

ParseCommandLine(GetCommandLineA());

sLine* command = GetCommand("-title");

if (command)
{
int len = strlen((char*)command->szText);
strncat_s(Vars.szTitle, (char*)command->szText, len);
}

command = GetCommand("-sleepy");

if (command)
{
Vars.bSleepy = 1;
}

command = GetCommand("-cachefix");

if (command)
{
Vars.bCacheFix = 1;
}


command = GetCommand("-multi");

if (command)
{
Vars.bMulti = TRUE;
}

#if 0
char errlog[516] = "";
sprintf_s(errlog, 516, "%sd2bs.log", Vars.szPath);
AllocConsole();
int handle = _open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
FILE* f = _fdopen(handle, "wt");
*stderr = *f;
setvbuf(stderr, NULL, _IONBF, 0);
freopen_s(&f, errlog, "a+t", f);
char errlog[516] = "";
sprintf_s(errlog, 516, "%sd2bs.log", Vars.szPath);
AllocConsole();
int handle = _open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
FILE* f = _fdopen(handle, "wt");
*stderr = *f;
setvbuf(stderr, NULL, _IONBF, 0);
freopen_s(&f, errlog, "a+t", f);
#endif

Vars.bShutdownFromDllMain = FALSE;
SetUnhandledExceptionFilter(ExceptionHandler);
if(!Startup())
return FALSE;
Vars.bShutdownFromDllMain = FALSE;
SetUnhandledExceptionFilter(ExceptionHandler);
if (!Startup())
return FALSE;
}
break;
case DLL_PROCESS_DETACH:
if (Vars.bNeedShutdown)
{
Vars.bShutdownFromDllMain = TRUE;
Shutdown();
}
break;
case DLL_PROCESS_DETACH:
if(Vars.bNeedShutdown)
{
Vars.bShutdownFromDllMain = TRUE;
Shutdown();
}
break;
}

return TRUE;
Expand All @@ -92,7 +125,7 @@ BOOL Startup(void)
InitializeCriticalSection(&Vars.cUnitListSection);
InitializeCriticalSection(&Vars.cFileSection);

Vars.bNeedShutdown = TRUE;
Vars.bNeedShutdown = TRUE;
Vars.bChangedAct = FALSE;
Vars.bGameLoopEntered = FALSE;

Expand All @@ -102,24 +135,25 @@ BOOL Startup(void)
Genhook::Initialize();
DefineOffsets();
InstallPatches();
InstallConditional();
CreateDdeServer();

if((hD2Thread = CreateThread(NULL, NULL, D2Thread, NULL, NULL, NULL)) == NULL)
if ((hD2Thread = CreateThread(NULL, NULL, D2Thread, NULL, NULL, NULL)) == NULL)
return FALSE;
// hEventThread = CreateThread(0, 0, EventThread, NULL, 0, 0);
// hEventThread = CreateThread(0, 0, EventThread, NULL, 0, 0);
return TRUE;
}

void Shutdown(void)
{
if(!Vars.bNeedShutdown)
if (!Vars.bNeedShutdown)
return;

Vars.bActive = FALSE;
if(!Vars.bShutdownFromDllMain)
if (!Vars.bShutdownFromDllMain)
WaitForSingleObject(hD2Thread, INFINITE);

SetWindowLong(D2GFX_GetHwnd(),GWL_WNDPROC,(LONG)Vars.oldWNDPROC);
SetWindowLong(D2GFX_GetHwnd(), GWL_WNDPROC, (LONG)Vars.oldWNDPROC);

RemovePatches();
Genhook::Destroy();
Expand Down
6 changes: 5 additions & 1 deletion D2BS.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#define XP_WIN

#define D2BS_VERSION "1.5.1857" //uptodate with d branch 1765 ff 20b
#define D2BS_VERSION "1.5.1862" //uptodate with d branch 1765 ff 20b

#include <windows.h>
#include <vector>
Expand Down Expand Up @@ -62,6 +62,9 @@ struct Variables
BOOL bForwardMessageBox;
BOOL bUseRawCDKey;
BOOL bQuitting;
BOOL bCacheFix;
BOOL bMulti;
BOOL bSleepy;
int dwMemUsage;
int dwConsoleFont;
HANDLE eventSignal;
Expand All @@ -76,6 +79,7 @@ struct Variables
char szDefault[_MAX_FNAME];
char szClassic[30];
char szLod[30];
char szTitle[128];

WNDPROC oldWNDPROC;
HHOOK hMouseHook;
Expand Down
7 changes: 1 addition & 6 deletions D2Handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ DWORD WINAPI D2Thread(LPVOID lpParam)
return FALSE;
}

ParseCommandLine(GetCommandLineA());
ParseCommandLine(GetCommandLineA());

command = GetCommand("-c0");

Expand Down Expand Up @@ -97,11 +97,6 @@ DWORD WINAPI D2Thread(LPVOID lpParam)
strncat_s(Vars.szLod, keys, len);
}

if (Vars.bUseRawCDKey == 1)
{
InstallConditional();
}

command = GetCommand("-handle");

if(command)
Expand Down
4 changes: 4 additions & 0 deletions D2Handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ void GameLeave(void);
void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
VOID __fastcall ClassicSTUB();
VOID __fastcall LodSTUB();
void FailToJoin();
HMODULE __stdcall Multi(LPSTR Class, LPSTR Window);
HANDLE __stdcall Windowname(DWORD dwExStyle, LPCSTR lpClassName, LPCSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
HANDLE __stdcall CacheFix(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
BOOL __fastcall RealmPacketRecv(BYTE* pPacket);
BOOL __fastcall ChatPacketRecv(BYTE* pPacket, int len);
LONG WINAPI MyUnhandledExceptionFilter(_In_ struct _EXCEPTION_POINTERS *ExceptionInfo);
Expand Down
44 changes: 10 additions & 34 deletions D2Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,50 +820,26 @@ void __declspec(naked) __fastcall D2CLIENT_ClickBelt(DWORD x, DWORD y, Inventory
}
}

void __declspec(naked) __stdcall D2CLIENT_LeftClickItem(UnitAny* pPlayer, Inventory* pInventory, int x, int y, DWORD dwClickType, InventoryLayout* pLayout, DWORD Location)
void __declspec(naked) __stdcall D2CLIENT_LeftClickItem(DWORD Location, UnitAny* pPlayer, Inventory* pInventory, int x, int y, DWORD dwClickType, InventoryLayout* pLayout)
{
__asm
{
/*mov eax, [esp + 0x18]
push eax
mov eax, [esp + 0x18]
push eax
mov eax, [esp + 0x18]
push eax
mov eax, [esp + 0x18]
push eax
mov eax, [esp + 0x18]
push eax
mov eax, [esp + 0x18]
push eax
mov eax, [esp + 0x18 + 0x18 + 4]*/
mov eax, [esp + 0x1C]
push eax
mov eax, [esp + 4] // return addr
mov [esp + 0x1C + 4], eax // store ret addr
pop eax
add esp, 4
call D2CLIENT_LeftClickItem_I
ret
pop ebx // pop return address
pop eax // pop Location to eax (optimized convention?)
push ebx // push return address
jmp D2CLIENT_LeftClickItem_I
}
}

void __declspec(naked) __fastcall D2CLIENT_ClickItemRight_ASM(DWORD x, DWORD y, DWORD Location, DWORD Player, DWORD pUnitInventory)
{
__asm
{
// ECX = y, EDX = x - Blizzard is weird :D
MOV EAX, ECX
MOV ECX, EDX
MOV EDX, EAX

MOV EAX, [ESP + 4] // location
PUSH EAX // save
MOV EAX, [ESP + 4] // ret addr
MOV [ESP + 8], EAX // overwrite location with ret addr
POP EAX
ADD ESP, 4 // pop ret addr copy
jmp D2CLIENT_ClickItemRight_I
xchg edx, ecx // x, y -> y, x
pop ebx // pop return address
pop eax // pop Location to eax (optimized convention?)
push ebx // push return address
jmp D2CLIENT_ClickItemRight_I
}
}

Expand Down
2 changes: 1 addition & 1 deletion D2Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ DWORD __cdecl D2CLIENT_GetMinionCount(UnitAny* pUnit, DWORD dwType);
void __fastcall D2CLIENT_HostilePartyUnit(RosterUnit* pUnit, DWORD dwButton);
void __stdcall D2CLIENT_TakeWaypoint(DWORD dwWaypointId, DWORD dwArea);
DWORD __fastcall D2CLIENT_SendGamePacket_ASM(DWORD dwLen, BYTE* bPacket);
void __stdcall D2CLIENT_LeftClickItem(UnitAny* pPlayer, Inventory* pInventory, int x, int y, DWORD dwClickType, InventoryLayout* pLayout, DWORD Location);
void __stdcall D2CLIENT_LeftClickItem(DWORD Location, UnitAny* pPlayer, Inventory* pInventory, int x, int y, DWORD dwClickType, InventoryLayout* pLayout);

double GetDistance(long x1, long y1, long x2, long y2, DistanceType type = Euclidean);
bool IsScrollingText();
Expand Down
Loading

0 comments on commit 11de04a

Please sign in to comment.