Permalink
Browse files

front: Generate cheat args directly from persistent data.

Eliminate dependency on the cheats preference tree.
  • Loading branch information...
littleguy77 committed May 2, 2015
1 parent 5d1165f commit 335dfafc38ecf8013489530361d4a1dc5450cf1b
@@ -2,7 +2,10 @@
import java.util.Collections;
import java.util.HashSet;
+import java.util.Map;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.mupen64plusae.v3.alpha.R;
@@ -177,13 +180,16 @@
private final SharedPreferences mPreferences;
+ private final RomHeader mHeader;
+
public GamePrefs( Context context, String romMd5, RomHeader header )
{
final AppData appData = new AppData( context );
final GlobalPrefs globalPrefs = new GlobalPrefs( context );
sharedPrefsName = romMd5.replace(' ', '_' ) + "_preferences";
mPreferences = context.getSharedPreferences( sharedPrefsName, Context.MODE_PRIVATE );
+ mHeader = header;
// Game-specific data
gameDataDir = String.format( "%s/GameData/%s %s %s", globalPrefs.userDataDir, header.name, header.countrySymbol, romMd5 );
@@ -357,6 +363,37 @@ public GamePrefs( Context context, String romMd5, RomHeader header )
isPlugged4 = isControllerEnabled4;
}
+ public String getCheatArgs()
+ {
+ if( !isCheatOptionsShown )
+ return "";
+
+ final Pattern pattern = Pattern.compile( "^" + mHeader.crc + " Cheat(\\d+)" );
+ StringBuilder builder = null;
+ Map<String, ?> map = mPreferences.getAll();
+ for (String key : map.keySet())
+ {
+ Matcher matcher = pattern.matcher( key );
+ if ( matcher.matches() && matcher.groupCount() > 0 )
+ {
+ int value = mPreferences.getInt( key, 0 );
+ if (value > 0)
+ {
+ int index = Integer.parseInt( matcher.group( 1 ) );
+
+ if (builder == null)
+ builder = new StringBuilder();
+ else
+ builder.append( ',' );
+ builder.append( index );
+ builder.append( '-' );
+ builder.append( value - 1 );
+ }
+ }
+ }
+ return builder == null ? "" : builder.toString();
+ }
+
private static Profile loadProfile( SharedPreferences prefs, String key, String defaultName,
String customPath, String builtinPath )
{
@@ -50,7 +50,6 @@
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
-import android.preference.PreferenceCategory;
import android.preference.PreferenceGroup;
import android.preference.PreferenceManager;
import android.text.TextUtils;
@@ -439,33 +438,7 @@ private void launchGame( boolean isRestarting )
}
// Launch the game activity
- ActivityHelper.startGameActivity( this, mRomPath, mRomMd5, getCheatArgs(), isRestarting, mGlobalPrefs.isTouchpadEnabled );
- }
-
- @SuppressWarnings( "deprecation" )
- private String getCheatArgs()
- {
- String cheatArgs = null;
-
- PreferenceCategory cheatsCategory = (PreferenceCategory) findPreference( CATEGORY_CHEATS );
- if( cheatsCategory != null )
- {
- for( int i = 0; i < cheatsCategory.getPreferenceCount(); i++ )
- {
- CheatPreference pref = (CheatPreference) cheatsCategory.getPreference( i );
- if( pref.isCheatEnabled() )
- {
- if( cheatArgs == null )
- cheatArgs = ""; // First time through
- else
- cheatArgs += ",";
-
- cheatArgs += pref.getCheatCodeString( i );
- }
- }
- }
-
- return cheatArgs;
+ ActivityHelper.startGameActivity( this, mRomPath, mRomMd5, mGamePrefs.getCheatArgs(), isRestarting, mGlobalPrefs.isTouchpadEnabled );
}
private void actionResetGamePrefs()

0 comments on commit 335dfaf

Please sign in to comment.