Skip to content

Commit 053edab

Browse files
committed
ShowStringPic: Support Var(Indirect) for system name and font name
1 parent fea9ed5 commit 053edab

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

src/game_interpreter.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4680,7 +4680,31 @@ bool Game_Interpreter::CommandManiacShowStringPicture(lcf::rpg::EventCommand con
46804680
}
46814681

46824682
params.system_name = components[2];
4683+
uint32_t var_id = 0;
4684+
auto mode = delims[1] - 1;
4685+
if (mode > 0) {
4686+
if (!ManiacPatch::DecodeStringToInt(params.system_name, var_id)) {
4687+
Output::Warning("ShowStringPic: Bad system name arg (id={}, arg={})", pic_id, components[2]);
4688+
return true;
4689+
}
4690+
4691+
params.system_name = Main_Data::game_strings->GetWithMode(
4692+
params.system_name, mode, var_id, *Main_Data::game_variables
4693+
);
4694+
}
4695+
46834696
text.font_name = components[3];
4697+
mode = delims[2] - 1;
4698+
if (mode > 0) {
4699+
if (!ManiacPatch::DecodeStringToInt(text.font_name, var_id)) {
4700+
Output::Warning("ShowStringPic: Bad font name arg (id={}, arg={})", pic_id, components[2]);
4701+
return true;
4702+
}
4703+
4704+
text.font_name = Main_Data::game_strings->GetWithMode(
4705+
text.font_name, mode, var_id, *Main_Data::game_variables
4706+
);
4707+
}
46844708

46854709
params.texts = {text};
46864710

src/maniac_patch.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,51 @@ std::string_view ManiacPatch::GetLcfDescription(int data_type, int id, bool is_d
834834
return {};
835835
}
836836

837+
bool ManiacPatch::DecodeStringToInt(std::string_view str, uint32_t& out) {
838+
/*
839+
Is a custom 5 bit encoding:
840+
841+
For the rightmost character c the value is:
842+
c - A + 1
843+
844+
For all other characters c with index i the value is:
845+
(c - a + 1) << (i * 5)
846+
847+
Due to integer overflow the max string length is 6.
848+
*/
849+
850+
out = 0;
851+
852+
if (!(str.size() > 0 && str.size() <= 6)) {
853+
return false;
854+
}
855+
856+
auto in_range = [](uint32_t value) {
857+
return value >= 0 && value < 32;
858+
};
859+
860+
out = (str.back() - 'A' + 1);
861+
862+
if (!in_range(out)) {
863+
return false;
864+
}
865+
866+
str.remove_suffix(1);
867+
868+
for (size_t i = 0; i < str.size(); ++i) {
869+
uint32_t result = (str[i] - 'a' + 1);
870+
871+
if (!in_range(result)) {
872+
return false;
873+
}
874+
875+
int chidx = str.size() - i;
876+
out += (result << (chidx * 5));
877+
}
878+
879+
return out;
880+
}
881+
837882
bool ManiacPatch::GlobalSave::Load() {
838883
if (!Player::IsPatchManiac()) {
839884
return true;

src/maniac_patch.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ namespace ManiacPatch {
3838
std::string_view GetLcfName(int data_type, int id, bool is_dynamic);
3939
std::string_view GetLcfDescription(int data_type, int id, bool is_dynamic);
4040

41+
/**
42+
* Decodes an encoded string as used by the ShowStringPicture to the number
43+
* representation.
44+
*
45+
* @param str String to decode
46+
* @param out The number represented by the string
47+
*
48+
* @return Whether the decoding was successful (this function will reject malformed strings)
49+
*/
50+
bool DecodeStringToInt(std::string_view str, uint32_t& out);
51+
4152
namespace GlobalSave {
4253
/**
4354
* Attempts to load Save.lgs from the save directory.

0 commit comments

Comments
 (0)