1
0
Fork 1
forked from suyu/suyu

android: Convert Settings to Kotlin

This commit is contained in:
Charles Lombardo 2023-03-06 15:54:24 -05:00 committed by bunnei
parent 753a0c6b5d
commit fa38c7be4f
2 changed files with 145 additions and 127 deletions

View file

@ -1,127 +0,0 @@
package org.yuzu.yuzu_emu.features.settings.model;
import android.text.TextUtils;
import org.yuzu.yuzu_emu.YuzuApplication;
import org.yuzu.yuzu_emu.R;
import org.yuzu.yuzu_emu.features.settings.ui.SettingsActivityView;
import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Settings {
public static final String SECTION_GENERAL = "General";
public static final String SECTION_SYSTEM = "System";
public static final String SECTION_RENDERER = "Renderer";
public static final String SECTION_AUDIO = "Audio";
public static final String SECTION_CPU = "Cpu";
private String gameId;
private static final Map<String, List<String>> configFileSectionsMap = new HashMap<>();
static {
configFileSectionsMap.put(SettingsFile.FILE_NAME_CONFIG, Arrays.asList(SECTION_GENERAL, SECTION_SYSTEM, SECTION_RENDERER, SECTION_AUDIO, SECTION_CPU));
}
/**
* 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();
loadCitraSettings(view);
if (!TextUtils.isEmpty(gameId)) {
loadCustomGameSettings(gameId, view);
}
}
private void loadCitraSettings(SettingsActivityView view) {
for (Map.Entry<String, List<String>> entry : configFileSectionsMap.entrySet()) {
String fileName = entry.getKey();
sections.putAll(SettingsFile.readFile(fileName, 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(YuzuApplication.getAppContext().getString(R.string.ini_saved), false);
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(YuzuApplication.getAppContext().getString(R.string.gameid_saved, gameId), false);
SettingsFile.saveCustomGameSettings(gameId, sections);
}
}
}

View file

@ -0,0 +1,145 @@
package org.yuzu.yuzu_emu.features.settings.model
import android.text.TextUtils
import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.YuzuApplication
import org.yuzu.yuzu_emu.features.settings.ui.SettingsActivityView
import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile
import java.util.*
class Settings {
private var gameId: String? = null
/**
* A HashMap<String></String>, SettingSection> that constructs a new SettingSection instead of returning null
* when getting a key not already in the map
*/
class SettingsSectionMap : HashMap<String, SettingSection?>() {
override operator fun get(key: String): SettingSection? {
if (!super.containsKey(key)) {
val section = SettingSection(key)
super.put(key, section)
return section
}
return super.get(key)
}
}
var sections: HashMap<String, SettingSection?> = SettingsSectionMap()
fun getSection(sectionName: String): SettingSection? {
return sections[sectionName]
}
val isEmpty: Boolean
get() = sections.isEmpty()
fun loadSettings(view: SettingsActivityView) {
sections = SettingsSectionMap()
loadYuzuSettings(view)
if (!TextUtils.isEmpty(gameId)) {
loadCustomGameSettings(gameId!!, view)
}
}
private fun loadYuzuSettings(view: SettingsActivityView) {
for ((fileName) in configFileSectionsMap) {
sections.putAll(SettingsFile.readFile(fileName, view))
}
}
private fun loadCustomGameSettings(gameId: String, view: SettingsActivityView) {
// Custom game settings
mergeSections(SettingsFile.readCustomGameSettings(gameId, view))
}
private fun mergeSections(updatedSections: HashMap<String, SettingSection?>) {
for ((key, updatedSection) in updatedSections) {
if (sections.containsKey(key)) {
val originalSection = sections[key]
originalSection!!.mergeSection(updatedSection!!)
} else {
sections[key] = updatedSection
}
}
}
fun loadSettings(gameId: String, view: SettingsActivityView) {
this.gameId = gameId
loadSettings(view)
}
fun saveSettings(view: SettingsActivityView) {
if (TextUtils.isEmpty(gameId)) {
view.showToastMessage(
YuzuApplication.appContext.getString(R.string.ini_saved),
false
)
for ((fileName, sectionNames) in configFileSectionsMap) {
val iniSections = TreeMap<String, SettingSection>()
for (section in sectionNames) {
iniSections[section] = sections[section]!!
}
SettingsFile.saveFile(fileName, iniSections, view)
}
} else {
// Custom game settings
view.showToastMessage(
YuzuApplication.appContext.getString(R.string.gameid_saved, gameId),
false
)
SettingsFile.saveCustomGameSettings(gameId, sections)
}
}
companion object {
const val SECTION_GENERAL = "General"
const val SECTION_SYSTEM = "System"
const val SECTION_RENDERER = "Renderer"
const val SECTION_AUDIO = "Audio"
const val SECTION_CPU = "Cpu"
const val PREF_OVERLAY_INIT = "OverlayInit"
const val PREF_CONTROL_SCALE = "controlScale"
const val PREF_TOUCH_ENABLED = "isTouchEnabled"
const val PREF_BUTTON_TOGGLE_0 = "buttonToggleO"
const val PREF_BUTTON_TOGGLE_1 = "buttonToggle1"
const val PREF_BUTTON_TOGGLE_2 = "buttonToggle2"
const val PREF_BUTTON_TOGGLE_3 = "buttonToggle3"
const val PREF_BUTTON_TOGGLE_4 = "buttonToggle4"
const val PREF_BUTTON_TOGGLE_5 = "buttonToggle5"
const val PREF_BUTTON_TOGGLE_6 = "buttonToggle6"
const val PREF_BUTTON_TOGGLE_7 = "buttonToggle7"
const val PREF_BUTTON_TOGGLE_8 = "buttonToggle8"
const val PREF_BUTTON_TOGGLE_9 = "buttonToggle9"
const val PREF_BUTTON_TOGGLE_10 = "buttonToggle10"
const val PREF_BUTTON_TOGGLE_11 = "buttonToggle11"
const val PREF_BUTTON_TOGGLE_12 = "buttonToggle12"
const val PREF_BUTTON_TOGGLE_13 = "buttonToggle13"
const val PREF_BUTTON_TOGGLE_14 = "buttonToggle14"
const val PREF_MENU_SETTINGS_JOYSTICK_REL_CENTER = "EmulationMenuSettings_JoystickRelCenter"
const val PREF_MENU_SETTINGS_DPAD_SLIDE = "EmulationMenuSettings_DpadSlideEnable"
const val PREF_MENU_SETTINGS_LANDSCAPE = "EmulationMenuSettings_LandscapeScreenLayout"
const val PREF_MENU_SETTINGS_SHOW_FPS = "EmulationMenuSettings_ShowFps"
const val PREF_MENU_SETTINGS_SHOW_OVERLAY = "EmulationMenuSettings_ShowOverlay"
const val PREF_FIRST_APP_LAUNCH = "FirstApplicationLaunch"
private val configFileSectionsMap: MutableMap<String, List<String>> = HashMap()
init {
configFileSectionsMap[SettingsFile.FILE_NAME_CONFIG] =
listOf(
SECTION_GENERAL,
SECTION_SYSTEM,
SECTION_RENDERER,
SECTION_AUDIO,
SECTION_CPU
)
}
}
}