Skip to content

Commit 10873d6

Browse files
Ensure OnBalanceCheck and OnBalanceUpdate events are called on main thread
1 parent cc19ab6 commit 10873d6

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

UconomyXP/DatabaseManager.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using fr34kyn01535.Uconomy.Models;
1+
using fr34kyn01535.Uconomy.Helpers;
2+
using fr34kyn01535.Uconomy.Models;
23
using fr34kyn01535.Uconomy.Storage;
3-
using Rocket.Core.Logging;
4-
using SDG.Unturned;
54
using Steamworks;
65
using System;
76
using System.Collections.Generic;
@@ -53,7 +52,10 @@ public decimal GetBalance(string id)
5352

5453
decimal balance = bal?.Balance ?? 0;
5554

56-
pluginInstance.OnBalanceChecked(id, balance);
55+
ThreadHelper.RunSynchronously(() =>
56+
{
57+
pluginInstance.OnBalanceChecked(id, balance);
58+
});
5759

5860
return balance;
5961
}
@@ -71,7 +73,10 @@ public decimal IncreaseBalance(string id, decimal increaseBy)
7173

7274
bal.SetExperience(new CSteamID(ulong.Parse(id)));
7375

74-
pluginInstance.BalanceUpdated(id, increaseBy);
76+
ThreadHelper.RunSynchronously(() =>
77+
{
78+
pluginInstance.BalanceUpdated(id, increaseBy);
79+
});
7580

7681
return bal.Experience;
7782
}

UconomyXP/Helpers/ThreadHelper.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Rocket.Core.Logging;
2+
using Rocket.Core.Utils;
3+
using SDG.Unturned;
4+
using System;
5+
using System.Threading;
6+
7+
namespace fr34kyn01535.Uconomy.Helpers
8+
{
9+
internal static class ThreadHelper
10+
{
11+
internal static void RunAsynchronously(System.Action action, string exceptionMessage = null)
12+
{
13+
if (Thread.CurrentThread != ThreadUtil.gameThread)
14+
{
15+
action.Invoke();
16+
return;
17+
}
18+
19+
ThreadPool.QueueUserWorkItem((_) =>
20+
{
21+
try
22+
{
23+
action.Invoke();
24+
}
25+
catch (Exception e)
26+
{
27+
RunSynchronously(() => Logger.LogException(e, exceptionMessage));
28+
}
29+
});
30+
}
31+
32+
internal static void RunSynchronously(System.Action action, float delaySeconds = 0)
33+
{
34+
if (Thread.CurrentThread == ThreadUtil.gameThread)
35+
{
36+
action.Invoke();
37+
return;
38+
}
39+
40+
TaskDispatcher.QueueOnMainThread(action, delaySeconds);
41+
}
42+
}
43+
}

UconomyXP/UconomyXP.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<LangVersion>latest</LangVersion>
66
<RootNamespace>fr34kyn01535.Uconomy</RootNamespace>
77
<AssemblyName>Uconomy</AssemblyName>
8-
<Version>1.1.1</Version>
8+
<Version>1.1.2</Version>
99
<AssemblyVersion>1.0.4.1</AssemblyVersion>
1010
<AssemblyTitle>Uconomy</AssemblyTitle>
1111
</PropertyGroup>

0 commit comments

Comments
 (0)