1111#include " StdInc.h"
1212#include " ../../vendor/unrar/dll.hpp"
1313
14- CFileGenerator::CFileGenerator (const SString& strTarget, const SString& strTargetMd5 , const std::vector<CFileGenerator::SResetItem>& targetResetList,
14+ CFileGenerator::CFileGenerator (const SString& strTarget, const SString& strRequiredTargetMd5 , const std::vector<CFileGenerator::SResetItem>& targetResetList,
1515 const SString& strPatchBase, const SString& strPatchDiff)
16- : m_strTarget(strTarget), m_strTargetMd5(strTargetMd5), m_targetResetList(targetResetList), m_strPatchBase(strPatchBase), m_strPatchDiff(strPatchDiff)
16+ : m_strTarget(strTarget),
17+ m_strRequiredTargetMd5(strRequiredTargetMd5),
18+ m_targetResetList(targetResetList),
19+ m_strPatchBase(strPatchBase),
20+ m_strPatchDiff(strPatchDiff)
1721{
1822}
1923
@@ -33,7 +37,7 @@ CFileGenerator::EResult CFileGenerator::CheckTarget(const SString& strTarget)
3337 // Load into a buffer
3438 CBuffer buffer;
3539 if (!buffer.LoadFromFile (strTarget))
36- return EResult::TargetLoadError;
40+ return RecordError ( EResult::TargetLoadError, strTarget) ;
3741
3842 // List of pokes to restore
3943 CBufferWriteStream stream (buffer);
@@ -47,16 +51,26 @@ CFileGenerator::EResult CFileGenerator::CheckTarget(const SString& strTarget)
4751 }
4852 }
4953
50- if (GenerateHashHexString (EHashFunctionType::MD5, buffer.GetData (), buffer.GetSize ()) != m_strTargetMd5)
51- return EResult::TargetHashIncorrect;
54+ m_strCurrentTargetMd5 = GenerateHashHexString (EHashFunctionType::MD5, buffer.GetData (), buffer.GetSize ());
55+ if (m_strCurrentTargetMd5 != m_strRequiredTargetMd5)
56+ return RecordError (EResult::TargetHashIncorrect, strTarget);
5257 return EResult::Success;
5358}
5459
60+ // ////////////////////////////////////////////////////////
61+ // Return actual MD5 of target file
62+ // ////////////////////////////////////////////////////////
63+ SString CFileGenerator::GetCurrentTargetMd5 ()
64+ {
65+ return m_strCurrentTargetMd5;
66+ }
67+
5568// ////////////////////////////////////////////////////////
5669// Generate target file
5770// ////////////////////////////////////////////////////////
5871CFileGenerator::EResult CFileGenerator::GenerateFile ()
5972{
73+ ClearErrorRecords ();
6074 // Base + Diff => rar archive containing new target file
6175 SString strTmpArchive = m_strPatchDiff + " .tmp" ;
6276 EResult result = ApplyPatchFile (m_strPatchBase, m_strPatchDiff, strTmpArchive);
@@ -79,7 +93,7 @@ CFileGenerator::EResult CFileGenerator::GenerateFile()
7993 }
8094 else
8195 {
82- result = EResult::TargetMoveError;
96+ result = RecordError ( EResult::TargetMoveError, strTmpNewTarget, m_strTarget) ;
8397 }
8498 }
8599 }
@@ -98,8 +112,10 @@ CFileGenerator::EResult CFileGenerator::ApplyPatchFile(const SString& strPatchBa
98112 std::vector<char > diffData;
99113 FileLoad (strPatchBase, baseData);
100114 FileLoad (strPatchDiff, diffData);
101- if (baseData.empty () || diffData.empty ())
102- return EResult::PatchInputError;
115+ if (baseData.empty ())
116+ return RecordError (EResult::PatchInputError, strPatchBase);
117+ if (diffData.empty ())
118+ return RecordError (EResult::PatchInputError, strPatchDiff);
103119
104120 // Reuncompression using future delta system
105121 uint uiPos = 0 ;
@@ -110,7 +126,7 @@ CFileGenerator::EResult CFileGenerator::ApplyPatchFile(const SString& strPatchBa
110126 }
111127
112128 if (!FileSave (strOutputFile, &diffData[0 ], diffData.size ()))
113- return EResult::PatchOutputError;
129+ return RecordError ( EResult::PatchOutputError, strOutputFile) ;
114130 return EResult::Success;
115131}
116132
@@ -119,24 +135,60 @@ CFileGenerator::EResult CFileGenerator::ApplyPatchFile(const SString& strPatchBa
119135// ////////////////////////////////////////////////////////
120136CFileGenerator::EResult CFileGenerator::UnrarFile (const SString& strArchive, const SString& strOutputFile)
121137{
138+ WString wstrArchive = FromUTF8 (strArchive);
139+ WString wstrOutputFile = FromUTF8 (strOutputFile);
122140 // Open archive
123- RAROpenArchiveData archiveData = {};
124- archiveData.ArcName = (char *)*strArchive ;
141+ RAROpenArchiveDataEx archiveData = {};
142+ archiveData.ArcNameW = (wchar_t *)*wstrArchive ;
125143 archiveData.OpenMode = RAR_OM_EXTRACT;
126- HANDLE hArcData = RAROpenArchive (&archiveData);
144+ HANDLE hArcData = RAROpenArchiveEx (&archiveData);
127145 if (!hArcData)
128- return EResult::ArchiveOpenError;
146+ return RecordError ( EResult::ArchiveOpenError, strArchive) ;
129147
130148 // Extract first file
131149 EResult result = EResult::ArchiveExtractError;
132150 RARHeaderData headerData = {};
133151 if (RARReadHeader (hArcData, &headerData) == 0 )
134152 {
135- if (RARProcessFile (hArcData, RAR_EXTRACT, nullptr , (char *)*strOutputFile ) == 0 )
153+ if (RARProcessFileW (hArcData, RAR_EXTRACT, nullptr , (wchar_t *)*wstrOutputFile ) == 0 )
136154 {
137155 result = EResult::Success;
138156 }
139157 }
140158 RARCloseArchive (hArcData);
159+
160+ if (result != EResult::Success)
161+ return RecordError (result, strArchive, strOutputFile);
141162 return result;
142163}
164+
165+ // ////////////////////////////////////////////////////////
166+ // Save error info for later
167+ // ////////////////////////////////////////////////////////
168+ CFileGenerator::EResult CFileGenerator::RecordError (CFileGenerator::EResult code, const SString& strContext, const SString& strContext2)
169+ {
170+ m_errorInfoList.push_back ({code, strContext, strContext2});
171+ return code;
172+ }
173+
174+ // ////////////////////////////////////////////////////////
175+ // Remove all error records
176+ // ////////////////////////////////////////////////////////
177+ void CFileGenerator::ClearErrorRecords ()
178+ {
179+ m_errorInfoList.clear ();
180+ }
181+
182+ // ////////////////////////////////////////////////////////
183+ // Return all error records as a string
184+ // ////////////////////////////////////////////////////////
185+ SString CFileGenerator::GetErrorRecords ()
186+ {
187+ SString strStatus;
188+ for (const auto & item : m_errorInfoList)
189+ {
190+ strStatus += SString (" [%d-%s-%s]" , item.code , *item.strContext , *item.strContext2 );
191+ }
192+ ClearErrorRecords ();
193+ return strStatus;
194+ }
0 commit comments