Skip to content

Commit

Permalink
Update v1.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
NockyCZ committed Oct 20, 2024
1 parent e38e18e commit bda55bd
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 325 deletions.
10 changes: 5 additions & 5 deletions source/Deathmatch/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ public void RegisterNewPreference(PreferencesData preferencesData)

public void StartCustomMode(int modeId)
{
if (!CustomModes.ContainsKey(modeId.ToString()))
if (!Config.CustomModes.ContainsKey(modeId.ToString()))
throw new Exception($"A Custom mode with ID '{modeId}' cannot be started, because this mode does not exist!");

SetupCustomMode(modeId.ToString());
}

public void ChangeNextMode(int modeId)
{
if (!CustomModes.ContainsKey(modeId.ToString()))
if (!Config.CustomModes.ContainsKey(modeId.ToString()))
throw new Exception($"A Custom mode with ID '{modeId}' cannot be set as next mode, because this mode does not exist!");

NextMode = modeId;
}

public void AddCustomMode(int modeId, ModeData mode)
{
if (CustomModes.ContainsKey(modeId.ToString()))
if (Config.CustomModes.ContainsKey(modeId.ToString()))
throw new Exception($"A Custom mode with ID '{modeId}' cannot be added, because this mode already exists!");

CustomModes.Add(modeId.ToString(), mode);
Config.CustomModes.Add(modeId.ToString(), mode);
}

public void ChangeCheckDistance(int distance)
Expand Down Expand Up @@ -88,6 +88,6 @@ public int GetActiveModeRemainingTime()

public Dictionary<string, ModeData> GetCustomModes()
{
return CustomModes;
return Config.CustomModes;
}
}
2 changes: 0 additions & 2 deletions source/Deathmatch/Common/Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public class DeathmatchPlayerData
public Dictionary<string, string> PrimaryWeapon { get; set; } = new();
public Dictionary<string, string> SecondaryWeapon { get; set; } = new();
public Dictionary<string, bool> Preferences { get; set; } = new();
public string LastPrimaryWeapon { get; set; } = "";
public string LastSecondaryWeapon { get; set; } = "";
public bool SpawnProtection { get; set; } = false;
public int KillStreak { get; set; } = 0;
public float BlockRandomWeaponsIntegeration { get; set; } = 0;
Expand Down
8 changes: 4 additions & 4 deletions source/Deathmatch/Common/Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ namespace Deathmatch
{
public partial class Deathmatch
{
public static Dictionary<string, ModeData> CustomModes = new();
public static Dictionary<string, Dictionary<string, Dictionary<RestrictType, RestrictData>>> RestrictedWeapons = new();
//public static Dictionary<string, ModeData> CustomModes = new();
//public static Dictionary<string, Dictionary<string, Dictionary<RestrictType, RestrictData>>> RestrictedWeapons = new();
public static Dictionary<Vector, QAngle> spawnPositionsCT = new();
public static Dictionary<Vector, QAngle> spawnPositionsT = new();
public static Dictionary<int, Vector> blockedSpawns = new();
public static PlayerCache<DeathmatchPlayerData> playerData = new PlayerCache<DeathmatchPlayerData>();
public static PlayerCache<DeathmatchPlayerData> playerData = new();
public static List<PreferencesData> Preferences = new();
public static List<CDynamicProp> savedSpawnsModel = new();
public static List<CPointWorldText> savedSpawnsVectorText = new();

readonly Dictionary<string, string> weaponSelectMapping = new Dictionary<string, string>
readonly Dictionary<string, string> weaponSelectMapping = new()
{
{ "m4a4", "weapon_m4a1" },
{ "weapon_m4a1", "weapon_m4a1" },
Expand Down
21 changes: 0 additions & 21 deletions source/Deathmatch/Common/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,5 @@ public enum RestrictType
VIP,
NonVIP,
}

public enum AcquireResult : int
{
Allowed = 0,
InvalidItem,
AlreadyOwned,
AlreadyPurchased,
ReachedGrenadeTypeLimit,
ReachedGrenadeTotalLimit,
NotAllowedByTeam,
NotAllowedByMap,
NotAllowedByMode,
NotAllowedForPurchase,
NotAllowedByProhibition,
};

public enum AcquireMethod : int
{
PickUp = 0,
Buy,
};
}
}
3 changes: 0 additions & 3 deletions source/Deathmatch/Common/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public partial class Deathmatch
public static int CheckedEnemiesDistance = 500;
public static bool IsCasualGamemode;
public static bool DefaultMapSpawnDisabled = false;
public static bool IsLinuxServer;
public static ModeData ActiveMode = new();
public MemoryFunctionWithReturn<CCSPlayer_ItemServices, CEconItemView, AcquireMethod, NativeObject, AcquireResult>? CCSPlayer_CanAcquireFunc;
public MemoryFunctionWithReturn<int, string, CCSWeaponBaseVData>? GetCSWeaponDataFromKeyFunc;
}
}
17 changes: 11 additions & 6 deletions source/Deathmatch/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class DeathmatchConfig : BasePluginConfig
[JsonPropertyName("Players Gameplay Settings")] public PlayersSettings PlayersSettings { get; set; } = new PlayersSettings();
[JsonPropertyName("Client Preferences")] public PlayersPreferences PlayersPreferences { get; set; } = new PlayersPreferences();
[JsonPropertyName("Custom Modes")]
public Dictionary<string, ModeData> Modes { get; set; } = new()
public Dictionary<string, ModeData> CustomModes { get; set; } = new()
{
["0"] = new ModeData
{
Expand All @@ -31,11 +31,16 @@ public class DeathmatchConfig : BasePluginConfig
"weapon_aug", "weapon_sg556", "weapon_xm1014",
"weapon_ak47", "weapon_famas", "weapon_galilar",
"weapon_m4a1", "weapon_m4a1_silencer", "weapon_mp5sd",
"weapon_mp7", "weapon_p90", "weapon_awp"
"weapon_mp7", "weapon_p90", "weapon_awp",
"weapon_ssg08", "weapon_scar20", "weapon_g3sg1",
"weapon_m249", "weapon_negev", "weapon_nova",
"weapon_sawedoff", "weapon_mag7", "weapon_ump45",
"weapon_bizon", "weapon_mac10", "weapon_mp9"
},
SecondaryWeapons = new List<string> {
SecondaryWeapons = new List<string> {
"weapon_usp_silencer", "weapon_p250", "weapon_glock",
"weapon_fiveseven", "weapon_hkp2000", "weapon_deagle"
"weapon_fiveseven", "weapon_hkp2000", "weapon_deagle",
"weapon_tec9", "weapon_revolver", "weapon_dualberettas"
},
Utilities = new List<string> {
"weapon_flashbang"
Expand Down Expand Up @@ -121,7 +126,7 @@ public class DeathmatchConfig : BasePluginConfig
ExecuteCommands = new List<string>()
}
};
[JsonPropertyName("Weapons Restrict")] public WeaponsRestrict WeaponsRestrict { get; set; } = new WeaponsRestrict();
[JsonPropertyName("Weapons Restrict")] public WeaponsRestrict RestrictedWeapons { get; set; } = new WeaponsRestrict();
}

