-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathProgram.cs
152 lines (139 loc) · 4.83 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#region
using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.ReactiveUI;
using Avalonia.Threading;
using Bluegrams.Application;
using FileWatcherEx;
using NLog;
using NLog.Targets;
using SFP.Models;
using SFP.Properties;
using SFP_UI.Models;
using SFP_UI.Targets;
using SFP_UI.Views;
using SkiaSharp;
#endregion
namespace SFP_UI;
internal static class Program
{
private static readonly string s_appDataPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "PhantomGamers", "SFP");
private static FileStream? s_fs;
private static FileSystemWatcherEx? s_fw;
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args)
{
if (!EnforceSingleInstance())
{
return;
}
SetupNLog();
Log.Logger.Info(
$"Initializing SFP version {UpdateChecker.Version} on platform {RuntimeInformation.RuntimeIdentifier}");
try
{
InitSettings();
_ = BuildAvaloniaApp().StartWithClassicDesktopLifetime(args, ShutdownMode.OnExplicitShutdown);
}
catch (Exception e)
{
Log.Logger.Error(e);
}
CloseFileStream();
}
private static void SetupNLog()
{
LogManager.AutoShutdown = true;
LogManager.Setup().SetupExtensions(ext => ext.RegisterTarget<OutputControlTarget>());
LogManager.Setup().LoadConfiguration(c =>
{
c.ForLogger().FilterMinLevel(LogLevel.Info).WriteToConsole().WithAsync();
using var fileTarget = new FileTarget();
fileTarget.FileName = Path.Join(s_appDataPath, "SFP.log");
fileTarget.ArchiveOldFileOnStartup = true;
fileTarget.OpenFileCacheTimeout = 30;
fileTarget.MaxArchiveFiles = 2;
fileTarget.ArchiveAboveSize = 1024 * 1024 * 10;
c.ForLogger().FilterMinLevel(LogLevel.Debug).WriteTo(fileTarget).WithAsync();
c.ForLogger().FilterMinLevel(LogLevel.Info).WriteTo(new OutputControlTarget()).WithAsync();
#if DEBUG
c.ForLogger().FilterMinLevel(LogLevel.Debug).WriteToDebug().WithAsync();
#endif
});
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
}
private static bool EnforceSingleInstance()
{
var tempPath = Path.GetTempPath();
try
{
s_fs = new FileStream(Path.Combine(tempPath, "sfp_ui"), FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.None);
}
catch (IOException)
{
File.Create(Path.Combine(tempPath, "sfp_ui_open"));
return false;
}
_ = Task.Run(WatchInstanceFile);
return true;
}
private static void CloseFileStream()
{
s_fs?.Close();
s_fs = null;
}
private static void WatchInstanceFile()
{
var tempPath = Path.GetTempPath();
s_fw = new FileSystemWatcherEx(tempPath) { Filter = "sfp_ui_open" };
s_fw.OnCreated += OnInstanceFileChanged;
s_fw.OnChanged += OnInstanceFileChanged;
s_fw.Start();
}
private static async void OnInstanceFileChanged(object? sender, FileChangedEvent e)
{
await Dispatcher.UIThread.InvokeAsync(MainWindow.ShowWindow);
}
private static void InitSettings()
{
if (File.Exists("SFP.config"))
{
PortableJsonSettingsProvider.SettingsFileName = "SFP.config";
}
else
{
_ = Directory.CreateDirectory(s_appDataPath);
PortableSettingsProviderBase.SettingsDirectory = s_appDataPath;
PortableJsonSettingsProvider.SettingsFileName = "settings.json";
}
PortableJsonSettingsProvider.ApplyProvider(Settings.Default);
Settings.Default.Reload();
Settings.Default.DummySetting = true;
Settings.Default.Save();
}
// Avalonia configuration, don't remove; also used by visual designer.
private static AppBuilder BuildAvaloniaApp()
{
var fontName = !string.IsNullOrEmpty(SKTypeface.Default.FamilyName)
? SKTypeface.Default.FamilyName
: "Century Schoolbook";
return AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToTrace()
.UseReactiveUI()
.With(new Win32PlatformOptions { OverlayPopups = true })
.With(new FontManagerOptions { DefaultFamilyName = fontName });
}
private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Log.Logger.Error(e.ExceptionObject);
LogManager.Shutdown();
CloseFileStream();
}
}