From 1a23d663381b9e5ab640c824be4de17695a81786 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sun, 5 Aug 2018 00:18:52 -0500 Subject: [PATCH] Fixed API endpoint --- IPA.Tests/updater_test.json | 49 ++++---- IllusionInjector/IllusionInjector.csproj | 2 +- .../Updating/ModsaberML/ApiEndpoint.cs | 6 +- .../Updating/ModsaberML/Updater.cs | 107 ++++++++---------- 4 files changed, 75 insertions(+), 89 deletions(-) diff --git a/IPA.Tests/updater_test.json b/IPA.Tests/updater_test.json index 4f2ab017..19df2e06 100644 --- a/IPA.Tests/updater_test.json +++ b/IPA.Tests/updater_test.json @@ -1,32 +1,27 @@ { - "mods": [ - { - "name": "bsipa-test-plugin", - "version": "0.0.1", - "approved": true, - "title": "BSIPA Test Plugin", - "description": "", - "type": "mod", - "published": "2018-08-02T03:12:18.805Z", - "gameVersion": "0.11.2", - "gameVersionID": "5b626bb15d243a0008b8886e", - "oldVersions": [], - "dependsOn": [], - "conflictsWith": [], + "name": "bsipa-test-plugin", + "version": "0.0.1", + "approved": true, + "title": "BSIPA Test Plugin", + "description": "", + "type": "mod", + "published": "2018-08-02T03:12:18.805Z", + "gameVersion": "0.11.2", + "gameVersionID": "5b626bb15d243a0008b8886e", + "oldVersions": [], + "dependsOn": [], + "conflictsWith": [], + "files": { + "steam": { + "hash": "a94e7eea2f656b2830a86000ee222b6cb06f8da5", "files": { - "steam": { - "hash": "a94e7eea2f656b2830a86000ee222b6cb06f8da5", - "files": { - "Plugins/": "da39a3ee5e6b4b0d3255bfef95601890afd80709", - "Plugins/RandomSong.dll": "64ee1ecfeda73c550004bdb5123c7ac47a45e428" - }, - "url": "https://www.modsaber.ml/cdn/randomsong/1.0.0-default.zip" - } + "Plugins/": "da39a3ee5e6b4b0d3255bfef95601890afd80709", + "Plugins/RandomSong.dll": "64ee1ecfeda73c550004bdb5123c7ac47a45e428" }, - "weight": 7, - "author": "danike", - "authorID": "5b62723f288b110008291cb9" + "url": "https://www.modsaber.ml/cdn/randomsong/1.0.0-default.zip" } - ], - "lastPage": 0 + }, + "weight": 7, + "author": "danike", + "authorID": "5b62723f288b110008291cb9" } \ No newline at end of file diff --git a/IllusionInjector/IllusionInjector.csproj b/IllusionInjector/IllusionInjector.csproj index d4a58b08..d3fe4104 100644 --- a/IllusionInjector/IllusionInjector.csproj +++ b/IllusionInjector/IllusionInjector.csproj @@ -21,7 +21,7 @@ full false bin\Debug\ - DEBUG;TRACE + TRACE;DEBUG prompt 4 false diff --git a/IllusionInjector/Updating/ModsaberML/ApiEndpoint.cs b/IllusionInjector/Updating/ModsaberML/ApiEndpoint.cs index 94fdaa0f..747b292a 100644 --- a/IllusionInjector/Updating/ModsaberML/ApiEndpoint.cs +++ b/IllusionInjector/Updating/ModsaberML/ApiEndpoint.cs @@ -10,12 +10,12 @@ namespace IllusionInjector.Updating.ModsaberML { class ApiEndpoint { -#if DEBUG +#if DEBUG && UPDATETEST public const string ApiBase = "file://Z:/Users/aaron/Source/Repos/IPA-Reloaded-BeatSaber/IPA.Tests/"; public const string GetApprovedEndpoint = "updater_test.json"; #else - public const string ApiBase = "https://www.modsaber.ml/api/"; - public const string GetApprovedEndpoint = "public/temp/approved"; + public const string ApiBase = "https://www.modsaber.ml/"; + public const string GetApprovedEndpoint = "registry/{0}"; #endif public class Mod diff --git a/IllusionInjector/Updating/ModsaberML/Updater.cs b/IllusionInjector/Updating/ModsaberML/Updater.cs index a2b2cdcf..f6977e4c 100644 --- a/IllusionInjector/Updating/ModsaberML/Updater.cs +++ b/IllusionInjector/Updating/ModsaberML/Updater.cs @@ -55,68 +55,67 @@ namespace IllusionInjector.Updating.ModsaberML Logger.log.Info("Checking for mod updates..."); var toUpdate = new List(); + var GameVersion = new Version(Application.version); - var modList = new List(); - using (var request = UnityWebRequest.Get(ApiEndpoint.ApiBase+ApiEndpoint.GetApprovedEndpoint)) + foreach (var plugin in PluginManager.BSMetas) { - yield return request.SendWebRequest(); + var info = plugin.ModsaberInfo; - if (request.isNetworkError) + using (var request = UnityWebRequest.Get(ApiEndpoint.ApiBase + string.Format(ApiEndpoint.GetApprovedEndpoint, info.InternalName))) { - Logger.log.Error("Network error while trying to update mods"); - Logger.log.Error(request.error); - yield break; - } - if (request.isHttpError) - { - Logger.log.Error($"Server returned an error code while trying to update mods"); - Logger.log.Error(request.error); - } + yield return request.SendWebRequest(); - var json = request.downloadHandler.text; + if (request.isNetworkError) + { + Logger.log.Error("Network error while trying to update mods"); + Logger.log.Error(request.error); + continue; + } + if (request.isHttpError) + { + if (request.responseCode == 404) + { + Logger.log.Error($"Mod {plugin.Plugin.Name} not found under name {info.InternalName}"); + continue; + } - JSONObject obj = null; - try - { - obj = JSON.Parse(json).AsObject; - } - catch (InvalidCastException) - { - Logger.log.Error($"Parse error while trying to update mods"); - Logger.log.Error($"Response doesn't seem to be a JSON object"); - yield break; - } - catch (Exception e) - { - Logger.log.Error($"Parse error while trying to update mods"); - Logger.log.Error(e); - yield break; - } + Logger.log.Error($"Server returned an error code while trying to update mod {plugin.Plugin.Name}"); + Logger.log.Error(request.error); + continue; + } - foreach (var modObj in obj["mods"].AsArray.Children) - { + var json = request.downloadHandler.text; + + JSONObject obj = null; try { - modList.Add(ApiEndpoint.Mod.DecodeJSON(modObj.AsObject)); + obj = JSON.Parse(json).AsObject; + } + catch (InvalidCastException) + { + Logger.log.Error($"Parse error while trying to update mods"); + Logger.log.Error($"Response doesn't seem to be a JSON object"); + continue; } catch (Exception e) { Logger.log.Error($"Parse error while trying to update mods"); - Logger.log.Error($"Response doesn't seem to be correctly formatted"); Logger.log.Error(e); - break; + continue; } - } - } - var GameVersion = new Version(Application.version); + ApiEndpoint.Mod modRegistry; + try + { + modRegistry = ApiEndpoint.Mod.DecodeJSON(obj); + } + catch (Exception e) + { + Logger.log.Error($"Parse error while trying to update mods"); + Logger.log.Error(e); + continue; + } - foreach (var plugin in PluginManager.BSMetas) - { - var info = plugin.ModsaberInfo; - var modRegistry = modList.FirstOrDefault(o => o.Name == info.InternalName); - if (modRegistry != null) - { // a.k.a we found it Logger.log.Debug($"Found Modsaber.ML registration for {plugin.Plugin.Name} ({info.InternalName})"); Logger.log.Debug($"Installed version: {info.CurrentVersion}; Latest version: {modRegistry.Version}"); if (modRegistry.Version > info.CurrentVersion) @@ -143,18 +142,14 @@ namespace IllusionInjector.Updating.ModsaberML if (toUpdate.Count == 0) yield break; - string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + Path.GetRandomFileName()); - Directory.CreateDirectory(tempDirectory); - Logger.log.Debug($"Created temp download dirtectory {tempDirectory}"); foreach (var item in toUpdate) { - StartCoroutine(UpdateModCoroutine(tempDirectory, item)); + StartCoroutine(UpdateModCoroutine(item)); } } class StreamDownloadHandler : DownloadHandlerScript { - public MemoryStream Stream { get; set; } public StreamDownloadHandler(MemoryStream stream) : base() @@ -197,12 +192,11 @@ namespace IllusionInjector.Updating.ModsaberML { return $"{base.ToString()} ({Stream?.ToString()})"; } - } private void ExtractPluginAsync(MemoryStream stream, UpdateStruct item, ApiEndpoint.Mod.PlatformFile fileInfo) { - Logger.log.Debug($"Getting ZIP file for {item.plugin.Plugin.Name}"); + Logger.log.Debug($"Extracting ZIP file for {item.plugin.Plugin.Name}"); //var stream = await httpClient.GetStreamAsync(url); var data = stream.GetBuffer(); @@ -216,8 +210,6 @@ namespace IllusionInjector.Updating.ModsaberML Logger.log.Debug("Streams opened"); foreach (var entry in zipFile) { - Logger.log.Debug(entry?.FileName ?? "NULL"); - if (entry.IsDirectory) { Logger.log.Debug($"Creating directory {entry.FileName}"); @@ -239,13 +231,12 @@ namespace IllusionInjector.Updating.ModsaberML FileInfo targetFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, entry.FileName)); if (targetFile.Exists) { - Logger.log.Debug($"Target file {targetFile.FullName} exists"); } + Logger.log.Debug($"Extracting file {targetFile.FullName}"); + var fstream = targetFile.Create(); ostream.CopyTo(fstream); - - Logger.log.Debug($"Wrote file {targetFile.FullName}"); } } } @@ -254,7 +245,7 @@ namespace IllusionInjector.Updating.ModsaberML Logger.log.Debug("Downloader exited"); } - IEnumerator UpdateModCoroutine(string tempdir, UpdateStruct item) + IEnumerator UpdateModCoroutine(UpdateStruct item) { ApiEndpoint.Mod.PlatformFile platformFile; if (SteamCheck.IsAvailable || item.externInfo.OculusFile == null)