Skip to content

Commit

Permalink
String Var: Fetch the text file for emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Oct 22, 2023
1 parent 26ff6b8 commit 6480213
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
10 changes: 10 additions & 0 deletions src/async_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ bool AsyncHandler::IsFilePending(bool important, bool graphic) {
return false;
}

void AsyncHandler::SaveFilesystem() {
#ifdef EMSCRIPTEN
// Save changed file system
EM_ASM({
FS.syncfs(function(err) {
});
});
#endif
}

bool AsyncHandler::IsImportantFilePending() {
return IsFilePending(true, false);
}
Expand Down
6 changes: 6 additions & 0 deletions src/async_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ namespace AsyncHandler {
* @return If any file with params is pending.
*/
bool IsFilePending(bool important, bool graphic);

/**
* Saves the state of the Save filesystem.
* Only works on emscripten, noop on other platforms.
*/
void SaveFilesystem();
}

using FileRequestBinding = std::shared_ptr<int>;
Expand Down
10 changes: 9 additions & 1 deletion src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4866,7 +4866,15 @@ bool Game_Interpreter::CommandManiacControlStrings(lcf::rpg::EventCommand const&
// maniacs does not like a file extension
Game_Strings::Str_t filename = Main_Data::game_strings->GetWithMode(str_param, args[0], modes[0]);
// args[1] is the encoding... 0 for ansi, 1 for utf8
result = Game_Strings::FromFile(filename, args[1]);
bool do_yield;
result = Game_Strings::FromFile(filename, args[1], do_yield);

if (do_yield) {
// Wait for text file download and repeat
_async_op = AsyncOp::MakeYieldRepeat();
return true;
}

break;
}
case 13: //Remove (rem) <fn(string base, int index, int size)>
Expand Down
13 changes: 12 additions & 1 deletion src/game_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// Headers
#include <regex>
#include <lcf/encoder.h>
#include "async_handler.h"
#include "game_message.h"
#include "game_strings.h"
#include "game_switches.h"
Expand Down Expand Up @@ -109,9 +110,17 @@ int Game_Strings::Split(Str_Params params, std::string delimiter, int string_out
return splits;
}

Game_Strings::Str_t Game_Strings::FromFile(StringView filename, int encoding) {
Game_Strings::Str_t Game_Strings::FromFile(StringView filename, int encoding, bool& do_yield) {
do_yield = false;

Filesystem_Stream::InputStream is = FileFinder::OpenText(filename);
if (!is) {
// Emscripten: Try to async fetch the file
auto* request = AsyncHandler::RequestFile("Text", filename);
request->SetImportantFile(true);
request->Start();
do_yield = !request->IsReady();

return {};
}

Expand Down Expand Up @@ -159,6 +168,8 @@ Game_Strings::Str_t Game_Strings::ToFile(Str_Params params, std::string filename
txt_out << str;
txt_out.Close();

AsyncHandler::SaveFilesystem();

return str;
}

Expand Down
2 changes: 1 addition & 1 deletion src/game_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Game_Strings {
int GetLen(Str_Params params, int var_id);
int InStr(Str_Params params, std::string search, int var_id, int begin = 0);
int Split(Str_Params params, std::string delimiter, int string_out_id, int var_id);
static Str_t FromFile(StringView filename, int encoding);
static Str_t FromFile(StringView filename, int encoding, bool& do_yield);
Str_t ToFile(Str_Params params, std::string filename, int encoding);
Str_t PopLine(Str_Params params, int offset, int string_out_id);
Str_t ExMatch(Str_Params params, std::string expr, int var_id, int begin, int string_out_id = -1);
Expand Down
9 changes: 1 addition & 8 deletions src/scene_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,7 @@ bool Scene_Save::Save(std::ostream& os, int slot_id, bool prepare_save) {
bool res = lcf::LSD_Reader::Save(os, save, lcf_engine, Player::encoding);

DynRpg::Save(slot_id);

#ifdef EMSCRIPTEN
// Save changed file system
EM_ASM({
FS.syncfs(function(err) {
});
});
#endif
AsyncHandler::SaveFilesystem();

return res;
}
Expand Down
8 changes: 1 addition & 7 deletions src/scene_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,13 +566,7 @@ bool Scene_Settings::SaveConfig(bool silent) {

cfg.WriteToStream(cfg_out);

#ifdef EMSCRIPTEN
// Save changed file system
EM_ASM({
FS.syncfs(function(err) {
});
});
#endif
AsyncHandler::SaveFilesystem();

if (silent) {
Output::Debug("Configuration saved to {}", cfg_out.GetName());
Expand Down

0 comments on commit 6480213

Please sign in to comment.