-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkinManager.cs
More file actions
233 lines (194 loc) · 7.71 KB
/
SkinManager.cs
File metadata and controls
233 lines (194 loc) · 7.71 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System.Collections.Concurrent;
using Godot;
using MegaCrit.Sts2.Core.Multiplayer.Game;
using Timer = System.Threading.Timer;
namespace MPSkins;
public static class SkinManager
{
// TODO Make these customizable
public static readonly (string Name, float Hue)[] TintSkins =
{
("Default", 0.00f),
("Yellow", 0.12f),
("Green", 0.25f),
("Teal", 0.42f),
("Blue", 0.65f),
("Pink", 0.88f),
};
private static readonly Dictionary<string, List<string>> _charTextureSkins = new();
private static readonly Dictionary<(string charId, string name), string> _skinFilePaths = new();
private static readonly Dictionary<(string charId, string name), Texture2D> _textureCache = new();
private static string? _skinsRoot;
private static FileSystemWatcher? _watcher;
private static Timer? _debounceTimer;
private static readonly ConcurrentDictionary<string, byte> _pendingReloads = new();
private static readonly Dictionary<ulong, string> _playerSkinNames = new();
private static string _resolvedSkinName = "Default";
public static object? CurrentLobby { get; set; }
public static INetGameService? NetService { get; set; }
public static ulong LocalPlayerId { get; set; }
public static string? CurrentCharacterId { get; set; }
public static string LocalSkinName { get; set; } = "Default";
public static string ResolvedSkinName => _resolvedSkinName;
public static string BroadcastSkinName =>
LocalSkinName == "Random" ? "Default" : LocalSkinName;
public static void ResolveLocalSkin(string? characterId)
{
if (LocalSkinName != "Random")
{
_resolvedSkinName = LocalSkinName;
return;
}
var pool = GetAvailableSkins(characterId);
pool.Remove("Random");
_resolvedSkinName = pool.Count > 0 ? pool[(int)GD.RandRange(0, pool.Count - 1)] : "Default";
}
/// <summary>Scan {modDir}/skins/{characterId}/*.png and register all found skins.</summary>
public static void LoadSkinsFromFolder(string modDir)
{
_skinsRoot = Path.Combine(modDir, "skins");
if (!Directory.Exists(_skinsRoot)) return;
string skinsRoot = _skinsRoot;
foreach (string charDir in Directory.GetDirectories(skinsRoot))
{
string characterId = Path.GetFileName(charDir).ToLower();
var names = new List<string>();
foreach (string file in Directory.GetFiles(charDir, "*.png"))
{
string skinName = Path.GetFileNameWithoutExtension(file);
names.Add(skinName);
_skinFilePaths[(characterId, skinName)] = file;
}
if (names.Count > 0)
_charTextureSkins[characterId] = names;
}
}
public static void Reset()
{
_playerSkinNames.Clear();
}
/// <summary>All skin names available for a given character.</summary>
public static List<string> GetAvailableSkins(string? characterId)
{
var list = new List<string>();
list.Add(TintSkins[0].Name);
list.Add("Random");
string? charIdLower = characterId?.ToLower();
_charTextureSkins.TryGetValue(charIdLower ?? "", out var texNames);
if (texNames != null)
list.AddRange(texNames);
foreach (var (name, _) in TintSkins[1..])
list.Add(name);
return list;
}
public static bool IsTintSkin(string skinName)
{
foreach (var (name, _) in TintSkins)
if (name == skinName) return true;
return false;
}
public static float GetHueForSkin(string skinName)
{
foreach (var (name, hue) in TintSkins)
if (name == skinName) return hue;
return 0f;
}
/// <summary>Loads the replacement atlas texture for a texture skin.</summary>
public static Texture2D? GetTextureForSkin(string characterId, string skinName)
{
var key = (characterId.ToLower(), skinName);
if (_textureCache.TryGetValue(key, out var cached)) return cached;
if (!_skinFilePaths.TryGetValue(key, out string? path)) return null;
var image = Image.LoadFromFile(path);
if (image == null) return null;
var texture = ImageTexture.CreateFromImage(image);
_textureCache[key] = texture;
return texture;
}
/// <summary>
/// Watches the skins folder for PNG changes.
/// </summary>
public static void StartFileWatcher()
{
if (_skinsRoot == null || !Directory.Exists(_skinsRoot)) return;
_debounceTimer = new Timer(_ =>
{
var paths = new List<string>(_pendingReloads.Keys);
_pendingReloads.Clear();
Callable.From(() =>
{
foreach (string path in paths)
{
try { ProcessSkinFileChange(path); }
catch { }
}
}).CallDeferred();
}, null, Timeout.Infinite, Timeout.Infinite);
_watcher = new FileSystemWatcher(_skinsRoot, "*.png")
{
IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.CreationTime
};
_watcher.Changed += (_, e) => QueueReload(e.FullPath);
_watcher.Created += (_, e) => QueueReload(e.FullPath);
_watcher.Deleted += (_, e) => QueueReload(e.FullPath);
_watcher.Renamed += (_, e) => { QueueReload(e.OldFullPath); QueueReload(e.FullPath); };
_watcher.EnableRaisingEvents = true;
}
private static void QueueReload(string fullPath)
{
_pendingReloads[fullPath] = 0;
_debounceTimer?.Change(200, Timeout.Infinite);
}
private static void ProcessSkinFileChange(string fullPath)
{
if (_skinsRoot == null) return;
string relative = Path.GetRelativePath(_skinsRoot, fullPath);
string[] parts = relative.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
if (parts.Length != 2) return;
string charId = parts[0].ToLower();
string skinName = Path.GetFileNameWithoutExtension(parts[1]);
var key = (charId, skinName);
if (!File.Exists(fullPath))
{
// Deleted
_skinFilePaths.Remove(key);
if (_charTextureSkins.TryGetValue(charId, out var names))
names.Remove(skinName);
return;
}
var image = Image.LoadFromFile(fullPath);
if (image == null) return;
if (_textureCache.TryGetValue(key, out var existing) && existing is ImageTexture imgTex)
{
imgTex.SetImage(image);
}
else
{
_skinFilePaths[key] = fullPath;
if (!_charTextureSkins.TryGetValue(charId, out var names))
{
names = new List<string>();
_charTextureSkins[charId] = names;
}
if (!names.Contains(skinName))
names.Add(skinName);
_textureCache[key] = ImageTexture.CreateFromImage(image);
}
}
public static void SetPlayerSkinName(ulong playerId, string skinName) =>
_playerSkinNames[playerId] = skinName;
public static string GetPlayerSkinName(ulong playerId) =>
_playerSkinNames.TryGetValue(playerId, out string? name) ? name : "Default";
public static Color GetSkinColor(string skinName)
{
if (skinName == "Random")
return new Color(0.5f, 0.5f, 0.5f, 1f);
if (IsTintSkin(skinName))
{
float hue = GetHueForSkin(skinName);
return hue == 0f ? Colors.White : Color.FromHsv(hue, 0.85f, 1.0f);
}
return new Color(0.7f, 0.7f, 0.7f, 1f);
}
}