Skip to content

Commit

Permalink
Add a Log system for mods. (#45)
Browse files Browse the repository at this point in the history
* added a log system for mods.

I'm not convinced this log file is in the right location on disk yet, currently it will be located right next to the mod api launcher though. also had to do a hack to StreamDefinitions to get it to compile

* add a switch to control the logfilename

* renamed command line argument

* resolve App.cpp compile errors
  • Loading branch information
Zarklord authored Nov 15, 2023
1 parent 8f5dfa3 commit e0b7025
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 18 deletions.
31 changes: 16 additions & 15 deletions Spore ModAPI/SourceCode/App/App.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#ifndef MODAPI_DLL_EXPORT
#include <Spore\App\Thumbnail_cImportExport.h>
#include <Spore\App\AppData.h>
Expand All @@ -6,9 +7,12 @@
#include <Spore\App\cSporeApp.h>
#include <Spore\App\cArithmeticaResource.h>
#include <Spore\App\ConfigManager.h>
#endif
#include <Spore\App\CommandLine.h>

namespace App
{
#ifndef MODAPI_DLL_EXPORT
auto_STATIC_METHOD_(Thumbnail_cImportExport, Thumbnail_cImportExport*, Get);

auto_METHOD(Thumbnail_cImportExport, bool, GetFolderPath,
Expand All @@ -30,25 +34,13 @@ namespace App

auto_STATIC_METHOD_(cCreatureModeStrategy, cCreatureModeStrategy*, Get);


auto_STATIC_METHOD_(IAppSystem, IAppSystem*, Get);

auto_STATIC_METHOD(cSporeApp, int, EAMain, Args(CommandLine* commandLine), Args(commandLine));
auto_STATIC_METHOD(cSporeApp, bool, Init, Args(CommandLine* commandLine), Args(commandLine));
auto_STATIC_METHOD_VOID_(cSporeApp, Run);
auto_STATIC_METHOD_(cSporeApp, bool, Shutdown);


auto_METHOD(CommandLine, eastl::string16&, Get, Args(int i), Args(i));

auto_METHOD(CommandLine, int, FindSwitch,
Args(const char16_t* a1, bool a2, eastl::string16* a3, int a4),
Args(a1, a2, a3, a4));

CommandLine* GetAppCommandLine() {
return *(CommandLine**)GetAddress(App, sAppCommandLine);
}

auto_STATIC_METHOD_(Canvas, Canvas*, Get);

//// cArithmeticaResource ////
Expand Down Expand Up @@ -80,7 +72,6 @@ namespace App
void cArithmeticaResource::func14h() {
}


//// ConfigManager ////

auto_STATIC_METHOD_(IConfigManager, IConfigManager*, Get);
Expand All @@ -89,5 +80,15 @@ namespace App
{
return *(PropertyList**)GetAddress(App, sPreferencesPropList);
}
}
#endif
#endif

auto_METHOD(CommandLine, eastl::string16&, Get, Args(int i), Args(i));

auto_METHOD(CommandLine, int, FindSwitch,
Args(const char16_t* a1, bool a2, eastl::string16* a3, int a4),
Args(a1, a2, a3, a4));

CommandLine* GetAppCommandLine() {
return *(CommandLine**)GetAddress(App, sAppCommandLine);
}
}
54 changes: 54 additions & 0 deletions Spore ModAPI/SourceCode/DLL/Application.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "Spore/App/CommandLine.h"
#ifdef MODAPI_DLL_EXPORT
#include "stdafx.h"
#include "Application.h"
#include <Spore\ArgScript\FormatParser.h>
#include <cuchar>
#include <Spore\IO.h>

bool ShaderFragments_detour::DETOUR(Resource::Database* pDBPF)
Expand Down Expand Up @@ -58,6 +60,8 @@ namespace ModAPI
eastl::fixed_vector<InitFunction, MAX_MODS> postInitFunctions;
eastl::fixed_vector<InitFunction, MAX_MODS> disposeFunctions;
eastl::fixed_map<uint32_t, ISimulatorStrategyPtr, MAX_MODS> simulatorStrategies;
FileStreamPtr logFile{};
__time64_t logFileStartTime;

uint32_t CRC_TABLE[256];

Expand Down Expand Up @@ -112,10 +116,57 @@ namespace ModAPI
}
}

EXTERN_C IMAGE_DOS_HEADER __ImageBase;

void CreateLogFile() {
_time64(&ModAPI::logFileStartTime);

TCHAR DllPath[MAX_PATH] = {0};
char DllPathA[MAX_PATH] = {0};
size_t i;
GetModuleFileName(reinterpret_cast<HINSTANCE>(&__ImageBase), DllPath, _countof(DllPath));
wcstombs_s(&i, DllPathA, MAX_PATH, DllPath, MAX_PATH);

eastl::string16 log_path;
log_path.reserve(MAX_PATH);

mbstate_t state{};
char16_t c16;
const char* ptr = DllPathA;
const char* end = DllPathA + strlen(DllPathA);
for (size_t rc; (rc = mbrtoc16(&c16, ptr, end - ptr + 1, &state)); ptr += rc)
log_path += c16;

//removes the mlibs\\file.dll from the path
log_path.resize(log_path.find_last_of('\\'));
log_path.resize(log_path.find_last_of('\\')+1);

eastl::string16 log_file_name;
AppCommandLine.FindSwitch(u"modapi-log-filename", false, &log_file_name);
if (log_file_name.empty())
log_file_name = u"spore_log";
log_file_name += u".txt";
log_path.append(log_file_name);

ModAPI::logFile = new IO::FileStream(log_path.c_str());
ModAPI::logFile->Open(IO::AccessFlags::Write, IO::CD::CreateAlways);
}

