Skip to content
Merged
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
16 changes: 12 additions & 4 deletions src/common/branding.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include <cstdio>
#include "version.h"

#include "config.h"
namespace branding
{
enum Game
Expand Down Expand Up @@ -52,7 +50,17 @@ inline const char *GetBrandingString(Game game, Mode mode)
break;
}

_snprintf_s(brandingBuffer, sizeof(brandingBuffer), "CoDxe - %s %s " __DATE__ " " __TIME__, gameName, modeName);
if (Config::active_mod.empty())
{
_snprintf_s(brandingBuffer, sizeof(brandingBuffer), "CoDxe - %s %s\nBuild: " __DATE__ " " __TIME__, gameName,
modeName);
}
else
{
_snprintf_s(brandingBuffer, sizeof(brandingBuffer),
"CoDxe - %s %s\nBuild: " __DATE__ " " __TIME__ "\nActive mod: %s", gameName, modeName,
Config::active_mod.c_str());
}

return brandingBuffer;
}
Expand Down
64 changes: 50 additions & 14 deletions src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const char *CONFIG_PATH = "game:\\_codxe\\codxe.json";
const char *MOD_DIR = "game:\\_codxe\\mods";
const char *DUMP_DIR = "game:\\_codxe\\dump";

// Default values
std::string Config::active_mod = "";
bool Config::dump_rawfile = false;
bool Config::dump_map_ents = false;

bool DirectoryExists(const char *path)
{
DWORD attrs = GetFileAttributesA(path);
Expand Down Expand Up @@ -37,15 +42,26 @@ bool ReadFileToString(const char *path, std::string &outString)
return file.read(&outString[0], size).good();
}

void DbgPrintConfig(const Config &config)
Config::Config()
{
DbgPrint("[codxe][Config] Loading configuration from: %s\n", CONFIG_PATH);

if (!LoadFromFile(CONFIG_PATH))
{
DbgPrint("[codxe][Config] Failed to load config file, using defaults\n");
}
}

Config::~Config()
{
DbgPrint("Configuration:\n");
DbgPrint("Active Mod: %s\n", config.active_mod.c_str());
DbgPrint("Dump Raw Scripts: %s\n", config.dump_raw ? "true" : "false");
DbgPrint("Dump Map Entities: %s\n", config.dump_map_ents ? "true" : "false");
// Reset to defaults on cleanup
active_mod = "";
dump_rawfile = false;
dump_map_ents = false;
DbgPrint("[codxe][Config] Configuration unloaded\n");
}

bool LoadConfigFromJson(const char *jsonBuffer, DWORD bufferSize, Config &outConfig)
bool Config::LoadFromJson(const char *jsonBuffer, DWORD bufferSize)
{
HJSONREADER hJsonReader = XJSONCreateReader();
if (!hJsonReader)
Expand Down Expand Up @@ -78,35 +94,55 @@ bool LoadConfigFromJson(const char *jsonBuffer, DWORD bufferSize, Config &outCon
XJSONGetTokenValue(hJsonReader, valueBuffer, ARRAYSIZE(valueBuffer));
char narrowValue[256];
wcstombs(narrowValue, valueBuffer, sizeof(narrowValue));
outConfig.active_mod = narrowValue;
active_mod = narrowValue;
}
else if (wcscmp(propertyName, L"dump_raw") == 0)
else if (wcscmp(propertyName, L"dump_rawfile") == 0)
{
outConfig.dump_raw = (jsonTokenType == Json_True);
dump_rawfile = (jsonTokenType == Json_True);
}
else if (wcscmp(propertyName, L"dump_map_ents") == 0)
{
outConfig.dump_map_ents = (jsonTokenType == Json_True);
dump_map_ents = (jsonTokenType == Json_True);
}
else
{
DbgPrint("WARNING: Ignoring unknown property in config: %ls\n", propertyName);
DbgPrint("[codxe][Config] WARNING: Ignoring unknown property: %ls\n", propertyName);
}
}
}

XJSONCloseReader(hJsonReader);

DbgPrintConfig(outConfig);
DbgPrint("[codxe][Config] Configuration loaded:\n");
DbgPrint(" Active Mod: %s\n", active_mod.c_str());
DbgPrint(" Dump Raw Scripts: %s\n", dump_rawfile ? "true" : "false");
DbgPrint(" Dump Map Entities: %s\n", dump_map_ents ? "true" : "false");

return true;
}

bool LoadConfigFromFile(const char *path, Config &outConfig)
bool Config::LoadFromFile(const char *path)
{
std::string jsonContent;
if (!ReadFileToString(path, jsonContent))
return false;

return LoadConfigFromJson(jsonContent.c_str(), static_cast<DWORD>(jsonContent.size()), outConfig);
return LoadFromJson(jsonContent.c_str(), static_cast<DWORD>(jsonContent.size()));
}

