-2

我正在尝试使用 GWT 开发基于 Web 客户端的在线游戏。

游戏机制现在工作得很好,我真的很想继续我的开发计划的下一步,但现在我坚持使用我创建的一个简单的编码/解码函数来将我的客户端数据存储到一个字符串中.

我采取的步骤并不复杂。在运行时,我的服务器创建游戏对象的“ImageStates”以在我的客户端的画布上绘制。每个 ImageState 包含许多层,其中包含必须在什么特定时间在什么地方绘制的详细信息等。

我尝试了 ObjectInput/OutputStream,结合了 GZip-Compression 和 ByteInputStreams,但我找不到前两个的任何 GWT 实现,所以我不得不考虑另一种解决方案并遇到了 gwt-lzma。

我的目标是将这些细节编码为单个字符串,在服务器上对其进行压缩,将压缩后的数据发送到客户端,在那里解压缩并解码,以便客户端可以“读取”它。为此,我创建了一个编码和一个解码函数:

编码器:

public static String encodeImageStateContainer(HashMap<String,HashMap<Long,ImageState>> imageStateContainer){
    StringBuilder mainStringBuilder = new StringBuilder();

    Iterator<Entry<String,HashMap<Long,ImageState>>> imageStateContainerIterator = imageStateContainer.entrySet().iterator();
    while(imageStateContainerIterator.hasNext()){
        Entry<String,HashMap<Long,ImageState>> tempImageStateManagerMetadata = imageStateContainerIterator.next();

        HashMap<Long,ImageState> tempImageStateManager = tempImageStateManagerMetadata.getValue();

        if(tempImageStateManager.size() > 0){

            if(mainStringBuilder.length() > 0){
                mainStringBuilder.append('}');  //Divisor between Image State Managers
            }

            mainStringBuilder.append(tempImageStateManagerMetadata.getKey());
            mainStringBuilder.append('|');  //Divisor between Name and following Data

            StringBuilder subOneStringBuilder = new StringBuilder();            

            Iterator<Entry<Long,ImageState>> tempImageStateManagerIterator = tempImageStateManager.entrySet().iterator();
            while(tempImageStateManagerIterator.hasNext()){
                Entry<Long,ImageState> tempImageStateMetaData = tempImageStateManagerIterator.next();

                if(subOneStringBuilder.length() > 0){
                    subOneStringBuilder.append(')');    //Divisor between Image Objects
                }

                subOneStringBuilder.append(tempImageStateMetaData.getKey());
                subOneStringBuilder.append('-');    //Divisor between Object Id and Image State Data

                StringBuilder subTwoStringBuilder = new StringBuilder();
                ImageState tempImageState = tempImageStateMetaData.getValue();
                subOneStringBuilder.append(tempImageState.getFirstFrameId());   //1. => First Frame Id
                subOneStringBuilder.append(';');    //Divisor between Image State data types
                subOneStringBuilder.append(tempImageState.getFramesPerLayer()); //2. => Total Frame Count
                subOneStringBuilder.append(';');
                subOneStringBuilder.append(tempImageState.getMinumimScaleFactor()); //3. => Minimum Scale Factor
                subOneStringBuilder.append(';');

                ImageStateLayer[] tempImageStateLayers = tempImageState.getImageStateLayers();
                for(int layerId = 0; layerId < tempImageStateLayers.length; ++layerId){

                    if(subTwoStringBuilder.length() > 0){
                        subTwoStringBuilder.append('/');    //Divisor between Image State Layers
                    }

                    //Frame Arrays
                    String[] imageNativePath = tempImageStateLayers[layerId].getImageNativePath();
                    short[] offsetX = tempImageStateLayers[layerId].getOffsetX();
                    short[] offsetY = tempImageStateLayers[layerId].getOffsetY();
                    short[] orientation = tempImageStateLayers[layerId].getOrientation();

                    //Finalization Arrays
                    byte[] imagePathChange = tempImageStateLayers[layerId].getImagePathChange();
                    byte[] offsetXChange = tempImageStateLayers[layerId].getOffsetXChange();
                    byte[] offsetYChange = tempImageStateLayers[layerId].getOffsetYChange();
                    byte[] orientationChange = tempImageStateLayers[layerId].getOrientationChange();


                    //Image Path Data
                    StringBuilder subThreeStrignBuilder = new StringBuilder();
                    for(int imagePathId = 0; imagePathId < imageNativePath.length; ++imagePathId){

                        if(subThreeStrignBuilder.length() > 0){
                            subThreeStrignBuilder.append('#');  //Divisor between Frame Data Sets
                        }

                        subThreeStrignBuilder.append(imageNativePath[imagePathId]);
                        subThreeStrignBuilder.append(',');  //Divisor between Frame Data Set Data
                        subThreeStrignBuilder.append(imagePathChange[imagePathId]);
                    }

                    subTwoStringBuilder.append(subThreeStrignBuilder.toString());
                    subTwoStringBuilder.append('[');    //Divisor between Frame Data Types

                    //OffsetX Data
                    subThreeStrignBuilder = new StringBuilder();
                    for(int offsetXId = 0; offsetXId < offsetX.length; ++offsetXId){

                        if(subThreeStrignBuilder.length() > 0){
                            subThreeStrignBuilder.append('#');  //Divisor between Frame Data Sets
                        }

                        subThreeStrignBuilder.append(offsetX[offsetXId]);
                        subThreeStrignBuilder.append(',');  //Divisor between Frame Data Set Data
                        subThreeStrignBuilder.append(offsetXChange[offsetXId]);
                    }

                    subTwoStringBuilder.append(subThreeStrignBuilder.toString());
                    subTwoStringBuilder.append('[');    //Divisor between Frame Data Types

                    //OffsetY Data
                    subThreeStrignBuilder = new StringBuilder();
                    for(int offsetYId = 0; offsetYId < offsetY.length; ++offsetYId){

                        if(subThreeStrignBuilder.length() > 0){
                            subThreeStrignBuilder.append('#');  //Divisor between Frame Data Sets
                        }

                        subThreeStrignBuilder.append(offsetY[offsetYId]);
                        subThreeStrignBuilder.append(',');  //Divisor between Frame Data Set Data
                        subThreeStrignBuilder.append(offsetYChange[offsetYId]);
                    }

                    subTwoStringBuilder.append(subThreeStrignBuilder.toString());
                    subTwoStringBuilder.append('[');    //Divisor between Frame Data Types

                    //Orientation Data
                    subThreeStrignBuilder = new StringBuilder();
                    for(int orientationId = 0; orientationId < orientation.length; ++orientationId){

                        if(subThreeStrignBuilder.length() > 0){
                            subThreeStrignBuilder.append('#');  //Divisor between Frame Data Sets
                        }

                        subThreeStrignBuilder.append(orientation[orientationId]);
                        subThreeStrignBuilder.append(',');  //Divisor between Frame Data Set Data
                        subThreeStrignBuilder.append(orientationChange[orientationId]);
                    }

                    subTwoStringBuilder.append(subThreeStrignBuilder.toString());

                }
                subOneStringBuilder.append(subTwoStringBuilder.toString());             
            }
            mainStringBuilder.append(subOneStringBuilder.toString());
        }
    }

    return mainStringBuilder.toString();
}

