From afd4a3f4e35473d7b82a86da132f87509a8ef649 Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Fri, 22 Mar 2024 01:22:34 +0100 Subject: [PATCH] custom mode manager and custom welcome messages --- menus.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ system.cpp | 12 +++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/menus.cpp b/menus.cpp index 691a8f56..06fd0068 100644 --- a/menus.cpp +++ b/menus.cpp @@ -472,6 +472,59 @@ EX void show_chaos() { dialog::display(); } +EX string custom_welcome; + +string customfile = "custom.hrm"; + +EX void show_custom() { + cmode = sm::SIDE | sm::MAYDARK; + gamescreen(); + dialog::init(XLAT("custom mode")); + + if(custom_welcome != "") { + dialog::addInfo("custom welcome message:"); + dialog::addInfo(custom_welcome); + dialog::addItem("edit", '/'); + } + else { + dialog::addItem("custom welcome message", '/'); + } + dialog::add_action([] () { + dialog::edit_string(custom_welcome, "custom welcome message", ""); + }); + dialog::addBreak(100); + dialog::addItem("save custom mode", 's'); + dialog::add_action([] { + dialog::openFileDialog(customfile, XLAT("file to save:"), ".hrm", [] () { + try { + save_mode_to_file(customfile); + addMessage(XLAT("Mode saved to %1", customfile)); + return true; + } + catch(hstream_exception& e) { + addMessage(XLAT("Failed to save mode to %1", customfile)); + return false; + } + }); + }); + dialog::addItem("load custom mode", 'l'); + dialog::add_action([] { + dialog::openFileDialog(customfile, XLAT("file to load:"), ".hrm", [] () { + try { + load_mode_from_file(customfile); + addMessage(XLAT("Loaded mode from %1", customfile)); + return true; + } + catch(hstream_exception& e) { + addMessage(XLAT("Failed to load mode from %1", customfile)); + return false; + } + }); + }); + dialog::addBack(); + dialog::display(); + } + EX void mode_higlights() { cmode = sm::NOSCR; gamescreen(); @@ -751,6 +804,8 @@ EX void showChangeMode() { dialog::addBreak(50); dialog::addItem(XLAT("highlights & achievements"), 'h'); dialog::add_action_push(mode_higlights); + dialog::addItem(XLAT("custom mode manager"), 'm'); + dialog::add_action_push(show_custom); dialog::addBack(); dialog::display(); diff --git a/system.cpp b/system.cpp index 6ffb1e9a..31c79206 100644 --- a/system.cpp +++ b/system.cpp @@ -80,7 +80,8 @@ EX hookset hooks_welcome_message; EX void welcomeMessage() { if(callhandlers(false, hooks_welcome_message)) return; if(nohelp == 1) return; - if(embedded_plane) return IPF(welcomeMessage()); + if(custom_welcome != "") addMessage(custom_welcome); + else if(embedded_plane) return IPF(welcomeMessage()); #if CAP_TOUR else if(tour::on) return; // displayed by tour #endif @@ -1796,17 +1797,26 @@ EX void save_mode_to_file(const string& fname) { save_mode_data(ss); string s = as_hexstring(ss.s); fhstream f(fname, "w"); + if(!f.f) throw hstream_exception(); println(f, modheader); println(f, s); + if(custom_welcome != "") println(f, "CMSG ", custom_welcome); } EX void load_mode_from_file(const string& fname) { fhstream f(fname, "r"); + if(!f.f) throw hstream_exception(); string header = scanline(f); if(header[0] != '#') throw hstream_exception(); string hex = scanline(f); shstream ss; ss.s = from_hexstring(hex + "00"); + custom_welcome = ""; + while(true) { + string s = scanline(f); + if(s == "") break; + if(s.substr(0, 5) == "CMSG ") custom_welcome = s.substr(5); + } stop_game(); load_mode_data_with_zero(ss); start_game();