From f390f4f6dd065b6fea06aaab75b0d1ac81ba2954 Mon Sep 17 00:00:00 2001 From: Recrown Date: Tue, 9 May 2017 15:27:25 -0500 Subject: [PATCH] added base 64 preference --- .../polyjet/util/Base64Preferences.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 core/src/zero1hd/polyjet/util/Base64Preferences.java 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); + } + + +}