StringUtil cleanup. Nothing seems broken.
[dolphin.git] / Source / Core / DolphinWX / Src / ARCodeAddEdit.cpp
blobfcd2a52ecd70be518f6d63d76d40a46b179787a2
1 // Copyright (C) 2003 Dolphin Project.
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
15 // Official SVN repository and contact information can be found at
16 // http://code.google.com/p/dolphin-emu/
18 #include "ARCodeAddEdit.h"
19 #include "ARDecrypt.h"
21 extern std::vector<ActionReplay::ARCode> arCodes;
23 BEGIN_EVENT_TABLE(CARCodeAddEdit, wxDialog)
24 EVT_CLOSE(CARCodeAddEdit::OnClose)
25 EVT_BUTTON(wxID_OK, CARCodeAddEdit::SaveCheatData)
26 EVT_SPIN(ID_ENTRY_SELECT, CARCodeAddEdit::ChangeEntry)
27 END_EVENT_TABLE()
29 CARCodeAddEdit::CARCodeAddEdit(int _selection, wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& position, const wxSize& size, long style)
30 : wxDialog(parent, id, title, position, size, style)
32 selection = _selection;
33 CreateGUIControls(selection);
36 CARCodeAddEdit::~CARCodeAddEdit()
40 void CARCodeAddEdit::CreateGUIControls(int _selection)
42 ActionReplay::ARCode tempEntries;
43 wxString currentName = wxT("Insert name here..");
45 if (_selection == -1)
47 tempEntries.name = "";
49 else
51 currentName = wxString(arCodes.at(_selection).name.c_str(), *wxConvCurrent);
52 tempEntries = arCodes.at(_selection);
55 wxBoxSizer* sEditCheat = new wxBoxSizer(wxVERTICAL);
56 wxBoxSizer* sEditCheatButtons = new wxBoxSizer(wxHORIZONTAL);
57 wxStaticBoxSizer* sbEntry = new wxStaticBoxSizer(wxVERTICAL, this, _("Cheat Code"));
58 wxGridBagSizer* sgEntry = new wxGridBagSizer(0, 0);
60 wxStaticText* EditCheatNameText = new wxStaticText(this, ID_EDITCHEAT_NAME_TEXT, _("Name:"), wxDefaultPosition, wxDefaultSize);
61 EditCheatName = new wxTextCtrl(this, ID_EDITCHEAT_NAME, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0);
62 EditCheatName->SetValue(currentName);
63 EntrySelection = new wxSpinButton(this, ID_ENTRY_SELECT, wxDefaultPosition, wxDefaultSize, wxVERTICAL);
64 EntrySelection->SetRange(1, ((int)arCodes.size()) > 0 ? (int)arCodes.size() : 1);
65 EntrySelection->SetValue((int)(arCodes.size() - _selection));
66 EditCheatCode = new wxTextCtrl(this, ID_EDITCHEAT_CODE, wxEmptyString, wxDefaultPosition, wxSize(300, 100), wxTE_MULTILINE);
67 UpdateTextCtrl(tempEntries);
68 wxButton* bOK = new wxButton(this, wxID_OK, _("OK"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator);
69 wxButton* bCancel = new wxButton(this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator);
71 sgEntry->Add(EditCheatNameText, wxGBPosition(0, 0), wxGBSpan(1, 1), wxALIGN_CENTER|wxALL, 5);
72 sgEntry->Add(EditCheatName, wxGBPosition(0, 1), wxGBSpan(1, 1), wxEXPAND|wxALL, 5);
73 sgEntry->Add(EntrySelection, wxGBPosition(0, 2), wxGBSpan(2, 1), wxEXPAND|wxALL, 5);
74 sgEntry->Add(EditCheatCode, wxGBPosition(1, 0), wxGBSpan(1, 2), wxEXPAND|wxALL, 5);
75 sgEntry->AddGrowableCol(1);
76 sgEntry->AddGrowableRow(1);
77 sbEntry->Add(sgEntry, 1, wxEXPAND|wxALL);
79 sEditCheatButtons->AddStretchSpacer();
80 sEditCheatButtons->Add(bOK, 0, wxALL, 5);
81 sEditCheatButtons->Add(bCancel, 0, wxALL, 5);
83 sEditCheat->Add(sbEntry, 1, wxEXPAND|wxALL, 5);
84 sEditCheat->Add(sEditCheatButtons, 0, wxEXPAND, 5);
86 SetSizerAndFit(sEditCheat);
89 void CARCodeAddEdit::OnClose(wxCloseEvent& WXUNUSED (event))
91 Destroy();
94 void CARCodeAddEdit::ChangeEntry(wxSpinEvent& event)
96 ActionReplay::ARCode currentCode = arCodes.at((int)arCodes.size() - event.GetPosition());
97 EditCheatName->SetValue(wxString(currentCode.name.c_str(), *wxConvCurrent));
98 UpdateTextCtrl(currentCode);
101 void CARCodeAddEdit::SaveCheatData(wxCommandEvent& WXUNUSED (event))
103 std::vector<ActionReplay::AREntry> tempEntries;
104 std::vector<std::string> encryptedLine;
105 std::string cheatValues = std::string(EditCheatCode->GetValue().mb_str());
106 size_t line = 0;
108 // there's no newline, or newline is imcomplete, stop here.
109 while (line != std::string::npos)
111 if (line > 0) line++;
112 std::vector<std::string> pieces;
113 std::string line_str = cheatValues.substr(line, cheatValues.find('\n', line) - line);
115 SplitString(line_str, ' ', pieces);
117 if (pieces.size() == 2 && pieces[0].size() == 8 && pieces[1].size() == 8)
119 u32 addr = strtoul(pieces[0].c_str(), NULL, 16);
120 u32 value = strtoul(pieces[1].c_str(), NULL, 16);
121 // Decrypted code
122 tempEntries.push_back(ActionReplay::AREntry(addr, value));
124 else
126 SplitString(line_str, '-', pieces);
128 if (pieces.size() == 3 && pieces[0].size() == 4 && pieces[1].size() == 4 && pieces[2].size() == 5)
130 // Encrypted code
131 encryptedLine.push_back(pieces[0]+pieces[1]+pieces[2]);
135 if ((line = cheatValues.find('\n', line)) == std::string::npos && encryptedLine.size())
137 ActionReplay::DecryptARCode(encryptedLine, tempEntries);
141 if (selection == -1)
143 ActionReplay::ARCode newCheat;
145 newCheat.name = std::string(EditCheatName->GetValue().mb_str());
146 newCheat.ops = tempEntries;
147 newCheat.active = true;
149 arCodes.push_back(newCheat);
151 else
153 arCodes.at(selection).name = std::string(EditCheatName->GetValue().mb_str());
154 arCodes.at(selection).ops = tempEntries;
157 AcceptAndClose();
160 void CARCodeAddEdit::UpdateTextCtrl(ActionReplay::ARCode arCode)
162 EditCheatCode->Clear();
164 if (arCode.name != "")
165 for (u32 i = 0; i < arCode.ops.size(); i++)
166 EditCheatCode->AppendText(wxString::Format(wxT("%08X %08X\n"), arCode.ops.at(i).cmd_addr, arCode.ops.at(i).value));
167 else
168 EditCheatCode->SetValue(_("Insert Encrypted or Decrypted code here..."));