Skip to content

Commit

Permalink
Migrate to optconfig instead of VV config system
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Jul 30, 2024
1 parent de31a10 commit ed68b7d
Show file tree
Hide file tree
Showing 9 changed files with 451 additions and 319 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ dependencies {
exclude group: "io.netty"
}
include "gs.mclo:api:3.0.1"
include "net.lenni0451:optconfig:1.0.0-SNAPSHOT"

includeJ8(compileOnly("xyz.wagyourtail.jvmdowngrader:jvmdowngrader:0.9.1"))
includeJ8 "xyz.wagyourtail.jvmdowngrader:jvmdowngrader-java-api:0.9.1:downgraded-8"
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/net/raphimc/viaproxy/ViaProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import net.lenni0451.classtransform.utils.tree.IClassProvider;
import net.lenni0451.lambdaevents.LambdaManager;
import net.lenni0451.lambdaevents.generator.LambdaMetaFactoryGenerator;
import net.lenni0451.optconfig.ConfigLoader;
import net.lenni0451.optconfig.provider.ConfigProvider;
import net.lenni0451.reflect.Agents;
import net.lenni0451.reflect.ClassLoaders;
import net.lenni0451.reflect.JavaBypass;
Expand Down Expand Up @@ -229,8 +231,13 @@ public static void injectedMain(final String injectionMethod, final String[] arg
progressConsumer.accept("Loading Saves");
SAVE_MANAGER = new SaveManager();
progressConsumer.accept("Loading Config");
CONFIG = new ViaProxyConfig(viaProxyConfigFile);
CONFIG.reload();
final ConfigLoader<ViaProxyConfig> configLoader = new ConfigLoader<>(ViaProxyConfig.class);
configLoader.getConfigOptions().setResetInvalidOptions(true).setRewriteConfig(true).setCommentSpacing(1);
try {
CONFIG = configLoader.load(ConfigProvider.file(viaProxyConfigFile)).getConfigInstance();
} catch (Throwable e) {
throw new RuntimeException("Failed to load config", e);
}

if (useUI) {
progressConsumer.accept("Loading GUI");
Expand All @@ -253,7 +260,11 @@ public static void injectedMain(final String injectionMethod, final String[] arg
if (useCLI) {
final String[] cliArgs = new String[args.length - 1];
System.arraycopy(args, 1, cliArgs, 0, cliArgs.length);
CONFIG.loadFromArguments(cliArgs);
try {
CONFIG.loadFromArguments(cliArgs);
} catch (Throwable e) {
throw new RuntimeException("Failed to load CLI arguments", e);
}
} else if (firstStart) {
Logger.LOGGER.info("This is the first start of ViaProxy. Please configure the settings in the " + viaProxyConfigFile.getName() + " file and restart ViaProxy.");
System.exit(0);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.util.config;

import net.lenni0451.optconfig.serializer.ConfigTypeSerializer;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.protocoltranslator.viaproxy.ViaProxyConfig;
import net.raphimc.viaproxy.saves.impl.accounts.Account;

import java.util.List;

public class AccountTypeSerializer extends ConfigTypeSerializer<ViaProxyConfig, Account> {

public AccountTypeSerializer(final ViaProxyConfig config) {
super(config);
}

@Override
public Account deserialize(final Class<Account> typeClass, final Object serializedObject) {
final List<Account> accounts = ViaProxy.getSaveManager().accountsSave.getAccounts();
final int accountIndex = (int) serializedObject;
if (this.config.getAuthMethod() == ViaProxyConfig.AuthMethod.ACCOUNT && accountIndex >= 0 && accountIndex < accounts.size()) {
return accounts.get(accountIndex);
} else {
return null;
}
}

@Override
public Object serialize(final Account object) {
if (object != null) {
return ViaProxy.getSaveManager().accountsSave.getAccounts().indexOf(object);
} else {
return 0;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.util.config;

import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import net.lenni0451.optconfig.serializer.ConfigTypeSerializer;
import net.raphimc.viaproxy.protocoltranslator.viaproxy.ViaProxyConfig;

public class ProtocolVersionTypeSerializer extends ConfigTypeSerializer<ViaProxyConfig, ProtocolVersion> {

public ProtocolVersionTypeSerializer(final ViaProxyConfig config) {
super(config);
}

@Override
public ProtocolVersion deserialize(final Class<ProtocolVersion> typeClass, final Object serializedObject) {
return ProtocolVersion.getClosest((String) serializedObject);
}

@Override
public Object serialize(final ProtocolVersion object) {
return object.getName();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.util.config;

import net.lenni0451.optconfig.serializer.ConfigTypeSerializer;
import net.raphimc.viaproxy.protocoltranslator.viaproxy.ViaProxyConfig;

import java.net.URI;
import java.net.URISyntaxException;

public class ProxyUriTypeSerializer extends ConfigTypeSerializer<ViaProxyConfig, URI> {

public ProxyUriTypeSerializer(final ViaProxyConfig config) {
super(config);
}

@Override
public URI deserialize(final Class<URI> typeClass, final Object serializedObject) {
final String proxyUrl = (String) serializedObject;
if (!proxyUrl.isBlank()) {
try {
return new URI(proxyUrl);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid proxy url: " + proxyUrl + ". Proxy url format: type://address:port or type://username:password@address:port");
}
}
return null;
}

@Override
public Object serialize(final URI object) {
if (object != null) {
return object.toString();
} else {
return "";
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.util.config;

import net.lenni0451.optconfig.serializer.ConfigTypeSerializer;
import net.raphimc.viaproxy.protocoltranslator.viaproxy.ViaProxyConfig;
import net.raphimc.viaproxy.util.AddressUtil;

import java.net.SocketAddress;

public class SocketAddressTypeSerializer extends ConfigTypeSerializer<ViaProxyConfig, SocketAddress> {

public SocketAddressTypeSerializer(final ViaProxyConfig config) {
super(config);
}

@Override
public SocketAddress deserialize(final Class<SocketAddress> typeClass, final Object serializedObject) {
return AddressUtil.parse((String) serializedObject, null);
}

@Override
public Object serialize(final SocketAddress object) {
return AddressUtil.toString(object);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.util.config;

import net.lenni0451.optconfig.serializer.ConfigTypeSerializer;
import net.raphimc.viaproxy.protocoltranslator.viaproxy.ViaProxyConfig;
import net.raphimc.viaproxy.util.AddressUtil;

import java.net.SocketAddress;

public class TargetAddressTypeSerializer extends ConfigTypeSerializer<ViaProxyConfig, SocketAddress> {

public TargetAddressTypeSerializer(final ViaProxyConfig config) {
super(config);
}

@Override
public SocketAddress deserialize(final Class<SocketAddress> typeClass, final Object serializedObject) {
return AddressUtil.parse((String) serializedObject, this.config.getTargetVersion());
}

@Override
public Object serialize(final SocketAddress object) {
return AddressUtil.toString(object);
}

}
90 changes: 0 additions & 90 deletions src/main/resources/assets/viaproxy/viaproxy.yml

This file was deleted.

0 comments on commit ed68b7d

Please sign in to comment.