Skip to content

Commit 18313b2

Browse files
committed
Save/load ini- need to do pngs next
1 parent 4de5aa1 commit 18313b2

File tree

4 files changed

+56
-26
lines changed

4 files changed

+56
-26
lines changed

Source/Managers/ActivityMan.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,6 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
8484
return false;
8585
}
8686

87-
// THIS BLOCK OF CODE NEEDS ZIPPIFIED!
88-
/*
89-
// TODO, save to a zip instead of a directory
90-
std::filesystem::create_directory(g_PresetMan.GetFullModulePath(c_UserScriptedSavesModuleName) + "/" + fileName);
91-
92-
if (scene->SaveData(c_UserScriptedSavesModuleName + "/" + fileName + "/Save") < 0) {
93-
// This print is actually pointless because game will abort if it fails to save layer bitmaps. It stays here for now because in reality the game doesn't properly abort if the layer bitmaps fail to save. It is what it is.
94-
g_ConsoleMan.PrintString("ERROR: Failed to save scene bitmaps while saving!");
95-
return false;
96-
}
97-
*/
98-
9987
// We need a copy of our scene, because we have to do some fixup to remove PLACEONLOAD items and only keep the current MovableMan state.
10088
std::unique_ptr<Scene> modifiableScene(dynamic_cast<Scene*>(scene->Clone()));
10189

@@ -203,16 +191,33 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
203191
bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
204192
m_SaveGameTask.wait();
205193

206-
// TODO- this needs to load a zip!
194+
std::string saveFilePath = g_PresetMan.GetFullModulePath(c_UserScriptedSavesModuleName) + "/" + fileName + ".ccsave";
207195

208-
std::string saveFilePath = g_PresetMan.GetFullModulePath(c_UserScriptedSavesModuleName) + "/" + fileName + "/Save.ini";
209-
210-
if (!std::filesystem::exists(saveFilePath)) {
196+
// load zip sav file
197+
unzFile zippedSaveFile = unzOpen(saveFilePath.c_str());
198+
if (!zippedSaveFile) {
211199
RTEError::ShowMessageBox("Game loading failed! Make sure you have a saved game called \"" + fileName + "\"");
212200
return false;
213201
}
214202

215-
Reader reader(saveFilePath, true, nullptr, false);
203+
unz_file_info info;
204+
char* buffer;
205+
206+
unzLocateFile(zippedSaveFile, (fileName + ".ini").c_str(), nullptr);
207+
unzOpenCurrentFile(zippedSaveFile);
208+
unzGetCurrentFileInfo(zippedSaveFile, &info, nullptr, 0, nullptr, 0, nullptr, 0);
209+
210+
buffer = (char*)malloc(info.uncompressed_size);
211+
if (!buffer) {
212+
// If this ever hits I've lost all faith in modern OSes, but alas when one is writing C, one must dance along
213+
RTEError::ShowMessageBox("Catastrophic failure! Failed to allocate memory for savegame");
214+
return false;
215+
}
216+
217+
unzReadCurrentFile(zippedSaveFile, buffer, info.uncompressed_size);
218+
unzCloseCurrentFile(zippedSaveFile);
219+
220+
Reader reader(std::make_unique<std::istringstream>(buffer), saveFilePath, true, nullptr, false);
216221

217222
std::unique_ptr<Scene> scene(std::make_unique<Scene>());
218223
std::unique_ptr<GAScripted> activity(std::make_unique<GAScripted>());
@@ -246,6 +251,8 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
246251
g_SceneMan.SetSceneToLoad(originalScenePresetName, placeObjectsIfSceneIsRestarted, placeUnitsIfSceneIsRestarted);
247252

248253
g_ConsoleMan.PrintString("SYSTEM: Game \"" + fileName + "\" loaded!");
254+
255+
free(buffer);
249256
return true;
250257
}
251258

Source/System/Reader.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ Reader::Reader(const std::string& fileName, bool overwrites, const ProgressCallb
3333
Create(fileName, overwrites, progressCallback, failOK);
3434
}
3535

