为了实现快捷的读取转换将荣地图,以及突破,终于我决定从将荣下手了,这个计划叫界限计划,此几个章节可能不涉及libgdx

1.首先通过对以前ncp的excel地图编辑器的分析,整理出来数据结构如下

<Files readRule="1" ruleId="1" version="0518">
<BtlInfos id="BtlInfos" ifCycle="false" Count="one" remark="主数据" >
    <BtlInfo name="biUnknown01" size="8" remark="biUnknown01" defaul="0" type="Long"/>
    <BtlInfo name="biMapWidth" size="8" remark="biMapWidth" defaul="0" type="Long"/>
    <BtlInfo name="biMapHeight" size="8" remark="biMapHeight" defaul="0" type="Long"/>
    <BtlInfo name="biTerrain" size="8" remark="biTerrain" defaul="0" type="Long"/>
    <BtlInfo name="biLegionNum" size="8" remark="biLegionNum" defaul="0" type="Long"/>
    <BtlInfo name="biArmyNum" size="8" remark="biArmyNum" defaul="0" type="Long"/>
    <BtlInfo name="biEventNum" size="8" remark="biEventNum" defaul="0" type="Long"/>
    <BtlInfo name="biVictory" size="8" remark="biVictory" defaul="0" type="Long"/>
    <BtlInfo name="biVictoryRound" size="8" remark="biVictoryRound" defaul="0" type="Long"/>
    <BtlInfo name="biGreatVictoryRound" size="8" remark="biGreatVictoryRound" defaul="0" type="Long"/>
    <BtlInfo name="biUnknown11" size="8" remark="biUnknown11" defaul="0" type="Long"/>
    <BtlInfo name="biUnknown12" size="8" remark="biUnknown12" defaul="0" type="Long"/>
    <BtlInfo name="biVersion" size="8" remark="biVersion" defaul="0" type="Long"/>
    <BtlInfo name="biUnknown14" size="8" remark="biUnknown14" defaul="0" type="Long"/>
    <BtlInfo name="biUnknown15" size="8" remark="biUnknown15" defaul="0" type="Long"/>
    <BtlInfo name="biUnknown16" size="8" remark="biUnknown16" defaul="0" type="Long"/>
    </BtlInfos>
    
    
<BtlLegions id="BtlLegions" ifCycle="true" Count="biLegionNum" remark="军团数据" >
    <BtlLegion name="blProduction" size="8" remark="blProduction" defaul="0" type="Long"/>
    <BtlLegion name="blIndustry" size="8" remark="blIndustry" defaul="0" type="Long"/>
    <BtlLegion name="blSelect" size="8" remark="blSelect" defaul="0" type="Long"/>
    <BtlLegion name="blColor" size="8" remark="blColor" defaul="0" type="Long"/>
    <BtlLegion name="blDefeat" size="8" remark="blDefeat" defaul="0" type="Long"/>
    <BtlLegion name="blName" size="32" remark="blName" defaul="0" type="String"/>
    <BtlLegion name="blCountry" size="32" remark="blCountry" defaul="0" type="String"/>
    <BtlLegion name="blProductionRate" size="8" remark="blProductionRate" defaul="0" type="Single"/>
    <BtlLegion name="blUnknown09" size="8" remark="blUnknown09" defaul="0" type="Single"/>
    <BtlLegion name="blInfantryForce" size="8" remark="blInfantryForce" defaul="0" type="Long"/>
    <BtlLegion name="blArtilleryForce" size="8" remark="blArtilleryForce" defaul="0" type="Long"/>
    <BtlLegion name="blArmoredForce" size="8" remark="blArmoredForce" defaul="0" type="Long"/>
    <BtlLegion name="blNavyForce" size="8" remark="blNavyForce" defaul="0" type="Long"/>
    <BtlLegion name="blAirForce" size="8" remark="blAirForce" defaul="0" type="Long"/>
    <BtlLegion name="blFinancial" size="8" remark="blFinancial" defaul="0" type="Long"/>
    <BtlLegion name="blUnknown16" size="8" remark="blUnknown16" defaul="0" type="Long"/>
    <BtlLegion name="blUnknown17" size="8" remark="blUnknown17" defaul="0" type="Long"/>
    <BtlLegion name="blUnknown18" size="8" remark="blUnknown18" defaul="0" type="Long"/>
    <BtlLegion name="blUnknown19" size="8" remark="blUnknown19" defaul="0" type="Long"/>
    </BtlLegions>
    
    
