Skip to content

Commit

Permalink
Use string.Format instead of Replace, it's there for a reason
Browse files Browse the repository at this point in the history
  • Loading branch information
KimihikoAkayasaki committed Feb 21, 2023
1 parent 87d14a9 commit 91c9ec1
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 70 deletions.
4 changes: 2 additions & 2 deletions Amethyst/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public App()
Logger.Fatal($"Unhandled Exception: {ex.GetType().Name} in {ex.Source}: {ex.Message}\n{ex.StackTrace}");
var stc = $"{ex.GetType().Name} in {ex.Source}: {ex.Message}\n{ex.StackTrace}";
var msg = Interfacing.LocalizedJsonString("/CrashHandler/Content/Crash/UnknownStack").Replace("{0}", stc);
var msg = string.Format(Interfacing.LocalizedJsonString("/CrashHandler/Content/Crash/UnknownStack"), stc);
Interfacing.Fail(msg != "/CrashHandler/Content/Crash/UnknownStack" ? msg : stc);
Crashes.TrackError(e.Exception); // Log the crash reason
Expand All @@ -57,7 +57,7 @@ public App()
Logger.Fatal($"Unhandled Exception: {ex.GetType().Name} in {ex.Source}: {ex.Message}\n{ex.StackTrace}");
var stc = $"{ex.GetType().Name} in {ex.Source}: {ex.Message}\n{ex.StackTrace}";
var msg = Interfacing.LocalizedJsonString("/CrashHandler/Content/Crash/UnknownStack").Replace("{0}", stc);
var msg = string.Format(Interfacing.LocalizedJsonString("/CrashHandler/Content/Crash/UnknownStack"), stc);
Interfacing.Fail(msg != "/CrashHandler/Content/Crash/UnknownStack" ? msg : stc);
Crashes.TrackError((Exception)e.ExceptionObject); // Log the crash reason
Expand Down
19 changes: 7 additions & 12 deletions Amethyst/Classes/AppTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ namespace Amethyst.Classes;

public class AppTracker : INotifyPropertyChanged
{
[JsonIgnore]
private readonly KalmanFilter _kalmanFilter = new();
[JsonIgnore] private readonly KalmanFilter _kalmanFilter = new();

[JsonIgnore]
private readonly LowPassFilter _lowPassFilter = new(6.9f, .005f);
[JsonIgnore] private readonly LowPassFilter _lowPassFilter = new(6.9f, .005f);

[JsonIgnore]
private readonly Vector3 _predictedPosition = new(0);
[JsonIgnore] private readonly Vector3 _predictedPosition = new(0);

// Is this tracker enabled?
private bool _isActive;
Expand Down Expand Up @@ -281,9 +278,8 @@ Role is TrackerType.TrackerLeftFoot or TrackerType.TrackerRightFoot &&
IsPositionOverridden ? OverrideGuid : AppData.Settings.TrackingDeviceGuid;

[JsonIgnore]
public string ManagingDevicePlaceholder =>
Interfacing.LocalizedJsonString("/SettingsPage/Filters/Managed")
.Replace("{0}", ManagingDeviceGuid);
public string ManagingDevicePlaceholder => string.Format(Interfacing.LocalizedJsonString(
"/SettingsPage/Filters/Managed"), ManagingDeviceGuid);

[JsonIgnore]
public bool IsTrackerExpanderOpen
Expand Down Expand Up @@ -446,9 +442,8 @@ public bool IsOrientationOverriddenBySelectedDevice
!string.IsNullOrEmpty(OverrideGuid) && OverrideGuid != AppData.Settings.SelectedTrackingDeviceGuid;

[JsonIgnore]
public string OverriddenByOtherDeviceString =>
Interfacing.LocalizedJsonString("/DevicesPage/ToolTips/Overrides/Overlapping")
.Replace("{0}", ManagingDeviceGuid);
public string OverriddenByOtherDeviceString => string.Format(Interfacing.LocalizedJsonString(
"/DevicesPage/ToolTips/Overrides/Overlapping"), ManagingDeviceGuid);

// MVVM: a connection of the transitions each tracker expander should animate
[JsonIgnore] public TransitionCollection SettingsExpanderTransitions { get; set; } = new();
Expand Down
10 changes: 4 additions & 6 deletions Amethyst/Classes/TrackingDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,15 @@ public static void TrackersConfigChanged(bool showToasts = true)
Interfacing.ShowToast(
Interfacing.LocalizedJsonString(
"/SharedStrings/Toasts/TrackersConfigChanged/Title"),
Interfacing.LocalizedJsonString(
"/SharedStrings/Toasts/TrackersConfigChanged")
.Replace("{0}", CurrentServiceEndpoint.Name),
string.Format(Interfacing.LocalizedJsonString(
"/SharedStrings/Toasts/TrackersConfigChanged"), CurrentServiceEndpoint.Name),
false, "focus_restart");

Interfacing.ShowServiceToast(
Interfacing.LocalizedJsonString(
"/SharedStrings/Toasts/TrackersConfigChanged/Title"),
Interfacing.LocalizedJsonString(
"/SharedStrings/Toasts/TrackersConfigChanged")
.Replace("{0}", CurrentServiceEndpoint.Name));
string.Format(Interfacing.LocalizedJsonString(
"/SharedStrings/Toasts/TrackersConfigChanged"), CurrentServiceEndpoint.Name));
}

