Example #1
0
        private static QMod LoadMod(QMod mod)
        {
            bool flag = mod == null;
            QMod result;

            if (flag)
            {
                result = null;
            }
            else
            {
                bool flag2 = string.IsNullOrEmpty(mod.EntryMethod);
                if (flag2)
                {
                    Console.WriteLine("QMOD ERR: No EntryMethod specified for {0}", mod.Id);
                }
                else
                {
                    try
                    {
                        string[] array = mod.EntryMethod.Split(new char[]
                        {
                            '.'
                        });
                        string     name   = string.Join(".", array.Take(array.Length - 1).ToArray <string>());
                        string     name2  = array[array.Length - 1];
                        MethodInfo method = mod.loadedAssembly.GetType(name).GetMethod(name2);
                        method.Invoke(mod.loadedAssembly, new object[0]);
                    }
                    catch (ArgumentNullException ex)
                    {
                        Console.WriteLine("QMOD ERR: Could not parse EntryMethod {0} for {1}", mod.AssemblyName, mod.Id);
                        Console.WriteLine(ex.InnerException.Message);
                        return(null);
                    }
                    catch (TargetInvocationException ex2)
                    {
                        Console.WriteLine("QMOD ERR: Invoking the specified EntryMethod {0} failed for {1}", mod.EntryMethod, mod.Id);
                        Console.WriteLine(ex2.InnerException.Message);
                        return(null);
                    }
                    catch (Exception ex3)
                    {
                        Console.WriteLine("QMOD ERR: something strange happened");
                        Console.WriteLine(ex3.Message);
                        return(null);
                    }
                }
                result = mod;
            }
            return(result);
        }
Example #2
0
        private static QMod LoadMod(QMod mod)
        {
            Console.WriteLine("QMOD: Loading mod " + mod.DisplayName);
            if (mod == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(mod.EntryMethod))
            {
                Console.WriteLine("QMOD ERR: No EntryMethod specified for {0}", mod.Id);
            }
            else
            {
                try
                {
                    var entryMethodSig = mod.EntryMethod.Split('.');
                    var entryType      = String.Join(".", entryMethodSig.Take(entryMethodSig.Length - 1).ToArray());
                    var entryMethod    = entryMethodSig[entryMethodSig.Length - 1];

                    MethodInfo qPatchMethod = mod.loadedAssembly.GetType(entryType).GetMethod(entryMethod);
                    qPatchMethod.Invoke(mod.loadedAssembly, new object[] { });
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine("QMOD ERR: Could not parse EntryMethod {0} for {1}", mod.AssemblyName, mod.Id);
                    Console.WriteLine(e.InnerException.Message);
                    return(null);
                }
                catch (TargetInvocationException e)
                {
                    Console.WriteLine("QMOD ERR: Invoking the specified EntryMethod {0} failed for {1}", mod.EntryMethod, mod.Id);
                    Console.WriteLine(e.InnerException.Message);
                    return(null);
                }
                catch (Exception e)
                {
                    Console.WriteLine("QMOD ERR: something strange happened");
                    Console.WriteLine(e.Message);
                    return(null);
                }
            }
            Console.WriteLine("QMOD: Finished loading mod " + mod.DisplayName);
            return(mod);
        }
Example #3
0
        public static void Patch()
        {
            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
            {
                var allDlls = new DirectoryInfo(qModBaseDir).GetFiles("*.dll", SearchOption.AllDirectories);
                foreach (var dll in allDlls)
                {
                    Console.WriteLine(Path.GetFileNameWithoutExtension(dll.Name) + " " + args.Name);
                    if (args.Name.Contains(Path.GetFileNameWithoutExtension(dll.Name)))
                    {
                        return(Assembly.LoadFrom(dll.FullName));
                    }
                }

                return(null);
            };

            if (patched)
            {
                return;
            }

            patched = true;

            if (!Directory.Exists(qModBaseDir))
            {
                Console.WriteLine("QMOD ERR: QMod directory was not found");
                Directory.CreateDirectory(qModBaseDir);
                Console.WriteLine("QMOD INFO: Creaated QMod directory at {0}", qModBaseDir);
                return;
            }

            var subDirs   = Directory.GetDirectories(qModBaseDir);
            var lastMods  = new List <QMod>();
            var firstMods = new List <QMod>();
            var otherMods = new List <QMod>();

            foreach (var subDir in subDirs)
            {
                var jsonFile = Path.Combine(subDir, "mod.json");

                if (!File.Exists(jsonFile))
                {
                    Console.WriteLine("QMOD ERR: Mod is missing a mod.json file");
                    File.WriteAllText(jsonFile, JsonConvert.SerializeObject(new QMod()));
                    Console.WriteLine("QMOD INFO: A template for mod.json was generated at {0}", jsonFile);
                    continue;
                }

                QMod mod = QMod.FromJsonFile(Path.Combine(subDir, "mod.json"));

                if (mod.Equals(null)) // QMod.FromJsonFile will throw parser errors
                {
                    continue;
                }

                if (mod.Enable.Equals(false))
                {
                    Console.WriteLine("QMOD WARN: {0} is disabled via config, skipping", mod.DisplayName);
                    continue;
                }

                var modAssemblyPath = Path.Combine(subDir, mod.AssemblyName);

                if (!File.Exists(modAssemblyPath))
                {
                    Console.WriteLine("QMOD ERR: No matching dll found at {0} for {1}", modAssemblyPath, mod.Id);
                    continue;
                }

                mod.loadedAssembly  = Assembly.LoadFrom(modAssemblyPath);
                mod.modAssemblyPath = modAssemblyPath;

                if (mod.Priority.Equals("Last"))
                {
                    lastMods.Add(mod);
                    continue;
                }
                else if (mod.Priority.Equals("First"))
                {
                    firstMods.Add(mod);
                    continue;
                }
                else
                {
                    otherMods.Add(mod);
                    continue;
                }
            }

            foreach (var mod in firstMods)
            {
                if (mod != null)
                {
                    loadedMods.Add(LoadMod(mod));
                }
            }

            foreach (var mod in otherMods)
            {
                if (mod != null)
                {
                    loadedMods.Add(LoadMod(mod));
                }
            }

            foreach (var mod in lastMods)
            {
                if (mod != null)
                {
                    loadedMods.Add(LoadMod(mod));
                }
            }
        }
