-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPlayerModelChanger.cs
341 lines (305 loc) · 11.7 KB
/
PlayerModelChanger.cs
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Utils;
using System.Drawing;
using CounterStrikeSharp.API.Modules.Config;
using Microsoft.Extensions.Logging;
using CounterStrikeSharp.API.Modules.Memory;
using System.Runtime.InteropServices;
namespace PlayerModelChanger;
public partial class PlayerModelChanger : BasePlugin, IPluginConfig<ModelConfig>
{
public override string ModuleName => "Player Model Changer";
public override string ModuleVersion => "1.8.5";
public override string ModuleAuthor => "samyyc";
public required ModelConfig Config { get; set; }
public required ModelService Service { get; set; }
public required DefaultModelManager DefaultModelManager { get; set; }
public required ModelMenuManager MenuManager { get; set; } = new();
private static PlayerModelChanger? _Instance { get; set; }
public bool Enable = true;
public override void Load(bool hotReload)
{
_Instance = this;
IStorage? Storage = null;
switch (Config.StorageType)
{
case "sqlite":
Storage = new SqliteStorage(ModuleDirectory);
break;
case "mysql":
Storage = new MySQLStorage(Config.MySQLIP, Config.MySQLPort, Config.MySQLUser, Config.MySQLPassword, Config.MySQLDatabase, Config.MySQLTable);
break;
};
if (Storage == null)
{
throw new Exception("[PlayerModelChanger] Failed to initialize storage. Please check your config");
}
DefaultModelManager = new DefaultModelManager();
this.Service = new ModelService(Config, Storage, Localizer, DefaultModelManager);
DefaultModelManager.ReloadConfig(ModuleDirectory, Service);
if (!Config.DisablePrecache)
{
RegisterListener<Listeners.OnServerPrecacheResources>(PrecacheResource);
}
RegisterEventHandler<EventPlayerSpawn>(OnPlayerSpawnEvent);
RegisterListener<Listeners.OnTick>(OnTick);
RegisterListener<Listeners.OnMapEnd>(() =>
{
Service.ClearMapDefaultModel();
});
Utils.InitializeLangPrefix();
RegisterEventHandler<EventPlayerActivate>((@event, info) =>
{
if (@event.Userid != null)
{
MenuManager.RemovePlayer(@event.Userid.Slot);
MenuManager.AddPlayer(@event.Userid.Slot, new ModelMenuPlayer { Player = @event.Userid, Buttons = 0 });
}
return HookResult.Continue;
});
RegisterEventHandler<EventPlayerDisconnect>((@event, info) =>
{
if (@event.Userid != null)
{
MenuManager.RemovePlayer(@event.Userid.Slot);
}
return HookResult.Continue;
});
if (hotReload)
{
MenuManager.ReloadPlayer();
}
Logger.LogInformation($"Loaded {Service.GetModelCount()} model(s) successfully.");
}
private void PrecacheResource(ResourceManifest manifest)
{
foreach (var model in Service.GetAllModels())
{
Logger.LogInformation($"Precaching {model.Path}");
manifest.AddResource(model.Path);
}
}
public override void Unload(bool hotReload)
{
_Instance = null;
RemoveListener<Listeners.OnServerPrecacheResources>(PrecacheResource);
RemoveListener<Listeners.OnTick>(OnTick);
DeregisterEventHandler<EventPlayerSpawn>(OnPlayerSpawnEvent);
Logger.LogInformation("Unloaded successfully.");
}
public static PlayerModelChanger getInstance()
{
return _Instance!;
}
public void ReloadConfig()
{
var config = typeof(ConfigManager)
.GetMethod("Load")!
.MakeGenericMethod(typeof(ModelConfig))
.Invoke(null, new object[] { Path.GetFileName(ModuleDirectory) }) as IBasePluginConfig;
OnConfigParsed((config as ModelConfig)!);
Unload(true);
Load(false);
Service.ReloadConfig(ModuleDirectory, Config);
}
public void OnConfigParsed(ModelConfig config)
{
var availableStorageType = new[] { "sqlite", "mysql" };
if (!availableStorageType.Contains(config.StorageType))
{
throw new Exception($"[PlayerModelChanger] Unknown storage type: {Config.StorageType}, available types: {string.Join(",", availableStorageType)}");
}
if (config.StorageType == "mysql")
{
if (config.MySQLIP == "")
{
throw new Exception("[PlayerModelChanger] You must fill in the MySQL_IP");
}
if (config.MySQLPort == "")
{
throw new Exception("[PlayerModelChanger] You must fill in the MYSQL_Port");
}
if (config.MySQLUser == "")
{
throw new Exception("[PlayerModelChanger] You must fill in the MYSQL_User");
}
if (config.MySQLPassword == "")
{
throw new Exception("[PlayerModelChanger] You must fill in the MYSQL_Password");
}
if (config.MySQLDatabase == "")
{
throw new Exception("[PlayerModelChanger] You must fill in the MySQL_Database");
}
}
if (config.ModelForBots == null)
{
config.ModelForBots = new BotsConfig();
}
for (int i = 0; i < config.Models.Count; i++)
{
var entry = config.Models.ElementAt(i);
ModelService.InitializeModel(entry.Key, entry.Value);
if (config.Models.Where(m => m.Value.Name == entry.Value.Name).Count() > 1)
{
throw new Exception($"[PlayerModelChanger] Found duplicated model name: {entry.Value.Name}");
}
}
Config = config;
}
public void OnTick()
{
Inspection.UpdateCamera();
MenuManager.Update();
}
// from https://github.com/Challengermode/cm-cs2-defaultskins/
[GameEventHandler]
public HookResult OnPlayerSpawnEvent(EventPlayerSpawn @event, GameEventInfo info)
{
if (!Enable)
{
return HookResult.Continue;
}
if (@event == null)
{
return HookResult.Continue;
}
CCSPlayerController? player = @event.Userid;
if (player == null
|| !player.IsValid)
{
return HookResult.Continue;
}
try
{
CsTeam team = (CsTeam)player.TeamNum;
if (team != CsTeam.Terrorist && team != CsTeam.CounterTerrorist)
{
return HookResult.Continue;
}
if (player.IsBot)
{
List<string> modelindexs = team == CsTeam.Terrorist ? Config.ModelForBots.T : Config.ModelForBots.CT;
if (modelindexs.Count() == 0)
{
return HookResult.Continue;
}
var modelindex = modelindexs[Random.Shared.Next(modelindexs.Count)];
var botmodel = Service.GetModel(modelindex);
if (modelindex == "@random")
{
botmodel = Service.GetRandomModel(player, team == CsTeam.Terrorist ? Side.T : Side.CT);
}
if (botmodel != null)
{
AddTimer(0.03f, () =>
{
SetModelNextServerFrame(player, botmodel, botmodel.Disableleg);
});
}
else
{
Server.NextFrame(() =>
{
var originalRender = player.Pawn.Value!.Render;
player.Pawn.Value.Render = Color.FromArgb(255, originalRender.R, originalRender.G, originalRender.B);
});
}
return HookResult.Continue;
}
if (player.AuthorizedSteamID == null)
{
return HookResult.Continue;
}
if (
player.PlayerPawn == null
|| !player.PlayerPawn.IsValid
|| player.PlayerPawn.Value == null
|| !player.PlayerPawn.Value.IsValid
)
{
return HookResult.Continue;
}
if (Config.AutoResyncCache)
{
Service.ResyncCache();
}
if (!Config.DisableAutoCheck)
{
var result = Service.CheckAndReplaceModel(player);
if (result.Item1 && result.Item2)
{
player.PrintToChat(Localizer["model.invalidreseted", Localizer["side.all"]]);
}
else if (result.Item1)
{
player.PrintToChat(Localizer["model.invalidreseted", Localizer["side.t"]]);
}
else if (result.Item2)
{
player.PrintToChat(Localizer["model.invalidreseted", Localizer["side.ct"]]);
}
}
AddTimer(0.03f, () =>
{
Server.NextFrame(() =>
{
if (!Service.MapDefaultModelInitialized(player))
{
Service.SetMapDefaultModel(player, player.PlayerPawn.Value.CBodyComponent!.SceneNode!.GetSkeletonInstance().ModelState.ModelName);
}
Server.NextFrame(() =>
{
var model = Service.GetPlayerNowTeamModel(player);
if (model != null)
{
SetModelNextServerFrame(player, model, model.Disableleg);
}
else
{
var originalRender = player.PlayerPawn.Value.Render;
player.PlayerPawn.Value.Render = Color.FromArgb(255, originalRender.R, originalRender.G, originalRender.B);
}
});
});
});
}
catch (Exception ex)
{
Logger.LogInformation("Could not set player model: {0}", ex);
}
return HookResult.Continue;
}
public Task SetModelNextServerFrame(CCSPlayerController player, Model? model, bool disableleg)
{
return Server.NextFrameAsync(() =>
{
var pawn = player.Pawn.Value!;
if (model == null)
{
var defaultModel = Service.GetMapDefaultModel(player);
if (defaultModel != null)
{
pawn.SetModel(defaultModel);
}
return;
}
pawn.SetModel(model.Path);
var originalRender = pawn.Render;
pawn.Render = Color.FromArgb(disableleg ? 254 : 255, originalRender.R, originalRender.G, originalRender.B);
ulong meshgroupmask = pawn.CBodyComponent.SceneNode.GetSkeletonInstance().ModelState.MeshGroupMask;
if (Service.InitMeshgroupPreference(player, model, meshgroupmask))
{
return;
}
meshgroupmask = Utils.CalculateMeshgroupmask(Service.GetMeshgroupPreference(player, model).ToArray(), model.FixedMeshgroups);
if (meshgroupmask != 0)
{
pawn.CBodyComponent.SceneNode.GetSkeletonInstance().ModelState.MeshGroupMask = meshgroupmask;
Utilities.SetStateChanged(pawn, "CBaseEntity", "m_CBodyComponent");
}
});
}
}