Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion CS2-GameHUD/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,46 @@ public void Native_GameHUD_ShowPermanent(CCSPlayerController Player, byte channe
if (!Player.IsValid || channel < 0 || channel >= GameHUD.MAXHUDCHANNELS) return;
GameHUD.g_HUD[Player.Slot].Channel[channel].ShowPermanent(message);
}
}

// 新增示例:用于设置 Owner、KeyValue、Target 等
// Added example: for setting Owner, KeyValue, Target, etc.
public void Native_GameHUD_SetOwner(CCSPlayerController Player, byte channel, CCSPlayerPawn owner)
{
if (!Player.IsValid || channel < 0 || channel >= GameHUD.MAXHUDCHANNELS) return;
GameHUD.g_HUD[Player.Slot].Channel[channel].SetOwner(owner);
}

public void Native_GameHUD_SetKeyValue(CCSPlayerController Player, byte channel, string key, string value)
{
if (!Player.IsValid || channel < 0 || channel >= GameHUD.MAXHUDCHANNELS) return;
GameHUD.g_HUD[Player.Slot].Channel[channel].SetKeyValue(key, value);
}

public void Native_GameHUD_SetTarget(CCSPlayerController Player, byte channel, string target)
{
if (!Player.IsValid || channel < 0 || channel >= GameHUD.MAXHUDCHANNELS) return;
GameHUD.g_HUD[Player.Slot].Channel[channel].SetTarget(target);
}

// Getters
// public CCSPlayerPawn? Native_GameHUD_GetOwner(CCSPlayerController Player, byte channel)
// {
// if (!Player.IsValid || channel < 0 || channel >= GameHUD.MAXHUDCHANNELS) return null;
// var hud = GameHUD.g_HUD[Player.Slot];
// var hudChannel = hud.Channel[channel];
// return hudChannel.GetOwner();
// }

public string? Native_GameHUD_GetKeyValue(CCSPlayerController Player, byte channel, string key)
{
if (!Player.IsValid || channel < 0 || channel >= GameHUD.MAXHUDCHANNELS) return null;
return GameHUD.g_HUD[Player.Slot].Channel[channel].GetKeyValue(key);
}

public string? Native_GameHUD_GetTarget(CCSPlayerController Player, byte channel)
{
if (!Player.IsValid || channel < 0 || channel >= GameHUD.MAXHUDCHANNELS) return null;
return GameHUD.g_HUD[Player.Slot].Channel[channel].GetTarget();
}
}
}
2 changes: 1 addition & 1 deletion CS2-GameHUD/CS2-GameHUD.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.316" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.319" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 24 additions & 2 deletions CS2-GameHUD/GameHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class GameHUD : BasePlugin
public static readonly int MAXHUDCHANNELS = 32;
public override string ModuleName => "GameHUD";
public override string ModuleDescription => "Shows text to the player using static point_worldtext";
public override string ModuleAuthor => "DarkerZ [RUS]";
public override string ModuleVersion => "1.DZ.0";
public override string ModuleAuthor => "DarkerZ [RUS], Oz_Lin";
public override string ModuleVersion => "1.DZ.1";

public static HUD[] g_HUD = new HUD[65];
static IGameHUDAPI? _api;
Expand Down Expand Up @@ -160,6 +160,28 @@ private static void UpdateEvent(CCSPlayerController? player)
});
}

// --- Getters for HUD API (for direct plugin use, not required for API interface) ---
public static CCSPlayerPawn? GetHUDOwner(int playerSlot, int channel)
{
if (playerSlot < 0 || playerSlot >= g_HUD.Length) return null;
if (channel < 0 || channel >= MAXHUDCHANNELS) return null;
return g_HUD[playerSlot].Channel[channel].GetOwner();
}

public static string? GetHUDKeyValue(int playerSlot, int channel, string key)
{
if (playerSlot < 0 || playerSlot >= g_HUD.Length) return null;
if (channel < 0 || channel >= MAXHUDCHANNELS) return null;
return g_HUD[playerSlot].Channel[channel].GetKeyValue(key);
}

public static string? GetHUDTarget(int playerSlot, int channel)
{
if (playerSlot < 0 || playerSlot >= g_HUD.Length) return null;
if (channel < 0 || channel >= MAXHUDCHANNELS) return null;
return g_HUD[playerSlot].Channel[channel].GetTarget();
}

