Skip to content

Commit 8aa6292

Browse files
authored
1.0.6
1 parent 46e0bc7 commit 8aa6292

File tree

9 files changed

+1201
-340
lines changed

9 files changed

+1201
-340
lines changed

Config/Configs.cs

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.Localization;
12
using System.Text.Json;
23
using System.Text.Json.Serialization;
34

@@ -7,7 +8,123 @@ public static class Configs
78
{
89
public static class Shared {
910
public static string? CookiesModule { get; set; }
11+
public static IStringLocalizer? StringLocalizer { get; set; }
1012
public static CustomGameData? CustomFunctions { get; set; }
1113
}
14+
15+
private static readonly string ConfigDirectoryName = "config";
16+
private static readonly string ConfigDirectoryName2 = "gamedata";
17+
private static readonly string ConfigFileName = "config.json";
18+
private static readonly string jsonFilePath2 = "Spawn_Loadout_gamedata.json";
19+
private static string? _jsonFilePath2;
20+
private static readonly string jsonFilePath3 = "Weapons_Settings.json";
21+
private static string? _jsonFilePath3;
22+
private static string? _configFilePath;
23+
private static ConfigData? _configData;
24+
25+
private static readonly JsonSerializerOptions SerializationOptions = new()
26+
{
27+
Converters =
28+
{
29+
new JsonStringEnumConverter()
30+
},
31+
WriteIndented = true,
32+
AllowTrailingCommas = true,
33+
ReadCommentHandling = JsonCommentHandling.Skip,
34+
};
35+
36+
public static bool IsLoaded()
37+
{
38+
return _configData is not null;
39+
}
40+
41+
public static ConfigData GetConfigData()
42+
{
43+
if (_configData is null)
44+
{
45+
Helper.DebugMessage($"Config not yet loaded.");
46+
throw new Exception("Config not yet loaded.");
47+
}
48+
49+
return _configData;
50+
}
51+
52+
public static ConfigData Load(string modulePath)
53+
{
54+
var configFileDirectory = Path.Combine(modulePath, ConfigDirectoryName);
55+
if(!Directory.Exists(configFileDirectory))
56+
{
57+
Directory.CreateDirectory(configFileDirectory);
58+
}
59+
60+
var configFileDirectory2 = Path.Combine(modulePath, ConfigDirectoryName2);
61+
if(!Directory.Exists(configFileDirectory2))
62+
{
63+
Directory.CreateDirectory(configFileDirectory2);
64+
}
65+
66+
67+
_jsonFilePath2 = Path.Combine(configFileDirectory2, jsonFilePath2);
68+
Helper.CreateGameData(_jsonFilePath2);
69+
70+
_jsonFilePath3 = Path.Combine(configFileDirectory, jsonFilePath3);
71+
Helper.CreateWeaponsJson(_jsonFilePath3);
72+
73+
_configFilePath = Path.Combine(configFileDirectory, ConfigFileName);
74+
if (File.Exists(_configFilePath))
75+
{
76+
_configData = JsonSerializer.Deserialize<ConfigData>(File.ReadAllText(_configFilePath), SerializationOptions);
77+
}
78+
else
79+
{
80+
_configData = new ConfigData();
81+
}
82+
83+
if (_configData is null)
84+
{
85+
Helper.DebugMessage($"Failed to load configs.");
86+
throw new Exception("Failed to load configs.");
87+
}
88+
89+
SaveConfigData(_configData);
90+
91+
return _configData;
92+
}
93+
94+
private static void SaveConfigData(ConfigData configData)
95+
{
96+
if (_configFilePath is null)
97+
{
98+
Helper.DebugMessage($"Config not yet loaded.");
99+
throw new Exception("Config not yet loaded.");
100+
}
101+
string json = JsonSerializer.Serialize(configData, SerializationOptions);
102+
103+
104+
json = System.Text.RegularExpressions.Regex.Unescape(json);
105+
File.WriteAllText(_configFilePath, json, System.Text.Encoding.UTF8);
106+
}
107+
108+
public class ConfigData
109+
{
110+
public string empty { get; set; }
111+
public string SL_Reload_Weapons_Settings_Flags { get; set; }
112+
public string SL_Reload_Weapons_Settings_CommandsInGame { get; set; }
113+
public string empty1 { get; set; }
114+
public bool EnableDebug { get; set; }
115+
public string empty2 { get; set; }
116+
public string Information_For_You_Dont_Delete_it { get; set; }
117+
118+
public ConfigData()
119+
{
120+
empty = "----------------------------[ ↓ Spawn Loadout Configs ↓ ]---------------------------------";
121+
SL_Reload_Weapons_Settings_Flags = "@css/root,@css/admin";
122+
SL_Reload_Weapons_Settings_CommandsInGame = "!loadouts,!relaodloadouts,!restartloadouts,!loadout,!relaodloadout,!restartloadout";
123+
empty1 = "----------------------------[ ↓ Debug ↓ ]----------------------------------------------";
124+
EnableDebug = false;
125+
empty2 = "----------------------------[ ↓ Info For All Configs Above ↓ ]----------------------------";
126+
Information_For_You_Dont_Delete_it = " Vist [https://github.com/oqyh/cs2-Spawn-Loadout-GoldKingZ/tree/main?tab=readme-ov-file#-configuration-] To Understand All Above";
127+
}
128+
}
12129
}
13-
}
130+
}

