public void loadPatches(TypeDefinitionCollection types)
        {
            //get Patches
            patches.Add(new PatchUpdater(types));
            patches.Add(new PatchPopups(types));
            //patches.Add(new PatchOffline(types));

            PatchSettingsMenu settingsMenuHook = new PatchSettingsMenu(types);

            publicAPI.setSceneCallback(settingsMenuHook);
            patches.Add(settingsMenuHook);

            PatchModsMenu modMenuHook = new PatchModsMenu(types, this);

            modMenuHook.Initialize(publicAPI);
            patches.Add(modMenuHook);

            //add Hooks
            addPatchHooks();
        }
 public void setSceneCallback(PatchSettingsMenu patch)
 {
     sceneHandler = patch;
 }
        public void loadPatches(TypeDefinitionCollection types)
        {
            //get Patches
            patches.Add (new PatchUpdater (types));
            patches.Add (new PatchPopups (types));
            //patches.Add(new PatchOffline(types));

            PatchSettingsMenu settingsMenuHook = new PatchSettingsMenu (types);
            publicAPI.setSceneCallback (settingsMenuHook);
            patches.Add (settingsMenuHook);

            PatchModsMenu modMenuHook = new PatchModsMenu (types, this);
            modMenuHook.Initialize (publicAPI);
            patches.Add (modMenuHook);

            //add Hooks
            addPatchHooks ();
        }
 public void setSceneCallback(PatchSettingsMenu patch)
 {
     sceneHandler = patch;
 }
        public ModLoader()
        {
            //System.Diagnostics.StackTrace t = new System.Diagnostics.StackTrace();
            //Console.WriteLine (t);

            String installPath = Platform.getGlobalScrollsInstallPath();
            String modLoaderPath = installPath + "ModLoader" + System.IO.Path.DirectorySeparatorChar;
            bool repatchNeeded = false;

            //load mod list
            if (!File.Exists (modLoaderPath+"mods.ini")) {
                File.CreateText (modLoaderPath+"mods.ini").Close();
                //first launch, set hooks for patches
                repatchNeeded = true;
            }
            modList = File.ReadAllLines (modLoaderPath+"mods.ini").ToList();

            //match it with mods avaiable
            if (!Directory.Exists (modLoaderPath+"mods"))
                Directory.CreateDirectory (modLoaderPath+"mods");
            String[] folderList = (from subdirectory in Directory.GetDirectories(modLoaderPath+"mods")
                                    where Directory.GetFiles(subdirectory, "*.mod.dll").Length != 0 ||
                                   		  Directory.GetFiles(subdirectory, "*.Mod.dll").Length != 0
                                    select subdirectory).ToArray();

            ResolveEventHandler resolver = new ResolveEventHandler(CurrentDomainOnAssemblyResolve);
            AppDomain.CurrentDomain.AssemblyResolve += resolver;

            //load mods
            foreach (String folder in folderList) {
                try {
                    String[] modFiles = Directory.GetFiles (folder, "*.mod.dll");
                    if (Platform.getOS() != Platform.OS.Win) modFiles.Concat(Directory.GetFiles (folder, "*.Mod.dll")).ToArray();
                    foreach (String modFile in modFiles) {
                        Assembly mod = Assembly.LoadFile(modFile);
                        Type[] modClasses = (from modClass in mod.GetTypes ()
                                             where modClass.InheritsFrom (typeof(ScrollsModLoader.Interfaces.BaseMod))
                                             select modClass).ToArray();
                        foreach (Type modClass in modClasses) {
                            modInstances.Add((BaseMod)(modClass.GetConstructor (Type.EmptyTypes).Invoke (new object[0])));
                            //Console.WriteLine("added mod");
                        }
                    }
                } catch (ReflectionTypeLoadException exp) {
                    Console.WriteLine(exp.ToString());
                }
            }

            //check mod list
            List<BaseMod> modInstancesCpy = new List<BaseMod>(modInstances);
            foreach (BaseMod mod in modInstancesCpy) {
                if (!modList.Contains (mod.GetName()+"."+mod.GetVersion())) {
                    modList.Add (mod.GetName()+"."+mod.GetVersion());
                    repatchNeeded = true;
                    break;
                } else {
                    //re-sort for mod order calling
                    modInstances.Remove (mod);
                    modInstances.Insert(modList.IndexOf(mod.GetName()+"."+mod.GetVersion()), mod);
                }
            }

            TypeDefinitionCollection types = AssemblyFactory.GetAssembly (modLoaderPath+"Assembly-CSharp.dll").MainModule.Types;
            TypeDefinition[] typeArray = new TypeDefinition[types.Count];
            types.CopyTo(typeArray, 0);

            //get ModAPI
            APIHandler api = new APIHandler ();

            //add Patches
            patches.Add(new PatchUpdater(types));
            //patches.Add(new PatchOffline(types));

            PatchSettingsMenu settingsMenuHook = new PatchSettingsMenu (types);
            api.setSceneCallback (settingsMenuHook);
            patches.Add (settingsMenuHook);

            PatchModsMenu modMenuHook = new PatchModsMenu (types);
            modMenuHook.Initialize (api);
            patches.Add (modMenuHook);

            publicAPI = api;

            foreach (BaseMod mod in modInstances) {
                mod.Initialize (publicAPI);
            }

            //we are loaded, get the hooks
            foreach (BaseMod mod in modInstancesCpy) {

                MethodDefinition[] requestedHooks = new MethodDefinition[] { };

                bool hooksAreValid = true;
                try {
                    requestedHooks = mod.GetHooks (types, SharedConstants.getGameVersion());
                } catch {
                    modInstances.Remove (mod);
                    hooksAreValid = false;
                }

                //check hooks
                foreach (MethodDefinition hook in requestedHooks) {
                    //type does not exists
                    if ((from type in typeArray
                         where type.Equals(hook.DeclaringType)
                         select type).Count() == 0) {
                        //disable mod
                        modInstances.Remove (mod);
                        hooksAreValid = false;
                        break;
                    }
                }
                if (!hooksAreValid)
                    continue;

                //add hooks
                foreach (MethodDefinition hook in requestedHooks) {
                    ScrollsFilter.AddHook(hook);
                }
            }

            //remove old mods from list
            foreach (String modDesc in modList.ToArray()) {
                bool loadedModFound = false;
                foreach (BaseMod mod in modInstances) {
                    if (modDesc.Equals (mod.GetName()+"."+mod.GetVersion()))
                        loadedModFound = true;
                }
                if (!loadedModFound)
                    modList.Remove (modDesc);
            }

            //save ModList
            File.Delete (modLoaderPath+"mods.ini");
            StreamWriter modListWriter = File.CreateText (modLoaderPath+"mods.ini");
            foreach (String modDesc in modList) {
                modListWriter.WriteLine (modDesc);
            }
            modListWriter.Flush ();
            modListWriter.Close ();

            //repatch if necessary
            if (repatchNeeded) {

                Patcher patcher = new Patcher ();
                if (!patcher.patchAssembly ()) {
                    //normal patching should never fail at this point
                    //because this is no update and we are already patched
                    //TO-DO get hook that crashed the patching and deactive mod instead
                    //No idea how to do that correctly however
                    Dialogs.showNotification ("Scrolls is broken", "Your Scrolls install appears to be broken or modified by other tools. ModLoader failed to load and will de-install itself");
                    File.Delete(installPath+"Assembly-CSharp.dll");
                    File.Copy(installPath+"ModLoader"+ System.IO.Path.DirectorySeparatorChar +"Assembly-CSharp.dll", installPath+"Assembly-CSharp.dll");
                    Application.Quit();
                }

                //restart the game
                if (Platform.getOS() == Platform.OS.Win)
                {
                    Console.WriteLine(Platform.getGlobalScrollsInstallPath() + "..\\..\\Scrolls.exe");
                    //try {
                        new Process { StartInfo = { FileName = Platform.getGlobalScrollsInstallPath() + "..\\..\\Scrolls.exe", Arguments = "" } }.Start();
                    //} catch {}
                    Application.Quit();
                }
                else if (Platform.getOS() == Platform.OS.Mac)
                {
                    new Process { StartInfo = { FileName = Platform.getGlobalScrollsInstallPath() + "/../../../../../run.sh", Arguments = "", UseShellExecute=true } }.Start();
                    Application.Quit();
                } else {
                    Application.Quit();
                }
            }
        }