-
Notifications
You must be signed in to change notification settings - Fork 11
/
KS3PUtil.cs
244 lines (231 loc) · 8.58 KB
/
KS3PUtil.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using KSP_PostProcessing.Parsers;
namespace KSP_PostProcessing
{
/// <summary>
/// Contains several (extension) methods used by KS3P.
/// </summary>
internal static class KS3PUtil
{
/// <summary>
/// Preprocesses a string for parsing.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
internal static string Prepare(string s) => s.Replace("_", string.Empty).ToLower();
internal static bool Contains<T>(this T[] array, T item)
{
for(int x = 0; x < array.Length; x++)
{
if(array[x].Equals(item))
{
return true;
}
}
return false;
}
internal static bool Contains<T>(this T[] array, T item, out int pos)
{
for (int x = 0; x < array.Length; x++)
{
if (array[x].Equals(item))
{
pos = x;
return true;
}
}
pos = -1;
return false;
}
internal static void Remove<T>(this List<T> list, int id)
{
list.Remove(list[id]);
}
internal static bool IsParsableType(this Type t)
{
switch (Type.GetTypeCode(t))
{
case TypeCode.Boolean: return true;
case TypeCode.Byte: return true;
case TypeCode.Char: return true;
case TypeCode.Decimal: return true;
case TypeCode.Double: return true;
case TypeCode.Int16: return true;
case TypeCode.Int32: return true;
case TypeCode.Int64: return true;
case TypeCode.Object: return true;
case TypeCode.SByte: return true;
case TypeCode.Single: return true;
case TypeCode.String: return true;
case TypeCode.UInt16: return true;
case TypeCode.UInt32: return true;
case TypeCode.UInt64: return true;
default:
if (t == typeof(Vector2))
{
return true;
}
else if (t == typeof(Vector3))
{
return true;
}
else if (t == typeof(Vector4))
{
return true;
}
else if (t == typeof(Color))
{
return true;
}
else if (t == typeof(Texture) || t == typeof(Texture2D))
{
return true;
}
else { return false; }
}
}
internal static V Grab<K, V>(this Dictionary<K,V> dictionary, K key) where V : class
{
if (dictionary.ContainsKey(key))
{
return dictionary[key];
}
else return null;
}
internal static string TrimRGBA(this string s)
{
char[] separator = { '(' };
string[] parts = s.Split(separator);
separator = new char[1] { ')' };
parts = parts[1].Split(separator);
return parts[0];
}
internal static bool TryParseKeyframe(string input, out Keyframe output)
{
char[] separator = { ',' };
string[] parts = input.Split(separator);
if (parts.Length < 2)
{
output = new Keyframe();
return false;
}
else
{
if (parts.Length == 2)
{
float[] data = { 0f, 0f };
float calcFloat;
if (float.TryParse(parts[0], out calcFloat))
{
data[0] = calcFloat;
}
if (float.TryParse(parts[1], out calcFloat))
{
data[1] = calcFloat;
}
output = new Keyframe(data[0], data[1]);
return true;
}
else
{
float[] data = { 0f, 0f, 0f, 0f };
float calcFloat;
for (int i = 0; i < parts.Length; i++)
{
if (float.TryParse(parts[i], out calcFloat))
{
data[i] = calcFloat;
}
}
output = new Keyframe(data[0], data[1], data[2], data[3]);
return true;
}
}
}
internal static bool TryAdd(this AnimationCurve curve, string input)
{
Keyframe frame;
if (TryParseKeyframe(input, out frame))
{
curve.AddKey(frame);
return true;
}
else return false;
}
internal static bool TryAdd(this AnimationCurve curve, string input, out int index)
{
Keyframe frame;
if (TryParseKeyframe(input, out frame))
{
index = curve.AddKey(frame);
return true;
}
else
{
index = 0;
return false;
}
}
internal static T[] AsArray<T>(this T obj)
{
return new T[1] { obj };
}
internal static string RemoveEnd(this string s, int count)
{
char[] parts = s.ToCharArray();
string target = string.Empty;
for(int i = 0; i < parts.Length - count; i++)
{
target += parts[i].ToString();
}
return target;
}
internal static string Root { get; private set; } = KSPUtil.ApplicationRootPath.Replace("KSP_x64_Data/../", string.Empty);
internal static string Log { get; private set; } = Path.Combine(Root, Path.Combine("GameData", "KS3P"));
internal static string Export { get; private set; } = Path.Combine(Log, "Export");
internal static void Blit(Texture source, Material mat)
{
Graphics.Blit(source, mat);
}
internal static void Blit(Texture source, RenderTexture dest)
{
Graphics.Blit(source, dest);
}
internal static void Blit(Texture source, Material mat, int pass)
{
Graphics.Blit(source, mat, pass);
}
internal static void Blit(Texture source, RenderTexture dest, Material mat)
{
Graphics.Blit(source, dest, mat);
}
internal static void Blit(Texture source, RenderTexture dest, Material mat, int pass)
{
Graphics.Blit(source, dest, mat, pass);
}
internal static void Blit(Texture source, RenderTexture dest, Vector2 scale, Vector2 offset)
{
Graphics.Blit(source, dest, scale, offset);
}
internal static AntiAliasingParser aaParser = new AntiAliasingParser();
internal static AmbientOcclusionParser aoParser = new AmbientOcclusionParser();
internal static DepthOfFieldParser dofParser = new DepthOfFieldParser();
internal static MotionBlurParser mbParser = new MotionBlurParser();
internal static EyeAdaptationParser eaParser = new EyeAdaptationParser();
internal static BloomParser bParser = new BloomParser();
internal static ColorGradingParser cgParser = new ColorGradingParser();
internal static UserLutParser ulParser = new UserLutParser();
internal static ChromaticAbberationParser caParser = new ChromaticAbberationParser();
internal static GrainParser gParser = new GrainParser();
internal static VignetteParser vParser = new VignetteParser();
internal static ScreenSpaceReflectionParser ssrParser = new ScreenSpaceReflectionParser();
internal static Vector2 GetBounds(this AnimationCurve curve)
{
var frames = curve.keys;
return new Vector2(frames[0].time, frames[frames.Length - 1].time);
}
}
}