forked from Fronkln/HActLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
144 lines (118 loc) · 4.13 KB
/
Extensions.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
using System;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using Yarhl.IO;
namespace HActLib
{
public static class Extensions
{
//https://stackoverflow.com/a/45034630/14569631
public static string ToTitleCase(this string title)
{
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(title.ToLower());
}
public static T Closest<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector, TKey pivot) where TKey : IComparable<TKey>
{
return source.Where(x => pivot.CompareTo(keySelector(x)) <= 0).OrderBy(keySelector).FirstOrDefault();
}
public static int Align(this DataWriter writer, int alignment)
{
int mod = (int)writer.Stream.Position % alignment;
if (mod == 0)
return 0;
int neededBytes = alignment - mod;
writer.WriteTimes(0, neededBytes);
return neededBytes;
}
public static Vector3 ReadVector3(this DataReader reader)
{
return new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}
public static Vector4 ReadVector4(this DataReader reader)
{
return new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}
public static RGB ReadRGB(this DataReader reader)
{
return new RGB(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
}
public static RGB32 ReadRGB32(this DataReader reader)
{
return new RGB32(reader.ReadUInt32(), reader.ReadUInt32(), reader.ReadUInt32());
}
public static RGBA32 ReadRGBA32(this DataReader reader)
{
return new RGBA32(reader.ReadUInt32(), reader.ReadUInt32(), reader.ReadUInt32(), reader.ReadUInt32());
}
public static Matrix4x4 ReadMatrix4x4(this DataReader reader)
{
Matrix4x4 mtx = new Matrix4x4();
mtx.VM0 = reader.ReadVector4();
mtx.VM1 = reader.ReadVector4();
mtx.VM2 = reader.ReadVector4();
mtx.VM3 = reader.ReadVector4();
return mtx;
}
public static string ReadStringPointer(this DataReader reader, int addr)
{
if (addr <= 0)
return null;
else
{
string str = null;
reader.Stream.RunInPosition(delegate { str = reader.ReadString(); }, addr, SeekMode.Start);
return str;
}
}
public static void Write(this DataWriter writer, Vector3 vec)
{
writer.Write(vec.x);
writer.Write(vec.y);
writer.Write(vec.z);
}
public static void Write(this DataWriter writer, Vector4 vec)
{
writer.Write(vec.x);
writer.Write(vec.y);
writer.Write(vec.z);
writer.Write(vec.w);
}
public static void Write(this DataWriter writer, RGB col)
{
writer.Write(col.R);
writer.Write(col.G);
writer.Write(col.B);
}
public static void Write(this DataWriter writer, RGB32 col)
{
writer.Write(col.R);
writer.Write(col.G);
writer.Write(col.B);
}
public static void Write(this DataWriter writer, RGBA32 col)
{
writer.Write(col.R);
writer.Write(col.G);
writer.Write(col.B);
writer.Write(col.A);
}
public static void Write(this DataWriter writer, RGBA col)
{
writer.Write(col.r);
writer.Write(col.g);
writer.Write(col.b);
writer.Write(col.a);
}
public static byte[] ToArray(this DataStream stream)
{
long pos = stream.Position;
stream.Position = 0;
byte[] buf = new byte[stream.Length];
stream.Read(buf, 0, buf.Length);
stream.Position = pos;
return buf;
}
}
}