-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cs
More file actions
267 lines (237 loc) · 8.94 KB
/
Helpers.cs
File metadata and controls
267 lines (237 loc) · 8.94 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
#if DEBUG
#define ENABLE_PROFILER
#endif
using I2.Loc;
using MelonLoader;
using NeonLite.Modules;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using Unity.Profiling;
using UnityEngine;
namespace NeonNetwork
{
internal static class Extensions
{
public static Vector3 ScreenToCanvasPosition(this Canvas canvas, Vector3 screenPosition)
{
var viewportPosition = new Vector3(screenPosition.x / Screen.width,
screenPosition.y / Screen.height,
0);
return canvas.ViewportToCanvasPosition(viewportPosition);
}
public static Vector3 ViewportToCanvasPosition(this Canvas canvas, Vector3 viewportPosition)
{
var centerBasedViewPortPosition = viewportPosition - new Vector3(0.5f, 0.5f, 0);
var canvasRect = canvas.GetComponent<RectTransform>();
var scale = canvasRect.sizeDelta;
return Vector3.Scale(centerBasedViewPortPosition, scale);
}
public static void OnAllChildren(this Transform transform, Action<Transform> callback)
{
if (transform.childCount == 0)
callback(transform);
else
{
foreach (Transform child in transform)
child.OnAllChildren(callback);
callback(transform);
}
}
// https://stackoverflow.com/a/12389412
public static IEnumerable<IEnumerable<TValue>> Chunk<TValue>(
this IEnumerable<TValue> values,
int chunkSize)
{
return values
.Select((v, i) => new { v, groupIndex = i / chunkSize })
.GroupBy(x => x.groupIndex)
.Select(g => g.Select(x => x.v));
}
}
internal class Transition(float speed, Func<float, float, float, float> ease, Dictionary<float, Action> timestamps)
{
public Dictionary<float, Action> timestamps = timestamps;
Func<float, float, float, float> easeFunc = ease;
List<float> hit = [];
public bool skip = false;
public float start;
public float goal;
public float result;
public bool running;
public float speed = speed;
public float time;
float speedMult;
public static readonly Func<float, float, float, float> opacityEase = AxKEasing.EaseOutQuart;
public static readonly Func<float, float, float, float> movementEase = AxKEasing.EaseOutCubic;
public void Start(float? s, float g, bool cont = true, float forceT = 0)
{
speedMult = 1;
start = s ?? result;
result = start;
goal = g;
hit = [];
if ((forceT != 0) || (cont && !s.HasValue && running && time != 0))
speedMult = 1 / (forceT == 0 ? time : forceT);
time = 0;
running = true;
}
public void Set(float value) => Stop(value);
public void Stop(float? value = null)
{
time = 1;
running = false;
goal = value ?? result;
Process();
}
void CheckTS()
{
foreach (var kp in timestamps.Where(kp => kp.Key <= time && !hit.Contains(kp.Key)))
{
hit.Add(kp.Key);
kp.Value.Invoke();
}
}
public void Process(bool skipTS = false)
{
if (time == 1f)
{
result = goal;
bool wasRunning = running;
running = false;
if (wasRunning && timestamps.TryGetValue(10f, out var finish))
finish();
}
if (!running)
return;
time = Math.Min(1f, time + (Time.unscaledDeltaTime * speedMult * speed));
result = easeFunc(start, goal, time);
if (!skipTS)
CheckTS();
}
public static void ProcessAll(object t)
{
var fields = t.GetType().GetFields(HarmonyLib.AccessTools.all);
var transitions = fields.Where(f => f.FieldType == typeof(Transition));
foreach (var transition in transitions.Select(f => (Transition)f.GetValue(t)))
{
//NeonNetwork.Logger.Msg($"{transition} {t}");
transition.Process();
}
}
}
internal static class Localization
{
public static AxKLocalizedText Setup(Component component, int fontBase = -1, List<AxKReplacementPair> pairs = null)
{
var ret = NeonLite.Modules.Localization.SetupUI(component, fontBase);
if (LocalizationManager.TryGetTranslation(ret.textMeshProUGUI.text, out _))
ret.SetKey(ret.textMeshProUGUI.text, pairs?.ToArray() ?? []);
return ret;
}
public static AxKLocalizedText Setup(GameObject obj, int fontBase = -1, List<AxKReplacementPair> pairs = null) => Setup(obj.transform, fontBase, pairs);
}
internal static class Helpers
{
public static int GetMedalIndexSafely(LevelData level, long time = -1)
{
if (CommunityMedals.medalTimes.ContainsKey(level.levelID))
return CommunityMedals.GetMedalIndex(level.levelID, time);
var stats = GameDataManager.GetLevelStats(level.levelID);
if (time == -1)
{
if (!stats.GetCompleted())
return -1;
time = stats._timeBestMicroseconds;
}
List<long> times = [
long.MaxValue,
Utils.ConvertSeconds_FloatToMicroseconds(level.GetTimeSilver()),
Utils.ConvertSeconds_FloatToMicroseconds(level.GetTimeGold()),
Utils.ConvertSeconds_FloatToMicroseconds(level.GetTimeAce()),
Utils.ConvertSeconds_FloatToMicroseconds(level.GetTimeDev()),
];
for (int i = times.Count - 1; i >= 0; i--)
{
if (time <= times[i])
return i;
}
return 0;
}
static readonly Stack<ProfilerMarker> currentMarkers = [];
static readonly Stack<Tuple<string, Stopwatch>> currentWatches = [];
static bool profiling = true;
#if !DEBUG
#pragma warning disable CS0162
#endif
[Conditional("ENABLE_PROFILER")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void EnableProfiling(bool enable) => profiling = enable;
[Conditional("ENABLE_PROFILER")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void StartProfiling(string name)
{
if (!profiling)
return;
if (NeonNetwork.DEBUG)
currentWatches.Push(new(name, new Stopwatch()));
currentMarkers.Push(new(ProfilerCategory.Scripts, name));
currentMarkers.Peek().Begin();
if (NeonNetwork.DEBUG)
{
//NeonNetwork.Logger.Msg($"{name} - START");
currentWatches.Peek().Item2.Start();
}
}
#if ENABLE_PROFILER
public static IEnumerable<T> ProfileLoop<T>(this IEnumerable<T> loop, string name)
{
StartProfiling(name);
int i = 0;
foreach (T t in loop)
{
StartProfiling($"{name}#{++i}");
yield return t;
EndProfiling();
}
EndProfiling();
}
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IEnumerable<T> ProfileLoop<T>(this IEnumerable<T> loop, string _) => loop;
#endif
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("ENABLE_PROFILER")]
public static void EndProfiling(string _) => EndProfiling();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("ENABLE_PROFILER")]
public static void EndProfiling()
{
if (!profiling)
return;
currentMarkers.Pop().End();
if (NeonNetwork.DEBUG)
{
(var name, var watch) = currentWatches.Pop();
watch.Stop();
//NeonNetwork.Logger.Msg($"{name} - {watch.Elapsed.TotalMilliseconds}ms");
}
}
[Conditional("DEBUG")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void DebugMsg(this MelonLogger.Instance log, string msg)
{
if (NeonNetwork.DEBUG)
{
log.Msg(msg);
UnityEngine.Debug.Log($"[NeonLite] {msg}");
}
}
[Conditional("DEBUG")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void DebugMsg(this MelonLogger.Instance log, object obj) => DebugMsg(log, obj.ToString());
}
}