| 1 | CheatDatabase* cheatDatabase = nullptr; |
| 2 | |
| 3 | CheatDatabase::CheatDatabase() { |
| 4 | setGeometry({256, 256, 600, 360}); |
| 5 | windowManager->append(this, "CheatDatabase"); |
| 6 | |
| 7 | layout.setMargin(5); |
| 8 | cheatList.setCheckable(); |
| 9 | selectAllButton.setText("Select All"); |
| 10 | unselectAllButton.setText("Unselect All"); |
| 11 | acceptButton.setText("Add Codes"); |
| 12 | |
| 13 | append(layout); |
| 14 | layout.append(cheatList, {~0, ~0}, 5); |
| 15 | layout.append(controlLayout, {~0, 0}); |
| 16 | controlLayout.append(selectAllButton, {100, 0}, 5); |
| 17 | controlLayout.append(unselectAllButton, {100, 0}, 5); |
| 18 | controlLayout.append(spacer, {~0, 0}); |
| 19 | controlLayout.append(acceptButton, {80, 0}); |
| 20 | |
| 21 | selectAllButton.onActivate = [&] { |
| 22 | for(unsigned n = 0; n < cheat.size(); n++) cheatList.setChecked(n, true); |
| 23 | }; |
| 24 | |
| 25 | unselectAllButton.onActivate = [&] { |
| 26 | for(unsigned n = 0; n < cheat.size(); n++) cheatList.setChecked(n, false); |
| 27 | }; |
| 28 | |
| 29 | acceptButton.onActivate = {&CheatDatabase::addCodes, this}; |
| 30 | } |
| 31 | |
| 32 | void CheatDatabase::findCodes() { |
| 33 | const string sha256 = system().sha256(); |
| 34 | cheatList.reset(); |
| 35 | cheat.reset(); |
| 36 | |
| 37 | auto document = Markup::Document(string::read(program->path("cheats.bml"))); |
| 38 | for(auto& node : document) { |
| 39 | if(node.name != "cartridge") continue; |
| 40 | if(node["sha256"].text() != sha256) continue; |
| 41 | |
| 42 | setTitle(node["name"].text()); |
| 43 | for(auto& cheat : node) { |
| 44 | if(cheat.name != "cheat") continue; |
| 45 | cheatList.append(cheat["description"].text()); |
| 46 | this->cheat.append({cheat["code"].text(), cheat["description"].text()}); |
| 47 | } |
| 48 | |
| 49 | setVisible(); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | MessageWindow().setParent(*tools).setText("Sorry, no cheat codes were found.").information(); |
| 54 | } |
| 55 | |
| 56 | void CheatDatabase::addCodes() { |
| 57 | for(unsigned n = 0; n < cheat.size(); n++) { |
| 58 | if(cheatList.checked(n) == false) continue; |
| 59 | if(cheatEditor->import(cheat[n].code, cheat[n].desc) == false) { |
| 60 | MessageWindow().setParent(*this).setText("Ran out of empty slots for cheat codes.\nNot all cheat codes were added.").warning(); |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | setVisible(false); |
| 66 | cheatEditor->synchronize(); |
| 67 | cheatEditor->refresh(); |
| 68 | } |