Skip to content

Commit 5fff423

Browse files
committed
Experimenting with an alternate way to attempt to fake our files as content
1 parent 5d9326d commit 5fff423

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

Source/Managers/ActivityMan.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
120120
writer->NewPropertyWithValue("Scene", modifiableScene.get());
121121

122122
// Get BITMAPS so save into our zip
123-
// I tired std::moving this into the function directly but threadpool really doesn't like that
123+
// I tried std::moving this into the function directly but threadpool really doesn't like that
124124
std::vector<SceneLayerInfo>* sceneLayerInfos = new std::vector<SceneLayerInfo>();
125125
*sceneLayerInfos = std::move(scene->GetCopiedSceneLayerBitmaps());
126126

@@ -141,7 +141,7 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
141141
}
142142

143143
const int defaultCompression = 6;
144-
zipOpenNewFileInZip(zippedSaveFile, (fileName + ".ini").c_str(), &zfi, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, defaultCompression);
144+
zipOpenNewFileInZip(zippedSaveFile, "Save.ini", &zfi, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, defaultCompression);
145145
zipWriteInFileInZip(zippedSaveFile, streamAsString.data(), streamAsString.size());
146146
zipCloseFileInZip(zippedSaveFile);
147147

@@ -164,7 +164,7 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
164164
size_t size;
165165
fmem_mem(&memStructure, &buffer, &size);
166166

167-
zipOpenNewFileInZip(zippedSaveFile, (fileName + " " + layerInfo.name + ".png").c_str(), &zfi, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, defaultCompression);
167+
zipOpenNewFileInZip(zippedSaveFile, ("Save " + layerInfo.name + ".png").c_str(), &zfi, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, defaultCompression);
168168
zipWriteInFileInZip(zippedSaveFile, static_cast<const char*>(buffer), size);
169169
zipCloseFileInZip(zippedSaveFile);
170170

@@ -191,9 +191,10 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
191191
bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
192192
m_SaveGameTask.wait();
193193

194-
std::string saveFilePath = g_PresetMan.GetFullModulePath(c_UserScriptedSavesModuleName) + "/" + fileName + ".ccsave";
194+
std::string filePath = g_PresetMan.GetFullModulePath(c_UserScriptedSavesModuleName) + "/" + fileName;
195195

196196
// load zip sav file
197+
std::string saveFilePath = filePath + ".ccsave";
197198
unzFile zippedSaveFile = unzOpen(saveFilePath.c_str());
198199
if (!zippedSaveFile) {
199200
RTEError::ShowMessageBox("Game loading failed! Make sure you have a saved game called \"" + fileName + "\"");
@@ -218,9 +219,9 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
218219
unzCloseCurrentFile(zippedSaveFile);
219220
};
220221

221-
unzipFileIntoBuffer(fileName + ".ini");
222+
unzipFileIntoBuffer("Save.ini");
222223

223-
Reader reader(std::make_unique<std::istringstream>(buffer), saveFilePath, true, nullptr, false);
224+
Reader reader(std::make_unique<std::istringstream>(buffer), filePath + "/Save.ini", true, nullptr, false);
224225

225226
std::unique_ptr<Scene> scene(std::make_unique<Scene>());
226227
std::unique_ptr<GAScripted> activity(std::make_unique<GAScripted>());
@@ -247,41 +248,47 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
247248

248249
int numberOfTeams = activity->m_TeamCount;
249250

250-
// SetSceneToLoad() doesn't Clone(), but when the Activity starts, it will eventually call LoadScene(), which does a Clone() of scene internally.
251-
g_SceneMan.SetSceneToLoad(scene.get(), true, true);
252-
// Saved Scenes get their presetname set to their filename to ensure they're separate from the preset Scene they're based off of.
253-
// However, saving a game you've already saved will end up with its OriginalScenePresetName set to the filename, which will screw up restarting the Activity, so we set its PresetName here.
254-
scene->SetPresetName(originalScenePresetName);
255-
// For starting Activity, we need to directly clone the Activity we want to start.
256-
StartActivity(dynamic_cast<GAScripted*>(activity->Clone()));
257-
// When this method exits, our Scene object will be destroyed, which will cause problems if you try to restart it. To avoid this, set the Scene to load to the preset object with the same name.
258-
g_SceneMan.SetSceneToLoad(originalScenePresetName, placeObjectsIfSceneIsRestarted, placeUnitsIfSceneIsRestarted);
259-
260-
// Replace our scene images with the ones from the zip
261-
std::vector<SceneLayerInfo> layerInfos;
262-
251+
// Manually load all our bitmaps into our cache so the activity skips looking for the file and just gets it directly from us
263252
PALETTE palette;
264253
get_palette(palette);
265254

266-
unzipFileIntoBuffer(fileName + " Mat.png");
267-
layerInfos.emplace_back("Mat", std::unique_ptr<BITMAP>(load_memory_png(buffer, info.uncompressed_size, palette)));
255+
unzipFileIntoBuffer("Save Mat.png");
256+
ContentFile::ManuallyLoadDataBitmap(filePath + "/Save Mat.png", load_memory_png(buffer, info.uncompressed_size, palette));
268257
free(buffer);
269258

