@@ -12,41 +12,15 @@ namespace SkyboxChanger;
1212
1313public class Helper
1414{
15- public static MemoryFunctionVoid < nint , uint , nint , nint , nint > ? SpawnPrefabEntities_Windows ;
1615
17- // first param should be double, but ccs doesnt support that and it doesnt matter i think
18- public static MemoryFunctionWithReturn < nint , float , nint , uint , nint , nint , nint , nint > ? SpawnPrefabEntities_Linux ;
16+ public static bool IsPlayerSkybox ( int slot , CEnvSky sky )
17+ {
18+ return slot == - 1 || sky . PrivateVScripts == "skyboxchanger_" + slot ;
19+ }
1920
2021 public static void Initialize ( )
2122 {
22- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
23- {
24- SpawnPrefabEntities_Windows = new ( GameData . GetSignature ( "SpawnPrefabEntities" ) ) ;
25- SpawnPrefabEntities_Windows . Hook ( hook =>
26- {
27- string ? prefab = KvLib . GetTargetMapName ( hook . GetParam < nint > ( 4 ) ) ;
28- if ( prefab != null )
29- {
30- SkyboxChanger . GetInstance ( ) . EnvManager . SetMapPrefab ( prefab ) ;
31- }
32- return HookResult . Continue ;
33- } , HookMode . Pre ) ;
34- }
35- else
36- {
37- SpawnPrefabEntities_Linux = new ( GameData . GetSignature ( "SpawnPrefabEntities" ) ) ;
38- SpawnPrefabEntities_Linux . Hook ( hook =>
39- {
40- // mismatch with ida
41- nint ptr = hook . GetParam < nint > ( 5 ) ;
42- string ? prefab = KvLib . GetTargetMapName ( ptr ) ;
43- if ( prefab != null )
44- {
45- SkyboxChanger . GetInstance ( ) . EnvManager . SetMapPrefab ( prefab ) ;
46- }
47- return HookResult . Continue ;
48- } , HookMode . Pre ) ;
49- }
23+
5024 }
5125
5226 public static unsafe IntPtr FindMaterialByPath ( string material )
@@ -74,60 +48,64 @@ public static unsafe IntPtr FindMaterialByPath(string material)
7448 }
7549 return * ( IntPtr * ) materialptr3 ; // CMaterial*** -> CMaterial** (InfoForResourceTypeIMaterial2)
7650 }
77- public static void SpawnSkybox ( int slot , string prefab )
51+
52+ public static unsafe void SpawnSkybox ( int slot , string fogTargetName , string material )
7853 {
79- if ( prefab == null || prefab == "" )
80- {
81- // directly spawn env_sky since prefab is empty so theres no need to create 3d skybox
82- Utilities . FindAllEntitiesByDesignerName < CSkyCamera > ( "sky_camera" ) . ToList ( ) . ForEach ( camera => camera . Remove ( ) ) ;
83- var sky = Utilities . CreateEntityByName < CEnvSky > ( "env_sky" ) ! ;
84- sky . PrivateVScripts = slot . ToString ( ) ;
85- sky . BrightnessScale = 1 ;
86- sky . StartDisabled = false ;
87- sky . Enabled = true ;
88- sky . TintColor = Color . Transparent ;
89- sky . DispatchSpawn ( ) ;
90- if ( SkyboxChanger . GetInstance ( ) . Config . Skyboxs . ContainsKey ( "" ) )
91- {
92- ChangeSkybox ( slot , SkyboxChanger . GetInstance ( ) . Config . Skyboxs [ "" ] ) ;
93- }
94- return ;
95- }
96- IntPtr ptr = Marshal . AllocHGlobal ( 0x30 ) ;
97- for ( int i = 0 ; i < 0x30 ; i ++ )
54+ var skycameras = Utilities . FindAllEntitiesByDesignerName < CSkyCamera > ( "sky_camera" ) ;
55+ uint spawngrouphandle = 0 ;
56+ if ( skycameras . Count ( ) != 0 ) // has 3d skybox
9857 {
99- Marshal . WriteByte ( ptr , i , 0 ) ;
100- }
101- CNetworkOriginCellCoordQuantizedVector vec = new ( ptr ) ;
102- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
103- {
104- SpawnPrefabEntities_Windows ! . Invoke ( 0 , 0 , 0 , ptr , KvLib . MakeKeyValue ( prefab ) ) ;
58+ var skycamera = skycameras . First ( ) ;
59+ spawngrouphandle = * ( uint * ) ( skycamera . Entity ! . Handle + 0x34 ) ;
60+ MemoryManager. CreateLoadingSpawnGroupAndSpawnEntities ( spawngrouphandle , true , true , KvLib . MakeKeyValue ( fogTargetName , "skyboxchanger_" + slot , material ) ) ;
10561 }
10662 else
10763 {
108- // the param shit mismatch with ida
109- SpawnPrefabEntities_Linux ! . Invoke ( 0 , 0 , 0 , 0 , ptr , KvLib . MakeKeyValue ( prefab ) , 0 ) ;
64+ var sky = Utilities . CreateEntityByName < CEnvSky > ( "env_sky" ) ;
65+ sky . PrivateVScripts = "skyboxchanger_" + slot ;
66+ sky . DispatchSpawn ( ) ;
67+ Server . NextFrame ( ( ) =>
68+ {
69+ ChangeSkybox ( slot , null , 1f , Color . White ) ;
70+ } ) ;
11071 }
11172 }
11273
113- public static unsafe bool ChangeSkybox ( int slot , Skybox skybox )
74+ public static unsafe bool ChangeSkybox ( int slot , Skybox ? skybox = null , float ? brightness = null , Color ? color = null )
11475 {
11576 // materialptr2 : CMaterial2** = InfoForResourceTypeIMaterial2
116- var materialptr2 = FindMaterialByPath ( skybox . Material ) ;
117- if ( materialptr2 == 0 )
118- {
119- return false ;
120- }
121- Utilities . FindAllEntitiesByDesignerName < CEnvSky > ( "env_sky" ) . ToList ( ) . ForEach ( sky =>
77+
78+
79+ var sky = Utilities . GetEntityFromIndex < CEnvSky > ( SkyboxChanger . GetInstance ( ) . EnvManager . SpawnedSkyboxes [ slot ] ) ! ;
80+ if ( skybox != null )
12281 {
123- if ( slot == - 1 || sky . PrivateVScripts == slot . ToString ( ) )
82+ var materialptr2 = FindMaterialByPath ( skybox . Material ) ;
83+ if ( materialptr2 == 0 )
12484 {
125- Unsafe . Write ( ( void * ) sky . SkyMaterial . Handle , materialptr2 ) ;
126- Unsafe . Write ( ( void * ) sky . SkyMaterialLightingOnly . Handle , materialptr2 ) ;
127- Utilities . SetStateChanged ( sky , "CEnvSky" , "m_hSkyMaterial" ) ;
128- Utilities . SetStateChanged ( sky , "CEnvSky" , "m_hSkyMaterialLightingOnly" ) ;
85+ return false ;
12986 }
130- } ) ;
87+ Unsafe . Write ( ( void * ) sky . SkyMaterial . Handle , materialptr2 ) ;
88+ Unsafe . Write ( ( void * ) sky . SkyMaterialLightingOnly . Handle , materialptr2 ) ;
89+ Utilities . SetStateChanged ( sky , "CEnvSky" , "m_hSkyMaterial" ) ;
90+ Utilities . SetStateChanged ( sky , "CEnvSky" , "m_hSkyMaterialLightingOnly" ) ;
91+ }
92+
93+ if ( color != null )
94+ {
95+ sky . TintColor = ( Color ) color ;
96+ }
97+ sky . BrightnessScale = brightness ?? skybox ? . Brightness ?? sky . BrightnessScale ;
98+ var colorData = skybox ? . Color ? . Split ( " " ) ;
99+ if ( colorData != null && colorData . Length == 4 )
100+ {
101+ var r = int . Parse ( colorData [ 0 ] ) ;
102+ var g = int . Parse ( colorData [ 1 ] ) ;
103+ var b = int . Parse ( colorData [ 2 ] ) ;
104+ var a = int . Parse ( colorData [ 3 ] ) ;
105+ sky . TintColor = Color . FromArgb ( a , r , g , b ) ;
106+ }
107+ Utilities . SetStateChanged ( sky , "CEnvSky" , "m_vTintColor" ) ;
108+ Utilities . SetStateChanged ( sky , "CEnvSky" , "m_flBrightnessScale" ) ;
131109 return true ;
132110 }
133111
0 commit comments