36-
Reader::Reader(std::unique_ptr<std::istream>&& stream, bool overwrites, const ProgressCallback& progressCallback, bool failOK) {
36+
Reader::Reader(std::unique_ptr<std::istream>&& stream, const std::string& fileName, bool overwrites, const ProgressCallback& progressCallback, bool failOK) {
3737
Clear();
38-
Create(std::move(stream), overwrites, progressCallback, failOK);
38+
Create(std::move(stream), fileName, overwrites, progressCallback, failOK);
3939
}
4040

4141
int Reader::Create(const std::string& fileName, bool overwrites, const ProgressCallback& progressCallback, bool failOK) {
4242
if (fileName.empty()) {
4343
return -1;
4444
}
45-
45+
4646
if (m_NonModulePath) {
4747
m_FilePath = std::filesystem::path(fileName).generic_string();
4848
// Associate non-module paths with Base to prevent implosions when dealing with creating Entities.
@@ -56,17 +56,36 @@ int Reader::Create(const std::string& fileName, bool overwrites, const ProgressC
5656
m_DataModuleName = g_PresetMan.GetModuleNameFromPath(m_FilePath);
5757
m_DataModuleID = g_PresetMan.GetModuleID(m_DataModuleName);
5858
}
59-
60-
return Create(std::make_unique<std::ifstream>(m_FilePath), overwrites, progressCallback, failOK);
59+
60+
return Create(std::make_unique<std::ifstream>(m_FilePath), fileName, overwrites, progressCallback, failOK);
6161
}
6262

63-
int Reader::Create(std::unique_ptr<std::istream>&& stream, bool overwrites, const ProgressCallback& progressCallback, bool failOK) {
63+
int Reader::Create(std::unique_ptr<std::istream>&& stream, const std::string& fileName, bool overwrites, const ProgressCallback& progressCallback, bool failOK) {
64+
// We redundantly do this following block of code in both constructors, which feels really ugly and lazy
65+
if (fileName.empty()) {
66+
return -1;
67+
}
68+
69+
if (m_NonModulePath) {
70+
m_FilePath = std::filesystem::path(fileName).generic_string();
71+
// Associate non-module paths with Base to prevent implosions when dealing with creating Entities.
72+
m_DataModuleName = "Base.rte";
73+
m_DataModuleID = 0;
74+
} else {
75+
m_FilePath = g_PresetMan.GetFullModulePath(fileName);
76+
77+
// Extract the file name and module name from the path
78+
m_FileName = m_FilePath.substr(m_FilePath.find_last_of("/\\") + 1);
79+
m_DataModuleName = g_PresetMan.GetModuleNameFromPath(m_FilePath);
80+
m_DataModuleID = g_PresetMan.GetModuleID(m_DataModuleName);
81+
}
82+
6483
m_CanFail = failOK;
6584

6685
m_Stream = std::move(stream);
6786

6887
if (!m_CanFail) {
69-
RTEAssert(System::PathExistsCaseSensitive(m_FilePath) && m_Stream->good(), "Failed to open data file \"" + m_FilePath + "\"!");
88+
RTEAssert(m_Stream->good(), "Failed to open data file \"" + m_FilePath + "\"!");
7089
}
7190

7291
m_OverwriteExisting = overwrites;

Source/System/Reader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace RTE {
3131
/// @param overwrites Whether object definitions read here overwrite existing ones with the same names.
3232
/// @param progressCallback A function pointer to a function that will be called and sent a string with information about the progress of this Reader's reading.
3333
/// @param failOK Whether it's ok for the file to not be there, ie we're only trying to open, and if it's not there, then fail silently.
34-
Reader(std::unique_ptr<std::istream>&& stream, bool overwrites = false, const ProgressCallback& progressCallback = nullptr, bool failOK = false);
34+
Reader(std::unique_ptr<std::istream>&& stream, const std::string& fileName, bool overwrites = false, const ProgressCallback& progressCallback = nullptr, bool failOK = false);
3535

3636
/// Makes the Reader object ready for use.
3737
/// @param fileName Path to the file to open for reading. If the file doesn't exist the stream will fail to open.
@@ -47,7 +47,7 @@ namespace RTE {
4747
/// @param progressCallback A function pointer to a function that will be called and sent a string with information about the progress of this Reader's reading.
4848
/// @param failOK Whether it's ok for the file to not be there, ie we're only trying to open, and if it's not there, then fail silently.
4949
/// @return An error return value signaling success or any particular failure. Anything below 0 is an error signal.
50-
int Create(std::unique_ptr<std::istream>&& stream, bool overwrites = false, const ProgressCallback& progressCallback = nullptr, bool failOK = false);
50+
int Create(std::unique_ptr<std::istream>&& stream, const std::string& fileName, bool overwrites = false, const ProgressCallback& progressCallback = nullptr, bool failOK = false);
5151
#pragma endregion
5252

5353
#pragma region Getters and Setters

imgui.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[Window][Debug##Default]
2+
Pos=60,60
3+
Size=400,400
4+

0 commit comments

Comments
 (0)