Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netkan errors and warnings for Harmony bundlers #3326

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Netkan/CKAN-netkan.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<Compile Include="Validators\CraftsInShipsValidator.cs" />
<Compile Include="Validators\DownloadVersionValidator.cs" />
<Compile Include="Validators\HasIdentifierValidator.cs" />
<Compile Include="Validators\HarmonyValidator.cs" />
<Compile Include="Validators\InstallsFilesValidator.cs" />
<Compile Include="Validators\InstallValidator.cs" />
<Compile Include="Validators\IsCkanModuleValidator.cs" />
Expand Down
1 change: 1 addition & 0 deletions Netkan/Validators/CkanValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public CkanValidator(IHttpService downloader, IModuleService moduleService)
new MatchesKnownGameVersionsValidator(),
new ObeysCKANSchemaValidator(),
new KindValidator(),
new HarmonyValidator(downloader, moduleService),
new ModuleManagerDependsValidator(downloader, moduleService),
new PluginCompatibilityValidator(downloader, moduleService),
new CraftsInShipsValidator(downloader, moduleService),
Expand Down
60 changes: 60 additions & 0 deletions Netkan/Validators/HarmonyValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Linq;

using Newtonsoft.Json.Linq;
using ICSharpCode.SharpZipLib.Zip;
using log4net;

using CKAN.Games;
using CKAN.NetKAN.Model;
using CKAN.NetKAN.Services;

namespace CKAN.NetKAN.Validators
{
internal sealed class HarmonyValidator : IValidator
{
public HarmonyValidator(IHttpService http, IModuleService moduleService)
{
_http = http;
_moduleService = moduleService;
}

public void Validate(Metadata metadata)
{
JObject json = metadata.Json();
CkanModule mod = CkanModule.FromJson(json.ToString());
// The Harmony2 module is allowed to install a Harmony DLL;
// anybody else must have "provides":["Harmony1"] to do so
if (!mod.IsDLC && mod.identifier != "Harmony2")
{
// Need to peek at the mod's files
var package = _http.DownloadModule(metadata);
if (!string.IsNullOrEmpty(package))
{
ZipFile zip = new ZipFile(package);
GameInstance inst = new GameInstance(new KerbalSpaceProgram(), "/", "dummy", new NullUser());

var harmonyDLLs = _moduleService.GetPlugins(mod, zip, inst)
.Select(instF => instF.source.Name)
.Where(f => f.IndexOf("Harmony", StringComparison.InvariantCultureIgnoreCase) != -1)
.ToList();
bool bundlesHarmony = harmonyDLLs.Any();
bool providesHarmony1 = mod.ProvidesList.Contains("Harmony1");
if (bundlesHarmony && !providesHarmony1)
{
throw new Kraken($"Harmony DLL found at {string.Join(", ", harmonyDLLs)}, but Harmony1 is not in the provides list");
}
else if (providesHarmony1 && !bundlesHarmony)
{
Log.Warn("Harmony1 provided by module that doesn't install a Harmony DLL, consider removing from provides list");
}
}
}
}

private readonly IHttpService _http;
private readonly IModuleService _moduleService;

private static readonly ILog Log = LogManager.GetLogger(typeof(HarmonyValidator));
}
}