From 333f0dd88b274c2c4f7b9c3e7071d6d07523e9ba Mon Sep 17 00:00:00 2001 From: 000hen Date: Sat, 11 Jul 2026 00:17:17 +0800 Subject: [PATCH 01/30] feat: shop-source UI, typed script config, mod compat warnings, FTB modpacks Implements the four-track plan. Shop sources (A): manage mod-shop scripts (import/remove/permissions) from the Scripts page, alongside providers/parsers/automation. Typed script config (B): ConfigOption/ScriptConfig proto + GetConfig/SetConfig on all four script services; engine ConfigStore + meta.config validation + ctx.config injection; a Configure dialog on each script card (secrets are write-only). Mod compatibility (C): new engine compat package (loader-family + Fabric/Maven version-range matching) sets ModFile.Compat; builtin parsers emit game versions/range; installed-mod and shop-version compat badges in the app. FTB modpacks (D): ModKind.MODPACK + ProviderCapabilities.hidden/ free_form_versions + Server.loader; an ftb shop (browse) and a hidden ftb provider (installs a pack as a whole server, resolving CurseForge-hosted files via the shared key); a modpack Create-server flow in the Mod Shop. LaunchSpec now carries mc_version/loader so a modpack resolves its concrete version at install. Contract-first: proto edits regenerated on both sides. Verified: engine go build/vet + 274 unit tests; app build (0 warnings) + 5 Core integration tests. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../Controls/ScriptEntryCard.xaml | 16 +- .../Controls/ScriptEntryCard.xaml.cs | 5 + .../Controls/ScriptSectionExpander.xaml | 1 + .../Controls/ScriptSectionExpander.xaml.cs | 4 + .../Controls/Server/ServerModsPanel.xaml.cs | 11 +- app/JustHostMC.App/Models/ParserItem.cs | 2 +- app/JustHostMC.App/Models/ProviderItem.cs | 2 +- app/JustHostMC.App/Models/ScriptEntryItem.cs | 9 +- app/JustHostMC.App/Models/ScriptItem.cs | 2 +- app/JustHostMC.App/Models/ServerItem.cs | 15 + app/JustHostMC.App/Models/ShopCompat.cs | 81 ++++++ app/JustHostMC.App/Models/ShopModels.cs | 52 +++- app/JustHostMC.App/Models/ShopSourceItem.cs | 43 +++ .../Strings/en-US/Resources.resw | 24 ++ .../Strings/zh-TW/Resources.resw | 24 ++ .../ViewModels/ScriptsViewModel.cs | 109 +++++++ .../ViewModels/ShopDetailViewModel.cs | 108 ++++++- .../ViewModels/ShopViewModel.cs | 37 ++- .../Views/ScriptConfigDialog.cs | 143 +++++++++ app/JustHostMC.App/Views/ScriptsPage.xaml | 16 ++ app/JustHostMC.App/Views/ScriptsPage.xaml.cs | 54 ++++ app/JustHostMC.App/Views/ServerDialog.xaml.cs | 3 + app/JustHostMC.App/Views/ServerPage.xaml | 1 + app/JustHostMC.App/Views/ServerPage.xaml.cs | 5 + app/JustHostMC.App/Views/ShopDetailPage.xaml | 29 +- .../Views/ShopDetailPage.xaml.cs | 50 ++++ app/JustHostMC.App/Views/ShopHomePage.xaml | 4 +- app/JustHostMC.App/Views/ShopSearchPage.xaml | 8 +- app/JustHostMC.App/Views/ShopWindow.xaml | 4 +- engine/cmd/engine/main.go | 23 +- engine/internal/grpc/configrpc.go | 92 ++++++ engine/internal/grpc/configrpc_test.go | 86 ++++++ engine/internal/grpc/modservice.go | 15 +- engine/internal/grpc/parserservice.go | 45 ++- engine/internal/grpc/parserservice_test.go | 4 +- engine/internal/grpc/providerservice.go | 51 +++- engine/internal/grpc/scriptservice.go | 54 +++- engine/internal/grpc/scriptservice_test.go | 4 +- engine/internal/grpc/serverservice.go | 20 +- engine/internal/grpc/shopservice.go | 67 ++++- engine/internal/provider/provider.go | 6 + .../internal/scripting/automation/manager.go | 17 ++ engine/internal/scripting/builtin/ftb.lua | 271 ++++++++++++++++++ .../internal/scripting/builtin_shops/ftb.lua | 183 ++++++++++++ .../internal/scripting/builtin_shops_test.go | 139 +++++++++ .../scripting/configinjection_test.go | 146 ++++++++++ engine/internal/scripting/configstore.go | 132 +++++++++ engine/internal/scripting/configstore_test.go | 83 ++++++ engine/internal/scripting/export.go | 4 + engine/internal/scripting/ftbprovider_test.go | 143 +++++++++ engine/internal/scripting/host.go | 23 +- engine/internal/scripting/hostfuncs.go | 11 + engine/internal/scripting/luaprovider.go | 19 +- engine/internal/scripting/luaprovider_test.go | 29 ++ engine/internal/scripting/meta.go | 143 +++++++++ engine/internal/scripting/metaconfig_test.go | 68 +++++ engine/internal/scripting/metakinds_test.go | 68 +++++ engine/internal/scripting/parser.go | 18 +- engine/internal/scripting/parserregistry.go | 12 + engine/internal/scripting/registry.go | 23 ++ engine/internal/scripting/shop.go | 32 ++- engine/internal/scripting/shopregistry.go | 13 + engine/internal/store/sqlite.go | 16 +- engine/internal/store/store.go | 2 + proto/mcmanager/v1/mcmanager.proto | 55 ++++ 65 files changed, 2872 insertions(+), 107 deletions(-) create mode 100644 app/JustHostMC.App/Models/ShopCompat.cs create mode 100644 app/JustHostMC.App/Models/ShopSourceItem.cs create mode 100644 app/JustHostMC.App/Views/ScriptConfigDialog.cs create mode 100644 engine/internal/grpc/configrpc.go create mode 100644 engine/internal/grpc/configrpc_test.go create mode 100644 engine/internal/scripting/builtin/ftb.lua create mode 100644 engine/internal/scripting/builtin_shops/ftb.lua create mode 100644 engine/internal/scripting/configinjection_test.go create mode 100644 engine/internal/scripting/configstore.go create mode 100644 engine/internal/scripting/configstore_test.go create mode 100644 engine/internal/scripting/ftbprovider_test.go create mode 100644 engine/internal/scripting/metaconfig_test.go create mode 100644 engine/internal/scripting/metakinds_test.go diff --git a/app/JustHostMC.App/Controls/ScriptEntryCard.xaml b/app/JustHostMC.App/Controls/ScriptEntryCard.xaml index 503093c..1a879f4 100644 --- a/app/JustHostMC.App/Controls/ScriptEntryCard.xaml +++ b/app/JustHostMC.App/Controls/ScriptEntryCard.xaml @@ -26,6 +26,7 @@ + @@ -66,10 +67,19 @@ + + @@ -100,7 +110,7 @@ (ScriptEntryItem?)GetValue(ItemProperty); set => SetValue(ItemProperty, value); @@ -39,6 +41,9 @@ private void OnRemoveConfirmClick(object sender, RoutedEventArgs e) { RemoveClick?.Invoke(this, e); } + private void OnConfigureClick(object sender, RoutedEventArgs e) => + ConfigureClick?.Invoke(this, e); + private void OnEnabledToggled(object sender, RoutedEventArgs e) => EnabledToggled?.Invoke(this, e); } diff --git a/app/JustHostMC.App/Controls/ScriptSectionExpander.xaml b/app/JustHostMC.App/Controls/ScriptSectionExpander.xaml index 6c37349..6ef7d97 100644 --- a/app/JustHostMC.App/Controls/ScriptSectionExpander.xaml +++ b/app/JustHostMC.App/Controls/ScriptSectionExpander.xaml @@ -71,6 +71,7 @@ diff --git a/app/JustHostMC.App/Controls/ScriptSectionExpander.xaml.cs b/app/JustHostMC.App/Controls/ScriptSectionExpander.xaml.cs index 90e63af..6d19bfa 100644 --- a/app/JustHostMC.App/Controls/ScriptSectionExpander.xaml.cs +++ b/app/JustHostMC.App/Controls/ScriptSectionExpander.xaml.cs @@ -63,6 +63,7 @@ private void UpdateIsEmpty() { public event RoutedEventHandler? AddScriptsClick; public event RoutedEventHandler? ScriptToggled; public event RoutedEventHandler? ScriptRemoved; + public event RoutedEventHandler? ScriptConfigured; public object? HeaderContent { get => GetValue(HeaderContentProperty); @@ -106,6 +107,9 @@ private void OnScriptToggled(object sender, RoutedEventArgs e) => private void OnScriptRemoved(object sender, RoutedEventArgs e) => ScriptRemoved?.Invoke(sender, e); + private void OnScriptConfigured(object sender, RoutedEventArgs e) => + ScriptConfigured?.Invoke(sender, e); + private static DependencyProperty Register(string name, T defaultValue) => DependencyProperty.Register(name, typeof(T), diff --git a/app/JustHostMC.App/Controls/Server/ServerModsPanel.xaml.cs b/app/JustHostMC.App/Controls/Server/ServerModsPanel.xaml.cs index 5e62183..f82d4e7 100644 --- a/app/JustHostMC.App/Controls/Server/ServerModsPanel.xaml.cs +++ b/app/JustHostMC.App/Controls/Server/ServerModsPanel.xaml.cs @@ -32,6 +32,11 @@ public ServerModsPanel() { InitializeComponent(); } + /// Raised when a shop opened from this panel created a brand-new + /// server (modpack install), so the page can refresh the server + /// list. + public event RoutedEventHandler? ServerCreated; + private static void OnViewModelChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { var panel = (ServerModsPanel)d; @@ -75,8 +80,10 @@ private void OnBrowseShopClick(object sender, RoutedEventArgs e) { return; var context = new ShopContext( - Server.Id, Server.McVersion, Server.ProviderId, Mods.Kind, - Mods.InstalledFileNames, () => _ = Mods.RefreshAsync()); + Server.Id, Server.McVersion, Server.EffectiveLoader, Mods.Kind, + Mods.InstalledFileNames, () => _ = Mods.RefreshAsync(), + OnServerCreated: () => + ServerCreated?.Invoke(this, new RoutedEventArgs())); new ShopWindow(context).Activate(); } diff --git a/app/JustHostMC.App/Models/ParserItem.cs b/app/JustHostMC.App/Models/ParserItem.cs index ee72471..e8f518a 100644 --- a/app/JustHostMC.App/Models/ParserItem.cs +++ b/app/JustHostMC.App/Models/ParserItem.cs @@ -9,7 +9,7 @@ public sealed class ParserItem : ScriptEntryItem { public ParserItem(ParserInfo info, ILocalizer localizer) : base(info.Id, info.Name, info.Author, info.Version, ComposeDescription(info, localizer), info.Permissions, - info.Granted) { + info.Granted, info.ConfigOptions) { Builtin = info.Builtin; } diff --git a/app/JustHostMC.App/Models/ProviderItem.cs b/app/JustHostMC.App/Models/ProviderItem.cs index 6939452..4e7c235 100644 --- a/app/JustHostMC.App/Models/ProviderItem.cs +++ b/app/JustHostMC.App/Models/ProviderItem.cs @@ -8,7 +8,7 @@ namespace JustHostMC.App.Models; public sealed class ProviderItem : ScriptEntryItem { public ProviderItem(ProviderInfo info, ILocalizer localizer) : base(info.Id, info.Name, info.Author, info.Version, info.Description, - info.Permissions, info.Granted) { + info.Permissions, info.Granted, info.ConfigOptions) { Builtin = info.Builtin; } diff --git a/app/JustHostMC.App/Models/ScriptEntryItem.cs b/app/JustHostMC.App/Models/ScriptEntryItem.cs index 20fef2f..86c6500 100644 --- a/app/JustHostMC.App/Models/ScriptEntryItem.cs +++ b/app/JustHostMC.App/Models/ScriptEntryItem.cs @@ -9,7 +9,8 @@ public abstract class ScriptEntryItem { protected ScriptEntryItem(string id, string name, string author, string version, string description, IEnumerable permissions, - IEnumerable granted) { + IEnumerable granted, + IEnumerable configOptions) { Id = id; Name = string.IsNullOrEmpty(name) ? id : name; Author = author; @@ -17,6 +18,7 @@ protected ScriptEntryItem(string id, string name, string author, Description = description; Permissions = permissions.ToList(); Granted = granted.ToList(); + ConfigOptions = configOptions.ToList(); } public string Id { get; } @@ -27,6 +29,11 @@ protected ScriptEntryItem(string id, string name, string author, public IReadOnlyList Permissions { get; } public IReadOnlyList Granted { get; } + /// Author-declared typed config options for this script (may be + /// empty). + public IReadOnlyList ConfigOptions { get; } + public bool HasConfig => ConfigOptions.Count > 0; + public virtual bool IsBuiltIn => false; public virtual bool CanRemove => true; public virtual bool SupportsToggle => false; diff --git a/app/JustHostMC.App/Models/ScriptItem.cs b/app/JustHostMC.App/Models/ScriptItem.cs index 313a3dc..896ef44 100644 --- a/app/JustHostMC.App/Models/ScriptItem.cs +++ b/app/JustHostMC.App/Models/ScriptItem.cs @@ -8,7 +8,7 @@ namespace JustHostMC.App.Models; public sealed class ScriptItem : ScriptEntryItem { public ScriptItem(ScriptInfo info, ILocalizer localizer) : base(info.Id, info.Name, info.Author, info.Version, info.Description, - info.Permissions, info.Granted) { + info.Permissions, info.Granted, info.ConfigOptions) { Enabled = info.Enabled; } diff --git a/app/JustHostMC.App/Models/ServerItem.cs b/app/JustHostMC.App/Models/ServerItem.cs index 3d3d7aa..6f6ea96 100644 --- a/app/JustHostMC.App/Models/ServerItem.cs +++ b/app/JustHostMC.App/Models/ServerItem.cs @@ -37,10 +37,24 @@ public partial string McVersion { [NotifyPropertyChangedFor(nameof(IsNeoForgeProvider))] [NotifyPropertyChangedFor(nameof(IsFabricProvider))] [NotifyPropertyChangedFor(nameof(IsTypeUnknown))] + [NotifyPropertyChangedFor(nameof(EffectiveLoader))] public partial string ProviderId { get; private set; } = ""; + /// Effective mod loader recorded at install ("fabric"/"forge"/…); + /// empty for providers whose loader equals their id. + [ObservableProperty] + [NotifyPropertyChangedFor(nameof(EffectiveLoader))] + public partial string Loader { + get; private set; + } = ""; + + /// The loader to reason about compatibility with: the recorded + /// loader when set, else the provider id (which doubles as the loader for + /// plain providers like fabric/paper/forge). + public string EffectiveLoader => Loader.Length > 0 ? Loader : ProviderId; + [ObservableProperty] [NotifyPropertyChangedFor(nameof(CanStart))] [NotifyPropertyChangedFor(nameof(CanStop))] @@ -225,6 +239,7 @@ public void Apply(Server proto) { Name = proto.Name; McVersion = proto.McVersion; ProviderId = proto.ProviderId; + Loader = proto.Loader; Status = proto.Status; Port = proto.Port; MemoryMb = proto.MemoryMb; diff --git a/app/JustHostMC.App/Models/ShopCompat.cs b/app/JustHostMC.App/Models/ShopCompat.cs new file mode 100644 index 0000000..d95d280 --- /dev/null +++ b/app/JustHostMC.App/Models/ShopCompat.cs @@ -0,0 +1,81 @@ +namespace JustHostMC.App.Models; + +/// Compatibility verdict for a shop listing against the server the +/// shop was opened for. App-local: installed jars get their verdict from the +/// engine (ModMetadata.loader_mismatch/game_version_mismatch); shop listings +/// are evaluated client-side because only the app knows the browsing +/// context. +public enum ShopCompatVerdict { + /// Not evaluated, or neither dimension was checkable. + Unknown, + Ok, + /// Built for a different mod loader. + LoaderMismatch, + /// Built for a different Minecraft version. + VersionMismatch, +} + +/// Client-side compatibility check for shop listings. Shop versions +/// carry explicit game-version and loader lists (unlike installed-jar +/// metadata, which may use ranges), so this only needs exact-list membership — +/// no range parser. The loader family table is a deliberate twin of the +/// engine's internal/grpc/modcompat.go table; keep the two in +/// sync. +public static class ShopCompat { + private static readonly Dictionary> LoaderFamilies = + new(StringComparer.OrdinalIgnoreCase) { + ["paper"] = + new(StringComparer.OrdinalIgnoreCase) { "paper", "spigot", + "bukkit" }, + ["spigot"] = + new(StringComparer.OrdinalIgnoreCase) { "spigot", "bukkit" }, + ["bukkit"] = new(StringComparer.OrdinalIgnoreCase) { "bukkit" }, + ["fabric"] = new(StringComparer.OrdinalIgnoreCase) { "fabric" }, + ["quilt"] = + new(StringComparer.OrdinalIgnoreCase) { "quilt", "fabric" }, + ["forge"] = new( + StringComparer.OrdinalIgnoreCase) { "forge", "forge-legacy" }, + ["neoforge"] = new(StringComparer.OrdinalIgnoreCase) { "neoforge" }, + ["vanilla"] = new(StringComparer.OrdinalIgnoreCase), + }; + + /// Verdict for a listing that supports and against a + /// server on / . Mirrors the engine's precedence: a confident + /// loader mismatch first, then a version mismatch, then Ok when at least + /// one dimension was checkable, else Unknown. + public static ShopCompatVerdict Evaluate( + string serverLoader, string serverMcVersion, + IReadOnlyList modLoaders, + IReadOnlyList modGameVersions) { + var loaderKnown = LoaderFamilies.TryGetValue( + (serverLoader ?? "").Trim(), out var family) && + modLoaders.Count > 0; + if (loaderKnown && !modLoaders.Any(l => family!.Contains(l.Trim()))) + return ShopCompatVerdict.LoaderMismatch; + + var versionKnown = + !string.IsNullOrEmpty(serverMcVersion) && modGameVersions.Count > 0; + if (versionKnown && + !modGameVersions.Any(v => VersionMatches(serverMcVersion, v))) + return ShopCompatVerdict.VersionMismatch; + + return loaderKnown || versionKnown ? ShopCompatVerdict.Ok + : ShopCompatVerdict.Unknown; + } + + // Shop lists are explicit, but tolerate a "1.20.x"/"1.20.*" family pin. + private static bool VersionMatches(string mc, string listed) { + listed = listed.Trim(); + if (listed.Length == 0) + return false; + if (listed.EndsWith(".x", StringComparison.Ordinal) || + listed.EndsWith(".*", StringComparison.Ordinal)) { + var prefix = listed[..^ 2]; + return mc == prefix || + mc.StartsWith(prefix + ".", StringComparison.Ordinal); + } + return string.Equals(mc, listed, StringComparison.OrdinalIgnoreCase); + } +} diff --git a/app/JustHostMC.App/Models/ShopModels.cs b/app/JustHostMC.App/Models/ShopModels.cs index cc30bfe..44f2d76 100644 --- a/app/JustHostMC.App/Models/ShopModels.cs +++ b/app/JustHostMC.App/Models/ShopModels.cs @@ -1,4 +1,5 @@ using CommunityToolkit.Mvvm.ComponentModel; +using JustHostMC.App.Services; using McManager.Grpc; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Media.Imaging; @@ -11,7 +12,11 @@ namespace JustHostMC.App.Models; /// install so the server's mod list updates. public sealed record ShopContext( string ServerId, string McVersion, string Loader, ModKind Kind, - Func> InstalledFileNames, Action OnInstalled); + Func> InstalledFileNames, Action OnInstalled, + // Invoked after a modpack shop creates a brand-new server so the opener + // can refresh its server list; null for a per-server mod shop, which only + // installs into the existing server. + Action? OnServerCreated = null); /// One project card (home/search results). public sealed class ShopProjectItem { @@ -92,6 +97,23 @@ public ShopVersionItem(ShopVersion version) { version.Dependencies.Where(d => d.Required).ToArray(); } + /// Overload that computes a compatibility badge against the + /// server the shop was opened for. is false + /// when the user's version/loader filters are active — the engine already + /// narrowed the list, so a badge would be noise. + public ShopVersionItem(ShopVersion version, ShopContext context, + bool showBadge, bool isModpack, ILocalizer localizer) + : this(version) { + ActionLabel = localizer.Get(isModpack ? "Shop_CreateServerButton" + : "Shop_InstallButtonText"); + if (showBadge) { + Compat = ShopCompat.Evaluate(context.Loader, context.McVersion, + version.Loaders, version.GameVersions); + CompatToolTip = + BuildCompatToolTip(Compat, version, context, localizer); + } + } + public ShopVersion Version { get; } public IReadOnlyList RequiredDependencies { get; } @@ -131,6 +153,34 @@ public string InfoLine { return string.Join(" · ", parts); } } + + /// Client-side compatibility verdict for this listing against the + /// server the shop was opened for; Unknown when not evaluated. + public ShopCompatVerdict Compat { get; } = ShopCompatVerdict.Unknown; + + /// Localized explanation shown on the warning badge; empty unless + /// a mismatch was detected. + public string CompatToolTip { get; } = ""; + + public Visibility CompatBadgeVisibility => + Compat is ShopCompatVerdict.LoaderMismatch or + ShopCompatVerdict.VersionMismatch + ? Visibility.Visible + : Visibility.Collapsed; + + private static string BuildCompatToolTip( + ShopCompatVerdict compat, ShopVersion version, ShopContext context, + ILocalizer localizer) => compat switch { + ShopCompatVerdict.LoaderMismatch => + localizer.Get("Shop_CompatLoaderMismatch", + ("modLoaders", string.Join(", ", version.Loaders)), + ("serverLoader", context.Loader)), + ShopCompatVerdict.VersionMismatch => localizer.Get( + "Shop_CompatVersionMismatch", + ("modVersions", string.Join(", ", version.GameVersions.Take(6))), + ("serverVersion", context.McVersion)), + _ => "", + }; } /// Display formatting shared by the shop UI. diff --git a/app/JustHostMC.App/Models/ShopSourceItem.cs b/app/JustHostMC.App/Models/ShopSourceItem.cs new file mode 100644 index 0000000..ad4ab01 --- /dev/null +++ b/app/JustHostMC.App/Models/ShopSourceItem.cs @@ -0,0 +1,43 @@ +using JustHostMC.App.Services; +using McManager.Grpc; + +namespace JustHostMC.App.Models; + +/// View model for one installed mod shop source (a remote catalog such +/// as Modrinth or CurseForge) in the Scripts page list. +public sealed class ShopSourceItem : ScriptEntryItem { + public ShopSourceItem(ShopInfo info, ILocalizer localizer) + : base(info.Id, info.Name, info.Author, info.Version, + ComposeDescription(info, localizer), info.Permissions, + info.Granted, info.ConfigOptions, localizer) { + Builtin = info.Builtin; + NeedsKey = info.NeedsKey; + Ready = info.Ready; + } + + public bool Builtin { get; } + + /// The shop declares it needs a per-shop API key. + public bool NeedsKey { get; } + + /// False only when the shop needs a key and none is configured + /// yet. + public bool Ready { get; } + + public override bool IsBuiltIn => Builtin; + + /// Built-in shop sources cannot be removed. + public override bool CanRemove => !Builtin; + + /// Appends a "needs API key" note to the description when the shop + /// requires a key but none is configured, so the shared entry card surfaces + /// it. + private static string ComposeDescription(ShopInfo info, + ILocalizer localizer) { + if (!info.NeedsKey || info.Ready) + return info.Description; + var note = localizer.Get("Scripts_ShopNeedsKey"); + return info.Description.Length == 0 ? note + : $"{info.Description}\n{note}"; + } +} diff --git a/app/JustHostMC.App/Strings/en-US/Resources.resw b/app/JustHostMC.App/Strings/en-US/Resources.resw index 12b898d..d2df84d 100644 --- a/app/JustHostMC.App/Strings/en-US/Resources.resw +++ b/app/JustHostMC.App/Strings/en-US/Resources.resw @@ -126,6 +126,7 @@ Extracting Java runtime Downloading installer Running installer + Downloading pack files Done. @@ -428,6 +429,15 @@ Mod Metadata Parsers No parsers installed. Parsers read a mod or plugin jar's embedded metadata (name, version, authors, icon) for the server Mods panel. Reads: {formats} + Mod Shops + No shop sources installed. Shop sources let you browse and download mods or plugins from a remote catalog such as Modrinth or CurseForge. + Requires an API key — set it in Settings. + Configure + Configure {name} + Save + Cancel + Leave blank to keep the current value + Not set Website Show scripts in folder Show in Folder @@ -569,6 +579,20 @@ No compatible versions for this server. Relax the filters to see more. The source no longer lists this project or version. The author disallows third-party downloads for this file. Use “Open website” instead. + Built for {modLoaders}; your server runs {serverLoader}. + Built for Minecraft {modVersions}; your server runs {serverVersion}. + This project may not support your server ({serverLoader}, Minecraft {serverVersion}). Check the Versions tab. + Install + Install latest release + Create server + Create a server + Create a new server from {0}. + Server name + Memory (MB) + Create + Server created. + Couldn't create the server. + This modpack has files hosted on CurseForge. Add a CurseForge API key in the FTB provider settings, then try again. This source needs an API key. Add one in Settings. Stop the server before installing mods. Most downloaded diff --git a/app/JustHostMC.App/Strings/zh-TW/Resources.resw b/app/JustHostMC.App/Strings/zh-TW/Resources.resw index 77fad35..2405c30 100644 --- a/app/JustHostMC.App/Strings/zh-TW/Resources.resw +++ b/app/JustHostMC.App/Strings/zh-TW/Resources.resw @@ -126,6 +126,7 @@ 解壓縮 Java 執行階段中 下載安裝程式中 執行安裝程式中 + 下載整合包檔案中 完成。 @@ -399,6 +400,15 @@ 提供者 模組資訊解析器 尚未安裝任何解析器。解析器會讀取模組或外掛 jar 內嵌的資訊(名稱、版本、作者、圖示),顯示在伺服器的模組面板。 + 模組商店 + 尚未安裝任何商店來源。商店來源讓您從 Modrinth 或 CurseForge 等遠端目錄瀏覽並下載模組或外掛。 + 需要 API 金鑰 — 請在設定中設定。 + 設定 + 設定 {name} + 儲存 + 取消 + 留空以保留目前的值 + 未設定 讀取:{formats} 網站 在資料夾中顯示腳本 @@ -558,6 +568,20 @@ 沒有與此伺服器相容的版本,放寬篩選即可看到更多。 來源已不再提供此專案或版本。 作者不允許第三方下載此檔案,請改用「開啟網站」。 + 此版本適用於 {modLoaders};你的伺服器執行的是 {serverLoader}。 + 此版本適用於 Minecraft {modVersions};你的伺服器執行的是 {serverVersion}。 + 此專案可能不支援你的伺服器({serverLoader},Minecraft {serverVersion})。請查看「版本」分頁。 + 安裝 + 安裝最新發行版 + 建立伺服器 + 建立伺服器 + 從 {0} 建立新的伺服器。 + 伺服器名稱 + 記憶體 (MB) + 建立 + 已建立伺服器。 + 無法建立伺服器。 + 此整合包有託管於 CurseForge 的檔案。請在 FTB 供應器設定中新增 CurseForge API 金鑰後再試一次。 此來源需要 API 金鑰,請到「設定」新增。 請先停止伺服器再安裝模組。 下載最多 diff --git a/app/JustHostMC.App/ViewModels/ScriptsViewModel.cs b/app/JustHostMC.App/ViewModels/ScriptsViewModel.cs index e8cc7eb..78c6df7 100644 --- a/app/JustHostMC.App/ViewModels/ScriptsViewModel.cs +++ b/app/JustHostMC.App/ViewModels/ScriptsViewModel.cs @@ -41,6 +41,7 @@ public ScriptsViewModel(DispatcherQueue dispatcher, ILocalizer localizer) { public ObservableCollection Providers { get; } = new(); public ObservableCollection Scripts { get; } = new(); public ObservableCollection Parsers { get; } = new(); + public ObservableCollection Shops { get; } = new(); /// /// Application logging sessions in reverse chronological order. The current @@ -83,6 +84,7 @@ public async Task RefreshAsync() { await RefreshProvidersAsync(daemon); await RefreshScriptsAsync(daemon); await RefreshParsersAsync(daemon); + await RefreshShopsAsync(daemon); } finally { RunOnUI(() => IsBusy = false); } @@ -135,6 +137,19 @@ private async Task } } + private async Task RefreshShopsAsync(JustHostMC.Core.DaemonClient daemon) { + try { + var list = await daemon.Shop.ListAsync(new Empty()); + RunOnUI(() => { + Shops.Clear(); + foreach (var s in list.Shops) + Shops.Add(new ShopSourceItem(s, _localizer)); + }); + } catch (RpcException ex) { + RunOnUI(() => StatusMessage = _localizer.Get(MapErrorKey(ex))); + } + } + /// Imports a provider script (+ optional jar) with the granted /// permissions. public async Task @@ -207,6 +222,27 @@ public async Task ImportParserAsync(string luaSource, } } + /// Imports a mod shop source with the granted + /// permissions. + public async Task ImportShopAsync(string luaSource, + IReadOnlyList granted) { + RunOnUI(() => { + IsBusy = true; + StatusMessage = ""; + }); + try { + var daemon = await App.Current.DaemonReady; + var info = await daemon.Shop.ImportAsync( + new ImportShopRequest { LuaSource = luaSource }); + await SetShopPermissionsAsync(daemon, info.Id, granted); + await RefreshShopsAsync(daemon); + } catch (RpcException ex) { + RunOnUI(() => StatusMessage = _localizer.Get(MapErrorKey(ex))); + } finally { + RunOnUI(() => IsBusy = false); + } + } + public async Task SetScriptEnabledAsync(ScriptItem item, bool enabled) { try { var daemon = await App.Current.DaemonReady; @@ -254,6 +290,16 @@ public async Task RemoveParserAsync(ParserItem item) { } } + public async Task RemoveShopAsync(ShopSourceItem item) { + try { + var daemon = await App.Current.DaemonReady; + await daemon.Shop.RemoveAsync(new ProviderRef { Id = item.Id }); + await RefreshShopsAsync(daemon); + } catch (RpcException ex) { + RunOnUI(() => StatusMessage = _localizer.Get(MapErrorKey(ex))); + } + } + private static async Task SetParserPermissionsAsync(JustHostMC.Core.DaemonClient daemon, string id, IReadOnlyList granted) { @@ -262,6 +308,69 @@ private static async Task await daemon.Parsers.SetPermissionsAsync(req); } + private static async Task + SetShopPermissionsAsync(JustHostMC.Core.DaemonClient daemon, string id, + IReadOnlyList granted) { + var req = new SetPermissionsRequest { Id = id }; + req.Granted.AddRange(granted); + await daemon.Shop.SetPermissionsAsync(req); + } + + /// Fetches the current config values for a scriptable item + /// (provider, automation script, parser or shop). Returns null on + /// error. + public async Task GetConfigAsync(ScriptEntryItem item) { + try { + var daemon = await App.Current.DaemonReady; + var reference = new ProviderRef { Id = item.Id }; + return item switch { + ProviderItem => + await daemon.Providers.GetConfigAsync(reference), + ScriptItem => await daemon.Scripts.GetConfigAsync(reference), + ParserItem => await daemon.Parsers.GetConfigAsync(reference), + ShopSourceItem => await daemon.Shop.GetConfigAsync(reference), + _ => null, + }; + } catch (RpcException ex) { + RunOnUI(() => StatusMessage = _localizer.Get(MapErrorKey(ex))); + return null; + } + } + + /// Persists the changed config values for a scriptable item, then + /// refreshes so derived state (e.g. a shop's Ready flag) updates. + public async Task SetConfigAsync(ScriptEntryItem item, + SetConfigRequest req) { + if (req.Values.Count == 0) + return; + RunOnUI(() => { + IsBusy = true; + StatusMessage = ""; + }); + try { + var daemon = await App.Current.DaemonReady; + switch (item) { + case ProviderItem: + await daemon.Providers.SetConfigAsync(req); + break; + case ScriptItem: + await daemon.Scripts.SetConfigAsync(req); + break; + case ParserItem: + await daemon.Parsers.SetConfigAsync(req); + break; + case ShopSourceItem: + await daemon.Shop.SetConfigAsync(req); + break; + } + await RefreshAsync(); + } catch (RpcException ex) { + RunOnUI(() => StatusMessage = _localizer.Get(MapErrorKey(ex))); + } finally { + RunOnUI(() => IsBusy = false); + } + } + private static async Task SetProviderPermissionsAsync(JustHostMC.Core.DaemonClient daemon, string id, IReadOnlyList granted) { diff --git a/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs b/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs index 18bb831..f6abb12 100644 --- a/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs +++ b/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs @@ -34,11 +34,22 @@ public ShopDetailViewModel(ShopViewModel shop, ShopProjectItem card, _localizer = localizer; _shopId = card.Project.ShopId; _projectId = card.Project.ProjectId; - SourceName = - shop.Shops.FirstOrDefault(s => s.Id == _shopId)?.Name ?? _shopId; - Card = card; + Card = card; + var shopInfo = shop.Shops.FirstOrDefault(s => s.Id == _shopId); + SourceName = shopInfo?.Name ?? _shopId; + IsModpack = shopInfo?.Kinds.Contains("modpack") ?? false; } + /// True when this project is a modpack: its versions create whole + /// new servers (via the ftb provider) instead of installing a file into + /// one. + public bool IsModpack { get; } + + /// Label for the header action button, matching the project + /// kind. + public string InstallActionLabel => _localizer.Get( + IsModpack ? "Shop_CreateServerButton" : "Shop_InstallLatestButtonText"); + /// The card the user clicked; the header shows it immediately /// while the full detail loads. [ObservableProperty] @@ -125,8 +136,29 @@ public partial string WebsiteUrl { get; private set; } = ""; + [ObservableProperty] + [NotifyPropertyChangedFor(nameof(HasProjectCompatWarning))] + public partial string ProjectCompatMessage { + get; private set; + } = ""; + + /// Whether to show the project-level "may not fit your server" + /// banner. + public bool HasProjectCompatWarning => ProjectCompatMessage.Length > 0; + public bool HasGallery => Gallery.Count > 0; + // A project-level mismatch means none of the project's loaders/versions + // fit the server, so both mismatch kinds collapse to one summary banner. + private string ProjectCompatText( + ShopCompatVerdict compat) => compat switch { + ShopCompatVerdict.LoaderMismatch or ShopCompatVerdict.VersionMismatch => + _localizer.Get("Shop_CompatProjectWarning", + ("serverLoader", _shop.Context.Loader), + ("serverVersion", _shop.Context.McVersion)), + _ => "", + }; + /// Loads detail + versions concurrently. public Task LoadAsync(bool darkTheme) => Task.WhenAll(LoadDetailAsync(darkTheme), LoadVersionsAsync()); @@ -145,6 +177,17 @@ private async Task LoadDetailAsync(bool darkTheme) { ? ShopBodyRenderer.ToHtml( detail.Body, detail.BodyFormat, darkTheme) : ""; + // The detail lists every loader/version the project supports; a + // mismatch here means none of them fits this server. Suppressed + // while filtering. + var filtersActive = _shop.UseVersionFilter || _shop.UseLoaderFilter; + var projectCompat = + filtersActive + ? ShopCompatVerdict.Unknown + : ShopCompat.Evaluate(_shop.Context.Loader, + _shop.Context.McVersion, + detail.Loaders, detail.GameVersions); + var compatMessage = ProjectCompatText(projectCompat); await RunOnUIAsync(() => { if (detail.Project is not null && detail.Project.Title.Length > 0) @@ -155,6 +198,7 @@ await RunOnUIAsync(() => { Gallery.Add(new ShopGalleryItem(image)); OnPropertyChanged(nameof(HasGallery)); BodyHtml = html; + ProjectCompatMessage = compatMessage; RefreshPrimaryAction(); }); } catch { @@ -177,10 +221,15 @@ private async Task LoadVersionsAsync() { Loader = _shop.UseLoaderFilter ? _shop.Context.Loader : "", }, deadline: DateTime.UtcNow.AddSeconds(30)); + // With a filter on, the engine already narrowed the list to + // matches, so a per-row badge would be redundant noise. + var showBadge = !_shop.UseVersionFilter && !_shop.UseLoaderFilter; await RunOnUIAsync(() => { Versions.Clear(); foreach (var version in list.Versions) - Versions.Add(new ShopVersionItem(version)); + Versions.Add(new ShopVersionItem(version, _shop.Context, + showBadge, IsModpack, + _localizer)); LatestRelease = Versions .Where(version => version.Version.Channel == @@ -285,6 +334,57 @@ private void RefreshPrimaryAction() { } } + /// Creates a brand-new server from a modpack version, streaming + /// the (long) install into the shared install-progress UI. + public async Task CreateServerAsync(ShopVersionItem version, string name, + int memoryMb) { + await RunOnUIAsync(() => { + IsInstalling = true; + OnPropertyChanged(nameof(CanInstallLatest)); + InstallSucceeded = false; + InstallProgress = 0; + StatusMessage = ""; + }); + try { + var daemon = await App.Current.DaemonReady; + var request = new CreateServerRequest { + Name = name, + ProviderId = "ftb", + McVersion = $"{_projectId}/{version.Version.Id}", + MemoryMb = memoryMb, + Port = 0, + }; + // A modpack install pulls hundreds of files and runs a loader + // installer, so stream without a tight deadline (like the main + // create-server flow). + using var call = daemon.Servers.Create(request); + await foreach (var progress in call.ResponseStream.ReadAllAsync()) { + var fraction = progress.Fraction; + await RunOnUIAsync(() => { + if (fraction >= 0) + InstallProgress = fraction; + }); + } + await RunOnUIAsync(() => { + InstallSucceeded = true; + StatusMessage = _localizer.Get("Shop_ServerCreated"); + }); + _shop.Context.OnServerCreated?.Invoke(); + } catch (RpcException ex) when (ex.Status.Detail.Contains( + "CurseForge API key", StringComparison.OrdinalIgnoreCase)) { + await RunOnUIAsync(() => StatusMessage = + _localizer.Get("Shop_CreateServerNeedsKey")); + } catch { + await RunOnUIAsync(() => StatusMessage = + _localizer.Get("Shop_CreateServerFailed")); + } finally { + await RunOnUIAsync(() => { + IsInstalling = false; + OnPropertyChanged(nameof(CanInstallLatest)); + }); + } + } + // Follows the app's StatusCode-level mapping convention (see // ModsViewModel.MapErrorKey); the diagnostic text disambiguates the two // FailedPrecondition causes without unpacking ErrorDetail. diff --git a/app/JustHostMC.App/ViewModels/ShopViewModel.cs b/app/JustHostMC.App/ViewModels/ShopViewModel.cs index 8678aa5..1d45f89 100644 --- a/app/JustHostMC.App/ViewModels/ShopViewModel.cs +++ b/app/JustHostMC.App/ViewModels/ShopViewModel.cs @@ -102,6 +102,16 @@ partial void OnSelectedShopChanged(ShopInfo? value) { OnPropertyChanged(nameof(SelectedShopName)); var categoryGeneration = Interlocked.Increment(ref _categoryGeneration); + OnPropertyChanged(nameof(SelectedShopIsModpack)); + OnPropertyChanged(nameof(ShowVersionFilter)); + OnPropertyChanged(nameof(ShowLoaderFilter)); + // Modpacks browse unfiltered; clear any active compat filter so its + // chip (bound to Use*Filter) also disappears, not just the hidden + // toggle. + if (SelectedShopIsModpack) { + UseVersionFilter = false; + UseLoaderFilter = false; + } CategoryFilters.Clear(); if (value?.Id == "modrinth") { foreach (var id in ModrinthCategories) @@ -164,9 +174,26 @@ public void ResetCategoryFilters() { foreach (var category in CategoryFilters) category.IsSelected = false; } + /// True when the selected shop serves modpacks (installed as + /// whole servers), which switches the browse kind and hides the compat + /// filters. + public bool SelectedShopIsModpack => + SelectedShop?.Kinds.Contains("modpack") ?? false; + + /// Version/loader filter toggles only make sense for mod/plugin + /// shops. + public bool ShowVersionFilter => !SelectedShopIsModpack && HasVersionFilter; + public bool ShowLoaderFilter => !SelectedShopIsModpack && HasLoaderFilter; + + private ModKind EffectiveKind => + SelectedShopIsModpack ? ModKind.Modpack : Context.Kind; + + // Modpacks pin their own MC version + loader, so a modpack shop always + // browses unfiltered regardless of the (hidden) toggle state. private string EffectiveVersion => - UseVersionFilter ? Context.McVersion : ""; - private string EffectiveLoader => UseLoaderFilter ? Context.Loader : ""; + !SelectedShopIsModpack && UseVersionFilter ? Context.McVersion : ""; + private string EffectiveLoader => + !SelectedShopIsModpack && UseLoaderFilter ? Context.Loader : ""; /// Loads the shop list and selects the first ready /// source. @@ -205,7 +232,7 @@ await RunOnUIAsync(() => { ShopId = shop.Id, McVersion = EffectiveVersion, Loader = EffectiveLoader, - Kind = Context.Kind, + Kind = EffectiveKind, }, deadline: DateTime.UtcNow.AddSeconds(30)); @@ -270,7 +297,7 @@ private async Task> LoadSearchPageAsync( Query = Query, McVersion = EffectiveVersion, Loader = EffectiveLoader, - Kind = Context.Kind, + Kind = EffectiveKind, Sort = Sort, Offset = offset, Limit = PageSize, @@ -305,7 +332,7 @@ public async Task> GetSuggestionsAsync(string text) { Query = text, McVersion = EffectiveVersion, Loader = EffectiveLoader, - Kind = Context.Kind, + Kind = EffectiveKind, Sort = ShopSort.Relevance, Offset = 0, Limit = SuggestionCount, diff --git a/app/JustHostMC.App/Views/ScriptConfigDialog.cs b/app/JustHostMC.App/Views/ScriptConfigDialog.cs new file mode 100644 index 0000000..7e842f5 --- /dev/null +++ b/app/JustHostMC.App/Views/ScriptConfigDialog.cs @@ -0,0 +1,143 @@ +using System.Collections.Generic; +using System.Globalization; +using JustHostMC.App.Services; +using McManager.Grpc; +using Microsoft.UI.Text; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; + +namespace JustHostMC.App.Views; + +/// Editor for a script's typed config options. Built imperatively +/// (the row control varies by option type), it is used as the content of a +/// ContentDialog. On save, yields only the values +/// the user actually changed. +public sealed class ScriptConfigDialog : UserControl { + private sealed class Field { + public Field(string key, ConfigOptionType type, FrameworkElement input, + string original) { + Key = key; + Type = type; + Input = input; + Original = original; + } + + public string Key { get; } + public ConfigOptionType Type { get; } + public FrameworkElement Input { get; } + public string Original { get; } + + public bool TryGetChange(out string value) { + value = Read(); + // Secrets start blank and are write-only: only persist a non-empty + // entry, so leaving the box empty keeps any existing secret intact. + if (Type == ConfigOptionType.ConfigOptionSecret) + return value.Length > 0; + return value != Original; + } + + private string Read() => Input switch { + PasswordBox pb => pb.Password, + ToggleSwitch ts => ts.IsOn ? "true" : "false", + NumberBox nb => + double.IsNaN(nb.Value) + ? "" + : nb.Value.ToString(CultureInfo.InvariantCulture), + TextBox tb => tb.Text, + _ => "", + }; + } + + private readonly string _id; + private readonly List _fields = new(); + + public ScriptConfigDialog(string id, IReadOnlyList options, + ScriptConfig current, ILocalizer localizer) { + _id = id; + + var stored = new Dictionary(); + foreach (var v in current.Values) stored[v.Key] = v; + + var panel = new StackPanel { Spacing = 16 }; + foreach (var opt in options) { + stored.TryGetValue(opt.Key, out var value); + var (input, original) = BuildInput(opt, value, localizer); + _fields.Add(new Field(opt.Key, opt.Type, input, original)); + + var row = new StackPanel { Spacing = 4 }; + row.Children.Add(new TextBlock { + Text = string.IsNullOrEmpty(opt.Name) ? opt.Key : opt.Name, + FontWeight = FontWeights.SemiBold, + }); + if (!string.IsNullOrEmpty(opt.Description)) + row.Children.Add(new TextBlock { + Text = opt.Description, + Opacity = 0.7, + TextWrapping = TextWrapping.WrapWholeWords, + }); + row.Children.Add(input); + panel.Children.Add(row); + } + + Content = new ScrollViewer { + VerticalScrollBarVisibility = ScrollBarVisibility.Auto, + HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled, + MinWidth = 360, + Content = panel, + }; + } + + private static (FrameworkElement input, string original) + BuildInput(ConfigOption opt, ScriptConfigValue? value, + ILocalizer localizer) { + var hasValue = value?.HasValue ?? false; + var storedValue = value?.Value ?? ""; + + switch (opt.Type) { + case ConfigOptionType.ConfigOptionSecret: + var pb = new PasswordBox { + PlaceholderText = localizer.Get( + hasValue ? "ScriptConfig_SecretSetPlaceholder" + : "ScriptConfig_SecretUnsetPlaceholder"), + }; + return (pb, ""); + + case ConfigOptionType.ConfigOptionBoolean: + var effective = hasValue ? storedValue : opt.Default; + var on = bool.TryParse(effective, out var b) && b; + return (new ToggleSwitch { IsOn = on }, on ? "true" : "false"); + + case ConfigOptionType.ConfigOptionNumber: + var nb = new NumberBox { + SpinButtonPlacementMode = + NumberBoxSpinButtonPlacementMode.Compact, + PlaceholderText = opt.Default, + }; + if (hasValue && + double.TryParse(storedValue, NumberStyles.Any, + CultureInfo.InvariantCulture, out var d)) + nb.Value = d; + else + nb.Value = double.NaN; + return (nb, hasValue ? storedValue : ""); + + default: // string + var tb = new TextBox { + Text = hasValue ? storedValue : "", + PlaceholderText = opt.Default, + }; + return (tb, hasValue ? storedValue : ""); + } + } + + /// Builds a SetConfigRequest with only the values the user changed + /// (secrets only when a new one was entered). + public SetConfigRequest BuildRequest() { + var req = new SetConfigRequest { Id = _id }; + foreach (var f in _fields) + if (f.TryGetChange(out var value)) + req.Values.Add( + new ScriptConfigValue { Key = f.Key, Value = value }); + return req; + } +} diff --git a/app/JustHostMC.App/Views/ScriptsPage.xaml b/app/JustHostMC.App/Views/ScriptsPage.xaml index e651717..400686f 100644 --- a/app/JustHostMC.App/Views/ScriptsPage.xaml +++ b/app/JustHostMC.App/Views/ScriptsPage.xaml @@ -41,6 +41,7 @@ Margin="0,24,0,0" AddScriptsClick="OnImportProviderClick" ItemsSource="{x:Bind ViewModel.Providers}" + ScriptConfigured="OnConfigureClick" ScriptRemoved="OnRemoveScript" ScriptToggled="OnScriptToggled" ShowInFolderClick="OnShowProvidersFolderClick"> @@ -53,6 +54,7 @@ @@ -61,10 +63,24 @@ + + + + + + + diff --git a/app/JustHostMC.App/Views/ScriptsPage.xaml.cs b/app/JustHostMC.App/Views/ScriptsPage.xaml.cs index 3365421..f8c6439 100644 --- a/app/JustHostMC.App/Views/ScriptsPage.xaml.cs +++ b/app/JustHostMC.App/Views/ScriptsPage.xaml.cs @@ -109,6 +109,27 @@ private async void OnImportParserClick(object sender, RoutedEventArgs e) { await ViewModel.ImportParserAsync(source, granted); } + private async void OnImportShopClick(object sender, RoutedEventArgs e) { + var lua = await PickLuaFileAsync(); + if (lua is null) + return; + + string source; + try { + source = await FileIO.ReadTextAsync(lua); + } catch (Exception ex) + when (ex is IOException or UnauthorizedAccessException) { + ViewModel.SetStatus(_localizer.Get("Scripts_ReadFailed")); + return; + } + + var granted = await RequestConsentAsync(lua.Name, source); + if (granted is null) + return; + + await ViewModel.ImportShopAsync(source, granted); + } + private void OnShowProvidersFolderClick(object sender, RoutedEventArgs e) => ShowInFolder("providers"); @@ -118,6 +139,9 @@ private void OnShowParsersFolderClick(object sender, RoutedEventArgs e) => private void OnShowAutomationFolderClick( object sender, RoutedEventArgs e) => ShowInFolder("scripts"); + private void OnShowShopsFolderClick(object sender, RoutedEventArgs e) => + ShowInFolder("shops"); + private void OnShowAllLogsClick(object sender, RoutedEventArgs e) { if (_logsWindow is null) { var window = new ScriptLogsWindow(ViewModel.LogSessions); @@ -180,6 +204,36 @@ private async void OnRemoveScript(object sender, RoutedEventArgs e) { await ViewModel.RemoveProviderAsync(provider); else if (sender is ScriptEntryCard { Item : ParserItem parser }) await ViewModel.RemoveParserAsync(parser); + else if (sender is ScriptEntryCard { Item : ShopSourceItem shop }) + await ViewModel.RemoveShopAsync(shop); + } + + private async void OnConfigureClick(object sender, RoutedEventArgs e) { + if (sender is not ScriptEntryCard { Item : {} item } || !item.HasConfig) + return; + + var current = await ViewModel.GetConfigAsync(item); + if (current is null) + return; + + var content = new ScriptConfigDialog(item.Id, item.ConfigOptions, + current, _localizer); + var dialog = new ContentDialog { + XamlRoot = XamlRoot, + Style = Application.Current + .Resources["DefaultContentDialogStyle"] as Style, + Title = + _localizer.Get("ScriptConfig_TitleNamed", ("name", item.Name)), + Content = content, + PrimaryButtonText = _localizer.Get("ScriptConfig_Save"), + CloseButtonText = _localizer.Get("ScriptConfig_Cancel"), + DefaultButton = ContentDialogButton.Primary, + }; + ContentDialogSizing.Apply(dialog); + + if (await dialog.ShowAsync() != ContentDialogResult.Primary) + return; + await ViewModel.SetConfigAsync(item, content.BuildRequest()); } private async void OnScriptToggled(object sender, RoutedEventArgs e) { diff --git a/app/JustHostMC.App/Views/ServerDialog.xaml.cs b/app/JustHostMC.App/Views/ServerDialog.xaml.cs index ee0955d..3e4bd75 100644 --- a/app/JustHostMC.App/Views/ServerDialog.xaml.cs +++ b/app/JustHostMC.App/Views/ServerDialog.xaml.cs @@ -88,6 +88,9 @@ private async Task LoadProvidersAsync() { var providers = await _viewModel.GetProvidersAsync(); TypeBox.ItemsSource = providers + // Hidden providers (e.g. the modpack installer) are driven + // by their own shop flow, not the manual create dialog. + .Where(p => p.Capabilities?.Hidden != true) .Select(p => new TypeChoice(p, string.IsNullOrEmpty(p.Name) ? p.Id : p.Name)) diff --git a/app/JustHostMC.App/Views/ServerPage.xaml b/app/JustHostMC.App/Views/ServerPage.xaml index d7cc0df..d805c85 100644 --- a/app/JustHostMC.App/Views/ServerPage.xaml +++ b/app/JustHostMC.App/Views/ServerPage.xaml @@ -84,6 +84,7 @@ x:Name="ModsPanel" Mods="{x:Bind Mods, Mode=OneWay}" Server="{x:Bind Server, Mode=OneWay}" + ServerCreated="OnShopServerCreated" Visibility="Collapsed" /> diff --git a/app/JustHostMC.App/Views/ServerPage.xaml.cs b/app/JustHostMC.App/Views/ServerPage.xaml.cs index b42b4cf..1d95191 100644 --- a/app/JustHostMC.App/Views/ServerPage.xaml.cs +++ b/app/JustHostMC.App/Views/ServerPage.xaml.cs @@ -241,4 +241,9 @@ private async void OnDeleteClick(object sender, RoutedEventArgs e) { _main.DeleteServerCommand.Execute(Server); _shell.RequestHome(); } + + /// A modpack shop opened from the mods panel can create whole new + /// servers; pull them into the main list. + private void OnShopServerCreated(object sender, RoutedEventArgs e) => + _main.RefreshCommand.Execute(null); } diff --git a/app/JustHostMC.App/Views/ShopDetailPage.xaml b/app/JustHostMC.App/Views/ShopDetailPage.xaml index 27c255f..1e4b19d 100644 --- a/app/JustHostMC.App/Views/ShopDetailPage.xaml +++ b/app/JustHostMC.App/Views/ShopDetailPage.xaml @@ -123,7 +123,7 @@ Style="{StaticResource AccentButtonStyle}" Tag="{x:Bind ViewModel.LatestRelease, Mode=OneWay}"> - + @@ -255,7 +255,7 @@ Style="{StaticResource AccentButtonStyle}" Tag="{x:Bind ViewModel.LatestRelease, Mode=OneWay}"> - + @@ -292,11 +292,22 @@ Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" RowSpacing="8"> + - + + + - + @@ -358,6 +369,14 @@ Foreground="{ThemeResource TextFillColorTertiaryBrush}" Style="{StaticResource CaptionTextBlockStyle}" Text="{x:Bind VersionNumber}" /> + chosen = []; if (dependencies.Count > 0) { @@ -109,6 +114,51 @@ private async void OnInstallClick(object sender, RoutedEventArgs e) { await ViewModel.InstallAsync(version, chosen); } + // Prompts for a name + memory, then creates a new server from a modpack + // version. Built imperatively to avoid ItemsControl/x:Bind template + // pitfalls. + private async Task CreateServerFlow(ShopVersionItem version) { + var nameBox = new TextBox { + Header = _localizer.Get("Shop_CreateServerNameLabel"), + Text = ViewModel.Card?.Title ?? "", + }; + var memoryBox = new NumberBox { + Header = _localizer.Get("Shop_CreateServerMemoryLabel"), + Value = 4096, + Minimum = 512, + Maximum = 65536, + SmallChange = 512, + LargeChange = 1024, + SpinButtonPlacementMode = NumberBoxSpinButtonPlacementMode.Inline, + }; + var panel = new StackPanel { Spacing = 12 }; + panel.Children.Add(new TextBlock { + Text = string.Format(_localizer.Get("Shop_CreateServerPromptBody"), + version.Name), + TextWrapping = TextWrapping.Wrap, + }); + panel.Children.Add(nameBox); + panel.Children.Add(memoryBox); + + var dialog = new ContentDialog { + XamlRoot = XamlRoot, + Title = _localizer.Get("Shop_CreateServerTitle"), + Content = panel, + PrimaryButtonText = _localizer.Get("Shop_CreateServerConfirm"), + CloseButtonText = _localizer.Get("Common_Cancel"), + DefaultButton = ContentDialogButton.Primary, + }; + if (await dialog.ShowAsync() != ContentDialogResult.Primary) + return; + + var name = nameBox.Text.Trim(); + if (name.Length == 0) + name = ViewModel.Card?.Title ?? "Modpack"; + var memory = + double.IsNaN(memoryBox.Value) ? 4096 : (int)memoryBox.Value; + await ViewModel.CreateServerAsync(version, name, memory); + } + private async void OnOpenWebsite(object sender, RoutedEventArgs e) { await OpenWebsiteAsync(); } diff --git a/app/JustHostMC.App/Views/ShopHomePage.xaml b/app/JustHostMC.App/Views/ShopHomePage.xaml index 84eca1b..0839ac6 100644 --- a/app/JustHostMC.App/Views/ShopHomePage.xaml +++ b/app/JustHostMC.App/Views/ShopHomePage.xaml @@ -210,7 +210,7 @@ + Visibility="{x:Bind ViewModel.ShowVersionFilter, Mode=OneWay}"> @@ -219,7 +219,7 @@ + Visibility="{x:Bind ViewModel.ShowLoaderFilter, Mode=OneWay}"> diff --git a/app/JustHostMC.App/Views/ShopSearchPage.xaml b/app/JustHostMC.App/Views/ShopSearchPage.xaml index 67e0dd4..c368c61 100644 --- a/app/JustHostMC.App/Views/ShopSearchPage.xaml +++ b/app/JustHostMC.App/Views/ShopSearchPage.xaml @@ -269,12 +269,12 @@ Click="OnVersionFilterChanged" Content="{x:Bind ViewModel.VersionFilterLabel, Mode=OneWay}" IsChecked="{x:Bind ViewModel.UseVersionFilter, Mode=TwoWay}" - Visibility="{x:Bind ViewModel.HasVersionFilter}" /> + Visibility="{x:Bind ViewModel.ShowVersionFilter, Mode=OneWay}" /> + Visibility="{x:Bind ViewModel.ShowLoaderFilter, Mode=OneWay}" /> @@ -363,12 +363,12 @@ Click="OnVersionFilterChanged" Content="{x:Bind ViewModel.VersionFilterLabel, Mode=OneWay}" IsChecked="{x:Bind ViewModel.UseVersionFilter, Mode=TwoWay}" - Visibility="{x:Bind ViewModel.HasVersionFilter}" /> + Visibility="{x:Bind ViewModel.ShowVersionFilter, Mode=OneWay}" /> + Visibility="{x:Bind ViewModel.ShowLoaderFilter, Mode=OneWay}" /> diff --git a/app/JustHostMC.App/Views/ShopWindow.xaml b/app/JustHostMC.App/Views/ShopWindow.xaml index 799b5cf..b9bc697 100644 --- a/app/JustHostMC.App/Views/ShopWindow.xaml +++ b/app/JustHostMC.App/Views/ShopWindow.xaml @@ -84,7 +84,7 @@ AutomationProperties.AutomationId="ShopVersionFilter" Click="OnFilterChanged" IsChecked="{x:Bind ViewModel.UseVersionFilter, Mode=TwoWay}" - Visibility="{x:Bind ViewModel.HasVersionFilter}"> + Visibility="{x:Bind ViewModel.ShowVersionFilter, Mode=OneWay}"> + Visibility="{x:Bind ViewModel.ShowLoaderFilter, Mode=OneWay}"> public sealed partial class ShopHomePage : Page { + private readonly ILocalizer _localizer = new LocalizationService(); private ShopWindow? _window; public ShopViewModel ViewModel { get; private set; } = null!; @@ -24,6 +26,13 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { _window = args.Window; ViewModel = args.Shop; Bindings.Update(); + // The x:Uid welcome copy is written for the server-scoped shop; the + // home-page modpack shop gets its own wording. + if (!ViewModel.Context.IsServerScoped) { + DiscoverTitle.Text = _localizer.Get("Shop_DiscoverTitleGlobal"); + DiscoverSubtitle.Text = + _localizer.Get("Shop_DiscoverSubtitleGlobal"); + } if (ViewModel.HomeSections.Count == 0) _ = ViewModel.LoadHomeAsync(); } diff --git a/app/JustHostMC.App/Views/ShopWindow.xaml.cs b/app/JustHostMC.App/Views/ShopWindow.xaml.cs index c2cc453..3f09021 100644 --- a/app/JustHostMC.App/Views/ShopWindow.xaml.cs +++ b/app/JustHostMC.App/Views/ShopWindow.xaml.cs @@ -79,7 +79,12 @@ public ShopWindow(ShopContext context) { InitializeComponent(); Title = ShopTitleBar.Title; - ExtendsContentIntoTitleBar = true; + if (!context.IsServerScoped) { + var title = _localizer.Get("ShopWindow_TitleModpacks"); + Title = title; + ShopTitleBar.Title = title; + } + ExtendsContentIntoTitleBar = true; AppWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall; SetTitleBar(ShopTitleBar); InstallMinimumWindowSizeHook(); From 04a06f280f0eeb3e3ea4c3a2931915c87d58b104 Mon Sep 17 00:00:00 2001 From: 000hen Date: Sat, 11 Jul 2026 22:09:20 +0800 Subject: [PATCH 03/30] docs: spec for FTB parallel downloads, modpack update and export Co-Authored-By: Claude Fable 5 --- ...07-11-ftb-parallel-update-export-design.md | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 docs/superpowers/specs/2026-07-11-ftb-parallel-update-export-design.md diff --git a/docs/superpowers/specs/2026-07-11-ftb-parallel-update-export-design.md b/docs/superpowers/specs/2026-07-11-ftb-parallel-update-export-design.md new file mode 100644 index 0000000..d71b121 --- /dev/null +++ b/docs/superpowers/specs/2026-07-11-ftb-parallel-update-export-design.md @@ -0,0 +1,162 @@ +# FTB modpacks: parallel downloads, update, and export + +Date: 2026-07-11 · Branch: `feature/modpack-support` · Status: approved + +## Problem + +1. FTB modpack installs are slow: `ftb.lua` downloads the manifest's files one at + a time, and every CurseForge-hosted file adds a blocking API round-trip to + resolve its download URL. +2. Once installed, a modpack server is frozen: the pack identity + (`packId/versionId`) is discarded after install (`ServerService.Create` + overwrites `rec.McVersion` with the resolved MC version), so there is no way + to move a server to a newer pack version. +3. Players joining a modded server need the matching client pack with the + server's configs. There is no way to export one. + +## Decisions (user-approved) + +- Update = **manifest diff** (like the official FTB App): delete files the new + version dropped, download new/changed ones; world and non-pack files + untouched. +- Export = **CurseForge pack zip** (`manifest.json` + `overrides/`), importable + by the CurseForge app, Prism, ATLauncher. +- UI placement: **server actions flyout** (伺服器動作), items visible only for + modpack servers. +- All four pieces in one round. + +## 1. Parallel downloads — `jhmc.download_all` + +New host function in `engine/internal/scripting/hostfuncs.go`: + +```lua +jhmc.download_all(items, { concurrency = 6 }) +-- item: { dest = "mods/x.jar", sha1 = "…", url = "https://…" } +-- or: { dest = "…", sha1 = "…", resolve = { url = "https://…", headers = {…} } } +``` + +- `resolve` means: GET the JSON endpoint, take the `data` string field as the + real download URL (the CurseForge `download-url` convention), then download. + Resolution happens inside the worker, so CF API round-trips parallelize too. +- Go side: worker pool (errgroup + semaphore), default concurrency 6, capped + (say 16). First error cancels the batch via context; the failing file's name + is in the error. Requires `network` permission; every `dest` goes through + `resolvePath` (server-dir confinement, unchanged). +- Progress: on each completed file emit `Progress{Step: "shop.install.downloading", + Fraction: completed/total, LogLine: dest-basename}`. Per-byte fractions from + individual downloads are NOT emitted (they'd interleave across workers). +- HTTP status/error semantics per item match today's `download_file` behavior, + including the CF 403 "author disallows third-party downloads" message. + +`ftb.lua` `install_files` builds the full item list (direct-URL files, and +CF-hosted ones as `resolve` items with the configured key header) and makes one +`download_all` call. The missing-CF-key check stays in Lua, up front, before +any download starts. + +## 2. Persist pack identity + +- `ftb.lua` `install()` (and `update()`) return `pack_version = ctx.version` + (the opaque `"packId/versionId"`) in the launch spec. +- Engine: new field on the provider spec struct, stored on the server registry + record (`ProviderVersion`), returned in the proto `Server` message as + `string provider_version` (new field, next free tag). +- Empty for every non-modpack provider. The app treats "non-empty + provider_version" as "this is a modpack server". + +## 3. Update modpack + +**Proto:** `rpc UpdateModpack(UpdateModpackRequest) returns (stream InstallProgress)` +on `ServerService`; request = `{ string id; string version; }` where version is +the new `"packId/versionId"`. + +**Engine (`serverservice.go`):** +- Server must be `STOPPED` (same guard style as delete/update); status flips to + `INSTALLING` during the run, back to `STOPPED` on success, and on failure the + server dir is NOT wiped (unlike create) — status returns to `STOPPED` and the + error is returned; the old install keeps working. +- Looks up the provider from `rec.ProviderID`, requires it to implement the new + optional Lua entry point `update(ctx)`; `ctx.version` = new id, + `ctx.old_version` = stored `ProviderVersion`. Progress streams through the + same `newProgressSender` + install log as create. +- On success: store new `ProviderVersion`, update `McVersion`/`JavaMajor`/launch + args from the returned spec (same post-install bookkeeping as create). + +**`ftb.lua` `update(ctx)`:** +- Fetch old + new version manifests. Diff server-side files by `path+name`: + - present only in old → delete (via `jhmc.fs` remove). + - present in both with different `sha1` → re-download. + - present only in new → download. + - user files (configs they edited that the pack doesn't own, world, etc.) are + untouched because only manifest-listed paths are considered. A pack file + the user edited (sha1 differs from BOTH old and new manifest) is + overwritten with the new pack version — pack files belong to the pack. +- Downloads go through `download_all`. +- If the modloader target (name or version) changed: re-run the loader install + (existing `install_loader`); else keep existing launch args. +- Returns the same spec shape as `install` (including `pack_version`). + +**App:** +- `ServerActionsFlyout` (wherever 伺服器動作 menu is built): add + "更新模組包…" / "Update modpack…", visible iff `Server.ProviderVersion != ""`, + enabled iff server stopped. +- Dialog lists versions from the existing `Shop.GetVersions(shopId, projectId)` + (shopId = `Server.ProviderId`, projectId = pack half of `ProviderVersion`), + newest first, current one marked, older ones allowed (downgrade) but the + current one disabled. +- Confirm → hand off to a new `MainViewModel.UpdateModpackAsync(server, version)` + that streams into the global `ProgressService` tracker exactly like + `InstallServerAsync` (step text, log lines, error detail, `RefreshAsync`). + +## 4. Export modpack + +**Proto:** `rpc ExportModpack(ExportModpackRequest) returns (stream InstallProgress)`; +request = `{ string id; string dest_path; }` (absolute zip path picked by the +user, following the existing `ExportAll` dest_path pattern). + +**Engine:** Go-side (not Lua — needs no provider polymorphism yet; FTB-only, +gated on `rec.ProviderID == "ftb"` + stored `ProviderVersion`): +- Refetch the pack version manifest for `ProviderVersion`. +- Build CurseForge pack zip: + - `manifest.json`: `manifestType: "minecraftModpack"`, `manifestVersion: 1`, + pack name (server name), `version` (pack version name), `minecraft` + `{ version, modLoaders: [{ id: "neoforge-"|"forge-"|"fabric-", primary: true }] }`, + `files[]` = every manifest file with `curseforge` project/file ids — + **including `clientonly` ones** — as `{ projectID, fileID, required: true }`, + `overrides: "overrides"`. + - `overrides/`: the server's live config-ish dirs (`config/`, `defaultconfigs/`, + `kubejs/`, `scripts/`, `resourcepacks/`, `shaderpacks/` — those that exist), + plus server-local mod jars NOT covered by a CF manifest entry. Excluded + always: `world*/`, `logs/`, `backups/`, `libraries/`, `crash-reports/`, + `.jhmc*`, `server.properties`, `eula.txt`, `*.jar` at root (loader/installer). + - `clientonly` files without CF ids (direct-URL) are downloaded at export time + into `overrides/` at their manifest path — hence the progress stream + (`shop.install.downloading` steps reuse existing localization). +- Progress: manifest fetch → client-only downloads (fraction) → zipping → done. +- No CurseForge API key needed (only CF-id references, no CF downloads). + +**App:** "匯出模組包…" / "Export modpack…" in the same flyout (visible iff +`ProviderVersion != ""`; works while running — read-only). `FileSavePicker` +(`.zip`, suggested name `-.zip`) → stream into the global +tracker with a distinct step key (`shop.export.*`). + +## Localization + +New keys (en-US + zh-TW): flyout items (`ServerAction_UpdateModpack`, +`ServerAction_ExportModpack`), update-dialog title/body/confirm, version-list +"current" badge, export step keys (`shop.export.preparing`, `shop.export.zipping`, +`shop.export.done`), tracker labels for update/export runs. + +## Testing + +- Go: unit tests for `download_all` (worker pool, resolve chain, error cancels + batch, checksum mismatch), manifest diff (delete/changed/new/user-file cases), + CF manifest.json builder (clientonly included, overrides exclusions). +- Live verify: install a pack (should be visibly faster), update it to an + adjacent version, export it and inspect the zip's manifest.json + overrides. + +## Out of scope + +- Update/export for non-FTB modpack sources (design leaves room: `update()` is + a generic provider entry point; export is FTB-gated in the engine for now). +- `.mrpack` export, server-pack export, config-only export. +- Parallelizing non-modpack providers' downloads (they're single large files). From 2b4b7efd534474e75055b49af403b54bc90038d6 Mon Sep 17 00:00:00 2001 From: 000hen Date: Sat, 11 Jul 2026 22:14:46 +0800 Subject: [PATCH 04/30] docs: implementation plan for FTB parallel downloads, update and export Co-Authored-By: Claude Fable 5 --- .../2026-07-11-ftb-parallel-update-export.md | 525 ++++++++++++++++++ 1 file changed, 525 insertions(+) create mode 100644 docs/superpowers/plans/2026-07-11-ftb-parallel-update-export.md diff --git a/docs/superpowers/plans/2026-07-11-ftb-parallel-update-export.md b/docs/superpowers/plans/2026-07-11-ftb-parallel-update-export.md new file mode 100644 index 0000000..5c89963 --- /dev/null +++ b/docs/superpowers/plans/2026-07-11-ftb-parallel-update-export.md @@ -0,0 +1,525 @@ +# FTB Parallel Downloads + Modpack Update/Export Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Speed up FTB modpack installs with a parallel batch-download host function, and add "update modpack" (manifest diff) and "export modpack" (CurseForge client zip) for modpack servers. + +**Architecture:** A new Go host function `jhmc.download_all` gives single-threaded Lua a worker pool; the pack identity (`packId/versionId`) is persisted on the server record so two new streaming RPCs (`UpdateModpack`, `ExportModpack`) can act on it. The app adds two flyout items on the server header, streaming both operations through the existing global install-progress tracker. + +**Tech Stack:** Go (engine, gopher-lua host), Lua (ftb.lua provider), protobuf/buf, WinUI 3 C# (app). + +**Spec:** `docs/superpowers/specs/2026-07-11-ftb-parallel-update-export-design.md` + +## Global Constraints + +- Go tests: `go test ./...` from `engine/`. App build: `dotnet build app\JustHostMC.App\JustHostMC.App.csproj -p:Platform=x64`. +- After editing `proto/mcmanager/v1/mcmanager.proto`: run `buf generate` from `proto/`. If Go later reports `undefined: mcmanagerv1.X`, a background VS build clobbered the stubs — just re-run `buf generate`. +- All edited `.xaml` files MUST keep their UTF-8 BOM. +- No `[ObservableProperty]` partial properties in C# (breaks the XAML compiler); use plain properties/`SetProperty`. +- Localized strings go in BOTH `app\JustHostMC.App\Strings\en-US\Resources.resw` and `app\JustHostMC.App\Strings\zh-TW\Resources.resw` (folder is `zh-TW`, not zh-Hant). +- gRPC stream sends are NOT concurrency-safe: any concurrent progress emission must be serialized with a mutex. +- Commit after each task; message prefix `feat:`/`test:` as appropriate, ending with the Claude Co-Authored-By trailer. + +--- + +### Task 1: Proto — provider_version field + UpdateModpack/ExportModpack RPCs + +**Files:** +- Modify: `proto/mcmanager/v1/mcmanager.proto` + +**Interfaces (produces):** +- `Server.provider_version = 11` (string) +- `message UpdateModpackRequest { string id = 1; string version = 2; }` +- `message ExportModpackRequest { string id = 1; string dest_path = 2; }` +- `rpc UpdateModpack(UpdateModpackRequest) returns (stream InstallProgress);` +- `rpc ExportModpack(ExportModpackRequest) returns (stream InstallProgress);` + +- [ ] **Step 1: Edit the proto.** In `message Server` (line ~36) add after `loader = 10`: +```proto + // Opaque provider pack version ("packId/versionId") for modpack servers; + // empty for normal servers. Non-empty means update/export are available. + string provider_version = 11; +``` +After `UpdateServerRequest` (line ~66) add: +```proto +message UpdateModpackRequest { + string id = 1; + string version = 2; // new opaque "packId/versionId" +} +message ExportModpackRequest { + string id = 1; + string dest_path = 2; // absolute .zip path chosen by the user +} +``` +In `service ServerService` (line ~748) add after `rpc Update`: +```proto + rpc UpdateModpack(UpdateModpackRequest) returns (stream InstallProgress); // manifest-diff move to another pack version + rpc ExportModpack(ExportModpackRequest) returns (stream InstallProgress); // CurseForge client pack zip +``` +- [ ] **Step 2: Regenerate stubs.** Run from `proto/`: `buf generate`. Expected: exit 0, `engine/gen/...` updated. +- [ ] **Step 3: Engine still compiles.** Run from `engine/`: `go build ./...`. Expected: exit 0 (new RPCs unimplemented is fine — the generated server uses `mustEmbedUnimplemented`). +- [ ] **Step 4: Commit** `feat(proto): provider_version + UpdateModpack/ExportModpack RPCs`. + +--- + +### Task 2: Persist pack identity end-to-end + +**Files:** +- Modify: `engine/internal/provider/provider.go` (LaunchSpec, ~line 19) +- Modify: `engine/internal/scripting/host.go` (`invocation.install`, ~line 225) +- Modify: `engine/internal/store/store.go` (Server struct + Proto, lines 15-45) +- Modify: `engine/internal/grpc/serverservice.go` (Create bookkeeping, ~line 188) +- Modify: `engine/internal/scripting/builtin/ftb.lua` (install return, ~line 265) +- Test: `engine/internal/scripting/luaprovider_test.go` (extend the existing `TestInstallLaunchSpecReadsMcVersionAndLoader` pattern) + +**Interfaces:** +- Produces: `provider.LaunchSpec.PackVersion string`; `store.Server.ProviderVersion string`; Lua spec key `pack_version`. + +- [ ] **Step 1: Write the failing test** — copy `TestInstallLaunchSpecReadsMcVersionAndLoader`, have the script return `pack_version = "95/12695"`, assert `spec.PackVersion == "95/12695"`. +- [ ] **Step 2: Run it** — `go test ./internal/scripting/ -run PackVersion -v` → FAIL (unknown field). +- [ ] **Step 3: Implement.** + - `provider.go` LaunchSpec: add `PackVersion string // opaque "packId/versionId" for modpack providers; empty otherwise`. + - `host.go` install() return: add `PackVersion: strField(spec, "pack_version"),`. + - `store.go` Server: add `ProviderVersion string`; Proto(): add `ProviderVersion: s.ProviderVersion,`. + - `serverservice.go` Create, next to `rec.Loader = spec.Loader` (~line 194): `rec.ProviderVersion = spec.PackVersion`. + - `ftb.lua` install() return table: add `pack_version = tostring(ctx.version),`. +- [ ] **Step 4: Test passes** — `go test ./internal/scripting/ ./internal/store/... ./internal/grpc/... ` → PASS. +- [ ] **Step 5: Commit** `feat(engine): persist modpack pack identity on the server record`. + +--- + +### Task 3: `jhmc.download_all` host function (parallel worker pool) + +**Files:** +- Modify: `engine/internal/scripting/hostfuncs.go` (register next to `download`, ~line 283; registration table near the `fs` table, look for `RawSetString("download", ...)`) +- Test: `engine/internal/scripting/hostfuncs_download_all_test.go` (new) + +**Interfaces:** +- Produces (Lua): `jhmc.download_all(items, opts?)`; + item = `{ dest = "mods/x.jar", sha1?, sha256?, url = "https://…" }` OR + `{ dest, sha1?, resolve = { url = "https://…", headers = { ["x-api-key"] = key } } }`; + opts = `{ concurrency = 6 }` (cap 16). Returns nothing; raises on first error. +- `resolve` semantics: GET the URL with headers, expect JSON `{ "data": "" }` + (the CurseForge download-url convention); 403 maps to the author-disallows message. + +- [ ] **Step 1: Write failing tests** (httptest server; use the package's existing test helpers for building an `invocation` — see how `hostfuncs` tests construct a Host): + - N files download concurrently and all land at their dests with correct content. + - A `resolve` item fetches the JSON endpoint and downloads `data`. + - One 404 item fails the whole call, error names the dest, and the context cancels in-flight work (assert with a slow handler + elapsed-time bound). + - `sha1` mismatch fails with `provider.ErrChecksumMismatch`. + - Progress: emitted fractions are `completed/total` and monotonically observed count == number of files. +- [ ] **Step 2: Run** — `go test ./internal/scripting/ -run DownloadAll -v` → FAIL (no `download_all`). +- [ ] **Step 3: Implement** in `hostfuncs.go`: +```go +type batchItem struct { + dest, full, url string // full = resolved absolute path + resolveURL string + resolveHeaders map[string]string + h func() hash.Hash // sha constructor or nil + want string +} + +// downloadAll backs jhmc.download_all: everything Lua-facing (arg parsing, +// resolvePath) happens on the Lua thread up front; workers are pure Go. +func (inv *invocation) downloadAll(L *lua.LState) int { + inv.require(L, mcmanagerv1.PermissionKind_PERMISSION_NETWORK) + itemsTbl := L.CheckTable(1) + concurrency := 6 + if opts, ok := L.Get(2).(*lua.LTable); ok { + if n, ok := opts.RawGetString("concurrency").(lua.LNumber); ok && int(n) > 0 { + concurrency = min(int(n), 16) + } + } + var items []batchItem + var parseErr error + itemsTbl.ForEach(func(_, v lua.LValue) { /* build batchItem; resolvePath(L, dest); + strField url/sha1/sha256; nested resolve table url+headers; set parseErr on bad item */ }) + if parseErr != nil { inv.fail(L, parseErr); return 0 } + + total := len(items) + var done atomic.Int64 + var emitMu sync.Mutex + emit := func(p provider.Progress) { emitMu.Lock(); inv.emit(p); emitMu.Unlock() } + + g, ctx := errgroup.WithContext(inv.ctx) + g.SetLimit(concurrency) + for _, it := range items { + it := it + g.Go(func() error { + url := it.url + if url == "" { // resolve chain + u, err := resolveDownloadURL(ctx, &inv.host.client, it.resolveURL, it.resolveHeaders) + if err != nil { return fmt.Errorf("%s: %w", it.dest, err) } + url = u + } + var h hash.Hash + if it.h != nil { h = it.h() } + sum, _, err := dl.Download(ctx, &inv.host.client, url, it.full, h, nil) + if err != nil { return fmt.Errorf("%s: %w", it.dest, err) } + if it.want != "" && !strings.EqualFold(sum, it.want) { + return fmt.Errorf("%s: %w (got %s want %s)", it.dest, provider.ErrChecksumMismatch, sum, it.want) + } + n := done.Add(1) + emit(provider.Progress{Step: "shop.install.downloading", + Fraction: float64(n) / float64(total), LogLine: path.Base(it.dest)}) + return nil + }) + } + if err := g.Wait(); err != nil { inv.fail(L, err); return 0 } + return 0 +} +``` + `resolveDownloadURL` (same file): GET with headers via `http.NewRequestWithContext`, 403 → `errors.New("CurseForge denied downloading (the author disallows third-party downloads)")`, non-2xx → status error, decode `{Data string}` and require non-empty. Register: `jhmcTbl.RawSetString("download_all", L.NewFunction(inv.downloadAll))` next to the existing `download` registration. Add `golang.org/x/sync/errgroup` to go.mod if absent (`go get golang.org/x/sync/errgroup`). + Note: per-byte progress is intentionally NOT forwarded (pass `nil` to `dl.Download`) — per-file completion only, so fractions don't interleave across workers. +- [ ] **Step 4: Tests pass** — `go test ./internal/scripting/ -run DownloadAll -v` → PASS. +- [ ] **Step 5: Commit** `feat(engine): jhmc.download_all parallel batch download host function`. + +--- + +### Task 4: ftb.lua uses download_all + +**Files:** +- Modify: `engine/internal/scripting/builtin/ftb.lua` (`download_file`/`install_files`, lines 66-118) +- Test: `engine/internal/scripting/builtin_ftb_test.go` if a test harness for the script exists (check for existing ftb.lua tests; else covered by Task 3 unit tests + live verify) + +- [ ] **Step 1: Replace `install_files`** (keep `join_path`; `download_file` is deleted): +```lua +-- install_files downloads every server-side file in the manifest through one +-- parallel batch; CurseForge-hosted files resolve their real URL inside the +-- batch workers. +local function install_files(ctx, files) + local key = curseforge_key(ctx) + local items = {} + for _, f in ipairs(files) do + if type(f) == "table" and not f.clientonly and (f.name or "") ~= "" then + local dest = join_path(f.path, f.name) + if (f.url or "") ~= "" then + items[#items + 1] = { dest = dest, sha1 = f.sha1, url = f.url } + else + local cf = f.curseforge + if type(cf) == "table" and cf.project and cf.file then + if key == "" then + error("install failed: file " .. (f.name or dest) .. + " is hosted on CurseForge and needs a CurseForge API key (set it in the FTB provider settings)") + end + items[#items + 1] = { dest = dest, sha1 = f.sha1, resolve = { + url = "https://api.curseforge.com/v1/mods/" .. cf.project .. + "/files/" .. cf.file .. "/download-url", + headers = { ["x-api-key"] = key }, + } } + else + error("install failed: file " .. (f.name or dest) .. " has no download source") + end + end + end + end + if #items == 0 then return end + ctx.step("shop.install.downloading", 0) + jhmc.download_all(items) +end +``` +- [ ] **Step 2: Engine tests still green** — `go test ./...` from `engine/` → PASS. +- [ ] **Step 3: Commit** `feat(engine): parallel FTB modpack file downloads`. + +--- + +### Task 5: Lua `update()` contract + LuaProvider.Update + +**Files:** +- Modify: `engine/internal/provider/provider.go` (new interface) +- Modify: `engine/internal/scripting/host.go` (new `invocation.update`, mirror `install` at line 177) +- Modify: `engine/internal/scripting/luaprovider.go` (new method) +- Test: `engine/internal/scripting/luaprovider_test.go` + +**Interfaces (produces):** +```go +// provider.go +// Updater is optionally implemented by providers that can move an installed +// server to another version in place (modpack manifest diff). +type Updater interface { + Update(ctx context.Context, dir, version, oldVersion string, progress func(Progress)) (LaunchSpec, error) +} +var ErrUpdateUnsupported = errors.New("provider does not support update") +``` +- Lua side: optional global `update(ctx)`; ctx has everything install's ctx has plus `ctx.old_version`. + +- [ ] **Step 1: Failing tests** — script with `update(ctx)` returning a spec echoing `ctx.old_version` in a field; assert `LuaProvider.Update` passes both versions and parses the spec (incl. `pack_version`). Script WITHOUT `update` → `ErrUpdateUnsupported`. +- [ ] **Step 2: Run** — FAIL (no Update method). +- [ ] **Step 3: Implement.** `host.go`: `func (inv *invocation) update(src, dir, version, oldVersion string) (provider.LaunchSpec, error)` — copy of `install` with: global name `"update"`, missing-function returns `provider.ErrUpdateUnsupported`, and `ictx.RawSetString("old_version", lua.LString(oldVersion))`. Extract the shared ctx-table construction and spec parsing into helpers (`installCtxTable`, `parseLaunchSpec`) used by both, so the two entry points can't drift. `luaprovider.go`: +```go +// Update implements provider.Updater. +func (p *LuaProvider) Update(ctx context.Context, dir, version, oldVersion string, progress func(provider.Progress)) (provider.LaunchSpec, error) { + inv := &invocation{ctx: ctx, host: p.host, granted: p.grants(), report: progress, assetDir: p.assetDir, config: p.config()} + return inv.update(p.source, dir, version, oldVersion) +} +``` +- [ ] **Step 4: Tests pass; Step 5: Commit** `feat(engine): optional update() provider entry point`. + +--- + +### Task 6: ftb.lua `update()` — manifest diff + +**Files:** +- Modify: `engine/internal/scripting/builtin/ftb.lua` (append after `install`) + +- [ ] **Step 1: Implement** (reuses `read_targets`, `join_path`, `install_files`, `install_loader`): +```lua +-- server_files indexes a manifest's server-side files by relative dest path. +local function server_files(manifest) + local by_dest = {} + for _, f in ipairs(manifest.files or {}) do + if type(f) == "table" and not f.clientonly and (f.name or "") ~= "" then + by_dest[join_path(f.path, f.name)] = f + end + end + return by_dest +end + +-- update moves an installed pack to another version: files only the old +-- version listed are deleted, new/changed ones are downloaded, and the loader +-- is reinstalled only when its pinned version changed. Files the pack never +-- listed (world, user configs) are untouched. +function update(ctx) + local pack, ver = tostring(ctx.version):match("^([^/]+)/([^/]+)$") + local opack, over = tostring(ctx.old_version):match("^([^/]+)/([^/]+)$") + if not pack or not ver then error("invalid modpack version id: " .. tostring(ctx.version)) end + if not opack or not over then error("invalid modpack version id: " .. tostring(ctx.old_version)) end + if pack ~= opack then error("update must stay within the same pack (" .. opack .. " -> " .. pack .. ")") end + + ctx.step("install.progress.resolving_version", -1) + local old_manifest = jhmc.http_json(FTB_API .. "/modpack/" .. opack .. "/" .. over) + local new_manifest = jhmc.http_json(FTB_API .. "/modpack/" .. pack .. "/" .. ver) + if type(old_manifest) ~= "table" or type(new_manifest) ~= "table" then + error("ftb: bad manifest") + end + + local old_files, new_files = server_files(old_manifest), server_files(new_manifest) + + -- Delete files the new version dropped. + for dest in pairs(old_files) do + if not new_files[dest] and jhmc.fs.exists(dest) then + ctx.log("- " .. dest) + jhmc.fs.remove(dest) + end + end + + -- Download new files and files whose pack hash changed. + local changed = {} + for dest, f in pairs(new_files) do + local old = old_files[dest] + if not old or (old.sha1 or "") ~= (f.sha1 or "") then + changed[#changed + 1] = f + end + end + install_files(ctx, changed) + + local mc, loader_name, loader_version = read_targets(new_manifest) + if not mc or mc == "" then error("ftb: modpack has no Minecraft version target") end + if not loader_name or loader_name == "" then error("ftb: modpack has no modloader target") end + local omc, oloader_name, oloader_version = read_targets(old_manifest) + + local java_major = jhmc.java_major_for(mc) + local args + if loader_name ~= oloader_name or loader_version ~= oloader_version or mc ~= omc then + args = install_loader(ctx, loader_name, loader_version, mc, java_major) + end + + ctx.step("install.progress.done", 1) + local spec = { + java_major = java_major, + mc_version = mc, + loader = loader_name, + pack_version = tostring(ctx.version), + } + spec.args = args -- nil = keep existing launch args (engine side) + return spec +end +``` + Note: `install_files` sorts nothing; changed-list order doesn't matter. `jhmc.fs.remove` exists (hostfuncs.go:368). +- [ ] **Step 2: Engine tests green** — `go test ./...` → PASS. **Step 3: Commit** `feat(engine): ftb.lua manifest-diff update()`. + +--- + +### Task 7: ServerService.UpdateModpack RPC + +**Files:** +- Modify: `engine/internal/grpc/serverservice.go` (new method after `Update`, ~line 310) +- Test: `engine/internal/grpc/serverservice_update_test.go` (extend; see `TestUpdateVersionRefreshesLaunchSpec` for the harness) + +**Interfaces:** +- Consumes: `provider.Updater`, `store.Server.ProviderVersion`, `newProgressSender`, `openInstallLog`, `maxJavaMajor`, `mapInstallError`. + +- [ ] **Step 1: Failing tests** (fake provider implementing Updater): happy path updates `ProviderVersion`/`McVersion`/status back to STOPPED; keeps old `LaunchArgs` when spec.Args is nil, replaces when set; rejects RUNNING server (`FailedPrecondition`); rejects server with empty `ProviderVersion`; provider error → status back to STOPPED, dir NOT deleted, record intact. +- [ ] **Step 2: Run** — FAIL. **Step 3: Implement:** +```go +// UpdateModpack moves a modpack server to another pack version in place, +// streaming progress. Unlike Create, a failure leaves the existing install +// untouched (no cleanup wipe) — the old version keeps working. +func (s *ServerService) UpdateModpack(req *mcmanagerv1.UpdateModpackRequest, stream grpc.ServerStreamingServer[mcmanagerv1.InstallProgress]) error { + ctx := stream.Context() + rec, ok := s.cfg.Store.Get(req.Id) + if !ok { return status.Error(codes.NotFound, "server not found") } + if rec.Status != mcmanagerv1.ServerStatus_STOPPED { + return status.Error(codes.FailedPrecondition, "server must be stopped") + } + if rec.ProviderVersion == "" { + return status.Error(codes.FailedPrecondition, "server was not installed from a modpack") + } + newVersion := strings.TrimSpace(req.Version) + if newVersion == "" { return status.Error(codes.InvalidArgument, "version is required") } + entry, ok := s.cfg.Providers.Get(rec.ProviderID) + if !ok { return status.Errorf(codes.Unimplemented, "provider %q not installed", rec.ProviderID) } + up, ok := entry.Provider.(provider.Updater) + if !ok { return status.Error(codes.Unimplemented, "provider does not support update") } + + oldVersion := rec.ProviderVersion + rec.Status = mcmanagerv1.ServerStatus_INSTALLING + _ = s.cfg.Store.Put(rec) + restore := func() { rec.Status = mcmanagerv1.ServerStatus_STOPPED; _ = s.cfg.Store.Put(rec) } + + il := s.openInstallLog(rec.ID) + defer il.Close() + il.recordLine("[update] " + oldVersion + " -> " + newVersion) + base := newProgressSender(stream) + send := func(p provider.Progress) { il.record(p); base(p) } + + spec, err := up.Update(ctx, s.cfg.Paths.ServerDir(rec.ID), newVersion, oldVersion, send) + if err != nil { + send(provider.Progress{LogLine: "[error] " + err.Error()}) + il.recordLine("[error] update: " + err.Error()) + restore() + return mapInstallError(err) + } + resolved := rec.McVersion + if spec.McVersion != "" { resolved = spec.McVersion } + spec.JavaMajor = maxJavaMajor(spec.JavaMajor, resolved) + if _, err := s.cfg.JRE.EnsureJRE(ctx, spec.JavaMajor, send); err != nil { + send(provider.Progress{LogLine: "[error] " + err.Error()}) + restore() + return mapInstallError(err) + } + rec.JavaMajor = spec.JavaMajor + if len(spec.Args) > 0 { rec.LaunchArgs = spec.Args } + if spec.McVersion != "" { rec.McVersion = spec.McVersion } + if spec.Loader != "" { rec.Loader = spec.Loader } + if spec.PackVersion != "" { rec.ProviderVersion = spec.PackVersion } + rec.Status = mcmanagerv1.ServerStatus_STOPPED + _ = s.cfg.Store.Put(rec) + send(provider.Progress{Step: "install.progress.done", Fraction: 1}) + return nil +} +``` +- [ ] **Step 4: Tests pass. Step 5: Commit** `feat(engine): UpdateModpack RPC (manifest-diff modpack update)`. + +--- + +### Task 8: ExportModpack — CurseForge zip builder + RPC + +**Files:** +- Create: `engine/internal/modpack/export.go` + `export_test.go` (pure logic, testable without gRPC) +- Modify: `engine/internal/grpc/serverservice.go` (thin RPC wrapper) + +**Interfaces (produces):** +```go +// package modpack +// Export builds a CurseForge-format client pack zip for an FTB pack install. +func Export(ctx context.Context, client *http.Client, o Options, progress func(provider.Progress)) error +type Options struct { + ServerDir string + DestZip string // absolute .zip + PackVersion string // "packId/versionId" + ServerName string + FTBAPIBase string // default https://api.feed-the-beast.com/v1/modpacks/public; overridable in tests +} +``` + +- [ ] **Step 1: Failing tests** (httptest FTB API fixture + temp server dir): + - `manifest.json` content: manifestType/manifestVersion/name/version, `minecraft.version`, `modLoaders[0].id` formatted `"-"` with the mc prefix stripped (reuse the ftb.lua `strip_mc_prefix` rule in Go), `files[]` includes CF entries for BOTH server and `clientonly` files, `overrides: "overrides"`. + - Overrides content: existing `config/`, `defaultconfigs/`, `kubejs/`, `scripts/`, `resourcepacks/`, `shaderpacks/` copied; `world/`, `logs/`, `backups/`, `libraries/`, `crash-reports/`, root `*.jar`, `server.properties`, `eula.txt`, `.jhmc*` excluded; local `mods/*.jar` NOT matched by a CF manifest entry included. + - `clientonly` file with direct `url` and no CF ids → downloaded into `overrides//`. + - Progress emits `shop.export.preparing` → downloading fractions → `shop.export.zipping` → done. +- [ ] **Step 2: Run** — FAIL. **Step 3: Implement `export.go`:** fetch manifest JSON (struct with `Name`, `Targets []{Type,Name,Version}`, `Files []{Path,Name,URL,SHA1,Clientonly bool, Curseforge *{Project,File int64}}`); build the manifest.json struct; identify CF-covered mod filenames (set of `Name` where `Curseforge != nil`); walk allow-listed dirs + `mods/` (skipping CF-covered jars); download client-only direct-URL files to a temp staging dir; stream everything into `zip.NewWriter` (forward-slash paths, `overrides/` prefix). Serialize progress with a mutex if downloads are parallel (reuse `errgroup` pattern from Task 3, concurrency 6). +- [ ] **Step 4: Tests pass** — `go test ./internal/modpack/ -v`. +- [ ] **Step 5: RPC wrapper** in `serverservice.go`: +```go +// ExportModpack writes a CurseForge-format client pack zip for a modpack +// server. Read-only with respect to the server dir; allowed while running. +func (s *ServerService) ExportModpack(req *mcmanagerv1.ExportModpackRequest, stream grpc.ServerStreamingServer[mcmanagerv1.InstallProgress]) error { + rec, ok := s.cfg.Store.Get(req.Id) + if !ok { return status.Error(codes.NotFound, "server not found") } + if rec.ProviderVersion == "" || rec.ProviderID != "ftb" { + return status.Error(codes.FailedPrecondition, "server was not installed from an FTB modpack") + } + dest := req.DestPath + if !filepath.IsAbs(dest) || !strings.EqualFold(filepath.Ext(dest), ".zip") { + return status.Error(codes.InvalidArgument, "dest_path must be an absolute .zip path") + } + send := newProgressSender(stream) + err := modpack.Export(stream.Context(), s.httpClient(), modpack.Options{ + ServerDir: s.cfg.Paths.ServerDir(rec.ID), DestZip: dest, + PackVersion: rec.ProviderVersion, ServerName: rec.Name, + }, send) + if err != nil { return status.Errorf(codes.Internal, "export: %v", err) } + send(provider.Progress{Step: "shop.export.done", Fraction: 1}) + return nil +} +``` + (`s.httpClient()`: check what ServerService already has; if nothing, add `HTTP *http.Client` to its cfg wired from `main.go` — same client the scripting host uses.) +- [ ] **Step 6: `go test ./...` green. Step 7: Commit** `feat(engine): ExportModpack RPC (CurseForge client pack zip)`. + +--- + +### Task 9: App — ProviderVersion + flyout items + dialogs + global-tracker streaming + +**Files:** +- Modify: `app/JustHostMC.App/Models/ServerItem.cs` (add `ProviderVersion` from proto — follow how `ProviderId` is mapped/applied) +- Modify: `app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml` (+ `.xaml.cs`) — two `MenuFlyoutItem`s in the existing flyout (after `ServerOpenInstanceFolderMenuItem`, line ~111): `x:Uid="ServerUpdateModpackMenuItem"` (Glyph ``) and `x:Uid="ServerExportModpackMenuItem"` (Glyph ``). Visibility bound to a code-behind helper `ModpackItemVisibility(Server)` (`ProviderVersion != ""`); update item additionally disabled unless stopped. **Preserve BOM.** +- Modify: `app/JustHostMC.App/ViewModels/MainViewModel.cs` — two new methods next to `InstallServerAsync` (line 174): + - `public async Task UpdateModpackAsync(ServerItem server, string version)` — clone of `InstallServerAsync`'s tracker/stream/error handling with `daemon.Servers.UpdateModpack(new UpdateModpackRequest { Id = server.Id, Version = version })`; tracker name = server name; on success `await RefreshAsync()`. + - `public async Task ExportModpackAsync(ServerItem server, string destPath)` — same shape over `ExportModpack`; no `RefreshAsync`; tracker `IsReadyToRun` stays false, `CurrentStep` = localized `shop.export.done` on completion. +- Modify: `app/JustHostMC.App/Views/ServerPage.xaml.cs` — handle the two new header-panel events (follow the existing `OnBackupsClick`/BackupsDialog wiring pattern in ServerHeaderPanel → ServerPage): + - **Update:** dialog (imperative, like `ShopDetailPage.CreateServerFlow`) that awaits `daemon.Shop.GetVersionsAsync(new ShopVersionsRequest { ShopId = server.ProviderId, ProjectId = packId })` where `packId = server.ProviderVersion.Split('/')[0]`; ComboBox of `ShopVersion.Name` newest-first; the entry whose `Id` == current `versionId` (`Split('/')[1]`) labeled with the "current" suffix and disabled; confirm → `_ = _main.UpdateModpackAsync(server, $"{packId}/{picked.Id}")`. + - **Export:** `FileSavePicker` (init with `WinRT.Interop.InitializeWithWindow` — copy the existing ExportAll picker usage, see `ModsViewModel.ExportAllAsync` call site), suggested filename `$"{server.Name}.zip"`, then `_ = _main.ExportModpackAsync(server, file.Path)`. + +- [ ] **Step 1: ServerItem.ProviderVersion** (map in ctor + `ApplyLocal`/merge path — mirror `ProviderId` handling). +- [ ] **Step 2: MainViewModel methods** (complete code mirroring lines 174-240, swapping the RPC call; keep `RunOnUI`, `MapErrorKey`, `ex.Status.Detail` handling identical). +- [ ] **Step 3: Flyout items + events + handlers** as above. +- [ ] **Step 4: Build** `dotnet build app\JustHostMC.App\JustHostMC.App.csproj -p:Platform=x64` → 0 errors. +- [ ] **Step 5: Commit** `feat(app): modpack update and export actions`. + +--- + +### Task 10: Localization + +**Files:** +- Modify: `app/JustHostMC.App/Strings/en-US/Resources.resw`, `app/JustHostMC.App/Strings/zh-TW/Resources.resw` + +- [ ] **Step 1: Add keys to BOTH files:** + +| Key | en-US | zh-TW | +|---|---|---| +| `ServerUpdateModpackMenuItem.Text` | Update modpack… | 更新模組包… | +| `ServerExportModpackMenuItem.Text` | Export modpack… | 匯出模組包… | +| `Server_UpdateModpackTitle` | Update modpack | 更新模組包 | +| `Server_UpdateModpackBody` | Pick a version for "{0}". The world and your own files are kept; pack files are replaced. | 為「{0}」選擇版本。世界與你自己的檔案會保留,模組包檔案將被更換。 | +| `Server_UpdateModpackConfirm` | Update | 更新 | +| `Server_UpdateModpackCurrent` | (current) | (目前版本) | +| `Server_UpdateModpackNoVersions` | No other versions available. | 沒有其他可用版本。 | +| `Server_ExportModpackPickerName` | CurseForge modpack | CurseForge 模組包 | +| `shop.export.preparing` | Preparing modpack export… | 正在準備匯出模組包… | +| `shop.export.zipping` | Packaging modpack… | 正在打包模組包… | +| `shop.export.done` | Modpack exported. | 模組包已匯出。 | + +- [ ] **Step 2: Build → 0 errors. Step 3: Commit** `feat(app): modpack update/export strings`. + +--- + +### Task 11: Full build + live verification + +- [ ] **Step 1:** `.\build.ps1` (runs buf generate + go build + go test + dotnet build + dotnet test) → all green. +- [ ] **Step 2: Live verify** (invoke the repo `verify` skill — `.claude/skills/verify/SKILL.md` has the launch/UIA recipe): + 1. Install a small FTB pack from the home-page modpack shop; compare wall-clock of the `shop.install.downloading` phase against the previous serial run (log timestamps in `%LOCALAPPDATA%\JustHostMC\logs\\install-*.log`) — expect a clear speedup. + 2. On the new server: actions flyout shows both items; Update lists versions with the current one disabled; pick the adjacent older version → global tracker streams; server flips back to stopped; `install-*.log` shows `[update]` line, deletions (`- path`) and downloads; world/eula untouched. + 3. Export → save zip → open it: `manifest.json` well-formed (spot-check `files[]` count vs manifest, `modLoaders[0].id`), `overrides/config/` present, no `world/`. + 4. Regression: normal (non-modpack) server shows neither flyout item; create-server and mod-shop installs still work. +- [ ] **Step 3: Commit** any fixes; final commit `feat: FTB parallel downloads, modpack update and export`. From 40b6d97112f0801426e2334cc3a042c5701145db Mon Sep 17 00:00:00 2001 From: 000hen Date: Sat, 11 Jul 2026 22:19:57 +0800 Subject: [PATCH 05/30] feat(proto): provider_version + UpdateModpack/ExportModpack RPCs Co-Authored-By: Claude Fable 5 --- proto/mcmanager/v1/mcmanager.proto | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/proto/mcmanager/v1/mcmanager.proto b/proto/mcmanager/v1/mcmanager.proto index d202675..04cac29 100644 --- a/proto/mcmanager/v1/mcmanager.proto +++ b/proto/mcmanager/v1/mcmanager.proto @@ -44,6 +44,9 @@ message Server { int32 sort_order = 8; string custom_java_args = 9; string loader = 10; // effective mod loader ("fabric"/"forge"/...); empty falls back to provider_id + // Opaque provider pack version ("packId/versionId") for modpack servers; + // empty for normal servers. Non-empty means update/export are available. + string provider_version = 11; } message CreateServerRequest { @@ -65,6 +68,15 @@ message UpdateServerRequest { string custom_java_args = 7; } +message UpdateModpackRequest { + string id = 1; + string version = 2; // new opaque "packId/versionId" +} +message ExportModpackRequest { + string id = 1; + string dest_path = 2; // absolute .zip path chosen by the user +} + // -- i18n / message strategy -------------------------------------------------- // Dynamic, display-only messages travel as a localization KEY in the form // "namespace.method.type" (e.g. "install.progress.downloading_server"); the @@ -796,6 +808,8 @@ service ServerService { rpc WatchChanges(Empty) returns (stream ServerChangeEvent); rpc Create(CreateServerRequest) returns (stream InstallProgress); // last message implies done rpc Update(UpdateServerRequest) returns (Server); + rpc UpdateModpack(UpdateModpackRequest) returns (stream InstallProgress); // manifest-diff move to another pack version + rpc ExportModpack(ExportModpackRequest) returns (stream InstallProgress); // CurseForge client pack zip rpc Start(ServerId) returns (Empty); rpc Stop(ServerId) returns (Empty); rpc Delete(ServerId) returns (Empty); From bd46e62258dc192a4d43d28297edd28d751dac05 Mon Sep 17 00:00:00 2001 From: 000hen Date: Sun, 12 Jul 2026 01:34:28 +0800 Subject: [PATCH 06/30] feat(engine): persist modpack pack identity on the server record ftb.lua echoes the opaque packId/versionId back as pack_version on the launch spec; the engine stores it (new provider_version SQLite column with migration) and returns it on the Server proto so the app can offer update/export for modpack servers. Co-Authored-By: Claude Fable 5 --- engine/internal/grpc/serverservice.go | 1 + engine/internal/provider/provider.go | 4 ++ engine/internal/scripting/builtin/ftb.lua | 1 + engine/internal/scripting/host.go | 9 ++-- engine/internal/scripting/luaprovider_test.go | 26 ++++++++++ engine/internal/store/sqlite.go | 21 +++++--- engine/internal/store/sqlite_test.go | 6 ++- engine/internal/store/store.go | 50 ++++++++++--------- 8 files changed, 81 insertions(+), 37 deletions(-) diff --git a/engine/internal/grpc/serverservice.go b/engine/internal/grpc/serverservice.go index 1c5080e..434da0c 100644 --- a/engine/internal/grpc/serverservice.go +++ b/engine/internal/grpc/serverservice.go @@ -259,6 +259,7 @@ func (s *ServerService) Create(req *mcmanagerv1.CreateServerRequest, stream grpc rec.McVersion = spec.McVersion } rec.Loader = spec.Loader + rec.ProviderVersion = spec.PackVersion rec.Status = mcmanagerv1.ServerStatus_STOPPED _ = s.cfg.Store.Put(rec) diff --git a/engine/internal/provider/provider.go b/engine/internal/provider/provider.go index ba64551..2d23e42 100644 --- a/engine/internal/provider/provider.go +++ b/engine/internal/provider/provider.go @@ -25,6 +25,10 @@ type LaunchSpec struct { // MC version here). Loader is the effective mod loader ("fabric"/"forge"/…). McVersion string Loader string + // PackVersion is the opaque provider pack version ("packId/versionId") a + // modpack provider echoes back so it can be persisted for later update and + // export; empty for normal providers. + PackVersion string } // JavaResolver resolves (downloading if needed) a java.exe for a Java major diff --git a/engine/internal/scripting/builtin/ftb.lua b/engine/internal/scripting/builtin/ftb.lua index 4dddc9f..76f3ffa 100644 --- a/engine/internal/scripting/builtin/ftb.lua +++ b/engine/internal/scripting/builtin/ftb.lua @@ -267,5 +267,6 @@ function install(ctx) args = args, mc_version = mc, loader = loader_name, + pack_version = tostring(ctx.version), } end diff --git a/engine/internal/scripting/host.go b/engine/internal/scripting/host.go index 6640816..5ec427a 100644 --- a/engine/internal/scripting/host.go +++ b/engine/internal/scripting/host.go @@ -223,10 +223,11 @@ func (inv *invocation) install(src, dir, version string) (provider.LaunchSpec, e }) } return provider.LaunchSpec{ - JavaMajor: major, - Args: args, - McVersion: strField(spec, "mc_version"), - Loader: strField(spec, "loader"), + JavaMajor: major, + Args: args, + McVersion: strField(spec, "mc_version"), + Loader: strField(spec, "loader"), + PackVersion: strField(spec, "pack_version"), }, nil } diff --git a/engine/internal/scripting/luaprovider_test.go b/engine/internal/scripting/luaprovider_test.go index 437af91..17bf610 100644 --- a/engine/internal/scripting/luaprovider_test.go +++ b/engine/internal/scripting/luaprovider_test.go @@ -154,6 +154,32 @@ end } } +// TestInstallLaunchSpecReadsPackVersion verifies a modpack provider can persist +// its opaque "packId/versionId" through the launch spec. +func TestInstallLaunchSpecReadsPackVersion(t *testing.T) { + script := ` +meta = { id = "packy", name = "Packy", mod_layout = "mods" } +function versions() return {} end +function install(ctx) + return { java_major = 21, args = { "-jar", "server.jar", "nogui" }, + mc_version = "1.20.1", loader = "neoforge", + pack_version = tostring(ctx.version) } +end +` + r := NewRegistry(NewHost(nil, nil, nil), nil) + e, err := r.AddSource(context.Background(), script, true) + if err != nil { + t.Fatalf("AddSource: %v", err) + } + spec, err := e.Provider.Install(context.Background(), t.TempDir(), "95/12695", nil) + if err != nil { + t.Fatalf("Install: %v", err) + } + if spec.PackVersion != "95/12695" { + t.Errorf("PackVersion = %q, want 95/12695", spec.PackVersion) + } +} + // TestUngrantedNetworkDenied proves a non-builtin script with no grants cannot // reach the network. func TestUngrantedNetworkDenied(t *testing.T) { diff --git a/engine/internal/store/sqlite.go b/engine/internal/store/sqlite.go index 87098da..eaf7cfe 100644 --- a/engine/internal/store/sqlite.go +++ b/engine/internal/store/sqlite.go @@ -25,7 +25,8 @@ CREATE TABLE IF NOT EXISTS servers ( sort_order INTEGER NOT NULL DEFAULT 0, java_major INTEGER NOT NULL, launch_args TEXT NOT NULL, - custom_java_args TEXT NOT NULL DEFAULT '' + custom_java_args TEXT NOT NULL DEFAULT '', + provider_version TEXT NOT NULL DEFAULT '' );` // SQLite is a Store persisted in a SQLite database file, so the registry @@ -61,21 +62,23 @@ func (s *SQLite) Put(srv *Server) error { return err } _, err = s.db.Exec(` - INSERT INTO servers (id, name, type, provider_id, mod_layout, mc_version, loader, memory_mb, port, status, sort_order, java_major, launch_args, custom_java_args) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO servers (id, name, type, provider_id, mod_layout, mc_version, loader, memory_mb, port, status, sort_order, java_major, launch_args, custom_java_args, provider_version) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET name=excluded.name, provider_id=excluded.provider_id, mod_layout=excluded.mod_layout, mc_version=excluded.mc_version, loader=excluded.loader, memory_mb=excluded.memory_mb, port=excluded.port, status=excluded.status, sort_order=excluded.sort_order, java_major=excluded.java_major, - launch_args=excluded.launch_args, custom_java_args=excluded.custom_java_args`, + launch_args=excluded.launch_args, custom_java_args=excluded.custom_java_args, + provider_version=excluded.provider_version`, srv.ID, srv.Name, 0, srv.ProviderID, modLayoutOrNone(srv.ModLayout), srv.McVersion, srv.Loader, - srv.MemoryMB, srv.Port, int(srv.Status), srv.SortOrder, srv.JavaMajor, string(args), srv.CustomJavaArgs) + srv.MemoryMB, srv.Port, int(srv.Status), srv.SortOrder, srv.JavaMajor, string(args), srv.CustomJavaArgs, + srv.ProviderVersion) return err } func (s *SQLite) Get(id string) (*Server, bool) { row := s.db.QueryRow(` - SELECT id, name, provider_id, mod_layout, mc_version, loader, memory_mb, port, status, sort_order, java_major, launch_args, custom_java_args + SELECT id, name, provider_id, mod_layout, mc_version, loader, memory_mb, port, status, sort_order, java_major, launch_args, custom_java_args, provider_version FROM servers WHERE id = ?`, id) srv, err := scanServer(row) if err != nil { @@ -86,7 +89,7 @@ func (s *SQLite) Get(id string) (*Server, bool) { func (s *SQLite) List() []*Server { rows, err := s.db.Query(` - SELECT id, name, provider_id, mod_layout, mc_version, loader, memory_mb, port, status, sort_order, java_major, launch_args, custom_java_args + SELECT id, name, provider_id, mod_layout, mc_version, loader, memory_mb, port, status, sort_order, java_major, launch_args, custom_java_args, provider_version FROM servers`) if err != nil { return nil @@ -124,7 +127,8 @@ func scanServer(sc rowScanner) (*Server, error) { argsJSON string ) if err := sc.Scan(&srv.ID, &srv.Name, &srv.ProviderID, &srv.ModLayout, &srv.McVersion, &srv.Loader, &srv.MemoryMB, - &srv.Port, &status, &srv.SortOrder, &srv.JavaMajor, &argsJSON, &srv.CustomJavaArgs); err != nil { + &srv.Port, &status, &srv.SortOrder, &srv.JavaMajor, &argsJSON, &srv.CustomJavaArgs, + &srv.ProviderVersion); err != nil { return nil, err } srv.Status = mcmanagerv1.ServerStatus(status) @@ -151,6 +155,7 @@ func ensureServerColumns(db *sql.DB) error { `ALTER TABLE servers ADD COLUMN provider_id TEXT NOT NULL DEFAULT ''`, `ALTER TABLE servers ADD COLUMN mod_layout TEXT NOT NULL DEFAULT 'none'`, `ALTER TABLE servers ADD COLUMN loader TEXT NOT NULL DEFAULT ''`, + `ALTER TABLE servers ADD COLUMN provider_version TEXT NOT NULL DEFAULT ''`, } { if _, err := db.Exec(stmt); err != nil { if !strings.Contains(strings.ToLower(err.Error()), "duplicate column") { diff --git a/engine/internal/store/sqlite_test.go b/engine/internal/store/sqlite_test.go index b0fcd6f..1b2342b 100644 --- a/engine/internal/store/sqlite_test.go +++ b/engine/internal/store/sqlite_test.go @@ -51,7 +51,8 @@ func TestSQLitePersistsAcrossReopen(t *testing.T) { t.Fatal(err) } _ = s1.Put(&Server{ID: "x", Name: "Persisted", ProviderID: "forge", ModLayout: "mods", - McVersion: "1.20.1", MemoryMB: 4096, Port: 25570, Status: mcmanagerv1.ServerStatus_RUNNING, + McVersion: "1.20.1", ProviderVersion: "95/12695", MemoryMB: 4096, Port: 25570, + Status: mcmanagerv1.ServerStatus_RUNNING, JavaMajor: 17, LaunchArgs: []string{"-jar", "forge.jar"}}) s1.Close() @@ -66,7 +67,8 @@ func TestSQLitePersistsAcrossReopen(t *testing.T) { t.Fatal("server not found after reopen") } if got.Name != "Persisted" || got.JavaMajor != 17 || got.MemoryMB != 4096 || - got.ProviderID != "forge" || len(got.LaunchArgs) != 2 { + got.ProviderID != "forge" || len(got.LaunchArgs) != 2 || + got.ProviderVersion != "95/12695" { t.Errorf("reopened server = %+v", got) } } diff --git a/engine/internal/store/store.go b/engine/internal/store/store.go index 662234d..fe1db68 100644 --- a/engine/internal/store/store.go +++ b/engine/internal/store/store.go @@ -13,34 +13,38 @@ import ( // Server is a registered server plus the launch details the engine needs to // start it. It carries proto enums directly to avoid an extra mapping layer. type Server struct { - ID string - Name string - ProviderID string - ModLayout string // "plugins" | "mods" | "none"; captured from the provider at create - McVersion string - Loader string // effective mod loader ("fabric"/"forge"/…); empty falls back to ProviderID - MemoryMB int - Port int - Status mcmanagerv1.ServerStatus - SortOrder int - JavaMajor int - LaunchArgs []string - CustomJavaArgs string + ID string + Name string + ProviderID string + ModLayout string // "plugins" | "mods" | "none"; captured from the provider at create + McVersion string + Loader string // effective mod loader ("fabric"/"forge"/…); empty falls back to ProviderID + // ProviderVersion is the opaque pack version ("packId/versionId") for + // modpack servers; empty otherwise. Non-empty enables update/export. + ProviderVersion string + MemoryMB int + Port int + Status mcmanagerv1.ServerStatus + SortOrder int + JavaMajor int + LaunchArgs []string + CustomJavaArgs string } // Proto projects a Server onto the wire type returned to the frontend. func (s *Server) Proto() *mcmanagerv1.Server { return &mcmanagerv1.Server{ - Id: s.ID, - Name: s.Name, - ProviderId: s.ProviderID, - McVersion: s.McVersion, - Loader: s.Loader, - MemoryMb: int32(s.MemoryMB), - Port: int32(s.Port), - Status: s.Status, - SortOrder: int32(s.SortOrder), - CustomJavaArgs: s.CustomJavaArgs, + Id: s.ID, + Name: s.Name, + ProviderId: s.ProviderID, + McVersion: s.McVersion, + Loader: s.Loader, + ProviderVersion: s.ProviderVersion, + MemoryMb: int32(s.MemoryMB), + Port: int32(s.Port), + Status: s.Status, + SortOrder: int32(s.SortOrder), + CustomJavaArgs: s.CustomJavaArgs, } } From 509edd15b742e79a5ff47b86b015a48d0f56f834 Mon Sep 17 00:00:00 2001 From: 000hen Date: Sun, 12 Jul 2026 01:37:51 +0800 Subject: [PATCH 07/30] feat(engine): jhmc.download_all parallel batch download host function Worker pool (default 6, cap 16) over errgroup; items carry a direct url or a resolve endpoint (CurseForge download-url convention) resolved inside the worker, so CF API round-trips parallelize too. First error cancels the batch; progress is per-file completion with serialized emission. Co-Authored-By: Claude Fable 5 --- engine/go.mod | 1 + engine/internal/scripting/hostfuncs.go | 155 ++++++++++++ .../scripting/hostfuncs_download_all_test.go | 220 ++++++++++++++++++ 3 files changed, 376 insertions(+) create mode 100644 engine/internal/scripting/hostfuncs_download_all_test.go diff --git a/engine/go.mod b/engine/go.mod index ed674c2..1cd9d84 100644 --- a/engine/go.mod +++ b/engine/go.mod @@ -7,6 +7,7 @@ require ( github.com/Microsoft/go-winio v0.6.2 github.com/Tnze/go-mc v1.20.2 github.com/yuin/gopher-lua v1.1.2 + golang.org/x/sync v0.20.0 golang.org/x/sys v0.46.0 google.golang.org/grpc v1.81.1 google.golang.org/protobuf v1.36.11 diff --git a/engine/internal/scripting/hostfuncs.go b/engine/internal/scripting/hostfuncs.go index 13e4946..9bb1da3 100644 --- a/engine/internal/scripting/hostfuncs.go +++ b/engine/internal/scripting/hostfuncs.go @@ -14,11 +14,15 @@ import ( "net/http" "os" "os/exec" + "path" "path/filepath" "strings" "sync" + "sync/atomic" "time" + "golang.org/x/sync/errgroup" + mcmanagerv1 "github.com/000hen/justhostmc/engine/gen/mcmanager/v1" "github.com/000hen/justhostmc/engine/internal/dl" "github.com/000hen/justhostmc/engine/internal/httpcache" @@ -39,6 +43,7 @@ func (inv *invocation) newJHMC(L *lua.LState) *lua.LTable { reg("http_json", inv.httpJSON) reg("http_cache", inv.httpCached) reg("download", inv.download) + reg("download_all", inv.downloadAll) reg("sha256", inv.sha256File) reg("unzip", inv.unzip) reg("run_jar", inv.runJar) @@ -318,6 +323,156 @@ func (inv *invocation) download(L *lua.LState) int { return 1 } +// batchItem is one download_all entry, fully resolved on the Lua thread so the +// worker goroutines never touch the Lua state. +type batchItem struct { + dest, full, url string + resolveURL string + resolveHeaders map[string]string + newHash func() hash.Hash // nil = no checksum + want string +} + +// downloadAll backs jhmc.download_all(items, opts): a parallel batch download +// with per-file completion progress. Items carry either a direct url or a +// resolve endpoint (GET returning {"data": ""}, the CurseForge +// download-url convention). The first error cancels the batch. +func (inv *invocation) downloadAll(L *lua.LState) int { + inv.require(L, mcmanagerv1.PermissionKind_PERMISSION_NETWORK) + itemsTbl := L.CheckTable(1) + concurrency := 6 + if opts, ok := L.Get(2).(*lua.LTable); ok { + if n, ok := opts.RawGetString("concurrency").(lua.LNumber); ok && int(n) > 0 { + concurrency = min(int(n), 16) + } + } + + var items []batchItem + var parseErr error + itemsTbl.ForEach(func(_, v lua.LValue) { + if parseErr != nil { + return + } + entry, ok := v.(*lua.LTable) + if !ok { + parseErr = fmt.Errorf("download_all: item is not a table") + return + } + it := batchItem{dest: strField(entry, "dest"), url: strField(entry, "url")} + if it.dest == "" { + parseErr = fmt.Errorf("download_all: item.dest is required") + return + } + it.full = inv.resolvePath(L, it.dest) // raises via fail on escape + if s := strField(entry, "sha256"); s != "" { + it.newHash, it.want = sha256.New, strings.ToLower(s) + } else if s := strField(entry, "sha1"); s != "" { + it.newHash, it.want = sha1.New, strings.ToLower(s) + } + if res, ok := entry.RawGetString("resolve").(*lua.LTable); ok { + it.resolveURL = strField(res, "url") + if hdrs, ok := res.RawGetString("headers").(*lua.LTable); ok { + it.resolveHeaders = map[string]string{} + hdrs.ForEach(func(k, hv lua.LValue) { + it.resolveHeaders[k.String()] = hv.String() + }) + } + } + if it.url == "" && it.resolveURL == "" { + parseErr = fmt.Errorf("download_all: %s has neither url nor resolve.url", it.dest) + return + } + items = append(items, it) + }) + if parseErr != nil { + inv.fail(L, parseErr) + return 0 + } + if len(items) == 0 { + return 0 + } + + total := len(items) + var done atomic.Int64 + // gRPC stream sends are not safe for concurrent use; serialize emission. + var emitMu sync.Mutex + emit := func(p provider.Progress) { + emitMu.Lock() + inv.emit(p) + emitMu.Unlock() + } + + g, ctx := errgroup.WithContext(inv.ctx) + g.SetLimit(concurrency) + for _, it := range items { + g.Go(func() error { + url := it.url + if url == "" { + u, err := resolveDownloadURL(ctx, &inv.host.client, it.resolveURL, it.resolveHeaders) + if err != nil { + return fmt.Errorf("%s: %w", it.dest, err) + } + url = u + } + var h hash.Hash + if it.newHash != nil { + h = it.newHash() + } + // Per-byte progress is intentionally dropped: fractions from + // parallel workers would interleave; completion counts are stable. + sum, _, err := dl.Download(ctx, &inv.host.client, url, it.full, h, nil) + if err != nil { + return fmt.Errorf("%s: %w", it.dest, err) + } + if it.want != "" && !strings.EqualFold(sum, it.want) { + return fmt.Errorf("%s: %w (got %s want %s)", it.dest, provider.ErrChecksumMismatch, sum, it.want) + } + emit(provider.Progress{Step: "shop.install.downloading", + Fraction: float64(done.Add(1)) / float64(total), LogLine: path.Base(it.dest)}) + return nil + }) + } + if err := g.Wait(); err != nil { + inv.fail(L, err) + return 0 + } + return 0 +} + +// resolveDownloadURL fetches a JSON endpoint whose "data" field is the real +// download URL (the CurseForge /download-url convention). +func resolveDownloadURL(ctx context.Context, client *http.Client, url string, headers map[string]string) (string, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return "", err + } + req.Header.Set("User-Agent", scriptUserAgent) + for k, v := range headers { + req.Header.Set(k, v) + } + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + if resp.StatusCode == http.StatusForbidden { + return "", fmt.Errorf("CurseForge denied downloading (the author disallows third-party downloads)") + } + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("GET %s: unexpected status %s", url, resp.Status) + } + var out struct { + Data string `json:"data"` + } + if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&out); err != nil { + return "", fmt.Errorf("resolve %s: %v", url, err) + } + if out.Data == "" { + return "", fmt.Errorf("resolve %s: empty download url", url) + } + return out.Data, nil +} + // -- filesystem (confined to the server dir) ---------------------------------- func (inv *invocation) requireFS(L *lua.LState) { diff --git a/engine/internal/scripting/hostfuncs_download_all_test.go b/engine/internal/scripting/hostfuncs_download_all_test.go new file mode 100644 index 0000000..eb562bc --- /dev/null +++ b/engine/internal/scripting/hostfuncs_download_all_test.go @@ -0,0 +1,220 @@ +package scripting + +import ( + "context" + "crypto/sha1" + "encoding/hex" + "fmt" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/000hen/justhostmc/engine/internal/provider" +) + +// downloadAllScript wraps a Lua install() body that calls jhmc.download_all. +func downloadAllScript(body string) string { + return fmt.Sprintf(` +meta = { id = "batch", name = "Batch", mod_layout = "mods", + permissions = { { kind = "network", reason = "test" } } } +function versions() return {} end +function install(ctx) +%s + return { java_major = 21, args = { "-jar", "s.jar" } } +end +`, body) +} + +func installBatch(t *testing.T, script, dir string, progress func(provider.Progress)) error { + t.Helper() + r := NewRegistry(NewHost(nil, nil, nil), nil) + e, err := r.AddSource(context.Background(), script, true) // builtin → network granted + if err != nil { + t.Fatalf("AddSource: %v", err) + } + _, err = e.Provider.Install(context.Background(), dir, "1.0", progress) + return err +} + +// TestDownloadAllDownloadsConcurrently proves N slow files complete in roughly +// one slot's time, land at their dests with the right bytes, and emit one +// completion progress line per file. +func TestDownloadAllDownloadsConcurrently(t *testing.T) { + const n = 6 + var inflight, peak atomic.Int64 + mux := http.NewServeMux() + mux.HandleFunc("/f/", func(w http.ResponseWriter, r *http.Request) { + cur := inflight.Add(1) + defer inflight.Add(-1) + for { + old := peak.Load() + if cur <= old || peak.CompareAndSwap(old, cur) { + break + } + } + time.Sleep(150 * time.Millisecond) + _, _ = w.Write([]byte("body-" + strings.TrimPrefix(r.URL.Path, "/f/"))) + }) + srv := httptest.NewServer(mux) + t.Cleanup(srv.Close) + + items := make([]string, 0, n) + for i := range n { + items = append(items, fmt.Sprintf(`{ dest = "mods/f%d.txt", url = %q }`, i, fmt.Sprintf("%s/f/%d", srv.URL, i))) + } + script := downloadAllScript(` jhmc.download_all({ ` + strings.Join(items, ", ") + ` })`) + + dir := t.TempDir() + var mu sync.Mutex + var logs []string + start := time.Now() + err := installBatch(t, script, dir, func(p provider.Progress) { + if p.LogLine != "" { + mu.Lock() + logs = append(logs, p.LogLine) + mu.Unlock() + } + }) + elapsed := time.Since(start) + if err != nil { + t.Fatalf("install: %v", err) + } + for i := range n { + b, err := os.ReadFile(filepath.Join(dir, "mods", fmt.Sprintf("f%d.txt", i))) + if err != nil || string(b) != fmt.Sprintf("body-%d", i) { + t.Errorf("f%d.txt content = %q err=%v", i, b, err) + } + } + if peak.Load() < 2 { + t.Errorf("peak concurrency = %d, want >= 2", peak.Load()) + } + if elapsed > 700*time.Millisecond { + t.Errorf("elapsed = %v, want well under serial 6*150ms + overhead", elapsed) + } + if len(logs) != n { + t.Errorf("completion log lines = %d (%v), want %d", len(logs), logs, n) + } +} + +// TestDownloadAllResolveChain proves a resolve item fetches the JSON endpoint +// (with headers) and downloads the URL in its data field. +func TestDownloadAllResolveChain(t *testing.T) { + mux := http.NewServeMux() + var gotKey atomic.Value + var base string + mux.HandleFunc("/resolve", func(w http.ResponseWriter, r *http.Request) { + gotKey.Store(r.Header.Get("x-api-key")) + fmt.Fprintf(w, `{"data":%q}`, base+"/real") + }) + mux.HandleFunc("/real", func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte("resolved-body")) + }) + srv := httptest.NewServer(mux) + base = srv.URL + t.Cleanup(srv.Close) + + script := downloadAllScript(fmt.Sprintf( + ` jhmc.download_all({ { dest = "mods/r.jar", resolve = { url = %q, headers = { ["x-api-key"] = "sekrit" } } } })`, + srv.URL+"/resolve")) + dir := t.TempDir() + if err := installBatch(t, script, dir, nil); err != nil { + t.Fatalf("install: %v", err) + } + if b, _ := os.ReadFile(filepath.Join(dir, "mods", "r.jar")); string(b) != "resolved-body" { + t.Errorf("resolved file content = %q", b) + } + if gotKey.Load() != "sekrit" { + t.Errorf("resolve header = %v, want sekrit", gotKey.Load()) + } +} + +// TestDownloadAllFirstErrorCancels proves one failing item fails the whole +// call, names the dest, and cancels in-flight downloads instead of waiting for +// them to finish. +func TestDownloadAllFirstErrorCancels(t *testing.T) { + mux := http.NewServeMux() + mux.HandleFunc("/bad", func(w http.ResponseWriter, _ *http.Request) { + http.Error(w, "nope", http.StatusNotFound) + }) + mux.HandleFunc("/slow", func(w http.ResponseWriter, r *http.Request) { + select { + case <-time.After(10 * time.Second): + case <-r.Context().Done(): + } + }) + srv := httptest.NewServer(mux) + t.Cleanup(srv.Close) + + script := downloadAllScript(fmt.Sprintf(` jhmc.download_all({ + { dest = "mods/slow.jar", url = %q }, + { dest = "mods/bad.jar", url = %q }, + })`, srv.URL+"/slow", srv.URL+"/bad")) + + start := time.Now() + err := installBatch(t, script, t.TempDir(), nil) + if err == nil { + t.Fatal("install succeeded, want error") + } + if !strings.Contains(err.Error(), "bad.jar") { + t.Errorf("error %q does not name the failing dest", err) + } + if elapsed := time.Since(start); elapsed > 5*time.Second { + t.Errorf("elapsed = %v; in-flight download was not canceled", elapsed) + } +} + +// TestDownloadAllChecksumMismatch proves a wrong sha1 fails the batch with the +// checksum sentinel. +func TestDownloadAllChecksumMismatch(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte("actual-bytes")) + })) + t.Cleanup(srv.Close) + + wrong := sha1.Sum([]byte("other-bytes")) + script := downloadAllScript(fmt.Sprintf( + ` jhmc.download_all({ { dest = "mods/c.jar", url = %q, sha1 = %q } })`, + srv.URL, hex.EncodeToString(wrong[:]))) + err := installBatch(t, script, t.TempDir(), nil) + if err == nil || !strings.Contains(err.Error(), "checksum") { + t.Fatalf("err = %v, want checksum mismatch", err) + } +} + +// TestDownloadAllProgressFractions proves fractions are completed/total. +func TestDownloadAllProgressFractions(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte("x")) + })) + t.Cleanup(srv.Close) + + script := downloadAllScript(fmt.Sprintf(` jhmc.download_all({ + { dest = "a.txt", url = %[1]q }, { dest = "b.txt", url = %[1]q }, + { dest = "c.txt", url = %[1]q }, { dest = "d.txt", url = %[1]q }, + }, { concurrency = 1 })`, srv.URL)) + + var fractions []float64 + err := installBatch(t, script, t.TempDir(), func(p provider.Progress) { + if p.LogLine != "" && p.Fraction >= 0 { + fractions = append(fractions, p.Fraction) + } + }) + if err != nil { + t.Fatalf("install: %v", err) + } + want := []float64{0.25, 0.5, 0.75, 1} + if len(fractions) != len(want) { + t.Fatalf("fractions = %v, want %v", fractions, want) + } + for i := range want { + if fractions[i] != want[i] { + t.Errorf("fractions[%d] = %v, want %v (all: %v)", i, fractions[i], want[i], fractions) + } + } +} From aadf4f21dd700c47a2d9cc8e345f7133c0de1a1d Mon Sep 17 00:00:00 2001 From: 000hen Date: Sun, 12 Jul 2026 01:38:51 +0800 Subject: [PATCH 08/30] feat(engine): parallel FTB modpack file downloads Co-Authored-By: Claude Fable 5 --- engine/internal/scripting/builtin/ftb.lua | 78 +++++++++-------------- 1 file changed, 30 insertions(+), 48 deletions(-) diff --git a/engine/internal/scripting/builtin/ftb.lua b/engine/internal/scripting/builtin/ftb.lua index 76f3ffa..a28ff2d 100644 --- a/engine/internal/scripting/builtin/ftb.lua +++ b/engine/internal/scripting/builtin/ftb.lua @@ -63,58 +63,40 @@ local function join_path(path, name) return dest end --- download_file fetches one manifest file to dest. Directly-hosted files carry a --- url; CurseForge-hosted files are resolved through the download-url endpoint --- with the configured key (files whose author disabled third-party downloads --- return 403). -local function download_file(ctx, f, dest) - if (f.url or "") ~= "" then - jhmc.download(f.url, { dest = dest, sha1 = f.sha1 }) - return - end - - local cf = f.curseforge - if type(cf) == "table" and cf.project and cf.file then - local key = curseforge_key(ctx) - if key == "" then - error("install failed: file " .. (f.name or dest) .. - " is hosted on CurseForge and needs a CurseForge API key (set it in the FTB provider settings)") - end - local res = jhmc.http({ - url = "https://api.curseforge.com/v1/mods/" .. cf.project .. - "/files/" .. cf.file .. "/download-url", - headers = { ["x-api-key"] = key }, - }) - if res.status == 403 then - error("install failed: CurseForge denied downloading " .. (f.name or dest) .. - " (the author disallows third-party downloads)") - end - if res.status < 200 or res.status >= 300 then - error("install failed: CurseForge API returned HTTP " .. res.status .. " for " .. (f.name or dest)) - end - local body = jhmc.json_decode(res.body) - local url = body and body.data - if type(url) ~= "string" or url == "" then - error("install failed: no download url for " .. (f.name or dest)) - end - jhmc.download(url, { dest = dest, sha1 = f.sha1 }) - return - end - - error("install failed: file " .. (f.name or dest) .. " has no download source") -end - --- install_files writes every server-side file in the manifest, reporting --- progress as a fraction of the file count. +-- install_files downloads every server-side file in the manifest through one +-- parallel jhmc.download_all batch. Directly-hosted files carry a url; +-- CurseForge-hosted ones resolve their real URL inside the batch workers via +-- the download-url endpoint (403 = the author disallows third-party +-- downloads). The missing-key check runs up front, before anything downloads. local function install_files(ctx, files) - local total = #files - for i, f in ipairs(files) do + local key = curseforge_key(ctx) + local items = {} + for _, f in ipairs(files) do if type(f) == "table" and not f.clientonly and (f.name or "") ~= "" then - ctx.step("shop.install.downloading", total > 0 and (i / total) or 0) - ctx.log(f.name) - download_file(ctx, f, join_path(f.path, f.name)) + local dest = join_path(f.path, f.name) + if (f.url or "") ~= "" then + items[#items + 1] = { dest = dest, sha1 = f.sha1, url = f.url } + else + local cf = f.curseforge + if type(cf) == "table" and cf.project and cf.file then + if key == "" then + error("install failed: file " .. (f.name or dest) .. + " is hosted on CurseForge and needs a CurseForge API key (set it in the FTB provider settings)") + end + items[#items + 1] = { dest = dest, sha1 = f.sha1, resolve = { + url = "https://api.curseforge.com/v1/mods/" .. cf.project .. + "/files/" .. cf.file .. "/download-url", + headers = { ["x-api-key"] = key }, + } } + else + error("install failed: file " .. (f.name or dest) .. " has no download source") + end + end end end + if #items == 0 then return end + ctx.step("shop.install.downloading", 0) + jhmc.download_all(items) end -- find_args_file walks libraries/ for a generated win_args.txt. jhmc.fs.glob is From ef47ef5576db2f1cf8a2c59fffb6958cfc61f134 Mon Sep 17 00:00:00 2001 From: 000hen Date: Sun, 12 Jul 2026 01:41:01 +0800 Subject: [PATCH 09/30] feat(engine): optional update() provider entry point provider.Updater interface + ErrUpdateUnsupported sentinel; the Lua host shares ctx-table construction and spec parsing between install() and update(), which additionally receives ctx.old_version. Co-Authored-By: Claude Fable 5 --- engine/internal/provider/errors.go | 2 + engine/internal/provider/provider.go | 8 ++ engine/internal/scripting/host.go | 78 +++++++++++++------ engine/internal/scripting/luaprovider.go | 7 ++ engine/internal/scripting/luaprovider_test.go | 50 ++++++++++++ 5 files changed, 123 insertions(+), 22 deletions(-) diff --git a/engine/internal/provider/errors.go b/engine/internal/provider/errors.go index 4c9e367..2e619b6 100644 --- a/engine/internal/provider/errors.go +++ b/engine/internal/provider/errors.go @@ -11,4 +11,6 @@ var ( ErrChecksumMismatch = errors.New("downloaded file failed checksum verification") // ErrInstallerFailed means an installer (Forge/NeoForge) did not complete. ErrInstallerFailed = errors.New("server installer failed") + // ErrUpdateUnsupported means the provider has no update() entry point. + ErrUpdateUnsupported = errors.New("provider does not support update") ) diff --git a/engine/internal/provider/provider.go b/engine/internal/provider/provider.go index 2d23e42..5a50f3d 100644 --- a/engine/internal/provider/provider.go +++ b/engine/internal/provider/provider.go @@ -46,6 +46,14 @@ type Provider interface { Install(ctx context.Context, dir, version string, progress func(Progress)) (LaunchSpec, error) } +// Updater is optionally implemented by providers that can move an installed +// server to another version in place (e.g. a modpack manifest diff). +// Implementations return ErrUpdateUnsupported when the underlying script has +// no update() entry point. +type Updater interface { + Update(ctx context.Context, dir, version, oldVersion string, progress func(Progress)) (LaunchSpec, error) +} + // report sends progress if a sink is set; it keeps call sites terse. func report(progress func(Progress), p Progress) { if progress != nil { diff --git a/engine/internal/scripting/host.go b/engine/internal/scripting/host.go index 5ec427a..7582901 100644 --- a/engine/internal/scripting/host.go +++ b/engine/internal/scripting/host.go @@ -172,21 +172,9 @@ func (inv *invocation) versions(src string) ([]string, error) { return out, nil } -// install loads src and calls install(ctx), returning the launch spec. dir is -// the server directory; downloads and fs access are confined to it. -func (inv *invocation) install(src, dir, version string) (provider.LaunchSpec, error) { - inv.baseDir = dir - L, err := inv.prepare(src) - if err != nil { - return provider.LaunchSpec{}, err - } - defer L.Close() - - fn := L.GetGlobal("install") - if fn.Type() != lua.LTFunction { - return provider.LaunchSpec{}, fmt.Errorf("script defines no install() function") - } - +// installCtxTable builds the ctx table shared by install(ctx) and update(ctx): +// dir/version, the step/log progress bridges, and the script config. +func (inv *invocation) installCtxTable(L *lua.LState, dir, version string) *lua.LTable { ictx := L.NewTable() ictx.RawSetString("dir", lua.LString(dir)) ictx.RawSetString("version", lua.LString(version)) @@ -201,17 +189,16 @@ func (inv *invocation) install(src, dir, version string) (provider.LaunchSpec, e return 0 })) ictx.RawSetString("config", inv.configTable(L)) + return ictx +} - if err := L.CallByParam(lua.P{Fn: fn, NRet: 1, Protect: true}, ictx); err != nil { - return provider.LaunchSpec{}, inv.mapErr(err) - } - ret := L.Get(-1) - L.Pop(1) +// parseLaunchSpec converts the table returned by install()/update() into a +// LaunchSpec. +func parseLaunchSpec(ret lua.LValue, fnName string) (provider.LaunchSpec, error) { spec, ok := ret.(*lua.LTable) if !ok { - return provider.LaunchSpec{}, fmt.Errorf("install() must return a launch spec table") + return provider.LaunchSpec{}, fmt.Errorf("%s() must return a launch spec table", fnName) } - major := 0 if n, ok := spec.RawGetString("java_major").(lua.LNumber); ok { major = int(n) @@ -231,6 +218,53 @@ func (inv *invocation) install(src, dir, version string) (provider.LaunchSpec, e }, nil } +// install loads src and calls install(ctx), returning the launch spec. dir is +// the server directory; downloads and fs access are confined to it. +func (inv *invocation) install(src, dir, version string) (provider.LaunchSpec, error) { + inv.baseDir = dir + L, err := inv.prepare(src) + if err != nil { + return provider.LaunchSpec{}, err + } + defer L.Close() + + fn := L.GetGlobal("install") + if fn.Type() != lua.LTFunction { + return provider.LaunchSpec{}, fmt.Errorf("script defines no install() function") + } + ictx := inv.installCtxTable(L, dir, version) + if err := L.CallByParam(lua.P{Fn: fn, NRet: 1, Protect: true}, ictx); err != nil { + return provider.LaunchSpec{}, inv.mapErr(err) + } + ret := L.Get(-1) + L.Pop(1) + return parseLaunchSpec(ret, "install") +} + +// update loads src and calls update(ctx) — the optional in-place move of an +// installed server to another version. ctx additionally carries old_version. +func (inv *invocation) update(src, dir, version, oldVersion string) (provider.LaunchSpec, error) { + inv.baseDir = dir + L, err := inv.prepare(src) + if err != nil { + return provider.LaunchSpec{}, err + } + defer L.Close() + + fn := L.GetGlobal("update") + if fn.Type() != lua.LTFunction { + return provider.LaunchSpec{}, provider.ErrUpdateUnsupported + } + ictx := inv.installCtxTable(L, dir, version) + ictx.RawSetString("old_version", lua.LString(oldVersion)) + if err := L.CallByParam(lua.P{Fn: fn, NRet: 1, Protect: true}, ictx); err != nil { + return provider.LaunchSpec{}, inv.mapErr(err) + } + ret := L.Get(-1) + L.Pop(1) + return parseLaunchSpec(ret, "update") +} + // mapErr recovers the structured error stashed by fail(), falling back to the // raw Lua error text. As a convenience it bridges the common script idiom // error("version not found: …") to the provider sentinel the gRPC layer maps. diff --git a/engine/internal/scripting/luaprovider.go b/engine/internal/scripting/luaprovider.go index 90cdf02..2d18126 100644 --- a/engine/internal/scripting/luaprovider.go +++ b/engine/internal/scripting/luaprovider.go @@ -71,3 +71,10 @@ func (p *LuaProvider) Install(ctx context.Context, dir, version string, progress inv := &invocation{ctx: ctx, host: p.host, granted: p.grants(), report: progress, assetDir: p.assetDir, config: p.config()} return inv.install(p.source, dir, version) } + +// Update implements provider.Updater; scripts without an update() function +// yield provider.ErrUpdateUnsupported. +func (p *LuaProvider) Update(ctx context.Context, dir, version, oldVersion string, progress func(provider.Progress)) (provider.LaunchSpec, error) { + inv := &invocation{ctx: ctx, host: p.host, granted: p.grants(), report: progress, assetDir: p.assetDir, config: p.config()} + return inv.update(p.source, dir, version, oldVersion) +} diff --git a/engine/internal/scripting/luaprovider_test.go b/engine/internal/scripting/luaprovider_test.go index 17bf610..81d6a53 100644 --- a/engine/internal/scripting/luaprovider_test.go +++ b/engine/internal/scripting/luaprovider_test.go @@ -180,6 +180,56 @@ end } } +// TestUpdatePassesVersionsAndParsesSpec verifies update(ctx) receives both the +// new and old opaque versions and its return parses like install's. +func TestUpdatePassesVersionsAndParsesSpec(t *testing.T) { + script := ` +meta = { id = "updy", name = "Updy", mod_layout = "mods" } +function versions() return {} end +function install(ctx) return { java_major = 21, args = { "a" } } end +function update(ctx) + return { java_major = 21, args = { "-jar", "s.jar" }, + mc_version = "1.21.1", loader = "neoforge", + pack_version = tostring(ctx.version) .. "|was|" .. tostring(ctx.old_version) } +end +` + r := NewRegistry(NewHost(nil, nil, nil), nil) + e, err := r.AddSource(context.Background(), script, true) + if err != nil { + t.Fatalf("AddSource: %v", err) + } + up, ok := e.Provider.(provider.Updater) + if !ok { + t.Fatal("LuaProvider does not implement provider.Updater") + } + spec, err := up.Update(context.Background(), t.TempDir(), "95/200", "95/100", nil) + if err != nil { + t.Fatalf("Update: %v", err) + } + if spec.PackVersion != "95/200|was|95/100" { + t.Errorf("PackVersion = %q, want versions threaded through", spec.PackVersion) + } +} + +// TestUpdateUnsupportedWithoutFunction proves a script lacking update() maps to +// the sentinel instead of a generic Lua error. +func TestUpdateUnsupportedWithoutFunction(t *testing.T) { + script := ` +meta = { id = "noup", name = "NoUp", mod_layout = "mods" } +function versions() return {} end +function install(ctx) return { java_major = 21, args = { "a" } } end +` + r := NewRegistry(NewHost(nil, nil, nil), nil) + e, err := r.AddSource(context.Background(), script, true) + if err != nil { + t.Fatalf("AddSource: %v", err) + } + up := e.Provider.(provider.Updater) + if _, err := up.Update(context.Background(), t.TempDir(), "95/200", "95/100", nil); !errors.Is(err, provider.ErrUpdateUnsupported) { + t.Fatalf("Update err = %v, want ErrUpdateUnsupported", err) + } +} + // TestUngrantedNetworkDenied proves a non-builtin script with no grants cannot // reach the network. func TestUngrantedNetworkDenied(t *testing.T) { From d2e4adb348f2032f5b97871307f4096e951d4395 Mon Sep 17 00:00:00 2001 From: 000hen Date: Sun, 12 Jul 2026 01:42:27 +0800 Subject: [PATCH 10/30] feat(engine): ftb.lua manifest-diff update() Co-Authored-By: Claude Fable 5 --- engine/internal/scripting/builtin/ftb.lua | 85 +++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/engine/internal/scripting/builtin/ftb.lua b/engine/internal/scripting/builtin/ftb.lua index a28ff2d..649c63d 100644 --- a/engine/internal/scripting/builtin/ftb.lua +++ b/engine/internal/scripting/builtin/ftb.lua @@ -252,3 +252,88 @@ function install(ctx) pack_version = tostring(ctx.version), } end + +-- server_files indexes a manifest's server-side files by relative dest path. +local function server_files(manifest) + local by_dest = {} + for _, f in ipairs(manifest.files or {}) do + if type(f) == "table" and not f.clientonly and (f.name or "") ~= "" then + by_dest[join_path(f.path, f.name)] = f + end + end + return by_dest +end + +-- update moves an installed pack to another version of the same pack: files +-- only the old version listed are deleted, new/changed ones are downloaded, +-- and the loader is reinstalled only when its pinned target changed. Files the +-- pack never listed (the world, user-added configs) are untouched; a pack file +-- the user edited is overwritten by the new pack version — pack files belong +-- to the pack. +function update(ctx) + local pack, ver = tostring(ctx.version):match("^([^/]+)/([^/]+)$") + local opack, over = tostring(ctx.old_version):match("^([^/]+)/([^/]+)$") + if not pack or not ver then + error("invalid modpack version id: " .. tostring(ctx.version)) + end + if not opack or not over then + error("invalid modpack version id: " .. tostring(ctx.old_version)) + end + if pack ~= opack then + error("update must stay within the same pack (" .. opack .. " -> " .. pack .. ")") + end + + ctx.step("install.progress.resolving_version", -1) + local old_manifest = jhmc.http_json(FTB_API .. "/modpack/" .. opack .. "/" .. over) + local new_manifest = jhmc.http_json(FTB_API .. "/modpack/" .. pack .. "/" .. ver) + if type(old_manifest) ~= "table" or type(new_manifest) ~= "table" then + error("ftb: bad manifest for update " .. tostring(ctx.old_version) .. + " -> " .. tostring(ctx.version)) + end + + local old_files, new_files = server_files(old_manifest), server_files(new_manifest) + + -- Delete files the new version dropped. + for dest in pairs(old_files) do + if not new_files[dest] and jhmc.fs.exists(dest) then + ctx.log("- " .. dest) + jhmc.fs.remove(dest) + end + end + + -- Download new files and files whose pack hash changed. + local changed = {} + for dest, f in pairs(new_files) do + local old = old_files[dest] + if not old or (old.sha1 or "") ~= (f.sha1 or "") then + changed[#changed + 1] = f + end + end + install_files(ctx, changed) + + local mc, loader_name, loader_version = read_targets(new_manifest) + if not mc or mc == "" then + error("ftb: modpack has no Minecraft version target") + end + if not loader_name or loader_name == "" then + error("ftb: modpack has no modloader target") + end + local omc, oloader_name, oloader_version = read_targets(old_manifest) + + local java_major = jhmc.java_major_for(mc) + local args + if loader_name ~= oloader_name or loader_version ~= oloader_version or mc ~= omc then + args = install_loader(ctx, loader_name, loader_version, mc, java_major) + end + + ctx.step("install.progress.done", 1) + return { + java_major = java_major, + -- args is nil when the loader target is unchanged; the engine keeps the + -- server's existing launch args in that case. + args = args, + mc_version = mc, + loader = loader_name, + pack_version = tostring(ctx.version), + } +end From 48e0cb67a25440130eceeca91aff15287f5a334b Mon Sep 17 00:00:00 2001 From: 000hen Date: Sun, 12 Jul 2026 01:45:09 +0800 Subject: [PATCH 11/30] feat(engine): UpdateModpack RPC (manifest-diff modpack update) Streams progress like Create; requires a stopped modpack server; failure restores STOPPED and leaves the existing install untouched. Empty spec.Args from update() keeps the server's current launch args. Co-Authored-By: Claude Fable 5 --- engine/internal/grpc/serverservice.go | 86 ++++++++++ .../grpc/serverservice_updatemodpack_test.go | 151 ++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 engine/internal/grpc/serverservice_updatemodpack_test.go diff --git a/engine/internal/grpc/serverservice.go b/engine/internal/grpc/serverservice.go index 434da0c..b6530b7 100644 --- a/engine/internal/grpc/serverservice.go +++ b/engine/internal/grpc/serverservice.go @@ -359,6 +359,92 @@ func (s *ServerService) Update(ctx context.Context, req *mcmanagerv1.UpdateServe return rec.Proto(), nil } +// UpdateModpack moves a modpack server to another pack version in place, +// streaming progress like Create. Unlike Create, a failure leaves the existing +// install untouched (no cleanup wipe) — the old version keeps working. +func (s *ServerService) UpdateModpack(req *mcmanagerv1.UpdateModpackRequest, stream grpc.ServerStreamingServer[mcmanagerv1.InstallProgress]) error { + ctx := stream.Context() + rec, ok := s.cfg.Store.Get(req.Id) + if !ok { + return status.Error(codes.NotFound, "server not found") + } + if rec.ProviderVersion == "" { + return errorStatus(codes.FailedPrecondition, mcmanagerv1.ErrorCode_ERROR_CODE_UNSPECIFIED, + "server was not installed from a modpack", nil) + } + if rec.Status != mcmanagerv1.ServerStatus_STOPPED { + return errorStatus(codes.FailedPrecondition, mcmanagerv1.ErrorCode_SERVER_RUNNING, + "server must be stopped before updating the modpack", nil) + } + newVersion := strings.TrimSpace(req.Version) + if newVersion == "" { + return status.Error(codes.InvalidArgument, "version is required") + } + entry, ok := s.cfg.Providers.Get(rec.ProviderID) + if !ok { + return status.Errorf(codes.Unimplemented, "provider %q not installed", rec.ProviderID) + } + up, ok := entry.Provider.(provider.Updater) + if !ok { + return status.Error(codes.Unimplemented, "provider does not support update") + } + + oldVersion := rec.ProviderVersion + rec.Status = mcmanagerv1.ServerStatus_INSTALLING + _ = s.cfg.Store.Put(rec) + restore := func() { + rec.Status = mcmanagerv1.ServerStatus_STOPPED + _ = s.cfg.Store.Put(rec) + } + + il := s.openInstallLog(rec.ID) + defer il.Close() + il.recordLine("[update] " + oldVersion + " -> " + newVersion) + base := newProgressSender(stream) + send := func(p provider.Progress) { + il.record(p) + base(p) + } + + spec, err := up.Update(ctx, s.cfg.Paths.ServerDir(rec.ID), newVersion, oldVersion, send) + if err != nil { + send(provider.Progress{LogLine: "[error] " + err.Error()}) + il.recordLine("[error] update: " + err.Error()) + restore() + return mapInstallError(err) + } + resolved := rec.McVersion + if spec.McVersion != "" { + resolved = spec.McVersion + } + spec.JavaMajor = maxJavaMajor(spec.JavaMajor, resolved) + if _, err := s.cfg.JRE.EnsureJRE(ctx, spec.JavaMajor, send); err != nil { + send(provider.Progress{LogLine: "[error] " + err.Error()}) + il.recordLine("[error] jre: " + err.Error()) + restore() + return mapInstallError(err) + } + + rec.JavaMajor = spec.JavaMajor + if len(spec.Args) > 0 { + rec.LaunchArgs = spec.Args + } + if spec.McVersion != "" { + rec.McVersion = spec.McVersion + } + if spec.Loader != "" { + rec.Loader = spec.Loader + } + if spec.PackVersion != "" { + rec.ProviderVersion = spec.PackVersion + } + rec.Status = mcmanagerv1.ServerStatus_STOPPED + _ = s.cfg.Store.Put(rec) + + send(provider.Progress{Step: "install.progress.done", Fraction: 1}) + return nil +} + // Start launches a previously created server. func (s *ServerService) Start(ctx context.Context, req *mcmanagerv1.ServerId) (*mcmanagerv1.Empty, error) { rec, ok := s.cfg.Store.Get(req.Id) diff --git a/engine/internal/grpc/serverservice_updatemodpack_test.go b/engine/internal/grpc/serverservice_updatemodpack_test.go new file mode 100644 index 0000000..44e9dc6 --- /dev/null +++ b/engine/internal/grpc/serverservice_updatemodpack_test.go @@ -0,0 +1,151 @@ +package grpcsvc + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" + + mcmanagerv1 "github.com/000hen/justhostmc/engine/gen/mcmanager/v1" + "github.com/000hen/justhostmc/engine/internal/appdata" + "github.com/000hen/justhostmc/engine/internal/jre" + "github.com/000hen/justhostmc/engine/internal/provider" + "github.com/000hen/justhostmc/engine/internal/store" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +// updateStubProvider is an installStubProvider that also implements +// provider.Updater, recording the versions it was called with. +type updateStubProvider struct { + installStubProvider + gotVersion, gotOld string + updateSpec provider.LaunchSpec + updateErr error +} + +func (p *updateStubProvider) Update(_ context.Context, _, version, oldVersion string, report func(provider.Progress)) (provider.LaunchSpec, error) { + p.gotVersion, p.gotOld = version, oldVersion + if report != nil { + report(provider.Progress{Step: "shop.install.downloading", Fraction: 0.5}) + } + return p.updateSpec, p.updateErr +} + +func modpackTestService(t *testing.T, prov provider.Provider, status mcmanagerv1.ServerStatus, providerVersion string) (*ServerService, *store.Memory, appdata.Paths) { + t.Helper() + paths := appdata.New(t.TempDir()) + if err := os.MkdirAll(paths.ServerDir("s1"), 0o755); err != nil { + t.Fatal(err) + } + cacheJava(t, paths.JRECache(), 21) + st := store.NewMemory() + _ = st.Put(&store.Server{ + ID: "s1", Name: "Pack", ProviderID: "ftb", ModLayout: "mods", + McVersion: "1.21.1", Loader: "neoforge", ProviderVersion: providerVersion, + MemoryMB: 4096, Port: 25565, Status: status, + JavaMajor: 21, LaunchArgs: []string{"-jar", "old-run.jar"}, + }) + svc := NewServerService(ServerServiceConfig{ + Store: st, + Providers: testRegistry("ftb", "mods", prov), + Paths: paths, + JRE: jre.NewManager(paths.JRECache()), + }) + return svc, st, paths +} + +func TestUpdateModpackHappyPath(t *testing.T) { + prov := &updateStubProvider{updateSpec: provider.LaunchSpec{ + JavaMajor: 21, McVersion: "1.21.4", Loader: "neoforge", + PackVersion: "95/300", + }} + svc, st, _ := modpackTestService(t, prov, mcmanagerv1.ServerStatus_STOPPED, "95/100") + + err := svc.UpdateModpack(&mcmanagerv1.UpdateModpackRequest{Id: "s1", Version: "95/300"}, + &fakeInstallStream{}) + if err != nil { + t.Fatalf("UpdateModpack: %v", err) + } + if prov.gotVersion != "95/300" || prov.gotOld != "95/100" { + t.Errorf("provider got (%q, %q), want (95/300, 95/100)", prov.gotVersion, prov.gotOld) + } + rec, _ := st.Get("s1") + if rec.ProviderVersion != "95/300" || rec.McVersion != "1.21.4" { + t.Errorf("record = %+v, want new pack + MC version", rec) + } + if rec.Status != mcmanagerv1.ServerStatus_STOPPED { + t.Errorf("status = %v, want STOPPED", rec.Status) + } + // spec.Args empty → existing launch args kept. + if len(rec.LaunchArgs) != 2 || rec.LaunchArgs[1] != "old-run.jar" { + t.Errorf("LaunchArgs = %v, want preserved", rec.LaunchArgs) + } +} + +func TestUpdateModpackReplacesArgsWhenProvided(t *testing.T) { + prov := &updateStubProvider{updateSpec: provider.LaunchSpec{ + JavaMajor: 21, Args: []string{"@libraries/run.args"}, McVersion: "1.21.4", + PackVersion: "95/300", + }} + svc, st, _ := modpackTestService(t, prov, mcmanagerv1.ServerStatus_STOPPED, "95/100") + if err := svc.UpdateModpack(&mcmanagerv1.UpdateModpackRequest{Id: "s1", Version: "95/300"}, + &fakeInstallStream{}); err != nil { + t.Fatalf("UpdateModpack: %v", err) + } + rec, _ := st.Get("s1") + if len(rec.LaunchArgs) != 1 || rec.LaunchArgs[0] != "@libraries/run.args" { + t.Errorf("LaunchArgs = %v, want replaced", rec.LaunchArgs) + } +} + +func TestUpdateModpackRejectsRunning(t *testing.T) { + prov := &updateStubProvider{} + svc, _, _ := modpackTestService(t, prov, mcmanagerv1.ServerStatus_RUNNING, "95/100") + err := svc.UpdateModpack(&mcmanagerv1.UpdateModpackRequest{Id: "s1", Version: "95/300"}, + &fakeInstallStream{}) + if status.Code(err) != codes.FailedPrecondition { + t.Fatalf("code = %v, want FailedPrecondition", status.Code(err)) + } +} + +func TestUpdateModpackRejectsNonModpackServer(t *testing.T) { + prov := &updateStubProvider{} + svc, _, _ := modpackTestService(t, prov, mcmanagerv1.ServerStatus_STOPPED, "") + err := svc.UpdateModpack(&mcmanagerv1.UpdateModpackRequest{Id: "s1", Version: "95/300"}, + &fakeInstallStream{}) + if status.Code(err) != codes.FailedPrecondition { + t.Fatalf("code = %v, want FailedPrecondition", status.Code(err)) + } +} + +// TestUpdateModpackFailureLeavesInstallIntact proves an update error restores +// STOPPED, keeps the record and the server dir — unlike Create's wipe. +func TestUpdateModpackFailureLeavesInstallIntact(t *testing.T) { + prov := &updateStubProvider{updateErr: errors.New("cdn exploded")} + svc, st, paths := modpackTestService(t, prov, mcmanagerv1.ServerStatus_STOPPED, "95/100") + marker := filepath.Join(paths.ServerDir("s1"), "world-data.txt") + if err := os.WriteFile(marker, []byte("keep me"), 0o644); err != nil { + t.Fatal(err) + } + + err := svc.UpdateModpack(&mcmanagerv1.UpdateModpackRequest{Id: "s1", Version: "95/300"}, + &fakeInstallStream{}) + if err == nil { + t.Fatal("UpdateModpack succeeded, want error") + } + rec, ok := st.Get("s1") + if !ok { + t.Fatal("record deleted on failure") + } + if rec.Status != mcmanagerv1.ServerStatus_STOPPED { + t.Errorf("status = %v, want restored STOPPED", rec.Status) + } + if rec.ProviderVersion != "95/100" { + t.Errorf("ProviderVersion = %q, want unchanged 95/100", rec.ProviderVersion) + } + if _, err := os.Stat(marker); err != nil { + t.Errorf("server dir wiped on failure: %v", err) + } +} From 21c4dca627eb0d9b89f07340e633dfc7e50d2a82 Mon Sep 17 00:00:00 2001 From: 000hen Date: Sun, 12 Jul 2026 13:50:14 +0800 Subject: [PATCH 12/30] feat(engine): ExportModpack RPC (CurseForge client pack zip) manifest.json lists every CF-hosted file including client-only mods; the server's live config dirs and hand-added mods go into overrides/; client-only direct-URL files are downloaded at export time. Co-Authored-By: Claude Fable 5 --- engine/internal/grpc/serverservice.go | 42 ++++ engine/internal/modpack/export.go | 328 +++++++++++++++++++++++++ engine/internal/modpack/export_test.go | 215 ++++++++++++++++ 3 files changed, 585 insertions(+) create mode 100644 engine/internal/modpack/export.go create mode 100644 engine/internal/modpack/export_test.go diff --git a/engine/internal/grpc/serverservice.go b/engine/internal/grpc/serverservice.go index b6530b7..3dde6e0 100644 --- a/engine/internal/grpc/serverservice.go +++ b/engine/internal/grpc/serverservice.go @@ -3,6 +3,7 @@ package grpcsvc import ( "context" "fmt" + "net/http" "os" "path/filepath" "strings" @@ -15,6 +16,7 @@ import ( "github.com/000hen/justhostmc/engine/internal/isolation" "github.com/000hen/justhostmc/engine/internal/jre" "github.com/000hen/justhostmc/engine/internal/logging" + "github.com/000hen/justhostmc/engine/internal/modpack" "github.com/000hen/justhostmc/engine/internal/provider" "github.com/000hen/justhostmc/engine/internal/scripting" "github.com/000hen/justhostmc/engine/internal/store" @@ -38,6 +40,9 @@ type ServerServiceConfig struct { // OnExit, if set, is called after a server's instance exits (stop or crash), // e.g. to reset the player event bus roster for that server. OnExit func(id string) + // HTTP, if set, is used for engine-side downloads outside the scripting + // host (modpack export); nil falls back to http.DefaultClient. + HTTP *http.Client } // ServerService implements the ServerService RPCs: it orchestrates the provider @@ -445,6 +450,43 @@ func (s *ServerService) UpdateModpack(req *mcmanagerv1.UpdateModpackRequest, str return nil } +// ExportModpack writes a CurseForge-format client pack zip for a modpack +// server. Read-only with respect to the server dir, so it is allowed while the +// server runs. +func (s *ServerService) ExportModpack(req *mcmanagerv1.ExportModpackRequest, stream grpc.ServerStreamingServer[mcmanagerv1.InstallProgress]) error { + rec, ok := s.cfg.Store.Get(req.Id) + if !ok { + return status.Error(codes.NotFound, "server not found") + } + // Export builds the manifest from the FTB API; other (future) modpack + // providers would need their own manifest source. + if rec.ProviderVersion == "" || rec.ProviderID != "ftb" { + return errorStatus(codes.FailedPrecondition, mcmanagerv1.ErrorCode_ERROR_CODE_UNSPECIFIED, + "server was not installed from an FTB modpack", nil) + } + dest := req.DestPath + if !filepath.IsAbs(dest) || !strings.EqualFold(filepath.Ext(dest), ".zip") { + return status.Error(codes.InvalidArgument, "dest_path must be an absolute .zip path") + } + + client := s.cfg.HTTP + if client == nil { + client = http.DefaultClient + } + send := newProgressSender(stream) + if err := modpack.Export(stream.Context(), client, modpack.Options{ + ServerDir: s.cfg.Paths.ServerDir(rec.ID), + DestZip: dest, + PackVersion: rec.ProviderVersion, + ServerName: rec.Name, + }, send); err != nil { + send(provider.Progress{LogLine: "[error] " + err.Error()}) + return status.Errorf(codes.Internal, "export modpack: %v", err) + } + send(provider.Progress{Step: "shop.export.done", Fraction: 1}) + return nil +} + // Start launches a previously created server. func (s *ServerService) Start(ctx context.Context, req *mcmanagerv1.ServerId) (*mcmanagerv1.Empty, error) { rec, ok := s.cfg.Store.Get(req.Id) diff --git a/engine/internal/modpack/export.go b/engine/internal/modpack/export.go new file mode 100644 index 0000000..5aedb82 --- /dev/null +++ b/engine/internal/modpack/export.go @@ -0,0 +1,328 @@ +// Package modpack builds client-importable modpack packages from an installed +// modpack server: a CurseForge-format zip (manifest.json + overrides/) that +// launchers like the CurseForge app, Prism, and ATLauncher import directly, +// carrying the server's live configs. +package modpack + +import ( + "archive/zip" + "context" + "encoding/json" + "fmt" + "io" + "io/fs" + "net/http" + "os" + "path" + "path/filepath" + "strings" + + "github.com/000hen/justhostmc/engine/internal/provider" + "golang.org/x/sync/errgroup" +) + +// DefaultFTBAPIBase is the public FTB modpack API (same upstream ftb.lua uses). +const DefaultFTBAPIBase = "https://api.feed-the-beast.com/v1/modpacks/public" + +// Options configures one export. +type Options struct { + ServerDir string // installed server directory (read-only for export) + DestZip string // absolute output .zip path + PackVersion string // opaque "packId/versionId" stored on the server record + ServerName string // used as the pack name in manifest.json + FTBAPIBase string // empty = DefaultFTBAPIBase; overridable in tests +} + +// overrideDirs are the server dirs copied verbatim into overrides/ (when they +// exist). mods/ is handled separately so CF-covered jars aren't duplicated. +var overrideDirs = []string{ + "config", "defaultconfigs", "kubejs", "scripts", "resourcepacks", "shaderpacks", +} + +// ftbManifest is the subset of the FTB version manifest export consumes. +type ftbManifest struct { + Name string `json:"name"` + Targets []struct { + Type string `json:"type"` + Name string `json:"name"` + Version string `json:"version"` + } `json:"targets"` + Files []ftbFile `json:"files"` +} + +type ftbFile struct { + Path string `json:"path"` + Name string `json:"name"` + URL string `json:"url"` + Clientonly bool `json:"clientonly"` + Curseforge *struct { + Project int64 `json:"project"` + File int64 `json:"file"` + } `json:"curseforge"` +} + +// dest is the file's zip-relative destination ("mods/x.jar"). +func (f ftbFile) dest() string { + p := strings.Trim(strings.TrimPrefix(strings.TrimSpace(f.Path), "./"), "/") + if p == "" { + return f.Name + } + return p + "/" + f.Name +} + +// cfManifest is the CurseForge pack manifest.json shape. +type cfManifest struct { + Minecraft struct { + Version string `json:"version"` + ModLoaders []cfModLoader `json:"modLoaders"` + } `json:"minecraft"` + ManifestType string `json:"manifestType"` + ManifestVersion int `json:"manifestVersion"` + Name string `json:"name"` + Version string `json:"version"` + Author string `json:"author"` + Files []cfFile `json:"files"` + Overrides string `json:"overrides"` +} + +type cfModLoader struct { + ID string `json:"id"` + Primary bool `json:"primary"` +} + +type cfFile struct { + ProjectID int64 `json:"projectID"` + FileID int64 `json:"fileID"` + Required bool `json:"required"` +} + +func report(progress func(provider.Progress), p provider.Progress) { + if progress != nil { + progress(p) + } +} + +// Export writes a CurseForge-format client pack zip for the FTB pack install +// in o.ServerDir. Manifest-listed files with CurseForge ids (including +// client-only ones) become manifest entries; the server's live config dirs and +// hand-added mods go into overrides/; client-only direct-URL files are +// downloaded at export time. +func Export(ctx context.Context, client *http.Client, o Options, progress func(provider.Progress)) error { + report(progress, provider.Progress{Step: "shop.export.preparing", Fraction: -1}) + + pack, ver, ok := strings.Cut(o.PackVersion, "/") + if !ok || pack == "" || ver == "" { + return fmt.Errorf("invalid pack version %q", o.PackVersion) + } + base := o.FTBAPIBase + if base == "" { + base = DefaultFTBAPIBase + } + manifest, err := fetchManifest(ctx, client, fmt.Sprintf("%s/modpack/%s/%s", base, pack, ver)) + if err != nil { + return err + } + + out := cfManifest{ + ManifestType: "minecraftModpack", + ManifestVersion: 1, + Name: o.ServerName, + Version: manifest.Name, + Overrides: "overrides", + } + var loaderOK bool + for _, t := range manifest.Targets { + switch t.Type { + case "game": + out.Minecraft.Version = t.Version + case "modloader": + out.Minecraft.ModLoaders = []cfModLoader{ + {ID: strings.ToLower(t.Name) + "-" + t.Version, Primary: true}, + } + loaderOK = true + } + } + if out.Minecraft.Version == "" || !loaderOK { + return fmt.Errorf("pack manifest has no game/modloader target") + } + + // CF-hosted files (server-side AND client-only) become manifest entries; + // their local copies must not be duplicated into overrides. + covered := map[string]bool{} + var clientDownloads []ftbFile + for _, f := range manifest.Files { + if f.Name == "" { + continue + } + switch { + case f.Curseforge != nil: + out.Files = append(out.Files, cfFile{ + ProjectID: f.Curseforge.Project, FileID: f.Curseforge.File, Required: true, + }) + covered[f.dest()] = true + case f.Clientonly && f.URL != "": + clientDownloads = append(clientDownloads, f) + } + } + + // Download client-only direct-URL files to a staging dir (they were never + // installed server-side). + staging, err := os.MkdirTemp("", "jhmc-export-*") + if err != nil { + return err + } + defer os.RemoveAll(staging) + if len(clientDownloads) > 0 { + g, gctx := errgroup.WithContext(ctx) + g.SetLimit(6) + done := 0 + total := len(clientDownloads) + results := make(chan string, total) + for _, f := range clientDownloads { + g.Go(func() error { + full := filepath.Join(staging, filepath.FromSlash(f.dest())) + if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { + return err + } + if err := downloadTo(gctx, client, f.URL, full); err != nil { + return fmt.Errorf("%s: %w", f.dest(), err) + } + results <- f.Name + return nil + }) + } + go func() { _ = g.Wait(); close(results) }() + for name := range results { + done++ + report(progress, provider.Progress{Step: "shop.install.downloading", + Fraction: float64(done) / float64(total), LogLine: name}) + } + if err := g.Wait(); err != nil { + return err + } + } + + report(progress, provider.Progress{Step: "shop.export.zipping", Fraction: -1}) + return writeZip(o, out, staging, covered) +} + +func fetchManifest(ctx context.Context, client *http.Client, url string) (*ftbManifest, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, err + } + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("GET %s: unexpected status %s", url, resp.Status) + } + var m ftbManifest + if err := json.NewDecoder(io.LimitReader(resp.Body, 32<<20)).Decode(&m); err != nil { + return nil, fmt.Errorf("decode pack manifest: %v", err) + } + return &m, nil +} + +func downloadTo(ctx context.Context, client *http.Client, url, dest string) error { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return err + } + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("GET %s: unexpected status %s", url, resp.Status) + } + f, err := os.Create(dest) + if err != nil { + return err + } + defer f.Close() + _, err = io.Copy(f, resp.Body) + return err +} + +// writeZip streams manifest.json, the staged client-only files, the server's +// override dirs, and uncovered mod jars into the destination zip. +func writeZip(o Options, manifest cfManifest, staging string, covered map[string]bool) error { + zf, err := os.Create(o.DestZip) + if err != nil { + return err + } + defer zf.Close() + zw := zip.NewWriter(zf) + + mj, err := zw.Create("manifest.json") + if err != nil { + return err + } + enc := json.NewEncoder(mj.(io.Writer)) + enc.SetIndent("", " ") + if err := enc.Encode(manifest); err != nil { + return err + } + + addFile := func(zipPath, fsPath string) error { + w, err := zw.Create(zipPath) + if err != nil { + return err + } + f, err := os.Open(fsPath) + if err != nil { + return err + } + defer f.Close() + _, err = io.Copy(w, f) + return err + } + addTree := func(root, zipPrefix string, skip func(rel string) bool) error { + return filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error { + if err != nil || d.IsDir() { + return err + } + rel, err := filepath.Rel(root, p) + if err != nil { + return err + } + relSlash := filepath.ToSlash(rel) + if skip != nil && skip(relSlash) { + return nil + } + return addFile(path.Join(zipPrefix, relSlash), p) + }) + } + + // Staged client-only downloads. + if _, err := os.Stat(staging); err == nil { + if err := addTree(staging, "overrides", nil); err != nil { + return err + } + } + // Live server config-ish dirs. + for _, dir := range overrideDirs { + root := filepath.Join(o.ServerDir, dir) + if _, err := os.Stat(root); err != nil { + continue + } + if err := addTree(root, "overrides/"+dir, nil); err != nil { + return err + } + } + // Hand-added mods (anything the CF manifest doesn't already deliver). + modsRoot := filepath.Join(o.ServerDir, "mods") + if _, err := os.Stat(modsRoot); err == nil { + if err := addTree(modsRoot, "overrides/mods", func(rel string) bool { + return covered["mods/"+rel] + }); err != nil { + return err + } + } + + return zw.Close() +} diff --git a/engine/internal/modpack/export_test.go b/engine/internal/modpack/export_test.go new file mode 100644 index 0000000..e29b1d9 --- /dev/null +++ b/engine/internal/modpack/export_test.go @@ -0,0 +1,215 @@ +package modpack + +import ( + "archive/zip" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/000hen/justhostmc/engine/internal/provider" +) + +// fixtureManifest mirrors the FTB version-manifest fields Export consumes. +const fixtureManifest = `{ + "name": "1.5.0", + "targets": [ + { "type": "game", "version": "1.21.1" }, + { "type": "modloader", "name": "neoforge", "version": "21.1.77" } + ], + "files": [ + { "path": "./mods/", "name": "server-mod.jar", "clientonly": false, + "curseforge": { "project": 111, "file": 1111 } }, + { "path": "./mods/", "name": "client-mod.jar", "clientonly": true, + "curseforge": { "project": 222, "file": 2222 } }, + { "path": "./config/", "name": "pack.toml", "clientonly": false, + "url": "IGNORED-server-file-comes-from-disk" }, + { "path": "./resourcepacks/", "name": "look.zip", "clientonly": true, + "url": "%s/blob/look.zip" } + ] +}` + +func exportFixture(t *testing.T) (Options, string) { + t.Helper() + mux := http.NewServeMux() + var base string + mux.HandleFunc("/modpack/95/300", func(w http.ResponseWriter, _ *http.Request) { + fmt.Fprintf(w, fixtureManifest, base) + }) + mux.HandleFunc("/blob/look.zip", func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte("client-only-bytes")) + }) + srv := httptest.NewServer(mux) + base = srv.URL + t.Cleanup(srv.Close) + + serverDir := t.TempDir() + mustWrite := func(rel, content string) { + full := filepath.Join(serverDir, filepath.FromSlash(rel)) + if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(full, []byte(content), 0o644); err != nil { + t.Fatal(err) + } + } + mustWrite("config/pack.toml", "edited-on-server") + mustWrite("config/nested/extra.json", "{}") + mustWrite("kubejs/server_scripts/recipes.js", "// kubejs") + mustWrite("mods/server-mod.jar", "cf-covered-jar") // covered by CF entry → excluded + mustWrite("mods/local-extra.jar", "hand-added-jar") // not in manifest → included + mustWrite("world/level.dat", "world") // excluded + mustWrite("logs/latest.log", "log") // excluded + mustWrite("libraries/dep.jar", "lib") // excluded + mustWrite("server.properties", "server-port=25565") // excluded + mustWrite("eula.txt", "eula=true") // excluded + mustWrite("neoforge-installer.jar", "root installer") // excluded (root jar) + + dest := filepath.Join(t.TempDir(), "out.zip") + return Options{ + ServerDir: serverDir, + DestZip: dest, + PackVersion: "95/300", + ServerName: "My Pack Server", + FTBAPIBase: srv.URL, + }, dest +} + +func readZip(t *testing.T, dest string) map[string]string { + t.Helper() + zr, err := zip.OpenReader(dest) + if err != nil { + t.Fatalf("open zip: %v", err) + } + defer zr.Close() + out := map[string]string{} + for _, f := range zr.File { + rc, err := f.Open() + if err != nil { + t.Fatalf("open %s: %v", f.Name, err) + } + b, _ := io.ReadAll(rc) + rc.Close() + out[f.Name] = string(b) + } + return out +} + +func TestExportBuildsCurseForgeManifest(t *testing.T) { + opts, dest := exportFixture(t) + if err := Export(context.Background(), http.DefaultClient, opts, nil); err != nil { + t.Fatalf("Export: %v", err) + } + files := readZip(t, dest) + + var m struct { + ManifestType string `json:"manifestType"` + ManifestVersion int `json:"manifestVersion"` + Name string `json:"name"` + Version string `json:"version"` + Overrides string `json:"overrides"` + Minecraft struct { + Version string `json:"version"` + ModLoaders []struct { + ID string `json:"id"` + Primary bool `json:"primary"` + } `json:"modLoaders"` + } `json:"minecraft"` + Files []struct { + ProjectID int64 `json:"projectID"` + FileID int64 `json:"fileID"` + Required bool `json:"required"` + } `json:"files"` + } + if err := json.Unmarshal([]byte(files["manifest.json"]), &m); err != nil { + t.Fatalf("manifest.json: %v (%q)", err, files["manifest.json"]) + } + if m.ManifestType != "minecraftModpack" || m.ManifestVersion != 1 { + t.Errorf("manifest type/version = %s/%d", m.ManifestType, m.ManifestVersion) + } + if m.Name != "My Pack Server" || m.Version != "1.5.0" || m.Overrides != "overrides" { + t.Errorf("name/version/overrides = %q/%q/%q", m.Name, m.Version, m.Overrides) + } + if m.Minecraft.Version != "1.21.1" || + len(m.Minecraft.ModLoaders) != 1 || m.Minecraft.ModLoaders[0].ID != "neoforge-21.1.77" || + !m.Minecraft.ModLoaders[0].Primary { + t.Errorf("minecraft block = %+v", m.Minecraft) + } + // Both the server-side AND the clientonly CF-hosted mods are listed. + if len(m.Files) != 2 { + t.Fatalf("files = %+v, want 2 CF entries", m.Files) + } + seen := map[int64]int64{} + for _, f := range m.Files { + if !f.Required { + t.Errorf("file %d not required", f.ProjectID) + } + seen[f.ProjectID] = f.FileID + } + if seen[111] != 1111 || seen[222] != 2222 { + t.Errorf("CF ids = %v", seen) + } +} + +func TestExportOverridesContents(t *testing.T) { + opts, dest := exportFixture(t) + if err := Export(context.Background(), http.DefaultClient, opts, nil); err != nil { + t.Fatalf("Export: %v", err) + } + files := readZip(t, dest) + + if files["overrides/config/pack.toml"] != "edited-on-server" { + t.Errorf("server-edited config missing or wrong: %q", files["overrides/config/pack.toml"]) + } + if _, ok := files["overrides/config/nested/extra.json"]; !ok { + t.Error("nested config missing") + } + if _, ok := files["overrides/kubejs/server_scripts/recipes.js"]; !ok { + t.Error("kubejs missing") + } + if files["overrides/mods/local-extra.jar"] != "hand-added-jar" { + t.Error("hand-added mod missing from overrides") + } + if _, ok := files["overrides/mods/server-mod.jar"]; ok { + t.Error("CF-covered mod duplicated into overrides") + } + // Client-only direct-URL file downloaded at export time. + if files["overrides/resourcepacks/look.zip"] != "client-only-bytes" { + t.Errorf("clientonly direct file = %q", files["overrides/resourcepacks/look.zip"]) + } + for name := range files { + for _, banned := range []string{"overrides/world/", "overrides/logs/", "overrides/libraries/", + "overrides/server.properties", "overrides/eula.txt", "overrides/neoforge-installer.jar"} { + if strings.HasPrefix(name, banned) { + t.Errorf("banned path in zip: %s", name) + } + } + } +} + +func TestExportEmitsProgressSteps(t *testing.T) { + opts, dest := exportFixture(t) + var steps []string + err := Export(context.Background(), http.DefaultClient, opts, func(p provider.Progress) { + if p.Step != "" { + steps = append(steps, p.Step) + } + }) + if err != nil { + t.Fatalf("Export: %v", err) + } + joined := strings.Join(steps, ",") + if !strings.Contains(joined, "shop.export.preparing") || + !strings.Contains(joined, "shop.export.zipping") { + t.Errorf("steps = %v", steps) + } + if _, err := os.Stat(dest); err != nil { + t.Errorf("dest zip missing: %v", err) + } +} From 9080dcd7738bc9bb1f58e3a4294f952870099610 Mon Sep 17 00:00:00 2001 From: 000hen Date: Sun, 12 Jul 2026 13:58:46 +0800 Subject: [PATCH 13/30] feat(app): modpack update and export actions Server header flyout gains Update/Export modpack items (modpack servers only; update requires the server stopped). Both stream through the global per-server progress tracker: update picks a version fetched from the shop and refreshes the list on completion; export saves a CurseForge client pack zip via a file picker. Co-Authored-By: Claude Fable 5 --- .../Controls/Server/ServerHeaderPanel.xaml | 17 +++ .../Controls/Server/ServerHeaderPanel.xaml.cs | 17 +++ app/JustHostMC.App/Models/ServerItem.cs | 21 +++- .../Strings/en-US/Resources.resw | 16 +++ .../Strings/zh-TW/Resources.resw | 16 +++ .../ViewModels/MainViewModel.cs | 96 ++++++++++++++++ app/JustHostMC.App/Views/ServerPage.xaml | 4 +- app/JustHostMC.App/Views/ServerPage.xaml.cs | 106 ++++++++++++++++++ 8 files changed, 288 insertions(+), 5 deletions(-) diff --git a/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml b/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml index 254b967..d815ff6 100644 --- a/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml +++ b/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml @@ -148,6 +148,23 @@ + + + + + + + + + + private void OnDeleteClick(object sender, RoutedEventArgs e) => DeleteClick?.Invoke(this, e); + private void OnUpdateModpackClick(object sender, RoutedEventArgs e) => + UpdateModpackClick?.Invoke(this, e); + private void OnExportModpackClick(object sender, RoutedEventArgs e) => + ExportModpackClick?.Invoke(this, e); + + /// Modpack actions only exist for servers installed from a + /// modpack (non-empty pack version). + public Visibility ModpackItemVisibility(string providerVersion) => + string.IsNullOrEmpty(providerVersion) ? Visibility.Collapsed + : Visibility.Visible; + + /// Updating replaces pack files, so the server must not be + /// running. + public bool UpdateModpackEnabled(ServerStatus s) => + s is ServerStatus.Stopped or ServerStatus.Crashed; private void OnPanelSizeChanged(object sender, SizeChangedEventArgs e) => UpdateResponsiveLayout(e.NewSize.Width); diff --git a/app/JustHostMC.App/Models/ServerItem.cs b/app/JustHostMC.App/Models/ServerItem.cs index 6f6ea96..34dfe21 100644 --- a/app/JustHostMC.App/Models/ServerItem.cs +++ b/app/JustHostMC.App/Models/ServerItem.cs @@ -55,6 +55,18 @@ public partial string Loader { /// plain providers like fabric/paper/forge). public string EffectiveLoader => Loader.Length > 0 ? Loader : ProviderId; + /// Opaque modpack pack version ("packId/versionId"); empty for + /// normal servers. + [ObservableProperty] + [NotifyPropertyChangedFor(nameof(IsModpackServer))] + public partial string ProviderVersion { + get; private set; + } = ""; + + /// True for servers installed from a modpack — enables the + /// update/export modpack actions. + public bool IsModpackServer => ProviderVersion.Length > 0; + [ObservableProperty] [NotifyPropertyChangedFor(nameof(CanStart))] [NotifyPropertyChangedFor(nameof(CanStop))] @@ -236,10 +248,11 @@ Status is ServerStatus.Starting or ServerStatus.Stopping or /// Refreshes this item from a fresh server snapshot. public void Apply(Server proto) { - Name = proto.Name; - McVersion = proto.McVersion; - ProviderId = proto.ProviderId; - Loader = proto.Loader; + Name = proto.Name; + McVersion = proto.McVersion; + ProviderId = proto.ProviderId; + Loader = proto.Loader; + ProviderVersion = proto.ProviderVersion; Status = proto.Status; Port = proto.Port; MemoryMb = proto.MemoryMb; diff --git a/app/JustHostMC.App/Strings/en-US/Resources.resw b/app/JustHostMC.App/Strings/en-US/Resources.resw index 9040544..3dc4227 100644 --- a/app/JustHostMC.App/Strings/en-US/Resources.resw +++ b/app/JustHostMC.App/Strings/en-US/Resources.resw @@ -328,6 +328,22 @@ Open instance folder + Update modpack… + Export modpack… + Update modpack + Pick a version for "{0}" (currently {1}). The world and your own files are kept; pack files are replaced. + Update + No other versions available. + CurseForge modpack + Preparing modpack export… + Packaging modpack… + Modpack exported. + + Instance folder not found + + + The instance folder has not been created yet, or the daemon is using a data location that is not exposed by this app version. + OP diff --git a/app/JustHostMC.App/Strings/zh-TW/Resources.resw b/app/JustHostMC.App/Strings/zh-TW/Resources.resw index 6772a42..236cc6d 100644 --- a/app/JustHostMC.App/Strings/zh-TW/Resources.resw +++ b/app/JustHostMC.App/Strings/zh-TW/Resources.resw @@ -326,6 +326,22 @@ 開啟執行個體資料夾 + 更新模組包… + 匯出模組包… + 更新模組包 + 為「{0}」選擇版本(目前為 {1})。世界與你自己的檔案會保留,模組包檔案將被更換。 + 更新 + 沒有其他可用版本。 + CurseForge 模組包 + 正在準備匯出模組包… + 正在打包模組包… + 模組包已匯出。 + + 找不到執行個體資料夾 + + + 執行個體資料夾尚未建立,或背景服務正在使用此應用程式版本尚未公開的資料位置。 + OP diff --git a/app/JustHostMC.App/ViewModels/MainViewModel.cs b/app/JustHostMC.App/ViewModels/MainViewModel.cs index 2fbad62..9792ecf 100644 --- a/app/JustHostMC.App/ViewModels/MainViewModel.cs +++ b/app/JustHostMC.App/ViewModels/MainViewModel.cs @@ -245,6 +245,102 @@ public async Task InstallServerAsync(CreateServerRequest request) { } } + /// Moves a modpack server to another pack version, streaming + /// progress into the server's global tracker so the operation survives + /// window/page changes. + public async Task UpdateModpackAsync(ServerItem server, string version) { + var tracker = ProgressService.GetOrCreateTracker(server.Id, server.Name); + RunOnUI(() => { + tracker.InstallLog.Clear(); + tracker.HasFailed = false; + tracker.IsInstalling = true; + tracker.IsActive = true; + tracker.IsReadyToRun = false; + tracker.IsIndeterminate = true; + tracker.ProgressFraction = 0; + tracker.CurrentStep = _localizer.Get("install_progress_preparing"); + }); + try { + var daemon = await App.Current.DaemonReady; + using var call = daemon.Servers.UpdateModpack( + new UpdateModpackRequest { Id = server.Id, Version = version }); + await foreach (var progress in call.ResponseStream.ReadAllAsync() + .ConfigureAwait(false)) { + var snapshot = progress; + RunOnUI(() => ApplyTrackerProgress(tracker, snapshot)); + } + await RefreshAsync(); + RunOnUI(() => { + tracker.IsInstalling = false; + tracker.IsActive = false; + tracker.CurrentStep = _localizer.Get("install_progress_done"); + }); + } catch (RpcException ex) { + FailTracker(tracker, ex); + } + } + + /// Exports a modpack server as a CurseForge client pack zip, + /// streaming progress into the server's global tracker. + public async Task ExportModpackAsync(ServerItem server, string destPath) { + var tracker = ProgressService.GetOrCreateTracker(server.Id, server.Name); + RunOnUI(() => { + tracker.InstallLog.Clear(); + tracker.HasFailed = false; + tracker.IsInstalling = true; + tracker.IsActive = true; + tracker.IsReadyToRun = false; + tracker.IsIndeterminate = true; + tracker.ProgressFraction = 0; + tracker.CurrentStep = _localizer.Get("shop.export.preparing"); + }); + try { + var daemon = await App.Current.DaemonReady; + using var call = daemon.Servers.ExportModpack( + new ExportModpackRequest { Id = server.Id, DestPath = destPath }); + await foreach (var progress in call.ResponseStream.ReadAllAsync() + .ConfigureAwait(false)) { + var snapshot = progress; + RunOnUI(() => ApplyTrackerProgress(tracker, snapshot)); + } + RunOnUI(() => { + tracker.IsInstalling = false; + tracker.IsActive = false; + tracker.CurrentStep = _localizer.Get("shop.export.done"); + }); + } catch (RpcException ex) { + FailTracker(tracker, ex); + } + } + + private void ApplyTrackerProgress(ServerProgressTracker tracker, + InstallProgress snapshot) { + if (snapshot.Step is { Key.Length : > 0 } step) + tracker.CurrentStep = _localizer.Get(step.Key); + if (snapshot.Fraction >= 0) { + tracker.IsIndeterminate = false; + tracker.ProgressFraction = snapshot.Fraction; + } else { + tracker.IsIndeterminate = true; + } + if (!string.IsNullOrEmpty(snapshot.LogLine)) + tracker.AppendLog(snapshot.LogLine); + } + + private void FailTracker(ServerProgressTracker tracker, RpcException ex) { + var key = MapErrorKey(ex); + var detail = ex.Status.Detail; + RunOnUI(() => { + tracker.HasFailed = true; + tracker.IsInstalling = false; + tracker.IsIndeterminate = false; + tracker.IsActive = false; + tracker.CurrentStep = string.IsNullOrEmpty(detail) + ? _localizer.Get(key) + : $"{_localizer.Get(key)}: {detail}"; + }); + } + public async Task UpdateServerAsync(UpdateServerRequest request) { var item = Servers.FirstOrDefault(s => s.Id == request.Id); var rollback = item is null ? null : BuildUpdateRequest(item); diff --git a/app/JustHostMC.App/Views/ServerPage.xaml b/app/JustHostMC.App/Views/ServerPage.xaml index d7cc0df..51c7626 100644 --- a/app/JustHostMC.App/Views/ServerPage.xaml +++ b/app/JustHostMC.App/Views/ServerPage.xaml @@ -21,10 +21,12 @@ BackupsClick="OnBackupsClick" DeleteClick="OnDeleteClick" EditClick="OnEditClick" + ExportModpackClick="OnExportModpackClick" OpenInstanceFolderClick="OnOpenInstanceFolderClick" Server="{x:Bind Server, Mode=OneWay}" StateButtonClick="OnStateButtonClick" - TitleRenameClick="OnTitleRenameClick" /> + TitleRenameClick="OnTitleRenameClick" + UpdateModpackClick="OnUpdateModpackClick" /> Splits "packId/versionId"; returns false for non-modpack + /// servers. + private bool TryGetPackIdentity(out string packId, out string versionId) { + var parts = Server.ProviderVersion.Split('/', 2); + if (parts.Length == 2 && parts[0].Length > 0 && parts[1].Length > 0) { + packId = parts[0]; + versionId = parts[1]; + return true; + } + packId = ""; + versionId = ""; + return false; + } + + private async void OnUpdateModpackClick(object sender, RoutedEventArgs e) { + if (!TryGetPackIdentity(out var packId, out var currentVersionId)) + return; + + ShopVersionList list; + try { + var daemon = await App.Current.DaemonReady; + list = await daemon.Shop.GetVersionsAsync( + new ShopVersionsRequest { + ShopId = Server.ProviderId, + ProjectId = packId, + }, + deadline: DateTime.UtcNow.AddSeconds(30)); + } catch (Exception) { + list = new ShopVersionList(); + } + var current = list.Versions.FirstOrDefault( + v => v.Id == currentVersionId); + var choices = list.Versions.Where(v => v.Id != currentVersionId) + .ToArray(); + if (choices.Length == 0) { + await new ContentDialog { + XamlRoot = XamlRoot, + Title = _localizer.Get("Server_UpdateModpackTitle"), + Content = _localizer.Get("Server_UpdateModpackNoVersions"), + CloseButtonText = _localizer.Get("Common_Cancel"), + DefaultButton = ContentDialogButton.Close, + }.ShowAsync(); + return; + } + + var versionBox = new ComboBox { + ItemsSource = choices.Select(v => v.Name.Length > 0 + ? v.Name + : v.VersionNumber) + .ToArray(), + SelectedIndex = 0, + HorizontalAlignment = HorizontalAlignment.Stretch, + }; + var panel = new StackPanel { Spacing = 12 }; + var currentName = + current is null + ? currentVersionId + : (current.Name.Length > 0 ? current.Name + : current.VersionNumber); + panel.Children.Add(new TextBlock { + Text = string.Format( + _localizer.Get("Server_UpdateModpackBody"), Server.Name, + currentName), + TextWrapping = TextWrapping.Wrap, + }); + panel.Children.Add(versionBox); + + var dialog = new ContentDialog { + XamlRoot = XamlRoot, + Title = _localizer.Get("Server_UpdateModpackTitle"), + Content = panel, + PrimaryButtonText = _localizer.Get("Server_UpdateModpackConfirm"), + CloseButtonText = _localizer.Get("Common_Cancel"), + DefaultButton = ContentDialogButton.Primary, + }; + if (await dialog.ShowAsync() != ContentDialogResult.Primary) + return; + if (versionBox.SelectedIndex < 0) + return; + var picked = choices[versionBox.SelectedIndex]; + _ = _main.UpdateModpackAsync(Server, $"{packId}/{picked.Id}"); + } + + private async void OnExportModpackClick(object sender, RoutedEventArgs e) { + if (!TryGetPackIdentity(out _, out _)) + return; + var picker = new FileSavePicker { + SuggestedFileName = Server.Name, + SuggestedStartLocation = PickerLocationId.DocumentsLibrary, + }; + picker.FileTypeChoices.Add( + _localizer.Get("Server_ExportModpackPickerName"), + new List { ".zip" }); + var hwnd = + WinRT.Interop.WindowNative.GetWindowHandle(App.Current.MainWindow); + WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd); + + var file = await picker.PickSaveFileAsync(); + if (file is not null) + _ = _main.ExportModpackAsync(Server, file.Path); + } + private async void OnTitleRenameClick(object sender, RoutedEventArgs e) => await ShowRenameDialogAsync(); From 095952e3b10369b6e4331e35b6f317a9e653ff07 Mon Sep 17 00:00:00 2001 From: 000hen Date: Sun, 12 Jul 2026 14:29:08 +0800 Subject: [PATCH 14/30] fix(engine): FTB API serves curseforge ids as strings Live export against the real API failed with a JSON unmarshal error: the manifest's curseforge.project/file are numeric strings, not numbers. Accept both via flexInt64 and skip zero ids. Adds an opt-in live-export diagnostic test (JHMC_LIVE_EXPORT_DIR/_PACK). Co-Authored-By: Claude Fable 5 --- engine/internal/modpack/export.go | 29 ++++++++++++--- engine/internal/modpack/export_live_test.go | 40 +++++++++++++++++++++ engine/internal/modpack/export_test.go | 2 +- 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 engine/internal/modpack/export_live_test.go diff --git a/engine/internal/modpack/export.go b/engine/internal/modpack/export.go index 5aedb82..1552baf 100644 --- a/engine/internal/modpack/export.go +++ b/engine/internal/modpack/export.go @@ -15,6 +15,7 @@ import ( "os" "path" "path/filepath" + "strconv" "strings" "github.com/000hen/justhostmc/engine/internal/provider" @@ -50,14 +51,32 @@ type ftbManifest struct { Files []ftbFile `json:"files"` } +// flexInt64 accepts both JSON numbers and numeric strings — the FTB API +// serves curseforge ids as strings. +type flexInt64 int64 + +func (f *flexInt64) UnmarshalJSON(b []byte) error { + s := strings.Trim(string(b), `"`) + if s == "" || s == "null" { + *f = 0 + return nil + } + n, err := strconv.ParseInt(s, 10, 64) + if err != nil { + return fmt.Errorf("not a numeric id: %s", b) + } + *f = flexInt64(n) + return nil +} + type ftbFile struct { Path string `json:"path"` Name string `json:"name"` URL string `json:"url"` Clientonly bool `json:"clientonly"` Curseforge *struct { - Project int64 `json:"project"` - File int64 `json:"file"` + Project flexInt64 `json:"project"` + File flexInt64 `json:"file"` } `json:"curseforge"` } @@ -155,9 +174,11 @@ func Export(ctx context.Context, client *http.Client, o Options, progress func(p continue } switch { - case f.Curseforge != nil: + case f.Curseforge != nil && f.Curseforge.Project != 0 && f.Curseforge.File != 0: out.Files = append(out.Files, cfFile{ - ProjectID: f.Curseforge.Project, FileID: f.Curseforge.File, Required: true, + ProjectID: int64(f.Curseforge.Project), + FileID: int64(f.Curseforge.File), + Required: true, }) covered[f.dest()] = true case f.Clientonly && f.URL != "": diff --git a/engine/internal/modpack/export_live_test.go b/engine/internal/modpack/export_live_test.go new file mode 100644 index 0000000..68e80d0 --- /dev/null +++ b/engine/internal/modpack/export_live_test.go @@ -0,0 +1,40 @@ +package modpack + +import ( + "context" + "fmt" + "net/http" + "os" + "path/filepath" + "testing" + + "github.com/000hen/justhostmc/engine/internal/provider" +) + +// TestExportLiveServer is an opt-in diagnostic: it exports a real installed +// modpack server against the real FTB API. Runs only when +// JHMC_LIVE_EXPORT_DIR (server directory) and JHMC_LIVE_EXPORT_PACK +// ("packId/versionId") are set. +func TestExportLiveServer(t *testing.T) { + dir := os.Getenv("JHMC_LIVE_EXPORT_DIR") + pack := os.Getenv("JHMC_LIVE_EXPORT_PACK") + if dir == "" || pack == "" { + t.Skip("JHMC_LIVE_EXPORT_DIR / JHMC_LIVE_EXPORT_PACK not set") + } + dest := filepath.Join(t.TempDir(), "live-export.zip") + err := Export(context.Background(), http.DefaultClient, Options{ + ServerDir: dir, + DestZip: dest, + PackVersion: pack, + ServerName: "Live Export", + }, func(p provider.Progress) { + if p.Step != "" { + fmt.Printf("[step] %s %.2f\n", p.Step, p.Fraction) + } + }) + if err != nil { + t.Fatalf("Export: %v", err) + } + fi, _ := os.Stat(dest) + fmt.Printf("zip size: %d\n", fi.Size()) +} diff --git a/engine/internal/modpack/export_test.go b/engine/internal/modpack/export_test.go index e29b1d9..24601cf 100644 --- a/engine/internal/modpack/export_test.go +++ b/engine/internal/modpack/export_test.go @@ -25,7 +25,7 @@ const fixtureManifest = `{ ], "files": [ { "path": "./mods/", "name": "server-mod.jar", "clientonly": false, - "curseforge": { "project": 111, "file": 1111 } }, + "curseforge": { "project": "111", "file": "1111" } }, { "path": "./mods/", "name": "client-mod.jar", "clientonly": true, "curseforge": { "project": 222, "file": 2222 } }, { "path": "./config/", "name": "pack.toml", "clientonly": false, From 66c2aa118ded5446af5e967a354a5f66ea6da5db Mon Sep 17 00:00:00 2001 From: 000hen Date: Mon, 13 Jul 2026 13:12:50 +0800 Subject: [PATCH 15/30] feat(proto): ImportModpack RPC New server from a local CurseForge client pack zip or Modrinth .mrpack, streaming install progress like Create. Co-Authored-By: Claude Opus 4.8 --- proto/mcmanager/v1/mcmanager.proto | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/proto/mcmanager/v1/mcmanager.proto b/proto/mcmanager/v1/mcmanager.proto index 04cac29..a46f965 100644 --- a/proto/mcmanager/v1/mcmanager.proto +++ b/proto/mcmanager/v1/mcmanager.proto @@ -76,6 +76,13 @@ message ExportModpackRequest { string id = 1; string dest_path = 2; // absolute .zip path chosen by the user } +message ImportModpackRequest { + string name = 1; // server name + string src_path = 2; // absolute path to a .zip (CurseForge pack) or .mrpack + int32 memory_mb = 3; + int32 port = 4; // 0 = auto-assign + string custom_java_args = 5; +} // -- i18n / message strategy -------------------------------------------------- // Dynamic, display-only messages travel as a localization KEY in the form @@ -810,6 +817,7 @@ service ServerService { rpc Update(UpdateServerRequest) returns (Server); rpc UpdateModpack(UpdateModpackRequest) returns (stream InstallProgress); // manifest-diff move to another pack version rpc ExportModpack(ExportModpackRequest) returns (stream InstallProgress); // CurseForge client pack zip + rpc ImportModpack(ImportModpackRequest) returns (stream InstallProgress); // new server from a local CurseForge/Modrinth pack file rpc Start(ServerId) returns (Empty); rpc Stop(ServerId) returns (Empty); rpc Delete(ServerId) returns (Empty); From 2fee0f737745fefb50222b618413cbae04b3ae39 Mon Sep 17 00:00:00 2001 From: 000hen Date: Mon, 13 Jul 2026 13:25:54 +0800 Subject: [PATCH 16/30] feat(engine): shared modpack Lua lib, unzip prefix, normalized manifest Extract the loader-install steps (fabric/forge/neoforge) and the file download/diff bookkeeping out of ftb.lua into a shared `mplib` module, loaded into every script sandbox by prepare(). New providers (CurseForge, Modrinth, import) build on it instead of copying ~140 lines. Add a {prefix=...} option to jhmc.unzip so a pack's overrides/ tree can be splatted into the server root, and have ftb.lua persist a normalized .jhmc/modpack.json at install/update so export and update work without re-fetching the upstream source. Co-Authored-By: Claude Opus 4.8 --- engine/internal/scripting/builtin/ftb.lua | 262 ++++------------ .../scripting/builtin_lib/modpack.lua | 289 ++++++++++++++++++ engine/internal/scripting/builtinlib.go | 60 ++++ engine/internal/scripting/host.go | 4 + engine/internal/scripting/hostfuncs.go | 22 +- engine/internal/scripting/modpacklib_test.go | 188 ++++++++++++ 6 files changed, 622 insertions(+), 203 deletions(-) create mode 100644 engine/internal/scripting/builtin_lib/modpack.lua create mode 100644 engine/internal/scripting/builtinlib.go create mode 100644 engine/internal/scripting/modpacklib_test.go diff --git a/engine/internal/scripting/builtin/ftb.lua b/engine/internal/scripting/builtin/ftb.lua index 649c63d..925c07a 100644 --- a/engine/internal/scripting/builtin/ftb.lua +++ b/engine/internal/scripting/builtin/ftb.lua @@ -2,8 +2,9 @@ -- server. It is hidden from the create-server UI — the "ftb" shop drives it, -- passing an opaque "/" as the version. install() fetches the -- pack's version manifest, downloads its server files (resolving CurseForge-hosted --- ones with the configured key), then installs the pinned mod loader by --- replicating the exact-version steps of the fabric/forge/neoforge providers. +-- ones with the configured key), installs the pinned mod loader via the shared +-- mplib helpers, and persists a normalized manifest so export/update work without +-- re-fetching the FTB API. meta = { id = "ftb", @@ -26,9 +27,6 @@ meta = { } local FTB_API = "https://api.feed-the-beast.com/v1/modpacks/public" -local FABRIC_META = "https://meta.fabricmc.net/v2" -local FORGE_MAVEN = "https://maven.minecraftforge.net/net/minecraftforge/forge" -local NEOFORGE_MAVEN = "https://maven.neoforged.net/releases/net/neoforged/neoforge" -- versions is required by the provider contract but never a real picker: a -- modpack installs from an opaque "packId/versionId" chosen in the shop, so this @@ -45,162 +43,6 @@ local function curseforge_key(ctx) return cfg.curseforge_api_key or "" end --- join_path turns an FTB file's { path = "./mods/", name = "x.jar" } into a clean --- relative destination, rejecting any parent-directory segment. jhmc.download --- also confines writes to the server dir, so this is defense in depth. -local function join_path(path, name) - local p = (path or ""):gsub("\\", "/") - p = p:gsub("^%./", ""):gsub("^/+", "") - if p ~= "" and not p:match("/$") then - p = p .. "/" - end - local dest = p .. (name or "") - for seg in dest:gmatch("[^/]+") do - if seg == ".." then - error("unsafe file path in modpack: " .. dest) - end - end - return dest -end - --- install_files downloads every server-side file in the manifest through one --- parallel jhmc.download_all batch. Directly-hosted files carry a url; --- CurseForge-hosted ones resolve their real URL inside the batch workers via --- the download-url endpoint (403 = the author disallows third-party --- downloads). The missing-key check runs up front, before anything downloads. -local function install_files(ctx, files) - local key = curseforge_key(ctx) - local items = {} - for _, f in ipairs(files) do - if type(f) == "table" and not f.clientonly and (f.name or "") ~= "" then - local dest = join_path(f.path, f.name) - if (f.url or "") ~= "" then - items[#items + 1] = { dest = dest, sha1 = f.sha1, url = f.url } - else - local cf = f.curseforge - if type(cf) == "table" and cf.project and cf.file then - if key == "" then - error("install failed: file " .. (f.name or dest) .. - " is hosted on CurseForge and needs a CurseForge API key (set it in the FTB provider settings)") - end - items[#items + 1] = { dest = dest, sha1 = f.sha1, resolve = { - url = "https://api.curseforge.com/v1/mods/" .. cf.project .. - "/files/" .. cf.file .. "/download-url", - headers = { ["x-api-key"] = key }, - } } - else - error("install failed: file " .. (f.name or dest) .. " has no download source") - end - end - end - end - if #items == 0 then return end - ctx.step("shop.install.downloading", 0) - jhmc.download_all(items) -end - --- find_args_file walks libraries/ for a generated win_args.txt. jhmc.fs.glob is --- backed by Go's filepath.Glob (no recursive "**"), so we probe nesting depths --- (copied from the forge/neoforge providers). -local function find_args_file() - local prefix = "libraries" - for _ = 1, 8 do - prefix = prefix .. "/*" - local hits = jhmc.fs.glob(prefix .. "/win_args.txt") - if hits[1] then - return hits[1] - end - end - return nil -end - --- detect_launch prefers a generated win_args.txt arg file under libraries/, else --- a runnable non-installer jar matching one of jar_patterns (copied from the --- forge/neoforge providers). -local function detect_launch(jar_patterns) - local args_file = find_args_file() - if args_file then - return { "@" .. args_file, "nogui" } - end - for _, pat in ipairs(jar_patterns) do - for _, jar in ipairs(jhmc.fs.glob(pat)) do - if not jar:lower():find("installer") then - return { "-jar", jar, "nogui" } - end - end - end - error("no win_args.txt or server jar after install") -end - --- fabric_installer returns the newest stable Fabric installer version (the loader --- version itself is pinned by the pack). -local function fabric_installer() - local entries = jhmc.http_json(FABRIC_META .. "/versions/installer") - for _, e in ipairs(entries) do - if e.stable and (e.version or "") ~= "" then return e.version end - end - for _, e in ipairs(entries) do - if (e.version or "") ~= "" then return e.version end - end - error("version not found: fabric installer") -end - -local function install_fabric(ctx, mc, loader_version) - local installer = fabric_installer() - local url = FABRIC_META .. "/versions/loader/" .. mc .. "/" .. loader_version .. - "/" .. installer .. "/server/jar" - ctx.step("install.progress.downloading_server", 0) - ctx.log("server.jar") - jhmc.download(url, { dest = "server.jar" }) - return { "-jar", "server.jar", "nogui" } -end - --- strip_mc_prefix removes a leading "-" from a loader version, so a pack that --- pins forge as either "47.2.0" or "1.20.1-47.2.0" yields the same coordinate. -local function strip_mc_prefix(version, mc) - local pfx = mc .. "-" - if version:sub(1, #pfx) == pfx then - return version:sub(#pfx + 1) - end - return version -end - -local function install_forge(ctx, mc, forge_version, java_major) - local full = mc .. "-" .. strip_mc_prefix(forge_version, mc) - local url = FORGE_MAVEN .. "/" .. full .. "/forge-" .. full .. "-installer.jar" - ctx.step("install.progress.downloading_installer", 0) - ctx.log("forge-" .. full .. "-installer.jar") - jhmc.download(url, { dest = "installer.jar" }) - ctx.step("install.progress.running_installer", -1) - ctx.log("java -jar installer.jar --installServer") - jhmc.run_jar({ java_major = java_major, args = { "-jar", "installer.jar", "--installServer" }, dir = "." }) - return detect_launch({ "forge*.jar", "server.jar" }) -end - -local function install_neoforge(ctx, neoforge_version, java_major) - local url = NEOFORGE_MAVEN .. "/" .. neoforge_version .. - "/neoforge-" .. neoforge_version .. "-installer.jar" - ctx.step("install.progress.downloading_installer", 0) - ctx.log("neoforge-" .. neoforge_version .. "-installer.jar") - jhmc.download(url, { dest = "installer.jar" }) - ctx.step("install.progress.running_installer", -1) - ctx.log("java -jar installer.jar --installServer") - jhmc.run_jar({ java_major = java_major, args = { "-jar", "installer.jar", "--installServer" }, dir = "." }) - return detect_launch({ "neoforge*.jar", "forge*.jar", "server.jar" }) -end - --- install_loader installs the pinned mod loader and returns the launch args. -local function install_loader(ctx, name, version, mc, java_major) - if name == "fabric" then - return install_fabric(ctx, mc, version) - elseif name == "forge" then - return install_forge(ctx, mc, version, java_major) - elseif name == "neoforge" then - return install_neoforge(ctx, version, java_major) - end - error("unsupported modloader: " .. name) -end - -- read_targets extracts the Minecraft version and mod loader (name + pinned -- version) from the manifest's targets array. local function read_targets(manifest) @@ -218,6 +60,43 @@ local function read_targets(manifest) return mc, loader_name, loader_version end +-- normalize_files maps the FTB manifest's files into mplib's normalized entries. +-- Client-only files are kept but flagged so export can carry them while install +-- and update skip them. +local function normalize_files(manifest) + local out = {} + for _, f in ipairs(manifest.files or {}) do + if type(f) == "table" and (f.name or "") ~= "" then + local entry = { dest = mplib.join_path(f.path, f.name), sha1 = f.sha1 } + if (f.url or "") ~= "" then + entry.url = f.url + elseif type(f.curseforge) == "table" and f.curseforge.project and f.curseforge.file then + entry.project_id = f.curseforge.project + entry.file_id = f.curseforge.file + end + if f.clientonly then + entry.client_only = true + end + out[#out + 1] = entry + end + end + return out +end + +-- persist writes the normalized manifest consumed by export and update. +local function persist(manifest, ver, files, mc, loader_name, loader_version) + local version_name = (manifest.name and manifest.name ~= "") and manifest.name or ver + mplib.write_manifest({ + format = 1, + name = manifest.name or "", + version_name = version_name, + mc_version = mc, + loader = loader_name, + loader_version = loader_version, + files = files, + }) +end + function install(ctx) local pack, ver = tostring(ctx.version):match("^([^/]+)/([^/]+)$") if not pack or not ver then @@ -238,10 +117,13 @@ function install(ctx) error("ftb: modpack has no modloader target") end - install_files(ctx, manifest.files or {}) + local files = normalize_files(manifest) + mplib.download_files(ctx, files, curseforge_key(ctx)) local java_major = jhmc.java_major_for(mc) - local args = install_loader(ctx, loader_name, loader_version, mc, java_major) + local args = mplib.install_loader(ctx, loader_name, loader_version, mc, java_major) + + persist(manifest, ver, files, mc, loader_name, loader_version) ctx.step("install.progress.done", 1) return { @@ -253,23 +135,12 @@ function install(ctx) } end --- server_files indexes a manifest's server-side files by relative dest path. -local function server_files(manifest) - local by_dest = {} - for _, f in ipairs(manifest.files or {}) do - if type(f) == "table" and not f.clientonly and (f.name or "") ~= "" then - by_dest[join_path(f.path, f.name)] = f - end - end - return by_dest -end - -- update moves an installed pack to another version of the same pack: files --- only the old version listed are deleted, new/changed ones are downloaded, --- and the loader is reinstalled only when its pinned target changed. Files the --- pack never listed (the world, user-added configs) are untouched; a pack file --- the user edited is overwritten by the new pack version — pack files belong --- to the pack. +-- only the old version listed are deleted, new/changed ones are downloaded, and +-- the loader is reinstalled only when its pinned target changed. Files the pack +-- never listed (the world, user-added configs) are untouched; a pack file the +-- user edited is overwritten by the new pack version — pack files belong to the +-- pack. function update(ctx) local pack, ver = tostring(ctx.version):match("^([^/]+)/([^/]+)$") local opack, over = tostring(ctx.old_version):match("^([^/]+)/([^/]+)$") @@ -291,26 +162,6 @@ function update(ctx) " -> " .. tostring(ctx.version)) end - local old_files, new_files = server_files(old_manifest), server_files(new_manifest) - - -- Delete files the new version dropped. - for dest in pairs(old_files) do - if not new_files[dest] and jhmc.fs.exists(dest) then - ctx.log("- " .. dest) - jhmc.fs.remove(dest) - end - end - - -- Download new files and files whose pack hash changed. - local changed = {} - for dest, f in pairs(new_files) do - local old = old_files[dest] - if not old or (old.sha1 or "") ~= (f.sha1 or "") then - changed[#changed + 1] = f - end - end - install_files(ctx, changed) - local mc, loader_name, loader_version = read_targets(new_manifest) if not mc or mc == "" then error("ftb: modpack has no Minecraft version target") @@ -318,14 +169,23 @@ function update(ctx) if not loader_name or loader_name == "" then error("ftb: modpack has no modloader target") end - local omc, oloader_name, oloader_version = read_targets(old_manifest) + local old_files = normalize_files(old_manifest) + local new_files = normalize_files(new_manifest) + local key = curseforge_key(ctx) + mplib.diff_apply(ctx, mplib.server_index(old_files), mplib.server_index(new_files), { + to_item = function(f) return mplib.to_download_item(f, key) end, + }) + + local omc, oloader_name, oloader_version = read_targets(old_manifest) local java_major = jhmc.java_major_for(mc) local args if loader_name ~= oloader_name or loader_version ~= oloader_version or mc ~= omc then - args = install_loader(ctx, loader_name, loader_version, mc, java_major) + args = mplib.install_loader(ctx, loader_name, loader_version, mc, java_major) end + persist(new_manifest, ver, new_files, mc, loader_name, loader_version) + ctx.step("install.progress.done", 1) return { java_major = java_major, diff --git a/engine/internal/scripting/builtin_lib/modpack.lua b/engine/internal/scripting/builtin_lib/modpack.lua new file mode 100644 index 0000000..6749a0c --- /dev/null +++ b/engine/internal/scripting/builtin_lib/modpack.lua @@ -0,0 +1,289 @@ +-- Shared modpack helpers exposed as the global `mplib`. Providers that install a +-- modpack (FTB, CurseForge, Modrinth, local import) build on these instead of +-- copying the loader-install steps and manifest bookkeeping. Loaded into every +-- sandbox by prepare(); side-effect-free at load time (defines functions only, +-- no network or filesystem access). + +local M = {} + +local FABRIC_META = "https://meta.fabricmc.net/v2" +local FORGE_MAVEN = "https://maven.minecraftforge.net/net/minecraftforge/forge" +local NEOFORGE_MAVEN = "https://maven.neoforged.net/releases/net/neoforged/neoforge" + +-- Where the normalized modpack manifest is persisted inside a server dir. Export +-- and update read it back so they work without re-fetching the upstream source. +local MANIFEST_DIR = ".jhmc" +local MANIFEST_PATH = ".jhmc/modpack.json" + +M.MANIFEST_PATH = MANIFEST_PATH + +-- join_path turns a { path = "./mods/", name = "x.jar" } pair into a clean +-- relative destination, rejecting any parent-directory segment. Host fs calls +-- also confine writes to the server dir, so this is defense in depth. +local function join_path(path, name) + local p = (path or ""):gsub("\\", "/") + p = p:gsub("^%./", ""):gsub("^/+", "") + if p ~= "" and not p:match("/$") then + p = p .. "/" + end + local dest = p .. (name or "") + for seg in dest:gmatch("[^/]+") do + if seg == ".." then + error("unsafe file path in modpack: " .. dest) + end + end + return dest +end +M.join_path = join_path + +-- safe_path validates an already-joined relative path (e.g. a Modrinth file +-- "path" field) the same way join_path validates its output, returning it +-- cleaned. +local function safe_path(p) + p = (p or ""):gsub("\\", "/"):gsub("^%./", ""):gsub("^/+", "") + for seg in p:gmatch("[^/]+") do + if seg == ".." then + error("unsafe file path in modpack: " .. p) + end + end + return p +end +M.safe_path = safe_path + +-- cf_resolve_item builds a download_all entry for a CurseForge-hosted file whose +-- real URL is resolved inside the batch via the download-url endpoint (403 there +-- means the author disallows third-party downloads). +local function cf_resolve_item(dest, sha1, project, file, key) + return { + dest = dest, + sha1 = sha1, + resolve = { + url = "https://api.curseforge.com/v1/mods/" .. project .. + "/files/" .. file .. "/download-url", + headers = { ["x-api-key"] = key }, + }, + } +end +M.cf_resolve_item = cf_resolve_item + +-- find_args_file walks libraries/ for a generated win_args.txt. jhmc.fs.glob is +-- backed by Go's filepath.Glob (no recursive "**"), so probe nesting depths. +local function find_args_file() + local prefix = "libraries" + for _ = 1, 8 do + prefix = prefix .. "/*" + local hits = jhmc.fs.glob(prefix .. "/win_args.txt") + if hits[1] then + return hits[1] + end + end + return nil +end +M.find_args_file = find_args_file + +-- detect_launch prefers a generated win_args.txt arg file under libraries/, else +-- a runnable non-installer jar matching one of jar_patterns. +local function detect_launch(jar_patterns) + local args_file = find_args_file() + if args_file then + return { "@" .. args_file, "nogui" } + end + for _, pat in ipairs(jar_patterns) do + for _, jar in ipairs(jhmc.fs.glob(pat)) do + if not jar:lower():find("installer") then + return { "-jar", jar, "nogui" } + end + end + end + error("no win_args.txt or server jar after install") +end +M.detect_launch = detect_launch + +-- fabric_installer returns the newest stable Fabric installer version (the loader +-- version itself is pinned by the pack). +local function fabric_installer() + local entries = jhmc.http_json(FABRIC_META .. "/versions/installer") + for _, e in ipairs(entries) do + if e.stable and (e.version or "") ~= "" then return e.version end + end + for _, e in ipairs(entries) do + if (e.version or "") ~= "" then return e.version end + end + error("version not found: fabric installer") +end + +local function install_fabric(ctx, mc, loader_version) + local installer = fabric_installer() + local url = FABRIC_META .. "/versions/loader/" .. mc .. "/" .. loader_version .. + "/" .. installer .. "/server/jar" + ctx.step("install.progress.downloading_server", 0) + ctx.log("server.jar") + jhmc.download(url, { dest = "server.jar" }) + return { "-jar", "server.jar", "nogui" } +end +M.install_fabric = install_fabric + +-- strip_mc_prefix removes a leading "-" from a loader version, so a pack that +-- pins forge as either "47.2.0" or "1.20.1-47.2.0" yields the same coordinate. +local function strip_mc_prefix(version, mc) + local pfx = mc .. "-" + if version:sub(1, #pfx) == pfx then + return version:sub(#pfx + 1) + end + return version +end +M.strip_mc_prefix = strip_mc_prefix + +local function install_forge(ctx, mc, forge_version, java_major) + local full = mc .. "-" .. strip_mc_prefix(forge_version, mc) + local url = FORGE_MAVEN .. "/" .. full .. "/forge-" .. full .. "-installer.jar" + ctx.step("install.progress.downloading_installer", 0) + ctx.log("forge-" .. full .. "-installer.jar") + jhmc.download(url, { dest = "installer.jar" }) + ctx.step("install.progress.running_installer", -1) + ctx.log("java -jar installer.jar --installServer") + jhmc.run_jar({ java_major = java_major, args = { "-jar", "installer.jar", "--installServer" }, dir = "." }) + return detect_launch({ "forge*.jar", "server.jar" }) +end +M.install_forge = install_forge + +local function install_neoforge(ctx, neoforge_version, java_major) + local url = NEOFORGE_MAVEN .. "/" .. neoforge_version .. + "/neoforge-" .. neoforge_version .. "-installer.jar" + ctx.step("install.progress.downloading_installer", 0) + ctx.log("neoforge-" .. neoforge_version .. "-installer.jar") + jhmc.download(url, { dest = "installer.jar" }) + ctx.step("install.progress.running_installer", -1) + ctx.log("java -jar installer.jar --installServer") + jhmc.run_jar({ java_major = java_major, args = { "-jar", "installer.jar", "--installServer" }, dir = "." }) + return detect_launch({ "neoforge*.jar", "forge*.jar", "server.jar" }) +end +M.install_neoforge = install_neoforge + +-- install_loader installs the pinned mod loader and returns the launch args. +local function install_loader(ctx, name, version, mc, java_major) + if name == "fabric" then + return install_fabric(ctx, mc, version) + elseif name == "forge" then + return install_forge(ctx, mc, version, java_major) + elseif name == "neoforge" then + return install_neoforge(ctx, version, java_major) + end + error("unsupported modloader: " .. name) +end +M.install_loader = install_loader + +-- to_download_item turns one normalized file entry into a jhmc.download_all item. +-- Directly-hosted files carry a url; CurseForge-hosted ones (project_id + +-- file_id, no url) resolve their real URL in the batch and need an API key. +local function to_download_item(f, key) + if (f.url or "") ~= "" then + return { dest = f.dest, sha1 = f.sha1, url = f.url } + end + if f.project_id and f.file_id then + if (key or "") == "" then + error("install failed: file " .. tostring(f.dest) .. + " is hosted on CurseForge and needs a CurseForge API key (set it in the provider settings)") + end + return cf_resolve_item(f.dest, f.sha1, f.project_id, f.file_id, key) + end + error("install failed: file " .. tostring(f.dest) .. " has no download source") +end +M.to_download_item = to_download_item + +-- download_files downloads the server-side (non-client_only) files of a +-- normalized manifest in one parallel batch. The missing-key check runs up +-- front, before anything downloads. +local function download_files(ctx, files, key) + local items = {} + for _, f in ipairs(files or {}) do + if type(f) == "table" and not f.client_only and (f.dest or "") ~= "" then + items[#items + 1] = to_download_item(f, key) + end + end + if #items == 0 then return end + ctx.step("shop.install.downloading", 0) + jhmc.download_all(items) +end +M.download_files = download_files + +-- index_by_dest turns a normalized files array into a dest -> entry map. +local function index_by_dest(files) + local by_dest = {} + for _, f in ipairs(files or {}) do + if type(f) == "table" and (f.dest or "") ~= "" then + by_dest[f.dest] = f + end + end + return by_dest +end +M.index_by_dest = index_by_dest + +-- server_index is index_by_dest restricted to server-side (non-client_only) +-- files — the set that actually lives on disk, and the correct input for an +-- update diff so a file that was client-only in the old version is treated as +-- new rather than assumed present. +local function server_index(files) + local by_dest = {} + for _, f in ipairs(files or {}) do + if type(f) == "table" and not f.client_only and (f.dest or "") ~= "" then + by_dest[f.dest] = f + end + end + return by_dest +end +M.server_index = server_index + +-- diff_apply reconciles an installed set of files (old_files: dest -> normalized +-- entry) with a target set (new_files). Files the target dropped are deleted; +-- new or changed server files are downloaded in one batch. opts.changed(old, +-- new) decides whether an existing dest must be re-downloaded (default: sha1 +-- differs); opts.to_item(entry) maps a target entry to a download_all item. +local function diff_apply(ctx, old_files, new_files, opts) + local changed = opts.changed or function(old, new) + return (old.sha1 or "") ~= (new.sha1 or "") + end + local to_item = opts.to_item + + -- Delete files the new version dropped (guarded by existence: client-only + -- entries were never written to the server dir). + for dest in pairs(old_files) do + if not new_files[dest] and jhmc.fs.exists(dest) then + ctx.log("- " .. dest) + jhmc.fs.remove(dest) + end + end + + -- Download new files and files whose pack version changed. + local items = {} + for dest, f in pairs(new_files) do + if not f.client_only then + local old = old_files[dest] + if not old or changed(old, f) then + items[#items + 1] = to_item(f) + end + end + end + if #items == 0 then return end + ctx.step("shop.install.downloading", 0) + jhmc.download_all(items) +end +M.diff_apply = diff_apply + +-- write_manifest persists the normalized modpack manifest into the server dir. +local function write_manifest(tbl) + jhmc.fs.mkdir(MANIFEST_DIR) + jhmc.fs.write(MANIFEST_PATH, jhmc.json_encode(tbl)) +end +M.write_manifest = write_manifest + +-- read_manifest loads the normalized manifest, or returns nil when absent. +local function read_manifest() + if not jhmc.fs.exists(MANIFEST_PATH) then + return nil + end + return jhmc.json_decode(jhmc.fs.read(MANIFEST_PATH)) +end +M.read_manifest = read_manifest + +mplib = M diff --git a/engine/internal/scripting/builtinlib.go b/engine/internal/scripting/builtinlib.go new file mode 100644 index 0000000..1b82a1c --- /dev/null +++ b/engine/internal/scripting/builtinlib.go @@ -0,0 +1,60 @@ +package scripting + +import ( + "embed" + "fmt" + "sort" + "strings" + + lua "github.com/yuin/gopher-lua" +) + +// builtin_lib holds shared Lua helper modules (e.g. mplib) replayed into every +// sandbox by prepare(), so provider/shop scripts can build on them instead of +// copying the code. It is a separate embed from builtin/ (which holds runnable +// provider scripts) so LoadBuiltins never picks these up as providers. +// +//go:embed builtin_lib/*.lua +var builtinLibFS embed.FS + +type builtinLib struct { + name string + src string +} + +// builtinLibSources is the ordered set of shared-library sources, read once at +// init. Loading them must be side-effect-free (define functions/constants only — +// no network or filesystem access), because they run on every script prepare(). +var builtinLibSources = mustLoadBuiltinLibs() + +func mustLoadBuiltinLibs() []builtinLib { + entries, err := builtinLibFS.ReadDir("builtin_lib") + if err != nil { + panic(fmt.Sprintf("read builtin_lib: %v", err)) + } + var libs []builtinLib + for _, e := range entries { + if e.IsDir() || !strings.HasSuffix(e.Name(), ".lua") { + continue + } + src, err := builtinLibFS.ReadFile("builtin_lib/" + e.Name()) + if err != nil { + panic(fmt.Sprintf("read builtin_lib/%s: %v", e.Name(), err)) + } + libs = append(libs, builtinLib{name: e.Name(), src: string(src)}) + } + sort.Slice(libs, func(i, j int) bool { return libs[i].name < libs[j].name }) + return libs +} + +// loadBuiltinLibs replays every shared-library source into L. Called after the +// jhmc table is installed and before the script's own source runs, so scripts +// see the library globals (the libs reference jhmc only at call time). +func loadBuiltinLibs(L *lua.LState) error { + for _, lib := range builtinLibSources { + if err := L.DoString(lib.src); err != nil { + return fmt.Errorf("load lib %s: %w", lib.name, err) + } + } + return nil +} diff --git a/engine/internal/scripting/host.go b/engine/internal/scripting/host.go index 7582901..55f1e0f 100644 --- a/engine/internal/scripting/host.go +++ b/engine/internal/scripting/host.go @@ -102,6 +102,10 @@ func (inv *invocation) fail(L *lua.LState, err error) { func (inv *invocation) prepare(src string) (*lua.LState, error) { L := newSandbox(inv.ctx) L.SetGlobal("jhmc", inv.newJHMC(L)) + if err := loadBuiltinLibs(L); err != nil { + L.Close() + return nil, err + } if err := L.DoString(src); err != nil { L.Close() return nil, fmt.Errorf("load script: %w", err) diff --git a/engine/internal/scripting/hostfuncs.go b/engine/internal/scripting/hostfuncs.go index 9bb1da3..4eae8f9 100644 --- a/engine/internal/scripting/hostfuncs.go +++ b/engine/internal/scripting/hostfuncs.go @@ -756,12 +756,20 @@ func (inv *invocation) jsonEncode(L *lua.LState) int { } // unzip extracts a zip (relative path within the server dir) into destDir -// (also within the server dir), guarding against zip-slip. +// (also within the server dir), guarding against zip-slip. An optional third +// arg { prefix = "overrides/" } extracts only entries under prefix, stripping +// it from each destination — used to splat a pack's overrides/ tree into the +// server root. func (inv *invocation) unzip(L *lua.LState) int { inv.requireFS(L) zipPath := inv.resolvePath(L, L.CheckString(1)) destDir := inv.resolvePath(L, L.CheckString(2)) + var prefix string + if opts, ok := L.Get(3).(*lua.LTable); ok { + prefix = strField(opts, "prefix") + } + zr, err := zip.OpenReader(zipPath) if err != nil { inv.fail(L, err) @@ -771,7 +779,17 @@ func (inv *invocation) unzip(L *lua.LState) int { dest := filepath.Clean(destDir) for _, f := range zr.File { - target := filepath.Clean(filepath.Join(dest, f.Name)) + name := filepath.ToSlash(f.Name) + if prefix != "" { + if !strings.HasPrefix(name, prefix) { + continue + } + name = name[len(prefix):] + if name == "" { + continue // the prefix directory entry itself + } + } + target := filepath.Clean(filepath.Join(dest, name)) if r, err := filepath.Rel(dest, target); err != nil || r == ".." || strings.HasPrefix(r, ".."+string(filepath.Separator)) { inv.fail(L, fmt.Errorf("%w: %s", ErrPathEscape, f.Name)) return 0 diff --git a/engine/internal/scripting/modpacklib_test.go b/engine/internal/scripting/modpacklib_test.go new file mode 100644 index 0000000..a98d03f --- /dev/null +++ b/engine/internal/scripting/modpacklib_test.go @@ -0,0 +1,188 @@ +package scripting + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "testing" + + mcmanagerv1 "github.com/000hen/justhostmc/engine/gen/mcmanager/v1" +) + +// fsInv returns an invocation rooted at dir with the fs_server grant, for +// exercising mplib helpers that read and write the server directory. +func fsInv(dir string) *invocation { + return &invocation{ + ctx: context.Background(), + host: NewHost(nil, nil, nil), + baseDir: dir, + granted: GrantSet{mcmanagerv1.PermissionKind_PERMISSION_FS_SERVER: true}, + } +} + +func TestModpackLibGlobalAndJoinPath(t *testing.T) { + inv := &invocation{ctx: context.Background(), host: NewHost(nil, nil, nil)} + src := ` +function check() + assert(type(mplib) == "table", "mplib global missing") + assert(mplib.join_path("./mods/", "a.jar") == "mods/a.jar", "join") + assert(mplib.join_path("", "a.jar") == "a.jar", "join empty path") + assert(mplib.join_path("config\\sub", "b.toml") == "config/sub/b.toml", "join backslash") + local ok = pcall(function() mplib.join_path("../evil", "x") end) + assert(not ok, "traversal must be rejected") +end` + if err := runInv(t, inv, src); err != nil { + t.Fatalf("script failed: %v", err) + } +} + +// TestModpackManifestPersisted validates the exact on-disk shape of +// .jhmc/modpack.json — the contract the Go export path reads back. +func TestModpackManifestPersisted(t *testing.T) { + dir := t.TempDir() + inv := fsInv(dir) + src := ` +function check() + assert(mplib.read_manifest() == nil, "manifest absent before write") + mplib.write_manifest({ + format = 1, + name = "All the Mods 9", + version_name = "0.2.60", + mc_version = "1.20.1", + loader = "forge", + loader_version = "47.2.20", + files = { + { dest = "mods/direct.jar", sha1 = "aa", url = "https://example/direct.jar" }, + { dest = "mods/cf.jar", sha1 = "bb", project_id = 123, file_id = 456 }, + { dest = "mods/client.jar", client_only = true, project_id = 1, file_id = 2 }, + }, + }) + local back = mplib.read_manifest() + assert(back ~= nil and back.name == "All the Mods 9", "round-trip name") + assert(#back.files == 3, "round-trip file count") +end` + if err := runInv(t, inv, src); err != nil { + t.Fatalf("script failed: %v", err) + } + + // Assert the on-disk JSON matches the schema the Go export consumes. + raw, err := os.ReadFile(filepath.Join(dir, ".jhmc", "modpack.json")) + if err != nil { + t.Fatalf("read manifest: %v", err) + } + var m struct { + Format int `json:"format"` + Name string `json:"name"` + VersionName string `json:"version_name"` + MCVersion string `json:"mc_version"` + Loader string `json:"loader"` + LoaderVersion string `json:"loader_version"` + Files []struct { + Dest string `json:"dest"` + Sha1 string `json:"sha1"` + URL string `json:"url"` + ProjectID int64 `json:"project_id"` + FileID int64 `json:"file_id"` + ClientOnly bool `json:"client_only"` + } `json:"files"` + } + if err := json.Unmarshal(raw, &m); err != nil { + t.Fatalf("unmarshal manifest: %v", err) + } + if m.Format != 1 || m.MCVersion != "1.20.1" || m.Loader != "forge" || m.LoaderVersion != "47.2.20" { + t.Fatalf("manifest header wrong: %+v", m) + } + if len(m.Files) != 3 { + t.Fatalf("files = %d, want 3", len(m.Files)) + } + byDest := map[string]int{} + for i, f := range m.Files { + byDest[f.Dest] = i + } + if f := m.Files[byDest["mods/direct.jar"]]; f.URL != "https://example/direct.jar" || f.Sha1 != "aa" { + t.Fatalf("direct file wrong: %+v", f) + } + if f := m.Files[byDest["mods/cf.jar"]]; f.ProjectID != 123 || f.FileID != 456 { + t.Fatalf("cf file wrong: %+v", f) + } + if f := m.Files[byDest["mods/client.jar"]]; !f.ClientOnly { + t.Fatalf("client file should be client_only: %+v", f) + } +} + +func TestModpackToDownloadItem(t *testing.T) { + inv := &invocation{ctx: context.Background(), host: NewHost(nil, nil, nil)} + src := ` +function check() + local u = mplib.to_download_item({ dest = "mods/a.jar", sha1 = "abc", url = "http://x/a.jar" }, "") + assert(u.url == "http://x/a.jar" and u.dest == "mods/a.jar" and u.sha1 == "abc", "direct item") + + local c = mplib.to_download_item({ dest = "mods/b.jar", sha1 = "d", project_id = 1, file_id = 2 }, "KEY") + assert(c.resolve.url:find("/mods/1/files/2/download%-url"), "cf resolve url: " .. c.resolve.url) + assert(c.resolve.headers["x-api-key"] == "KEY", "cf key header") + + local ok, err = pcall(function() + mplib.to_download_item({ dest = "mods/c.jar", project_id = 1, file_id = 2 }, "") + end) + assert(not ok, "missing key must error") + assert(tostring(err):find("CurseForge API key"), "error should mention the key: " .. tostring(err)) +end` + if err := runInv(t, inv, src); err != nil { + t.Fatalf("script failed: %v", err) + } +} + +// TestModpackDiffApplyDeletes covers diff_apply's delete path with no downloads: +// files the new version dropped are removed, unchanged ones are kept. +func TestModpackDiffApplyDeletes(t *testing.T) { + dir := t.TempDir() + inv := fsInv(dir) + src := ` +function check() + local ctx = { log = function() end, step = function() end } + jhmc.fs.write("mods/keep.jar", "K") + jhmc.fs.write("mods/drop.jar", "D") + local old = { + ["mods/keep.jar"] = { dest = "mods/keep.jar", sha1 = "1" }, + ["mods/drop.jar"] = { dest = "mods/drop.jar", sha1 = "1" }, + } + local new = { ["mods/keep.jar"] = { dest = "mods/keep.jar", sha1 = "1" } } + mplib.diff_apply(ctx, old, new, { + to_item = function() error("no downloads expected") end, + }) + assert(jhmc.fs.exists("mods/keep.jar"), "kept file must remain") + assert(not jhmc.fs.exists("mods/drop.jar"), "dropped file must be deleted") +end` + if err := runInv(t, inv, src); err != nil { + t.Fatalf("script failed: %v", err) + } +} + +func TestUnzipPrefix(t *testing.T) { + dir := t.TempDir() + writeTestJar(t, filepath.Join(dir, "pack.zip"), map[string]string{ + "manifest.json": `{"name":"x"}`, + "overrides/config/a.toml": "A", + "overrides/mods/m.jar": "M", + }) + inv := fsInv(dir) + src := ` +function check() + -- Plain unzip extracts everything verbatim. + jhmc.unzip("pack.zip", "full") + assert(jhmc.fs.exists("full/manifest.json"), "full manifest") + assert(jhmc.fs.exists("full/overrides/config/a.toml"), "full override") + + -- Prefixed unzip extracts only overrides/, stripping the prefix. + jhmc.unzip("pack.zip", "srv", { prefix = "overrides/" }) + assert(jhmc.fs.exists("srv/config/a.toml"), "stripped config") + assert(jhmc.fs.read("srv/config/a.toml") == "A", "stripped content") + assert(jhmc.fs.exists("srv/mods/m.jar"), "stripped mod") + assert(not jhmc.fs.exists("srv/manifest.json"), "manifest excluded by prefix") + assert(not jhmc.fs.exists("srv/overrides/config/a.toml"), "prefix not stripped") +end` + if err := runInv(t, inv, src); err != nil { + t.Fatalf("script failed: %v", err) + } +} From 4246b091cefcdd3928cbba0fe7d98ca89c2c1a94 Mon Sep 17 00:00:00 2001 From: 000hen Date: Mon, 13 Jul 2026 13:31:11 +0800 Subject: [PATCH 17/30] feat(engine): export modpacks from persisted manifest Export now reads the server's normalized .jhmc/modpack.json when present, building the CurseForge client zip from it instead of re-fetching the FTB API. This makes Export work for every modpack provider (CurseForge, Modrinth, imported) and offline; the FTB-API path stays as a fallback for legacy FTB servers installed before manifests were persisted. Relax the ExportModpack RPC gate from FTB-only to any modpack server. Co-Authored-By: Claude Opus 4.8 --- engine/internal/grpc/serverservice.go | 10 +- engine/internal/modpack/export.go | 219 ++++++++++++++---- .../internal/modpack/export_manifest_test.go | 143 ++++++++++++ 3 files changed, 321 insertions(+), 51 deletions(-) create mode 100644 engine/internal/modpack/export_manifest_test.go diff --git a/engine/internal/grpc/serverservice.go b/engine/internal/grpc/serverservice.go index 3dde6e0..8f1d77c 100644 --- a/engine/internal/grpc/serverservice.go +++ b/engine/internal/grpc/serverservice.go @@ -458,11 +458,12 @@ func (s *ServerService) ExportModpack(req *mcmanagerv1.ExportModpackRequest, str if !ok { return status.Error(codes.NotFound, "server not found") } - // Export builds the manifest from the FTB API; other (future) modpack - // providers would need their own manifest source. - if rec.ProviderVersion == "" || rec.ProviderID != "ftb" { + // Any modpack server can be exported: providers persist a normalized + // manifest (.jhmc/modpack.json) that Export reads, falling back to the FTB + // API only for legacy FTB servers installed before manifests were persisted. + if rec.ProviderVersion == "" { return errorStatus(codes.FailedPrecondition, mcmanagerv1.ErrorCode_ERROR_CODE_UNSPECIFIED, - "server was not installed from an FTB modpack", nil) + "server was not installed from a modpack", nil) } dest := req.DestPath if !filepath.IsAbs(dest) || !strings.EqualFold(filepath.Ext(dest), ".zip") { @@ -479,6 +480,7 @@ func (s *ServerService) ExportModpack(req *mcmanagerv1.ExportModpackRequest, str DestZip: dest, PackVersion: rec.ProviderVersion, ServerName: rec.Name, + ProviderID: rec.ProviderID, }, send); err != nil { send(provider.Progress{LogLine: "[error] " + err.Error()}) return status.Errorf(codes.Internal, "export modpack: %v", err) diff --git a/engine/internal/modpack/export.go b/engine/internal/modpack/export.go index 1552baf..b317ea9 100644 --- a/engine/internal/modpack/export.go +++ b/engine/internal/modpack/export.go @@ -31,9 +31,36 @@ type Options struct { DestZip string // absolute output .zip path PackVersion string // opaque "packId/versionId" stored on the server record ServerName string // used as the pack name in manifest.json + ProviderID string // provider that installed the server; gates the FTB-API fallback FTBAPIBase string // empty = DefaultFTBAPIBase; overridable in tests } +// normManifest is the normalized modpack manifest providers persist at +// .jhmc/modpack.json. Export prefers it over re-fetching the upstream source, so +// export works for every modpack provider (and offline). +type normManifest struct { + Format int `json:"format"` + Name string `json:"name"` + VersionName string `json:"version_name"` + MCVersion string `json:"mc_version"` + Loader string `json:"loader"` + LoaderVersion string `json:"loader_version"` + Files []normFile `json:"files"` +} + +type normFile struct { + Dest string `json:"dest"` + Sha1 string `json:"sha1"` + URL string `json:"url"` + ProjectID flexInt64 `json:"project_id"` + FileID flexInt64 `json:"file_id"` + ClientOnly bool `json:"client_only"` +} + +// normManifestRelPath is the server-dir-relative location of the persisted +// manifest, matching mplib.MANIFEST_PATH on the Lua side. +var normManifestRelPath = filepath.Join(".jhmc", "modpack.json") + // overrideDirs are the server dirs copied verbatim into overrides/ (when they // exist). mods/ is handled separately so CF-covered jars aren't duplicated. var overrideDirs = []string{ @@ -121,17 +148,109 @@ func report(progress func(provider.Progress), p provider.Progress) { } } -// Export writes a CurseForge-format client pack zip for the FTB pack install -// in o.ServerDir. Manifest-listed files with CurseForge ids (including -// client-only ones) become manifest entries; the server's live config dirs and -// hand-added mods go into overrides/; client-only direct-URL files are -// downloaded at export time. +// Export writes a CurseForge-format client pack zip for the modpack install in +// o.ServerDir. When the server has a persisted .jhmc/modpack.json (every current +// provider writes one), the manifest is built from it; otherwise, for legacy FTB +// servers installed before manifests were persisted, it is fetched from the FTB +// API. Either way, files with CurseForge ids (including client-only ones) become +// manifest entries, the server's live config dirs and hand-added mods go into +// overrides/, and client-only direct-URL files are downloaded at export time. func Export(ctx context.Context, client *http.Client, o Options, progress func(provider.Progress)) error { report(progress, provider.Progress{Step: "shop.export.preparing", Fraction: -1}) + staging, err := os.MkdirTemp("", "jhmc-export-*") + if err != nil { + return err + } + defer os.RemoveAll(staging) + + var ( + out cfManifest + covered map[string]bool + ) + if _, statErr := os.Stat(filepath.Join(o.ServerDir, normManifestRelPath)); statErr == nil { + out, covered, err = buildFromManifest(ctx, client, o, staging, progress) + } else if o.ProviderID == "" || o.ProviderID == "ftb" { + out, covered, err = buildFromFTB(ctx, client, o, staging, progress) + } else { + return fmt.Errorf("no persisted modpack manifest for %q server", o.ProviderID) + } + if err != nil { + return err + } + + report(progress, provider.Progress{Step: "shop.export.zipping", Fraction: -1}) + return writeZip(o, out, staging, covered) +} + +// buildFromManifest constructs the CurseForge manifest from the server's +// persisted .jhmc/modpack.json. CurseForge-hosted files become manifest entries; +// client-only direct-URL files are downloaded into staging (they never lived on +// the server); server-side files are shipped from the live server dir by +// writeZip, so nothing is done for them here. +func buildFromManifest(ctx context.Context, client *http.Client, o Options, staging string, progress func(provider.Progress)) (cfManifest, map[string]bool, error) { + raw, err := os.ReadFile(filepath.Join(o.ServerDir, normManifestRelPath)) + if err != nil { + return cfManifest{}, nil, err + } + var nm normManifest + if err := json.Unmarshal(raw, &nm); err != nil { + return cfManifest{}, nil, fmt.Errorf("read modpack manifest: %w", err) + } + + name := o.ServerName + if name == "" { + name = nm.Name + } + out := cfManifest{ + ManifestType: "minecraftModpack", + ManifestVersion: 1, + Name: name, + Version: nm.VersionName, + Overrides: "overrides", + } + out.Minecraft.Version = nm.MCVersion + if nm.Loader != "" { + id := nm.Loader + if nm.LoaderVersion != "" { + id += "-" + nm.LoaderVersion + } + out.Minecraft.ModLoaders = []cfModLoader{{ID: id, Primary: true}} + } + if out.Minecraft.Version == "" || len(out.Minecraft.ModLoaders) == 0 { + return cfManifest{}, nil, fmt.Errorf("modpack manifest has no minecraft version or loader") + } + + covered := map[string]bool{} + var downloads []stagedDL + for _, f := range nm.Files { + if f.Dest == "" { + continue + } + switch { + case f.ProjectID != 0 && f.FileID != 0: + out.Files = append(out.Files, cfFile{ + ProjectID: int64(f.ProjectID), + FileID: int64(f.FileID), + Required: true, + }) + covered[f.Dest] = true + case f.ClientOnly && f.URL != "": + downloads = append(downloads, stagedDL{dest: f.Dest, url: f.URL, name: path.Base(f.Dest)}) + } + } + if err := downloadStaged(ctx, client, downloads, staging, progress); err != nil { + return cfManifest{}, nil, err + } + return out, covered, nil +} + +// buildFromFTB is the legacy path for FTB servers installed before manifests +// were persisted: it re-fetches the pack version manifest from the FTB API. +func buildFromFTB(ctx context.Context, client *http.Client, o Options, staging string, progress func(provider.Progress)) (cfManifest, map[string]bool, error) { pack, ver, ok := strings.Cut(o.PackVersion, "/") if !ok || pack == "" || ver == "" { - return fmt.Errorf("invalid pack version %q", o.PackVersion) + return cfManifest{}, nil, fmt.Errorf("invalid pack version %q", o.PackVersion) } base := o.FTBAPIBase if base == "" { @@ -139,7 +258,7 @@ func Export(ctx context.Context, client *http.Client, o Options, progress func(p } manifest, err := fetchManifest(ctx, client, fmt.Sprintf("%s/modpack/%s/%s", base, pack, ver)) if err != nil { - return err + return cfManifest{}, nil, err } out := cfManifest{ @@ -162,13 +281,13 @@ func Export(ctx context.Context, client *http.Client, o Options, progress func(p } } if out.Minecraft.Version == "" || !loaderOK { - return fmt.Errorf("pack manifest has no game/modloader target") + return cfManifest{}, nil, fmt.Errorf("pack manifest has no game/modloader target") } // CF-hosted files (server-side AND client-only) become manifest entries; // their local copies must not be duplicated into overrides. covered := map[string]bool{} - var clientDownloads []ftbFile + var downloads []stagedDL for _, f := range manifest.Files { if f.Name == "" { continue @@ -182,49 +301,55 @@ func Export(ctx context.Context, client *http.Client, o Options, progress func(p }) covered[f.dest()] = true case f.Clientonly && f.URL != "": - clientDownloads = append(clientDownloads, f) + downloads = append(downloads, stagedDL{dest: f.dest(), url: f.URL, name: f.Name}) } } + if err := downloadStaged(ctx, client, downloads, staging, progress); err != nil { + return cfManifest{}, nil, err + } + return out, covered, nil +} - // Download client-only direct-URL files to a staging dir (they were never - // installed server-side). - staging, err := os.MkdirTemp("", "jhmc-export-*") - if err != nil { - return err +// stagedDL is one client-only direct-URL file to download into the export +// staging dir (later shipped under overrides/). +type stagedDL struct { + dest string // server-dir-relative destination + url string + name string // label for progress +} + +// downloadStaged fetches client-only direct-URL files into staging in parallel, +// reporting progress. Files were never installed server-side, so they are pulled +// fresh at export time. +func downloadStaged(ctx context.Context, client *http.Client, dls []stagedDL, staging string, progress func(provider.Progress)) error { + if len(dls) == 0 { + return nil } - defer os.RemoveAll(staging) - if len(clientDownloads) > 0 { - g, gctx := errgroup.WithContext(ctx) - g.SetLimit(6) - done := 0 - total := len(clientDownloads) - results := make(chan string, total) - for _, f := range clientDownloads { - g.Go(func() error { - full := filepath.Join(staging, filepath.FromSlash(f.dest())) - if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { - return err - } - if err := downloadTo(gctx, client, f.URL, full); err != nil { - return fmt.Errorf("%s: %w", f.dest(), err) - } - results <- f.Name - return nil - }) - } - go func() { _ = g.Wait(); close(results) }() - for name := range results { - done++ - report(progress, provider.Progress{Step: "shop.install.downloading", - Fraction: float64(done) / float64(total), LogLine: name}) - } - if err := g.Wait(); err != nil { - return err - } + g, gctx := errgroup.WithContext(ctx) + g.SetLimit(6) + total := len(dls) + results := make(chan string, total) + for _, f := range dls { + g.Go(func() error { + full := filepath.Join(staging, filepath.FromSlash(f.dest)) + if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { + return err + } + if err := downloadTo(gctx, client, f.url, full); err != nil { + return fmt.Errorf("%s: %w", f.dest, err) + } + results <- f.name + return nil + }) } - - report(progress, provider.Progress{Step: "shop.export.zipping", Fraction: -1}) - return writeZip(o, out, staging, covered) + go func() { _ = g.Wait(); close(results) }() + done := 0 + for name := range results { + done++ + report(progress, provider.Progress{Step: "shop.install.downloading", + Fraction: float64(done) / float64(total), LogLine: name}) + } + return g.Wait() } func fetchManifest(ctx context.Context, client *http.Client, url string) (*ftbManifest, error) { diff --git a/engine/internal/modpack/export_manifest_test.go b/engine/internal/modpack/export_manifest_test.go new file mode 100644 index 0000000..8d4491a --- /dev/null +++ b/engine/internal/modpack/export_manifest_test.go @@ -0,0 +1,143 @@ +package modpack + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" +) + +// persistedFixture builds a non-FTB modpack server: a server dir carrying a +// persisted .jhmc/modpack.json plus live files, and a server that serves the one +// client-only direct-URL file. The FTB API is deliberately not wired — a correct +// export must never reach it when the manifest is present. +func persistedFixture(t *testing.T) (Options, string) { + t.Helper() + mux := http.NewServeMux() + mux.HandleFunc("/blob/look.zip", func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte("client-only-bytes")) + }) + mux.HandleFunc("/modpack/", func(w http.ResponseWriter, _ *http.Request) { + t.Error("FTB API must not be called when a persisted manifest exists") + http.Error(w, "unexpected", http.StatusInternalServerError) + }) + srv := httptest.NewServer(mux) + t.Cleanup(srv.Close) + + serverDir := t.TempDir() + mustWrite := func(rel, content string) { + full := filepath.Join(serverDir, filepath.FromSlash(rel)) + if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(full, []byte(content), 0o644); err != nil { + t.Fatal(err) + } + } + manifest := fmt.Sprintf(`{ + "format": 1, + "name": "Persisted Pack", + "version_name": "2.0.0", + "mc_version": "1.21.1", + "loader": "neoforge", + "loader_version": "21.1.77", + "files": [ + { "dest": "mods/server-mod.jar", "sha1": "a", "project_id": 111, "file_id": 1111 }, + { "dest": "mods/client-mod.jar", "client_only": true, "project_id": 222, "file_id": 2222 }, + { "dest": "resourcepacks/look.zip", "client_only": true, "url": "%s/blob/look.zip" }, + { "dest": "config/pack.toml", "url": "IGNORED-server-file-from-disk" } + ] + }`, srv.URL) + mustWrite(".jhmc/modpack.json", manifest) + mustWrite("config/pack.toml", "edited-on-server") // server-side → from disk + mustWrite("mods/server-mod.jar", "cf-covered") // covered by CF entry → excluded + mustWrite("mods/local-extra.jar", "hand-added") // not in manifest → included + mustWrite("world/level.dat", "world") // excluded + + dest := filepath.Join(t.TempDir(), "out.zip") + return Options{ + ServerDir: serverDir, + DestZip: dest, + PackVersion: "999/1", // must be ignored in favor of the persisted manifest + ServerName: "Persisted Server", + ProviderID: "curseforge_modpacks", + FTBAPIBase: srv.URL, + }, dest +} + +func TestExportFromPersistedManifest(t *testing.T) { + opts, dest := persistedFixture(t) + if err := Export(context.Background(), http.DefaultClient, opts, nil); err != nil { + t.Fatalf("Export: %v", err) + } + files := readZip(t, dest) + + var m struct { + ManifestType string `json:"manifestType"` + Name string `json:"name"` + Version string `json:"version"` + Overrides string `json:"overrides"` + Minecraft struct { + Version string `json:"version"` + ModLoaders []struct { + ID string `json:"id"` + Primary bool `json:"primary"` + } `json:"modLoaders"` + } `json:"minecraft"` + Files []struct { + ProjectID int64 `json:"projectID"` + FileID int64 `json:"fileID"` + Required bool `json:"required"` + } `json:"files"` + } + if err := json.Unmarshal([]byte(files["manifest.json"]), &m); err != nil { + t.Fatalf("manifest.json: %v (%q)", err, files["manifest.json"]) + } + if m.ManifestType != "minecraftModpack" || m.Name != "Persisted Server" || + m.Version != "2.0.0" || m.Overrides != "overrides" { + t.Errorf("header = type=%q name=%q version=%q overrides=%q", + m.ManifestType, m.Name, m.Version, m.Overrides) + } + if m.Minecraft.Version != "1.21.1" || len(m.Minecraft.ModLoaders) != 1 || + m.Minecraft.ModLoaders[0].ID != "neoforge-21.1.77" || !m.Minecraft.ModLoaders[0].Primary { + t.Errorf("minecraft = %+v", m.Minecraft) + } + // Both the server-side and the client-only CF-hosted mods are listed. + if len(m.Files) != 2 { + t.Fatalf("files = %+v, want 2 CF entries", m.Files) + } + seen := map[int64]int64{} + for _, f := range m.Files { + if !f.Required { + t.Errorf("file %d not required", f.ProjectID) + } + seen[f.ProjectID] = f.FileID + } + if seen[111] != 1111 || seen[222] != 2222 { + t.Errorf("CF ids = %v", seen) + } + + // Overrides: live config + hand-added mod + downloaded client-only file; + // the CF-covered mod must not be duplicated; excluded dirs stay out. + if files["overrides/config/pack.toml"] != "edited-on-server" { + t.Errorf("server config = %q", files["overrides/config/pack.toml"]) + } + if files["overrides/mods/local-extra.jar"] != "hand-added" { + t.Error("hand-added mod missing") + } + if _, ok := files["overrides/mods/server-mod.jar"]; ok { + t.Error("CF-covered mod duplicated into overrides") + } + if files["overrides/resourcepacks/look.zip"] != "client-only-bytes" { + t.Errorf("client-only direct file = %q", files["overrides/resourcepacks/look.zip"]) + } + for name := range files { + if name == "overrides/world/level.dat" { + t.Errorf("banned path in zip: %s", name) + } + } +} From bdec0a2be49e544a3c18e03aa248b7b9a42885dc Mon Sep 17 00:00:00 2001 From: 000hen Date: Mon, 13 Jul 2026 18:02:36 +0800 Subject: [PATCH 18/30] feat(engine): CurseForge modpack source (provider + shop) Add a browseable CurseForge modpack shop (classId 4471) and a hidden provider that installs a pack from its client manifest: download the pack zip, resolve every listed mod's name/hash/url via the batch files endpoint, install the pinned loader, splat overrides/ into the server root, and persist the normalized manifest for export/update. The shared install and update routines live in mplib so the import provider can reuse them. All CurseForge sources share one API key, seeded from the CurseForge shop key. Co-Authored-By: Claude Opus 4.8 --- engine/cmd/engine/main.go | 24 ++- .../scripting/builtin/curseforge_modpacks.lua | 152 +++++++++++++ .../scripting/builtin_lib/modpack.lua | 140 ++++++++++++ .../builtin_shops/curseforge_modpacks.lua | 199 ++++++++++++++++++ engine/internal/scripting/modpack_cf_test.go | 159 ++++++++++++++ 5 files changed, 668 insertions(+), 6 deletions(-) create mode 100644 engine/internal/scripting/builtin/curseforge_modpacks.lua create mode 100644 engine/internal/scripting/builtin_shops/curseforge_modpacks.lua create mode 100644 engine/internal/scripting/modpack_cf_test.go diff --git a/engine/cmd/engine/main.go b/engine/cmd/engine/main.go index 62fad47..e6ffd14 100644 --- a/engine/cmd/engine/main.go +++ b/engine/cmd/engine/main.go @@ -135,22 +135,34 @@ func main() { // through a disk-backed ETag cache. Keyed sources resolve their API key // from the user's settings first, then the baked-in build default. host.SetHTTPCache(httpcache.New(paths.HTTPCache(), 0)) - bakedShopKeys := map[string]string{"curseforge": defaultCurseForgeKey} + // All CurseForge sources (the mod shop, the modpack shop, and CF-hosted pack + // files) share one API key; the modpack shop reuses the mod shop's user key. + bakedShopKeys := map[string]string{ + "curseforge": defaultCurseForgeKey, + "curseforge_modpacks": defaultCurseForgeKey, + } shopKey := func(shopID string) string { if s, err := settingsStore.Load(); err == nil { if k := s.ShopKeys[shopID]; k != "" { return k } + if shopID == "curseforge_modpacks" { + if k := s.ShopKeys["curseforge"]; k != "" { + return k + } + } } return bakedShopKeys[shopID] } - // The ftb modpack provider reuses the CurseForge shop key for CF-hosted pack - // files, so a user who set it once for the shop needn't re-enter it. The raw + // Modpack providers that pull CF-hosted files reuse the CurseForge shop key, + // so a user who set it once for the shop needn't re-enter it. The raw // providerConfig still backs GetConfig/SetConfig (the UI shows the unset // value); only the ctx.config injected at install time gets the fallback. - providers.SetConfigStore(scripting.NewFallbackConfigReader( - providerConfig, "ftb", "curseforge_api_key", - func() string { return shopKey("curseforge") })) + curseKey := func() string { return shopKey("curseforge") } + providerCfg := scripting.NewFallbackConfigReader(providerConfig, "ftb", "curseforge_api_key", curseKey) + providerCfg = scripting.NewFallbackConfigReader(providerCfg, "curseforge_modpacks", "curseforge_api_key", curseKey) + providerCfg = scripting.NewFallbackConfigReader(providerCfg, "import", "curseforge_api_key", curseKey) + providers.SetConfigStore(providerCfg) shopGrants := scripting.NewGrantStore(filepath.Join(paths.Base, "shop-grants.json")) shops := scripting.NewShopSet(host, shopGrants, shopKey) shopConfig := scripting.NewConfigStore(filepath.Join(paths.Base, "shop-config.json")) diff --git a/engine/internal/scripting/builtin/curseforge_modpacks.lua b/engine/internal/scripting/builtin/curseforge_modpacks.lua new file mode 100644 index 0000000..ab44f20 --- /dev/null +++ b/engine/internal/scripting/builtin/curseforge_modpacks.lua @@ -0,0 +1,152 @@ +-- CurseForge modpack provider: installs a CurseForge modpack as a self-contained +-- server from its client pack (manifest.json + overrides/). Hidden from the +-- create-server UI — the "curseforge_modpacks" shop drives it, passing an opaque +-- "/" as the version. install() downloads the pack zip, +-- resolves its listed mods through the CurseForge API, installs the pinned loader +-- (shared mplib helpers), and persists a normalized manifest so export/update work +-- without re-fetching. The CurseForge API key is reused from the CurseForge shop. + +meta = { + id = "curseforge_modpacks", + name = "CurseForge", + website = "https://www.curseforge.com", + description = "Installs a CurseForge modpack as a self-contained server.", + version = "1.0.0", + author = "JustHostMC", + mod_layout = "mods", + hidden = true, + permissions = { + { kind = "network", reason = "Download the modpack, resolve its files via the CurseForge API, and fetch the loader installer." }, + { kind = "install", reason = "Resolve a JRE and run the Forge/NeoForge installer's --installServer." }, + { kind = "fs_server", reason = "Write the pack's mods/configs and detect the launch command." }, + }, + config = { + { key = "curseforge_api_key", type = "secret", name = "CurseForge API key", + description = "Required to resolve and download CurseForge pack files", required = false }, + }, +} + +local API = "https://api.curseforge.com" + +function versions() + return {} +end + +local function key(ctx) + local cfg = ctx.config or {} + return cfg.curseforge_api_key or "" +end + +-- pack_zip_url resolves the direct download URL for a CurseForge pack file, +-- falling back to the dedicated download-url endpoint when downloadUrl is null. +local function pack_zip_url(project, file, k) + local res = jhmc.http({ + url = API .. "/v1/mods/" .. project .. "/files/" .. file, + headers = { ["x-api-key"] = k }, + }) + if res.status < 200 or res.status >= 300 then + error("curseforge: HTTP " .. res.status .. " resolving pack file " .. project .. "/" .. file) + end + local f = jhmc.json_decode(res.body).data or {} + local url = f.downloadUrl + if not url or url == "" then + local r2 = jhmc.http({ + url = API .. "/v1/mods/" .. project .. "/files/" .. file .. "/download-url", + headers = { ["x-api-key"] = k }, + }) + if r2.status >= 200 and r2.status < 300 then + url = jhmc.json_decode(r2.body).data + end + end + if not url or url == "" then + error("curseforge: modpack file not distributable (author disabled third-party downloads)") + end + return url +end + +-- fetch_pack downloads the pack zip for "/" to .jhmc/pack.zip. +local function fetch_pack(ctx, project, file, k) + local url = pack_zip_url(project, file, k) + ctx.step("install.progress.downloading_server", 0) + ctx.log("modpack.zip") + jhmc.fs.mkdir(".jhmc") + jhmc.download(url, { dest = ".jhmc/pack.zip" }) +end + +local function parse_version(v) + local project, file = tostring(v):match("^([^/]+)/([^/]+)$") + if not project or not file then + error("invalid modpack version id: " .. tostring(v)) + end + return project, file +end + +function install(ctx) + local project, file = parse_version(ctx.version) + local k = key(ctx) + if k == "" then + error("this modpack is hosted on CurseForge and needs a CurseForge API key (set it in the provider settings)") + end + + ctx.step("install.progress.resolving_version", -1) + fetch_pack(ctx, project, file, k) + + local spec = mplib.install_cf_pack(ctx, ".jhmc/pack.zip", k) + jhmc.fs.remove(".jhmc/pack.zip") + + ctx.step("install.progress.done", 1) + spec.pack_version = tostring(ctx.version) + return spec +end + +function update(ctx) + local project, file = parse_version(ctx.version) + local oproject = parse_version(ctx.old_version) + if project ~= oproject then + error("update must stay within the same modpack (" .. oproject .. " -> " .. project .. ")") + end + local k = key(ctx) + if k == "" then + error("this modpack is hosted on CurseForge and needs a CurseForge API key (set it in the provider settings)") + end + local old = mplib.read_manifest() + if not old then + error("cannot update: this server has no saved modpack manifest") + end + + ctx.step("install.progress.resolving_version", -1) + fetch_pack(ctx, project, file, k) + local m = mplib.cf_pack_meta(".jhmc/pack.zip", k) + + mplib.diff_apply(ctx, mplib.server_index(old.files or {}), mplib.server_index(m.files), { + changed = function(a, b) return tostring(a.file_id or "") ~= tostring(b.file_id or "") end, + to_item = function(x) return mplib.to_download_item(x, k) end, + }) + jhmc.unzip(".jhmc/pack.zip", ".", { prefix = "overrides/" }) + + local java_major = jhmc.java_major_for(m.mc) + local args + if m.loader_name ~= old.loader or m.loader_version ~= old.loader_version or m.mc ~= old.mc_version then + args = mplib.install_loader(ctx, m.loader_name, m.loader_version, m.mc, java_major) + end + + mplib.write_manifest({ + format = 1, + name = m.name, + version_name = m.version, + mc_version = m.mc, + loader = m.loader_name, + loader_version = m.loader_version, + files = m.files, + }) + jhmc.fs.remove(".jhmc/pack.zip") + + ctx.step("install.progress.done", 1) + return { + java_major = java_major, + args = args, + mc_version = m.mc, + loader = m.loader_name, + pack_version = tostring(ctx.version), + } +end diff --git a/engine/internal/scripting/builtin_lib/modpack.lua b/engine/internal/scripting/builtin_lib/modpack.lua index 6749a0c..a701f86 100644 --- a/engine/internal/scripting/builtin_lib/modpack.lua +++ b/engine/internal/scripting/builtin_lib/modpack.lua @@ -286,4 +286,144 @@ local function read_manifest() end M.read_manifest = read_manifest +-- ── CurseForge client pack (manifest.json + overrides/) ───────────────────── + +local CF_API = "https://api.curseforge.com" + +-- parse_loader splits a CurseForge modLoader id ("forge-47.2.0") into loader +-- name and version. +local function parse_loader(id) + local name, ver = tostring(id):match("^([^-]+)-(.+)$") + if not name then + return string.lower(tostring(id)), nil + end + return string.lower(name), ver +end +M.parse_loader = parse_loader + +local function cf_post(url, body_tbl, key) + if (key or "") == "" then + error("this modpack is hosted on CurseForge and needs a CurseForge API key (set it in the provider settings)") + end + local res = jhmc.http({ + url = url, + method = "POST", + body = jhmc.json_encode(body_tbl), + headers = { ["x-api-key"] = key, ["Content-Type"] = "application/json" }, + }) + if res.status < 200 or res.status >= 300 then + error("curseforge: HTTP " .. res.status .. " for " .. url) + end + return jhmc.json_decode(res.body) +end + +local function cf_sha1(f) + for _, h in ipairs(f.hashes or {}) do + if h.algo == 1 then return h.value end -- HashAlgo 1 = Sha1 + end + return nil +end + +-- cf_pack_meta reads a CurseForge client pack's manifest.json from a zip already +-- on disk, resolves every listed file's real name/hash/url via one batch call, +-- and returns { name, version, mc, loader_name, loader_version, files } with +-- files in mplib's normalized shape. No filesystem writes — install/update layer +-- their own steps on top. CurseForge manifests carry only project+file ids, so a +-- key is required to learn the filenames. +local function cf_pack_meta(zip_rel, key) + local manifest = jhmc.json_decode(jhmc.zip_read(zip_rel, "manifest.json")) + if type(manifest) ~= "table" or type(manifest.minecraft) ~= "table" then + error("not a CurseForge modpack: manifest.json missing the minecraft block") + end + local mc = manifest.minecraft.version + if not mc or mc == "" then + error("CurseForge pack has no Minecraft version") + end + local loader_name, loader_version + for _, ml in ipairs(manifest.minecraft.modLoaders or {}) do + if ml.primary or not loader_name then + loader_name, loader_version = parse_loader(ml.id) + end + end + if not loader_name then + error("CurseForge pack has no mod loader") + end + + -- Resolve names/hashes/urls for all files in one batch request. + local file_ids = {} + for _, f in ipairs(manifest.files or {}) do + if f.fileID then + file_ids[#file_ids + 1] = f.fileID + end + end + local details = {} + if #file_ids > 0 then + local body = cf_post(CF_API .. "/v1/mods/files", { fileIds = file_ids }, key) + for _, d in ipairs(body.data or {}) do + details[tostring(d.id)] = d + end + end + + local files = {} + for _, f in ipairs(manifest.files or {}) do + local d = details[tostring(f.fileID)] + if d then + -- CurseForge client packs place all listed mods under mods/. + local entry = { + dest = "mods/" .. d.fileName, + sha1 = cf_sha1(d), + project_id = f.projectID, + file_id = f.fileID, + } + if (d.downloadUrl or "") ~= "" then + entry.url = d.downloadUrl + end + files[#files + 1] = entry + elseif f.required ~= false then + error("CurseForge pack references a file that is no longer available (project " .. + tostring(f.projectID) .. ", file " .. tostring(f.fileID) .. ")") + end + end + + return { + name = manifest.name or "", + version = manifest.version or "", + mc = mc, + loader_name = loader_name, + loader_version = loader_version, + files = files, + } +end +M.cf_pack_meta = cf_pack_meta + +-- install_cf_pack installs a CurseForge client pack whose zip is already on disk +-- at zip_rel: it downloads the listed mods, splats overrides/ into the server +-- root, installs the pinned loader, and persists the normalized manifest. The +-- returned launch spec has no pack_version — the caller sets it. +local function install_cf_pack(ctx, zip_rel, key) + local m = cf_pack_meta(zip_rel, key) + download_files(ctx, m.files, key) + jhmc.unzip(zip_rel, ".", { prefix = "overrides/" }) + + local java_major = jhmc.java_major_for(m.mc) + local args = install_loader(ctx, m.loader_name, m.loader_version, m.mc, java_major) + + write_manifest({ + format = 1, + name = m.name, + version_name = m.version, + mc_version = m.mc, + loader = m.loader_name, + loader_version = m.loader_version, + files = m.files, + }) + return { + java_major = java_major, + args = args, + mc_version = m.mc, + loader = m.loader_name, + } +end +M.install_cf_pack = install_cf_pack + mplib = M diff --git a/engine/internal/scripting/builtin_shops/curseforge_modpacks.lua b/engine/internal/scripting/builtin_shops/curseforge_modpacks.lua new file mode 100644 index 0000000..2a5e1d4 --- /dev/null +++ b/engine/internal/scripting/builtin_shops/curseforge_modpacks.lua @@ -0,0 +1,199 @@ +-- CurseForge modpack shop. Talks to the same CurseForge REST API as the mod +-- shop, filtered to classId 4471 (modpacks), and browses packs to install as new +-- servers. Needs an x-api-key (meta.needs_key); the engine injects it via +-- ctx.config.api_key, reusing the CurseForge shop key. A modpack is not a single +-- downloadable file — it installs as a whole server through the +-- "curseforge_modpacks" provider, so resolve_file is intentionally an error. + +meta = { + id = "curseforge_modpacks", + name = "CurseForge", + website = "https://www.curseforge.com", + description = "Browse and install CurseForge modpacks as new servers.", + version = "1.0.0", + author = "JustHostMC", + kinds = { "modpack" }, + needs_key = true, + permissions = { + { kind = "network", reason = "Query the CurseForge API to browse and install modpacks" }, + }, +} + +local API = "https://api.curseforge.com" +local GAME_MINECRAFT = 432 +local CLASS_MODPACKS = 4471 + +local TTL_BROWSE = 120 +local TTL_DETAIL = 600 + +-- ModsSearchSortField enum (docs #tocS_ModsSearchSortField). +local SORTS = { relevance = 2, downloads = 6, follows = 2, newest = 11, updated = 3 } + +local function urlencode(s) + return (string.gsub(s, "[^%w%-%._~]", function(c) + return string.format("%%%02X", string.byte(c)) + end)) +end + +local api_key -- set per call from ctx.config + +local function check(res, url) + if res.status == 404 then error("curseforge: not found: " .. url) end + if res.status == 401 or res.status == 403 then + error("curseforge: HTTP " .. res.status .. " (invalid or missing API key)") + end + if res.status < 200 or res.status >= 300 then + error("curseforge: HTTP " .. res.status .. " for " .. url) + end + return jhmc.json_decode(res.body) +end + +local function get(url, ttl) + return check(jhmc.http_cache({ url = url, ttl = ttl, headers = { ["x-api-key"] = api_key } }), url) +end + +local function post(url, body_tbl) + return check(jhmc.http({ + url = url, + method = "POST", + body = jhmc.json_encode(body_tbl), + headers = { ["x-api-key"] = api_key, ["Content-Type"] = "application/json" }, + }), url) +end + +local function project_card(m) + local cats = {} + for _, c in ipairs(m.categories or {}) do cats[#cats + 1] = c.name end + return { + project_id = tostring(m.id), + slug = m.slug, + title = m.name, + summary = m.summary, + icon_url = m.logo and m.logo.thumbnailUrl or "", + author = m.authors and m.authors[1] and m.authors[1].name or "", + downloads = m.downloadCount, + follows = m.thumbsUpCount, + categories = cats, + project_type = "modpack", + } +end + +-- search_url builds /v1/mods/search for modpacks. Packs pin their own MC version +-- and loader, so no loader dimension is applied. +local function search_url(ctx, sort_field, offset, limit) + local url = API .. "/v1/mods/search?gameId=" .. GAME_MINECRAFT + .. "&classId=" .. CLASS_MODPACKS + .. "&sortField=" .. sort_field .. "&sortOrder=desc" + .. "&index=" .. offset .. "&pageSize=" .. limit + if (ctx.query or "") ~= "" then + url = url .. "&searchFilter=" .. urlencode(ctx.query) + end + return url +end + +function search(ctx) + api_key = ctx.config.api_key + local sort_field = SORTS[ctx.sort] or SORTS.relevance + local body = get(search_url(ctx, sort_field, ctx.offset or 0, ctx.limit or 20), TTL_BROWSE) + local projects = {} + for _, m in ipairs(body.data or {}) do + projects[#projects + 1] = project_card(m) + end + local total = body.pagination and body.pagination.totalCount or 0 + return { projects = projects, total = total } +end + +function home(ctx) + api_key = ctx.config.api_key + local function popular_section() + local popular = get(search_url({}, SORTS.downloads, 0, 12), TTL_BROWSE) + local projects = {} + for _, m in ipairs(popular.data or {}) do + projects[#projects + 1] = project_card(m) + end + return { title_key = "shop.home.popular", projects = projects } + end + local function updated_section() + local recent = get(search_url({}, SORTS.updated, 0, 12), TTL_BROWSE) + local projects = {} + for _, m in ipairs(recent.data or {}) do + projects[#projects + 1] = project_card(m) + end + return { title_key = "shop.home.updated", projects = projects } + end + return { sections = { popular_section(), updated_section() } } +end + +function detail(ctx) + api_key = ctx.config.api_key + local m = get(API .. "/v1/mods/" .. urlencode(ctx.project_id), TTL_DETAIL).data + local desc = get(API .. "/v1/mods/" .. urlencode(ctx.project_id) .. "/description", TTL_DETAIL).data + local gallery = {} + for _, sshot in ipairs(m.screenshots or {}) do + gallery[#gallery + 1] = { url = sshot.url, title = sshot.title, description = sshot.description } + end + local gvs, seen = {}, {} + for _, idx in ipairs(m.latestFilesIndexes or {}) do + if idx.gameVersion and not seen[idx.gameVersion] then + seen[idx.gameVersion] = true + gvs[#gvs + 1] = idx.gameVersion + end + end + return { + project = project_card(m), + body = desc, + body_format = "html", + gallery = gallery, + links = { + website = m.links and m.links.websiteUrl or "", + source = m.links and m.links.sourceUrl or "", + issues = m.links and m.links.issuesUrl or "", + wiki = m.links and m.links.wikiUrl or "", + }, + game_versions = gvs, + license = "", + updated = m.dateModified, + } +end + +local CHANNELS = { [1] = "release", [2] = "beta", [3] = "alpha" } + +local function sha1_of(f) + for _, h in ipairs(f.hashes or {}) do + if h.algo == 1 then return h.value end -- HashAlgo 1 = Sha1 + end + return nil +end + +function versions(ctx) + api_key = ctx.config.api_key + -- All pack files, newest-first; a modpack file is not loader-filtered. + local body = get(API .. "/v1/mods/" .. urlencode(ctx.project_id) .. "/files?pageSize=50", TTL_BROWSE) + local out = {} + for _, f in ipairs(body.data or {}) do + local gvs = {} + for _, sgv in ipairs(f.sortableGameVersions or {}) do + if sgv.gameVersion and sgv.gameVersion ~= "" then + gvs[#gvs + 1] = sgv.gameVersion + end + end + out[#out + 1] = { + id = tostring(f.id), + name = f.displayName, + version_number = f.fileName, + channel = CHANNELS[f.releaseType] or "", + game_versions = gvs, + date = f.fileDate, + downloads = f.downloadCount, + filename = f.fileName, + size = f.fileLength, + } + end + return { versions = out } +end + +function resolve_file(ctx) + -- Required by the shop contract but never reached by the modpack flow: a pack + -- installs as a whole server by the "curseforge_modpacks" provider. + error("not distributable: CurseForge modpacks install as a new server") +end diff --git a/engine/internal/scripting/modpack_cf_test.go b/engine/internal/scripting/modpack_cf_test.go new file mode 100644 index 0000000..b34a771 --- /dev/null +++ b/engine/internal/scripting/modpack_cf_test.go @@ -0,0 +1,159 @@ +package scripting + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "net/url" + "path/filepath" + "testing" + + mcmanagerv1 "github.com/000hen/justhostmc/engine/gen/mcmanager/v1" +) + +// builtinProvider loads the embedded providers and returns one by id. +func builtinProvider(t *testing.T, id string) *Entry { + t.Helper() + r := NewRegistry(NewHost(http.DefaultClient, nil, nil), nil) + if err := LoadBuiltins(context.Background(), r); err != nil { + t.Fatalf("LoadBuiltins: %v", err) + } + e, ok := r.Get(id) + if !ok { + t.Fatalf("%s provider not registered", id) + } + return e +} + +func TestCurseForgeModpacksProviderHidden(t *testing.T) { + e := builtinProvider(t, "curseforge_modpacks") + if !e.Meta.Hidden { + t.Error("curseforge_modpacks provider should be hidden") + } + if e.Meta.ModLayout != "mods" { + t.Errorf("mod_layout = %q, want mods", e.Meta.ModLayout) + } + var hasKey bool + for _, c := range e.Meta.Config { + if c.Key == "curseforge_api_key" && c.Type == mcmanagerv1.ConfigOptionType_CONFIG_OPTION_SECRET { + hasKey = true + } + } + if !hasKey { + t.Errorf("should declare a curseforge_api_key secret, config=%+v", e.Meta.Config) + } +} + +func TestCurseForgeModpacksShopIsModpackKind(t *testing.T) { + ss := newBuiltinShops(t, http.NotFoundHandler()) + sh, ok := ss.Get("curseforge_modpacks") + if !ok { + t.Fatal("curseforge_modpacks shop not registered") + } + if !sh.Meta().NeedsKey { + t.Error("curseforge_modpacks shop should need a key") + } + var isModpack bool + for _, k := range sh.Meta().Kinds { + if k == "modpack" { + isModpack = true + } + } + if !isModpack { + t.Errorf("kinds = %v, want to contain modpack", sh.Meta().Kinds) + } +} + +// TestCFPackMeta covers parsing a CurseForge client pack's manifest.json and +// resolving its file names/hashes/urls through the batch files endpoint. +func TestCFPackMeta(t *testing.T) { + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost && r.URL.Path == "/v1/mods/files" { + _ = json.NewEncoder(w).Encode(map[string]any{ + "data": []map[string]any{ + {"id": 100, "modId": 10, "fileName": "cool-1.0.jar", + "downloadUrl": "http://edge/cool-1.0.jar", + "hashes": []map[string]any{{"algo": 1, "value": "aaa"}}}, + {"id": 200, "modId": 20, "fileName": "neat-2.0.jar", + "downloadUrl": nil, // author-restricted → resolved later via download-url + "hashes": []map[string]any{{"algo": 1, "value": "bbb"}}}, + }, + }) + return + } + http.NotFound(w, r) + }) + srv := httptest.NewServer(handler) + t.Cleanup(srv.Close) + u, _ := url.Parse(srv.URL) + client := &http.Client{Transport: rewriteTransport{target: u}} + + dir := t.TempDir() + manifest := `{ + "minecraft": { "version": "1.20.1", "modLoaders": [{"id":"forge-47.2.0","primary":true}] }, + "manifestType": "minecraftModpack", "name": "Test Pack", "version": "1.0.0", + "files": [ + {"projectID":10,"fileID":100,"required":true}, + {"projectID":20,"fileID":200,"required":true} + ], + "overrides": "overrides" + }` + writeTestJar(t, filepath.Join(dir, ".jhmc", "pack.zip"), map[string]string{"manifest.json": manifest}) + + inv := &invocation{ + ctx: context.Background(), + host: NewHost(client, nil, nil), + baseDir: dir, + granted: GrantSet{ + mcmanagerv1.PermissionKind_PERMISSION_NETWORK: true, + mcmanagerv1.PermissionKind_PERMISSION_FS_SERVER: true, + }, + } + src := ` +function check() + local m = mplib.cf_pack_meta(".jhmc/pack.zip", "KEY") + assert(m.mc == "1.20.1", "mc " .. tostring(m.mc)) + assert(m.loader_name == "forge" and m.loader_version == "47.2.0", "loader") + assert(m.name == "Test Pack" and m.version == "1.0.0", "name/version") + assert(#m.files == 2, "files " .. #m.files) + local by = {} + for _, f in ipairs(m.files) do by[f.dest] = f end + local a = by["mods/cool-1.0.jar"] + assert(a and a.url == "http://edge/cool-1.0.jar", "cool url") + assert(tostring(a.project_id) == "10" and tostring(a.file_id) == "100", "cool ids") + assert(a.sha1 == "aaa", "cool sha1") + local b = by["mods/neat-2.0.jar"] + assert(b ~= nil, "neat present") + assert(b.url == nil or b.url == "", "neat has no direct url") + assert(b.sha1 == "bbb", "neat sha1") + assert(tostring(b.project_id) == "20", "neat project") +end` + if err := runInv(t, inv, src); err != nil { + t.Fatalf("script failed: %v", err) + } +} + +// TestCFPackMetaRequiresKey verifies the manifest lookup fails clearly without a +// CurseForge API key. +func TestCFPackMetaRequiresKey(t *testing.T) { + dir := t.TempDir() + manifest := `{ + "minecraft": { "version": "1.20.1", "modLoaders": [{"id":"forge-47.2.0","primary":true}] }, + "name": "P", "version": "1", "files": [{"projectID":10,"fileID":100}] + }` + writeTestJar(t, filepath.Join(dir, ".jhmc", "pack.zip"), map[string]string{"manifest.json": manifest}) + inv := &invocation{ + ctx: context.Background(), + host: NewHost(http.DefaultClient, nil, nil), + baseDir: dir, + granted: GrantSet{ + mcmanagerv1.PermissionKind_PERMISSION_NETWORK: true, + mcmanagerv1.PermissionKind_PERMISSION_FS_SERVER: true, + }, + } + err := runInv(t, inv, `function check() mplib.cf_pack_meta(".jhmc/pack.zip", "") end`) + if err == nil { + t.Fatal("expected an error without an API key") + } +} From 8f69a0a5545445941739cb9684df27cdc0594cbf Mon Sep 17 00:00:00 2001 From: 000hen Date: Mon, 13 Jul 2026 18:06:32 +0800 Subject: [PATCH 19/30] feat(engine): Modrinth modpack source (provider + shop) Add a keyless Modrinth modpack shop (project_type:modpack) and a hidden provider that installs a pack from its .mrpack: download the mrpack, install the files modrinth.index.json lists (skipping env.server=="unsupported", which are kept as client_only for export fidelity), extract overrides/ then server-overrides/ (server wins), install the pinned loader, and persist the normalized manifest. Quilt packs are rejected with a clear message. The shared mrpack routines live in mplib for the import provider to reuse. Co-Authored-By: Claude Opus 4.8 --- .../scripting/builtin/modrinth_modpacks.lua | 123 +++++++++++++ .../scripting/builtin_lib/modpack.lua | 91 +++++++++ .../builtin_shops/modrinth_modpacks.lua | 173 ++++++++++++++++++ .../internal/scripting/modpack_mrpack_test.go | 122 ++++++++++++ 4 files changed, 509 insertions(+) create mode 100644 engine/internal/scripting/builtin/modrinth_modpacks.lua create mode 100644 engine/internal/scripting/builtin_shops/modrinth_modpacks.lua create mode 100644 engine/internal/scripting/modpack_mrpack_test.go diff --git a/engine/internal/scripting/builtin/modrinth_modpacks.lua b/engine/internal/scripting/builtin/modrinth_modpacks.lua new file mode 100644 index 0000000..3026da7 --- /dev/null +++ b/engine/internal/scripting/builtin/modrinth_modpacks.lua @@ -0,0 +1,123 @@ +-- Modrinth modpack provider: installs a Modrinth modpack (.mrpack) as a +-- self-contained server. Hidden from the create-server UI — the +-- "modrinth_modpacks" shop drives it, passing an opaque "/" +-- as the version. install() downloads the .mrpack, installs its listed files and +-- the pinned loader (shared mplib helpers), and persists a normalized manifest so +-- export/update work without re-fetching. Modrinth read access is keyless. + +meta = { + id = "modrinth_modpacks", + name = "Modrinth", + website = "https://modrinth.com", + description = "Installs a Modrinth modpack as a self-contained server.", + version = "1.0.0", + author = "JustHostMC", + mod_layout = "mods", + hidden = true, + permissions = { + { kind = "network", reason = "Download the modpack, its files, and the loader installer." }, + { kind = "install", reason = "Resolve a JRE and run the Forge/NeoForge installer's --installServer." }, + { kind = "fs_server", reason = "Write the pack's mods/configs and detect the launch command." }, + }, +} + +local API = "https://api.modrinth.com/v2" + +function versions() + return {} +end + +local function parse_version(v) + local project, version_id = tostring(v):match("^([^/]+)/([^/]+)$") + if not project or not version_id then + error("invalid modpack version id: " .. tostring(v)) + end + return project, version_id +end + +-- primary_file picks the .mrpack artifact: the primary flag when set, else the +-- first file. +local function primary_file(v) + local f = v.files and v.files[1] + for _, cand in ipairs(v.files or {}) do + if cand.primary then f = cand break end + end + return f +end + +-- fetch_pack downloads the .mrpack for version_id to .jhmc/pack.mrpack. +local function fetch_pack(ctx, version_id) + local res = jhmc.http({ url = API .. "/version/" .. version_id }) + if res.status < 200 or res.status >= 300 then + error("modrinth: HTTP " .. res.status .. " resolving version " .. version_id) + end + local f = primary_file(jhmc.json_decode(res.body)) + if not f or not f.url or f.url == "" then + error("modrinth: version has no downloadable file") + end + ctx.step("install.progress.downloading_server", 0) + ctx.log(f.filename or "modpack.mrpack") + jhmc.fs.mkdir(".jhmc") + jhmc.download(f.url, { dest = ".jhmc/pack.mrpack", sha1 = f.hashes and f.hashes.sha1 }) +end + +function install(ctx) + local _, version_id = parse_version(ctx.version) + + ctx.step("install.progress.resolving_version", -1) + fetch_pack(ctx, version_id) + + local spec = mplib.install_mrpack(ctx, ".jhmc/pack.mrpack") + jhmc.fs.remove(".jhmc/pack.mrpack") + + ctx.step("install.progress.done", 1) + spec.pack_version = tostring(ctx.version) + return spec +end + +function update(ctx) + local project, version_id = parse_version(ctx.version) + local oproject = parse_version(ctx.old_version) + if project ~= oproject then + error("update must stay within the same modpack (" .. oproject .. " -> " .. project .. ")") + end + local old = mplib.read_manifest() + if not old then + error("cannot update: this server has no saved modpack manifest") + end + + ctx.step("install.progress.resolving_version", -1) + fetch_pack(ctx, version_id) + local m = mplib.mrpack_meta(".jhmc/pack.mrpack") + + mplib.diff_apply(ctx, mplib.server_index(old.files or {}), mplib.server_index(m.files), { + to_item = function(x) return mplib.to_download_item(x, nil) end, + }) + mplib.unzip_overrides(".jhmc/pack.mrpack") + + local java_major = jhmc.java_major_for(m.mc) + local args + if m.loader_name ~= old.loader or m.loader_version ~= old.loader_version or m.mc ~= old.mc_version then + args = mplib.install_loader(ctx, m.loader_name, m.loader_version, m.mc, java_major) + end + + mplib.write_manifest({ + format = 1, + name = m.name, + version_name = m.version, + mc_version = m.mc, + loader = m.loader_name, + loader_version = m.loader_version, + files = m.files, + }) + jhmc.fs.remove(".jhmc/pack.mrpack") + + ctx.step("install.progress.done", 1) + return { + java_major = java_major, + args = args, + mc_version = m.mc, + loader = m.loader_name, + pack_version = tostring(ctx.version), + } +end diff --git a/engine/internal/scripting/builtin_lib/modpack.lua b/engine/internal/scripting/builtin_lib/modpack.lua index a701f86..e8e35d5 100644 --- a/engine/internal/scripting/builtin_lib/modpack.lua +++ b/engine/internal/scripting/builtin_lib/modpack.lua @@ -426,4 +426,95 @@ local function install_cf_pack(ctx, zip_rel, key) end M.install_cf_pack = install_cf_pack +-- ── Modrinth pack (.mrpack: modrinth.index.json + overrides/) ─────────────── + +-- mrpack_meta reads a .mrpack's modrinth.index.json (the mrpack is a zip already +-- on disk) and returns { name, version, mc, loader_name, loader_version, files }. +-- Files unsupported on the server are kept but flagged client_only so export can +-- round-trip them; install/update skip them. Keyless — Modrinth files carry +-- direct download URLs. +local function mrpack_meta(zip_rel) + local idx = jhmc.json_decode(jhmc.zip_read(zip_rel, "modrinth.index.json")) + if type(idx) ~= "table" or type(idx.dependencies) ~= "table" then + error("not a Modrinth modpack: modrinth.index.json missing or malformed") + end + local deps = idx.dependencies + local mc = deps.minecraft + if not mc or mc == "" then + error("Modrinth pack has no Minecraft version") + end + local loader_name, loader_version + if deps["fabric-loader"] then + loader_name, loader_version = "fabric", deps["fabric-loader"] + elseif deps["neoforge"] then + loader_name, loader_version = "neoforge", deps["neoforge"] + elseif deps["forge"] then + loader_name, loader_version = "forge", deps["forge"] + elseif deps["quilt-loader"] then + error("Quilt modpacks are not supported yet") + else + error("Modrinth pack declares no supported mod loader") + end + + local files = {} + for _, f in ipairs(idx.files or {}) do + local server_env = (f.env and f.env.server) or "required" + local entry = { + dest = safe_path(f.path), + sha1 = f.hashes and f.hashes.sha1, + url = f.downloads and f.downloads[1], + } + if server_env == "unsupported" then + entry.client_only = true + end + files[#files + 1] = entry + end + + return { + name = idx.name or "", + version = idx.versionId or "", + mc = mc, + loader_name = loader_name, + loader_version = loader_version, + files = files, + } +end +M.mrpack_meta = mrpack_meta + +-- unzip_overrides splats a pack's overrides/ then server-overrides/ into the +-- server root; server-overrides win because they are extracted last. +local function unzip_overrides(zip_rel) + jhmc.unzip(zip_rel, ".", { prefix = "overrides/" }) + jhmc.unzip(zip_rel, ".", { prefix = "server-overrides/" }) +end +M.unzip_overrides = unzip_overrides + +-- install_mrpack installs a Modrinth pack whose .mrpack is already on disk at +-- zip_rel. The returned launch spec has no pack_version — the caller sets it. +local function install_mrpack(ctx, zip_rel) + local m = mrpack_meta(zip_rel) + download_files(ctx, m.files, nil) + unzip_overrides(zip_rel) + + local java_major = jhmc.java_major_for(m.mc) + local args = install_loader(ctx, m.loader_name, m.loader_version, m.mc, java_major) + + write_manifest({ + format = 1, + name = m.name, + version_name = m.version, + mc_version = m.mc, + loader = m.loader_name, + loader_version = m.loader_version, + files = m.files, + }) + return { + java_major = java_major, + args = args, + mc_version = m.mc, + loader = m.loader_name, + } +end +M.install_mrpack = install_mrpack + mplib = M diff --git a/engine/internal/scripting/builtin_shops/modrinth_modpacks.lua b/engine/internal/scripting/builtin_shops/modrinth_modpacks.lua new file mode 100644 index 0000000..5f790ca --- /dev/null +++ b/engine/internal/scripting/builtin_shops/modrinth_modpacks.lua @@ -0,0 +1,173 @@ +-- Modrinth modpack shop. Talks to the same keyless Modrinth v2 API as the mod +-- shop, filtered to project_type:modpack, and browses packs to install as new +-- servers. A modpack is not a single installable file — it installs as a whole +-- server through the "modrinth_modpacks" provider, so resolve_file is an error. + +meta = { + id = "modrinth_modpacks", + name = "Modrinth", + website = "https://modrinth.com", + description = "Browse and install Modrinth modpacks as new servers.", + version = "1.0.0", + author = "JustHostMC", + kinds = { "modpack" }, + needs_key = false, + permissions = { + { kind = "network", reason = "Query the Modrinth API to browse and install modpacks" }, + }, +} + +local API = "https://api.modrinth.com/v2" + +local TTL_BROWSE = 120 +local TTL_DETAIL = 600 + +local function urlencode(s) + return (string.gsub(s, "[^%w%-%._~]", function(c) + return string.format("%%%02X", string.byte(c)) + end)) +end + +local function get(url, ttl) + local res = jhmc.http_cache({ url = url, ttl = ttl }) + if res.status == 404 or res.status == 410 then + error("modrinth: not found: " .. url) + end + if res.status < 200 or res.status >= 300 then + error("modrinth: HTTP " .. res.status .. " for " .. url) + end + return jhmc.json_decode(res.body) +end + +-- facets filters to modpacks; packs pin their own MC version and loader, so only +-- project_type and any chosen categories are applied. +local function facets(ctx) + local groups = { '["project_type:modpack"]' } + if #(ctx.categories or {}) > 0 then + local parts = {} + for _, category in ipairs(ctx.categories) do + parts[#parts + 1] = '"categories:' .. category .. '"' + end + groups[#groups + 1] = "[" .. table.concat(parts, ",") .. "]" + end + return "[" .. table.concat(groups, ",") .. "]" +end + +local function project_card(hit) + return { + project_id = hit.project_id or hit.id, + slug = hit.slug, + title = hit.title, + summary = hit.description, + icon_url = hit.icon_url, + author = hit.author, + downloads = hit.downloads, + follows = hit.follows or hit.followers, + categories = hit.display_categories or hit.categories, + project_type = "modpack", + } +end + +local function run_search(ctx, index, offset, limit) + local url = API .. "/search?query=" .. urlencode(ctx.query or "") + .. "&facets=" .. urlencode(facets(ctx)) + .. "&index=" .. index + .. "&offset=" .. offset + .. "&limit=" .. limit + local body = get(url, TTL_BROWSE) + local projects = {} + for _, hit in ipairs(body.hits or {}) do + projects[#projects + 1] = project_card(hit) + end + return projects, body.total_hits or 0 +end + +function home(ctx) + local popular = run_search(ctx, "downloads", 0, 12) + local recommended = run_search(ctx, "follows", 0, 12) + local updated = run_search(ctx, "updated", 0, 12) + return { sections = { + { title_key = "shop.home.popular", projects = popular }, + { title_key = "shop.home.recommended", projects = recommended }, + { title_key = "shop.home.updated", projects = updated }, + } } +end + +local SORTS = { relevance = "relevance", downloads = "downloads", follows = "follows", + newest = "newest", updated = "updated" } + +function search(ctx) + local projects, total = run_search(ctx, SORTS[ctx.sort] or "relevance", + ctx.offset or 0, ctx.limit or 20) + return { projects = projects, total = total } +end + +function detail(ctx) + local p = get(API .. "/project/" .. urlencode(ctx.project_id), TTL_DETAIL) + local gallery = {} + for _, g in ipairs(p.gallery or {}) do + gallery[#gallery + 1] = { url = g.url, title = g.title, description = g.description } + end + return { + project = { + project_id = p.id, + slug = p.slug, + title = p.title, + summary = p.description, + icon_url = p.icon_url, + downloads = p.downloads, + follows = p.followers, + categories = p.categories, + project_type = "modpack", + }, + body = p.body, + body_format = "markdown", + gallery = gallery, + links = { + website = "https://modrinth.com/modpack/" .. (p.slug or p.id), + source = p.source_url, + issues = p.issues_url, + wiki = p.wiki_url, + discord = p.discord_url, + }, + game_versions = p.game_versions, + loaders = p.loaders, + license = p.license and p.license.id or "", + updated = p.updated, + } +end + +local function primary_file(v) + local f = v.files and v.files[1] + for _, cand in ipairs(v.files or {}) do + if cand.primary then f = cand break end + end + return f +end + +function versions(ctx) + -- All pack versions, newest-first; a modpack version is not loader-filtered. + local list = get(API .. "/project/" .. urlencode(ctx.project_id) .. "/version", TTL_BROWSE) + local out = {} + for _, v in ipairs(list) do + local f = primary_file(v) + out[#out + 1] = { + id = v.id, + name = v.name, + version_number = v.version_number, + channel = v.version_type, + game_versions = v.game_versions, + date = v.date_published, + downloads = v.downloads, + filename = f and f.filename or "", + size = f and f.size or 0, + } + end + return { versions = out } +end + +function resolve_file(ctx) + -- Required by the shop contract but never reached by the modpack flow: a pack + -- installs as a whole server by the "modrinth_modpacks" provider. + error("not distributable: Modrinth modpacks install as a new server") +end diff --git a/engine/internal/scripting/modpack_mrpack_test.go b/engine/internal/scripting/modpack_mrpack_test.go new file mode 100644 index 0000000..9e6e5ff --- /dev/null +++ b/engine/internal/scripting/modpack_mrpack_test.go @@ -0,0 +1,122 @@ +package scripting + +import ( + "net/http" + "path/filepath" + "testing" + + mcmanagerv1 "github.com/000hen/justhostmc/engine/gen/mcmanager/v1" +) + +func TestModrinthModpacksProviderHidden(t *testing.T) { + e := builtinProvider(t, "modrinth_modpacks") + if !e.Meta.Hidden { + t.Error("modrinth_modpacks provider should be hidden") + } + if e.Meta.ModLayout != "mods" { + t.Errorf("mod_layout = %q, want mods", e.Meta.ModLayout) + } +} + +func TestModrinthModpacksShopIsModpackKind(t *testing.T) { + ss := newBuiltinShops(t, http.NotFoundHandler()) + sh, ok := ss.Get("modrinth_modpacks") + if !ok { + t.Fatal("modrinth_modpacks shop not registered") + } + if sh.Meta().NeedsKey { + t.Error("modrinth_modpacks shop is keyless") + } + var isModpack bool + for _, k := range sh.Meta().Kinds { + if k == "modpack" { + isModpack = true + } + } + if !isModpack { + t.Errorf("kinds = %v, want to contain modpack", sh.Meta().Kinds) + } +} + +// TestMrpackMeta covers parsing modrinth.index.json: loader detection, direct +// download URLs, and env.server=="unsupported" files flagged client_only rather +// than installed. +func TestMrpackMeta(t *testing.T) { + dir := t.TempDir() + index := `{ + "formatVersion": 1, "game": "minecraft", "versionId": "1.2.0", "name": "MR Pack", + "files": [ + { "path": "mods/sodium.jar", "hashes": {"sha1":"s1","sha512":"x"}, + "env": {"client":"required","server":"required"}, "downloads": ["http://cdn/sodium.jar"], "fileSize": 100 }, + { "path": "mods/iris.jar", "hashes": {"sha1":"s2"}, + "env": {"client":"required","server":"unsupported"}, "downloads": ["http://cdn/iris.jar"], "fileSize": 50 } + ], + "dependencies": { "minecraft": "1.20.1", "fabric-loader": "0.15.7" } + }` + writeTestJar(t, filepath.Join(dir, ".jhmc", "pack.mrpack"), + map[string]string{"modrinth.index.json": index}) + + inv := fsInv(dir) + src := ` +function check() + local m = mplib.mrpack_meta(".jhmc/pack.mrpack") + assert(m.mc == "1.20.1", "mc " .. tostring(m.mc)) + assert(m.loader_name == "fabric" and m.loader_version == "0.15.7", "loader") + assert(m.name == "MR Pack" and m.version == "1.2.0", "name/version") + assert(#m.files == 2, "files " .. #m.files) + local by = {} + for _, f in ipairs(m.files) do by[f.dest] = f end + local s = by["mods/sodium.jar"] + assert(s and s.url == "http://cdn/sodium.jar" and s.sha1 == "s1", "sodium") + assert(not s.client_only, "sodium is a server file") + local i = by["mods/iris.jar"] + assert(i and i.client_only, "iris unsupported-on-server must be client_only") +end` + if err := runInv(t, inv, src); err != nil { + t.Fatalf("script failed: %v", err) + } +} + +func TestMrpackQuiltUnsupported(t *testing.T) { + dir := t.TempDir() + index := `{ + "formatVersion": 1, "name": "Q", "versionId": "1", + "files": [], "dependencies": { "minecraft": "1.20.1", "quilt-loader": "0.20.0" } + }` + writeTestJar(t, filepath.Join(dir, ".jhmc", "pack.mrpack"), + map[string]string{"modrinth.index.json": index}) + inv := fsInv(dir) + err := runInv(t, inv, `function check() mplib.mrpack_meta(".jhmc/pack.mrpack") end`) + if err == nil { + t.Fatal("expected Quilt to be rejected") + } +} + +// fsInvNet is fsInv plus the network grant, for pack routines that download. +func fsInvNet(dir string) *invocation { + inv := fsInv(dir) + inv.granted[mcmanagerv1.PermissionKind_PERMISSION_NETWORK] = true + return inv +} + +// TestMrpackServerOverridesWin verifies overrides/ then server-overrides/ are +// both extracted, with server-overrides winning on conflict. +func TestMrpackServerOverridesWin(t *testing.T) { + dir := t.TempDir() + writeTestJar(t, filepath.Join(dir, ".jhmc", "pack.mrpack"), map[string]string{ + "modrinth.index.json": `{"dependencies":{"minecraft":"1.20.1","fabric-loader":"0.15.7"}}`, + "overrides/config/a.toml": "client-value", + "server-overrides/config/a.toml": "server-value", + "overrides/config/only-client.txt": "c", + }) + inv := fsInvNet(dir) + src := ` +function check() + mplib.unzip_overrides(".jhmc/pack.mrpack") + assert(jhmc.fs.read("config/a.toml") == "server-value", "server-overrides must win") + assert(jhmc.fs.exists("config/only-client.txt"), "plain override extracted") +end` + if err := runInv(t, inv, src); err != nil { + t.Fatalf("script failed: %v", err) + } +} From 061532f0e0ca0bf403c337e4e938ab8f10e04a57 Mon Sep 17 00:00:00 2001 From: 000hen Date: Mon, 13 Jul 2026 18:15:38 +0800 Subject: [PATCH 20/30] feat(engine): ImportModpack RPC + local import provider MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the ImportModpack handler: it validates the picked file, stages it into the new server dir at .jhmc/import.zip, and drives a hidden "import" provider that detects the format (CurseForge manifest.json or Modrinth modrinth.index.json) and reuses the shared mplib install routines. The create flow is factored into runCreate with a staging hook shared by Create and ImportModpack. Imported servers persist the normalized manifest (Export works) but have no update() (Update stays Unimplemented — a local file has no upstream to diff). Co-Authored-By: Claude Opus 4.8 --- engine/internal/grpc/serverservice.go | 123 +++++++++++++++--- .../grpc/serverservice_importmodpack_test.go | 117 +++++++++++++++++ engine/internal/scripting/builtin/import.lua | 68 ++++++++++ .../scripting/builtin_lib/modpack.lua | 2 + .../internal/scripting/modpack_import_test.go | 75 +++++++++++ 5 files changed, 370 insertions(+), 15 deletions(-) create mode 100644 engine/internal/grpc/serverservice_importmodpack_test.go create mode 100644 engine/internal/scripting/builtin/import.lua create mode 100644 engine/internal/scripting/modpack_import_test.go diff --git a/engine/internal/grpc/serverservice.go b/engine/internal/grpc/serverservice.go index 8f1d77c..33995ff 100644 --- a/engine/internal/grpc/serverservice.go +++ b/engine/internal/grpc/serverservice.go @@ -3,6 +3,7 @@ package grpcsvc import ( "context" "fmt" + "io" "net/http" "os" "path/filepath" @@ -167,15 +168,41 @@ func (s *ServerService) Instance(id string) (isolation.Instance, bool) { return inst, ok } -// Create provisions a new server, streaming install progress, and registers it -// ready to start. +// Create provisions a new server from a provider version, streaming install +// progress, and registers it ready to start. func (s *ServerService) Create(req *mcmanagerv1.CreateServerRequest, stream grpc.ServerStreamingServer[mcmanagerv1.InstallProgress]) error { + return s.runCreate(stream, createParams{ + name: req.Name, + providerID: req.ProviderId, + version: req.McVersion, + memoryMB: int(req.MemoryMb), + port: int(req.Port), + customJavaArgs: req.CustomJavaArgs, + }) +} + +// createParams carries the inputs shared by Create and ImportModpack. +type createParams struct { + name string + providerID string + version string // opaque version handed to the provider + memoryMB int + port int + customJavaArgs string + stage func(dir string) error // optional: runs after dir creation, before install +} + +// runCreate is the shared provisioning flow behind Create and ImportModpack: it +// makes the server dir (optionally staging input into it first), runs the +// provider install, ensures the JRE, writes server files, and persists the +// record. Any failure wipes the new server dir. +func (s *ServerService) runCreate(stream grpc.ServerStreamingServer[mcmanagerv1.InstallProgress], p createParams) error { ctx := stream.Context() - entry, ok := s.cfg.Providers.Get(req.ProviderId) + entry, ok := s.cfg.Providers.Get(p.providerID) if !ok { return errorStatus(codes.Unimplemented, mcmanagerv1.ErrorCode_ERROR_CODE_UNSPECIFIED, - fmt.Sprintf("provider %q not installed", req.ProviderId), nil) + fmt.Sprintf("provider %q not installed", p.providerID), nil) } id := genID() @@ -188,11 +215,11 @@ func (s *ServerService) Create(req *mcmanagerv1.CreateServerRequest, stream grpc _ = os.RemoveAll(dir) } - port := resolvePort(int(req.Port)) + port := resolvePort(p.port) rec := &store.Server{ - ID: id, Name: req.Name, ProviderID: req.ProviderId, ModLayout: entry.Meta.ModLayout, - McVersion: req.McVersion, - MemoryMB: int(req.MemoryMb), Port: port, Status: mcmanagerv1.ServerStatus_INSTALLING, + ID: id, Name: p.name, ProviderID: p.providerID, ModLayout: entry.Meta.ModLayout, + McVersion: p.version, + MemoryMB: p.memoryMB, Port: port, Status: mcmanagerv1.ServerStatus_INSTALLING, SortOrder: s.nextSortOrder(), } _ = s.cfg.Store.Put(rec) @@ -220,12 +247,21 @@ func (s *ServerService) Create(req *mcmanagerv1.CreateServerRequest, stream grpc il := s.openInstallLog(id) defer il.Close() base := newProgressSender(stream) - send := func(p provider.Progress) { - il.record(p) - base(p) + send := func(pr provider.Progress) { + il.record(pr) + base(pr) } - spec, err := entry.Provider.Install(installCtx, dir, req.McVersion, send) + if p.stage != nil { + if err := p.stage(dir); err != nil { + send(provider.Progress{LogLine: "[error] " + err.Error()}) + il.recordLine("[error] stage: " + err.Error()) + cleanup() + return status.Errorf(codes.Internal, "stage modpack: %v", err) + } + } + + spec, err := entry.Provider.Install(installCtx, dir, p.version, send) if err != nil { // Surface the error in the live log so the user can see what went wrong. send(provider.Progress{LogLine: "[error] " + err.Error()}) @@ -233,10 +269,10 @@ func (s *ServerService) Create(req *mcmanagerv1.CreateServerRequest, stream grpc cleanup() return mapInstallError(err) } - // A modpack provider takes an opaque "packId/versionId" as req.McVersion and + // A modpack provider takes an opaque "packId/versionId" as the version and // resolves the concrete Minecraft version in spec.McVersion; prefer it for // Java selection and the stored record. - resolvedVersion := req.McVersion + resolvedVersion := p.version if spec.McVersion != "" { resolvedVersion = spec.McVersion } @@ -259,7 +295,7 @@ func (s *ServerService) Create(req *mcmanagerv1.CreateServerRequest, stream grpc rec.JavaMajor = spec.JavaMajor rec.LaunchArgs = spec.Args - rec.CustomJavaArgs = req.CustomJavaArgs + rec.CustomJavaArgs = p.customJavaArgs if spec.McVersion != "" { rec.McVersion = spec.McVersion } @@ -489,6 +525,63 @@ func (s *ServerService) ExportModpack(req *mcmanagerv1.ExportModpackRequest, str return nil } +// ImportModpack provisions a new server from a local modpack file (a CurseForge +// client pack zip or a Modrinth .mrpack), streaming install progress like +// Create. The file is staged into the new server dir and installed by the +// "import" provider, which detects the format. Imported servers can be exported +// but not updated (a local file has no upstream to diff against). +func (s *ServerService) ImportModpack(req *mcmanagerv1.ImportModpackRequest, stream grpc.ServerStreamingServer[mcmanagerv1.InstallProgress]) error { + name := strings.TrimSpace(req.Name) + if name == "" { + return status.Error(codes.InvalidArgument, "server name is required") + } + src := req.SrcPath + if !filepath.IsAbs(src) { + return status.Error(codes.InvalidArgument, "src_path must be an absolute path") + } + if ext := strings.ToLower(filepath.Ext(src)); ext != ".zip" && ext != ".mrpack" { + return errorStatus(codes.InvalidArgument, mcmanagerv1.ErrorCode_ERROR_CODE_UNSPECIFIED, + "modpack file must be a .zip (CurseForge) or .mrpack (Modrinth)", nil) + } + if info, err := os.Stat(src); err != nil || info.IsDir() { + return status.Error(codes.InvalidArgument, "modpack file not found") + } + + return s.runCreate(stream, createParams{ + name: name, + providerID: "import", + version: "import/local", + memoryMB: int(req.MemoryMb), + port: int(req.Port), + customJavaArgs: req.CustomJavaArgs, + stage: func(dir string) error { return stageModpackFile(src, dir) }, + }) +} + +// stageModpackFile copies the user's modpack file into the new server dir at +// .jhmc/import.zip, where the import provider (sandboxed to the server dir) picks +// it up. Streamed rather than read whole — packs can be hundreds of MB. +func stageModpackFile(src, dir string) error { + dst := filepath.Join(dir, ".jhmc", "import.zip") + if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { + return err + } + in, err := os.Open(src) + if err != nil { + return err + } + defer in.Close() + out, err := os.Create(dst) + if err != nil { + return err + } + if _, err := io.Copy(out, in); err != nil { + out.Close() + return err + } + return out.Close() +} + // Start launches a previously created server. func (s *ServerService) Start(ctx context.Context, req *mcmanagerv1.ServerId) (*mcmanagerv1.Empty, error) { rec, ok := s.cfg.Store.Get(req.Id) diff --git a/engine/internal/grpc/serverservice_importmodpack_test.go b/engine/internal/grpc/serverservice_importmodpack_test.go new file mode 100644 index 0000000..87d4912 --- /dev/null +++ b/engine/internal/grpc/serverservice_importmodpack_test.go @@ -0,0 +1,117 @@ +package grpcsvc + +import ( + "context" + "os" + "path/filepath" + "testing" + + mcmanagerv1 "github.com/000hen/justhostmc/engine/gen/mcmanager/v1" + "github.com/000hen/justhostmc/engine/internal/appdata" + "github.com/000hen/justhostmc/engine/internal/jre" + "github.com/000hen/justhostmc/engine/internal/provider" + "github.com/000hen/justhostmc/engine/internal/store" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +// importStubProvider records the dir/version it was installed with and whether +// the modpack file was staged into the server dir before install ran. +type importStubProvider struct { + spec provider.LaunchSpec + stagedSeen bool + gotVersion string +} + +func (p *importStubProvider) Versions(context.Context) ([]string, error) { return nil, nil } +func (p *importStubProvider) Install(_ context.Context, dir, version string, _ func(provider.Progress)) (provider.LaunchSpec, error) { + p.gotVersion = version + if _, err := os.Stat(filepath.Join(dir, ".jhmc", "import.zip")); err == nil { + p.stagedSeen = true + } + return p.spec, nil +} + +func importTestService(t *testing.T, prov provider.Provider) (*ServerService, *store.Memory) { + t.Helper() + paths := appdata.New(t.TempDir()) + cacheJava(t, paths.JRECache(), 21) + st := store.NewMemory() + svc := NewServerService(ServerServiceConfig{ + Store: st, + Providers: testRegistry("import", "mods", prov), + Paths: paths, + JRE: jre.NewManager(paths.JRECache()), + }) + return svc, st +} + +func writeTempFile(t *testing.T, name, content string) string { + t.Helper() + p := filepath.Join(t.TempDir(), name) + if err := os.WriteFile(p, []byte(content), 0o644); err != nil { + t.Fatal(err) + } + return p +} + +func TestImportModpackStagesAndCreates(t *testing.T) { + prov := &importStubProvider{spec: provider.LaunchSpec{ + JavaMajor: 21, McVersion: "1.20.1", Loader: "forge", + Args: []string{"@run"}, PackVersion: "import/1.0", + }} + svc, st := importTestService(t, prov) + src := writeTempFile(t, "pack.zip", "PK fake zip") + + if err := svc.ImportModpack(&mcmanagerv1.ImportModpackRequest{ + Name: "My Import", SrcPath: src, MemoryMb: 4096, + }, &fakeInstallStream{}); err != nil { + t.Fatalf("ImportModpack: %v", err) + } + if !prov.stagedSeen { + t.Error("staged .jhmc/import.zip missing when the provider ran") + } + if prov.gotVersion != "import/local" { + t.Errorf("provider version = %q, want import/local", prov.gotVersion) + } + servers := st.List() + if len(servers) != 1 { + t.Fatalf("servers = %d, want 1", len(servers)) + } + rec := servers[0] + if rec.Name != "My Import" || rec.ProviderID != "import" || rec.ProviderVersion != "import/1.0" { + t.Errorf("record = %+v", rec) + } + if rec.Status != mcmanagerv1.ServerStatus_STOPPED { + t.Errorf("status = %v, want STOPPED", rec.Status) + } + if rec.McVersion != "1.20.1" || rec.Loader != "forge" { + t.Errorf("record mc/loader = %q/%q", rec.McVersion, rec.Loader) + } +} + +func TestImportModpackValidation(t *testing.T) { + svc, st := importTestService(t, &importStubProvider{}) + good := writeTempFile(t, "ok.zip", "z") + + cases := []struct { + name string + req *mcmanagerv1.ImportModpackRequest + }{ + {"empty name", &mcmanagerv1.ImportModpackRequest{Name: " ", SrcPath: good}}, + {"relative path", &mcmanagerv1.ImportModpackRequest{Name: "n", SrcPath: "pack.zip"}}, + {"bad extension", &mcmanagerv1.ImportModpackRequest{Name: "n", SrcPath: writeTempFile(t, "pack.rar", "z")}}, + {"missing file", &mcmanagerv1.ImportModpackRequest{Name: "n", SrcPath: filepath.Join(t.TempDir(), "nope.zip")}}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + err := svc.ImportModpack(c.req, &fakeInstallStream{}) + if status.Code(err) != codes.InvalidArgument { + t.Fatalf("code = %v, want InvalidArgument (err=%v)", status.Code(err), err) + } + }) + } + if n := len(st.List()); n != 0 { + t.Errorf("no server should be created on validation failure, got %d", n) + } +} diff --git a/engine/internal/scripting/builtin/import.lua b/engine/internal/scripting/builtin/import.lua new file mode 100644 index 0000000..3aad430 --- /dev/null +++ b/engine/internal/scripting/builtin/import.lua @@ -0,0 +1,68 @@ +-- Local modpack import provider: installs a server from a modpack file the user +-- picked on their machine (a CurseForge client pack zip or a Modrinth .mrpack). +-- Hidden from the create-server UI — the ImportModpack RPC stages the file into +-- the new server dir at .jhmc/import.zip and drives this provider. It detects the +-- format and reuses the shared mplib install routines, so an imported server +-- carries the same normalized manifest as a shop-installed one (Export works). +-- There is no update() — a local file has no upstream version to diff against. + +meta = { + id = "import", + name = "Imported Modpack", + website = "", + description = "Installs a server from a local CurseForge or Modrinth modpack file.", + version = "1.0.0", + author = "JustHostMC", + mod_layout = "mods", + hidden = true, + permissions = { + { kind = "network", reason = "Download the pack's mods and the loader installer." }, + { kind = "install", reason = "Resolve a JRE and run the Forge/NeoForge installer's --installServer." }, + { kind = "fs_server", reason = "Unpack the modpack and detect the launch command." }, + }, + config = { + { key = "curseforge_api_key", type = "secret", name = "CurseForge API key", + description = "Required only when importing a CurseForge pack with CurseForge-hosted mods", required = false }, + }, +} + +local STAGED = ".jhmc/import.zip" + +function versions() + return {} +end + +local function key(ctx) + local cfg = ctx.config or {} + return cfg.curseforge_api_key or "" +end + +function install(ctx) + local entries = jhmc.zip_entries(STAGED) + local has = {} + for _, e in ipairs(entries) do has[e] = true end + + ctx.step("install.progress.resolving_version", -1) + local spec + if has["manifest.json"] then + local k = key(ctx) + if k == "" then + error("this CurseForge modpack needs a CurseForge API key to resolve its mods (set it in the provider settings)") + end + spec = mplib.install_cf_pack(ctx, STAGED, k) + elseif has["modrinth.index.json"] then + spec = mplib.install_mrpack(ctx, STAGED) + else + error("not a recognized modpack file: expected a CurseForge manifest.json or a Modrinth modrinth.index.json") + end + + jhmc.fs.remove(STAGED) + + ctx.step("install.progress.done", 1) + local version = (spec.version_name and spec.version_name ~= "") and spec.version_name or "local" + spec.version_name = nil + -- A non-empty pack_version enables the Export action; the "import" provider id + -- is how the app keeps the Update action hidden (no upstream to diff against). + spec.pack_version = "import/" .. version + return spec +end diff --git a/engine/internal/scripting/builtin_lib/modpack.lua b/engine/internal/scripting/builtin_lib/modpack.lua index e8e35d5..4e26e29 100644 --- a/engine/internal/scripting/builtin_lib/modpack.lua +++ b/engine/internal/scripting/builtin_lib/modpack.lua @@ -422,6 +422,7 @@ local function install_cf_pack(ctx, zip_rel, key) args = args, mc_version = m.mc, loader = m.loader_name, + version_name = m.version, -- used by the import provider to label the pack } end M.install_cf_pack = install_cf_pack @@ -513,6 +514,7 @@ local function install_mrpack(ctx, zip_rel) args = args, mc_version = m.mc, loader = m.loader_name, + version_name = m.version, -- used by the import provider to label the pack } end M.install_mrpack = install_mrpack diff --git a/engine/internal/scripting/modpack_import_test.go b/engine/internal/scripting/modpack_import_test.go new file mode 100644 index 0000000..c63168b --- /dev/null +++ b/engine/internal/scripting/modpack_import_test.go @@ -0,0 +1,75 @@ +package scripting + +import ( + "context" + "errors" + "path/filepath" + "strings" + "testing" + + mcmanagerv1 "github.com/000hen/justhostmc/engine/gen/mcmanager/v1" + "github.com/000hen/justhostmc/engine/internal/provider" +) + +func TestImportProviderHidden(t *testing.T) { + e := builtinProvider(t, "import") + if !e.Meta.Hidden { + t.Error("import provider should be hidden") + } + if e.Meta.ModLayout != "mods" { + t.Errorf("mod_layout = %q, want mods", e.Meta.ModLayout) + } + var hasKey bool + for _, c := range e.Meta.Config { + if c.Key == "curseforge_api_key" && c.Type == mcmanagerv1.ConfigOptionType_CONFIG_OPTION_SECRET { + hasKey = true + } + } + if !hasKey { + t.Errorf("import should declare a curseforge_api_key secret, config=%+v", e.Meta.Config) + } +} + +// stageImportZip writes the staged import file the "import" provider reads. +func stageImportZip(t *testing.T, dir string, entries map[string]string) { + t.Helper() + writeTestJar(t, filepath.Join(dir, ".jhmc", "import.zip"), entries) +} + +func TestImportProviderRejectsUnknownFormat(t *testing.T) { + dir := t.TempDir() + stageImportZip(t, dir, map[string]string{"random.txt": "x"}) + e := builtinProvider(t, "import") + _, err := e.Provider.Install(context.Background(), dir, "import/local", nil) + if err == nil || !strings.Contains(err.Error(), "not a recognized modpack") { + t.Fatalf("want unrecognized-format error, got %v", err) + } +} + +func TestImportProviderCurseForgeNeedsKey(t *testing.T) { + dir := t.TempDir() + stageImportZip(t, dir, map[string]string{ + "manifest.json": `{"minecraft":{"version":"1.20.1","modLoaders":[{"id":"forge-47.2.0","primary":true}]},"name":"P","version":"1","files":[]}`, + }) + e := builtinProvider(t, "import") + _, err := e.Provider.Install(context.Background(), dir, "import/local", nil) + if err == nil || !strings.Contains(err.Error(), "CurseForge API key") { + t.Fatalf("want CurseForge-key error, got %v", err) + } +} + +// TestImportProviderUpdateUnsupported confirms the import provider yields +// ErrUpdateUnsupported — imported packs can be exported but not updated (a local +// file has no upstream to diff against), which is how the RPC answers +// Unimplemented for them. +func TestImportProviderUpdateUnsupported(t *testing.T) { + e := builtinProvider(t, "import") + up, ok := e.Provider.(provider.Updater) + if !ok { + t.Fatal("Lua providers expose Update; import should report it unsupported at runtime") + } + _, err := up.Update(context.Background(), t.TempDir(), "import/local", "import/local", nil) + if !errors.Is(err, provider.ErrUpdateUnsupported) { + t.Fatalf("import Update err = %v, want ErrUpdateUnsupported", err) + } +} From 9292be72f1a9cb4681b0252adc88875805448cfe Mon Sep 17 00:00:00 2001 From: 000hen Date: Mon, 13 Jul 2026 23:09:37 +0800 Subject: [PATCH 21/30] feat(app): import a local modpack file as a new server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add an Import button to the home header: it picks a .zip (CurseForge) or .mrpack (Modrinth) file, prompts for a server name (defaulted from the file name) and memory, and streams install progress through the shared new-server flow (InstallServerAsync and the new ImportModpackAsync now both route through RunNewServerInstallAsync). Imported servers show Export but not Update — the Update menu item hides when the provider id is "import". Co-Authored-By: Claude Opus 4.8 --- .../Controls/Server/ServerHeaderPanel.xaml | 2 +- .../Controls/Server/ServerHeaderPanel.xaml.cs | 9 +++ .../Strings/en-US/Resources.resw | 5 ++ .../Strings/zh-TW/Resources.resw | 5 ++ .../ViewModels/MainViewModel.cs | 26 ++++++++- app/JustHostMC.App/Views/HomePage.xaml | 10 ++++ app/JustHostMC.App/Views/HomePage.xaml.cs | 57 +++++++++++++++++++ app/JustHostMC.App/Views/ServerPage.xaml.cs | 4 ++ 8 files changed, 114 insertions(+), 4 deletions(-) diff --git a/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml b/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml index d815ff6..7793cff 100644 --- a/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml +++ b/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml @@ -152,7 +152,7 @@ x:Uid="ServerUpdateModpackMenuItem" Click="OnUpdateModpackClick" IsEnabled="{x:Bind UpdateModpackEnabled(Server.Status), Mode=OneWay}" - Visibility="{x:Bind ModpackItemVisibility(Server.ProviderVersion), Mode=OneWay}"> + Visibility="{x:Bind UpdateModpackVisibility(Server.ProviderVersion, Server.ProviderId), Mode=OneWay}"> diff --git a/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml.cs b/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml.cs index 46b3b3f..3515154 100644 --- a/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml.cs +++ b/app/JustHostMC.App/Controls/Server/ServerHeaderPanel.xaml.cs @@ -65,6 +65,15 @@ public Visibility ModpackItemVisibility(string providerVersion) => string.IsNullOrEmpty(providerVersion) ? Visibility.Collapsed : Visibility.Visible; + /// The Update action additionally hides for imported servers: a + /// local modpack file has no upstream version to update to (Export still + /// works). + public Visibility UpdateModpackVisibility(string providerVersion, + string providerId) => + string.IsNullOrEmpty(providerVersion) || providerId == "import" + ? Visibility.Collapsed + : Visibility.Visible; + /// Updating replaces pack files, so the server must not be /// running. public bool UpdateModpackEnabled(ServerStatus s) => diff --git a/app/JustHostMC.App/Strings/en-US/Resources.resw b/app/JustHostMC.App/Strings/en-US/Resources.resw index 3dc4227..549973f 100644 --- a/app/JustHostMC.App/Strings/en-US/Resources.resw +++ b/app/JustHostMC.App/Strings/en-US/Resources.resw @@ -335,6 +335,10 @@ Update No other versions available. CurseForge modpack + Import modpack + Server name + Memory + Import Preparing modpack export… Packaging modpack… Modpack exported. @@ -515,6 +519,7 @@ Mod ShopCanonical Mod Shop window title; copied to Window.Title after XAML initialization. Modpack Shop Modpack shop + Import modpack Start a new server from a modpack Browse community modpacks. Installing one creates a brand-new server, fully set up. Find something brilliant for your server diff --git a/app/JustHostMC.App/Strings/zh-TW/Resources.resw b/app/JustHostMC.App/Strings/zh-TW/Resources.resw index 236cc6d..17ed228 100644 --- a/app/JustHostMC.App/Strings/zh-TW/Resources.resw +++ b/app/JustHostMC.App/Strings/zh-TW/Resources.resw @@ -333,6 +333,10 @@ 更新 沒有其他可用版本。 CurseForge 模組包 + 匯入模組包 + 伺服器名稱 + 記憶體 + 匯入 正在準備匯出模組包… 正在打包模組包… 模組包已匯出。 @@ -504,6 +508,7 @@ 模組商店模組商店視窗的標準標題;XAML 初始化後複製到 Window.Title。 模組包商店 模組包商店 + 匯入模組包 用模組包建立新的伺服器 瀏覽社群製作的模組包,安裝後會自動建立並設定好一台全新伺服器。 為你的伺服器找到精彩內容 diff --git a/app/JustHostMC.App/ViewModels/MainViewModel.cs b/app/JustHostMC.App/ViewModels/MainViewModel.cs index 9792ecf..980bc24 100644 --- a/app/JustHostMC.App/ViewModels/MainViewModel.cs +++ b/app/JustHostMC.App/ViewModels/MainViewModel.cs @@ -185,9 +185,29 @@ private async Task Refresh() { /// Creates a server, streaming localized install progress + raw /// log. - public async Task InstallServerAsync(CreateServerRequest request) { + public Task InstallServerAsync(CreateServerRequest request) => + RunNewServerInstallAsync(request.Name, + daemon => daemon.Servers.Create(request)); + + /// Imports a local modpack file (CurseForge zip or Modrinth + /// .mrpack) as a brand-new server, streaming the same install progress as a + /// shop install. + public Task ImportModpackAsync(string name, string srcPath, int memoryMb) => + RunNewServerInstallAsync( + name, + daemon => daemon.Servers.ImportModpack(new ImportModpackRequest { + Name = name, SrcPath = srcPath, MemoryMb = memoryMb, + })); + + /// Shared flow behind creating and importing a brand-new server: + /// drives both the global install UI and the server's progress tracker off + /// one InstallProgress stream, so the operation survives window/page + /// changes. + private async Task RunNewServerInstallAsync( + string name, + Func> startCall) { using var backgroundTask = _backgroundTasks.Begin("server-install"); - var tracker = ProgressService.GetOrCreateTracker(null, request.Name); + var tracker = ProgressService.GetOrCreateTracker(null, name); RunOnUI(() => { tracker.InstallLog.Clear(); tracker.HasFailed = false; @@ -212,7 +232,7 @@ public async Task InstallServerAsync(CreateServerRequest request) { try { var daemon = await App.Current.DaemonReady; - using var call = daemon.Servers.Create(request); + using var call = startCall(daemon); await foreach (var progress in call.ResponseStream.ReadAllAsync() .ConfigureAwait(false)) { progressBuffer.Post(progress); diff --git a/app/JustHostMC.App/Views/HomePage.xaml b/app/JustHostMC.App/Views/HomePage.xaml index f4a61f9..2ee6753 100644 --- a/app/JustHostMC.App/Views/HomePage.xaml +++ b/app/JustHostMC.App/Views/HomePage.xaml @@ -396,6 +396,7 @@ + @@ -437,6 +438,15 @@ + diff --git a/app/JustHostMC.App/Views/ShopDetailPage.xaml.cs b/app/JustHostMC.App/Views/ShopDetailPage.xaml.cs index 015c31c..2ed664c 100644 --- a/app/JustHostMC.App/Views/ShopDetailPage.xaml.cs +++ b/app/JustHostMC.App/Views/ShopDetailPage.xaml.cs @@ -115,48 +115,16 @@ private async void OnInstallClick(object sender, RoutedEventArgs e) { } // Prompts for a name + memory, then creates a new server from a modpack - // version. Built imperatively to avoid ItemsControl/x:Bind template - // pitfalls. + // version. The dialog owns its localized chrome and sizing in XAML. private async Task CreateServerFlow(ShopVersionItem version) { - var nameBox = new TextBox { - Header = _localizer.Get("Shop_CreateServerNameLabel"), - Text = ViewModel.Card?.Title ?? "", - }; - var memoryBox = new NumberBox { - Header = _localizer.Get("Shop_CreateServerMemoryLabel"), - Value = 4096, - Minimum = 512, - Maximum = 65536, - SmallChange = 512, - LargeChange = 1024, - SpinButtonPlacementMode = NumberBoxSpinButtonPlacementMode.Inline, - }; - var panel = new StackPanel { Spacing = 12 }; - panel.Children.Add(new TextBlock { - Text = string.Format(_localizer.Get("Shop_CreateServerPromptBody"), - version.Name), - TextWrapping = TextWrapping.Wrap, - }); - panel.Children.Add(nameBox); - panel.Children.Add(memoryBox); - - var dialog = new ContentDialog { - XamlRoot = XamlRoot, - Title = _localizer.Get("Shop_CreateServerTitle"), - Content = panel, - PrimaryButtonText = _localizer.Get("Shop_CreateServerConfirm"), - CloseButtonText = _localizer.Get("Common_Cancel"), - DefaultButton = ContentDialogButton.Primary, + var fallbackName = ViewModel.Card?.Title ?? "Modpack"; + var dialog = new CreateShopServerDialog(fallbackName) { + XamlRoot = XamlRoot, }; if (await dialog.ShowAsync() != ContentDialogResult.Primary) return; - - var name = nameBox.Text.Trim(); - if (name.Length == 0) - name = ViewModel.Card?.Title ?? "Modpack"; - var memory = - double.IsNaN(memoryBox.Value) ? 4096 : (int)memoryBox.Value; - await ViewModel.CreateServerAsync(version, name, memory); + await ViewModel.CreateServerAsync(version, dialog.ServerName, + dialog.MemoryMb); } private async void OnOpenWebsite(object sender, RoutedEventArgs e) { diff --git a/app/JustHostMC.App/Views/ShopHomePage.xaml b/app/JustHostMC.App/Views/ShopHomePage.xaml index b9a6a16..36bb2ee 100644 --- a/app/JustHostMC.App/Views/ShopHomePage.xaml +++ b/app/JustHostMC.App/Views/ShopHomePage.xaml @@ -197,17 +197,32 @@ HorizontalAlignment="Left" VerticalAlignment="Center" Spacing="10"> - - + + + + + + + + Shop landing page: trending / most-downloaded sections. public sealed partial class ShopHomePage : Page { - private readonly ILocalizer _localizer = new LocalizationService(); private ShopWindow? _window; public ShopViewModel ViewModel { get; private set; } = null!; @@ -26,13 +24,6 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { _window = args.Window; ViewModel = args.Shop; Bindings.Update(); - // The x:Uid welcome copy is written for the server-scoped shop; the - // home-page modpack shop gets its own wording. - if (!ViewModel.Context.IsServerScoped) { - DiscoverTitle.Text = _localizer.Get("Shop_DiscoverTitleGlobal"); - DiscoverSubtitle.Text = - _localizer.Get("Shop_DiscoverSubtitleGlobal"); - } if (ViewModel.HomeSections.Count == 0) _ = ViewModel.LoadHomeAsync(); } diff --git a/app/JustHostMC.App/Views/ShopWindow.xaml b/app/JustHostMC.App/Views/ShopWindow.xaml index b9bc697..eb0b5ee 100644 --- a/app/JustHostMC.App/Views/ShopWindow.xaml +++ b/app/JustHostMC.App/Views/ShopWindow.xaml @@ -33,6 +33,12 @@ + + + ") { t.Fatal("build.ps1 must supply redacted display arguments for the key-bearing engine build") } + if !strings.Contains(buildScript, "Remove-Item Env:JHMC_CURSEFORGE_API_KEY") { + t.Fatal("build.ps1 must not pass the plaintext key to the child go process") + } +} + +func TestReleaseWorkflowUsesObfuscatedKeyFragment(t *testing.T) { + workflow := repositoryFile(t, ".github", "workflows", "release.yml") + if strings.Contains(workflow, "main.defaultCurseForgeKey=") { + t.Fatal("release workflow must not inject the CurseForge key as plaintext") + } + if !strings.Contains(workflow, "Get-CurseForgeKeyLdflagsFragment") { + t.Fatal("release workflow must use the shared XOR key helper") + } + if !strings.Contains(workflow, "Remove-Item Env:JHMC_CURSEFORGE_API_KEY") { + t.Fatal("release workflow must not pass the plaintext key to the child go process") + } } diff --git a/engine/internal/backup/backup.go b/engine/internal/backup/backup.go index 451d860..06ba605 100644 --- a/engine/internal/backup/backup.go +++ b/engine/internal/backup/backup.go @@ -30,12 +30,8 @@ func Archive(srcDir, destZip string) error { if err != nil { return err } - defer out.Close() - zw := zip.NewWriter(out) - defer zw.Close() - - return filepath.WalkDir(srcDir, func(path string, d os.DirEntry, walkErr error) error { + walkErr := filepath.WalkDir(srcDir, func(path string, d os.DirEntry, walkErr error) error { if walkErr != nil { return walkErr } @@ -51,6 +47,22 @@ func Archive(srcDir, destZip string) error { } return addFile(zw, path, filepath.ToSlash(rel)) }) + if walkErr != nil { + _ = zw.Close() + _ = out.Close() + _ = os.Remove(destZip) + return walkErr + } + if err := zw.Close(); err != nil { + _ = out.Close() + _ = os.Remove(destZip) + return fmt.Errorf("finalize archive: %w", err) + } + if err := out.Close(); err != nil { + _ = os.Remove(destZip) + return fmt.Errorf("close archive: %w", err) + } + return nil } func addFile(zw *zip.Writer, path, name string) error { diff --git a/engine/internal/grpc/serverservice.go b/engine/internal/grpc/serverservice.go index 33995ff..0142005 100644 --- a/engine/internal/grpc/serverservice.go +++ b/engine/internal/grpc/serverservice.go @@ -13,6 +13,7 @@ import ( mcmanagerv1 "github.com/000hen/justhostmc/engine/gen/mcmanager/v1" "github.com/000hen/justhostmc/engine/internal/appdata" + "github.com/000hen/justhostmc/engine/internal/backup" "github.com/000hen/justhostmc/engine/internal/console" "github.com/000hen/justhostmc/engine/internal/isolation" "github.com/000hen/justhostmc/engine/internal/jre" @@ -404,7 +405,6 @@ func (s *ServerService) Update(ctx context.Context, req *mcmanagerv1.UpdateServe // streaming progress like Create. Unlike Create, a failure leaves the existing // install untouched (no cleanup wipe) — the old version keeps working. func (s *ServerService) UpdateModpack(req *mcmanagerv1.UpdateModpackRequest, stream grpc.ServerStreamingServer[mcmanagerv1.InstallProgress]) error { - ctx := stream.Context() rec, ok := s.cfg.Store.Get(req.Id) if !ok { return status.Error(codes.NotFound, "server not found") @@ -430,12 +430,82 @@ func (s *ServerService) UpdateModpack(req *mcmanagerv1.UpdateModpackRequest, str return status.Error(codes.Unimplemented, "provider does not support update") } - oldVersion := rec.ProviderVersion + updateCtx, cancelUpdate := context.WithCancel(stream.Context()) + installation := &activeInstallation{ + cancel: cancelUpdate, + done: make(chan struct{}), + } + + // Claim the server and persist INSTALLING while holding the lifecycle lock. + // Start makes its reciprocal STOPPED -> STARTING transition under this same + // lock, so exactly one of the two operations can win. + s.mu.Lock() + rec, ok = s.cfg.Store.Get(req.Id) + if !ok { + s.mu.Unlock() + cancelUpdate() + return status.Error(codes.NotFound, "server not found") + } + if s.installations[req.Id] != nil { + s.mu.Unlock() + cancelUpdate() + return status.Error(codes.FailedPrecondition, "server installation is already in progress") + } + if s.instances[req.Id] != nil || rec.Status != mcmanagerv1.ServerStatus_STOPPED { + s.mu.Unlock() + cancelUpdate() + return errorStatus(codes.FailedPrecondition, mcmanagerv1.ErrorCode_SERVER_RUNNING, + "server must be stopped before updating the modpack", nil) + } + original := *rec + original.LaunchArgs = append([]string(nil), rec.LaunchArgs...) + s.installations[req.Id] = installation rec.Status = mcmanagerv1.ServerStatus_INSTALLING - _ = s.cfg.Store.Put(rec) - restore := func() { - rec.Status = mcmanagerv1.ServerStatus_STOPPED - _ = s.cfg.Store.Put(rec) + if err := s.cfg.Store.Put(rec); err != nil { + delete(s.installations, req.Id) + s.mu.Unlock() + cancelUpdate() + return status.Errorf(codes.Internal, "save update state: %v", err) + } + s.mu.Unlock() + defer func() { + cancelUpdate() + s.mu.Lock() + if s.installations[req.Id] == installation { + delete(s.installations, req.Id) + } + close(installation.done) + s.mu.Unlock() + }() + + oldVersion := rec.ProviderVersion + dir := s.cfg.Paths.ServerDir(rec.ID) + snapshotDir := filepath.Join(s.cfg.Paths.Base, ".update-snapshots") + snapshotPath := filepath.Join(snapshotDir, rec.ID+"-"+genID()+".zip") + if err := backup.Archive(dir, snapshotPath); err != nil { + original.Status = mcmanagerv1.ServerStatus_STOPPED + if restoreErr := s.cfg.Store.Put(&original); restoreErr != nil { + return status.Errorf(codes.Internal, "snapshot server before update: %v; restore state: %v", err, restoreErr) + } + return status.Errorf(codes.Internal, "snapshot server before update: %v", err) + } + defer func() { + _ = os.Remove(snapshotPath) + _ = os.Remove(snapshotDir) + }() + + rollback := func(updateErr error) error { + if err := backup.Restore(snapshotPath, dir); err != nil { + return status.Errorf(codes.Internal, "update failed (%v) and restore failed: %v", updateErr, err) + } + original.Status = mcmanagerv1.ServerStatus_STOPPED + if err := s.cfg.Store.Put(&original); err != nil { + return status.Errorf(codes.Internal, "update failed (%v) and restoring server state failed: %v", updateErr, err) + } + return mapInstallError(updateErr) + } + if err := updateCtx.Err(); err != nil { + return rollback(err) } il := s.openInstallLog(rec.ID) @@ -447,23 +517,24 @@ func (s *ServerService) UpdateModpack(req *mcmanagerv1.UpdateModpackRequest, str base(p) } - spec, err := up.Update(ctx, s.cfg.Paths.ServerDir(rec.ID), newVersion, oldVersion, send) + spec, err := up.Update(updateCtx, dir, newVersion, oldVersion, send) if err != nil { send(provider.Progress{LogLine: "[error] " + err.Error()}) il.recordLine("[error] update: " + err.Error()) - restore() - return mapInstallError(err) + return rollback(err) } resolved := rec.McVersion if spec.McVersion != "" { resolved = spec.McVersion } spec.JavaMajor = maxJavaMajor(spec.JavaMajor, resolved) - if _, err := s.cfg.JRE.EnsureJRE(ctx, spec.JavaMajor, send); err != nil { + if _, err := s.cfg.JRE.EnsureJRE(updateCtx, spec.JavaMajor, send); err != nil { send(provider.Progress{LogLine: "[error] " + err.Error()}) il.recordLine("[error] jre: " + err.Error()) - restore() - return mapInstallError(err) + return rollback(err) + } + if err := updateCtx.Err(); err != nil { + return rollback(err) } rec.JavaMajor = spec.JavaMajor @@ -480,7 +551,9 @@ func (s *ServerService) UpdateModpack(req *mcmanagerv1.UpdateModpackRequest, str rec.ProviderVersion = spec.PackVersion } rec.Status = mcmanagerv1.ServerStatus_STOPPED - _ = s.cfg.Store.Put(rec) + if err := s.cfg.Store.Put(rec); err != nil { + return rollback(fmt.Errorf("save updated server: %w", err)) + } send(provider.Progress{Step: "install.progress.done", Fraction: 1}) return nil @@ -589,12 +662,65 @@ func (s *ServerService) Start(ctx context.Context, req *mcmanagerv1.ServerId) (* return nil, status.Error(codes.NotFound, "server not found") } + startCtx, cancelStart := context.WithCancel(ctx) + operation := &activeInstallation{ + cancel: cancelStart, + done: make(chan struct{}), + } s.mu.Lock() - _, running := s.instances[req.Id] - s.mu.Unlock() - if running { + if s.instances[req.Id] != nil { + s.mu.Unlock() + cancelStart() return &mcmanagerv1.Empty{}, nil // idempotent } + if s.installations[req.Id] != nil { + s.mu.Unlock() + cancelStart() + return nil, status.Error(codes.FailedPrecondition, "server installation is in progress") + } + rec, ok = s.cfg.Store.Get(req.Id) + if !ok { + s.mu.Unlock() + cancelStart() + return nil, status.Error(codes.NotFound, "server not found") + } + if rec.Status == mcmanagerv1.ServerStatus_STARTING { + s.mu.Unlock() + cancelStart() + return &mcmanagerv1.Empty{}, nil + } + if rec.Status == mcmanagerv1.ServerStatus_INSTALLING { + s.mu.Unlock() + cancelStart() + return nil, status.Error(codes.FailedPrecondition, "server installation is in progress") + } + previousStatus := rec.Status + s.installations[req.Id] = operation + rec.Status = mcmanagerv1.ServerStatus_STARTING + if err := s.cfg.Store.Put(rec); err != nil { + delete(s.installations, req.Id) + close(operation.done) + s.mu.Unlock() + cancelStart() + return nil, status.Errorf(codes.Internal, "save start state: %v", err) + } + s.mu.Unlock() + defer func() { + cancelStart() + s.mu.Lock() + if s.installations[req.Id] == operation { + delete(s.installations, req.Id) + } + close(operation.done) + s.mu.Unlock() + }() + restoreStatus := func() { + latest, exists := s.cfg.Store.Get(req.Id) + if exists && latest.Status == mcmanagerv1.ServerStatus_STARTING { + latest.Status = previousStatus + _ = s.cfg.Store.Put(latest) + } + } javaMajor := maxJavaMajor(rec.JavaMajor, rec.McVersion) if javaMajor != rec.JavaMajor { @@ -602,12 +728,13 @@ func (s *ServerService) Start(ctx context.Context, req *mcmanagerv1.ServerId) (* _ = s.cfg.Store.Put(rec) } - javaPath, err := s.cfg.JRE.EnsureJRE(ctx, javaMajor, nil) + javaPath, err := s.cfg.JRE.EnsureJRE(startCtx, javaMajor, nil) if err != nil { + restoreStatus() return nil, mapInstallError(err) } - inst, err := s.cfg.Backend.Start(ctx, isolation.InstanceSpec{ + inst, err := s.cfg.Backend.Start(startCtx, isolation.InstanceSpec{ ID: rec.ID, Dir: s.cfg.Paths.ServerDir(rec.ID), JavaPath: javaPath, @@ -617,21 +744,44 @@ func (s *ServerService) Start(ctx context.Context, req *mcmanagerv1.ServerId) (* Port: rec.Port, }) if err != nil { + restoreStatus() + if startCtx.Err() != nil { + return nil, status.FromContextError(startCtx.Err()).Err() + } return nil, status.Errorf(codes.Internal, "start server: %v", err) } s.mu.Lock() + if startCtx.Err() != nil { + s.mu.Unlock() + cleanupCtx, cancelCleanup := context.WithTimeout(context.Background(), 30*time.Second) + _ = s.cfg.Backend.Stop(cleanupCtx, req.Id, false) + select { + case <-inst.Done(): + case <-cleanupCtx.Done(): + } + cancelCleanup() + restoreStatus() + return nil, status.FromContextError(startCtx.Err()).Err() + } s.instances[rec.ID] = inst delete(s.stopping, rec.ID) + rec.Status = mcmanagerv1.ServerStatus_RUNNING + if err := s.cfg.Store.Put(rec); err != nil { + delete(s.instances, rec.ID) + s.mu.Unlock() + cleanupCtx, cancelCleanup := context.WithTimeout(context.Background(), 30*time.Second) + _ = s.cfg.Backend.Stop(cleanupCtx, req.Id, false) + cancelCleanup() + restoreStatus() + return nil, status.Errorf(codes.Internal, "save running state: %v", err) + } s.mu.Unlock() if s.cfg.Console != nil { s.cfg.Console.Register(rec.ID, inst) } - rec.Status = mcmanagerv1.ServerStatus_RUNNING - _ = s.cfg.Store.Put(rec) - go s.watchExit(rec.ID, inst) return &mcmanagerv1.Empty{}, nil } @@ -645,11 +795,36 @@ func (s *ServerService) Stop(ctx context.Context, req *mcmanagerv1.ServerId) (*m s.mu.Lock() inst := s.instances[req.Id] + operation := s.installations[req.Id] if inst != nil { s.stopping[req.Id] = true } + if operation != nil { + operation.cancel() + } s.mu.Unlock() + if operation != nil { + select { + case <-operation.done: + case <-ctx.Done(): + return nil, status.FromContextError(ctx.Err()).Err() + } + // The operation may have published an instance immediately before Stop + // acquired the lifecycle lock, so refresh both persisted and live state. + latest, exists := s.cfg.Store.Get(req.Id) + if !exists { + return &mcmanagerv1.Empty{}, nil + } + rec = latest + s.mu.Lock() + inst = s.instances[req.Id] + if inst != nil { + s.stopping[req.Id] = true + } + s.mu.Unlock() + } + if inst == nil { rec.Status = mcmanagerv1.ServerStatus_STOPPED _ = s.cfg.Store.Put(rec) @@ -721,14 +896,26 @@ func (s *ServerService) Delete(ctx context.Context, req *mcmanagerv1.ServerId) ( // lives at the data-dir root (not under a wiped subtree), so its open handle stays // valid and the engine keeps running afterwards. func (s *ServerService) RemoveAllData(ctx context.Context, _ *mcmanagerv1.Empty) (*mcmanagerv1.Empty, error) { - // Snapshot and force-stop every running instance. + // Snapshot and cancel every active operation before wiping its data. s.mu.Lock() insts := make(map[string]isolation.Instance, len(s.instances)) for id, inst := range s.instances { insts[id] = inst s.stopping[id] = true } + installations := make([]*activeInstallation, 0, len(s.installations)) + for _, installation := range s.installations { + installation.cancel() + installations = append(installations, installation) + } s.mu.Unlock() + for _, installation := range installations { + select { + case <-installation.done: + case <-ctx.Done(): + return nil, status.FromContextError(ctx.Err()).Err() + } + } for id, inst := range insts { _ = s.cfg.Backend.Stop(ctx, id, false) @@ -769,6 +956,7 @@ func (s *ServerService) RemoveAllData(ctx context.Context, _ *mcmanagerv1.Empty) s.mu.Lock() s.instances = make(map[string]isolation.Instance) s.stopping = make(map[string]bool) + s.installations = make(map[string]*activeInstallation) s.mu.Unlock() return &mcmanagerv1.Empty{}, nil diff --git a/engine/internal/grpc/serverservice_start_test.go b/engine/internal/grpc/serverservice_start_test.go index 482ef0f..f764e64 100644 --- a/engine/internal/grpc/serverservice_start_test.go +++ b/engine/internal/grpc/serverservice_start_test.go @@ -2,18 +2,35 @@ package grpcsvc import ( "context" + "errors" "fmt" "os" "path/filepath" "strings" "testing" + "time" mcmanagerv1 "github.com/000hen/justhostmc/engine/gen/mcmanager/v1" "github.com/000hen/justhostmc/engine/internal/appdata" + "github.com/000hen/justhostmc/engine/internal/isolation" "github.com/000hen/justhostmc/engine/internal/jre" "github.com/000hen/justhostmc/engine/internal/store" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) +type blockingStartBackend struct { + fakeBackend + started chan struct{} +} + +func (b *blockingStartBackend) Start(ctx context.Context, spec isolation.InstanceSpec) (isolation.Instance, error) { + b.startSpec = spec + close(b.started) + <-ctx.Done() + return nil, ctx.Err() +} + func TestStartUpgradesStaleJavaMajorForCurrentMinecraft(t *testing.T) { paths := appdata.New(t.TempDir()) cacheJava(t, paths.JRECache(), 25) @@ -50,6 +67,108 @@ func TestStartUpgradesStaleJavaMajorForCurrentMinecraft(t *testing.T) { } } +func TestDeleteCancelsAndWaitsForStart(t *testing.T) { + svc, st, paths, backend := startRaceService(t) + startDone := make(chan error, 1) + go func() { + _, err := svc.Start(context.Background(), &mcmanagerv1.ServerId{Id: "s1"}) + startDone <- err + }() + waitForStart(t, backend.started) + + if _, err := svc.Delete(context.Background(), &mcmanagerv1.ServerId{Id: "s1"}); err != nil { + t.Fatalf("Delete: %v", err) + } + if err := <-startDone; status.Code(err) != codes.Canceled { + t.Fatalf("Start error = %v, want context canceled", err) + } + if _, ok := st.Get("s1"); ok { + t.Fatal("canceled Start resurrected deleted server") + } + if _, err := os.Stat(paths.ServerDir("s1")); !errors.Is(err, os.ErrNotExist) { + t.Errorf("server directory still exists after delete: %v", err) + } +} + +func TestRemoveAllDataCancelsAndWaitsForStart(t *testing.T) { + svc, st, paths, backend := startRaceService(t) + startDone := make(chan error, 1) + go func() { + _, err := svc.Start(context.Background(), &mcmanagerv1.ServerId{Id: "s1"}) + startDone <- err + }() + waitForStart(t, backend.started) + + if _, err := svc.RemoveAllData(context.Background(), &mcmanagerv1.Empty{}); err != nil { + t.Fatalf("RemoveAllData: %v", err) + } + if err := <-startDone; status.Code(err) != codes.Canceled { + t.Fatalf("Start error = %v, want context canceled", err) + } + if _, ok := st.Get("s1"); ok { + t.Fatal("canceled Start resurrected server after RemoveAllData") + } + if _, err := os.Stat(paths.ServersRoot()); !errors.Is(err, os.ErrNotExist) { + t.Errorf("servers root still exists after RemoveAllData: %v", err) + } +} + +func TestStopCancelsAndWaitsForStart(t *testing.T) { + svc, st, _, backend := startRaceService(t) + startDone := make(chan error, 1) + go func() { + _, err := svc.Start(context.Background(), &mcmanagerv1.ServerId{Id: "s1"}) + startDone <- err + }() + waitForStart(t, backend.started) + + if _, err := svc.Stop(context.Background(), &mcmanagerv1.ServerId{Id: "s1"}); err != nil { + t.Fatalf("Stop: %v", err) + } + if err := <-startDone; status.Code(err) != codes.Canceled { + t.Fatalf("Start error = %v, want context canceled", err) + } + rec, ok := st.Get("s1") + if !ok { + t.Fatal("Stop deleted the server record") + } + if rec.Status != mcmanagerv1.ServerStatus_STOPPED { + t.Fatalf("status = %v, want STOPPED", rec.Status) + } + if _, running := svc.Instance("s1"); running { + t.Fatal("canceled Start published a running instance after Stop") + } +} + +func startRaceService(t *testing.T) (*ServerService, *store.Memory, appdata.Paths, *blockingStartBackend) { + t.Helper() + paths := appdata.New(t.TempDir()) + cacheJava(t, paths.JRECache(), 21) + if err := os.MkdirAll(paths.ServerDir("s1"), 0o755); err != nil { + t.Fatal(err) + } + st := store.NewMemory() + _ = st.Put(&store.Server{ + ID: "s1", Name: "s1", McVersion: "1.21.1", MemoryMB: 1024, + Port: 25565, Status: mcmanagerv1.ServerStatus_STOPPED, + JavaMajor: 21, LaunchArgs: []string{"-jar", "server.jar", "nogui"}, + }) + backend := &blockingStartBackend{started: make(chan struct{})} + svc := NewServerService(ServerServiceConfig{ + Store: st, JRE: jre.NewManager(paths.JRECache()), Backend: backend, Paths: paths, + }) + return svc, st, paths, backend +} + +func waitForStart(t *testing.T, started <-chan struct{}) { + t.Helper() + select { + case <-started: + case <-time.After(5 * time.Second): + t.Fatal("Start did not reach the backend") + } +} + func cacheJava(t *testing.T, cache string, major int) { t.Helper() binDir := filepath.Join(cache, fmt.Sprint(major), fmt.Sprintf("jdk-%d-jre", major), "bin") diff --git a/engine/internal/grpc/serverservice_updatemodpack_test.go b/engine/internal/grpc/serverservice_updatemodpack_test.go index 04e8eda..3105524 100644 --- a/engine/internal/grpc/serverservice_updatemodpack_test.go +++ b/engine/internal/grpc/serverservice_updatemodpack_test.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "testing" + "time" mcmanagerv1 "github.com/000hen/justhostmc/engine/gen/mcmanager/v1" "github.com/000hen/justhostmc/engine/internal/appdata" @@ -23,13 +24,28 @@ type updateStubProvider struct { gotVersion, gotOld string updateSpec provider.LaunchSpec updateErr error + mutate func(dir string) error + started chan struct{} + blockUntilCanceled bool } -func (p *updateStubProvider) Update(_ context.Context, _, version, oldVersion string, report func(provider.Progress)) (provider.LaunchSpec, error) { +func (p *updateStubProvider) Update(ctx context.Context, dir, version, oldVersion string, report func(provider.Progress)) (provider.LaunchSpec, error) { p.gotVersion, p.gotOld = version, oldVersion if report != nil { report(provider.Progress{Step: "shop.install.downloading", Fraction: 0.5}) } + if p.mutate != nil { + if err := p.mutate(dir); err != nil { + return provider.LaunchSpec{}, err + } + } + if p.started != nil { + close(p.started) + } + if p.blockUntilCanceled { + <-ctx.Done() + return provider.LaunchSpec{}, ctx.Err() + } return p.updateSpec, p.updateErr } @@ -141,7 +157,15 @@ func TestUpdateModpackUnsupportedIsUnimplemented(t *testing.T) { // TestUpdateModpackFailureLeavesInstallIntact proves an update error restores // STOPPED, keeps the record and the server dir — unlike Create's wipe. func TestUpdateModpackFailureLeavesInstallIntact(t *testing.T) { - prov := &updateStubProvider{updateErr: errors.New("cdn exploded")} + prov := &updateStubProvider{ + updateErr: errors.New("cdn exploded"), + mutate: func(dir string) error { + if err := os.WriteFile(filepath.Join(dir, "world-data.txt"), []byte("mutated"), 0o644); err != nil { + return err + } + return os.WriteFile(filepath.Join(dir, "new-mod.jar"), []byte("partial"), 0o644) + }, + } svc, st, paths := modpackTestService(t, prov, mcmanagerv1.ServerStatus_STOPPED, "95/100") marker := filepath.Join(paths.ServerDir("s1"), "world-data.txt") if err := os.WriteFile(marker, []byte("keep me"), 0o644); err != nil { @@ -166,4 +190,110 @@ func TestUpdateModpackFailureLeavesInstallIntact(t *testing.T) { if _, err := os.Stat(marker); err != nil { t.Errorf("server dir wiped on failure: %v", err) } + got, err := os.ReadFile(marker) + if err != nil || string(got) != "keep me" { + t.Errorf("marker after rollback = %q, %v; want original content", got, err) + } + if _, err := os.Stat(filepath.Join(paths.ServerDir("s1"), "new-mod.jar")); !errors.Is(err, os.ErrNotExist) { + t.Errorf("partial new file survived rollback: %v", err) + } +} + +func TestDeleteCancelsAndWaitsForModpackUpdate(t *testing.T) { + started := make(chan struct{}) + prov := &updateStubProvider{started: started, blockUntilCanceled: true} + svc, st, paths := modpackTestService(t, prov, mcmanagerv1.ServerStatus_STOPPED, "95/100") + + updateDone := make(chan error, 1) + go func() { + updateDone <- svc.UpdateModpack( + &mcmanagerv1.UpdateModpackRequest{Id: "s1", Version: "95/300"}, + &fakeInstallStream{}) + }() + select { + case <-started: + case <-time.After(5 * time.Second): + t.Fatal("update did not start") + } + + if _, err := svc.Delete(context.Background(), &mcmanagerv1.ServerId{Id: "s1"}); err != nil { + t.Fatalf("Delete: %v", err) + } + select { + case err := <-updateDone: + if err == nil { + t.Fatal("canceled update succeeded") + } + case <-time.After(5 * time.Second): + t.Fatal("Delete returned before update stopped") + } + if _, ok := st.Get("s1"); ok { + t.Fatal("canceled update resurrected deleted server") + } + if _, err := os.Stat(paths.ServerDir("s1")); !errors.Is(err, os.ErrNotExist) { + t.Errorf("server directory still exists after delete: %v", err) + } +} + +func TestStartRejectsActiveModpackUpdate(t *testing.T) { + started := make(chan struct{}) + prov := &updateStubProvider{started: started, blockUntilCanceled: true} + svc, _, _ := modpackTestService(t, prov, mcmanagerv1.ServerStatus_STOPPED, "95/100") + + updateDone := make(chan error, 1) + go func() { + updateDone <- svc.UpdateModpack( + &mcmanagerv1.UpdateModpackRequest{Id: "s1", Version: "95/300"}, + &fakeInstallStream{}) + }() + select { + case <-started: + case <-time.After(5 * time.Second): + t.Fatal("update did not start") + } + + _, err := svc.Start(context.Background(), &mcmanagerv1.ServerId{Id: "s1"}) + if status.Code(err) != codes.FailedPrecondition { + t.Fatalf("Start code = %v, want FailedPrecondition", status.Code(err)) + } + + svc.mu.Lock() + svc.installations["s1"].cancel() + svc.mu.Unlock() + <-updateDone +} + +func TestStopCancelsAndWaitsForModpackUpdate(t *testing.T) { + started := make(chan struct{}) + prov := &updateStubProvider{started: started, blockUntilCanceled: true} + svc, st, _ := modpackTestService(t, prov, mcmanagerv1.ServerStatus_STOPPED, "95/100") + + updateDone := make(chan error, 1) + go func() { + updateDone <- svc.UpdateModpack( + &mcmanagerv1.UpdateModpackRequest{Id: "s1", Version: "95/300"}, + &fakeInstallStream{}) + }() + select { + case <-started: + case <-time.After(5 * time.Second): + t.Fatal("update did not start") + } + + if _, err := svc.Stop(context.Background(), &mcmanagerv1.ServerId{Id: "s1"}); err != nil { + t.Fatalf("Stop: %v", err) + } + if err := <-updateDone; status.Code(err) != codes.Canceled { + t.Fatalf("UpdateModpack error = %v, want Canceled", err) + } + rec, ok := st.Get("s1") + if !ok { + t.Fatal("Stop deleted the server record") + } + if rec.Status != mcmanagerv1.ServerStatus_STOPPED { + t.Fatalf("status = %v, want STOPPED", rec.Status) + } + if rec.ProviderVersion != "95/100" { + t.Fatalf("ProviderVersion = %q, want original version", rec.ProviderVersion) + } } diff --git a/keycipher.ps1 b/keycipher.ps1 index af7d59b..f582b61 100644 --- a/keycipher.ps1 +++ b/keycipher.ps1 @@ -9,9 +9,11 @@ app/Engine.targets) bakes the same kind of key: - build.ps1 dot-sources this file and calls Get-CurseForgeKeyLdflagsFragment. - - app/Engine.targets executes this file directly and captures stdout. + - app/Engine.targets executes this file with -BuildEngine so reversible + key material never enters an MSBuild property or binlog. - When executed directly this script prints EXACTLY one line to stdout: + When executed directly without -BuildEngine this script prints EXACTLY one + line to stdout: * the fragment -X main.defaultCurseForgeKeyCipher= -X main.defaultCurseForgeKeyPad= when $env:JHMC_CURSEFORGE_API_KEY is set, or * an empty line when it is not. @@ -31,6 +33,13 @@ PS7 static ::Fill) and avoids PS7-only syntax (no '&&', no ternary). #> +[CmdletBinding()] +param( + [switch]$BuildEngine, + [string]$EngineSourceDir, + [string]$EngineOutput +) + function Get-KeyCipherPadBytes { # Returns the XOR pad as a byte[]. Uses JHMC_KEY_CIPHER_PAD when set # (validated: hex only, even length, >= 32 bytes decoded; invalid -> throw), @@ -88,13 +97,48 @@ function Get-CurseForgeKeyLdflagsFragment { return "-X main.defaultCurseForgeKeyCipher=$cipher -X main.defaultCurseForgeKeyPad=$padHex" } +function Invoke-DefaultEngineBuild { + param( + [Parameter(Mandatory = $true)][string]$SourceDir, + [Parameter(Mandatory = $true)][string]$OutputPath + ) + + $ldflags = '-s -w -buildid=' + $keyFragment = Get-CurseForgeKeyLdflagsFragment + # The linker needs only the derived cipher/pad. Do not pass the plaintext + # source secret through to the child go process environment. + Remove-Item Env:JHMC_CURSEFORGE_API_KEY -ErrorAction SilentlyContinue + if ($keyFragment) { + $ldflags += " $keyFragment" + } + + Push-Location $SourceDir + try { + & go build -trimpath -buildvcs=false -mod=readonly ` + "-ldflags=$ldflags" -o $OutputPath ./cmd/engine + if ($LASTEXITCODE -ne 0) { + throw "go build failed with exit code $LASTEXITCODE." + } + } finally { + Pop-Location + } +} + # When executed directly (not dot-sourced), emit the fragment on one stdout line # and translate a validation failure into stderr + exit 1. Dot-sourcing (build.ps1) # sets InvocationName to '.' and skips this block, importing only the functions. if ($MyInvocation.InvocationName -ne '.') { try { - $fragment = Get-CurseForgeKeyLdflagsFragment - [Console]::Out.WriteLine($fragment) + if ($BuildEngine) { + if (-not $EngineSourceDir -or -not $EngineOutput) { + throw '-BuildEngine requires -EngineSourceDir and -EngineOutput.' + } + Invoke-DefaultEngineBuild -SourceDir $EngineSourceDir ` + -OutputPath $EngineOutput + } else { + $fragment = Get-CurseForgeKeyLdflagsFragment + [Console]::Out.WriteLine($fragment) + } } catch { [Console]::Error.WriteLine($_.Exception.Message) exit 1 From 599e582a49b9082b60135d63fa8b5edbe7350260 Mon Sep 17 00:00:00 2001 From: 000hen Date: Sat, 18 Jul 2026 22:41:52 +0800 Subject: [PATCH 26/30] fix: keep modpack operations active in tray --- .../TrayLifecycleTests.cs | 31 +++++++++++++++++++ .../ViewModels/MainViewModel.cs | 2 ++ 2 files changed, 33 insertions(+) create mode 100644 app/JustHostMC.App.ResourceTests/TrayLifecycleTests.cs diff --git a/app/JustHostMC.App.ResourceTests/TrayLifecycleTests.cs b/app/JustHostMC.App.ResourceTests/TrayLifecycleTests.cs new file mode 100644 index 0000000..fc9c955 --- /dev/null +++ b/app/JustHostMC.App.ResourceTests/TrayLifecycleTests.cs @@ -0,0 +1,31 @@ +using Xunit; + +namespace JustHostMC.App.ResourceTests; + +public sealed class TrayLifecycleTests { + [Fact] + public void ModpackOperationsRegisterWithBackgroundTaskService() { + var source = + RepositoryLayout.ReadAppFile("ViewModels", "MainViewModel.cs"); + var update = + MethodBetween(source, "public async Task UpdateModpackAsync", + "public async Task ExportModpackAsync"); + var export = + MethodBetween(source, "public async Task ExportModpackAsync", + "private void ApplyTrackerProgress"); + + Assert.Contains("_backgroundTasks.Begin(\"modpack-update\")", update, + StringComparison.Ordinal); + Assert.Contains("_backgroundTasks.Begin(\"modpack-export\")", export, + StringComparison.Ordinal); + } + + private static string MethodBetween(string source, string startMarker, + string endMarker) { + var start = source.IndexOf(startMarker, StringComparison.Ordinal); + Assert.True(start >= 0, $"Missing method marker: {startMarker}"); + var end = source.IndexOf(endMarker, start, StringComparison.Ordinal); + Assert.True(end > start, $"Missing method marker: {endMarker}"); + return source[start..end]; + } +} diff --git a/app/JustHostMC.App/ViewModels/MainViewModel.cs b/app/JustHostMC.App/ViewModels/MainViewModel.cs index 28a9b48..df4315d 100644 --- a/app/JustHostMC.App/ViewModels/MainViewModel.cs +++ b/app/JustHostMC.App/ViewModels/MainViewModel.cs @@ -271,6 +271,7 @@ private async Task RunNewServerInstallAsync( /// progress into the server's global tracker so the operation survives /// window/page changes. public async Task UpdateModpackAsync(ServerItem server, string version) { + using var backgroundTask = _backgroundTasks.Begin("modpack-update"); var tracker = ProgressService.GetOrCreateTracker(server.Id, server.Name); RunOnUI(() => { @@ -306,6 +307,7 @@ public async Task UpdateModpackAsync(ServerItem server, string version) { /// Exports a modpack server as a CurseForge client pack zip, /// streaming progress into the server's global tracker. public async Task ExportModpackAsync(ServerItem server, string destPath) { + using var backgroundTask = _backgroundTasks.Begin("modpack-export"); var tracker = ProgressService.GetOrCreateTracker(server.Id, server.Name); RunOnUI(() => { From 9c27bcf93fea7a67817f1bf8ac1dea51abe6958d Mon Sep 17 00:00:00 2001 From: 000hen Date: Sat, 18 Jul 2026 23:15:53 +0800 Subject: [PATCH 27/30] fix: preserve tray lifecycle for server work --- .../TrayLifecycleTests.cs | 90 +++++++++++++++++++ .../ViewModels/BackupsViewModel.cs | 6 ++ .../ViewModels/MainViewModel.cs | 14 ++- .../ViewModels/SettingsViewModel.cs | 4 + 4 files changed, 113 insertions(+), 1 deletion(-) diff --git a/app/JustHostMC.App.ResourceTests/TrayLifecycleTests.cs b/app/JustHostMC.App.ResourceTests/TrayLifecycleTests.cs index fc9c955..76d7b34 100644 --- a/app/JustHostMC.App.ResourceTests/TrayLifecycleTests.cs +++ b/app/JustHostMC.App.ResourceTests/TrayLifecycleTests.cs @@ -3,6 +3,46 @@ namespace JustHostMC.App.ResourceTests; public sealed class TrayLifecycleTests { + [Fact] + public void ServerStatePathsSynchronizePersistentBackgroundActivity() { + var source = + RepositoryLayout.ReadAppFile("ViewModels", "MainViewModel.cs"); + var merge = MethodBetween(source, "private void MergeServers(", + "private void ApplyServerChange"); + var apply = MethodBetween(source, "private void ApplyServerChange", + "private void UpsertServer"); + + AssertSynchronizesAfterStateUpdate(merge, "_serverState.Reconcile"); + AssertSynchronizesAfterStateUpdate(apply, "_serverState.Apply"); + Assert.Contains( + "_backgroundTasks.SynchronizeServers(_serverState.Servers)", source, + StringComparison.Ordinal); + } + + [Fact] + public void InitialServerSynchronizationKeepsCloseProtectionActive() { + var source = + RepositoryLayout.ReadAppFile("ViewModels", "MainViewModel.cs"); + var connect = + MethodBetween(source, "public async Task ConnectAsync", + "public async Task GetVersionsAsync"); + var merge = MethodBetween(source, "private void MergeServers(", + "private void ApplyServerChange"); + + AssertTracked(source, "public async Task ConnectAsync", + "public async Task GetVersionsAsync", + "server-sync"); + Assert.Contains("await _initialServerSync.Task", connect, + StringComparison.Ordinal); + var synchronize = merge.IndexOf("SynchronizeBackgroundTasks();", + StringComparison.Ordinal); + var ready = merge.IndexOf("_initialServerSync.TrySetResult();", + StringComparison.Ordinal); + Assert.True(ready > synchronize, + "initial synchronization must complete only after " + + "persistent server activity is registered"); + } + [Fact] public void ModpackOperationsRegisterWithBackgroundTaskService() { var source = @@ -20,6 +60,56 @@ public void ModpackOperationsRegisterWithBackgroundTaskService() { StringComparison.Ordinal); } + [Fact] + public void LongRunningMutationsRegisterWithBackgroundTaskService() { + var main = + RepositoryLayout.ReadAppFile("ViewModels", "MainViewModel.cs"); + var backups = + RepositoryLayout.ReadAppFile("ViewModels", "BackupsViewModel.cs"); + var settings = + RepositoryLayout.ReadAppFile("ViewModels", "SettingsViewModel.cs"); + + AssertTracked(main, "public async Task UpdateServerAsync", + "public Task RenameServerAsync", "server-update"); + AssertTracked(main, "private async Task DeleteServer", + "private void ApplyInstallProgress", "server-delete"); + AssertTracked(backups, "private async Task CreateBackup", + "private async Task Restore", "backup-create"); + AssertTracked(backups, "private async Task Restore", + "private async Task Delete", "backup-restore"); + AssertTracked(backups, "private async Task Delete", + "private static string MapBackupError", "backup-delete"); + AssertTracked(settings, "private async Task RemoveAllData", + "private async Task RemoveIncompleteInstallations", + "data-remove"); + AssertTracked( + settings, "private async Task RemoveIncompleteInstallations", + "private static string FormatSize", "incomplete-install-remove"); + } + + private static void AssertSynchronizesAfterStateUpdate( + string method, string stateUpdateMarker) { + var update = + method.IndexOf(stateUpdateMarker, StringComparison.Ordinal); + Assert.True(update >= 0, $"Missing state update: {stateUpdateMarker}"); + var synchronize = method.IndexOf("SynchronizeBackgroundTasks();", + StringComparison.Ordinal); + Assert.True(synchronize > update, + $"{stateUpdateMarker} must be followed by background " + + "server synchronization"); + } + + private static void AssertTracked(string source, string startMarker, + string endMarker, string category) { + var method = MethodBetween(source, startMarker, endMarker); + var begin = method.IndexOf($"BackgroundTasks.Begin(\"{category}\")", + StringComparison.OrdinalIgnoreCase); + Assert.True(begin >= 0, $"{startMarker} does not track {category}"); + var firstAwait = method.IndexOf("await ", StringComparison.Ordinal); + Assert.True(firstAwait < 0 || begin < firstAwait, + $"{category} must be tracked before the first await"); + } + private static string MethodBetween(string source, string startMarker, string endMarker) { var start = source.IndexOf(startMarker, StringComparison.Ordinal); diff --git a/app/JustHostMC.App/ViewModels/BackupsViewModel.cs b/app/JustHostMC.App/ViewModels/BackupsViewModel.cs index b1152b7..b41356b 100644 --- a/app/JustHostMC.App/ViewModels/BackupsViewModel.cs +++ b/app/JustHostMC.App/ViewModels/BackupsViewModel.cs @@ -137,6 +137,8 @@ public async Task LoadAsync() { [RelayCommand] private async Task CreateBackup() { + using var backgroundTask = + App.Current.BackgroundTasks.Begin("backup-create"); RunOnUI(() => { IsBusy = true; SetStatus(BackupStatus.Creating); @@ -160,6 +162,8 @@ await daemon.Backups.CreateAsync( private async Task Restore(BackupItem? item) { if (item is null) return; + using var backgroundTask = + App.Current.BackgroundTasks.Begin("backup-restore"); RunOnUI(() => { IsBusy = true; SetStatus(BackupStatus.Restoring); @@ -182,6 +186,8 @@ await daemon.Backups.RestoreAsync( private async Task Delete(BackupItem? item) { if (item is null) return; + using var backgroundTask = + App.Current.BackgroundTasks.Begin("backup-delete"); RunOnUI(() => { IsBusy = true; SetStatus(BackupStatus.Deleting); diff --git a/app/JustHostMC.App/ViewModels/MainViewModel.cs b/app/JustHostMC.App/ViewModels/MainViewModel.cs index df4315d..fa506c0 100644 --- a/app/JustHostMC.App/ViewModels/MainViewModel.cs +++ b/app/JustHostMC.App/ViewModels/MainViewModel.cs @@ -30,6 +30,8 @@ public partial class MainViewModel : ObservableObject, IAsyncDisposable { private readonly ILocalizer _localizer; private readonly DispatcherQueue _dispatcher; private readonly BackgroundTaskService _backgroundTasks; + private readonly TaskCompletionSource _initialServerSync = + new(TaskCreationOptions.RunContinuationsAsynchronously); private readonly PendingServerUpdates _pendingUpdates = new(); private readonly ServerChangeSynchronizer _serverChanges = new(); private readonly ServerListState _serverState = new(); @@ -115,6 +117,7 @@ public MainViewModel(ILocalizer localizer, DispatcherQueue dispatcher, /// Waits for the engine, probes Health, and starts the server /// change stream. public async Task ConnectAsync() { + using var backgroundTask = _backgroundTasks.Begin("server-sync"); RunOnUI(() => EngineConnectionState = EngineConnectionStatus.Connecting); try { @@ -131,6 +134,7 @@ await daemon.Engine.HealthAsync( TaskContinuationOptions.OnlyOnFaulted); _serverChangesSession ??= CreateServerChangeSession(daemon); await _serverChangesSession.StartAsync(); + await _initialServerSync.Task; } catch (Exception) { RunOnUI(() => EngineConnectionState = EngineConnectionStatus.Failed); @@ -366,6 +370,7 @@ private void FailTracker(ServerProgressTracker tracker, RpcException ex) { } public async Task UpdateServerAsync(UpdateServerRequest request) { + using var backgroundTask = _backgroundTasks.Begin("server-update"); var item = Servers.FirstOrDefault(s => s.Id == request.Id); var rollback = item is null ? null : BuildUpdateRequest(item); var optimistic = request.Clone(); @@ -478,6 +483,7 @@ await daemon.Servers.StopAsync( private async Task DeleteServer(ServerItem? item) { if (item is null) return; + using var backgroundTask = _backgroundTasks.Begin("server-delete"); try { var daemon = await App.Current.DaemonReady; await daemon.Servers.DeleteAsync( @@ -519,7 +525,8 @@ private void MergeServers( System.Collections.Generic.IEnumerable incoming) { var list = incoming.ToList(); _serverState.Reconcile(list); - _backgroundTasks.SynchronizeServers(list); + SynchronizeBackgroundTasks(); + _initialServerSync.TrySetResult(); foreach (var proto in list) UpsertServer(proto); @@ -535,6 +542,7 @@ private void MergeServers( private void ApplyServerChange(ServerChangeEvent change) { _serverState.Apply(change); + SynchronizeBackgroundTasks(); switch (change.ChangeCase) { case ServerChangeEvent.ChangeOneofCase.Upsert: UpsertServer(change.Upsert); @@ -548,6 +556,9 @@ private void ApplyServerChange(ServerChangeEvent change) { OnServerStatsChanged(); } + private void SynchronizeBackgroundTasks() => + _backgroundTasks.SynchronizeServers(_serverState.Servers); + private void UpsertServer(Server proto) { var tracker = ProgressService.GetOrCreateTracker(proto.Id, proto.Name); if (proto.Status is not(ServerStatus.Installing or ServerStatus @@ -639,6 +650,7 @@ private ServerChangeSession CreateServerChangeSession( change => RunOnUI(() => ApplyServerChange(change))); public async ValueTask DisposeAsync() { + _initialServerSync.TrySetCanceled(); if (_serverChangesSession is not null) await _serverChangesSession.DisposeAsync(); } diff --git a/app/JustHostMC.App/ViewModels/SettingsViewModel.cs b/app/JustHostMC.App/ViewModels/SettingsViewModel.cs index 5ac53c9..b8e6afa 100644 --- a/app/JustHostMC.App/ViewModels/SettingsViewModel.cs +++ b/app/JustHostMC.App/ViewModels/SettingsViewModel.cs @@ -274,6 +274,8 @@ private async Task PurgeNow() { [RelayCommand] private async Task RemoveAllData() { + using var backgroundTask = + App.Current.BackgroundTasks.Begin("data-remove"); RunOnUI(() => { IsBusy = true; SetWorkflowStatus(SettingsWorkflowStatus.RemovingData); @@ -294,6 +296,8 @@ await daemon.Servers.RemoveAllDataAsync( [RelayCommand] private async Task RemoveIncompleteInstallations() { + using var backgroundTask = + App.Current.BackgroundTasks.Begin("incomplete-install-remove"); RunOnUI(() => { IsBusy = true; SetWorkflowStatus( From c91175a82741b62a3e00354460b360fed3b3d61d Mon Sep 17 00:00:00 2001 From: 000hen Date: Mon, 20 Jul 2026 21:10:14 +0800 Subject: [PATCH 28/30] refactor: use context for modpack browse mode - Make SelectedShopIsModpack derive from Context.Kind rather than merged shop capabilities - Read IsModpack in ShopDetailViewModel from SelectedShopIsModpack - Add ShopBrowsingContractTests to assert browse mode comes from context --- .../ShopBrowsingContractTests.cs | 25 +++++++++++++++++++ .../ViewModels/ShopDetailViewModel.cs | 2 +- .../ViewModels/ShopViewModel.cs | 9 +++---- 3 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 app/JustHostMC.App.ResourceTests/ShopBrowsingContractTests.cs diff --git a/app/JustHostMC.App.ResourceTests/ShopBrowsingContractTests.cs b/app/JustHostMC.App.ResourceTests/ShopBrowsingContractTests.cs new file mode 100644 index 0000000..09ca9c9 --- /dev/null +++ b/app/JustHostMC.App.ResourceTests/ShopBrowsingContractTests.cs @@ -0,0 +1,25 @@ +using Xunit; + +namespace JustHostMC.App.ResourceTests; + +public sealed class ShopBrowsingContractTests { + [Fact] + public void BrowseModeComesFromContextRatherThanMergedSourceCapabilities() { + var shop = + RepositoryLayout.ReadAppFile("ViewModels", "ShopViewModel.cs"); + var detail = RepositoryLayout.ReadAppFile( + "ViewModels", "ShopDetailViewModel.cs"); + + Assert.Contains( + "public bool SelectedShopIsModpack => Context.Kind == ModKind.Modpack;", + shop, StringComparison.Ordinal); + Assert.DoesNotContain( + "SelectedShop?.Kinds.Contains(\"modpack\")", shop, + StringComparison.Ordinal); + Assert.Contains("IsModpack = shop.SelectedShopIsModpack;", detail, + StringComparison.Ordinal); + Assert.DoesNotContain( + "IsModpack = shopInfo?.Kinds.Contains(\"modpack\")", detail, + StringComparison.Ordinal); + } +} diff --git a/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs b/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs index c6b1e74..a2d35c0 100644 --- a/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs +++ b/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs @@ -37,7 +37,7 @@ public ShopDetailViewModel(ShopViewModel shop, ShopProjectItem card, Card = card; var shopInfo = shop.Shops.FirstOrDefault(s => s.Id == _shopId); SourceName = shopInfo?.Name ?? _shopId; - IsModpack = shopInfo?.Kinds.Contains("modpack") ?? false; + IsModpack = shop.SelectedShopIsModpack; } /// True when this project is a modpack: its versions create whole diff --git a/app/JustHostMC.App/ViewModels/ShopViewModel.cs b/app/JustHostMC.App/ViewModels/ShopViewModel.cs index 4f15c4f..29aa354 100644 --- a/app/JustHostMC.App/ViewModels/ShopViewModel.cs +++ b/app/JustHostMC.App/ViewModels/ShopViewModel.cs @@ -174,11 +174,10 @@ public void ResetCategoryFilters() { foreach (var category in CategoryFilters) category.IsSelected = false; } - /// True when the selected shop serves modpacks (installed as - /// whole servers), which switches the browse kind and hides the compat - /// filters. - public bool SelectedShopIsModpack => - SelectedShop?.Kinds.Contains("modpack") ?? false; + /// True when this window is browsing modpacks (installed as whole + /// servers). The context is authoritative because a merged source can also + /// advertise mod and plugin capabilities. + public bool SelectedShopIsModpack => Context.Kind == ModKind.Modpack; /// Version/loader filter toggles only make sense for mod/plugin /// shops. From 3ecf9d011aab0236aaeb54d2bd9ad1d163f15a23 Mon Sep 17 00:00:00 2001 From: 000hen Date: Mon, 20 Jul 2026 22:36:05 +0800 Subject: [PATCH 29/30] fix: set project author when missing --- app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs b/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs index a2d35c0..2f86c8c 100644 --- a/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs +++ b/app/JustHostMC.App/ViewModels/ShopDetailViewModel.cs @@ -190,8 +190,11 @@ private async Task LoadDetailAsync(bool darkTheme) { var compatMessage = ProjectCompatText(projectCompat); await RunOnUIAsync(() => { if (detail.Project is not null && - detail.Project.Title.Length > 0) + detail.Project.Title.Length > 0) { + if (string.IsNullOrWhiteSpace(detail.Project.Author)) + detail.Project.Author = Card.Author; Card = new ShopProjectItem(detail.Project); + } WebsiteUrl = detail.Links?.Website ?? ""; Gallery.Clear(); foreach (var image in detail.Gallery) From d7eb0b5cd713d60e75d2a1e3abe47af623816d58 Mon Sep 17 00:00:00 2001 From: 000hen Date: Mon, 20 Jul 2026 22:53:22 +0800 Subject: [PATCH 30/30] chore: wrap lines in ShopBrowsingContractTests --- .../ShopBrowsingContractTests.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/JustHostMC.App.ResourceTests/ShopBrowsingContractTests.cs b/app/JustHostMC.App.ResourceTests/ShopBrowsingContractTests.cs index 09ca9c9..8d7a58f 100644 --- a/app/JustHostMC.App.ResourceTests/ShopBrowsingContractTests.cs +++ b/app/JustHostMC.App.ResourceTests/ShopBrowsingContractTests.cs @@ -7,15 +7,14 @@ public sealed class ShopBrowsingContractTests { public void BrowseModeComesFromContextRatherThanMergedSourceCapabilities() { var shop = RepositoryLayout.ReadAppFile("ViewModels", "ShopViewModel.cs"); - var detail = RepositoryLayout.ReadAppFile( - "ViewModels", "ShopDetailViewModel.cs"); + var detail = RepositoryLayout.ReadAppFile("ViewModels", + "ShopDetailViewModel.cs"); Assert.Contains( "public bool SelectedShopIsModpack => Context.Kind == ModKind.Modpack;", shop, StringComparison.Ordinal); - Assert.DoesNotContain( - "SelectedShop?.Kinds.Contains(\"modpack\")", shop, - StringComparison.Ordinal); + Assert.DoesNotContain("SelectedShop?.Kinds.Contains(\"modpack\")", shop, + StringComparison.Ordinal); Assert.Contains("IsModpack = shop.SelectedShopIsModpack;", detail, StringComparison.Ordinal); Assert.DoesNotContain(