Compare commits

...

4 Commits

Author SHA1 Message Date
Nereziel
643beaad46 Merge pull request #90 from daffyyyy/some-changes
!skins thread-safe and checking version
2023-12-06 17:18:06 +01:00
daffyyyy
6d44e582be !skins thread-safe and checking version 2023-12-06 14:47:26 +01:00
Nereziel
813a9abcc8 Merge pull request #89 from daffyyyy/some-changes
Small fixes cssharp >= 101
2023-12-05 19:32:00 +01:00
daffyyyy
b0790729be Small fixes 2023-12-05 19:30:10 +01:00
7 changed files with 89 additions and 26 deletions

View File

@@ -1,5 +1,7 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities.Constants;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Menu;
namespace WeaponPaints
@@ -109,6 +111,7 @@ namespace WeaponPaints
{
if (Utility.IsPlayerValid(player))
{
if (player == null) return;
var knifeName = option.Text;
var knifeKey = knivesOnly.FirstOrDefault(x => x.Value == knifeName).Key;
if (!string.IsNullOrEmpty(knifeKey))
@@ -127,14 +130,24 @@ namespace WeaponPaints
player!.PrintToChat(Utility.ReplaceTags(temp));
}
PlayerInfo playerInfo = new PlayerInfo
{
UserId = player.UserId,
Index = (int)player.Index,
SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(),
Name = player?.PlayerName,
IpAddress = player?.IpAddress?.Split(":")[0]
};
g_playersKnife[(int)player!.Index] = knifeKey;
if (player!.PawnIsAlive && g_bCommandsAllowed)
{
RefreshWeapons(player);
}
if (weaponSync != null)
Task.Run(() => weaponSync.SyncKnifeToDatabase((int)player.Index, knifeKey));
Task.Run(async () => await weaponSync.SyncKnifeToDatabase(playerInfo, knifeKey));
}
}
};
@@ -220,16 +233,22 @@ namespace WeaponPaints
}
gPlayerWeaponsInfo[playerIndex][weaponDefIndex].Paint = paintID;
gPlayerWeaponsInfo[playerIndex][weaponDefIndex].Wear = 0.0f;
gPlayerWeaponsInfo[playerIndex][weaponDefIndex].Wear = 0.01f;
gPlayerWeaponsInfo[playerIndex][weaponDefIndex].Seed = 0;
PlayerInfo playerInfo = new PlayerInfo
{
UserId = player.UserId,
Index = (int)player.Index,
SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(),
Name = player?.PlayerName,
IpAddress = player?.IpAddress?.Split(":")[0]
};
if (!Config.GlobalShare)
{
if (weaponSync == null) return;
Task.Run(async () =>
{
await weaponSync.SyncWeaponPaintsToDatabase(p);
});
if (weaponSync != null)
Task.Run(async () => await weaponSync.SyncWeaponPaintsToDatabase(playerInfo));
}
}
};

View File

