2013-04-18 03:43:35 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2014-02-22 22:36:30 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <wx/chartype.h>
|
|
|
|
#include <wx/defs.h>
|
|
|
|
#include <wx/dialog.h>
|
|
|
|
#include <wx/event.h>
|
|
|
|
#include <wx/gbsizer.h>
|
|
|
|
#include <wx/gdicmn.h>
|
2014-07-25 01:46:46 +00:00
|
|
|
#include <wx/msgdlg.h>
|
2014-02-22 22:36:30 +00:00
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <wx/spinbutt.h>
|
|
|
|
#include <wx/stattext.h>
|
|
|
|
#include <wx/string.h>
|
|
|
|
#include <wx/textctrl.h>
|
|
|
|
#include <wx/translation.h>
|
|
|
|
#include <wx/windowid.h>
|
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-22 22:36:30 +00:00
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "Core/ActionReplay.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/ARDecrypt.h"
|
|
|
|
#include "DolphinWX/ARCodeAddEdit.h"
|
|
|
|
#include "DolphinWX/WxUtils.h"
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2015-02-24 23:32:17 +00:00
|
|
|
CARCodeAddEdit::CARCodeAddEdit(int _selection, const std::vector<ActionReplay::ARCode>& _arCodes, wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& position, const wxSize& size, long style)
|
2009-02-23 06:15:48 +00:00
|
|
|
: wxDialog(parent, id, title, position, size, style)
|
2011-03-17 04:26:01 +00:00
|
|
|
, selection(_selection)
|
2015-02-24 23:32:17 +00:00
|
|
|
, arCodes(_arCodes)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
2014-11-22 21:44:31 +00:00
|
|
|
Bind(wxEVT_BUTTON, &CARCodeAddEdit::SaveCheatData, this, wxID_OK);
|
2014-11-09 00:26:20 +00:00
|
|
|
|
2009-06-28 20:38:01 +00:00
|
|
|
ActionReplay::ARCode tempEntries;
|
2011-01-05 04:35:46 +00:00
|
|
|
wxString currentName = _("Insert name here..");
|
2011-03-17 04:26:01 +00:00
|
|
|
|
2011-05-29 13:51:42 +00:00
|
|
|
if (selection == wxNOT_FOUND)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
|
|
|
tempEntries.name = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-28 04:37:38 +00:00
|
|
|
currentName = StrToWxStr(arCodes.at(selection).name);
|
2011-03-17 04:26:01 +00:00
|
|
|
tempEntries = arCodes.at(selection);
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxBoxSizer* sEditCheat = new wxBoxSizer(wxVERTICAL);
|
2009-05-06 18:53:27 +00:00
|
|
|
wxStaticBoxSizer* sbEntry = new wxStaticBoxSizer(wxVERTICAL, this, _("Cheat Code"));
|
2009-11-08 23:12:35 +00:00
|
|
|
wxGridBagSizer* sgEntry = new wxGridBagSizer(0, 0);
|
|
|
|
|
2014-11-09 00:26:20 +00:00
|
|
|
wxStaticText* EditCheatNameText = new wxStaticText(this, wxID_ANY, _("Name:"));
|
|
|
|
EditCheatName = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
|
2009-02-23 06:15:48 +00:00
|
|
|
EditCheatName->SetValue(currentName);
|
2014-11-09 00:26:20 +00:00
|
|
|
|
|
|
|
EntrySelection = new wxSpinButton(this);
|
2013-10-29 05:23:17 +00:00
|
|
|
EntrySelection->SetRange(1, ((int)arCodes.size()) > 0 ? (int)arCodes.size() : 1);
|
2011-03-17 04:26:01 +00:00
|
|
|
EntrySelection->SetValue((int)(arCodes.size() - selection));
|
2014-11-09 00:26:20 +00:00
|
|
|
EntrySelection->Bind(wxEVT_SPIN, &CARCodeAddEdit::ChangeEntry, this);
|
|
|
|
|
|
|
|
EditCheatCode = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(300, 100), wxTE_MULTILINE);
|
2009-02-23 06:15:48 +00:00
|
|
|
UpdateTextCtrl(tempEntries);
|
2009-05-06 18:53:27 +00:00
|
|
|
|
2015-02-15 19:43:31 +00:00
|
|
|
sgEntry->Add(EditCheatNameText, wxGBPosition(0, 0), wxGBSpan(1, 1), wxALIGN_CENTER | wxALL, 5);
|
|
|
|
sgEntry->Add(EditCheatName, wxGBPosition(0, 1), wxGBSpan(1, 1), wxEXPAND | wxALL, 5);
|
|
|
|
sgEntry->Add(EntrySelection, wxGBPosition(0, 2), wxGBSpan(2, 1), wxEXPAND | wxALL, 5);
|
|
|
|
sgEntry->Add(EditCheatCode, wxGBPosition(1, 0), wxGBSpan(1, 2), wxEXPAND | wxALL, 5);
|
2010-06-11 08:39:03 +00:00
|
|
|
sgEntry->AddGrowableCol(1);
|
|
|
|
sgEntry->AddGrowableRow(1);
|
2015-02-15 19:43:31 +00:00
|
|
|
sbEntry->Add(sgEntry, 1, wxEXPAND | wxALL);
|
2009-11-08 23:12:35 +00:00
|
|
|
|
2015-02-15 19:43:31 +00:00
|
|
|
sEditCheat->Add(sbEntry, 1, wxEXPAND | wxALL, 5);
|
2011-03-17 04:26:01 +00:00
|
|
|
sEditCheat->Add(CreateButtonSizer(wxOK | wxCANCEL), 0, wxEXPAND | wxALL, 5);
|
2009-11-08 23:12:35 +00:00
|
|
|
|
|
|
|
SetSizerAndFit(sEditCheat);
|
2011-03-17 04:26:01 +00:00
|
|
|
SetFocus();
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CARCodeAddEdit::ChangeEntry(wxSpinEvent& event)
|
|
|
|
{
|
2009-03-22 15:25:26 +00:00
|
|
|
ActionReplay::ARCode currentCode = arCodes.at((int)arCodes.size() - event.GetPosition());
|
2013-02-28 04:37:38 +00:00
|
|
|
EditCheatName->SetValue(StrToWxStr(currentCode.name));
|
2009-02-23 06:15:48 +00:00
|
|
|
UpdateTextCtrl(currentCode);
|
|
|
|
}
|
|
|
|
|
2015-02-15 19:43:31 +00:00
|
|
|
void CARCodeAddEdit::SaveCheatData(wxCommandEvent& WXUNUSED(event))
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
2011-05-29 13:51:42 +00:00
|
|
|
std::vector<ActionReplay::AREntry> decryptedLines;
|
|
|
|
std::vector<std::string> encryptedLines;
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2011-05-29 13:51:42 +00:00
|
|
|
// Split the entered cheat into lines.
|
|
|
|
std::vector<std::string> userInputLines;
|
2013-02-28 04:37:38 +00:00
|
|
|
SplitString(WxStrToStr(EditCheatCode->GetValue()), '\n', userInputLines);
|
2011-05-29 13:51:42 +00:00
|
|
|
|
2015-02-15 19:43:31 +00:00
|
|
|
for (size_t i = 0; i < userInputLines.size(); i++)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
2011-05-29 13:51:42 +00:00
|
|
|
// Make sure to ignore unneeded whitespace characters.
|
|
|
|
std::string line_str = StripSpaces(userInputLines[i]);
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2011-05-29 13:51:42 +00:00
|
|
|
if (line_str == "")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Let's parse the current line. Is it in encrypted or decrypted form?
|
|
|
|
std::vector<std::string> pieces;
|
2010-11-10 04:12:31 +00:00
|
|
|
SplitString(line_str, ' ', pieces);
|
2011-05-29 13:51:42 +00:00
|
|
|
|
2009-05-06 18:53:27 +00:00
|
|
|
if (pieces.size() == 2 && pieces[0].size() == 8 && pieces[1].size() == 8)
|
|
|
|
{
|
2011-05-29 13:51:42 +00:00
|
|
|
// Decrypted code line.
|
2014-03-12 19:33:41 +00:00
|
|
|
u32 addr = std::stoul(pieces[0], nullptr, 16);
|
|
|
|
u32 value = std::stoul(pieces[1], nullptr, 16);
|
2011-05-29 13:51:42 +00:00
|
|
|
|
2015-02-04 15:36:42 +00:00
|
|
|
decryptedLines.emplace_back(addr, value);
|
2011-05-29 13:51:42 +00:00
|
|
|
continue;
|
2009-05-06 18:53:27 +00:00
|
|
|
}
|
2011-05-29 13:51:42 +00:00
|
|
|
else if (pieces.size() == 1)
|
2009-05-06 18:53:27 +00:00
|
|
|
{
|
2010-11-10 04:12:31 +00:00
|
|
|
SplitString(line_str, '-', pieces);
|
2011-05-29 13:51:42 +00:00
|
|
|
|
|
|
|
if (pieces.size() == 3 && pieces[0].size() == 4 && pieces[1].size() == 4 && pieces[2].size() == 5)
|
2009-05-06 18:53:27 +00:00
|
|
|
{
|
2011-05-29 13:51:42 +00:00
|
|
|
// Encrypted code line. We'll have to decode it later.
|
|
|
|
encryptedLines.push_back(pieces[0] + pieces[1] + pieces[2]);
|
|
|
|
continue;
|
2009-05-06 18:53:27 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-29 13:51:42 +00:00
|
|
|
|
|
|
|
// If the above-mentioned conditions weren't met, then something went wrong.
|
2013-10-03 00:46:46 +00:00
|
|
|
if (!PanicYesNoT("Unable to parse line %u of the entered AR code as a valid "
|
2011-05-29 13:51:42 +00:00
|
|
|
"encrypted or decrypted code. Make sure you typed it correctly.\n"
|
2015-02-15 19:43:31 +00:00
|
|
|
"Would you like to ignore this line and continue parsing?", (unsigned)(i + 1)))
|
2009-05-06 18:53:27 +00:00
|
|
|
{
|
2011-05-29 13:51:42 +00:00
|
|
|
return;
|
2009-05-06 18:53:27 +00:00
|
|
|
}
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:51:42 +00:00
|
|
|
// If the entered code was in encrypted form, we decode it here.
|
|
|
|
if (encryptedLines.size())
|
|
|
|
{
|
|
|
|
// TODO: what if both decrypted AND encrypted lines are entered into a single AR code?
|
|
|
|
ActionReplay::DecryptARCode(encryptedLines, decryptedLines);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Codes with no lines appear to be deleted/hidden from the list. Let's prevent that.
|
|
|
|
if (!decryptedLines.size())
|
|
|
|
{
|
2014-07-25 01:46:46 +00:00
|
|
|
WxUtils::ShowErrorDialog(_("The resulting decrypted AR code doesn't contain any lines."));
|
2011-05-29 13:51:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (selection == wxNOT_FOUND)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
2011-05-29 13:51:42 +00:00
|
|
|
// Add a new AR cheat code.
|
2009-02-23 06:15:48 +00:00
|
|
|
ActionReplay::ARCode newCheat;
|
2009-05-06 18:53:27 +00:00
|
|
|
|
2013-02-28 04:37:38 +00:00
|
|
|
newCheat.name = WxStrToStr(EditCheatName->GetValue());
|
2011-05-29 13:51:42 +00:00
|
|
|
newCheat.ops = decryptedLines;
|
2009-02-23 06:15:48 +00:00
|
|
|
newCheat.active = true;
|
|
|
|
|
|
|
|
arCodes.push_back(newCheat);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-29 13:51:42 +00:00
|
|
|
// Update the currently-selected AR cheat code.
|
2013-02-28 04:37:38 +00:00
|
|
|
arCodes.at(selection).name = WxStrToStr(EditCheatName->GetValue());
|
2011-05-29 13:51:42 +00:00
|
|
|
arCodes.at(selection).ops = decryptedLines;
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AcceptAndClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CARCodeAddEdit::UpdateTextCtrl(ActionReplay::ARCode arCode)
|
|
|
|
{
|
|
|
|
EditCheatCode->Clear();
|
|
|
|
|
2009-05-06 18:53:27 +00:00
|
|
|
if (arCode.name != "")
|
2013-04-08 05:16:50 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& op : arCode.ops)
|
2014-05-17 17:17:28 +00:00
|
|
|
EditCheatCode->AppendText(wxString::Format("%08X %08X\n", op.cmd_addr, op.value));
|
2013-04-08 05:16:50 +00:00
|
|
|
}
|
2009-05-06 18:53:27 +00:00
|
|
|
else
|
2013-04-08 05:16:50 +00:00
|
|
|
{
|
2009-05-06 18:53:27 +00:00
|
|
|
EditCheatCode->SetValue(_("Insert Encrypted or Decrypted code here..."));
|
2013-04-08 05:16:50 +00:00
|
|
|
}
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|