// Compare with saved settings and unlock the restart
Expand Down
4 changes: 2 additions & 2 deletions Amethyst/Controls/JointSettingsExpander.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ private bool Show
}
}

private string NotSupportedText => Interfacing.LocalizedJsonString("/SharedStrings/Joints/NotSupported/Tooltip")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name);
private string NotSupportedText => string.Format(Interfacing.LocalizedJsonString(
"/SharedStrings/Joints/NotSupported/Tooltip"), TrackingDevices.CurrentServiceEndpoint.Name);

private int TrackersCount =>
Trackers.All(x => x.IsSupported) ||
Expand Down
28 changes: 14 additions & 14 deletions Amethyst/Pages/General.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,17 @@ public General()
private List<Line> BoneLines { get; } = new(24);
private List<Ellipse> JointPoints { get; } = new(60);

private string ServiceSettingsText => Interfacing.LocalizedJsonString("/SettingsPage/Buttons/ServiceSettings")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name);
private string ServiceSettingsText => string.Format(Interfacing.LocalizedJsonString(
"/SettingsPage/Buttons/ServiceSettings"), TrackingDevices.CurrentServiceEndpoint.Name);

private string DeviceSettingsText => Interfacing.LocalizedJsonString("/GeneralPage/Buttons/DeviceSettings")
.Replace("{0}", TrackingDevices.BaseTrackingDevice.Name);
private string DeviceSettingsText => string.Format(Interfacing.LocalizedJsonString(
"/GeneralPage/Buttons/DeviceSettings"), TrackingDevices.BaseTrackingDevice.Name);

private string AutoStartTipText => Interfacing.LocalizedJsonString("/NUX/Tip2/Content")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name);
private string AutoStartTipText => string.Format(Interfacing.LocalizedJsonString(
"/NUX/Tip2/Content"), TrackingDevices.CurrentServiceEndpoint.Name);

private string ServiceStatusLabel => Interfacing.LocalizedJsonString("/GeneralPage/Captions/DriverStatus/Label")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name);
private string ServiceStatusLabel => string.Format(Interfacing.LocalizedJsonString(
"/GeneralPage/Captions/DriverStatus/Label"), TrackingDevices.CurrentServiceEndpoint.Name);

private bool AllowCalibration => Interfacing.AppTrackersInitialized && ServiceStatusOk;

Expand All @@ -145,7 +145,7 @@ private string[] ServiceStatusText
: message; // If everything is all right this time
}
}

public event PropertyChangedEventHandler PropertyChanged;

