|
| 1 | +using Celeste.Mod; |
| 2 | +using Microsoft.Xna.Framework; |
| 3 | +using MonoMod.Detour; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Celeste.Mod.Rainbow { |
| 12 | + public class RainbowModule : EverestModule { |
| 13 | + |
| 14 | + public static RainbowModule Instance; |
| 15 | + |
| 16 | + public override Type SettingsType => typeof(RainbowModuleSettings); |
| 17 | + public static RainbowModuleSettings Settings => (RainbowModuleSettings) Instance._Settings; |
| 18 | + |
| 19 | + // The methods we want to hook. |
| 20 | + private readonly static MethodInfo m_GetHairColor = typeof(PlayerHair).GetMethod("GetHairColor", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); |
| 21 | + private readonly static MethodInfo m_GetTrailColor = typeof(Player).GetMethod("GetTrailColor", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); |
| 22 | + |
| 23 | + private static int trailIndex = 0; |
| 24 | + |
| 25 | + public RainbowModule() { |
| 26 | + Instance = this; |
| 27 | + } |
| 28 | + |
| 29 | + public override void Load() { |
| 30 | + // Runtime hooks are quite different from static patches. |
| 31 | + Type t_RainbowModule = GetType(); |
| 32 | + // [trampoline] = [method we want to hook] .Detour< [signature] >( [replacement method] ); |
| 33 | + orig_GetHairColor = m_GetHairColor.Detour<d_GetHairColor>(t_RainbowModule.GetMethod("GetHairColor")); |
| 34 | + orig_GetTrailColor = m_GetTrailColor.Detour<d_GetTrailColor>(t_RainbowModule.GetMethod("GetTrailColor")); |
| 35 | + } |
| 36 | + |
| 37 | + public override void Unload() { |
| 38 | + // Let's just hope that nothing else detoured this, as this is depth-based... |
| 39 | + RuntimeDetour.Undetour(m_GetHairColor); |
| 40 | + RuntimeDetour.Undetour(m_GetTrailColor); |
| 41 | + } |
| 42 | + |
| 43 | + // The delegate tells MonoMod.Detour / RuntimeDetour about the method signature. |
| 44 | + // Instance (non-static) methods must become static, which means we add "this" as the first argument. |
| 45 | + public delegate Color d_GetHairColor(PlayerHair self, int index); |
| 46 | + // A field containing the trampoline to the original method. |
| 47 | + // You don't need to care about how RuntimeDetour handles this behind the scenes. |
| 48 | + public static d_GetHairColor orig_GetHairColor; |
| 49 | + public static Color GetHairColor(PlayerHair self, int index) { |
| 50 | + Color colorOrig = orig_GetHairColor(self, index); |
| 51 | + if (!Settings.Enabled || self.GetSprite().Mode == PlayerSpriteMode.Badeline) |
| 52 | + return colorOrig; |
| 53 | + |
| 54 | + float wave = self.GetWave() * 60f; |
| 55 | + wave *= Settings.SpeedFactor; |
| 56 | + Color colorRainbow = ColorFromHSV((index / (float) self.GetSprite().HairCount) * 180f + wave, 0.6f, 0.6f); |
| 57 | + return new Color( |
| 58 | + (colorOrig.R / 255f) * 0.3f + (colorRainbow.R / 255f) * 0.7f, |
| 59 | + (colorOrig.G / 255f) * 0.3f + (colorRainbow.G / 255f) * 0.7f, |
| 60 | + (colorOrig.B / 255f) * 0.3f + (colorRainbow.B / 255f) * 0.7f, |
| 61 | + colorOrig.A |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public delegate Color d_GetTrailColor(Player self, bool wasDashB); |
| 66 | + public static d_GetTrailColor orig_GetTrailColor; |
| 67 | + public static Color GetTrailColor(Player self, bool wasDashB) { |
| 68 | + if (!Settings.Enabled || self.Sprite.Mode == PlayerSpriteMode.Badeline || self.Hair == null) |
| 69 | + return orig_GetTrailColor(self, wasDashB); |
| 70 | + |
| 71 | + return self.Hair.GetHairColor((trailIndex++) % self.Hair.GetSprite().HairCount); |
| 72 | + } |
| 73 | + |
| 74 | + // Conversion algorithms found randomly on the net - best source for HSV <-> RGB ever:tm: |
| 75 | + |
| 76 | + private static void ColorToHSV(Color c, out float h, out float s, out float v) { |
| 77 | + float r = c.R / 255f; |
| 78 | + float g = c.G / 255f; |
| 79 | + float b = c.B / 255f; |
| 80 | + float min, max, delta; |
| 81 | + min = Math.Min(Math.Min(r, g), b); |
| 82 | + max = Math.Max(Math.Max(r, g), b); |
| 83 | + v = max; |
| 84 | + delta = max - min; |
| 85 | + if (max != 0) { |
| 86 | + s = delta / max; |
| 87 | + |
| 88 | + if (r == max) |
| 89 | + h = (g - b) / delta; |
| 90 | + else if (g == max) |
| 91 | + h = 2 + (b - r) / delta; |
| 92 | + else |
| 93 | + h = 4 + (r - g) / delta; |
| 94 | + h *= 60f; |
| 95 | + if (h < 0) |
| 96 | + h += 360f; |
| 97 | + } else { |
| 98 | + s = 0f; |
| 99 | + h = 0f; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static Color ColorFromHSV(float hue, float saturation, float value) { |
| 104 | + int hi = (int) (Math.Floor(hue / 60f)) % 6; |
| 105 | + float f = hue / 60f - (float) Math.Floor(hue / 60f); |
| 106 | + |
| 107 | + value = value * 255; |
| 108 | + int v = (int) (value); |
| 109 | + int p = (int) (value * (1 - saturation)); |
| 110 | + int q = (int) (value * (1 - f * saturation)); |
| 111 | + int t = (int) (value * (1 - (1 - f) * saturation)); |
| 112 | + |
| 113 | + if (hi == 0) |
| 114 | + return new Color(255, v, t, p); |
| 115 | + else if (hi == 1) |
| 116 | + return new Color(255, q, v, p); |
| 117 | + else if (hi == 2) |
| 118 | + return new Color(255, p, v, t); |
| 119 | + else if (hi == 3) |
| 120 | + return new Color(255, p, q, v); |
| 121 | + else if (hi == 4) |
| 122 | + return new Color(255, t, p, v); |
| 123 | + else |
| 124 | + return new Color(255, v, p, q); |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | +} |
0 commit comments