Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #7292 from mahdihijazi/settings_refactor
Android: Settings refactor & Fix custom game settings
  • Loading branch information
degasus committed Aug 9, 2018
2 parents 7be818a + f41785e commit bfbf9ed
Show file tree
Hide file tree
Showing 44 changed files with 1,711 additions and 1,536 deletions.
2 changes: 1 addition & 1 deletion Source/Android/app/src/main/AndroidManifest.xml
Expand Up @@ -54,7 +54,7 @@
</activity>

<activity
android:name=".ui.settings.SettingsActivity"
android:name=".features.settings.ui.SettingsActivity"
android:theme="@style/DolphinSettingsGamecube"
android:label="@string/preferences_settings"/>

Expand Down
Expand Up @@ -14,8 +14,8 @@
import org.dolphinemu.dolphinemu.activities.EmulationActivity;
import org.dolphinemu.dolphinemu.model.GameFile;
import org.dolphinemu.dolphinemu.services.DirectoryInitializationService;
import org.dolphinemu.dolphinemu.ui.settings.MenuTag;
import org.dolphinemu.dolphinemu.ui.settings.SettingsActivity;
import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag;
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsActivity;
import org.dolphinemu.dolphinemu.utils.PicassoUtils;
import org.dolphinemu.dolphinemu.viewholders.GameViewHolder;

Expand Down
Expand Up @@ -17,8 +17,8 @@
import org.dolphinemu.dolphinemu.model.GameFile;
import org.dolphinemu.dolphinemu.services.DirectoryInitializationService;
import org.dolphinemu.dolphinemu.ui.platform.Platform;
import org.dolphinemu.dolphinemu.ui.settings.MenuTag;
import org.dolphinemu.dolphinemu.ui.settings.SettingsActivity;
import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag;
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsActivity;
import org.dolphinemu.dolphinemu.utils.PicassoUtils;
import org.dolphinemu.dolphinemu.viewholders.TvGameViewHolder;

Expand Down
Expand Up @@ -8,7 +8,7 @@
import android.view.KeyEvent;
import android.view.MotionEvent;

import org.dolphinemu.dolphinemu.model.settings.view.InputBindingSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.InputBindingSetting;
import org.dolphinemu.dolphinemu.utils.ControllerMappingHelper;
import org.dolphinemu.dolphinemu.utils.Log;

Expand Down
@@ -1,12 +1,12 @@
package org.dolphinemu.dolphinemu.model.settings;
package org.dolphinemu.dolphinemu.features.settings.model;

