/// <summary> /// 生成流程图脚本 /// </summary> /// <param name="FlowID">流程图ID</param> /// <param name="data">流程图数据</param> /// <param name="manager">流程图管理器</param> /// <returns>是否生成成功</returns> public override bool GenerateCode(int FlowID, List<FlowChartMetaData> data, FlowChartMetaDataManager manager) { // 初始化数据 string fullPath = Path.Combine(rootDirectory, "scripts\\Map"); fullPath = Path.Combine(fullPath, scriptPath); string strOutDir = fullPath; this.m_outdir = fullPath; this.m_rootdir = rootDirectory; this.m_mapname = manager.MapName; #if DEBUG m_lua.Init(); //重载载入编译模板 #endif RegistLuaFunction(manager); // 注册lua函数 LuaFunction fun = m_lua.GetFunction("Compiler.main"); string strCurrTemp = Path.Combine(Application.StartupPath, "eventimp.template"); object[] rets = fun.Call(strCurrTemp); if (rets != null) { string s = rets[0].ToString(); DirectoryInfo dinfo = Directory.GetParent(strOutDir); dinfo.Create(); #region 测试代码 File.WriteAllText(strOutDir + ".temp.obj", s, m_gb2312Encoding); #endregion m_cwm.Init(); try { m_lua.DoString(s); ExportLib(); // 导出库文件 return true; } catch(Exception e) { throw e; } finally { if (saveFile != null) { // 为多语言版本转换中文字符串 string scriptText = m_cwm.ToString(); saveFile(string.Format("{0}.lua", strOutDir), scriptText); } } } return false; }
/// <summary> /// 注册lua函数 /// </summary> private void RegistLuaFunction(FlowChartMetaDataManager manager) { m_lua.RegisterFunction("GetEventCount", manager, typeof(FlowChartMetaDataManager).GetMethod("GetEventCount")); m_lua.RegisterFunction("GetEventFunctionName", manager, typeof(FlowChartMetaDataManager).GetMethod("GetEventFunctionName")); m_lua.RegisterFunction("GetNodeNames", manager, typeof(FlowChartMetaDataManager).GetMethod("GetNodeNames")); m_lua.RegisterFunction("GetNodeCount", manager, typeof(FlowChartMetaDataManager).GetMethod("GetNodeCount")); m_lua.RegisterFunction("GetNodeName", manager, typeof(FlowChartMetaDataManager).GetMethod("GetNodeName")); m_lua.RegisterFunction("GetNodeType", manager, typeof(FlowChartMetaDataManager).GetMethod("GetNodeType")); m_lua.RegisterFunction("GetNodeCode", manager, typeof(FlowChartMetaDataManager).GetMethod("GetNodeCode")); m_lua.RegisterFunction("GetNextConnetionCount", manager, typeof(FlowChartMetaDataManager).GetMethod("GetNextConnetionCount")); m_lua.RegisterFunction("IsNextNodeFinish", manager, typeof(FlowChartMetaDataManager).GetMethod("IsNextNodeFinish")); m_lua.RegisterFunction("GetNextNodeName", manager, typeof(FlowChartMetaDataManager).GetMethod("GetNextNodeName")); m_lua.RegisterFunction("GetNextThreadCount", manager, typeof(FlowChartMetaDataManager).GetMethod("GetNextThreadCount")); m_lua.RegisterFunction("GetNodeSwitchCount", manager, typeof(FlowChartMetaDataManager).GetMethod("GetNodeSwitchCount")); m_lua.RegisterFunction("GetConditionResult", manager, typeof(FlowChartMetaDataManager).GetMethod("GetConditionResult")); m_lua.RegisterFunction("GetNodeText", manager, typeof(FlowChartMetaDataManager).GetMethod("GetNodeText")); m_lua.RegisterFunction("GetEventText", manager, typeof(FlowChartMetaDataManager).GetMethod("GetEventText")); m_lua.RegisterFunction("GetEventData", manager, typeof(FlowChartMetaDataManager).GetMethod("GetEventData")); // m_lua.RegisterFunction("log", this, typeof(Main).GetMethod("Log")); m_lua.RegisterFunction("ExpToString", this, typeof(CodeProviderClass).GetMethod("ExpToString")); m_lua.RegisterFunction("SetScript", this, typeof(Main).GetMethod("SetScript")); m_lua["out"] = m_cwm; }
/// <summary> /// 生成脚本 /// </summary> /// <returns>是否生成成功</returns> protected virtual bool GenerateCode() { bool success = true; Helper helper = Helper.GetHelper(); DocumentManager documentManager = DocumentManager.GetDocumentManager(); Hashtable fileTable = documentManager.FileTable; FlowChartManager flowChartManager = documentManager.CurrentFlowChartManager; if (flowChartManager == null) { return false; } string id = flowChartManager.ID; GraphManager graphManager = flowChartManager.CurrentGraphManager; DataManager dataManager = flowChartManager.CurrentDataManager; List<SlotContainer> slotContainerList = graphManager.SlotContainerList; List<FlowChartMetaData> metaDataList = GetMetaData(dataManager, slotContainerList); if (compiler == null) { return false; } compiler.PrintDebugInformation = documentManager.PrintText; compiler.SaveTheFile = documentManager.SaveTextToFile; FlowChartMetaDataManager flowChartMetaDataManager = new FlowChartMetaDataManager(int.Parse(id), metaDataList); fileTable.Clear(); documentManager.PrintText("------ 开始初始化脚本 ------"); // 设置生成脚本参数 compiler.RootDirectory = helper.OutputDir; compiler.ScriptPath = flowChartManager.CurrentPath; flowChartMetaDataManager.MapName = flowChartManager.MapName; flowChartMetaDataManager.Version = flowChartMetaDataManager.Version; try { success = compiler.GenerateCode(int.Parse(id), metaDataList, flowChartMetaDataManager); } catch (Exception ex) // 抛出异常则输出调试信息 { documentManager.PrintText(string.Format("异常信息:{0}", ex.Message)); success = false; } if (!success) { documentManager.PrintText("初始化脚本失败!"); return false; } documentManager.PrintText("初始化脚本成功!"); // 写文件 documentManager.PrintText("------ 开始生成脚本 ------"); foreach (string fileName in fileTable.Keys) { success = helper.SaveFile(fileName, fileTable[fileName] as string); if (success) // 写文件成功 { documentManager.PrintText(string.Format("生成脚本文件 {0} 成功!", fileName)); } else { documentManager.PrintText(string.Format("生成脚本文件 {0} 失败!", fileName)); break; } } if (!success) { return false; } documentManager.PrintText(string.Format("====== 所有文件都成功生成!共生成{0}个文件 ======", fileTable.Keys.Count)); return success; }
/// <summary> /// 生成脚本 /// </summary> /// <param name="chartID">绘图ID</param> /// <param name="data">绘图数据</param> /// <param name="manager">绘图数据管理器</param> /// <returns>是否生成成功</returns> public abstract bool GenerateCode(int chartID, List<FlowChartMetaData> data, FlowChartMetaDataManager manager);
/// <summary> /// 编辑流程图 /// </summary> /// <param name="FlowID">流程图ID</param> /// <param name="data">元数据链表</param> /// <param name="manager">元数据管理器</param> /// <param name="flowVersion">绘图的版本号</param> /// <returns>是否编译成功</returns> public override bool GenerateCode(int FlowID, List<FlowChartMetaData> data, FlowChartMetaDataManager manager) { // 初始化数据 string[] dataArray = scriptPath.Split(new char[] { '\\' }); string shortPath = dataArray[1]; for (int i = 2; i < dataArray.Length; i++) { shortPath = shortPath + string.Format("\\{0}", dataArray[i]); } string fullPath = string.Format(@"{0}\scripts\Map\{1}\ai\{2}", rootDirectory.TrimEnd(new char[] { '\\' }), dataArray[0], shortPath); bool success = true; DataBaseManager dataBaseManager = DataBaseManager.GetDataBaseManager(); customFunctionList.Clear(); // 初始化参数 StringBuilder code = new StringBuilder(); string initialStateName = CodeProviderClass.GetStateString(manager.GetEventData(1)); // 初态的变量名 List<FlowChartMetaData> metaDataList = manager.GetAllNodes(); List<FlowChartMetaData> stateList = new List<FlowChartMetaData>(); List<FlowChartMetaData> actionList = new List<FlowChartMetaData>(); Hashtable registActions = new Hashtable(); string sqlString = "SELECT actionid, parms FROM AI_Action_Define"; actionTable = dataBaseManager.GetDataTable(sqlString, dataBaseManager.Connection_Jx3web); foreach (FlowChartMetaData metaData in metaDataList) { switch (metaData.DataType) { case "AIStateNode": { stateList.Add(metaData); break; } case "AIActionNode": { actionList.Add(metaData); string[] information = CodeProviderClass.GetActionInformation(metaData.Data); int actionID = int.Parse(information[2]); if ((actionID == 0) || (actionID > 1000 && registActions[information[2]] == null)) // 注册自定义动作 { registActions[information[2]] = metaData; } break; } } } // 添加注释 code.AppendLine(string.Format("-- 编写者:{0}", Dns.GetHostName())); code.AppendLine(string.Format("-- 版本号:{0}", manager.Version)); code.AppendLine(); // 引用头文件 code.AppendLine("Include('scripts/ai/customFuctions.lua')"); code.AppendLine("Include('scripts/ai/AIParam.lua')"); code.AppendLine("Include('scripts/ai/argumentStrings.ls')"); code.AppendLine(); code.AppendLine("g_AIDebugInfo = {}"); code.AppendLine(); // 声明变量 code.AppendLine("-- 声明状态变量"); foreach (FlowChartMetaData stateNode in stateList) { string stateName = stateNode.Code; code.AppendLine(string.Format("local {0} = {1} -- {2}", stateName, stateNode.ID, stateNode.DisplayText)); } code.AppendLine(); // 生成自定义附加脚本 FlowChartInteractor it = Interactor.GetInteractor().CurrentFlowChartInteractor; string customCode = it.CustomText; if(customCode != null && customCode != "") { code.AppendLine("-- 自定义附加脚本"); code.AppendLine(string.Format("{0}", customCode)); code.AppendLine(); } // 写主方法 code.AppendLine("function Setup(ai)"); code.AppendLine(" local state"); code.AppendLine(" local action"); code.AppendLine(" local param = g_AIParam[ai.nAIType]"); code.AppendLine(); // 注册自定义动作 code.AppendLine(" -- 注册自定义动作"); foreach (object o in registActions.Values) { FlowChartMetaData actionNode = o as FlowChartMetaData; string[] information = CodeProviderClass.GetActionInformation(actionNode.Data); code.AppendLine(string.Format(" ai.RegisterUserAction({0}, '{1}')", information[2], information[0])); } code.AppendLine(); List<FlowChartMetaData> list = new List<FlowChartMetaData>(); GenerateCode(code, manager.GetEventNode(1), list); code.AppendLine(string.Format(" ai.SetInitState({0})", initialStateName)); code.AppendLine("end"); code.AppendLine(); // 调试方法定义 code.AppendLine("-- 调试方法"); code.AppendLine("function DebugOnCallAction(action_id)"); code.AppendLine(" Log('[AI] Call action: ' .. g_AIDebugInfo[action_id])"); code.AppendLine("end"); // 写本文件自定义脚本动作定义 if(customFunctionList.Count > 0) { code.AppendLine(); code.AppendLine("-- 本AI图内自定义脚本"); foreach(string s in customFunctionList) { code.AppendLine(string.Format("{0}", s)); code.AppendLine(); } } // 提取转换中文字符串 string scriptText = code.ToString(); scriptText = ConvertScriptText(scriptText); // 输出文件 string fileName = string.Format("{0}.lua", fullPath); saveFile(fileName, scriptText); string userActionScripts = dataBaseManager.GetUserActionContext(); fileName = Path.Combine(rootDirectory, @"scripts\ai\customFuctions.lua"); saveFile(fileName, userActionScripts); fileName = Path.Combine(rootDirectory, @"scripts\ai\argumentStrings.ls"); string lsContent = ConstructLSFile(); saveFile(fileName, lsContent); return success; }