Package squidpony.store.text
Class TextStorage
java.lang.Object
squidpony.store.text.TextStorage
public class TextStorage extends Object
Helps games store information in libGDX's Preferences class as Strings, then get it back out. Does not use JSON,
instead using a customized and customizable manual serialization style based around
StringConvert
.
Created by Tommy Ettinger on 9/16/2016.-
Field Summary
Fields Modifier and Type Field Description boolean
compress
protected OrderedMap<String,String>
contents
long[]
garbleKey
StringConvert<OrderedMap<String,String>>
mapConverter
com.badlogic.gdx.Preferences
preferences
String
storageName
-
Constructor Summary
Constructors Constructor Description TextStorage()
Please don't use this constructor if possible; it simply callsTextStorage(String)
with the constant String "nameless".TextStorage(String fileName)
Creates a JsonStorage with the given fileName to save using Preferences from libGDX.TextStorage(String fileName, long[] garble)
Creates a JsonStorage with the given fileName to save using Preferences from libGDX.TextStorage(String fileName, String garble)
Creates a JsonStorage with the given fileName to save using Preferences from libGDX. -
Method Summary
Modifier and Type Method Description TextStorage
clear()
Clears the current group of objects; recommended if you intend to store under multiple outerName keys.<T> T
get(String outerName, String innerName, CharSequence typeName, Class<T> type)
Gets an object from the storage by the givenouterName
key fromstore(String)
andinnerName
key fromput(String, Object, StringConvert)
, and uses the class given bytype
for the returned value, assuming it matches the object that was originally put with those keys.<T> T
get(String outerName, String innerName, StringConvert<?> converter, Class<T> type)
Gets an object from the storage by the givenouterName
key fromstore(String)
andinnerName
key fromput(String, Object, StringConvert)
, and uses the class given bytype
for the returned value, assuming it matches the object that was originally put with those keys.int
preferencesSize()
Gets the approximate size of the currently-stored preferences.<T> TextStorage
put(String innerName, T o, StringConvert converter)
Prepares to store the Objecto
to be retrieved withinnerName
in the current group of objects.TextStorage
remove(String innerName)
Removes one object from the current group by theinnerName
it was prepared with usingput(String, Object, StringConvert)
.String
show()
Gets a String representation of the data that would be saved whenstore(String)
is called.TextStorage
store(String outerName)
Actually stores all objects that had previously been prepared withput(String, Object, StringConvert)
, withouterName
used as a key to retrieve any object in the current group.
-
Field Details
-
Constructor Details
-
TextStorage
public TextStorage()Please don't use this constructor if possible; it simply callsTextStorage(String)
with the constant String "nameless". This could easily overlap with other files/sections in Preferences, so you should always prefer giving a String argument to the constructor, typically the name of the game.- See Also:
the recommended constructor to use
-
TextStorage
Creates a JsonStorage with the given fileName to save using Preferences from libGDX. The name should generally be the name of this game or application, and must be a valid name for a file (so no slashes, backslashes, colons, semicolons, or commas for certain, and other non-alphanumeric characters are also probably invalid). You should not assume anything is present in the Preferences storage unless you have put it there, and this applies doubly to games or applications other than your own; you should avoid values for fileName that might overlap with another game's Preferences values.
To organize saved data into sub-sections, you specify logical units (like different players' saved games) with a String outerName when you callstore(String)
, and can further distinguish data under the outerName when you callput(String, Object, StringConvert)
to put each individual item into the saved storage with its own innerName.
Calling this also sets up custom serializers for several important types in SquidLib; char[][], OrderedMap, IntDoubleOrderedMap, FakeLanguageGen, GreasedRegion, and notably Pattern from RegExodus all have smaller serialized representations than the default. OrderedMap allows non-String keys, which gets around a limitation in JSON maps normally, and both FakeLanguageGen and Pattern are amazingly smaller with the custom representation. The custom char[][] representation is about half the normal size by omitting commas after each char.- Parameters:
fileName
- the valid file name to create or open from Preferences; typically the name of the game/app.
-
TextStorage
Creates a JsonStorage with the given fileName to save using Preferences from libGDX. The name should generally be the name of this game or application, and must be a valid name for a file (so no slashes, backslashes, colons, semicolons, or commas for certain, and other non-alphanumeric characters are also probably invalid). You should not assume anything is present in the Preferences storage unless you have put it there, and this applies doubly to games or applications other than your own; you should avoid values for fileName that might overlap with another game's Preferences values. This constructor also allows you to specify a "garble" String; if this is non-null, it will be used as a key to obfuscate the output and de-obfuscate the loaded input using fairly basic methods. If garble is null, it is ignored.
To organize saved data into sub-sections, you specify logical units (like different players' saved games) with a String outerName when you callstore(String)
, and can further distinguish data under the outerName when you callput(String, Object, StringConvert)
to put each individual item into the saved storage with its own innerName.
Calling this also sets up custom serializers for several important types in SquidLib; char[][], OrderedMap, IntDoubleOrderedMap, FakeLanguageGen, GreasedRegion, and notably Pattern from RegExodus all have smaller serialized representations than the default. OrderedMap allows non-String keys, which gets around a limitation in JSON maps normally, and both FakeLanguageGen and Pattern are amazingly smaller with the custom representation. The custom char[][] representation is about half the normal size by omitting commas after each char.- Parameters:
fileName
- the valid file name to create or open from Preferences; typically the name of the game/app.garble
- the key that must be used exactly to decrypt any data saved by this TextStorage
-
TextStorage
Creates a JsonStorage with the given fileName to save using Preferences from libGDX. The name should generally be the name of this game or application, and must be a valid name for a file (so no slashes, backslashes, colons, semicolons, or commas for certain, and other non-alphanumeric characters are also probably invalid). You should not assume anything is present in the Preferences storage unless you have put it there, and this applies doubly to games or applications other than your own; you should avoid values for fileName that might overlap with another game's Preferences values. This constructor also allows you to specify a "garble" long array; if this is non-empty, it will be used as a key to obfuscate the output and de-obfuscate the loaded input using fairly basic methods. If garble is null or empty, it is ignored.
To organize saved data into sub-sections, you specify logical units (like different players' saved games) with a String outerName when you callstore(String)
, and can further distinguish data under the outerName when you callput(String, Object, StringConvert)
to put each individual item into the saved storage with its own innerName.
Calling this also sets up custom serializers for several important types in SquidLib; char[][], OrderedMap, IntDoubleOrderedMap, FakeLanguageGen, GreasedRegion, and notably Pattern from RegExodus all have smaller serialized representations than the default. OrderedMap allows non-String keys, which gets around a limitation in JSON maps normally, and both FakeLanguageGen and Pattern are amazingly smaller with the custom representation. The custom char[][] representation is about half the normal size by omitting commas after each char.- Parameters:
fileName
- the valid file name to create or open from Preferences; typically the name of the game/app.garble
- the key that must be used exactly to decrypt any data saved by this TextStorage; will be copied
-
-
Method Details
-
put
Prepares to store the Objecto
to be retrieved withinnerName
in the current group of objects. Does not write to a permanent location untilstore(String)
is called. The innerName used to store an object is required to get it back again, and can also be used to remove it before storing (or storing again).- Parameters:
innerName
- one of the two Strings needed to retrieve this latero
- the Object to prepare to storeconverter
- a StringConvert that supports the type of o- Returns:
- this for chaining
-
store
Actually stores all objects that had previously been prepared withput(String, Object, StringConvert)
, withouterName
used as a key to retrieve any object in the current group. Flushes the preferences, making the changes permanent (until overwritten), but does not change the current group (you may want to call this method again with additional items in the current group, and that would simply involve calling put() again). If you want to clear the current group, useclear()
. If you want to remove just one object from the current group, useremove(String)
.- Parameters:
outerName
- one of the two Strings needed to retrieve any of the objects in the current group- Returns:
- this for chaining
-
show
Gets a String representation of the data that would be saved whenstore(String)
is called. This can be useful for finding particularly problematic objects that require unnecessary space when serialized.- Returns:
- a String that previews what would be stored permanently when
store(String)
is called
-
clear
Clears the current group of objects; recommended if you intend to store under multiple outerName keys.- Returns:
- this for chaining
-
remove
Removes one object from the current group by theinnerName
it was prepared with usingput(String, Object, StringConvert)
. This does not affect already-stored objects unlessstore(String)
is called after this, in which case the new version of the current group, without the object this removed, is stored.- Parameters:
innerName
- the String key used to put an object in the current group withput(String, Object, StringConvert)
- Returns:
- this for chaining
-
get
Gets an object from the storage by the givenouterName
key fromstore(String)
andinnerName
key fromput(String, Object, StringConvert)
, and uses the class given bytype
for the returned value, assuming it matches the object that was originally put with those keys. If no such object exists, returns null. Results are undefined iftype
doesn't match the actual class of the stored object.- Type Parameters:
T
- the type of the value to retrieve; if type wasRNG.class
, this would beRNG
- Parameters:
outerName
- the key used to store the group of objects withstore(String)
innerName
- the key used to store the specific object withput(String, Object, StringConvert)
converter
- a StringConvert, such as one fromConverters
or found withStringConvert.get(CharSequence)
, to deserialize the datatype
- the class of the value; for a class like RNG, useRNG.class
, but changed to fit- Returns:
- the retrieved value if successful, or null otherwise
-
get
Gets an object from the storage by the givenouterName
key fromstore(String)
andinnerName
key fromput(String, Object, StringConvert)
, and uses the class given bytype
for the returned value, assuming it matches the object that was originally put with those keys. Uses typeName to find an appropriate StringConvert that has already been created (and thus registered), and because typeName is a CharSequence instead of a Class, it doesn't suffer from generic type erasure at runtime, It can and should have the generic type arguments as if it were the type for a variable, e.g.OrderedSet<ArrayList<String>>
. If no such object exists, returns null. Results are undefined iftype
doesn't match the actual class of the stored object, and this will return null if there is no known StringConvert for the given typeName.- Type Parameters:
T
- the type of the value to retrieve; if type wasRNG.class
, this would beRNG
- Parameters:
outerName
- the key used to store the group of objects withstore(String)
innerName
- the key used to store the specific object withput(String, Object, StringConvert)
typeName
- the name of the type to produce, with generic type parameters intact; used to find an appropriate StringConverttype
- the class of the value; for a class like RNG, useRNG.class
, but changed to fit- Returns:
- the retrieved value if successful, or null otherwise
-
preferencesSize
Gets the approximate size of the currently-stored preferences. This assumes UTF-16 storage, which is the case for GWT's LocalStorage. Since GWT is restricted to the size the browser permits for LocalStorage, and this limit can be rather small (about 5 MB, sometimes more but not reliably), this method is especially useful there, but it may yield inaccurate sizes on other platforms that save Preferences data differently.- Returns:
- the size, in bytes, of the already-stored preferences
-