fix: prevent instance not exiting setup stage when redownload cancelled

This commit is contained in:
Johannes Frohnmeyer 2023-10-21 15:50:35 +02:00
parent 9e1c20737d
commit 5cc650921b
Signed by: Johannes
GPG Key ID: E76429612C2929F4
7 changed files with 62 additions and 35 deletions

View File

@ -1,9 +1,7 @@
package io.gitlab.jfronny.inceptum.gtk.window.create
import io.gitlab.jfronny.commons.StringFormatter
import io.gitlab.jfronny.commons.io.JFiles
import io.gitlab.jfronny.inceptum.common.InceptumConfig
import io.gitlab.jfronny.inceptum.common.MetaHolder
import io.gitlab.jfronny.inceptum.gtk.control.KDropDown
import io.gitlab.jfronny.inceptum.gtk.control.KEntry
import io.gitlab.jfronny.inceptum.gtk.control.assistant.KAssistant
@ -22,7 +20,6 @@ import io.gitlab.jfronny.inceptum.launcher.system.setup.SetupStepInfo
import io.gitlab.jfronny.inceptum.launcher.system.setup.Steps
import org.gnome.glib.GLib
import org.gnome.gtk.*
import java.io.IOException
class NewInstanceWindow(app: Application) : KAssistant(app) {
@ -143,21 +140,19 @@ class NewInstanceWindow(app: Application) : KAssistant(app) {
try {
for (step in Steps.STEPS) {
if (state.isCancelled) {
try {
JFiles.deleteRecursive(MetaHolder.INSTANCE_DIR.resolve(state.name))
} catch (e: IOException) {
Log.error("Could not delete instance dir", e)
}
state.tryRemoveInstance()
return@Thread
}
pState.incrementStep(step.name)
step.execute(state)
}
state.clearSetupLock()
} catch (e: Throwable) {
pState.cancel()
Log.error("Could not create instance")
failureMessage = StringFormatter.toString(e)
isFailure = true
state.tryRemoveInstance()
} finally {
finished = true
schedule { setComplete(true) }

View File

@ -40,7 +40,7 @@ class GeneralTab(window: InstanceSettingsWindow) : SectionedSettingsTab<Instance
val newPath = MetaHolder.INSTANCE_DIR.resolve(InstanceNameTool.getNextValid(entry.text))
Files.move(instance.path, newPath)
window.close()
InstanceSettingsWindow(window.application, InstanceList.read(newPath)).show()
InstanceSettingsWindow(window.application, InstanceList.read(newPath)).visible = true
} catch (e: IOException) {
showError("Could not rename", e)
}

View File

@ -2,7 +2,6 @@ package io.gitlab.jfronny.inceptum.imgui.window;
import io.gitlab.jfronny.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.common.MetaHolder;
import io.gitlab.jfronny.inceptum.common.Utils;
import io.gitlab.jfronny.inceptum.imgui.GuiMain;
import io.gitlab.jfronny.inceptum.imgui.window.dialog.ProcessStateWatcherWindow;
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv;
@ -44,17 +43,14 @@ public class GuiUtil {
GuiMain.open(new ProcessStateWatcherWindow("Creating Instance", "Could not create instance", pState, () -> {
for (Step step : Steps.STEPS) {
if (state.isCancelled()) {
try {
JFiles.deleteRecursive(MetaHolder.INSTANCE_DIR.resolve(state.name()));
} catch (IOException e) {
Utils.LOGGER.error("Could not delete instance dir", e);
}
state.tryRemoveInstance();
return;
}
pState.incrementStep(step.getName());
step.execute(state);
}
state.clearSetupLock();
LauncherEnv.showInfo("The instance was successfully created. You can now launch it using the main menu", "Successfully installed");
}));
}, t -> state.tryRemoveInstance()));
}
}

View File