@@ -95,7 +95,7 @@ namespace WeaponPaints
if (@event.Defindex == 42 || @event.Defindex == 59)
{
CCSPlayerController? player = @event.Userid;
if (!Utility.IsPlayerValid(player) || !player.PawnIsAlive || g_knifePickupCount[(int)player.Index] >= 2) return HookResult.Continue;
if (!Utility.IsPlayerValid(player) || !player.PawnIsAlive || g_knifePickupCount[(int)player.Index] >= 1) return HookResult.Continue;
if (g_playersKnife.ContainsKey((int)player.Index)
&&
@@ -207,6 +207,7 @@ namespace WeaponPaints
return HookResult.Continue;
}
private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
g_bCommandsAllowed = false;

View File

@@ -5,6 +5,7 @@ using MySqlConnector;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Reflection;
using Microsoft.Extensions.Logging;
namespace WeaponPaints
{
@@ -107,6 +108,45 @@ namespace WeaponPaints
return message;
}
internal static async Task CheckVersion(string version)
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("https://github.com/Nereziel/cs2-WeaponPaints/blob/main/VERSION");
if (response.IsSuccessStatusCode)
{
string remoteVersion = await response.Content.ReadAsStringAsync();
remoteVersion = remoteVersion.Trim();
int comparisonResult = string.Compare(version, remoteVersion);
if (comparisonResult < 0)
{
WeaponPaints.logger!.LogWarning("Plugin is outdated! Check https://github.com/Nereziel/cs2-WeaponPaints");
}
else if (comparisonResult > 0)
{
WeaponPaints.logger!.LogInformation("Probably dev version detected");
}
else
{
WeaponPaints.logger!.LogInformation("Plugin is up to date");
}
}
else
{
WeaponPaints.logger!.LogWarning("Failed to check version");
}
}
catch (Exception)
{
}
}
}
internal static void ShowAd(string moduleVersion)
{
Console.WriteLine(" ");

1
VERSION Normal file
View File

@@ -0,0 +1 @@
1.3f

View File

@@ -2,6 +2,7 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using Microsoft.Extensions.Logging;
namespace WeaponPaints
{
@@ -81,7 +82,7 @@ namespace WeaponPaints
{
if (!_config.Additional.KnifeEnabled) return false;
if (player == null || !player.IsValid || !player.PlayerPawn.IsValid)
if (player == null || !player.IsValid || player.PlayerPawn == null || !player.PlayerPawn.IsValid || !player.PawnIsAlive)
{
return false;
}
@@ -221,6 +222,7 @@ namespace WeaponPaints
}
catch (Exception ex)
{
Logger.LogWarning("Refreshing weapons exception");
Console.WriteLine("[WeaponPaints] Refreshing weapons exception");
Console.WriteLine(ex.Message);
}
@@ -240,6 +242,7 @@ namespace WeaponPaints
if (weapons != null && weapons.Count > 0)
{
CCSPlayer_ItemServices service = new CCSPlayer_ItemServices(player.PlayerPawn.Value.ItemServices.Handle);
//var dropWeapon = VirtualFunction.CreateVoid<nint, nint>(service.Handle, GameData.GetOffset("CCSPlayer_ItemServices_DropActivePlayerWeapon"));
foreach (var weapon in weapons)
{

View File

@@ -3,12 +3,14 @@ using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Modules.Cvars;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Logging;
namespace WeaponPaints;
[MinimumApiVersion(101)]
public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{
internal static ILogger? logger;
internal static readonly Dictionary<string, string> weaponList = new()
{
{"weapon_deagle", "Desert Eagle"},
@@ -76,7 +78,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
//internal static List<int> g_changedKnife = new();
internal bool g_bCommandsAllowed = true;
internal Uri GlobalShareApi = new Uri("https://weaponpaints.fun/api.php");
internal Uri GlobalShareApi = new("https://weaponpaints.fun/api.php");
internal int GlobalShareServerId = 0;
private DateTime[] commandCooldown = new DateTime[Server.MaxPlayers];
private string DatabaseConnectionString = string.Empty;
@@ -142,7 +144,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
public override string ModuleAuthor => "Nereziel & daffyy";
public override string ModuleDescription => "Skin and knife selector, standalone and web-based";
public override string ModuleName => "WeaponPaints";
public override string ModuleVersion => "1.3c";
public override string ModuleVersion => "1.3f";
public static WeaponPaintsConfig GetWeaponPaintsConfig()
{
return _config;
@@ -203,6 +205,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
{
if (config.DatabaseHost.Length < 1 || config.DatabaseName.Length < 1 || config.DatabaseUser.Length < 1)
{
Logger.LogError("You need to setup Database credentials in config!");
throw new Exception("[WeaponPaints] You need to setup Database credentials in config!");
}
}
@@ -211,6 +214,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
_config = config;
Utility.Config = config;
Utility.ShowAd(ModuleVersion);
Task.Run(async () => await Utility.CheckVersion(ModuleVersion));
}
public override void Unload(bool hotReload)
@@ -246,10 +250,12 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
}
else
{
Logger.LogError("Unable to retrieve serverid from GlobalShare!");
throw new Exception("[WeaponPaints] Unable to retrieve serverid from GlobalShare!");
}
}
Logger.LogInformation("GlobalShare ONLINE!");
Console.WriteLine("[WeaponPaints] GlobalShare ONLINE");
}
}

View File

@@ -190,20 +190,17 @@ namespace WeaponPaints
}
}
internal async Task SyncKnifeToDatabase(int playerIndex, string knife)
internal async Task SyncKnifeToDatabase(PlayerInfo player, string knife)
{
if (!_config.Additional.KnifeEnabled) return;
try
{
CCSPlayerController player = Utilities.GetPlayerFromIndex(playerIndex);
if (player == null || !player.IsValid) return;
if (player.AuthorizedSteamID == null) return;
string steamId = player.AuthorizedSteamID.SteamId64.ToString();
if (player.SteamId == null || player.Index == 0) return;
using var connection = new MySqlConnection(_databaseConnectionString);
await connection.OpenAsync();
string query = "INSERT INTO `wp_player_knife` (`steamid`, `knife`) VALUES(@steamid, @newKnife) ON DUPLICATE KEY UPDATE `knife` = @newKnife";
await connection.ExecuteAsync(query, new { steamid = steamId, newKnife = knife });
await connection.ExecuteAsync(query, new { steamid = player.SteamId, newKnife = knife });
await connection.CloseAsync();
}
catch (Exception e)
@@ -212,21 +209,17 @@ namespace WeaponPaints
return;
}
}
internal async Task SyncWeaponPaintsToDatabase(CCSPlayerController? player)
internal async Task SyncWeaponPaintsToDatabase(PlayerInfo player)
{
if (player == null || !Utility.IsPlayerValid(player)) return;
int playerIndex = (int)player.Index;
if (player.AuthorizedSteamID == null) return;
string steamId = player.AuthorizedSteamID.SteamId64.ToString();
if (player == null || player.Index <= 0 || player.SteamId == null) return;
using var connection = new MySqlConnection(_databaseConnectionString);
await connection.OpenAsync();
if (!WeaponPaints.gPlayerWeaponsInfo.ContainsKey(playerIndex))
if (!WeaponPaints.gPlayerWeaponsInfo.ContainsKey(player.Index))
return;
foreach (var weaponInfoPair in WeaponPaints.gPlayerWeaponsInfo[playerIndex])
foreach (var weaponInfoPair in WeaponPaints.gPlayerWeaponsInfo[player.Index])
{
int weaponDefIndex = weaponInfoPair.Key;
WeaponInfo weaponInfo = weaponInfoPair.Value;
@@ -239,7 +232,7 @@ namespace WeaponPaints
"`weapon_wear` = @wear, `weapon_seed` = @seed WHERE `steamid` = @steamid " +
"AND `weapon_defindex` = @weaponDefIndex";
var updateParams = new { paintId, wear, seed, steamid = steamId, weaponDefIndex };
var updateParams = new { paintId, wear, seed, steamid = player.SteamId, weaponDefIndex };
int rowsAffected = await connection.ExecuteAsync(updateSql, updateParams);
if (rowsAffected == 0)