| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json.Linq;
- using static FactorioModManager.ModWorker;
- namespace FactorioModManager
- {
- public class ModWorker
- {
- public class Mod
- {
- public enum Availability
- {
- Required,
- Banned,
- Optional
- }
- public bool Enabled { get; set; } = true;
- public string Name { get;}
- public string Title { get; private set; }
- public Version Version { get; }
- public string Author { get; }
- public string Contact { get; }
- public string WebSite { get; }
- public string Description { get; private set; }
- public Version FactorioVersion { get; }
- public Image Thumbnail { get; set; }
- public bool Deletable { get; private set; } = true;
- public List<string> DependenciesModNames { get
- {
- List<string> val = new List<string>();
- for(int i = 0; i < Dependencies.Count; i++)
- {
- string dep = Dependencies.Keys.ElementAt(i);
- string mod = "";
- for(int j = 0; j < dep.Length; j++)
- {
- if(Char.IsLetterOrDigit(dep[j]) || dep[j] == '_' || dep[j] == '-')
- {
- mod += dep[j];
- }
- if (dep[j] == '>' || dep[j] == '<') break;
- }
- val.Add(mod);
- }
- return val;
- } }
- public Dictionary<string, Availability> Dependencies { get; } = new Dictionary<string, Availability>();
- public string FileName => Name + "_" + Version.ToString() + ".zip";
- public static Mod CreateSimple(string name, string title,bool enabled)
- {
- JObject json = new JObject()
- {
- {"name", name},
- {"title", title},
- {"version", "0.0.1" },
- {"factorio_version", "2.0" }
- };
- Mod mod = new Mod(json);
- mod.Enabled = enabled;
- mod.Deletable = false;
- return mod;
- }
- public Mod(JObject json)
- {
- if (json == null) return;
- if (json.ContainsKey("name")) Name = (string)json["name"];
- if (json.ContainsKey("version")) Version = new Version((string)json["version"]);
- if (json.ContainsKey("factorio_version")) FactorioVersion = new Version((string)json["factorio_version"]);
- if (json.ContainsKey("title")) Title = (string)json["title"];
- if (json.ContainsKey("author")) Author = (string)json["author"];
- if (json.ContainsKey("contact")) Contact = (string)json["contact"];
- if (json.ContainsKey("homepage")) WebSite = (string)json["homepage"];
- if (json.ContainsKey("description")) Description = (string)json["description"];
- if (json.ContainsKey("dependencies")) {
- JArray deps = (JArray)json["dependencies"];
- for(int i = 0; i < deps.Count; i++)
- {
- string data = deps[i].ToString();
- if (!data.Contains("base"))
- {
- Availability availability = Availability.Required;
- if (data.Contains("?"))
- {
- availability = Availability.Optional;
- }
- else if(data.Contains("!"))
- {
- availability = Availability.Banned;
- }
- Dependencies.Add(data, availability);
- }
- }
- }
- }
- public void SetLocalizedTitle(string Title)
- {
- this.Title = Title;
- }
- public void SetLocalizedDescription(string Description)
- {
- this.Description = Description;
- }
- }
- public delegate void OnModsLoaded();
- public OnModsLoaded ModsLoaded;
- public string WorkDir { get; set; } = @"data\mods";
- public F_API API { get; set; } = null;
- public List<Mod> InstalledMods { get; } = new List<Mod>();
- public List<Mod> UpdatebleMods { get; } = new List<Mod>();
- public static readonly string[] DLC_MODS =
- {
- "space-age",
- "elevated-rails",
- "quality"
- };
- public ModWorker()
- {
- CheckDirSize();
-
- }
- public void LoadModList()
- {
- InstalledMods.Clear();
- if (!String.IsNullOrEmpty(Settings.GamePath))
- {
- string[] files = Directory.GetFiles(Settings.GamePath);
- foreach(string mod in files)
- {
- if (mod.EndsWith(".zip"))
- {
- Mod jMod = null;
- StreamReader sr = new StreamReader(mod);
- using (ZipArchive archive = new ZipArchive(sr.BaseStream))
- {
- string modname = archive.Entries[0].FullName.Substring(0, archive.Entries[0].FullName.IndexOf('/') + 1);
- for (int i = 0; i < archive.Entries.Count; i++)
- {
- string path = archive.Entries[i].FullName;
- if (archive.Entries[i].FullName == modname + "info.json")
- {
- StreamReader ssr = new StreamReader(archive.Entries[i].Open());
- jMod = new Mod(JObject.Parse(ssr.ReadToEnd()));
- ssr.Close();
- break;
- }
- }
- for (int i = 0; i < archive.Entries.Count; i++)
- {
- if (archive.Entries[i].FullName.StartsWith(modname + "locale/" + Settings.LastLanguage + "/") && archive.Entries[i].FullName.EndsWith(".cfg"))
- {
- StreamReader ssr = new StreamReader(archive.Entries[i].Open());
- string content = ssr.ReadToEnd();
- ssr.Close();
- string[] lines = content.Split('\n');
- for (int j = 0; j < lines.Length; j++)
- {
- lines[j] = lines[j].Trim();
- if (lines[j].Equals("[mod-name]"))
- {
- jMod.SetLocalizedTitle(lines[j + 1].Split('=')[1].Replace("\\n","\n").Replace("\\t", "\t"));
- }
- if (lines[j].Equals("[mod-description]"))
- {
- jMod.SetLocalizedDescription(lines[j + 1].Split('=')[1].Replace("\\n", "\n").Replace("\\t", "\t"));
- }
- }
- }
- if(archive.Entries[i].FullName.StartsWith(modname+ "thumbnail."))
- {
- jMod.Thumbnail = Image.FromStream(archive.Entries[i].Open());
- }
- }
- }
- sr.Close();
- InstalledMods.Add(jMod);
- }
- }
- string[] folders = Directory.GetDirectories(Settings.GamePath);
- foreach (string folder in folders)
- {
- Version v;
- if(folder.Contains("_") && Version.TryParse(folder.Substring(folder.LastIndexOf("_") + 1),out v))
- {
- }
- }
- StreamReader srr = new StreamReader(Path.Combine(Settings.GamePath, "mod-list.json"));
- JObject jlist = JObject.Parse(srr.ReadToEnd());
- srr.Close();
- JArray list = (JArray)jlist["mods"];
- for(int i = 0; i < list.Count; i++)
- {
- string name = (string)((JObject)list[i])["name"];
- bool enabled = (bool)((JObject)list[i])["enabled"];
- if (DLC_MODS.Contains(name))
- {
- InstalledMods.Insert(0, Mod.CreateSimple(name,name,enabled));
- }
- int id = InstalledMods.FindIndex(x => x.Name == name);
- if (id != -1) InstalledMods[id].Enabled = enabled;
- }
- }
- //InstalledMods.Sort((x, y) => x.Title.CompareTo(y.Title));
- ModsLoaded?.Invoke();
- }
- public void CheckDirSize()
- {
- string dir = Path.Combine(Directory.GetCurrentDirectory(), WorkDir);
- Directory.CreateDirectory(dir);
- long size = new DirectoryInfo(dir).EnumerateFiles().Sum(file => file.Length);
- double sizeInMb = size / 1024 / 1024;
- if (sizeInMb > 500)
- {
- List<FileInfo> files = new DirectoryInfo(dir).EnumerateFiles().ToList();
- foreach(FileInfo file in files){
- File.Delete(file.FullName);
- }
- }
-
- }
- public string GetModList(bool enabledOnly = false)
- {
- JObject json = new JObject();
- JArray list = new JArray
- {
- new JObject()
- {
- {"name", "base"},
- {"enabled", true}
- }
- };
- List<Mod> mods = enabledOnly ? InstalledMods.Where(x=>x.Enabled).ToList() : InstalledMods;
- foreach (Mod mod in mods)
- {
- list.Add(new JObject()
- {
- {"name", mod.Name},
- {"enabled", mod.Enabled}
- });
- }
- json.Add("mods", list);
- return json.ToString();
- }
- }
- }
|