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
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ static void install(
if (markerMatches(existing, expectedMarker)) {
if (Files.isRegularFile(remixSdkDir.resolve("d3d9.dll"))) {
cleanupObsoleteRuntimeTextures(gameDir, reporter);
if (restoreMissingRtxConf(nativeRoot, gameDir)) {
if (restoreMissingRtxConf(nativeRoot, gameDir, Files::newInputStream, Thread::sleep, reporter)) {
reporter.info("Restored mod-owned " + RTX_CONF
+ " after it was deleted since the last extraction");
}
extractTextures(nativeRoot, gameDir, true, fileCount, totalBytes);
extractTextures(nativeRoot, gameDir, true, Files::newInputStream, Thread::sleep, reporter,
fileCount, totalBytes);
extractAuthorPayload(nativeRoot, gameDir, history, reporter,
fileCount, totalBytes, keptCount);
reporter.info("Native runtime " + expectedMarker + " already extracted; restored or updated "
Expand Down Expand Up @@ -243,18 +244,19 @@ static void install(
}
}
}
extractTextures(nativeRoot, gameDir, false, fileCount, totalBytes);
extractTextures(nativeRoot, gameDir, false, Files::newInputStream, Thread::sleep, reporter,
fileCount, totalBytes);
extractAuthorPayload(nativeRoot, gameDir, history, reporter, fileCount, totalBytes, keptCount);

// Commit: swap staging into place. If either move fails we abort
// without writing the marker, so next run re-extracts.
retireAndSwap(remixSdkDir, sdkStaging, retiredSdkCleanup, reporter);

if (Files.exists(rtxConfStaging)) {
// This single-file replacement is the closest to atomic we get
// on Windows (ReplaceFileW underneath).
// One replacing rename, so the live conf is never absent between
// a delete and a rename.
retrying("Moving " + rtxConfStaging + " to " + rtxConfFile,
() -> Files.move(rtxConfStaging, rtxConfFile, StandardCopyOption.REPLACE_EXISTING),
() -> moveReplacing(rtxConfStaging, rtxConfFile),
Thread::sleep, RENAME_RETRY_DELAYS_MS, reporter);
}

Expand Down Expand Up @@ -292,8 +294,9 @@ private static void extractAuthorPayload(
* written; without it every shipped texture replaces its live copy. Other
* files in the folder are never touched.
*/
private static void extractTextures(Path nativeRoot, Path gameDir, boolean onlyMissing,
AtomicInteger fileCount, AtomicLong totalBytes) throws IOException {
static void extractTextures(Path nativeRoot, Path gameDir, boolean onlyMissing,
FileOpener opener, Sleeper sleeper, Reporter reporter,
AtomicInteger fileCount, AtomicLong totalBytes) throws IOException {
Path bundled = nativeRoot.resolve(TEXTURES_DIR);
if (!Files.isDirectory(bundled)) {
return;
Expand All @@ -309,9 +312,7 @@ private static void extractTextures(Path nativeRoot, Path gameDir, boolean onlyM
// Through a staging file, so an interrupted copy never leaves a
// truncated texture that a matching marker would then keep.
Files.createDirectories(texturesDir);
Path staging = texturesDir.resolve(name + ".staging");
Files.copy(texture, staging, StandardCopyOption.REPLACE_EXISTING);
Files.move(staging, live, StandardCopyOption.REPLACE_EXISTING);
writeThroughStaging(texture, live, opener, sleeper, reporter);
fileCount.incrementAndGet();
totalBytes.addAndGet(Files.size(live));
}
Expand Down Expand Up @@ -346,14 +347,20 @@ private static void cleanupObsoleteRuntimeTextures(Path gameDir, Reporter report
* {@code rtx.conf} carries no intent: it is mod-owned, so the only way to
* change it is to edit it, and deleting it asks for the shipped default
* back. Restoring is therefore safe, and an existing file is never touched.
* That is also why the copy goes through a staging file: a truncated conf
* exists, so no later launch would restore it.
*
* @param nativeRoot the JAR's {@code native/} root, which need not exist
* @param gameDir the Minecraft game directory
* @param opener opens the bundled conf
* @param sleeper waits between attempts to move the copy into place
* @param reporter receives the retry warnings
* @return true when a file was written, false when the live conf was
* already present or the JAR carries no bundled conf
* @throws IOException if the copy fails
* @throws IOException if the copy fails, leaving no conf behind
*/
static boolean restoreMissingRtxConf(Path nativeRoot, Path gameDir) throws IOException {
static boolean restoreMissingRtxConf(
Path nativeRoot, Path gameDir, FileOpener opener, Sleeper sleeper, Reporter reporter) throws IOException {
Path liveConf = gameDir.resolve(RTX_CONF);
if (Files.exists(liveConf)) {
return false;
Expand All @@ -362,7 +369,7 @@ static boolean restoreMissingRtxConf(Path nativeRoot, Path gameDir) throws IOExc
if (!Files.isRegularFile(bundledConf)) {
return false;
}
Files.copy(bundledConf, liveConf);
writeThroughStaging(bundledConf, liveConf, opener, sleeper, reporter);
return true;
}

Expand Down Expand Up @@ -433,11 +440,12 @@ interface FileOpener {
/**
* Writes the bundled {@code file} to {@code target} through a staging file
* beside it, so the live path keeps its previous content until the whole
* copy exists. A partial author-owned file matches no shipped version, so
* each later launch would keep it as an edit and the update could never
* finish. The copy is flushed to disk before the move for the same reason:
* after a power loss the move can otherwise be on disk without the bytes
* it moved. A failed write removes the staging file and nothing else.
* copy exists. A partial file would be kept: an author-owned one matches
* no shipped version, so each later launch would take it for an edit, and
* a mod-owned one is restored only while it is missing. The copy is flushed
* to disk before the move for the same reason: after a power loss the move
* can otherwise be on disk without the bytes it moved. A failed write
* removes the staging file and nothing else.
*/
static void writeThroughStaging(
Path file, Path target, FileOpener opener, Sleeper sleeper, Reporter reporter) throws IOException {
Expand All @@ -457,7 +465,7 @@ static void writeThroughStaging(
sleeper, RENAME_RETRY_DELAYS_MS, reporter);
} catch (IOException e) {
try {
// One entry, never a tree: a folder holding this name is the author's.
// One entry, never a tree: a folder holding this name is not ours.
Files.deleteIfExists(staging);
} catch (IOException cleanupFailure) {
e.addSuppressed(cleanupFailure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,56 @@ void matchingMarkerRestoresADeletedTextureWithoutReplacingTheSdk(@TempDir Path t
assertEquals("existing runtime", Files.readString(gameDir.resolve("remix-sdk/d3d9.dll")));
}

@Test
void anInterruptedTextureWriteLeavesTheOldTextureAndNoStagingFile(@TempDir Path tmp) throws IOException {
// The textures folder is shared with other files, so nothing else
// would ever clear a partial staging copy left beside the texture.
Path nativeRoot = createNativePayload(tmp);
Path gameDir = Files.createDirectories(tmp.resolve("game"));
Path textures = Files.createDirectories(gameDir.resolve("textures"));
Files.writeString(textures.resolve("overcast_thunderstorm_bc6u.dds"), "previous release's overcast");
AtomicInteger files = new AtomicInteger();
AtomicLong bytes = new AtomicLong();

IOException failure = assertThrows(IOException.class, () -> DirectoryRuntimePayload.extractTextures(
nativeRoot, gameDir, false, DirectoryRuntimePayloadTest::openFailingPartway, millis -> { },
DirectoryRuntimePayload.Reporter.NONE, files, bytes));

assertEquals(READ_FAILURE, failure.getMessage());
assertEquals("previous release's overcast",
Files.readString(textures.resolve("overcast_thunderstorm_bc6u.dds")));
assertEquals(List.of("overcast_thunderstorm_bc6u.dds"), fileNames(textures),
"the staging file must not outlive the failure");
assertEquals(0, files.get());
}

@Test
void aTextureMoveBlockedByAnotherProgramSucceedsOnceItLetsGo(@TempDir Path tmp) throws IOException {
// A scanner can still have the live texture open when its replacement
// is ready; the move waits it out as the SDK rename does.
Path nativeRoot = createNativePayload(tmp);
Path gameDir = Files.createDirectories(tmp.resolve("game"));
Path overcast = Files.createDirectories(gameDir.resolve("textures"))
.resolve("overcast_thunderstorm_bc6u.dds");
Files.writeString(overcast, "previous release's overcast");
List<Long> waits = new ArrayList<>();

// java.io opens a file without delete sharing, so Windows refuses to
// replace it until the stream closes, here during the second wait.
try (InputStream holder = new FileInputStream(overcast.toFile())) {
DirectoryRuntimePayload.extractTextures(nativeRoot, gameDir, false, Files::newInputStream, millis -> {
waits.add(millis);
if (waits.size() == 2) {
closeUnchecked(holder);
}
}, DirectoryRuntimePayload.Reporter.NONE, new AtomicInteger(), new AtomicLong());
}

assertEquals(2, waits.size());
assertEquals("shipped overcast", Files.readString(overcast));
assertEquals(List.of("overcast_thunderstorm_bc6u.dds"), fileNames(overcast.getParent()));
}

@Test
void aVersionChangeRemovesAnObsoleteRuntimeTextureOnly(@TempDir Path tmp) throws IOException {
Path nativeRoot = createNativePayload(tmp);
Expand Down Expand Up @@ -606,7 +656,7 @@ void restoreMissingRtxConfWritesTheShippedConfWhenTheLiveCopyWasDeleted(@TempDir
Path gameDir = Files.createDirectories(tmp.resolve("game"));
Files.writeString(nativeRoot.resolve("rtx.conf"), "rtx.showUICursor = false");

assertTrue(DirectoryRuntimePayload.restoreMissingRtxConf(nativeRoot, gameDir));
assertTrue(restoreMissingRtxConf(nativeRoot, gameDir));
assertEquals("rtx.showUICursor = false", Files.readString(gameDir.resolve("rtx.conf")));
}

Expand All @@ -619,7 +669,7 @@ void restoreMissingRtxConfLeavesAnExistingConfAlone(@TempDir Path tmp) throws IO
Files.writeString(nativeRoot.resolve("rtx.conf"), "shipped");
Files.writeString(gameDir.resolve("rtx.conf"), "hand tuned");

assertFalse(DirectoryRuntimePayload.restoreMissingRtxConf(nativeRoot, gameDir));
assertFalse(restoreMissingRtxConf(nativeRoot, gameDir));
assertEquals("hand tuned", Files.readString(gameDir.resolve("rtx.conf")));
}

Expand All @@ -628,7 +678,40 @@ void restoreMissingRtxConfIsANoOpWhenTheJarCarriesNoNativePayload(@TempDir Path
// Development runs load classes without a bundled native/ root.
Path gameDir = Files.createDirectories(tmp.resolve("game"));

assertFalse(DirectoryRuntimePayload.restoreMissingRtxConf(tmp.resolve("absent-native"), gameDir));
assertFalse(restoreMissingRtxConf(tmp.resolve("absent-native"), gameDir));
assertFalse(Files.exists(gameDir.resolve("rtx.conf")));
}

@Test
void anInterruptedRtxConfRestoreLeavesNoConfForLaterLaunchesToKeep(@TempDir Path tmp) throws IOException {
// Restoring happens only while rtx.conf is missing, so a truncated
// one would stay until the next version change replaced it.
Path nativeRoot = Files.createDirectories(tmp.resolve("native"));
Path gameDir = Files.createDirectories(tmp.resolve("game"));
Files.writeString(nativeRoot.resolve("rtx.conf"), "rtx.showUICursor = false");

IOException failure = assertThrows(IOException.class, () -> DirectoryRuntimePayload.restoreMissingRtxConf(
nativeRoot, gameDir, DirectoryRuntimePayloadTest::openFailingPartway, millis -> { },
DirectoryRuntimePayload.Reporter.NONE));

assertEquals(READ_FAILURE, failure.getMessage());
assertEquals(List.of(), fileNames(gameDir));

assertTrue(restoreMissingRtxConf(nativeRoot, gameDir));
assertEquals("rtx.showUICursor = false", Files.readString(gameDir.resolve("rtx.conf")));
assertEquals(List.of("rtx.conf"), fileNames(gameDir));
}

private static boolean restoreMissingRtxConf(Path nativeRoot, Path gameDir) throws IOException {
return DirectoryRuntimePayload.restoreMissingRtxConf(
nativeRoot, gameDir, Files::newInputStream, millis -> { }, DirectoryRuntimePayload.Reporter.NONE);
}

private static void closeUnchecked(InputStream stream) {
try {
stream.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}