<BtlTiles id="BtlTiles" ifCycle="true" Count="sumGride" remark="地形数据" >
    <!-- <BtlTile name="btID" size="8" remark="btID" defaul="0" type="Id"/> -->
    <BtlTile name="btLegion" size="2" remark="btLegion" defaul="0" type="Byte"/>
    <BtlTile name="btBuild" size="2" remark="btBuild" defaul="0" type="Byte"/>
    <BtlTile name="btBuildLevel" size="2" remark="btBuildLevel" defaul="0" type="Byte"/>
    <BtlTile name="btInstallation" size="2" remark="btInstallation" defaul="0" type="Byte"/>
    <BtlTile name="btTerrain1" size="2" remark="btTerrain1" defaul="0" type="Byte"/>
    <BtlTile name="btTile1" size="2" remark="btTile1" defaul="0" type="Byte"/>
    <BtlTile name="btTerrain2" size="2" remark="btTerrain2" defaul="0" type="Byte"/>
    <BtlTile name="btTile2" size="2" remark="btTile2" defaul="0" type="Byte"/>
    <BtlTile name="btIsStrongPointF" size="2" remark="btIsStrongPointF" defaul="0" type="Byte"/>
    <BtlTile name="btEvent" size="2" remark="btEvent" defaul="0" type="Byte"/>
    <BtlTile name="btAreaName" size="4" remark="btAreaName" defaul="0" type="Integer"/>
    </BtlTiles>
    
    
<BtlArmys id="BtlArmys" ifCycle="true" Count="biArmyNum" remark="兵种数据" >
    <BtlArmy name="baCard" size="2" remark="baCard" defaul="0" type="Byte"/>
    <BtlArmy name="baMovement" size="2" remark="baMovement" defaul="0" type="Byte"/>
    <BtlArmy name="baTile" size="4" remark="baTile" defaul="BtlArmy" type="Integer"/>
    
    <BtlArmy name="baStrengthNow" size="4" remark="baStrengthNow" defaul="0" type="Integer"/>
    <BtlArmy name="baStrengthMax" size="4" remark="baStrengthMax" defaul="0" type="Integer"/>
    <BtlArmy name="baUnknown07" size="8" remark="baUnknown07" defaul="0" type="Long"/>
    <BtlArmy name="baUnknown08" size="2" remark="baUnknown08" defaul="0" type="Byte"/>
    <BtlArmy name="baLevel" size="2" remark="baLevel" defaul="0" type="Byte"/>
    <BtlArmy name="baUnknown09" size="2" remark="baUnknown09" defaul="0" type="Byte"/>
    <BtlArmy name="baUnknown10" size="2" remark="baUnknown10" defaul="0" type="Byte"/>
    <BtlArmy name="baCommander" size="2" remark="baCommander" defaul="0" type="Byte"/>
    <BtlArmy name="baUnknown16" size="2" remark="baUnknown16" defaul="0" type="Byte"/>
    <BtlArmy name="baHasTroopship" size="2" remark="baHasTroopship" defaul="0" type="Byte"/>
    <BtlArmy name="baUnknown17" size="2" remark="baUnknown17" defaul="0" type="Byte"/>
    <BtlArmy name="baUnknown18" size="2" remark="baUnknown18" defaul="0" type="Byte"/>
     <BtlArmy name="baUnknown19" size="2" remark="baUnknown19" defaul="0" type="Byte"/> 
     <BtlArmy name="baUnknown20" size="2" remark="baUnknown20" defaul="0" type="Byte"/> 
    <BtlArmy name="baEvent" size="2" remark="baEvent" defaul="0" type="Byte"/>
    </BtlArmys>
    