Config/CustomGameData.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System.Runtime.InteropServices;
2+
using CounterStrikeSharp.API;
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
5+
using Spawn_Loadout_GoldKingZ.Config;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
8+
9+
namespace Spawn_Loadout_GoldKingZ;
10+
11+
public class CustomGameData
12+
{
13+
public static Dictionary<string, Dictionary<OSPlatform, string>> _customGameData = new();
14+
15+
private readonly MemoryFunctionVoid<IntPtr, string, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr>? GiveNamedItem2;
16+
17+
public CustomGameData()
18+
{
19+
LoadCustomGameDataFromJson();
20+
21+
GiveNamedItem2 = new(GetCustomGameDataKey("GiveNamedItem2"));
22+
}
23+
public void LoadCustomGameDataFromJson()
24+
{
25+
string jsonFilePath = $"{Configs.Shared.CookiesModule}/../../plugins/Spawn-Loadout-GoldKingZ/gamedata/Spawn_Loadout_gamedata.json";
26+
if (!File.Exists(jsonFilePath))
27+
{
28+
Helper.DebugMessage($"JSON file does not exist at path: {jsonFilePath}. Returning without loading custom game data.");
29+
return;
30+
}
31+
32+
try
33+
{
34+
var jsonData = File.ReadAllText(jsonFilePath);
35+
var jsonObject = JObject.Parse(jsonData);
36+
37+
foreach (var item in jsonObject.Properties())
38+
{
39+
string key = item.Name;
40+
var platformData = new Dictionary<OSPlatform, string>();
41+
var signatures = item.Value["signatures"];
42+
if (signatures != null)
43+
{
44+
if (signatures["windows"] != null)
45+
{
46+
platformData[OSPlatform.Windows] = signatures["windows"]!.ToString();
47+
}
48+
if (signatures["linux"] != null)
49+
{
50+
platformData[OSPlatform.Linux] = signatures["linux"]!.ToString();
51+
}
52+
}
53+
54+
_customGameData[key] = platformData;
55+
}
56+
}
57+
catch (Exception ex)
58+
{
59+
Helper.DebugMessage($"Error loading custom game data: {ex.Message}");
60+
}
61+
}
62+
63+
public string GetCustomGameDataKey(string key)
64+
{
65+
if (!_customGameData.TryGetValue(key, out var customGameData))
66+
{
67+
Helper.DebugMessage($"Invalid key {key}. Throwing exception.");
68+
throw new Exception($"Invalid key {key}");
69+
}
70+
71+
OSPlatform platform;
72+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
73+
{
74+
platform = OSPlatform.Linux;
75+
}
76+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
77+
{
78+
platform = OSPlatform.Windows;
79+
}
80+
else
81+
{
82+
Helper.DebugMessage("Unsupported platform. Throwing exception.");
83+
throw new Exception("Unsupported platform");
84+
}
85+
86+
return customGameData.TryGetValue(platform, out var customData)
87+
? customData
88+
: throw new Exception($"Missing custom data for {key} on {platform}");
89+
}
90+
public void PlayerGiveNamedItem(CCSPlayerController player, string item)
91+
{
92+
if (!player.PlayerPawn.IsValid) return;
93+
if (player.PlayerPawn.Value == null) return;
94+
if (!player.PlayerPawn.Value.IsValid) return;
95+
if (player.PlayerPawn.Value.ItemServices == null) return;
96+
97+
if (GiveNamedItem2 is not null)
98+
{
99+
GiveNamedItem2.Invoke(player.PlayerPawn.Value.ItemServices.Handle, item, 0, 0, 0, 0, 0, 0);
100+
}
101+
else
102+
{
103+
player.GiveNamedItem(item);
104+
}
105+
}
106+
}

