Example #1
0
        public static void Initialize(Main main)
        {
            Game = main;

            #if CLIENT
            XNAConsole = new TerrariaConsole(Game);
            Game.Components.Add(XNAConsole);
            #endif

            Application.EnableVisualStyles();

            if (!Directory.Exists(PluginsPath))
            {
                Directory.CreateDirectory(PluginsPath);
            }

            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            // Not loaded for some reason :s
            Assembly.Load("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
            Assemblies = AppDomain.CurrentDomain.GetAssemblies();

            bool error = false;

            foreach (FileInfo f in new DirectoryInfo(PluginsPath).GetFiles("*.dll"))
            {
                try
                {
                    string name = Path.GetFileNameWithoutExtension(f.Name);
                    Assembly asm;
                    if (!LoadedAssemblies.TryGetValue(name, out asm))
                    {
                        asm = Assembly.Load(File.ReadAllBytes(f.FullName));
                        LoadedAssemblies.Add(name, asm);
                    }

                    foreach (var t in asm.GetTypes())
                    {
                        if (t.BaseType == typeof(TerrariaPlugin))
                        {
                            if (Compatible(t))
                            {
                                Plugins.Add(new PluginContainer((TerrariaPlugin)Activator.CreateInstance(t, Game)));
                            }
                            else
                            {
                                AppendLog("Outdated plugin: {0} ({1})", f.Name, t);

                                error = true;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    if (e is TargetInvocationException)
                        e = ((TargetInvocationException)e).InnerException;

                    AppendLog(f.Name, e);

                    FailedPlugins.Add(f.Name);
                    error = true;
                }
            }

            foreach (FileInfo f in new DirectoryInfo(PluginsPath).GetFiles("*.cs"))
            {
                try
                {
                    Assembly asm = Compile(f.Name, File.ReadAllText(f.FullName));

                    if (asm != null)
                    {
                        foreach (Type t in asm.GetTypes())
                        {
                            if (t.BaseType == typeof(TerrariaPlugin))
                            {
                                if (Compatible(t))
                                {
                                    Plugins.Add(new PluginContainer((TerrariaPlugin)Activator.CreateInstance(t, Game), false));
                                }
                                else
                                {
                                    AppendLog("Outdated plugin: {0} ({1})", f.Name, t);

                                    error = true;
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    if (e is TargetInvocationException)
                        e = ((TargetInvocationException)e).InnerException;

                    AppendLog(f.Name, e);

                    FailedPlugins.Add(f.Name);
                    error = true;
                }
            }

            Console.WriteLine("TerrariaAPI v{0}", ApiVersion);

            if (error)
            {
            #if CLIENT
                MessageBox.Show("There were errors while loading the mods, check logs.txt for more details.",
                    "Terraria API", MessageBoxButtons.OK, MessageBoxIcon.Error);
            #else
                Console.WriteLine("There were errors while loading the mods, check logs.txt for more details.");
            #endif
            }

            #if CLIENT
            GameHooks.Update += gameTime => InputManager.Update(gameTime);
            #endif

            var sortedPlugins = Plugins.OrderBy(x => x.Plugin.Order).ThenBy(x => x.Plugin.Name);

            foreach (PluginContainer p in sortedPlugins)
            {
                p.Initialize();

                Console.WriteLine("{0} v{1} ({2}) initiated.", p.Plugin.Name, p.Plugin.Version, p.Plugin.Author);
            }

            if (FailedPlugins.Count > 0)
            {
                Console.WriteLine("Plugins failed to load: " + string.Join(", ", FailedPlugins));
            }

            ClientHooks.Chat += ClientHooks_Chat;
            GameHooks.LoadContent += GameHooks_LoadContent;
        }
Example #2
0
        public static void Initialize(Main main)
        {
            Game = main;

#if CLIENT
            XNAConsole = new TerrariaConsole(Game);
            Game.Components.Add(XNAConsole);
#endif

            Application.EnableVisualStyles();

            if (!Directory.Exists(PluginsPath))
            {
                Directory.CreateDirectory(PluginsPath);
            }

            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            // Not loaded for some reason :s
            Assembly.Load("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
            Assemblies = AppDomain.CurrentDomain.GetAssemblies();

            bool error = false;

            foreach (FileInfo f in new DirectoryInfo(PluginsPath).GetFiles("*.dll"))
            {
                try
                {
                    string   name = Path.GetFileNameWithoutExtension(f.Name);
                    Assembly asm;
                    if (!LoadedAssemblies.TryGetValue(name, out asm))
                    {
                        asm = Assembly.Load(File.ReadAllBytes(f.FullName));
                        LoadedAssemblies.Add(name, asm);
                    }

                    foreach (var t in asm.GetTypes())
                    {
                        if (t.BaseType == typeof(TerrariaPlugin))
                        {
                            if (Compatible(t))
                            {
                                Plugins.Add(new PluginContainer((TerrariaPlugin)Activator.CreateInstance(t, Game)));
                            }
                            else
                            {
                                AppendLog("Outdated plugin: {0} ({1})", f.Name, t);

                                error = true;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    if (e is TargetInvocationException)
                    {
                        e = ((TargetInvocationException)e).InnerException;
                    }

                    AppendLog(f.Name, e);

                    FailedPlugins.Add(f.Name);
                    error = true;
                }
            }

            foreach (FileInfo f in new DirectoryInfo(PluginsPath).GetFiles("*.cs"))
            {
                try
                {
                    Assembly asm = Compile(f.Name, File.ReadAllText(f.FullName));

                    if (asm != null)
                    {
                        foreach (Type t in asm.GetTypes())
                        {
                            if (t.BaseType == typeof(TerrariaPlugin))
                            {
                                if (Compatible(t))
                                {
                                    Plugins.Add(new PluginContainer((TerrariaPlugin)Activator.CreateInstance(t, Game), false));
                                }
                                else
                                {
                                    AppendLog("Outdated plugin: {0} ({1})", f.Name, t);

                                    error = true;
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    if (e is TargetInvocationException)
                    {
                        e = ((TargetInvocationException)e).InnerException;
                    }

                    AppendLog(f.Name, e);

                    FailedPlugins.Add(f.Name);
                    error = true;
                }
            }

            Console.WriteLine("TerrariaAPI v{0}", ApiVersion);

            if (error)
            {
#if CLIENT
                MessageBox.Show("There were errors while loading the mods, check logs.txt for more details.",
                                "Terraria API", MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
                Console.WriteLine("There were errors while loading the mods, check logs.txt for more details.");
#endif
            }

#if CLIENT
            GameHooks.Update += gameTime => InputManager.Update(gameTime);
#endif


            var sortedPlugins = Plugins.OrderBy(x => x.Plugin.Order).ThenBy(x => x.Plugin.Name);

            foreach (PluginContainer p in sortedPlugins)
            {
                p.Initialize();

                Console.WriteLine("{0} v{1} ({2}) initiated.", p.Plugin.Name, p.Plugin.Version, p.Plugin.Author);
            }

            if (FailedPlugins.Count > 0)
            {
                Console.WriteLine("Plugins failed to load: " + string.Join(", ", FailedPlugins));
            }

            ClientHooks.Chat      += ClientHooks_Chat;
            GameHooks.LoadContent += GameHooks_LoadContent;
        }