Android: Hook up global settings to the new config system

This commit is contained in:
JosJuice 2020-07-22 15:28:26 +02:00
parent f011e859b4
commit a538301891
11 changed files with 373 additions and 21 deletions

View File

@ -0,0 +1,57 @@
package org.dolphinemu.dolphinemu.features.settings.model;
public class AdHocBooleanSetting extends AbstractLegacySetting implements AbstractBooleanSetting
{
private final boolean mDefaultValue;
public AdHocBooleanSetting(String file, String section, String key, boolean defaultValue)
{
super(file, section, key);
mDefaultValue = defaultValue;
if (!NativeConfig.isSettingSaveable(file, section, key))
{
throw new IllegalArgumentException("File/section/key is unknown or legacy");
}
}
@Override
public boolean delete(Settings settings)
{
if (!settings.isGameSpecific())
{
return NativeConfig.deleteKey(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey);
}
else
{
return settings.getSection(mFile, mSection).delete(mKey);
}
}
@Override
public boolean getBoolean(Settings settings)
{
if (!settings.isGameSpecific())
{
return NativeConfig.getBoolean(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey,
mDefaultValue);
}
else
{
return settings.getSection(mFile, mSection).getBoolean(mKey, mDefaultValue);
}
}
@Override
public void setBoolean(Settings settings, boolean newValue)
{
if (!settings.isGameSpecific())
{
NativeConfig.setBoolean(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey, newValue);
}
else
{
settings.getSection(mFile, mSection).setBoolean(mKey, newValue);
}
}
}

View File

@ -109,19 +109,41 @@ public enum BooleanSetting implements AbstractBooleanSetting
@Override
public boolean delete(Settings settings)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
return NativeConfig.deleteKey(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey);
}
else
{
return settings.getSection(mFile, mSection).delete(mKey);
}
}
@Override
public boolean getBoolean(Settings settings)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
return NativeConfig.getBoolean(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey,
mDefaultValue);
}
else
{
return settings.getSection(mFile, mSection).getBoolean(mKey, mDefaultValue);
}
}
@Override
public void setBoolean(Settings settings, boolean newValue)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
NativeConfig.setBoolean(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey, newValue);
}
else
{
settings.getSection(mFile, mSection).setBoolean(mKey, newValue);
}
}
}

View File

@ -22,19 +22,41 @@ public enum FloatSetting implements AbstractFloatSetting
@Override
public boolean delete(Settings settings)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
return NativeConfig.deleteKey(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey);
}
else
{
return settings.getSection(mFile, mSection).delete(mKey);
}
}
@Override
public float getFloat(Settings settings)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
return NativeConfig.getFloat(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey,
mDefaultValue);
}
else
{
return settings.getSection(mFile, mSection).getFloat(mKey, mDefaultValue);
}
}
@Override
public void setFloat(Settings settings, float newValue)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
NativeConfig.setFloat(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey, newValue);
}
else
{
settings.getSection(mFile, mSection).setFloat(mKey, newValue);
}
}
}

View File

@ -49,19 +49,41 @@ public enum IntSetting implements AbstractIntSetting
@Override
public boolean delete(Settings settings)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
return NativeConfig.deleteKey(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey);
}
else
{
return settings.getSection(mFile, mSection).delete(mKey);
}
}
@Override
public int getInt(Settings settings)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
return NativeConfig.getInt(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey,
mDefaultValue);
}
else
{
return settings.getSection(mFile, mSection).getInt(mKey, mDefaultValue);
}
}
@Override
public void setInt(Settings settings, int newValue)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
NativeConfig.setInt(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey, newValue);
}
else
{
settings.getSection(mFile, mSection).setInt(mKey, newValue);
}
}
}

View File

@ -0,0 +1,36 @@
package org.dolphinemu.dolphinemu.features.settings.model;
public class NativeConfig
{
public static final int LAYER_BASE_OR_CURRENT = 0;
public static final int LAYER_LOCAL_GAME = 1;
public static native boolean isSettingSaveable(String file, String section, String key);
public static native void save(int layer);
public static native boolean deleteKey(int layer, String file, String section, String key);
public static native String getString(int layer, String file, String section, String key,
String defaultValue);
public static native boolean getBoolean(int layer, String file, String section, String key,
boolean defaultValue);
public static native int getInt(int layer, String file, String section, String key,
int defaultValue);
public static native float getFloat(int layer, String file, String section, String key,
float defaultValue);
public static native void setString(int layer, String file, String section, String key,
String value);
public static native void setBoolean(int layer, String file, String section, String key,
boolean value);
public static native void setInt(int layer, String file, String section, String key, int value);
public static native void setFloat(int layer, String file, String section, String key,
float value);
}

View File

