Esempio n. 1
0
        // Token: 0x06000D5C RID: 3420 RVA: 0x0005B1A0 File Offset: 0x000593A0
        public static bool LoadMod(ModContainer mod)
        {
            bool result = true;

            foreach (ModInfo.AssemblyInfo assemblyInfo in mod.Info.Assemblies)
            {
                assemblyInfo.Mod = mod;
                assemblyInfo.Resolve();
                ModAssembly item;
                if (!ValidateAssembly(assemblyInfo, out item))
                {
                    MLog.Error(string.Concat(new string[]
                    {
                        "Assembly for ",
                        mod.Info.Name,
                        " did not validate, not loading it! (",
                        assemblyInfo.Path,
                        ")"
                    }));
                    result = false;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("[SUCSESS] Mod Loaded :D");
                }
            }
            return(result);
        }
Esempio n. 2
0
        // Token: 0x06000D5E RID: 3422 RVA: 0x0005B33C File Offset: 0x0005953C
        private static bool ValidateAssembly(ModInfo.AssemblyInfo info, out ModAssembly mAssembly)
        {
            List <Assembly> list = new List <Assembly>();

            if (!AssemblyScanner.Scan(info, list))
            {
                mAssembly = null;
                return(false);
            }
            mAssembly = new ModAssembly(info, list);
            if (info.Mod.Assemblies.Any((ModAssembly a) => a.Info.Path == info.Path))
            {
                MLog.Error("The same assmbly is listed in the manifest more than once!");
                mAssembly = null;
                return(false);
            }
            return(true);
        }
Esempio n. 3
0
        // Token: 0x06000D5D RID: 3421 RVA: 0x0005B280 File Offset: 0x00059480
        public bool ActivateMod(ModContainer mod)
        {
            bool result = true;

            foreach (ModAssembly modAssembly in mod.Assemblies)
            {
                if (!this.LoadAssembly(modAssembly))
                {
                    result = false;
                }
                else if (modAssembly.HasModEntryPoint)
                {
                    modAssembly.ModEntryPoint.ModContainer = mod;
                    if (!this.InitializeAssembly(modAssembly))
                    {
                        result = false;
                        MLog.Error("There was an error activating assembly: " + modAssembly.Assembly.GetName().Name);
                    }
                }
            }
            return(result);
        }
