Skip to content

Commit

Permalink
Refactor InitializePaths and update project version
Browse files Browse the repository at this point in the history
Refactored `InitializePaths` method in `UpdateApp.cs` to handle cases where `app_location.json` is empty, corrupted, or doesn't exist. Added `SetDefaultPaths` method to set default paths. Commented out a large block of code in `VRChatOSCQuery.cs` related to OSC queries. Incremented project version in `MagicChatbox.csproj` from `0.8.865` to `0.8.866` and added a new package reference `VRChat.OSCQuery` with version `0.0.7`.
  • Loading branch information
BoiHanny committed May 6, 2024
1 parent 8c0b6ca commit f74dd9c
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 15 deletions.
50 changes: 36 additions & 14 deletions vrcosc-magicchatbox/Classes/DataAndSecurity/UpdateApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading;
Expand Down Expand Up @@ -35,30 +36,51 @@ private void InitializePaths()
string jsonFilePath = Path.Combine(dataPath, "app_location.json");
if (File.Exists(jsonFilePath))
{
JObject appLocation = JObject.Parse(File.ReadAllText(jsonFilePath));
currentAppPath = appLocation["currentAppPath"].ToString();
tempPath = appLocation["tempPath"].ToString();
unzipPath = appLocation["unzipPath"].ToString();
magicChatboxExePath = appLocation["magicChatboxExePath"].ToString();
backupPath = Path.Combine(dataPath, "backup");

var settingsJson = File.ReadAllText(jsonFilePath);

if (string.IsNullOrWhiteSpace(settingsJson) || settingsJson.All(c => c == '\0'))
{
Logging.WriteInfo("The app_location.json file is empty or corrupted.");
SetDefaultPaths();
}
else
{
try
{
JObject appLocation = JObject.Parse(settingsJson);
currentAppPath = appLocation["currentAppPath"].ToString();
tempPath = appLocation["tempPath"].ToString();
unzipPath = appLocation["unzipPath"].ToString();
magicChatboxExePath = appLocation["magicChatboxExePath"].ToString();
backupPath = Path.Combine(dataPath, "backup");
}
catch (Newtonsoft.Json.JsonReaderException ex)
{
Logging.WriteInfo($"Error parsing app_location.json: {ex.Message}");
SetDefaultPaths();
}
}
}
else
{
currentAppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
tempPath = Path.Combine(Path.GetTempPath(), "vrcosc_magicchatbox_update");
unzipPath = Path.Combine(tempPath, "update_unzip");
magicChatboxExePath = Path.Combine(unzipPath, "MagicChatbox.exe");
backupPath = Path.Combine(dataPath, "backup");

SetDefaultPaths();
}
if(!Directory.Exists(tempPath))

if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
}

private void SetDefaultPaths()
{
currentAppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
tempPath = Path.Combine(Path.GetTempPath(), "vrcosc_magicchatbox_update");
unzipPath = Path.Combine(tempPath, "update_unzip");
magicChatboxExePath = Path.Combine(unzipPath, "MagicChatbox.exe");
backupPath = Path.Combine(dataPath, "backup");
}