@ -1,16 +1,22 @@
package io.gitlab.jfronny.inceptum.imgui.window.dialog;
import imgui.ImGui;
import io.gitlab.jfronny.commons.ref.R;
import io.gitlab.jfronny.commons.throwable.ThrowingRunnable;
import io.gitlab.jfronny.inceptum.imgui.window.Window;
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv;
import io.gitlab.jfronny.inceptum.launcher.util.ProcessState;
import java.util.function.Consumer;
public class ProcessStateWatcherWindow extends Window {
private final ProcessState state;
private boolean finished;
public ProcessStateWatcherWindow(String title, String errorMessage, ProcessState state, ThrowingRunnable<?> executor) {
this(title, errorMessage, state, executor, R::nop);
}
public ProcessStateWatcherWindow(String title, String errorMessage, ProcessState state, ThrowingRunnable<?> executor, Consumer<Throwable> onFail) {
super(title);
this.state = state;
new Thread(() -> {
@ -18,6 +24,7 @@ public class ProcessStateWatcherWindow extends Window {
executor.run();
} catch (Throwable e) {
state.cancel();
onFail.accept(e);
LauncherEnv.showError(errorMessage, e);
} finally {
finished = true;

View File

@ -1,9 +1,16 @@
package io.gitlab.jfronny.inceptum.launcher.system.setup;
import io.gitlab.jfronny.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.common.MetaHolder;
import io.gitlab.jfronny.inceptum.common.Utils;
import io.gitlab.jfronny.inceptum.launcher.model.mojang.VersionInfo;
import io.gitlab.jfronny.inceptum.launcher.system.instance.Instance;
import io.gitlab.jfronny.inceptum.launcher.system.instance.LoaderInfo;
import io.gitlab.jfronny.inceptum.launcher.util.ProcessState;
import java.io.IOException;
import java.nio.file.Files;
public record SetupStepInfo(VersionInfo version,
LoaderInfo loader,
String name,
@ -15,4 +22,23 @@ public record SetupStepInfo(VersionInfo version,
public boolean isCancelled() {
return currentState.isCancelled();
}
public void tryRemoveInstance() {
try {
removeInstance();
} catch (IOException e) {
Utils.LOGGER.error("Could not delete instance dir", e);
}
}
public void removeInstance() throws IOException {
JFiles.deleteRecursive(MetaHolder.INSTANCE_DIR.resolve(name));
}
public void clearSetupLock() {
try {
Files.deleteIfExists(MetaHolder.INSTANCE_DIR.resolve(name).resolve(Instance.SETUP_LOCK_NAME));
} catch (IOException ignored) {
}
}
}

View File

@ -15,11 +15,11 @@ import java.util.*;
public class Steps {
public static Set<Step> STEPS = new LinkedHashSet<>(List.of(
new SetupDirsStep(),
new WriteMetadataStep(),
new DownloadJavaStep(),
new DownloadClientStep(),
new DownloadLibrariesStep(),
new DownloadAssetsStep(),
new WriteMetadataStep(),
new RunMdsStep()
));
@ -29,24 +29,28 @@ public class Steps {
public static void reDownload(Instance instance, ProcessState state) throws IOException {
if (instance.isLocked()) return;
boolean found = false;
for (VersionsListInfo version : McApi.getVersions().versions()) {
if (version.id.equals(instance.getGameVersion())) {
found = true;
VersionInfo vi = McApi.getVersionInfo(version);
if (instance.isFabric())
vi = FabricMetaApi.addFabric(vi, instance.getLoaderVersion(), FabricMetaApi.FabricVersionInfoType.Both);
LoaderInfo li = instance.isFabric()
? new LoaderInfo(LoaderInfo.Type.Fabric, instance.getLoaderVersion())
: LoaderInfo.NONE;
SetupStepInfo info = new SetupStepInfo(vi, li, instance.getName(), state);
for (Step step : Steps.STEPS) {
state.incrementStep(step.getName());
step.execute(info);
if (state.isCancelled()) return;
try {
boolean found = false;
for (VersionsListInfo version : McApi.getVersions().versions()) {
if (version.id.equals(instance.getGameVersion())) {
found = true;
VersionInfo vi = McApi.getVersionInfo(version);
if (instance.isFabric())
vi = FabricMetaApi.addFabric(vi, instance.getLoaderVersion(), FabricMetaApi.FabricVersionInfoType.Both);
LoaderInfo li = instance.isFabric()
? new LoaderInfo(LoaderInfo.Type.Fabric, instance.getLoaderVersion())
: LoaderInfo.NONE;
SetupStepInfo info = new SetupStepInfo(vi, li, instance.getName(), state);
for (Step step : Steps.STEPS) {
state.incrementStep(step.getName());
step.execute(info);
if (state.isCancelled()) return;
}
}
}
if (!found) throw new IOException("Could not identify minecraft version " + instance.getGameVersion());
} finally {
instance.setSetupLock(false);
}
if (!found) throw new IOException("Could not identify minecraft version " + instance.getGameVersion());
}
}

View File

@ -22,7 +22,6 @@ public class WriteMetadataStep implements Step {
meta.gameVersion = info.version().id;
GC_InstanceMeta.write(meta, metaPath);
}
Instance.setSetupLock(instance, false);
if (!Files.exists(instance.resolve(".gitignore"))) {
Files.writeString(instance.resolve(".gitignore"), """
realms_persistence.json