-
Notifications
You must be signed in to change notification settings - Fork 101
/
CheatEditorActivity.java
executable file
·517 lines (479 loc) · 20.3 KB
/
CheatEditorActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/**
* Mupen64PlusAE, an N64 emulator for the Android platform
*
* Copyright (C) 2013 Paul Lamb
*
* This file is part of Mupen64PlusAE.
*
* Mupen64PlusAE is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Mupen64PlusAE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mupen64PlusAE. If
* not, see <http://www.gnu.org/licenses/>.
*
* Authors: xperia64
*/
package paulscode.android.mupen64plusae.cheat;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.mupen64plusae.v3.alpha.R;
import paulscode.android.mupen64plusae.ActivityHelper;
import paulscode.android.mupen64plusae.cheat.CheatUtils.Cheat;
import paulscode.android.mupen64plusae.compat.AppCompatListActivity;
import paulscode.android.mupen64plusae.dialog.Prompt;
import paulscode.android.mupen64plusae.dialog.Prompt.PromptTextListener;
import paulscode.android.mupen64plusae.persistent.AppData;
import paulscode.android.mupen64plusae.persistent.GlobalPrefs;
import paulscode.android.mupen64plusae.util.RomHeader;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class CheatEditorActivity extends AppCompatListActivity implements View.OnClickListener, OnItemLongClickListener
{
private static class CheatListAdapter extends ArrayAdapter<Cheat>
{
private static final int RESID = R.layout.list_item_two_text_icon;
public CheatListAdapter( Context context, List<Cheat> cheats )
{
super( context, RESID, cheats );
}
@Override
public View getView( int position, View convertView, ViewGroup parent )
{
Context context = getContext();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = convertView;
if( view == null )
view = inflater.inflate( RESID, null );
Cheat item = getItem( position );
if( item != null )
{
TextView text1 = (TextView) view.findViewById( R.id.text1 );
TextView text2 = (TextView) view.findViewById( R.id.text2 );
ImageView icon = (ImageView) view.findViewById( R.id.icon );
text1.setText( item.name );
text2.setText( item.desc );
icon.setImageResource( R.drawable.ic_key );
}
return view;
}
}
private final ArrayList<Cheat> cheats = new ArrayList<Cheat>();
private CheatListAdapter cheatListAdapter = null;
private AppData mAppData = null;
private GlobalPrefs mGlobalPrefs = null;
private RomHeader mRomHeader = null;
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
mAppData = new AppData( this );
mGlobalPrefs = new GlobalPrefs( this );
mGlobalPrefs.enforceLocale( this );
// Get the ROM header info
Bundle extras = getIntent().getExtras();
if( extras == null )
throw new Error( "ROM path must be passed via the extras bundle when starting CheatEditorActivity" );
String romPath = extras.getString( ActivityHelper.Keys.ROM_PATH );
if( TextUtils.isEmpty( romPath ) )
throw new Error( "ROM path must be passed via the extras bundle when starting CheatEditorActivity" );
mRomHeader = new RomHeader( new File( romPath ) );
setContentView( R.layout.cheat_editor );
reload( mRomHeader.crc );
findViewById( R.id.imgBtnChtAdd ).setOnClickListener( this );
findViewById( R.id.imgBtnChtEdit ).setOnClickListener( this );
findViewById( R.id.imgBtnChtSave ).setOnClickListener( this );
findViewById( R.id.imgBtnChtInfo ).setOnClickListener( this );
getListView().setOnItemLongClickListener( this );
}
private void reload( String crc )
{
Log.v( "CheatEditorActivity", "building from CRC = " + crc );
if( crc == null )
return;
// Get the appropriate section of the config file, using CRC as the key
CheatFile mupencheat_default = new CheatFile( mAppData.mupencheat_default );
CheatFile usrcheat_txt = new CheatFile( mGlobalPrefs.customCheats_txt );
cheats.addAll( CheatUtils.populate( crc, mupencheat_default, true, this ) );
cheats.addAll( CheatUtils.populate( crc, usrcheat_txt, false, this ) );
cheatListAdapter = new CheatListAdapter( this, cheats );
setListAdapter( cheatListAdapter );
}
private void save( String crc )
{
CheatFile usrcheat_txt = new CheatFile( mGlobalPrefs.customCheats_txt );
CheatFile mupencheat_txt = new CheatFile( mAppData.mupencheat_txt );
CheatUtils.save( crc, usrcheat_txt, cheats, mRomHeader, this, false );
CheatUtils.save( crc, mupencheat_txt, cheats, mRomHeader, this, true );
}
private boolean isHexNumber( String num )
{
try
{
Long.parseLong( num, 16 );
return true;
}
catch( NumberFormatException ex )
{
return false;
}
}
@Override
protected void onListItemClick( ListView l, View v, final int position, long id )
{
Cheat cheat = cheats.get( position );
StringBuilder message = new StringBuilder();
message.append( getString( R.string.cheatEditor_title2 ) + "\n" );
message.append( cheat.name + "\n" );
message.append( getString( R.string.cheatEditor_notes2 ) + "\n" );
message.append( cheat.desc + "\n" );
message.append( getString( R.string.cheatEditor_code2 ) + "\n" );
message.append( cheat.code );
if( !TextUtils.isEmpty( cheat.option ) && cheat.code.contains( "?" ) )
{
message.append( "\n" + getString( R.string.cheatEditor_option2 ) );
message.append( "\n" + cheat.option );
}
Builder builder = new Builder( this );
builder.setTitle( R.string.cheatEditor_info );
builder.setMessage( message.toString() );
builder.create().show();
}
@Override
public void onClick( View v )
{
AlertDialog alertDialog;
switch( v.getId() )
{
case R.id.imgBtnChtAdd:
Cheat cheat = new Cheat();
cheat.name = getString( R.string.cheatEditor_empty );
cheat.desc = getString( R.string.cheatNotes_none );
cheat.code = "";
cheat.option = "";
cheats.add( cheat );
cheatListAdapter = new CheatListAdapter( CheatEditorActivity.this, cheats );
setListAdapter( cheatListAdapter );
Toast t = Toast.makeText( CheatEditorActivity.this, getString( R.string.cheatEditor_added ), Toast.LENGTH_SHORT );
t.show();
break;
case R.id.imgBtnChtEdit:
alertDialog = new AlertDialog.Builder( CheatEditorActivity.this ).create();
alertDialog.setTitle( getString( R.string.cheatEditor_edit ) );
alertDialog.setMessage( getString( R.string.cheatEditor_edit_desc ) );
alertDialog.show();
break;
case R.id.imgBtnChtSave:
save( mRomHeader.crc );
CheatUtils.reset();
CheatEditorActivity.this.finish();
break;
case R.id.imgBtnChtInfo:
StringBuilder message = new StringBuilder();
message.append( getString( R.string.cheatEditor_readme1 ) + "\n\n" );
message.append( getString( R.string.cheatEditor_readme2 ) + "\n\n" );
message.append( getString( R.string.cheatEditor_readme3 ) + "\n\n" );
message.append( getString( R.string.cheatEditor_readme4 ) + "\n\n" );
message.append( getString( R.string.cheatEditor_readme5 ) + "\n\n" );
message.append( getString( R.string.cheatEditor_readme6 ) );
alertDialog = new AlertDialog.Builder( CheatEditorActivity.this ).create();
alertDialog.setTitle( getString( R.string.cheatEditor_help ) );
alertDialog.setMessage( message.toString() );
alertDialog.show();
break;
}
}
@SuppressLint( "InflateParams" )
@Override
public boolean onItemLongClick( AdapterView<?> av, View v, final int pos, long id )
{
final Cheat cheat = cheats.get( pos );
// Inflate the long-click dialog
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate( R.layout.cheat_editor_longclick_dialog, null );
// Build the alert dialog
Builder builder = new Builder( this );
builder.setTitle( R.string.cheatEditor_config );
builder.setMessage( R.string.cheatEditor_config_desc );
builder.setView( ll );
final AlertDialog parentDialog = builder.create();
// Define the handler for button clicks
final View.OnClickListener listener = new View.OnClickListener()
{
@Override
public void onClick( View v )
{
parentDialog.dismiss();
switch( v.getId() )
{
case R.id.btnEditTitle:
promptTitle( cheat );
break;
case R.id.btnEditNotes:
promptNotes( cheat );
break;
case R.id.btnEditCode:
promptCode( cheat );
break;
case R.id.btnEditOption:
promptOption( cheat );
break;
case R.id.btnDelete:
promptDelete( pos );
break;
}
}
};
// Assign the button click handler
ll.findViewById( R.id.btnEditTitle ).setOnClickListener( listener );
ll.findViewById( R.id.btnEditNotes ).setOnClickListener( listener );
ll.findViewById( R.id.btnEditCode ).setOnClickListener( listener );
ll.findViewById( R.id.btnEditOption ).setOnClickListener( listener );
ll.findViewById( R.id.btnDelete ).setOnClickListener( listener );
if( pos < CheatUtils.numberOfSystemCheats )
{
ll.findViewById( R.id.btnDelete ).setEnabled( false );
}
// Hide the edit option button if not applicable
if( !cheats.get( pos ).code.contains( "?" ) )
ll.findViewById( R.id.btnEditOption ).setVisibility( View.GONE );
// Show the long-click dialog
parentDialog.show();
return true;
}
@Override
// onBackPressed could probably be used
public boolean onKeyDown( int KeyCode, KeyEvent event )
{
if( KeyCode == KeyEvent.KEYCODE_BACK )
{
final OnClickListener listener = new OnClickListener()
{
@Override
public void onClick( DialogInterface dialog, int which )
{
if( which == DialogInterface.BUTTON_POSITIVE )
{
save( mRomHeader.crc );
}
CheatUtils.reset();
CheatEditorActivity.this.finish();
}
};
Builder builder = new Builder( this );
builder.setTitle( R.string.cheatEditor_saveConfirm );
builder.setPositiveButton( android.R.string.yes, listener );
builder.setNegativeButton( android.R.string.no, listener );
builder.create().show();
return true;
}
return super.onKeyDown( KeyCode, event );
}
private void promptTitle( final Cheat cheat )
{
CharSequence title = getText( R.string.cheatEditor_title );
CharSequence message = getText( R.string.cheatEditor_title_desc );
int inputType = InputType.TYPE_CLASS_TEXT;
Prompt.promptText( this, title, message, cheat.name, null, inputType, new PromptTextListener()
{
@Override
public void onDialogClosed( CharSequence text, int which )
{
if( which == DialogInterface.BUTTON_POSITIVE )
{
String str = text.toString().replace( '\n', ' ' );
cheat.name = str;
cheatListAdapter = new CheatListAdapter( CheatEditorActivity.this, cheats );
setListAdapter( cheatListAdapter );
}
}
} );
}
private void promptNotes( final Cheat cheat )
{
CharSequence title = getText( R.string.cheatEditor_notes );
CharSequence message = getText( R.string.cheatEditor_notes_desc );
CharSequence text;
if( TextUtils.isEmpty( cheat.desc ) || cheat.desc.equals( getString( R.string.cheatNotes_none ) ) )
{
text = "";
}
else
{
text = cheat.desc;
}
int inputType = InputType.TYPE_CLASS_TEXT;
Prompt.promptText( this, title, message, text, null, inputType, new PromptTextListener()
{
@Override
public void onDialogClosed( CharSequence text, int which )
{
if( which == DialogInterface.BUTTON_POSITIVE )
{
String str = text.toString().replace( '\n', ' ' );
if( TextUtils.isEmpty( str ) )
{
str = getString( R.string.cheatNotes_none );
}
cheat.desc = str;
}
}
} );
}
private void promptCode( final Cheat cheat )
{
CharSequence title = getText( R.string.cheatEditor_code );
CharSequence message = getText( R.string.cheatEditor_code_desc );
int inputType = InputType.TYPE_CLASS_TEXT;
Prompt.promptText( this, title, message, cheat.code, null, inputType, new PromptTextListener()
{
@Override
public void onDialogClosed( CharSequence text, int which )
{
if( which == DialogInterface.BUTTON_POSITIVE )
{
String verify = text.toString();
String[] split = verify.split( "\n" );
boolean bad = false;
for( int o = 0; o < split.length; o++ )
{
if( split[o].length() != 13 )
{
bad = true;
break;
}
if( split[o].indexOf( ' ' ) != -1 )
{
if( !isHexNumber( split[o].substring( 0, split[o].indexOf( ' ' ) ) ) )
{
bad = true;
break;
}
if( !isHexNumber( split[o].substring( split[o].indexOf( ' ' ) + 1 ) ) && !split[o].substring( split[o].indexOf( ' ' ) + 1 ).equals( "????" ) )
{
bad = true;
break;
}
}
else
{
bad = true;
break;
}
}
if( !bad )
{
cheat.code = text.toString().toUpperCase( Locale.US );
}
else
{
Toast t = Toast.makeText( CheatEditorActivity.this, getString( R.string.cheatEditor_badCode ), Toast.LENGTH_SHORT );
t.show();
}
}
}
} );
}
private void promptOption( final Cheat cheat )
{
CharSequence title = getText( R.string.cheatEditor_option );
CharSequence message = getText( R.string.cheatEditor_option_desc );
int inputType = InputType.TYPE_CLASS_TEXT;
Prompt.promptText( this, title, message, cheat.option, null, inputType, new PromptTextListener()
{
@Override
public void onDialogClosed( CharSequence text, int which )
{
if( which == DialogInterface.BUTTON_POSITIVE )
{
String verify = text.toString();
String[] split = verify.split( "\n" );
boolean bad = false;
verify = "";
for( int o = 0; o < split.length; o++ )
{
if( split[o].length() <= 5 )
{
bad = true;
break;
}
if( !isHexNumber( split[o].substring( split[o].length() - 4 ) ) )
{
bad = true;
break;
}
if( split[o].lastIndexOf( ' ' ) != split[o].length() - 5 )
{
bad = true;
break;
}
split[o] = split[o].substring( 0, split[o].length() - 5 ) + " " + split[o].substring( split[o].length() - 4 ).toUpperCase( Locale.US );
String y = "";
if( o != split.length - 1 )
{
y = "\n";
}
verify += split[o] + y;
}
if( !bad )
{
cheat.option = verify;
}
else
{
Toast t = Toast.makeText( CheatEditorActivity.this, getString( R.string.cheatEditor_badOption ), Toast.LENGTH_SHORT );
t.show();
}
}
}
} );
}
private void promptDelete( final int pos )
{
final OnClickListener listener = new OnClickListener()
{
@Override
public void onClick( DialogInterface dialog, int which )
{
if( which == DialogInterface.BUTTON_POSITIVE )
{
cheats.remove( pos );
cheatListAdapter = new CheatListAdapter( CheatEditorActivity.this, cheats );
setListAdapter( cheatListAdapter );
}
}
};
Builder builder = new Builder( this );
builder.setTitle( R.string.cheatEditor_delete );
builder.setMessage( R.string.cheatEditor_confirm );
builder.setPositiveButton( android.R.string.yes, listener );
builder.setNegativeButton( android.R.string.no, listener );
builder.create().show();
}
}