Skip to content

[WIP] Plugin: Vault #1449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ find_package(Sanitizers)

execute_process(COMMAND git rev-parse --short HEAD OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE SHORT_HASH)
set(TARGET_NWN_BUILD 8193)
set(TARGET_NWN_BUILD_REVISION 36)
set(TARGET_NWN_BUILD_POSTFIX 12)
set(TARGET_NWN_BUILD_REVISION 37)
set(TARGET_NWN_BUILD_POSTFIX 5)
set(NWNX_BUILD_SHA ${SHORT_HASH})
set(PLUGIN_PREFIX NWNX_)

Expand Down
1 change: 0 additions & 1 deletion Compatibility/nwnx_time.nss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/// @brief Provides various time related functions
/// @{
/// @file nwnx_time.nss
#include "nwnx"
#include "nwnx_util"
#include "inc_sqlite_time"

Expand Down
110 changes: 55 additions & 55 deletions Core/NWNXCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,10 @@ void NWNXCore::ConfigureLogLevel(const std::string& plugin)

void NWNXCore::InitialSetupHooks()
{
m_vmSetVarHook = Hooks::HookFunction(&CNWSVirtualMachineCommands::ExecuteCommandSetVar, &SetVarHandler, Hooks::Order::Final);
m_vmGetVarHook = Hooks::HookFunction(&CNWSVirtualMachineCommands::ExecuteCommandGetVar, &GetVarHandler, Hooks::Order::Final);
m_vmTagEffectHook = Hooks::HookFunction(&CNWSVirtualMachineCommands::ExecuteCommandTagEffect, &TagEffectHandler, Hooks::Order::Final);
m_vmTagItemProperyHook = Hooks::HookFunction(&CNWSVirtualMachineCommands::ExecuteCommandTagItemProperty, &TagItemPropertyHandler, Hooks::Order::Final);
m_vmPlaySoundHook = Hooks::HookFunction(&CNWSVirtualMachineCommands::ExecuteCommandPlaySound, &PlaySoundHandler, Hooks::Order::Final);


m_destroyServerHook = Hooks::HookFunction(&CAppManager::DestroyServer, &DestroyServerHandler, Hooks::Order::Final);
m_mainLoopInternalHook = Hooks::HookFunction(&CServerExoAppInternal::MainLoop, &MainLoopInternalHandler, Hooks::Order::Final);
m_vmPlaySoundHook = Hooks::HookFunction(&CNWSVirtualMachineCommands::ExecuteCommandPlaySound, &PlaySoundHandler, Hooks::Order::Final);
m_nwnxFunctionManagementHook = Hooks::HookFunction(&CNWSVirtualMachineCommands::ExecuteCommandNWNXFunctionManagement, &NWNXFunctionManagementHandler, Hooks::Order::Final);
m_destroyServerHook = Hooks::HookFunction(&CAppManager::DestroyServer, &DestroyServerHandler, Hooks::Order::Final);
m_mainLoopInternalHook = Hooks::HookFunction(&CServerExoAppInternal::MainLoop, &MainLoopInternalHandler, Hooks::Order::Final);

POS::InitializeHooks();

Expand Down Expand Up @@ -367,69 +362,73 @@ void NWNXCore::InitialSetupPlugins()

void NWNXCore::InitialSetupResourceDirectories()
{
auto nwnxResDirPath = Config::Get<std::string>("NWNX_RESOURCE_DIRECTORY_PATH", Globals::ExoBase()->m_sUserDirectory.CStr() + std::string("/nwnx"));
auto nwnxResDirPriority = Config::Get<int32_t>("NWNX_RESOURCE_DIRECTORY_PRIORITY", 70000000);
static Hooks::Hook s_SetupDefaultSearchPathHook = Hooks::HookFunction(&CExoResMan::SetupDefaultSearchPath,
+[](CExoResMan *pThis) -> void
{
s_SetupDefaultSearchPathHook->CallOriginal<void>(pThis);

std::unordered_map<std::string, std::pair<std::string, int32_t>> resourceDirectories;
resourceDirectories.emplace("NWNX", std::make_pair(nwnxResDirPath, nwnxResDirPriority));
if (g_CoreShuttingDown)
return;

if (auto customResmanDefinition = Config::Get<std::string>("CUSTOM_RESMAN_DEFINITION"))
{
std::string crdPath = *customResmanDefinition;
FILE* file = std::fopen(crdPath.c_str(), "r");
auto nwnxResDirPath = Config::Get<std::string>("NWNX_RESOURCE_DIRECTORY_PATH", Globals::ExoBase()->m_sUserDirectory.CStr() + std::string("/nwnx"));
auto nwnxResDirPriority = Config::Get<int32_t>("NWNX_RESOURCE_DIRECTORY_PRIORITY", 70000000);

if (file)
{
LOG_INFO("Custom Resman Definition File: %s", crdPath);
std::unordered_map<std::string, std::pair<std::string, int32_t>> resourceDirectories;
resourceDirectories.emplace("NWNX", std::make_pair(nwnxResDirPath, nwnxResDirPriority));

char line[640];
char alias[64];
char path[512];
int32_t priority;
if (auto customResmanDefinition = Config::Get<std::string>("CUSTOM_RESMAN_DEFINITION"))
{
std::string crdPath = *customResmanDefinition;
FILE* file = std::fopen(crdPath.c_str(), "r");

while (std::fgets(line, 640, file))
if (file)
{
if (sscanf(line, "%s %s %i", alias, path, &priority) == 3)
{
resourceDirectories.try_emplace(alias, std::make_pair(path, priority));
}
else
LOG_INFO("Custom Resman Definition File: %s", crdPath);

char line[640];
char alias[64];
char path[512];
int32_t priority;

while (std::fgets(line, 640, file))
{
std::string errorLine = std::string(line);
LOG_WARNING("Invalid Custom Resman Definition Line: %s", String::Trim(errorLine));
if (sscanf(line, "%s %s %i", alias, path, &priority) == 3)
{
resourceDirectories.try_emplace(alias, std::make_pair(path, priority));
}
else
{
std::string errorLine = std::string(line);
LOG_WARNING("Invalid Custom Resman Definition Line: %s", String::Trim(errorLine));
}
}
}

std::fclose(file);
std::fclose(file);
}
else
LOG_ERROR("Failed to open Custom Resman Definition File: %s", crdPath);
}
else
LOG_ERROR("Failed to open Custom Resman Definition File: %s", crdPath);
}

Tasks::QueueOnMainThread([resourceDirectories]
for (const auto& resDir : resourceDirectories)
{
if (g_CoreShuttingDown)
return;
CExoString alias = CExoString(resDir.first + ":");
CExoString path = CExoString(resDir.second.first);

for (const auto& resDir : resourceDirectories)
if (Globals::ExoBase()->m_pcExoAliasList->GetAliasPath(alias).IsEmpty())
{
CExoString alias = CExoString(resDir.first + ":");
CExoString path = CExoString(resDir.second.first);

if (Globals::ExoBase()->m_pcExoAliasList->GetAliasPath(alias).IsEmpty())
{
LOG_INFO("Setting up Resource Directory: %s%s (Priority: %i)", alias, path, resDir.second.second);
LOG_INFO("Setting up Resource Directory: %s%s (Priority: %i)", alias, path, resDir.second.second);

g_core->m_CustomResourceDirectoryAliases.emplace_back(resDir.first);
g_core->m_CustomResourceDirectoryAliases.emplace_back(resDir.first);

Globals::ExoBase()->m_pcExoAliasList->Add(resDir.first, path);
Globals::ExoResMan()->CreateDirectory(alias);
Globals::ExoResMan()->AddResourceDirectory(alias, resDir.second.second, true);
}
else
LOG_WARNING("Resource Directory with alias '%s' already exists. Please use nwn.ini to redefine base game resource directories.", alias);
Globals::ExoBase()->m_pcExoAliasList->Add(resDir.first, path);
Globals::ExoResMan()->CreateDirectory(alias);
Globals::ExoResMan()->AddResourceDirectory(alias, resDir.second.second, true);
}
});
else
LOG_WARNING("Resource Directory with alias '%s' already exists. Please use nwn.ini to redefine base game resource directories.", alias);
}

}, Hooks::Order::Early);
}

