Translater/src/main/java/io/gitlab/jfronny/translater/CachingTransformer.java

71 lines
2.6 KiB
Java
Raw Normal View History

2020-07-10 15:51:27 +02:00
package io.gitlab.jfronny.translater;
2020-07-13 17:16:06 +02:00
import io.gitlab.jfronny.translater.interfaces.StringTransformer;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.ConfigManager;
2020-07-10 15:51:27 +02:00
import net.fabricmc.loader.api.FabricLoader;
import java.io.*;
import java.util.Properties;
public abstract class CachingTransformer implements StringTransformer {
private static final File cacheFile = new File(FabricLoader.getInstance().getConfigDirectory(), "Translater.cache");
2020-07-10 21:24:20 +02:00
Properties cache = null;
2020-07-10 15:51:27 +02:00
@Override
public String transform(String str) {
2020-07-10 21:59:12 +02:00
if (str == null)
2020-07-13 17:16:06 +02:00
return null;
2020-07-10 15:51:27 +02:00
if (cache == null) {
cache = new Properties();
if (ModInit.cfg.forceRegenerate) {
2020-07-10 20:46:39 +02:00
ModInit.cfg.forceRegenerate = false;
((ConfigManager<Cfg>) AutoConfig.getConfigHolder(Cfg.class)).save();
} else {
2020-07-10 20:46:39 +02:00
if (cacheFile.exists()) {
try {
FileInputStream inS = new FileInputStream(cacheFile);
cache.load(inS);
inS.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (!ModInit.cfg.breakFully && ModInit.cfg.rounds == 5) {
2020-07-10 20:46:39 +02:00
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inS = classLoader.getResourceAsStream("namecache.ini");
if (inS != null) {
cache.load(inS);
inS.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
2020-07-10 15:51:27 +02:00
}
}
2020-07-10 21:16:49 +02:00
try {
FileOutputStream outS = new FileOutputStream(cacheFile);
cache.store(outS, "---Lang---");
outS.close();
} catch (IOException e) {
e.printStackTrace();
}
2020-07-10 15:51:27 +02:00
}
if (!cache.containsKey(str)) {
cache.put(str, getTransformed(str));
try {
FileOutputStream outS = new FileOutputStream(cacheFile);
cache.store(outS, "---Lang---");
outS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return (String) cache.get(str);
}
public abstract String getTransformed(String str);
}