<BtlEvents id="BtlEvents" ifCycle="true" Count="biEventNum" remark="剧情数据" >
     <BtlEvent name="beID" size="8" remark="beID" defaul="0" type="Long"/> 
    <BtlEvent name="beTrigger" size="8" remark="beTrigger" defaul="0" type="Long"/>
    <BtlEvent name="beMorale" size="8" remark="beMorale" defaul="0" type="Long"/>
    <BtlEvent name="beRound" size="8" remark="beRound" defaul="0" type="Long"/>
    <BtlEvent name="beDialogue" size="8" remark="beDialogue" defaul="0" type="Long"/>
    <BtlEvent name="beLegion" size="8" remark="beLegion" defaul="0" type="Long"/>
    <BtlEvent name="blUnknown" size="32" remark="beLegion" defaul="0" type="String"/>
    </BtlEvents>
</Files>
View Code

解析器的思路为:

1.首先创建对应类,存储全部为String,因为e社的类型包括单精度,ascii码等,并不全是数字

2.分析类型,制作出结构xml(上面的xml),创建结构类,加载这个xml

3.根据结构类,读取bin16进制数据并转换

4.把结果赋值,并使用反射优化过程

代码如下

package code;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import code.dao.DefRule;
import code.dao.GG1DAO;
import code.dao.gg1.BtlArmy;
import code.dao.gg1.BtlEvent;
import code.dao.gg1.BtlInfo;
import code.dao.gg1.BtlLegion;
import code.dao.gg1.BtlTile;

