Skip to content

Commit

Permalink
fix: Fix potential crash when the preferences file is read-only.
Browse files Browse the repository at this point in the history
  • Loading branch information
crykn committed Sep 5, 2024
1 parent 2cf246f commit fa8631c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import java.util.Map;

import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.utils.GdxRuntimeException;

import de.damios.guacamole.Exceptions;
import de.damios.guacamole.gdx.log.Logger;
import de.damios.guacamole.gdx.log.LoggerService;

/**
* Wraps a {@link Preferences} instance to enable automatic flushing after every
Expand All @@ -27,7 +32,9 @@
*/
public class AutoFlushingPreferences implements Preferences {

private Logger LOG = LoggerService.getLogger(AutoFlushingPreferences.class);
private boolean autoFlush = false;
private boolean flushSavely = false;
private Preferences preferences;

public static AutoFlushingPreferences createInstance(
Expand All @@ -47,51 +54,59 @@ public boolean isAutoFlushing() {
return autoFlush;
}

public void setFlushSavely(boolean flushSavely) {
this.flushSavely = flushSavely;
}

public boolean isFlushingSavely() {
return flushSavely;
}

@Override
public Preferences putBoolean(String key, boolean value) {
preferences.putBoolean(key, value);
if (autoFlush)
preferences.flush();
flush();
return this;
}

@Override
public Preferences putInteger(String key, int value) {
preferences.putInteger(key, value);
if (autoFlush)
preferences.flush();
flush();
return this;
}

@Override
public Preferences putLong(String key, long value) {
preferences.putLong(key, value);
if (autoFlush)
preferences.flush();
flush();
return this;
}

@Override
public Preferences putFloat(String key, float value) {
preferences.putFloat(key, value);
if (autoFlush)
preferences.flush();
flush();
return this;
}

@Override
public Preferences putString(String key, String value) {
preferences.putString(key, value);
if (autoFlush)
preferences.flush();
flush();
return this;
}

@Override
public Preferences put(Map<String, ?> vals) {
preferences.put(vals);
if (autoFlush)
preferences.flush();
flush();
return this;
}

Expand Down Expand Up @@ -167,7 +182,16 @@ public void remove(String key) {

@Override
public void flush() {
preferences.flush();
if (flushSavely) {
try {
preferences.flush();
} catch (GdxRuntimeException e) {
LOG.error("Problem while saving preferences: %s",
Exceptions.getStackTraceAsString(e));
}
} else {
preferences.flush();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public EskalonSettings(EskalonApplicationContext appContext) {
this.preferences = AutoFlushingPreferences
.createInstance(Gdx.app.getPreferences(fileName));
this.preferences.setAutoFlushing(true);
this.preferences.setFlushSavely(true);

this.isFirstStartup = this.preferences.getBoolean(FIRST_STARTUP_SETTING,
true);
Expand Down

0 comments on commit fa8631c

Please sign in to comment.