コード例 #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);
        }