diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/create/NewInstanceWindow.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/create/NewInstanceWindow.kt index f2f30a4..534d212 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/create/NewInstanceWindow.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/create/NewInstanceWindow.kt @@ -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) } diff --git a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/GeneralTab.kt b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/GeneralTab.kt index fc1eb4b..77b7825 100644 --- a/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/GeneralTab.kt +++ b/launcher-gtk/src/main/kotlin/io/gitlab/jfronny/inceptum/gtk/window/settings/instance/GeneralTab.kt @@ -40,7 +40,7 @@ class GeneralTab(window: InstanceSettingsWindow) : SectionedSettingsTab { 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())); } } diff --git a/launcher-imgui/src/main/java/io/gitlab/jfronny/inceptum/imgui/window/dialog/ProcessStateWatcherWindow.java b/launcher-imgui/src/main/java/io/gitlab/jfronny/inceptum/imgui/window/dialog/ProcessStateWatcherWindow.java index 652ca24..5aa1649 100644 --- a/launcher-imgui/src/main/java/io/gitlab/jfronny/inceptum/imgui/window/dialog/ProcessStateWatcherWindow.java +++ b/launcher-imgui/src/main/java/io/gitlab/jfronny/inceptum/imgui/window/dialog/ProcessStateWatcherWindow.java @@ -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 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; diff --git a/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/SetupStepInfo.java b/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/SetupStepInfo.java index 07e3f18..7fcb0d7 100644 --- a/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/SetupStepInfo.java +++ b/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/SetupStepInfo.java @@ -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) { + } + } } diff --git a/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/Steps.java b/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/Steps.java index 2ca6f4b..28a7e44 100644 --- a/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/Steps.java +++ b/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/Steps.java @@ -15,11 +15,11 @@ import java.util.*; public class Steps { public static Set 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()); } } diff --git a/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/steps/WriteMetadataStep.java b/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/steps/WriteMetadataStep.java index 0a6b19d..cd1cc0a 100644 --- a/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/steps/WriteMetadataStep.java +++ b/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/system/setup/steps/WriteMetadataStep.java @@ -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