示例#1
0
        void LoadDLL(string file)
        {
            try
            {
                Assembly asm   = Assembly.LoadFrom(file);
                bool     isMod = false;

                AssemblyName[] list = asm.GetReferencedAssemblies();
                if (File.ReadAllText(file).Contains("RegistryKey") || File.ReadAllText(file).Contains("Steamworks"))
                {
                    throw new FileLoadException();
                }

                //Warn about wrong .net target, source of some mod crashes.
                if (!asm.ImageRuntimeVersion.Equals(Assembly.GetExecutingAssembly().ImageRuntimeVersion))
                {
                    ModConsole.Warning($"File <b>{Path.GetFileName(file)}</b> is targeting runtime version <b>{asm.ImageRuntimeVersion}</b> which is different that current running version <b>{Assembly.GetExecutingAssembly().ImageRuntimeVersion}</b>. This may cause unexpected behaviours, check your target assembly.");
                }

                Type mod = asm.GetTypes().FirstOrDefault(type => typeof(Mod).IsAssignableFrom(type));
                if (mod != null)
                {
                    if (list.Any(assembly => assembly.Name == "Assembly-CSharp-firstpass") && (File.ReadAllText(file).Contains("Steamworks") || File.ReadAllText(file).Contains("GetSteamID")))
                    {
                        throw new Exception("Targeting forbidden reference");
                    }

                    string       msVer     = "";
                    AssemblyName mscLoader = list.FirstOrDefault(assembly => assembly.Name == "MSCLoader");
                    if (mscLoader != null)
                    {
                        string[] verparse = mscLoader.Version.ToString().Split('.');
                        msVer = (mscLoader.Version.ToString() == "1.0.0.0") ? "0.1" : $"{verparse[0]}.{verparse[1]}.{verparse[2]}";
                    }

                    isMod = true;
                    LoadMod((Mod)Activator.CreateInstance(mod), msVer, file);
                }

                if (!isMod)
                {
                    ModConsole.Error($"<b>{Path.GetFileName(file)}</b> - doesn't look like a mod or missing Mod subclass!");
                    InvalidMods.Add(Path.GetFileName(file));
                }
            }
            catch (Exception e)
            {
                ModConsole.Error($"<b>{Path.GetFileName(file)}</b> - doesn't look like a mod, remove this file from mods folder!");
                ModConsole.Error(e.ToString());
                System.Console.WriteLine(e);
                InvalidMods.Add(Path.GetFileName(file));
            }
        }