Browse Source

Fixed API endpoint

refactor
Anairkoen Schno 6 years ago
parent
commit
1a23d66338
4 changed files with 75 additions and 89 deletions
  1. +22
    -27
      IPA.Tests/updater_test.json
  2. +1
    -1
      IllusionInjector/IllusionInjector.csproj
  3. +3
    -3
      IllusionInjector/Updating/ModsaberML/ApiEndpoint.cs
  4. +49
    -58
      IllusionInjector/Updating/ModsaberML/Updater.cs

+ 22
- 27
IPA.Tests/updater_test.json View File

@ -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"
}

+ 1
- 1
IllusionInjector/IllusionInjector.csproj View File

@ -21,7 +21,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>


+ 3
- 3
IllusionInjector/Updating/ModsaberML/ApiEndpoint.cs View File

@ -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


+ 49
- 58
IllusionInjector/Updating/ModsaberML/Updater.cs View File

@ -55,68 +55,67 @@ namespace IllusionInjector.Updating.ModsaberML
Logger.log.Info("Checking for mod updates...");
var toUpdate = new List<UpdateStruct>();
var GameVersion = new Version(Application.version);
var modList = new List<ApiEndpoint.Mod>();
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)


Loading…
Cancel
Save