Skip to content

Commit

Permalink
Merge pull request #253 from daffyyyy/main
Browse files Browse the repository at this point in the history
2.4e
  • Loading branch information
daffyyyy authored Apr 28, 2024
2 parents 74ec584 + c06f7ae commit 48854c4
Show file tree
Hide file tree
Showing 10 changed files with 448 additions and 539 deletions.
481 changes: 233 additions & 248 deletions Commands.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ public class WeaponPaintsConfig : BasePluginConfig
public string Website { get; set; } = "example.com/skins";

[JsonPropertyName("Additional")]
public Additional Additional { get; set; } = new Additional();
public Additional Additional { get; set; } = new();
}
}
67 changes: 21 additions & 46 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public HookResult OnClientFullConnect(EventPlayerConnectFull @event, GameEventIn
if (player is null || !player.IsValid || player.IsBot || player.IsHLTV || player.SteamID.ToString().Length != 17 ||
weaponSync == null || _database == null) return HookResult.Continue;

PlayerInfo playerInfo = new PlayerInfo
PlayerInfo? playerInfo = new PlayerInfo
{
UserId = player.UserId,
Slot = player.Slot,
Expand Down Expand Up @@ -54,7 +54,7 @@ public HookResult OnClientFullConnect(EventPlayerConnectFull @event, GameEventIn
}
*/
}
catch (Exception)
catch
{
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo
private void GivePlayerWeaponSkin(CCSPlayerController player, CBasePlayerWeapon weapon)
{
if (!Config.Additional.SkinEnabled) return;
if (!gPlayerWeaponsInfo.TryGetValue(player.Slot, out System.Collections.Concurrent.ConcurrentDictionary<int, WeaponInfo>? _value)) return;
if (!gPlayerWeaponsInfo.TryGetValue(player.Slot, out _)) return;

bool isKnife = weapon.DesignerName.Contains("knife") || weapon.DesignerName.Contains("bayonet");

Expand Down Expand Up @@ -143,61 +143,44 @@ private void GivePlayerWeaponSkin(CCSPlayerController player, CBasePlayerWeapon
weapon.FallbackPaintKit = GetRandomPaint(weaponDefIndex);
weapon.FallbackSeed = 0;
weapon.FallbackWear = 0.000001f;
CAttributeList_SetOrAddAttributeValueByName.Invoke(weapon.AttributeManager.Item.NetworkedDynamicAttributes.Handle, "set item texture prefab", weapon.FallbackPaintKit);
CAttributeListSetOrAddAttributeValueByName.Invoke(
weapon.AttributeManager.Item.NetworkedDynamicAttributes.Handle, "set item texture prefab",
weapon.FallbackPaintKit);

fallbackPaintKit = weapon.FallbackPaintKit;

if (fallbackPaintKit == 0)
return;

if (!isKnife)
{
if (newPaints.Contains(fallbackPaintKit))
{
UpdatePlayerWeaponMeshGroupMask(player, weapon, false);
}
else
{
UpdatePlayerWeaponMeshGroupMask(player, weapon, true);
}
}

if (isKnife) return;
UpdatePlayerWeaponMeshGroupMask(player, weapon, !newPaints.Contains(fallbackPaintKit));
return;
}

if (!gPlayerWeaponsInfo[player.Slot].TryGetValue(weaponDefIndex, out WeaponInfo? value) || value.Paint == 0) return;
if (!gPlayerWeaponsInfo[player.Slot].TryGetValue(weaponDefIndex, out var value) || value.Paint == 0) return;

WeaponInfo weaponInfo = value;
var weaponInfo = value;
//Log($"Apply on {weapon.DesignerName}({weapon.AttributeManager.Item.ItemDefinitionIndex}) paint {gPlayerWeaponPaints[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} seed {gPlayerWeaponSeed[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]} wear {gPlayerWeaponWear[steamId.SteamId64][weapon.AttributeManager.Item.ItemDefinitionIndex]}");
weapon.AttributeManager.Item.ItemID = 16384;
weapon.AttributeManager.Item.ItemIDLow = 16384 & 0xFFFFFFFF;
weapon.AttributeManager.Item.ItemIDHigh = weapon.AttributeManager.Item.ItemIDLow >> 32;
weapon.FallbackPaintKit = weaponInfo.Paint;
weapon.FallbackSeed = weaponInfo.Seed;
weapon.FallbackWear = weaponInfo.Wear;
CAttributeList_SetOrAddAttributeValueByName.Invoke(weapon.AttributeManager.Item.NetworkedDynamicAttributes.Handle, "set item texture prefab", weapon.FallbackPaintKit);
CAttributeListSetOrAddAttributeValueByName.Invoke(weapon.AttributeManager.Item.NetworkedDynamicAttributes.Handle, "set item texture prefab", weapon.FallbackPaintKit);

fallbackPaintKit = weapon.FallbackPaintKit;

if (fallbackPaintKit == 0)
return;

if (!isKnife)
{
if (newPaints.Contains(fallbackPaintKit))
{
UpdatePlayerWeaponMeshGroupMask(player, weapon, false);
}
else
{
UpdatePlayerWeaponMeshGroupMask(player, weapon, true);
}
}
if (isKnife) return;
UpdatePlayerWeaponMeshGroupMask(player, weapon, !newPaints.Contains(fallbackPaintKit));
}

private void OnMapStart(string mapName)
{
if (!Config.Additional.KnifeEnabled && !Config.Additional.SkinEnabled && !Config.Additional.GloveEnabled) return;
if (Config.Additional is { KnifeEnabled: false, SkinEnabled: false, GloveEnabled: false }) return;

if (_database != null)
weaponSync = new WeaponSynchronization(_database, Config);
Expand All @@ -207,7 +190,7 @@ private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;

if (player is null || !player.IsValid || !Config.Additional.KnifeEnabled && !Config.Additional.GloveEnabled)
if (player is null || !player.IsValid || Config.Additional is { KnifeEnabled: false, GloveEnabled: false })
return HookResult.Continue;

CCSPlayerPawn? pawn = player.PlayerPawn.Value;
Expand Down Expand Up @@ -237,17 +220,10 @@ private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)

private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
/*
NativeAPI.IssueServerCommand("mp_t_default_melee \"\"");
NativeAPI.IssueServerCommand("mp_ct_default_melee \"\"");
NativeAPI.IssueServerCommand("mp_equipment_reset_rounds 0");
*/
g_bCommandsAllowed = true;

return HookResult.Continue;
}


public HookResult OnGiveNamedItemPost(DynamicHook hook)
{
try
Expand All @@ -266,7 +242,6 @@ public HookResult OnGiveNamedItemPost(DynamicHook hook)
return HookResult.Continue;
}


public void OnEntityCreated(CEntityInstance entity)
{
var designerName = entity.DesignerName;
Expand All @@ -276,7 +251,7 @@ public void OnEntityCreated(CEntityInstance entity)
Server.NextFrame(() =>
{
var weapon = new CBasePlayerWeapon(entity.Handle);
if (weapon == null || !weapon.IsValid) return;
if (!weapon.IsValid) return;

try
{
Expand All @@ -289,7 +264,7 @@ public void OnEntityCreated(CEntityInstance entity)

if (_steamid != null && _steamid.IsValid())
{
player = Utilities.GetPlayers().Where(p => p is not null && p.IsValid && p.SteamID == _steamid.SteamId64).FirstOrDefault();
player = Utilities.GetPlayers().FirstOrDefault(p => p.IsValid && p.SteamID == _steamid.SteamId64);

if (player == null)
player = Utilities.GetPlayerFromSteamId(weapon.OriginalOwnerXuidLow);
Expand All @@ -301,7 +276,7 @@ public void OnEntityCreated(CEntityInstance entity)
}

if (string.IsNullOrEmpty(player?.PlayerName)) return;
if (player is null || !Utility.IsPlayerValid(player)) return;
if (!Utility.IsPlayerValid(player)) return;

GivePlayerWeaponSkin(player, weapon);
}
Expand All @@ -318,13 +293,13 @@ private void OnTick()
if (!Config.Additional.ShowSkinImage) return;

foreach (var player in Utilities.GetPlayers().Where(p =>
p is not null && p.IsValid && p.PlayerPawn != null && p.PlayerPawn.IsValid &&
p is { IsValid: true, PlayerPawn.IsValid: true } &&
(LifeState_t)p.LifeState == LifeState_t.LIFE_ALIVE && p.SteamID.ToString().Length == 17
&& !p.IsBot && !p.IsHLTV && p.Connected == PlayerConnectedState.PlayerConnected
&& !p.IsBot && p is { IsHLTV: false, Connected: PlayerConnectedState.PlayerConnected }
)
)
{
if (PlayerWeaponImage.TryGetValue(player.Slot, out string? value) && !string.IsNullOrEmpty(value))
if (PlayerWeaponImage.TryGetValue(player.Slot, out var value) && !string.IsNullOrEmpty(value))
{
player.PrintToCenterHtml("<img src='{PATH}'</img>".Replace("{PATH}", value));
}
Expand Down
4 changes: 2 additions & 2 deletions PlayerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
public class PlayerInfo
{
public int Index { get; set; }
public int Slot { get; set; }
public int Slot { get; init; }
public int? UserId { get; set; }
public string? SteamId { get; set; }
public string? SteamId { get; init; }
public string? Name { get; set; }
public string? IpAddress { get; set; }
}
Expand Down
98 changes: 53 additions & 45 deletions Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,42 @@ internal static async Task CheckDatabaseTables()
{
string[] createTableQueries =
[
@"CREATE TABLE IF NOT EXISTS `wp_player_skins` (
`steamid` varchar(18) NOT NULL,
`weapon_defindex` int(6) NOT NULL,
`weapon_paint_id` int(6) NOT NULL,
`weapon_wear` float NOT NULL DEFAULT 0.000001,
`weapon_seed` int(16) NOT NULL DEFAULT 0
) ENGINE=InnoDB",
"""
CREATE TABLE IF NOT EXISTS `wp_player_skins` (
`steamid` varchar(18) NOT NULL,
`weapon_defindex` int(6) NOT NULL,
`weapon_paint_id` int(6) NOT NULL,
`weapon_wear` float NOT NULL DEFAULT 0.000001,
`weapon_seed` int(16) NOT NULL DEFAULT 0
) ENGINE=InnoDB
""",
@"CREATE TABLE IF NOT EXISTS `wp_player_knife` (
`steamid` varchar(18) NOT NULL,
`knife` varchar(64) NOT NULL,
UNIQUE (`steamid`)
) ENGINE = InnoDB",
@"CREATE TABLE IF NOT EXISTS `wp_player_gloves` (
`steamid` varchar(18) NOT NULL,
`weapon_defindex` int(11) NOT NULL,
UNIQUE (`steamid`)
) ENGINE=InnoDB",
@"CREATE TABLE IF NOT EXISTS `wp_player_agents` (
`steamid` varchar(18) NOT NULL,
`agent_ct` varchar(64) DEFAULT NULL,
`agent_t` varchar(64) DEFAULT NULL,
UNIQUE (`steamid`)
) ENGINE=InnoDB",
@"CREATE TABLE IF NOT EXISTS `wp_player_music` (
`steamid` varchar(64) NOT NULL,
`music_id` int(11) NOT NULL,
UNIQUE (`steamid`)
) ENGINE=InnoDB",
"""
CREATE TABLE IF NOT EXISTS `wp_player_gloves` (
`steamid` varchar(18) NOT NULL,
`weapon_defindex` int(11) NOT NULL,
UNIQUE (`steamid`)
) ENGINE=InnoDB
""",
"""
CREATE TABLE IF NOT EXISTS `wp_player_agents` (
`steamid` varchar(18) NOT NULL,
`agent_ct` varchar(64) DEFAULT NULL,
`agent_t` varchar(64) DEFAULT NULL,
UNIQUE (`steamid`)
) ENGINE=InnoDB
""",
"""
CREATE TABLE IF NOT EXISTS `wp_player_music` (
`steamid` varchar(64) NOT NULL,
`music_id` int(11) NOT NULL,
UNIQUE (`steamid`)
) ENGINE=InnoDB
""",
];

foreach (var query in createTableQueries)
Expand All @@ -78,16 +86,16 @@ internal static bool IsPlayerValid(CCSPlayerController? player)
{
if (player is null || WeaponPaints.weaponSync is null) return false;

return (player.IsValid && !player.IsBot && !player.IsHLTV && player.UserId.HasValue);
return player is { IsValid: true, IsBot: false, IsHLTV: false, UserId: not null };
}

internal static void LoadSkinsFromFile(string filePath, ILogger logger)
{
string json = File.ReadAllText(filePath);
try
{
string json = File.ReadAllText(filePath);
var deserializedSkins = JsonConvert.DeserializeObject<List<JObject>>(json);
WeaponPaints.skinsList = deserializedSkins ?? new List<JObject>();
WeaponPaints.skinsList = deserializedSkins ?? [];
}
catch (FileNotFoundException)
{
Expand All @@ -99,9 +107,9 @@ internal static void LoadGlovesFromFile(string filePath, ILogger logger)
{
try
{
string json = File.ReadAllText(filePath);
var json = File.ReadAllText(filePath);
var deserializedSkins = JsonConvert.DeserializeObject<List<JObject>>(json);
WeaponPaints.glovesList = deserializedSkins ?? new List<JObject>();
WeaponPaints.glovesList = deserializedSkins ?? [];
}
catch (FileNotFoundException)
{
Expand All @@ -113,9 +121,9 @@ internal static void LoadAgentsFromFile(string filePath, ILogger logger)
{
try
{
string json = File.ReadAllText(filePath);
var json = File.ReadAllText(filePath);
var deserializedSkins = JsonConvert.DeserializeObject<List<JObject>>(json);
WeaponPaints.agentsList = deserializedSkins ?? new List<JObject>();
WeaponPaints.agentsList = deserializedSkins ?? [];
}
catch (FileNotFoundException)
{
Expand All @@ -127,9 +135,9 @@ internal static void LoadMusicFromFile(string filePath, ILogger logger)
{
try
{
string json = File.ReadAllText(filePath);
var json = File.ReadAllText(filePath);
var deserializedSkins = JsonConvert.DeserializeObject<List<JObject>>(json);
WeaponPaints.musicList = deserializedSkins ?? new List<JObject>();
WeaponPaints.musicList = deserializedSkins ?? [];
}
catch (FileNotFoundException)
{
Expand All @@ -156,26 +164,26 @@ internal static async Task CheckVersion(string version, ILogger logger)

try
{
HttpResponseMessage response = await client.GetAsync("https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/VERSION").ConfigureAwait(false);
var response = await client.GetAsync("https://raw.githubusercontent.com/Nereziel/cs2-WeaponPaints/main/VERSION").ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
string remoteVersion = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var remoteVersion = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
remoteVersion = remoteVersion.Trim();

int comparisonResult = string.Compare(version, remoteVersion);
var comparisonResult = string.CompareOrdinal(version, remoteVersion);

if (comparisonResult < 0)
{
logger.LogWarning("Plugin is outdated! Check https://github.com/Nereziel/cs2-WeaponPaints");
}
else if (comparisonResult > 0)
{
logger.LogInformation("Probably dev version detected");
}
else
switch (comparisonResult)
{
logger.LogInformation("Plugin is up to date");
case < 0:
logger.LogWarning("Plugin is outdated! Check https://github.com/Nereziel/cs2-WeaponPaints");
break;
case > 0:
logger.LogInformation("Probably dev version detected");
break;
default:
logger.LogInformation("Plugin is up to date");
break;
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4d
2.4e
Loading

0 comments on commit 48854c4

Please sign in to comment.