Skip to content

Commit

Permalink
Updates and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Natsumi-sama committed Jan 9, 2024
1 parent 581d5fe commit cd2387a
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 136 deletions.
32 changes: 0 additions & 32 deletions Dotnet/AppApi/AppApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,6 @@ public void RestartVR()
VRCXVR.Instance.Restart();
}

/// <summary>
/// Returns an array of arrays containing information about the connected VR devices.
/// Each sub-array contains the type of device and its current state
/// </summary>
/// <returns>An array of arrays containing information about the connected VR devices.</returns>
public string[][] GetVRDevices()
{
return VRCXVR.Instance.GetDevices();
}

/// <summary>
/// Returns the current CPU usage as a percentage.
/// </summary>
/// <returns>The current CPU usage as a percentage.</returns>
public float CpuUsage()
{
return CpuMonitor.Instance.CpuUsage;
}

/// <summary>
/// Retrieves an image from the VRChat API and caches it for future use. The function will return the cached image if it already exists.
/// </summary>
Expand Down Expand Up @@ -352,19 +333,6 @@ public void DoFunny()
WinformThemer.DoFunny();
}

/// <summary>
/// Returns the number of milliseconds that the system has been running.
/// </summary>
/// <returns>The number of milliseconds that the system has been running.</returns>
public double GetUptime()
{
using (var uptime = new PerformanceCounter("System", "System Up Time"))
{
uptime.NextValue();
return TimeSpan.FromSeconds(uptime.NextValue()).TotalMilliseconds;
}
}

/// <summary>
/// Returns a color value derived from the given user ID.
/// This is, essentially, and is used for, random colors.
Expand Down
62 changes: 62 additions & 0 deletions Dotnet/AppApi/AppApiVr.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Diagnostics;
using System.Globalization;
using CefSharp;

namespace VRCX
{
public class AppApiVr
{
public static readonly AppApiVr Instance;

static AppApiVr()
{
Instance = new AppApiVr();
}

public void VrInit()
{
if (MainForm.Instance?.Browser != null && !MainForm.Instance.Browser.IsLoading && MainForm.Instance.Browser.CanExecuteJavascriptInMainFrame)
MainForm.Instance.Browser.ExecuteScriptAsync("$app.vrInit", "");
}

/// <summary>
/// Returns the current CPU usage as a percentage.
/// </summary>
/// <returns>The current CPU usage as a percentage.</returns>
public float CpuUsage()
{
return CpuMonitor.Instance.CpuUsage;
}

/// <summary>
/// Returns an array of arrays containing information about the connected VR devices.
/// Each sub-array contains the type of device and its current state
/// </summary>
/// <returns>An array of arrays containing information about the connected VR devices.</returns>
public string[][] GetVRDevices()
{
return VRCXVR.Instance.GetDevices();
}

/// <summary>
/// Returns the number of milliseconds that the system has been running.
/// </summary>
/// <returns>The number of milliseconds that the system has been running.</returns>
public double GetUptime()
{
using var uptime = new PerformanceCounter("System", "System Up Time");
uptime.NextValue();
return TimeSpan.FromSeconds(uptime.NextValue()).TotalMilliseconds;
}

/// <summary>
/// Returns the current language of the operating system.
/// </summary>
/// <returns>The current language of the operating system.</returns>
public string CurrentCulture()
{
return CultureInfo.CurrentCulture.ToString();
}
}
}
43 changes: 22 additions & 21 deletions Dotnet/AppApi/GameHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,51 +58,52 @@ public int QuitGame()
/// Starts the VRChat game process with the specified command-line arguments.
/// </summary>
/// <param name="arguments">The command-line arguments to pass to the VRChat game.</param>
public void StartGame(string arguments)
public bool StartGame(string arguments)
{
// try stream first
try
{
using (var key = Registry.ClassesRoot.OpenSubKey(@"steam\shell\open\command"))
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)
{
// "C:\Program Files (x86)\Steam\steam.exe" -- "%1"
var match = Regex.Match(key.GetValue(string.Empty) as string, "^\"(.+?)\\\\steam.exe\"");
if (match.Success)
{
var path = match.Groups[1].Value;
// var _arguments = Uri.EscapeDataString(arguments);
Process.Start(new ProcessStartInfo
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;
}
})
?.Close();
return true;
}
}
catch
{
logger.Warn("Failed to start VRChat from Steam");
}

