Skip to content

Commit 467c594

Browse files
committed
Initial commit
0 parents  commit 467c594

15 files changed

+838
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
name: Build & Publish
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
setup:
10+
permissions:
11+
contents: write
12+
runs-on: ubuntu-latest
13+
outputs:
14+
buildnumber: ${{ steps.buildnumber.outputs.build_number }}
15+
steps:
16+
- name: Generate build number
17+
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
18+
id: buildnumber
19+
uses: onyxmueller/build-tag-number@v1
20+
with:
21+
token: ${{ secrets.GITHUB_TOKEN }}
22+
23+
build:
24+
needs: setup
25+
runs-on: ubuntu-latest
26+
permissions:
27+
contents: write
28+
steps:
29+
- name: Prepare env
30+
shell: bash
31+
run: echo "GITHUB_SHA_SHORT=${GITHUB_SHA::7}" >> $GITHUB_ENV
32+
33+
- name: Fallback build number
34+
if: ${{ github.event_name == 'pull_request' || github.ref != 'refs/heads/master' }}
35+
shell: bash
36+
run: echo "BUILD_NUMBER=0" >> $GITHUB_ENV
37+
38+
- name: Master build number
39+
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
40+
run: echo "BUILD_NUMBER=${{ needs.setup.outputs.buildnumber }}" >> $GITHUB_ENV
41+
42+
- name: Checkout repository
43+
uses: actions/checkout@v3
44+
45+
- name: Build runtime v${{ env.BUILD_NUMBER }}
46+
uses: actions/setup-dotnet@v3
47+
with:
48+
dotnet-version: '8.0.x'
49+
50+
- name: Restore dependencies
51+
run: dotnet restore
52+
53+
- name: Publish artifacts
54+
run: |
55+
dotnet build -c Release --no-restore /p:Version=1.0.${{ env.BUILD_NUMBER }}
56+
dotnet pack -c Release --no-build --output ./nupkg /p:Version=1.0.${{ env.BUILD_NUMBER }}
57+
58+
- name: Check contents of bin/Release directory
59+
run: |
60+
echo "Listing contents of CSSharpUtils/bin/Release/net8.0/"
61+
ls -la CSSharpUtils/bin/Release/net8.0/
62+
63+
- name: Create a ZIP file with binaries
64+
run: |
65+
mkdir release
66+
cp -r CSSharpUtils/bin/Release/net8.0/* ./release/
67+
ls -la ./release
68+
cd release
69+
zip -r ../CSSharpUtils-v${{ env.BUILD_NUMBER }}-${{ env.GITHUB_SHA_SHORT }}.zip *
70+
71+
- name: Publish NuGet package
72+
run: dotnet nuget push ./nupkg/CSSharpUtils.1.0.${{ env.BUILD_NUMBER }}.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
73+
74+
- name: Create GitHub Release
75+
id: create_release
76+
uses: actions/create-release@v1
77+
env:
78+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
79+
with:
80+
tag_name: v${{ env.BUILD_NUMBER }}
81+
release_name: Release v${{ env.BUILD_NUMBER }}
82+
draft: false
83+
prerelease: false
84+
body: |
85+
## Changes
86+
- Auto-generated release
87+
${{ github.event.head_commit.message }}
88+
89+
- name: Upload Release Asset
90+
uses: actions/upload-release-asset@v1
91+
env:
92+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
93+
with:
94+
upload_url: ${{ steps.create_release.outputs.upload_url }}
95+
asset_path: ./CSSharpUtils-v${{ env.BUILD_NUMBER }}-${{ env.GITHUB_SHA_SHORT }}.zip
96+
asset_name: CSSharpUtils-v${{ env.BUILD_NUMBER }}-${{ env.GITHUB_SHA_SHORT }}.zip
97+
asset_content_type: application/zip

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
CSSharpUtils/bin
3+
CSSharpUtils/obj

CSSharpUtils.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSSharpUtils", "CSSharpUtils\CSSharpUtils.csproj", "{068A86ED-1AA3-4BFE-9530-5EDAF1D3F721}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{068A86ED-1AA3-4BFE-9530-5EDAF1D3F721}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{068A86ED-1AA3-4BFE-9530-5EDAF1D3F721}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{068A86ED-1AA3-4BFE-9530-5EDAF1D3F721}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{068A86ED-1AA3-4BFE-9530-5EDAF1D3F721}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

CSSharpUtils/CSSharpUtils.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<Title>CSSharpUtils</Title>
8+
<Authors>imi-tat0r</Authors>
9+
<Description>CSSharpUtils is an extension library for CounterStrikeSharp that simplifies player management, message formatting, configuration updates, and more.</Description>
10+
<Copyright>imi-tat0r</Copyright>
11+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.253" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using CounterStrikeSharp.API.Core;
2+
3+
namespace CSSharpUtils.Extensions;
4+
5+
public static class BasePluginExtensions
6+
{
7+
private static BasePlugin Instance { get; set; } = null!;
8+
public static BasePlugin PluginInstance => Instance ?? throw new Exception("BasePlugin instance is not initialized!. this.InitializeUtils() must be called in your plugin's Load() function.");
9+
10+
public static void InitializeUtils(this BasePlugin instance)
11+
{
12+
Instance = instance;
13+
}
14+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System.Reflection;
2+
using System.Text.Json;
3+
using CounterStrikeSharp.API;
4+
using CounterStrikeSharp.API.Core;
5+
6+
namespace CSSharpUtils.Extensions;
7+
8+
/// <summary>
9+
/// Provides extension methods for <see cref="BasePluginConfig"/>.
10+
/// </summary>
11+
public static class ConfigExtensions
12+
{
13+
// Holds the name of the executing assembly, used in constructing the configuration file path.
14+
private static readonly string AssemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? "";
15+
16+
// Defines the path to the configuration file, based on the game directory and assembly name.
17+
private static readonly string CfgPath =
18+
$"{Server.GameDirectory}/csgo/addons/counterstrikesharp/configs/plugins/{AssemblyName}/{AssemblyName}.json";
19+
20+
// Specifies the options for JSON serialization, including indentation for readability.
21+
private static readonly JsonSerializerOptions WriteSerializerOptions = new() { WriteIndented = true };
22+
23+
// Specifies the options for JSON deserialization.
24+
private static readonly JsonSerializerOptions ReadSerializerOptions = new() { ReadCommentHandling = JsonCommentHandling.Skip };
25+
26+
27+
/// <summary>
28+
/// Updates the version of the provided configuration object and serializes it back to JSON.
29+
/// This method ensures that the configuration file reflects the most recent version,
30+
/// including all properties of the configuration object, even those not initially set.
31+
/// </summary>
32+
/// <typeparam name="T">The type of the configuration object, must inherit from BasePluginConfig.</typeparam>
33+
/// <param name="config">The configuration object to update and serialize.</param>
34+
public static void Update<T>(this T config) where T : BasePluginConfig, new()
35+
{
36+
// get newest config version
37+
var newCfgVersion = new T().Version;
38+
39+
// loaded config is up-to-date
40+
if (config.Version == newCfgVersion)
41+
return;
42+
43+
// update the version
44+
config.Version = newCfgVersion;
45+
46+
// serialize the updated config back to json
47+
var updatedJsonContent = JsonSerializer.Serialize(config, WriteSerializerOptions);
48+
File.WriteAllText(CfgPath, updatedJsonContent);
49+
}
50+
51+
/// <summary>
52+
/// Reloads the configuration from disk, deserializing it back into the configuration object.
53+
/// </summary>
54+
/// <typeparam name="T">The type of the configuration object, must inherit from BasePluginConfig.</typeparam>
55+
/// <param name="config">The configuration object to reload.</param>
56+
/// <returns>The reloaded configuration object.</returns>
57+
/// <remarks>
58+
/// You should pass the result of this method to your plugins OnConfigParsed() method.
59+
/// </remarks>
60+
public static T Reload<T>(this T config) where T : BasePluginConfig, new()
61+
{
62+
// read the configuration file content
63+
var configContent = File.ReadAllText(CfgPath);
64+
65+
// deserialize the configuration content back to the object
66+
return JsonSerializer.Deserialize<T>(configContent, ReadSerializerOptions)!;
67+
}
68+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using CounterStrikeSharp.API;
2+
using CounterStrikeSharp.API.Core;
3+
4+
namespace CSSharpUtils.Extensions;
5+
6+
/// <summary>
7+
/// Provides extension methods for <see cref="CCSGameRules"/> to enhance functionality.
8+
/// </summary>
9+
public static class GameRulesExtensions
10+
{
11+
/// <summary>
12+
/// Calculates the remaining time in the current round.
13+
/// </summary>
14+
/// <param name="gameRules">The game rules instance, or null if not available.</param>
15+
/// <returns>The remaining time in seconds; returns 0.0f if <paramref name="gameRules"/> is null.</returns>
16+
public static float GetRemainingRoundTime(this CCSGameRules? gameRules)
17+
{
18+
if (gameRules == null)
19+
return 0.0f;
20+
21+
// Calculate remaining time by subtracting the current time from the sum of round start time and round duration
22+
return (gameRules.RoundStartTime + gameRules.RoundTime) - Server.CurrentTime;
23+
}
24+
}

0 commit comments

Comments
 (0)