Rework mod content installation.

This commit is contained in:
Paul Chote
2024-10-19 12:32:56 +01:00
committed by Gustas
parent c84d088dfa
commit b57be1cc08
61 changed files with 744 additions and 538 deletions

View File

@@ -0,0 +1,64 @@
#region Copyright & License Information
/*
* Copyright (c) The OpenRA Developers and Contributors
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
namespace OpenRA.Mods.Common.FileSystem
{
public class ContentInstallerFileSystemLoader : IFileSystemLoader, IFileSystemExternalContent
{
[FieldLoader.Require]
public readonly string ContentInstallerMod = null;
[FieldLoader.Require]
public readonly Dictionary<string, string> Packages = null;
public readonly Dictionary<string, string> ContentPackages = null;
public readonly Dictionary<string, string> ContentFiles = null;
bool contentAvailable = true;
public void Mount(OpenRA.FileSystem.FileSystem fileSystem, ObjectCreator objectCreator)
{
foreach (var kv in Packages)
fileSystem.Mount(kv.Key, kv.Value);
if (ContentPackages != null)
{
foreach (var kv in ContentPackages)
{
try
{
fileSystem.Mount(kv.Key, kv.Value);
}
catch
{
contentAvailable = false;
}
}
}
if (ContentFiles != null)
foreach (var kv in ContentFiles)
if (!fileSystem.Exists(kv.Key))
contentAvailable = false;
}
bool IFileSystemExternalContent.InstallContentIfRequired(ModData modData)
{
if (!contentAvailable)
Game.InitializeMod(ContentInstallerMod, new Arguments());
return !contentAvailable;
}
}
}

View File

@@ -10,8 +10,6 @@
#endregion
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.FileSystem
@@ -22,7 +20,7 @@ namespace OpenRA.Mods.Common.FileSystem
public bool InstallContentIfRequired(ModData modData);
}
public class DefaultFileSystemLoader : IFileSystemLoader, IFileSystemExternalContent
public class DefaultFileSystemLoader : IFileSystemLoader
{
public readonly Dictionary<string, string> Packages = null;
@@ -32,29 +30,5 @@ namespace OpenRA.Mods.Common.FileSystem
foreach (var kv in Packages)
fileSystem.Mount(kv.Key, kv.Value);
}
bool IFileSystemExternalContent.InstallContentIfRequired(ModData modData)
{
// If a ModContent section is defined then we need to make sure that the
// required content is installed or switch to the defined content installer.
if (!modData.Manifest.Contains<ModContent>())
return false;
var content = modData.Manifest.Get<ModContent>();
var contentInstalled = content.Packages
.Where(p => p.Value.Required)
.All(p => p.Value.TestFiles.All(f => File.Exists(Platform.ResolvePath(f))));
if (contentInstalled)
return false;
string translationPath;
using (var fs = (FileStream)modData.DefaultFileSystem.Open(content.Translation))
translationPath = fs.Name;
Game.InitializeMod(
content.ContentInstallerMod,
new Arguments(new[] { "Content.Mod=" + modData.Manifest.Id, "Content.TranslationFile=" + translationPath }));
return true;
}
}
}

View File

@@ -44,7 +44,7 @@ namespace OpenRA.Mods.Common.Lint
var mapTranslations = FieldLoader.GetValue<string[]>("value", map.TranslationDefinitions.Value);
var allModTranslations = modData.Manifest.Translations.Append(modData.Manifest.Get<ModContent>().Translation).ToArray();
var allModTranslations = modData.Manifest.Translations;
foreach (var language in GetModLanguages(allModTranslations))
{
// Check keys and variables are not missing across all language files.
@@ -80,7 +80,7 @@ namespace OpenRA.Mods.Common.Lint
foreach (var context in usedKeys.EmptyKeyContexts)
emitWarning($"Empty key in mod translation files required by {context}");
var allModTranslations = modData.Manifest.Translations.Append(modData.Manifest.Get<ModContent>().Translation).ToArray();
var allModTranslations = modData.Manifest.Translations.ToArray();
foreach (var language in GetModLanguages(allModTranslations))
{
Console.WriteLine($"Testing language: {language}");

View File

@@ -30,8 +30,7 @@ namespace OpenRA.Mods.Common.Lint
void ILintPass.Run(Action<string> emitError, Action<string> emitWarning, ModData modData)
{
var allModTranslations = modData.Manifest.Translations.Append(modData.Manifest.Get<ModContent>().Translation);
Run(emitError, emitWarning, modData.DefaultFileSystem, allModTranslations);
Run(emitError, emitWarning, modData.DefaultFileSystem, modData.Manifest.Translations);
}
static void Run(Action<string> emitError, Action<string> emitWarning, IReadOnlyFileSystem fileSystem, IEnumerable<string> paths)

View File

@@ -9,9 +9,6 @@
*/
#endregion
using System;
using System.IO;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Primitives;
@@ -48,49 +45,7 @@ namespace OpenRA.Mods.Common.LoadScreens
public override void StartGame(Arguments args)
{
var modId = args.GetValue("Content.Mod", null);
if (modId == null || !Game.Mods.TryGetValue(modId, out var selectedMod))
throw new InvalidOperationException("Invalid or missing Content.Mod argument.");
var translationFilePath = args.GetValue("Content.TranslationFile", null);
if (translationFilePath == null || !File.Exists(translationFilePath))
throw new InvalidOperationException("Invalid or missing Content.TranslationFile argument.");
var content = selectedMod.Get<ModContent>(Game.ModData.ObjectCreator);
Ui.LoadWidget("MODCONTENT_BACKGROUND", Ui.Root, new WidgetArgs());
if (!IsModInstalled(content))
{
var widgetArgs = new WidgetArgs
{
{ "continueLoading", () => Game.RunAfterTick(() => Game.InitializeMod(modId, new Arguments())) },
{ "mod", selectedMod },
{ "content", content },
{ "translationFilePath", translationFilePath },
};
Ui.OpenWindow("CONTENT_PROMPT_PANEL", widgetArgs);
}
else
{
var widgetArgs = new WidgetArgs
{
{ "onCancel", () => Game.RunAfterTick(() => Game.InitializeMod(modId, new Arguments())) },
{ "mod", selectedMod },
{ "content", content },
{ "translationFilePath", translationFilePath },
};
Ui.OpenWindow("CONTENT_PANEL", widgetArgs);
}
}
static bool IsModInstalled(ModContent content)
{
return content.Packages
.Where(p => p.Value.Required)
.All(p => p.Value.TestFiles.All(f => File.Exists(Platform.ResolvePath(f))));
}
public override bool BeforeLoad()

