Skip to content

Commit 5f48d9b

Browse files
authored
Improve log file location detection (#55)
* vcxproj: enable MultiProcessorCompilation * improve log file location detection
1 parent e0b7025 commit 5f48d9b

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

Spore ModAPI/SourceCode/DLL/Application.cpp

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "Application.h"
55
#include <Spore\ArgScript\FormatParser.h>
66
#include <cuchar>
7+
#include <tlhelp32.h>
8+
#include <psapi.h>
79
#include <Spore\IO.h>
810

911
bool ShaderFragments_detour::DETOUR(Resource::Database* pDBPF)
@@ -116,30 +118,75 @@ namespace ModAPI
116118
}
117119
}
118120

119-
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
120-
121121
void CreateLogFile() {
122122
_time64(&ModAPI::logFileStartTime);
123123

124-
TCHAR DllPath[MAX_PATH] = {0};
125-
char DllPathA[MAX_PATH] = {0};
126-
size_t i;
127-
GetModuleFileName(reinterpret_cast<HINSTANCE>(&__ImageBase), DllPath, _countof(DllPath));
128-
wcstombs_s(&i, DllPathA, MAX_PATH, DllPath, MAX_PATH);
124+
wchar_t log_path_buf[MAX_PATH] = { 0 };
125+
126+
// try to find parent process
127+
PROCESSENTRY32 processEntry = { 0 };
128+
processEntry.dwSize = sizeof(PROCESSENTRY32);
129+
const int pid = GetCurrentProcessId();
130+
HANDLE toolHelpHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
131+
bool foundParentProccess = false;
132+
bool hasParentProcessPath = false;
133+
DWORD parentProcessId = 0;
134+
135+
if (toolHelpHandle != INVALID_HANDLE_VALUE &&
136+
Process32First(toolHelpHandle, &processEntry))
137+
{
138+
do
139+
{
140+
if (processEntry.th32ProcessID == pid)
141+
{
142+
parentProcessId = processEntry.th32ParentProcessID;
143+
foundParentProccess = true;
144+
break;
145+
}
146+
} while (Process32Next(toolHelpHandle, &processEntry));
147+
}
148+
149+
// attempt to retrieve the module file path
150+
if (foundParentProccess && parentProcessId != 0)
151+
{
152+
HANDLE parentProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, parentProcessId);
153+
if (parentProcessHandle != INVALID_HANDLE_VALUE)
154+
{
155+
if (GetModuleFileNameExW(parentProcessHandle, 0, log_path_buf, _countof(log_path_buf)) != 0)
156+
{
157+
hasParentProcessPath = true;
158+
}
159+
CloseHandle(parentProcessHandle);
160+
}
161+
}
162+
163+
// when we've found the parent process, ensure we have an allowed filename
164+
// currently, only the launcher kit & SMM are allowed
165+
if (hasParentProcessPath)
166+
{
167+
if (wcsstr(log_path_buf, L"Spore ModAPI Launcher.exe") == nullptr &&
168+
wcsstr(log_path_buf, L"Launch Spore.exe") == nullptr)
169+
{
170+
hasParentProcessPath = false;
171+
}
172+
}
173+
174+
// when we don't have the parent process' path,
175+
// fallback to the executable path
176+
if (!hasParentProcessPath)
177+
{
178+
GetModuleFileNameW(nullptr, log_path_buf, _countof(log_path_buf));
179+
}
129180

130181
eastl::string16 log_path;
131182
log_path.reserve(MAX_PATH);
132183

133-
mbstate_t state{};
134-
char16_t c16;
135-
const char* ptr = DllPathA;
136-
const char* end = DllPathA + strlen(DllPathA);
137-
for (size_t rc; (rc = mbrtoc16(&c16, ptr, end - ptr + 1, &state)); ptr += rc)
138-
log_path += c16;
139-
140-
//removes the mlibs\\file.dll from the path
141-
log_path.resize(log_path.find_last_of('\\'));
142-
log_path.resize(log_path.find_last_of('\\')+1);
184+
// fill log_path with log_path_buf
185+
for (size_t i = 0; i < wcslen(log_path_buf); i++)
186+
log_path += log_path_buf[i];
187+
188+
// strip the filename from the path
189+
log_path.resize(log_path.find_last_of(u'\\')+1);
143190

144191
eastl::string16 log_file_name;
145192
AppCommandLine.FindSwitch(u"modapi-log-filename", false, &log_file_name);

Spore ModAPI/Spore ModAPI.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<SDLCheck>true</SDLCheck>
105105
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
106106
<PreprocessToFile>false</PreprocessToFile>
107+
<MultiProcessorCompilation>true</MultiProcessorCompilation>
107108
</ClCompile>
108109
<Link>
109110
<SubSystem>Windows</SubSystem>
@@ -126,6 +127,7 @@
126127
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
127128
<SDLCheck>true</SDLCheck>
128129
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
130+
<MultiProcessorCompilation>true</MultiProcessorCompilation>
129131
</ClCompile>
130132
<Link>
131133
<SubSystem>Windows</SubSystem>
@@ -148,6 +150,7 @@
148150
<SDLCheck>true</SDLCheck>
149151
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
150152
<WholeProgramOptimization>false</WholeProgramOptimization>
153+
<MultiProcessorCompilation>true</MultiProcessorCompilation>
151154
</ClCompile>
152155
<Link>
153156
<SubSystem>Windows</SubSystem>
@@ -172,6 +175,7 @@
172175
<SDLCheck>true</SDLCheck>
173176
<DisableSpecificWarnings>4351</DisableSpecificWarnings>
174177
<WholeProgramOptimization>false</WholeProgramOptimization>
178+
<MultiProcessorCompilation>true</MultiProcessorCompilation>
175179
</ClCompile>
176180
<Link>
177181
<SubSystem>Windows</SubSystem>

0 commit comments

Comments
 (0)