Skip to content

Commit

Permalink
fix(gestalt): Use AccessController::doPrivileged when saving to allow…
Browse files Browse the repository at this point in the history
… module-initiated saves (#681)
  • Loading branch information
BenjaminAmos authored Mar 31, 2023
1 parent 0f6926b commit 3d015cf
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions engine/src/main/java/org/destinationsol/game/SaveManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -61,16 +63,19 @@ protected SaveManager() { }
public static void writeShips(HullConfig hull, float money, List<SolItem> itemsList, Hero hero, HullConfigManager hullConfigManager) {
String hullName = hullConfigManager.getName(hull);

writeMercs(hero, hullConfigManager);
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
writeMercs(hero, hullConfigManager);

String items = itemsToString(itemsList);
String items = itemsToString(itemsList);

Vector2 pos = hero.getPosition();
Vector2 pos = hero.getPosition();

String waypoints = waypointsToString(hero.getWaypoints());
String waypoints = waypointsToString(hero.getWaypoints());

IniReader.write(Const.SAVE_FILE_NAME, "hull", hullName, "money", (int) money, "items", items,
"x", pos.x, "y", pos.y, "waypoints", waypoints, "version", Const.VERSION);
IniReader.write(Const.SAVE_FILE_NAME, "hull", hullName, "money", (int) money, "items", items,
"x", pos.x, "y", pos.y, "waypoints", waypoints, "version", Const.VERSION);
return null;
});
}

private static String waypointsToString(ArrayList<Waypoint> waypoints) {
Expand Down Expand Up @@ -261,12 +266,16 @@ public static void saveWorld(WorldConfig worldConfig) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String stringToWrite = gson.toJson(world);

try (PrintWriter writer = new PrintWriter(fileName, "UTF-8")) {
writer.write(stringToWrite);
logger.debug("Successfully saved the world file");
} catch (FileNotFoundException | UnsupportedEncodingException e) {
logger.error("Could not save world file", e);
}
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
try (PrintWriter writer = new PrintWriter(fileName, "UTF-8")) {
writer.write(stringToWrite);
logger.debug("Successfully saved the world file");
} catch (FileNotFoundException | UnsupportedEncodingException e) {
logger.error("Could not save world file", e);
}

return null;
});
}

/**
Expand Down

0 comments on commit 3d015cf

Please sign in to comment.