private async void Page_Loaded(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -173,7 +173,9 @@ private async Task Page_LoadedHandler()
AppData.Settings.AutoSpawnEnabledJoints) // If autospawn
{
if (await Interfacing.SpawnEnabledTrackers())
{
ToggleTrackersButton.IsChecked = true; // Mark as spawned
}

// Cry about it
else
Expand Down Expand Up @@ -785,7 +787,6 @@ private async void ToggleTrackersButton_Checked(object sender, RoutedEventArgs e

// Optionally spawn trackers
if (!Interfacing.AppTrackersSpawned)
{
if (!await Interfacing.SpawnEnabledTrackers()) // Mark as spawned
{
Interfacing.ServiceEndpointFailure = true; // WAAAAAAA
Expand All @@ -799,8 +800,7 @@ private async void ToggleTrackersButton_Checked(object sender, RoutedEventArgs e
Interfacing.LocalizedJsonString("/SharedStrings/Toasts/AutoSpawnFailed/Title"),
Interfacing.LocalizedJsonString("/SharedStrings/Toasts/AutoSpawnFailed"));
}
}


// Give up if failed
if (!Interfacing.ServiceEndpointFailure)
{
Expand Down Expand Up @@ -895,12 +895,12 @@ private async void StatusTeachingTip_CloseButtonClick(TeachingTip sender, object
SettingsPage.ManageTrackersTeachingTip.TailVisibility = TeachingTipTailVisibility.Collapsed;
SettingsPage.ManageTrackersTeachingTip.IsOpen = true;
}

private void AdditionalDeviceErrorsHyperlink_Tapped(object sender, TappedRoutedEventArgs e)
{
Shared.General.AdditionalDeviceErrorsHyperlinkTappedEvent.Start();
}

private async void ServiceSettingsButton_Click(object sender, RoutedEventArgs e)
{
// Go to the bottom of the settings page to view service endpoint settings
Expand Down
47 changes: 22 additions & 25 deletions Amethyst/Pages/Settings.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,39 +164,36 @@ private string[] ServiceStatusText
ServiceSupportsSettings ? new CornerRadius(0) : new CornerRadius(0, 0, 4, 4);

// MVVM stuff: bound strings with placeholders
private string RestartServiceText => Interfacing.LocalizedJsonString("/SettingsPage/Buttons/RestartService")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name);
private string RestartServiceText => string.Format(Interfacing.LocalizedJsonString(
"/SettingsPage/Buttons/RestartService"), TrackingDevices.CurrentServiceEndpoint.Name);

private string RestartServiceNoteL1 => Interfacing
.LocalizedJsonString("/SettingsPage/Captions/TrackersRestart/Line1")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name);
private string RestartServiceNoteL1 => string.Format(Interfacing.LocalizedJsonString(
"/SettingsPage/Captions/TrackersRestart/Line1"), TrackingDevices.CurrentServiceEndpoint.Name);

private string RestartServiceNoteL2 => Interfacing
.LocalizedJsonString("/SettingsPage/Captions/TrackersRestart/Line2")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name);
private string RestartServiceNoteL2 => string.Format(Interfacing.LocalizedJsonString(
"/SettingsPage/Captions/TrackersRestart/Line2"), TrackingDevices.CurrentServiceEndpoint.Name);

private string AutoStartLabelText => Interfacing.LocalizedJsonString("/SettingsPage/Captions/AutoStart")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name);
private string AutoStartLabelText => string.Format(Interfacing.LocalizedJsonString(
"/SettingsPage/Captions/AutoStart"), TrackingDevices.CurrentServiceEndpoint.Name);

private string AutoStartTipText => Interfacing.LocalizedJsonString("/NUX/Tip7/Title")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name);
private string AutoStartTipText => string.Format(Interfacing.LocalizedJsonString(
"/NUX/Tip7/Title"), TrackingDevices.CurrentServiceEndpoint.Name);

private string AutoStartTipContent => Interfacing.LocalizedJsonString("/NUX/Tip7/Content")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name);
private string AutoStartTipContent => string.Format(Interfacing.LocalizedJsonString(
"/NUX/Tip7/Content"), TrackingDevices.CurrentServiceEndpoint.Name);

private string ManageTrackersText
{
get
{
if (TrackingDevices.CurrentServiceEndpoint.IsRestartOnChangesNeeded)
return Interfacing.LocalizedJsonString("/SettingsLearn/Captions/ManageTrackers")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name)
.Replace("[[", "").Replace("]]", "");
return string.Format(Interfacing.LocalizedJsonString("/SettingsLearn/Captions/ManageTrackers"),
TrackingDevices.CurrentServiceEndpoint.Name).Replace("[[", "").Replace("]]", "");

// If the service doesn't need restarting
return StringUtils.RemoveBetween(
Interfacing.LocalizedJsonString("/SettingsLearn/Captions/ManageTrackers")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name), "[[", "]]");
string.Format(Interfacing.LocalizedJsonString("/SettingsLearn/Captions/ManageTrackers"),
TrackingDevices.CurrentServiceEndpoint.Name), "[[", "]]");
}
}

