-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
54 lines (52 loc) · 1.77 KB
/
Extensions.cs
File metadata and controls
54 lines (52 loc) · 1.77 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
using BlueprintCore.Utils;
using Kingmaker.Blueprints;
using System;
using System.Linq;
namespace WOTR_BOAT_BOAT_BOAT
{
static class Extensions
{
public static void RemoveComponents<T>(this BlueprintScriptableObject obj) where T : BlueprintComponent
{
var components_to_remove = obj.GetComponents<T>().ToArray();
foreach (var c in components_to_remove)
{
obj.SetComponents(obj.ComponentsArray.RemoveFromArray(c));
}
}
public static T[] RemoveFromArray<T>(this T[] array, T value)
{
var list = array.ToList();
return list.Remove(value) ? list.ToArray() : array;
}
public static void AddComponent<T>(this BlueprintScriptableObject obj, Action<T> init = null) where T : BlueprintComponent, new()
{
obj.SetComponents(obj.ComponentsArray.AppendToArray(Create(init)));
}
public static T[] AppendToArray<T>(this T[] array, T value)
{
var len = array.Length;
var result = new T[len + 1];
Array.Copy(array, result, len);
result[len] = value;
return result;
}
public static T Create<T>(Action<T> init = null) where T : new()
{
var result = new T();
init?.Invoke(result);
return result;
}
public static T EditComponent<T>(this BlueprintScriptableObject obj, Action<T> build) where T : BlueprintComponent
{
var component = obj.GetComponent<T>();
build(component);
return component;
}
public static T GetComponent<T>(this T obj, Action<T> init)
{
init?.Invoke(obj);
return obj;
}
}
}