Esempio n. 4
0
        // Token: 0x06000D5F RID: 3423 RVA: 0x0005B3B8 File Offset: 0x000595B8
        private bool LoadAssembly(ModAssembly mAssembly)
        {
            bool result;

            try
            {
                Assembly assembly = Assembly.LoadFrom(mAssembly.Info.Path);
                mAssembly.Assembly = assembly;
                List <Type> list = (from t in assembly.GetTypes()
                                    where t.IsSubclassOf(typeof(ModEntryPoint)) && !t.IsAbstract
                                    select t).ToList <Type>();
                if (list.Count > 1)
                {
                    throw new Exception("Too many types extending ModEntryPoint!");
                }
                if (list.Count == 1)
                {
                    Type          type          = list[0];
                    ModEntryPoint modEntryPoint = (ModEntryPoint)Activator.CreateInstance(type);
                    mAssembly.HasModEntryPoint = true;
                    mAssembly.ModEntryPoint    = modEntryPoint;
                }
                else
                {
                    mAssembly.HasModEntryPoint = false;
                }
                result = true;
            }
            catch (Exception ex)
            {
                MLog.Error("Error loading assembly: " + mAssembly.Info.Path);
                MLog.Error(ex.ToString());
                result = false;
            }
            return(result);
        }
 // Token: 0x06000D6F RID: 3439 RVA: 0x0005BF94 File Offset: 0x0005A194
 private void LogError(string msg)
 {
     MLog.Error("[Security] " + msg);
     MLog.Error("[Security] Not loading " + this.CurrentlyScanning.Path);
 }
        // Token: 0x06000D69 RID: 3433 RVA: 0x0005B7EC File Offset: 0x000599EC
        public static bool Scan(ModInfo.AssemblyInfo info, List <Assembly> additionalRefs)
        {
            if (BesiegeLogFilter.logDev)
            {
                Console.WriteLine("[AssemblyScanner] Scanning {0}.", new object[]
                {
                    info.Path
                });
            }
            bool result;

            try
            {
                AssemblyScanner assemblyScanner = new AssemblyScanner();
                assemblyScanner.CurrentlyScanning = info;
                AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(info.Path);
                Assembly[]         assemblies         = AppDomain.CurrentDomain.GetAssemblies();
                if (BesiegeLogFilter.logDev)
                {
                    string   format = "[AssemblyScanner] Got loaded assembly list:\n{0}.";
                    object[] array  = new object[1];
                    array[0] = string.Join("\n", (from a in assemblies
                                                  select a.FullName + "(" + a.CodeBase + ")").ToArray <string>());
                    Debug.LogFormat(format, array);
                }
                foreach (ModuleDefinition moduleDefinition in assemblyDefinition.Modules)
                {
                    AssemblyNameReference reference;
                    foreach (AssemblyNameReference reference2 in moduleDefinition.AssemblyReferences)
                    {
                        reference = reference2;
                        if (BesiegeLogFilter.logDev)
                        {
                            Debug.LogFormat("[AssemblyScanner] Checking if reference {0} is loaded.", new object[]
                            {
                                reference.FullName
                            });
                        }
                        if (assemblies.Any((Assembly a) => a.GetName().Name == reference.Name))
                        {
                            if (BesiegeLogFilter.logDev)
                            {
                                Debug.LogFormat("[AssemblyScanner] Found matching loaded assembly!", new object[0]);
                            }
                        }
                        else
                        {
                            if (BesiegeLogFilter.logDev)
                            {
                                Debug.LogFormat("[AssemblyScanner] Did not find matching loaded assembly!", new object[0]);
                            }
                            Assembly assembly;
                            try
                            {
                                assembly = Assembly.ReflectionOnlyLoad(reference.FullName);
                            }
                            catch (FileNotFoundException)
                            {
                                string text = Path.Combine(new FileInfo(info.Path).DirectoryName, reference.Name + ".dll");
                                if (!File.Exists(text))
                                {
                                    continue;
                                }
                                assembly = Assembly.ReflectionOnlyLoadFrom(text);
                            }
                            additionalRefs.Add(assembly);
                            ModInfo.AssemblyInfo info2 = new ModInfo.AssemblyInfo
                            {
                                Path = assembly.Location
                            };
                            if (!AssemblyScanner.Scan(info2, additionalRefs))
                            {
                                MLog.Error("Referenced assembly did not validate: " + reference.Name);
                                MLog.Error("Not loading " + assemblyDefinition.Name);
                                return(false);
                            }
                        }
                    }
                    foreach (TypeDefinition type in moduleDefinition.Types)
                    {
                        if (!assemblyScanner.ScanType(type))
                        {
                            return(false);
                        }
                    }
                }
                result = true;
            }
            catch (Exception exception)
            {
                Debug.LogError("Error scanning assembly, not loading!");
                Debug.LogException(exception);
                result = false;
            }
            return(result);
        }
        // Token: 0x06000D55 RID: 3413 RVA: 0x0005AF34 File Offset: 0x00059134
        public static string ResolveScriptAssembly(string codeDir, ModContainer mod)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(codeDir);

            if (!directoryInfo.Exists)
            {
                MLog.Error("Code directory " + codeDir + " does not exist!");
                return(string.Empty);
            }
            string assemblyPath = ModPaths.GetAssemblyPath(mod, directoryInfo.Name);

            if (File.Exists(assemblyPath))
            {
                return(assemblyPath);
            }
            CompilerParameters compilerParameters = new CompilerParameters
            {
                GenerateExecutable = false,
                GenerateInMemory   = false,
                OutputAssembly     = assemblyPath
            };

            compilerParameters.ReferencedAssemblies.AddRange((from a in AppDomain.CurrentDomain.GetAssemblies().Where(delegate(Assembly a)
            {
                bool result;
                try
                {
                    result = !string.IsNullOrEmpty(a.Location);
                }
                catch (NotSupportedException)
                {
                    result = false;
                }
                return(result);
            })
                                                              select a.Location).ToArray <string>());
            string[] array = (from f in directoryInfo.GetFiles("*.cs", SearchOption.AllDirectories)
                              select f.FullName).ToArray <string>();
            if (array.Length == 0)
            {
                MLog.Error("Code directory " + codeDir + " does not contain any source files!");
            }
            CSharpCompiler.CodeCompiler codeCompiler    = new CSharpCompiler.CodeCompiler();
            CompilerResults             compilerResults = codeCompiler.CompileAssemblyFromFileBatch(compilerParameters, array);

            foreach (object obj in compilerResults.Errors)
            {
                CompilerError compilerError = (CompilerError)obj;
                string        message       = compilerError.ToString();
                if (compilerError.IsWarning)
                {
                    MLog.Warn(message);
                }
                else
                {
                    MLog.Error(message);
                }
            }
            if (compilerResults.Errors.HasErrors)
            {
                MLog.Error("There were errors compiling the ScriptAssembly at " + codeDir + "!");
            }
            return(assemblyPath);
        }