2020-11-24 22:04:13 +01:00
|
|
|
package io.gitlab.jfronny.respackopts;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
2020-12-21 14:51:49 +01:00
|
|
|
import io.gitlab.jfronny.respackopts.conditions.SyntaxError;
|
2020-11-24 22:04:13 +01:00
|
|
|
import io.gitlab.jfronny.respackopts.data.Config;
|
|
|
|
import io.gitlab.jfronny.respackopts.data.Respackmeta;
|
|
|
|
import net.fabricmc.api.ClientModInitializer;
|
|
|
|
import net.fabricmc.api.EnvType;
|
|
|
|
import net.fabricmc.api.Environment;
|
|
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.HashMap;
|
2020-11-25 21:58:21 +01:00
|
|
|
import java.util.HashSet;
|
2020-12-21 14:51:49 +01:00
|
|
|
import java.util.Objects;
|
2020-11-26 20:11:38 +01:00
|
|
|
import java.util.TreeSet;
|
2020-12-21 14:51:49 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2020-11-24 22:04:13 +01:00
|
|
|
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
|
|
public class Respackopts implements ClientModInitializer {
|
|
|
|
public static HashMap<String, HashMap<String, Boolean>> boolVals;
|
|
|
|
public static HashMap<String, HashMap<String, Double>> numVals;
|
|
|
|
public static HashMap<String, HashMap<String, String>> strVals;
|
2020-11-26 20:11:38 +01:00
|
|
|
public static HashMap<String, HashMap<String, TreeSet<String>>> enumKeys;
|
2020-11-24 22:04:13 +01:00
|
|
|
public static Gson g = new Gson();
|
|
|
|
public static GuiFactory factory = new GuiFactory();
|
|
|
|
public static final Integer metaVersion = 1;
|
|
|
|
public static HashMap<String, Respackmeta> resPackMetas = new HashMap<>();
|
|
|
|
public static final String ID = "respackopts";
|
2020-11-25 14:28:29 +01:00
|
|
|
static final Path p = FabricLoader.getInstance().getConfigDir().resolve("respackopts");
|
2020-11-26 20:11:38 +01:00
|
|
|
public static final HashSet<Runnable> saveActions = new HashSet<>();
|
2020-12-21 14:51:49 +01:00
|
|
|
public static final String fileExtension = ".rpo";
|
|
|
|
public static boolean forceRespackReload = false;
|
2020-11-24 22:04:13 +01:00
|
|
|
@Override
|
|
|
|
public void onInitializeClient() {
|
2020-12-20 14:24:48 +01:00
|
|
|
deNull();
|
2020-11-25 21:58:21 +01:00
|
|
|
if (FabricLoader.getInstance().isDevelopmentEnvironment())
|
2020-11-26 20:11:38 +01:00
|
|
|
saveActions.add(() -> System.out.println("Save"));
|
2020-11-25 14:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void save() {
|
|
|
|
for (String s : resPackMetas.keySet()) {
|
|
|
|
s = resPackMetas.get(s).id;
|
|
|
|
Config cfg = new Config();
|
|
|
|
if (boolVals.containsKey(s))
|
|
|
|
cfg.bools = boolVals.get(s);
|
|
|
|
if (numVals.containsKey(s))
|
|
|
|
cfg.doubles = numVals.get(s);
|
|
|
|
if (strVals.containsKey(s))
|
|
|
|
cfg.strings = strVals.get(s);
|
|
|
|
Path q = p.resolve(s + ".json");
|
|
|
|
try {
|
|
|
|
Writer writer = Files.newBufferedWriter(q);
|
|
|
|
g.toJson(cfg, writer);
|
|
|
|
writer.flush();
|
|
|
|
writer.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 20:11:38 +01:00
|
|
|
for (Runnable action : saveActions) {
|
|
|
|
action.run();
|
|
|
|
}
|
2020-11-25 14:28:29 +01:00
|
|
|
}
|
|
|
|
|
2020-12-20 14:24:48 +01:00
|
|
|
public static void load(String id) {
|
|
|
|
Path q = p.resolve(id + ".json");
|
|
|
|
if (Files.exists(q)) {
|
|
|
|
try {
|
|
|
|
Reader reader = Files.newBufferedReader(q);
|
|
|
|
Config c = g.fromJson(reader, Config.class);
|
|
|
|
reader.close();
|
2020-12-20 22:28:25 +01:00
|
|
|
if (c.bools != null) {
|
|
|
|
boolVals.get(id).forEach((i, val) -> {
|
|
|
|
if (c.bools.containsKey(i)) {
|
|
|
|
boolVals.get(id).put(i, c.bools.get(i));
|
|
|
|
} else
|
|
|
|
System.out.println("Could not find bool " + i + " in " + id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (c.doubles != null) {
|
|
|
|
numVals.get(id).forEach((i, val) -> {
|
|
|
|
if (c.doubles.containsKey(i)) {
|
|
|
|
numVals.get(id).put(i, c.doubles.get(i));
|
|
|
|
} else
|
|
|
|
System.out.println("Could not find num " + i + " in " + id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (c.strings != null) {
|
|
|
|
strVals.get(id).forEach((i, val) -> {
|
|
|
|
if (c.strings.containsKey(i)) {
|
|
|
|
strVals.get(id).put(i, c.strings.get(i));
|
|
|
|
} else
|
|
|
|
System.out.println("Could not find str " + i + " in " + id);
|
|
|
|
});
|
|
|
|
}
|
2020-12-20 14:24:48 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:28:29 +01:00
|
|
|
}
|
|
|
|
|
2020-11-26 20:11:38 +01:00
|
|
|
private static void deNull() {
|
2020-12-20 14:24:48 +01:00
|
|
|
try {
|
|
|
|
Files.createDirectories(p);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-11-24 22:04:13 +01:00
|
|
|
if (boolVals == null)
|
|
|
|
boolVals = new HashMap<>();
|
|
|
|
if (numVals == null)
|
|
|
|
numVals = new HashMap<>();
|
|
|
|
if (strVals == null)
|
|
|
|
strVals = new HashMap<>();
|
2020-11-26 20:11:38 +01:00
|
|
|
if (enumKeys == null)
|
|
|
|
enumKeys = new HashMap<>();
|
2020-11-25 20:14:58 +01:00
|
|
|
}
|
2020-12-21 14:51:49 +01:00
|
|
|
|
|
|
|
public static boolean matchStringCondition(String condition) throws SyntaxError {
|
|
|
|
if (condition == null) {
|
|
|
|
throw new SyntaxError("Condition must not be null");
|
|
|
|
}
|
|
|
|
if (!condition.contains(":")) {
|
|
|
|
throw new SyntaxError("You must include you resource pack ID in conditions (format: pack:some.key)");
|
|
|
|
}
|
|
|
|
AtomicBoolean found = new AtomicBoolean(false);
|
|
|
|
AtomicBoolean output = new AtomicBoolean(false);
|
|
|
|
Respackopts.resPackMetas.forEach((r, v) -> {
|
|
|
|
String sourcePack = condition.split(":")[0];
|
|
|
|
String name = condition.substring(condition.indexOf(':') + 1);
|
|
|
|
if (Objects.equals(v.id, sourcePack)) {
|
|
|
|
found.set(true);
|
|
|
|
output.set(Respackopts.boolVals.get(sourcePack).get(name));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!found.get()) {
|
|
|
|
throw new SyntaxError("Could not find pack with specified ID");
|
|
|
|
}
|
|
|
|
return output.get();
|
|
|
|
}
|
2020-11-24 22:04:13 +01:00
|
|
|
}
|