forked from henpemaz/Rain-Meadow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
331 lines (289 loc) · 12.7 KB
/
Extensions.cs
File metadata and controls
331 lines (289 loc) · 12.7 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using Menu;
using RWCustom;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace RainMeadow
{
public static class Extensions
{
public static Color ColorFromHex(int value)
{
return new Color(((value >> 16) & 0xff) / 255f, ((value >> 8) & 0xff) / 255f, (value & 0xff) / 255f);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static WorldSession? GetResource(this World world)
{
return WorldSession.map.TryGetValue(world, out var ws) ? ws : null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RoomSession? GetResource(this AbstractRoom room)
{
return RoomSession.map.TryGetValue(room, out var rs) ? rs : null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static OnlinePhysicalObject? GetOnlineObject(this AbstractPhysicalObject apo)
{
return OnlinePhysicalObject.map.TryGetValue(apo, out var oe) ? oe : null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool GetOnlineObject(this AbstractPhysicalObject apo, out OnlinePhysicalObject? opo) => OnlinePhysicalObject.map.TryGetValue(apo, out opo);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static OnlineCreature? GetOnlineCreature(this AbstractCreature ac) => GetOnlineObject(ac) as OnlineCreature;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool GetOnlineCreature(this AbstractCreature apo, out OnlineCreature? oc) => (oc = GetOnlineCreature(apo)) is not null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsLocal(this AbstractPhysicalObject apo) => OnlineManager.lobby is null || (GetOnlineObject(apo)?.isMine ?? true);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsLocal(this AbstractPhysicalObject apo, out OnlinePhysicalObject? opo)
{
opo = null;
return OnlineManager.lobby is null || (OnlinePhysicalObject.map.TryGetValue(apo, out opo) && opo.isMine);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsLocal(this PhysicalObject po) => IsLocal(po.abstractPhysicalObject);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsLocal(this PhysicalObject po, out OnlinePhysicalObject? opo) => IsLocal(po.abstractPhysicalObject, out opo);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CanMove(this AbstractPhysicalObject apo, WorldCoordinate? newCoord=null, bool quiet=false)
{
if (!GetOnlineObject(apo, out var oe)) return true;
if (!oe.isMine && !oe.beingMoved && (newCoord is null || oe.roomSession is null || oe.roomSession.absroom.index != newCoord.Value.room))
{
if (!quiet) RainMeadow.Error($"Remote entity trying to move: {oe} at {oe.roomSession} {Environment.StackTrace}");
return false;
}
return true;
}
public static void MoveMovable(this AbstractPhysicalObject apo, WorldCoordinate newCoord) {
foreach (AbstractPhysicalObject obj in apo.GetAllConnectedObjects()) {
if (obj.CanMove(newCoord, true)) {
if (newCoord.CompareDisregardingTile(obj.pos)) return;
obj.timeSpentHere = 0;
if (newCoord.room != obj.pos.room)
{
obj.ChangeRooms(newCoord);
}
if (!newCoord.TileDefined && obj.pos.room == newCoord.room)
{
newCoord.Tile = obj.pos.Tile;
}
obj.pos = newCoord;
obj.world.GetResource().ApoEnteringWorld(obj);
obj.world.GetAbstractRoom(newCoord.room).GetResource()?.ApoEnteringRoom(obj, newCoord);
}
}
}
public static void MoveOnly(this AbstractPhysicalObject apo, WorldCoordinate newCoord) {
if (apo.CanMove(newCoord)) {
if (newCoord.CompareDisregardingTile(apo.pos)) return;
apo.timeSpentHere = 0;
if (newCoord.room != apo.pos.room)
{
apo.ChangeRooms(newCoord);
}
if (!newCoord.TileDefined && apo.pos.room == newCoord.room)
{
newCoord.Tile = apo.pos.Tile;
}
apo.pos = newCoord;
apo.world.GetResource().ApoEnteringWorld(apo);
apo.world.GetAbstractRoom(newCoord.room).GetResource()?.ApoEnteringRoom(apo, newCoord);
}
}
public static bool RemoveFromShortcuts<T>(ref List<T> vessels, Creature creature, AbstractRoom? room = null) where T : ShortcutHandler.Vessel
{
bool removefromallrooms = room is null;
for (int i = 0; i < vessels.Count; i++)
{
if (vessels[i].creature == creature && ((vessels[i].room == room) || removefromallrooms))
{
vessels.RemoveAt(i);
creature.inShortcut = false;
return true;
}
}
return false;
}
public static bool RemoveFromShortcuts(this Creature creature, AbstractRoom? room = null)
{
if (!creature.inShortcut) return true;
var handler = creature.abstractCreature.world.game.shortcuts;
bool found = false;
if (RemoveFromShortcuts(ref handler.transportVessels, creature, room)) found = true;
if (RemoveFromShortcuts(ref handler.borderTravelVessels, creature, room)) found = true;
if (RemoveFromShortcuts(ref handler.betweenRoomsWaitingLobby, creature, room)) found = true;
if (!found) RainMeadow.Debug("not found");
return found;
}
// suck it, linq
public static Dictionary<TKey, TElement> ToDictionary<TKey, TElement>(this IEnumerable<KeyValuePair<TKey, TElement>> source)
{
Dictionary<TKey, TElement> dictionary = new Dictionary<TKey, TElement>();
foreach (KeyValuePair<TKey, TElement> item in source) // really though, you'd think there would be something like AddRange, but nah
{
dictionary.Add(item.Key, item.Value);
}
return dictionary;
}
public static (List<T1>, List<T2>) ToListTuple<T1, T2>(this IEnumerable<(T1, T2)> source)
{
var list = source.ToList(); // eval once
var listA = new List<T1>(list.Count);
var listB = new List<T2>(list.Count);
foreach (var t in list)
{
listA.Add(t.Item1);
listB.Add(t.Item2);
}
return (listA, listB);
}
// making linq better one extension at a time
public static T MinBy<T>(this IEnumerable<T> source, Func<T, float> evaluator)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
float num = 0f;
bool init = false;
T best = default(T);
foreach (T item in source)
{
var val = evaluator(item);
if (init)
{
if (!init || val < num || float.IsNaN(val))
{
num = val;
best = item;
}
}
else
{
num = val;
best = item;
init = true;
}
}
if (init)
{
return best;
}
throw new ArgumentException("no elements in sequence");
}
public static T? GetValueOrDefault<T>(this IList<T> iList, int index)
{
return iList.GetValueOrDefault(index, default);
}
public static T? GetValueOrDefault<T>(this IList<T> iList, int index, T? defaultVal)
{
return iList != null && index >= 0 && iList.Count > index? iList[index] : defaultVal;
}
public static bool CloseEnoughZeroSnap(this Vector2 a, Vector2 b, float sqrltol)
{
if (a == b) return true;
if (a.x == 0 && a.y == 0) return false; // zero and non-zero situation!
if (b.x == 0 && b.y == 0) return false;
return (a - b).sqrMagnitude < sqrltol;
}
public static bool CloseEnough(this Vector2 a, Vector2 b, float sqrtol)
{
return a == b || (a - b).sqrMagnitude < sqrtol;
}
public static HSLColor ToHSL(this Color c)
{
var cv = Custom.RGB2HSL(c);
return new HSLColor(cv[0], cv[1], cv[2]);
}
// copied from futile but tweaked for accurate result on non-zero-centered rect
public static Vector2 GetClosestInteriorPointAlongLineFromCenter(this Rect rect, Vector2 targetPoint)
{
//if it's inside the rect, don't do anything
if (targetPoint.x >= rect.xMin &&
targetPoint.x <= rect.xMax &&
targetPoint.y >= rect.yMin &&
targetPoint.y <= rect.yMax) return targetPoint;
float halfWidth = rect.width * 0.5f;
float halfHeight = rect.height * 0.5f;
targetPoint -= rect.center; // from center
targetPoint.Normalize();
float absX = Mathf.Abs(targetPoint.x);
float absY = Mathf.Abs(targetPoint.y);
if (halfWidth * absY <= halfHeight * absX)
{
return targetPoint * halfWidth / absX + rect.center;
}
else
{
return targetPoint * halfHeight / absY + rect.center;
}
}
public static Color SafeColorRange(this Color valuecolor)
{
return new Color(Mathf.Clamp(valuecolor.r, 1f / 255f, 1f), Mathf.Clamp(valuecolor.g, 1f / 255f, 1f), Mathf.Clamp(valuecolor.b, 1f / 255f, 1f));
}
public static Type[] GetTypesSafely(this Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException e) // happens often with soft-dependencies, did you know
{
return e.Types.Where(x => x != null).ToArray();
}
}
//faster for adding menuobjects smth
public static void ClearMenuObjectIList<T>(this MenuObject owner, IEnumerable<T>? menuObjects) where T : MenuObject
{
if (menuObjects != null)
{
foreach (MenuObject menuObject in menuObjects)
{
owner.ClearMenuObject(menuObject);
}
}
}
public static void ClearMenuObject<T>(this MenuObject owner, ref T? subObject) where T : MenuObject
{
owner.ClearMenuObject(subObject);
subObject = null;
}
public static void ClearMenuObject(this MenuObject owner, MenuObject? subObject)
{
if (subObject != null)
{
subObject.RemoveSprites();
owner.RemoveSubObject(subObject);
}
}
public static void TryBind(this MenuObject? menuObject, MenuObject? bindWith, bool left = false, bool right = false, bool top = false, bool bottom = false)
{
if (menuObject != null && bindWith != null)
{
menuObject.nextSelectable[0] = (left ? bindWith : menuObject.nextSelectable[0]);
menuObject.nextSelectable[1] = (top ? bindWith : menuObject.nextSelectable[1]);
menuObject.nextSelectable[2] = (right ? bindWith : menuObject.nextSelectable[2]);
menuObject.nextSelectable[3] = (bottom ? bindWith : menuObject.nextSelectable[3]);
}
}
public static void TryMutualBind(this Menu.Menu? menu, MenuObject? first, MenuObject? second, bool leftRight = false, bool bottomTop = false)
{
if (menu != null && first != null && second != null)
{
if (leftRight)
{
menu.MutualHorizontalButtonBind(first, second);
}
if (bottomTop)
{
menu.MutualVerticalButtonBind(first, second);
}
}
}
}
}