public final class BooleanSetting extends Setting
{
private boolean mValue;

public BooleanSetting(String key, String section, int file, boolean value)
public BooleanSetting(String key, String section, boolean value)
{
super(key, section, file);
super(key, section);
mValue = value;
}

Expand Down
@@ -1,12 +1,12 @@
package org.dolphinemu.dolphinemu.model.settings;
package org.dolphinemu.dolphinemu.features.settings.model;

public final class FloatSetting extends Setting
{
private float mValue;

public FloatSetting(String key, String section, int file, float value)
public FloatSetting(String key, String section, float value)
{
super(key, section, file);
super(key, section);
mValue = value;
}

Expand Down
@@ -1,21 +1,21 @@
package org.dolphinemu.dolphinemu.model.settings;
package org.dolphinemu.dolphinemu.features.settings.model;

import org.dolphinemu.dolphinemu.ui.settings.MenuTag;
import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag;

public final class IntSetting extends Setting
{
private int mValue;
private MenuTag menuTag;

public IntSetting(String key, String section, int file, int value)
public IntSetting(String key, String section, int value)
{
super(key, section, file);
super(key, section);
mValue = value;
}

public IntSetting(String key, String section, int file, int value, MenuTag menuTag)
public IntSetting(String key, String section, int value, MenuTag menuTag)
{
super(key, section, file);
super(key, section);
mValue = value;
this.menuTag = menuTag;
}
Expand Down
@@ -1,4 +1,4 @@
package org.dolphinemu.dolphinemu.model.settings;
package org.dolphinemu.dolphinemu.features.settings.model;

/**
* Abstraction for a setting item as read from / written to Dolphin's configuration ini files.
Expand All @@ -10,20 +10,17 @@ public abstract class Setting
{
private String mKey;
private String mSection;
private int mFile;

/**
* Base constructor.
*
* @param key Everything to the left of the = in a line from the ini file.
* @param section The corresponding recent section header; e.g. [Core] or [Enhancements] without the brackets.
* @param file The ini file the Setting is stored in.
*/
public Setting(String key, String section, int file)
public Setting(String key, String section)
{
mKey = key;
mSection = section;
mFile = file;
}

/**
Expand All @@ -44,14 +41,6 @@ public String getSection()
return mSection;
}

/**
*
* @return The ini file the Setting is stored in.
*/
public int getFile()
{
return mFile;
}

/**
* @return A representation of this Setting's backing value converted to a String (e.g. for serialization).
Expand Down
@@ -1,4 +1,4 @@
package org.dolphinemu.dolphinemu.model.settings;
package org.dolphinemu.dolphinemu.features.settings.model;

import java.util.HashMap;

Expand Down Expand Up @@ -52,4 +52,12 @@ public HashMap<String, Setting> getSettings()
{
return mSettings;
}

public void mergeSection(SettingSection settingSection)
{
for (Setting setting : settingSection.mSettings.values())
{
putSetting(setting);
}
}
}
@@ -0,0 +1,179 @@
package org.dolphinemu.dolphinemu.features.settings.model;

import android.text.TextUtils;

import org.dolphinemu.dolphinemu.features.settings.ui.SettingsActivityView;
import org.dolphinemu.dolphinemu.features.settings.utils.SettingsFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Settings
{
public static final String SECTION_INI_CORE = "Core";
public static final String SECTION_INI_INTERFACE = "Interface";

public static final String SECTION_GFX_SETTINGS = "Settings";
public static final String SECTION_GFX_ENHANCEMENTS = "Enhancements";
public static final String SECTION_GFX_HACKS = "Hacks";

public static final String SECTION_STEREOSCOPY = "Stereoscopy";

public static final String SECTION_WIIMOTE = "Wiimote";

public static final String SECTION_BINDINGS = "Android";

private String gameId;

private static final Map<String, List<String>> configFileSectionsMap = new HashMap<>();

static
{
configFileSectionsMap.put(SettingsFile.FILE_NAME_DOLPHIN, Arrays.asList(SECTION_INI_CORE, SECTION_INI_INTERFACE, SECTION_BINDINGS));
configFileSectionsMap.put(SettingsFile.FILE_NAME_GFX, Arrays.asList(SECTION_GFX_SETTINGS, SECTION_GFX_ENHANCEMENTS, SECTION_GFX_HACKS, SECTION_STEREOSCOPY));
configFileSectionsMap.put(SettingsFile.FILE_NAME_WIIMOTE, Arrays.asList(SECTION_WIIMOTE + 1, SECTION_WIIMOTE + 2, SECTION_WIIMOTE + 3, SECTION_WIIMOTE + 4));
}

/**
* A HashMap<String, SettingSection> that constructs a new SettingSection instead of returning null
* when getting a key not already in the map
*/
public static final class SettingsSectionMap extends HashMap<String, SettingSection>
{
@Override
public SettingSection get(Object key)
{
if (!(key instanceof String))
{
return null;
}

String stringKey = (String) key;

if (!super.containsKey(stringKey))
{
SettingSection section = new SettingSection(stringKey);
super.put(stringKey, section);
return section;
}
return super.get(key);
}
}

private HashMap<String, SettingSection> sections = new Settings.SettingsSectionMap();

public SettingSection getSection(String sectionName)
{
return sections.get(sectionName);
}

public boolean isEmpty()
{
return sections.isEmpty();
}

public HashMap<String, SettingSection> getSections()
{
return sections;
}

public void loadSettings(SettingsActivityView view)
{
sections = new Settings.SettingsSectionMap();

HashSet<String> filesToExclude = new HashSet<>();
if (!TextUtils.isEmpty(gameId))
{
// for per-game settings, don't load the WiiMoteNew.ini settings
filesToExclude.add(SettingsFile.FILE_NAME_WIIMOTE);
}

loadDolphinSettings(view, filesToExclude);

if (!TextUtils.isEmpty(gameId))
{
loadGenericGameSettings(gameId, view);
loadCustomGameSettings(gameId, view);
}
}

private void loadDolphinSettings(SettingsActivityView view, HashSet<String> filesToExclude)
{
for (Map.Entry<String, List<String>> entry : configFileSectionsMap.entrySet())
{
String fileName = entry.getKey();
if(filesToExclude == null || !filesToExclude.contains(fileName))
{
sections.putAll(SettingsFile.readFile(fileName, view));
}
}
}

private void loadGenericGameSettings(String gameId, SettingsActivityView view)
{
// generic game settings
mergeSections(SettingsFile.readGenericGameSettings(gameId, view));
mergeSections(SettingsFile.readGenericGameSettingsForAllRegions(gameId, view));
}

private void loadCustomGameSettings(String gameId, SettingsActivityView view)
{
// custom game settings
mergeSections(SettingsFile.readCustomGameSettings(gameId, view));
}

private void mergeSections(HashMap<String, SettingSection> updatedSections)
{
for (Map.Entry<String, SettingSection> entry : updatedSections.entrySet())
{
if (sections.containsKey(entry.getKey()))
{
SettingSection originalSection = sections.get(entry.getKey());
SettingSection updatedSection = entry.getValue();
originalSection.mergeSection(updatedSection);
}
else
{
sections.put(entry.getKey(), entry.getValue());
}
}
}

public void loadSettings(String gameId, SettingsActivityView view)
{
this.gameId = gameId;
loadSettings(view);
}

public void saveSettings(SettingsActivityView view)
{
if (TextUtils.isEmpty(gameId))
{
view.showToastMessage("Saved settings to INI files");

for (Map.Entry<String, List<String>> entry : configFileSectionsMap.entrySet())
{
String fileName = entry.getKey();
List<String> sectionNames = entry.getValue();
TreeMap<String, SettingSection> iniSections = new TreeMap<>();
for (String section : sectionNames)
{
iniSections.put(section, sections.get(section));
}

SettingsFile.saveFile(fileName, iniSections, view);
}
}
else
{
// custom game settings
view.showToastMessage("Saved settings for " + gameId);
SettingsFile.saveCustomGameSettings(gameId, sections);
}

}
}
@@ -1,12 +1,12 @@
package org.dolphinemu.dolphinemu.model.settings;
package org.dolphinemu.dolphinemu.features.settings.model;

public final class StringSetting extends Setting
{
private String mValue;

public StringSetting(String key, String section, int file, String value)
public StringSetting(String key, String section, String value)
{
super(key, section, file);
super(key, section);
mValue = value;
}

Expand Down
@@ -1,15 +1,15 @@
package org.dolphinemu.dolphinemu.model.settings.view;
package org.dolphinemu.dolphinemu.features.settings.model.view;

import org.dolphinemu.dolphinemu.model.settings.BooleanSetting;
import org.dolphinemu.dolphinemu.model.settings.Setting;
import org.dolphinemu.dolphinemu.features.settings.model.BooleanSetting;
import org.dolphinemu.dolphinemu.features.settings.model.Setting;

public final class CheckBoxSetting extends SettingsItem
{
private boolean mDefaultValue;

public CheckBoxSetting(String key, String section, int file, int titleId, int descriptionId, boolean defaultValue, Setting setting)
public CheckBoxSetting(String key, String section, int titleId, int descriptionId, boolean defaultValue, Setting setting)
{
super(key, section, file, setting, titleId, descriptionId);
super(key, section, setting, titleId, descriptionId);
mDefaultValue = defaultValue;
}

Expand All @@ -35,7 +35,7 @@ public BooleanSetting setChecked(boolean checked)
{
if (getSetting() == null)
{
BooleanSetting setting = new BooleanSetting(getKey(), getSection(), getFile(), checked);
BooleanSetting setting = new BooleanSetting(getKey(), getSection(), checked);
setSetting(setting);
return setting;
}
Expand Down

0 comments on commit bfbf9ed

Please sign in to comment.