Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,8 @@ Notes; finished-work narrative does not.
nested aliases. Investigate changes between independent roots before promising stable identity there. New
snapshots also require a restart when compile-time evaluation observes integer object/handle identities;
integer fields and keys cannot be relocated safely onto the live allocator.
- The command-line session has only `start` and `send`, so `grill run --hot` replaces the running map's session
before it launches, and a failed launch has already deleted its payloads. Add staged activation/restoration in
the owning CLI integration, matching the language server's launch lifecycle. The first reload after a command-line
start checks the whole model again (about 47 s on Castle Fight). `-luaHotReload` in `wurst_run.args` is combined
- The first reload after a command-line start checks the whole model again (about 47 s on Castle Fight).
`-luaHotReload` in `wurst_run.args` is combined
with the build-only `+` lines of a release build and refused. The initializer-order check lists every key
(about 370 KB with 23,000 keys on a large map), and generated object-data functions ship whole. Measure smaller
representations without discarding the order check or splitting effectful initialization incorrectly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,37 @@ private static final class State {
* of an earlier session are deleted;</li>
* <li>{@code send <build dir> <CustomMapData>}: writes the next payload, which takes the running map to the program
* of the last build, where the map polls for it;</li>
* <li>{@code prepare <build dir> <CustomMapData>}: stages the last build and sets old payloads aside before launch;</li>
* <li>{@code activate <build dir> <CustomMapData>}: commits a prepared launch and discards the old payloads;</li>
* <li>{@code abort <build dir> <CustomMapData>}: drops a prepared launch and restores the old payloads;</li>
* <li>{@code <baseline manifest.json> <current manifest.json> <output .pld>}: the first payload a map running the
* first manifest would get for the second, written as a preload file.</li>
* </ul>
* A payload the edit cannot carry exits with 1 and prints the reasons.
*/
public static void main(String[] args) throws IOException {
if (args.length == 3 && (args[0].equals("start") || args[0].equals("send"))) {
if (args.length == 3 && Set.of("start", "send", "prepare", "activate", "abort").contains(args[0])) {
Path buildDir = Path.of(args[1]);
Path customMapData = Path.of(args[2]);
if (args[0].equals("activate")) {
completeLaunch(buildDir, customMapData);
System.out.println("session activated");
return;
}
if (args[0].equals("abort")) {
abortLaunch(buildDir, customMapData);
System.out.println("launch aborted; previous session restored");
return;
}
LuaHotReload.Manifest manifest = readManifest(buildDir.resolve(SESSION_FOLDER).resolve("manifest.json"));
if (args[0].equals("prepare")) {
prepareLaunch(buildDir, customMapData, manifest);
System.out.println("session prepared");
return;
}
if (args[0].equals("start")) {
setAsideDeliveries(customMapData);
discardDeliveries(customMapData);
start(buildDir, manifest);
prepareLaunch(buildDir, customMapData, manifest);
completeLaunch(buildDir, customMapData);
System.out.println("session started");
return;
}
Expand All @@ -113,7 +130,7 @@ public static void main(String[] args) throws IOException {
return;
}
if (args.length != 3) {
System.err.println("usage: start <build dir> <CustomMapData> | send <build dir> <CustomMapData>"
System.err.println("usage: start|send|prepare|activate|abort <build dir> <CustomMapData>"
+ " | <baseline manifest.json> <current manifest.json> <output .pld>");
System.exit(2);
}
Expand Down Expand Up @@ -146,6 +163,44 @@ public static void start(Path buildDir, LuaHotReload.Manifest manifest) throws I
activate(buildDir);
}

/** Preserve the active session while hiding its payloads from a map which is about to launch. */
public static void prepareLaunch(Path buildDir, Path customMapData, LuaHotReload.Manifest manifest) throws IOException {
stage(buildDir, manifest);
try {
setAsideDeliveries(customMapData);
} catch (IOException e) {
try {
unstage(buildDir);
} catch (IOException cleanup) {
e.addSuppressed(cleanup);
}
throw e;
}
}

/** Called only after the launcher succeeds; errors after activation must not restore obsolete payloads. */
public static void completeLaunch(Path buildDir, Path customMapData) throws IOException {
// Persist the handoff before replacing session.json: activate's later cleanup can fail or be interrupted.
Path aside = markDeliveriesSuperseded(customMapData);
activate(buildDir);
deleteFolder(aside);
}

/** Attempt both rollback steps, preserving diagnostics if either fails. Safe when no launch was prepared. */
public static void abortLaunch(Path buildDir, Path customMapData) throws IOException {
try {
restoreDeliveries(customMapData);
} catch (IOException e) {
try {
unstage(buildDir);
} catch (IOException cleanup) {
e.addSuppressed(cleanup);
}
throw e;
}
unstage(buildDir);
}

/**
* Writes the session of a map about to be launched beside the current one, which stays in effect: a launch which
* fails leaves the map that is still running its session. {@link #activate} makes the staged one current, as one
Expand Down Expand Up @@ -280,6 +335,9 @@ public static String warningNote(LuaHotReload.Payload payload) {
*/
public static Sent send(Path buildDir, Path customMapData, LuaHotReload.Manifest current)
throws IOException, LuaHotReload.Rejected {
if (Files.exists(stagedFile(buildDir))) {
throw new LuaHotReload.Rejected(List.of("a hot reload launch is prepared: activate or abort it before sending a payload"));
}
Path file = stateFile(buildDir);
if (!Files.exists(file)) {
throw new LuaHotReload.Rejected(List.of("no map was started with hot run (or -luaHotReload): start the map with hot run first"));
Expand Down Expand Up @@ -457,7 +515,11 @@ public static void setAsideDeliveries(Path customMapData) throws IOException {
}
} catch (IOException e) {
// A file the running map has not read must not stay hidden: put back what was moved.
restoreDeliveries(customMapData);
try {
restoreDeliveries(customMapData);
} catch (IOException cleanup) {
e.addSuppressed(cleanup);
}
throw e;
}
}
Expand All @@ -469,6 +531,11 @@ public static void restoreDeliveries(Path customMapData) throws IOException {
if (!Files.isDirectory(aside)) {
return;
}
if (Files.exists(aside.resolve(SUPERSEDED))) {
// Activation already committed. Cleanup may be retried, but these files must never reach the new map.
deleteFolder(aside);
return;
}
for (Path file : deliveries(aside)) {
Files.move(file, folder.resolve(file.getFileName()), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
}
Expand All @@ -480,12 +547,15 @@ public static void restoreDeliveries(Path customMapData) throws IOException {
* a deletion which is interrupted leaves files the next launch knows to delete rather than restore.
*/
public static void discardDeliveries(Path customMapData) throws IOException {
deleteFolder(markDeliveriesSuperseded(customMapData));
}

private static Path markDeliveriesSuperseded(Path customMapData) throws IOException {
Path aside = deliveryFolder(customMapData).resolve(SET_ASIDE);
if (!Files.isDirectory(aside)) {
return;
if (Files.isDirectory(aside)) {
Files.writeString(aside.resolve(SUPERSEDED), "");
}
Files.writeString(aside.resolve(SUPERSEDED), "");
deleteFolder(aside);
return aside;
}

private static final String SET_ASIDE = "hotreload-set-aside";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,18 @@ private String compileMap(ModelManager modelManager, WurstGui gui, WurstProjectC
// The new map polls from the first file. The session and the files of a map which is still running
// change only once the new map is launched, so a launch which fails leaves that map's session intact.
Path customMapData = getCustomMapDataPath();
LuaHotReloadSession.stage(buildDir.toPath(), luaHotReloadManifest);
LuaHotReloadSession.setAsideDeliveries(customMapData);
LuaHotReloadSession.prepareLaunch(buildDir.toPath(), customMapData, luaHotReloadManifest);
try {
startGame(gui, result);
} catch (Exception e) {
LuaHotReloadSession.restoreDeliveries(customMapData);
LuaHotReloadSession.unstage(buildDir.toPath());
try {
LuaHotReloadSession.abortLaunch(buildDir.toPath(), customMapData);
} catch (IOException cleanup) {
e.addSuppressed(cleanup);
}
throw e;
}
LuaHotReloadSession.activate(buildDir.toPath());
LuaHotReloadSession.discardDeliveries(customMapData);
LuaHotReloadSession.completeLaunch(buildDir.toPath(), customMapData);
}
}
return null;
Expand Down
Loading
Loading