public static void PrintToConsole(string sMessage)
{
Console.ForegroundColor = (ConsoleColor)8;
Expand Down
71 changes: 70 additions & 1 deletion CS2-GameHUD/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public class HUDChannel(int slot)
string Message = "";
readonly int PlayerSlot = slot;

// For getter support
CCSPlayerPawn? LastOwner = null;
string? LastTarget = null;
Dictionary<string, string> LastKeyValues = new();

~HUDChannel()
{
RemoveHUD();
Expand Down Expand Up @@ -244,6 +249,14 @@ public bool CreateHUD()
WorldText = entity;
WorldText.AcceptInput("SetMessage", null, null, Message);

// Restore last known owner, target, keyvalues if any
if (LastOwner != null)
SetOwner(LastOwner);
if (LastTarget != null)
SetTarget(LastTarget);
foreach (var kv in LastKeyValues)
SetKeyValue(kv.Key, kv.Value);

return true;
}

Expand Down Expand Up @@ -273,6 +286,10 @@ public void RemoveHUD()
ReorientMode = PointWorldTextReorientMode_t.POINT_WORLD_TEXT_REORIENT_NONE;
BackgroundBorderHeight = 0.0f;
BackgroundBorderWidth = 0.0f;

LastOwner = null;
LastTarget = null;
LastKeyValues.Clear();
}

public bool EmptyMessage()
Expand Down Expand Up @@ -320,5 +337,57 @@ void CloseTimer()
timer = null;
}
}
}

// Setter methods
public void SetOwner(CCSPlayerPawn owner)
{
if (!WTIsValid()) CreateHUD();
if (WTIsValid())
{
WorldText!.AcceptInput("SetOwner", owner);
LastOwner = owner;
}
}

public void SetKeyValue(string key, string value)
{
if (!WTIsValid()) CreateHUD();
if (WTIsValid())
{
// 输入格式: "KeyValue" <unused> <unused> <arguments>
// Input format: "KeyValue" <unused> <unused> <arguments>
WorldText!.AcceptInput("KeyValue", null, null, $"{key} {value}");
LastKeyValues[key] = value;
}
}

public void SetTarget(string target)
{
if (!WTIsValid()) CreateHUD();
if (WTIsValid())
{
WorldText!.AcceptInput("KeyValue", null, null, $"targetname {target}");
WorldText!.Target = target;
LastTarget = target;
}
}

// Getter methods
public CCSPlayerPawn? GetOwner()
{
return LastOwner;
}

public string? GetKeyValue(string key)
{
if (LastKeyValues.TryGetValue(key, out var value))
return value;
return null;
}

public string? GetTarget()
{
return LastTarget;
}
}
}
2 changes: 1 addition & 1 deletion CS2-GameHUDAPI/CS2-GameHUDAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.316" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.319" />
</ItemGroup>

</Project>
64 changes: 63 additions & 1 deletion CS2-GameHUDAPI/IGameHUDAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,67 @@ public interface IGameHUDAPI
* On error/errors: Invalid player, Invalid channel
*/
void Native_GameHUD_Remove(CCSPlayerController Player, byte channel);
}

/**
* Sets the owner of the HUD entity for a desired channel.
*
* @param Player CCSPlayerController for whom this request is made
* @param channel Channel number where the owner needs to be set
* @param owner The pawn to be assigned as the owner of the HUD entity
*
* On error/errors: Invalid player, Invalid channel
*/
void Native_GameHUD_SetOwner(CCSPlayerController Player, byte channel, CCSPlayerPawn owner);

/**
* Sets a key-value pair on the HUD entity, typically used for further customization.
*
* @param Player CCSPlayerController for whom this request is made
* @param channel Channel number on which the key-value pair is set
* @param key The key to be set
* @param value The value to assign to the key
*
* On error/errors: Invalid player, Invalid channel
*/
void Native_GameHUD_SetKeyValue(CCSPlayerController Player, byte channel, string key, string value);

/**
* Assigns a target name to the HUD entity, which can be retrieved by other scripts or services.
*
* @param Player CCSPlayerController for whom this request is made
* @param channel Channel number whose target should be updated
* @param target The new target name for the HUD entity
*
* On error/errors: Invalid player, Invalid channel
*/
void Native_GameHUD_SetTarget(CCSPlayerController Player, byte channel, string target);

/**
* Gets the owner of the HUD entity for a desired channel.
*
* @param Player CCSPlayerController for whom this request is made
* @param channel Channel number to query
* @return The pawn assigned as the owner, or null if not set
*/
// CCSPlayerPawn? Native_GameHUD_GetOwner(CCSPlayerController Player, byte channel);

/**
* Gets a key-value pair from the HUD entity.
*
* @param Player CCSPlayerController for whom this request is made
* @param channel Channel number to query
* @param key The key to retrieve
* @return The value assigned to the key, or null if not set
*/
string? Native_GameHUD_GetKeyValue(CCSPlayerController Player, byte channel, string key);

/**
* Gets the target name of the HUD entity.
*
* @param Player CCSPlayerController for whom this request is made
* @param channel Channel number to query
* @return The target name, or null if not set
*/
string? Native_GameHUD_GetTarget(CCSPlayerController Player, byte channel);
}
}
2 changes: 1 addition & 1 deletion CS2-GameHUDTest/CS2-GameHUDTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.316" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.319" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading