示例#1
0
        public CompilerResult Compile(PatchData data, CompilerSettings settings)
        {
            CompilerParameters compilerParameters = new CompilerParameters();

            compilerParameters.GenerateInMemory        = settings.GenerateInMemory;
            compilerParameters.GenerateExecutable      = false;
            compilerParameters.IncludeDebugInformation = true;
            compilerParameters.ReferencedAssemblies.AddRange(settings.References.ToArray());
            compilerParameters.WarningLevel = -1;

            if (!settings.GenerateInMemory)
            {
                compilerParameters.OutputAssembly = settings.OutputPath; // data.BuildFolder + "Mods.dll"; // settings.OutputPath;
            }
            var flag = settings.IsDedicateServerBuild ? "IsDedi" : "IsClient";

            compilerParameters.CompilerOptions += " /define:" + flag + " /nostdlib";

            var codeProvider = new CSharpCodeProvider(new Dictionary <string, string>
            {
                //{"CompilerVersion", settings.CompilerVersion},
            }
                                                      );


            CompilerResult scriptCompilerResults = new CompilerResult();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            CompilerResults compilerResults = codeProvider.CompileAssemblyFromFile(compilerParameters, settings.Files.ToArray());

            scriptCompilerResults.Duration = stopwatch.ElapsedMilliseconds;
            stopwatch.Stop();

            foreach (string line in compilerResults.Output)
            {
                if (line.Contains("error CS"))
                {
                    scriptCompilerResults.Errors.Add(line);
                }
            }

            if (compilerResults.Errors.Count == 0)
            {
                scriptCompilerResults.Assembly = compilerResults.CompiledAssembly;
                scriptCompilerResults.Success  = true;
            }

            return(scriptCompilerResults);
        }
示例#2
0
        public bool RunPatchScripts(PatchData data, out IPatcherMod[] ret)
        {
            var settings = CompilerSettings.CreatePatchScripts(data);

            if (settings.Files.Count == 0)
            {
                Logging.Log("No patch scripts found...");
                ret = Empty;
                return(true);
            }

            settings.AssemblyName = "PatchScripts";
            settings.OutputPath   = data.BuildFolder + settings.AssemblyName + ".dll";
            var compilerResults = Compile(data, settings); // ScriptCompiler.Compile(compilerSettings, Plugin.DataDirectory, gamePath, false);

            Logging.LogInfo($"Built patch file in {compilerResults.Duration}ms");

            if (compilerResults.Success == false)
            {
                Logging.LogError("Compile errors");
                foreach (var t in compilerResults.Warnings)
                {
                    Logging.LogWarning(t);
                }
                foreach (var t in compilerResults.Errors)
                {
                    Logging.LogError(t);
                }

                Logging.LogError("Failed to compile PatchMods");
                ret = Empty;
                return(false);
            }

            Logging.Log("PatchMods compile successful");
            var patches = compilerResults.Assembly.GetInterfaceImplementers <IPatcherMod>();

            Logging.Log("Found patcher mods: " + patches.Length);
            ret = patches;
            return(true);
        }
