コード例 #1
0
ファイル: TagTranslator.cs プロジェクト: randoro/Gridrift
        public static void writeTag(Tag tag, ref byte[] chunkData, ref int position)
        {
            TagID tagID = tag.getID();
            byte[] tagIDbytes = new byte[1];
            tagIDbytes[0] = (byte)tagID;
            writePayloadBasicType(tagIDbytes, ref chunkData, ref position, sizeof(byte));

            if(tagID.Equals(TagID.End))
            {
                return;
            }

            String tagName = tag.getName();
            short stringLength = (short)tagName.Length;

            byte[] tagNameLengthBytes = BitConverter.GetBytes(stringLength);
            writePayloadBasicType(tagNameLengthBytes, ref chunkData, ref position, sizeof(short));

            byte[] tagNameBytes = Encoding.UTF8.GetBytes(tagName);
            writePayloadBasicType(tagNameBytes, ref chunkData, ref position, tagNameBytes.Length);

            byte[] payload;
                switch (tagID)
                {
                    case TagID.End:
                        //using payload byte array
                        return; //never happens
                        break;
                    case TagID.Byte:
                        //using payload byte array
                        payload = tag.getPayload();
                        writePayloadBasicType(payload, ref chunkData, ref position, sizeof(byte));
                        break;
                    case TagID.Short:
                        //using payload byte array
                        payload = tag.getPayload();
                        writePayloadBasicType(payload, ref chunkData, ref position, sizeof(short));
                        break;
                    case TagID.Int:
                        //using payload byte array
                        payload = tag.getPayload();
                        writePayloadBasicType(payload, ref chunkData, ref position, sizeof(int));
                        break;
                    case TagID.Long:
                        //using payload byte array
                        payload = tag.getPayload();
                        writePayloadBasicType(payload, ref chunkData, ref position, sizeof(long));
                        break;
                    case TagID.Float:
                        //using payload byte array
                        payload = tag.getPayload();
                        writePayloadBasicType(payload, ref chunkData, ref position, sizeof(float));
                        break;
                    case TagID.Double:
                        //using payload byte array
                        payload = tag.getPayload();
                        writePayloadBasicType(payload, ref chunkData, ref position, sizeof(double));
                        break;
                    case TagID.ByteArray:
                        //using payload byte array
                        payload = tag.getPayload();
                        int payloadLength = payload.Length;
                        byte[] payloadLengthBytes = BitConverter.GetBytes(payloadLength);
                        writePayloadBasicType(payloadLengthBytes, ref chunkData, ref position,sizeof(int));

                        writePayloadBasicType(payload, ref chunkData, ref position, (sizeof(byte) * payloadLength));
                        break;
                    case TagID.List:
                        //using payloadList
                        TagID tagListType = tag.getListType();
                        byte[] tagListTypeBytes = new byte[0];
                        tagListTypeBytes[0] = (byte)tagListType;
                        writePayloadBasicType(tagListTypeBytes, ref chunkData, ref position, sizeof(byte));

                        List<Tag> payloadList = tag.getPayloadList();
                        int listLength = payloadList.Count;
                        byte[] listLengthBytes = BitConverter.GetBytes(listLength);
                        writePayloadBasicType(listLengthBytes, ref chunkData, ref position, sizeof(int));

                        foreach (Tag e in payloadList)
                        {
                            if(e.getID().Equals(tagListType))
                            {
                                TagTranslator.writeTag(e, ref chunkData, ref position);
                            }
                            else
                            {
                                throw new Exception("Wrong type of element in List");
                            }

                        }
                        break;
                    case TagID.Compound:
                        //using payloadList

                        List<Tag> payloadList2 = tag.getPayloadList();
                        foreach (Tag e in payloadList2)
                        {
                                TagTranslator.writeTag(e, ref chunkData, ref position);
                        }
                        Tag endTag = new Tag(TagID.End, null, null);
                        TagTranslator.writeTag(endTag, ref chunkData, ref position);

                        break;
                    case TagID.String:
                        //using payload byte array
                        payload = tag.getPayload();
                        int tempPosition2 = 0;
                        byte[] arrayLength2 = readPayloadBasicType(payload, ref tempPosition2, sizeof(short), tagName);
                        short arraySizeNumber2 = BitConverter.ToInt16(arrayLength2, 0);

                        writePayloadBasicType(payload, ref chunkData, ref position, sizeof(short) + (sizeof(char) * arraySizeNumber2));
                        break;
                    case TagID.IntArray:
                        //using payload byte array
                        payload = tag.getPayload();
                        int tempPosition3 = 0;
                        byte[] arrayLength3 = readPayloadBasicType(payload, ref tempPosition3, sizeof(int), tagName);
                        int arraySizeNumber3 = BitConverter.ToInt32(arrayLength3, 0);

                        writePayloadBasicType(payload, ref chunkData, ref position, sizeof(int) + (sizeof(int) * arraySizeNumber3));
                        break;
                    default:
                        break;

            #region oldcode
            //fileStream.WriteByte((byte)tagID);

            //if (tagID.Equals(TagID.End))
            //{
            //    return; //No payload
            //}

            //byte[] byteArray3 = BitConverter.GetBytes((short)tagName.Length);
            //byte[] buffer3 = Encoding.UTF8.GetBytes(tagName);

            //fileStream.WriteByte(byteArray3[0]);
            //fileStream.WriteByte(byteArray3[1]);
            //fileStream.Write(buffer3, 0, (short)tagName.Length);

            //byte[] dataInBytes;
            ////Payload
            //switch (tagID)
            //{
            //    case TagID.End:
            //        return; //No payload
            //        break;
            //    case TagID.Byte:
            //        unchecked
            //        {
            //            fileStream.Write((byte[])data, 0, 1);
            //        }
            //        return;
            //        break;
            //    case TagID.Short:
            //        //dataInBytes = BitConverter.GetBytes((short)data);
            //        fileStream.Write((byte[])data, 0, 2);
            //        return;

            //        break;
            //    case TagID.Int:

            //        //dataInBytes = BitConverter.GetBytes((int)data);
            //        fileStream.Write((byte[])data, 0, 4);
            //        return;

            //        break;
            //    case TagID.Long:
            //        //dataInBytes = BitConverter.GetBytes((long)data);
            //        fileStream.Write((byte[])data, 0, 8);
            //        return;

            //        break;
            //    case TagID.Float:
            //        //dataInBytes = BitConverter.GetBytes((float)data);
            //        fileStream.Write((byte[])data, 0, 4);
            //        return;

            //        break;
            //    case TagID.Double:
            //        //dataInBytes = BitConverter.GetBytes((double)data);
            //        fileStream.Write((byte[])data, 0, 8);
            //        return;

            //        break;
            //    case TagID.ByteArray:

            //        int sizeArray = ((byte[])data).Length; //changed for each tag
            //        byte[] sizeArrayArray = BitConverter.GetBytes((int)sizeArray);

            //        fileStream.Write(sizeArrayArray, 0, 4);
            //        fileStream.Write((byte[])data, 0, sizeArray);
            //        return;
            //        break;
            //    case TagID.String:
            //        short stringLength = (short)((byte[])data).Length;
            //        byte[] stringArrayLength = BitConverter.GetBytes(stringLength);

            //        fileStream.WriteByte(stringArrayLength[0]);
            //        fileStream.WriteByte(stringArrayLength[1]);
            //        fileStream.Write((byte[])data, 0, stringLength);

            //        return;

            //        break;
            //    case TagID.List:
            //        //fix this

            //        List<List<Tag>> theData = ((List<List<Tag>>)data);

            //        //fileStream.WriteByte((byte)tag.getListID());
            //        int elementsInList = theData.Count;
            //        byte[] elementArray = BitConverter.GetBytes(elementsInList);
            //        fileStream.Write(elementArray, 0, 4);

            //        if (elementsInList > 0)
            //        {
            //            if (tag.getListID().Equals(TagID.Compound))
            //            {
            //                foreach (List<Tag> list in theData)
            //                {
            //                    foreach (Tag innerTag in list)
            //                    {
            //                        TagTranslator.writeTag(innerTag, fileStream);
            //                    }
            //                }
            //            }
            //        }

            //        break;
            //    case TagID.Compound:
            //        return; //No payload
            //        break;
            //    case TagID.IntArray:
            //        //    fix this

            //        //    byte[] sizeArray3 = new byte[4]; //changed for each tag
            //        //    fileStream.Read(sizeArray3, 0, 4);
            //        //    int arraySizeNumber2 = BitConverter.ToInt32(sizeArray3, 0);

            //        //    payload = new byte[4 + arraySizeNumber2 * 4]; //changed for each tag
            //        //    payload[0] = sizeArray3[0];
            //        //    payload[1] = sizeArray3[1];
            //        //    payload[2] = sizeArray3[2];
            //        //    payload[3] = sizeArray3[3];
            //        //    fileStream.Read(payload, 4, arraySizeNumber2 * 4);
            //        //    returnTag = new Tag(tagID, tagIdentifier, payload, tagID);
            //        //    return returnTag;

            //        //    break;
            //        //default:
            //        //    returnTag = new Tag(tagID, tagIdentifier, null, tagID);
            //        return; //No payload
            //        break;
            #endregion
                }
        }