void NWNXCore::InitialSetupCommands()
Expand Down Expand Up @@ -614,6 +613,7 @@ void NWNXCore::CreateServerHandler(CAppManager* app)

g_core->m_createServerHook.reset();
app->CreateServer();
MessageBus::Broadcast("NWNX_CORE_SIGNAL", { "ON_CREATE_SERVER" });
}

void NWNXCore::DestroyServerHandler(CAppManager* app)
Expand Down
16 changes: 4 additions & 12 deletions Core/NWNXCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,16 @@ class NWNXCore
NWNXCore();
~NWNXCore();

static int32_t GetVarHandler(CNWSVirtualMachineCommands*, int32_t, int32_t);
static int32_t SetVarHandler(CNWSVirtualMachineCommands*, int32_t, int32_t);
static int32_t TagEffectHandler(CNWSVirtualMachineCommands*, int32_t, int32_t);
static int32_t TagItemPropertyHandler(CNWSVirtualMachineCommands*, int32_t, int32_t);
static int32_t PlaySoundHandler(CNWSVirtualMachineCommands*, int32_t, int32_t);

std::unique_ptr<NWNXLib::Services::ServiceList> m_services;

const std::vector<std::string>& GetCustomResourceDirectoryAliases() const { return m_CustomResourceDirectoryAliases; }

NWNXLib::Hooks::Hook m_vmSetVarHook;
NWNXLib::Hooks::Hook m_vmGetVarHook;

private:
NWNXLib::Hooks::Hook m_createServerHook;
NWNXLib::Hooks::Hook m_vmTagEffectHook;
NWNXLib::Hooks::Hook m_vmTagItemProperyHook;
NWNXLib::Hooks::Hook m_vmPlaySoundHook;
NWNXLib::Hooks::Hook m_destroyServerHook;
NWNXLib::Hooks::Hook m_mainLoopInternalHook;
NWNXLib::Hooks::Hook m_vmPlaySoundHook;
NWNXLib::Hooks::Hook m_nwnxFunctionManagementHook;

std::unique_ptr<NWNXLib::Services::ProxyServiceList> m_coreServices;

Expand All @@ -57,6 +47,8 @@ class NWNXCore
static void CreateServerHandler(CAppManager*);
static void DestroyServerHandler(CAppManager*);
static int32_t MainLoopInternalHandler(CServerExoAppInternal*);
static int32_t PlaySoundHandler(CNWSVirtualMachineCommands*, int32_t, int32_t);
static int32_t NWNXFunctionManagementHandler(CNWSVirtualMachineCommands*, int32_t, int32_t);

int m_ScriptChunkRecursion;
std::vector<std::string> m_CustomResourceDirectoryAliases;
Expand Down
Loading