/// <summary> /// <para>Called from within the game's code. Notifying all enabled mods that it can draw custom gui elements</para> /// <para>Do not call this manually</para> /// </summary> public void __OnGui() { if (_FirstPass) { _FirstPass = false; _debugWindow = new GUI.Window(ModGUI.GetWindowIndex(), "Mod Debug") { Visible = false, IsDraggable = true, IsResizeable = true }; _debugWindow.Rect = new UnityEngine.Rect((UnityEngine.Screen.width - 400) / 2, (UnityEngine.Screen.height - 400) / 2, 400, 400); _debugWindow.Items.Add(new GUI.TextArea() { IsRichText = true, IsEditable = false }); GUI.Button closeDebugBtn = new GUI.Button("Close"); closeDebugBtn.Clicked += CloseDebugBtn_Clicked; _debugWindow.Items.Add(new GUI.FlexibleSpace()); _debugWindow.Items.Add(closeDebugBtn); if (_errorBeforeLoad.Count > 0) { ShowError(_errorBeforeLoad[0].ModName, _errorBeforeLoad[0].ModVersion, _errorBeforeLoad[0].Error, _errorBeforeLoad[0].Where); } } _debugWindow.__Draw(); if (ManagerMenu.mainMenuActive) { if (!_ModAreaResized) { _ModAreaResized = true; } _ModListButtonArea.__Draw(); } }
/// <summary> /// Register every Mod in the Mods directory /// </summary> /// <param name="ModsFolder"></param> private void RegisterMods(String ModsFolder) { Type AbstractModType = typeof(Mod); System.IO.DirectoryInfo ModsFolderInfo = new System.IO.DirectoryInfo(ModsFolder); List <Mod> PreliminaryMods = new List <Mod>(); foreach (System.IO.DirectoryInfo modDir in ModsFolderInfo.GetDirectories("*", System.IO.SearchOption.TopDirectoryOnly)) { if (System.IO.File.Exists(System.IO.Path.Combine(modDir.FullName, "Mod.json"))) { try { String modInfoJson = System.IO.File.ReadAllText(System.IO.Path.Combine(modDir.FullName, "Mod.json")); ModInfo modInfo = Pathfinding.Serialization.JsonFx.JsonReader.Deserialize <ModInfo>(modInfoJson); if (modInfo == null) { throw new Exception("Invalid Mod.json file"); } try { String dllFile = System.IO.Path.Combine(modDir.FullName, modInfo.EntryDLL); if (String.IsNullOrEmpty(modInfo.EntryDLL) || !System.IO.File.Exists(dllFile)) { throw new System.IO.FileNotFoundException("Missing EntryDLL file"); } System.Reflection.Assembly ModAssembly = System.Reflection.Assembly.LoadFile(dllFile); bool ModTypeFound = false; foreach (Type T in ModAssembly.GetTypes().Where(t => t.IsSubclassOf(AbstractModType) && t.IsPublic && !t.IsInterface && !t.IsAbstract)) { Mod M = (Mod)Activator.CreateInstance(T); if (M != null) { M.Info = modInfo; M.ModFolder = modDir.FullName; M._Logger = new UnityEngine.Logger(new ModLoggerHandler(M)); PreliminaryMods.Add(M); ModTypeFound = true; } } if (!ModTypeFound) { UnityEngine.Debug.LogWarning("No mod found in file " + dllFile + " but it's designated as a mod"); } } catch (Exception innerEx) { ShowError(modInfo.Name, modInfo.Version.ToString(), innerEx, "Load"); UnityEngine.Debug.LogError("Failed to initialize mod from folder " + modInfo.Name + ": " + innerEx.Message); } } catch (Exception ex) { ShowError(modDir.Name, "", ex, "Load"); UnityEngine.Debug.LogError("Failed to initialize mod from folder " + modDir.Name + ": " + ex.Message); } } } foreach (Mod M in PreliminaryMods) { foreach (ModDependency dep in M.Info.Dependencies) { bool has = false; foreach (Mod N in PreliminaryMods) { if (!N.Enabled) { continue; } if (dep.Name == N.Info.Name) { has = true; if (dep.MinimumVersion != null && dep.MinimumVersion > N.Info.Version) { has = false; } else if (dep.MaximumVersion != null && dep.MaximumVersion < N.Info.Version) { has = false; } } } if (!has) { UnityEngine.Debug.LogWarning("Mod " + M.Info.Name + " is missing dependency " + dep.Name); M.Enabled = false; break; } } if (M.Enabled) { _Mods.Add(M); } } foreach (Mod M in _Mods) { M.ModWindow = new GUI.Window(ModGUI.GetWindowIndex(), M.Info.DisplayName) { Visible = false, IsDraggable = true, IsResizeable = true, _drawingMod = M, MinWidth = 200, MaxWidth = UnityEngine.Screen.width, MinHeight = 20, MaxHeight = UnityEngine.Screen.height }; GUI.Button modBtn = new GUI.Button(M.Info.DisplayName) { Tag = M, Visible = false }; modBtn.Clicked += ModBtn_Clicked; _ModListButtonArea.Items.Add(modBtn); M.OnInitialize(); } }