DebugViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using PixiEditor.Helpers;
  2. using PixiEditor.Models.Commands.Attributes;
  3. using PixiEditor.Models.Dialogs;
  4. using PixiEditor.Models.UserPreferences;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Reflection;
  8. namespace PixiEditor.ViewModels.SubViewModels.Main
  9. {
  10. [Command.Group("PixiEditor.Debug", "Debug")]
  11. public class DebugViewModel : SubViewModel<ViewModelMain>
  12. {
  13. public bool IsDebugBuild { get; set; }
  14. public bool IsDebugModeEnabled { get; set; }
  15. private bool useDebug;
  16. public bool UseDebug
  17. {
  18. get => useDebug;
  19. set => SetProperty(ref useDebug, value);
  20. }
  21. public DebugViewModel(ViewModelMain owner, IPreferences preferences)
  22. : base(owner)
  23. {
  24. SetDebug();
  25. preferences.AddCallback<bool>("IsDebugModeEnabled", UpdateDebugMode);
  26. UpdateDebugMode(preferences.GetPreference<bool>("IsDebugModeEnabled"));
  27. }
  28. [Command.Debug("PixiEditor.Debug.OpenTempDirectory", "%Temp%/PixiEditor", "Open Temp Directory", "Open Temp Directory")]
  29. [Command.Debug("PixiEditor.Debug.OpenLocalAppDataDirectory", "%LocalAppData%/PixiEditor", "Open Local AppData Directory", "Open Local AppData Directory")]
  30. [Command.Debug("PixiEditor.Debug.OpenRoamingAppDataDirectory", "%AppData%/PixiEditor", "Open Roaming AppData Directory", "Open Roaming AppData Directory")]
  31. [Command.Debug("PixiEditor.Debug.OpenCrashReportsDirectory", "%LocalAppData%/PixiEditor/crash_logs", "Open Crash Reports Directory", "Open Crash Reports Directory")]
  32. public static void OpenFolder(string path)
  33. {
  34. if (!Directory.Exists(path))
  35. {
  36. NoticeDialog.Show($"{path} does not exist.", "Location does not exist");
  37. return;
  38. }
  39. ProcessHelpers.ShellExecuteEV(path);
  40. }
  41. [Command.Debug("PixiEditor.Debug.OpenInstallDirectory", "Open Installation Directory", "Open Installation Directory")]
  42. public static void OpenInstallLocation()
  43. {
  44. ProcessHelpers.ShellExecuteEV(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
  45. }
  46. [Command.Debug("PixiEditor.Debug.Crash", "Crash", "Crash Application")]
  47. public static void Crash() => throw new InvalidOperationException("User requested to crash :c");
  48. [Command.Debug("PixiEditor.Debug.DeleteUserPreferences", @"%appdata%\PixiEditor\user_preferences.json", "Delete User Preferences (Roaming)", "Delete User Preferences (Roaming AppData)")]
  49. [Command.Debug("PixiEditor.Debug.DeleteShortcutFile", @"%appdata%\PixiEditor\shortcuts.json", "Delete Shortcut File (Roaming)", "Delete Shortcut File (Roaming AppData)")]
  50. [Command.Debug("PixiEditor.Debug.DeleteEditorData", @"%localappdata%\PixiEditor\editor_data.json", "Delete Editor Data (Local)", "Delete Editor Data (Local AppData)")]
  51. public static void DeleteFile(string path)
  52. {
  53. string file = Environment.ExpandEnvironmentVariables(path);
  54. if (!File.Exists(file))
  55. {
  56. NoticeDialog.Show($"File {path} does not exist\n(Full Path: {file})", "File not found");
  57. return;
  58. }
  59. OptionsDialog<string> dialog = new("Are you sure?", $"Are you sure you want to delete {path}?\nThis data will be lost for all installations.\n(Full Path: {file})")
  60. {
  61. { "Yes", x => File.Delete(file) },
  62. "Cancel"
  63. };
  64. dialog.ShowDialog();
  65. }
  66. [Conditional("DEBUG")]
  67. private void SetDebug() => IsDebugBuild = true;
  68. private void UpdateDebugMode(bool setting)
  69. {
  70. IsDebugModeEnabled = setting;
  71. UseDebug = IsDebugBuild || IsDebugModeEnabled;
  72. }
  73. }
  74. }