Config/EventPlayerChat.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using CounterStrikeSharp.API.Core;
2+
using Spawn_Loadout_GoldKingZ.Config;
3+
using CounterStrikeSharp.API.Modules.Commands;
4+
5+
namespace Spawn_Loadout_GoldKingZ;
6+
7+
public class PlayerChat
8+
{
9+
public HookResult OnPlayerChat(CCSPlayerController? player, CommandInfo info, bool TeamChat)
10+
{
11+
if (player == null || !player.IsValid)return HookResult.Continue;
12+
var playerid = player.SteamID;
13+
var eventmessage = info.ArgString;
14+
eventmessage = eventmessage.TrimStart('"');
15+
eventmessage = eventmessage.TrimEnd('"');
16+
17+
if (string.IsNullOrWhiteSpace(eventmessage)) return HookResult.Continue;
18+
string trimmedMessageStart = eventmessage.TrimStart();
19+
string message = trimmedMessageStart.TrimEnd();
20+
21+
string[] SL_Reload_Weapons_Settings_CommandsInGames = Configs.GetConfigData().SL_Reload_Weapons_Settings_CommandsInGame.Split(',');
22+
if (SL_Reload_Weapons_Settings_CommandsInGames.Any(cmd => cmd.Equals(message, StringComparison.OrdinalIgnoreCase)))
23+
{
24+
if (!string.IsNullOrEmpty(Configs.GetConfigData().SL_Reload_Weapons_Settings_Flags) && !Helper.IsPlayerInGroupPermission(player, Configs.GetConfigData().SL_Reload_Weapons_Settings_Flags))
25+
{
26+
Helper.AdvancedPlayerPrintToChat(player, Configs.Shared.StringLocalizer!["PrintChatToPlayer.Not.Allowed.ToReload"]);
27+
return HookResult.Continue;
28+
}
29+
Helper.ClearVariables();
30+
Helper.SetValuesToGlobals();
31+
Helper.AdvancedPlayerPrintToChat(player, Configs.Shared.StringLocalizer!["PrintChatToPlayer.Plugin.Reloaded"]);
32+
}
33+
return HookResult.Continue;
34+
}
35+
}

Config/Globals.cs

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,112 @@
1+
using CounterStrikeSharp.API;
12
using CounterStrikeSharp.API.Core;
3+
using Newtonsoft.Json;
4+
using Spawn_Loadout_GoldKingZ.Config;
25

36
namespace Spawn_Loadout_GoldKingZ;
47