解码器:

public static HashMap<String,HashMap<Long,ImageState>> decodeImageStateContainer(String data){      

    String[] imageStateManagerArray = data.split("\\}");
    HashMap<String,HashMap<Long,ImageState>> imageStateManagerContainer = new HashMap<String,HashMap<Long,ImageState>>(imageStateManagerArray.length);

    for(int managerId = 0; managerId < imageStateManagerArray.length; ++managerId){
        String[] tempImageStateData = imageStateManagerArray[managerId].split("\\|");

        HashMap<Long,ImageState> tempImageStateManager = new HashMap<Long,ImageState>();
        imageStateManagerContainer.put(tempImageStateData[0], tempImageStateManager);

        String[] tempImageStateManagerObjects = tempImageStateData[1].split("\\)");

        for(int objectId = 0; objectId < tempImageStateManagerObjects.length; ++objectId){
            String[] tempImageObjectData = tempImageStateManagerObjects[objectId].split("\\-");

            long imageObjectId = Long.parseLong(tempImageObjectData[0]);
            String[] imageStateMetaData = tempImageObjectData[1].split("\\;");              
            ImageState tempImageState = new ImageState(Integer.parseInt(imageStateMetaData[1]), Integer.parseInt(imageStateMetaData[0]), Integer.parseInt(imageStateMetaData[2]));
            tempImageStateManager.put(imageObjectId, tempImageState);

            String[] tempImageStateLayerMetaData = imageStateMetaData[3].split("\\/");
            ImageStateLayer[] tempImageStateLayers = new ImageStateLayer[tempImageStateLayerMetaData.length];

            for(int layerId = 0; layerId < tempImageStateLayerMetaData.length; ++layerId){
                String[] layerElements = tempImageStateLayerMetaData[layerId].split("\\[");     

                String[] imageNativePathDetails = layerElements[0].split("\\#");
                String[] offsetXDetails = layerElements[1].split("\\#");
                String[] offsetYDetails = layerElements[2].split("\\#");
                **String[] orientationDetails = layerElements[3].split("\\#");**

                //Image Path Data
                String[] imageNativePaths = new String[imageNativePathDetails.length];
                byte[] imagePathChange = new byte[imageNativePathDetails.length];
                for(int id = 0; id < imageNativePathDetails.length; ++id){                      
                    String[] imagePathDetailElements = imageNativePathDetails[id].split("\\,");
                    imageNativePaths[id] = imagePathDetailElements[0];
                    imagePathChange[id] = Byte.parseByte(imagePathDetailElements[1]);
                }

                //OffsetX Data
                short[] offsetX = new short[offsetXDetails.length];
                byte[] offsetXChange = new byte[offsetXDetails.length];
                for(int id = 0; id < offsetXDetails.length; ++id){
                    String[] offsetXDetailElements = offsetXDetails[id].split("\\,");
                    offsetX[id] = Short.parseShort(offsetXDetailElements[0]);
                    offsetXChange[id] = Byte.parseByte(offsetXDetailElements[1]);
                }

                //OffsetY Data
                short[] offsetY = new short[offsetYDetails.length];
                byte[] offsetYChange = new byte[offsetYDetails.length];
                for(int id = 0; id < offsetYDetails.length; ++id){
                    String[] offsetYDetailElements = offsetYDetails[id].split("\\,");
                    offsetY[id] = Short.parseShort(offsetYDetailElements[0]);
                    offsetYChange[id] = Byte.parseByte(offsetYDetailElements[1]);
                }

                //Orientation Data
                short[] orientation = new short[orientationDetails.length];
                byte[] orientationChange = new byte[orientationDetails.length];
                for(int id = 0; id < orientationDetails.length; ++id){
                    String[] orientationDetailElements = orientationDetails[id].split("\\,");
                    orientation[id] = Short.parseShort(orientationDetailElements[0]);
                    orientationChange[id] = Byte.parseByte(orientationDetailElements[1]);
                }

                //Create the Layer and Add it to the Array
                tempImageStateLayers[layerId] = new ImageStateLayer(imageNativePaths,new short[][]{offsetX,offsetY,orientation}, new byte[][]{imagePathChange,offsetXChange,offsetYChange,orientationChange});

            }

            //Connect the Reference to the layers with the Image State
            tempImageState.setImageStateLayers(tempImageStateLayers);

        }           

    }       

    return imageStateManagerContainer;
}

