-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cs
More file actions
125 lines (109 loc) · 4.16 KB
/
Main.cs
File metadata and controls
125 lines (109 loc) · 4.16 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
namespace MoreLandings;
[Landfall.Modding.LandfallPlugin]
public class MoreLandings
{
/// <summary>
/// Static constructor. Shocker right?
/// </summary>
static MoreLandings()
{
// Create the config
HastySetting cfg = new("MoreLandings");
// Tell our config class to load the UI
cfg.OnConfig += () => new Config(cfg);
// Add event
On.UI_LandingScore.Land += ChangeTextOnLand;
}
/// <summary>
/// Console command that displays the current JSON path.
/// </summary>
[Zorro.Core.CLI.ConsoleCommand]
public static void JSONPath()
{
Informer.Inform($"Your JSON path is: {Config.jsonPath}", InformType.Info);
}
/// <summary>
/// Console command that displays the contents of each landing type.
/// </summary>
[Zorro.Core.CLI.ConsoleCommand]
public static void Landings()
{
foreach (KeyValuePair<Config.MoreLandingType, List<string>> kvp in Config.LandingStrings)
{
List<string> strings = kvp.Value.ToList();
Informer.Inform($"{kvp.Key}: [", InformType.Info);
strings.ForEach(s => Informer.Inform($" - {s}", InformType.Warn));
Informer.Inform("]", InformType.Info);
}
}
/// <summary>
/// Console command to reload landing strings from the JSON file.
/// </summary>
[Zorro.Core.CLI.ConsoleCommand]
public static void Reload()
{
Informer.Inform("Loading json strings. Please wait...", InformType.Info);
Config.LoadStrings();
}
/// <summary>
/// Hook method that changes the landing text when a landing event occurs.
/// </summary>
/// <param name="orig">The original landing method delegate.</param>
/// <param name="self">The UI_LandingScore instance.</param>
/// <param name="type">The type of landing.</param>
/// <param name="savedLanding">Indicates if the landing was saved.</param>
private static void ChangeTextOnLand(On.UI_LandingScore.orig_Land orig, UI_LandingScore self, PlayerCharacter player, LandingType type, bool savedLanding)
{
// Check if we should force a landing type
switch (Config.ForceType?.Value)
{
case Config.MoreLandingType.None:
break; // Do nothing, use original landing type
case Config.MoreLandingType.Bad:
case Config.MoreLandingType.Ok:
case Config.MoreLandingType.Good:
case Config.MoreLandingType.Perfect:
type = (LandingType)Config.ForceType.Value; // Force the landing type
break;
case Config.MoreLandingType.Saved:
savedLanding = true;
break;
default:
Informer.Inform($"Unknown landing type: {Config.ForceType!.Value}", InformType.Error);
return;
}
// Run original method
orig(self, player, type, savedLanding);
// Roll
if (UnityEngine.Random.Range(0f, 1f) > Config.CustomFreq?.Value) { return; }
// Get animator
if (self.TryGetComponent(out UnityEngine.Animator anim))
{
// Get text
System.Reflection.FieldInfo textField = typeof(UI_LandingScore).GetField("text", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (textField != null && textField.GetValue(self) is TMPro.TextMeshProUGUI text)
{
// Get random string from landingstrings
string landText = RandomElement(Config.LandingStrings[savedLanding ? Config.MoreLandingType.Saved : (Config.MoreLandingType)type]);
// Calculate the animation speed based on the length of the landing text, clamping it between 0.25f and 1f
float speed = UnityEngine.Mathf.Clamp(1f / (landText.Length / 6), Config.MinTextTime.Value, 1f);
// Set the animation speed and update the displayed text with the random landing string
anim.speed = speed;
text.text = landText;
}
else { Informer.Inform("Somehow failed to get the text field", InformType.Error); }
}
else { Informer.Inform("Failed to get animator", InformType.Error); }
}
/// <summary>
/// Returns a random element from the given enumerable.
/// </summary>
/// <typeparam name="T">The type of elements in the enumerable.</typeparam>
/// <param name="enumerable">The enumerable to select from.</param>
/// <returns>A randomly selected element.</returns>
private static T RandomElement<T>(IEnumerable<T> enumerable)
{
IReadOnlyList<T> readOnlyList = (enumerable as IReadOnlyList<T>) ?? enumerable.ToArray();
return readOnlyList[UnityEngine.Random.Range(0, readOnlyList.Count)];
}
}