Beispiel #1
0
        /// <summary>
        /// Register a new EverestModule (mod) dynamically. Invokes LoadSettings and Load.
        /// </summary>
        /// <param name="module">Mod to register.</param>
        public static void Register(this EverestModule module)
        {
            lock (_Modules) {
                _Modules.Add(module);
            }

            LuaLoader.Precache(module.GetType().Assembly);

            bool newStrawberriesRegistered = false;

            foreach (Type type in module.GetType().Assembly.GetTypesSafe())
            {
                // Search for all entities marked with the CustomEntityAttribute.
                foreach (CustomEntityAttribute attrib in type.GetCustomAttributes <CustomEntityAttribute>())
                {
                    foreach (string idFull in attrib.IDs)
                    {
                        string   id;
                        string   genName;
                        string[] split = idFull.Split('=');

                        if (split.Length == 1)
                        {
                            id      = split[0];
                            genName = "Load";
                        }
                        else if (split.Length == 2)
                        {
                            id      = split[0];
                            genName = split[1];
                        }
                        else
                        {
                            Logger.Log(LogLevel.Warn, "core", $"Invalid number of custom entity ID elements: {idFull} ({type.FullName})");
                            continue;
                        }

                        id      = id.Trim();
                        genName = genName.Trim();

                        patch_Level.EntityLoader loader = null;

                        ConstructorInfo ctor;
                        MethodInfo      gen;

                        gen = type.GetMethod(genName, new Type[] { typeof(Level), typeof(LevelData), typeof(Vector2), typeof(EntityData) });
                        if (gen != null && gen.IsStatic && gen.ReturnType.IsCompatible(typeof(Entity)))
                        {
                            loader = (level, levelData, offset, entityData) => (Entity)gen.Invoke(null, new object[] { level, levelData, offset, entityData });
                            goto RegisterEntityLoader;
                        }

                        ctor = type.GetConstructor(new Type[] { typeof(EntityData), typeof(Vector2), typeof(EntityID) });
                        if (ctor != null)
                        {
                            loader = (level, levelData, offset, entityData) => (Entity)ctor.Invoke(new object[] { entityData, offset, new EntityID(levelData.Name, entityData.ID) });
                            goto RegisterEntityLoader;
                        }

                        ctor = type.GetConstructor(new Type[] { typeof(EntityData), typeof(Vector2) });
                        if (ctor != null)
                        {
                            loader = (level, levelData, offset, entityData) => (Entity)ctor.Invoke(new object[] { entityData, offset });
                            goto RegisterEntityLoader;
                        }

                        ctor = type.GetConstructor(new Type[] { typeof(Vector2) });
                        if (ctor != null)
                        {
                            loader = (level, levelData, offset, entityData) => (Entity)ctor.Invoke(new object[] { offset });
                            goto RegisterEntityLoader;
                        }

                        ctor = type.GetConstructor(_EmptyTypeArray);
                        if (ctor != null)
                        {
                            loader = (level, levelData, offset, entityData) => (Entity)ctor.Invoke(_EmptyObjectArray);
                            goto RegisterEntityLoader;
                        }

RegisterEntityLoader:
                        if (loader == null)
                        {
                            Logger.Log(LogLevel.Warn, "core", $"Found custom entity without suitable constructor / {genName}(Level, LevelData, Vector2, EntityData): {id} ({type.FullName})");
                            continue;
                        }
                        patch_Level.EntityLoaders[id] = loader;
                    }
                }
                // Register with the StrawberryRegistry all entities marked with RegisterStrawberryAttribute.
                foreach (RegisterStrawberryAttribute attrib in type.GetCustomAttributes <RegisterStrawberryAttribute>())
                {
                    List <string> names = new List <string>();
                    foreach (CustomEntityAttribute nameAttrib in type.GetCustomAttributes <CustomEntityAttribute>())
                    {
                        foreach (string idFull in nameAttrib.IDs)
                        {
                            string[] split = idFull.Split('=');
                            if (split.Length == 0)
                            {
                                Logger.Log(LogLevel.Warn, "core", $"Invalid number of custom entity ID elements: {idFull} ({type.FullName})");
                                continue;
                            }
                            names.Add(split[0]);
                        }
                    }
                    if (names.Count == 0)
                    {
                        goto NoDefinedBerryNames; // no customnames? skip out on registering berry
                    }
                    foreach (string name in names)
                    {
                        StrawberryRegistry.Register(type, name, attrib.isTracked, attrib.blocksNormalCollection);
                        newStrawberriesRegistered = true;
                    }
                }
NoDefinedBerryNames:
                ;

                // Search for all Entities marked with the CustomEventAttribute.
                foreach (CustomEventAttribute attrib in type.GetCustomAttributes <CustomEventAttribute>())
                {
                    foreach (string idFull in attrib.IDs)
                    {
                        string   id;
                        string   genName;
                        string[] split = idFull.Split('=');

                        if (split.Length == 1)
                        {
                            id      = split[0];
                            genName = "Load";
                        }
                        else if (split.Length == 2)
                        {
                            id      = split[0];
                            genName = split[1];
                        }
                        else
                        {
                            Logger.Log(LogLevel.Warn, "core", $"Invalid number of custom cutscene ID elements: {idFull} ({type.FullName})");
                            continue;
                        }

                        id      = id.Trim();
                        genName = genName.Trim();

                        patch_EventTrigger.CutsceneLoader loader = null;

                        ConstructorInfo ctor;
                        MethodInfo      gen;

                        gen = type.GetMethod(genName, new Type[] { typeof(EventTrigger), typeof(Player), typeof(string) });
                        if (gen != null && gen.IsStatic && gen.ReturnType.IsCompatible(typeof(Entity)))
                        {
                            loader = (trigger, player, eventID) => (Entity)gen.Invoke(null, new object[] { trigger, player, eventID });
                            goto RegisterCutsceneLoader;
                        }

                        ctor = type.GetConstructor(new Type[] { typeof(EventTrigger), typeof(Player), typeof(string) });
                        if (ctor != null)
                        {
                            loader = (trigger, player, eventID) => (Entity)ctor.Invoke(new object[] { trigger, player, eventID });
                            goto RegisterCutsceneLoader;
                        }

                        ctor = type.GetConstructor(_EmptyTypeArray);
                        if (ctor != null)
                        {
                            loader = (trigger, player, eventID) => (Entity)ctor.Invoke(_EmptyObjectArray);
                            goto RegisterCutsceneLoader;
                        }

RegisterCutsceneLoader:
                        if (loader == null)
                        {
                            Logger.Log(LogLevel.Warn, "core", $"Found custom cutscene without suitable constructor / {genName}(EventTrigger, Player, string): {id} ({type.FullName})");
                            continue;
                        }
                        patch_EventTrigger.CutsceneLoaders[id] = loader;
                    }
                }
            }

            module.LoadSettings();
            module.Load();
            if (_ContentLoaded)
            {
                module.LoadContent(true);
            }
            if (_Initialized)
            {
                Tracker.Initialize();
                module.Initialize();
                Input.Initialize();
                ((Monocle.patch_Commands)Engine.Commands).ReloadCommandsList();

                if (SaveData.Instance != null)
                {
                    // we are in a save. we are expecting the save data to already be loaded at this point
                    Logger.Log("core", $"Loading save data slot {SaveData.Instance.FileSlot} for {module.Metadata}");
                    if (module.SaveDataAsync)
                    {
                        module.DeserializeSaveData(SaveData.Instance.FileSlot, module.ReadSaveData(SaveData.Instance.FileSlot));
                    }
                    else
                    {
#pragma warning disable CS0618 // Synchronous save / load IO is obsolete but some mods still override / use it.
                        module.LoadSaveData(SaveData.Instance.FileSlot);
#pragma warning restore CS0618
                    }

                    if (SaveData.Instance.CurrentSession?.InArea ?? false)
                    {
                        // we are in a level. we are expecting the session to already be loaded at this point
                        Logger.Log("core", $"Loading session slot {SaveData.Instance.FileSlot} for {module.Metadata}");
                        if (module.SaveDataAsync)
                        {
                            module.DeserializeSession(SaveData.Instance.FileSlot, module.ReadSession(SaveData.Instance.FileSlot));
                        }
                        else
                        {
#pragma warning disable CS0618 // Synchronous save / load IO is obsolete but some mods still override / use it.
                            module.LoadSession(SaveData.Instance.FileSlot, false);
#pragma warning restore CS0618
                        }
                    }
                }

                // Check if the module defines a PrepareMapDataProcessors method. If this is the case, we want to reload maps so that they are applied.
                // We should also run the map data processors again if new berry types are registered, so that CoreMapDataProcessor assigns them checkpoint IDs and orders.
                if (newStrawberriesRegistered || module.GetType().GetMethod("PrepareMapDataProcessors", new Type[] { typeof(MapDataFixup) })?.DeclaringType == module.GetType())
                {
                    Logger.Log("core", $"Module {module.Metadata} has custom strawberries or map data processors: reloading maps.");
                    AssetReloadHelper.ReloadAllMaps();
                }
            }

            if (Engine.Instance != null && Engine.Scene is Overworld overworld)
            {
                // we already are in the overworld. Register new Ouis real quick!
                Type[] types = FakeAssembly.GetFakeEntryAssembly().GetTypesSafe();
                foreach (Type type in types)
                {
                    if (typeof(Oui).IsAssignableFrom(type) && !type.IsAbstract && !overworld.UIs.Any(ui => ui.GetType() == type))
                    {
                        Logger.Log("core", $"Instanciating UI from {module.Metadata}: {type.FullName}");

                        Oui oui = (Oui)Activator.CreateInstance(type);
                        oui.Visible = false;
                        overworld.Add(oui);
                        overworld.UIs.Add(oui);
                    }
                }
            }

            InvalidateInstallationHash();

            EverestModuleMetadata meta = module.Metadata;
            meta.Hash = GetChecksum(meta);

            // Audio banks are cached, and as such use the module's hash. We can only ingest those now.
            if (patch_Audio.AudioInitialized)
            {
                patch_Audio.IngestNewBanks();
            }

            Logger.Log(LogLevel.Info, "core", $"Module {module.Metadata} registered.");
            Events.Everest.RegisterModule(module);

            CheckDependenciesOfDelayedMods();
        }