Skip to content

Commit 8d761b8

Browse files
authored
Merge pull request #3435 from Ghabry/maniac-control-global-save
Rewrite ControlGlobalSave
2 parents 30e1426 + 1a96d92 commit 8d761b8

8 files changed

Lines changed: 180 additions & 116 deletions

src/game_interpreter.cpp

Lines changed: 9 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,18 @@
2626
#include <cassert>
2727
#include "game_interpreter.h"
2828
#include "async_handler.h"
29-
#include "audio.h"
3029
#include "game_dynrpg.h"
3130
#include "filefinder.h"
3231
#include "game_destiny.h"
3332
#include "game_map.h"
3433
#include "game_event.h"
35-
#include "game_enemyparty.h"
36-
#include "game_ineluki.h"
3734
#include "game_player.h"
3835
#include "game_targets.h"
3936
#include "game_switches.h"
4037
#include "game_variables.h"
4138
#include "game_party.h"
4239
#include "game_actors.h"
40+
#include "game_strings.h"
4341
#include "game_system.h"
4442
#include "game_message.h"
4543
#include "game_pictures.h"
@@ -70,7 +68,6 @@
7068
#include "transition.h"
7169
#include "baseui.h"
7270
#include "algo.h"
73-
#include "rand.h"
7471

7572
using namespace Game_Interpreter_Shared;
7673