public void UpdateApplication(bool admin = false, string customZipPath = null)
{
bool useCustomZip = !string.IsNullOrEmpty(customZipPath);
Expand Down
185 changes: 185 additions & 0 deletions vrcosc-magicchatbox/Classes/DataAndSecurity/VRChatOSCQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//using CommunityToolkit.Mvvm.ComponentModel;
//using System;
//using System.Linq;
//using System.IO;
//using VRC.OSCQuery;
//using Newtonsoft.Json;
//using Microsoft.Extensions.Logging.Abstractions;
//using System.Collections.Generic;
//using System.Net;

//namespace vrcosc_magicchatbox.Classes.DataAndSecurity
//{
// public partial class VRChatOSCQuerySettings : ObservableObject
// {

// private const string SettingsFileName = "VRChatOSCQuerySettings.json";

// [ObservableProperty]
// string oSCAddress;

// [ObservableProperty]
// int oSCPort;



// private VRChatOSCQuerySettings()
// {
// LoadSettings();
// }

// public static VRChatOSCQuerySettings LoadSettings()
// {
// var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Vrcosc-MagicChatbox", SettingsFileName);

// if (File.Exists(path))
// {
// var settingsJson = File.ReadAllText(path);

// if (string.IsNullOrWhiteSpace(settingsJson) || settingsJson.All(c => c == '\0'))
// {
// Logging.WriteInfo("The settings JSON file is empty or corrupted.");
// return new VRChatOSCQuerySettings();
// }

// try
// {
// var settings = JsonConvert.DeserializeObject<VRChatOSCQuerySettings>(settingsJson);

// if (settings != null)
// {
// return settings;
// }
// else
// {
// Logging.WriteInfo("Failed to deserialize the settings JSON.");
// return new VRChatOSCQuerySettings();
// }
// }
// catch (JsonException ex)
// {
// Logging.WriteInfo($"Error parsing settings JSON: {ex.Message}");
// return new VRChatOSCQuerySettings();
// }
// }
// else
// {
// Logging.WriteInfo("Settings file does not exist, returning new settings instance.");
// return new VRChatOSCQuerySettings();
// }
// }

// public void SaveSettings()
// {
// var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Vrcosc-MagicChatbox", SettingsFileName);
// Directory.CreateDirectory(Path.GetDirectoryName(path)); // Ensure directory exists
// var settingsJson = JsonConvert.SerializeObject(this, Formatting.Indented);
// File.WriteAllText(path, settingsJson);
// }
// }

// public partial class VRChatOSCQuery : ObservableObject
// {
// using System;
//using System.Collections.Generic;
//using System.Net;
//using VRC.OSCQuery;

//public class VRChatOSC : IDisposable
// {
// private OSCQueryService _oscQueryService;
// private Dictionary<string, OSCQueryNode> _avatarParameters = new Dictionary<string, OSCQueryNode>();

// public event Action<string, object> OnAvatarParameterChanged;

// public VRChatOSC()
// {
// // Create a new OSCQueryService instance
// _oscQueryService = new OSCQueryServiceBuilder()
// .WithDefaults()
// .WithServiceName("MyOSCService")
// .WithLogger(new NullLogger<OSCQueryService>())
// .Build();

// // Subscribe to OSC and OSCQuery service discovery events
// _oscQueryService.OnOscServiceAdded += OnOscServiceDiscovered;
// _oscQueryService.OnOscQueryServiceAdded += OnOscQueryServiceDiscovered;

// // Subscribe to avatar change events
// _oscQueryService.RootNode.AddNode(new OSCQueryNode("/avatar/change")). += OnAvatarChanged;
// }

// private void OnOscServiceDiscovered(OSCQueryServiceProfile profile)
// {
// Console.WriteLine($"Discovered OSC service: {profile.Name} at {profile.Address}:{profile.Port}");
// }

// private void OnOscQueryServiceDiscovered(OSCQueryServiceProfile profile)
// {
// Console.WriteLine($"Discovered OSCQuery service: {profile.Name} at {profile.Address}:{profile.Port}");
// }

// private void OnAvatarChanged(object sender, ValueChangedEventArgs args)
// {
// string avatarId = (string)args.Value;
// Console.WriteLine($"Avatar changed to: {avatarId}");

// // Clear existing avatar parameters
// _avatarParameters.Clear();

// // Get the new avatar's OSC tree
// IPAddress ip = _oscQueryService.HostIP;
// int port = _oscQueryService.TcpPort;
// var oscTree = Extensions.GetOSCTree(ip, port).Result;

// // Find all avatar parameters in the OSC tree
// var parameterNodes = oscTree.GetNodeWithPath("/avatar/parameters")?.Contents;
// if (parameterNodes != null)
// {
// foreach (var paramNode in parameterNodes.Values)
// {
// _avatarParameters[paramNode.Name] = paramNode;
// paramNode.OnValueChangedEvent += OnAvatarParameterValueChanged;
// }
// }
// }

// private void OnAvatarParameterValueChanged(object sender, ValueChangedEventArgs args)
// {
// var node = (OSCQueryNode)sender;
// string parameterName = node.Name;
// OnAvatarParameterChanged?.Invoke(parameterName, args.Value);
// }

// public void SetAvatarParameter<T>(string parameterName, T value)
// {
// if (_avatarParameters.TryGetValue(parameterName, out var node))
// {
// node.Value = new object[] { value };
// }
// else
// {
// Console.WriteLine($"Avatar parameter not found: {parameterName}");
// }
// }

// public T GetAvatarParameter<T>(string parameterName)
// {
// if (_avatarParameters.TryGetValue(parameterName, out var node))
// {
// return (T)node.Value[0];
// }
// else
// {
// Console.WriteLine($"Avatar parameter not found: {parameterName}");
// return default;
// }
// }

// public void Dispose()
// {
// _oscQueryService?.Dispose();
// }
// }

//}
3 changes: 2 additions & 1 deletion vrcosc-magicchatbox/MagicChatbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<Version>0.8.865</Version>
<Version>0.8.866</Version>
<TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
<RootNamespace>vrcosc_magicchatbox</RootNamespace>
<Nullable>enable</Nullable>
Expand Down Expand Up @@ -183,6 +183,7 @@
<PackageReference Include="NHotkey.Wpf" Version="3.0.0" />
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="OpenAI-DotNet" Version="7.7.8" />
<PackageReference Include="VRChat.OSCQuery" Version="0.0.7" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit f74dd9c

Please sign in to comment.