From 4bfd108702215443ecd008cad04a7fda53bc7143 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Sun, 26 Dec 2021 16:04:15 -0700 Subject: [PATCH] Finish porting Wabbajack.Lib --- Wabbajack.Lib/LibCefHelpers/Init.cs | 62 +----- .../ModListRegistry/InstalledModLists.cs | 66 ------- .../ModListRegistry/ModListMetadata.cs | 180 ------------------ Wabbajack.Lib/WebAutomation/WebAutomation.cs | 1 - 4 files changed, 7 insertions(+), 302 deletions(-) delete mode 100644 Wabbajack.Lib/ModListRegistry/InstalledModLists.cs delete mode 100644 Wabbajack.Lib/ModListRegistry/ModListMetadata.cs diff --git a/Wabbajack.Lib/LibCefHelpers/Init.cs b/Wabbajack.Lib/LibCefHelpers/Init.cs index 08a28b66..4f86218c 100644 --- a/Wabbajack.Lib/LibCefHelpers/Init.cs +++ b/Wabbajack.Lib/LibCefHelpers/Init.cs @@ -4,26 +4,25 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Reflection; -using System.Text; -using System.Threading; using System.Threading.Tasks; using Alphaleonis.Win32.Filesystem; using CefSharp; using CefSharp.OffScreen; using Wabbajack.Common; -using Wabbajack.Common.Serialization.Json; +using Wabbajack.DTOs.JsonConverters; using Cookie = CefSharp.Cookie; namespace Wabbajack.Lib.LibCefHelpers { public static class Helpers { - public static Wabbajack.Lib.Http.Client GetClient(IEnumerable cookies, string referer) + public static HttpRequestMessage MakeMessage(HttpMethod method, Uri uri, IEnumerable cookies, string referer) { - var client = new Wabbajack.Lib.Http.Client(); - client.Headers.Add(("Referrer", referer)); - client.Cookies.AddRange(cookies.Select(cookie => new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain))); - return client; + var msg = new HttpRequestMessage(method, uri); + msg.Headers.Add("Referrer", referer); + var cs = string.Join(",", cookies.Select(c => $"{c.Name}={c.Value}")); + msg.Headers.Add("Cookie", cs); + return msg; } private static CookieContainer ToCookieContainer(IEnumerable cookies) @@ -85,43 +84,6 @@ namespace Wabbajack.Lib.LibCefHelpers public string Path { get; set; } = string.Empty; } - public static Func? SchemeHandler { get; set; } - - private static object _initLock = new object(); - public static void Init() - { - lock (_initLock) - { - if (Inited || Cef.IsInitialized) return; - Inited = true; - CefSettings settings = new CefSettings(); - settings.CachePath = Consts.CefCacheLocation.ToString(); - settings.JavascriptFlags = "--noexpose_wasm"; - settings.RegisterScheme(new CefCustomScheme() - { - SchemeName = "wabbajack", SchemeHandlerFactory = new SchemeHandlerFactor() - }); - - - Cef.Initialize(settings); - } - } - - private class SchemeHandlerFactor : ISchemeHandlerFactory - { - public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request) - { - Utils.LogStraightToFile($"Scheme Handler Got: {schemeName} : {request.Url}"); - if (SchemeHandler != null && schemeName == "wabbajack") - { - return SchemeHandler!(browser, frame, schemeName, request); - } - return new ResourceHandler(); - } - } - - public static bool Inited { get; set; } - public static void ClearCookies() { var manager = Cef.GetGlobalCookieManager(); @@ -168,14 +130,4 @@ namespace Wabbajack.Lib.LibCefHelpers return true; } } - - public static class ModuleInitializer - { - public static void Initialize() - { - var es = Assembly.GetEntryAssembly(); - if (es != null && es.Location != null && Path.GetFileNameWithoutExtension(es.Location) == "Wabbajack") - Helpers.Init(); - } - } } diff --git a/Wabbajack.Lib/ModListRegistry/InstalledModLists.cs b/Wabbajack.Lib/ModListRegistry/InstalledModLists.cs deleted file mode 100644 index c946a1bb..00000000 --- a/Wabbajack.Lib/ModListRegistry/InstalledModLists.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Wabbajack.Common; -using Wabbajack.Common.Serialization.Json; - -namespace Wabbajack.Lib.ModListRegistry -{ - public class InstalledModLists - { - public static AbsolutePath InstalledModlistsLocation = Consts.LocalAppDataPath.Combine("installed_modlists.json"); - private static AsyncLock _lock = new(); - - public static async Task AddModListInstall(ModlistMetadata? metadata, ModList modList, AbsolutePath installPath, - AbsolutePath downloadPath, AbsolutePath wabbjackPath) - { - modList = modList.Clone(); - modList.Directives = new List(); - modList.Archives = new List(); - - var newRecord = new ModListInstall() - { - Metadata = metadata, - ModList = modList, - InstallationPath = installPath, - DownloadPath = downloadPath, - WabbajackPath = wabbjackPath, - }; - await UpsertInstall(newRecord); - } - - public static async Task UpsertInstall(ModListInstall newRecord) - { - using var _ = await _lock.WaitAsync(); - Dictionary oldRecords = new(); - if (InstalledModlistsLocation.Exists) - oldRecords = await InstalledModlistsLocation.FromJsonAsync>(); - - oldRecords[newRecord.InstallationPath] = newRecord; - - CleanEntries(oldRecords); - - await oldRecords.ToJsonAsync(InstalledModlistsLocation); - } - - private static void CleanEntries(Dictionary oldRecords) - { - oldRecords.Keys - .Where(k => !k.IsDirectory) - .ToArray() - .Do(k => oldRecords.Remove(k)); - } - } - - [JsonName("ModListInstall")] - public class ModListInstall - { - public ModlistMetadata? Metadata { get; set; } - public ModList ModList { get; set; } = new(); - public AbsolutePath InstallationPath { get; set; } - public AbsolutePath DownloadPath { get; set; } - public AbsolutePath WabbajackPath { get; set; } - public DateTime InstalledAt { get; set; } = DateTime.UtcNow; - } -} diff --git a/Wabbajack.Lib/ModListRegistry/ModListMetadata.cs b/Wabbajack.Lib/ModListRegistry/ModListMetadata.cs deleted file mode 100644 index a5f72a7a..00000000 --- a/Wabbajack.Lib/ModListRegistry/ModListMetadata.cs +++ /dev/null @@ -1,180 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Newtonsoft.Json; -using Wabbajack.Common; -using Wabbajack.Common.Serialization.Json; -using Game = Wabbajack.Common.Game; - -namespace Wabbajack.Lib.ModListRegistry -{ - [JsonName("ModListMetadata")] - public class ModlistMetadata - { - [JsonProperty("title")] - public string Title { get; set; } = string.Empty; - - [JsonProperty("description")] - public string Description { get; set; } = string.Empty; - - [JsonProperty("author")] - public string Author { get; set; } = string.Empty; - - [JsonProperty("maintainers")] public string[] - Maintainers { get; set; } = Array.Empty(); - - [JsonProperty("game")] - public Game Game { get; set; } - - [JsonIgnore] public string GameName => Game.ToDescriptionString(); - - [JsonProperty("official")] - public bool Official { get; set; } - - [JsonProperty("tags")] - public List tags { get; set; } = new List(); - - [JsonProperty("nsfw")] - public bool NSFW { get; set; } - - [JsonProperty("utility_list")] - public bool UtilityList { get; set; } - - [JsonProperty("image_contains_title")] - public bool ImageContainsTitle { get; set; } - - [JsonProperty("force_down")] - public bool ForceDown { get; set; } - - [JsonProperty("links")] - public LinksObject Links { get; set; } = new LinksObject(); - - [JsonProperty("download_metadata")] - public DownloadMetadata? DownloadMetadata { get; set; } - - [JsonProperty("version")] - public Version? Version { get; set; } - - [JsonIgnore] - public ModListSummary ValidationSummary { get; set; } = new ModListSummary(); - - [JsonName("Links")] - public class LinksObject - { - [JsonProperty("image")] - public string ImageUri { get; set; } = string.Empty; - - [JsonProperty("readme")] - public string Readme { get; set; } = string.Empty; - - [JsonProperty("download")] - public string Download { get; set; } = string.Empty; - - [JsonProperty("machineURL")] - public string MachineURL { get; set; } = string.Empty; - } - - public static async Task> LoadFromGithub() - { - var client = new Http.Client(); - Utils.Log("Loading ModLists from GitHub"); - var metadataResult = client.GetStringAsync(Consts.ModlistMetadataURL); - var utilityResult = client.GetStringAsync(Consts.UtilityModlistMetadataURL); - var summaryResult = client.GetStringAsync(Consts.ModlistSummaryURL); - - var metadata = (await metadataResult).FromJsonString>(); - metadata = metadata.Concat((await utilityResult).FromJsonString>()).ToList(); - try - { - var summaries = (await summaryResult).FromJsonString>().ToDictionary(d => d.MachineURL); - - foreach (var data in metadata) - if (summaries.TryGetValue(data.Links.MachineURL, out var summary)) - data.ValidationSummary = summary; - } - catch (Exception) - { - // ignored - } - - var random = new Random(); - return metadata - // Sort randomly initially, just to give each list a fair shake - .Shuffle(random) - // Put broken lists at bottom - .OrderBy(m => (m.ValidationSummary?.HasFailures ?? false ? 1 : 0)) - .ToList(); - } - - public static async Task> LoadUnlistedFromGithub() - { - try - { - var client = new Http.Client(); - return (await client.GetStringAsync(Consts.UnlistedModlistMetadataURL)).FromJsonString>(); - } - catch (Exception) - { - Utils.LogStatus("Error loading unlisted modlists"); - return new List(); - } - - } - - public async ValueTask NeedsDownload(AbsolutePath modlistPath) - { - if (!modlistPath.Exists) return true; - if (DownloadMetadata?.Hash == null) - { - return true; - } - return DownloadMetadata.Hash != await modlistPath.FileHashCachedAsync(); - } - } - - [JsonName("DownloadMetadata")] - public class DownloadMetadata - { - public Hash Hash { get; set; } - public long Size { get; set; } - public long NumberOfArchives { get; set; } - public long SizeOfArchives { get; set; } - public long NumberOfInstalledFiles { get; set; } - public long SizeOfInstalledFiles { get; set; } - } - - [JsonName("ModListSummary")] - public class ModListSummary - { - [JsonProperty("name")] - public string Name { get; set; } = string.Empty; - - [JsonProperty("machineURL")] - public string MachineURL { get; set; } = string.Empty; - - [JsonProperty("checked")] - public DateTime Checked { get; set; } - [JsonProperty("failed")] - public int Failed { get; set; } - [JsonProperty("passed")] - public int Passed { get; set; } - [JsonProperty("updating")] - public int Updating { get; set; } - - [JsonProperty("mirrored")] - public int Mirrored { get; set; } - - [JsonProperty("link")] - public string Link => $"/lists/status/{MachineURL}.json"; - [JsonProperty("report")] - public string Report => $"/lists/status/{MachineURL}.html"; - - [JsonProperty("modlist_missing")] - public bool ModListIsMissing { get; set; } - - [JsonProperty("has_failures")] - public bool HasFailures => Failed > 0 || ModListIsMissing; - } - -} diff --git a/Wabbajack.Lib/WebAutomation/WebAutomation.cs b/Wabbajack.Lib/WebAutomation/WebAutomation.cs index ca31f705..defde19f 100644 --- a/Wabbajack.Lib/WebAutomation/WebAutomation.cs +++ b/Wabbajack.Lib/WebAutomation/WebAutomation.cs @@ -27,7 +27,6 @@ namespace Wabbajack.Lib.WebAutomation } public static async Task Create(ILogger logger) { - Helpers.Init(); var driver = new Driver(logger); await driver._driver.WaitForInitialized(); return driver;