2016-07-21 16:22:43 +00:00
using System ;
2013-09-14 03:13:22 +00:00
using System.Collections.Generic ;
2011-02-17 16:27:39 +00:00
using System.Drawing ;
2013-09-14 03:13:22 +00:00
using System.Linq ;
2011-02-17 16:27:39 +00:00
using System.Windows.Forms ;
2013-10-25 00:57:23 +00:00
using BizHawk.Client.Common ;
2013-11-03 03:54:37 +00:00
namespace BizHawk.Client.EmuHawk
2011-02-17 16:27:39 +00:00
{
2011-06-19 23:39:25 +00:00
public partial class RamPoke : Form
{
2013-11-29 19:55:05 +00:00
// TODO: don't use textboxes as labels
2013-09-14 03:13:22 +00:00
private List < Watch > _watchList = new List < Watch > ( ) ;
2017-04-19 12:50:25 +00:00
public Point InitialLocation { get ; set ; } = new Point ( 0 , 0 ) ;
2011-06-19 23:39:25 +00:00
public RamPoke ( )
{
InitializeComponent ( ) ;
}
2015-01-19 01:39:47 +00:00
public IToolForm ParentTool { get ; set ; }
2013-11-25 02:08:45 +00:00
public void SetWatch ( IEnumerable < Watch > watches )
2013-09-14 03:13:22 +00:00
{
2013-11-25 02:08:45 +00:00
_watchList = watches . ToList ( ) ;
2013-09-14 03:13:22 +00:00
}
private void UnSupportedConfiguration ( )
2011-06-19 23:39:25 +00:00
{
2016-07-21 16:22:43 +00:00
MessageBox . Show ( "RAM Poke does not support mixed types" , "Unsupported Options" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2013-09-14 03:13:22 +00:00
Close ( ) ;
2011-06-19 23:39:25 +00:00
}
private void RamPoke_Load ( object sender , EventArgs e )
{
2013-11-29 19:55:05 +00:00
_watchList = _watchList . Where ( x = > ! x . IsSeparator ) . ToList ( ) ; // Weed out separators just in case
2011-09-17 14:38:21 +00:00
2013-09-14 03:13:22 +00:00
if ( _watchList . Count = = 0 )
2012-09-03 23:42:00 +00:00
{
2013-09-14 03:13:22 +00:00
ValueBox . Enabled = false ;
return ;
2012-09-03 23:42:00 +00:00
}
2013-09-14 03:13:22 +00:00
if ( InitialLocation . X > 0 | | InitialLocation . Y > 0 )
2012-09-03 23:42:00 +00:00
{
2013-09-14 03:13:22 +00:00
Location = InitialLocation ;
2012-09-03 23:42:00 +00:00
}
2011-06-19 23:39:25 +00:00
2013-09-14 03:13:22 +00:00
if ( _watchList . Count > 1 )
2013-04-14 23:56:45 +00:00
{
2015-03-22 16:55:34 +00:00
bool hasMixedSizes = _watchList . Select ( x = > x . Size ) . Distinct ( ) . Count ( ) > 1 ;
bool hasMixedTypes = _watchList . Select ( x = > x . Type ) . Distinct ( ) . Count ( ) > 1 ;
bool hasMixedEndian = _watchList . Select ( x = > x . BigEndian ) . Distinct ( ) . Count ( ) > 1 ;
2011-06-19 23:39:25 +00:00
2013-09-14 03:13:22 +00:00
if ( hasMixedSizes | | hasMixedTypes | | hasMixedEndian )
{
UnSupportedConfiguration ( ) ;
}
}
2013-11-29 19:55:05 +00:00
2013-10-11 17:57:58 +00:00
AddressBox . SetHexProperties ( _watchList [ 0 ] . Domain . Size ) ;
2019-03-18 14:06:37 +00:00
AddressBox . Text = ( _watchList . Count > 10 ? _watchList . Take ( 10 ) : _watchList ) // Hack in case an absurd amount of addresses are picked, this can be slow and create too long of a string
. Select ( a = > a . AddressString )
. Distinct ( )
. Aggregate ( ( addrStr , nextStr ) = > $"{addrStr},{nextStr}" ) ;
2015-01-19 02:04:04 +00:00
2015-03-22 16:55:34 +00:00
ValueBox . ByteSize = _watchList [ 0 ] . Size ;
ValueBox . Type = _watchList [ 0 ] . Type ;
2017-05-10 11:45:23 +00:00
ValueHexLabel . Text = _watchList [ 0 ] . Type = = DisplayType . Hex ? "0x" : "" ;
ValueBox . Text = _watchList [ 0 ] . ValueString . Replace ( " " , "" ) ;
2013-09-14 03:13:22 +00:00
DomainLabel . Text = _watchList [ 0 ] . Domain . Name ;
SizeLabel . Text = _watchList [ 0 ] . Size . ToString ( ) ;
DisplayTypeLabel . Text = Watch . DisplayTypeToString ( _watchList [ 0 ] . Type ) ;
BigEndianLabel . Text = _watchList [ 0 ] . BigEndian ? "Big Endian" : "Little Endian" ;
SetTitle ( ) ;
2011-06-19 23:39:25 +00:00
}
2013-09-14 03:13:22 +00:00
private void SetTitle ( )
2011-06-19 23:39:25 +00:00
{
2019-03-18 14:06:37 +00:00
Text = $"RAM Poke - {_watchList[0].Domain.Name}" ;
2011-06-19 23:39:25 +00:00
}
private void Cancel_Click ( object sender , EventArgs e )
{
2013-09-14 03:13:22 +00:00
DialogResult = DialogResult . Cancel ;
2013-04-14 23:56:45 +00:00
Close ( ) ;
2011-06-19 23:39:25 +00:00
}
2013-11-29 19:55:05 +00:00
private void Ok_Click ( object sender , EventArgs e )
2011-06-19 23:39:25 +00:00
{
2013-11-29 19:55:05 +00:00
var success = _watchList . All ( watch = > watch . Poke ( ValueBox . Text ) ) ;
2015-01-30 16:15:08 +00:00
2017-04-19 12:50:25 +00:00
ParentTool ? . UpdateValues ( ) ;
2015-01-19 01:39:47 +00:00
2015-01-30 16:15:08 +00:00
if ( success )
{
DialogResult = DialogResult . OK ;
Close ( ) ;
}
else
{
OutputLabel . Text = "An error occured when writing Value." ;
}
2011-06-19 23:39:25 +00:00
}
}
2011-02-17 16:27:39 +00:00
}