forked from vrcx-team/VRCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppApi.cs
322 lines (289 loc) · 14.9 KB
/
AppApi.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright(c) 2019-2020 pypy. All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using CefSharp;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Net;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;
using librsync.net;
using System.Net.Sockets;
using System.Text.Json.Serialization;
namespace VRCX
{
public class AppApi
{
public static readonly AppApi Instance;
static AppApi()
{
Instance = new AppApi();
}
public string MD5File(string Blob)
{
byte[] fileData = Convert.FromBase64CharArray(Blob.ToCharArray(), 0, Blob.Length);
byte[] md5 = MD5.Create().ComputeHash(fileData);
return System.Convert.ToBase64String(md5);
}
public string SignFile(string Blob)
{
byte[] fileData = Convert.FromBase64CharArray(Blob.ToCharArray(), 0, Blob.Length);
Stream sig = Librsync.ComputeSignature(new MemoryStream(fileData));
var memoryStream = new MemoryStream();
sig.CopyTo(memoryStream);
byte[] sigBytes = memoryStream.ToArray();
return System.Convert.ToBase64String(sigBytes);
}
public string FileLength(string Blob)
{
byte[] fileData = Convert.FromBase64CharArray(Blob.ToCharArray(), 0, Blob.Length);
return fileData.Length.ToString();
}
public string ReadConfigFile()
{
var logPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"Low\VRChat\VRChat\";
var configFile = Path.Combine(logPath, @"config.json");
if (!Directory.Exists(logPath) || !File.Exists(configFile))
{
return "";
}
var json = File.ReadAllText(configFile);
return json;
}
public void WriteConfigFile(string json)
{
var logPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"Low\VRChat\VRChat\";
var configFile = Path.Combine(logPath, @"config.json");
File.WriteAllText(configFile, json);
}
public void ShowDevTools()
{
MainForm.Instance.Browser.ShowDevTools();
}
public void DeleteAllCookies()
{
Cef.GetGlobalCookieManager().DeleteCookies();
}
public string LoginWithSteam()
{
var rpc = VRChatRPC.Instance;
return rpc.Update() == true
? rpc.GetAuthSessionTicket()
: string.Empty;
}
public bool[] CheckGameRunning()
{
var isGameRunning = false;
var isGameNoVR = false;
var hwnd = WinApi.FindWindow("UnityWndClass", "VRChat");
if (hwnd != IntPtr.Zero)
{
var cmdline = string.Empty;
try
{
WinApi.GetWindowThreadProcessId(hwnd, out uint pid);
using (var searcher = new ManagementObjectSearcher($"SELECT CommandLine FROM Win32_Process WHERE ProcessId = {pid}"))
using (var objects = searcher.Get())
{
cmdline = objects.Cast<ManagementBaseObject>().SingleOrDefault()?["CommandLine"]?.ToString();
}
}
catch
{
}
isGameRunning = true;
isGameNoVR = cmdline.Contains("--no-vr");
}
return new bool[]
{
isGameRunning,
isGameNoVR
};
}
public void StartGame(string arguments)
{
// try stream first
try
{
using (var key = Registry.ClassesRoot.OpenSubKey(@"steam\shell\open\command"))
{
// "C:\Program Files (x86)\Steam\steam.exe" -- "%1"
var match = Regex.Match(key.GetValue(string.Empty) as string, "^\"(.+?)\\\\steam.exe\"");
if (match.Success == true)
{
var path = match.Groups[1].Value;
// var _arguments = Uri.EscapeDataString(arguments);
Process.Start(new ProcessStartInfo
{
WorkingDirectory = path,
FileName = $"{path}\\steam.exe",
UseShellExecute = false,
Arguments = $"-applaunch 438100 {arguments}"
}).Close();
return;
}
}
}
catch
{
}
// fallback
try
{
using (var key = Registry.ClassesRoot.OpenSubKey(@"VRChat\shell\open\command"))
{
// "C:\Program Files (x86)\Steam\steamapps\common\VRChat\launch.exe" "%1" %*
var match = Regex.Match(key.GetValue(string.Empty) as string, "(?!\")(.+?\\\\VRChat.*)(!?\\\\launch.exe\")");
if (match.Success == true)
{
var path = match.Groups[1].Value;
Process.Start(new ProcessStartInfo
{
WorkingDirectory = path,
FileName = $"{path}\\VRChat.exe",
UseShellExecute = false,
Arguments = arguments
}).Close();
}
}
}
catch
{
}
}
public void OpenLink(string url)
{
if (url.StartsWith("http://") == true ||
url.StartsWith("https://") == true)
{
Process.Start(url).Close();
}
}
public void ShowVRForm()
{
try
{
MainForm.Instance.BeginInvoke(new MethodInvoker(() =>
{
if (VRForm.Instance == null)
{
new VRForm().Show();
}
}));
}
catch
{
}
}
public void StartVR()
{
VRCXVR.Instance.SetActive(true);
}
public void StopVR()
{
VRCXVR.Instance.SetActive(false);
}
public void RefreshVR()
{
VRCXVR.Instance.Refresh();
}
public string[][] GetVRDevices()
{
return VRCXVR.Instance.GetDevices();
}
public float CpuUsage()
{
return CpuMonitor.Instance.CpuUsage;
}
public void CacheImage(string Base64File)
{
String Icon = Path.Combine(Program.BaseDirectory, "cache\\toast");
File.WriteAllBytes(Icon, Convert.FromBase64String(Base64File));
}
public void DesktopNotification(string BoldText, string Text, bool Image)
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText02);
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
String imagePath = Path.Combine(Program.BaseDirectory, "VRCX.ico");
if (Image)
{
imagePath = Path.Combine(Program.BaseDirectory, "cache\\toast");
}
stringElements[0].AppendChild(toastXml.CreateTextNode(BoldText));
stringElements[1].AppendChild(toastXml.CreateTextNode(Text));
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier("VRCX").Show(toast);
}
private struct XSOMessage
{
public int messageType { get; set; }
public int index { get; set; }
public float volume { get; set; }
public string audioPath { get; set; }
public float timeout { get; set; }
public string title { get; set; }
public string content { get; set; }
public string icon { get; set; }
public float height { get; set; }
public float opacity { get; set; }
public bool useBase64Icon { get; set; }
public string sourceApp { get; set; }
}
public void XSNotification(string Title, string Content, int Timeout, bool Image)
{
bool UseBase64Icon = true;
String Icon = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAHaGlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDUgNzkuMTYzNDk5LCAyMDE4LzA4LzEzLTE2OjQwOjIyICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIH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M8Bjk7OwsKhQICAwOZ3xMSEkw6e7AEANVjAAwPD3ObmvsBVlVVgUr1z8FOQ0MD8zsukMrLyx+1JhBcDtjd3YWIiAgmOLwdtP9dTHpJSUl6d4M4bVolADzdKSkpYYIKCAjgdn/3NT8/Dz4+Pkz5mJgYkw5DBAUAh3ZzczOzDcYVYE1NzYNL5bq6Or1LZVw7nJ6eWg8APMHBRQ0ehkilUggODuaSHp4QGdriHh0dcTMDlsV6ISEhXF0cGb29vRYHMGTqqTCWmZiYgKWlJVheXgaZTMatAw4PD43WVSqVMD09zdVD48kRtiWXy98mzYe0Id+gADb4ADCMjSuPlYJ9MKLYVFAAm3wAaMbGFxcXBQugu7ubAviDDwCfY+N4Ro/DVGjCmUIrcX7P1+PxfdpJ68tWGBoagr7+PrMZH3DiwglnBGPCtQOWxeSIM45W8IvEUr4AfEG8xPcj7sbGRqMAVpZX9NWdIv6Ur/cDqD4k/o64j/h34jEzeUTTHhdMX2+fQQCyVzIa9KXmwe0z4hB6kXwCQL2DLyEQ+xK/byZ7EfsRN1AICwsLDwLAKVZTDkfkZ8RO2hfINwA+9YQ+iUYf/nloDADe80/vN2LNAFCRxGsYx4vnL/QmRS0Ar4g/sjUAqC/pKGhvb7dLAKhyCmFyctIuAbxL3EEhaL+eowVARvyxrQJASYlnKAS6IbInAKg44lMKAYU7Ra1p8BNbB4D6hvgGY8MlMG6PNQBWiCPsAYAL8Y96lr+4ivzAHgDQpPiS+EwikfxFPl8Tf00s4RWA+Lq8CEAEIAIQAYgARAA26b8BaVJkoY+4rDoAAAAASUVORK5CYII=";
if (Image)
{
UseBase64Icon = false;
Icon = Path.Combine(Program.BaseDirectory, "cache\\toast");
}
IPAddress broadcastIP = IPAddress.Parse("127.0.0.1");
Socket broadcastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint endPoint = new IPEndPoint(broadcastIP, 42069);
XSOMessage msg = new XSOMessage();
msg.messageType = 1;
msg.title = Title;
msg.content = Content;
msg.height = 110f;
msg.sourceApp = "VRCX";
msg.timeout = Timeout;
msg.audioPath = "";
msg.useBase64Icon = UseBase64Icon;
msg.icon = Icon;
byte[] byteBuffer = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(msg);
broadcastSocket.SendTo(byteBuffer, endPoint);
}
public void SetStartup(bool enabled)
{
try
{
using (var key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
if (enabled == true)
{
var path = Application.ExecutablePath;
key.SetValue("VRCX", $"\"{path}\" --startup");
}
else
{
key.DeleteValue("VRCX", false);
}
}
}
catch
{
}
}
}
}