-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.cs
189 lines (174 loc) · 5.34 KB
/
Helper.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
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
namespace SkyboxChanger;
public class Helper
{
public static bool IsPlayerSkybox(int slot, CEnvSky sky)
{
return slot == -1 || sky.PrivateVScripts == "skyboxchanger_" + slot;
}
public static void Initialize()
{
}
public static unsafe IntPtr FindMaterialByPath(string material)
{
if (material.EndsWith("_c"))
{
material = material.Substring(0, material.Length - 2);
}
IntPtr pIMaterialSystem2 = NativeAPI.GetValveInterface(0, "VMaterialSystem2_001");
var FindOrCreateFromResource = VirtualFunction.Create<IntPtr, IntPtr, string, IntPtr>(pIMaterialSystem2, 14);
IntPtr outMaterial = 0;
IntPtr pOutMaterial = (nint)(&outMaterial);
IntPtr materialptr3;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
materialptr3 = FindOrCreateFromResource.Invoke(pIMaterialSystem2, pOutMaterial, material);
}
else
{
materialptr3 = FindOrCreateFromResource.Invoke(pOutMaterial, 0, material);
}
if (materialptr3 == 0)
{
return 0;
}
return *(IntPtr*)materialptr3; // CMaterial*** -> CMaterial** (InfoForResourceTypeIMaterial2)
}
public static unsafe void SpawnSkybox(int slot, string fogTargetName, string material)
{
var skycameras = Utilities.FindAllEntitiesByDesignerName<CSkyCamera>("sky_camera");
uint spawngrouphandle = 0;
if (skycameras.Count() != 0) // has 3d skybox
{
var skycamera = skycameras.First();
spawngrouphandle = *(uint*)(skycamera.Entity!.Handle + 0x34);
MemoryManager.CreateLoadingSpawnGroupAndSpawnEntities(spawngrouphandle, true, true, KvLib.MakeKeyValue(fogTargetName, "skyboxchanger_" + slot, material));
}
else
{
var sky = Utilities.CreateEntityByName<CEnvSky>("env_sky");
sky.PrivateVScripts = "skyboxchanger_" + slot;
sky.DispatchSpawn();
Server.NextFrame(() =>
{
ChangeSkybox(slot, null, 1f, Color.White);
});
}
}
public static unsafe bool ChangeSkybox(int slot, Skybox? skybox = null, float? brightness = null, Color? color = null)
{
// materialptr2 : CMaterial2** = InfoForResourceTypeIMaterial2
var sky = Utilities.GetEntityFromIndex<CEnvSky>(SkyboxChanger.GetInstance().EnvManager.SpawnedSkyboxes[slot])!;
if (skybox != null)
{
var materialptr2 = FindMaterialByPath(skybox.Material);
if (materialptr2 == 0)
{
return false;
}
Unsafe.Write((void*)sky.SkyMaterial.Handle, materialptr2);
Unsafe.Write((void*)sky.SkyMaterialLightingOnly.Handle, materialptr2);
Utilities.SetStateChanged(sky, "CEnvSky", "m_hSkyMaterial");
Utilities.SetStateChanged(sky, "CEnvSky", "m_hSkyMaterialLightingOnly");
}
if (color != null)
{
sky.TintColor = (Color)color;
}
sky.BrightnessScale = brightness ?? skybox?.Brightness ?? sky.BrightnessScale;
var colorData = skybox?.Color?.Split(" ");
if (colorData != null && colorData.Length == 4)
{
var r = int.Parse(colorData[0]);
var g = int.Parse(colorData[1]);
var b = int.Parse(colorData[2]);
var a = int.Parse(colorData[3]);
sky.TintColor = Color.FromArgb(a, r, g, b);
}
Utilities.SetStateChanged(sky, "CEnvSky", "m_vTintColor");
Utilities.SetStateChanged(sky, "CEnvSky", "m_flBrightnessScale");
return true;
}
public static bool PlayerHasPermission(CCSPlayerController player, string[]? permissions, string[]? permissionsOr)
{
if (permissions != null)
{
foreach (string perm in permissions)
{
if (perm.StartsWith("@"))
{
if (!AdminManager.PlayerHasPermissions(player, [perm]))
{
return false;
}
}
else if (perm.StartsWith("#"))
{
if (!AdminManager.PlayerInGroup(player, [perm]))
{
return false;
}
}
else
{
ulong steamId;
if (!ulong.TryParse(perm, out steamId))
{
throw new FormatException($"Unknown SteamID64 format: {perm}");
}
else
{
if (player.SteamID != steamId)
{
return false;
}
}
}
}
}
if (permissionsOr != null)
{
foreach (string perm in permissionsOr)
{
if (perm.StartsWith("@"))
{
if (AdminManager.PlayerHasPermissions(player, perm))
{
return true;
}
}
else if (perm.StartsWith("#"))
{
if (AdminManager.PlayerInGroup(player, perm))
{
return true;
}
}
else
{
ulong steamId;
if (!ulong.TryParse(perm, out steamId))
{
throw new FormatException($"Unknown SteamID64 format: {perm}");
}
else
{
if (player.SteamID == steamId)
{
return true;
}
}
}
}
}
return permissionsOr == null || permissionsOr.Length == 0;
}
}