58
public class Globals
69
{
7-
public static Dictionary<ulong, HashSet<string>> loadoutsGivenPerPlayer = new Dictionary<ulong, HashSet<string>>();
8-
public static string SMapName => NativeAPI.GetMapName();
10+
public bool RemoveBuyMenu = false;
11+
public bool RemoveKnife = false;
12+
public bool ForceRemoveServerCommands = false;
13+
public bool ForceRemoveClientCommands = false;
14+
public bool ForceStripPlayers = false;
15+
public int ForceRemoveGroundWeapons = 0;
16+
public float DelayGiveLoadOut = 0.0f;
17+
18+
public class GetPlayerWeapons
19+
{
20+
public CCSPlayerController Player { get; set; }
21+
public CounterStrikeSharp.API.Modules.Timers.Timer Timer{ get; set; }
22+
public CounterStrikeSharp.API.Modules.Timers.Timer KillTheTimer{ get; set; }
23+
24+
public HashSet<string> WeaponsNeeded { get; set; }
25+
public GetPlayerWeapons(CCSPlayerController player, CounterStrikeSharp.API.Modules.Timers.Timer timer, CounterStrikeSharp.API.Modules.Timers.Timer killTheTimer, HashSet<string> weaponsNeeded)
26+
{
27+
Player = player;
28+
Timer = timer;
29+
KillTheTimer = killTheTimer;
30+
WeaponsNeeded = weaponsNeeded;
31+
}
32+
}
33+
public Dictionary<CCSPlayerController, GetPlayerWeapons> PlayerCleanUp = new Dictionary<CCSPlayerController, GetPlayerWeapons>();
34+
public Dictionary<CCSPlayerController, HashSet<string>> loadoutsGivenPerPlayer = new Dictionary<CCSPlayerController, HashSet<string>>();
35+
36+
public Dictionary<string, dynamic> jsonValuesCache = new Dictionary<string, dynamic>();
37+
public Dictionary<string, dynamic> GetJsonValues()
38+
{
39+
if (jsonValuesCache.Count > 0)
40+
{
41+
Helper.DebugMessage("Returning cached jsonValuesCache.");
42+
return jsonValuesCache;
43+
}
44+
45+
string mapname = NativeAPI.GetMapName();
46+
if (string.IsNullOrEmpty(mapname))
47+
{
48+
Helper.DebugMessage("Map Name Is Empty. Can't GetJsonValues.");
49+
return jsonValuesCache;
50+
}
51+
52+
int underscoreIndex = mapname.IndexOf('_');
53+
int nextUnderscoreIndex = mapname.IndexOf('_', underscoreIndex + 1);
54+
55+
string prefix = underscoreIndex != -1 ? mapname.Substring(0, underscoreIndex + 1) : mapname;
56+
string prefix2 = underscoreIndex != -1 && nextUnderscoreIndex != -1 ? mapname.Substring(0, nextUnderscoreIndex + 1) : mapname;
57+
58+
string jsonFilePath = $"{Configs.Shared.CookiesModule}/../../plugins/Spawn-Loadout-GoldKingZ/config/Weapons_Settings.json";
59+
60+
if (!File.Exists(jsonFilePath))
61+
{
62+
Helper.DebugMessage($"JSON file does not exist at path: {jsonFilePath}. Returning cached jsonValuesCache.");
63+
return jsonValuesCache;
64+
}
65+
66+
string jsonContent = File.ReadAllText(jsonFilePath);
67+
if (string.IsNullOrEmpty(jsonContent))
68+
{
69+
Helper.DebugMessage("JSON content is empty. Returning cached jsonValuesCache.");
70+
return jsonValuesCache;
71+
}
72+
73+
dynamic jsonData = JsonConvert.DeserializeObject(jsonContent)!;
74+
dynamic GetMap = null!;
75+
76+
if (jsonData.ContainsKey("ANY"))
77+
{
78+
GetMap = jsonData["ANY"];
79+
}
80+
else if (jsonData.ContainsKey(prefix))
81+
{
82+
GetMap = jsonData[prefix];
83+
}
84+
else if (jsonData.ContainsKey(prefix2))
85+
{
86+
GetMap = jsonData[prefix2];
87+
}
88+
else if (jsonData.ContainsKey(mapname))
89+
{
90+
GetMap = jsonData[mapname];
91+
}
92+
93+
if (GetMap != null)
94+
{
95+
foreach (var item in GetMap)
96+
{
97+
jsonValuesCache[item.Name] = item.Value;
98+
}
99+
}
100+
else
101+
{
102+
Helper.DebugMessage("GetMap is null. Returning cached jsonValuesCache.");
103+
}
104+
105+
return jsonValuesCache;
106+
}
107+
108+
public void ClearJsonValues()
109+
{
110+
jsonValuesCache.Clear();
111+
}
9112
}

0 commit comments

Comments
 (0)