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 package io.gitlab.jfronny.inceptum.gtk.window.create
import io.gitlab.jfronny.commons.StringFormatter 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.InceptumConfig
import io.gitlab.jfronny.inceptum.common.MetaHolder
import io.gitlab.jfronny.inceptum.gtk.control.KDropDown import io.gitlab.jfronny.inceptum.gtk.control.KDropDown
import io.gitlab.jfronny.inceptum.gtk.control.KEntry import io.gitlab.jfronny.inceptum.gtk.control.KEntry
import io.gitlab.jfronny.inceptum.gtk.control.assistant.KAssistant 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 io.gitlab.jfronny.inceptum.launcher.system.setup.Steps
import org.gnome.glib.GLib import org.gnome.glib.GLib
import org.gnome.gtk.* import org.gnome.gtk.*
import java.io.IOException
class NewInstanceWindow(app: Application) : KAssistant(app) { class NewInstanceWindow(app: Application) : KAssistant(app) {
@ -143,21 +140,19 @@ class NewInstanceWindow(app: Application) : KAssistant(app) {
try { try {
for (step in Steps.STEPS) { for (step in Steps.STEPS) {
if (state.isCancelled) { if (state.isCancelled) {
try { state.tryRemoveInstance()
JFiles.deleteRecursive(MetaHolder.INSTANCE_DIR.resolve(state.name))
} catch (e: IOException) {
Log.error("Could not delete instance dir", e)
}
return@Thread return@Thread
} }
pState.incrementStep(step.name) pState.incrementStep(step.name)
step.execute(state) step.execute(state)
} }
state.clearSetupLock()
} catch (e: Throwable) { } catch (e: Throwable) {
pState.cancel() pState.cancel()
Log.error("Could not create instance") Log.error("Could not create instance")
failureMessage = StringFormatter.toString(e) failureMessage = StringFormatter.toString(e)
isFailure = true isFailure = true
state.tryRemoveInstance()
} finally { } finally {
finished = true finished = true
schedule { setComplete(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)) val newPath = MetaHolder.INSTANCE_DIR.resolve(InstanceNameTool.getNextValid(entry.text))
Files.move(instance.path, newPath) Files.move(instance.path, newPath)
window.close() window.close()
InstanceSettingsWindow(window.application, InstanceList.read(newPath)).show() InstanceSettingsWindow(window.application, InstanceList.read(newPath)).visible = true
} catch (e: IOException) { } catch (e: IOException) {
showError("Could not rename", e) 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.commons.io.JFiles;
import io.gitlab.jfronny.inceptum.common.MetaHolder; 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.GuiMain;
import io.gitlab.jfronny.inceptum.imgui.window.dialog.ProcessStateWatcherWindow; import io.gitlab.jfronny.inceptum.imgui.window.dialog.ProcessStateWatcherWindow;
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv; 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, () -> { GuiMain.open(new ProcessStateWatcherWindow("Creating Instance", "Could not create instance", pState, () -> {
for (Step step : Steps.STEPS) { for (Step step : Steps.STEPS) {
if (state.isCancelled()) { if (state.isCancelled()) {
try { state.tryRemoveInstance();
JFiles.deleteRecursive(MetaHolder.INSTANCE_DIR.resolve(state.name()));
} catch (IOException e) {
Utils.LOGGER.error("Could not delete instance dir", e);
}
return; return;
} }
pState.incrementStep(step.getName()); pState.incrementStep(step.getName());
step.execute(state); step.execute(state);
} }
state.clearSetupLock();
LauncherEnv.showInfo("The instance was successfully created. You can now launch it using the main menu", "Successfully installed"); 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; package io.gitlab.jfronny.inceptum.imgui.window.dialog;
import imgui.ImGui; import imgui.ImGui;
import io.gitlab.jfronny.commons.ref.R;
import io.gitlab.jfronny.commons.throwable.ThrowingRunnable; import io.gitlab.jfronny.commons.throwable.ThrowingRunnable;
import io.gitlab.jfronny.inceptum.imgui.window.Window; import io.gitlab.jfronny.inceptum.imgui.window.Window;
import io.gitlab.jfronny.inceptum.launcher.LauncherEnv; import io.gitlab.jfronny.inceptum.launcher.LauncherEnv;
import io.gitlab.jfronny.inceptum.launcher.util.ProcessState; import io.gitlab.jfronny.inceptum.launcher.util.ProcessState;
import java.util.function.Consumer;
public class ProcessStateWatcherWindow extends Window { public class ProcessStateWatcherWindow extends Window {
private final ProcessState state; private final ProcessState state;
private boolean finished; private boolean finished;
public ProcessStateWatcherWindow(String title, String errorMessage, ProcessState state, ThrowingRunnable<?> executor) { 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); super(title);
this.state = state; this.state = state;
new Thread(() -> { new Thread(() -> {
@ -18,6 +24,7 @@ public class ProcessStateWatcherWindow extends Window {
executor.run(); executor.run();
} catch (Throwable e) { } catch (Throwable e) {
state.cancel(); state.cancel();
onFail.accept(e);
LauncherEnv.showError(errorMessage, e); LauncherEnv.showError(errorMessage, e);
} finally { } finally {
finished = true; finished = true;

View File

@ -1,9 +1,16 @@
package io.gitlab.jfronny.inceptum.launcher.system.setup; 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.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.system.instance.LoaderInfo;
import io.gitlab.jfronny.inceptum.launcher.util.ProcessState; import io.gitlab.jfronny.inceptum.launcher.util.ProcessState;
import java.io.IOException;
import java.nio.file.Files;
public record SetupStepInfo(VersionInfo version, public record SetupStepInfo(VersionInfo version,
LoaderInfo loader, LoaderInfo loader,
String name, String name,
@ -15,4 +22,23 @@ public record SetupStepInfo(VersionInfo version,
public boolean isCancelled() { public boolean isCancelled() {
return currentState.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 class Steps {
public static Set<Step> STEPS = new LinkedHashSet<>(List.of( public static Set<Step> STEPS = new LinkedHashSet<>(List.of(
new SetupDirsStep(), new SetupDirsStep(),
new WriteMetadataStep(),
new DownloadJavaStep(), new DownloadJavaStep(),
new DownloadClientStep(), new DownloadClientStep(),
new DownloadLibrariesStep(), new DownloadLibrariesStep(),
new DownloadAssetsStep(), new DownloadAssetsStep(),
new WriteMetadataStep(),
new RunMdsStep() new RunMdsStep()
)); ));
@ -29,24 +29,28 @@ public class Steps {
public static void reDownload(Instance instance, ProcessState state) throws IOException { public static void reDownload(Instance instance, ProcessState state) throws IOException {
if (instance.isLocked()) return; if (instance.isLocked()) return;
boolean found = false; try {
for (VersionsListInfo version : McApi.getVersions().versions()) { boolean found = false;
if (version.id.equals(instance.getGameVersion())) { for (VersionsListInfo version : McApi.getVersions().versions()) {
found = true; if (version.id.equals(instance.getGameVersion())) {
VersionInfo vi = McApi.getVersionInfo(version); found = true;
if (instance.isFabric()) VersionInfo vi = McApi.getVersionInfo(version);
vi = FabricMetaApi.addFabric(vi, instance.getLoaderVersion(), FabricMetaApi.FabricVersionInfoType.Both); if (instance.isFabric())
LoaderInfo li = instance.isFabric() vi = FabricMetaApi.addFabric(vi, instance.getLoaderVersion(), FabricMetaApi.FabricVersionInfoType.Both);
? new LoaderInfo(LoaderInfo.Type.Fabric, instance.getLoaderVersion()) LoaderInfo li = instance.isFabric()
: LoaderInfo.NONE; ? new LoaderInfo(LoaderInfo.Type.Fabric, instance.getLoaderVersion())
SetupStepInfo info = new SetupStepInfo(vi, li, instance.getName(), state); : LoaderInfo.NONE;
for (Step step : Steps.STEPS) { SetupStepInfo info = new SetupStepInfo(vi, li, instance.getName(), state);
state.incrementStep(step.getName()); for (Step step : Steps.STEPS) {
step.execute(info); state.incrementStep(step.getName());
if (state.isCancelled()) return; 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; meta.gameVersion = info.version().id;
GC_InstanceMeta.write(meta, metaPath); GC_InstanceMeta.write(meta, metaPath);
} }
Instance.setSetupLock(instance, false);
if (!Files.exists(instance.resolve(".gitignore"))) { if (!Files.exists(instance.resolve(".gitignore"))) {
Files.writeString(instance.resolve(".gitignore"), """ Files.writeString(instance.resolve(".gitignore"), """
realms_persistence.json realms_persistence.json