Ejemplo n.º 1
0
        // HACK : 作成するイベントのネームスペースは可変のほうがよい気がする
        public Func<Scripter, IEnumerable<int>> Load(string filepath, IList<UnitData> units)
        {
            Contract.Requires(filepath != null, "filepath");
            //			Contract.Requires(File.Exists(filepath), "filepath");
            var lex = new ScriptLexicalAnalyzer(filepath);
            var syn = new ScriptSyntaxAnalyzer(lex);
            var sem = new ScriptSemanticalAnalyzer(lex, syn, units);

            string source = sem.ToSource();

            CodeDomProvider provider = new CSharpCodeProvider();
            CompilerParameters option = new CompilerParameters(new []{
                //"FarenDotNet.exe"
                Assembly.GetEntryAssembly().Location
            });
            option.GenerateInMemory = true;

            CompilerResults result =
                provider.CompileAssemblyFromSource(option, source);

            bool errFlag = false;
            foreach (CompilerError err in result.Errors)
            {
                if (!err.IsWarning)
                {
                    errFlag = true;
                    MessageBox.Show(err.ErrorText, "コンパイルエラー");
                }
            }
            //using (FileStream s = new FileStream(@"C:\Documents and Settings\Ground\デスクトップ\EventScript.cs", FileMode.Create))
            //using (TextWriter w = new StreamWriter(s))
            //{
            //    w.WriteLine(source);
            //}

            if (errFlag)
                return null;
            try
            {
                Assembly asm = result.CompiledAssembly;
                Type script = asm.GetType("FarenDotNet.Reign.EventScript");
                MethodInfo run = script.GetMethod("Run");

                return run.ToDelegate<Func<Scripter, IEnumerable<int>>>();

            } catch (NullReferenceException) { return null; }
        }
 public ScriptSyntaxAnalyzer(ScriptLexicalAnalyzer lex)
 {
     _lex = lex;
 }