2020-06-16 17:22:29 +00:00
|
|
|
|
using GCMM.Properties;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-06-16 21:33:10 +00:00
|
|
|
|
using System.Drawing;
|
2020-06-16 17:22:29 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2020-06-17 13:08:22 +00:00
|
|
|
|
using System.Net;
|
2020-06-16 17:22:29 +00:00
|
|
|
|
using System.Reflection;
|
2020-06-17 13:08:22 +00:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2020-06-16 17:22:29 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace GCMM
|
|
|
|
|
{
|
|
|
|
|
partial class MainForm
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public void GetInstalledMods()
|
|
|
|
|
{
|
2020-07-03 00:00:08 +00:00
|
|
|
|
foreach (var modPath in Directory.GetFiles(GamePath(@"\Plugins"), "*.dll"))
|
2020-06-16 17:22:29 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-06-16 21:33:10 +00:00
|
|
|
|
var an = AssemblyName.GetAssemblyName(modPath);
|
2020-06-16 17:22:29 +00:00
|
|
|
|
if (an.Name == "0Harmony") continue;
|
2020-06-17 16:31:20 +00:00
|
|
|
|
//Use filename to avoid differences between repository & assembly name casing
|
|
|
|
|
AddUpdateModInList(new ModInfo { Name = Path.GetFileNameWithoutExtension(modPath), Version = an.Version, LastUpdated = File.GetLastWriteTime(modPath) });
|
2020-06-16 17:22:29 +00:00
|
|
|
|
}
|
|
|
|
|
catch (BadImageFormatException)
|
|
|
|
|
{ //Not a .NET assembly
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async void GetAvailableMods()
|
|
|
|
|
{
|
2020-07-17 18:57:46 +00:00
|
|
|
|
bool preview = GetExe()?.Contains("Preview") ?? false;
|
2020-06-17 21:56:18 +00:00
|
|
|
|
using (var client = GetClient())
|
2020-06-17 13:08:22 +00:00
|
|
|
|
{
|
2020-06-17 21:56:18 +00:00
|
|
|
|
string str = await client.DownloadStringTaskAsync("https://exmods.org/mods/modlist.tsv");
|
2020-06-18 16:28:07 +00:00
|
|
|
|
foreach (string line in str.Trim().Split('\n'))
|
2020-06-17 13:08:22 +00:00
|
|
|
|
{
|
2020-06-17 21:56:18 +00:00
|
|
|
|
var sp = line.Split('\t');
|
2020-06-18 16:28:07 +00:00
|
|
|
|
if (sp.Length < 2) continue;
|
2020-06-17 21:56:18 +00:00
|
|
|
|
var mod = new ModInfo
|
|
|
|
|
{
|
|
|
|
|
Author = sp[0].Trim(),
|
|
|
|
|
Name = sp[1].Trim()
|
|
|
|
|
};
|
2020-07-17 18:57:46 +00:00
|
|
|
|
if (await FetchModInfo(mod, preview)) //If it's actually a mod
|
2020-06-17 21:56:18 +00:00
|
|
|
|
AddUpdateModInList(mod);
|
|
|
|
|
}
|
2020-06-16 21:33:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 18:57:46 +00:00
|
|
|
|
public async Task<bool> FetchModInfo(ModInfo mod, bool preview)
|
2020-06-16 21:33:10 +00:00
|
|
|
|
{
|
|
|
|
|
string repoURL = "/api/v1/repos/" + mod.Author + "/" + mod.Name + "/releases";
|
|
|
|
|
using (var client = GetClient())
|
|
|
|
|
{
|
2020-06-17 13:08:22 +00:00
|
|
|
|
var arr = JArray.Parse(await client.DownloadStringTaskAsync(repoURL));
|
2020-07-17 18:57:46 +00:00
|
|
|
|
var release = arr.FirstOrDefault(rel =>
|
|
|
|
|
{
|
|
|
|
|
if ((bool) rel["prerelease"] || (bool) rel["draft"])
|
|
|
|
|
return false;
|
|
|
|
|
var vs = rel["tag_name"].ToString();
|
|
|
|
|
int ind = vs.IndexOf('-');
|
|
|
|
|
if (ind != -1)
|
|
|
|
|
{
|
|
|
|
|
if (vs.Substring(ind + 1).Equals("preview", StringComparison.InvariantCultureIgnoreCase)
|
|
|
|
|
&& !preview)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2020-06-16 21:33:10 +00:00
|
|
|
|
if (release == null)
|
|
|
|
|
return false;
|
2020-07-17 18:57:46 +00:00
|
|
|
|
var verstr = release["tag_name"].ToString().Replace("v", "");
|
|
|
|
|
int index = verstr.IndexOf('-');
|
|
|
|
|
if (index != -1)
|
|
|
|
|
verstr = verstr.Substring(0, index);
|
|
|
|
|
|
2020-06-18 16:28:07 +00:00
|
|
|
|
JToken asset;
|
2020-06-17 13:08:22 +00:00
|
|
|
|
if (release["assets"].Count() == 1)
|
2020-06-18 16:28:07 +00:00
|
|
|
|
asset = release["assets"].First;
|
|
|
|
|
else
|
|
|
|
|
asset = release["assets"].FirstOrDefault(token =>
|
|
|
|
|
{
|
|
|
|
|
string name = token["name"].ToString();
|
|
|
|
|
return name == mod.Name + ".dll" || name == mod.Name + ".zip";
|
|
|
|
|
});
|
2020-07-17 18:57:46 +00:00
|
|
|
|
|
2020-06-18 16:28:07 +00:00
|
|
|
|
mod.DownloadURL = asset?["browser_download_url"]?.ToString();
|
2020-06-17 13:08:22 +00:00
|
|
|
|
mod.LastUpdated = (DateTime)release["published_at"];
|
2020-07-17 18:57:46 +00:00
|
|
|
|
|
|
|
|
|
var ver = verstr.Split('.').Select(str => int.Parse(str)).ToArray();
|
2020-06-17 13:08:22 +00:00
|
|
|
|
int getver(byte i) => ver.Length > i ? ver[i] : 0; //By default it sets values not present to -1, but we need them to be 0
|
|
|
|
|
mod.LatestVersion = new Version(getver(0), getver(1), getver(2), getver(3));
|
2020-06-17 21:56:18 +00:00
|
|
|
|
mod.UpdateDetails = release["name"] + "\n\n" + release["body"].ToString();
|
2020-06-17 13:08:22 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var obj = JObject.Parse(await client.DownloadStringTaskAsync("/api/v1/repos/" + mod.Author + "/" + mod.Name + "/contents/README.md"));
|
|
|
|
|
mod.Description = Encoding.UTF8.GetString(Convert.FromBase64String(obj["content"].ToString()));
|
|
|
|
|
}
|
|
|
|
|
catch(WebException)
|
|
|
|
|
{ //It returns a HTTP 500 if it doesn't exist...
|
2020-06-16 17:22:29 +00:00
|
|
|
|
}
|
2020-06-17 13:08:22 +00:00
|
|
|
|
return true;
|
2020-06-16 17:22:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-16 21:33:10 +00:00
|
|
|
|
|
|
|
|
|
public void AddUpdateModInList(ModInfo mod)
|
|
|
|
|
{
|
2020-06-17 16:31:20 +00:00
|
|
|
|
if (mods.ContainsKey(mod.Name) ^ modlist.Items.ContainsKey(mod.Name)) //The ListView's keys aren't case sensitive
|
2020-06-16 21:33:10 +00:00
|
|
|
|
throw new InvalidOperationException("The mod isn't present in one of the two places: " + mod.Name);
|
2020-06-17 21:56:18 +00:00
|
|
|
|
ListViewItem item;
|
2020-06-16 21:33:10 +00:00
|
|
|
|
if (modlist.Items.ContainsKey(mod.Name))
|
|
|
|
|
{
|
|
|
|
|
var omod = mods[mod.Name];
|
2020-06-17 21:56:18 +00:00
|
|
|
|
item = modlist.Items[mod.Name];
|
2020-06-16 21:33:10 +00:00
|
|
|
|
var items = item.SubItems;
|
|
|
|
|
omod.Author = mod.Author ?? omod.Author;
|
|
|
|
|
omod.Version = mod.Version ?? omod.Version;
|
|
|
|
|
omod.LatestVersion = mod.LatestVersion ?? omod.LatestVersion;
|
|
|
|
|
omod.LastUpdated = mod.LastUpdated == default ? omod.LastUpdated : mod.LastUpdated;
|
2020-06-17 13:08:22 +00:00
|
|
|
|
omod.Description = mod.Description ?? omod.Description;
|
|
|
|
|
omod.DownloadURL = mod.DownloadURL ?? omod.DownloadURL;
|
2020-06-17 21:56:18 +00:00
|
|
|
|
omod.UpdateDetails = mod.UpdateDetails ?? omod.UpdateDetails;
|
2020-06-16 21:33:10 +00:00
|
|
|
|
items[1].Text = omod.Author ?? "";
|
2020-06-17 13:08:22 +00:00
|
|
|
|
items[2].Text = (omod.Version ?? omod.LatestVersion)?.ToString();
|
2020-06-16 21:33:10 +00:00
|
|
|
|
items[3].Text = omod.LastUpdated.ToString();
|
|
|
|
|
item.Group = omod.Installed ? modlist.Groups["installed"] : modlist.Groups["available"];
|
2020-06-17 21:56:18 +00:00
|
|
|
|
mod = omod;
|
2020-06-16 21:33:10 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mods.Add(mod.Name, mod);
|
2020-06-17 21:56:18 +00:00
|
|
|
|
item = new ListViewItem(new[] { mod.Name, mod.Author ?? "", (mod.Version ?? mod.LatestVersion)?.ToString() ?? "", mod.LastUpdated.ToString() }, modlist.Groups[mod.Installed ? "installed" : "available"]);
|
2020-06-16 21:33:10 +00:00
|
|
|
|
item.Name = mod.Name;
|
|
|
|
|
modlist.Items.Add(item);
|
|
|
|
|
}
|
2020-06-17 21:56:18 +00:00
|
|
|
|
if (mod.LatestVersion != null && mod.Version != null && mod.Version < mod.LatestVersion)
|
|
|
|
|
item.ForeColor = Color.Blue;
|
2020-07-03 00:00:08 +00:00
|
|
|
|
else
|
|
|
|
|
item.ForeColor = modlist.ForeColor;
|
2020-06-16 21:33:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveModFromList(ModInfo mod)
|
|
|
|
|
{
|
|
|
|
|
if (mods.Remove(mod.Name))
|
|
|
|
|
modlist.Items.RemoveByKey(mod.Name);
|
|
|
|
|
}
|
2020-06-16 17:22:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|