void CloseLogFile() {
if (ModAPI::logFile) {
ModAPI::logFile->Close();
ModAPI::logFile.reset();
}
}

int ModAPI::sub_7E6C60_detour::DETOUR(int arg_0)
{
int result = original_function(this, arg_0);

CreateLogFile();
ModAPI::Log("Spore ModAPI %d.%d.%d loaded.", ModAPI::GetMajorVersion(), ModAPI::GetMinorVersion(), ModAPI::GetBuildVersion());
ModAPI::Log("Platform: %s", ModAPI::GetGameType() == ModAPI::GameType::Disk ? "Disk" : "March2017");

for (ModAPI::InitFunction& func : ModAPI::initFunctions) func();

return result;
Expand All @@ -138,6 +189,9 @@ int ModAPI::AppShutdown_detour::DETOUR()
for (auto it : ModAPI::simulatorStrategies) it.second->Dispose();
ModAPI::simulatorStrategies.clear();

ModAPI::Log("Shutting Down");
CloseLogFile();

return original_function(this);
}

Expand Down
5 changes: 5 additions & 0 deletions Spore ModAPI/SourceCode/DLL/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <Spore\CommonIDs.h>
#include <Spore\Messaging.h>
#include <Spore\Cheats.h>
#include <Spore\IO.h>
#include <ctime>

virtual_detour(ShaderFragments_detour, Graphics::cMaterialManager, Graphics::IMaterialManager, bool(Resource::Database*)) {};

Expand All @@ -24,6 +26,9 @@ namespace ModAPI

extern eastl::fixed_map<uint32_t, ISimulatorStrategyPtr, MAX_MODS> simulatorStrategies;

extern FileStreamPtr logFile;
extern __time64_t logFileStartTime;

long AttachDetour();
void DetachDetour();

Expand Down
39 changes: 39 additions & 0 deletions Spore ModAPI/SourceCode/DLL/DllModAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,45 @@ namespace ModAPI
disposeFunctions.push_back(f);
}

static constexpr unsigned int SCRATCH_SIZE = 4096;
char logScratch[SCRATCH_SIZE];

void Log(const char* fmt, ...) {
unsigned int offset = 0;

__time64_t long_time;
_time64(&long_time);

long_time -= logFileStartTime;

constexpr static char formatted[] = {"[00:00:00]: "};
constexpr char format[] = {"[%02d:%02d:%02d]: "};
const int secs = long_time % 60;
long_time /= 60;
const int mins = long_time % 60;
long_time /= 60;
const int hours = long_time % 24;

sprintf_s(logScratch + offset, SCRATCH_SIZE - offset, format, hours,mins,secs);
offset += (sizeof(formatted)-1)/sizeof(formatted[0]);

va_list argList;
va_start(argList, fmt);
vsnprintf(logScratch + offset, SCRATCH_SIZE - offset, fmt, argList);
va_end(argList);

// vsnprintf does not guarantee a null terminator if the formatted string exceeds the buffer size
logScratch[SCRATCH_SIZE - 1] = 0;

if (logFile)
{
logFile->Write(logScratch, strlen(logScratch));
logFile->Write("\n", 1);
logFile->Flush();
}
App::ConsolePrintF(logScratch + offset);
}

bool AddSimulatorStrategy(Simulator::ISimulatorStrategy* strategy, uint32_t id) {
if (strategy && simulatorStrategies.find(id) == simulatorStrategies.end()) {
simulatorStrategies[id] = strategy;
Expand Down
6 changes: 3 additions & 3 deletions Spore ModAPI/SourceCode/IO/StreamDefinitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ namespace IO
/////////////////////////
}

#ifndef MODAPI_DLL_EXPORT
namespace IO
{
//////////////////////
Expand Down Expand Up @@ -125,7 +124,8 @@ namespace IO
auto_METHOD(FileStream, size_t, GetPathCString, Args(char* pPath8, size_t nPathLength), Args(pPath8, nPathLength));
auto_METHOD(FileStream, bool, Open, Args(AccessFlags nAccessFlags, CD nCreationDisposition, int nSharing, int nUsageHints), Args(nAccessFlags, nCreationDisposition, nSharing, nUsageHints));



#ifndef MODAPI_DLL_EXPORT
//////////////////////


Expand Down Expand Up @@ -395,5 +395,5 @@ namespace IO
auto_METHOD(StreamNull, int, Write, Args(const void* pData, size_t nSize), Args(pData, nSize));

//////////////////////
}
#endif
}
2 changes: 2 additions & 0 deletions Spore ModAPI/Spore/ModAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace ModAPI
extern MODAPI void AddPostInitFunction(InitFunction);
extern MODAPI void AddDisposeFunction(InitFunction);

extern MODAPI void Log(const char* fmt, ...);

extern MODAPI bool AddSimulatorStrategy(Simulator::ISimulatorStrategy* strategy, uint32_t id);

extern MODAPI GameType GetGameType();
Expand Down

0 comments on commit e0b7025

Please sign in to comment.