forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
173 lines (154 loc) · 6.4 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright(c) 2019-2022 pypy, Natsumi and individual contributors.
// All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using NLog;
using NLog.Targets;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace VRCX
{
public static class Program
{
public static string BaseDirectory { get; private set; }
public static readonly string AppDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "VRCX");
public static string ConfigLocation;
public static string Version { get; private set; }
public static bool LaunchDebug;
public static bool GPUFix;
private static readonly NLog.Logger logger = NLog.LogManager.GetLogger("VRCX");
static Program()
{
BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
ConfigLocation = Path.Combine(Program.AppDataDirectory, "VRCX.sqlite3");
if (!Directory.Exists(AppDataDirectory))
{
Directory.CreateDirectory(AppDataDirectory);
// Migrate config to AppData
if (File.Exists(Path.Combine(BaseDirectory, "VRCX.json")))
{
File.Move(Path.Combine(BaseDirectory, "VRCX.json"), Path.Combine(AppDataDirectory, "VRCX.json"));
File.Copy(Path.Combine(AppDataDirectory, "VRCX.json"), Path.Combine(AppDataDirectory, "VRCX-backup.json"));
}
if (File.Exists(Path.Combine(BaseDirectory, "VRCX.sqlite3")))
{
File.Move(Path.Combine(BaseDirectory, "VRCX.sqlite3"), Path.Combine(AppDataDirectory, "VRCX.sqlite3"));
File.Copy(Path.Combine(AppDataDirectory, "VRCX.sqlite3"), Path.Combine(AppDataDirectory, "VRCX-backup.sqlite3"));
}
}
}
private static void ConfigureLogger()
{
NLog.LogManager.Setup().LoadConfiguration(builder =>
{
var fileTarget = new FileTarget("fileTarget")
{
FileName = Path.Combine(AppDataDirectory, "logs", "VRCX.log"),
//Layout = "${longdate} [${level:uppercase=true}] ${logger} - ${message} ${exception:format=tostring}",
// Layout with padding between the level/logger and message so that the message always starts at the same column
Layout = "${longdate} [${level:uppercase=true:padding=-5}] ${logger:padding=-20} - ${message} ${exception:format=tostring}",
ArchiveFileName = Path.Combine(AppDataDirectory, "logs", "VRCX.{#}.log"),
ArchiveNumbering = ArchiveNumberingMode.DateAndSequence,
ArchiveEvery = FileArchivePeriod.Day,
MaxArchiveFiles = 4,
MaxArchiveDays = 7,
ArchiveAboveSize = 10000000,
ArchiveOldFileOnStartup = true,
ConcurrentWrites = true,
KeepFileOpen = true,
AutoFlush = true,
Encoding = System.Text.Encoding.UTF8
};
if (Program.LaunchDebug)
{
builder.ForLogger().FilterMinLevel(LogLevel.Debug).WriteTo(fileTarget);
}
else
{
#if DEBUG
// Archive maximum of 3 files 10MB each, kept for a maximum of 7 days
builder.ForLogger().FilterMinLevel(LogLevel.Debug).WriteTo(fileTarget);
#else
builder.ForLogger().FilterMinLevel(LogLevel.Debug).WriteTo(fileTarget);
#endif
}
});
}
[STAThread]
private static void Main()
{
ConfigureLogger();
try
{
Run();
}
catch (Exception e)
{
logger.Fatal(e, "Unhandled Exception, program dying");
MessageBox.Show(e.ToString(), "PLEASE REPORT IN https://vrcx.pypy.moe/discord", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
}
private static void GetVersion()
{
var buildName = "VRCX";
try
{
Version = $"{buildName} {File.ReadAllText(Path.Combine(BaseDirectory, "Version"))}";
}
catch (Exception)
{
Version = $"{buildName} Build";
}
}
private static void Run()
{
Update.Check();
StartupArgs.ArgsCheck();
GetVersion();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
logger.Info("{0} Starting...", Version);
// I'll re-do this whole function eventually I swear
var worldDBServer = new WorldDBManager("http://127.0.0.1:22500/");
Task.Run(worldDBServer.Start);
ProcessMonitor.Instance.Init();
SQLiteLegacy.Instance.Init();
VRCXStorage.Load();
LoadFromConfig();
CpuMonitor.Instance.Init();
Discord.Instance.Init();
WebApi.Instance.Init();
LogWatcher.Instance.Init();
AutoAppLaunchManager.Instance.Init();
CefService.Instance.Init();
IPCServer.Instance.Init();
VRCXVR.Instance.Init();
Application.Run(new MainForm());
logger.Info("{0} Exiting...", Version);
WebApi.Instance.SaveCookies();
VRCXVR.Instance.Exit();
CefService.Instance.Exit();
AutoAppLaunchManager.Instance.Exit();
LogWatcher.Instance.Exit();
WebApi.Instance.Exit();
worldDBServer.Stop();
Discord.Instance.Exit();
CpuMonitor.Instance.Exit();
VRCXStorage.Save();
SQLiteLegacy.Instance.Exit();
ProcessMonitor.Instance.Exit();
}
/// <summary>
/// Sets GPUFix to true if it is not already set and the VRCX_GPUFix key in the database is true.
/// </summary>
private static void LoadFromConfig()
{
if (!GPUFix)
GPUFix = VRCXStorage.Instance.Get("VRCX_GPUFix") == "true";
}
}
}