Skip to content

Commit

Permalink
Data problem fix
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarper committed Oct 23, 2024
1 parent 5c51f2d commit dbc7254
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 38 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.vs/
**/obj
**/bin
**/bin

# Ignore everything in BuildOutput directory
BuildOutput/
12 changes: 12 additions & 0 deletions TagsApi/TagsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ public class Tag
public string ChatColor { get; set; } = string.Empty;
public string NameColor { get; set; } = string.Empty;
public bool ChatSound { get; set; } = true;

public Tag Clone()
{
return new Tag
{
ScoreTag = ScoreTag,
ChatTag = ChatTag,
ChatColor = ChatColor,
NameColor = NameColor,
ChatSound = ChatSound
};
}
}

public enum Tags_Tags
Expand Down
2 changes: 2 additions & 0 deletions TagsApi/TagsApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputPath>$(ProjectDir)..\BuildOutput\shared\TagsApi\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

<ItemGroup>
Expand Down
Binary file added Tomlyn.dll
Binary file not shown.
26 changes: 26 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[Settings]
DeadName = ""
NoneName = "{White}(NONE)"
SpecName = "{Purple}(SPEC)"
TName = "{Yellow}(T)"
CTName = "{Blue}(CT)"

[Default]
ScoreTag = ""
ChatTag = "{Grey}[Player]"
ChatColor = "{White}"
NameColor = "{TeamColor}"
ChatSound = true

[76561199165718810]
ScoreTag = ""
ChatTag = "{Red}[schwarper] "
ChatColor = "{green}"
NameColor = "{TeamColor}"
ChatSound = false

["#OWNER"]
ScoreTag = ""
ChatTag = "{Red}[OWNER] "
ChatColor = "{green}"
NameColor = "{TeamColor}"
18 changes: 18 additions & 0 deletions cs2-tags/cs2-tags.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputPath>$(ProjectDir)..\BuildOutput\plugins\cs2-tags\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

<ItemGroup>
Expand All @@ -14,4 +16,20 @@
<ItemGroup>
<ProjectReference Include="..\TagsApi\TagsApi.csproj" />
</ItemGroup>

<Target Name="ExcludeTagsApiFiles" AfterTargets="Build">
<Delete Files="$(OutputPath)TagsApi.dll" />
<Delete Files="$(OutputPath)TagsApi.pdb" />
</Target>

<Target Name="CopyConfigFiles" AfterTargets="Build">
<ItemGroup>
<FilesToCopy Include="$(ProjectDir)..\config.toml" />
</ItemGroup>
<Copy SourceFiles="@(FilesToCopy)" DestinationFolder="$(ProjectDir)..\BuildOutput\configs\plugins\cs2-tags\" />
</Target>

<Target Name="CopyNuGetAssemblies" AfterTargets="Build">
<Copy SourceFiles="../Tomlyn.dll" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true" />
</Target>
</Project>
41 changes: 18 additions & 23 deletions cs2-tags/src/config/config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,26 @@ public class Cfg
}

public static Cfg Config { get; set; } = new Cfg();
private static string? ConfigPath;
private static readonly string ConfigPath;

public static void Load()
static Config_Config()
{
if (ConfigPath == null)
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? string.Empty;

ConfigPath = Path.Combine(Server.GameDirectory,
"csgo",
"addons",
"counterstrikesharp",
"configs",
"plugins",
assemblyName,
"config.toml"
);

if (!File.Exists(ConfigPath))
{
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? string.Empty;

ConfigPath = Path.Combine(Server.GameDirectory,
"csgo",
"addons",
"counterstrikesharp",
"configs",
"plugins",
assemblyName,
"config.toml"
);

if (!File.Exists(ConfigPath))
{
throw new FileNotFoundException($"Configuration file not found: {ConfigPath}");
}
throw new FileNotFoundException($"Configuration file not found: {ConfigPath}");
}

LoadConfig(ConfigPath);
}

public static void Reload()
Expand All @@ -73,9 +68,9 @@ public static void LoadPlayersTag()
}
}

