Skip to content

Commit db410fd

Browse files
Fixed renaming files vs file locale
1 parent 65582d5 commit db410fd

File tree

7 files changed

+124
-16
lines changed

7 files changed

+124
-16
lines changed

make-msvc.bat

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ if exist "%PROGRAM_FILES_X64%\Microsoft Visual Studio\2022\Enterprise\VC\Auxilia
2525
if exist "%PROGRAM_FILES_X64%\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvarsall.bat" set VCVARS_20xx=%PROGRAM_FILES_X64%\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvarsall.bat
2626
if exist "%PROGRAM_FILES_X64%\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" set VCVARS_20xx=%PROGRAM_FILES_X64%\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat
2727

28-
:: Build all libraries using Visual Studio 2008 and 202x
28+
:: Build all libraries using Visual Studio 2008
29+
rmdir /S /Q .\bin\%LIB_NAME%
2930
if not "x%VCVARS_2008%" == "x" call :BuildLibs "%VCVARS_2008%" x86 %LIB_NAME%_vs08.sln vs2008
3031
if not "x%VCVARS_2008%" == "x" call :BuildLibs "%VCVARS_2008%" x64 %LIB_NAME%_vs08.sln vs2008
32+
33+
:: Build all libraries using Visual Studio 2017+
34+
rmdir /S /Q .\bin\%LIB_NAME%
3135
if not "x%VCVARS_20xx%" == "x" call :BuildLibs "%VCVARS_20xx%" x86 %LIB_NAME%.sln
3236
if not "x%VCVARS_20xx%" == "x" call :BuildLibs "%VCVARS_20xx%" x64 %LIB_NAME%.sln
3337
goto:eof
@@ -92,7 +96,7 @@ devenv.com %1 /project "%LIB_NAME%" /rebuild "%5|%2"
9296
if not exist ..\aaa goto:eof
9397
if not exist ..\aaa\inc md ..\aaa\inc
9498
if not exist ..\aaa\%3 md ..\aaa\%3
95-
copy /Y /D .\src\StormLib.h ..\aaa\inc >nul
96-
copy /Y /D .\src\StormPort.h ..\aaa\inc >nul
97-
copy /Y /D .\bin\StormLib\%2\%5\%4.lib ..\aaa\%3\%4.lib >nul
98-
copy /Y /D .\bin\StormLib\%2\%5\%4.pdb ..\aaa\%3\%4.pdb >nul
99+
copy /Y .\src\StormLib.h ..\aaa\inc >nul
100+
copy /Y .\src\StormPort.h ..\aaa\inc >nul
101+
copy /Y .\bin\%LIB_NAME%\%2\%5\%4.lib ..\aaa\%3\%4.lib >nul
102+
copy /Y .\bin\%LIB_NAME%\%2\%5\%4.pdb ..\aaa\%3\%4.pdb >nul

make.bat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ echo.
2020
:BUILD_BINARY_64
2121
echo Building %BINARY_NAME%.lib (64-bit) ...
2222
set DDKBUILDENV=
23-
call %WDKDIR%\bin\setenv.bat %WDKDIR%\ fre x64 WLH
23+
call %WDKDIR%\bin\setenv.bat %WDKDIR%\ fre x64 wnet
2424
cd /d %PROJECT_DIR%
2525
build.exe -czgw
26-
del buildfre_wlh_amd64.log
26+
del buildfre_wnet_amd64.log
2727
echo.
2828

2929
:BUILD_BINARY_32
@@ -42,8 +42,8 @@ if not exist ..\aaa\lib32 md ..\aaa\lib32
4242
if not exist ..\aaa\lib64 md ..\aaa\lib64
4343
copy /Y .\src\StormLib.h ..\aaa\inc >nul
4444
copy /Y .\src\StormPort.h ..\aaa\inc >nul
45-
copy /Y .\objfre_wlh_amd64\amd64\%BINARY_NAME%.lib ..\aaa\lib64\%BINARY_NAME%.lib >nul
46-
copy /Y .\objfre_w2k_x86\i386\%BINARY_NAME%.lib ..\aaa\lib32\%BINARY_NAME%.lib >nul
45+
copy /Y .\objfre_wnet_amd64\amd64\%BINARY_NAME%.lib ..\aaa\lib64\%BINARY_NAME%.lib >nul
46+
copy /Y .\objfre_w2k_x86\i386\%BINARY_NAME%.lib ..\aaa\lib32\%BINARY_NAME%.lib >nul
4747

