Ejemplo n.º 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);
        }
Ejemplo n.º 2
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);
        }