WIP encode and decode save files in action logs

This commit is contained in:
2024-06-13 20:21:55 -06:00
committed by Celtic Minstrel
parent 542822885c
commit 31dcb26d9d
5 changed files with 58 additions and 5 deletions

View File

@@ -360,6 +360,8 @@ if not env.GetOption('clean'):
if not path.exists('deps/cppcodec/README.md'):
subprocess.call(["git", "submodule", "update", "--init", "deps/cppcodec"])
env.Append(CPPPATH=[path.join(os.getcwd(), 'deps/cppcodec')])
# On Linux, build TGUI from the subtree if necessary
if platform == 'posix':
def check_tgui(conf, second_attempt=False):

View File

@@ -20,6 +20,7 @@
#include "porting.hpp"
#include "fileio/tagfile.hpp"
#include "fileio/tarball.hpp"
#include "replay.hpp"
extern bool mac_is_intel;
extern fs::path progDir, tempDir;
@@ -117,19 +118,29 @@ bool load_party(fs::path file_to_load, cUniverse& univ){
}else format = unknown;
fclose(file_id);
bool result = false;
switch(format){
case old_mac:
return load_party_v1(file_to_load, univ, town_restore, in_scen, maps_there, mac_is_intel);
result = load_party_v1(file_to_load, univ, town_restore, in_scen, maps_there, mac_is_intel);
break;
case old_win:
return load_party_v1(file_to_load, univ, town_restore, in_scen, maps_there, !mac_is_intel);
result = load_party_v1(file_to_load, univ, town_restore, in_scen, maps_there, !mac_is_intel);
break;
case new_oboe:
return load_party_v2(file_to_load, univ);
result = load_party_v2(file_to_load, univ);
break;
case unknown:
showError("This is not a Blades of Exile save file.");
return false;
result = false;
break;
}
return true;
if(recording && result){
record_action("load_party", encode_file(file_to_load));
}
return result;
}
bool load_party_v1(fs::path file_to_load, cUniverse& real_univ, bool town_restore, bool in_scen, bool maps_there, bool must_port){

View File

@@ -250,6 +250,15 @@ void process_args(int argc, char* argv[]) {
}
}
void replay_next_action() {
auto next_action = pop_next_action();
std::string t = next_action->Value();
if (t == "load_party") {
decode_file(next_action->GetText(), "temp.exg");
load_party("temp.exg", univ);
}
}
void init_boe(int argc, char* argv[]) {
set_up_apple_events();
init_directories(argv[0]);
@@ -307,6 +316,12 @@ void init_boe(int argc, char* argv[]) {
init_mini_map();
redraw_screen(REFRESH_NONE);
showMenuBar();
if(replaying){
while(has_next_action()){
replay_next_action();
}
}
}
void showWelcome() {

View File

@@ -5,7 +5,10 @@
#include "ticpp.h"
#include <ctime>
#include <iomanip>
#include <fstream>
#include <boost/algorithm/string/predicate.hpp>
#include <cppcodec/base64_rfc4648.hpp>
using base64 = cppcodec::base64_rfc4648;
bool recording = false;
bool replaying = false;
@@ -81,4 +84,23 @@ Element* pop_next_action(std::string expected_action_type) {
root->RemoveChild(next_action);
return clone;
}
std::string encode_file(fs::path file) {
std::ifstream ifs(file.string(), std::ios::binary|std::ios::ate);
std::ifstream::pos_type pos = ifs.tellg();
std::vector<char> result(pos);
ifs.seekg(0, std::ios::beg);
ifs.read(result.data(), pos);
return base64::encode(result.data(), result.size() * sizeof(char));
}
void decode_file(std::string data, fs::path file) {
std::ofstream ofs(file.string(), std::ios::binary|std::ios::ate);
std::vector<uint8_t> bytes = base64::decode(data.c_str(), strlen(data.c_str()) * sizeof(char));
char* bytes_as_c_str = reinterpret_cast<char*>(bytes.data());
ofs.write(bytes_as_c_str, bytes.size() / sizeof(char));
}

View File

@@ -3,6 +3,7 @@
#include <string>
#include <sstream>
#include <boost/filesystem.hpp>
// Input recording system
namespace ticpp { class Element; }
@@ -15,5 +16,7 @@ extern bool init_action_log(std::string command, std::string file);
extern void record_action(std::string action_type, std::string inner_text);
extern bool has_next_action();
extern Element* pop_next_action(std::string expected_action_type="");
extern std::string encode_file(fs::path file);
extern void decode_file(std::string data, fs::path file);
#endif