Implement plugin browser and downloader
This commit is contained in:
@@ -39,6 +39,10 @@
|
||||
<HintPath>..\GameBinaries\HavokWrapper.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
@@ -189,6 +193,7 @@
|
||||
<Compile Include="Session\ITorchSessionManager.cs" />
|
||||
<Compile Include="Session\TorchSessionState.cs" />
|
||||
<Compile Include="TorchGameState.cs" />
|
||||
<Compile Include="WebAPI\PluginQuery.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
159
Torch.API/WebAPI/PluginQuery.cs
Normal file
159
Torch.API/WebAPI/PluginQuery.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
|
||||
namespace Torch.API.WebAPI
|
||||
{
|
||||
public class PluginQuery
|
||||
{
|
||||
private const string ALL_QUERY = "https://torchapi.net/api/plugins";
|
||||
private const string PLUGIN_QUERY = "https://torchapi.net/api/plugins/{0}";
|
||||
private readonly HttpClient _client;
|
||||
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private static PluginQuery _instance;
|
||||
public static PluginQuery Instance => _instance ?? (_instance = new PluginQuery());
|
||||
|
||||
private PluginQuery()
|
||||
{
|
||||
_client = new HttpClient();
|
||||
}
|
||||
|
||||
public async Task<PluginResponse> QueryAll()
|
||||
{
|
||||
var h = await _client.GetAsync(ALL_QUERY);
|
||||
if (!h.IsSuccessStatusCode)
|
||||
{
|
||||
Log.Error($"Plugin query returned response {h.StatusCode}");
|
||||
return null;
|
||||
}
|
||||
|
||||
var r = await h.Content.ReadAsStringAsync();
|
||||
|
||||
PluginResponse response;
|
||||
try
|
||||
{
|
||||
response = JsonConvert.DeserializeObject<PluginResponse>(r);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Failed to deserialize plugin query response!");
|
||||
return null;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<PluginFullItem> QueryOne(Guid guid)
|
||||
{
|
||||
return await QueryOne(guid.ToString());
|
||||
}
|
||||
|
||||
public async Task<PluginFullItem> QueryOne(string guid)
|
||||
{
|
||||
|
||||
var h = await _client.GetAsync(string.Format(PLUGIN_QUERY, guid));
|
||||
if (!h.IsSuccessStatusCode)
|
||||
{
|
||||
Log.Error($"Plugin query returned response {h.StatusCode}");
|
||||
return null;
|
||||
}
|
||||
|
||||
var r = await h.Content.ReadAsStringAsync();
|
||||
|
||||
PluginFullItem response;
|
||||
try
|
||||
{
|
||||
response = JsonConvert.DeserializeObject<PluginFullItem>(r);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Failed to deserialize plugin query response!");
|
||||
return null;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<bool> DownloadPlugin(Guid guid)
|
||||
{
|
||||
return await DownloadPlugin(guid.ToString());
|
||||
}
|
||||
|
||||
public async Task<bool> DownloadPlugin(string guid)
|
||||
{
|
||||
var item = await QueryOne(guid);
|
||||
return await DownloadPlugin(item);
|
||||
}
|
||||
|
||||
public async Task<bool> DownloadPlugin(PluginFullItem item)
|
||||
{
|
||||
try
|
||||
{
|
||||
var h = await _client.GetAsync(string.Format(PLUGIN_QUERY, item.ID));
|
||||
string res = await h.Content.ReadAsStringAsync();
|
||||
var response = JsonConvert.DeserializeObject<PluginFullItem>(res);
|
||||
if (response.Versions.Length == 0)
|
||||
{
|
||||
Log.Error($"Selected plugin {item.Name} does not have any versions to download!");
|
||||
return false;
|
||||
}
|
||||
var version = response.Versions.FirstOrDefault(v => v.Version == response.LatestVersion);
|
||||
if (version == null)
|
||||
{
|
||||
Log.Error($"Could not find latest version for selected plugin {item.Name}");
|
||||
return false;
|
||||
}
|
||||
var s = await _client.GetStreamAsync(version.URL);
|
||||
using (var f = new FileStream($"Plugins\\{item.Name}.zip", FileMode.Create))
|
||||
{
|
||||
await s.CopyToAsync(f);
|
||||
await f.FlushAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Failed to download plugin!");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class PluginResponse
|
||||
{
|
||||
public PluginItem[] Plugins;
|
||||
public int Count;
|
||||
}
|
||||
|
||||
public class PluginItem
|
||||
{
|
||||
public string ID;
|
||||
public string Name;
|
||||
public string Author;
|
||||
public string Description;
|
||||
public string LatestVersion;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
|
||||
public class PluginFullItem : PluginItem
|
||||
{
|
||||
public VersionItem[] Versions;
|
||||
}
|
||||
|
||||
public class VersionItem
|
||||
{
|
||||
public string Version;
|
||||
public string Note;
|
||||
public bool IsBeta;
|
||||
public string URL;
|
||||
}
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Mono.TextTransform" version="1.0.0" targetFramework="net461" />
|
||||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
|
||||
<package id="NLog" version="4.4.12" targetFramework="net461" />
|
||||
</packages>
|
Reference in New Issue
Block a user