Skip to content

Commit

Permalink
Merge pull request #161 from daffyyyy/main
Browse files Browse the repository at this point in the history
1.8a
  • Loading branch information
daffyyyy authored Feb 19, 2024
2 parents fb4340c + a6821b8 commit 63f4b4c
Show file tree
Hide file tree
Showing 92 changed files with 369 additions and 152 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ jobs:
${{ env.OUTPUT_PATH }}/Microsoft.Extensions.DependencyModel.dll \
- name: Copy skins.json
run: cp website/data/skins.json ${{ env.OUTPUT_PATH }}/skins.json
- name: Copy gloves.json
run: cp website/data/gloves.json ${{ env.OUTPUT_PATH }}/gloves.json
- name: Zip
uses: thedoctor0/[email protected]
with:
Expand Down
115 changes: 106 additions & 9 deletions Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ private void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
DateTime.UtcNow >= (commandsCooldown.TryGetValue((int)player.UserId, out cooldownEndTime) ? cooldownEndTime : DateTime.UtcNow))
{
commandsCooldown[(int)player.UserId] = DateTime.UtcNow.AddSeconds(Config.CmdRefreshCooldownSeconds);

if (weaponSync != null)
Task.Run(async () => await weaponSync.GetWeaponPaintsFromDatabase(playerInfo));

if (Config.Additional.GloveEnabled && weaponSync != null)
Task.Run(async () => await weaponSync.GetGloveFromDatabase(playerInfo));

if (Config.Additional.KnifeEnabled)
{
if (weaponSync != null)
Expand Down Expand Up @@ -68,11 +73,18 @@ private void OnCommandWS(CCSPlayerController? player, CommandInfo command)
{
player!.Print(Localizer["wp_info_refresh"]);
}
if (!Config.Additional.KnifeEnabled) return;
if (!string.IsNullOrEmpty(Localizer["wp_info_knife"]))
{
player!.Print(Localizer["wp_info_knife"]);
}

if (Config.Additional.GloveEnabled)
if (!string.IsNullOrEmpty(Localizer["wp_info_glove"]))
{
player!.Print(Localizer["wp_info_glove"]);
}

if (Config.Additional.KnifeEnabled)
if (!string.IsNullOrEmpty(Localizer["wp_info_knife"]))
{
player!.Print(Localizer["wp_info_knife"]);
}
}

private void RegisterCommands()
Expand Down Expand Up @@ -107,7 +119,6 @@ private void SetupKnifeMenu()
.ToDictionary(pair => pair.Key, pair => pair.Value);

var giveItemMenu = new ChatMenu(Localizer["wp_knife_menu_title"]);
giveItemMenu.PostSelectAction = PostSelectAction.Close;
var handleGive = (CCSPlayerController player, ChatMenuOption option) =>
{
if (!Utility.IsPlayerValid(player)) return;
Expand Down Expand Up @@ -138,7 +149,7 @@ private void SetupKnifeMenu()
g_playersKnife[(int)player!.Index] = knifeKey;
if (g_bCommandsAllowed && (LifeState_t)player.LifeState == LifeState_t.LIFE_ALIVE)
RefreshKnife(player);
AddTimer(0.15f, () => RefreshWeapons(player), CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
_ = weaponSync?.SyncKnifeToDatabase(playerInfo, knifeKey) ?? Task.CompletedTask;
}
Expand All @@ -157,6 +168,7 @@ private void SetupKnifeMenu()
DateTime.UtcNow >= (commandsCooldown.TryGetValue((int)player.UserId, out cooldownEndTime) ? cooldownEndTime : DateTime.UtcNow))
{
commandsCooldown[(int)player.UserId] = DateTime.UtcNow.AddSeconds(Config.CmdRefreshCooldownSeconds);
giveItemMenu.PostSelectAction = PostSelectAction.Close;
MenuManager.OpenChatMenu(player, giveItemMenu);
return;
}
Expand Down Expand Up @@ -226,7 +238,7 @@ private void SetupSkinsMenu()
);
string image = foundSkin?["image"]?.ToString() ?? "";
PlayerWeaponImage[p.Slot] = image;
AddTimer(2.0f, () => PlayerWeaponImage.Remove(p.Slot));
AddTimer(2.0f, () => PlayerWeaponImage.Remove(p.Slot), CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
}
p.Print(Localizer["wp_skin_menu_select", selectedSkin]);
Expand All @@ -250,7 +262,8 @@ private void SetupSkinsMenu()
};
if (g_bCommandsAllowed && (LifeState_t)p.LifeState == LifeState_t.LIFE_ALIVE)
RefreshWeapons(p);
AddTimer(0.15f, () => RefreshWeapons(p), CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
if (!Config.GlobalShare)
{
Expand Down Expand Up @@ -309,5 +322,89 @@ private void SetupSkinsMenu()
}
});
}

