-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTable.cs
177 lines (145 loc) · 5.93 KB
/
Table.cs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using GotaSoundIO.IO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GotaSoundIO {
/// <summary>
/// A table.
/// </summary>
/// <typeparam name="T">Type represented in the table.</typeparam>
public class Table<T> : IList<T>, IReadable, IWriteable {
/// <summary>
/// Items.
/// </summary>
private List<T> items = new List<T>();
/// <summary>
/// Get the items in the table as a list.
/// </summary>
/// <param name="t">Table.</param>
public static implicit operator List<T>(Table<T> t) => t.items;
/// <summary>
/// Make a table from a list of items.
/// </summary>
/// <param name="l">List.</param>
public static explicit operator Table<T>(List<T> l) { return new Table<T>() { items = l }; }
/// <summary>
/// Read the table.
/// </summary>
/// <param name="r">The reader.</param>
public void Read(FileReader r) {
//Get count.
uint count = r.ReadUInt32();
//Types.
if (typeof(T).Equals(typeof(ulong))) {
items = r.ReadUInt64s((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(uint))) {
items = r.ReadUInt32s((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(ushort))) {
items = r.ReadUInt16s((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(byte))) {
items = r.ReadBytes((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(long))) {
items = r.ReadInt64s((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(int))) {
items = r.ReadInt32s((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(short))) {
items = r.ReadInt16s((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(sbyte))) {
items = r.ReadSBytes((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(string))) {
items = r.ReadStrings((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(char))) {
items = r.ReadChars((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(float))) {
items = r.ReadSingles((int)count).ConvertTo<T>().ToList();
} else if (typeof(T).Equals(typeof(double))) {
items = r.ReadDoubles((int)count).ConvertTo<T>().ToList();
}
//Custom.
else {
for (int i = 0; i < count; i++) {
items.Add(r.Read<T>());
}
}
}
/// <summary>
/// Write the table.
/// </summary>
/// <param name="w">The writer.</param>
public void Write(FileWriter w) {
//Write count.
w.Write((uint)items.Count);
//Types.
if (typeof(T).Equals(typeof(ulong))) {
w.Write(items.ConvertTo<ulong>());
} else if (typeof(T).Equals(typeof(uint))) {
w.Write(items.ConvertTo<uint>());
} else if (typeof(T).Equals(typeof(ushort))) {
w.Write(items.ConvertTo<ushort>());
} else if (typeof(T).Equals(typeof(byte))) {
w.Write(items.ConvertTo<byte>().ToArray());
} else if (typeof(T).Equals(typeof(long))) {
w.Write(items.ConvertTo<long>());
} else if (typeof(T).Equals(typeof(int))) {
w.Write(items.ConvertTo<int>());
} else if (typeof(T).Equals(typeof(short))) {
w.Write(items.ConvertTo<short>());
} else if (typeof(T).Equals(typeof(sbyte))) {
w.Write(items.ConvertTo<byte>().ToArray());
} else if (typeof(T).Equals(typeof(string))) {
w.Write(items.ConvertTo<string>());
} else if (typeof(T).Equals(typeof(char))) {
w.Write(items.ConvertTo<char>().ToArray());
} else if (typeof(T).Equals(typeof(float))) {
w.Write(items.ConvertTo<float>());
} else if (typeof(T).Equals(typeof(double))) {
w.Write(items.ConvertTo<double>());
}
//Custom.
else {
foreach (var i in items) {
w.Write(i as IWriteable);
}
}
}
//List stuff.
#region ListStuff
public T this[int index] { get => items[index]; set => items[index] = value; }
public int Count => items.Count();
public bool IsReadOnly => false;
public void Add(T item) {
items.Add(item);
}
public void Clear() {
items.Clear();
}
public bool Contains(T item) {
return items.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex) {
items.CopyTo(array, arrayIndex);
}
public IEnumerator<T> GetEnumerator() {
return items.GetEnumerator();
}
public int IndexOf(T item) {
return items.IndexOf(item);
}
public void Insert(int index, T item) {
items.Insert(index, item);
}
public bool Remove(T item) {
return items.Remove(item);
}
public void RemoveAt(int index) {
items.RemoveAt(index);
}
IEnumerator IEnumerable.GetEnumerator() {
return items.GetEnumerator();
}
#endregion
}
}