Example #1
0
        internal void Remove()
        {
            try
            {
                if (!IsInjected())
                {
                    Console.WriteLine("Tried to uninstall, but patch was not present");
                    Console.WriteLine("Skipping uninstallation");
                    Console.WriteLine();
                    Console.WriteLine("Trying to disable Unity sound...");

                    AudioFixer.ChangeDisableUnityAudio(globalgamemanagers, true, Executable.game);

                    Console.WriteLine("Unity sound disabled successfully");
                    Environment.Exit(0);
                }

                // Remove backup file if it exists
                string backupFilePath = Path.Combine(managedDirectory, "Assembly-CSharp.qoriginal.dll");
                if (File.Exists(backupFilePath))
                {
                    File.Delete(backupFilePath);
                }

                AssemblyDefinition game = AssemblyDefinition.ReadAssembly(mainFilename);

                TypeDefinition   gameInputDef = game.MainModule.GetType("GameInput");
                MethodDefinition awakeMethod  = gameInputDef.Methods.First(x => x.Name == "Awake");

                Instruction patchMethodCall = null;

                foreach (Instruction instruction in awakeMethod.Body.Instructions)
                {
                    if (instruction.OpCode == OpCodes.Call && instruction.Operand.ToString().Equals("System.Void QModInstaller.QModPatcher::Patch()"))
                    {
                        patchMethodCall = instruction;
                    }
                }

                if (patchMethodCall != null)
                {
                    awakeMethod.Body.GetILProcessor().Remove(patchMethodCall);
                }
                else
                {
                    Console.WriteLine("An unexpected error has occurred.");
                    Console.WriteLine("The patch method couldn't be found. Was it even injected?");
                    Console.WriteLine();
                    Console.WriteLine("Press any key to exit");
                    Console.ReadKey();
                    Environment.Exit(3);
                }

                game.Write(mainFilename);

                Console.WriteLine("QModManager was uninstalled successfully");
                Console.WriteLine();
                Console.WriteLine("Trying to disable Unity sound...");

                AudioFixer.ChangeDisableUnityAudio(globalgamemanagers, true, Executable.game);

                Console.WriteLine("Unity sound disabled successfully");
                Environment.Exit(0);
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION CAUGHT!");
                Console.WriteLine(e.ToString());
                Console.WriteLine();
                Console.ReadKey();
                Environment.Exit(2);
            }
        }
