Replaced hardcoded SourceType with custom defined ISourceResolver.

This commit is contained in:
IceReaper
2022-11-06 19:33:01 +01:00
committed by Matthias Mailänder
parent 7188f88ba1
commit 35eb246080
34 changed files with 313 additions and 117 deletions

View File

@@ -0,0 +1,20 @@
#region Copyright & License Information
/*
* Copyright 2007-2022 The OpenRA Developers (see AUTHORS)
* 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
namespace OpenRA.Mods.Common.Installer
{
public enum Availability
{
Unavailable,
GameSource,
DigitalInstall
}
}

View File

@@ -0,0 +1,19 @@
#region Copyright & License Information
/*
* Copyright 2007-2022 The OpenRA Developers (see AUTHORS)
* 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
namespace OpenRA.Mods.Common.Installer
{
public interface ISourceResolver
{
string FindSourcePath(ModContent.ModSource modSource);
Availability GetAvailability();
}
}

View File

@@ -43,10 +43,10 @@ namespace OpenRA.Mods.Common.Installer
var length = source.Length;
Action<long> onProgress = null;
if (length < InstallFromDiscLogic.ShowPercentageThreshold)
updateMessage(modData.Translation.GetString(InstallFromDiscLogic.CopyingFilename, Translation.Arguments("filename", displayFilename)));
if (length < InstallFromSourceLogic.ShowPercentageThreshold)
updateMessage(modData.Translation.GetString(InstallFromSourceLogic.CopyingFilename, Translation.Arguments("filename", displayFilename)));
else
onProgress = b => updateMessage(modData.Translation.GetString(InstallFromDiscLogic.CopyingFilenameProgress, Translation.Arguments("filename", displayFilename, "progress", 100 * b / length)));
onProgress = b => updateMessage(modData.Translation.GetString(InstallFromSourceLogic.CopyingFilenameProgress, Translation.Arguments("filename", displayFilename, "progress", 100 * b / length)));
InstallerUtils.CopyStream(source, target, length, onProgress);
}

View File

@@ -60,10 +60,10 @@ namespace OpenRA.Mods.Common.Installer
var displayFilename = Path.GetFileName(Path.GetFileName(targetPath));
Action<long> onProgress = null;
if (length < InstallFromDiscLogic.ShowPercentageThreshold)
updateMessage(modData.Translation.GetString(InstallFromDiscLogic.Extracing, Translation.Arguments("filename", displayFilename)));
if (length < InstallFromSourceLogic.ShowPercentageThreshold)
updateMessage(modData.Translation.GetString(InstallFromSourceLogic.Extracing, Translation.Arguments("filename", displayFilename)));
else
onProgress = b => updateMessage(modData.Translation.GetString(InstallFromDiscLogic.ExtracingProgress, Translation.Arguments("filename", displayFilename, "progress", 100 * b / length)));
onProgress = b => updateMessage(modData.Translation.GetString(InstallFromSourceLogic.ExtracingProgress, Translation.Arguments("filename", displayFilename, "progress", 100 * b / length)));
using (var target = File.OpenWrite(targetPath))
{

View File

@@ -63,7 +63,7 @@ namespace OpenRA.Mods.Common.Installer
{
Log.Write("install", $"Extracting {sourcePath} -> {targetPath}");
var displayFilename = Path.GetFileName(Path.GetFileName(targetPath));
Action<int> onProgress = percent => updateMessage(modData.Translation.GetString(InstallFromDiscLogic.ExtracingProgress, Translation.Arguments("filename", displayFilename, "progress", percent)));
Action<int> onProgress = percent => updateMessage(modData.Translation.GetString(InstallFromSourceLogic.ExtracingProgress, Translation.Arguments("filename", displayFilename, "progress", percent)));
reader.ExtractFile(node.Value.Value, target, onProgress);
}
}

View File

@@ -44,7 +44,7 @@ namespace OpenRA.Mods.Common.Installer
{
Log.Write("install", $"Extracting {sourcePath} -> {targetPath}");
var displayFilename = Path.GetFileName(Path.GetFileName(targetPath));
Action<int> onProgress = percent => updateMessage(modData.Translation.GetString(InstallFromDiscLogic.ExtracingProgress, Translation.Arguments("filename", displayFilename, "progress", percent)));
Action<int> onProgress = percent => updateMessage(modData.Translation.GetString(InstallFromSourceLogic.ExtracingProgress, Translation.Arguments("filename", displayFilename, "progress", percent)));
reader.ExtractFile(node.Value.Value, target, onProgress);
}
}

View File

@@ -59,10 +59,10 @@ namespace OpenRA.Mods.Common.Installer
var displayFilename = Path.GetFileName(Path.GetFileName(targetPath));
Action<long> onProgress = null;
if (length < InstallFromDiscLogic.ShowPercentageThreshold)
updateMessage(modData.Translation.GetString(InstallFromDiscLogic.Extracing, Translation.Arguments("filename", displayFilename)));
if (length < InstallFromSourceLogic.ShowPercentageThreshold)
updateMessage(modData.Translation.GetString(InstallFromSourceLogic.Extracing, Translation.Arguments("filename", displayFilename)));
else
onProgress = b => updateMessage(modData.Translation.GetString(InstallFromDiscLogic.ExtracingProgress, Translation.Arguments("filename", displayFilename, "progress", 100 * b / length)));
onProgress = b => updateMessage(modData.Translation.GetString(InstallFromSourceLogic.ExtracingProgress, Translation.Arguments("filename", displayFilename, "progress", 100 * b / length)));
using (var target = File.OpenWrite(targetPath))
{

View File

@@ -0,0 +1,60 @@
#region Copyright & License Information
/*
* Copyright 2007-2022 The OpenRA Developers (see AUTHORS)
* 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;
using System.IO;
using System.Linq;
namespace OpenRA.Mods.Common.Installer
{
public class DiscSourceResolver : ISourceResolver
{
public string FindSourcePath(ModContent.ModSource source)
{
var volumes = DriveInfo.GetDrives()
.Where(d =>
{
if (d.DriveType == DriveType.CDRom && d.IsReady)
return true;
// HACK: the "TFD" DVD is detected as a fixed udf-formatted drive on OSX
if (d.DriveType == DriveType.Fixed && d.DriveFormat == "udf")
return true;
return false;
})
.Select(v => v.RootDirectory.FullName);
if (Platform.CurrentPlatform == PlatformType.Linux)
{
// Outside of Gnome, most mounting tools on Linux don't set DriveType.CDRom
// so provide a fallback by allowing users to manually mount images on known paths
volumes = volumes.Concat(new[]
{
"/media/openra",
"/media/" + Environment.UserName + "/openra",
"/mnt/openra"
});
}
foreach (var volume in volumes)
if (InstallerUtils.IsValidSourcePath(volume, source))
return volume;
return null;
}
public Availability GetAvailability()
{
return Availability.GameSource;
}
}
}

View File

@@ -0,0 +1,50 @@
#region Copyright & License Information
/*
* Copyright 2007-2022 The OpenRA Developers (see AUTHORS)
* 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.IO;
using System.Runtime.InteropServices;
namespace OpenRA.Mods.Common.Installer
{
public class RegistryDirectoryFromFileSourceResolver : ISourceResolver
{
public string FindSourcePath(ModContent.ModSource source)
{
if (source.RegistryKey == null)
return null;
if (Platform.CurrentPlatform != PlatformType.Windows)
return null;
// We need an extra check for the platform here to silence a warning when the registry is accessed
// TODO: Remove this once our platform checks use the same method
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return null;
foreach (var prefix in source.RegistryPrefixes)
{
if (!(Microsoft.Win32.Registry.GetValue(prefix + source.RegistryKey, source.RegistryValue, null) is string path))
continue;
path = Path.GetDirectoryName(path);
return InstallerUtils.IsValidSourcePath(path, source) ? path : null;
}
return null;
}
public Availability GetAvailability()
{
return Platform.CurrentPlatform != PlatformType.Windows ? Availability.DigitalInstall : Availability.Unavailable;
}
}
}

View File

@@ -0,0 +1,47 @@
#region Copyright & License Information
/*
* Copyright 2007-2022 The OpenRA Developers (see AUTHORS)
* 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.Runtime.InteropServices;
namespace OpenRA.Mods.Common.Installer
{
public class RegistryDirectorySourceResolver : ISourceResolver
{
public string FindSourcePath(ModContent.ModSource source)
{
if (source.RegistryKey == null)
return null;
if (Platform.CurrentPlatform != PlatformType.Windows)
return null;
// We need an extra check for the platform here to silence a warning when the registry is accessed
// TODO: Remove this once our platform checks use the same method
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return null;
foreach (var prefix in source.RegistryPrefixes)
{
if (!(Microsoft.Win32.Registry.GetValue(prefix + source.RegistryKey, source.RegistryValue, null) is string path))
continue;
return InstallerUtils.IsValidSourcePath(path, source) ? path : null;
}
return null;
}
public Availability GetAvailability()
{
return Platform.CurrentPlatform != PlatformType.Windows ? Availability.DigitalInstall : Availability.Unavailable;
}
}
}

View File

@@ -18,7 +18,6 @@ namespace OpenRA
{
public class ModContent : IGlobalModData
{
public enum SourceType { Disc, RegistryDirectory, RegistryDirectoryFromFile }
public class ModPackage
{
public readonly string Title;
@@ -42,7 +41,9 @@ namespace OpenRA
public class ModSource
{
public readonly ObjectCreator ObjectCreator;
public readonly SourceType Type = SourceType.Disc;
[FieldLoader.Ignore]
public readonly MiniYaml Type;
// Used to find installation locations for SourceType.Install
public readonly string[] RegistryPrefixes = { string.Empty };
@@ -62,6 +63,10 @@ namespace OpenRA
ObjectCreator = objectCreator;
Title = yaml.Value;
var type = yaml.Nodes.FirstOrDefault(n => n.Key == "Type");
if (type != null)
Type = type.Value;
var idFiles = yaml.Nodes.FirstOrDefault(n => n.Key == "IDFiles");
if (idFiles != null)
IDFiles = idFiles.Value;

View File

@@ -19,7 +19,7 @@ using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class InstallFromDiscLogic : ChromeLogic
public class InstallFromSourceLogic : ChromeLogic
{
// Hide percentage indicators for files smaller than 25 MB
public const int ShowPercentageThreshold = 26214400;
@@ -54,19 +54,19 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Mode visible = Mode.Progress;
[TranslationReference]
static readonly string DetectingDrives = "detecting-drives";
static readonly string DetectingSources = "detecting-sources";
[TranslationReference]
static readonly string CheckingDiscs = "checking-discs";
static readonly string CheckingSources = "checking-sources";
[TranslationReference("title")]
static readonly string SearchingDiscFor = "searching-disc-for";
static readonly string SearchingSourceFor = "searching-source-for";
[TranslationReference]
static readonly string ContentPackageInstallation = "content-package-installation";
[TranslationReference]
static readonly string GameDiscs = "game-discs";
static readonly string GameSources = "game-sources";
[TranslationReference]
static readonly string DigitalInstalls = "digital-installs";
@@ -111,7 +111,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
static readonly string Back = "back";
[ObjectCreator.UseCtor]
public InstallFromDiscLogic(Widget widget, ModData modData, ModContent content, Dictionary<string, ModContent.ModSource> sources)
public InstallFromSourceLogic(Widget widget, ModData modData, ModContent content, Dictionary<string, ModContent.ModSource> sources)
{
this.modData = modData;
this.content = content;
@@ -119,7 +119,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Log.AddChannel("install", "install.log");
panel = widget.Get("DISC_INSTALL_PANEL");
panel = widget.Get("SOURCE_INSTALL_PANEL");
titleLabel = panel.Get<LabelWidget>("TITLE");
@@ -149,65 +149,39 @@ namespace OpenRA.Mods.Common.Widgets.Logic
listLabel = listContainer.Get<LabelWidget>("LIST_MESSAGE");
DetectContentDisks();
DetectContentSources();
}
static bool IsValidDrive(DriveInfo d)
void DetectContentSources()
{
if (d.DriveType == DriveType.CDRom && d.IsReady)
return true;
// HACK: the "TFD" DVD is detected as a fixed udf-formatted drive on OSX
if (d.DriveType == DriveType.Fixed && d.DriveFormat == "udf")
return true;
return false;
}
void DetectContentDisks()
{
var message = modData.Translation.GetString(DetectingDrives);
ShowProgressbar(modData.Translation.GetString(CheckingDiscs), () => message);
ShowBackRetry(DetectContentDisks);
var message = modData.Translation.GetString(DetectingSources);
ShowProgressbar(modData.Translation.GetString(CheckingSources), () => message);
ShowBackRetry(DetectContentSources);
new Task(() =>
{
var volumes = DriveInfo.GetDrives()
.Where(IsValidDrive)
.Select(v => v.RootDirectory.FullName);
if (Platform.CurrentPlatform == PlatformType.Linux)
{
// Outside of Gnome, most mounting tools on Linux don't set DriveType.CDRom
// so provide a fallback by allowing users to manually mount images on known paths
volumes = volumes.Concat(new[]
{
"/media/openra",
"/media/" + Environment.UserName + "/openra",
"/mnt/openra"
});
}
foreach (var kv in sources)
{
message = modData.Translation.GetString(SearchingDiscFor, Translation.Arguments("title", kv.Value.Title));
message = modData.Translation.GetString(SearchingSourceFor, Translation.Arguments("title", kv.Value.Title));
var path = FindSourcePath(kv.Value, volumes);
var sourceResolver = kv.Value.ObjectCreator.CreateObject<ISourceResolver>($"{kv.Value.Type.Value}SourceResolver");
var path = sourceResolver.FindSourcePath(kv.Value);
if (path != null)
{
Log.Write("install", $"Using installer `{kv.Key}: {kv.Value.Title}` of type `{kv.Value.Type}`:");
Log.Write("install", $"Using installer `{kv.Key}: {kv.Value.Title}` of type `{kv.Value.Type.Value}`:");
var packages = content.Packages.Values
.Where(p => p.Sources.Contains(kv.Key) && !p.IsInstalled())
.Select(p => p.Title);
// Ignore disc if content is already installed
// Ignore source if content is already installed
if (packages.Any())
{
Game.RunAfterTick(() =>
{
ShowList(kv.Value.Title, modData.Translation.GetString(ContentPackageInstallation), packages);
ShowContinueCancel(() => InstallFromDisc(path, kv.Value));
ShowContinueCancel(() => InstallFromSource(path, kv.Value));
});
return;
@@ -220,35 +194,38 @@ namespace OpenRA.Mods.Common.Widgets.Logic
.SelectMany(p => p.Sources)
.Select(d => sources[d]);
var discs = missingSources
.Where(s => s.Type == ModContent.SourceType.Disc)
.Select(s => s.Title)
.Distinct();
var gameSources = new HashSet<string>();
var digitalInstalls = new HashSet<string>();
var options = new Dictionary<string, IEnumerable<string>>()
foreach (var source in missingSources)
{
{ modData.Translation.GetString(GameDiscs), discs },
};
var sourceResolver = source.ObjectCreator.CreateObject<ISourceResolver>($"{source.Type.Value}SourceResolver");
if (Platform.CurrentPlatform == PlatformType.Windows)
{
var installations = missingSources
.Where(s => s.Type == ModContent.SourceType.RegistryDirectory || s.Type == ModContent.SourceType.RegistryDirectoryFromFile)
.Select(s => s.Title)
.Distinct();
var availability = sourceResolver.GetAvailability();
options.Add(modData.Translation.GetString(DigitalInstalls), installations);
if (availability == Availability.GameSource)
gameSources.Add(source.Title);
else if (availability == Availability.DigitalInstall)
digitalInstalls.Add(source.Title);
}
var options = new Dictionary<string, IEnumerable<string>>();
if (gameSources.Any())
options.Add(modData.Translation.GetString(GameSources), gameSources);
if (digitalInstalls.Any())
options.Add(modData.Translation.GetString(DigitalInstalls), digitalInstalls);
Game.RunAfterTick(() =>
{
ShowList(modData.Translation.GetString(GameContentNotFound), modData.Translation.GetString(AlternativeContentSources), options);
ShowBackRetry(DetectContentDisks);
ShowBackRetry(DetectContentSources);
});
}).Start();
}
void InstallFromDisc(string path, ModContent.ModSource modSource)
void InstallFromSource(string path, ModContent.ModSource modSource)
{
var message = "";
ShowProgressbar(modData.Translation.GetString(InstallingContent), () => message);
@@ -281,7 +258,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Game.RunAfterTick(() =>
{
ShowMessage(modData.Translation.GetString(InstallationFailed), modData.Translation.GetString(CheckInstallLog));
ShowBackRetry(() => InstallFromDisc(path, modSource));
ShowBackRetry(() => InstallFromSource(path, modSource));
});
}
}).Start();

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
readonly Dictionary<string, ModContent.ModSource> sources = new Dictionary<string, ModContent.ModSource>();
readonly Dictionary<string, ModContent.ModDownload> downloads = new Dictionary<string, ModContent.ModDownload>();
bool discAvailable;
bool sourceAvailable;
[TranslationReference]
static readonly string ManualInstall = "manual-install";
@@ -76,11 +76,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
panel.Bounds.Y -= headerHeight / 2;
scrollPanel.Bounds.Y += headerHeight;
var discButton = panel.Get<ButtonWidget>("CHECK_DISC_BUTTON");
discButton.Bounds.Y += headerHeight;
discButton.IsVisible = () => discAvailable;
var sourceButton = panel.Get<ButtonWidget>("CHECK_SOURCE_BUTTON");
sourceButton.Bounds.Y += headerHeight;
sourceButton.IsVisible = () => sourceAvailable;
discButton.OnClick = () => Ui.OpenWindow("DISC_INSTALL_PANEL", new WidgetArgs
sourceButton.OnClick = () => Ui.OpenWindow("SOURCE_INSTALL_PANEL", new WidgetArgs
{
{ "sources", sources },
{ "content", content }
@@ -113,7 +113,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var requiredWidget = container.Get<LabelWidget>("REQUIRED");
requiredWidget.IsVisible = () => p.Value.Required;
var sourceWidget = container.Get<ImageWidget>("DISC");
var sourceWidget = container.Get<ImageWidget>("SOURCE");
var sourceTitles = p.Value.Sources.Select(s => sources[s].Title).Distinct();
var sourceList = sourceTitles.JoinWith("\n");
var isSourceAvailable = sourceTitles.Any();
@@ -139,18 +139,18 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var installedWidget = container.Get<LabelWidget>("INSTALLED");
installedWidget.IsVisible = () => installed;
var requiresDiscWidget = container.Get<LabelWidget>("REQUIRES_DISC");
requiresDiscWidget.IsVisible = () => !installed && !downloadEnabled;
var requiresSourceWidget = container.Get<LabelWidget>("REQUIRES_SOURCE");
requiresSourceWidget.IsVisible = () => !installed && !downloadEnabled;
if (!isSourceAvailable)
{
var manualInstall = modData.Translation.GetString(ManualInstall);
requiresDiscWidget.GetText = () => manualInstall;
requiresSourceWidget.GetText = () => manualInstall;
}
scrollPanel.AddChild(container);
}
discAvailable = content.Packages.Values.Any(p => p.Sources.Length > 0 && !p.IsInstalled());
sourceAvailable = content.Packages.Values.Any(p => p.Sources.Length > 0 && !p.IsInstalled());
}
}
}

View File

@@ -14,38 +14,38 @@ using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class ModContentDiscTooltipLogic : ChromeLogic
public class ModContentSourceTooltipLogic : ChromeLogic
{
[ObjectCreator.UseCtor]
public ModContentDiscTooltipLogic(Widget widget, Func<string> getText)
public ModContentSourceTooltipLogic(Widget widget, Func<string> getText)
{
var discs = widget.Get<ContainerWidget>("DISCS");
var template = discs.Get<LabelWidget>("DISC_TEMPLATE");
discs.RemoveChildren();
var sources = widget.Get<ContainerWidget>("SOURCES");
var template = sources.Get<LabelWidget>("SOURCE_TEMPLATE");
sources.RemoveChildren();
var desc = widget.Get<LabelWidget>("DESCRIPTION");
var font = Game.Renderer.Fonts[template.Font];
var discTitles = getText().Split('\n');
var sourceTitles = getText().Split('\n');
var maxWidth = Game.Renderer.Fonts[desc.Font].Measure(desc.Text).X;
var sideMargin = desc.Bounds.X;
var bottomMargin = discs.Bounds.Height;
foreach (var disc in discTitles)
var bottomMargin = sources.Bounds.Height;
foreach (var source in sourceTitles)
{
var label = (LabelWidget)template.Clone();
var title = disc;
var title = source;
label.GetText = () => title;
label.Bounds.Y = discs.Bounds.Height;
label.Bounds.Width = font.Measure(disc).X;
label.Bounds.Y = sources.Bounds.Height;
label.Bounds.Width = font.Measure(source).X;
maxWidth = Math.Max(maxWidth, label.Bounds.Width + label.Bounds.X);
discs.AddChild(label);
discs.Bounds.Height += label.Bounds.Height;
sources.AddChild(label);
sources.Bounds.Height += label.Bounds.Height;
}
widget.Bounds.Width = 2 * sideMargin + maxWidth;
widget.Bounds.Height = discs.Bounds.Y + bottomMargin + discs.Bounds.Height;
widget.Bounds.Height = sources.Bounds.Y + bottomMargin + sources.Bounds.Height;
}
}
}