public class ToolUtil {
    //加载gogBtl
    public static GG1DAO LoadGG1Btl(String path) {
        byte[] rsbt=null;
        GG1DAO gg1 = new GG1DAO();
         BtlInfo bi;
         BtlLegion bl;
         BtlTile bt;
         BtlArmy ba;
         BtlEvent be;
          List<BtlLegion> bls;
          List<BtlTile> bts;
          List<BtlArmy> bas;
          List<BtlEvent> bes;
        
        try {
            rsbt = ComUtil.readFile(path);//"D:\\test1.bin"
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        StringBuilder buf = new StringBuilder();
        int line = 0;// 十六进制标记
        for (byte d : rsbt) {
            if (line % 1 == 0) {
                buf.append(String.format("%02x", d));
                // System.out.println(String.format("%02x", d));
                // buf.append(",") ;
                line++;
            }
        }
        int cutTag=0;
        String cutStr="";
        int bufTag=0;
        int cutSumCt=1;//总循环次数
        int mapW,mapH,i;
        JSONObject row;//{"Count":"biLegionNum","id":"BtlLegions","ifCycle":"true","nodeList":[{}]}
        List<DefRule> rs;
        boolean ifCycle;
        Map rsMap = null;
        Map biMap = null;
        {   //得到相关信息,必须加载
            cutStr="BtlInfos";
             row=getInfoByRootName(Config.Rules.GG1_RULE,cutStr);
             rs=getDefRuleInfosByRow(row);
             bi=new BtlInfo();
             rsMap=cutBtl(rs,bi,buf,bufTag,cutSumCt);
             bi=(BtlInfo) rsMap.get("T");
             gg1.setBtlInfo(bi);
             bufTag=Integer.parseInt(rsMap.get("bufTag").toString());
             mapW=Integer.parseInt(bi.getBiMapWidth());
             mapH=Integer.parseInt(bi.getBiMapHeight());
             biMap=ComUtil.ConvertObjToMap(bi);
        }
        
        {   
            Object[] objects =new  Object[]{(new BtlLegion()),(new BtlTile()),(new BtlArmy()),(new BtlEvent())  };
             String[] cutStrs=new String[] {"BtlLegions","BtlTiles","BtlArmys","BtlEvents"};
             for(i=0;i<objects.length;i++) {
             row=getInfoByRootName(Config.Rules.GG1_RULE,cutStrs[i]);
             rs=getDefRuleInfosByRow(row);
             if(row.getBoolean("ifCycle")) {
                 if(row.getString("Count").equals("one")) {
                     cutSumCt=1;
                 }else if(row.getString("Count").equals("sumGride")){
                     cutSumCt=mapW*mapH;
                 }else {
                     cutSumCt=Integer.parseInt((String) biMap.get(row.getString("Count")));
                 }
             }else {
                 cutSumCt=1;
             }
             rsMap=cutBtl(rs,objects[i],buf,bufTag,cutSumCt);
             ComUtil.setVal(gg1, "set"+(cutStrs[i].substring(0, +cutStrs[i].length()-1)), rsMap.get("T"));
             bufTag=Integer.parseInt(rsMap.get("bufTag").toString());
             }
        }
        
        
        
        
        /*{
            cutStr="BtlLegions";
             row=getInfoByRootName(Config.Rules.GG1_RULE,cutStr);
             rs=getDefRuleInfosByRow(row);
             if(row.getBoolean("ifCycle")) {
                 cutSumCt=Integer.parseInt(bi.biLegionNum);
             }else {
                 cutSumCt=1;
             }
             bl=new BtlLegion();
             rsMap=cutBtl(rs,bl,buf,bufTag,cutSumCt);
             bls=(List<BtlLegion>) rsMap.get("T");
             gg1.setBtlLegion(bls);
             bufTag=Integer.parseInt(rsMap.get("bufTag").toString());
        }
        

        {
            cutStr="BtlTiles";
             row=getInfoByRootName(Config.Rules.GG1_RULE,cutStr);
             rs=getDefRuleInfosByRow(row);
             if(row.getBoolean("ifCycle")) {
                 cutSumCt=mapW*mapH;
             }else {
                 cutSumCt=1;
             }
             bt=new BtlTile();
             rsMap=cutBtl(rs,bt,buf,bufTag,cutSumCt);
             bts=(List<BtlTile>) rsMap.get("T");
             gg1.setBtlTile(bts);
             bufTag=Integer.parseInt(rsMap.get("bufTag").toString());
        }
        {
            cutStr="BtlArmys";
             row=getInfoByRootName(Config.Rules.GG1_RULE,cutStr);
             rs=getDefRuleInfosByRow(row);
             if(row.getBoolean("ifCycle")) {
                 cutSumCt=Integer.parseInt(bi.biArmyNum);
             }else {
                 cutSumCt=1;
             }
             ba=new BtlArmy();
             rsMap=cutBtl(rs,ba,buf,bufTag,cutSumCt);
             bas=(List<BtlArmy>) rsMap.get("T");
             gg1.setBtlArmy(bas);
             bufTag=Integer.parseInt(rsMap.get("bufTag").toString());
        }
        {
            cutStr="BtlEvents";
             row=getInfoByRootName(Config.Rules.GG1_RULE,cutStr);
             rs=getDefRuleInfosByRow(row);
             if(row.getBoolean("ifCycle")) {
                 cutSumCt=Integer.parseInt(bi.biEventNum);
             }else {
                 cutSumCt=1;
             }
             be=new BtlEvent();
             rsMap=cutBtl(rs,be,buf,bufTag,cutSumCt);
             bes=(List<BtlEvent>) rsMap.get("T");
             gg1.setBtlEvent(bes);
             bufTag=Integer.parseInt(rsMap.get("bufTag").toString());
        } */
        return gg1;
    }
        
    
   
    
    public static void main(String[] args) {
        /*List<DefRule> rs=getDefRuleInfosByXml(Config.Rules.GG1_RULE,"BtlLegions");
        
        for(DefRule r:rs) {
            System.out.println("Name:"+r.getName()+" Type:"+r.getType()+" Size:"+r.getSize()+" Remark:"+r.getRemark()+" Defaul:"+r.getDefaul());
        }
        
        JSONObject r=getInfoByRootName(Config.Rules.GG1_RULE,"BtlLegions");

        System.out.println(r.toString());
        System.out.println("end");
        */
        
        
        LoadGG1Btl(Config.Btls.GG1_BTL);
        //System.out.println(ComUtil.convertStringToHex("de1"));
        //System.out.println(ComUtil.converString("64653100CDCDCDCDCDCDCDCDCDCDCDCD"));
        
        
        
    }
    
    
  //根据pathName 来获取  List<DefRule> rootName为分节点的id
    public static List<DefRule> getDefRuleInfosByXml(String path,String rootName){
        List<DefRule> defRules=new ArrayList<DefRule>();
        DefRule defRule;
        JSONObject obj = ComUtil.XmlRead(path);
        JSONArray array=obj.getJSONArray("attribute");
        JSONObject row = null;
        JSONObject row2 = null;
        for(int i=0; i<array.size(); i++){
            row = array.getJSONObject(i);
            if (row.get("id").equals(rootName)){
                JSONArray arraycoord = row.getJSONArray("nodeList");;
                for(int j=0; j<arraycoord.size(); j++){
                        row2 = arraycoord.getJSONObject(j);
                        defRule=new DefRule();
                        defRule.setName(row2.getString("name"));
                        defRule.setSize(row2.getIntValue("size"));
                        defRule.setType(row2.getString("type"));
                        defRule.setRemark(row2.getString("remark"));
                        defRule.setDefaul(row2.getString("defaul"));
                        defRules.add(defRule);
                }
                break;
            }
        }
        return defRules;
    }
    
    public static JSONObject getInfoByRootName(String path,String rootName){
        List<DefRule> defRules=new ArrayList<DefRule>();
        DefRule defRule;
        JSONObject obj = ComUtil.XmlRead(path);
        JSONArray array=obj.getJSONArray("attribute");
        JSONObject row = null;
        for(int i=0; i<array.size(); i++){
            row = array.getJSONObject(i);
            if (row.get("id").equals(rootName)){
                return row;
            }
        }
        return row;
    }
    
  //根据pathName 来获取  List<DefRule> rootName为分节点的id
    public static List<DefRule> getDefRuleInfosByRow(JSONObject row){
        List<DefRule> defRules=new ArrayList<DefRule>();
        DefRule defRule;
        JSONObject row2 = null;
                JSONArray arraycoord = row.getJSONArray("nodeList");;
                for(int j=0; j<arraycoord.size(); j++){
                        row2 = arraycoord.getJSONObject(j);
                        defRule=new DefRule();
                        defRule.setName(row2.getString("name"));
                        defRule.setSize(row2.getIntValue("size"));
                        defRule.setType(row2.getString("type"));
                        defRule.setRemark(row2.getString("remark"));
                        defRule.setDefaul(row2.getString("defaul"));
                        defRules.add(defRule);
                }
        return defRules;
    }
    
    //切割btl根据xml的记录
    //<T>
    //bufTag
    public static <T> Map<String, Object> cutBtl(List<DefRule> rs,T item,StringBuilder buf,int bufTag,int cycleCount){
        //BtlInfo mp = new BtlInfo();
        //mp.setBiUnknown01(Integer.parseInt(buf.substring(bufTag, bufTag + mp.getLBiUnknown01()), 16));
        //bufTag=bufTag+mp.getLBiUnknown01();
        Map<String, Object> rsMap=new HashMap<String, Object>();
        List<T> ts=new ArrayList<T>();
        Class clazz=item.getClass();
        Field[] fieldName;
        Class clazs;
        Field f = null;
        int cutL,c;String str=null;
        
        for(c=0;c<cycleCount;c++) {
                 fieldName = clazz.getDeclaredFields();
                try {
                    item = (T) clazz.newInstance();
                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 clazs=item.getClass();
                 for (DefRule r : rs) {
                for(int i=0;i<fieldName.length;i++) {
                    // 创建实例
                     try {
                        f  = clazs.getDeclaredField(fieldName[i].getName());
                    } catch (NoSuchFieldException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                        f.setAccessible(true);
                        if(r.getName().equals(fieldName[i].getName().toString())) {
                            
                            
                            cutL=r.getSize();
                            
                            
                            //System.out.println("B:Name1:"+fieldName[i].getName()+" c:"+c+" bs:"+bufTag+"~"+(bufTag + cutL)+" v:"+buf.substring(bufTag, bufTag + cutL));
                            
                            if(r.getType().equals("Byte")) {
                                str=ComUtil.converByte(buf.substring(bufTag, bufTag + cutL));
                            }else if(r.getType().equals("Integer")) {
                                str=ComUtil.converInteger(buf.substring(bufTag, bufTag + cutL));
                            }else if(r.getType().equals("Long")) {
                                str=ComUtil.converLong(buf.substring(bufTag, bufTag + cutL));
                            }else if(r.getType().equals("Single")) {
                                str=ComUtil.converSingle(buf.substring(bufTag, bufTag + cutL));
                            }else if(r.getType().equals("String")) {
                                str=ComUtil.converString(buf.substring(bufTag, bufTag + cutL));
                            }
                            
                            if(c<3) {
                            System.out.println("E:Name1:"+fieldName[i].getName()+" c:"+c+" bs:"+bufTag+"~"+(bufTag + cutL)+" v:"+buf.substring(bufTag, bufTag + cutL)+" cv:"+str);
                            }
                            bufTag=bufTag+cutL;
                            
                            
                            try {
                                f.set(item, str);
                            } catch (IllegalArgumentException | IllegalAccessException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                }
            }
                 if(item!=null) {
                     ts.add(item);
                 }
        }
        
        if(cycleCount==1) {
            rsMap.put("T", item);
        }else {
            rsMap.put("T", ts);
        }
        rsMap.put("bufTag", bufTag);
        return rsMap;
    }
    
    
    
}
主要解析类
package code;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ComUtil {
    //加载xml
    // 此方法调用dom4j来解析xml
       public static JSONObject XmlRead(String path) {
           // 解析books.xml文件
           // 创建SAXReader的对象reader
           SAXReader reader = new SAXReader();
           JSONObject jsonObject = new JSONObject();
           try {
               // 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。
               Document document = reader.read(new File(path));
               // 通过document对象获取根节点bookstore
               Element bookStore = document.getRootElement();
               // 通过element对象的elementIterator方法获取迭代器
               Iterator it = bookStore.elementIterator();
               // 遍历迭代器,获取根节点中的信息()
               JSONArray jsonArray = new JSONArray();

               while (it.hasNext()) {

                   // System.out.println("=====开始遍历xml属性=====");
                   Element book = (Element) it.next();
                   Map<String, Object> param = new HashMap<String, Object>();
                   JSONArray nodeList = new JSONArray();

                   // 获取book的属性名以及 属性值
                   List<Attribute> bookAttrs = book.attributes();
                   for (Attribute attr : bookAttrs) {
                       //System.out.println("属性名:" + attr.getName() + "--属性值:" + attr.getValue());
                       param.put(attr.getName(), attr.getValue());
                   }

                   jsonObject.put("attribute", jsonArray);
                   // 解析子节点的信息
                   Iterator itt = book.elementIterator();
                   while (itt.hasNext()) {
                       Element bookChild = (Element) itt.next();
                       List<Attribute> bookAttrss = bookChild.attributes();

                       JSONObject obj = new JSONObject();

                       for (Attribute attr : bookAttrss) {
                           //System.out.println("属性名:" + attr.getName() + "--属性值:" + attr.getValue());
                           obj.put(attr.getName(), attr.getValue());
                       }

                       nodeList.add(obj);
                       // System.out.println("节点名:" + bookChild.getName() +
                       // "--节点值:" + bookChild.getStringValue());

                   }
                   param.put("nodeList", nodeList);
                   jsonArray.add(param);

                   // System.out.println("=====结束遍历xml属性=====");
               }
           } catch (DocumentException e) {
               e.printStackTrace();
           }
           return jsonObject;
       }
       
       

       //读取16进制文件 yjl
       public static byte[] readFile(String file) throws IOException{ 
           InputStream is=new FileInputStream(file) ;
           int length=is.available() ;
           byte bt[]=new byte[length] ;
           is.read(bt) ;
           return bt;
          }
       
       //Integer.parseInt(buf.substring(bufTag, bufTag + cutL), 16)+"";
       public static String converLong(String param) {
           int len = param.length();
           if(len!=8){
               return "长度不符";
           }else{
               String str1 = param.substring(0,2);
               String str2 = param.substring(2,4);
               String str3 = param.substring(4,6);
               String str4 = param.substring(6,8);
               return String.valueOf(Integer.parseInt(str4+str3+str2+str1, 16));
           }
           
       }

       public static String converInteger(String param) {
           int len = param.length();
           if(len!=4){
               return "长度不符";
           }else{
               String str1 = param.substring(0,2);
               String str2 = param.substring(2,4);
               String str3 =String.valueOf(Integer.parseInt(str2+str1, 16));
               return str3;
           }
       }
       public static String converSingle(String param) {
           int len = param.length();
           if(len!=8){
               return "长度不符";
           }else{//3100cdcd
               String str1 = param.substring(0,2);
               String str2 = param.substring(2,4);
               String str3 = param.substring(4,6);
               String str4 = param.substring(6,8);
               return String.valueOf(Integer.parseInt(str4+str3+str2+str1, 16));
           }
       }
       public static String converByte(String param) {
           int len = param.length();
           if(len!=2){
               return "长度不符";
           }else{
               String str1 = param.substring(0,2);
           return String.valueOf(Integer.parseInt(str1, 16));
           }
       }

       public static String converString(String param) {
           int len = param.length();
           if(len!=32){
               return "长度不符";
           }else{//64 65 31 00 CD CD CD CD CD CD CD CD CD CD CD CD;
               int i=getCharacterPosition(param,"cd",1);
               if(i!=-1) {
                   param=param.substring(0,i);
                   System.out.println(param);
                   param=convertHexToString(param);
               }else {
                   param="解析错误";
               }
           return param;
           }
       }
       
       
       public static String convertStringToHex(String str){
           
           char[] chars = str.toCharArray();
      
           StringBuffer hex = new StringBuffer();
           for(int i = 0; i < chars.length; i++){
             hex.append(Integer.toHexString((int)chars[i]));
           }
      
           return hex.toString();
           }
      
       public static String convertHexToString(String hex){
      
           StringBuilder sb = new StringBuilder();
           StringBuilder temp = new StringBuilder();
      
           //49204c6f7665204a617661 split into two characters 49, 20, 4c...
           for( int i=0; i<hex.length()-1; i+=2 ){
      
               //grab the hex in pairs
               String output = hex.substring(i, (i + 2));
               //convert hex to decimal
               int decimal = Integer.parseInt(output, 16);
               //convert the decimal to character
               sb.append((char)decimal);
      
               temp.append(decimal);
           }
      
           return sb.toString();
           }
       
       public static int getCharacterPosition(String url,String s,int i){
           //这里是获取"/"符号的位置 lastindexof从字符串末尾开始检索,检索到子字符
           Matcher slashMatcher = Pattern.compile(s).matcher(url);
           int mIdx = 0;
           while(slashMatcher.find()) {
              mIdx++;
              //当"/"符号第i次出现的位置
              if(mIdx == i){
                 break;
              }
           }
           int rs;
           try{
               rs=slashMatcher.start();
           } catch (Exception e) {
               rs=-1;
           }
            return rs;
       }
       
       //对象转Map
       public static Map ConvertObjToMap(Object obj){
           Map<String,Object> reMap = new HashMap<String,Object>();
           if (obj == null) 
            return null;
           Field[] fields = obj.getClass().getDeclaredFields();
           try {
            for(int i=0;i<fields.length;i++){
             try {
              Field f = obj.getClass().getDeclaredField(fields[i].getName());
              f.setAccessible(true);
                    Object o = f.get(obj);
                    reMap.put(fields[i].getName(), o);
             } catch (NoSuchFieldException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             } catch (IllegalArgumentException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             } catch (IllegalAccessException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }
            }
           } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
           } 
           return reMap;
          }
       
       //示例 setVal(obj,"setUpdateUser","修改人"); 给对象赋值
       public static void setVal(Object obj, String methodName, Object value) {
           String method_name = methodName;
           Method[] methods = obj.getClass().getMethods();
           for (Method method : methods) {
               /**
                *     因为这里只是调用bean中属性的set方法,属性名称不能重复
                * 所以set方法也不会重复,所以就直接用方法名称去锁定一个方法
                * (注:在java中,锁定一个方法的条件是方法名及参数)
                * **/
               if(method.getName().equals(method_name))
               {
                   Class[] parameterC = method.getParameterTypes();
                   try {
                       /**如果是基本数据类型时(如int、float、double、byte、char、boolean)
                        * 需要先将Object转换成相应的封装类之后再转换成对应的基本数据类型
                        * 否则会报 ClassCastException**/
                       if(parameterC[0] == int.class)
                       {
                           method.invoke(obj,((Integer)value).intValue());
                           break;
                       }else if(parameterC[0] == float.class){
                           method.invoke(obj, ((Float)value).floatValue());
                           break;
                       }else if(parameterC[0] == double.class)
                       {
                           method.invoke(obj, ((Double)value).doubleValue());
                           break;
                       }else if(parameterC[0] == byte.class)
                       {
                           method.invoke(obj, ((Byte)value).byteValue());
                           break;
                       }else if(parameterC[0] == char.class)
                       {
                           method.invoke(obj, ((Character)value).charValue());
                           break;
                       }else if(parameterC[0] == boolean.class)
                       {
                           method.invoke(obj, ((Boolean)value).booleanValue());
                           break;
                       }else
                       {
                           method.invoke(obj,parameterC[0].cast(value));
                           break;
                       }
                   } catch (IllegalArgumentException e) {
                       e.printStackTrace();
                   } catch (IllegalAccessException e) {
                       e.printStackTrace();
                   } catch (InvocationTargetException e) {
                       e.printStackTrace();
                   } catch (SecurityException e) {
                       e.printStackTrace();
                   } 
               }
           }
       }
       

       
}
辅助工具类

然后我们就得到了 将荣的整体数据,然后就可以对他进行随意操作了

以后就可以直接写转化方法在这里把相关的数据转化过来了

顺便分析下将荣的数据结构

大概分为5块

1.BtlInfo 数据介绍类,地图的长宽高,兵种数量,军团数量,胜利条件等信息

2.BtlLegion 军团信息,list类,包含军团的国家,国家码,失败条件,阵营,资源,科技等

3.BtlTile 地图信息,list类,包含主要地图,建筑,地名

4.BtlArmy 士兵信息,list类,士兵类别,位置,所属军团等

5.BtlEvent 事件信息,list类,主要是剧情,对话等一些触发

我们主要用的是地图类,可以转为我们使用的地图

士兵,军团等可以配合直接制作战役

 

posted on 2019-05-19 16:53  黑狱  阅读(649)  评论(0编辑  收藏  举报