コード例 #2
0
ファイル: TagTranslator.cs プロジェクト: randoro/Gridrift
        public static void configureChunk(Chunk chunk, Tag tag, ref int layerDepth)
        {
            TagID tagID = tag.getID();
            String tagName = tag.getName();

            switch (tagID)
                    {
                        case TagID.End:
                            layerDepth--;
                            break;
                        case TagID.Byte:
                            if (tagName.Equals("TerrainPopulated"))
                            {
                                chunk.terrainPopulated = tag.getByte();
                            }
                            else if (tagName.Equals("StructurePopulated"))
                            {
                                chunk.structurePopulated = tag.getByte();
                            }
                            break;
                        case TagID.Short:
                            break;
                        case TagID.Int:
                            if (tagName.Equals("XPos"))
                            {
                                chunk.xCoordinate = tag.getInt();
                            }
                            else if (tagName.Equals("YPos"))
                            {
                                chunk.yCoordinate = tag.getInt();
                            }
                            break;
                        case TagID.Long:
                            if (tagName.Equals("LastUpdate"))
                            {
                                chunk.lastUpdate = tag.getLong();
                            }
                            else if (tagName.Equals("InhabitedTime"))
                            {
                                chunk.inhabitedTime = tag.getLong();
                            }
                            break;
                        case TagID.Float:
                            break;
                        case TagID.Double:
                            break;
                        case TagID.ByteArray:
                            if (tagName.Equals("Biomes"))
                            {
                                chunk.biomes = tag.getPayload();
                            }
                            else if (tagName.Equals("Blocks"))
                            {
                                chunk.blocks = tag.getPayload();
                            }
                            else if (tagName.Equals("Objects"))
                            {
                                chunk.objects = tag.getPayload();
                            }
                            break;
                        case TagID.String:
                            break;
                        case TagID.List:
                            if (tagName.Equals("Entities"))
                            {
                                //chunk.entities = (List<List<Tag>>)currentTag.getData();
                            }
                            else if (tagName.Equals("Rooms"))
                            {
                                //newChunk.rooms = (List<List<Tag>>)currentTag.getData();
                            }
                            break;
                        case TagID.Compound:
                            layerDepth++;
                            break;
                        case TagID.IntArray:
                            break;
                        default:
                            layerDepth = 0;
                            break;
                    }
        }