Skip to content

Commit 02c7a35

Browse files
committed
chore: 防止开多份
1 parent 200ecf1 commit 02c7a35

File tree

9 files changed

+99
-67
lines changed

9 files changed

+99
-67
lines changed

MaiChartManager/AppMain.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using SingleInstanceCore;
2+
using System.Text.Json;
3+
using MaiChartManager.Controllers.Music;
4+
using Xabe.FFmpeg;
5+
6+
namespace MaiChartManager;
7+
8+
public class AppMain : ISingleInstance
9+
{
10+
public const string Version = "1.3.2";
11+
public static Browser? BrowserWin { get; set; }
12+
13+
private Launcher _launcher;
14+
15+
private static ILoggerFactory _loggerFactory = LoggerFactory.Create(builder =>
16+
{
17+
builder.AddConsole();
18+
builder.SetMinimumLevel(LogLevel.Information);
19+
});
20+
21+
public static ILogger GetLogger<T>() => _loggerFactory.CreateLogger<T>();
22+
23+
public void Run()
24+
{
25+
try
26+
{
27+
SentrySdk.Init(o =>
28+
{
29+
// Tells which project in Sentry to send events to:
30+
o.Dsn = "https://[email protected]/3";
31+
// Set TracesSampleRate to 1.0 to capture 100% of transactions for tracing.
32+
// We recommend adjusting this value in production.
33+
o.TracesSampleRate = 0.5;
34+
# if DEBUG
35+
o.Environment = "development";
36+
# endif
37+
}
38+
);
39+
40+
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
41+
ApplicationConfiguration.Initialize();
42+
FFmpeg.SetExecutablesPath(StaticSettings.exeDir);
43+
MovieConvertController.CheckHardwareAcceleration();
44+
45+
Directory.CreateDirectory(StaticSettings.appData);
46+
Directory.CreateDirectory(StaticSettings.tempPath);
47+
if (File.Exists(Path.Combine(StaticSettings.appData, "config.json")))
48+
StaticSettings.Config = JsonSerializer.Deserialize<Config>(File.ReadAllText(Path.Combine(StaticSettings.appData, "config.json")));
49+
IapManager.Init();
50+
51+
_launcher = new Launcher();
52+
53+
Application.Run();
54+
}
55+
catch (Exception e)
56+
{
57+
SentrySdk.CaptureException(e);
58+
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
59+
throw;
60+
}
61+
}
62+
63+
public void OnInstanceInvoked(string[] args)
64+
{
65+
_launcher.ShowWindow();
66+
}
67+
}

MaiChartManager/Browser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace MaiChartManager;
66
public sealed partial class Browser : Form
77
{
88
private readonly Uri loopbackUrl;
9-
private static ILogger logger = Program.GetLogger<Browser>();
9+
private static ILogger logger = AppMain.GetLogger<Browser>();
1010

1111
private static bool IsRunningAsUwp()
1212
{

MaiChartManager/Controllers/AssetDir/AssetDirController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ public void PutAssetDirTxtValue([FromBody] PutAssetDirTxtValueRequest req)
5555
[HttpPost]
5656
public void RequestLocalImportDir()
5757
{
58-
if (Program.BrowserWin is null) return;
58+
if (AppMain.BrowserWin is null) return;
5959
var dialog = new FolderBrowserDialog
6060
{
6161
Description = "请选择资源目录(OPT)的文件夹",
6262
ShowNewFolderButton = false
6363
};
64-
if (Program.BrowserWin.Invoke(() => dialog.ShowDialog(Program.BrowserWin)) != DialogResult.OK) return;
64+
if (AppMain.BrowserWin.Invoke(() => dialog.ShowDialog(AppMain.BrowserWin)) != DialogResult.OK) return;
6565
var src = dialog.SelectedPath;
6666
logger.LogInformation("LocalImportDir: {src}", src);
6767
if (src is null) return;
@@ -136,4 +136,4 @@ public async Task<UploadAssetDirResult> UploadAssetDir(string? destName)
136136

137137
return new UploadAssetDirResult(destName);
138138
}
139-
}
139+
}

MaiChartManager/Controllers/Music/MusicTransferController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public record RequestCopyToRequest(MusicBatchController.MusicIdAndAssetDirPair[]
2121
[Route("/MaiChartManagerServlet/[action]Api")]
2222
public void RequestCopyTo(RequestCopyToRequest request)
2323
{
24-
if (Program.BrowserWin is null) return;
24+
if (AppMain.BrowserWin is null) return;
2525
var dialog = new FolderBrowserDialog
2626
{
2727
Description = "请选择目标位置"
2828
};
29-
if (Program.BrowserWin.Invoke(() => dialog.ShowDialog(Program.BrowserWin)) != DialogResult.OK) return;
29+
if (AppMain.BrowserWin.Invoke(() => dialog.ShowDialog(AppMain.BrowserWin)) != DialogResult.OK) return;
3030
var dest = dialog.SelectedPath;
3131
logger.LogInformation("CopyTo: {dest}", dest);
3232

@@ -41,7 +41,7 @@ public void RequestCopyTo(RequestCopyToRequest request)
4141
CancelMessage = "正在取消…",
4242
HideTimeRemaining = true,
4343
};
44-
progress.Start(Program.BrowserWin);
44+
progress.Start(AppMain.BrowserWin);
4545
progress.UpdateProgress(0, (ulong)request.music.Length);
4646
}
4747

MaiChartManager/Launcher.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MaiChartManager/Launcher.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ private void button4_Click(object sender, EventArgs e)
189189

