mirror of
https://github.com/ViaVersion/ViaFabricPlus.git
synced 2024-10-03 05:17:29 +02:00
added more Abstraction for file saving
This commit is contained in:
parent
df598e95be
commit
73836131ec
@ -22,7 +22,7 @@ import com.google.gson.GsonBuilder;
|
||||
import de.florianmichael.viafabricplus.definition.ChatLengthDefinition;
|
||||
import de.florianmichael.viafabricplus.definition.ItemReleaseVersionDefinition;
|
||||
import de.florianmichael.viafabricplus.definition.PackFormatsDefinition;
|
||||
import de.florianmichael.viafabricplus.definition.bedrock.BedrockAccountManager;
|
||||
import de.florianmichael.viafabricplus.definition.bedrock.BedrockAccountHandler;
|
||||
import de.florianmichael.viafabricplus.definition.c0_30.ClassicItemSelectionScreen;
|
||||
import de.florianmichael.viafabricplus.definition.c0_30.protocol.CustomClassicProtocolExtensions;
|
||||
import de.florianmichael.viafabricplus.definition.c0_30.command.ClassicProtocolCommands;
|
||||
@ -66,7 +66,7 @@ public class ViaFabricPlus {
|
||||
ArmorPointsDefinition.load();
|
||||
|
||||
// Bedrock Stuff
|
||||
BedrockAccountManager.INSTANCE.load();
|
||||
BedrockAccountHandler.create();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus
|
||||
* Copyright (C) 2021-2023 FlorianMichael/EnZaXD 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 de.florianmichael.viafabricplus.definition.bedrock;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import de.florianmichael.viafabricplus.util.FileSaver;
|
||||
import net.raphimc.mcauth.MinecraftAuth;
|
||||
import net.raphimc.mcauth.step.bedrock.StepMCChain;
|
||||
import net.raphimc.mcauth.util.MicrosoftConstants;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class BedrockAccountHandler extends FileSaver {
|
||||
public static BedrockAccountHandler INSTANCE;
|
||||
|
||||
public static void create() {
|
||||
BedrockAccountHandler.INSTANCE = new BedrockAccountHandler();
|
||||
BedrockAccountHandler.INSTANCE.init();
|
||||
}
|
||||
|
||||
private StepMCChain.MCChain account;
|
||||
|
||||
public BedrockAccountHandler() {
|
||||
super("bedrock.account");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonObject object) {
|
||||
for (Map.Entry<String, JsonElement> entry : account.toJson().entrySet()) {
|
||||
object.add(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(JsonObject object) {
|
||||
try {
|
||||
account = MinecraftAuth.Bedrock.Title.MC_CHAIN.fromJson(object);
|
||||
try (final CloseableHttpClient httpClient = MicrosoftConstants.createHttpClient()) {
|
||||
account = MinecraftAuth.Bedrock.Title.MC_CHAIN.refresh(httpClient, account);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public StepMCChain.MCChain getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public void setAccount(StepMCChain.MCChain account) {
|
||||
this.account = account;
|
||||
}
|
||||
}
|
@ -1,78 +0,0 @@
|
||||
/*
|
||||
* This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus
|
||||
* Copyright (C) 2021-2023 FlorianMichael/EnZaXD 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 de.florianmichael.viafabricplus.definition.bedrock;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import de.florianmichael.viafabricplus.ViaFabricPlus;
|
||||
import net.raphimc.mcauth.MinecraftAuth;
|
||||
import net.raphimc.mcauth.step.bedrock.StepMCChain;
|
||||
import net.raphimc.mcauth.util.MicrosoftConstants;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class BedrockAccountManager {
|
||||
public final static BedrockAccountManager INSTANCE = new BedrockAccountManager();
|
||||
|
||||
private final File ACCOUNT_FILE = new File(ViaFabricPlus.RUN_DIRECTORY, "bedrock.account");
|
||||
private StepMCChain.MCChain account;
|
||||
|
||||
public void load() {
|
||||
if (ACCOUNT_FILE.exists()) {
|
||||
final JsonObject mcChain;
|
||||
try {
|
||||
mcChain = ViaFabricPlus.GSON.fromJson(new FileReader(ACCOUNT_FILE), JsonObject.class).getAsJsonObject();
|
||||
account = MinecraftAuth.Bedrock.Title.MC_CHAIN.fromJson(mcChain);
|
||||
|
||||
try (final CloseableHttpClient httpClient = MicrosoftConstants.createHttpClient()) {
|
||||
account = MinecraftAuth.Bedrock.Title.MC_CHAIN.refresh(httpClient, account);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::save));
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (account != null) {
|
||||
ACCOUNT_FILE.delete();
|
||||
try {
|
||||
ACCOUNT_FILE.createNewFile();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try (final FileWriter fw = new FileWriter(ACCOUNT_FILE)) {
|
||||
fw.write(ViaFabricPlus.GSON.toJson(account.toJson()));
|
||||
fw.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public StepMCChain.MCChain getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public void setAccount(StepMCChain.MCChain account) {
|
||||
this.account = account;
|
||||
}
|
||||
}
|
@ -26,7 +26,7 @@ import de.florianmichael.viafabricplus.injection.access.IClientPlayerEntity;
|
||||
import de.florianmichael.viafabricplus.injection.access.IScreenHandler;
|
||||
import de.florianmichael.viafabricplus.protocolhack.ProtocolHack;
|
||||
import de.florianmichael.viafabricplus.protocolhack.provider.viaversion.ViaFabricPlusHandItemProvider;
|
||||
import de.florianmichael.viafabricplus.util.ItemTranslator;
|
||||
import de.florianmichael.viafabricplus.protocolhack.usage.ItemTranslator;
|
||||
import de.florianmichael.vialoadingbase.ViaLoadingBase;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.network.ClientPlayNetworkHandler;
|
||||
|
@ -20,7 +20,7 @@ package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.screen;
|
||||
import com.viaversion.viaversion.api.connection.UserConnection;
|
||||
import com.viaversion.viaversion.api.minecraft.ProfileKey;
|
||||
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
||||
import de.florianmichael.viafabricplus.definition.bedrock.BedrockAccountManager;
|
||||
import de.florianmichael.viafabricplus.definition.bedrock.BedrockAccountHandler;
|
||||
import de.florianmichael.viafabricplus.injection.access.IPublicKeyData;
|
||||
import de.florianmichael.viafabricplus.definition.v1_19_0.storage.ChatSession1_19_0;
|
||||
import de.florianmichael.viafabricplus.definition.v1_19_2.storage.ChatSession1_19_2;
|
||||
@ -85,7 +85,7 @@ public class MixinConnectScreen_1 {
|
||||
return;
|
||||
}
|
||||
if (ViaLoadingBase.getClassWrapper().getTargetVersion().isEqualTo(BedrockProtocolVersion.bedrockLatest)) {
|
||||
final StepMCChain.MCChain account = BedrockAccountManager.INSTANCE.getAccount();
|
||||
final StepMCChain.MCChain account = BedrockAccountHandler.INSTANCE.getAccount();
|
||||
|
||||
if (account != null) {
|
||||
userConnection.put(new AuthChainData(userConnection, account.mojangJwt(), account.identityJwt(), account.publicKey(), account.privateKey()));
|
||||
|
@ -21,7 +21,7 @@ import com.viaversion.viaversion.api.connection.UserConnection;
|
||||
import com.viaversion.viaversion.api.minecraft.item.Item;
|
||||
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
||||
import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.HandItemProvider;
|
||||
import de.florianmichael.viafabricplus.util.ItemTranslator;
|
||||
import de.florianmichael.viafabricplus.protocolhack.usage.ItemTranslator;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public class ViaFabricPlusHandItemProvider extends HandItemProvider {
|
||||
|
@ -15,7 +15,7 @@
|
||||
* 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 de.florianmichael.viafabricplus.util;
|
||||
package de.florianmichael.viafabricplus.protocolhack.usage;
|
||||
|
||||
import com.viaversion.viaversion.api.Via;
|
||||
import com.viaversion.viaversion.api.connection.UserConnection;
|
@ -19,31 +19,20 @@ package de.florianmichael.viafabricplus.screen;
|
||||
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
||||
import de.florianmichael.viafabricplus.definition.bedrock.BedrockAccountManager;
|
||||
import de.florianmichael.viafabricplus.screen.settings.SettingsScreen;
|
||||
import de.florianmichael.viafabricplus.settings.groups.GeneralSettings;
|
||||
import de.florianmichael.vialoadingbase.ViaLoadingBase;
|
||||
import de.florianmichael.vialoadingbase.platform.InternalProtocolList;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.font.TextRenderer;
|
||||
import net.minecraft.client.gui.screen.NoticeScreen;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.screen.TitleScreen;
|
||||
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
|
||||
import net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget;
|
||||
import net.minecraft.client.gui.widget.ButtonWidget;
|
||||
import net.minecraft.client.sound.PositionedSoundInstance;
|
||||
import net.minecraft.client.util.math.MatrixStack;
|
||||
import net.minecraft.sound.SoundEvents;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Util;
|
||||
import net.raphimc.mcauth.MinecraftAuth;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@SuppressWarnings({"DataFlowIssue", "DuplicatedCode"})
|
||||
public class ProtocolSelectionScreen extends Screen {
|
||||
|
@ -18,24 +18,26 @@
|
||||
package de.florianmichael.viafabricplus.settings;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import de.florianmichael.viafabricplus.ViaFabricPlus;
|
||||
import de.florianmichael.viafabricplus.event.InitializeSettingsCallback;
|
||||
import de.florianmichael.viafabricplus.settings.base.AbstractSetting;
|
||||
import de.florianmichael.viafabricplus.settings.base.SettingGroup;
|
||||
import de.florianmichael.viafabricplus.settings.groups.*;
|
||||
import de.florianmichael.viafabricplus.util.FileSaver;
|
||||
import de.florianmichael.vialoadingbase.ViaLoadingBase;
|
||||
import de.florianmichael.vialoadingbase.platform.InternalProtocolList;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class SettingsSystem {
|
||||
private final File CONFIG_FILE = new File(ViaFabricPlus.RUN_DIRECTORY, "settings.json");
|
||||
|
||||
public class SettingsSystem extends FileSaver {
|
||||
private final List<SettingGroup> groups = new ArrayList<>();
|
||||
|
||||
public SettingsSystem() {
|
||||
super("settings.json");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
addGroup(
|
||||
GeneralSettings.INSTANCE,
|
||||
@ -48,56 +50,35 @@ public class SettingsSystem {
|
||||
|
||||
InitializeSettingsCallback.EVENT.invoker().onInitializeSettings();
|
||||
|
||||
loadConfig();
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::save));
|
||||
super.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JsonObject object) {
|
||||
object.addProperty("protocol", ViaLoadingBase.getClassWrapper().getTargetVersion().getVersion());
|
||||
for (SettingGroup group : groups) {
|
||||
for (AbstractSetting<?> setting : group.getSettings()) {
|
||||
setting.write(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(JsonObject object) {
|
||||
if (object.has("protocol")) {
|
||||
ViaLoadingBase.getClassWrapper().reload(InternalProtocolList.fromProtocolId(object.get("protocol").getAsInt()));
|
||||
}
|
||||
for (SettingGroup group : groups) {
|
||||
for (AbstractSetting<?> setting : group.getSettings()) {
|
||||
setting.read(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addGroup(final SettingGroup... groups) {
|
||||
Collections.addAll(this.groups, groups);
|
||||
}
|
||||
|
||||
public void loadConfig() {
|
||||
if (CONFIG_FILE.exists()) {
|
||||
final JsonObject parentNode;
|
||||
try {
|
||||
parentNode = ViaFabricPlus.GSON.fromJson(new FileReader(CONFIG_FILE), JsonObject.class).getAsJsonObject();
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (parentNode.has("protocol")) {
|
||||
ViaLoadingBase.getClassWrapper().reload(InternalProtocolList.fromProtocolId(parentNode.get("protocol").getAsInt()));
|
||||
}
|
||||
for (SettingGroup group : groups) {
|
||||
for (AbstractSetting<?> setting : group.getSettings()) {
|
||||
setting.read(parentNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
CONFIG_FILE.delete();
|
||||
try {
|
||||
CONFIG_FILE.createNewFile();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try (final FileWriter fw = new FileWriter(CONFIG_FILE)) {
|
||||
final JsonObject parentNode = new JsonObject();
|
||||
parentNode.addProperty("protocol", ViaLoadingBase.getClassWrapper().getTargetVersion().getVersion());
|
||||
for (SettingGroup group : groups) {
|
||||
for (AbstractSetting<?> setting : group.getSettings()) {
|
||||
setting.write(parentNode);
|
||||
}
|
||||
}
|
||||
fw.write(ViaFabricPlus.GSON.toJson(parentNode));
|
||||
fw.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public List<SettingGroup> getGroups() {
|
||||
return groups;
|
||||
}
|
||||
|
@ -17,7 +17,7 @@
|
||||
*/
|
||||
package de.florianmichael.viafabricplus.settings.groups;
|
||||
|
||||
import de.florianmichael.viafabricplus.definition.bedrock.BedrockAccountManager;
|
||||
import de.florianmichael.viafabricplus.definition.bedrock.BedrockAccountHandler;
|
||||
import de.florianmichael.viafabricplus.screen.ProtocolSelectionScreen;
|
||||
import de.florianmichael.viafabricplus.screen.settings.SettingsScreen;
|
||||
import de.florianmichael.viafabricplus.settings.base.SettingGroup;
|
||||
@ -28,7 +28,6 @@ import net.minecraft.client.gui.screen.TitleScreen;
|
||||
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Formatting;
|
||||
import net.minecraft.util.Util;
|
||||
import net.raphimc.mcauth.MinecraftAuth;
|
||||
|
||||
@ -41,7 +40,7 @@ public class BedrockSettings extends SettingGroup {
|
||||
|
||||
public final ButtonSetting BEDROCK_ACCOUNT = new ButtonSetting(this, Text.translatable("bedrock.viafabricplus.set"), () -> CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
BedrockAccountManager.INSTANCE.setAccount(MinecraftAuth.requestBedrockLogin(msaDeviceCode -> {
|
||||
BedrockAccountHandler.INSTANCE.setAccount(MinecraftAuth.requestBedrockLogin(msaDeviceCode -> {
|
||||
MinecraftClient.getInstance().execute(() -> MinecraftClient.getInstance().setScreen(new NoticeScreen(() -> {
|
||||
MinecraftClient.getInstance().setScreen(SettingsScreen.get(new MultiplayerScreen(new TitleScreen())));
|
||||
Thread.currentThread().interrupt();
|
||||
@ -62,8 +61,8 @@ public class BedrockSettings extends SettingGroup {
|
||||
})) {
|
||||
@Override
|
||||
public MutableText displayValue() {
|
||||
if (BedrockAccountManager.INSTANCE.getAccount() != null) {
|
||||
return Text.literal("Bedrock account: " + BedrockAccountManager.INSTANCE.getAccount().displayName());
|
||||
if (BedrockAccountHandler.INSTANCE.getAccount() != null) {
|
||||
return Text.literal("Bedrock account: " + BedrockAccountHandler.INSTANCE.getAccount().displayName());
|
||||
}
|
||||
return super.displayValue();
|
||||
}
|
||||
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus
|
||||
* Copyright (C) 2021-2023 FlorianMichael/EnZaXD 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 de.florianmichael.viafabricplus.util;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import de.florianmichael.viafabricplus.ViaFabricPlus;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public abstract class FileSaver {
|
||||
private final File file;
|
||||
|
||||
public FileSaver(final String name) {
|
||||
file = new File(ViaFabricPlus.RUN_DIRECTORY, name);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
if (file.exists()) {
|
||||
final JsonObject parentNode;
|
||||
try {
|
||||
parentNode = ViaFabricPlus.GSON.fromJson(new FileReader(file), JsonObject.class).getAsJsonObject();
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
read(parentNode);
|
||||
}
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
file.delete();
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try (final FileWriter fw = new FileWriter(file)) {
|
||||
final JsonObject parentNode = new JsonObject();
|
||||
write(parentNode);
|
||||
fw.write(ViaFabricPlus.GSON.toJson(parentNode));
|
||||
fw.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public abstract void write(final JsonObject object);
|
||||
public abstract void read(final JsonObject object);
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user