Expand Down Expand Up @@ -639,9 +636,9 @@ private async void AddTrackersTeachingTip_CloseButtonClick(TeachingTip sender, o

private void RestartButton_Click(object sender, RoutedEventArgs e)
{
TrackingDevices.CurrentServiceEndpoint.RequestServiceRestart(
Interfacing.LocalizedJsonString("/SettingsPage/Captions/ServiceRestart")
.Replace("{0}", TrackingDevices.CurrentServiceEndpoint.Name));
TrackingDevices.CurrentServiceEndpoint.RequestServiceRestart(string.Format(
Interfacing.LocalizedJsonString("/SettingsPage/Captions/ServiceRestart"),
TrackingDevices.CurrentServiceEndpoint.Name));

// Play a sound
AppSounds.PlayAppSound(AppSounds.AppSoundType.Invoke);
Expand Down Expand Up @@ -872,7 +869,7 @@ private void RefreshServiceButton_Click(SplitButton sender, SplitButtonClickEven
Interfacing.LocalizedJsonString(Interfacing.AppTrackersSpawned
? "/GeneralPage/Buttons/TrackersToggle/Reconnect"
: "/GeneralPage/Buttons/TrackersToggle/Connect");

// Mark the service as non-failed, clean
Interfacing.ServiceEndpointFailure = false;
Interfacing.AppTrackersSpawned = false;
Expand Down Expand Up @@ -922,7 +919,7 @@ private void ShutdownServiceButton_Click(object sender, RoutedEventArgs e)
Interfacing.LocalizedJsonString(Interfacing.AppTrackersSpawned
? "/GeneralPage/Buttons/TrackersToggle/Reconnect"
: "/GeneralPage/Buttons/TrackersToggle/Connect");

// Mark the service as non-failed, clean
Interfacing.ServiceEndpointFailure = false;
Interfacing.AppTrackersSpawned = false;
Expand Down Expand Up @@ -1010,7 +1007,7 @@ private async void SelectedServiceComboBox_SelectionChanged(object sender, Selec
Interfacing.LocalizedJsonString(Interfacing.AppTrackersSpawned
? "/GeneralPage/Buttons/TrackersToggle/Reconnect"
: "/GeneralPage/Buttons/TrackersToggle/Connect");

// Mark the service as non-failed, disable trackers
Interfacing.ServiceEndpointFailure = false;
Interfacing.AppTrackersInitialized = false;
Expand Down
18 changes: 9 additions & 9 deletions Amethyst/Utils/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.AppCenter.Crashes;
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.AppCenter.Crashes;

namespace Amethyst.Utils;

Expand All @@ -13,15 +13,15 @@ namespace Amethyst.Utils;
/// designed to be a simple drag and drop logger which relies solely on built-in C# APIs
/// </summary>
/// @https://github.com/KinectToVR/Amethyst-Installer @Hekky
public static class Logger
public static partial class Logger
{
public static string LogFilePath;
public static string LogFilePath { get; set; }

// This is used instead of Thread.CurrentThread.ManagedThreadId since it returns the OS thread rather than the managed thread
// Consider using ManagedThreadId instead of this if you have to run this on non-Windows platforms
// https://stackoverflow.com/a/1679270
[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
private static extern int GetCurrentWin32ThreadId();
[LibraryImport("Kernel32", EntryPoint = "GetCurrentThreadId")]
private static partial int GetCurrentWin32ThreadId();

#region Log Functions

Expand Down Expand Up @@ -114,11 +114,11 @@ public static void Init(string filePath = "")
{
LogFilePath = filePath == ""
? $"{Assembly.GetCallingAssembly().GetName()}" +
$"_{DateTime.Now.ToString("yyyyMMdd-HHmmss.ffffff")}.log"
$"_{DateTime.Now:yyyyMMdd-HHmmss.ffffff}.log"
: filePath;

LogFilePath = Path.GetFullPath(filePath);
var dir = Path.GetFullPath(Path.GetDirectoryName(LogFilePath));
var dir = Path.GetFullPath(Path.GetDirectoryName(LogFilePath) ?? string.Empty);

var loggingPathDidntExist = false;
if (!Directory.Exists(dir))
Expand All @@ -142,7 +142,7 @@ public static void Init(string filePath = "")
private static string FormatToLogMessage(string message, string level, int lineNumber, string filePath,
string memberName)
{
return $"{level}{DateTime.Now.ToString("yyyyMMdd HH:mm:ss.ffffff")} " +
return $"{level}{DateTime.Now:yyyyMMdd HH:mm:ss.ffffff} " +
$"{GetCurrentWin32ThreadId(),5:#####} {Path.GetFileName(filePath)}::{memberName}:{lineNumber}] {message}";
}

Expand Down

0 comments on commit 91c9ec1

Please sign in to comment.