private void SetupGlovesMenu()
{
var glovesSelectionMenu = new ChatMenu(Localizer["wp_glove_menu_title"]);

var handleGloveSelection = (CCSPlayerController? player, ChatMenuOption option) =>
{
if (!Utility.IsPlayerValid(player)) return;
uint playerIndex = player!.Index;
string selectedPaintName = option.Text;
var selectedGlove = glovesList.FirstOrDefault(g => g.ContainsKey("paint_name") && g["paint_name"]?.ToString() == selectedPaintName);
if (selectedGlove != null)
{
if (
selectedGlove != null &&
selectedGlove.ContainsKey("weapon_defindex") &&
selectedGlove.ContainsKey("paint") &&
int.TryParse(selectedGlove["weapon_defindex"]?.ToString(), out int weaponDefindex) &&
int.TryParse(selectedGlove["paint"]?.ToString(), out int paint)
)
{
if (Config.Additional.ShowSkinImage)
{
string image = selectedGlove["image"]?.ToString() ?? "";
PlayerWeaponImage[player.Slot] = image;
AddTimer(2.0f, () => PlayerWeaponImage.Remove(player.Slot), CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
}
PlayerInfo playerInfo = new PlayerInfo
{
UserId = player.UserId,
Index = (int)player.Index,
SteamId = player.SteamID.ToString(),
Name = player.PlayerName,
IpAddress = player.IpAddress?.Split(":")[0]
};
if (paint != 0)
g_playersGlove[playerIndex] = ((ushort)weaponDefindex, paint);
else
g_playersGlove.TryRemove(playerIndex, out _);
if (!string.IsNullOrEmpty(Localizer["wp_glove_menu_select"]))
{
player!.Print(Localizer["wp_glove_menu_select", selectedPaintName]);
}
_ = weaponSync?.SyncGloveToDatabase(playerInfo, (ushort)weaponDefindex, paint) ?? Task.CompletedTask;
}
};
};

// Add weapon options to the weapon selection menu
foreach (var gloveObject in glovesList)
{
string paintName = gloveObject["paint_name"]?.ToString() ?? "";

if (paintName.Length > 0)
glovesSelectionMenu.AddMenuOption(paintName, handleGloveSelection);
}

// Command to open the weapon selection menu for players
AddCommand($"css_{Config.Additional.CommandGlove}", "Gloves selection menu", (player, info) =>
{
if (!Utility.IsPlayerValid(player)) return;
if (player == null || player.UserId == null) return;
if (!commandsCooldown.TryGetValue((int)player.UserId, out DateTime cooldownEndTime) ||
DateTime.UtcNow >= (commandsCooldown.TryGetValue((int)player.UserId, out cooldownEndTime) ? cooldownEndTime : DateTime.UtcNow))
{
commandsCooldown[(int)player.UserId] = DateTime.UtcNow.AddSeconds(Config.CmdRefreshCooldownSeconds);
glovesSelectionMenu.PostSelectAction = PostSelectAction.Close;
MenuManager.OpenChatMenu(player, glovesSelectionMenu);
return;
}
if (!string.IsNullOrEmpty(Localizer["wp_command_cooldown"]))
{
player!.Print(Localizer["wp_command_cooldown"]);
}
});
}
}
}
6 changes: 6 additions & 0 deletions Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class Additional
[JsonPropertyName("KnifeEnabled")]
public bool KnifeEnabled { get; set; } = true;

[JsonPropertyName("GloveEnabled")]
public bool GloveEnabled { get; set; } = true;

[JsonPropertyName("SkinEnabled")]
public bool SkinEnabled { get; set; } = true;

