summaryrefslogtreecommitdiff
path: root/NW4RTools/OutputStream.cs
blob: 2243d9052f1357e49c397a31133279994fe70656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.IO;

namespace NW4RTools {
	public class OutputStream {
		// TODO: look into making this (and InputStream) inherit from Stream, or something
		public readonly ByteEndian Endian;
		private readonly bool MustReverseArrays;
		private readonly System.IO.MemoryStream BaseStream;

		public int Position {
			get { return (int)BaseStream.Position; }
		}

		public byte[] GetBuffer() {
			return BaseStream.ToArray();
		}

		public OutputStream() {
			BaseStream = new MemoryStream();
			Endian = ByteEndian.BigEndian;
			MustReverseArrays = BitConverter.IsLittleEndian;
		}

		public OutputStream(ByteEndian endian) {
			BaseStream = new MemoryStream();
			Endian = endian;

			if (Endian == ByteEndian.BigEndian)
				MustReverseArrays = BitConverter.IsLittleEndian;
			else
				MustReverseArrays = !BitConverter.IsLittleEndian;
		}

		public void Seek(int pos) {
			if (pos < 0 || pos > BaseStream.Length)
				throw new ArgumentOutOfRangeException();
			
			BaseStream.Position = pos;
		}

		public void AlignTo(int num) {
			if ((Position & (num - 1)) == 0)
				return;

			for (int i = (num - (Position & (num - 1))); i != 0; i--) {
				WriteByte(0);
			}
		}

		public void AddPadding(int count) {
			if (count <= 0)
				return;
			else if (count == 1)
				WriteByte(0);
			else
				for (int i = count; i != 0; i--)
					WriteByte(0);
		}

		public void PadToSize(int size) {
			AddPadding(size - Position);
		}

		public void WriteBytes(byte[] data) {
			BaseStream.Write(data, 0, data.Length);
		}

		private void WriteReversedBytes(byte[] data) {
			// TODO: figure out if this modifies the array
			if (MustReverseArrays)
				Array.Reverse(data);
			BaseStream.Write(data, 0, data.Length);
		}

		public void WriteByte(byte val) {
			BaseStream.WriteByte(val);
		}

		public void WriteSByte(sbyte val) {
			WriteByte(unchecked((byte)val));
		}

		public void WriteInt16(Int16 val) {
			WriteReversedBytes(BitConverter.GetBytes(val));
		}

		public void WriteUInt16(UInt16 val) {
			WriteReversedBytes(BitConverter.GetBytes(val));
		}

		public void WriteInt32(Int32 val) {
			WriteReversedBytes(BitConverter.GetBytes(val));
		}

		public void WriteUInt32(UInt32 val) {
			WriteReversedBytes(BitConverter.GetBytes(val));
		}

		public void WriteFloat(float val) {
			WriteReversedBytes(BitConverter.GetBytes(val));
		}

		public void WriteDouble(double val) {
			WriteReversedBytes(BitConverter.GetBytes(val));
		}

		public void WriteBool(bool val) {
			// TODO: ReadBool in InputStream
			WriteByte(val ? (byte)1 : (byte)0);
		}

		public void WriteColor(Color col) {
			WriteByte(col.r);
			WriteByte(col.g);
			WriteByte(col.b);
			WriteByte(col.a);
		}

		public void WriteVec2(Vec2 vec) {
			WriteFloat(vec.x);
			WriteFloat(vec.y);
		}

		public void WriteVec3(Vec3 vec) {
			WriteFloat(vec.x);
			WriteFloat(vec.y);
			WriteFloat(vec.z);
		}

		public void WriteMatrix(Matrix m) {
			WriteFloat(m.v00);
			WriteFloat(m.v01);
			WriteFloat(m.v02);
			WriteFloat(m.v03);
			WriteFloat(m.v10);
			WriteFloat(m.v11);
			WriteFloat(m.v12);
			WriteFloat(m.v13);
			WriteFloat(m.v20);
			WriteFloat(m.v21);
			WriteFloat(m.v22);
			WriteFloat(m.v23);
		}

		public void WriteName(string name) {
			byte[] encoded = System.Text.Encoding.GetEncoding("Shift_JIS").GetBytes(name);
			WriteInt32(encoded.Length);
			WriteBytes(encoded);
			WriteByte(0);

			// add 1 to the length to include the zero byte
			if (((encoded.Length + 1) & 3) != 0) {
				for (int i = 4 - ((encoded.Length + 1) & 3); i > 0; i--) {
					WriteByte(0);
				}
			}
		}
	}
}