private static void Generate_cpp_file(AIRuntimeTaskData task) { string ai_n_c = ai_c; ai_n_c = ai_n_c.Replace("[*Name*]", task.type); string dest_c = Path.Combine(path_dest, "AIRuntime" + task.type + ".cpp"); if (task.vars != null) { string rel_str = string.Empty; for (int i = 0, max = task.vars.Count; i < max; i++) { AIVar var = task.vars[i]; if (var.type == "GameObject" || var.type == "Transform") { rel_str += "delete " + var.name + ";"; } } ai_n_c = ai_n_c.Replace("[*arg-0*]", rel_str); //Init(AITaskData* data) string init_str = string.Empty; for (int i = 0, max = task.vars.Count; i < max; i++) { string val = "data->vars[\"" + task.vars[i].name + "\"]->val"; string op = GetOpByType(task.vars[i].type, val); if (!string.IsNullOrEmpty(op)) { if (task.vars[i] is AITreeSharedVar) { AITreeSharedVar var = task.vars[i] as AITreeSharedVar; if (!var.IsShared) { init_str += var.name + " = " + op + " \n\t"; } } else { init_str += task.vars[i].name + " = " + op + " \n\t"; } } } ai_n_c = ai_n_c.Replace("[*arg-1*]", init_str); //OnTick() string tick_str = string.Empty; string imp_str = string.Empty; if (task.vars != null) { for (int i = 0, max = task.vars.Count; i < max; i++) { imp_str += "," + task.vars[i].name; if (task.vars[i] is AITreeSharedVar) { AITreeSharedVar var = task.vars[i] as AITreeSharedVar; if (var.IsShared) { tick_str += var.name + " = " + GetTreeVarCode(var.type, var.BindName) + "\n\t"; } } } } ai_n_c = ai_n_c.Replace("[*arg-2*]", tick_str); ai_n_c = ai_n_c.Replace("[*tickarg*]", imp_str); } else { ai_n_c = ai_n_c.Replace("[*arg-0*]", ""); ai_n_c = ai_n_c.Replace("[*arg-1*]", ""); ai_n_c = ai_n_c.Replace("[*arg-2*]", ""); ai_n_c = ai_n_c.Replace("[*tickarg*]", ""); } File.WriteAllText(dest_c, ai_n_c); }
private static void GenerateTaskCode(AIRuntimeTaskData task) { //声明代码的部分 CodeCompileUnit compunit = new CodeCompileUnit(); CodeNamespace sample = new CodeNamespace("AI.Runtime"); //引用命名空间 sample.Imports.Add(new CodeNamespaceImport("UnityEngine")); compunit.Namespaces.Add(sample); //在命名空间下添加一个类 CodeTypeDeclaration wrapClass = new CodeTypeDeclaration("AIRuntime" + task.type + " : AIRunTimeBase"); sample.Types.Add(wrapClass); //添加成员 if (task.vars != null) { for (int i = 0, max = task.vars.Count; i < max; i++) { CodeTypeReference type = new CodeTypeReference(task.vars[i].type); CodeMemberField field = new CodeMemberField(type, task.vars[i].name); field.Attributes = MemberAttributes.Public; wrapClass.Members.Add(field); } } //为这个类添加一个方法 Init() CodeMemberMethod method = new CodeMemberMethod(); method.Name = "Init"; method.Attributes = MemberAttributes.Override | MemberAttributes.Public; method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(AIRuntimeTaskData), "data")); method.ReturnType = new CodeTypeReference(typeof(void));//返回值 AddState(method, "base.Init(data);"); if (task.vars != null) { for (int i = 0, max = task.vars.Count; i < max; i++) { if (task.vars[i] is AITreeSharedVar) { AITreeSharedVar var = task.vars[i] as AITreeSharedVar; if (!var.IsShared) { InnerMakeTask(var, i, method); } } else { AIVar var = task.vars[i]; if (var.val != null) { InnerMakeTask(var, i, method); } } } } //OnTick() CodeMemberMethod method2 = new CodeMemberMethod(); method2.Name = "OnTick"; method2.Attributes = MemberAttributes.Override | MemberAttributes.Public; method2.Parameters.Add(new CodeParameterDeclarationExpression(typeof(XEntity), "entity")); method2.ReturnType = new CodeTypeReference("AIRuntimeStatus");//返回值 string state = "return AITreeImpleted." + task.type + "Update(entity"; if (task.vars != null) { for (int i = 0, max = task.vars.Count; i < max; i++) { state += ", " + task.vars[i].name; if (task.vars[i] is AITreeSharedVar) { AITreeSharedVar var = task.vars[i] as AITreeSharedVar; if (var.IsShared) { string st = "var in" + var.name + " = _tree.GetVariable(\"" + var.BindName + "\"); "; AddState(method2, st); st = "if (in" + var.name + " != null) " + var.name + " = (" + var.type + ")in" + var.name + ";"; AddState(method2, st); } } } } state += ");"; method2.Statements.Add(new CodeSnippetStatement("\t\t\t" + state)); wrapClass.Members.Add(method); wrapClass.Members.Add(method2); //output StringBuilder fileContent = new StringBuilder(); using (StringWriter sw = new StringWriter(fileContent)) { CodeDomProvider.CreateProvider("CSharp").GenerateCodeFromCompileUnit(compunit, sw, new CodeGeneratorOptions()); } string filePath = unity_AI_code + "AIRuntime" + task.type + ".cs"; if (File.Exists(filePath)) { File.Delete(filePath); } File.WriteAllText(filePath, fileContent.ToString()); maker_list.Add(task.type); }