private void createRobotServerMappingFile(List<Element> msgElements, String module) throws Exception { List<MessageObject> toGSMsgs = new ArrayList<MessageObject>(); for (Iterator i = msgElements.iterator(); i.hasNext(); ) { Element message = (Element) i.next(); MessageObject msg = new MessageObject(); String msgType = message.getAttributeValue("type"); msg.setType(msgType); msg.setClassName(GeneratorHelper.generateServerClassName(msgType)); String msgTypePrefix = msgType.substring(1, 2); toGSMsgs.add(msg); } if (toGSMsgs.size() == 0) { return; } VelocityContext context = new VelocityContext(); context.put("module", module); String mappingClassName = StringUtils.upperCaseFirstCharOnly(module) + "MsgMappingProvider"; context.put("classname", mappingClassName); if (toGSMsgs.size() > 0) { context.put("msgs", toGSMsgs); String outputFile = robotRootPath + "msg" + File.separator + mappingClassName + ".java"; GeneratorHelper.generate(context, robotMappingTemplate_game, outputFile); } }
/** * 生成服务器端文件,每个消息一个文件 * * @param messages * @throws Exception */ private void createServerFiles(List<Element> messages, String module) throws Exception { for (Iterator i = messages.iterator(); i.hasNext(); ) { Element msgElement = (Element) i.next(); MessageObject msgObj = new MessageObject(); String msgType = msgElement.getAttributeValue("type"); msgObj.setType(msgType); msgObj.setClassName(GeneratorHelper.generateServerClassName(msgType)); msgObj.setModule(module); msgObj.setComment(msgElement.getAttributeValue("comment")); msgObj.setHandleMethodName(GeneratorHelper.generateHandleMethodName(msgType)); if (msgElement.getAttributeValue("playerQueue") != null) { msgObj.setPlayerQueue( msgElement.getAttributeValue("playerQueue").equals("true") ? true : false); } if (msgElement.getAttributeValue("friendQueue") != null) { msgObj.setFriendQueue(msgElement.getAttribute("friendQueue").getValue().equals("true")); } if (msgElement.getAttributeValue("guildQueue") != null) { msgObj.setGuildQueue(msgElement.getAttribute("guildQueue").getValue().equals("true")); } List fElements = msgElement.getChildren("field", NAME_SPACE); setMsgObjFields(msgObj, fElements, false, false); VelocityContext context = new VelocityContext(); context.put("message", msgObj); context.put("list", msgObj.getFields()); String templateFileName = ""; String outputFile = ""; for (int j = 0; j < serverMsgTemplates.length; j++) { String templateName = serverMsgTemplates[j]; if (templateName.substring(0, 2).equalsIgnoreCase(msgType.substring(0, 2))) { templateFileName = templateName; char lastCharOfTempate = templateName.charAt(templateName.length() - 4); switch (lastCharOfTempate) { case 'g': // 放在GameServer outputFile = gameRootPath + msgObj.getModule() + File.separator + "msg" + File.separator + msgObj.getClassName() + ".java"; break; default: throw new RuntimeException("模板名称非法," + templateName); } GeneratorHelper.generate(context, templateFileName, outputFile); // JavaGeneratorHelper.generateJavaFile(msgObj, outputFile + "0"); } } msgs.put(msgObj.getClassName(), msgObj); } }
/** * 生成客户端文件,每个模块一个文件 * * @param msgElements * @throws Exception */ private void createClientFile( List<Element> msgElements, String module, List<Element> contantElements) throws Exception { List<MessageObject> cgMsgs = new ArrayList<MessageObject>(); List<MessageObject> gcMsgs = new ArrayList<MessageObject>(); List<MessageObject> allClientMsgs = new ArrayList<MessageObject>(); for (Iterator i = msgElements.iterator(); i.hasNext(); ) { Element mElement = (Element) i.next(); MessageObject msgObj = new MessageObject(); String msgType = mElement.getAttributeValue("type"); fields.put(msgType, msgObj.getFields()); msgObj.setType(msgType); msgObj.setClassName(GeneratorHelper.generateServerClassName(msgType)); msgObj.setModule(mElement.getAttributeValue("module")); msgObj.setComment(mElement.getAttributeValue("comment")); msgObj.setHandleMethodName(GeneratorHelper.generateHandleMethodName(msgType)); List fElements = mElement.getChildren("field", NAME_SPACE); this.setMsgObjFields(msgObj, fElements, true, false); if (msgType.substring(0, 2).equalsIgnoreCase("cg")) { cgMsgs.add(msgObj); } else if (msgType.substring(0, 2).equalsIgnoreCase("gc")) { gcMsgs.add(msgObj); } if (msgType.contains("c") || msgType.contains("C")) { allClientMsgs.add(msgObj); } } this.createClientModel(allClientMsgs, module); List<ConstantObject> contants = new ArrayList<ConstantObject>(); for (Iterator i = contantElements.iterator(); i.hasNext(); ) { Element constantElement = (Element) i.next(); String constantName = constantElement.getAttributeValue("name"); String constantDesc = constantElement.getValue(); ConstantObject constantObj = new ConstantObject( constantName, constantDesc .replaceAll(File.separator + "n", "") .replaceAll(File.separator + "r", "") .trim()); contants.add(constantObj); } VelocityContext context = new VelocityContext(); context.put("module", StringUtils.upperCaseFirstCharOnly(module)); context.put("modulename", module); for (String templateFileName : clientMsgTemplates) { String outputFileNameSuffix = null; String templatePrefix = templateFileName.substring(0, 2); if (templatePrefix.equals("gc")) { if (gcMsgs.size() == 0) { continue; } context.put("msgs", gcMsgs); outputFileNameSuffix = "GCMessage.as"; } else if (templatePrefix.equals("cg")) { if (cgMsgs.size() == 0) { continue; } context.put("msgs", cgMsgs); outputFileNameSuffix = "CGMessage.as"; } context.put("constants", contants); String outputFilePath; if (replaceDirectly) { outputFilePath = messagePath + StringUtils.upperCaseFirstCharOnly(module) + outputFileNameSuffix; } else { outputFilePath = clientRootPath + File.separator + module + File.separator + "message" + File.separator + StringUtils.upperCaseFirstCharOnly(module) + outputFileNameSuffix; } GeneratorHelper.generate(context, templateFileName, outputFilePath); } }