270-
unzipFileIntoBuffer(fileName + " FG.png");
271-
layerInfos.emplace_back("FG", std::unique_ptr<BITMAP>(load_memory_png(buffer, info.uncompressed_size, palette)));
259+
unzipFileIntoBuffer("Save FG.png");
260+
ContentFile::ManuallyLoadDataBitmap(filePath + "/Save FG.png", load_memory_png(buffer, info.uncompressed_size, palette));
272261
free(buffer);
273262

274-
unzipFileIntoBuffer(fileName + " BG.png");
275-
layerInfos.emplace_back("BG", std::unique_ptr<BITMAP>(load_memory_png(buffer, info.uncompressed_size, palette)));
263+
unzipFileIntoBuffer("Save BG.png");
264+
ContentFile::ManuallyLoadDataBitmap(filePath + "/Save BG.png", load_memory_png(buffer, info.uncompressed_size, palette));
276265
free(buffer);
277266

278267
for (int i = 0; i < numberOfTeams; ++i) {
279-
unzipFileIntoBuffer(fileName + std::format(" T%i.png", i));
280-
layerInfos.emplace_back(std::format("T%i", i), std::unique_ptr<BITMAP>(load_memory_png(buffer, info.uncompressed_size, palette)));
268+
unzipFileIntoBuffer(std::format("Save UST%i.png", i));
269+
ContentFile::ManuallyLoadDataBitmap(filePath + std::format("/Save UST%i", i), load_memory_png(buffer, info.uncompressed_size, palette));
281270
free(buffer);
282271
}
283272

284-
g_SceneMan.GetScene()->ConstructSceneLayersFromBitmaps(std::move(layerInfos));
273+
unzClose(zippedSaveFile);
274+
275+
// SetSceneToLoad() doesn't Clone(), but when the Activity starts, it will eventually call LoadScene(), which does a Clone() of scene internally.
276+
g_SceneMan.SetSceneToLoad(scene.get(), true, true);
277+
// Saved Scenes get their presetname set to their filename to ensure they're separate from the preset Scene they're based off of.
278+
// However, saving a game you've already saved will end up with its OriginalScenePresetName set to the filename, which will screw up restarting the Activity, so we set its PresetName here.
279+
scene->SetPresetName(originalScenePresetName);
280+
// For starting Activity, we need to directly clone the Activity we want to start.
281+
StartActivity(dynamic_cast<GAScripted*>(activity->Clone()));
282+
// When this method exits, our Scene object will be destroyed, which will cause problems if you try to restart it. To avoid this, set the Scene to load to the preset object with the same name.
283+
g_SceneMan.SetSceneToLoad(originalScenePresetName, placeObjectsIfSceneIsRestarted, placeUnitsIfSceneIsRestarted);
284+
285+
// Clear out the cache, we don't need it anymore (and don't want a cache reload to look for this file thinking it really exists)
286+
ContentFile::ManuallyClearDataBitmap(filePath + "/Save Mat.png");
287+
ContentFile::ManuallyClearDataBitmap(filePath + "/Save FG.png");
288+
ContentFile::ManuallyClearDataBitmap(filePath + "/Save BG.png");
289+
for (int i = 0; i < numberOfTeams; ++i) {
290+
ContentFile::ManuallyClearDataBitmap(filePath + std::format("/Save UST%i.png", i));
291+
}
285292

286293
g_ConsoleMan.PrintString("SYSTEM: Game \"" + fileName + "\" loaded!");
287294

Source/System/ContentFile.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ void ContentFile::ReadAndStoreBMPFileInfo(FILE* imageFile) {
182182
}
183183
}
184184

185+
void ContentFile::ManuallyLoadDataBitmap(const std::string& filePath, BITMAP* bitmap, int conversionMode) {
186+
const int bitDepth = conversionMode == COLORCONV_8_TO_32 ? BitDepths::ThirtyTwo : BitDepths::Eight;
187+
s_LoadedBitmaps[bitDepth].try_emplace(filePath, bitmap);
188+
}
189+
190+
void ContentFile::ManuallyClearDataBitmap(const std::string& filePath, int conversionMode) {
191+
const int bitDepth = conversionMode == COLORCONV_8_TO_32 ? BitDepths::ThirtyTwo : BitDepths::Eight;
192+
std::unordered_map<std::string, BITMAP*>::iterator foundBitmap = s_LoadedBitmaps[bitDepth].find(filePath);
193+
if (foundBitmap != s_LoadedBitmaps[bitDepth].end()) {
194+
delete (*foundBitmap).second;
195+
s_LoadedBitmaps[bitDepth].erase(filePath);
196+
}
197+
}
198+
185199
void ContentFile::ReloadAllBitmaps() {
186200
for (const std::unordered_map<std::string, BITMAP*>& bitmapCache: s_LoadedBitmaps) {
187201
for (const auto& [filePath, oldBitmap]: bitmapCache) {

Source/System/ContentFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ namespace RTE {
115115
#pragma endregion
116116

117117
#pragma region Data Handling
118+
static void ManuallyLoadDataBitmap(const std::string& filePath, BITMAP* bitmap, int conversionMode = 0);
119+
static void ManuallyClearDataBitmap(const std::string& filePath, int conversionMode = 0);
120+
118121
/// Reloads all BITMAPs in the cache from disk, allowing any changes to be reflected at runtime.
119122
static void ReloadAllBitmaps();
120123

0 commit comments

Comments
 (0)