public void InjectPatches()
        {
            Patches.ModLoaderPatch loaderPatch = (Patches.ModLoaderPatch)_Patches.Where(p => p.Name == "Mod Loader Patch").FirstOrDefault();
            if (loaderPatch != null)
            {
                _Patches.Remove(loaderPatch);
                _Patches.Insert(0, loaderPatch);
            }

            foreach (Patches.BasePatch P in _Patches)
            {
                try
                {
                    if (P.ShouldInject())
                    {
                        P.Inject();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error Injecting Patch " + P.Name + ": " + ex.Message);
                    Error?.Invoke(this, new StringEventArgs("Error Injecting Patch '" + P.Name + "': " + ex.Message));
                }
            }

            Patches.BasePatch.GameModule.Assembly.Write(PathZ.Combine(_ApplicationPath, "StarshipTheory_Data", "Managed", "Assembly-CSharp.dll"));

            new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StarshipTheory.ModLib.dll")).CopyTo(PathZ.Combine(_ApplicationPath, "StarshipTheory_Data", "Managed", "StarshipTheory.ModLib.dll"), true);
        }
        private void file_executable_TextChanged(object sender, Controls.FileTextboxChangedEventArgs e)
        {
            btnPatch.IsEnabled = false;
            String AppDir = null;

            if (String.IsNullOrEmpty(file_executable.Filename))
            {
                return;
            }

            if (File.Exists(file_executable.Filename))
            {
                if (Directory.Exists(Path.Combine(new FileInfo(file_executable.Filename).Directory.FullName, "StarshipTheory_Data")))
                {
                    AppDir = new FileInfo(file_executable.Filename).Directory.FullName;
                }
            }
            else if (Directory.Exists(file_executable.Filename) && Directory.Exists(Path.Combine(file_executable.Filename, "StarshipTheory_Data")))
            {
                AppDir = file_executable.Filename;
            }

            if (String.IsNullOrEmpty(AppDir))
            {
                MessageBox.Show("Unable to find StarshipTheory_Data directory in the path specified", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                file_executable.Focus();
            }
            else
            {
                btnPatch.IsEnabled       = true;
                btnRestoreGame.IsEnabled = Directory.Exists(PathZ.Combine(AppDir, "StarshipTheory_Data", "Managed", "@BACKUP"));
                injector.ApplicationPath = AppDir;
            }
        }
 private void btnRestoreGame_Click(object sender, RoutedEventArgs e)
 {
     injector.RestoreFiles();
     btnRestoreGame.Dispatcher.Invoke(new Action(() => {
         btnRestoreGame.IsEnabled = Directory.Exists(PathZ.Combine(injector.ApplicationPath, "StarshipTheory_Data", "Managed", "@BACKUP"));
         MessageBox.Show("All files has been restored to their original state", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
     }));
 }
        public void BackupFiles()
        {
            DirectoryInfo BackupDir  = new DirectoryInfo(PathZ.Combine(_ApplicationPath, "StarshipTheory_Data", "Managed", "@BACKUP"));
            DirectoryInfo ManagedDir = new DirectoryInfo(PathZ.Combine(_ApplicationPath, "StarshipTheory_Data", "Managed"));

            if (!BackupDir.Exists)
            {
                BackupDir.Create();

                foreach (FileInfo File in ManagedDir.GetFiles("*.*", SearchOption.TopDirectoryOnly))
                {
                    File.CopyTo(Path.Combine(BackupDir.FullName, File.Name), true);
                }
            }
        }
        public Mono.Cecil.ModuleDefinition GetModuleZ(String DllFilename)
        {
            if (_AssemblyCache.ContainsKey(DllFilename))
            {
                return(_AssemblyCache[DllFilename].MainModule);
            }

            FileInfo DllFile = null;

            if (!Path.IsPathRooted(DllFilename))
            {
                if (File.Exists(Path.Combine(_ApplicationPath, DllFilename)))
                {
                    DllFile = new FileInfo(Path.Combine(_ApplicationPath, DllFilename));
                }
                else if (File.Exists(PathZ.Combine(_ApplicationPath, "StarshipTheory_Data", DllFilename)))
                {
                    DllFile = new FileInfo(PathZ.Combine(_ApplicationPath, "StarshipTheory_Data", DllFilename));
                }
                else if (File.Exists(PathZ.Combine(_ApplicationPath, "StarshipTheory_Data", "Managed", DllFilename)))
                {
                    DllFile = new FileInfo(PathZ.Combine(_ApplicationPath, "StarshipTheory_Data", "Managed", DllFilename));
                }
                else if (File.Exists(PathZ.Combine(AppDomain.CurrentDomain.BaseDirectory, DllFilename)))
                {
                    DllFile = new FileInfo(PathZ.Combine(AppDomain.CurrentDomain.BaseDirectory, DllFilename));
                }
            }
            else if (File.Exists(DllFilename))
            {
                DllFile = new FileInfo(DllFilename);
            }

            if (DllFile == null)
            {
                return(null);
            }

            Mono.Cecil.AssemblyDefinition Def = Mono.Cecil.AssemblyDefinition.ReadAssembly(DllFile.FullName);
            if (Def != null)
            {
                _AssemblyCache[DllFilename] = Def;
                return(Def.MainModule);
            }

            return(null);
        }
        private void Patch()
        {
            injector.RestoreFiles();
            injector.BackupFiles();

            int loaded = injector.LoadPatches(System.Reflection.Assembly.GetExecutingAssembly());

            Console.WriteLine("Loaded " + loaded + " patches");
            injector.InjectPatches();

            btnPatch.Dispatcher.Invoke(new Action(() => {
                Properties.Settings.Default.last_path = injector.ApplicationPath;
                Properties.Settings.Default.Save();

                if (!Directory.Exists(Path.Combine(injector.ApplicationPath, "Mods")))
                {
                    Directory.CreateDirectory(Path.Combine(injector.ApplicationPath, "Mods"));
                }

                if (!injectionError)
                {
                    MessageBox.Show("All patches has been applied!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    if (MessageBox.Show("Patch injection failed.\nWould you like to restore the game to it's original state?", "Patching Failed", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                    {
                        btnRestoreGame_Click(null, null);
                    }
                }

                btnPatch.IsEnabled       = true;
                btnPatch.Content         = "Patch";
                btnRestoreGame.IsEnabled = Directory.Exists(PathZ.Combine(injector.ApplicationPath, "StarshipTheory_Data", "Managed", "@BACKUP"));
            }));
        }
        public int LoadPatches(System.Reflection.Assembly Assembly)
        {
            Patches.BasePatch.Injector     = this;
            Patches.BasePatch.GameModule   = Mono.Cecil.AssemblyDefinition.ReadAssembly(PathZ.Combine(_ApplicationPath, "StarshipTheory_Data", "Managed", "Assembly-CSharp.dll")).MainModule;
            Patches.BasePatch.ModLibModule = Mono.Cecil.AssemblyDefinition.ReadAssembly(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StarshipTheory.ModLib.dll")).MainModule;

            _Patches = new List <Patches.BasePatch>();
            new InjectionHelper(this);

            Type BasePatchType = typeof(Patches.BasePatch);
            int  LoadedPatches = 0;

            foreach (Type PatchType in Assembly.GetTypes().Where(t => t.IsSubclassOf(BasePatchType) && !t.IsInterface && !t.IsAbstract))
            {
                try
                {
                    Patches.BasePatch P = (Patches.BasePatch)Activator.CreateInstance(PatchType);
                    _Patches.Add(P);
                    LoadedPatches++;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error Initializing Patch " + PatchType.Name + ": " + ex.Message);
                    Error?.Invoke(this, new StringEventArgs("Error Initializing Patch '" + PatchType.Name + "': " + ex.Message));
                }
            }

            return(LoadedPatches);
        }