示例#1
0
        /// <summary>
        /// Compiles a script File
        /// </summary>
        /// <param name="ScriptFile">Script File</param>
        /// <param name="OutFile">Output File</param>
        /// <param name="Optimize">Optimize Code. This makes debugging harder</param>
        /// <returns>Compiler Errors</returns>
        public static CompilerError[] Compile(string ScriptFile, string OutFile, bool Optimize = true)
        {
            var Deps = ResolveDependencies(ScriptFile);

            using (var Handler = new TempFileHandler(ScriptFile + ".norm"))
            {
                File.WriteAllText(Handler.TempName, NormalizeScript(ScriptFile));
                var Refs = Deps
                           .Where(m => m.Type == ScriptDependencyType.ScriptBinary || m.Type == ScriptDependencyType.Library)
                           .Concat(Deps.Where(m => m.Type == ScriptDependencyType.Library))
                           .Select(m => m.Path)
                           .ToArray();
                var Scripts = (new string[] { Handler.TempName }).Concat(Deps
                                                                         .Where(m => m.Type == ScriptDependencyType.ScriptFile)
                                                                         .Select(m => m.Path))
                              .ToArray();
                return(Compile(Scripts, Refs, OutFile, false));
            }
        }
示例#2
0
        /// <summary>
        /// Runs a Script file or Assembly
        /// </summary>
        /// <param name="ScriptFile">Script file</param>
        /// <param name="ScriptArguments">Arguments to pass to the script</param>
        /// <returns>Result Code</returns>
        public static int Run(string ScriptFile, string[] ScriptArguments = null, bool Optimize = true)
        {
            //Simulates Real Main method Behavior
            if (ScriptArguments == null)
            {
                ScriptArguments = new string[0];
            }
            var      T = GetScriptType(ScriptFile);
            Assembly Script;

            if (T == ScriptDependencyType.ScriptFile)
            {
                using (var TFH = new TempFileHandler())
                {
                    Compile(ScriptFile, TFH.TempName, Optimize);
                    Script = Assembly.LoadFile(TFH.TempName);
                }
            }
            else
            {
                Script = Assembly.LoadFile(ScriptFile);
            }

            foreach (var AssemblyType in Script.GetTypes())
            {
                //Only support classes
                if (AssemblyType.IsClass)
                {
                    //Find a static "Main" Method
                    var M = AssemblyType.GetMethod("Main", BindingFlags.Static | BindingFlags.Public);
                    if (M != null)
                    {
                        //Get Parameter Number
                        var Params = M.GetParameters();
                        if (Params.Length < 2)
                        {
                            //Run Method only if no parameters are needed or a single string array is needed.
                            if (Params.Length == 0 || Params[0].ParameterType == typeof(string[]))
                            {
                                object ret = null;
                                try
                                {
                                    ret = M.Invoke(null, Params.Length == 0 ? null : new object[] { ScriptArguments });
                                }
                                catch (TargetInvocationException ex)
                                {
                                    //Strip ex because it's unnecessary and confusing for script developers
                                    throw new ScriptException(ScriptFile, ex.InnerException);
                                }
                                catch (Exception ex)
                                {
                                    throw new ScriptException(ScriptFile, ex);
                                }
                                //Run according to return type
                                return(M.ReturnType == typeof(int) ? (int)ret : 0);
                            }
                        }
                    }
                }
            }
            throw new BadImageFormatException("No suitable method signature found in the given File", ScriptFile);
        }