2013-10-05 15:45:39 +00:00
using System ;
2014-07-28 02:15:21 +00:00
using System.Linq ;
2013-10-05 15:45:39 +00:00
using System.Windows.Forms ;
2013-10-25 00:57:23 +00:00
using BizHawk.Client.Common ;
2015-11-28 21:47:16 +00:00
using Emu = BizHawk . Emulation . Common ;
2015-01-25 16:20:53 +00:00
using BizHawk.Emulation.Common.IEmulatorExtensions ;
2013-10-25 00:57:23 +00:00
2015-11-28 21:47:16 +00:00
2013-11-03 03:54:37 +00:00
namespace BizHawk.Client.EmuHawk
2013-10-05 15:45:39 +00:00
{
public partial class CheatEdit : UserControl
{
2015-11-28 21:47:16 +00:00
public Emu . IMemoryDomains MemoryDomains { get ; set ; }
2014-09-01 18:43:41 +00:00
2013-10-05 15:45:39 +00:00
public CheatEdit ( )
{
InitializeComponent ( ) ;
2013-10-05 20:22:55 +00:00
AddressBox . Nullable = false ;
ValueBox . Nullable = false ;
2013-10-05 15:45:39 +00:00
}
2013-10-05 20:22:55 +00:00
#region Privates
private const string HexInd = "0x" ;
2014-01-01 15:50:33 +00:00
private Cheat _cheat ;
private bool _loading ;
private bool _editmode ;
private Action _addCallback ;
private Action _editCallback ;
2013-10-05 20:22:55 +00:00
2013-10-05 15:45:39 +00:00
private void CheatEdit_Load ( object sender , EventArgs e )
2015-11-05 03:23:27 +00:00
{
Restart ( ) ;
}
public void Restart ( )
2013-10-05 15:45:39 +00:00
{
2015-01-14 21:55:48 +00:00
if ( MemoryDomains ! = null ) // the designer needs this check
2013-10-05 20:22:55 +00:00
{
2014-07-28 02:15:21 +00:00
DomainDropDown . Items . Clear ( ) ;
2015-01-14 21:55:48 +00:00
DomainDropDown . Items . AddRange ( MemoryDomains
2015-01-25 16:20:53 +00:00
. Where ( d = > d . CanPoke ( ) )
2014-07-28 02:15:21 +00:00
. Select ( d = > d . ToString ( ) )
. ToArray ( ) ) ;
2014-09-13 14:21:08 +00:00
2015-01-24 15:49:02 +00:00
if ( MemoryDomains . HasSystemBus )
2014-09-13 14:21:08 +00:00
{
2015-01-24 15:49:02 +00:00
DomainDropDown . SelectedItem = MemoryDomains . SystemBus . ToString ( ) ;
2014-09-13 14:21:08 +00:00
}
else
{
2015-01-14 21:55:48 +00:00
DomainDropDown . SelectedItem = MemoryDomains . MainMemory . ToString ( ) ;
2014-09-13 14:21:08 +00:00
}
2013-10-05 20:22:55 +00:00
}
2014-01-01 15:50:33 +00:00
2013-10-05 15:45:39 +00:00
SetFormToDefault ( ) ;
}
private void SetFormToCheat ( )
{
_loading = true ;
SetSizeSelected ( _cheat . Size ) ;
PopulateTypeDropdown ( ) ;
SetTypeSelected ( _cheat . Type ) ;
SetDomainSelected ( _cheat . Domain ) ;
AddressBox . SetHexProperties ( _cheat . Domain . Size ) ;
ValueBox . ByteSize =
CompareBox . ByteSize =
_cheat . Size ;
ValueBox . Type =
CompareBox . Type =
_cheat . Type ;
ValueHexIndLabel . Text =
CompareHexIndLabel . Text =
2015-11-28 21:47:16 +00:00
_cheat . Type = = DisplayType . Hex ? HexInd : string . Empty ;
2013-10-05 15:45:39 +00:00
BigEndianCheckBox . Checked = _cheat . BigEndian . Value ;
2015-10-15 06:21:05 +00:00
NameBox . Text = _cheat . Name ;
AddressBox . Text = _cheat . AddressStr ;
ValueBox . Text = _cheat . ValueStr ;
CompareBox . Text = _cheat . Compare . HasValue ? _cheat . CompareStr : String . Empty ;
2016-02-10 04:33:44 +00:00
if ( _cheat . ComparisonType . Equals ( Cheat . COMPARISONTYPE . NONE ) )
{
CompareTypeDropDown . SelectedIndex = 0 ;
}
else
{
CompareTypeDropDown . SelectedIndex = ( ( int ) _cheat . ComparisonType - 1 ) ;
}
2015-10-15 06:21:05 +00:00
2013-10-05 15:45:39 +00:00
CheckFormState ( ) ;
2013-11-05 23:14:22 +00:00
if ( ! _cheat . Compare . HasValue )
{
2014-01-01 15:50:33 +00:00
CompareBox . Text = String . Empty ; // Necessary hack until WatchValueBox.ToRawInt() becomes nullable
2013-11-05 23:14:22 +00:00
}
2014-01-01 15:50:33 +00:00
2013-10-05 15:45:39 +00:00
_loading = false ;
}
private void SetFormToDefault ( )
{
_loading = true ;
2015-11-28 21:47:16 +00:00
SetSizeSelected ( WatchSize . Byte ) ;
2013-10-05 15:45:39 +00:00
PopulateTypeDropdown ( ) ;
2014-11-23 16:03:04 +00:00
NameBox . Text = string . Empty ;
2013-10-05 20:22:55 +00:00
2015-01-14 21:55:48 +00:00
if ( MemoryDomains ! = null )
2013-10-05 20:22:55 +00:00
{
2015-01-24 15:49:02 +00:00
AddressBox . SetHexProperties ( MemoryDomains . SystemBus . Size ) ;
2013-10-05 20:22:55 +00:00
}
2013-10-05 15:45:39 +00:00
ValueBox . ByteSize =
CompareBox . ByteSize =
2015-11-28 21:47:16 +00:00
WatchSize . Byte ;
2013-10-05 15:45:39 +00:00
ValueBox . Type =
CompareBox . Type =
2015-11-28 21:47:16 +00:00
DisplayType . Hex ;
2013-10-05 15:45:39 +00:00
ValueBox . ResetText ( ) ;
CompareBox . ResetText ( ) ;
ValueHexIndLabel . Text =
CompareHexIndLabel . Text =
HexInd ;
BigEndianCheckBox . Checked = false ;
2015-11-28 21:47:16 +00:00
SetTypeSelected ( DisplayType . Hex ) ;
2013-10-05 20:22:55 +00:00
2013-10-05 15:45:39 +00:00
CheckFormState ( ) ;
2014-09-01 18:43:41 +00:00
CompareBox . Text = string . Empty ; // TODO: A needed hack until WatchValueBox.ToRawInt() becomes nullable
2013-10-05 15:45:39 +00:00
_loading = false ;
}
2015-11-28 21:47:16 +00:00
private void SetSizeSelected ( WatchSize size )
2013-10-05 15:45:39 +00:00
{
switch ( size )
{
default :
2015-11-28 21:47:16 +00:00
case WatchSize . Byte :
2013-10-05 15:45:39 +00:00
SizeDropDown . SelectedIndex = 0 ;
break ;
2015-11-28 21:47:16 +00:00
case WatchSize . Word :
2013-10-05 15:45:39 +00:00
SizeDropDown . SelectedIndex = 1 ;
break ;
2015-11-28 21:47:16 +00:00
case WatchSize . DWord :
2013-10-05 15:45:39 +00:00
SizeDropDown . SelectedIndex = 2 ;
break ;
}
}
2015-11-28 21:47:16 +00:00
private void SetTypeSelected ( Common . DisplayType type )
2013-10-05 15:45:39 +00:00
{
foreach ( var item in DisplayTypeDropDown . Items )
{
if ( item . ToString ( ) = = Watch . DisplayTypeToString ( type ) )
{
DisplayTypeDropDown . SelectedItem = item ;
return ;
}
}
}
2015-11-28 21:47:16 +00:00
private void SetDomainSelected ( Emu . MemoryDomain domain )
2013-10-05 15:45:39 +00:00
{
foreach ( var item in DomainDropDown . Items )
{
if ( item . ToString ( ) = = domain . Name )
{
DomainDropDown . SelectedItem = item ;
return ;
}
}
}
private void PopulateTypeDropdown ( )
{
DisplayTypeDropDown . Items . Clear ( ) ;
switch ( SizeDropDown . SelectedIndex )
{
default :
case 0 :
2015-11-28 21:47:16 +00:00
foreach ( DisplayType t in ByteWatch . ValidTypes )
{
DisplayTypeDropDown . Items . Add ( Watch . DisplayTypeToString ( t ) ) ;
}
2013-10-05 15:45:39 +00:00
break ;
case 1 :
2015-11-28 21:47:16 +00:00
foreach ( DisplayType t in WordWatch . ValidTypes )
{
DisplayTypeDropDown . Items . Add ( Watch . DisplayTypeToString ( t ) ) ;
}
2013-10-05 15:45:39 +00:00
break ;
case 2 :
2015-11-28 21:47:16 +00:00
foreach ( DisplayType t in DWordWatch . ValidTypes )
{
DisplayTypeDropDown . Items . Add ( Watch . DisplayTypeToString ( t ) ) ;
}
2013-10-05 15:45:39 +00:00
break ;
}
2014-01-01 15:50:33 +00:00
2013-10-05 15:45:39 +00:00
DisplayTypeDropDown . SelectedItem = DisplayTypeDropDown . Items [ 0 ] ;
}
private void CheckFormState ( )
{
2014-01-01 15:50:33 +00:00
var valid = ! String . IsNullOrWhiteSpace ( AddressBox . Text ) & & ! String . IsNullOrWhiteSpace ( ValueBox . Text ) ;
2013-10-05 20:22:55 +00:00
AddButton . Enabled = valid ;
EditButton . Enabled = _editmode & & valid ;
2013-10-05 15:45:39 +00:00
}
private void SizeDropDown_SelectedIndexChanged ( object sender , EventArgs e )
{
if ( ! _loading )
{
PopulateTypeDropdown ( ) ;
ValueBox . ByteSize =
CompareBox . ByteSize =
GetCurrentSize ( ) ;
}
}
private void DomainDropDown_SelectedIndexChanged ( object sender , EventArgs e )
{
if ( ! _loading )
{
2015-01-14 21:55:48 +00:00
var domain = MemoryDomains [ DomainDropDown . SelectedItem . ToString ( ) ] ;
2013-10-05 15:45:39 +00:00
AddressBox . SetHexProperties ( domain . Size ) ;
}
}
2015-11-28 21:47:16 +00:00
private WatchSize GetCurrentSize ( )
2013-10-05 15:45:39 +00:00
{
switch ( SizeDropDown . SelectedIndex )
{
default :
case 0 :
2015-11-28 21:47:16 +00:00
return WatchSize . Byte ;
2013-10-05 15:45:39 +00:00
case 1 :
2015-11-28 21:47:16 +00:00
return WatchSize . Word ;
2013-10-05 15:45:39 +00:00
case 2 :
2015-11-28 21:47:16 +00:00
return WatchSize . DWord ;
2013-10-05 15:45:39 +00:00
}
}
2013-10-05 20:22:55 +00:00
private void DisplayTypeDropDown_SelectedIndexChanged ( object sender , EventArgs e )
{
ValueBox . Type =
CompareBox . Type =
Watch . StringToDisplayType ( DisplayTypeDropDown . SelectedItem . ToString ( ) ) ;
}
private void AddButton_Click ( object sender , EventArgs e )
{
if ( _addCallback ! = null )
{
_addCallback ( ) ;
}
}
private void EditButton_Click ( object sender , EventArgs e )
{
if ( _editCallback ! = null )
{
_editCallback ( ) ;
}
}
#endregion
2013-10-05 15:45:39 +00:00
#region API
2013-10-06 16:40:51 +00:00
public void SetCheat ( Cheat cheat )
2013-10-05 15:45:39 +00:00
{
2013-10-05 20:22:55 +00:00
_editmode = true ;
2013-10-05 15:45:39 +00:00
_cheat = cheat ;
if ( cheat . IsSeparator )
{
SetFormToDefault ( ) ;
}
else
{
SetFormToCheat ( ) ;
}
}
2013-10-05 20:22:55 +00:00
public void ClearForm ( )
{
2013-10-06 16:40:51 +00:00
_cheat = Cheat . Separator ;
2013-10-05 23:16:35 +00:00
_editmode = false ;
SetFormToDefault ( ) ;
2013-10-05 20:22:55 +00:00
}
2014-01-01 15:50:33 +00:00
public Cheat OriginalCheat
{
get { return _cheat ; }
}
2016-11-12 15:00:11 +00:00
public Cheat GetCheat ( )
2013-10-05 20:22:55 +00:00
{
2016-11-12 15:00:11 +00:00
Cheat . COMPARISONTYPE comparisonType = Cheat . COMPARISONTYPE . NONE ;
var domain = MemoryDomains [ DomainDropDown . SelectedItem . ToString ( ) ] ;
var address = AddressBox . ToRawInt ( ) . Value ;
if ( address < domain . Size )
2013-10-05 20:22:55 +00:00
{
2016-11-12 15:00:11 +00:00
var watch = Watch . GenerateWatch (
MemoryDomains [ DomainDropDown . SelectedItem . ToString ( ) ] ,
AddressBox . ToRawInt ( ) . Value ,
GetCurrentSize ( ) ,
Watch . StringToDisplayType ( DisplayTypeDropDown . SelectedItem . ToString ( ) ) ,
BigEndianCheckBox . Checked ,
NameBox . Text
) ;
switch ( CompareTypeDropDown . SelectedItem . ToString ( ) )
2014-03-23 15:58:44 +00:00
{
2016-11-12 15:00:11 +00:00
case "" : comparisonType = Cheat . COMPARISONTYPE . NONE ; break ;
case "=" : comparisonType = Cheat . COMPARISONTYPE . EQUAL ; break ;
case ">" : comparisonType = Cheat . COMPARISONTYPE . GREATER_THAN ; break ;
case ">=" : comparisonType = Cheat . COMPARISONTYPE . GREATER_THAN_OR_EQUAL ; break ;
case "<" : comparisonType = Cheat . COMPARISONTYPE . LESS_THAN ; break ;
case "<=" : comparisonType = Cheat . COMPARISONTYPE . LESS_THAN_OR_EQUAL ; break ;
case "!=" : comparisonType = Cheat . COMPARISONTYPE . NOT_EQUAL ; break ;
default : comparisonType = Cheat . COMPARISONTYPE . NONE ; break ;
}
2016-02-03 04:32:46 +00:00
2016-11-12 15:00:11 +00:00
int? c = CompareBox . ToRawInt ( ) = = null ? null : ( int? ) CompareBox . ToRawInt ( ) . Value ;
2016-02-08 05:40:14 +00:00
2016-11-12 15:00:11 +00:00
return new Cheat (
watch ,
ValueBox . ToRawInt ( ) . Value ,
CompareBox . ToRawInt ( ) = = null ? null : ( int? ) CompareBox . ToRawInt ( ) . Value ,
true ,
comparisonType
) ;
2016-02-03 04:32:46 +00:00
2016-11-12 15:00:11 +00:00
}
else
{
MessageBox . Show ( address . ToString ( ) + " is not a valid address for the domain " + domain . Name , "Index out of range" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
return Cheat . Separator ;
2013-10-05 20:22:55 +00:00
}
}
2014-01-01 15:50:33 +00:00
public void SetAddEvent ( Action addCallback )
2013-10-05 20:22:55 +00:00
{
2014-01-01 15:50:33 +00:00
_addCallback = addCallback ;
2013-10-05 20:22:55 +00:00
}
2013-10-05 15:45:39 +00:00
2014-01-01 15:50:33 +00:00
public void SetEditEvent ( Action editCallback )
2013-10-05 20:22:55 +00:00
{
2014-01-01 15:50:33 +00:00
_editCallback = editCallback ;
2013-10-05 20:22:55 +00:00
}
2013-10-05 15:45:39 +00:00
2013-10-05 20:22:55 +00:00
#endregion
2016-02-09 04:56:11 +00:00
private void CompareBox_TextChanged ( object sender , EventArgs e )
{
WatchValueBox compareBox = ( WatchValueBox ) sender ;
PopulateComparisonTypeBox ( String . IsNullOrWhiteSpace ( compareBox . Text ) ) ;
}
/// <summary>
/// Populates the comparison type drop down
/// </summary>
/// <param name="empty">True if drop down should be left empty</param>
private void PopulateComparisonTypeBox ( bool empty = false )
{
// Don't need to do anything in this case
2016-02-10 04:33:44 +00:00
if ( empty & & this . CompareTypeDropDown . Items . Count = = 1 )
{
return ;
}
// Don't need to do anything in this case
if ( ! empty & & this . CompareTypeDropDown . Items . Count = = 6 )
2016-02-09 04:56:11 +00:00
{
return ;
}
this . CompareTypeDropDown . Items . Clear ( ) ;
2016-02-10 04:33:44 +00:00
if ( empty )
{
this . CompareTypeDropDown . Items . AddRange ( new object [ ] {
""
} ) ;
}
else
2016-02-09 04:56:11 +00:00
{
this . CompareTypeDropDown . Items . AddRange ( new object [ ] {
"=" ,
">" ,
">=" ,
"<" ,
"<=" ,
"!="
} ) ;
}
2016-02-10 04:33:44 +00:00
this . CompareTypeDropDown . SelectedIndex = 0 ;
2016-02-09 04:56:11 +00:00
}
2013-10-05 15:45:39 +00:00
}
}