190190
private void label1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
191191
{
192-
if (Program.BrowserWin is null || Program.BrowserWin.IsDisposed)
192+
if (AppMain.BrowserWin is null || AppMain.BrowserWin.IsDisposed)
193193
{
194-
Program.BrowserWin = new Browser(loopbackUrl);
195-
Program.BrowserWin.Show();
194+
AppMain.BrowserWin = new Browser(loopbackUrl);
195+
AppMain.BrowserWin.Show();
196196
}
197197
else
198198
{
199-
Program.BrowserWin.Activate();
199+
AppMain.BrowserWin.Activate();
200200
}
201201
}
202202

@@ -230,11 +230,13 @@ private async void checkBox_startup_Click(object sender, EventArgs e)
230230
}
231231
}
232232

233-
private void notifyIcon1_Click(object sender, EventArgs e)
233+
public void ShowWindow(object sender = null, EventArgs e = null)
234234
{
235235
Visible = true;
236236
WindowState = FormWindowState.Normal;
237237
notifyIcon1.Visible = false;
238+
Show();
239+
Focus();
238240
}
239241

240242
private static async Task SaveConfigFileAsync()

MaiChartManager/MaiChartManager.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<PackageReference Include="pythonnet" Version="3.0.4" />
6464
<PackageReference Include="Sentry" Version="4.10.2" />
6565
<PackageReference Include="Sentry.AspNetCore" Version="4.10.2" />
66+
<PackageReference Include="SingleInstanceCore" Version="2.2.2"/>
6667
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.9" />
6768
<PackageReference Include="Standart.Hash.xxHash" Version="4.0.5" />
6869
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.1" />

MaiChartManager/Program.cs

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,34 @@
11
using System.Runtime.InteropServices;
2-
using System.Security.Cryptography;
3-
using System.Text.Json;
4-
using Windows.ApplicationModel;
5-
using MaiChartManager.Controllers.Music;
6-
using Microsoft.IdentityModel.JsonWebTokens;
7-
using Microsoft.IdentityModel.Tokens;
8-
using Xabe.FFmpeg;
2+
using SingleInstanceCore;
93

104
namespace MaiChartManager;
115

126
public static partial class Program
137
{
14-
public const string Version = "1.3.2";
15-
public static Browser? BrowserWin { get; set; }
16-
178
[LibraryImport("kernel32.dll", SetLastError = true)]
189
private static partial void SetConsoleOutputCP(uint wCodePageID);
1910

20-
private static ILoggerFactory _loggerFactory = LoggerFactory.Create(builder =>
21-
{
22-
builder.AddConsole();
23-
builder.SetMinimumLevel(LogLevel.Information);
24-
});
25-
26-
public static ILogger GetLogger<T>() => _loggerFactory.CreateLogger<T>();
27-
2811
/// <summary>
2912
/// The main entry point for the application.
3013
/// </summary>
3114
[STAThread]
3215
public static void Main()
3316
{
3417
SetConsoleOutputCP(65001);
35-
try
36-
{
37-
SentrySdk.Init(o =>
38-
{
39-
// Tells which project in Sentry to send events to:
40-
o.Dsn = "https://[email protected]/3";
41-
// Set TracesSampleRate to 1.0 to capture 100% of transactions for tracing.
42-
// We recommend adjusting this value in production.
43-
o.TracesSampleRate = 0.5;
44-
# if DEBUG
45-
o.Environment = "development";
46-
# endif
47-
}
48-
);
49-
50-
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
51-
ApplicationConfiguration.Initialize();
52-
FFmpeg.SetExecutablesPath(StaticSettings.exeDir);
53-
MovieConvertController.CheckHardwareAcceleration();
5418

55-
Directory.CreateDirectory(StaticSettings.appData);
56-
Directory.CreateDirectory(StaticSettings.tempPath);
57-
if (File.Exists(Path.Combine(StaticSettings.appData, "config.json")))
58-
StaticSettings.Config = JsonSerializer.Deserialize<Config>(File.ReadAllText(Path.Combine(StaticSettings.appData, "config.json")));
59-
IapManager.Init();
19+
var app = new AppMain();
6020

61-
new Launcher();
62-
63-
Application.Run();
64-
}
65-
catch (Exception e)
21+
var isFirstInstance = app.InitializeAsFirstInstance("MaiChartManager");
22+
if (isFirstInstance)
6623
{
67-
SentrySdk.CaptureException(e);
68-
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
69-
throw;
24+
try
25+
{
26+
app.Run();
27+
}
28+
finally
29+
{
30+
SingleInstance.Cleanup();
31+
}
7032
}
7133
}
7234
}

MaiChartManager/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
using MaiChartManager;
33

44
[assembly: AssemblyCompany("Clansty")]
5-
[assembly: AssemblyFileVersion(Program.Version)]
6-
[assembly: AssemblyInformationalVersion(Program.Version)]
5+
[assembly: AssemblyFileVersion(AppMain.Version)]
6+
[assembly: AssemblyInformationalVersion(AppMain.Version)]
77
[assembly: AssemblyProduct("MaiChartManager")]
88
[assembly: AssemblyTitle("MaiChartManager")]
9-
[assembly: AssemblyVersion(Program.Version)]
9+
[assembly: AssemblyVersion(AppMain.Version)]
1010
[assembly: System.Runtime.Versioning.TargetPlatformAttribute("Windows10.0.17763.0")]
1111
[assembly: System.Runtime.Versioning.SupportedOSPlatformAttribute("Windows10.0.14393.0")]

0 commit comments

Comments
 (0)