Strip whitespace and empty lines from the cheat editor.

Previously, bsnes would turn the cheat text into a list of cheats by splitting
on '\n', producing an empty "cheat" after the last line of the editor:

    "7e0003=00\n".split("\n") -> ["7e0003=00", ""]

That empty "cheat" is not a valid cheat, so the result would be marked invalid.
Teaching bsnes to silently skip empty lines worked, but it would preserve
multiple blank lines between two cheats, which was silly.

Now we iterate over all the lines of the cheat editor, putting valid codes in a
new vector, and setting a flag for invalid codes.

Fixes #63.
This commit is contained in:
Tim Allen 2020-10-20 17:39:12 +11:00 committed by Screwtapello
parent 968e6b5feb
commit 090b79b3be
1 changed files with 13 additions and 5 deletions

View File

@ -101,11 +101,17 @@ auto CheatWindow::doChange() -> void {
} }
auto CheatWindow::doAccept() -> void { auto CheatWindow::doAccept() -> void {
auto codes = codeValue.text().downcase().transform("+", "\n").split("\n").strip(); auto raw_codes = codeValue.text().downcase().transform("+", "\n").split("\n").strip();
vector<string> valid_codes;
string invalid; //if empty after below for-loop, code is considered valid string invalid; //if empty after below for-loop, code is considered valid
for(auto& code : codes) { for(auto& code : raw_codes) {
if(code.length() == 0) {
continue;
}
if(!program.gameBoy.program) { if(!program.gameBoy.program) {
if(!cheatEditor.decodeSNES(code)) { if(cheatEditor.decodeSNES(code)) {
valid_codes.append(code);
} else {
invalid = invalid =
"Invalid code(s), please only use codes in the following format:\n" "Invalid code(s), please only use codes in the following format:\n"
"\n" "\n"
@ -115,7 +121,9 @@ auto CheatWindow::doAccept() -> void {
"higan (aaaaaa=cc?dd)"; "higan (aaaaaa=cc?dd)";
} }
} else { } else {
if(!cheatEditor.decodeGB(code)) { if(cheatEditor.decodeGB(code)) {
valid_codes.append(code);
} else {
invalid = invalid =
"Invalid code(s), please only use codes in the following format:\n" "Invalid code(s), please only use codes in the following format:\n"
"\n" "\n"
@ -129,7 +137,7 @@ auto CheatWindow::doAccept() -> void {
} }
if(invalid) return (void)MessageDialog().setAlignment(*toolsWindow).setText(invalid).error(); if(invalid) return (void)MessageDialog().setAlignment(*toolsWindow).setText(invalid).error();
Cheat cheat = {nameValue.text().strip(), codes.merge("+"), enableOption.checked()}; Cheat cheat = {nameValue.text().strip(), valid_codes.merge("+"), enableOption.checked()};
if(acceptButton.text() == "Add") { if(acceptButton.text() == "Add") {
cheatEditor.addCheat(cheat); cheatEditor.addCheat(cheat);
} else { } else {