From 090b79b3be8dff3de5566bcbcbc5675d798d6e79 Mon Sep 17 00:00:00 2001 From: Tim Allen Date: Tue, 20 Oct 2020 17:39:12 +1100 Subject: [PATCH] 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. --- bsnes/target-bsnes/tools/cheat-editor.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/bsnes/target-bsnes/tools/cheat-editor.cpp b/bsnes/target-bsnes/tools/cheat-editor.cpp index 6fbf2ced..66ec2466 100644 --- a/bsnes/target-bsnes/tools/cheat-editor.cpp +++ b/bsnes/target-bsnes/tools/cheat-editor.cpp @@ -101,11 +101,17 @@ auto CheatWindow::doChange() -> 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 valid_codes; 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(!cheatEditor.decodeSNES(code)) { + if(cheatEditor.decodeSNES(code)) { + valid_codes.append(code); + } else { invalid = "Invalid code(s), please only use codes in the following format:\n" "\n" @@ -115,7 +121,9 @@ auto CheatWindow::doAccept() -> void { "higan (aaaaaa=cc?dd)"; } } else { - if(!cheatEditor.decodeGB(code)) { + if(cheatEditor.decodeGB(code)) { + valid_codes.append(code); + } else { invalid = "Invalid code(s), please only use codes in the following format:\n" "\n" @@ -129,7 +137,7 @@ auto CheatWindow::doAccept() -> void { } 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") { cheatEditor.addCheat(cheat); } else {