56 lines
2.0 KiB
Java
56 lines
2.0 KiB
Java
package io.gitlab.jfronny.translater;
|
|
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
|
|
import java.io.*;
|
|
import java.util.Properties;
|
|
|
|
public abstract class CachingTransformer implements StringTransformer {
|
|
Properties cache = null;
|
|
private static final File cacheFile = new File(FabricLoader.getInstance().getConfigDirectory(), "Translater.cache");
|
|
@Override
|
|
public String transform(String str) {
|
|
if (cache == null) {
|
|
cache = new Properties();
|
|
if (!ModInit.cfg.forceRegenerate) {
|
|
ModInit.cfg.forceRegenerate = false;
|
|
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 == 10) {
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
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);
|
|
}
|