usage of memory

This commit is contained in:
1MacK
2023-11-07 23:29:47 -03:00
parent fee3810d66
commit 861be80368

View File

@@ -11,12 +11,15 @@ public class WeaponPaints : BasePlugin
public override string ModuleAuthor => "Nereziel"; public override string ModuleAuthor => "Nereziel";
public override string ModuleVersion => "0.4"; public override string ModuleVersion => "0.4";
MySqlDb? MySql = null; MySqlDb? MySql = null;
private Dictionary<ulong, Dictionary<nint, int>> g_playersSkins = new Dictionary<ulong, Dictionary<nint, int>>();
public override void Load(bool hotReload) public override void Load(bool hotReload)
{ {
new Cfg().CheckConfig(ModuleDirectory); new Cfg().CheckConfig(ModuleDirectory);
MySql = new MySqlDb(Cfg.config.DatabaseHost!, Cfg.config.DatabaseUser!, Cfg.config.DatabasePassword!, Cfg.config.DatabaseName!, (int)Cfg.config.DatabasePort); MySql = new MySqlDb(Cfg.config.DatabaseHost!, Cfg.config.DatabaseUser!, Cfg.config.DatabasePassword!, Cfg.config.DatabaseName!, (int)Cfg.config.DatabasePort);
RegisterListener<Listeners.OnEntitySpawned>(OnEntitySpawned); RegisterListener<Listeners.OnEntitySpawned>(OnEntitySpawned);
RegisterListener<Listeners.OnClientPutInServer>(OnClientConnect);
} }
private void OnEntitySpawned(CEntityInstance entity) private void OnEntitySpawned(CEntityInstance entity)
@@ -31,14 +34,26 @@ public class WeaponPaints : BasePlugin
if (!weapon.IsValid) return; if (!weapon.IsValid) return;
var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex((int)weapon.OwnerEntity.Value.EntityIndex!.Value.Value)); var pawn = new CBasePlayerPawn(NativeAPI.GetEntityFromIndex((int)weapon.OwnerEntity.Value.EntityIndex!.Value.Value));
if (!pawn.IsValid) return; if (!pawn.IsValid) return;
var playerIndex = (int)pawn.Controller.Value.EntityIndex!.Value.Value; var playerIndex = (int)pawn.Controller.Value.EntityIndex!.Value.Value;
int weaponPaint = GetPlayersWeaponPaint(playerIndex, weapon.AttributeManager.Item.ItemDefinitionIndex);
if (weaponPaint == 0) return; CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
if (player == null || !player.IsValid) return;
var steamId = new SteamID(player.SteamID);
if (g_playersSkins.TryGetValue(steamId.SteamId64, out var weaponIDs))
{
if (weaponIDs.TryGetValue(weapon.AttributeManager.Item.ItemDefinitionIndex, out var weaponPaint))
{
weapon.AttributeManager.Item.ItemIDLow = unchecked((uint)-1); weapon.AttributeManager.Item.ItemIDLow = unchecked((uint)-1);
weapon.AttributeManager.Item.ItemIDHigh = unchecked((uint)-1); weapon.AttributeManager.Item.ItemIDHigh = unchecked((uint)-1);
weapon.FallbackPaintKit = weaponPaint; weapon.FallbackPaintKit = weaponPaint;
weapon.FallbackSeed = 0; weapon.FallbackSeed = 0;
weapon.FallbackWear = 0.0001f; weapon.FallbackWear = 0.0001f;
}
}
}); });
} }
private static void Log(string message) private static void Log(string message)
@@ -48,27 +63,38 @@ public class WeaponPaints : BasePlugin
Console.WriteLine(message); Console.WriteLine(message);
Console.ResetColor(); Console.ResetColor();
} }
public int GetPlayersWeaponPaint(int playerIndex, int weaponDefIndex) private void OnClientConnect(int playerSlot)
{ {
try try
{ {
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex); CCSPlayerController player = Utilities.GetPlayerFromSlot(playerSlot);
if (player == null || !player.IsValid)
return 0; if (player == null || !player.IsValid) return;
var steamId = new SteamID(player.SteamID); var steamId = new SteamID(player.SteamID);
MySqlQueryCondition conditions = new MySqlQueryCondition() MySqlQueryCondition conditions = new MySqlQueryCondition()
.Add("steamid", "=", steamId.SteamId64.ToString()) .Add("steamid", "=", steamId.SteamId64.ToString());
.Add("weapon_defindex", "=", weaponDefIndex);
MySqlQueryResult result = MySql!.Table("wp_player_skins").Where(conditions).Select(); MySqlQueryResult result = MySql!.Table("wp_player_skins").Where(conditions).Select();
int weaponPaint = result.Get<int>(0, "weapon_paint_id");
return weaponPaint; result.ToList().ForEach(pair =>
{
int weponId = result.Get<int>(pair.Key, "weapon_defindex");
int weponPaint = result.Get<int>(pair.Key, "weapon_paint_id");
if (!g_playersSkins.ContainsKey(steamId.SteamId64))
{
g_playersSkins[steamId.SteamId64] = new Dictionary<nint, int>();
}
g_playersSkins[steamId.SteamId64][weponId] = weponPaint;
}
);
} }
catch (Exception) catch (Exception)
{ {
return 0; return;
} }
} }
} }