public class Database
Expand Down Expand Up @@ -278,7 +283,7 @@ public class WeaponsRestrict
[JsonPropertyName("Global Restrict")] public bool Global { get; set; } = true;

[JsonPropertyName("Weapons")]
public Dictionary<string, Dictionary<string, Dictionary<RestrictType, RestrictData>>> DefaultRestrictions { get; set; } = new()
public Dictionary<string, Dictionary<string, Dictionary<RestrictType, RestrictData>>> Restrictions { get; set; } = new()
{
["weapon_ak47"] = new Dictionary<string, Dictionary<RestrictType, RestrictData>>()
{
Expand Down
115 changes: 19 additions & 96 deletions source/Deathmatch/Deathmatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Entities.Constants;
using CounterStrikeSharp.API.Modules.Menu;
using Newtonsoft.Json;
using CounterStrikeSharp.API.Core.Capabilities;
using DeathmatchAPI.Helpers;
using static DeathmatchAPI.Events.IDeathmatchEventsAPI;
Expand All @@ -20,7 +19,7 @@ public partial class Deathmatch : BasePlugin, IPluginConfig<DeathmatchConfig>
{
public override string ModuleName => "Deathmatch Core";
public override string ModuleAuthor => "Nocky";
public override string ModuleVersion => "1.2.1";
public override string ModuleVersion => "1.2.2";

public void OnConfigParsed(DeathmatchConfig config)
{
Expand All @@ -31,17 +30,9 @@ public override void Load(bool hotReload)
{
var API = new Deathmatch();
Capabilities.RegisterPluginCapability(DeathmatchAPI, () => API);
IsLinuxServer = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
if (IsLinuxServer)
{
GetCSWeaponDataFromKeyFunc = new(GameData.GetSignature("GetCSWeaponDataFromKey"));
CCSPlayer_CanAcquireFunc = new(GameData.GetSignature("CCSPlayer_CanAcquire"));
CCSPlayer_CanAcquireFunc.Hook(OnWeaponCanAcquire, HookMode.Pre);
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnTakeDamage, HookMode.Pre);
}
VirtualFunctions.CCSPlayer_ItemServices_CanAcquireFunc.Hook(OnWeaponCanAcquire, HookMode.Pre);
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnTakeDamage, HookMode.Pre);

LoadCustomModes();
LoadWeaponsRestrict();
if (Config.SaveWeapons)
_ = CreateDatabaseConnection();

Expand Down Expand Up @@ -79,6 +70,7 @@ public override void Load(bool hotReload)
{
mapLoaded = true;
DefaultMapSpawnDisabled = false;
playerData.Clear();
AddTimer(3.0f, () =>
{
LoadCustomConfigFile();
Expand All @@ -102,12 +94,12 @@ public override void Load(bool hotReload)
{
SetupCustomMode(NextMode.ToString());
}
if (!string.IsNullOrEmpty(ActiveMode.CenterMessageText) && CustomModes.ContainsKey(NextMode.ToString()))
if (!string.IsNullOrEmpty(ActiveMode.CenterMessageText) && Config.CustomModes.ContainsKey(NextMode.ToString()))
{
var time = TimeSpan.FromSeconds(RemainingTime);
var formattedTime = $"{time.Minutes}:{time.Seconds:D2}";//RemainingTime > 60 ? $"{time.Minutes}:{time.Seconds:D2}" : $"{time.Seconds}";

var NextModeData = CustomModes[NextMode.ToString()];
var NextModeData = Config.CustomModes[NextMode.ToString()];
ModeCenterMessage = ActiveMode.CenterMessageText.Replace("{REMAININGTIME}", formattedTime);
ModeCenterMessage = ModeCenterMessage.Replace("{NEXTMODE}", NextModeData.Name);
}
Expand All @@ -133,20 +125,12 @@ public override void Load(bool hotReload)
}
}
});
RegisterListener<OnServerPrecacheResources>((manifest) =>
{
manifest.AddResource("characters/models/shared/animsets/animset_uiplayer.vmdl");

});
RegisterListener<OnTick>(() =>
{
if (VisibleHud)
{
foreach (var p in Utilities.GetPlayers().Where(p => playerData.ContainsPlayer(p)))
{
if (!playerData[p].Preferences.ContainsKey("HudMessages") || !playerData[p].Preferences["HudMessages"])
continue;

if (ActiveEditor == p)
{
var ctSpawns = DefaultMapSpawnDisabled ? spawnPositionsCT.Count : 0;
Expand All @@ -155,7 +139,7 @@ public override void Load(bool hotReload)
}
else
{
if (MenuManager.GetActiveMenu(p) != null)
if (!playerData[p].Preferences.TryGetValue("HudMessages", out var hudMessage) || !hudMessage || MenuManager.GetActiveMenu(p) != null)
continue;

if (!string.IsNullOrEmpty(ActiveMode.CenterMessageText))
Expand All @@ -176,11 +160,11 @@ public override void Load(bool hotReload)
}
else
{
var NextModeData = CustomModes[NextMode.ToString()];
var NextModeData = Config.CustomModes[NextMode.ToString()];
if (Config.Gameplay.HudType == 1)
p.PrintToCenter($"{Localizer["Hud.NewModeStarting", RemainingTime, NextModeData.Name]}");
else
p.PrintToCenterHtml($"{Localizer["Hud.NewModeStarting", RemainingTime, NextModeData.Name]}");
else
p.PrintToCenter($"{Localizer["Hud.NewModeStarting", RemainingTime, NextModeData.Name]}");
}
}
}
Expand All @@ -191,27 +175,23 @@ public override void Load(bool hotReload)

public override void Unload(bool hotReload)
{

if (IsLinuxServer)
{
CCSPlayer_CanAcquireFunc?.Unhook(OnWeaponCanAcquire, HookMode.Pre);
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(OnTakeDamage, HookMode.Pre);
}
VirtualFunctions.CCSPlayer_ItemServices_CanAcquireFunc.Unhook(OnWeaponCanAcquire, HookMode.Pre);
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(OnTakeDamage, HookMode.Pre);
}

public void SetupCustomMode(string modeId)
{
ActiveMode = CustomModes[modeId];
ActiveMode = Config.CustomModes[modeId];
bool bNewmode = true;
if (modeId.Equals(ActiveCustomMode.ToString()))
bNewmode = false;

ActiveCustomMode = modeId;
NextMode = GetModeType();

if (CustomModes.ContainsKey(NextMode.ToString()) && !string.IsNullOrEmpty(ActiveMode.CenterMessageText))
if (Config.CustomModes.ContainsKey(NextMode.ToString()) && !string.IsNullOrEmpty(ActiveMode.CenterMessageText))
{
var NextModeData = CustomModes[NextMode.ToString()];
var NextModeData = Config.CustomModes[NextMode.ToString()];
ModeCenterMessage = ActiveMode.CenterMessageText.Replace("{NEXTMODE}", NextModeData.Name);
ModeCenterMessage = ModeCenterMessage.Replace("{REMAININGTIME}", RemainingTime.ToString());
}
Expand Down Expand Up @@ -306,75 +286,18 @@ sv_cheats 0
}
Server.ExecuteCommand("exec deathmatch/deathmatch.cfg");
}
public void LoadCustomModes()
{
string filePath = Server.GameDirectory + "/csgo/addons/counterstrikesharp/configs/plugins/Deathmatch/Deathmatch.json";
if (File.Exists(filePath))
{
try
{
var jsonData = File.ReadAllText(filePath);
dynamic deserializedJson = JsonConvert.DeserializeObject(jsonData)!;
var modes = deserializedJson["Custom Modes"].ToObject<Dictionary<string, ModeData>>();
CustomModes = new Dictionary<string, ModeData>(modes);
SendConsoleMessage($"[Deathmatch] Loaded {CustomModes.Count} Custom Modes", ConsoleColor.Green);

}
catch (Exception ex)
{
SendConsoleMessage($"[Deathmatch] An error occurred while loading Custom Modes: {ex.Message}", ConsoleColor.Red);
throw new Exception($"An error occurred while loading Custom Modes: {ex.Message}");
}
}
}

public void LoadWeaponsRestrict()
{
string filePath = Server.GameDirectory + "/csgo/addons/counterstrikesharp/configs/plugins/Deathmatch/Deathmatch.json";
if (File.Exists(filePath))
{
try
{
var jsonData = File.ReadAllText(filePath);
dynamic deserializedJson = JsonConvert.DeserializeObject(jsonData)!;
var weapons = deserializedJson["Weapons Restrict"]["Weapons"].ToObject<Dictionary<string, Dictionary<string, Dictionary<RestrictType, RestrictData>>>>();
RestrictedWeapons = new(weapons);
SendConsoleMessage($"[Deathmatch] Total Restricted Weapons: {RestrictedWeapons.Count}", ConsoleColor.Green);
/*foreach (var item in RestrictedWeapons)
{
SendConsoleMessage(item.Key, ConsoleColor.Magenta);
foreach (var mode in item.Value)
{
SendConsoleMessage(mode.Key, ConsoleColor.Magenta);
foreach (var type in mode.Value)
{
SendConsoleMessage($"type - {type.Key}", ConsoleColor.Magenta);
var data = type.Value;
SendConsoleMessage($"data T - {data.T}", ConsoleColor.Magenta);
SendConsoleMessage($"data CT - {data.CT}", ConsoleColor.Magenta);
SendConsoleMessage($"data GLOBAL - {data.Global}", ConsoleColor.Magenta);
}
}
}*/
}
catch (Exception ex)
{
SendConsoleMessage($"[Deathmatch] An error occurred while loading Weapons Restrictions: {ex.Message}", ConsoleColor.Red);
throw new Exception($"An error occurred while loading Weapons Restrictions: {ex.Message}");
}
}
}
public void SetupDeathMatchConfigValues()
{
var gameType = ConVar.Find("game_type")!.GetPrimitiveValue<int>();
IsCasualGamemode = gameType != 1;
if (!IsLinuxServer && !IsCasualGamemode)
/*if (!IsLinuxServer && !IsCasualGamemode)
{
SendConsoleMessage("======= Deathmatch WARNING =======", ConsoleColor.Red);
SendConsoleMessage("Your server is running on Windows, the Deathmatch plugin does not work properly if you have deathmatch game mode (game_type 1 and game_mode 2)", ConsoleColor.DarkYellow);
SendConsoleMessage("Please use game mode Casual!", ConsoleColor.DarkYellow);
SendConsoleMessage("======= Deathmatch WARNING =======", ConsoleColor.Red);
}
}*/

var iHideSecond = Config.General.HideRoundSeconds ? 1 : 0;
var iFFA = Config.Gameplay.IsFFA ? 1 : 0;
Expand All @@ -401,13 +324,13 @@ public int GetModeType()
int iRandomMode;
do
{
iRandomMode = Random.Next(0, CustomModes.Count);
iRandomMode = Random.Next(0, Config.CustomModes.Count);
} while (iRandomMode == modeId);
return iRandomMode;
}
else
{
if (modeId + 1 != CustomModes.Count && modeId + 1 < CustomModes.Count)
if (modeId + 1 != Config.CustomModes.Count && modeId + 1 < Config.CustomModes.Count)
return modeId + 1;
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion source/Deathmatch/Deathmatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.264" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.282" />
<PackageReference Include="MysqlConnector" Version="2.3.7" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<ProjectReference Include="..\DeathmatchAPI\DeathmatchAPI.csproj" />
Expand Down
Loading

0 comments on commit bda55bd

Please sign in to comment.