Ejemplo n.º 1
0
 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.");
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Restores active savefile from a given backup; aborts if active savefile is not empty.
 /// </summary>
 /// <param name="backup"><see cref="UserDataStateRelay"/> to be cloned.</param>
 /// <returns><c>true</c> if the operation was successful; <c>false</c> otherwise.</returns>
 public static bool RestoreActiveSaveFromBackup(UserDataStateRelay backup)
 {
     if (backup.Location == ActiveSave?.Location)
     {
         Wood.WriteLine("Can not copy active save into itself!"); return(false);
     }
     if (ActiveSave?.CurrState != UserDataStateRelay.UDSRState.Empty)
     {
         Wood.WriteLine("Active save not empty, will not overwrite!"); return(false);
     }
     ;
     try
     {
         Wood.WriteLine("Restoring save from backup...");
         ActiveSave = backup.CloneTo(UserDataFolder);
         Wood.WriteLine("Backup restore successful.");
         return(true);
     }
     catch (NullReferenceException ne)
     {
         Wood.WriteLine("ERROR RESTORING A SAVEFILE BACKUP:");
         Wood.WriteLine(ne, 1);
     }
     return(true);
 }
Ejemplo n.º 3
0
 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;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Recursively moves a directory and all its contents into a new location.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns>Number of IO errors encountered during the operation. </returns>
        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.WriteLine(ioe, 1);
                    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.WriteLine(ioe, 1);
                }
            }
            return(errc);
        }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 7
0
        /// <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);
            }
        }
Ejemplo n.º 8
0
 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();
     }
 }
Ejemplo n.º 9
0
        public static int TryLoadCargo(DirectoryInfo target)
        {
            var start = DateTime.UtcNow;

            currentSourceDir = target;
            cargo.Clear();
            if (!target.Exists)
            {
                return(-1);
            }
            var errcount = 0;

            foreach (var file in target.GetFiles("*.dll", SearchOption.TopDirectoryOnly))
            {
                try
                {
                    var mr = new ModRelay(file);
                    if (mr.AssociatedModData == null)
                    {
                        throw new ArgumentNullException("NULL MOD DATA! something went wrong in ModRelay ctor");
                    }
                    cargo.Add(mr);
                }
                catch (Exception e) { errcount++; Wood.WriteLine("Error checking mod entry:"); Wood.WriteLine(e); }
            }
            Wood.WriteLine($"Sync loading complete. Time elapsed: {DateTime.UtcNow - start}");
            return(errcount);
        }
Ejemplo n.º 10
0
        public static void FetchList()
        {
            using (var wc = new WebClient())
            {
                try
                {
                    string euv_json = wc.DownloadString("https://beestuff.pythonanywhere.com/audb/api/v2/enduservisible");
                    var    list     = JsonConvert.DeserializeObject <List <AUDBEntryRelay> >(euv_json);
                    EntryList = list;
                }

                catch (WebException we) { Wood.WriteLine("Error fetching AUDB entries:");  Wood.WriteLine(we.Response, 1); }
            }
        }
Ejemplo n.º 11
0
 public static EUModType GetModType(string path)
 {
     try
     {
         using ModuleDefinition md = ModuleDefinition.ReadModule(path);
         return(GetModType(md));
     }
     catch (Exception e)
     {
         Wood.WriteLine($"ERROR CHECKING MODTYPE FOR {path}");
         Wood.WriteLine(e);
         return(EUModType.Unknown);
     }
 }
Ejemplo n.º 12
0
        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());
        }
Ejemplo n.º 13
0
 public static void WriteConfig()
 {
     try
     {
         Wood.WriteLine($"Saving config: {BlepOut.cfgpath}");
         File.WriteAllText(BlepOut.cfgpath, confjo.ToString());
     }
     catch (NullReferenceException)
     {
         Wood.WriteLine("Can not save config: nothing to write");
     }
     catch (Exception e)
     {
         Wood.WriteLine("ERROR SERIALIZING BOI CONFIG FILE:");
         Wood.WriteLine(e, 1);
     }
 }
Ejemplo n.º 14
0
 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();
     }
 }
Ejemplo n.º 15
0
 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);
     }
 }