Example #4
0
        public static void Patch()
        {
            AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs args)
            {
                FileInfo[] files = new DirectoryInfo(QModPatcher.qModBaseDir).GetFiles("*.dll", SearchOption.AllDirectories);
                foreach (FileInfo fileInfo in files)
                {
                    Console.WriteLine(Path.GetFileNameWithoutExtension(fileInfo.Name) + " " + args.Name);
                    bool flag12 = args.Name.Contains(Path.GetFileNameWithoutExtension(fileInfo.Name));
                    if (flag12)
                    {
                        return(Assembly.LoadFrom(fileInfo.FullName));
                    }
                }
                return(null);
            };
            bool flag = QModPatcher.patched;

            if (!flag)
            {
                QModPatcher.patched = true;
                bool flag2 = !Directory.Exists(QModPatcher.qModBaseDir);
                if (flag2)
                {
                    Console.WriteLine("QMOD ERR: QMod directory was not found");
                    Directory.CreateDirectory(QModPatcher.qModBaseDir);
                    Console.WriteLine("QMOD INFO: Created QMod directory at {0}", QModPatcher.qModBaseDir);
                }
                else
                {
                    string[]    directories = Directory.GetDirectories(QModPatcher.qModBaseDir);
                    List <QMod> list        = new List <QMod>();
                    List <QMod> list2       = new List <QMod>();
                    List <QMod> list3       = new List <QMod>();
                    foreach (string path in directories)
                    {
                        string text  = Path.Combine(path, "mod.json");
                        bool   flag3 = !File.Exists(text);
                        if (flag3)
                        {
                            Console.WriteLine("QMOD ERR: Mod is missing a mod.json file");
                            File.WriteAllText(text, JsonConvert.SerializeObject(new QMod()));
                            Console.WriteLine("QMOD INFO: A template for mod.json was generated at {0}", text);
                        }
                        else
                        {
                            QMod qmod  = QMod.FromJsonFile(Path.Combine(path, "mod.json"));
                            bool flag4 = qmod.Equals(null);
                            if (!flag4)
                            {
                                bool flag5 = qmod.Enable.Equals(false);
                                if (flag5)
                                {
                                    Console.WriteLine("QMOD WARN: {0} is disabled via config, skipping", qmod.DisplayName);
                                }
                                else
                                {
                                    string text2 = Path.Combine(path, qmod.AssemblyName);
                                    bool   flag6 = !File.Exists(text2);
                                    if (flag6)
                                    {
                                        Console.WriteLine("QMOD ERR: No matching dll found at {0} for {1}", text2, qmod.Id);
                                    }
                                    else
                                    {
                                        qmod.loadedAssembly  = Assembly.LoadFrom(text2);
                                        qmod.modAssemblyPath = text2;
                                        bool flag7 = qmod.Priority.Equals("Last");
                                        if (flag7)
                                        {
                                            list.Add(qmod);
                                        }
                                        else
                                        {
                                            bool flag8 = qmod.Priority.Equals("First");
                                            if (flag8)
                                            {
                                                list2.Add(qmod);
                                            }
                                            else
                                            {
                                                list3.Add(qmod);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    foreach (QMod qmod2 in list2)
                    {
                        bool flag9 = qmod2 != null;
                        if (flag9)
                        {
                            QModPatcher.loadedMods.Add(QModPatcher.LoadMod(qmod2));
                        }
                    }
                    foreach (QMod qmod3 in list3)
                    {
                        bool flag10 = qmod3 != null;
                        if (flag10)
                        {
                            QModPatcher.loadedMods.Add(QModPatcher.LoadMod(qmod3));
                        }
                    }
                    foreach (QMod qmod4 in list)
                    {
                        bool flag11 = qmod4 != null;
                        if (flag11)
                        {
                            QModPatcher.loadedMods.Add(QModPatcher.LoadMod(qmod4));
                        }
                    }
                }
            }
        }