Esempio n. 1
0
        /// <summary>
        /// Handles the plugin console command.
        /// </summary>
        /// <param name="e"></param>
        private void OnPluginCommand(ConsoleCommandArgs e)
        {
            if (e.Arguments.Count == 3 && e.Arguments[1].ToLower() == "load")
            {
                // Attempt to load the given plugin..
                var ret = DetoxPluginManager.LoadPlugin(e.Arguments[2].ToLower());
                if (ret != 0)
                {
                    this.LogConsoleMessage(new DetoxAPI.ConsoleMessage(string.Format(
                                                                           "Failed to load plugin: {0} -- {1}",
                                                                           e.Arguments[2].ToLower(), DetoxPluginErrorReason.ErrorStrings[(int)ret]), ConsoleMessageType.Error));
                }
            }
            else if (e.Arguments.Count == 2 && e.Arguments[1].ToLower() == "list")
            {
                var plugins = DetoxPluginManager.Plugins.OrderBy(p => p.Plugin.Name).ToList();
                this.LogConsoleMessage(new DetoxAPI.ConsoleMessage("Loaded plugins:", ConsoleMessageType.About));
                this.LogConsoleMessage(new DetoxAPI.ConsoleMessage("----------------------------------------------------", ConsoleMessageType.About));
                plugins.ForEach(p => this.LogConsoleMessage(new DetoxAPI.ConsoleMessage(string.Format(
                                                                                            " {0} v{1} by {2} -- {3}", p.Plugin.Name, p.Plugin.Version, p.Plugin.Author, p.Plugin.Description), ConsoleMessageType.Warning)));
                this.LogConsoleMessage(new DetoxAPI.ConsoleMessage(string.Format("{0} loaded plugins.", plugins.Count), ConsoleMessageType.About));
            }
            else
            {
                this.LogConsoleMessage(new DetoxAPI.ConsoleMessage("Invalid command syntax; valid plugin commands are:", ConsoleMessageType.Error));
                this.LogConsoleMessage(new DetoxAPI.ConsoleMessage("  /plugin load [pluginname]", ConsoleMessageType.Error));
                this.LogConsoleMessage(new DetoxAPI.ConsoleMessage("  /plugin list", ConsoleMessageType.Error));
            }

            e.Handled = true;
        }