@ -59,7 +59,7 @@ public class Settings
private IniFile getGameSpecificFile()
{
if (TextUtils.isEmpty(gameId) || mIniFiles.size() != 1)
if (!isGameSpecific() || mIniFiles.size() != 1)
throw new IllegalStateException();
return mIniFiles.get(GAME_SETTINGS_PLACEHOLDER_FILE_NAME);
@ -67,7 +67,7 @@ public class Settings
public IniFile.Section getSection(String fileName, String sectionName)
{
if (TextUtils.isEmpty(gameId))
if (!isGameSpecific())
{
return mIniFiles.get(fileName).getOrCreateSection(sectionName);
}
@ -78,6 +78,11 @@ public class Settings
}
}
public boolean isGameSpecific()
{
return !TextUtils.isEmpty(gameId);
}
public boolean isEmpty()
{
return mIniFiles.isEmpty();
@ -87,7 +92,7 @@ public class Settings
{
mIniFiles = new HashMap<>();
if (TextUtils.isEmpty(gameId))
if (!isGameSpecific())
{
loadDolphinSettings(view);
}
@ -129,7 +134,7 @@ public class Settings
public void saveSettings(SettingsActivityView view, Context context)
{
if (TextUtils.isEmpty(gameId))
if (!isGameSpecific())
{
if (context != null)
Toast.makeText(context, "Saved settings to INI files", Toast.LENGTH_SHORT).show();
@ -139,6 +144,8 @@ public class Settings
SettingsFile.saveFile(entry.getKey(), entry.getValue(), view);
}
NativeConfig.save(NativeConfig.LAYER_BASE_OR_CURRENT);
// Notify the native code of the changes
NativeLibrary.ReloadConfig();
NativeLibrary.ReloadWiimoteConfig();
@ -190,7 +197,7 @@ public class Settings
// possible to know which lines were added intentionally by the user and which lines were added
// unintentionally, which is why we have to delete the whole file in order to fix everything.
if (TextUtils.isEmpty(gameId))
if (!isGameSpecific())
return false;
return getSection(Settings.FILE_DOLPHIN, SECTION_INI_INTERFACE).exists("ThemeName");

View File

@ -35,19 +35,41 @@ public enum StringSetting implements AbstractStringSetting
@Override
public boolean delete(Settings settings)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
return NativeConfig.deleteKey(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey);
}
else
{
return settings.getSection(mFile, mSection).delete(mKey);
}
}
@Override
public String getString(Settings settings)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
return NativeConfig.getString(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey,
mDefaultValue);
}
else
{
return settings.getSection(mFile, mSection).getString(mKey, mDefaultValue);
}
}
@Override
public void setString(Settings settings, String newValue)
{
if (NativeConfig.isSettingSaveable(mFile, mSection, mKey) && !settings.isGameSpecific())
{
NativeConfig.setString(NativeConfig.LAYER_BASE_OR_CURRENT, mFile, mSection, mKey, newValue);
}
else
{
settings.getSection(mFile, mSection).setString(mKey, newValue);
}
}
}

View File