Ejemplo n.º 16
0
 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();
     }
 }
Ejemplo n.º 17
0
 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();
         }
     }
 }
Ejemplo n.º 18
0
 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.WriteLine(joe, 1);
     }
     catch (Exception e)
     {
         Wood.WriteLine("ERROR OPENING BOI CONFIG FILE:");
         Wood.WriteLine(e, 1);
     }
 }
Ejemplo n.º 19
0
        public static int TryLoadCargoAsync(DirectoryInfo target)
        {
            Wood.WriteLine($"Attempting to load cargo from {target}.");
            currentSourceDir = target;
            cargo.Clear();
            if (!target.Exists)
            {
                return(-1);
            }
            Wood.WriteLine("Path valid. ");
            var start    = DateTime.UtcNow;
            var tasklist = new List <Task <ModRelay> >();

            foreach (var file in target.GetFiles("*.dll", SearchOption.TopDirectoryOnly))
            {
                var nt = new Task <ModRelay>(() => (ModRelay)Activator.CreateInstance(typeof(ModRelay), file));
                nt.Start();
                tasklist.Add(nt);
            }
            Task.WaitAll(tasklist.ToArray());
            var errc = 0;

            foreach (var t in tasklist)
            {
                if (t.Exception != null)
                {
                    Wood.WriteLine($"Unhandled exception during creation of a ModRelay: {t.Exception}");
                    errc++;
                    continue;
                }
                if (t.Result.AssociatedModData == null)
                {
                    Wood.WriteLine($"Empty mod data: something went wrong in ModRelay ctor for {t.Result.AssociatedModData.OrigLocation}");
                    errc++;
                    continue;
                }
                cargo.Add(t.Result);
            }
            Wood.WriteLine($"Loading complete. Time elapsed {DateTime.UtcNow - start}");
            return(errc);
        }
Ejemplo n.º 20
0
 public bool TryDownload(string rootpath)
 {
     if (rootpath == null)
     {
         throw new ArgumentNullException();
     }
     try
     {
         var tdi = new DirectoryInfo(rootpath);
         if (!tdi.Exists)
         {
             tdi.Create();
         }
         return(mod.TryDownload(Path.Combine(rootpath, path)));
     }
     catch (Exception e)
     {
         Wood.WriteLine("Unhandled error downloading beppart:");
         Wood.WriteLine(e, 1);
         return(false);
     }
 }
Ejemplo n.º 21
0
        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);
            }
        }
Ejemplo n.º 22
0
 /// <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);
 }
Ejemplo n.º 23
0
 /// <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);
 }
Ejemplo n.º 24
0
            public bool TryDownload(string TargetDirectory)
            {
                using (var dwc = new WebClient())
                {
                    try
                    {
                        var mcts = dwc.DownloadData(download);
                        var sha  = new SHA512Managed();

                        var modhash  = sha.ComputeHash(mcts);
                        var sigbytes = Convert.FromBase64String(sig);
                        var keyData  = new RSAParameters();
                        keyData.Exponent = Convert.FromBase64String(key["e"]);
                        keyData.Modulus  = Convert.FromBase64String(key["n"]);
                        var rsa = RSA.Create();
                        rsa.ImportParameters(keyData);
                        var def = new RSAPKCS1SignatureDeformatter(rsa);
                        def.SetHashAlgorithm("SHA512");

                        if (def.VerifySignature(modhash, sigbytes))
                        {
                            Wood.WriteLine($"Mod sig verified: {this.name}, saving");
                            try
                            {
                                var tfi = new DirectoryInfo(TargetDirectory);
                                if (!tfi.Exists)
                                {
                                    tfi.Create(); tfi.Refresh();
                                }
                                File.WriteAllBytes(Path.Combine(TargetDirectory, $"{this.name}.dll"), mcts);
                                if (deps.Count > 0)
                                {
                                    Wood.WriteLine("");
                                    foreach (var dep in deps)
                                    {
                                        if (dep.TryDownload(TargetDirectory))
                                        {
                                        }
                                    }
                                }
                            }
                            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);
                    }
                    finally
                    {
                    }
                }
                return(false);
            }