Example #2
0
        internal static void Main(string[] args)
        {
            try
            {
                Dictionary <string, string> parsedArgs = new Dictionary <string, string>();

                foreach (string arg in args)
                {
                    if (arg.Contains("="))
                    {
                        parsedArgs = args.Select(s => s.Split(new[] { '=' }, 1)).ToDictionary(s => s[0], s => s[1]);
                    }
                    else if (arg == "-i")
                    {
                        action = Action.Install;
                    }
                    else if (arg == "-u")
                    {
                        action = Action.Uninstall;
                    }
                }

                string managedDirectory   = Environment.CurrentDirectory;
                string globalgamemanagers = Path.Combine(managedDirectory, "../globalgamemanagers");

                if (!File.Exists(Path.Combine(managedDirectory, "Assembly-CSharp.dll")))
                {
                    Console.WriteLine("Could not find the assembly file.");
                    Console.WriteLine("Please make sure you have installed QModManager in the right folder.");
                    Console.WriteLine("If the problem persists, open a bug report on NexusMods or an issue on GitHub");
                    Console.WriteLine();
                    Console.WriteLine("Press any key to exit...");
                    Console.ReadKey();
                    Environment.Exit(1);
                }

                GetInfo(out os, out string directory, out game);

                Injector injector;

                if (os == OS.Both)
                {
                    // This runs if both windows and mac files were detected, but it should NEVER happen.
                    Console.WriteLine("An unexpected error has occurred.");
                    Console.WriteLine("Both Windows and Mac files detected!");
                    Console.WriteLine("Is this a Windows or a Mac environment?");
                    Console.WriteLine();
                    Console.WriteLine("Press any key to exit...");
                    Console.ReadKey();
                    Environment.Exit(1);
                    return;
                }
                else if (os == OS.Windows || os == OS.Mac)
                {
                    injector = new Injector(directory, managedDirectory);
                }
                else
                {
                    Console.WriteLine("Could not find any game to patch!");
                    Console.WriteLine("An assembly file was found, but no executable was detected.");
                    Console.WriteLine("Please make sure you have installed QModManager in the right folder.");
                    Console.WriteLine("If the problem persists, open a bug report on NexusMods or an issue on GitHub");
                    Console.WriteLine();
                    Console.WriteLine("Press any key to exit...");
                    Console.ReadKey();
                    Environment.Exit(1);
                    return;
                }

                bool isInjected = injector.IsInjected();

                if (action == Action.Install)
                {
                    if (!isInjected)
                    {
                        Console.WriteLine("Installing QModManager...");
                        injector.Inject();
                    }
                    else
                    {
                        Console.WriteLine("QModManager is already installed!");
                        Console.WriteLine("Skipping installation");
                        Console.WriteLine();
                        Console.WriteLine("Trying to enable Unity sound...");

                        AudioFixer.ChangeDisableUnityAudio(globalgamemanagers, false, game);

                        Console.WriteLine("Unity sound enabled successfully");
                        Environment.Exit(0);
                    }
                }
                else if (action == Action.Uninstall)
                {
                    if (isInjected)
                    {
                        Console.WriteLine("Uninstalling QModManager...");
                        injector.Remove();
                    }
                    else
                    {
                        Console.WriteLine("QModManager is already uninstalled!");
                        Console.WriteLine("Skipping uninstallation");
                        Console.WriteLine();
                        Console.WriteLine("Trying to disable Unity sound...");

                        AudioFixer.ChangeDisableUnityAudio(globalgamemanagers, true, game);

                        Console.WriteLine("Unity sound disabled successfully");
                        Environment.Exit(0);
                    }
                }
                else
                {
                    if (!isInjected)
                    {
                        Console.Write("No patch detected, install? [Y/N] > ");
                        ConsoleKey key = Console.ReadKey().Key;
                        Console.WriteLine();
                        if (key == ConsoleKey.Y)
                        {
                            Console.WriteLine("Installing QModManager...");
                            injector.Inject();
                        }
                        else if (key == ConsoleKey.N)
                        {
                            Console.WriteLine("Press any key to exit...");
                            Console.ReadKey();
                            Environment.Exit(0);
                        }
                    }
                    else
                    {
                        Console.Write("Patch installed, remove? [Y/N] > ");
                        ConsoleKey key = Console.ReadKey().Key;
                        Console.WriteLine();
                        if (key == ConsoleKey.Y)
                        {
                            Console.Write("Uninstalling QModManager...");
                            injector.Remove();
                        }
                        else if (key == ConsoleKey.N)
                        {
                            Console.WriteLine("Press any key to exit...");
                            Console.ReadKey();
                            Environment.Exit(0);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION CAUGHT!");
                Console.WriteLine(e.ToString());
                Console.WriteLine();
                Console.ReadKey();
                Environment.Exit(2);
            }
        }
Example #3
0
        internal void Inject()
        {
            try
            {
                if (IsInjected())
                {
                    Console.WriteLine("Tried to install, but it was already injected");
                    Console.WriteLine("Skipping installation");
                    Console.WriteLine();
                    Console.WriteLine("Trying to enable Unity sound...");

                    AudioFixer.ChangeDisableUnityAudio(globalgamemanagers, false, Executable.game);

                    Console.WriteLine("Unity sound enabled successfully");
                    Environment.Exit(0);
                }

                // Remove backup file if it exists
                string backupFilePath = Path.Combine(managedDirectory, "Assembly-CSharp.qoriginal.dll");
                if (File.Exists(backupFilePath))
                {
                    File.Delete(backupFilePath);
                }

                AssemblyDefinition game = AssemblyDefinition.ReadAssembly(mainFilename);

                AssemblyDefinition installer   = AssemblyDefinition.ReadAssembly(installerFilename);
                MethodDefinition   patchMethod = installer.MainModule.GetType("QModInstaller.QModPatcher").Methods.First(x => x.Name == "Patch");

                TypeDefinition   type   = game.MainModule.GetType("GameInput");
                MethodDefinition method = type.Methods.Single(x => x.Name == "Awake");

                method.Body.GetILProcessor().InsertBefore(method.Body.Instructions[0], Instruction.Create(OpCodes.Call, method.Module.Import(patchMethod)));

                game.Write(mainFilename);

                string qmodsDirectory = Path.Combine(gameDirectory, "QMods");

                if (!Directory.Exists(qmodsDirectory))
                {
                    Directory.CreateDirectory(qmodsDirectory);
                }

                Console.WriteLine("QModManager installed successfully");
                Console.WriteLine();
                Console.WriteLine("Trying to enable Unity sound...");

                AudioFixer.ChangeDisableUnityAudio(globalgamemanagers, false, Executable.game);

                Console.WriteLine("Unity sound enabled successfully");
                Environment.Exit(0);
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION CAUGHT!");
                Console.WriteLine(e.ToString());
                Console.WriteLine();
                Console.ReadKey();
                Environment.Exit(1);
            }
        }