// fallback
try
{
using (var key = Registry.ClassesRoot.OpenSubKey(@"VRChat\shell\open\command"))
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)
{
// "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)
{
var path = match.Groups[1].Value;
StartGameFromPath(path, arguments);
}
var path = match.Groups[1].Value;
return StartGameFromPath(path, arguments);
}
}
catch
{
logger.Warn("Failed to start VRChat from registry");
}

return false;
}

/// <summary>
Expand All @@ -116,7 +117,7 @@ public bool StartGameFromPath(string path, string arguments)
if (!path.EndsWith(".exe"))
path = Path.Combine(path, "launch.exe");

if (!File.Exists(path))
if (!path.EndsWith("launch.exe") || !File.Exists(path))
return false;

Process.Start(new ProcessStartInfo
Expand Down
11 changes: 8 additions & 3 deletions Dotnet/Util.cs → Dotnet/JavascriptBindings.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using CefSharp;

namespace VRCX
{
public static class Util
public static class JavascriptBindings
{
public static void ApplyJavascriptBindings(IJavascriptObjectRepository repository)
public static void ApplyAppJavascriptBindings(IJavascriptObjectRepository repository)
{
repository.NameConverter = null;
repository.Register("AppApi", AppApi.Instance);
Expand All @@ -17,5 +16,11 @@ public static void ApplyJavascriptBindings(IJavascriptObjectRepository repositor
repository.Register("Discord", Discord.Instance);
repository.Register("AssetBundleCacher", AssetBundleCacher.Instance);
}

public static void ApplyVrJavascriptBindings(IJavascriptObjectRepository repository)
{
repository.NameConverter = null;
repository.Register("AppApiVr", AppApiVr.Instance);
}
}
}
2 changes: 1 addition & 1 deletion Dotnet/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public MainForm()
Browser.ShowDevTools();
};

Util.ApplyJavascriptBindings(Browser.JavascriptObjectRepository);
JavascriptBindings.ApplyAppJavascriptBindings(Browser.JavascriptObjectRepository);
Browser.ConsoleMessage += (_, args) =>
{
jslogger.Debug(args.Message + " (" + args.Source + ":" + args.Line + ")");
Expand Down
2 changes: 1 addition & 1 deletion Dotnet/Overlay/OffScreenBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public OffScreenBrowser(string address, int width, int height)
Size = new System.Drawing.Size(width, height);
RenderHandler = this;

Util.ApplyJavascriptBindings(JavascriptObjectRepository);
JavascriptBindings.ApplyVrJavascriptBindings(JavascriptObjectRepository);
}

public new void Dispose()
Expand Down
4 changes: 2 additions & 2 deletions Dotnet/Overlay/VRForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public VRForm()
Dock = DockStyle.Fill
};

Util.ApplyJavascriptBindings(_browser1.JavascriptObjectRepository);
Util.ApplyJavascriptBindings(_browser2.JavascriptObjectRepository);
JavascriptBindings.ApplyVrJavascriptBindings(_browser1.JavascriptObjectRepository);
JavascriptBindings.ApplyVrJavascriptBindings(_browser2.JavascriptObjectRepository);