View File

@@ -15,7 +15,7 @@ using System.Collections.Immutable;
using System.IO;
using System.Linq;
namespace OpenRA
namespace OpenRA.Mods.Common
{
public class ModContent : IGlobalModData
{
@@ -42,8 +42,6 @@ namespace OpenRA
public class ModSource
{
public readonly ObjectCreator ObjectCreator;
[FieldLoader.Ignore]
public readonly MiniYaml Type;
@@ -62,9 +60,8 @@ namespace OpenRA
public readonly string TooltipText;
public ModSource(MiniYaml yaml, ObjectCreator objectCreator)
public ModSource(MiniYaml yaml)
{
ObjectCreator = objectCreator;
Title = yaml.Value;
var type = yaml.NodeWithKeyOrDefault("Type");
@@ -85,7 +82,6 @@ namespace OpenRA
public class ModDownload
{
public readonly ObjectCreator ObjectCreator;
public readonly string Title;
public readonly string URL;
public readonly string MirrorList;
@@ -93,21 +89,17 @@ namespace OpenRA
public readonly string Type;
public readonly Dictionary<string, string> Extract;
public ModDownload(MiniYaml yaml, ObjectCreator objectCreator)
public ModDownload(MiniYaml yaml)
{
ObjectCreator = objectCreator;
Title = yaml.Value;
FieldLoader.Load(this, yaml);
}
}
[FluentReference]
public readonly string InstallPromptMessage;
public readonly string QuickDownload;
[FluentReference]
public readonly string HeaderMessage;
public readonly string ContentInstallerMod = "modcontent";
public readonly string Translation;
[FieldLoader.Require]
public readonly string Mod;
[FieldLoader.LoadUsing(nameof(LoadPackages))]
public readonly Dictionary<string, ModPackage> Packages = new();

View File

@@ -235,7 +235,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
using (var stream = File.OpenRead(file))
{
var packageLoader = download.ObjectCreator.CreateObject<IPackageLoader>($"{download.Type}Loader");
var packageLoader = modData.ObjectCreator.CreateObject<IPackageLoader>($"{download.Type}Loader");
if (packageLoader.TryParsePackage(stream, file, modData.ModFiles, out var package))
{

View File

@@ -87,7 +87,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
readonly ModData modData;
readonly ModContent content;
readonly Dictionary<string, ModContent.ModSource> sources;
readonly FluentBundle externalFluentBundle;
readonly Widget panel;
readonly LabelWidget titleLabel;
@@ -118,12 +117,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
[ObjectCreator.UseCtor]
public InstallFromSourceLogic(
Widget widget, ModData modData, ModContent content, Dictionary<string, ModContent.ModSource> sources, FluentBundle externalFluentBundle)
Widget widget, ModData modData, ModContent content, Dictionary<string, ModContent.ModSource> sources)
{
this.modData = modData;
this.content = content;
this.sources = sources;
this.externalFluentBundle = externalFluentBundle;
Log.AddChannel("install", "install.log");
@@ -173,7 +171,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
message = FluentProvider.GetString(SearchingSourceFor, "title", kv.Value.Title);
var sourceResolver = kv.Value.ObjectCreator.CreateObject<ISourceResolver>($"{kv.Value.Type.Value}SourceResolver");
var sourceResolver = modData.ObjectCreator.CreateObject<ISourceResolver>($"{kv.Value.Type.Value}SourceResolver");
var path = sourceResolver.FindSourcePath(kv.Value);
if (path != null)
@@ -210,7 +208,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
foreach (var source in missingSources)
{
var sourceResolver = source.ObjectCreator.CreateObject<ISourceResolver>($"{source.Type.Value}SourceResolver");
var sourceResolver = modData.ObjectCreator.CreateObject<ISourceResolver>($"{source.Type.Value}SourceResolver");
var availability = sourceResolver.GetAvailability();
@@ -260,7 +258,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var split = key.IndexOf('@');
if (split != -1)
key = key[..split];
var sourceAction = modSource.ObjectCreator.CreateObject<ISourceAction>($"{key}SourceAction");
var sourceAction = modData.ObjectCreator.CreateObject<ISourceAction>($"{key}SourceAction");
sourceAction.RunActionOnSource(sourceActionNode.Value, path, modData, extracted, m => message = m);
}
}
@@ -342,7 +340,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
var containerWidget = (ContainerWidget)checkboxListTemplate.Clone();
var checkboxWidget = containerWidget.Get<CheckboxWidget>("PACKAGE_CHECKBOX");
var title = externalFluentBundle.GetString(package.Title);
var title = FluentProvider.GetString(package.Title);
checkboxWidget.GetText = () => title;
checkboxWidget.IsDisabled = () => package.Required;
checkboxWidget.IsChecked = () => selectedPackages[package.Identifier];

View File

@@ -13,13 +13,47 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.FileSystem;
using OpenRA.Widgets;
using FS = OpenRA.FileSystem.FileSystem;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class ModContentLogic : ChromeLogic
{
[ObjectCreator.UseCtor]
public ModContentLogic(ModData modData)
{
var content = modData.Manifest.Get<ModContent>();
if (!IsModInstalled(content))
{
var widgetArgs = new WidgetArgs
{
{ "continueLoading", () => Game.RunAfterTick(() => Game.InitializeMod(content.Mod, new Arguments())) },
{ "content", content },
};
Ui.OpenWindow("CONTENT_PROMPT_PANEL", widgetArgs);
}
else
{
var widgetArgs = new WidgetArgs
{
{ "onCancel", () => Game.RunAfterTick(() => Game.InitializeMod(content.Mod, new Arguments())) },
{ "content", content },
};
Ui.OpenWindow("CONTENT_PANEL", widgetArgs);
}
}
static bool IsModInstalled(ModContent content)
{
return content.Packages
.Where(p => p.Value.Required)
.All(p => p.Value.TestFiles.All(f => File.Exists(Platform.ResolvePath(f))));
}
}
public class ModContentInstallerLogic : ChromeLogic
{
[FluentReference]
const string ManualInstall = "button-manual-install";
@@ -31,56 +65,28 @@ namespace OpenRA.Mods.Common.Widgets.Logic
readonly Dictionary<string, ModContent.ModSource> sources = new();
readonly Dictionary<string, ModContent.ModDownload> downloads = new();
readonly FluentBundle externalFluentBundle;
bool sourceAvailable;
[ObjectCreator.UseCtor]
public ModContentLogic(Widget widget, Manifest mod, ModContent content, Action onCancel, string translationFilePath)
public ModContentInstallerLogic(ModData modData, Widget widget, ModContent content, Action onCancel)
{
this.content = content;
var panel = widget.Get("CONTENT_PANEL");
var modObjectCreator = new ObjectCreator(mod, Game.Mods);
var modPackageLoaders = modObjectCreator.GetLoaders<IPackageLoader>(mod.PackageFormats, "package");
var modFileSystem = new FS(mod.Id, Game.Mods, modPackageLoaders);
var modFileSystemLoader = modObjectCreator.GetLoader<IFileSystemLoader>(mod.FileSystem.Value, "filesystem");
FieldLoader.Load(modFileSystemLoader, mod.FileSystem);
modFileSystemLoader.Mount(modFileSystem, modObjectCreator);
modFileSystem.TrimExcess();
var sourceYaml = MiniYaml.Load(modFileSystem, content.Sources, null);
var sourceYaml = MiniYaml.Load(modData.DefaultFileSystem, content.Sources, null);
foreach (var s in sourceYaml)
sources.Add(s.Key, new ModContent.ModSource(s.Value, modObjectCreator));
sources.Add(s.Key, new ModContent.ModSource(s.Value));
var downloadYaml = MiniYaml.Load(modFileSystem, content.Downloads, null);
var downloadYaml = MiniYaml.Load(modData.DefaultFileSystem, content.Downloads, null);
foreach (var d in downloadYaml)
downloads.Add(d.Key, new ModContent.ModDownload(d.Value, modObjectCreator));
modFileSystem.UnmountAll();
externalFluentBundle = new FluentBundle(Game.Settings.Player.Language, File.ReadAllText(translationFilePath), _ => { });
downloads.Add(d.Key, new ModContent.ModDownload(d.Value));
scrollPanel = panel.Get<ScrollPanelWidget>("PACKAGES");
template = scrollPanel.Get<ContainerWidget>("PACKAGE_TEMPLATE");
var headerTemplate = panel.Get<LabelWidget>("HEADER_TEMPLATE");
var headerLines =
!string.IsNullOrEmpty(content.HeaderMessage)
? externalFluentBundle.GetString(content.HeaderMessage)
: null;
var headerHeight = 0;
if (headerLines != null)
{
var label = (LabelWidget)headerTemplate.Clone();
label.GetText = () => headerLines;
label.IncreaseHeightToFitCurrentText();
panel.AddChild(label);
headerHeight += label.Bounds.Height;
}
var headerLabel = panel.Get<LabelWidget>("HEADER_LABEL");
headerLabel.IncreaseHeightToFitCurrentText();
var headerHeight = headerLabel.Bounds.Height;
panel.Bounds.Height += headerHeight;
panel.Bounds.Y -= headerHeight / 2;
@@ -94,7 +100,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
{ "sources", sources },
{ "content", content },
{ "externalFluentBundle", externalFluentBundle },
});
var backButton = panel.Get<ButtonWidget>("BACK_BUTTON");
@@ -118,7 +123,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
var container = template.Clone();
var titleWidget = container.Get<LabelWidget>("TITLE");
var title = externalFluentBundle.GetString(p.Value.Title);
var title = FluentProvider.GetString(p.Value.Title);
titleWidget.GetText = () => title;
var requiredWidget = container.Get<LabelWidget>("REQUIRED");

View File

@@ -12,9 +12,7 @@
using System;
using System.IO;
using System.Linq;
using OpenRA.FileSystem;
using OpenRA.Widgets;
using FS = OpenRA.FileSystem.FileSystem;
namespace OpenRA.Mods.Common.Widgets.Logic
{
@@ -27,36 +25,21 @@ namespace OpenRA.Mods.Common.Widgets.Logic
const string Quit = "button-quit";
readonly ModContent content;
readonly FluentBundle externalFluentBundle;
bool requiredContentInstalled;
[ObjectCreator.UseCtor]
public ModContentPromptLogic(ModData modData, Widget widget, Manifest mod, ModContent content, Action continueLoading, string translationFilePath)
public ModContentPromptLogic(ModData modData, Widget widget, ModContent content, Action continueLoading)
{
this.content = content;
CheckRequiredContentInstalled();
externalFluentBundle = new FluentBundle(Game.Settings.Player.Language, File.ReadAllText(translationFilePath), _ => { });
var continueMessage = FluentProvider.GetString(Continue);
var quitMessage = FluentProvider.GetString(Quit);
var panel = widget.Get("CONTENT_PROMPT_PANEL");
var headerTemplate = panel.Get<LabelWidget>("HEADER_TEMPLATE");
var headerLines =
!string.IsNullOrEmpty(content.InstallPromptMessage)
? externalFluentBundle.GetString(content.InstallPromptMessage)
: null;
var headerHeight = 0;
if (headerLines != null)
{
var label = (LabelWidget)headerTemplate.Clone();
label.GetText = () => headerLines;
label.IncreaseHeightToFitCurrentText();
panel.AddChild(label);
headerHeight += label.Bounds.Height;
}
var headerLabel = panel.Get<LabelWidget>("HEADER_LABEL");
headerLabel.IncreaseHeightToFitCurrentText();
var headerHeight = headerLabel.Bounds.Height;
panel.Bounds.Height += headerHeight;
panel.Bounds.Y -= headerHeight / 2;
@@ -68,9 +51,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Ui.OpenWindow("CONTENT_PANEL", new WidgetArgs
{
{ "onCancel", CheckRequiredContentInstalled },
{ "mod", mod },
{ "content", content },
{ "translationFilePath", translationFilePath },
});
};
@@ -79,25 +60,14 @@ namespace OpenRA.Mods.Common.Widgets.Logic
quickButton.Bounds.Y += headerHeight;
quickButton.OnClick = () =>
{
var modObjectCreator = new ObjectCreator(mod, Game.Mods);
var modPackageLoaders = modObjectCreator.GetLoaders<IPackageLoader>(mod.PackageFormats, "package");
var modFileSystem = new FS(mod.Id, Game.Mods, modPackageLoaders);
var modFileSystemLoader = modObjectCreator.GetLoader<IFileSystemLoader>(mod.FileSystem.Value, "filesystem");
FieldLoader.Load(modFileSystemLoader, mod.FileSystem);
modFileSystemLoader.Mount(modFileSystem, modObjectCreator);
modFileSystem.TrimExcess();
var downloadYaml = MiniYaml.Load(modFileSystem, content.Downloads, null);
modFileSystem.UnmountAll();
var downloadYaml = MiniYaml.Load(modData.DefaultFileSystem, content.Downloads, null);
var download = downloadYaml.FirstOrDefault(n => n.Key == content.QuickDownload);
if (download == null)
throw new InvalidOperationException($"Mod QuickDownload `{content.QuickDownload}` definition not found.");
Ui.OpenWindow("PACKAGE_DOWNLOAD_PANEL", new WidgetArgs
{
{ "download", new ModContent.ModDownload(download.Value, modObjectCreator) },
{ "download", new ModContent.ModDownload(download.Value) },
{ "onSuccess", continueLoading }
});
};

View File

@@ -15,6 +15,7 @@ using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using OpenRA.Mods.Common.FileSystem;
using OpenRA.Network;
using OpenRA.Support;
using OpenRA.Widgets;
@@ -81,24 +82,16 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var contentButton = mainMenu.GetOrNull<ButtonWidget>("CONTENT_BUTTON");
if (contentButton != null)
{
var hasContent = modData.Manifest.Contains<ModContent>();
contentButton.Disabled = !hasContent;
var contentInstaller = modData.FileSystemLoader as ContentInstallerFileSystemLoader;
contentButton.Disabled = contentInstaller == null;
contentButton.OnClick = () =>
{
// Switching mods changes the world state (by disposing it),
// so we can't do this inside the input handler.
Game.RunAfterTick(() =>
{
if (!hasContent)
return;
var content = modData.Manifest.Get<ModContent>();
string translationPath;
using (var fs = (FileStream)modData.DefaultFileSystem.Open(content.Translation))
translationPath = fs.Name;
Game.InitializeMod(
content.ContentInstallerMod,
new Arguments(new[] { "Content.Mod=" + modData.Manifest.Id, "Content.TranslationFile=" + translationPath }));
if (contentInstaller != null)
Game.InitializeMod(contentInstaller.ContentInstallerMod, new Arguments());
});
};
}