|
4 | 4 | #include "Application.h" |
5 | 5 | #include <Spore\ArgScript\FormatParser.h> |
6 | 6 | #include <cuchar> |
| 7 | +#include <tlhelp32.h> |
| 8 | +#include <psapi.h> |
7 | 9 | #include <Spore\IO.h> |
8 | 10 |
|
9 | 11 | bool ShaderFragments_detour::DETOUR(Resource::Database* pDBPF) |
@@ -116,30 +118,75 @@ namespace ModAPI |
116 | 118 | } |
117 | 119 | } |
118 | 120 |
|
119 | | -EXTERN_C IMAGE_DOS_HEADER __ImageBase; |
120 | | - |
121 | 121 | void CreateLogFile() { |
122 | 122 | _time64(&ModAPI::logFileStartTime); |
123 | 123 |
|
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 | + } |
129 | 180 |
|
130 | 181 | eastl::string16 log_path; |
131 | 182 | log_path.reserve(MAX_PATH); |
132 | 183 |
|
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); |
143 | 190 |
|
144 | 191 | eastl::string16 log_file_name; |
145 | 192 | AppCommandLine.FindSwitch(u"modapi-log-filename", false, &log_file_name); |
|
0 commit comments