Skip to content

Commit

Permalink
CoreConfig implementation on the managed side (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Wilson <[email protected]>
  • Loading branch information
KillStr3aK and roflmuffin authored Nov 12, 2023
1 parent fe23680 commit 18e9e37
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 0 deletions.
5 changes: 5 additions & 0 deletions configs/addons/counterstrikesharp/configs/core.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"PublicChatTrigger": "!",
"SilentChatTrigger": "/",
"FollowCS2ServerGuidelines": true
}
26 changes: 26 additions & 0 deletions docs/src/content/docs/reference/core-configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: Core Configuration
description: Summary for core configuration values
---

## PublicChatTrigger

List of characters to use for public chat triggers.

## SilentChatTrigger

List of characters to use for silent chat triggers.

## FollowCS2ServerGuidelines

Per [CS2 Server Guidelines](https://blog.counter-strike.net/index.php/server_guidelines/), certain plugin
functionality will trigger all of the game server owner's Game Server Login Tokens
(GSLTs) to get banned when executed on a Counter-Strike 2 game server.

Enabling this option will block plugins from using functionality that is known to cause this.
This option only has any effect on CS2. Note that this does NOT guarantee that you cannot
receive a ban.

:::note
Disable this option at your own risk.
:::
118 changes: 118 additions & 0 deletions managed/CounterStrikeSharp.API/Core/CoreConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* This file is part of CounterStrikeSharp.
* CounterStrikeSharp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CounterStrikeSharp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
*/

using System;
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;

namespace CounterStrikeSharp.API.Core
{
/// <summary>
/// Serializable instance of the CoreConfig
/// </summary>
internal sealed partial class CoreConfigData
{
[JsonPropertyName("PublicChatTrigger")] public string PublicChatTrigger { get; internal set; } = "!";

[JsonPropertyName("SilentChatTrigger")] public string SilentChatTrigger { get; internal set; } = "/";

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

/// <summary>
/// Configuration related to the Core API.
/// </summary>
public static partial class CoreConfig
{
/// <summary>
/// List of characters to use for public chat triggers.
/// </summary>
public static string PublicChatTrigger => _coreConfig.PublicChatTrigger;

/// <summary>
/// List of characters to use for silent chat triggers.
/// </summary>
public static string SilentChatTrigger => _coreConfig.SilentChatTrigger;

/// <summary>
/// <para>
/// Per <see href="http://blog.counter-strike.net/index.php/server_guidelines/"/>, certain plugin
/// functionality will trigger all of the game server owner's Game Server Login Tokens
/// (GSLTs) to get banned when executed on a Counter-Strike 2 game server.
/// </para>
///
/// <para>
/// Enabling this option will block plugins from using functionality that is known to cause this.
/// Note that this does NOT guarantee that you cannot
/// receive a ban.
/// </para>
///
/// <para>
/// Disable this option at your own risk.
/// </para>
/// </summary>
public static bool FollowCS2ServerGuidelines => _coreConfig.FollowCS2ServerGuidelines;
}

public static partial class CoreConfig
{
private static CoreConfigData _coreConfig = new CoreConfigData();

static CoreConfig()
{
CommandUtils.AddStandaloneCommand("css_core_reload", "Reloads the core configuration file.", ReloadCoreConfigCommand);
}

[RequiresPermissions("@css/config")]
[CommandHelper(whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
private static void ReloadCoreConfigCommand(CCSPlayerController? player, CommandInfo command)
{
var rootDir = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.Parent;
Load(Path.Combine(rootDir.FullName, "configs", "core.json"));
}

public static void Load(string coreConfigPath)
{
if (!File.Exists(coreConfigPath))
{
Console.WriteLine($"Core configuration could not be found at path '{coreConfigPath}', fallback values will be used.");
return;
}

try
{
var data = JsonSerializer.Deserialize<CoreConfigData>(File.ReadAllText(coreConfigPath), new JsonSerializerOptions() { ReadCommentHandling = JsonCommentHandling.Skip });

if (data != null)
{
_coreConfig = data;
}

Console.WriteLine($"Loaded core configuration");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load core configuration: {ex}, fallback values will be used.");
}
}
}
}
3 changes: 3 additions & 0 deletions managed/CounterStrikeSharp.API/Core/GlobalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public void OnNativeUnload()
}
public void InitGlobalContext()
{
Console.WriteLine("Loading CoreConfig from \"configs/core.json\"");
CoreConfig.Load(Path.Combine(rootDir.FullName, "configs", "core.json"));

Console.WriteLine("Loading GameData from \"gamedata/gamedata.json\"");
GameData.Load(Path.Combine(rootDir.FullName, "gamedata", "gamedata.json"));

Expand Down
50 changes: 50 additions & 0 deletions managed/CounterStrikeSharp.API/Modules/Memory/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,53 @@ public class Schema
{
private static Dictionary<Tuple<string, string>, short> _schemaOffsets = new();

private static HashSet<string> _cs2BadList = new HashSet<string>()
{
"m_bIsValveDS",
"m_bIsQuestEligible",
// "m_iItemDefinitionIndex", // as of 2023.11.11 this is currently not blocked
"m_iEntityLevel",
"m_iItemIDHigh",
"m_iItemIDLow",
"m_iAccountID",
"m_iEntityQuality",

"m_bInitialized",
"m_szCustomName",
"m_iAttributeDefinitionIndex",
"m_iRawValue32",
"m_iRawInitialValue32",
"m_flValue", // MNetworkAlias "m_iRawValue32"
"m_flInitialValue", // MNetworkAlias "m_iRawInitialValue32"
"m_bSetBonus",
"m_nRefundableCurrency",

"m_OriginalOwnerXuidLow",
"m_OriginalOwnerXuidHigh",

"m_nFallbackPaintKit",
"m_nFallbackSeed",
"m_flFallbackWear",
"m_nFallbackStatTrak",

"m_iCompetitiveWins",
"m_iCompetitiveRanking",
"m_iCompetitiveRankType",
"m_iCompetitiveRankingPredicted_Win",
"m_iCompetitiveRankingPredicted_Loss",
"m_iCompetitiveRankingPredicted_Tie",

"m_nActiveCoinRank",
"m_nMusicID",
};

public static short GetSchemaOffset(string className, string propertyName)
{
if (CoreConfig.FollowCS2ServerGuidelines && _cs2BadList.Contains(propertyName))
{
throw new Exception($"Cannot set or get '{className}::{propertyName}' with \"FollowCS2ServerGuidelines\" option enabled.");
}

var key = new Tuple<string, string>(className, propertyName);
if (!_schemaOffsets.TryGetValue(key, out var offset))
{
Expand All @@ -29,6 +74,11 @@ public static T GetSchemaValue<T>(IntPtr handle, string className, string proper

public static void SetSchemaValue<T>(IntPtr handle, string className, string propertyName, T value)
{
if (CoreConfig.FollowCS2ServerGuidelines && _cs2BadList.Contains(propertyName))
{
throw new Exception($"Cannot set or get '{className}::{propertyName}' with \"FollowCS2ServerGuidelines\" option enabled.");
}

NativeAPI.SetSchemaValueByName<T>(handle, (int)typeof(T).ToDataType(), className, propertyName, value);
}

Expand Down

0 comments on commit 18e9e37

Please sign in to comment.