@ -1,6 +1,6 @@
package org.dolphinemu.dolphinemu.features.settings.model.view;
import org.dolphinemu.dolphinemu.features.settings.model.LegacyBooleanSetting;
import org.dolphinemu.dolphinemu.features.settings.model.AdHocBooleanSetting;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
public class LogCheckBoxSetting extends CheckBoxSetting
@ -9,7 +9,7 @@ public class LogCheckBoxSetting extends CheckBoxSetting
public LogCheckBoxSetting(String key, int titleId, int descriptionId)
{
super(new LegacyBooleanSetting(Settings.FILE_LOGGER, Settings.SECTION_LOGGER_LOGS, key, false),
super(new AdHocBooleanSetting(Settings.FILE_LOGGER, Settings.SECTION_LOGGER_LOGS, key, false),
titleId, descriptionId);
mKey = key;
}

View File

@ -13,7 +13,7 @@ import androidx.recyclerview.widget.RecyclerView;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.dialogs.MotionAlertDialog;
import org.dolphinemu.dolphinemu.features.settings.model.LegacyBooleanSetting;
import org.dolphinemu.dolphinemu.features.settings.model.AdHocBooleanSetting;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
import org.dolphinemu.dolphinemu.features.settings.model.StringSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.CheckBoxSetting;
@ -335,7 +335,7 @@ public final class SettingsAdapter extends RecyclerView.Adapter<SettingViewHolde
for (Map.Entry<String, String> entry : SettingsFragmentPresenter.LOG_TYPE_NAMES.entrySet())
{
new LegacyBooleanSetting(Settings.FILE_LOGGER, Settings.SECTION_LOGGER_LOGS, entry.getKey(),
new AdHocBooleanSetting(Settings.FILE_LOGGER, Settings.SECTION_LOGGER_LOGS, entry.getKey(),
false).setBoolean(settings, value);
}

View File

@ -5,6 +5,7 @@ add_library(main SHARED
GameList/GameFileCache.cpp
IniFile.cpp
MainAndroid.cpp
NativeConfig.cpp
)
target_link_libraries(main

View File

@ -0,0 +1,163 @@
// Copyright 2020 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <memory>
#include <string>
#include <jni.h>
#include "Common/Assert.h"
#include "Common/Config/Config.h"
#include "Core/ConfigLoaders/IsSettingSaveable.h"
#include "jni/AndroidCommon/AndroidCommon.h"
constexpr jint LAYER_BASE_OR_CURRENT = 0;
constexpr jint LAYER_LOCAL_GAME = 1;
static Config::Location GetLocation(JNIEnv* env, jstring file, jstring section, jstring key)
{
const std::string decoded_file = GetJString(env, file);
Config::System system;
if (decoded_file == "Dolphin")
{
system = Config::System::Main;
}
else if (decoded_file == "GFX")
{
system = Config::System::GFX;
}
else if (decoded_file == "Logger")
{
system = Config::System::Logger;
}
else
{
ASSERT(false);
return {};
}
return Config::Location{system, GetJString(env, section), GetJString(env, key)};
}
static std::shared_ptr<Config::Layer> GetLayer(jint layer, const Config::Location& location)
{
switch (layer)
{
case LAYER_BASE_OR_CURRENT:
if (GetActiveLayerForConfig(location) == Config::LayerType::Base)
return Config::GetLayer(Config::LayerType::Base);
else
return Config::GetLayer(Config::LayerType::CurrentRun);
case LAYER_LOCAL_GAME:
return Config::GetLayer(Config::LayerType::LocalGame);
default:
ASSERT(false);
return nullptr;
}
}
template <typename T>
static T Get(jint layer, const Config::Location& location, T default_value)
{
return GetLayer(layer, location)->Get<T>(location).value_or(default_value);
}
template <typename T>
static void Set(jint layer, const Config::Location& location, T value)
{
GetLayer(layer, location)->Set(location, value);
Config::InvokeConfigChangedCallbacks();
}
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_isSettingSaveable(
JNIEnv* env, jclass obj, jstring file, jstring section, jstring key)
{
const Config::Location location = GetLocation(env, file, section, key);
return static_cast<jboolean>(ConfigLoaders::IsSettingSaveable(location));
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_save(
JNIEnv* env, jclass obj, jint layer)
{
return GetLayer(layer, {})->Save();
}
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_deleteKey(
JNIEnv* env, jclass obj, jint layer, jstring file, jstring section, jstring key)
{
const Config::Location location = GetLocation(env, file, section, key);
return static_cast<jboolean>(GetLayer(layer, location)->DeleteKey(location));
}
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_getString(
JNIEnv* env, jclass obj, jint layer, jstring file, jstring section, jstring key,
jstring default_value)
{
const Config::Location location = GetLocation(env, file, section, key);
return ToJString(env, Get(layer, location, GetJString(env, default_value)));
}
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_getBoolean(
JNIEnv* env, jclass obj, jint layer, jstring file, jstring section, jstring key,
jboolean default_value)
{
const Config::Location location = GetLocation(env, file, section, key);
return static_cast<jboolean>(Get(layer, location, static_cast<bool>(default_value)));
}
JNIEXPORT jint JNICALL Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_getInt(
JNIEnv* env, jclass obj, jint layer, jstring file, jstring section, jstring key,
jint default_value)
{
return Get(layer, GetLocation(env, file, section, key), default_value);
}
JNIEXPORT jfloat JNICALL
Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_getFloat(
JNIEnv* env, jclass obj, jint layer, jstring file, jstring section, jstring key,
jfloat default_value)
{
return Get(layer, GetLocation(env, file, section, key), default_value);
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_setString(
JNIEnv* env, jclass obj, jint layer, jstring file, jstring section, jstring key, jstring value)
{
return Set(layer, GetLocation(env, file, section, key), GetJString(env, value));
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_setBoolean(
JNIEnv* env, jclass obj, jint layer, jstring file, jstring section, jstring key, jboolean value)
{
return Set(layer, GetLocation(env, file, section, key), static_cast<bool>(value));
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_setInt(
JNIEnv* env, jclass obj, jint layer, jstring file, jstring section, jstring key, jint value)
{
return Set(layer, GetLocation(env, file, section, key), value);
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_features_settings_model_NativeConfig_setFloat(
JNIEnv* env, jclass obj, jint layer, jstring file, jstring section, jstring key, jfloat value)
{
return Set(layer, GetLocation(env, file, section, key), value);
}
#ifdef __cplusplus
}
#endif