Skip to content

Commit

Permalink
Merge pull request #9 from Silver-Ore-Team/fix/unsafe-code
Browse files Browse the repository at this point in the history
Fix potentially unsafe code, improve logging, and update CMake to support dynamic project versioning
  • Loading branch information
muczc1wek authored Oct 5, 2024
2 parents 294a8a0 + 379b849 commit a0ac288
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.25)
project(zMultilogue VERSION 0.0.1 LANGUAGES CXX)
set(PROJECT_VERSION_CMAKE "0.0.3")
project(zMultilogue VERSION "${PROJECT_VERSION_CMAKE}" LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
31 changes: 20 additions & 11 deletions src/Gothic/zMul_Helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,28 @@ namespace GOTHIC_NAMESPACE

void AI_WaitTillEnd(oCNpc* self, oCNpc* other)
{
if (self && other) {
oCMsgConversation* msg = zNEW(oCMsgConversation)(oCMsgConversation::EV_WAITTILLEND,other);
int nr = other -> GetEM()->GetNumMessages();
zCEventMessage* watch = nullptr;
for (int i = nr - 1; i >= 0; i--) {
watch = other->GetEM()->GetEventMessage(i);
if (!watch->IsOverlay()) {
msg->watchMsg = watch;
break;
}
static NH::Logger* log = NH::CreateLogger("AI_WaitTillEnd");
if (!self || !other) {
log->Warning("Invalid NPC instances.");
return;
}
if(self->idx == other->idx) {
//Imo this is not an error, no need to log it
//log->Debug("Self and other are the same.");
return;
}
log->Trace("Synching {0} with {1}", self->idx, other->idx);
oCMsgConversation* msg = zNEW(oCMsgConversation)(oCMsgConversation::EV_WAITTILLEND,other);
int nr = other -> GetEM()->GetNumMessages();
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
40 changes: 21 additions & 19 deletions src/Gothic/zMultilogue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace GOTHIC_NAMESPACE
npc->state.StartAIState("ZS_MULTILOGUE", 0, 0, 0, 0);
parser->SetInstance("SELF", self);
m_Npcs[npc->idx] = npc;
log->Info("NPC with ID {0} added.", npc->idx);
}

inline void zCMultilogue::Start()
Expand Down Expand Up @@ -117,22 +118,27 @@ namespace GOTHIC_NAMESPACE
return;
}
m_Running = false;
m_Npcs.clear();
m_LastSelf = nullptr;
m_CameraAdapter.SetTarget(nullptr);
log->Info("Finishing multilogue with {0} NPCs.", m_Npcs.size());
m_Npcs.clear();
}


inline void zCMultilogue::ListNpcs() {
static NH::Logger* log = NH::CreateLogger("zCMultilogue::ListNpcs");
log->Trace("Listing NPCs:");
static NH::Logger* log = NH::CreateLogger("zCMultilogue::ListNpcs");
std::string npcList = "Current NPCs: ";
for (auto& [key, value] : m_Npcs) {
log->Trace("NPC ID: {0}", key);
npcList += std::to_string(key) + ", ";
}
if (!m_Npcs.empty()) {
npcList.pop_back(); // Remove the last space
npcList.pop_back(); // Remove the last comma
}
log->Info(npcList.c_str());
}

inline void zCMultilogue::Wait(oCNpc* npc)
{
inline void zCMultilogue::Wait(oCNpc* npc) {
static NH::Logger* log = NH::CreateLogger("zCMultilogue::Wait");
if (!npc) {
log->Warning("Invalid NPC.");
Expand All @@ -150,17 +156,13 @@ namespace GOTHIC_NAMESPACE
AI_WaitTillEnd(npc, player);

//Sync all Npcs invited to trialogue
oCNpc* lastNpc;
oCNpc* nextNpc;

lastNpc = m_Npcs[0];
oCNpc* lastNpc = std::prev(m_Npcs.end())->second;

for (auto & item : m_Npcs) {
nextNpc = item.second;
if (lastNpc && nextNpc) {
AI_WaitTillEnd(nextNpc, lastNpc);
lastNpc = nextNpc;
for (auto & [id, currentNpc]: m_Npcs) {
if (lastNpc && currentNpc) {
AI_WaitTillEnd(npc, currentNpc);
}
lastNpc = currentNpc;
}
}

Expand Down Expand Up @@ -189,13 +191,13 @@ namespace GOTHIC_NAMESPACE
}

inline void zCMultilogue::EV_Next(int id) {
oCNpc* npc = m_Npcs[id];
if (npc) {
auto item = m_Npcs.find(id);
if (item->second) {
static NH::Logger* log = NH::CreateLogger("zCMultilogue::EV_Next");
log->Info("Next NPC: {0}", id);
npc->talkOther = nullptr;
item->second->talkOther = nullptr;
// Currently does nothing
m_CameraAdapter.SetTarget(npc);
m_CameraAdapter.SetTarget(item->second);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NH/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ namespace NH
template<class T>
T* GetAdapter() const
{
for (int i = 0; i < m_Adapters.GetCount(); i++)
for (size_t i = 0; i < m_Adapters.GetCount(); i++)
{
ILoggerAdapter* adapter = m_Adapters[i];
T* ptr = dynamic_cast<T*>(adapter);
Expand Down

0 comments on commit a0ac288

Please sign in to comment.