panel1.Controls.Add(_browser1);
panel2.Controls.Add(_browser2);
Expand Down
51 changes: 41 additions & 10 deletions Dotnet/PWI/WorldDBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,41 @@ namespace VRCX
{
public class WorldDBManager
{
public static WorldDBManager Instance;
public static readonly WorldDBManager Instance;
private readonly HttpListener listener;
private readonly WorldDatabase worldDB;
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private const string WorldDBServerUrl = "http://127.0.0.1:22500/";

private string lastError = null;
private bool debugWorld = false;

public WorldDBManager(string url)
static WorldDBManager()
{
Instance = new WorldDBManager();
}

public WorldDBManager()
{
Instance = this;
// http://localhost:22500
listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Prefixes.Add(WorldDBServerUrl);

worldDB = new WorldDatabase(Path.Combine(Program.AppDataDirectory, "VRCX-WorldData.db"));
}

public void Init()
{
if (VRCXStorage.Instance.Get("VRCX_DisableWorldDatabase") == "true")
{
logger.Info("World database is disabled. Not starting.");
return;
}

Task.Run(Start);
}

public async Task Start()
private async Task Start()
{
// typing this in vr gonna kms
try
Expand Down Expand Up @@ -223,9 +238,13 @@ private async Task<WorldDataRequestResponse> HandleDataRequest(HttpListenerConte
}

var worldOverride = request.QueryString["world"];
if (worldOverride == "global")
{
TryInitializeWorld(worldOverride, out connectionKey);
}
if (worldOverride != null && worldId != worldOverride)
{
var allowed = worldDB.GetWorldAllowExternalRead(worldOverride);
var allowed = worldDB.GetWorldAllowExternalRead(worldOverride) || worldOverride == "global";
if (!allowed)
{
return ConstructSuccessResponse(null, connectionKey);
Expand Down Expand Up @@ -262,9 +281,13 @@ private async Task<WorldDataRequestResponse> HandleAllDataRequest(HttpListenerCo
}

var worldOverride = request.QueryString["world"];
if (worldOverride == "global")
{
TryInitializeWorld(worldOverride, out connectionKey);
}
if (worldOverride != null && worldId != worldOverride)
{
var allowed = worldDB.GetWorldAllowExternalRead(worldOverride);
var allowed = worldDB.GetWorldAllowExternalRead(worldOverride) || worldOverride == "global";
if (!allowed)
{
return ConstructSuccessResponse(null, connectionKey);
Expand Down Expand Up @@ -319,9 +342,13 @@ private async Task<WorldDataRequestResponse> HandleBulkDataRequest(HttpListenerC
}

var worldOverride = request.QueryString["world"];
if (worldOverride == "global")
{
TryInitializeWorld(worldOverride, out connectionKey);
}
if (worldOverride != null && worldId != worldOverride)
{
var allowed = worldDB.GetWorldAllowExternalRead(worldOverride);
var allowed = worldDB.GetWorldAllowExternalRead(worldOverride) || worldOverride == "global";
if (!allowed)
{
return ConstructSuccessResponse(null, connectionKey);
Expand Down Expand Up @@ -526,8 +553,6 @@ public void ProcessLogWorldDataRequest(string json)
return;
}



// Make sure the connection key is a valid GUID. No point in doing anything else if it's not.
if (!debugWorld && !Guid.TryParse(request.ConnectionKey, out Guid _))
{
Expand All @@ -539,6 +564,12 @@ public void ProcessLogWorldDataRequest(string json)

// Get the world ID from the connection key
string worldId = worldDB.GetWorldByConnectionKey(request.ConnectionKey);

// Global override
if (request.ConnectionKey == "global")
{
worldId = "global";
}

// World ID is null, which means the connection key is invalid (or someone just deleted a world from the DB while VRCX was running lol).
if (worldId == null)
Expand Down
8 changes: 3 additions & 5 deletions Dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,14 @@ private static void Run()
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();
CpuMonitor.Instance.Init();
Discord.Instance.Init();
WorldDBManager.Instance.Init();
WebApi.Instance.Init();
LogWatcher.Instance.Init();
AutoAppLaunchManager.Instance.Init();
Expand All @@ -161,7 +159,7 @@ private static void Run()
AutoAppLaunchManager.Instance.Exit();
LogWatcher.Instance.Exit();
WebApi.Instance.Exit();
worldDBServer.Stop();
WorldDBManager.Instance.Stop();

Discord.Instance.Exit();
CpuMonitor.Instance.Exit();
Expand Down
Loading

0 comments on commit cd2387a

Please sign in to comment.