Click here to Skip to main content
15,881,413 members
Articles / Desktop Programming / Win32

Overlay using raphook.dll

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
21 Sep 2008CPOL2 min read 41.1K   1.4K   12  
A OverlayMgr based on Ray Adam's raphook.dll
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Policy;
using System.Threading;
using System.Windows.Forms;
using OverlayWrapper;
using Timer=System.Threading.Timer;

namespace OverlayMgr {
    /// <summary>
    /// Program
    /// </summary>
    internal static class Program {
        /// <summary>
        /// Tick Interval
        /// </summary>
        public const int INTERVAL = 1000;

        private static readonly List<IPlugin> m_Plugins = new List<IPlugin>();

        private static string m_Config = "osd.cfg";

        private static MainForm m_Form = null;
        private static Timer m_Timer = null;

        ///<summary>
        /// Plugins
        ///</summary>
        public static List<IPlugin> Plugins {
            get { return m_Plugins; }
        }

        ///<summary>
        /// Config
        ///</summary>
        public static string Config {
            get { return m_Config; }
        }

        ///<summary>
        /// Main Form
        ///</summary>
        public static MainForm Form {
            get { return m_Form; }
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main(string[] args) {
            Application.ThreadException += new ThreadExceptionEventHandler(delegate(object sender, ThreadExceptionEventArgs e)
                                                                               {
                                                                                   MessageBox.Show(e.Exception.ToString(), "Unhandeled Exception!",
                                                                                                   MessageBoxButtons.OK, MessageBoxIcon.Error);
                                                                               });

            foreach (string arg in args) {
                if (arg.StartsWith("-")) {}
                else
                    m_Config = arg;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            using (m_Form = new MainForm()) {
                //1.) Init
                Overlay.LoadData(m_Config);
                Overlay.Install(m_Form);
                LoadPlugins();

                StartTimer();

                //2.) Run
                Application.Run();

                //3.) Deinit
                StopTimer();

                foreach (IPlugin p in Plugins) {
                    if (p.Active)
                        p.Unload();
                }

                Overlay.Uninstall();
            }

            Environment.Exit(0);
        }

        /// <summary>
        /// Load Plugins
        /// </summary>
        public static void LoadPlugins() {
            if (!Directory.Exists("plugins"))
                Directory.CreateDirectory("plugins");

            Evidence ev = new Evidence();
            ev.AddAssembly(typeof (MainForm).Assembly);
            ev.AddAssembly(typeof (Overlay).Assembly);

            foreach (string pl in Directory.GetFiles("plugins", "*.dll", SearchOption.TopDirectoryOnly)) {
                try {
                    Assembly a = Assembly.LoadFile(Path.GetFullPath(pl), ev);

                    foreach (Type t in a.GetExportedTypes()) {
                        if (typeof (IPlugin).IsAssignableFrom(t) && !t.IsAbstract) {
                            IPlugin plugin = null;

                            string data = Path.GetDirectoryName(pl) + "\\" + Path.GetFileNameWithoutExtension(pl) + ".cfg";
                            if (t.IsSerializable && File.Exists(data)) {
                                using (FileStream fs = new FileStream(data, FileMode.Open, FileAccess.Read)) {
                                    BinaryFormatter bf = new BinaryFormatter();
                                    bf.Binder = new FixedBinder();
                                    plugin = (IPlugin) bf.Deserialize(fs);
                                }
                            }
                            else plugin = (IPlugin) Activator.CreateInstance(t);

                            m_Plugins.Add(plugin);

                            if (plugin.Active)
                                plugin.Load();
                        }
                    }
                }
                catch (Exception e) {
                    Console.WriteLine(e);
                    continue;
                }
            }
        }

        /// <summary>
        /// Save Plugins
        /// </summary>
        public static void SavePlugins() {
            foreach (IPlugin p in Plugins) {
                if (p.Active && p.GetType().IsSerializable) {
                    string pl = p.GetType().Assembly.Location;
                    string file = Path.GetDirectoryName(pl) + "\\" + Path.GetFileNameWithoutExtension(pl) + ".cfg";
                    using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write))
                        new BinaryFormatter().Serialize(fs, p);
                }
            }
        }

        /// <summary>
        /// Start Timer
        /// </summary>
        public static void StartTimer() {
            m_Timer = new Timer(Tick, null, 1000, INTERVAL);
        }

        /// <summary>
        /// Stop Timer
        /// </summary>
        public static void StopTimer() {
            m_Timer.Change(Timeout.Infinite, Timeout.Infinite);
        }

        /// <summary>
        /// Called every tick
        /// </summary>
        public static void Tick(object o) {
            if (!Overlay.Hooked)
                return;

            //1.) Reset Data
            Overlay.Data.CustomString = "";

            //2.) Plugins
            foreach (IPlugin p in Plugins) {
                if (p.Active)
                    p.Tick();
            }
            Overlay.Data.CustomString = Overlay.Data.CustomString.Trim();

            //3.) Update
            Overlay.UpdateOSD();
        }

        #region Nested type: FixedBinder

        public class FixedBinder : SerializationBinder {
            public override Type BindToType(string assemblyName, string typeName) {
                Type tyType = null;
                string sShortAssemblyName = assemblyName.Split(',')[0];
                Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();
                foreach (Assembly ayAssembly in ayAssemblies) {
                    if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0]) {
                        tyType = ayAssembly.GetType(typeName);
                        break;
                    }
                }

                return tyType;
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions