Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 9 additions & 82 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@
#include <cassert>
#include "game_interpreter.h"
#include "async_handler.h"
#include "audio.h"
#include "game_dynrpg.h"
#include "filefinder.h"
#include "game_destiny.h"
#include "game_map.h"
#include "game_event.h"
#include "game_enemyparty.h"
#include "game_ineluki.h"
#include "game_player.h"
#include "game_targets.h"
#include "game_switches.h"
#include "game_variables.h"
#include "game_party.h"
#include "game_actors.h"
#include "game_strings.h"
#include "game_system.h"
#include "game_message.h"
#include "game_pictures.h"
Expand Down Expand Up @@ -70,7 +68,6 @@
#include "transition.h"
#include "baseui.h"
#include "algo.h"
#include "rand.h"

using namespace Game_Interpreter_Shared;

Expand Down Expand Up @@ -4903,91 +4900,21 @@ bool Game_Interpreter::CommandManiacControlGlobalSave(lcf::rpg::EventCommand con

int operation = com.parameters[0];

auto load_global_save = [&]() {
Main_Data::global_save_opened = true;

// Load
auto lgs = FileFinder::Save().OpenFile("Save.lgs");
if (!lgs) {
return;
}

lcf::LcfReader reader(lgs);
std::string header;
reader.ReadString(header, reader.ReadInt());
if (header.length() != 13 || header != "LcfGlobalSave") {
Output::Debug("This is not a valid global save.");
return;
}

lcf::LcfReader::Chunk chunk;

while (!reader.Eof()) {
chunk.ID = reader.ReadInt();
chunk.length = reader.ReadInt();
switch (chunk.ID) {
case 1: {
Game_Switches::Switches_t switches;
reader.Read(switches, chunk.length);
Main_Data::game_switches_global->SetData(std::move(switches));
break;
}
case 2: {
Game_Variables::Variables_t variables;
reader.Read(variables, chunk.length);
Main_Data::game_variables_global->SetData(std::move(variables));
break;
}
default:
reader.Skip(chunk, "CommandManiacControlGlobalSave");
}
}
};

if (operation == 0) {
// Open
load_global_save();
// Open: Fill Global Save with data from Save.lgs
// Does nothing when already opened
ManiacPatch::GlobalSave::Load();
} else if (operation == 1) {
// Close
Main_Data::global_save_opened = false;
// Marks the file as closed and does nothing
ManiacPatch::GlobalSave::Close();
} else if (operation == 2 || operation == 3) {
// 2: Save (write to file)
// 3: Save and Close
if (!Main_Data::global_save_opened) {
return true;
}

auto savelgs_name = FileFinder::Save().FindFile("Save.lgs");
if (savelgs_name.empty()) {
savelgs_name = "Save.lgs";
}

auto lgs_out = FileFinder::Save().OpenOutputStream(savelgs_name);
if (!lgs_out) {
Output::Warning("Maniac ControlGlobalSave: Saving failed");
return true;
}

lcf::LcfWriter writer(lgs_out, lcf::EngineVersion::e2k3);
writer.WriteInt(13);
const std::string header = "LcfGlobalSave";
writer.Write(header);
writer.WriteInt(1);
writer.WriteInt(Main_Data::game_switches_global->GetSize());
writer.Write(Main_Data::game_switches_global->GetData());
writer.WriteInt(2);
writer.WriteInt(Main_Data::game_variables_global->GetSize() * sizeof(int32_t));
writer.Write(Main_Data::game_variables_global->GetData());

AsyncHandler::SaveFilesystem();

if (operation == 3) {
Main_Data::global_save_opened = false;
}
ManiacPatch::GlobalSave::Save(operation == 3);
} else if (operation == 4 || operation == 5) {
if (!Main_Data::global_save_opened) {
load_global_save();
}
// Reload the file when it was already closed
ManiacPatch::GlobalSave::Load();

int type = com.parameters[2];
int game_state_idx = ValueOrVariableBitfield(com.parameters[1], 0, com.parameters[3]);
Expand Down
16 changes: 2 additions & 14 deletions src/game_interpreter_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@
*/

#include "game_interpreter_shared.h"
#include "game_actors.h"
#include "game_enemyparty.h"
#include "game_ineluki.h"
#include "game_map.h"
#include "game_party.h"
#include "game_player.h"
#include "game_strings.h"
#include "game_switches.h"
#include "game_system.h"
#include "game_variables.h"
#include "maniac_patch.h"
#include "main_data.h"
#include "output.h"
#include "player.h"
#include "rand.h"
#include "util_macro.h"
#include "utils.h"
#include "audio.h"
#include "baseui.h"
#include <cmath>
#include <cstdint>
#include <lcf/rpg/savepartylocation.h>
#include <lcf/reader_util.h>
Expand Down
7 changes: 0 additions & 7 deletions src/main_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
// Headers
#include <cstdlib>
#include "main_data.h"
#include "filefinder.h"
#include "filefinder_rtp.h"
#include "filesystem.h"
#include "game_destiny.h"
#include "game_system.h"
#include "game_actors.h"
Expand All @@ -38,10 +36,7 @@
#include "game_targets.h"
#include "game_quit.h"
#include "game_windows.h"
#include "font.h"
#include "player.h"
#include "system.h"
#include "output.h"

#ifndef _WIN32
# include <unistd.h>
Expand Down Expand Up @@ -75,7 +70,6 @@ namespace Main_Data {
std::unique_ptr<Game_DynRpg> game_dynrpg;
std::unique_ptr<Game_Ineluki> game_ineluki;
std::unique_ptr<Game_Destiny> game_destiny;
bool global_save_opened = false;
std::unique_ptr<Game_Switches> game_switches_global;
std::unique_ptr<Game_Variables> game_variables_global;

Expand Down Expand Up @@ -132,7 +126,6 @@ void Main_Data::Cleanup() {
game_dynrpg.reset();
game_ineluki.reset();
game_destiny.reset();
global_save_opened = false;
game_switches_global.reset();
game_variables_global.reset();
}
Expand Down
113 changes: 113 additions & 0 deletions src/maniac_patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "maniac_patch.h"

#include "filesystem_stream.h"
#include "input.h"
#include "game_actors.h"
#include "game_interpreter_control_variables.h"
Expand All @@ -29,7 +30,9 @@
#include "output.h"
#include "player.h"

#include <lcf/reader_lcf.h>
#include <lcf/reader_util.h>
#include <lcf/writer_lcf.h>
#include <vector>

/*
Expand Down Expand Up @@ -117,6 +120,8 @@ namespace {
Divmul,
Between
};

bool global_save_opened = false;
}

struct ProcessAssignmentRet {
Expand Down Expand Up @@ -941,3 +946,111 @@ std::string_view ManiacPatch::GetLcfDescription(int data_type, int id, bool is_d
Output::Warning("GetLcfDescription: Unsupported data_type {} {}", data_type, id);
return {};
}

bool ManiacPatch::GlobalSave::Load() {
if (!Player::IsPatchManiac()) {
return true;
}

if (global_save_opened) {
return true;
}

// Even consider it opened when the file is missing
// It will be created on Save
global_save_opened = true;

auto lgs_in = FileFinder::Save().OpenFile("Save.lgs");
if (!lgs_in) {
return false;
}

return Load(lgs_in);
}

bool ManiacPatch::GlobalSave::Load(Filesystem_Stream::InputStream& lgs_in) {
if (!lgs_in) {
return false;
}

lcf::LcfReader reader(lgs_in);
std::string header;
reader.ReadString(header, reader.ReadInt());
if (header.length() != 13 || header != "LcfGlobalSave") {
Output::Debug("This is not a valid global save.");
return false;
}

lcf::LcfReader::Chunk chunk;

while (!reader.Eof()) {
chunk.ID = reader.ReadInt();
chunk.length = reader.ReadInt();
switch (chunk.ID) {
case 1: {
Game_Switches::Switches_t switches;
reader.Read(switches, chunk.length);
Main_Data::game_switches_global->SetData(std::move(switches));
break;
}
case 2: {
Game_Variables::Variables_t variables;
reader.Read(variables, chunk.length);
Main_Data::game_variables_global->SetData(std::move(variables));
break;
}
default:
reader.Skip(chunk, "CommandManiacControlGlobalSave");
}
}

return true;
}

bool ManiacPatch::GlobalSave::Save(bool close_global_save) {
if (!Player::IsPatchManiac()) {
return true;
}

if (!global_save_opened) {
return true;
}

auto savelgs_name = FileFinder::Save().FindFile("Save.lgs");
if (savelgs_name.empty()) {
savelgs_name = "Save.lgs";
}

auto lgs_out = FileFinder::Save().OpenOutputStream(savelgs_name);
if (!Save(lgs_out)) {
Output::Warning("Maniac ControlGlobalSave: Saving failed");
return false;
}

global_save_opened = !close_global_save;

AsyncHandler::SaveFilesystem();
return true;
}

bool ManiacPatch::GlobalSave::Save(Filesystem_Stream::OutputStream& lgs_out) {
if (!lgs_out) {
return false;
}

lcf::LcfWriter writer(lgs_out, lcf::EngineVersion::e2k3);
writer.WriteInt(13);
const std::string header = "LcfGlobalSave";
writer.Write(header);
writer.WriteInt(1);
writer.WriteInt(Main_Data::game_switches_global->GetSize());
writer.Write(Main_Data::game_switches_global->GetData());
writer.WriteInt(2);
writer.WriteInt(Main_Data::game_variables_global->GetSize() * sizeof(int32_t));
writer.Write(Main_Data::game_variables_global->GetData());
return true;
}

void ManiacPatch::GlobalSave::Close() {
global_save_opened = false;
}
Loading