Expand All @@ -23,6 +26,9 @@ public class Additional
[JsonPropertyName("CommandKnife")]
public string CommandKnife { get; set; } = "knife";

[JsonPropertyName("CommandGlove")]
public string CommandGlove { get; set; } = "gloves";

[JsonPropertyName("CommandSkin")]
public string CommandSkin { get; set; } = "ws";

Expand Down
48 changes: 33 additions & 15 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private void OnClientPutInServer(int playerSlot)
CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot);

if (player is null || !player.IsValid || player.IsBot || player.IsHLTV || player.SteamID.ToString().Length != 17 ||
weaponSync == null || player.Connected == PlayerConnectedState.PlayerDisconnecting) return;
weaponSync == null) return;

PlayerInfo playerInfo = new PlayerInfo
{
Expand All @@ -21,17 +21,16 @@ private void OnClientPutInServer(int playerSlot)
IpAddress = player.IpAddress?.Split(":")[0]
};

if (!gPlayerWeaponsInfo.ContainsKey((int)player.Index))
Task.Run(async () =>
{
_ = Task.Run(async () =>
{
if (Config.Additional.SkinEnabled)
await weaponSync.GetWeaponPaintsFromDatabase(playerInfo);
if (Config.Additional.SkinEnabled)
await weaponSync.GetWeaponPaintsFromDatabase(playerInfo);
if (Config.Additional.KnifeEnabled)
await weaponSync.GetKnifeFromDatabase(playerInfo);
});
}
if (Config.Additional.KnifeEnabled)
await weaponSync.GetKnifeFromDatabase(playerInfo);
if (Config.Additional.GloveEnabled)
await weaponSync.GetGloveFromDatabase(playerInfo);
});
}

private void OnClientDisconnect(int playerSlot)
Expand All @@ -42,6 +41,8 @@ private void OnClientDisconnect(int playerSlot)

if (Config.Additional.KnifeEnabled)
g_playersKnife.TryRemove((int)player.Index, out _);
if (Config.Additional.GloveEnabled)
g_playersGlove.TryRemove(player.Index, out _);