Esempio n. 2
0
        private static void Main()
        {
            // Store the base path to Detox..
            Detox.DetoxBasePath = AppDomain.CurrentDomain.BaseDirectory;

            Logging.Instance.Log("[Detox] Detox started at: " + DateTime.Now);

            try
            {
                // See if we have Terraria in the same folder..
                if (!File.Exists("Terraria.exe"))
                {
                    // Attempt to obtain Terraria's path from the registry..
                    var path = Program.GetValue <string>("HKEY_LOCAL_MACHINE\\SOFTWARE\\Re-Logic\\Terraria", "Install_Path");
                    Detox.TerrariaBasePath = path;
                    Detox.TerrariaAsm      = AssemblyDefinition.ReadAssembly(Path.Combine(path, "Terraria.exe"));
                }
                else
                {
                    // Load Terraria..
                    Detox.TerrariaBasePath = AppDomain.CurrentDomain.BaseDirectory;
                    Detox.TerrariaAsm      = AssemblyDefinition.ReadAssembly("Terraria.exe");
                }
            }
            catch
            {
                MessageBox.Show("There was an error locating Terraria.exe", "Detox Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Logging.Instance.Log("[Detox] Detox located Terraria at: " + Detox.TerrariaBasePath);

            // Set the working folder to Terraria's path..
            Directory.SetCurrentDirectory(Detox.TerrariaBasePath);

            // Load the configuration file..
            Logging.Instance.Log("[Detox] Loading configuration file..");
            Configurations.Instance.LoadConfig(AppDomain.CurrentDomain.BaseDirectory + "\\detox.config.json");
            Logging.Instance.Log("[Detox] Configuration file loaded!");

            // Initialize Steam if requested..
            if (Configurations.Instance.Current.Steam.InitializeSteam)
            {
                Logging.Instance.Log("[Steam] Initializing Steam due to configuration option.");
                Steam.Initialize();
            }

            // Prepare and apply hooks..
            Logging.Instance.Log("[Detox] Registering internal hooks..");
            Events.Initialize();
            Events.XnaEvents.PreInitialize.Register(null, Hooks.OnXnaPreInitialize, 0);
            Events.XnaEvents.PostInitialize.Register(null, Hooks.OnXnaPostInitialize, 0);
            Events.XnaEvents.PostLoadContent.Register(null, Hooks.OnXnaPostLoadContent, 0);
            Events.XnaEvents.PreUpdate.Register(null, Hooks.OnXnaPreUpdate, 0);
            Events.XnaEvents.PostUpdate.Register(null, Hooks.OnXnaPostUpdate, 0);
            Events.XnaEvents.PreDraw.Register(null, Hooks.OnXnaPreDraw, 0);
            Events.XnaEvents.PostDraw.Register(null, Hooks.OnXnaPostDraw, 0);

            // Apply internal detours..
            Logging.Instance.Log("[Detox] Applying internal detours / patches..");
            (from t in Assembly.GetExecutingAssembly().GetTypes()
             from m in t.GetMethods()
             from a in m.GetCustomAttributes(typeof(InjectionAttribute), false)
             select m).ToList().ForEach(m => m.Invoke(null, new object[] { Detox.TerrariaAsm }));

            // Load auto-start plugins..
            foreach (var s in Configurations.Instance.Current.Plugins.AutoLoadPlugins)
            {
                try
                {
                    DetoxPluginManager.LoadPlugin(s);
                    Logging.Instance.Log("[Detox] Loaded auto-load plugin: " + s);
                }
                catch
                {
                    Logging.Instance.Log("[Detox] Failed to load auto-load plugin: " + s);
                }
            }

            // Invoke Detox Initialize event..
            Events.DetoxEvents.InvokeDetoxInitialize();

            try
            {
                // Initialize Terraria..
                using (var mStream = new MemoryStream())
                {
                    // Write the new file to memory and load..
                    Detox.TerrariaAsm.Write(mStream);
                    Detox.Terraria = Assembly.Load(mStream.GetBuffer());

#if DEBUG
                    // Save a copy for debugging..
                    File.WriteAllBytes("detox.debug.exe", mStream.GetBuffer());
#endif

                    // Attempt to locate the constructor..
                    var mainType = Detox.Terraria.GetType("Terraria.Main");
                    var ctorInfo = mainType.GetConstructor(new Type[] { });
                    if (ctorInfo == null)
                    {
                        throw new InvalidOperationException();
                    }

                    // Attempt to invoke the constructor..
                    var ctor = ctorInfo.Invoke(null);
                    var run  = mainType.GetMethod("Run", Detox.BindFlags);
                    if (run == null)
                    {
                        throw new InvalidOperationException();
                    }

                    // Run Terraria..
                    run.Invoke(ctor, null);
                }
            }
            catch (Exception e)
            {
                Logging.Instance.Log("[Detox] Encountered an error while starting / running Terraria:");
                Logging.Instance.Log(e.ToString());
            }

            // Deinitialize Steam if requested..
            if (Configurations.Instance.Current.Steam.InitializeSteam)
            {
                Logging.Instance.Log("[Steam] Shutting down Steam due to configuration option.");
                Steam.Shutdown();
            }

            // Save the configuration file..
            Logging.Instance.Log("[Detox] Saving configuration file..");
            Configurations.Instance.SaveConfig(AppDomain.CurrentDomain.BaseDirectory + "\\detox.config.json");
            Logging.Instance.Log("[Detox] Configuration file saved!");

            Logging.Instance.Log("[Detox] Detox exited at: " + DateTime.Now);
        }