1+ using CounterStrikeSharp . API ;
2+ using CounterStrikeSharp . API . Core ;
3+ using CounterStrikeSharp . API . Core . Attributes . Registration ;
4+ using System . Text . Json . Serialization ;
5+ using CounterStrikeSharp . API . Modules . Timers ;
6+
7+ namespace Map_Configs_Prefix ;
8+
9+ public class MapConfigsPrefixConfig : BasePluginConfig
10+ {
11+ [ JsonPropertyName ( "ForceCvar" ) ] public bool ForceCvar { get ; set ; } = false ;
12+ [ JsonPropertyName ( "EnableErrorLogChecker" ) ] public bool EnableErrorLogChecker { get ; set ; } = false ;
13+ }
14+
15+ public class MapConfigsPrefix : BasePlugin , IPluginConfig < MapConfigsPrefixConfig >
16+ {
17+ public override string ModuleName => "Map Configs Prefix" ;
18+ public override string ModuleVersion => "1.0.0" ;
19+ public override string ModuleAuthor => "Gold KingZ" ;
20+ public override string ModuleDescription => "Map Configs Depend Map Name" ;
21+ public MapConfigsPrefixConfig Config { get ; set ; } = new MapConfigsPrefixConfig ( ) ;
22+ public static string MapName => NativeAPI . GetMapName ( ) ;
23+
24+ private bool onetime ;
25+ private string Tpath = "" ;
26+ private string Date = "" ;
27+ private CounterStrikeSharp . API . Modules . Timers . Timer ? _forcecvar ;
28+ public void OnConfigParsed ( MapConfigsPrefixConfig config )
29+ {
30+ Config = config ;
31+ }
32+
33+ public override void Load ( bool hotReload )
34+ {
35+ string Fpath = Path . Combine ( ModuleDirectory , "../../plugins/Map_Configs_Prefix/ErrorLogs/" ) ;
36+ Date = DateTime . Now . ToString ( "MM-dd-yyyy" ) ;
37+ string fileName = DateTime . Now . ToString ( "MM-dd-yyyy" ) + ".txt" ;
38+ Tpath = Path . Combine ( ModuleDirectory , "../../plugins/Map_Configs_Prefix/ErrorLogs/" ) + $ "{ fileName } ";
39+
40+ if ( Config . EnableErrorLogChecker && ! Directory . Exists ( Fpath ) )
41+ {
42+ Directory . CreateDirectory ( Fpath ) ;
43+ }
44+
45+ if ( Config . EnableErrorLogChecker && ! File . Exists ( Tpath ) )
46+ {
47+ File . Create ( Tpath ) ;
48+ }
49+
50+ onetime = false ;
51+ ExecCommandMap ( ) ;
52+ if ( Config . ForceCvar )
53+ {
54+ _forcecvar ? . Kill ( ) ;
55+ _forcecvar = null ;
56+ _forcecvar = AddTimer ( 0.1f , forcecvarTimer_Callback , TimerFlags . REPEAT | TimerFlags . STOP_ON_MAPCHANGE ) ;
57+ }
58+ }
59+ private void forcecvarTimer_Callback ( )
60+ {
61+ ExecCommandMap ( ) ;
62+ }
63+
64+ [ GameEventHandler ( HookMode . Pre ) ]
65+ public HookResult OnRoundStart ( EventRoundStart @event , GameEventInfo info )
66+ {
67+ if ( Config . ForceCvar )
68+ {
69+ _forcecvar ? . Kill ( ) ;
70+ _forcecvar = null ;
71+ _forcecvar = AddTimer ( 0.1f , forcecvarTimer_Callback , TimerFlags . REPEAT | TimerFlags . STOP_ON_MAPCHANGE ) ;
72+ }
73+ if ( onetime == false )
74+ {
75+ ExecCommandMap ( ) ;
76+ Server . NextFrame ( ( ) =>
77+ {
78+ ExecCommandMap ( ) ;
79+ AddTimer ( 3.0f , ( ) =>
80+ {
81+ ExecCommandMap ( ) ;
82+ onetime = true ;
83+ } ) ;
84+ } ) ;
85+ }
86+ return HookResult . Continue ;
87+ }
88+
89+ private void ExecCommandMap ( )
90+ {
91+ string folderPath = Path . Combine ( ModuleDirectory , "../../plugins/Map_Configs_Prefix" ) ;
92+
93+ int underscoreIndex = MapName . IndexOf ( '_' ) ;
94+ string result = underscoreIndex != - 1 ? MapName . Substring ( 0 , underscoreIndex + 1 ) : MapName ;
95+
96+ for ( int i = 0 ; i < 4 ; i ++ )
97+ {
98+ folderPath = Path . Combine ( folderPath , ".." ) ;
99+ }
100+
101+ string cfgDirectory = Path . Combine ( folderPath , "cfg" ) ;
102+ if ( Directory . Exists ( cfgDirectory ) )
103+ {
104+ string mapsCfgDirectory = Path . Combine ( cfgDirectory , "Map-Configs-Prefix" ) ;
105+ if ( Directory . Exists ( mapsCfgDirectory ) )
106+ {
107+ string [ ] fileNamess = Directory . GetFiles ( mapsCfgDirectory ) ;
108+ bool foundMatch = false ;
109+ foreach ( string fileName in fileNamess )
110+ {
111+ string shortFileName = Path . GetFileNameWithoutExtension ( fileName ) ;
112+ if ( ! string . IsNullOrEmpty ( shortFileName ) &&
113+ ( result . Equals ( shortFileName , StringComparison . OrdinalIgnoreCase ) ||
114+ MapName . Equals ( shortFileName , StringComparison . OrdinalIgnoreCase ) ) )
115+ {
116+ Server . ExecuteCommand ( $ "execifexists Map-Configs-Prefix/{ shortFileName } ") ;
117+ foundMatch = true ;
118+ break ;
119+ }
120+ }
121+
122+ if ( ! foundMatch )
123+ {
124+ if ( Config . EnableErrorLogChecker && File . Exists ( Tpath ) )
125+ {
126+ try
127+ {
128+ File . AppendAllLines ( Tpath , new [ ] { $ "[{ Date } - Couldn't Found Files in csgo/cfg/Map-Configs-Prefix/ That Match Current Map]"} ) ;
129+ } catch
130+ {
131+ File . AppendAllLines ( Tpath , new [ ] { $ "[{ Date } - Please Give Map_Configs_Prefix.dll Permissions To Write.]"} ) ;
132+ }
133+ }
134+
135+ string defaultFileName = "_default_.cfg" ;
136+ string defaultFilePath = Path . Combine ( mapsCfgDirectory , defaultFileName ) ;
137+ if ( File . Exists ( defaultFilePath ) )
138+ {
139+ Server . ExecuteCommand ( $ "execifexists Map-Configs-Prefix/{ defaultFileName } ") ;
140+ } else if ( ! File . Exists ( defaultFilePath ) )
141+ {
142+ if ( Config . EnableErrorLogChecker && File . Exists ( Tpath ) )
143+ {
144+ try
145+ {
146+ File . AppendAllLines ( Tpath , new [ ] { $ "[{ Date } - csgo/cfg/Map-Configs-Prefix/_default_.cfg file does not exist.]"} ) ;
147+ } catch
148+ {
149+ File . AppendAllLines ( Tpath , new [ ] { $ "[{ Date } - Please Give Map_Configs_Prefix.dll Permissions To Write.]"} ) ;
150+ }
151+ }
152+ }
153+ }
154+ }
155+ else
156+ {
157+ if ( Config . EnableErrorLogChecker && File . Exists ( Tpath ) )
158+ {
159+ try
160+ {
161+ File . AppendAllLines ( Tpath , new [ ] { $ "[{ Date } - csgo/cfg/Map-Configs-Prefix/ directory does not exist.]"} ) ;
162+ } catch
163+ {
164+ File . AppendAllLines ( Tpath , new [ ] { $ "[{ Date } - Please Give Map_Configs_Prefix.dll Permissions To Write.]"} ) ;
165+ }
166+ }
167+
168+ }
169+ }
170+ else
171+ {
172+ if ( Config . EnableErrorLogChecker && File . Exists ( Tpath ) )
173+ {
174+ try
175+ {
176+ File . AppendAllLines ( Tpath , new [ ] { $ "[{ Date } - csgo/cfg/ directory does not exist.]"} ) ;
177+ } catch
178+ {
179+ File . AppendAllLines ( Tpath , new [ ] { $ "[{ Date } - Please Give Map_Configs_Prefix.dll Permissions To Write.]"} ) ;
180+ }
181+ }
182+ }
183+ }
184+
185+ }
0 commit comments