Skip to content

Commit 803cdd1

Browse files
committed
1.6: Addendum to 05ae2ac: Forgot to make base dir check release-build ready
Noote: This is a little different from master patch due to C++ 14 constraint.
1 parent 05ae2ac commit 803cdd1

File tree

1 file changed

+40
-59
lines changed

1 file changed

+40
-59
lines changed

Shared/sdk/SharedUtil.Misc.hpp

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -180,40 +180,20 @@ static void InitializeProcessBaseDir(SString& strProcessBaseDir)
180180
DWORD lengthFull = GetFullPathNameW(corePathBuffer.c_str(), static_cast<DWORD>(fullPath.size()), fullPath.data(), nullptr);
181181
if (lengthFull > 0)
182182
{
183-
std::wstring fullPathStr;
184-
185-
// If buffer too small, resize for long path support
186-
if (static_cast<size_t>(lengthFull) > fullPath.size())
187-
{
188-
if (static_cast<size_t>(lengthFull) > MAX_UNICODE_PATH)
189-
return; // Path too long, validation failed
190-
191-
std::wstring fullPathBuffer;
192-
fullPathBuffer.resize(static_cast<size_t>(lengthFull));
193-
lengthFull = GetFullPathNameW(corePathBuffer.c_str(), static_cast<DWORD>(fullPathBuffer.size()), &fullPathBuffer[0], nullptr);
194-
if (lengthFull > 0 && static_cast<size_t>(lengthFull) < fullPathBuffer.size())
195-
{
196-
fullPathStr = fullPathBuffer.substr(0, static_cast<size_t>(lengthFull));
197-
}
198-
}
199-
else
200-
{
201-
fullPathStr = std::wstring(fullPath.data(), static_cast<size_t>(lengthFull));
202-
}
203-
204-
// Process path and extract base directory by walking up to find Bin/ directory
205-
if (!fullPathStr.empty())
206-
{
183+
// Process path and extract base directory
184+
// The directory above /MTA/ is always the base directory
185+
const auto processPath = [&strProcessBaseDir](const std::wstring& fullPathStr) {
207186
const size_t lastSeparator = fullPathStr.find_last_of(L"\\/");
208187
if (lastSeparator != std::wstring::npos)
209188
{
210189
std::wstring currentPath = fullPathStr.substr(0, lastSeparator);
211190

212-
// Walk up the directory tree to find Bin/ folder
191+
// Walk up to find MTA/ folder
192+
// Stop at first MTA folder found - it's guaranteed to be the correct one
213193
// Check current directory and up to 2 parent levels
214194
for (int level = 0; level < 3; ++level)
215195
{
216-
// Extract the current folder name (last component of path)
196+
// Extract folder name from current path
217197
const size_t lastSep = currentPath.find_last_of(L"\\/");
218198
std::wstring folderName = (lastSep != std::wstring::npos)
219199
? currentPath.substr(lastSep + 1)
@@ -222,48 +202,49 @@ static void InitializeProcessBaseDir(SString& strProcessBaseDir)
222202
// Convert to lowercase for case-insensitive comparison
223203
std::transform(folderName.begin(), folderName.end(), folderName.begin(), ::towlower);
224204

225-
if (folderName == L"bin")
205+
if (folderName == L"mta")
226206
{
227-
strProcessBaseDir = ToUTF8(currentPath);
228-
return;
207+
// Found MTA folder - base directory is its parent
208+
// Stop searching immediately to avoid finding outer "MTA" folders (e.g user with custom install dir)
209+
if (lastSep != std::wstring::npos)
210+
{
211+
std::wstring parentPath = currentPath.substr(0, lastSep);
212+
// Add trailing separator for drive roots to match filesystem::path::parent_path() behavior
213+
if (parentPath.length() == 2 && parentPath[1] == L':')
214+
parentPath += L'\\';
215+
strProcessBaseDir = ToUTF8(parentPath);
216+
}
217+
return; // Always stop at first MTA folder found
229218
}
230219

231-
// Move up one level for next iteration
220+
// Move up one level
232221
if (lastSep != std::wstring::npos)
233-
{
234222
currentPath = currentPath.substr(0, lastSep);
235-
}
236223
else
237-
{
238224
break; // Reached root, can't go further
239-
}
240225
}
241-
242-
// Fallback: Check if current working directory is or contains Bin/
243-
if (!bCoreModuleFound)
244-
{
245-
std::vector<wchar_t> cwdBuffer(MAX_PATH);
246-
if (GetCurrentDirectoryW(MAX_PATH, cwdBuffer.data()) > 0)
247-
{
248-
std::wstring cwdPath(cwdBuffer.data());
249-
// Extract the folder name (last component)
250-
const size_t cwdLastSep = cwdPath.find_last_of(L"\\/");
251-
std::wstring cwdName = (cwdLastSep != std::wstring::npos)
252-
? cwdPath.substr(cwdLastSep + 1)
253-
: cwdPath;
254-
255-
std::transform(cwdName.begin(), cwdName.end(), cwdName.begin(), ::towlower);
256-
257-
if (cwdName == L"bin")
258-
{
259-
strProcessBaseDir = ToUTF8(cwdPath);
260-
return;
261-
}
262-
}
263-
}
264-
265-
// No Bin/ folder found - leave strProcessBaseDir empty
266226
}
227+
};
228+
229+
// If buffer too small, resize for long path support
230+
if (static_cast<size_t>(lengthFull) > fullPath.size())
231+
{
232+
if (static_cast<size_t>(lengthFull) > MAX_UNICODE_PATH)
233+
return; // Path too long, validation failed
234+
235+
std::wstring fullPathBuffer;
236+
fullPathBuffer.resize(static_cast<size_t>(lengthFull));
237+
lengthFull = GetFullPathNameW(corePathBuffer.c_str(), static_cast<DWORD>(fullPathBuffer.size()), &fullPathBuffer[0], nullptr);
238+
if (lengthFull > 0 && static_cast<size_t>(lengthFull) < fullPathBuffer.size())
239+
{
240+
fullPathBuffer.resize(static_cast<size_t>(lengthFull));
241+
processPath(fullPathBuffer);
242+
}
243+
}
244+
else
245+
{
246+
std::wstring fullPathStr(fullPath.data(), static_cast<size_t>(lengthFull));
247+
processPath(fullPathStr);
267248
}
268249
}
269250
}

0 commit comments

Comments
 (0)