private void StateManager_OnStateChange(StateManager.StateTypes state) { if (state == StateManager.StateTypes.Start) { StateManager.OnStateChange -= StateManager_OnStateChange; SetCursor(true); } }
public void StateManager_OnStateChange(StateManager.StateTypes state) { if (state == StateManager.StateTypes.Start) { alreadyAtStartMenuState = true; Init(); InvokeModLoaders(state); } else if (state == StateManager.StateTypes.Game) { alreadyAtStartMenuState = true; InvokeModLoaders(state); StateManager.OnStateChange -= StateManager_OnStateChange; } }
/// <summary> /// Returns a list of any valid mod setup functions. /// </summary> /// <param name="state"></param> /// <returns></returns> public List <SetupOptions> FindModLoaders(StateManager.StateTypes state) { List <SetupOptions> modLoaders; #if UNITY_EDITOR if (IsVirtual) { modLoaders = new List <SetupOptions>(); foreach (Type type in types) { FindModLoaders(state, type, modLoaders); } modLoaders.Sort(); return(modLoaders); } #endif if (assemblies == null || assemblies.Count < 1) { return(null); } modLoaders = new List <SetupOptions>(1); for (int i = 0; i < assemblies.Count; i++) { try { Type[] types = assemblies[i].GetTypes(); foreach (Type t in types) { if (t.IsClass) { FindModLoaders(state, t, modLoaders); } } } catch (Exception ex) { Debug.LogError($"Failed to seek mod loader on {Title}: {ex.Message}"); CheckMissingReferences(assemblies[i]); continue; } } modLoaders.Sort(); return(modLoaders); }
private void InvokeModLoaders(StateManager.StateTypes state) { #if DEBUG System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); timer.Start(); #endif if (alreadyAtStartMenuState) { Mod[] mods = GetAllMods(); for (int i = 0; i < mods.Length; i++) { List <SetupOptions> setupOptions = mods[i].FindModLoaders(state); if (setupOptions == null) { Debug.Log("No mod loaders found for mod: " + mods[i].Title); continue; } for (int j = 0; j < setupOptions.Count; j++) { SetupOptions options = setupOptions[j]; MethodInfo mi = options.mi; if (mi == null) { continue; } InitParams initParams = new InitParams(options.mod, ModManager.Instance.GetModIndex(options.mod.Title), LoadedModCount); try { mi.Invoke(null, new object[] { initParams }); } catch (TargetInvocationException e) { Debug.LogError($"Exception has been thrown by entry point \"{mi.Name}\" of mod \"{mods[i].Title}\":\n{e.InnerException}"); } } } #if DEBUG timer.Stop(); Debug.Log("InvokeModLoaders() finished...time: " + timer.ElapsedMilliseconds); #endif } }
private void FindModLoaders(StateManager.StateTypes state, Type type, List <SetupOptions> modLoaders) { foreach (MethodInfo mi in type.GetMethods()) { if (!mi.IsPublic || !mi.IsStatic) { continue; } else if (mi.ContainsGenericParameters) { continue; } Invoke initAttribute = (Invoke)Attribute.GetCustomAttribute(mi, typeof(Invoke)); if (initAttribute == null) { continue; } else if (initAttribute.StartState != state) { continue; } ParameterInfo[] pi = mi.GetParameters(); if (pi.Length != 1) { continue; } else if (pi[0].ParameterType != typeof(InitParams)) { continue; } SetupOptions options = new SetupOptions(initAttribute.Priority, this, mi); #if DEBUG Debug.Log(string.Format("found new loader: {0} for mod: {1}", options.mi.Name, this.Title)); #endif modLoaders.Add(options); } }
public List <SetupOptions> FindModLoaders(StateManager.StateTypes state) { if (Assemblies == null || Assemblies.Count < 1) { return(null); } List <SetupOptions> modLoaders = new List <SetupOptions>(1); for (int i = 0; i < Assemblies.Count; i++) { try { Type[] types = Assemblies[i].GetTypes(); foreach (Type t in types) { if (!t.IsClass) { continue; } foreach (MethodInfo mi in t.GetMethods()) { if (!mi.IsPublic || !mi.IsStatic) { continue; } else if (mi.ContainsGenericParameters) { continue; } Invoke initAttribute = (Invoke)Attribute.GetCustomAttribute(mi, typeof(Invoke)); if (initAttribute == null) { continue; } else if (initAttribute.startState != state) { continue; } ParameterInfo[] pi = mi.GetParameters(); if (pi.Length != 1) { continue; } else if (pi[0].ParameterType != typeof(InitParams)) { continue; } SetupOptions options = new SetupOptions(initAttribute.priority, this, mi); #if DEBUG Debug.Log(string.Format("found new loader: {0} for mod: {1}", options.mi.Name, this.Name)); #endif modLoaders.Add(options); } } } catch (Exception ex) { Debug.LogError(ex.Message); continue; } } modLoaders.Sort(); return(modLoaders); }
/// <summary> /// Request the mod manager to invoke this method at the specified state. /// </summary> /// <param name="startState">At which state the ModManager will invoke this method; typically this the Start or the Game state.</param> /// <param name="priority">Defines a per-mod order if there are multiple invoked methods for the same state.</param> public Invoke(StateManager.StateTypes startState = StateManager.StateTypes.Start, int priority = 99) { this.Priority = priority; this.StartState = startState; }