Skip to content

Commit 3d015cf

Browse files
authored
fix(gestalt): Use AccessController::doPrivileged when saving to allow module-initiated saves (#681)
1 parent 0f6926b commit 3d015cf

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

engine/src/main/java/org/destinationsol/game/SaveManager.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import java.io.IOException;
4848
import java.io.PrintWriter;
4949
import java.io.UnsupportedEncodingException;
50+
import java.security.AccessController;
51+
import java.security.PrivilegedAction;
5052
import java.util.ArrayList;
5153
import java.util.List;
5254
import java.util.Optional;
@@ -61,16 +63,19 @@ protected SaveManager() { }
6163
public static void writeShips(HullConfig hull, float money, List<SolItem> itemsList, Hero hero, HullConfigManager hullConfigManager) {
6264
String hullName = hullConfigManager.getName(hull);
6365

64-
writeMercs(hero, hullConfigManager);
66+
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
67+
writeMercs(hero, hullConfigManager);
6568

66-
String items = itemsToString(itemsList);
69+
String items = itemsToString(itemsList);
6770

68-
Vector2 pos = hero.getPosition();
71+
Vector2 pos = hero.getPosition();
6972

70-
String waypoints = waypointsToString(hero.getWaypoints());
73+
String waypoints = waypointsToString(hero.getWaypoints());
7174

72-
IniReader.write(Const.SAVE_FILE_NAME, "hull", hullName, "money", (int) money, "items", items,
73-
"x", pos.x, "y", pos.y, "waypoints", waypoints, "version", Const.VERSION);
75+
IniReader.write(Const.SAVE_FILE_NAME, "hull", hullName, "money", (int) money, "items", items,
76+
"x", pos.x, "y", pos.y, "waypoints", waypoints, "version", Const.VERSION);
77+
return null;
78+
});
7479
}
7580

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

264-
try (PrintWriter writer = new PrintWriter(fileName, "UTF-8")) {
265-
writer.write(stringToWrite);
266-
logger.debug("Successfully saved the world file");
267-
} catch (FileNotFoundException | UnsupportedEncodingException e) {
268-
logger.error("Could not save world file", e);
269-
}
269+
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
270+
try (PrintWriter writer = new PrintWriter(fileName, "UTF-8")) {
271+
writer.write(stringToWrite);
272+
logger.debug("Successfully saved the world file");
273+
} catch (FileNotFoundException | UnsupportedEncodingException e) {
274+
logger.error("Could not save world file", e);
275+
}
276+
277+
return null;
278+
});
270279
}
271280

272281
/**

0 commit comments

Comments
 (0)