@@ -4908,91 +4905,21 @@ bool Game_Interpreter::CommandManiacControlGlobalSave(lcf::rpg::EventCommand con
49084905

49094906
int operation = com.parameters[0];
49104907

4911-
auto load_global_save = [&]() {
4912-
Main_Data::global_save_opened = true;
4913-
4914-
// Load
4915-
auto lgs = FileFinder::Save().OpenFile("Save.lgs");
4916-
if (!lgs) {
4917-
return;
4918-
}
4919-
4920-
lcf::LcfReader reader(lgs);
4921-
std::string header;
4922-
reader.ReadString(header, reader.ReadInt());
4923-
if (header.length() != 13 || header != "LcfGlobalSave") {
4924-
Output::Debug("This is not a valid global save.");
4925-
return;
4926-
}
4927-
4928-
lcf::LcfReader::Chunk chunk;
4929-
4930-
while (!reader.Eof()) {
4931-
chunk.ID = reader.ReadInt();
4932-
chunk.length = reader.ReadInt();
4933-
switch (chunk.ID) {
4934-
case 1: {
4935-
Game_Switches::Switches_t switches;
4936-
reader.Read(switches, chunk.length);
4937-
Main_Data::game_switches_global->SetData(std::move(switches));
4938-
break;
4939-
}
4940-
case 2: {
4941-
Game_Variables::Variables_t variables;
4942-
reader.Read(variables, chunk.length);
4943-
Main_Data::game_variables_global->SetData(std::move(variables));
4944-
break;
4945-
}
4946-
default:
4947-
reader.Skip(chunk, "CommandManiacControlGlobalSave");
4948-
}
4949-
}
4950-
};
4951-
49524908
if (operation == 0) {
4953-
// Open
4954-
load_global_save();
4909+
// Open: Fill Global Save with data from Save.lgs
4910+
// Does nothing when already opened
4911+
ManiacPatch::GlobalSave::Load();
49554912
} else if (operation == 1) {
49564913
// Close
4957-
Main_Data::global_save_opened = false;
4914+
// Marks the file as closed and does nothing
4915+
ManiacPatch::GlobalSave::Close();
49584916
} else if (operation == 2 || operation == 3) {
49594917
// 2: Save (write to file)
49604918
// 3: Save and Close
4961-
if (!Main_Data::global_save_opened) {
4962-
return true;
4963-
}
4964-
4965-
auto savelgs_name = FileFinder::Save().FindFile("Save.lgs");
4966-
if (savelgs_name.empty()) {
4967-
savelgs_name = "Save.lgs";
4968-
}
4969-
4970-
auto lgs_out = FileFinder::Save().OpenOutputStream(savelgs_name);
4971-
if (!lgs_out) {
4972-
Output::Warning("Maniac ControlGlobalSave: Saving failed");
4973-
return true;
4974-
}
4975-
4976-
lcf::LcfWriter writer(lgs_out, lcf::EngineVersion::e2k3);
4977-
writer.WriteInt(13);
4978-
const std::string header = "LcfGlobalSave";
4979-
writer.Write(header);
4980-
writer.WriteInt(1);
4981-
writer.WriteInt(Main_Data::game_switches_global->GetSize());
4982-
writer.Write(Main_Data::game_switches_global->GetData());
4983-
writer.WriteInt(2);
4984-
writer.WriteInt(Main_Data::game_variables_global->GetSize() * sizeof(int32_t));
4985-
writer.Write(Main_Data::game_variables_global->GetData());
4986-
4987-
AsyncHandler::SaveFilesystem();
4988-
4989-
if (operation == 3) {
4990-
Main_Data::global_save_opened = false;
4991-
}
4919+
ManiacPatch::GlobalSave::Save(operation == 3);
49924920
} else if (operation == 4 || operation == 5) {
4993-
if (!Main_Data::global_save_opened) {
4994-
load_global_save();
4995-
}
4921+
// Reload the file when it was already closed
4922+
ManiacPatch::GlobalSave::Load();
49964923

49974924
int type = com.parameters[2];
49984925
int game_state_idx = ValueOrVariableBitfield(com.parameters[1], 0, com.parameters[3]);

src/game_interpreter_shared.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,12 @@
1616
*/
1717

1818
#include "game_interpreter_shared.h"
19-
#include "game_actors.h"
20-
#include "game_enemyparty.h"
21-
#include "game_ineluki.h"
22-
#include "game_map.h"
23-
#include "game_party.h"
24-
#include "game_player.h"
19+
#include "game_strings.h"
2520
#include "game_switches.h"
26-
#include "game_system.h"
21+
#include "game_variables.h"
2722
#include "maniac_patch.h"
2823
#include "main_data.h"
29-
#include "output.h"
3024
#include "player.h"
31-
#include "rand.h"
32-
#include "util_macro.h"
33-
#include "utils.h"
34-
#include "audio.h"
35-
#include "baseui.h"
36-
#include <cmath>
3725
#include <cstdint>
3826
#include <lcf/rpg/savepartylocation.h>
3927
#include <lcf/reader_util.h>

src/main_data.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
// Headers
1919
#include <cstdlib>
2020
#include "main_data.h"
21-
#include "filefinder.h"
2221
#include "filefinder_rtp.h"
23-
#include "filesystem.h"
2422
#include "game_destiny.h"
2523
#include "game_system.h"
2624
#include "game_actors.h"
@@ -38,10 +36,7 @@
3836
#include "game_targets.h"
3937
#include "game_quit.h"
4038
#include "game_windows.h"
41-
#include "font.h"
42-
#include "player.h"
4339
#include "system.h"
44-
#include "output.h"
4540

4641
#ifndef _WIN32
4742
# include <unistd.h>
@@ -75,7 +70,6 @@ namespace Main_Data {
7570
std::unique_ptr<Game_DynRpg> game_dynrpg;
7671
std::unique_ptr<Game_Ineluki> game_ineluki;
7772
std::unique_ptr<Game_Destiny> game_destiny;
78-
bool global_save_opened = false;
7973
std::unique_ptr<Game_Switches> game_switches_global;
8074
std::unique_ptr<Game_Variables> game_variables_global;
8175

@@ -132,7 +126,6 @@ void Main_Data::Cleanup() {
132126
game_dynrpg.reset();
133127
game_ineluki.reset();
134128
game_destiny.reset();
135-
global_save_opened = false;
136129
game_switches_global.reset();
137130
game_variables_global.reset();
138131
}

src/maniac_patch.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "maniac_patch.h"
1919

20+
#include "filesystem_stream.h"
2021
#include "input.h"
2122
#include "game_actors.h"
2223
#include "game_interpreter_control_variables.h"
@@ -29,7 +30,9 @@
2930
#include "output.h"
3031
#include "player.h"
3132

33+
#include <lcf/reader_lcf.h>
3234
#include <lcf/reader_util.h>
35+
#include <lcf/writer_lcf.h>
3336
#include <vector>
3437

3538
/*
@@ -117,6 +120,8 @@ namespace {
117120
Divmul,
118121
Between
119122
};
123+
124+
bool global_save_opened = false;
120125
}
121126

122127
struct ProcessAssignmentRet {
@@ -828,3 +833,111 @@ std::string_view ManiacPatch::GetLcfDescription(int data_type, int id, bool is_d
828833
Output::Warning("GetLcfDescription: Unsupported data_type {} {}", data_type, id);
829834
return {};
830835
}
836+
837+
bool ManiacPatch::GlobalSave::Load() {
838+
if (!Player::IsPatchManiac()) {
839+
return true;
840+
}
841+
842+
if (global_save_opened) {
843+
return true;
844+
}
845+
846+
// Even consider it opened when the file is missing
847+
// It will be created on Save
848+
global_save_opened = true;
849+
850+
auto lgs_in = FileFinder::Save().OpenFile("Save.lgs");
851+
if (!lgs_in) {
852+
return false;
853+
}
854+
855+
return Load(lgs_in);
856+
}
857+
858+
bool ManiacPatch::GlobalSave::Load(Filesystem_Stream::InputStream& lgs_in) {
859+
if (!lgs_in) {
860+
return false;
861+
}
862+
863+
lcf::LcfReader reader(lgs_in);
864+
std::string header;
865+
reader.ReadString(header, reader.ReadInt());
866+
if (header.length() != 13 || header != "LcfGlobalSave") {
867+
Output::Debug("This is not a valid global save.");
868+
return false;
869+
}
870+
871+
lcf::LcfReader::Chunk chunk;
872+
873+
while (!reader.Eof()) {
874+
chunk.ID = reader.ReadInt();
875+
chunk.length = reader.ReadInt();
876+
switch (chunk.ID) {
877+
case 1: {
878+
Game_Switches::Switches_t switches;
879+
reader.Read(switches, chunk.length);
880+
Main_Data::game_switches_global->SetData(std::move(switches));
881+
break;
882+
}
883+
case 2: {
884+
Game_Variables::Variables_t variables;
885+
reader.Read(variables, chunk.length);
886+
Main_Data::game_variables_global->SetData(std::move(variables));
887+
break;
888+
}
889+
default:
890+
reader.Skip(chunk, "CommandManiacControlGlobalSave");
891+
}
892+
}
893+
894+
return true;
895+
}
896+
897+
bool ManiacPatch::GlobalSave::Save(bool close_global_save) {
898+
if (!Player::IsPatchManiac()) {
899+
return true;
900+
}
901+
902+
if (!global_save_opened) {
903+
return true;
904+
}
905+
906+
auto savelgs_name = FileFinder::Save().FindFile("Save.lgs");
907+
if (savelgs_name.empty()) {
908+
savelgs_name = "Save.lgs";
909+
}
910+
911+
auto lgs_out = FileFinder::Save().OpenOutputStream(savelgs_name);
912+
if (!Save(lgs_out)) {
913+
Output::Warning("Maniac ControlGlobalSave: Saving failed");
914+
return false;
915+
}
916+
917+
global_save_opened = !close_global_save;
918+
919+
AsyncHandler::SaveFilesystem();
920+
return true;
921+
}
922+
923+
bool ManiacPatch::GlobalSave::Save(Filesystem_Stream::OutputStream& lgs_out) {
924+
if (!lgs_out) {
925+
return false;
926+
}
927+
928+
lcf::LcfWriter writer(lgs_out, lcf::EngineVersion::e2k3);
929+
writer.WriteInt(13);
930+
const std::string header = "LcfGlobalSave";
931+
writer.Write(header);
932+
writer.WriteInt(1);
933+
writer.WriteInt(Main_Data::game_switches_global->GetSize());
934+
writer.Write(Main_Data::game_switches_global->GetData());
935+
writer.WriteInt(2);
936+
writer.WriteInt(Main_Data::game_variables_global->GetSize() * sizeof(int32_t));
937+
writer.Write(Main_Data::game_variables_global->GetData());
938+
return true;
939+
}
940+
941+
void ManiacPatch::GlobalSave::Close() {
942+
global_save_opened = false;
943+
}

0 commit comments

Comments
 (0)