|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using GCMM.Properties;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace GCMM
- {
- partial class MainForm
- {
-
- public void GetInstalledMods()
- {
- foreach (var modPath in Directory.GetFiles(Settings.Default.GamePath + @"\Plugins", "*.dll"))
- {
- try
- {
- var an = AssemblyName.GetAssemblyName(modPath);
- if (an.Name == "0Harmony") continue;
- AddUpdateModInList(new ModInfo { Name = an.Name, Version = an.Version.ToString(), LastUpdated = File.GetLastWriteTime(modPath) });
- }
- catch (BadImageFormatException)
- { //Not a .NET assembly
- }
- }
- }
-
- public async void GetAvailableMods()
- {
- string reposURL = "/api/v1/repos/search?sort=updated&order=desc";
- using (var client = GetClient())
- {
- var obj = JObject.Parse(await client.DownloadStringTaskAsync(reposURL));
- if (!(bool)obj["ok"]) return;
- foreach (var repo in obj["data"])
- {
- var mod = new ModInfo
- {
- Name = repo["name"].ToString(),
- Author = repo["owner"]["username"].ToString(),
- LastUpdated = (DateTime)repo["updated_at"]
- };
- if (await FetchModInfo(mod)) //If it's actually a mod
- AddUpdateModInList(mod);
- }
- }
- }
-
- public async Task<bool> FetchModInfo(ModInfo mod)
- {
- string repoURL = "/api/v1/repos/" + mod.Author + "/" + mod.Name + "/releases";
- using (var client = GetClient())
- {
- var obj = JArray.Parse(await client.DownloadStringTaskAsync(repoURL));
- var release = obj.FirstOrDefault(rel => !(bool)rel["prerelease"] && !(bool)rel["draft"]);
- if (release == null)
- return false;
- else
- {
- mod.DownloadURL = release["assets"].First["browser_download_url"].ToString();
- mod.LastUpdated = (DateTime)release["published_at"];
- mod.LatestVersion = release["tag_name"].ToString().Replace("v", "");
- return true;
- }
- }
- }
-
- public void AddUpdateModInList(ModInfo mod)
- {
- if (mods.ContainsKey(mod.Name) ^ modlist.Items.ContainsKey(mod.Name))
- throw new InvalidOperationException("The mod isn't present in one of the two places: " + mod.Name);
- if (modlist.Items.ContainsKey(mod.Name))
- {
- var omod = mods[mod.Name];
- var item = modlist.Items[mod.Name];
- 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;
- items[1].Text = omod.Author ?? "";
- items[2].Text = omod.Version ?? omod.LatestVersion;
- items[3].Text = omod.LastUpdated.ToString();
- item.Group = omod.Installed ? modlist.Groups["installed"] : modlist.Groups["available"];
- if (mod.Version != mod.LatestVersion)
- items[2].ForeColor = Color.Red;
- }
- else
- {
- mods.Add(mod.Name, mod);
- var item = new ListViewItem(new[] { mod.Name, mod.Author ?? "", mod.Version ?? mod.LatestVersion ?? "", mod.LastUpdated.ToString() }, modlist.Groups[mod.Installed ? "installed" : "available"]);
- item.Name = mod.Name;
- modlist.Items.Add(item);
- }
- }
-
- public void RemoveModFromList(ModInfo mod)
- {
- if (mods.Remove(mod.Name))
- modlist.Items.RemoveByKey(mod.Name);
- }
- }
- }
|