mirror of
https://github.com/Wurst-Imperium/Wurst7.git
synced 2024-09-20 01:12:13 +02:00
Improve config files
This commit is contained in:
parent
a9b66d91ca
commit
81e0f50b54
@ -7,19 +7,16 @@
|
||||
*/
|
||||
package net.wurstclient.analytics;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
import net.wurstclient.analytics.dmurph.VisitorData;
|
||||
import net.wurstclient.utils.JsonException;
|
||||
import net.wurstclient.utils.JsonUtils;
|
||||
import net.wurstclient.utils.WsonObject;
|
||||
|
||||
public final class AnalyticsConfigFile
|
||||
{
|
||||
@ -34,72 +31,29 @@ public final class AnalyticsConfigFile
|
||||
{
|
||||
try
|
||||
{
|
||||
JsonElement json = parseJson(path);
|
||||
tracker.setEnabled(readEnabled(json));
|
||||
tracker.getConfigData().setVisitorData(readVisitorData(json));
|
||||
|
||||
}catch(ConfigFileException e)
|
||||
{
|
||||
System.out.println("Couldn't load " + path.getFileName());
|
||||
e.printStackTrace();
|
||||
WsonObject wson = JsonUtils.parseWsonObject(path);
|
||||
tracker.setEnabled(wson.getBoolean("enabled"));
|
||||
tracker.getConfigData().setVisitorData(readVisitorData(wson));
|
||||
|
||||
}catch(NoSuchFileException e)
|
||||
{
|
||||
// The file doesn't exist yet. No problem, we'll create it later.
|
||||
|
||||
}catch(IOException | JsonException e)
|
||||
{
|
||||
System.out.println("Couldn't load " + path.getFileName());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
save(tracker);
|
||||
}
|
||||
|
||||
private JsonElement parseJson(Path path)
|
||||
throws NoSuchFileException, ConfigFileException
|
||||
private VisitorData readVisitorData(WsonObject wson) throws JsonException
|
||||
{
|
||||
try(BufferedReader reader = Files.newBufferedReader(path))
|
||||
{
|
||||
return JsonUtils.JSON_PARSER.parse(reader);
|
||||
|
||||
}catch(NoSuchFileException e)
|
||||
{
|
||||
throw e;
|
||||
|
||||
}catch(IOException | JsonParseException e)
|
||||
{
|
||||
throw new ConfigFileException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean readEnabled(JsonElement json) throws ConfigFileException
|
||||
{
|
||||
try
|
||||
{
|
||||
return json.getAsJsonObject().get("enabled").getAsBoolean();
|
||||
|
||||
}catch(Exception e)
|
||||
{
|
||||
throw new ConfigFileException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private VisitorData readVisitorData(JsonElement json)
|
||||
throws ConfigFileException
|
||||
{
|
||||
int visitorID;
|
||||
long firstLaunch;
|
||||
long lastLaunch;
|
||||
int launches;
|
||||
|
||||
try
|
||||
{
|
||||
JsonObject jo = json.getAsJsonObject();
|
||||
visitorID = jo.get("id").getAsInt();
|
||||
firstLaunch = jo.get("first_launch").getAsLong();
|
||||
lastLaunch = jo.get("last_launch").getAsLong();
|
||||
launches = jo.get("launches").getAsInt();
|
||||
|
||||
}catch(Exception e)
|
||||
{
|
||||
throw new ConfigFileException(e);
|
||||
}
|
||||
int visitorID = wson.getInt("id");
|
||||
long firstLaunch = wson.getLong("first_launch");
|
||||
long lastLaunch = wson.getLong("last_launch");
|
||||
int launches = wson.getInt("launches");
|
||||
|
||||
return VisitorData.newSession(visitorID, firstLaunch, lastLaunch,
|
||||
launches);
|
||||
@ -109,11 +63,11 @@ public final class AnalyticsConfigFile
|
||||
{
|
||||
JsonObject json = createJson(tracker);
|
||||
|
||||
try(BufferedWriter writer = Files.newBufferedWriter(path))
|
||||
try
|
||||
{
|
||||
JsonUtils.PRETTY_GSON.toJson(json, writer);
|
||||
JsonUtils.toJson(json, path);
|
||||
|
||||
}catch(IOException | JsonParseException e)
|
||||
}catch(IOException | JsonException e)
|
||||
{
|
||||
System.out.println("Couldn't save " + path.getFileName());
|
||||
e.printStackTrace();
|
||||
@ -133,12 +87,4 @@ public final class AnalyticsConfigFile
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
private static final class ConfigFileException extends Exception
|
||||
{
|
||||
public ConfigFileException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,22 +7,16 @@
|
||||
*/
|
||||
package net.wurstclient.hack;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
import net.wurstclient.utils.JsonException;
|
||||
import net.wurstclient.utils.JsonUtils;
|
||||
import net.wurstclient.utils.WsonArray;
|
||||
|
||||
public final class EnabledHacksFile
|
||||
{
|
||||
@ -38,61 +32,43 @@ public final class EnabledHacksFile
|
||||
{
|
||||
try
|
||||
{
|
||||
JsonArray json = parseJson(path);
|
||||
enableHacks(hackList, json);
|
||||
|
||||
}catch(ConfigFileException e)
|
||||
{
|
||||
System.out.println("Couldn't load " + path.getFileName());
|
||||
e.printStackTrace();
|
||||
WsonArray wson = JsonUtils.parseWsonArray(path);
|
||||
enableHacks(hackList, wson);
|
||||
|
||||
}catch(NoSuchFileException e)
|
||||
{
|
||||
// The file doesn't exist yet. No problem, we'll create it later.
|
||||
|
||||
}catch(IOException | JsonException e)
|
||||
{
|
||||
System.out.println("Couldn't load " + path.getFileName());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
save(hackList);
|
||||
}
|
||||
|
||||
private JsonArray parseJson(Path path)
|
||||
throws NoSuchFileException, ConfigFileException
|
||||
private void enableHacks(HackList hackList, WsonArray wson)
|
||||
{
|
||||
try(BufferedReader reader = Files.newBufferedReader(path))
|
||||
try
|
||||
{
|
||||
JsonElement json = JsonUtils.JSON_PARSER.parse(reader);
|
||||
if(!json.isJsonArray())
|
||||
throw new ConfigFileException();
|
||||
disableSaving = true;
|
||||
|
||||
return json.getAsJsonArray();
|
||||
for(String name : wson.getAllStrings())
|
||||
{
|
||||
Hack hack = hackList.getHackByName(name);
|
||||
if(hack == null || !hack.isStateSaved())
|
||||
continue;
|
||||
|
||||
hack.setEnabled(true);
|
||||
}
|
||||
|
||||
}catch(NoSuchFileException e)
|
||||
}finally
|
||||
{
|
||||
throw e;
|
||||
|
||||
}catch(IOException | JsonParseException e)
|
||||
{
|
||||
throw new ConfigFileException(e);
|
||||
disableSaving = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void enableHacks(HackList hackList, JsonArray json)
|
||||
{
|
||||
Stream<JsonElement> jsonElements =
|
||||
StreamSupport.stream(json.spliterator(), false);
|
||||
|
||||
Stream<String> names = jsonElements.filter(JsonElement::isJsonPrimitive)
|
||||
.map(JsonElement::getAsJsonPrimitive)
|
||||
.filter(JsonPrimitive::isString).map(JsonPrimitive::getAsString);
|
||||
|
||||
Stream<Hack> hacksToEnable =
|
||||
names.map(name -> hackList.getHackByName(name))
|
||||
.filter(Objects::nonNull).filter(Hack::isStateSaved);
|
||||
|
||||
disableSaving = true;
|
||||
hacksToEnable.forEach(hack -> hack.setEnabled(true));
|
||||
disableSaving = false;
|
||||
}
|
||||
|
||||
public void save(HackList hax)
|
||||
{
|
||||
if(disableSaving)
|
||||
@ -100,11 +76,11 @@ public final class EnabledHacksFile
|
||||
|
||||
JsonArray json = createJson(hax);
|
||||
|
||||
try(BufferedWriter writer = Files.newBufferedWriter(path))
|
||||
try
|
||||
{
|
||||
JsonUtils.PRETTY_GSON.toJson(json, writer);
|
||||
JsonUtils.toJson(json, path);
|
||||
|
||||
}catch(IOException | JsonParseException e)
|
||||
}catch(IOException | JsonException e)
|
||||
{
|
||||
System.out.println("Couldn't save " + path.getFileName());
|
||||
e.printStackTrace();
|
||||
@ -121,17 +97,4 @@ public final class EnabledHacksFile
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
private static final class ConfigFileException extends Exception
|
||||
{
|
||||
public ConfigFileException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public ConfigFileException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
21
src/main/java/net/wurstclient/utils/JsonException.java
Normal file
21
src/main/java/net/wurstclient/utils/JsonException.java
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (C) 2014 - 2019 | Wurst-Imperium | All rights reserved.
|
||||
*
|
||||
* This source code is subject to the terms of the GNU General Public
|
||||
* License, version 3. If a copy of the GPL was not distributed with this
|
||||
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
package net.wurstclient.utils;
|
||||
|
||||
public final class JsonException extends Exception
|
||||
{
|
||||
public JsonException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public JsonException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
@ -7,9 +7,13 @@
|
||||
*/
|
||||
package net.wurstclient.utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonParser;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import com.google.gson.*;
|
||||
|
||||
public enum JsonUtils
|
||||
{
|
||||
@ -21,4 +25,105 @@ public enum JsonUtils
|
||||
new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
public static final JsonParser JSON_PARSER = new JsonParser();
|
||||
|
||||
public static JsonElement parse(Path path) throws IOException, JsonException
|
||||
{
|
||||
try(BufferedReader reader = Files.newBufferedReader(path))
|
||||
{
|
||||
return JSON_PARSER.parse(reader);
|
||||
|
||||
}catch(JsonParseException e)
|
||||
{
|
||||
throw new JsonException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonArray parseJsonArray(Path path)
|
||||
throws IOException, JsonException
|
||||
{
|
||||
JsonElement json = parse(path);
|
||||
|
||||
if(!json.isJsonArray())
|
||||
throw new JsonException();
|
||||
|
||||
return json.getAsJsonArray();
|
||||
}
|
||||
|
||||
public static WsonArray parseWsonArray(Path path)
|
||||
throws IOException, JsonException
|
||||
{
|
||||
return new WsonArray(parseJsonArray(path));
|
||||
}
|
||||
|
||||
public static JsonObject parseJsonObject(Path path)
|
||||
throws IOException, JsonException
|
||||
{
|
||||
JsonElement json = parse(path);
|
||||
|
||||
if(!json.isJsonObject())
|
||||
throw new JsonException();
|
||||
|
||||
return json.getAsJsonObject();
|
||||
}
|
||||
|
||||
public static WsonObject parseWsonObject(Path path)
|
||||
throws IOException, JsonException
|
||||
{
|
||||
return new WsonObject(parseJsonObject(path));
|
||||
}
|
||||
|
||||
public static void toJson(JsonElement json, Path path)
|
||||
throws IOException, JsonException
|
||||
{
|
||||
try(BufferedWriter writer = Files.newBufferedWriter(path))
|
||||
{
|
||||
JsonUtils.PRETTY_GSON.toJson(json, writer);
|
||||
|
||||
}catch(JsonParseException e)
|
||||
{
|
||||
throw new JsonException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isBoolean(JsonElement json)
|
||||
{
|
||||
if(json == null || !json.isJsonPrimitive())
|
||||
return false;
|
||||
|
||||
JsonPrimitive primitive = json.getAsJsonPrimitive();
|
||||
return primitive.isBoolean();
|
||||
}
|
||||
|
||||
public static boolean getAsBoolean(JsonElement json) throws JsonException
|
||||
{
|
||||
if(!isBoolean(json))
|
||||
throw new JsonException();
|
||||
|
||||
return json.getAsBoolean();
|
||||
}
|
||||
|
||||
public static boolean isNumber(JsonElement json)
|
||||
{
|
||||
if(json == null || !json.isJsonPrimitive())
|
||||
return false;
|
||||
|
||||
JsonPrimitive primitive = json.getAsJsonPrimitive();
|
||||
return primitive.isNumber();
|
||||
}
|
||||
|
||||
public static int getAsInt(JsonElement json) throws JsonException
|
||||
{
|
||||
if(!isNumber(json))
|
||||
throw new JsonException();
|
||||
|
||||
return json.getAsInt();
|
||||
}
|
||||
|
||||
public static long getAsLong(JsonElement json) throws JsonException
|
||||
{
|
||||
if(!isNumber(json))
|
||||
throw new JsonException();
|
||||
|
||||
return json.getAsLong();
|
||||
}
|
||||
}
|
||||
|
41
src/main/java/net/wurstclient/utils/WsonArray.java
Normal file
41
src/main/java/net/wurstclient/utils/WsonArray.java
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) 2014 - 2019 | Wurst-Imperium | All rights reserved.
|
||||
*
|
||||
* This source code is subject to the terms of the GNU General Public
|
||||
* License, version 3. If a copy of the GPL was not distributed with this
|
||||
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
package net.wurstclient.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public final class WsonArray
|
||||
{
|
||||
private final JsonArray json;
|
||||
|
||||
public WsonArray(JsonArray json)
|
||||
{
|
||||
this.json = Objects.requireNonNull(json);
|
||||
}
|
||||
|
||||
public ArrayList<String> getAllStrings()
|
||||
{
|
||||
return StreamSupport.stream(json.spliterator(), false)
|
||||
.filter(JsonElement::isJsonPrimitive)
|
||||
.map(JsonElement::getAsJsonPrimitive)
|
||||
.filter(JsonPrimitive::isString).map(JsonPrimitive::getAsString)
|
||||
.collect(Collectors.toCollection(() -> new ArrayList<>()));
|
||||
}
|
||||
|
||||
public JsonArray toJsonArray()
|
||||
{
|
||||
return json;
|
||||
}
|
||||
}
|
42
src/main/java/net/wurstclient/utils/WsonObject.java
Normal file
42
src/main/java/net/wurstclient/utils/WsonObject.java
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) 2014 - 2019 | Wurst-Imperium | All rights reserved.
|
||||
*
|
||||
* This source code is subject to the terms of the GNU General Public
|
||||
* License, version 3. If a copy of the GPL was not distributed with this
|
||||
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
*/
|
||||
package net.wurstclient.utils;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public final class WsonObject
|
||||
{
|
||||
private final JsonObject json;
|
||||
|
||||
public WsonObject(JsonObject json)
|
||||
{
|
||||
this.json = Objects.requireNonNull(json);
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key) throws JsonException
|
||||
{
|
||||
return JsonUtils.getAsBoolean(json.get(key));
|
||||
}
|
||||
|
||||
public int getInt(String key) throws JsonException
|
||||
{
|
||||
return JsonUtils.getAsInt(json.get(key));
|
||||
}
|
||||
|
||||
public long getLong(String key) throws JsonException
|
||||
{
|
||||
return JsonUtils.getAsLong(json.get(key));
|
||||
}
|
||||
|
||||
public JsonObject toJsonObject()
|
||||
{
|
||||
return json;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user