168 lines
4.4 KiB
C#
168 lines
4.4 KiB
C#
using LibXom.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LibXom.Streams
|
|
{
|
|
public class XomStreamWriter : IDisposable
|
|
{
|
|
private Stream xStream;
|
|
public Stream BaseStream
|
|
{
|
|
get
|
|
{
|
|
return xStream;
|
|
}
|
|
}
|
|
public void WriteByte(byte b)
|
|
{
|
|
xStream.WriteByte(b);
|
|
}
|
|
public int Pos()
|
|
{
|
|
return Convert.ToInt32(xStream.Position);
|
|
}
|
|
public void Rewind(int amt)
|
|
{
|
|
xStream.Seek(-amt, SeekOrigin.Current);
|
|
}
|
|
public void Skip(int amt)
|
|
{
|
|
int cpos = Pos();
|
|
int len = Convert.ToInt32(xStream.Length);
|
|
int remain = len - cpos;
|
|
|
|
if (amt > remain)
|
|
{
|
|
xStream.Seek(remain, SeekOrigin.Current);
|
|
amt -= remain;
|
|
WritePadding(0, amt);
|
|
}
|
|
else
|
|
{
|
|
xStream.Seek(amt, SeekOrigin.Current);
|
|
}
|
|
}
|
|
public void WritePadding(byte pad, int len)
|
|
{
|
|
byte[] buf = new byte[len];
|
|
if (pad != 0)
|
|
for (int i = 0; i < len; i++)
|
|
buf[i] = pad;
|
|
WriteBytes(buf);
|
|
}
|
|
|
|
|
|
public void WriteInt32Array(int[] uncompressedArr)
|
|
{
|
|
this.WriteCompressedInt(uncompressedArr.Length);
|
|
for (int i = 0; i < uncompressedArr.Length; i++) WriteInt32(uncompressedArr[i]);
|
|
}
|
|
|
|
public void WriteCompressedIntArray(int[] uncompressedArr)
|
|
{
|
|
this.WriteCompressedInt(uncompressedArr.Length);
|
|
for (int i = 0; i < uncompressedArr.Length; i++) WriteCompressedInt(uncompressedArr[i]);
|
|
}
|
|
|
|
public void WriteCompressedInt(int uncompressedInt)
|
|
{
|
|
XomCompressor.compressIntAndWriteToStream(xStream, uncompressedInt);
|
|
}
|
|
|
|
public void WriteByteArray(byte[] data)
|
|
{
|
|
this.WriteCompressedInt(data.Length);
|
|
WriteBytes(data);
|
|
}
|
|
|
|
public void WriteBytes(byte[] bytes)
|
|
{
|
|
xStream.Write(bytes, 0, bytes.Length);
|
|
}
|
|
|
|
public void WriteBool(bool value)
|
|
{
|
|
if (value) this.WriteByte(0x01);
|
|
else this.WriteByte(0x00);
|
|
}
|
|
public void WriteFloat(float value)
|
|
{
|
|
byte[] buffer = BitConverter.GetBytes(value);
|
|
WriteBytes(buffer);
|
|
}
|
|
|
|
public void WriteDateTime(DateTime date)
|
|
{
|
|
WriteByte(Convert.ToByte(date.Second));
|
|
WriteByte(Convert.ToByte(date.Minute));
|
|
WriteByte(Convert.ToByte(date.Hour));
|
|
WriteByte(Convert.ToByte(date.Day));
|
|
WriteByte(Convert.ToByte(date.Month));
|
|
WriteUInt16(Convert.ToUInt16(date.Year));
|
|
}
|
|
public void WriteUInt16(ushort value)
|
|
{
|
|
byte[] buffer = BitConverter.GetBytes((ushort)value);
|
|
WriteBytes(buffer);
|
|
}
|
|
|
|
public void WriteInt16(short value)
|
|
{
|
|
byte[] buffer = BitConverter.GetBytes((short)value);
|
|
WriteBytes(buffer);
|
|
}
|
|
public void WriteUInt32(uint value)
|
|
{
|
|
byte[] buffer = BitConverter.GetBytes((uint)value);
|
|
WriteBytes(buffer);
|
|
}
|
|
|
|
public void WriteInt32(int value)
|
|
{
|
|
byte[] buffer = BitConverter.GetBytes((int)value);
|
|
WriteBytes(buffer);
|
|
}
|
|
public void WriteInt32BE(int value)
|
|
{
|
|
byte[] buffer = BitConverter.GetBytes(value);
|
|
buffer.Reverse();
|
|
WriteBytes(buffer);
|
|
}
|
|
|
|
public void WriteStrLen(string str, int len)
|
|
{
|
|
WriteStr(str);
|
|
|
|
int padLen = len - str.Length;
|
|
if (padLen > 0)
|
|
WritePadding(0x00, padLen);
|
|
}
|
|
|
|
public void WriteStr(string str)
|
|
{
|
|
byte[] buffer = Encoding.UTF8.GetBytes(str);
|
|
WriteBytes(buffer);
|
|
}
|
|
|
|
public void WriteCStr(string str)
|
|
{
|
|
WriteStr(str);
|
|
WriteByte(0);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
this.xStream.Dispose();
|
|
}
|
|
|
|
public XomStreamWriter(Stream xStream)
|
|
{
|
|
this.xStream = xStream;
|
|
}
|
|
}
|
|
}
|