Skip to content

Commit

Permalink
fix: Non-game thread issue on uploading demo
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed Feb 2, 2025
1 parent 93a1a64 commit a154ad2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
-- 2025.02.02 - v2.0.1

- fix: Non-game thread issue on uploading demo

-- 2025.02.01 - v2.0.0 Major Update

- feat: Made Discord webhook optional - plugin now works without Discord integration
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<a name="readme-top"></a>

![GitHub tag (with filter)](https://img.shields.io/github/v/tag/K4ryuu/K4-GOTV?style=for-the-badge&label=Version)
![GitHub Repo stars](https://img.shields.io/github/stars/K4ryuu/K4-GOTV?style=for-the-badge)
![GitHub issues](https://img.shields.io/github/issues/K4ryuu/K4-GOTV?style=for-the-badge)
![GitHub](https://img.shields.io/github/license/K4ryuu/K4-GOTV?style=for-the-badge)
![GitHub all releases](https://img.shields.io/github/downloads/K4ryuu/K4-GOTV/total?style=for-the-badge)
![GitHub last commit (branch)](https://img.shields.io/github/last-commit/K4ryuu/K4-GOTV/dev?style=for-the-badge)
![GitHub tag (with filter)](https://img.shields.io/github/v/tag/KitsuneLab-Development/K4-GOTV?style=for-the-badge&label=Version)
![GitHub Repo stars](https://img.shields.io/github/stars/KitsuneLab-Development/K4-GOTV?style=for-the-badge)
![GitHub issues](https://img.shields.io/github/issues/KitsuneLab-Development/K4-GOTV?style=for-the-badge)
![GitHub](https://img.shields.io/github/license/KitsuneLab-Development/K4-GOTV?style=for-the-badge)
![GitHub all releases](https://img.shields.io/github/downloads/KitsuneLab-Development/K4-GOTV/total?style=for-the-badge)
![GitHub last commit (branch)](https://img.shields.io/github/last-commit/KitsuneLab-Development/K4-GOTV/dev?style=for-the-badge)

<!-- PROJECT LOGO -->
<br />
Expand All @@ -16,11 +16,11 @@

<p align="center">
<br />
<a href="https://github.com/K4ryuu/K4-GOTV/releases">Download</a>
<a href="https://github.com/KitsuneLab-Development/K4-GOTV/releases">Download</a>
·
<a href="https://github.com/K4ryuu/K4-GOTV/issues/new?assignees=KitsuneLab-Development&labels=bug&projects=&template=bug_report.md&title=%5BBUG%5D">Report Bug</a>
<a href="https://github.com/KitsuneLab-Development/K4-GOTV/issues/new?assignees=KitsuneLab-Development&labels=bug&projects=&template=bug_report.md&title=%5BBUG%5D">Report Bug</a>
·
<a href="https://github.com/K4ryuu/K4-GOTV/issues/new?assignees=KitsuneLab-Development&labels=enhancement&projects=&template=feature_request.md&title=%5BREQ%5D">Request Feature</a>
<a href="https://github.com/KitsuneLab-Development/K4-GOTV/issues/new?assignees=KitsuneLab-Development&labels=enhancement&projects=&template=feature_request.md&title=%5BREQ%5D">Request Feature</a>
</p>
</div>

Expand Down
10 changes: 5 additions & 5 deletions src/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
namespace K4GOTV;

[MinimumApiVersion(300)]
public class CS2GOTVDiscordPlugin : BasePlugin, IPluginConfig<PluginConfig>
public sealed partial class Plugin : BasePlugin, IPluginConfig<PluginConfig>
{
public override string ModuleName => "K4-GOTV";
public override string ModuleDescription => "Advanced GOTV handler with Discord, database, FTP, SFTP and Mega integration";
public override string ModuleVersion => "2.0.0";
public override string ModuleVersion => "2.0.1";
public override string ModuleAuthor => "K4ryuu @ KitsuneLab";

public required PluginConfig Config { get; set; } = new PluginConfig();
Expand Down Expand Up @@ -125,7 +125,7 @@ public override void Unload(bool hotReload)

public override void OnAllPluginsLoaded(bool isReload)
{
Task.Run(async () =>
CSSThread.RunOnMainThread(async () =>
{
if (Config.General.DeleteEveryDemoFromServerAfterServerStart)
{
Expand Down Expand Up @@ -196,7 +196,7 @@ private HookResult CommandListener_StopRecord(CCSPlayerController? player, Comma
if (Config.DemoRequest.Enabled && !DemoRequestedThisRound)
{
if (Config.DemoRequest.DeleteUnused)
Task.Run(() => FileManager.DeleteFileAsync(demoPath, Logger, Config.General.LogDeletions));
CSSThread.RunOnMainThread(async () => await FileManager.DeleteFileAsync(demoPath, Logger, Config.General.LogDeletions));

ResetVariables();
return HookResult.Continue;
Expand Down Expand Up @@ -238,7 +238,7 @@ public void ProcessUpload(string fileName, string demoPath)
["fileSizeInKB"] = "0"
};

Task.Run(async () =>
CSSThread.RunOnMainThread(async () =>
{
long fileSizeInBytes = 0;
try
Expand Down
59 changes: 59 additions & 0 deletions src/ThreadModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;

namespace K4GOTV;

public sealed partial class Plugin : BasePlugin
{
public static class CSSThread
{
public static void RunOnMainThread(Action callback)
{
using SyncContextScope synchronizationContext = new SyncContextScope();
callback.Invoke();
}

public static async Task RunOnMainThreadAsync(Func<Task> callback)
{
await new Func<Task>(async () =>
{
using SyncContextScope synchronizationContext = new SyncContextScope();
await callback.Invoke();
}).Invoke();
}
}

public class SourceSynchronizationContext : SynchronizationContext
{
public override void Post(SendOrPostCallback callback, object? state)
{
Server.NextWorldUpdate(() => callback(state));
}

public override SynchronizationContext CreateCopy()
{
return this;
}
}

public class SyncContextScope : IDisposable
{
private static SynchronizationContext _sourceContext = new SourceSynchronizationContext();

private SynchronizationContext? _oldContext;

public SyncContextScope()
{
_oldContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(_sourceContext);
}

public void Dispose()
{
if (_oldContext != null)
{
SynchronizationContext.SetSynchronizationContext(_oldContext);
}
}
}
}

0 comments on commit a154ad2

Please sign in to comment.