Пример #1
0
        ////////////////////////////////////////////////////////////////
        // Methods
        ////////////////////////////////////////////////////////////////

        public void WriteTo(SourceCodeData sourceCodeData, string fileName, int filePart)
        {
            sourceCodeData.Append("public " + ReturnType + " " + MethodName + "(" + MethodVariablesStr + ") {");
            string[] lines = SourceCodeData.SplitLines(MethodBody);
            sourceCodeData.Append(fileName, filePart, 0, lines);
            sourceCodeData.Append("}");
        }
Пример #2
0
        ////////////////////////////////////////////////////////////////
        // Methods
        ////////////////////////////////////////////////////////////////

        private Error[] __TransformErrors(CompilerErrorCollection errors, SourceCodeData sourceCode)
        {
            List <Error> ret = new List <Error>();

            foreach (CompilerError e in errors)
            {
                if (e.IsWarning)
                {
                    continue;
                }
                SourceCodeLocation location = sourceCode.GetLocation(e.Line);
                ret.Add(new Error(e.ErrorNumber, e.ErrorText, location));
            }

            return(ret.ToArray());
        }
Пример #3
0
        /// <summary>
        /// Compiles given source file.
        /// </summary>
        /// <param name="scriptFileObject">The source file to be compiled.</param>
        public CompiledUnit Compile(string filePath, object scriptFileObject)
        {
            if (filePath == null)
            {
                throw new Exception("No file path specified!");
            }
            if (scriptFileObject == null)
            {
                throw new Exception("No script file object specified!");
            }

            ISourceCodeGenerator gen = codeGenList[scriptFileObject.GetType()];

            counter++;

            // compile the code

            SourceCodeData sourceCode = gen.CreateSourceContent(filePath, scriptFileObject, "ScriptedNameSpace" + counter, "ScriptedClass" + counter);

            // extract all using-information

            HashSet <string> usings = new HashSet <string>();

            foreach (ISourceCodeLine line in sourceCode)
            {
                string s = line.LineText.Trim();
                if (!s.StartsWith("using"))
                {
                    continue;
                }
                if (!s.EndsWith(";"))
                {
                    continue;
                }
                s = s.Substring("using".Length);
                if (!char.IsWhiteSpace(s[0]))
                {
                    continue;
                }
                s = s.Trim();
                s = s.Substring(0, s.Length - 1);
                if (!NameSpaceReference.IsMatch(s))
                {
                    continue;
                }
                usings.Add(s);
            }

            // prepare compilation

            CompilerParameters compilerParameters = new CompilerParameters {
                GenerateExecutable = false,
                GenerateInMemory   = true,
                // CompilerOptions = "/o+",
                IncludeDebugInformation = true,
                CompilerOptions         = " /debug:pdbonly",
                OutputAssembly          = tempDir2 + "ScriptedAssembly" + counter + ".dll",
            };

            // add additional assemblies

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

            foreach (AssemblyManager.NamespaceAssemblyInfo ai in AssemblyManager.GetNamespaceAssemblyInfos(usings))
            {
                compilerParameters.ReferencedAssemblies.Add(ai.Path);
                referencedAssemblies.Add(ai.Path);
            }

            /*
             * ReferencedAssemblyList referencedAssemblyList = new ReferencedAssemblyList();
             * if (gen.ReferencedAssemblies != null) {
             *      referencedAssemblyList.AddLoadAssemblies(gen.ReferencedAssemblies);
             * }
             * compilerParameters.ReferencedAssemblies.AddRange(referencedAssemblyList.ToArray());
             */

            /* does not work
             * foreach (string assemblyPath in compilerParameters.ReferencedAssemblies) {
             *      Assembly.LoadFrom(assemblyPath);
             * }			*/

            // check compilation result

            CompilerResults results = csharpCompiler.CompileAssemblyFromSource(compilerParameters, sourceCode.ToString());

            if (results.Errors.Count > 0)
            {
                return(new CompiledUnit(sourceSpec, filePath, referencedAssemblies.ToArray(), __TransformErrors(results.Errors, sourceCode)));
            }
            else
            if (results.CompiledAssembly == null)
            {
                throw new InvalidOperationException("Unable to compile scripts: No result assembly from compiler!");
            }

            // try to get main class

            Assembly compiledAssembly = results.CompiledAssembly;
            Type     scriptClass      = compiledAssembly.GetType("ScriptedNameSpace" + counter + ".ScriptedClass" + counter);

            if (scriptClass == null)
            {
                throw new InvalidOperationException("Unable to compile scripts: scripted class not found!");
            }

            return(new CompiledUnit(sourceSpec, filePath, referencedAssemblies.ToArray(), compiledAssembly, scriptClass));
        }