-
Notifications
You must be signed in to change notification settings - Fork 1
/
Util.cs
433 lines (377 loc) · 15.2 KB
/
Util.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Nautilus.Handlers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Tweaks_Fixes;
using UnityEngine;
using static ErrorMessage;
using static Nautilus.Assets.CustomModelData;
namespace Tweaks_Fixes
{
public static class Util
{
static Dictionary<TechType, GameObject> prefabs = new Dictionary<TechType, GameObject>();
public static bool spawning;
public static bool GetTarget(Vector3 startPos, Vector3 dir, float distance, out RaycastHit hitInfo)
{
//return Physics.Raycast(startPos, dir, out hitInfo, distance, Voxeland.GetTerrainLayerMask(), QueryTriggerInteraction.Ignore);
return Physics.Raycast(startPos, dir, out hitInfo, distance);
}
public static IEnumerator SetParent(GameObject go, Transform parent, int framesToWait = 1)
{
while (framesToWait >= 0)
{
framesToWait--;
yield return null;
if (framesToWait == 0)
{
go.transform.SetParent(parent);
AddDebug("SetParent ");
}
}
}
public static IEnumerator Cook(GameObject go)
{
TechType cookedData = CraftData.GetCookedData(CraftData.GetTechType(go.gameObject));
//Main.logger.LogDebug("CookFish " + go.name + " cookedData " + cookedData);
if (cookedData == TechType.None)
yield break;
TaskResult<GameObject> result = new TaskResult<GameObject>();
yield return CraftData.InstantiateFromPrefabAsync(cookedData, (IOut<GameObject>)result);
GameObject cooked = result.Get();
cooked.transform.position = go.transform.position;
cooked.transform.rotation = go.transform.rotation;
Rigidbody rb = cooked.GetComponent<Rigidbody>();
rb.mass = go.GetComponent<Rigidbody>().mass;
rb.velocity = go.GetComponent<Rigidbody>().velocity;
rb.angularDrag = 1;
rb.drag = 1; // WorldForces.Start overwrites drag
UnityEngine.Object.Destroy(go);
}
public static Component CopyComponent(Component original, GameObject destination)
{
System.Type type = original.GetType();
Component copy = destination.AddComponent(type);
// Copied fields can be restricted with BindingFlags
System.Reflection.FieldInfo[] fields = type.GetFields();
foreach (System.Reflection.FieldInfo field in fields)
{
field.SetValue(copy, field.GetValue(original));
}
return copy;
}
public static T CopyComponent<T>(T original, GameObject destination) where T : Component
{
Type type = ((object)original).GetType();
Component component = destination.AddComponent(type);
foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
field.SetValue((object)component, field.GetValue((object)original));
return component as T;
}
public static void DropItems(ItemsContainer container)
{
List<Pickupable> pickList = new List<Pickupable>();
Dictionary<TechType, ItemsContainer.ItemGroup>.Enumerator enumerator = container._items.GetEnumerator();
while (enumerator.MoveNext())
{
List<InventoryItem> items = enumerator.Current.Value.items;
for (int index = 0; index < items.Count; ++index)
pickList.Add(items[index].item);
}
foreach (Pickupable p in pickList)
{
//AddDebug("Drop " + p.GetTechName());
p.Drop();
}
}
public static bool CanPlayerEat()
{
if (GameModeUtils.IsOptionActive(GameModeOption.NoSurvival))
return false;
bool cantEat = ConfigMenu.cantEatUnderwater.Value && Player.main.isUnderwater.value;
return !cantEat;
}
public static void FreezeObject(GameObject go, bool state)
{
WorldForces wf = go.GetComponent<WorldForces>();
Rigidbody rb = go.GetComponent<Rigidbody>();
if (wf && rb)
{
wf.enabled = !state;
rb.isKinematic = state;
}
}
public static Bounds GetAABB(GameObject go)
{
FixedBounds fb = go.GetComponent<FixedBounds>();
Bounds bounds = fb == null ? UWE.Utils.GetEncapsulatedAABB(go) : fb.bounds;
return bounds;
}
public static string GetGameObjectPath(GameObject obj)
{
string path = "/" + obj.name;
while (obj.transform.parent != null)
{
obj = obj.transform.parent.gameObject;
path = "/" + obj.name + path;
}
return path;
}
public static ItemsContainer GetOpenContainer()
{
int storageCount = Inventory.main.usedStorage.Count;
if (storageCount > 0)
{
IItemsContainer itemsContainer = Inventory.main.usedStorage[storageCount - 1];
if (itemsContainer is ItemsContainer)
return itemsContainer as ItemsContainer;
}
return null;
}
public static bool IsDead(GameObject go)
{
LiveMixin liveMixin = go.GetComponent<LiveMixin>();
return liveMixin && !liveMixin.IsAlive();
}
public static bool IsDestroyed(GameObject gameObject)
{
// UnityEngine overloads the == opeator for the GameObject type
// and returns null when the object has been destroyed, but
// actually the object is still there but has not been cleaned up yet
// if we test both we can determine if the object has been destroyed.
return gameObject == null && !ReferenceEquals(gameObject, null);
}
public static bool IsDestroyed(Component component)
{
return component == null && !ReferenceEquals(component, null);
}
public static bool IsEatableFish(GameObject go)
{
Creature creature = go.GetComponent<Creature>();
if (creature == null)
return false;
return go.GetComponent<Eatable>();
}
public static bool IsCreatureAlive(GameObject go)
{
Creature creature = go.GetComponent<Creature>();
if (creature == null)
return false;
LiveMixin liveMixin = go.GetComponent<LiveMixin>();
if (liveMixin == null)
return false;
return liveMixin.IsAlive();
}
public static void MakeEatable(GameObject go, float food)
{
Eatable eatable = go.EnsureComponent<Eatable>();
eatable.foodValue = food;
eatable.despawns = IsEatableFish(go);
}
public static void MakeDrinkable(GameObject go, float water)
{
Eatable eatable = go.EnsureComponent<Eatable>();
eatable.waterValue = water;
eatable.despawns = IsEatableFish(go);
}
public static void Message(string str)
{
int count = main.messages.Count;
if (count == 0)
{
AddDebug(str);
}
else
{
_Message message = main.messages[main.messages.Count - 1];
message.messageText = str;
message.entry.text = str;
}
}
public static float NormalizeTo01range(int value, int min, int max)
{
float fl;
int oldRange = max - min;
if (oldRange == 0)
fl = 0f;
else
fl = ((float)value - (float)min) / (float)oldRange;
return fl;
}
public static float NormalizeTo01range(float value, float min, float max)
{
float fl;
float oldRange = max - min;
if (oldRange == 0)
fl = 0f;
else
fl = ((float)value - (float)min) / (float)oldRange;
return fl;
}
public static int NormalizeToRange(int value, int oldMin, int oldMax, int newMin, int newMax)
{
int oldRange = oldMax - oldMin;
int newValue;
if (oldRange == 0)
newValue = newMin;
else
{
int newRange = newMax - newMin;
newValue = ((value - oldMin) * newRange) / oldRange + newMin;
}
return newValue;
}
public static float NormalizeToRange(float value, float oldMin, float oldMax, float newMin, float newMax)
{
float oldRange = oldMax - oldMin;
float newValue;
if (oldRange == 0)
newValue = newMin;
else
{
float newRange = newMax - newMin;
newValue = ((value - oldMin) * newRange) / oldRange + newMin;
}
return newValue;
}
static IEnumerator PlayClip(Animator animator, string name, float delay = 0f)
{
AddDebug("PlayClip start " + delay);
yield return new WaitForSeconds(delay);
AddDebug("PlayClip " + name);
animator.Play(name);
}
static bool CloseToPosition(Vector3 pos1, Vector3 pos2, float range) => (pos1 - pos2).sqrMagnitude < range * range;
public static void FindObjectClosestToPlayer(float dist)
{
Transform[] ts = UnityEngine.Object.FindObjectsOfType<Transform>();
AddDebug("Transforms found " + ts.Length);
List<GameObject> list = new List<GameObject>();
foreach (Transform t in ts)
{
if (t.GetComponentInParent<Player>())
continue;
Vector3 dir = t.transform.position - Player.mainObject.transform.position;
if (dir.magnitude < dist)
list.Add(t.gameObject);
}
foreach (GameObject go in list)
{
AddDebug(" " + go.name);
Main.logger.LogInfo("FindObjectClosestToPlayer " + go.name);
}
}
public static void EnsureFruits(GameObject go)
{
PickPrefab[] pickPrefabs = go.GetComponentsInChildren<PickPrefab>(true);
if (pickPrefabs.Length == 0)
return;
FruitPlant fp = go.EnsureComponent<FruitPlant>();
fp.fruitSpawnEnabled = true;
//AddDebug(__instance.name + " fruitSpawnInterval orig " + fp.fruitSpawnInterval);
// fruitSpawnInterval will be mult by 'plants growth' from Day night speed mod
fp.fruitSpawnInterval = ConfigMenu.fruitGrowTime.Value * Main.dayLengthSeconds;
//AddDebug(__instance.name + " fruitSpawnInterval " + fp.fruitSpawnInterval);
if (fp.fruitSpawnInterval == 0f)
fp.fruitSpawnInterval = 1f;
//AddDebug(__instance.name + " fruitSpawnInterval after " + fp.fruitSpawnInterval);
fp.fruits = pickPrefabs;
}
public static VFXSurfaceTypes GetObjectSurfaceType(GameObject obj)
{
VFXSurfaceTypes result = VFXSurfaceTypes.none;
if (obj)
{
VFXSurface vfxSurface = obj.GetComponent<VFXSurface>();
if (vfxSurface)
{
result = vfxSurface.surfaceType;
//AddDebug(" VFXSurface " + component.name);
//AddDebug(" VFXSurface parent " + component.transform.parent.name);
//AddDebug(" VFXSurface parent parent " + component.transform.parent.parent.name);
}
else
vfxSurface = obj.FindAncestor<VFXSurface>();
if (vfxSurface)
result = vfxSurface.surfaceType;
}
return result;
}
static IEnumerator PrintMass(TechType techType)
{
CoroutineTask<GameObject> request = CraftData.GetPrefabForTechTypeAsync(techType, false);
yield return request;
GameObject go = request.GetResult();
if (go)
{
Rigidbody rb = go.GetComponent<Rigidbody>();
if (rb)
{
string name = Language.main.Get(techType);
string s = techType + ", " + name + ", mass " + rb.mass;
//massList.Add(s);
}
}
}
public static GameObject GetEntityRoot(GameObject go)
{
UniqueIdentifier prefabIdentifier = go.GetComponent<UniqueIdentifier>();
if (prefabIdentifier == null)
prefabIdentifier = go.GetComponentInParent<UniqueIdentifier>();
return prefabIdentifier != null ? prefabIdentifier.gameObject : null;
}
public static IEnumerable<GameObject> FindAllRootGameObjects()
{
return Resources.FindObjectsOfTypeAll<Transform>()
.Where(t => t.parent == null)
.Select(x => x.gameObject);
}
public static bool Approximately(float a, float b, float tolerance = 0.00001f)
{ // Mathf.Approximately does not work when compare to 0
return (Mathf.Abs(a - b) < tolerance);
}
public static IEnumerator Spawn(TechType techType, Vector3 pos = default, bool fadeIn = false)
{
//AddDebug("Spawn " + techType);
GameObject prefab;
if (prefabs.ContainsKey(techType))
prefab = prefabs[techType];
else
{
TaskResult<GameObject> result = new TaskResult<GameObject>();
yield return CraftData.GetPrefabForTechTypeAsync(techType, false, result);
prefab = result.Get();
prefabs[techType] = prefab;
}
if (!fadeIn)
spawning = true;
GameObject go = prefab == null ? Utils.CreateGenericLoot(techType) : Utils.SpawnFromPrefab(prefab, null);
if (go != null)
{
if (pos == default)
{
Transform camTr = MainCamera.camera.transform;
go.transform.position = camTr.position + camTr.forward * 3f;
}
go.transform.position = pos;
//AddDebug("Spawn " + techType + " " + pos);
CrafterLogic.NotifyCraftEnd(go, techType);
}
spawning = false;
}
public static float CelciusToFahrenhiet(float celcius)
{
return celcius * 1.8f + 32f;
}
public static bool IsGraphicsPresetHighDetail()
{
return GraphicsPreset.GetPresets()[QualitySettings.GetQualityLevel()].detail == 2;
}
}
}