Example #1
0
        public static void GenerateApls(String folder)
        {
            apls.Clear();
            inst.actions.Reset();

            if (current_action_list != null)
            {
                current_action_list.Unload();
            }


            SimcraftImpl.Write("Compiling Action Lists");

            foreach (var filename in Directory.GetFiles(folder))
            {
                if (!filename.EndsWith(".simc"))
                {
                    continue;
                }
                String contents   = File.ReadAllText(filename);
                var    currentApl = ActionPrioriyList.FromString(contents);
                if (currentApl.Class != Me.Class)
                {
                    continue;
                }

                var code      = currentApl.ToCode();
                var classname = RandomString(10);
                code = code.Replace("public class SimcraftRota", "public class " + classname);

                //SimcraftImpl.Write(code);

                Assembly asm = RuntimeCodeCompiler.CompileCode(code);

                Behavior attributes =
                    (Behavior)asm.GetTypes()[0].GetMembers()[0].GetCustomAttributes(typeof(Behavior), true).First();

                currentApl.Assembly = asm;

                SimcraftImpl.Write("New Apl: " + currentApl.Name);

                apls[currentApl.ToString()] = currentApl;
            }
        }
Example #2
0
            public static INewCEPExpression MakeExt(YoloItem item, out string compileErrors)
            {
                string emptystr = " ";

                if (item.Usings == null)
                {
                    item.Usings = emptystr;
                }
                if (item.References == null)
                {
                    item.References = emptystr;
                }
                string code = Resources.ExtensionTemplate;

                code = code.Replace("@@USINGS@@", item.Usings);
                code = code.Replace("@@EXTCLASS@@", item.Code);

                //string code = File.ReadAllText(TEMPLATE_PATH).Replace(REPLACE_TOKEN, lambda);

                var provider = new CSharpCodeProvider(
                    new Dictionary <String, String> {
                    { "CompilerVersion", "v4.0" }
                });
                ICodeCompiler compiler = provider.CreateCompiler();

                var compilerparams = new CompilerParameters();

                compilerparams.GenerateExecutable = false;
                compilerparams.GenerateInMemory   = true;

                IEnumerable <string> referencedAssemblies = typeof(YoloWrapper).Assembly.GetReferencedAssemblies().Select(a => a.Name);

                foreach (string referencedAssembly in referencedAssemblies)
                {
                    string name = referencedAssembly + ".dll";
                    if (name != "WindowsBase.dll")
                    {
                        compilerparams.ReferencedAssemblies.Add(name);
                    }
                }

                //compilerparams.ReferencedAssemblies.Add(typeof(YoloWrapper).Assembly.GetName().Name + ".exe");

                foreach (string f in Directory.GetFiles(Environment.CurrentDirectory, "*.dll"))
                {
                    compilerparams.ReferencedAssemblies.Add(f);
                }
                string          allcode = code;
                CompilerResults results = RuntimeCodeCompiler.CompileCode(code);

                if (results.Errors.HasErrors)
                {
                    var errors = new StringBuilder("Compiler Errors :\r\n");
                    foreach (CompilerError error in results.Errors)
                    {
                        errors.AppendFormat("Line {0},{1}\t: {2}\n",
                                            error.Line, error.Column, error.ErrorText);
                    }
                    Console.Write("-");
                    compileErrors = errors.ToString();
                    throw new Exception(errors.ToString());
                }
                else
                {
                    CompiledExtensionFunctions += "\n " + code;
                }

                //Assembly assm = results.CompiledAssembly;
                //INewCEPExpression expr2 = Yolo.Express();
                //foreach (Type t in assm.DefinedTypes)
                //{
                //    object wrapper = Activator.CreateInstance(t);
                //    expr2 = (INewCEPExpression)t.InvokeMember("Generate", BindingFlags.InvokeMethod, null, wrapper, null);
                //}

                //if (item.ItemType == YoloItemType.Component)
                //{
                //    Yolo.Manager.Register(expr2 as INewComponent, item.Name, 0);
                //}
                //if (item.ItemType == YoloItemType.Extension)
                //{
                //    Yolo.Expressions.AddExpression(item.Name);
                //}
                compileErrors = "";
                //return expr2;

                return(null);
            }
Example #3
0
            public static INewCEPExpression MakeExpr(string expr, out string compileErrors)
            {
                Yolo.Express();
                //string code = Resource1.template;
                string code = Resources.templateExpr.Replace(REPLACE_TOKEN, expr);
                //string code = File.ReadAllText(TEMPLATE_PATH).Replace(REPLACE_TOKEN, lambda);

                var provider = new CSharpCodeProvider(
                    new Dictionary <String, String> {
                    { "CompilerVersion", "v4.0" }
                });
                ICodeCompiler compiler = provider.CreateCompiler();

                var compilerparams = new CompilerParameters();

                compilerparams.GenerateExecutable = false;
                compilerparams.GenerateInMemory   = true;

                IEnumerable <string> referencedAssemblies = typeof(YoloWrapper).Assembly.GetReferencedAssemblies().Select(a => a.Name);

                foreach (string referencedAssembly in referencedAssemblies)
                {
                    string name = referencedAssembly + ".dll";
                    if (name != "WindowsBase.dll")
                    {
                        compilerparams.ReferencedAssemblies.Add(name);
                    }
                }

                //compilerparams.ReferencedAssemblies.Add(typeof(YoloWrapper).Assembly.GetName().Name + ".exe");

                foreach (string f in Directory.GetFiles(Environment.CurrentDirectory, "*.dll"))
                {
                    compilerparams.ReferencedAssemblies.Add(f);
                }

                CompilerResults results = RuntimeCodeCompiler.CompileCode(code);

                if (results.Errors.HasErrors)
                {
                    var errors = new StringBuilder("Compiler Errors :\r\n");
                    foreach (CompilerError error in results.Errors)
                    {
                        errors.AppendFormat("Line {0},{1}\t: {2}\n",
                                            error.Line, error.Column, error.ErrorText);
                    }
                    Console.Write("-");
                    compileErrors = errors.ToString();
                    throw new Exception(errors.ToString());
                }
                compileErrors = "";

                Assembly assm = results.CompiledAssembly;

                foreach (Type t in assm.DefinedTypes)
                {
                    object wrapper = Activator.CreateInstance(t);
                    return((INewCEPExpression)t.InvokeMember("Generate", BindingFlags.InvokeMethod, null, wrapper, null));
                }
                compileErrors = "";
                return(null);
            }