/// <summary> /// Async variant of <see cref="TryDownloadBep(string)"/> /// </summary> /// <param name="RootPath"></param> /// <returns></returns> public static int DownloadBepAsync(string RootPath) { var start = DateTime.UtcNow; Wood.WriteLine($"Installing bepinex to {RootPath}. Total file count: {BepElements.Count}."); Wood.Indent(); var tasklist = new List <Task <bool> >(); foreach (var elm in BepElements) { var downT = new Task <bool>(() => elm.TryDownload(RootPath)); downT.Start(); tasklist.Add(downT); } //Large files create chokepoints. Task.WaitAll(tasklist.ToArray()); int errc = 0; foreach (var r in tasklist) { if (!r.Result) { errc++; } } Wood.Unindent(); Wood.WriteLine($"Bep installation finished; downloaded {BepElements.Count - errc}/{BepElements.Count} files."); Wood.WriteLine($"Elapsed time: {DateTime.UtcNow - start}"); return(errc); }
public static void loadJo() { jo = null; if (!edtConfigExists) { return; } try { string jsf = File.ReadAllText(edtConfigPath); jo = JObject.Parse(jsf); } catch (IOException ioe) { Wood.WriteLine("Error reading EDT config file:"); Wood.Indent(); Wood.WriteLine(ioe); Wood.Unindent(); } catch (JsonReaderException jre) { Wood.WriteLine("Error parsing EDT config:"); Wood.Indent(); Wood.WriteLine(jre); Wood.Unindent(); } hasBeenChanged = false; }
/// <summary> /// Attempts downloading BepInEx into specified directory, treated as game root folder. /// </summary> /// <param name="RootPath"></param> /// <returns>Number of errors encountered during operation</returns> public static int TryDownloadBep(string RootPath) { var start = DateTime.UtcNow; int errc = 0; Wood.WriteLine($"Installing bepinex to {RootPath}. Total file count: {BepElements.Count}"); Wood.Indent(); foreach (var part in BepElements) { if (part.TryDownload(RootPath)) { Wood.WriteLine($"Downloaded {part.mod.filename}"); } else { Wood.WriteLine($"WARNING: Couldn't download {part.mod.filename}!"); errc++; } } Wood.Unindent(); Wood.WriteLine($"Bep installation finished; downloaded {BepElements.Count - errc}/{BepElements.Count} files."); TimeSpan ts = DateTime.UtcNow - start; Wood.WriteLine($"Elapsed time: {ts}"); return(errc); }
public static void DeleteSave(UserDataStateRelay toDelete) { try { Directory.Delete(toDelete.Location, true); if (toDelete.Location == ActiveSave?.Location) { Directory.CreateDirectory(toDelete.Location); } if (AllBackups.Contains(toDelete)) { AllBackups.Remove(toDelete); } else if (ActiveSave == toDelete) { ActiveSave = null; } } catch (IOException ioe) { Wood.WriteLine($"ERROR DELETING SAVE {toDelete.MyName}:"); Wood.Indent(); Wood.WriteLine(ioe); Wood.Unindent(); } }
public static void SaveJo() { if (!hasBeenChanged) { return; } try { File.WriteAllText(edtConfigPath, jo.ToString()); Wood.WriteLine("Saving config. Contents:"); Wood.Indent(); Wood.WriteLine(jo.ToString()); Wood.Unindent(); } catch (IOException ioe) { Wood.WriteLine("Error writing EDT config file:"); Wood.Indent(); Wood.WriteLine(ioe); Wood.Unindent(); } catch (System.ArgumentNullException) { Wood.WriteLine("JO is null; nothing to write."); } }
/// <summary> /// Deserializes tag data from a given file. /// </summary> /// <param name="filepath"></param> /// <returns><c>true</c> if successful, otherwise <c>false</c>.</returns> public static bool ReadTagsFromFile(string filepath) { try { Wood.WriteLine($"Reading mod tags from file: {filepath}"); if (!File.Exists(filepath)) { goto fnf; } string json = File.ReadAllText(filepath); ReadTagData(json); Wood.WriteLine("Mod tags loaded successfully."); return(true); fnf: Wood.WriteLine($"NO TAG FILE TO READ: {filepath}"); return(false); } catch (Exception e) { Wood.WriteLine($"ERROR READING TAGS FILE FROM {filepath}:"); Wood.Indent(); Wood.WriteLine(e); Wood.Unindent(); return(false); } }
public void WriteRegInfo() { if (jo == null) { Wood.WriteLine($"Region mod {regionName} does not have a config file; cannot apply any changes."); return; } Wood.WriteLine($"Writing changes to regpack config for: {regionName}, contents:"); Wood.Indent(); Wood.WriteLine(jo); Wood.Unindent(); hasBeenChanged = false; File.WriteAllText(pathToCfg, jo.ToString()); }
private static void ReadTagData(string json) { try { if (!string.IsNullOrEmpty(json)) { TagData = JsonConvert.DeserializeObject <Dictionary <string, string> >(json); } } catch (JsonException je) { Wood.WriteLine("ERROR PARSING TAG DATA FILE:"); Wood.Indent(); Wood.WriteLine(je); Wood.Unindent(); } }
public static bool ReadTagsFromFile(string filepath) { try { string json = File.ReadAllText(filepath); ReadTagData(json); return(true); } catch (IOException ioe) { Wood.WriteLine($"ERROR READING TAGS FILE FROM {filepath}:"); Wood.Indent(); Wood.WriteLine(ioe); Wood.Unindent(); return(false); } }
public void ReadRegInfo() { if (pathToCfg != null) { try { string jscts = File.ReadAllText(pathToCfg); jo = JObject.Parse(jscts); } catch (JsonException ioe) { Wood.WriteLine($"ERROR READING REGPACK CONFIG JSON FOR: {regionName}"); Wood.Indent(); Wood.WriteLine(ioe); Wood.Unindent(); } } }
public static void WriteConfig() { try { File.WriteAllText(BlepOut.cfgpath, confjo.ToString()); } catch (NullReferenceException) { Wood.WriteLine("Can not save config: nothing to write"); } catch (IOException ioe) { Wood.WriteLine("ERROR SERIALIZING BOI CONFIG FILE:"); Wood.Indent(); Wood.WriteLine(ioe); Wood.Unindent(); } }
public static int BOIC_RecursiveDirectoryCopy(string from, string to) { int errc = 0; DirectoryInfo din = new DirectoryInfo(from); DirectoryInfo dout = new DirectoryInfo(to); if (!din.Exists) { throw new IOException($"An attempt to copy a nonexistent directory ({from}) to {to} has occured."); } if (!dout.Exists) { Directory.CreateDirectory(to); } foreach (FileInfo fi in din.GetFiles()) { try { File.Copy(fi.FullName, Path.Combine(to, fi.Name)); } catch (IOException ioe) { Wood.Write("Could not copy a file during recursive copy process"); Wood.Indent(); Wood.WriteLine(ioe); Wood.Unindent(); errc++; } } foreach (DirectoryInfo di in din.GetDirectories()) { try { errc += BOIC_RecursiveDirectoryCopy(di.FullName, Path.Combine(to, di.Name)); } catch (IOException ioe) { Wood.Write("Could not copy a subfolder during recursive copy process"); Wood.Indent(); Wood.WriteLine(ioe); Wood.Unindent(); } } return(errc); }
public static void ReadConfig() { try { confjo = null; string tfcont = File.ReadAllText(BlepOut.cfgpath); confjo = JObject.Parse(tfcont); } catch (JsonException joe) { Wood.WriteLine("ERROR PARSING BOI CONFIG FILE:"); Wood.Indent(); Wood.WriteLine(joe); Wood.Unindent(); } catch (IOException ioe) { Wood.WriteLine("ERROR OPENING BOI CONFIG FILE:"); Wood.Indent(); Wood.WriteLine(ioe); Wood.Unindent(); } }
/// <summary> /// Downloads file relays for installable mods and bepinex parts. /// </summary> /// <returns></returns> public static bool FetchRelays() { using var wc = new WebClient(); try { ModEntryList.Clear(); BepElements.Clear(); Wood.WriteLine($"Fetching mod entries from AUDB... {DateTime.UtcNow}"); string json = wc.DownloadString(ModEntriesEP); var jo = JArray.Parse(json); foreach (JToken entry in jo) { try { AUDBEntryRelay rel = entry.ToObject <AUDBEntryRelay>(); ModEntryList.Add(rel); } catch (JsonReaderException je) { Wood.WriteLine($"Error deserializing AUDB entry :"); Wood.WriteLine(je, 1); Wood.WriteLine("Json text:"); Wood.WriteLine(entry, 1); } } Wood.WriteLine("Entrylist fetched and parsed:"); Wood.Indent(); foreach (var entry in ModEntryList) { Wood.WriteLine(entry.name); } Wood.Unindent(); Wood.WriteLine($"Fetching bep parts from AUDB... {DateTime.UtcNow}"); json = wc.DownloadString(BepEP); jo = JArray.Parse(json); foreach (var entry in jo) { try { var rel = entry.ToObject <BepPartRelay>(); BepElements.Add(rel); } catch (JsonReaderException je) { Wood.WriteLine($"Error deserializing AUDB entry :"); Wood.WriteLine(je, 1); Wood.WriteLine("Json text:"); Wood.WriteLine(entry, 1); } } Wood.WriteLine("Bep parts fetched and parsed:"); Wood.Indent(); foreach (var part in BepElements) { Wood.WriteLine(part.mod.name); } Wood.Unindent(); return(true); } catch (WebException we) { Wood.WriteLine("Error fetching info from AUDB:"); Wood.WriteLine(we.Response, 1); } catch (JsonException jse) { Wood.WriteLine("Error deserializing AUDB entry lists"); Wood.WriteLine(jse.Message, 1); } return(false); }
/// <summary> /// Attempts downloading the entry into a selected directory. /// </summary> /// <param name="TargetDirectory">Target path.</param> /// <returns><c>true</c> if successful, <c>false</c> otherwise. </returns> public bool TryDownload(string TargetDirectory) { using (var dwc = new WebClient()) { try { var fileContents = dwc.DownloadData(download); var sha = new SHA512Managed(); var modhash = sha.ComputeHash(fileContents); var sigbytes = Convert.FromBase64String(sig); var rsaParams = new RSAParameters { Exponent = Convert.FromBase64String(key.e), Modulus = Convert.FromBase64String(key.n) }; var rsa = RSA.Create(); rsa.ImportParameters(rsaParams); var def = new RSAPKCS1SignatureDeformatter(rsa); def.SetHashAlgorithm("SHA512"); bool directSigCorrect = def.VerifySignature(modhash, sigbytes); bool keySigCorrect = key.e == PrimeKeyE && key.n == PrimeKeyN; if (key.sig != null) { rsaParams.Exponent = Convert.FromBase64String(key.sig.by.e); rsaParams.Modulus = Convert.FromBase64String(key.sig.by.n); rsa.ImportParameters(rsaParams); def = new RSAPKCS1SignatureDeformatter(rsa); def.SetHashAlgorithm("SHA512"); var bee = Encoding.ASCII.GetBytes($"postkey:{key.e}-{key.n}"); modhash = sha.ComputeHash(bee); keySigCorrect = def.VerifySignature(modhash, Convert.FromBase64String(key.sig.data)); } if (directSigCorrect && keySigCorrect) { Wood.WriteLine($"Mod verified: {this.name}, saving..."); try { var resultingFilePath = Path.Combine(TargetDirectory, filename); var tfi = new DirectoryInfo(TargetDirectory); var tdi = new FileInfo(resultingFilePath); if (!tfi.Exists) { tfi.Create(); tfi.Refresh(); } else if (tdi.Exists) { Wood.WriteLine($"File {filename} already present on disk; replacing."); tdi.Delete(); } File.WriteAllBytes(resultingFilePath, fileContents); if (deps.Count > 0) { Wood.WriteLine($"{name}: Dependencies present!"); Wood.Indent(); foreach (var dep in deps) { if (dep.TryDownload(TargetDirectory)) { } } Wood.Unindent(); } } catch (IOException ioe) { Wood.WriteLine($"Can not write the downloaded mod {this.name}:"); Wood.WriteLine(ioe, 1); return(false); } return(true); } else { Wood.WriteLine($"Mod sig incorrect: {this.name}, download aborted"); return(false); } } catch (WebException we) { Wood.WriteLine($"Error downloading data from AUDB entry {name}:"); Wood.WriteLine(we, 1); } catch (Exception e) { Wood.WriteLine("Error during attempted mod download:"); Wood.WriteLine(e, 1); } } return(false); }
public static ModType GetModType(string path) { mttup ultstate = new mttup(false, false, false); try { using (ModuleDefinition md = ModuleDefinition.ReadModule(path)) { foreach (TypeDefinition t in md.Types) { mttup tstate = new mttup(false, false, false); CheckThisType(t, out tstate); ultstate.ishk = (tstate.ishk) ? true : ultstate.ishk; ultstate.ispt = (tstate.ispt) ? true : ultstate.ispt; ultstate.isbeppl = (tstate.isbeppl) ? true : ultstate.isbeppl; } } } catch (IOException ioe) { Wood.WriteLine("ERROR CHECKING ASSEMBLY TYPE: IOException occured"); Wood.Indent(); Wood.WriteLine(ioe); Wood.Unindent(); } int ftc = 0; if (ultstate.ishk) { ftc++; } if (ultstate.ispt) { ftc++; } if (ultstate.isbeppl) { ftc++; } switch (ftc) { case 0: return(ModType.Unknown); case 3: return(ModType.Invalid); case 2: return((ultstate.ispt) ? ModType.Invalid : ModType.Unknown); case 1: if (ultstate.ishk) { return(ModType.Partmod); } else if (ultstate.ispt) { return(ModType.Patch); } else { return(ModType.BepPlugin); } default: return(ModType.Unknown); } }