// Helper method - requires logic beyond simple field access
std::string Config::GetModBasePath()
{
if (active_mod.empty())
{
return "";
}

std::string mod_base_path = std::string(MOD_DIR) + "\\" + active_mod;
if (!DirectoryExists(mod_base_path.c_str()))
{
DbgPrint("[codxe][Config] Active mod directory does not exist: %s\n", mod_base_path.c_str());
return "";
}
return mod_base_path;
}
36 changes: 15 additions & 21 deletions src/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,24 @@ bool DirectoryExists(const char *path);
bool FileExists(const char *path);
bool ReadFileToString(const char *path, std::string &outString);

struct Config
class Config : public Module
{
std::string active_mod;
bool dump_raw;
bool dump_map_ents;
public:
Config();
~Config();

Config()
const char *get_name() override
{
active_mod = "";
dump_raw = false;
dump_map_ents = false;
return "Config";
}

std::string GetModBasePath()
{
std::string mod_base_path = std::string(MOD_DIR) + "\\" + active_mod;
if (!DirectoryExists(mod_base_path.c_str()))
{
DbgPrint("Config: Active mod directory does not exist: %s\n", mod_base_path.c_str());
return "";
}
return mod_base_path;
}
};
static std::string active_mod;
static bool dump_rawfile;
static bool dump_map_ents;

bool LoadConfigFromJson(const char *jsonBuffer, DWORD bufferSize, Config &outConfig);
bool LoadConfigFromFile(const char *path, Config &outConfig);
static std::string GetModBasePath();

private:
bool LoadFromJson(const char *jsonBuffer, DWORD bufferSize);
bool LoadFromFile(const char *path);
};
7 changes: 2 additions & 5 deletions src/game/iw2/mp/components/scr_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ char *Scr_AddSourceBuffer_Hook(const char *filename, const char *extFilename, co
archive);
};

Config config;
LoadConfigFromFile(CONFIG_PATH, config);

if (config.dump_raw)
if (Config::dump_rawfile)
{
DbgPrint("GSCLoader: Dumping script %s\n", extFilename);
auto contents = callOriginal();
Expand All @@ -35,7 +32,7 @@ char *Scr_AddSourceBuffer_Hook(const char *filename, const char *extFilename, co
}

// Check if mod is active
std::string modBasePath = config.GetModBasePath();
std::string modBasePath = Config::GetModBasePath();
if (modBasePath.empty())
return callOriginal();

Expand Down
1 change: 1 addition & 0 deletions src/game/iw2/mp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace mp
IW2_MP_Plugin::IW2_MP_Plugin()
{
DbgPrint("IW2 MP Plugin initialized\n");
RegisterModule(new Config());
RegisterModule(new scr_parser());
}

Expand Down
7 changes: 2 additions & 5 deletions src/game/iw2/sp/components/scr_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ char *Scr_AddSourceBuffer_Hook(const char *filename, const char *extFilename, co
archive);
};

Config config;
LoadConfigFromFile(CONFIG_PATH, config);

if (config.dump_raw)
if (Config::dump_rawfile)
{
DbgPrint("GSCLoader: Dumping script %s\n", extFilename);
auto contents = callOriginal();
Expand All @@ -35,7 +32,7 @@ char *Scr_AddSourceBuffer_Hook(const char *filename, const char *extFilename, co
}

// Check if mod is active
std::string modBasePath = config.GetModBasePath();
std::string modBasePath = Config::GetModBasePath();
if (modBasePath.empty())
return callOriginal();

Expand Down
1 change: 1 addition & 0 deletions src/game/iw2/sp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace sp
IW2_SP_Plugin::IW2_SP_Plugin()
{
DbgPrint("IW2 SP Plugin initialized\n");
RegisterModule(new Config());
this->RegisterModule(new scr_parser());
}

Expand Down
5 changes: 1 addition & 4 deletions src/game/iw3/mp/components/cmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,8 @@ bool Cmd_ExecFromFastFile_Hook(int localClientNum, int controllerIndex, const ch
controllerIndex, filename);
};

Config config;
LoadConfigFromFile(CONFIG_PATH, config);

// Check if mod is active
std::string modBasePath = config.GetModBasePath();
std::string modBasePath = Config::GetModBasePath();
if (modBasePath.empty())
return callOriginal();

Expand Down
7 changes: 2 additions & 5 deletions src/game/iw3/mp/components/scr_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ char *Scr_AddSourceBuffer_Hook(const char *filename, const char *extFilename, co
archive);
};

Config config;
LoadConfigFromFile(CONFIG_PATH, config);

