Skip to content

Commit 3c826cb

Browse files
committed
feat(launcher): improve instance management
1 parent ee1fe0c commit 3c826cb

File tree

21 files changed

+119
-35
lines changed

21 files changed

+119
-35
lines changed

src/components/home/instance/CurrentInstance.vue

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,67 @@
11
<script setup lang="ts">
2-
import { computed, inject } from "vue";
2+
import { computed, inject, ref } from "vue";
33
44
import Image from "@/components/general/base/Image.vue";
55
import MaterialRipple from "@/components/general/base/MaterialRipple.vue";
6-
import { InstanceStatesContextKey } from "@/constants/application.ts";
6+
import { GlobalStatesContextKey, InstanceStatesContextKey } from "@/constants/application.ts";
7+
import Errors from "@/lib/errors";
78
import Instances from "@/lib/instances";
8-
import type {
9-
InstanceStatesType,
10-
InstanceStateType,
11-
} from "@/types/application/instance-states.type.ts";
9+
import { log } from "@/lib/logging/scopes/log.ts";
10+
import type { ContextGlobalStatesType } from "@/types/application/global-states.type.ts";
11+
import type { InstanceStatesType } from "@/types/application/instance-states.type.ts";
12+
import type { CurrentInstanceType } from "@/types/launcher/current-instance.type.ts";
1213
14+
const globalStates = inject<ContextGlobalStatesType>(GlobalStatesContextKey);
1315
const instanceStates = inject<InstanceStatesType>(InstanceStatesContextKey);
1416
15-
const currentInstance = computed((): InstanceStateType | undefined => (
16-
Instances.findCurrent(instanceStates)
17+
const currentInstance = computed((): CurrentInstanceType => (
18+
Instances.findCurrent(globalStates?.layout?.currentInstance, instanceStates)
1719
));
20+
21+
const disabled = ref<boolean>(false);
22+
23+
async function __changeName(): Promise<void> {
24+
if (!currentInstance.value) {
25+
return;
26+
}
27+
28+
disabled.value = true;
29+
30+
try {
31+
const id = currentInstance.value.id;
32+
const instance = currentInstance.value.instance;
33+
34+
Instances.change(id, {
35+
...instance,
36+
"name": `Vanilla 1.${Math.floor(Math.random() * 21)}`,
37+
});
38+
39+
if (!instanceStates) {
40+
return;
41+
}
42+
43+
await Instances.syncMetadata(instanceStates);
44+
} catch (error: unknown) {
45+
log.error("Could not change the current instance name:", Errors.prettify(error));
46+
}
47+
48+
disabled.value = false;
49+
}
1850
</script>
1951

2052
<template>
2153
<button
2254
v-if="currentInstance"
55+
@click="__changeName"
56+
:disabled="disabled"
2357
id="__home-page__current-instance-button"
24-
class="relative flex flex-nowrap items-center gap-2 rounded-md p-2"
58+
class="relative flex flex-nowrap items-center gap-2 rounded-md p-2 transition-[opacity] disabled:cursor-default disabled:opacity-50"
2559
>
2660
<Image
2761
id="__home-page__current-instance-logo"
2862
class-names="rounded-md size-12 p-1"
29-
:src="currentInstance.icon"
30-
:alt="`${currentInstance.name}'s icon`"
63+
:src="currentInstance.instance.icon"
64+
:alt="`${currentInstance.instance.name}'s icon`"
3165
/>
3266
<span
3367
id="__home-page__current-instance-information-wrapper"
@@ -37,13 +71,13 @@ const currentInstance = computed((): InstanceStateType | undefined => (
3771
id="__home-page__current-instance-information-title"
3872
class="block font-medium"
3973
>
40-
{{ currentInstance.name }}
74+
{{ currentInstance.instance.name }}
4175
</span>
4276
<span
4377
id="__home-page__current-instance-information-version"
4478
class="block text-neutral-400"
4579
>
46-
{{ currentInstance.version }}
80+
{{ currentInstance.instance.version }}
4781
</span>
4882
</span>
4983
<MaterialRipple />

src/components/home/instance/Launch.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
import { computed, inject, ref } from "vue";
33
44
import MaterialRipple from "@/components/general/base/MaterialRipple.vue";
5-
import { InstanceStatesContextKey } from "@/constants/application.ts";
5+
import { GlobalStatesContextKey, InstanceStatesContextKey } from "@/constants/application.ts";
66
import { LaunchStatus } from "@/constants/launcher.ts";
77
import Instances from "@/lib/instances";
88
import Launcher from "@/lib/launcher";
99
import { log } from "@/lib/logging/scopes/log.ts";
10-
import type {
11-
InstanceStatesType,
12-
InstanceStateType,
13-
} from "@/types/application/instance-states.type.ts";
10+
import type { ContextGlobalStatesType } from "@/types/application/global-states.type.ts";
11+
import type { InstanceStatesType } from "@/types/application/instance-states.type.ts";
12+
import type { CurrentInstanceType } from "@/types/launcher/current-instance.type.ts";
1413
import type { LaunchStatusType } from "@/types/launcher/launch-status.type.ts";
1514
15+
const globalStates = inject<ContextGlobalStatesType>(GlobalStatesContextKey);
1616
const instanceStates = inject<InstanceStatesType>(InstanceStatesContextKey);
1717
18-
const currentInstance = computed((): InstanceStateType | undefined => (
19-
Instances.findCurrent(instanceStates)
18+
const currentInstance = computed((): CurrentInstanceType => (
19+
Instances.findCurrent(globalStates?.layout?.currentInstance, instanceStates)
2020
));
2121
2222
const status = ref<LaunchStatusType>(LaunchStatus.General.Starting);

src/lib/__delete/helpers/parse-rules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { arch, version } from "@tauri-apps/plugin-os";
22

33
import { transformPlatform } from "@/lib/__delete/helpers/transform-platform.ts";
4-
import type { LibraryRuleType } from "@/types/minecraft/minecraft.type.ts";
4+
import type { LibraryRuleType } from "@/types/__delete-minecraft/minecraft.type.ts";
55

66
export async function evaluateRules(rules: LibraryRuleType[]): Promise<boolean> {
77
const platform = transformPlatform();

src/lib/__delete/launcher/core/download.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import type {
2424
LoggingConfigType,
2525
VersionManifestType,
2626
VersionMetaModernType,
27-
} from "@/types/minecraft/minecraft.type.ts";
27+
} from "@/types/__delete-minecraft/minecraft.type.ts";
2828

2929
export async function fetchVersionManifest(): Promise<VersionManifestType> {
3030
const raw = await fetch("https://piston-meta.mojang.com/mc/game/version_manifest.json");

src/lib/configs/scopes/get-config-file.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const defaultConfig: ConfigType = {
99
"allowUnrestrictedUntrusted": true,
1010
},
1111
"layout": {
12+
"currentInstance" : null,
1213
"enableMaterialYouRipple": true,
1314
"custom" : false,
1415
"background" : {

src/lib/configs/scopes/get-default-config.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ test(testName, async () => {
1414
"allowUnrestrictedUntrusted": true,
1515
},
1616
"layout": {
17+
"currentInstance" : null,
1718
"enableMaterialYouRipple": true,
1819
"custom" : false,
1920
"background" : {

src/lib/configs/scopes/get-default-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export async function getDefaultConfig(): Promise<ConfigType> {
2525
"allowUnrestrictedUntrusted": true,
2626
},
2727
"layout": {
28+
"currentInstance" : null,
2829
"enableMaterialYouRipple": true,
2930
"custom" : false,
3031
"background" : {

src/lib/global-state-helpers/scopes/get-default-global-states.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export function getDefaultGlobalStates(): GlobalStatesType {
1010
return {
1111
"translations": EnglishTranslations,
1212
"layout" : {
13+
"currentInstance" : null,
1314
"enableMaterialYouRipple": true,
1415
"custom" : false,
1516
"background" : {

src/lib/instances/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ApplicationNamespace } from "@/constants/application.ts";
22
import { findCurrent } from "@/lib/instances/scopes/find-current.ts";
33
import { getConfigInstanceStates } from "@/lib/instances/scopes/get-config-instance-states.ts";
44
import { readStoredInstances } from "@/lib/instances/scopes/read-stored-instances.ts";
5+
import { saveInstanceStatesToFile } from "@/lib/instances/scopes/save-instance-states-to-file.ts";
56
import type { InstanceStatesType } from "@/types/application/instance-states.type.ts";
67

78
export default {
@@ -12,5 +13,6 @@ export default {
1213
): void => window[ApplicationNamespace].__internals.changeInstanceStates(key, value),
1314
"getFromConfig": getConfigInstanceStates,
1415
"readStored" : readStoredInstances,
16+
"syncMetadata" : saveInstanceStatesToFile,
1517
findCurrent,
1618
} as const;

src/lib/instances/scopes/find-current.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ import type {
22
InstanceStatesType,
33
InstanceStateType,
44
} from "@/types/application/instance-states.type.ts";
5+
import type { CurrentInstanceType } from "@/types/launcher/current-instance.type.ts";
56

67
export function findCurrent(
8+
id: string | null | undefined,
79
instances: InstanceStatesType | undefined,
8-
): InstanceStateType | undefined {
9-
const keys = Object.keys(instances ?? {});
10+
): CurrentInstanceType {
11+
if (!id) {
12+
return;
13+
}
14+
15+
const instance: InstanceStateType | undefined = instances?.[id];
1016

11-
if (keys.length === 0) {
17+
if (!instance) {
1218
return undefined;
1319
}
1420

15-
return instances?.[keys?.[0]];
21+
return { instance, id };
1622
}

0 commit comments

Comments
 (0)