Example #1
0
 private CompilerResults CompileFromSourceBatch(CompilerParameters options, string[] sources)
 {
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     if (sources == null)
     {
         throw new ArgumentNullException("sources");
     }
     string[] array = new string[sources.Length];
     for (int i = 0; i < sources.Length; i++)
     {
         array[i] = ColossalCSharpCodeCompiler.GetTempFileNameWithExtension(options.TempFiles, i.ToString() + ".cs");
         FileStream fileStream = new FileStream(array[i], FileMode.OpenOrCreate);
         using (StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
         {
             streamWriter.Write(sources[i]);
             streamWriter.Close();
         }
         fileStream.Close();
     }
     return(this.CompileFromFileBatch(options, array));
 }
Example #2
0
        private static string BuildArgs(CompilerParameters options, string[] fileNames)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (options.GenerateExecutable)
            {
                throw new NotSupportedException();
            }
            stringBuilder.Append("/target:library ");
            string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;

            if (!string.IsNullOrEmpty(privateBinPath))
            {
                stringBuilder.AppendFormat("/lib:\"{0}\" ", privateBinPath);
            }
            if (options.IncludeDebugInformation)
            {
                stringBuilder.Append("/debug+ /optimize- ");
            }
            else
            {
                stringBuilder.Append("/debug- /optimize+ ");
            }
            if (options.TreatWarningsAsErrors)
            {
                stringBuilder.Append("/warnaserror ");
            }
            if (options.WarningLevel >= 0)
            {
                stringBuilder.AppendFormat("/warn:{0} ", options.WarningLevel);
            }
            if (string.IsNullOrEmpty(options.OutputAssembly))
            {
                options.OutputAssembly = ColossalCSharpCodeCompiler.GetTempFileNameWithExtension(options.TempFiles, "dll", !options.GenerateInMemory);
            }
            stringBuilder.AppendFormat("/out:\"{0}\" ", options.OutputAssembly);
            foreach (string text in options.ReferencedAssemblies)
            {
                if (text != null && text.Length != 0)
                {
                    stringBuilder.AppendFormat("/r:\"{0}\" ", text);
                }
            }
            if (options.CompilerOptions != null)
            {
                stringBuilder.Append(options.CompilerOptions);
                stringBuilder.Append(" ");
            }
            foreach (string arg in options.EmbeddedResources)
            {
                stringBuilder.AppendFormat("/resource:\"{0}\" ", arg);
            }
            foreach (string arg2 in options.LinkedResources)
            {
                stringBuilder.AppendFormat("/linkresource:\"{0}\" ", arg2);
            }
            stringBuilder.Append(" -- ");
            foreach (string arg3 in fileNames)
            {
                stringBuilder.AppendFormat("\"{0}\" ", arg3);
            }
            return(stringBuilder.ToString());
        }
Example #3
0
        private CompilerResults CompileFromFileBatch(CompilerParameters options, string[] fileNames)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (fileNames == null)
            {
                throw new ArgumentNullException("fileNames");
            }
            ColossalCompilerResults colossalCompilerResults = new ColossalCompilerResults(options.TempFiles);
            Process process = new Process();

            process.StartInfo.FileName  = ColossalCSharpCodeCompiler.kMonoPath;
            process.StartInfo.Arguments = "\"" + ColossalCSharpCodeCompiler.kMcsPath + "\" " + ColossalCSharpCodeCompiler.BuildArgs(options, fileNames);
            FileUtils.SetExecutablePermissions(ColossalCSharpCodeCompiler.kMonoPath);
            this.mcsOutput   = new StringCollection();
            this.mcsOutMutex = new Mutex();
            string environmentVariable = Environment.GetEnvironmentVariable("MONO_PATH");
            string text           = string.IsNullOrEmpty(environmentVariable) ? ColossalCSharpCodeCompiler.kEnvMonoPath : environmentVariable;
            string privateBinPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;

            if (!string.IsNullOrEmpty(privateBinPath))
            {
                text = string.Format("{0}:{1}", privateBinPath, text);
            }
            if (text.Length > 0)
            {
                StringDictionary environmentVariables = process.StartInfo.EnvironmentVariables;
                if (environmentVariables.ContainsKey("MONO_PATH"))
                {
                    environmentVariables["MONO_PATH"] = text;
                }
                else
                {
                    environmentVariables.Add("MONO_PATH", text);
                }
            }
            process.StartInfo.CreateNoWindow         = true;
            process.StartInfo.UseShellExecute        = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError  = true;
            process.ErrorDataReceived += this.McsStderrDataReceived;
            try
            {
                process.Start();
            }
            catch (Exception ex)
            {
                Win32Exception ex2    = ex as Win32Exception;
                MethodInfo     method = typeof(Win32Exception).GetMethod("W32ErrorMessage", BindingFlags.Static | BindingFlags.NonPublic);
                if (ex2 != null)
                {
                    throw new SystemException(string.Format("Error running {0}: {1} ({2})", process.StartInfo.FileName, ex2.Message, (method != null) ? method.Invoke(null, new object[]
                    {
                        ex2.NativeErrorCode
                    }) : ex2.NativeErrorCode.ToString()));
                }
                throw;
            }
            try
            {
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();
                colossalCompilerResults.NativeCompilerReturnValue = process.ExitCode;
            }
            finally
            {
                process.CancelErrorRead();
                process.CancelOutputRead();
                process.Close();
            }
            StringCollection stringCollection = this.mcsOutput;
            bool             flag             = true;

            foreach (string error_string in this.mcsOutput)
            {
                CompilerError compilerError = ColossalCSharpCodeCompiler.CreateErrorFromString(error_string);
                if (compilerError != null)
                {
                    colossalCompilerResults.Errors.Add(compilerError);
                    if (!compilerError.IsWarning)
                    {
                        flag = false;
                    }
                }
            }
            if (stringCollection.Count > 0)
            {
                stringCollection.Insert(0, process.StartInfo.FileName + " " + process.StartInfo.Arguments + Environment.NewLine);
                colossalCompilerResults.Output = stringCollection;
            }
            if (flag)
            {
                if (!File.Exists(options.OutputAssembly))
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (string str in stringCollection)
                    {
                        stringBuilder.Append(str + Environment.NewLine);
                    }
                    throw new Exception("Compiler failed to produce the assembly. Output: '" + stringBuilder.ToString() + "'");
                }
                if (options.GenerateInMemory)
                {
                    using (FileStream fileStream = File.OpenRead(options.OutputAssembly))
                    {
                        byte[] array = new byte[fileStream.Length];
                        fileStream.Read(array, 0, array.Length);
                        colossalCompilerResults.CompiledAssembly = Assembly.Load(array, null, options.Evidence);
                        fileStream.Close();
                        return(colossalCompilerResults);
                    }
                }
                colossalCompilerResults.PathToAssembly = options.OutputAssembly;
            }
            else
            {
                colossalCompilerResults.CompiledAssembly = null;
            }
            return(colossalCompilerResults);
        }