@@ -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+
10521071static 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+
30603134static 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" ));
0 commit comments