Skip to content

Commit 3f8141d

Browse files
committed
perf: GameManager
1 parent c059b2b commit 3f8141d

8 files changed

Lines changed: 56 additions & 54 deletions

File tree

Assets/Scripts/Global/Managers/GameManager.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.IO;
1616
using System.Linq;
1717
using System.Reflection;
18+
using Unity.VisualScripting;
1819
using UnityEditor;
1920
using UnityEngine;
2021
using UnityEngine.Networking;
@@ -26,6 +27,8 @@ namespace MajdataPlay
2627
#nullable enable
2728
internal sealed class GameManager : MajSingleton
2829
{
30+
public static event EventHandler<EventArgs?>? OnAppQuit;
31+
public static event EventHandler<EventArgs?>? OnSave;
2932
#if UNITY_ANDROID
3033
public static event EventHandler<AndroidJavaObject?>? OnNewIntent;
3134
public static event EventHandler<bool>? OnAppFocus;
@@ -337,7 +340,20 @@ void ChangeTimerIfRequested()
337340
void OnApplicationQuit()
338341
{
339342
Screen.sleepTimeout = SleepTimeout.SystemSetting;
340-
MajEnv.OnApplicationQuitRequested();
343+
RequestSave(this);
344+
try
345+
{
346+
if (OnAppQuit is not null)
347+
{
348+
OnAppQuit(this, null);
349+
}
350+
}
351+
catch (Exception ex)
352+
{
353+
MajDebug.LogException(ex);
354+
}
355+
OnAppQuit = null;
356+
OnSave = null;
341357
}
342358
#if UNITY_ANDROID || UNITY_IOS
343359
void OnApplicationFocus(bool focus)
@@ -348,7 +364,7 @@ void OnApplicationFocus(bool focus)
348364
}
349365
if (!focus)
350366
{
351-
MajEnv.RequestSave();
367+
RequestSave(this);
352368
}
353369
}
354370

@@ -360,7 +376,7 @@ void OnApplicationPause(bool pause)
360376
}
361377
if (pause)
362378
{
363-
MajEnv.RequestSave();
379+
RequestSave(this);
364380
}
365381
}
366382
[Preserve]
@@ -391,7 +407,20 @@ public void DisableGC()
391407
MajDebug.LogWarning("GC has been disabled");
392408
#endif
393409
}
394-
410+
public static void RequestSave(object? sender)
411+
{
412+
try
413+
{
414+
if (OnSave is not null)
415+
{
416+
OnSave(sender, null);
417+
}
418+
}
419+
catch (Exception ex)
420+
{
421+
MajDebug.LogException(ex);
422+
}
423+
}
395424
private static void ExtractAssetsIos()
396425
{
397426
var extractRoot = Path.Combine(MajEnv.RootPath, "ExtStreamingAssets/");

Assets/Scripts/IO/InputManager/InputManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ static InputManager()
356356
//{
357357
// _touchRecords.Add((SensorArea)i, new(10));
358358
//}
359-
MajEnv.OnApplicationQuit += OnApplicationQuit;
359+
GameManager.OnAppQuit += OnApplicationQuit;
360360
}
361361
internal static void Init(IReadOnlyDictionary<int, int> instanceID2SensorIndexMappingTable)
362362
{
@@ -1270,13 +1270,13 @@ public static void ClearAllSubscriber()
12701270
button.ClearSubscriber();
12711271
OnAnyAreaTrigger = null;
12721272
}
1273-
static void OnApplicationQuit()
1273+
static void OnApplicationQuit(object? sender, EventArgs? e)
12741274
{
12751275
if (_posData is not null)
12761276
{
12771277
UnsafeHelper.Free(_posData);
12781278
}
1279-
MajEnv.OnApplicationQuit -= OnApplicationQuit;
1279+
GameManager.OnAppQuit -= OnApplicationQuit;
12801280
}
12811281
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12821282
static void PushEvent(InputEventArgs args)

