-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into virtual-hooks
- Loading branch information
Showing
8 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ on: | |
push: | ||
branches: | ||
- main | ||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: read | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,2 @@ | ||
- name: Core Configuration | ||
href: core-configuration.md | ||
|
||
- name: Referencing Players | ||
href: referencing-players.md | ||
href: core-configuration.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
managed/CounterStrikeSharp.API/Core/Model/CTakeDamageInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace CounterStrikeSharp.API.Core; | ||
|
||
public partial class CTakeDamageInfo | ||
{ | ||
/// <summary> | ||
/// Retrieves the hitgroup | ||
/// </summary> | ||
/// <returns> | ||
/// Returns a <see cref="HitGroup_t"/> enumeration representing the player's current hit group, | ||
/// or <see cref="HitGroup_t.HITGROUP_INVALID"/> if the hit group cannot be determined. | ||
/// </returns> | ||
public HitGroup_t GetHitGroup() | ||
{ | ||
IntPtr v4 = Marshal.ReadIntPtr(Handle, 0x78); | ||
|
||
if (v4 == nint.Zero) | ||
{ | ||
return HitGroup_t.HITGROUP_INVALID; | ||
} | ||
|
||
IntPtr v1 = Marshal.ReadIntPtr(v4, 16); | ||
|
||
if (v1 == nint.Zero) | ||
{ | ||
return HitGroup_t.HITGROUP_GENERIC; | ||
} | ||
|
||
return (HitGroup_t)Marshal.ReadInt32(v1, 56); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
managed/CounterStrikeSharp.API/Modules/Extensions/PluginConfigExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Text.Json; | ||
using System.Reflection; | ||
|
||
namespace CounterStrikeSharp.API.Modules.Extensions; | ||
|
||
public static class PluginConfigExtensions | ||
{ | ||
private static readonly JsonSerializerOptions _jsonSerializerOptions = new() | ||
{ | ||
WriteIndented = true | ||
}; | ||
|
||
public static JsonSerializerOptions JsonSerializerOptions => _jsonSerializerOptions; | ||
|
||
/// <summary> | ||
/// Gets the configuration file path | ||
/// </summary> | ||
/// <typeparam name="T">Type of the plugin configuration.</typeparam> | ||
/// <param name="_">Current configuration instance</param> | ||
public static string GetConfigPath<T>(this T _) where T : BasePluginConfig, new() | ||
{ | ||
string assemblyName = typeof(T).Assembly.GetName().Name ?? string.Empty; | ||
return Path.Combine(Server.GameDirectory, "csgo", "addons", "counterstrikesharp", "configs", "plugins", assemblyName, $"{assemblyName}.json"); | ||
} | ||
|
||
/// <summary> | ||
/// Updates the configuration file | ||
/// </summary> | ||
/// <typeparam name="T">Type of the plugin configuration.</typeparam> | ||
/// <param name="config">Current configuration instance</param> | ||
public static void Update<T>(this T config) where T : BasePluginConfig, new() | ||
{ | ||
var configPath = config.GetConfigPath(); | ||
|
||
try | ||
{ | ||
using var stream = new FileStream(configPath, FileMode.Create, FileAccess.Write, FileShare.None); | ||
using var writer = new StreamWriter(stream); | ||
writer.Write(JsonSerializer.Serialize(config, JsonSerializerOptions)); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new Exception($"Failed to update configuration file at '{configPath}'.", ex); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Reloads the configuration file and updates current configuration instance. | ||
/// </summary> | ||
/// <typeparam name="T">Type of the plugin configuration.</typeparam> | ||
/// <param name="config">Current configuration instance</param> | ||
public static void Reload<T>(this T config) where T : BasePluginConfig, new() | ||
{ | ||
var configPath = config.GetConfigPath(); | ||
|
||
try | ||
{ | ||
if (!File.Exists(configPath)) | ||
{ | ||
throw new FileNotFoundException($"Configuration file '{configPath} not found."); | ||
} | ||
|
||
var configContent = File.ReadAllText(configPath); | ||
|
||
var newConfig = JsonSerializer.Deserialize<T>(configContent) | ||
?? throw new JsonException($"Deserialization failed for configuration file '{configPath}'."); | ||
|
||
foreach (var property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)) | ||
{ | ||
if (property.CanWrite) | ||
{ | ||
property.SetValue(config, property.GetValue(newConfig)); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new Exception($"Failed to reload configuration file at '{configPath}'.", ex); | ||
} | ||
} | ||
} |