Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 467f313

Browse files
Rewrote most of the plugin, updated to Spectrum UltraViolet, added randomization and single color features and new configuration
1 parent 99d0dea commit 467f313

File tree

1 file changed

+99
-38
lines changed

1 file changed

+99
-38
lines changed

NamePlugin/NamePlugin.cs

Lines changed: 99 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,143 @@
22
using Spectrum.API;
33
using Spectrum.API.Interfaces.Plugins;
44
using Spectrum.API.Interfaces.Systems;
5+
using Spectrum.API.Configuration;
6+
using System.Collections.Generic;
57

68
namespace NamePlugin {
79
public class Entry : IPlugin {
810
public string FriendlyName => "Name Colorizer";
911
public string Author => "timawesomeness";
10-
public string Contact => "moo@timawesomeness.com";
11-
public APILevel CompatibleAPILevel => APILevel.InfraRed;
12+
public string Contact => "Steam/Discord: timawesomeness";
13+
public APILevel CompatibleAPILevel => APILevel.UltraViolet;
1214

1315
private string username;
14-
private float[] colorData;
1516

16-
private bool alreadySet = false;
17+
private Settings settings = new Settings(typeof(Entry));
1718
private ClientLogic cl;
19+
private Random random = new Random();
1820

1921
public void Initialize(IManager manager) {
20-
FileSystem fs = new FileSystem(typeof(Entry));
21-
try {
22-
LoadColors(fs);
23-
} catch (Exception ex) {
24-
fs.CreateFile("name.txt");
25-
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs.OpenFile("name.txt"));
26-
sw.WriteLine("hue start: 0");
27-
sw.WriteLine("hue end: 360");
28-
sw.WriteLine("saturation: 100");
29-
sw.WriteLine("value: 100");
30-
sw.WriteLine("Instructions: Change hue start and hue end to a hue value between 0 and 360. Change saturation and value to a value between 0 and 100.");
31-
sw.Dispose();
32-
33-
colorData = new float[] { 0f, 1f, 1f, 1f };
22+
// Generate settings if they do not exist
23+
if (!settings.ContainsKey("Randomize Color")) {
24+
Console.WriteLine("Name Colorizer configuration not found. Generating new configuration.");
25+
26+
settings.Add("Randomize Color", false);
27+
28+
settings.Add("Use Single Color", false);
29+
settings.Add("Single Color Hue", 180);
30+
settings.Add("Single Color Saturation", 100);
31+
settings.Add("Single Color Value", 100);
32+
33+
settings.Add("Gradient Hue Start", 0);
34+
settings.Add("Gradient Hue End", 360);
35+
settings.Add("Gradient Saturation", 100);
36+
settings.Add("Gradient Value", 100);
37+
38+
settings.Save();
3439
}
3540

41+
// Get game's ClientLogic and the player's username.
3642
cl = G.Sys.PlayerManager_.GetComponent<ClientLogic>();
3743
username = G.Sys.GameManager_.GetOnlineProfileName(0);
3844

45+
// Update the name to be colored when the chat window is open, not colored otherwise.
3946
Events.ChatWindow.ChatVisibilityChanged.Subscribe(data => {
4047
if (!data.isShowing_) {
41-
cl.GetLocalPlayerInfo().GetType().GetField("username_", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(cl.GetLocalPlayerInfo(), username);
42-
alreadySet = false;
43-
} else if (data.isShowing_) {
48+
SetName(username);
49+
} else {
4450
UpdateName();
4551
}
4652
});
47-
Events.Network.DisconnectedFromServer.Subscribe(data => {
48-
alreadySet = false;
49-
});
53+
54+
// Reload the colors from file
5055
Spectrum.API.Game.Network.Chat.MessageSent += (sender, args) => {
5156
if (args.Author == username && args.Message.Contains("!updatename"))
52-
LoadColors(fs);
57+
settings = new Settings(typeof(Entry));
5358
};
5459
}
5560

56-
private void LoadColors(FileSystem fs) {
57-
System.IO.StreamReader sr = new System.IO.StreamReader(fs.OpenFile("name.txt"));
58-
colorData = new float[] { Convert.ToSingle(sr.ReadLine().Split(new string[] { "hue start: " }, StringSplitOptions.None)[1]) / 360f, Convert.ToSingle(sr.ReadLine().Split(new string[] { "hue end: " }, StringSplitOptions.None)[1]) / 360f, Convert.ToSingle(sr.ReadLine().Split(new string[] { "saturation: " }, StringSplitOptions.None)[1]) / 100, Convert.ToSingle(sr.ReadLine().Split(new string[] { "value: " }, StringSplitOptions.None)[1]) / 100 };
59-
sr.Dispose();
60-
}
61-
61+
/// <summary>
62+
/// Update the player's name to be colored.
63+
/// </summary>
6264
private void UpdateName() {
63-
if (!alreadySet) {
64-
try {
65-
cl.GetLocalPlayerInfo().GetType().GetField("username_", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(cl.GetLocalPlayerInfo(), Colorize(username, colorData[0], colorData[1], colorData[2], colorData[3]));
66-
alreadySet = true;
67-
} catch (Exception ex) {
68-
Console.WriteLine(ex.ToString());
65+
if (settings.GetItem<bool>("Randomize Color")) {
66+
if (settings.GetItem<bool>("Use Single Color")) {
67+
SetName(ColorizeFlat(username, (float)random.NextDouble(), Math.Max((float)random.NextDouble(), .33f), 1f));
68+
} else {
69+
SetName(ColorizeGradient(username, (float)random.NextDouble(), (float)random.NextDouble(), Math.Max((float)random.NextDouble(), .33f), 1f));
70+
}
71+
} else {
72+
if (settings.GetItem<bool>("Use Single Color")) {
73+
SetName(ColorizeFlat(username,
74+
ConvertHueToSingle(settings.GetItem<int>("Single Color Hue")),
75+
ConvertPercentToSingle(settings.GetItem<int>("Single Color Saturation")),
76+
ConvertPercentToSingle(settings.GetItem<int>("Single Color Value"))));
77+
} else {
78+
SetName(ColorizeGradient(username,
79+
ConvertHueToSingle(settings.GetItem<int>("Gradient Hue Start")),
80+
ConvertHueToSingle(settings.GetItem<int>("Gradient Hue End")),
81+
ConvertPercentToSingle(settings.GetItem<int>("Gradient Saturation")),
82+
ConvertPercentToSingle(settings.GetItem<int>("Gradient Value"))));
6983
}
7084
}
7185
}
7286

73-
private string Colorize(string str, float hueStart, float hueEnd, float sat, float val) {
87+
/// <summary>
88+
/// Converts a hue wheel value (0-360) to a float (0-1) because that's what ColorEx uses.
89+
/// </summary>
90+
/// <param name="hue">Hue to convert</param>
91+
/// <returns>Float from 0 to 1 that represents the hue.</returns>
92+
private float ConvertHueToSingle(int hue) {
93+
return Convert.ToSingle(hue / 360f);
94+
}
95+
96+
/// <summary>
97+
/// Converts a percent (0-100) to a float from 0-1 because that's what ColorEx uses.
98+
/// </summary>
99+
/// <param name="percent">Percent to convert</param>
100+
/// <returns>Float from 0 to 1 that represents the hue.</returns>
101+
private float ConvertPercentToSingle(int percent) {
102+
return Convert.ToSingle(percent / 100f);
103+
}
104+
105+
/// <summary>
106+
/// Sets the player's name to a string
107+
/// </summary>
108+
/// <param name="name">String to set name to</param>
109+
private void SetName(string name) {
110+
cl.GetLocalPlayerInfo().GetType().GetField("username_", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(cl.GetLocalPlayerInfo(), name);
111+
}
112+
113+
/// <summary>
114+
/// Colorize a string based on a gradient on the hue wheel.
115+
/// </summary>
116+
/// <param name="str">String to colorize</param>
117+
/// <param name="hueStart">Hue to start at</param>
118+
/// <param name="hueEnd">Hue to end at</param>
119+
/// <param name="sat">Saturation of the color</param>
120+
/// <param name="val">Value of the color</param>
121+
/// <returns>Colorized string</returns>
122+
private string ColorizeGradient(string str, float hueStart, float hueEnd, float sat, float val) {
74123
string newStr = "";
75124
for (int i = 0; i < str.Length; i++) {
76125
newStr += "[" + ColorEx.ColorToHexNGUI(new ColorHSB(((hueEnd - hueStart) / str.Length) * i + hueStart, sat, val, 1f).ToColor()) + "]" + str[i] + "[-]";
77126
}
78127
return newStr;
79128
}
80129

130+
/// <summary>
131+
/// Colorize a string based on a single color on the hue wheel.
132+
/// </summary>
133+
/// <param name="str">String to colorize</param>
134+
/// <param name="hue">Hue of the color</param>
135+
/// <param name="sat">Saturation of the color</param>
136+
/// <param name="val">Value of the color</param>
137+
/// <returns>Colorized string</returns>
138+
private string ColorizeFlat(string str, float hue, float sat, float val) {
139+
return "[" + ColorEx.ColorToHexNGUI(new ColorHSB(hue, sat, val, 1f).ToColor()) + "]" + str + "[-]";
140+
}
141+
81142
public void Shutdown() {
82143

83144
}

0 commit comments

Comments
 (0)