private static void LoadConfig(string configPath)
public static void Load()
{
string configText = File.ReadAllText(configPath);
string configText = File.ReadAllText(ConfigPath);
TomlTable model = Toml.ToModel(configText);

TomlTable table = (TomlTable)model["Settings"];
Expand Down
19 changes: 16 additions & 3 deletions cs2-tags/src/cs2-tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Tags;
public partial class Tags : BasePlugin
{
public override string ModuleName => "Tags";
public override string ModuleVersion => "0.0.9";
public override string ModuleVersion => "1.0";
public override string ModuleAuthor => "schwarper";

public static ConcurrentDictionary<ulong, Tag> PlayerTagsList { get; set; } = [];
Expand All @@ -29,9 +29,11 @@ public override void Load(bool hotReload)
Api = new TagsAPI();
Capabilities.RegisterPluginCapability(ITagApi.Capability, () => Api);

AddCommandListener("css_admins_reload", AdminsReload, HookMode.Pre);

HookUserMessage(118, OnMessage, HookMode.Pre);

AddTimer(5.0f, UpdateTags, TimerFlags.REPEAT);
AddTimer(10.0f, UpdateTags, TimerFlags.REPEAT);

if (hotReload)
{
Expand All @@ -43,6 +45,17 @@ public override void Load(bool hotReload)
}
}

public override void Unload(bool hotReload)
{
RemoveCommandListener("css_admins_reload", AdminsReload, HookMode.Pre);
}

public static HookResult AdminsReload(CCSPlayerController? player, CommandInfo info)
{
Reload();
return HookResult.Continue;
}

[GameEventHandler]
public HookResult OnPlayerConnect(EventPlayerConnectFull @event, GameEventInfo info)
{
Expand Down Expand Up @@ -155,7 +168,7 @@ public static void UpdateTags()

foreach ((ulong steamid, Tag tag) in PlayerTagsList)
{
if (players.FirstOrDefault(p => p.SteamID == steamid) is not CCSPlayerController player)
if (players.SingleOrDefault(p => p.SteamID == steamid) is not CCSPlayerController player)
{
continue;
}
Expand Down
19 changes: 8 additions & 11 deletions cs2-tags/src/library/library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public static partial class TagsLibrary
{
public static void LoadTag(this CCSPlayerController player)
{
PlayerTagsList.TryAdd(player.SteamID, player.GetTag());
var tag = player.GetTag().Clone();
PlayerTagsList.TryAdd(player.SteamID, tag);
}
public static Tag GetTag(this CCSPlayerController player)
{
Expand All @@ -27,21 +28,17 @@ public static Tag GetTag(this CCSPlayerController player)
return steamidTag;
}

foreach (KeyValuePair<string, Tag> tag in tags.Where(tag => tag.Key.StartsWith('#')))
foreach (var tag in tags)
{
bool isInGroup = AdminManager.PlayerInGroup(player, tag.Key);

if (isInGroup)
if (tag.Key[0] == '#' && AdminManager.PlayerInGroup(player, tag.Key))
{
return tag.Value;
}
}

foreach (KeyValuePair<string, Tag> tag in tags.Where(tag => tag.Key.StartsWith('@')))
foreach (var tag in tags)
{
bool hasPermission = AdminManager.PlayerHasPermissions(player, tag.Key);

if (hasPermission)
if (tag.Key[0] == '@' && AdminManager.PlayerHasPermissions(player, tag.Key))
{
return tag.Value;
}
Expand Down Expand Up @@ -84,7 +81,7 @@ public static void ResetTag(this CCSPlayerController player, Tags_Tags tag)
{
if (PlayerTagsList.TryGetValue(player.SteamID, out var playerData))
{
Tag defaultTag = GetTag(player);
Tag defaultTag = GetTag(player).Clone();
switch (tag)
{
case Tags_Tags.ScoreTag:
Expand Down Expand Up @@ -132,7 +129,7 @@ public static void ResetColor(this CCSPlayerController player, Tags_Colors color
{
if (PlayerTagsList.TryGetValue(player.SteamID, out var playerData))
{
Tag defaultTag = GetTag(player);
Tag defaultTag = GetTag(player).Clone();
switch (color)
{
case Tags_Colors.NameColor:
Expand Down

0 comments on commit dbc7254

Please sign in to comment.