|
2 | 2 | using Spectrum.API;
|
3 | 3 | using Spectrum.API.Interfaces.Plugins;
|
4 | 4 | using Spectrum.API.Interfaces.Systems;
|
| 5 | +using Spectrum.API.Configuration; |
| 6 | +using System.Collections.Generic; |
5 | 7 |
|
6 | 8 | namespace NamePlugin {
|
7 | 9 | public class Entry : IPlugin {
|
8 | 10 | public string FriendlyName => "Name Colorizer";
|
9 | 11 | 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; |
12 | 14 |
|
13 | 15 | private string username;
|
14 |
| - private float[] colorData; |
15 | 16 |
|
16 |
| - private bool alreadySet = false; |
| 17 | + private Settings settings = new Settings(typeof(Entry)); |
17 | 18 | private ClientLogic cl;
|
| 19 | + private Random random = new Random(); |
18 | 20 |
|
19 | 21 | 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(); |
34 | 39 | }
|
35 | 40 |
|
| 41 | + // Get game's ClientLogic and the player's username. |
36 | 42 | cl = G.Sys.PlayerManager_.GetComponent<ClientLogic>();
|
37 | 43 | username = G.Sys.GameManager_.GetOnlineProfileName(0);
|
38 | 44 |
|
| 45 | + // Update the name to be colored when the chat window is open, not colored otherwise. |
39 | 46 | Events.ChatWindow.ChatVisibilityChanged.Subscribe(data => {
|
40 | 47 | 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 { |
44 | 50 | UpdateName();
|
45 | 51 | }
|
46 | 52 | });
|
47 |
| - Events.Network.DisconnectedFromServer.Subscribe(data => { |
48 |
| - alreadySet = false; |
49 |
| - }); |
| 53 | + |
| 54 | + // Reload the colors from file |
50 | 55 | Spectrum.API.Game.Network.Chat.MessageSent += (sender, args) => {
|
51 | 56 | if (args.Author == username && args.Message.Contains("!updatename"))
|
52 |
| - LoadColors(fs); |
| 57 | + settings = new Settings(typeof(Entry)); |
53 | 58 | };
|
54 | 59 | }
|
55 | 60 |
|
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> |
62 | 64 | 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")))); |
69 | 83 | }
|
70 | 84 | }
|
71 | 85 | }
|
72 | 86 |
|
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) { |
74 | 123 | string newStr = "";
|
75 | 124 | for (int i = 0; i < str.Length; i++) {
|
76 | 125 | newStr += "[" + ColorEx.ColorToHexNGUI(new ColorHSB(((hueEnd - hueStart) / str.Length) * i + hueStart, sat, val, 1f).ToColor()) + "]" + str[i] + "[-]";
|
77 | 126 | }
|
78 | 127 | return newStr;
|
79 | 128 | }
|
80 | 129 |
|
| 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 | + |
81 | 142 | public void Shutdown() {
|
82 | 143 |
|
83 | 144 | }
|
|
0 commit comments