现在我的问题:如果我在服务器上对其进行编码并在之后直接解码它大约需要三个编码周期,直到我在解码器中的这个特定位置得到一个 IndexOutofBoundsException,最后一行是错误似乎是根据的行堆栈跟踪:

for(int layerId = 0; layerId < tempImageStateLayerMetaData.length; ++layerId){
                String[] layerElements = tempImageStateLayerMetaData[layerId].split("\\[");     

                String[] imageNativePathDetails = layerElements[0].split("\\#");
                String[] offsetXDetails = layerElements[1].split("\\#");
                String[] offsetYDetails = layerElements[2].split("\\#");
                String[] orientationDetails = layerElements[3].split("\\#");

我不会说我是专家,但是在测试了编码器之后,我可以肯定地说它生成的字符串总是有效的,我得到这个错误的字符串也是完整的。我的解码功能一定有问题,但我不知道它可能是什么。

正如我所说,前三个周期被正确编码和解码,并且要编码的数据没有显着变化。编码字符串不可能导致字符串数组小于 4 个元素。基本上,据我了解我的代码,此错误不存在。

我认为这可能是某种内存分配问题阻止了字符串被正确拆分,但这只是一个笨拙的程序员的想法。

任何帮助都深表感谢!

4

1 回答 1

2

我找到了解决问题的方法:

在编码时,我将数字类型打印到我的字符串中。在选择我的分隔符符号时,我没有采用负值,导致在考虑数字之前有一个“-”,这导致当我尝试按“-”拆分时,我的字符串不受控制地拆分。我用另一个字符替换了减号,现在一切正常!

于 2012-05-25T12:26:07.693 回答