Assets/Scripts/Misc/Base/ChartSettingStorage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using System.Text;
99
using System.Threading;
1010
using System.Threading.Tasks;
11-
11+
#nullable enable
1212
namespace MajdataPlay.Settings
1313
{
1414
internal static class ChartSettingStorage
@@ -33,7 +33,7 @@ public static async ValueTask InitAsync()
3333
STORAGE_PATH = Path.Combine(MajEnv.RootPath, "ChartSetting.db");
3434
}
3535
await UniTask.SwitchToThreadPool();
36-
MajEnv.OnSave += OnSave;
36+
GameManager.OnSave += OnSave;
3737
if (!File.Exists(STORAGE_PATH))
3838
{
3939
return;
@@ -156,7 +156,7 @@ public static void ConvertUnitToSecond()
156156
}
157157
}
158158
}
159-
static void OnSave()
159+
static void OnSave(object? sender, EventArgs? args)
160160
{
161161
if (!_isInited)
162162
{

Assets/Scripts/Misc/Base/MajDebug.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ internal static void Init()
4949
args.SetObserved();
5050
};
5151
StartLogWritebackTask();
52-
MajEnv.OnApplicationQuit += OnApplicationQuit;
53-
MajEnv.OnSave += OnSave;
52+
GameManager.OnAppQuit += OnApplicationQuit;
53+
GameManager.OnSave += OnSave;
5454
Application.logMessageReceivedThreaded += (string condition, string stackTrace, LogType type) =>
5555
{
5656
var sb = ZString.CreateStringBuilder();
@@ -126,7 +126,7 @@ public static void LogException<T>(T obj) where T: Exception
126126
Log(obj, LogLevel.Error);
127127
}
128128

129-
static void OnApplicationQuit()
129+
static void OnApplicationQuit(object? sender, EventArgs? e)
130130
{
131131
try
132132
{
@@ -147,10 +147,10 @@ static void OnApplicationQuit()
147147
}
148148
finally
149149
{
150-
MajEnv.OnApplicationQuit -= OnApplicationQuit;
150+
GameManager.OnAppQuit -= OnApplicationQuit;
151151
}
152152
}
153-
static void OnSave()
153+
static void OnSave(object? sender, EventArgs? args)
154154
{
155155
if (_fileStream is null)
156156
{

Assets/Scripts/Misc/Base/MajEnv.cs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ internal static class MajEnv
5656
".mp4"
5757
};
5858

59-
public static event Action? OnApplicationQuit;
60-
public static event Action? OnSave;
61-
6259
#if UNITY_ANDROID // Android Only (User Agent)
6360
public static string HTTP_USER_AGENT { get; } = $"MajdataPlay Android/{MajInstances.GameVersion.ToString()}";
6461
#elif UNITY_IOS // iOS Only (User Agent)
@@ -149,6 +146,7 @@ static MajEnv()
149146
{
150147
UnityWebRequestFactory.Timeout = TimeSpan.FromMilliseconds(HTTP_TIMEOUT_MS);
151148
UnityWebRequestFactory.UserAgent = HTTP_USER_AGENT;
149+
GameManager.OnAppQuit += OnApplicationQuit;
152150
}
153151

154152
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
@@ -492,7 +490,7 @@ internal static void Init()
492490
MainThread.Name = "MajdataPlay MainThread";
493491
}
494492
#endif
495-
OnSave += SaveConfig;
493+
GameManager.OnSave += OnSave;
496494
#if UNITY_STANDALONE_WIN
497495
MajDebug.LogInfo("[VLC] init");
498496
if (VLCLibrary != null)
@@ -522,8 +520,10 @@ static void TryDeleteDirectory(string path)
522520
}
523521
}
524522

525-
internal static void OnApplicationQuitRequested()
523+
static void OnApplicationQuit(object? sender, EventArgs? e)
526524
{
525+
GameManager.OnAppQuit -= OnApplicationQuit;
526+
GameManager.OnSave -= OnSave;
527527
SharedHttpClient.CancelPendingRequests();
528528
SharedHttpClient.Dispose();
529529
#if UNITY_STANDALONE_WIN
@@ -533,40 +533,13 @@ internal static void OnApplicationQuitRequested()
533533
}
534534
#endif
535535
_globalCTS.Cancel();
536-
RequestSave();
537-
try
538-
{
539-
if (OnApplicationQuit is not null)
540-
{
541-
OnApplicationQuit();
542-
}
543-
}
544-
finally
545-
{
546536
#if UNITY_STANDALONE_WIN
547537
WinHidManager.QuitThisBs();
548538
#elif UNITY_STANDALONE_OSX
549539
MacHidManager.QuitThisBs();
550540
#endif
551-
}
552541
}
553-
554-
internal static void RequestSave()
555-
{
556-
try
557-
{
558-
if (OnSave is not null)
559-
{
560-
OnSave();
561-
}
562-
}
563-
catch (Exception ex)
564-
{
565-
MajDebug.LogException(ex);
566-
}
567-
}
568-
569-
static void SaveConfig()
542+
static void OnSave(object? sender, EventArgs? e)
570543
{
571544
//var listConfig = RuntimeConfig.List;
572545
//listConfig.SelectedSongIndex = SongStorage.WorkingCollection.Index;

Assets/Scripts/Misc/Base/SongStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ await Task.Run(async () =>
104104
}
105105
finally
106106
{
107-
MajEnv.OnSave += OnSave;
107+
GameManager.OnSave += OnSave;
108108
}
109109
}
110110
internal static async Task RefreshAsync(IProgress<string>? progressReporter = null)
@@ -597,7 +597,7 @@ static async Task<SongCollection> GetOnlineCollection(ApiEndpoint api, IProgress
597597
};
598598
});
599599
}
600-
static void OnSave()
600+
static void OnSave(object? sender, EventArgs? args)
601601
{
602602
try
603603
{

Assets/Scripts/Misc/Recording/RecordHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static bool IsEnabled
4141
static RecordHelper()
4242
{
4343
_recorder = new OBSRecorder();
44-
MajEnv.OnApplicationQuit += OnApplicationQuit;
44+
GameManager.OnAppQuit += OnApplicationQuit;
4545
}
4646
public static void StartRecord(string filename)
4747
{
@@ -81,14 +81,14 @@ internal static void OnLateUpdate()
8181
{
8282
_recorder.OnLateUpdate();
8383
}
84-
private static void OnApplicationQuit()
84+
private static void OnApplicationQuit(object? sender, EventArgs? e)
8585
{
8686
if (!IsEnabled || !_recorder.IsConnected)
8787
{
8888
return;
8989
}
9090
_recorder.StopRecord();
91-
MajEnv.OnApplicationQuit -= OnApplicationQuit;
91+
GameManager.OnAppQuit -= OnApplicationQuit;
9292
}
9393
}
9494
}

Assets/Scripts/Scenes/Setting/SettingManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private void OnDestroy()
261261
{
262262
_isExited = true;
263263
InputManager.TouchButtonRingEdge = 5.4f;
264-
MajEnv.RequestSave();
264+
GameManager.RequestSave(this);
265265
GC.Collect();
266266
}
267267
}

0 commit comments

Comments
 (0)