示例#3
0
        public static CompilerSettings CreatePatchScripts(PatchData data)
        {
            CompilerSettings compilerSettings = new CompilerSettings();
            var patchScriptPaths = data.FindFiles("PatchScripts", "*.cs", true);

            if (patchScriptPaths.Count == 0)
            {
                //return compilerSettings;
            }

            foreach (string path in patchScriptPaths)
            {
                compilerSettings.Files.Add(path);
            }

            compilerSettings.GenerateInMemory = true;
            compilerSettings.AddReference("DMT.dll");
            compilerSettings.AddReference("Mono.Cecil.dll");
            compilerSettings.AddReferences(data.FindFiles("PatchScripts", "*.dll", true));

            var dllPaths = Directory.GetFiles(data.ManagedFolder, "*.dll").Where(d => d.EndsWith("/Mods.dll") == false && d.Contains("Assembly-CSharp") == false).ToArray();

            compilerSettings.AddReferences(dllPaths);

            if (data.RunSection == RunSection.InitialPatch)
            {
                compilerSettings.AddReference(data.BackupDllLocataion);
            }
            else if (data.RunSection == RunSection.LinkedPatch)
            {
                compilerSettings.AddReference(data.InitialDllLocation);
                //compilerSettings.AddReference(data.ManagedFolder + "Assembly-CSharp.dll");
            }
            else
            {
                compilerSettings.AddReference(data.LinkedDllLocation);
            }

            //for (int i = 0; i < dllPaths.Length; i++)
            //{
            //    var path = dllPaths[i];
            //    if (path.EndsWith(Path.DirectorySeparatorChar + "Assembly-CSharp.dll"))
            //    {
            //        var z = dllPaths[0];
            //        dllPaths[0] = path;
            //        dllPaths[i] = z;
            //        break;
            //    }
            //}

            List <string> namespaces = new List <string>();

            for (int i = 0; i < dllPaths.Length; i++)
            {
                string dllPath = dllPaths[i];
                if (!Path.GetFileName(dllPath).Contains("mscorlib") && !Path.GetFileName(dllPath).Contains("Mono.Cecil")) //
                {
                    var mod = data.ReadModuleDefinition(dllPath);
                    if (namespaces.Contains(mod.Name))
                    {
                        Logging.LogWarning($"Duplicate dll namespace found '{mod.Name}'. Skipping dll at path: '{dllPath}'");
                        continue;
                    }
                    namespaces.Add(mod.Name);
                    compilerSettings.AddReference(dllPath);
                }
            }

            compilerSettings.GenerateInMemory = true;
            return(compilerSettings);
        }
示例#4
0
        public CompilerResult Compile(PatchData data, CompilerSettings settings)
        {
            CompilerResult scriptCompilerResults = new CompilerResult();

            if (File.Exists(settings.OutputPath))
            {
                scriptCompilerResults.Assembly = Assembly.LoadFile(settings.OutputPath);
                scriptCompilerResults.Success  = true;
                return(scriptCompilerResults);
            }


            Logging.Log("Compiling Roslyn Scripts assembly for " + data.RunSection + "...");

            var trees = new List <SyntaxTree>();

            foreach (var f in settings.Files)
            {
                var sourceCode = System.IO.File.ReadAllText(f);
                var tree       = SyntaxFactory.ParseSyntaxTree(SourceText.From(sourceCode)).WithFilePath(f);
                trees.Add(tree);
            }
            //var syntaxTree = SyntaxFactory.ParseSyntaxTree(SourceText.From(sourceCode));

            var assName = settings.AssemblyName;

            var compilation = CSharpCompilation.Create(assName)
                              .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, false, assName))
                              .AddReferences(
                settings.References.Select(d => MetadataReference.CreateFromFile(d))
                )
                              .AddSyntaxTrees(trees);

            //scriptCompilerResults.AssemblyLocation = assemblyPath;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            var result = compilation.Emit(settings.OutputPath);

            stopwatch.Stop();


            if (result.Success)
            {
                scriptCompilerResults.Assembly = Assembly.LoadFile(settings.OutputPath);
                scriptCompilerResults.Success  = true;
                //File.Copy(assemblyPath, data.ModsDllTempLocation, true);
            }

            foreach (var d in result.Diagnostics)
            {
                if (d.Severity == DiagnosticSeverity.Error)
                {
                    scriptCompilerResults.Errors.Add(d.ToString());
                }
                else
                {
                    if (d.ToString().Contains("Assuming assembly reference 'mscorlib") && (scriptCompilerResults.Warnings.Any(e => e.Contains("Assuming assembly reference 'mscorlib"))))
                    {
                        continue;
                    }
                    //result.Success ||
                    scriptCompilerResults.Warnings.Add(d.ToString());
                }
            }


            return(scriptCompilerResults);
        }