Пример #1
0
        internal bool LoadModDll()
        {
            if (Instance != null)
            {
                throw new Exception("Error! Mod has already been loaded!");
            }

            try
            {
                ModAssembly = Assembly.Load(GetDllBytes());
                if (ModAssembly.GetTypes().Count(x => x.BaseType == typeof(Mod)) > 0)
                {
                    var type     = ModAssembly.GetTypes().First(x => x.BaseType == typeof(Mod));
                    Mod instance = (Mod)ModAssembly.CreateInstance(type.ToString());
                    if (instance != null)
                    {
                        instance.ModSettings = this;
                        instance.Entry();
                    }
                    Instance = instance;
                    Log.Verbose($"Loaded mod dll: {Name}");
                }
                else
                {
                    var types = ModAssembly.GetTypes();
                    foreach (var layer in ModLoader.CompatibilityLayers)
                    {
                        if (!layer.ContainsOurModType(types))
                        {
                            continue;
                        }

                        Instance = layer.LoadMod(ModAssembly, types, this);
                        Log.Verbose($"Loaded mod dll: {Name}");
                        break;
                    }
                }

                if (Instance == null)
                {
                    throw new Exception("Invalid Mod DLL");
                }
            }
            catch (Exception ex)
            {
                var exception = ex as ReflectionTypeLoadException;
                if (exception != null)
                {
                    foreach (var e in exception.LoaderExceptions)
                    {
                        Log.Exception("loaderexceptions entry: " +
                                      $"{e.Message} ${e.Source} ${e.TargetSite} ${e.StackTrace} ${e.Data}", e);
                    }
                }
                Log.Exception("Error loading mod DLL", ex);
                //throw new Exception(string.Format($"Failed to load mod '{modDllPath}'\n\t-{ex.Message}\n\t\t-{ex.StackTrace}"), ex);
            }

            return(Instance != null);
        }
Пример #2
0
        internal bool LoadModDll()
        {
            if (Instance != null)
            {
                throw new Exception("Error! Mod has already been loaded!");
            }

            var modDllPath = $"{ModDirectory}\\{ModDll}";

            if (!modDllPath.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase))
            {
                modDllPath += ".dll";
            }

            try
            {
                ModAssembly = Assembly.LoadFrom(modDllPath);
                if (ModAssembly.GetTypes().Count(x => x.BaseType == typeof(Farmhand.Mod)) > 0)
                {
                    var type     = ModAssembly.GetTypes().First(x => x.BaseType == typeof(Farmhand.Mod));
                    Mod instance = (Mod)ModAssembly.CreateInstance(type.ToString());
                    if (instance != null)
                    {
                        instance.ModSettings = this;
                        instance.Entry();
                    }
                    Instance = instance;
                    Log.Verbose($"Loaded mod dll: {Name}");
                }
                else
                {
                    var types = ModAssembly.GetTypes();
                    foreach (var layer in ModLoader.CompatibilityLayers)
                    {
                        if (!layer.ContainsOurModType(types))
                        {
                            continue;
                        }

                        Instance = layer.LoadMod(ModAssembly, types, this);
                        Log.Verbose($"Loaded mod dll: {Name}");
                        break;
                    }
                }

                if (Instance == null)
                {
                    throw new Exception("Invalid Mod DLL");
                }
            }
            catch (Exception ex)
            {
                Log.Exception("Error loading mod DLL", ex);
                //throw new Exception(string.Format($"Failed to load mod '{modDllPath}'\n\t-{ex.Message}\n\t\t-{ex.StackTrace}"), ex);
            }

            return(Instance != null);
        }
Пример #3
0
        internal void Initialize()
        {
            string path = Path.Combine(FileIO.DataDirectory, InternalName, InternalName + ".dll");

            ModAssembly = Assembly.LoadFrom(path);
            Type   mainType = ModAssembly.GetTypes().First(t => typeof(IModEntryPoint).IsAssignableFrom(t));
            object obj      = Activator.CreateInstance(mainType);

            ((IModEntryPoint)obj).Main();
            IsLoaded = true;
        }
Пример #4
0
 internal static void Initialize(string[] args = null)
 {
     if (args != null)
     {
         for (int i = 0; i < args.Length; i++)
         {
             if (args[i] == "--mod" && Directory.Exists(Path.Combine(Application.StartupPath, "mods", args[i + 1])))
             {
                 ModName = args[i + 1];
             }
             if (args[i] == "--visisble3d") //this opens a form and renders the gameplay in realtime(will certainly make it slower, well, unsure. Since otherwise it would be rendered in memory)
             {
                 new FrmMain().Show();      //requires client zips
             }
         }
     }
     EventRegistry = new GameEventRegistry();
     BF2Engine     = new BF2Engine();
     if (string.IsNullOrEmpty(ModName))
     {
         Console.WriteLine("No mod was defined, which mod would you like to load?");
         var directories = Directory.EnumerateDirectories(Path.Combine(Application.StartupPath, "mods")).Select(d => new DirectoryInfo(d).Name);
         for (int i = 0; i < directories.Count(); i++)
         {
             Console.WriteLine("[" + i + "] " + directories.ToArray()[i]);
         }
         Console.Write(">");
         var mod = Console.ReadLine();
         if (!directories.Contains(mod))
         {
             Console.WriteLine("Invalid mod! Press any key to exit...");
             Console.ReadKey();
             Environment.Exit(-1);
         }
         ModName          = "mods/" + mod;
         ModAssembly      = Assembly.Load(File.ReadAllBytes(Path.Combine(Application.StartupPath, ModName, "phoenixmod.dll")));
         ModPath          = Path.Combine(Application.StartupPath, ModName);
         LoadedMod        = (IMod)Activator.CreateInstance(ModAssembly.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(IMod))).First());
         ConFileProcessor = LoadedMod.GetConFileProcessor();
     }
     LoadedMod.Initialize(BF2Engine, EventRegistry);
     Console.WriteLine("Loaded " + LoadedMod.Name);
     BF2Engine.InitEngine();
     BF2Engine.LoadServerArchives();
 }