コード例 #1
0
        public static Byte[] GetBytes(String input, Encoding encoding)
        {
            Assert.IsNotNull(input, nameof(input));
            Assert.IsNotNull(encoding, nameof(encoding));

            var buffer = new Byte[GetSize(input)];

            var offset = 0;

            for (var i = 0; i != input.Length; ++i)
            {
                var @char = input[i];
                if (@char > 0x20)
                {
                    offset += encoding.GetBytes(input, i, 1, buffer, offset);
                }
                else if (@char == (Byte)StringCode.COLOR)
                {
                    buffer[offset + 0] = (Byte)@char;
                    buffer[offset + 1] = (Byte)input[i + 1];

                    i      += 1;
                    offset += 2;
                }
                else if (@char == (Byte)StringCode.ITEM)
                {
                    buffer[offset + 0] = (Byte)@char;
                    BinaryIO.WriteIntoBuffer(buffer, offset + 1, (UInt16)input[i + 1], Endian.LittleEndian);

                    i      += 1;
                    offset += 3;
                }
                else
                {
                    buffer[offset] = (Byte)@char;
                    offset        += 1;
                }
            }

            return(buffer);
        }
コード例 #2
0
        public Byte[] Write(Encoding encoding)
        {
            Assert.IsNotNull(encoding, nameof(encoding));

            using (var stream = new MemoryStream())
            {
                foreach (var record in Records)
                {
                    var idbytes   = ConvertId(record.Id);
                    var filebytes = record.MonsterDefinitionFile.Write(encoding);

                    var sizebytes = new Byte[4];
                    BinaryIO.WriteIntoBuffer(sizebytes, 0, (Int32)filebytes.Length);

                    stream.Write(idbytes, 0, idbytes.Length);
                    stream.Write(sizebytes, 0, sizebytes.Length);
                    stream.Write(filebytes, 0, filebytes.Length);
                }

                stream.Write(new Byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, 0, 8);

                return(stream.ToArray());
            }
        }
コード例 #3
0
        public UInt32 ReadUInt32(Endian endian = Endian.Default)
        {
            Stream.Read(Buffer, 0, 4);

            return(BinaryIO.ReadUInt32FromBuffer(Buffer, 0, endian));
        }
コード例 #4
0
        public Int16 ReadInt16(Endian endian = Endian.Default)
        {
            Stream.Read(Buffer, 0, 2);

            return(BinaryIO.ReadInt16FromBuffer(Buffer, 0, endian));
        }
コード例 #5
0
        public SByte ReadSByte(Endian endian = Endian.Default)
        {
            Stream.Read(Buffer, 0, 1);

            return(BinaryIO.ReadSByteFromBuffer(Buffer, 0, endian));
        }
コード例 #6
0
        public String ReadString()
        {
            StringBuilder.Length = 0;

            while (true)
            {
                var bytesread = Stream.Read(Buffer, 0, Buffer.Length);
                if (bytesread == 0)
                {
                    throw new Exception();
                }

                for (var i = 0; i < bytesread; ++i)
                {
                    if (Buffer[i] == 0)
                    {
                        var rewindamount = bytesread - i - 1;
                        Position -= rewindamount;

                        return(StringBuilder.ToString());
                    }
                    else if (Buffer[i] < 0x20)
                    {
                        if (Buffer[i] == (Byte)StringCode.COLOR)
                        {
                            if (i == Buffer.Length - 1)
                            {
                                --Position;
                                break;
                            }

                            StringBuilder.Append((Char)Buffer[i]);
                            StringBuilder.Append((Char)Buffer[i + 1]);

                            ++i;
                        }
                        else if (Buffer[i] == (Byte)StringCode.ITEM)
                        {
                            if (i >= Buffer.Length - 2)
                            {
                                Position -= 2;
                                break;
                            }

                            var item = BinaryIO.ReadUInt16FromBuffer(Buffer, i + 1);

                            StringBuilder.Append((Char)Buffer[i]);
                            StringBuilder.Append((Char)item);

                            i = i + 2;
                        }
                        else
                        {
                            StringBuilder.Append((Char)Buffer[i]);
                        }
                    }
                    else if (Buffer[i] < 0x7F)
                    {
                        StringBuilder.Append((Char)Buffer[i]);
                    }
                    else
                    {
                        if (i == Buffer.Length - 1)
                        {
                            --Position;
                            break;
                        }

                        var substring = Encoding.GetString(Buffer, i, 2);
                        StringBuilder.Append(substring);

                        ++i;
                    }
                }
            }
        }