Skip to content

Commit

Permalink
Fix memory unsafe zMultilogue lifecycle and formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
muczc1wek committed Sep 16, 2024
1 parent 71006ca commit e1457d9
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 194 deletions.
45 changes: 8 additions & 37 deletions src/Gothic/Externals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,32 @@ namespace GOTHIC_NAMESPACE {

int zMul_Invite() {
zCParser* par = zCParser::GetParser();
oCNpc* npc;
npc = (oCNpc*)par->GetInstance();

if (!zMultilogue) {
zMultilogue = new zCMultilogue();
}
zMultilogue->AddNpc(npc);
oCNpc* npc = reinterpret_cast<oCNpc*>(par->GetInstance());
zMultilogue.AddNpc(npc);
return 0;
};

int zMul_Start() {
static NH::Logger* log = NH::CreateLogger("Externals::zMul_Start");
if (!zMultilogue) {
log->Error("Multilogue not initialized.");
return 1;
}
zMultilogue->Start();
zMultilogue.Start();
return 0;
};

int zMul_Finish() {
static NH::Logger* log = NH::CreateLogger("Externals::zMul_Finish");
if (!zMultilogue) {
log->Error("Multilogue not initialized.");
return 1;
}
zMultilogue->Finish();
zMultilogue.Finish();
return 0;
};

int zMul_Next() {
zCParser* par = zCParser::GetParser();
oCNpc* npc;
npc = (oCNpc*)par->GetInstance();

static NH::Logger* log = NH::CreateLogger("Externals::zMul_Next");
if (!zMultilogue) {
log->Error("Multilogue not initialized.");
return 1;
}
zMultilogue->MakeSelf(npc);
oCNpc* npc = reinterpret_cast<oCNpc*>(par->GetInstance());
zMultilogue.MakeSelf(npc);
return 0;
};

int zMul_Wait() {
zCParser* par = zCParser::GetParser();
oCNpc* npc;
npc = (oCNpc*)par->GetInstance();

static NH::Logger* log = NH::CreateLogger("Externals::zMul_Wait");
if (!zMultilogue) {
log->Error("Multilogue not initialized.");
return 1;
}
zMultilogue->Wait(npc);
oCNpc* npc = reinterpret_cast<oCNpc*>(par->GetInstance());
zMultilogue.Wait(npc);
return 0;
};

Expand Down
11 changes: 5 additions & 6 deletions src/Gothic/Hooks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ namespace GOTHIC_NAMESPACE {
// G1A: 0x006715F0 private: void __thiscall oCGame::DefineExternals_Ulfi(class zCParser *)
// G2: 0x00677A00 private: void __thiscall oCGame::DefineExternals_Ulfi(class zCParser *)
// G2A: 0x006D4780 private: void __thiscall oCGame::DefineExternals_Ulfi(class zCParser *)
void* oCGame_DefineExternals_Ulfi = (void*)zSwitch(0x006495B0, 0x006715F0, 0x00677A00, 0x006D4780);
void* oCGame_DefineExternals_Ulfi = reinterpret_cast<void*>(zSwitch(0x006495B0, 0x006715F0, 0x00677A00, 0x006D4780));
void __fastcall DefineExternals_Ulfi_PartialHook(Union::Registers& reg)
{
Game_DefineExternals();
DefineExternals();
}
auto Partial_DefineExternals_Ulfi = Union::CreatePartialHook(oCGame_DefineExternals_Ulfi, &DefineExternals_Ulfi_PartialHook);

Expand All @@ -30,13 +30,12 @@ namespace GOTHIC_NAMESPACE {
int oCNpc::EV_Exchange_Hook( oCMsgManipulate* csg ) {
zSTRING slot = csg->slot;
if(slot) {
static NH::Logger* log = NH::CreateLogger("oCNpc::EV_Exchange_Hook");
if (slot.Upper() == "EV_FINISH") {
zMultilogue->EV_Finish();
zMultilogue.EV_Finish();
return TRUE;
}
else if (slot.Upper() == "EV_NEXT") {
zMultilogue->EV_Next(csg->flag);
if (slot.Upper() == "EV_NEXT") {
zMultilogue.EV_Next(csg->flag);
return TRUE;
}

Expand Down
8 changes: 0 additions & 8 deletions src/Gothic/Lifecycle.hpp

This file was deleted.

8 changes: 4 additions & 4 deletions src/Gothic/zMul_Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ namespace GOTHIC_NAMESPACE {
class zCMultilogueCameraAdapter
{
private:
oCNpc* target;
oCNpc* m_Target = nullptr;
public:
void SetTarget(oCNpc* npc);
oCNpc* GetTarget() { return target; }
oCNpc* GetTarget() const { return m_Target; }
};

void zCMultilogueCameraAdapter::SetTarget(oCNpc* npc)
inline void zCMultilogueCameraAdapter::SetTarget(oCNpc* npc)
{
target = npc;
m_Target = npc;
}
}
16 changes: 6 additions & 10 deletions src/Gothic/zMul_Helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ namespace GOTHIC_NAMESPACE
if (self) {
return self;
}
else {
log->Error("Failed to cast SELF to oCNpc.");
return nullptr;
}
}
else {
log->Error("Symbol SELF not found.");
log->Error("Failed to cast SELF to oCNpc.");
return nullptr;
}
log->Error("Symbol SELF not found.");
return nullptr;
}

bool Npc_IsInActiveVoblist(oCNpc* npc) {
Expand All @@ -37,15 +33,15 @@ namespace GOTHIC_NAMESPACE
if (self && other) {
oCMsgConversation* msg = zNEW(oCMsgConversation)(oCMsgConversation::EV_WAITTILLEND,other);
int nr = other -> GetEM()->GetNumMessages();
zCEventMessage* watch = NULL;
for (int i=nr-1; i>=0; i--) {
zCEventMessage* watch = nullptr;
for (int i = nr - 1; i >= 0; i--) {
watch = other->GetEM()->GetEventMessage(i);
if (!watch->IsOverlay()) {
msg->watchMsg = watch;
break;
}
}
self -> GetEM() -> OnMessage(msg ,self);
self->GetEM()->OnMessage(msg, self);
}
}

Expand Down
Loading

0 comments on commit e1457d9

Please sign in to comment.