|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using KSPBuildTools; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace Shabby; |
| 7 | + |
| 8 | +internal abstract class Prop; |
| 9 | + |
| 10 | +internal class Prop<T>(T value) : Prop |
| 11 | +{ |
| 12 | + internal T Value = value; |
| 13 | + |
| 14 | + public override string ToString() => Value.ToString(); |
| 15 | +} |
| 16 | + |
| 17 | +public sealed class Props(int priority) |
| 18 | +{ |
| 19 | + public readonly int Priority = priority; |
| 20 | + |
| 21 | + private readonly Dictionary<int, Prop> _props = []; |
| 22 | + |
| 23 | + internal bool Dirty = false; |
| 24 | + |
| 25 | + private static uint _idCounter = 0; |
| 26 | + private static uint _nextId() => _idCounter++; |
| 27 | + private readonly uint _uniqueId = _nextId(); |
| 28 | + |
| 29 | + // Note that this is compatible with default object reference equality. |
| 30 | + public static readonly Comparer<Props> PriorityComparer = Comparer<Props>.Create((a, b) => |
| 31 | + { |
| 32 | + var priorityCmp = a.Priority.CompareTo(b.Priority); |
| 33 | + return priorityCmp != 0 ? priorityCmp : a._uniqueId.CompareTo(b._uniqueId); |
| 34 | + }); |
| 35 | + |
| 36 | + internal IEnumerable<int> ManagedIds => _props.Keys; |
| 37 | + |
| 38 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 39 | + private void _internalSet<T>(int id, T value) |
| 40 | + { |
| 41 | + Dirty = true; |
| 42 | + |
| 43 | + MaterialPropertyManager.Instance.LogDebug($"setting {id} to {value}"); |
| 44 | + |
| 45 | + if (!_props.TryGetValue(id, out var prop)) { |
| 46 | + _props[id] = new Prop<T>(value); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + if (prop is not Prop<T> propT) { |
| 51 | + MaterialPropertyManager.Instance.LogWarning( |
| 52 | + $"property {id} has mismatched type; overwriting with {typeof(T).Name}!"); |
| 53 | + _props[id] = new Prop<T>(value); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + propT.Value = value; |
| 58 | + } |
| 59 | + |
| 60 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 61 | + public void SetColor(int id, Color value) => _internalSet<Color>(id, value); |
| 62 | + |
| 63 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 64 | + public void SetFloat(int id, float value) => _internalSet<float>(id, value); |
| 65 | + |
| 66 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 67 | + public void SetInt(int id, int value) => _internalSet<int>(id, value); |
| 68 | + |
| 69 | + public void SetTexture(int id, Texture value) => _internalSet<Texture>(id, value); |
| 70 | + public void SetVector(int id, Vector4 value) => _internalSet<Vector4>(id, value); |
| 71 | + |
| 72 | + private bool _internalHas<T>(int id) => _props.TryGetValue(id, out var prop) && prop is Prop<T>; |
| 73 | + |
| 74 | + public bool HasColor(int id) => _internalHas<Color>(id); |
| 75 | + public bool HasFloat(int id) => _internalHas<float>(id); |
| 76 | + public bool HasInt(int id) => _internalHas<int>(id); |
| 77 | + public bool HasTexture(int id) => _internalHas<Texture>(id); |
| 78 | + public bool HasVector(int id) => _internalHas<Vector4>(id); |
| 79 | + |
| 80 | + internal void Write(int id, MaterialPropertyBlock mpb) |
| 81 | + { |
| 82 | + if (!_props.TryGetValue(id, out var prop)) { |
| 83 | + throw new KeyNotFoundException($"property {id} not found"); |
| 84 | + } |
| 85 | + |
| 86 | + switch (prop) { |
| 87 | + case Prop<Color> c: mpb.SetColor(id, c.Value); break; |
| 88 | + case Prop<float> f: mpb.SetFloat(id, f.Value); break; |
| 89 | + case Prop<int> i: mpb.SetInt(id, i.Value); break; |
| 90 | + case Prop<Texture> t: mpb.SetTexture(id, t.Value); break; |
| 91 | + case Prop<Vector4> v: mpb.SetVector(id, v.Value); break; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public override string ToString() |
| 96 | + { |
| 97 | + var sb = StringBuilderCache.Acquire(); |
| 98 | + sb.AppendFormat("(Priority {0}) {{\n", Priority); |
| 99 | + foreach (var (id, prop) in _props) { |
| 100 | + sb.AppendFormat("{0} = {1}\n", id, prop); |
| 101 | + } |
| 102 | + |
| 103 | + sb.AppendLine("}"); |
| 104 | + return sb.ToStringAndRelease(); |
| 105 | + } |
| 106 | +} |
0 commit comments