/
Utils.cs
163 lines (142 loc) · 5.14 KB
/
Utils.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
using BepInEx;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace WafflesWeapons
{
public static class PathUtils
{
public static string GameDirectory()
{
string path = Application.dataPath;
if (Application.platform == RuntimePlatform.OSXPlayer)
{
path = Utility.ParentDirectory(path, 2);
}
else if (Application.platform == RuntimePlatform.WindowsPlayer)
{
path = Utility.ParentDirectory(path, 1);
}
return path;
}
public static string ModDirectory()
{
// return Path.Combine(GameDirectory(), @"BepInEx\UMM Mods");
return Path.Combine(GameDirectory(), "BepInEx", "plugins");
}
public static string ModPath()
{
return Assembly.GetExecutingAssembly().Location.Substring(0, Assembly.GetExecutingAssembly().Location.LastIndexOf(Path.DirectorySeparatorChar));
}
}
public static class UnityUtils
{
public static GameObject ChildByName(this GameObject from, string name)
{
List<GameObject> children = new List<GameObject>();
int count = 0;
while (count < from.transform.childCount)
{
children.Add(from.transform.GetChild(count).gameObject);
count++;
}
if (count == 0)
{
return null;
}
for (int i = 0; i < children.Count; i++)
{
if (children[i].name == name)
{
return children[i];
}
}
return null;
}
public static List<GameObject> FindSceneObjects(string sceneName)
{
List<GameObject> objs = new List<GameObject>();
foreach (GameObject obj in GameObject.FindObjectsOfType<GameObject>())
{
if (obj.scene.name == sceneName)
{
objs.Add(obj);
}
}
return objs;
}
public static List<GameObject> ChildrenList(this GameObject from)
{
List<GameObject> children = new List<GameObject>();
int count = 0;
while (count < from.transform.childCount)
{
children.Add(from.transform.GetChild(count).gameObject);
count++;
}
return children;
}
public static bool Contains(this LayerMask mask, int layer)
{
return mask == (mask | (1 << layer));
}
}
public class UltrakillUtils
{
public static GameObject NearestEnemy(Vector3 point, float maxDistance, EnemyIdentifier exclude = null)
{
float max = maxDistance;
GameObject nearestEnemy = null;
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject enemy in enemies)
{
if (enemy.GetComponent<EnemyIdentifier>() != null && !enemy.GetComponent<EnemyIdentifier>().dead &&
enemy.GetComponent<EnemyIdentifier>() != exclude &&Vector3.Distance(point, enemy.transform.position) < max)
{
max = Vector3.Distance(point, enemy.transform.position);
nearestEnemy = enemy;
}
}
return nearestEnemy;
}
public static Vector3 NearestEnemyPoint(Vector3 point, float maxDistance, EnemyIdentifier exclude = null)
{
float max = maxDistance;
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
Transform enemyFinal = null;
foreach (GameObject enemy in enemies)
{
EnemyIdentifier eid = enemy.GetComponent<EnemyIdentifier>();
Transform enemyTransform = null;
if (eid != null && !eid.dead && eid != exclude && Vector3.Distance(point, enemy.transform.position) < max)
{
if (eid.weakPoint != null && eid.weakPoint.activeInHierarchy)
{
enemyTransform = eid.weakPoint.transform;
}
else
{
EnemyIdentifierIdentifier eidid = eid.GetComponentInChildren<EnemyIdentifierIdentifier>();
if (eidid != null)
{
enemyTransform = eidid.transform;
}
else
{
enemyTransform = enemy.transform;
}
}
max = Vector3.Distance(point, enemyTransform.position);
enemyFinal = enemyTransform;
}
}
return (enemyFinal != null ? enemyFinal.position : point);
}
}
}