generated from KitsuneLab-Development/CS2-TemplateRepo
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Non-game thread issue on uploading demo
- Loading branch information
Showing
4 changed files
with
77 additions
and
14 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
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
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,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); | ||
} | ||
} | ||
} | ||
} |