if (Config.Additional.SkinEnabled && gPlayerWeaponsInfo.TryGetValue((int)player.Index, out var innerDictionary))
{
Expand Down Expand Up @@ -118,10 +119,9 @@ public HookResult OnPickup(CEntityIOOutput output, string name, CEntityInstance

if (Config.Additional.GiveKnifeAfterRemove)
{
AddTimer(0.37f, () =>
AddTimer(0.10f, () =>
{
player.RemoveItemByDesignerName(weapon.DesignerName, true);
GiveKnifeToPlayer(player);
RefreshWeapons(player);
});
}
}
Expand All @@ -131,7 +131,7 @@ public HookResult OnPickup(CEntityIOOutput output, string name, CEntityInstance

private void OnMapStart(string mapName)
{
if (!Config.Additional.KnifeEnabled && !Config.Additional.SkinEnabled) return;
if (!Config.Additional.KnifeEnabled && !Config.Additional.SkinEnabled && !Config.Additional.GloveEnabled) return;

if (_database != null)
weaponSync = new WeaponSynchronization(_database, Config, GlobalShareApi, GlobalShareServerId);
Expand All @@ -153,9 +153,27 @@ private HookResult OnPlayerSpawn(EventPlayerSpawn @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;

if (player is null || !player.IsValid || !Config.Additional.KnifeEnabled)
if (player is null || !player.IsValid || !Config.Additional.KnifeEnabled && !Config.Additional.GloveEnabled)
return HookResult.Continue;

if (g_playersGlove.TryGetValue(player.Index, out var gloveInfo) && gloveInfo.Paint != 0)
{
player.PlayerPawn!.Value!.EconGloves.ItemDefinitionIndex = gloveInfo.Definition;
player.PlayerPawn!.Value!.EconGloves.ItemIDLow = 16384 & 0xFFFFFFFF;
player.PlayerPawn!.Value!.EconGloves.ItemIDHigh = 16384 >> 32;
player.PlayerPawn!.Value!.EconGloves.EntityQuality = 3;
player.PlayerPawn!.Value!.EconGloves.EntityLevel = 1;

Server.NextFrame(() =>
{
player.PlayerPawn!.Value!.EconGloves.Initialized = true;
SetPlayerBody(player, "default_gloves", 1);
SetOrAddAttributeValueByName(player.PlayerPawn!.Value!.EconGloves.NetworkedDynamicAttributes, "set item texture prefab", gloveInfo.Paint);
Utilities.SetStateChanged(player.PlayerPawn.Value, "CCSPlayerPawn", "m_EconGloves");
});
}

g_knifePickupCount[(int)player.Index] = 0;
GiveKnifeToPlayer(player);

Expand Down
24 changes: 22 additions & 2 deletions Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ internal static async Task CheckDatabaseTables()
`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 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci",
) ENGINE=InnoDB",
@"CREATE TABLE IF NOT EXISTS `wp_player_knife` (
`steamid` varchar(64) NOT NULL,
`knife` varchar(64) NOT NULL,
UNIQUE (`steamid`)
) ENGINE = InnoDB"
) ENGINE = InnoDB",
@"CREATE TABLE IF NOT EXISTS `wp_player_gloves` (
`steamid` varchar(64) NOT NULL,
`weapon_defindex` int(11) NOT NULL,
`paint` int(11) NOT NULL,
UNIQUE (`steamid`)
) ENGINE=InnoDB"
};

foreach (var query in createTableQueries)
Expand Down Expand Up @@ -98,6 +104,20 @@ internal static void LoadSkinsFromFile(string filePath)
}
}

internal static void LoadGlovesFromFile(string filePath)
{
try
{
string json = File.ReadAllText(filePath);
var deserializedSkins = JsonConvert.DeserializeObject<List<JObject>>(json);
WeaponPaints.glovesList = deserializedSkins ?? new List<JObject>();
}
catch (FileNotFoundException)
{
throw;
}
}

internal static void Log(string message)
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7a
1.8a
20 changes: 16 additions & 4 deletions WeaponAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,10 @@ internal void RefreshWeapons(CCSPlayerController? player)
}

player.RemoveWeapons();
AddTimer(0.2f, () =>
AddTimer(0.35f, () =>
{
GiveKnifeToPlayer(player);
if (bomb)
player.GiveNamedItem("weapon_c4");
Expand All @@ -286,8 +288,6 @@ internal void RefreshWeapons(CCSPlayerController? player)
if (healthshot)
player.GiveNamedItem("weapon_healtshot");
GiveKnifeToPlayer(player);
foreach (var entry in weaponsWithAmmo)
{
foreach (var ammo in entry.Value)
Expand All @@ -310,7 +310,7 @@ internal void RefreshWeapons(CCSPlayerController? player)
});
}
}
});
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
}
}

Expand Down Expand Up @@ -375,6 +375,18 @@ private static CSkeletonInstance GetSkeletonInstance(CGameSceneNode node)
return new CSkeletonInstance(GetSkeletonInstance(node.Handle));
}

public void SetOrAddAttributeValueByName(CAttributeList attr, string name, float f)
{
var SetAttr = VirtualFunction.Create<IntPtr, string, float, int>("\\x55\\x48\\x89\\xE5\\x41\\x57\\x41\\x56\\x49\\x89\\xFE\\x41\\x55\\x41\\x54\\x49\\x89\\xF4\\x53\\x48\\x83\\xEC\\x78");
SetAttr(attr.Handle, name, f);
}

public void SetPlayerBody(CCSPlayerController player, string model, int i)
{
var SetBody = VirtualFunction.Create<IntPtr, string, int, int>("\\x55\\x48\\x89\\xE5\\x41\\x56\\x49\\x89\\xF6\\x41\\x55\\x41\\x89\\xD5\\x41\\x54\\x49\\x89\\xFC\\x48\\x83\\xEC\\x08");
SetBody(player.PlayerPawn.Value!.Handle, model, i);
}

private static unsafe CHandle<CBaseViewModel>[]? GetPlayerViewModels(CCSPlayerController player)
{
if (player.PlayerPawn.Value == null || player.PlayerPawn.Value.ViewModelServices == null) return null;
Expand Down
Loading

0 comments on commit 63f4b4c

Please sign in to comment.