Skip to content

Commit

Permalink
Add support for inspecting keychains
Browse files Browse the repository at this point in the history
  • Loading branch information
Citrinate committed Dec 24, 2024
1 parent 77f4e29 commit 6f9dd76
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions CS2Interface/GameData/GameObjects/Items/InspectItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ internal InspectItem(CMsgGCCStrike15_v2_Client2GCEconPreviewDataBlockResponse it
PaintIndex = ItemInfo.paintindex;
StickerID = ItemInfo.stickers.FirstOrDefault()?.sticker_id;
TintID = ItemInfo.stickers.FirstOrDefault()?.tint_id;
KeychainID = ItemInfo.keychains.FirstOrDefault()?.sticker_id;
Quality = ItemInfo.quality;
Rarity = ItemInfo.rarity;
Origin = ItemInfo.origin;
Expand Down
1 change: 1 addition & 0 deletions CS2Interface/GameData/GameObjects/Items/InventoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal InventoryItem(CSOEconItem item) {
StickerID = Attributes.GetValueOrDefault("sticker slot 0 id")?.ToUInt32();
TintID = Attributes.GetValueOrDefault("spray tint id")?.ToUInt32();
MusicID = Attributes.GetValueOrDefault("music id")?.ToUInt32();
KeychainID = Attributes.GetValueOrDefault("keychain slot 0 id")?.ToUInt32();
if (Attributes.GetValueOrDefault("set item texture wear") != null) {
Wear = (double) BitConverter.UInt32BitsToSingle(BitConverter.SingleToUInt32Bits(Attributes.GetValueOrDefault("set item texture wear")!.ToSingle()));
}
Expand Down
9 changes: 5 additions & 4 deletions CS2Interface/GameData/GameObjects/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class Item : GameObject {
public uint? StickerID;
public uint? TintID;
public uint? MusicID;
public uint? KeychainID;
public uint Quality;
public uint Rarity;
public uint Origin;
Expand Down Expand Up @@ -164,7 +165,7 @@ protected override bool SetAdditionalProperties() {

// Set the item name, which will be something like: what kind of sticker it is, or the name of the weapon skin, or the type of pin/coin
// If an item has a wear value, but uses the default paint_kit (vanilla knives for example), this will be "-"
ItemName = GameData.CsgoEnglish[(ItemData.MusicDef?["loc_name"].Value ?? ItemData.StickerKitDef?["item_name"].Value ?? ItemData.PaintKitDef?["description_tag"].Value ?? ItemData.ItemDef["item_name"].Value)?.Substring(1)];
ItemName = GameData.CsgoEnglish[(ItemData.KeychainDef?["loc_name"].Value ?? ItemData.MusicDef?["loc_name"].Value ?? ItemData.StickerKitDef?["item_name"].Value ?? ItemData.PaintKitDef?["description_tag"].Value ?? ItemData.ItemDef["item_name"].Value)?.Substring(1)];

// Set the tool named, used for various things like differentiating between Graffiti and Sealed Graffiti
if (ItemData.ItemDef["prefab"].Value == "csgo_tool") {
Expand Down Expand Up @@ -205,7 +206,7 @@ protected override bool SetAdditionalProperties() {
FullTypeName = String.Format("{0} {1} {2}", displayQualityName, RarityName, TypeName).Trim();
// FullTypeName = String.Format(GameData.CsgoEnglish.Format("ItemTypeDescKillEater") ?? "{0} {1} {2}", displayQualityName, RarityName, TypeName).Trim();

if (PaintIndex == 0 && ItemData.StickerKitDef == null && ItemData.MusicDef == null) {
if (PaintIndex == 0 && ItemData.StickerKitDef == null && ItemData.MusicDef == null && ItemData.KeychainDef == null) {
FullName = String.Format("{0} {1}", displayQualityName, ToolName ?? WeaponName ?? ItemName).Trim(); // Collectibles (Pins, Coins), Vanilla Knives
} else if (WearName != null || TintName != null) {
FullName = String.Format("{0} {1} | {2} ({3})", displayQualityName, WeaponName ?? ToolName ?? TypeName, ItemName, WearName ?? TintName).Trim(); // Weapon Skins, Gloves, Graffiti
Expand All @@ -217,10 +218,10 @@ protected override bool SetAdditionalProperties() {
}

// Set the name id, used for determining related set and crate
if (PaintIndex == 0 && ItemData.StickerKitDef == null && ItemData.MusicDef == null) {
if (PaintIndex == 0 && ItemData.StickerKitDef == null && ItemData.MusicDef == null && ItemData.KeychainDef == null) {
NameID = ItemData.ItemDef["name"].Value; // Collectibles, Vanilla Knives
} else {
NameID = String.Format("[{0}]{1}", (ItemData.MusicDef ?? ItemData.StickerKitDef ?? ItemData.PaintKitDef)?["name"].Value, ItemData.ItemDef["name"].Value); // Everything else
NameID = String.Format("[{0}]{1}", (ItemData.KeychainDef ?? ItemData.MusicDef ?? ItemData.StickerKitDef ?? ItemData.PaintKitDef)?["name"].Value, ItemData.ItemDef["name"].Value); // Everything else
}

if (NameID != null) {
Expand Down
26 changes: 26 additions & 0 deletions CS2Interface/GameData/GameObjects/Items/ItemData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@ public class ItemData {
[JsonConverter(typeof(KVConverter))]
public KeyValue? MusicDef { get; private init; }

[JsonInclude]
[JsonPropertyName("keychain_def")]
[JsonConverter(typeof(KVConverter))]
public KeyValue? KeychainDef { get; private init; }

public bool ShouldSerializeItemDef() => ItemDef != null;
public bool ShouldSerializePaintKitDef() => PaintKitDef != null;
public bool ShouldSerializeStickerKitDef() => StickerKitDef != null;
public bool ShouldSerializeMusicDef() => MusicDef != null;
public bool ShouldSerializeKeychainDefDef() => KeychainDef != null;

internal ItemData(Item item) {
ItemDef = CreateItemDef(item);
PaintKitDef = CreatePaintKitDef(item);
StickerKitDef = CreateStickerKitDef(item);
MusicDef = CreateMusicDef(item);
KeychainDef = CreateKeychainDef(item);
}

private KeyValue CreateItemDef(Item item) {
Expand Down Expand Up @@ -160,5 +167,24 @@ private bool MergePrefab(KeyValue itemDef, string? prefab) {

return musicDef.Clone();
}


private KeyValue? CreateKeychainDef(Item item) {
if (item.KeychainID == null
|| !(
item.DefIndex == 1355 // Charm
)
) {
// This item has no keychain definition
return null;
}

KeyValue? keychainDef = GameData.ItemsGame.GetDef("keychain_definitions", item.KeychainID.ToString()!);
if (keychainDef == null) {
throw new Exception();
}

return keychainDef.Clone();
}
}
}

0 comments on commit 6f9dd76

Please sign in to comment.