Skip to content

Commit

Permalink
Merge pull request #9 from nooperation/dehackit_debug
Browse files Browse the repository at this point in the history
Fixed crash caused by the Flee module
  • Loading branch information
nooperation authored Jul 22, 2023
2 parents 1f5b712 + be58334 commit 475c4b6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
35 changes: 24 additions & 11 deletions D2Hackit/Core/Server20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ BOOL EXPORT FindUnitByClassID(DWORD dwUnitClassID, DWORD dwUnitType, LPGAMEUNIT

::memset(lpBuffer, 0, sizeof(GAMEUNIT));

Room* pRoom = D2COMMON_GetRoomFromUnit(D2CLIENT_GetPlayerUnit());
const auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
Room* pRoom = D2COMMON_GetRoomFromUnit(currentPlayerUnit);
if (!pRoom)
return FALSE; // not in a game?

Expand Down Expand Up @@ -406,7 +407,8 @@ BOOL EXPORT FindUnitByName(LPCSTR lpszUnitName, DWORD dwUnitType, LPGAMEUNIT lpB
wchar_t wszName[256] = {0};
ConvertToUnicode(lpszUnitName, wszName, 255);

Room* pRoom = D2COMMON_GetRoomFromUnit(D2CLIENT_GetPlayerUnit());
const auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
Room* pRoom = D2COMMON_GetRoomFromUnit(currentPlayerUnit);
if (!pRoom)
return FALSE; // not in a game?

Expand Down Expand Up @@ -702,7 +704,8 @@ BOOL EXPORT GetMyMercUnit(LPGAMEUNIT lpBuffer) // Get player's mercenary
if (PID == 0)
return FALSE;

Room* pRoom = D2COMMON_GetRoomFromUnit(D2CLIENT_GetPlayerUnit());
const auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
Room* pRoom = D2COMMON_GetRoomFromUnit(currentPlayerUnit);
if (!pRoom)
return FALSE; // not in a game?

Expand Down Expand Up @@ -751,7 +754,8 @@ BOOL EXPORT EnumUnits(DWORD dwUnitType, fnEnumUnitProc lpfnEnumUnitProc, LPARAM
if (lpfnEnumUnitProc == NULL)
return FALSE;

Room* pRoom = D2COMMON_GetRoomFromUnit(D2CLIENT_GetPlayerUnit());
const auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
Room* pRoom = D2COMMON_GetRoomFromUnit(currentPlayerUnit);
if (!pRoom)
return FALSE; // not in a game?

Expand Down Expand Up @@ -894,7 +898,8 @@ int EXPORT GetMyCorpseCount()
wchar_t wszName[256] = {0};
ConvertToUnicode(GetSafePlayerName(), wszName, 255);

Room* pRoom = D2COMMON_GetRoomFromUnit(D2CLIENT_GetPlayerUnit());
const auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
Room* pRoom = D2COMMON_GetRoomFromUnit(currentPlayerUnit);
if (!pRoom)
return 0; // not in a game?

Expand Down Expand Up @@ -927,7 +932,8 @@ BOOL EXPORT PickMyCorpse()
wchar_t wszName[256] = {0};
ConvertToUnicode(GetSafePlayerName(), wszName, 255);

Room* pRoom = D2COMMON_GetRoomFromUnit(D2CLIENT_GetPlayerUnit());
const auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
Room* pRoom = D2COMMON_GetRoomFromUnit(currentPlayerUnit);
if (!pRoom)
return 0; // not in a game?

Expand Down Expand Up @@ -1355,7 +1361,8 @@ void GetRoomsRecursive(RoomOther *ro, CArrayEx<DWORD, DWORD>& aChecked, std::vec
if(!ro->pRoom)
{
add_room=TRUE;
D2COMMON_AddRoomData(D2CLIENT_GetPlayerUnit()->ptAct, GetCurrentMapID(), ro->xPos, ro->yPos, D2CLIENT_GetPlayerUnit());
auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
D2COMMON_AddRoomData(currentPlayerUnit->ptAct, GetCurrentMapID(), ro->xPos, ro->yPos, currentPlayerUnit);
}

aChecked.Add((DWORD)ro);
Expand All @@ -1373,7 +1380,8 @@ void GetRoomsRecursive(RoomOther *ro, CArrayEx<DWORD, DWORD>& aChecked, std::vec
}
if(add_room)
{
D2COMMON_RemoveRoomData(D2CLIENT_GetPlayerUnit()->ptAct, GetCurrentMapID(), ro->xPos, ro->yPos, D2CLIENT_GetPlayerUnit());
auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
D2COMMON_RemoveRoomData(currentPlayerUnit->ptAct, GetCurrentMapID(), ro->xPos, ro->yPos, currentPlayerUnit);
}
}

Expand Down Expand Up @@ -1415,7 +1423,9 @@ BOOL RevealPresetUnits(CArrayEx<PRESETUNIT, const PRESETUNIT&>& aUnits)

void FindPresetUnitsFromRoom2(RoomOther *ro, CArrayEx<DWORD, DWORD>& aChecked, CArrayEx<PRESETUNIT, const PRESETUNIT&>& aUnits)
{
if(ro->ptDrlgLevel->LevelNo!=GetCurrentMapID())
const auto currentMapId = GetCurrentMapID();

if(ro->ptDrlgLevel->LevelNo != currentMapId)
return;

if (aChecked.Find((DWORD)ro) != -1)
Expand All @@ -1425,7 +1435,9 @@ void FindPresetUnitsFromRoom2(RoomOther *ro, CArrayEx<DWORD, DWORD>& aChecked, C
if(!ro->pRoom)
{
add_room=TRUE;
D2COMMON_AddRoomData(D2CLIENT_GetPlayerUnit()->ptAct, GetCurrentMapID(), ro->xPos, ro->yPos, D2CLIENT_GetPlayerUnit());
const auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();

D2COMMON_AddRoomData(currentPlayerUnit->ptAct, currentMapId, ro->xPos, ro->yPos, currentPlayerUnit);
}

aChecked.Add((DWORD)ro);
Expand Down Expand Up @@ -1453,7 +1465,8 @@ void FindPresetUnitsFromRoom2(RoomOther *ro, CArrayEx<DWORD, DWORD>& aChecked, C
}
if(add_room)
{
D2COMMON_RemoveRoomData(D2CLIENT_GetPlayerUnit()->ptAct, GetCurrentMapID(), ro->xPos, ro->yPos, D2CLIENT_GetPlayerUnit());
auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
D2COMMON_RemoveRoomData(currentPlayerUnit->ptAct, GetCurrentMapID(), ro->xPos, ro->yPos, currentPlayerUnit);
}
}

Expand Down
15 changes: 12 additions & 3 deletions D2Hackit/Core/me.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,14 @@ BYTE EXPORT GetCurrentMapID()
if (!pRoom)
return MAP_UNKNOWN;

return D2COMMON_GetLevelIdFromRoom(pRoom);
if (!pRoom->ptRoomOther) {
return MAP_UNKNOWN;
}
if (!pRoom->ptRoomOther->ptDrlgLevel) {
return MAP_UNKNOWN;
}

return pRoom->ptRoomOther->ptDrlgLevel->LevelNo;
}

BYTE EXPORT GetBeltRows() // Row number of the player's belt(1-4)
Expand Down Expand Up @@ -2064,7 +2071,8 @@ void EXPORT CloseAllUIs()

DWORD EXPORT GetStashGoldLimit() // stash gold limit
{
return D2COMMON_GetStashGoldLimit(D2CLIENT_GetPlayerUnit());
const auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
return D2COMMON_GetStashGoldLimit(currentPlayerUnit);
}

DWORD EXPORT GetInventoryGoldLimit() // inventory gold limit
Expand Down Expand Up @@ -2097,7 +2105,8 @@ BOOL EXPORT GetAlwaysRun() // is "always run" on?

DWORD EXPORT GetCursorItem() // returns ID of the item on cursor, if any
{
UnitAny* p = D2COMMON_GetCursorItem(D2CLIENT_GetPlayerUnit()->ptInventory);
const auto currentPlayerUnit = D2CLIENT_GetPlayerUnit();
UnitAny* p = D2COMMON_GetCursorItem(currentPlayerUnit->ptInventory);
return p ? p ->dwId : 0;
}

Expand Down
19 changes: 1 addition & 18 deletions D2Hackit/Modules/flee/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,24 +310,7 @@ VOID EXPORT OnThisPlayerMessage(UINT nMessage, WPARAM wParam, LPARAM lParam)
fleePortalRequested = false;
break;
case PM_LEAVETOWN:
if (!isSuppressingAutoTpWhenLeavingTown && createPortalWhenLeavingTown)
{
// Only create the portal if the waypoint is far enough away (we didn't just use a wp to leave town)
PRESETUNIT unit = {0};
if(server->EnumPresetUnits(enumPresetsForWaypoints, (LPARAM)&unit))
{
RequestTP(false);
break;
}

auto myPosition = me->GetPosition();
auto distanceToWaypoint = server->GetDistance(myPosition.x, myPosition.y, unit.x, unit.y);
if (distanceToWaypoint > 10)
{
RequestTP(false);
return;
}
}
// EnumPresetUnits can cause a crash so, enjoy your portals every time you leave town!
break;
}
}
Expand Down

0 comments on commit 475c4b6

Please sign in to comment.