MCPcopy Create free account
hub / github.com/diasurgical/devilution / OpenPatchedFile

Function OpenPatchedFile

3rdParty/StormLib/src/SFileOpenFileEx.cpp:102–163  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

100}
101
102bool OpenPatchedFile(HANDLE hMpq, const char * szFileName, HANDLE * PtrFile)
103{
104 TMPQArchive * haBase = NULL;
105 TMPQArchive * ha = (TMPQArchive *)hMpq;
106 TFileEntry * pFileEntry;
107 TMPQFile * hfPatch; // Pointer to patch file
108 TMPQFile * hfBase = NULL; // Pointer to base open file
109 TMPQFile * hf = NULL;
110 HANDLE hPatchFile;
111 char szNameBuffer[MAX_PATH];
112
113 // First of all, find the latest archive where the file is in base version
114 // (i.e. where the original, unpatched version of the file exists)
115 while(ha != NULL)
116 {
117 // If the file is there, then we remember the archive
118 pFileEntry = GetFileEntryExact(ha, GetPatchFileName(ha, szFileName, szNameBuffer), 0, NULL);
119 if(pFileEntry != NULL && (pFileEntry->dwFlags & MPQ_FILE_PATCH_FILE) == 0)
120 haBase = ha;
121
122 // Move to the patch archive
123 ha = ha->haPatch;
124 }
125
126 // If we couldn't find the base file in any of the patches, it doesn't exist
127 if((ha = haBase) == NULL)
128 {
129 SetLastError(ERROR_FILE_NOT_FOUND);
130 return false;
131 }
132
133 // Now open the base file
134 if(SFileOpenFileEx((HANDLE)ha, GetPatchFileName(ha, szFileName, szNameBuffer), SFILE_OPEN_BASE_FILE, (HANDLE *)&hfBase))
135 {
136 // The file must be a base file, i.e. without MPQ_FILE_PATCH_FILE
137 assert((hfBase->pFileEntry->dwFlags & MPQ_FILE_PATCH_FILE) == 0);
138 hf = hfBase;
139
140 // Now open all patches and attach them on top of the base file
141 for(ha = ha->haPatch; ha != NULL; ha = ha->haPatch)
142 {
143 // Prepare the file name with a correct prefix
144 if(SFileOpenFileEx((HANDLE)ha, GetPatchFileName(ha, szFileName, szNameBuffer), SFILE_OPEN_BASE_FILE, &hPatchFile))
145 {
146 // Remember the new version
147 hfPatch = (TMPQFile *)hPatchFile;
148
149 // We should not find patch file
150 assert((hfPatch->pFileEntry->dwFlags & MPQ_FILE_PATCH_FILE) != 0);
151
152 // Attach the patch to the base file
153 hf->hfPatch = hfPatch;
154 hf = hfPatch;
155 }
156 }
157 }
158
159 // Give the updated base MPQ

Callers 1

SFileOpenFileExFunction · 0.85

Calls 4

GetFileEntryExactFunction · 0.85
GetPatchFileNameFunction · 0.85
SetLastErrorFunction · 0.85
SFileOpenFileExFunction · 0.85

Tested by

no test coverage detected