Settings refactor

Remove the big saveSetup function and set individual settings immediately after being changed in the UI. Group them all under Netplay.Settings
This commit is contained in:
Tom Pratt 2026-04-19 20:07:37 +02:00
parent a87d315283
commit fd21ca13ff
4 changed files with 89 additions and 175 deletions

View File

@ -202,99 +202,46 @@ object Netplay {
}
// Settings
object Settings {
@JvmStatic
external fun getNickname(): String
@JvmStatic
external fun getNickname(): String
@JvmStatic
external fun setNickname(nickname: String)
fun getConnectionType(): ConnectionType = ConnectionType.all
.find { it.configValue == getTraversalChoice() } ?: throw IllegalStateException()
fun getConnectionType(): ConnectionType = ConnectionType.all
.find { it.configValue == getTraversalChoice() } ?: throw IllegalStateException()
@JvmStatic
external fun getTraversalChoice(): String
@JvmStatic
external fun getTraversalChoice(): String
@JvmStatic
external fun getAddress(): String
@JvmStatic
external fun setTraversalChoice(traversalChoice: String)
@JvmStatic
external fun getHostCode(): String
@JvmStatic
external fun getAddress(): String
@JvmStatic
external fun getConnectPort(): Int
@JvmStatic
external fun setAddress(address: String)
@JvmStatic
external fun getHostPort(): Int
@JvmStatic
external fun getHostCode(): String
@JvmStatic
external fun getUseUpnp(): Boolean
@JvmStatic
external fun setHostCode(hostCode: String)
@JvmStatic
external fun getEnableChunkedUploadLimit(): Boolean
@JvmStatic
external fun getConnectPort(): Int
@JvmStatic
external fun getChunkedUploadLimit(): Int
@JvmStatic
external fun setConnectPort(port: Int)
@JvmStatic
external fun getUseIndex(): Boolean
@JvmStatic
external fun getClientBufferSize(): Int
@JvmStatic
external fun getIndexRegion(): String
@JvmStatic
external fun getIndexName(): String
@JvmStatic
external fun getIndexPassword(): String
@JvmStatic
external fun getClientBufferSize(): Int
@JvmStatic
external fun setClientBufferSize(buffer: Int)
suspend fun saveSetup(
nickname: String,
connectionType: ConnectionType,
address: String,
hostCode: String,
connectPort: Int,
) = withContext(Dispatchers.IO) {
SaveSetup(
nickname = nickname,
traversalChoice = connectionType.configValue,
address = address,
hostCode = hostCode,
connectPort = connectPort,
hostPort = 2626,
useUpnp = false,
useListenPort = false,
listenPort = 2626,
enableChunkedUploadLimit = false,
chunkedUploadLimit = 3000,
useIndex = false,
indexRegion = "",
indexName = "",
indexPassword = "",
)
@JvmStatic
external fun setClientBufferSize(buffer: Int)
}
@JvmStatic
external fun SaveSetup(
nickname: String,
traversalChoice: String,
address: String,
hostCode: String,
connectPort: Int,
hostPort: Int,
useUpnp: Boolean,
useListenPort: Boolean,
listenPort: Int,
enableChunkedUploadLimit: Boolean,
chunkedUploadLimit: Int,
useIndex: Boolean,
indexRegion: String,
indexName: String,
indexPassword: String,
)
}
private fun Channel<String>.flush() {

View File

@ -19,19 +19,19 @@ class NetplaySetupViewModel : ViewModel() {
private val _connectionRole = MutableStateFlow<ConnectionRole>(ConnectionRole.Connect)
val connectionRole = _connectionRole.asStateFlow()
private val _nickname = MutableStateFlow(Netplay.getNickname())
private val _nickname = MutableStateFlow(Netplay.Settings.getNickname())
val nickname = _nickname.asStateFlow()
private val _connectionType = MutableStateFlow(Netplay.getConnectionType())
private val _connectionType = MutableStateFlow(Netplay.Settings.getConnectionType())
val connectionType = _connectionType.asStateFlow()
private val _ipAddress = MutableStateFlow(Netplay.getAddress())
private val _ipAddress = MutableStateFlow(Netplay.Settings.getAddress())
val ipAddress = _ipAddress.asStateFlow()
private val _hostCode = MutableStateFlow(Netplay.getHostCode())
private val _hostCode = MutableStateFlow(Netplay.Settings.getHostCode())
val hostCode = _hostCode.asStateFlow()
private val _connectPort = MutableStateFlow(Netplay.getConnectPort().toString())
private val _connectPort = MutableStateFlow(Netplay.Settings.getConnectPort().toString())
val connectPort = _connectPort.asStateFlow()
private val _showNetplayScreen = Channel<Unit>(CONFLATED)
@ -52,25 +52,30 @@ class NetplaySetupViewModel : ViewModel() {
fun setNickname(nickname: String) {
_nickname.value = nickname
Netplay.Settings.setNickname(nickname)
}
fun setConnectionType(connectionType: ConnectionType) {
_connectionType.value = connectionType
Netplay.Settings.setTraversalChoice(connectionType.configValue)
}
fun setIpAddress(ipAddress: String) {
if (ipAddress.all { it.isDigit() || it == '.' }) {
_ipAddress.value = ipAddress
Netplay.Settings.setAddress(ipAddress)
}
}
fun setHostCode(hostCode: String) {
_hostCode.value = hostCode
Netplay.Settings.setHostCode(hostCode)
}
fun setConnectPort(port: String) {
if (port.all { it.isDigit() }) {
_connectPort.value = port
port.toIntOrNull()?.let { Netplay.Settings.setConnectPort(it) }
}
}
@ -80,14 +85,6 @@ class NetplaySetupViewModel : ViewModel() {
viewModelScope.launch {
GameFileCacheManager.isLoading().asFlow().first { it == false }
Netplay.saveSetup(
nickname = nickname.value,
connectionType = connectionType.value,
address = ipAddress.value,
hostCode = hostCode.value,
connectPort = connectPort.value.toInt(),
)
if (Netplay.join()) {
_showNetplayScreen.trySend(Unit)
}

View File

@ -37,7 +37,7 @@ class NetplayViewModel : ViewModel() {
val hostInputAuthority = Netplay.hostInputAuthorityEnabled
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), false)
private val _maxBuffer = MutableStateFlow(Netplay.getClientBufferSize())
private val _maxBuffer = MutableStateFlow(Netplay.Settings.getClientBufferSize())
val maxBuffer = _maxBuffer.asStateFlow()
init {
@ -57,7 +57,7 @@ class NetplayViewModel : ViewModel() {
fun setMaxBuffer(buffer: Int) {
_maxBuffer.value = buffer
Netplay.setClientBufferSize(buffer)
Netplay.Settings.setClientBufferSize(buffer)
Netplay.adjustPadBufferSize(buffer)
}

View File

@ -26,123 +26,93 @@ static NetPlay::NetPlayClient* GetPointer(JNIEnv* env)
extern "C" {
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getNickname(JNIEnv* env, jclass)
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_getNickname(JNIEnv* env,
jclass)
{
return ToJString(env, Config::Get(Config::NETPLAY_NICKNAME));
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_setNickname(JNIEnv* env,
jclass,
jstring jnickname)
{
Config::SetBase(Config::NETPLAY_NICKNAME, GetJString(env, jnickname));
}
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getTraversalChoice(JNIEnv* env, jclass)
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_getTraversalChoice(
JNIEnv* env, jclass)
{
return ToJString(env, Config::Get(Config::NETPLAY_TRAVERSAL_CHOICE));
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_setTraversalChoice(
JNIEnv* env, jclass, jstring jtraversalChoice)
{
Config::SetBase(Config::NETPLAY_TRAVERSAL_CHOICE, GetJString(env, jtraversalChoice));
}
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getAddress(JNIEnv* env, jclass)
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_getAddress(JNIEnv* env,
jclass)
{
return ToJString(env, Config::Get(Config::NETPLAY_ADDRESS));
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_setAddress(JNIEnv* env,
jclass,
jstring jaddress)
{
Config::SetBase(Config::NETPLAY_ADDRESS, GetJString(env, jaddress));
}
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getHostCode(JNIEnv* env, jclass)
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_getHostCode(JNIEnv* env,
jclass)
{
return ToJString(env, Config::Get(Config::NETPLAY_HOST_CODE));
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_setHostCode(JNIEnv* env,
jclass,
jstring jhostCode)
{
Config::SetBase(Config::NETPLAY_HOST_CODE, GetJString(env, jhostCode));
}
JNIEXPORT jint JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getConnectPort(JNIEnv*, jclass)
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_getConnectPort(JNIEnv*,
jclass)
{
return static_cast<jint>(Config::Get(Config::NETPLAY_CONNECT_PORT));
}
JNIEXPORT jint JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getHostPort(JNIEnv*, jclass)
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_setConnectPort(JNIEnv*,
jclass,
jint port)
{
return static_cast<jint>(Config::Get(Config::NETPLAY_HOST_PORT));
}
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getUseUpnp(JNIEnv*, jclass)
{
return static_cast<jboolean>(Config::Get(Config::NETPLAY_USE_UPNP));
}
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getEnableChunkedUploadLimit(JNIEnv*, jclass)
{
return static_cast<jboolean>(Config::Get(Config::NETPLAY_ENABLE_CHUNKED_UPLOAD_LIMIT));
Config::SetBase(Config::NETPLAY_CONNECT_PORT, static_cast<u16>(port));
}
JNIEXPORT jint JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getChunkedUploadLimit(JNIEnv*, jclass)
{
return static_cast<jint>(Config::Get(Config::NETPLAY_CHUNKED_UPLOAD_LIMIT));
}
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getUseIndex(JNIEnv*, jclass)
{
return static_cast<jboolean>(Config::Get(Config::NETPLAY_USE_INDEX));
}
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getIndexRegion(JNIEnv* env, jclass)
{
return ToJString(env, Config::Get(Config::NETPLAY_INDEX_REGION));
}
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getIndexName(JNIEnv* env, jclass)
{
return ToJString(env, Config::Get(Config::NETPLAY_INDEX_NAME));
}
JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getIndexPassword(JNIEnv* env, jclass)
{
return ToJString(env, Config::Get(Config::NETPLAY_INDEX_PASSWORD));
}
JNIEXPORT jint JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_getClientBufferSize(JNIEnv*, jclass)
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_getClientBufferSize(JNIEnv*,
jclass)
{
return static_cast<jint>(Config::Get(Config::NETPLAY_CLIENT_BUFFER_SIZE));
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_setClientBufferSize(JNIEnv*, jclass,
jint buffer)
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_00024Settings_setClientBufferSize(
JNIEnv*, jclass, jint buffer)
{
Config::SetBase(Config::NETPLAY_CLIENT_BUFFER_SIZE, static_cast<u32>(buffer));
}
JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_SaveSetup(
JNIEnv* env, jclass, jstring jnickname, jstring traversalChoice, jstring jaddress,
jstring jhostCode, jint connectPort, jint hostPort, jboolean useUpnp, jboolean useListenPort,
jint listenPort, jboolean enableChunkedUploadLimit, jint chunkedUploadLimit, jboolean useIndex,
jstring jindexRegion, jstring jindexName, jstring jindexPassword)
{
Config::ConfigChangeCallbackGuard config_guard;
Config::SetBaseOrCurrent(Config::NETPLAY_NICKNAME, GetJString(env, jnickname));
Config::SetBaseOrCurrent(Config::NETPLAY_TRAVERSAL_CHOICE, GetJString(env, traversalChoice));
Config::SetBaseOrCurrent(Config::NETPLAY_ADDRESS, GetJString(env, jaddress));
Config::SetBaseOrCurrent(Config::NETPLAY_HOST_CODE, GetJString(env, jhostCode));
Config::SetBaseOrCurrent(Config::NETPLAY_CONNECT_PORT, static_cast<u16>(connectPort));
Config::SetBaseOrCurrent(Config::NETPLAY_HOST_PORT, static_cast<u16>(hostPort));
Config::SetBaseOrCurrent(Config::NETPLAY_USE_UPNP, static_cast<bool>(useUpnp));
Config::SetBaseOrCurrent(Config::NETPLAY_ENABLE_CHUNKED_UPLOAD_LIMIT,
static_cast<bool>(enableChunkedUploadLimit));
Config::SetBaseOrCurrent(Config::NETPLAY_CHUNKED_UPLOAD_LIMIT,
static_cast<u32>(chunkedUploadLimit));
Config::SetBaseOrCurrent(Config::NETPLAY_USE_INDEX, static_cast<bool>(useIndex));
Config::SetBaseOrCurrent(Config::NETPLAY_INDEX_REGION, GetJString(env, jindexRegion));
Config::SetBaseOrCurrent(Config::NETPLAY_INDEX_NAME, GetJString(env, jindexName));
Config::SetBaseOrCurrent(Config::NETPLAY_INDEX_PASSWORD, GetJString(env, jindexPassword));
Config::SetBaseOrCurrent(Config::NETPLAY_LISTEN_PORT, static_cast<u16>(listenPort));
}
JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_netplay_Netplay_isClientConnected(JNIEnv* env, jclass)
{