if (config.dump_raw)
if (Config::dump_rawfile)
{
DbgPrint("GSCLoader: Dumping script %s\n", extFilename);
auto contents = callOriginal();
Expand All @@ -35,7 +32,7 @@ char *Scr_AddSourceBuffer_Hook(const char *filename, const char *extFilename, co
}

// Check if mod is active
std::string modBasePath = config.GetModBasePath();
std::string modBasePath = Config::GetModBasePath();
if (modBasePath.empty())
return callOriginal();

Expand Down
20 changes: 8 additions & 12 deletions src/game/iw3/mp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "components/pm.h"
#include "components/scr_parser.h"
#include "components/scr_vm_functions.h"
#include "common/config.h"

// Structure to hold data for the active keyboard request
struct KeyboardRequest
Expand Down Expand Up @@ -391,12 +392,10 @@ void Load_MapEntsPtr_Hook()
std::replace(file_path.begin(), file_path.end(), '/', '\\'); // Replace forward slashes with backslashes
filesystem::write_file_to_disk(file_path.c_str(), mapEnts->entityString, mapEnts->numEntityChars);

Config config;
LoadConfigFromFile(CONFIG_PATH, config);

// Use new ConfigModule API
// Load map ents from file
// Path to check for existing entity file
std::string raw_file_path = config.GetModBasePath();
std::string raw_file_path = Config::GetModBasePath();

raw_file_path += std::string("\\") + mapEnts->name;
raw_file_path += ".ents"; // IW4x naming convention
Expand Down Expand Up @@ -465,10 +464,8 @@ bool R_StreamLoadHighMipReplacement(const char *filename, unsigned int bytesToRe

auto image = asset->entry.asset.header.image;

Config config;
LoadConfigFromFile(CONFIG_PATH, config);

std::string replacement_path = config.GetModBasePath() + "\\highmip" + "\\" + asset_name + ".dds";
// Use new ConfigModule API
std::string replacement_path = Config::GetModBasePath() + "\\highmip" + "\\" + asset_name + ".dds";
std::ifstream file(replacement_path, std::ios::binary | std::ios::ate);
if (!file)
{
Expand Down Expand Up @@ -1202,10 +1199,7 @@ void Image_Replace_Cube(GfxImage *image, const DDSImage &ddsImage)

void Image_Replace(GfxImage *image)
{
Config config;
LoadConfigFromFile(CONFIG_PATH, config);

const std::string replacement_base_dir = config.GetModBasePath() + "\\images";
const std::string replacement_base_dir = Config::GetModBasePath() + "\\images";
const std::string replacement_path = replacement_base_dir + "\\" + image->name + ".dds";

if (!filesystem::file_exists(replacement_path))
Expand Down Expand Up @@ -1539,6 +1533,8 @@ IW3_MP_Plugin::IW3_MP_Plugin()
{
DbgPrint("Initializing MP\n");

RegisterModule(new Config());

RegisterModule(new cg());
RegisterModule(new cj_tas());
RegisterModule(new clipmap());
Expand Down
7 changes: 2 additions & 5 deletions src/game/iw3/sp/components/scr_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ char *Scr_AddSourceBuffer_Hook(const char *filename, const char *extFilename, co
archive);
};

Config config;
LoadConfigFromFile(CONFIG_PATH, config);

if (config.dump_raw)
if (Config::dump_rawfile)
{
DbgPrint("GSCLoader: Dumping script %s\n", extFilename);
auto contents = callOriginal();
Expand All @@ -35,7 +32,7 @@ char *Scr_AddSourceBuffer_Hook(const char *filename, const char *extFilename, co
}

// Check if mod is active
std::string modBasePath = config.GetModBasePath();
std::string modBasePath = Config::GetModBasePath();
if (modBasePath.empty())
return callOriginal();

Expand Down
1 change: 1 addition & 0 deletions src/game/iw3/sp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void Cmd_Dumpraw_f()
IW3_SP_Plugin::IW3_SP_Plugin()
{
DbgPrint("IW3 SP: Plugin loaded\n");
RegisterModule(new Config());
RegisterModule(new scr_parser());

CL_GamepadButtonEvent_Detour = Detour(CL_GamepadButtonEvent, CL_GamepadButtonEvent_Hook);
Expand Down
7 changes: 2 additions & 5 deletions src/game/iw4/mp/components/scr_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ char *Scr_AddSourceBuffer_Hook(const char *filename, const char *extFilename)
auto callOriginal = [&]()
{ return Scr_AddSourceBuffer_Detour.GetOriginal<decltype(Scr_AddSourceBuffer)>()(filename, extFilename); };

Config config;
LoadConfigFromFile(CONFIG_PATH, config);

if (config.dump_raw)
if (Config::dump_rawfile)
{
auto contents = callOriginal();
// Dump the script to a file
Expand All @@ -27,7 +24,7 @@ char *Scr_AddSourceBuffer_Hook(const char *filename, const char *extFilename)
}

// Check if mod is active
std::string modBasePath = config.GetModBasePath();
std::string modBasePath = Config::GetModBasePath();
if (modBasePath.empty())
return callOriginal();

Expand Down
1 change: 1 addition & 0 deletions src/game/iw4/mp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ IW4_MP_Plugin::IW4_MP_Plugin()
{

DbgPrint("IW4 MP: Registering modules\n");
RegisterModule(new Config());
RegisterModule(new Events()); // Must be registered first to ensure hooks are in place
RegisterModule(new cg());
RegisterModule(new clipmap());
Expand Down
Loading