Skip to content

Commit

Permalink
Refactor variable handling and update method calls to use Vars class …
Browse files Browse the repository at this point in the history
…and removing OSCQuery from Sender
  • Loading branch information
Sergey004 committed Jan 13, 2025
1 parent 66f8a35 commit f7e41da
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 74 deletions.
15 changes: 11 additions & 4 deletions Quest2-VRC.Core/Services/PacketSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Net;
using static Quest2_VRC.Vars;
using static Quest2_VRC.Logger;

namespace Quest2_VRC
Expand All @@ -15,12 +16,18 @@ public class PacketSender

static public void SendPacket(params VRChatMessage[] Params)
{
string json = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(json);

int SendPort = Global.SendPort;
if (Global.UseCustomPort)
{
SendPort = Global.SendPort;
}
else
{
SendPort = 9000;
}

int SendPort = int.Parse((string)vars["SendPort"]);
var IP = IPAddress.Parse((string)vars["HostIP"]);
var IP = IPAddress.Parse(Global.HostIP);
IPEndPoint VRChat = new IPEndPoint(IP, SendPort);
foreach (var Param in Params)
{
Expand Down
35 changes: 24 additions & 11 deletions Quest2-VRC.Core/Services/Receiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using static Quest2_VRC.Vars;
using Extensions = VRC.OSCQuery.Extensions;
using VRC.OSCQuery;

Expand All @@ -26,7 +27,9 @@ public class Receiver
private static readonly string[] rgbAddresses = { "/avatar/parameters/R", "/avatar/parameters/G", "/avatar/parameters/B" };
private static readonly Timer processTimer = new(230);
public static async void Run()
{
{
string json = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(json);
processTimer.Elapsed += ProcessBufferedData;
processTimer.AutoReset = true;
processTimer.Start();
Expand All @@ -35,21 +38,31 @@ public static async void Run()
await Task.Delay(20);
RGBController.SendRGBRawData(0, 0, 0); // Set to Black
var tcpPort = Extensions.GetAvailableTcpPort();
var udpPort = Extensions.GetAvailableUdpPort();
var oscQuery = new OSCQueryServiceBuilder()
.WithTcpPort(tcpPort)
.WithUdpPort(udpPort)
.WithServiceName("Quest2-VRC OSCQuery Receiver")
.WithDefaults()
.Build();
int udpPort;
if (vars["UseCustomPort"] != null && (bool)vars["UseCustomPort"])
{

udpPort = Extensions.GetAvailableUdpPort();
var oscQuery = new OSCQueryServiceBuilder()
.WithTcpPort(tcpPort)
.WithUdpPort(udpPort)
.WithServiceName("Quest2-VRC OSCQuery Receiver")
.WithDefaults()
.Build();
}
else
{
udpPort = (int)vars["ReceivePort"];
}





string json = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(json);



var IP = IPAddress.Parse((string)vars["HostIP"]);
var IP = IPAddress.Parse((string)Global.HostIP);

OscServer oscServer;
oscServer = new OscServer((Bespoke.Common.Net.TransportType)TransportType.Udp, IP, udpPort);
Expand Down
32 changes: 9 additions & 23 deletions Quest2-VRC.Core/Services/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using VRC.OSCQuery;
using static Quest2_VRC.ADB;
using static Quest2_VRC.Logger;
using static Quest2_VRC.PacketSender;
Expand All @@ -18,7 +17,7 @@ namespace Quest2_VRC
{
public static class Sender
{


public class Global
{
Expand All @@ -38,24 +37,11 @@ public static async void Run(bool wirlessmode, bool audioEnadled, bool disableer
string json = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(json);

int Uport;
if (vars["UseCustomPort"] != null && (bool)vars["UseCustomPort"])
{
Uport = (int)vars["SendPort"];
}
else
{
Uport = Extensions.GetAvailableUdpPort();
}
var oscQuery = new OSCQueryServiceBuilder()
.WithTcpPort(tcpPort)
.WithUdpPort(Uport)
.WithServiceName("Quest2-VRC OSCQuery Sender")
.WithDefaults()
.Build();

oscQuery.AddEndpoint<int>("/avatar", Attributes.AccessValues.WriteOnly);
Console.WriteLine("OSC UDP port is {0}", Uport);
int Uport = Extensions.GetAvailableUdpPort(); ;


Console.WriteLine("OSC CLIENT UDP port is {0}", Uport);

await questwd(Uport, wirlessmode, audioEnadled, disableerrmsg, hostip);
}

Expand All @@ -64,7 +50,7 @@ public static async Task questwd(int Uport, bool wirlessmode, bool audioEnadled,
// Create a bogus port for the client
OscPacket.UdpClient = new UdpClient(Uport);


bool LowHMDBat = false;
bool HMDCrit = false;
bool audioPlayedHMD = false;
Expand All @@ -76,7 +62,7 @@ public static async Task questwd(int Uport, bool wirlessmode, bool audioEnadled,
{
try
{


int Hbatlevelint = 0;
int Rbatlevelint = 0;
Expand All @@ -85,7 +71,7 @@ public static async Task questwd(int Uport, bool wirlessmode, bool audioEnadled,
int gputempint = 0;
string WifiRSSI = null;
int WifiInt = 0;




Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;

namespace Quest2_VRC
{
public class Check_Vars
public class Vars
{
public class Global
{
public static string HMDBat { get; set; }
public static string ControllerBatL { get; set; }
public static string ControllerBatR { get; set; }
public static bool UseCustomPort { get; set; }
public static int SendPort { get; set; }
public static int ReceivePort { get; set; }
public static string HostIP { get; set; }
public static string LastKnownIP { get; set; }
}

public static void CheckVars()
{
bool exists = File.Exists("vars.json");
if (!exists)
{
Console.WriteLine("vars.json does not exist, creating...");


JObject vars = new JObject( // Default settings
new JProperty("HMDBat", "HMDBat"),
new JProperty("ControllerBatL", "ControllerBatL"),
new JProperty("ControllerBatR", "ControllerBatR"),
new JProperty("UseCustomPort", "False"),
new JProperty("SendPort", "9000"),
new JProperty("ReceivePort", "9001"),
new JProperty("HostIP", "127.0.0.1"),
new JProperty("LastKnownIP", "127.0.0.1"));


File.WriteAllText(@"vars.json", vars.ToString());

}

else
{
Console.WriteLine("vars.json exists");
}

LoadVars();
}

public static void WriteJSON(string lastip)
{
string jsonString = File.ReadAllText("vars.json");
Expand All @@ -42,15 +53,31 @@ public static void WriteJSON(string lastip)
jToken.Replace(lastip);
string updatedJsonString = jObject.ToString();
File.WriteAllText("vars.json", updatedJsonString);

Global.LastKnownIP = lastip;
}

public static string ReadJSON(string lastip)
{
string jsonString = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(jsonString);

return _ = ((string)vars["LastKnownIP"]);
}

private static void LoadVars()
{
string jsonString = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(jsonString);

Global.HMDBat = (string)vars["HMDBat"];
Global.ControllerBatL = (string)vars["ControllerBatL"];
Global.ControllerBatR = (string)vars["ControllerBatR"];
Global.UseCustomPort = bool.Parse((string)vars["UseCustomPort"]);
Global.SendPort = int.Parse((string)vars["SendPort"]);
Global.ReceivePort = int.Parse((string)vars["ReceivePort"]);
Global.HostIP = (string)vars["HostIP"];
Global.LastKnownIP = (string)vars["LastKnownIP"];
}

}
}
6 changes: 3 additions & 3 deletions Quest2-VRC.GUI/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public MainWindow()
{
StartServerResult result = server.StartServer(@"platform-tools\adb.exe", false);
}
catch (Exception ex) {
catch (Exception) {
MessageBox.Show(string.Format("ADB process running from another location is detected, the program closes."), "Results", MessageBoxButtons.OK);
Environment.Exit(1987);


}
materialTextBox1.Text = Check_Vars.ReadJSON(lastip);
materialTextBox1.Text = Vars.ReadJSON(lastip);

}

Expand Down Expand Up @@ -334,7 +334,7 @@ private void Form1_FormClosing(object sender, FormClosingEventArgs e)
}
else
{
Check_Vars.WriteJSON(materialTextBox1.Text);
Vars.WriteJSON(materialTextBox1.Text);
File.Delete("odtout.txt");
ADB.StopADB();
}
Expand Down
37 changes: 16 additions & 21 deletions Quest2-VRC.GUI/OSCTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.IO;
using System.Windows.Forms;
using static Quest2_VRC.Vars;
using static Quest2_VRC.PacketSender;

namespace Quest2_VRC
Expand All @@ -17,43 +18,39 @@ public OSCTest()
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.DARK;
materialSkinManager.ColorScheme = new ColorScheme(Primary.Amber800, Primary.Amber900, Primary.Cyan500, Accent.Cyan700, TextShade.WHITE);
materialComboBox1.Items.AddRange(new string[] { "bool", "int", "float", "string" });
materialComboBox1.Items.AddRange(["bool", "int", "float", "string"]);
}

private void Form2_Load(object sender, EventArgs e)
{
string json = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(json);
materialSlider1.Text = (string)vars["HMDBat"];
materialSlider2.Text = (string)vars["ControllerBatL"];
materialSlider3.Text = (string)vars["ControllerBatR"];

materialSlider1.Text = (string)Global.HMDBat;
materialSlider2.Text = (string)Global.ControllerBatL;
materialSlider3.Text = (string)Global.ControllerBatR;
}
private void materialSlider1_onValueChanged(object sender, int newValue)
{
string json = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(json);


string HMDBat = (string)vars["HMDBat"];
string HMDBat = (string)Global.HMDBat;
VRChatMessage Msg_emu1 = new VRChatMessage(HMDBat, (float)newValue / 100);
SendPacket(Msg_emu1);
}

private void materialSlider2_onValueChanged(object sender, int newValue)
{
string json = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(json);


string ControllerBatL = (string)vars["ControllerBatL"];
string ControllerBatL = (string)Global.ControllerBatL;
VRChatMessage Msg_emu2 = new VRChatMessage(ControllerBatL, (float)newValue / 100);
SendPacket(Msg_emu2);
}

private void materialSlider3_onValueChanged(object sender, int newValue)
{
string json = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(json);


string ControllerBatR = (string)vars["ControllerBatR"];
string ControllerBatR = (string)Global.ControllerBatR; ;
VRChatMessage Msg_emu3 = new VRChatMessage(ControllerBatR, (float)newValue / 100);
SendPacket(Msg_emu3);

Expand All @@ -73,12 +70,10 @@ private void materialSlider4_onValueChanged(object sender, int newValue)
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
string json = File.ReadAllText("vars.json");
JObject vars = JObject.Parse(json);

string HMDBat = (string)vars["HMDBat"];
string ControllerBatL = (string)vars["ControllerBatL"];
string ControllerBatR = (string)vars["ControllerBatR"];

string HMDBat = (string)Global.HMDBat;
string ControllerBatL = (string)Global.ControllerBatL;
string ControllerBatR = (string)Global.ControllerBatR;
VRChatMessage Msg_emu1 = new VRChatMessage(HMDBat, 0f);
VRChatMessage Msg_emu2 = new VRChatMessage(ControllerBatL, 0f);
VRChatMessage Msg_emu3 = new VRChatMessage(ControllerBatR, 0f);
Expand Down
8 changes: 4 additions & 4 deletions Quest2-VRC.GUI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ static void Handler(bool forceeng, bool enhancedoculuscontrol)
if (forceeng == false && enhancedoculuscontrol == false)
{

Check_Vars.CheckVars();
Vars.CheckVars();
Console.WriteLine("Logs redirected to main window");
GUI();
}
if (forceeng == true && enhancedoculuscontrol == false)
{

Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Check_Vars.CheckVars();
Vars.CheckVars();
Console.WriteLine("Logs redirected to main window");
GUI();
}
Expand All @@ -64,7 +64,7 @@ static void Handler(bool forceeng, bool enhancedoculuscontrol)
{
Task.Factory.StartNew(() => OculusStaff.DashWatchDog(), TaskCreationOptions.LongRunning),
};
Check_Vars.CheckVars();
Vars.CheckVars();
Console.WriteLine("Logs redirected to main window");
GUI();
}
Expand All @@ -78,7 +78,7 @@ static void Handler(bool forceeng, bool enhancedoculuscontrol)
{
Task.Factory.StartNew(() => OculusStaff.DashWatchDog(), TaskCreationOptions.LongRunning),
};
Check_Vars.CheckVars();
Vars.CheckVars();
Console.WriteLine("Logs redirected to main window");
GUI();
}
Expand Down

0 comments on commit f7e41da

Please sign in to comment.