示例#1
0
        void Start()
        {
            ModLoaderStats stats = ModLoaderStats.Instance;

            if (stats.WasLoaded)
            {
                return;
            }

            AddPiece = null;

            var console = gameObject.AddComponent <Console>(); // Attach the console before loading the config so it can display possible errors

            Configuration = Configuration.LoadOrCreateDefault(Configuration.CONFIG_FILE_NAME);
            Keys.LoadKeys();

            gameObject.AddComponent <KeySettings>();
            observer = gameObject.AddComponent <GameObserver>();
#if DEV_BUILD
            gameObject.AddComponent <ObjectExplorer>();
#endif
            console.EnableInterface(); // Enable the console interface since it can now ask the configuration for the correct keys to use
            stats.WasLoaded = true;

            FileInfo[] files = (new DirectoryInfo(Application.dataPath + "/Mods")).GetFiles("*.dll");
            foreach (FileInfo fileInfo in files)
            {
                if (!fileInfo.Name.EndsWith(".no.dll") && fileInfo.Name != "SpaarModLoader.dll")
                {
                    Debug.Log("Trying to load " + fileInfo.FullName);
                    try
                    {
                        Assembly assembly = Assembly.LoadFrom(fileInfo.FullName);
                        var      types    = assembly.GetExportedTypes();

                        bool foundAttrib = false;

                        foreach (var type in types)
                        {
                            var attrib = Attribute.GetCustomAttribute(type, typeof(Mod)) as Mod;
                            if (attrib != null)
                            {
                                gameObject.AddComponent(type);
                                Debug.Log("Successfully loaded " + attrib.Name() + " (" + attrib.version + ") by " + attrib.author);
                                foundAttrib = true;
                            }
                        }

                        if (!foundAttrib)
                        {
                            // Continue to load a Mod class if not Mod attribute is found for now.
                            // Otherwise all current mods would break and require an update.
                            // TODO: Remove this fall-back after most mods have updated
                            foreach (var type in types)
                            {
                                if (type.Name == "Mod")
                                {
                                    gameObject.AddComponent(type);
                                    Debug.Log("Successfully loaded " + fileInfo.Name);
                                    break;
                                }
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Debug.Log("Could not load " + fileInfo.Name + ":");
                        Debug.LogException(exception);
                    }
                }
            }
        }
示例#2
0
 /// <summary>
 /// Register an IGameStateObserver. Every registered observer is always
 /// notified of the supported status changes that occur in the game.
 /// </summary>
 /// <param name="observer">The observer to be registered.</param>
 public static void RegisterGameStateObserver(IGameStateObserver observer)
 {
     GameObserver.RegisterGameStateObserver(observer);
 }