G-Earth/Extensions/G-Translator_1.0.2/decompiled/gearth/misc/Cacher.java
Administrator 368b92d87a G-Earth 1.5.4 beta 22 - Initial release
Komplettes G-Earth Paket inkl. JRE, Extensions und Tools.

Extensions:
- G-BuildTools, G-Click Ultimate, G-Loader, G-Manipulate
- G-Presets, G-Translator, G-Trigger, G-itemViewer
- Market Utils, Packet Info Explorer, Plants
- RandomRoomVisitor, RoomLogger, Sanbovir Photo Inspector
- SpyFriends, WallAligner, XabboScripter, xabbo
2026-03-16 09:45:04 +01:00

135 lines
4.0 KiB
Java

/*
* Decompiled with CFR 0.152.
*/
package gearth.misc;
import gearth.GEarth;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class Cacher {
private static final String DEFAULT_CACHE_FILENAME = "cache.json";
private static String cacheDir;
public static void setCacheDir(String s) {
cacheDir = s;
}
public static String getCacheDir() {
return cacheDir;
}
public static boolean cacheFileExists(String cache_filename) {
File f = new File(Cacher.getCacheDir(), cache_filename);
return f.exists() && !f.isDirectory();
}
public static JSONObject getCacheContents(String cache_filename) {
if (Cacher.cacheFileExists(cache_filename)) {
try {
File f = new File(Cacher.getCacheDir(), cache_filename);
String contents = String.join((CharSequence)"\n", Files.readAllLines(f.toPath()));
return new JSONObject(contents);
}
catch (IOException e) {
e.printStackTrace();
}
}
return new JSONObject();
}
private static void updateCache(JSONObject contents, String cache_filename) {
Cacher.updateCache(contents.toString(), cache_filename);
}
public static void updateCache(String content, String cache_filename) {
File parent_dir = new File(Cacher.getCacheDir());
parent_dir.mkdirs();
try (FileWriter file = new FileWriter(new File(Cacher.getCacheDir(), cache_filename));){
file.write(content);
file.flush();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void put(String key, Object val, String cache_filename) {
JSONObject object = Cacher.getCacheContents(cache_filename);
if (object.has(key)) {
object.remove(key);
}
object.put(key, val);
Cacher.updateCache(object, cache_filename);
}
public static Object get(String key, String cache_filename) {
JSONObject object = Cacher.getCacheContents(cache_filename);
if (object.has(key)) {
return object.get(key);
}
return null;
}
public static List<Object> getList(String key, String cache_filename) {
JSONObject object = Cacher.getCacheContents(cache_filename);
if (object.has(key)) {
return ((JSONArray)object.get(key)).toList();
}
return null;
}
public static void clear(String cache_filename) {
Cacher.updateCache(new JSONObject(), cache_filename);
}
public static boolean cacheFileExists() {
return Cacher.cacheFileExists(DEFAULT_CACHE_FILENAME);
}
public static JSONObject getCacheContents() {
return Cacher.getCacheContents(DEFAULT_CACHE_FILENAME);
}
private static void updateCache(JSONObject contents) {
Cacher.updateCache(contents, DEFAULT_CACHE_FILENAME);
}
public static void put(String key, Object val) {
Cacher.put(key, val, DEFAULT_CACHE_FILENAME);
}
public static Object get(String key) {
return Cacher.get(key, DEFAULT_CACHE_FILENAME);
}
public static List<Object> getList(String key) {
return Cacher.getList(key, DEFAULT_CACHE_FILENAME);
}
public static void clear() {
Cacher.clear(DEFAULT_CACHE_FILENAME);
}
static {
File GEarthDir = null;
try {
GEarthDir = new File(GEarth.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getParentFile();
if (GEarthDir.getName().equals("Extensions")) {
GEarthDir = GEarthDir.getParentFile();
}
}
catch (URISyntaxException uRISyntaxException) {
// empty catch block
}
cacheDir = GEarthDir + File.separator + "Cache";
}
}