Skip to content

Commit

Permalink
StringVar: refactor from DBString to std string
Browse files Browse the repository at this point in the history
also includes safe checks around nullptr when retrieving names and descriptions
  • Loading branch information
enewey committed Jul 12, 2023
1 parent 8627ee2 commit 7e6e982
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 117 deletions.
252 changes: 165 additions & 87 deletions src/game_interpreter.cpp

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/game_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@ class Game_Interpreter
lcf::rpg::SaveEventExecState _state;
KeyInputState _keyinput;
AsyncOp _async_op = {};

std::string ManiacGetLcfDataName(int data_type, int id, bool is_dynamic) const;
std::string ManiacGetLcfDataDescription(int data_type, int id, bool is_dynamic) const;

private:
template<typename T> std::string ManiacGetNameSafely(std::vector<T>, int id) const;
template<typename T> std::string ManiacGetDescriptionSafely(std::vector<T>, int id) const;
template<> std::string ManiacGetDescriptionSafely<lcf::rpg::Actor>(std::vector<lcf::rpg::Actor>, int id) const;

};

inline const lcf::rpg::SaveEventExecFrame* Game_Interpreter::GetFramePtr() const {
Expand Down
50 changes: 25 additions & 25 deletions src/game_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ Game_Strings::Str_t Game_Strings::Cat(Str_Params params, Str_t string) {
if (!ResizeWithId(params.string_id)) return "";

Str_t s = Get(params.string_id);
std::string op_string = static_cast<std::string>(s);
op_string.append(static_cast<std::string>(string));
Set(params, static_cast<Str_t>(op_string));
std::string op_string = s;
op_string.append(string);
Set(params, op_string);
return s;
}

int Game_Strings::ToNum(Str_Params params, int var_id) {
if (!ResizeWithId(params.string_id)) return -1;
std::string str = static_cast<std::string>(Get(params.string_id));
std::string str = Get(params.string_id);

int num;
if (params.hex)
Expand All @@ -60,7 +60,7 @@ int Game_Strings::ToNum(Str_Params params, int var_id) {
int Game_Strings::GetLen(Str_Params params, int var_id) {
if (!ResizeWithId(params.string_id)) return -1;

int len = static_cast<std::string>(Get(params.string_id)).length();
int len = Get(params.string_id).length();
Main_Data::game_variables->Set(var_id, len);
return len;
}
Expand All @@ -69,14 +69,14 @@ int Game_Strings::InStr(Str_Params params, std::string search, int var_id, int b
if (!ResizeWithId(params.string_id)) return -1;

if (params.extract) {
search = static_cast<std::string>(Extract(static_cast<Str_t>(search), params.hex));
search = Extract(search, params.hex);
}

std::string str = static_cast<std::string>(Get(params.string_id));
std::string str = Get(params.string_id);

Output::Debug("Searching for {} in {}", search, str);

int index = static_cast<std::string>(Get(params.string_id)).find(search, begin);
int index = Get(params.string_id).find(search, begin);
Main_Data::game_variables->Set(var_id, index);
return index;
}
Expand All @@ -89,29 +89,29 @@ int Game_Strings::Split(Str_Params params, std::string delimiter, int string_out

// always returns at least 1
int splits = 1;
std::string str = static_cast<std::string>(Get(params.string_id));
std::string str = Get(params.string_id);

params.string_id = string_out_id;

for (index = str.find(delimiter); index != std::string::npos; index = str.find(delimiter)) {
token = str.substr(0, index);
Set(params, static_cast<Str_t>(token));
Set(params, token);
params.string_id++;
splits++;
str.erase(0, index + delimiter.length());
}

// set the remaining string
Set(params, static_cast<Str_t>(str));
Set(params, str);
Main_Data::game_variables->Set(var_id, splits);
return splits;
}

Game_Strings::Str_t Game_Strings::ToFile(Str_Params params, std::string filename, int encoding) {
std::string str = static_cast<std::string>(Get(params.string_id));
std::string str = Get(params.string_id);

if (params.extract) {
filename = static_cast<std::string>(Extract(static_cast<Str_t>(filename), params.hex));
filename = Extract(filename, params.hex);
}

// this sucks but it is what maniacs does
Expand All @@ -127,25 +127,25 @@ Game_Strings::Str_t Game_Strings::ToFile(Str_Params params, std::string filename
txt_out << str;
txt_out.Close();

return static_cast<Game_Strings::Str_t>(str);
return str;
}

Game_Strings::Str_t Game_Strings::PopLine(Str_Params params, int offset, int string_out_id) {
if (!ResizeWithId(params.string_id)) return "";

int index;
std::string result;
std::string str = static_cast<std::string>(Get(params.string_id));
std::string str = Get(params.string_id);

std::stringstream ss(str);

while (offset >= 0 && std::getline(ss, result)) { offset--; }

offset = ss.rdbuf()->in_avail();

Set(params, static_cast<Str_t>(ss.str().substr(str.length() - offset)));
Set(params, ss.str().substr(str.length() - offset));
params.string_id = string_out_id;
return Set(params, static_cast<Str_t>(result));
return Set(params, result);
}

Game_Strings::Str_t Game_Strings::ExMatch(Str_Params params, std::string expr, int var_id, int begin, int string_out_id) {
Expand All @@ -154,18 +154,18 @@ Game_Strings::Str_t Game_Strings::ExMatch(Str_Params params, std::string expr, i
std::smatch match;

if (params.extract) {
expr = static_cast<std::string>(Extract(static_cast<Str_t>(expr), params.hex));
expr = Extract(expr, params.hex);
}

std::string base = static_cast<std::string>(Get(params.string_id)).erase(0, begin);
std::string base = Get(params.string_id).erase(0, begin);
std::regex r(expr);

std::regex_search(base, match, r);

var_result = match.position() + begin;
Main_Data::game_variables->Set(var_id, var_result);

str_result = static_cast<Str_t>(match.str());
str_result = match.str();
if (string_out_id > 0) {
params.string_id = string_out_id;
Set(params, str_result);
Expand Down Expand Up @@ -203,11 +203,11 @@ const Game_Strings::Strings_t& Game_Strings::RangeOp(Str_Params params, int stri
case 1: Cat(params, string); break;
case 2: ToNum(params, args[0] + (params.string_id - start)); break;
case 3: GetLen(params, args[0] + (params.string_id - start)); break;
case 4: InStr(params, static_cast<std::string>(string), args[1], args[2]); break;
case 5: params.string_id += Split(params, static_cast<std::string>(string), args[1], args[2]); break;
case 4: InStr(params, string, args[1], args[2]); break;
case 5: params.string_id += Split(params, string, args[1], args[2]); break;
case 8: break; // range case not applicable for popLine; see case in game_interpreter.cpp
case 9: ExMatch(params, static_cast<std::string>(string), args[1] + (params.string_id - start), args[2]); break;
case 10: ExMatch(params, static_cast<std::string>(string), args[1] + (params.string_id - start), args[2], args[3]); break;
case 9: ExMatch(params, string, args[1] + (params.string_id - start), args[2]); break;
case 10: ExMatch(params, string, args[1] + (params.string_id - start), args[2], args[3]); break;
}
}
return GetData();
Expand All @@ -217,7 +217,7 @@ Game_Strings::Str_t Game_Strings::PrependMin(Str_t string, int min_size, char c)
if (string.size() < min_size) {
int s = min_size - string.size();
std::string res = std::string(s, c) + (std::string)string;
return (Str_t)res;
return res;
}
return string;
}
Expand Down
16 changes: 13 additions & 3 deletions src/game_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
class Game_Strings {
public:
using Str_t = lcf::DBString;
using Str_t = std::string;
using Strings_t = std::vector<Str_t>;

static constexpr int max_warnings = 10;
Expand All @@ -44,7 +44,9 @@ class Game_Strings {
Game_Strings();

void SetData(Strings_t s);
void SetData(std::vector<lcf::DBString> s);
const Strings_t& GetData() const;
std::vector<lcf::DBString> GetLcfData();

Str_t Get(int id) const;
Str_t GetIndirect(int id) const;
Expand Down Expand Up @@ -92,10 +94,18 @@ inline void Game_Strings::SetData(Strings_t s) {
_strings = std::move(s);
}

inline void Game_Strings::SetData(std::vector<lcf::DBString> s) {
_strings = std::vector<Str_t>(s.begin(), s.end());
}

inline const Game_Strings::Strings_t& Game_Strings::GetData() const {
return _strings;
}

inline std::vector<lcf::DBString> Game_Strings::GetLcfData() {
return std::vector<lcf::DBString>(_strings.begin(), _strings.end());
}

inline bool Game_Strings::ShouldWarn(int id) const {
return id <= 0 && _warnings > 0;
}
Expand Down Expand Up @@ -144,7 +154,7 @@ inline Game_Strings::Str_t Game_Strings::GetWithModeAndPos(Str_t str_data, int a
Str_t ret;
switch (mode) {
case 0:
ret = static_cast<Str_t>(static_cast<std::string>(str_data).substr(*pos, arg));
ret = str_data.substr(*pos, arg);
*pos += arg;
break;
case 1: // direct string reference
Expand All @@ -158,5 +168,5 @@ inline Game_Strings::Str_t Game_Strings::GetWithModeAndPos(Str_t str_data, int a
}

inline Game_Strings::Str_t Game_Strings::Extract(Str_t string, bool as_hex) {
return static_cast<Str_t>(PendingMessage::ApplyTextInsertingCommands(static_cast<std::string>(string), Player::escape_char, true, as_hex));
return PendingMessage::ApplyTextInsertingCommands(string, Player::escape_char, true, as_hex);
}
2 changes: 1 addition & 1 deletion src/game_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void Game_Windows::Window_User::Refresh(bool& async_wait) {

fonts.emplace_back(font);

std::string extracted_text = ToString(Main_Data::game_strings->Extract(text.text, false));
std::string extracted_text = Main_Data::game_strings->Extract(ToString(text.text), false);
std::stringstream ss(extracted_text);
std::string out;
PendingMessage pm;
Expand Down
2 changes: 1 addition & 1 deletion src/scene_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ bool Scene_Save::Save(std::ostream& os, int slot_id, bool prepare_save) {
save.system = Main_Data::game_system->GetSaveData();
save.system.switches = Main_Data::game_switches->GetData();
save.system.variables = Main_Data::game_variables->GetData();
save.system.maniac_strings = Main_Data::game_strings->GetData();
save.system.maniac_strings = Main_Data::game_strings->GetLcfData();
save.inventory = Main_Data::game_party->GetSaveData();
save.actors = Main_Data::game_actors->GetSaveData();
save.screen = Main_Data::game_screen->GetSaveData();
Expand Down

0 comments on commit 7e6e982

Please sign in to comment.