4848
:CLEANUP
4949
if exist sources-cpp.cpp del sources-cpp.cpp

src/SFileAddFile.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,8 +1163,11 @@ bool WINAPI SFileRemoveFile(HANDLE hMpq, const char * szFileName, DWORD dwSearch
11631163
bool WINAPI SFileRenameFile(HANDLE hMpq, const char * szFileName, const char * szNewFileName)
11641164
{
11651165
TMPQArchive * ha = IsValidMpqHandle(hMpq);
1166+
TFileEntry * pFileEntry;
11661167
TMPQFile * hf;
1168+
DWORD dwHashIndex = 0;
11671169
DWORD dwErrCode = ERROR_SUCCESS;
1170+
LCID lcFileLocale = 0;
11681171

11691172
// Test the valid parameters
11701173
if(ha == NULL)
@@ -1181,11 +1184,25 @@ bool WINAPI SFileRenameFile(HANDLE hMpq, const char * szFileName, const char * s
11811184
dwErrCode = ERROR_ACCESS_DENIED;
11821185
}
11831186

1184-
// Open the new file. If exists, we don't allow rename operation
1187+
// Retrieve the locale of the existing file
1188+
// Could be the preferred one or neutral one
1189+
if(dwErrCode == ERROR_SUCCESS && ha->pHashTable != NULL)
1190+
{
1191+
if((pFileEntry = GetFileEntryLocale(ha, szFileName, g_lcFileLocale, &dwHashIndex)) != NULL)
1192+
{
1193+
lcFileLocale = ha->pHashTable[dwHashIndex].Locale;
1194+
}
1195+
else
1196+
dwErrCode = ERROR_FILE_NOT_FOUND;
1197+
}
1198+
1199+
// The target file entry must not be there
11851200
if(dwErrCode == ERROR_SUCCESS)
11861201
{
1187-
if(GetFileEntryLocale(ha, szNewFileName, g_lcFileLocale) != NULL)
1202+
if(GetFileEntryExact(ha, szNewFileName, lcFileLocale) != NULL)
1203+
{
11881204
dwErrCode = ERROR_ALREADY_EXISTS;
1205+
}
11891206
}
11901207

11911208
// Open the file from the MPQ
@@ -1195,9 +1212,9 @@ bool WINAPI SFileRenameFile(HANDLE hMpq, const char * szFileName, const char * s
11951212
if(SFileOpenFileEx(hMpq, szFileName, SFILE_OPEN_BASE_FILE, (HANDLE *)&hf))
11961213
{
11971214
ULONGLONG RawDataOffs;
1198-
TFileEntry * pFileEntry = hf->pFileEntry;
11991215

12001216
// Invalidate the entries for internal files
1217+
pFileEntry = hf->pFileEntry;
12011218
InvalidateInternalFiles(ha);
12021219

12031220
// Rename the file entry in the table
@@ -1266,15 +1283,21 @@ bool WINAPI SFileSetFileLocale(HANDLE hFile, LCID lcNewLocale)
12661283
TFileEntry * pFileEntry;
12671284
TMPQFile * hf = IsValidFileHandle(hFile);
12681285

1269-
// Invalid handle => do nothing
1286+
// Invalid file handle => return error
12701287
if(hf == NULL)
12711288
{
12721289
SErrSetLastError(ERROR_INVALID_HANDLE);
12731290
return false;
12741291
}
12751292

1293+
// Invalid archive handle => return error
1294+
if((ha = IsValidMpqHandle(hf->ha)) == NULL)
1295+
{
1296+
SErrSetLastError(ERROR_INVALID_HANDLE);
1297+
return false;
1298+
}
1299+
12761300
// Do not allow to rename files in MPQ open for read only
1277-
ha = hf->ha;
12781301
if(ha->dwFlags & MPQ_FLAG_READ_ONLY)
12791302
{
12801303
SErrSetLastError(ERROR_ACCESS_DENIED);

src/StormCommon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ void FreeBetTable(TMPQBetTable * pBetTable);
366366

367367
// Functions for finding files in the file table
368368
TFileEntry * GetFileEntryLocale(TMPQArchive * ha, const char * szFileName, LCID lcFileLocale, LPDWORD PtrHashIndex = NULL);
369-
TFileEntry * GetFileEntryExact(TMPQArchive * ha, const char * szFileName, LCID lcFileLocale, LPDWORD PtrHashIndex);
369+
TFileEntry * GetFileEntryExact(TMPQArchive * ha, const char * szFileName, LCID lcFileLocale, LPDWORD PtrHashIndex = NULL);
370370

371371
// Allocates file name in the file entry
372372
void AllocateFileName(TMPQArchive * ha, TFileEntry * pFileEntry, const char * szFileName);

src/lzma/C/Threads.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
static WRes GetError()
1111
{
12-
DWORD res = SErrGetLastError();
12+
DWORD res = GetLastError();
1313
return (res) ? (WRes)(res) : 1;
1414
}
1515

test/StormTest.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,25 @@ static DWORD GetFilePatchCount(TLogHelper * pLogger, HANDLE hMpq, LPCSTR szFileN
10491049
return nPatchCount;
10501050
}
10511051

1052+
static DWORD SetLocaleForFileOperations(HANDLE hMpq, LPCSTR szFileName, LCID lcLocale)
1053+
{
1054+
HANDLE hFile = NULL;
1055+
DWORD dwErrCode = ERROR_SUCCESS;
1056+
1057+
if(SFileOpenFileEx(hMpq, szFileName, 0, &hFile))
1058+
{
1059+
if(!SFileSetFileLocale(hFile, lcLocale))
1060+
dwErrCode = SErrGetLastError();
1061+
SFileSetLocale(lcLocale);
1062+
SFileCloseFile(hFile);
1063+
}
1064+
else
1065+
{
1066+
dwErrCode = SErrGetLastError();
1067+
}
1068+
return dwErrCode;
1069+
}
1070+
10521071
static DWORD VerifyFilePatchCount(TLogHelper * pLogger, HANDLE hMpq, LPCSTR szFileName, DWORD dwExpectedPatchCount)
10531072
{
10541073
DWORD dwPatchCount = 0;
@@ -3057,6 +3076,61 @@ static DWORD TestCreateArchive(const TEST_INFO2 & TestInfo)
30573076
return TestCreateArchive(szPlainNameT, TestInfo.szName2, TestInfo.dwFlags);
30583077
}
30593078

3079+
static DWORD TestRenameFile(LPCTSTR szPlainName)
3080+
{
3081+
TLogHelper Logger("TestRenameFile", szPlainName);
3082+
LPCSTR szSourceFile = "war3map.mmp";
3083+
LPCSTR szTargetFile = "war3map.wts";
3084+
HANDLE hMpq = NULL;
3085+
DWORD dwErrCode;
3086+
DWORD dwFailed = 0;
3087+
LCID LCID_RURU = 0x0419;
3088+
LCID LCID_ESES = 0x040A;
3089+
3090+
// Create copy of the archive and open it
3091+
dwErrCode = OpenExistingArchiveWithCopy(&Logger, szPlainName, szPlainName, &hMpq);
3092+
if(dwErrCode == ERROR_SUCCESS)
3093+
{
3094+
// Try to rename an existing file to "war3map.wts".
3095+
// This must fail because the file already exists.
3096+
if(SFileRenameFile(hMpq, szSourceFile, szTargetFile))
3097+
dwFailed++;
3098+
3099+
// Now change locale of an existing file to Russian
3100+
if(SetLocaleForFileOperations(hMpq, szSourceFile, LCID_RURU) != ERROR_SUCCESS)
3101+
dwFailed++;
3102+
3103+
// The rename should work now
3104+
if(!SFileRenameFile(hMpq, szSourceFile, szTargetFile))
3105+
dwFailed++;
3106+
3107+
// Changing the file locale to Neutral should fail now,
3108+
// because such file already exists
3109+
if(SetLocaleForFileOperations(hMpq, szTargetFile, 0) != ERROR_ALREADY_EXISTS)
3110+
dwFailed++;
3111+
3112+
// Pick another source file
3113+
szSourceFile = "war3map.shd";
3114+
3115+
// Change the file locale to Spain
3116+
if(SetLocaleForFileOperations(hMpq, szSourceFile, LCID_ESES) != ERROR_SUCCESS)
3117+
dwFailed++;
3118+
3119+
// This rename should also work, because there is no target file with Spanish locale
3120+
if(!SFileRenameFile(hMpq, szSourceFile, szTargetFile))
3121+
dwFailed++;
3122+
3123+
// Evaluate the result
3124+
if(dwFailed != 0)
3125+
dwErrCode = ERROR_CAN_NOT_COMPLETE;
3126+
SFileCloseArchive(hMpq);
3127+
}
3128+
3129+
// Restore the original file locale
3130+
SFileSetLocale(LANG_NEUTRAL);
3131+
return dwErrCode;
3132+
}
3133+
30603134
static DWORD TestCreateArchive_TestGaps(LPCTSTR szPlainName)
30613135
{
30623136
TLogHelper Logger("TestCreateGaps", szPlainName);
@@ -3322,6 +3396,7 @@ static DWORD TestCreateArchive_FileFlagTest(LPCTSTR szPlainName)
33223396
// Create paths for local file to be added
33233397
CreateFullPathName(szFileName1, _countof(szFileName1), szDataFileDir, _T("new-file.exe"));
33243398
CreateFullPathName(szFileName2, _countof(szFileName2), szDataFileDir, _T("new-file.bin"));
3399+
SFileSetLocale(LANG_NEUTRAL);
33253400

33263401
// Create an empty file that will serve as holder for the MPQ
33273402
dwErrCode = CreateEmptyFile(&Logger, szPlainName, 0x100000, szFullPath);
@@ -4503,6 +4578,11 @@ int _tmain(int argc, TCHAR * argv[])
45034578
#endif
45044579

45054580
#ifdef TEST_MISC_MPQS
4581+
4582+
// Test creating of an archive the same way like MPQ Editor does
4583+
if(dwErrCode == ERROR_SUCCESS)
4584+
dwErrCode = TestRenameFile(_T("MPQ_2002_v1_StrongSignature.w3m"));
4585+
45064586
// Test creating of an archive the same way like MPQ Editor does
45074587
if(dwErrCode == ERROR_SUCCESS)
45084588
dwErrCode = TestCreateArchive_TestGaps(_T("StormLibTest_GapsTest.mpq"));

test/stormlib-test-001.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ CreateNewMpq (StormLibTest_简体中文.mpq) succeeded.
163163
CreateNewMpq (StormLibTest_الععربية.mpq) succeeded.
164164
CreateNewMpq (StormLibTest_NonStdNames.mpq) succeeded.
165165
CreateNewMpq (StormLibTest_MpqEditorTest.mpq) succeeded.
166+
TestRenameFile (MPQ_2002_v1_StrongSignature.w3m) succeeded.
166167
TestCreateGaps (StormLibTest_GapsTest.mpq) succeeded.
167168
TestCreateFull (StormLibTest_FileTableFull.mpq) succeeded.
168169
TestCreateFull (StormLibTest_FileTableFull.mpq) succeeded.

0 commit comments

Comments
 (0)