diff --git a/core/src/zero1hd/polyjet/util/Base64Preferences.java b/core/src/zero1hd/polyjet/util/Base64Preferences.java new file mode 100644 index 0000000..e21a1bf --- /dev/null +++ b/core/src/zero1hd/polyjet/util/Base64Preferences.java @@ -0,0 +1,48 @@ +package zero1hd.polyjet.util; + +import java.util.HashMap; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.files.FileHandle; +import com.badlogic.gdx.utils.Base64Coder; +import com.badlogic.gdx.utils.Json; + +public class Base64Preferences { + FileHandle base64File; + HashMap prefKeys; + Json json; + @SuppressWarnings("unchecked") + public Base64Preferences(String prefName) { + base64File = Gdx.files.external(".prefs/" + prefName); + json = new Json(); + + flush(); + } + + public void putString(String key, String string) { + prefKeys.put(key, string); + } + + public void putObj(String key, Object object) { + prefKeys.put(key, json.toJson(object)); + } + + public void writeBase64String(String text) { + base64File.writeString(Base64Coder.encodeString(text), true); + } + + public void writeBase64Object(Object obj) { + base64File.writeString(Base64Coder.encodeString(json.toJson(obj)), true); + } + + public void flush() { + prefKeys = json.fromJson(HashMap.class, new String(Base64Coder.decode(base64File.readString()))); + if (prefKeys == null) { + prefKeys = new HashMap<>(); + } + base64File.writeString(null, false); + writeBase64Object(json); + } + + +}