Skip to content

Commit

Permalink
fix: fallout 4 crash fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed Nov 20, 2021
1 parent 8842773 commit 1e2bd32
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Code/client/Games/Fallout4/TESObjectREFR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ static TRemoveInventoryItem* RealRemoveInventoryItem = nullptr;

void TESObjectREFR::SaveInventory(BGSSaveFormBuffer* apBuffer) const noexcept
{
TP_ASSERT(inventory, "SaveInventory is called, but inventory is null!");

TP_THIS_FUNCTION(TSaveFunc, void, void, BGSSaveFormBuffer*);

POINTER_FALLOUT4(TSaveFunc, s_save, 0x1401ACB20 - 0x140000000);
Expand Down
13 changes: 12 additions & 1 deletion Code/client/Games/Skyrim/TESObjectREFR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,21 @@ void* TP_MAKE_THISCALL(HookAddInventoryItem, TESObjectREFR, TESBoundObject* apIt
return ThisCall(RealAddInventoryItem, apThis, apItem, apExtraData, aCount, apOldOwner);
}

// TODO: here's your deadlock/memory leak, fix that
void* TP_MAKE_THISCALL(HookRemoveInventoryItem, TESObjectREFR, float* apUnk0, TESBoundObject* apItem, uint32_t aCount, uint32_t aUnk1, BSExtraDataList* apExtraData, TESObjectREFR* apNewOwner, NiPoint3* apUnk2, NiPoint3* apUnk3)
{
static uint32_t count = 0;
count++;
if (count > 1)
spdlog::error("\tRecursive RemoveInventoryItem!");

World::Get().GetRunner().Trigger(InventoryChangeEvent(apThis->formID));
return ThisCall(RealRemoveInventoryItem, apThis, apUnk0, apItem, aCount, aUnk1, apExtraData, apNewOwner, apUnk2, apUnk3);

auto result = ThisCall(RealRemoveInventoryItem, apThis, apUnk0, apItem, aCount, aUnk1, apExtraData, apNewOwner, apUnk2, apUnk3);

count--;

return result;
}

static TiltedPhoques::Initializer s_objectReferencesHooks([]() {
Expand Down
2 changes: 1 addition & 1 deletion Code/client/Services/Generic/InventoryService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void InventoryService::RunObjectInventoryUpdates() noexcept
{
const auto* pObject = RTTI_CAST(TESForm::GetById(objectId), TESForm, TESObjectREFR);

if (!pObject)
if (!pObject || !pObject->inventory)
continue;

ObjectData objectData;
Expand Down
15 changes: 6 additions & 9 deletions Code/client/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
#define POINTER_FALLOUT4(className, variableName, ...) ;
#endif

#ifdef DEBUG
#define TP_ASSERT(Expr, Msg, ...) \
if (!(Expr)) \
{ \
utils::Assert(#Expr, fmt::format(Msg, __VA_ARGS__).c_str()); \
}
#else
#define TP_ASSERT(Expr, Msg, ...) ;
#endif
// TODO: should this be debug only? I removed the check since debug is broken, can only use releasedbg
#define TP_ASSERT(Expr, Msg, ...) \
if (!(Expr)) \
{ \
utils::Assert(#Expr, fmt::format(Msg, __VA_ARGS__).c_str()); \
}

namespace utils
{
Expand Down

0 comments on commit 1e2bd32

Please sign in to comment.