Skip to content

Commit

Permalink
Log the exception inside flush and fix NullPointerException if LocalS…
Browse files Browse the repository at this point in the history
…torage is not supported (#5838)

* log the exception inside flush, remove unnecessary creation of objects from the same type in toObject (the constructors are also deprecated since java 9)

* add preferences test

* null check if LocalStorage is not supported to prevent game interruption
  • Loading branch information
SimonIT authored and MobiDevelop committed Nov 27, 2019
1 parent 25da0db commit c0c1f51
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
Expand Up @@ -24,7 +24,7 @@

public class GwtFiles implements Files {

public static final Storage LocalStorage = Storage.getLocalStorageIfSupported();
public static final Storage LocalStorage = Storage.getLocalStorageIfSupported(); // Can be null if cookies are disabled or blocked by the browser with "block third-party cookies"

final Preloader preloader;

Expand Down
Expand Up @@ -30,24 +30,26 @@ public class GwtPreferences implements Preferences {
GwtPreferences (String prefix) {
this.prefix = prefix + ":";
int prefixLength = this.prefix.length();
try {
for (int i = 0; i < GwtFiles.LocalStorage.getLength(); i++) {
String key = GwtFiles.LocalStorage.key(i);
if (key.startsWith(prefix)) {
String value = GwtFiles.LocalStorage.getItem(key);
values.put(key.substring(prefixLength, key.length() - 1), toObject(key, value));
if (GwtFiles.LocalStorage != null) {
try {
for (int i = 0; i < GwtFiles.LocalStorage.getLength(); i++) {
String key = GwtFiles.LocalStorage.key(i);
if (key.startsWith(prefix)) {
String value = GwtFiles.LocalStorage.getItem(key);
values.put(key.substring(prefixLength, key.length() - 1), toObject(key, value));
}
}
} catch (Exception e) {
values.clear();
}
} catch (Exception e) {
values.clear();
}
}

private Object toObject (String key, String value) {
if (key.endsWith("b")) return new Boolean(Boolean.parseBoolean(value));
if (key.endsWith("i")) return new Integer(Integer.parseInt(value));
if (key.endsWith("l")) return new Long(Long.parseLong(value));
if (key.endsWith("f")) return new Float(Float.parseFloat(value));
if (key.endsWith("b")) return Boolean.parseBoolean(value);
if (key.endsWith("i")) return Integer.parseInt(value);
if (key.endsWith("l")) return Long.parseLong(value);
if (key.endsWith("f")) return Float.parseFloat(value);
return value;
}

Expand All @@ -61,22 +63,24 @@ private String toStorageKey (String key, Object value) {

@Override
public void flush () {
try {
// remove all old values
for (int i = 0; i < GwtFiles.LocalStorage.getLength(); i++) {
String key = GwtFiles.LocalStorage.key(i);
if (key.startsWith(prefix)) GwtFiles.LocalStorage.removeItem(key);
}
if (GwtFiles.LocalStorage != null) {
try {
// remove all old values
for (int i = 0; i < GwtFiles.LocalStorage.getLength(); i++) {
String key = GwtFiles.LocalStorage.key(i);
if (key.startsWith(prefix)) GwtFiles.LocalStorage.removeItem(key);
}

// push new values to LocalStorage
for (String key : values.keys()) {
String storageKey = toStorageKey(key, values.get(key));
String storageValue = "" + values.get(key).toString();
GwtFiles.LocalStorage.setItem(storageKey, storageValue);
}
// push new values to LocalStorage
for (String key : values.keys()) {
String storageKey = toStorageKey(key, values.get(key));
String storageValue = "" + values.get(key).toString();
GwtFiles.LocalStorage.setItem(storageKey, storageValue);
}

} catch (Exception e) {
throw new GdxRuntimeException("Couldn't flush preferences");
} catch (Exception e) {
throw new GdxRuntimeException("Couldn't flush preferences", e);
}
}
}

Expand Down
Expand Up @@ -85,6 +85,7 @@
import com.badlogic.gdx.tests.ParticleEmitterTest;
import com.badlogic.gdx.tests.PixelsPerInchTest;
import com.badlogic.gdx.tests.PixmapPackerTest;
import com.badlogic.gdx.tests.PreferencesTest;
import com.badlogic.gdx.tests.ProjectiveTextureTest;
import com.badlogic.gdx.tests.ReflectionCorrectnessTest;
import com.badlogic.gdx.tests.ReflectionTest;
Expand Down Expand Up @@ -722,6 +723,10 @@ public GdxTest instance () {
},
// new Instancer() {public GdxTest instance(){return new PixmapBlendingTest();}}, // FIXME no idea why this doesn't work
new Instancer() {
public GdxTest instance () {
return new PreferencesTest();
}
}, new Instancer() {
public GdxTest instance () {
return new ProjectiveTextureTest();
}
Expand Down

0 comments on commit c0c1f51

Please sign in to comment.