diff --git a/ZuneModCore/Mods/VideoSyncMod.cs b/ZuneModCore/Mods/VideoSyncMod.cs index fb0f504..b651773 100644 --- a/ZuneModCore/Mods/VideoSyncMod.cs +++ b/ZuneModCore/Mods/VideoSyncMod.cs @@ -23,7 +23,6 @@ namespace ZuneModCore.Mods public override string Id => nameof(VideoSyncMod); -#pragma warning disable CA1416 // Validate platform compatibility public override async Task Apply() { // Make a backup of the original file @@ -39,23 +38,8 @@ namespace ZuneModCore.Mods try { - // Activate necessary admin privileges to make changes without NTFS perms - TokenManipulator.AddPrivilege("SeRestorePrivilege"); // Necessary to set Owner Permissions - TokenManipulator.AddPrivilege("SeBackupPrivilege"); // Necessary to bypass Traverse Checking - TokenManipulator.AddPrivilege("SeTakeOwnershipPrivilege"); // Necessary to override FilePermissions - - // Get access control - FileSecurity security = wmvDllInfo.GetAccessControl(); - SecurityIdentifier? cu = WindowsIdentity.GetCurrent().User; - if (cu == null) - return "Failed to set permissions on WMVCORE.dll, current user was null."; - - // Set owner to current user - security.SetOwner(cu); - security.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.Modify, AccessControlType.Allow)); - - // Update the Access Control on the original WMVCORE.dll - wmvDllInfo.SetAccessControl(security); + // Take ownership of DLL + TokenManipulator.TakeOwnership(wmvDllInfo); // Replace with pre-Anniversary Update WMVCORE.dll await File.WriteAllBytesAsync(wmvDllInfo.FullName, wmvDllAniv); @@ -71,7 +55,6 @@ namespace ZuneModCore.Mods return ex.Message; } } -#pragma warning restore CA1416 // Validate platform compatibility public override Task Reset() { diff --git a/ZuneModCore/Mods/WebservicesMod.cs b/ZuneModCore/Mods/WebservicesMod.cs index ff73e73..9160070 100644 --- a/ZuneModCore/Mods/WebservicesMod.cs +++ b/ZuneModCore/Mods/WebservicesMod.cs @@ -50,7 +50,7 @@ namespace ZuneModCore.Mods // Make a backup if it doesn't already exist FileInfo zsDllBackupInfo = new(Path.Combine(StorageDirectory, "ZuneService.original.dll")); - if (zsDllBackupInfo.Exists) + if (!zsDllBackupInfo.Exists) { File.Copy(zsDllInfo.FullName, zsDllBackupInfo.FullName, true); } diff --git a/ZuneModCore/Win32/TokenManipulator.cs b/ZuneModCore/Win32/TokenManipulator.cs index a7fd6d8..b9bb82f 100644 --- a/ZuneModCore/Win32/TokenManipulator.cs +++ b/ZuneModCore/Win32/TokenManipulator.cs @@ -1,5 +1,8 @@ using System; +using System.IO; using System.Runtime.InteropServices; +using System.Security.AccessControl; +using System.Security.Principal; namespace ZuneModCore.Win32 { @@ -69,5 +72,43 @@ namespace ZuneModCore.Win32 throw; } } + +#pragma warning disable CA1416 // Validate platform compatibility + public static void TakeOwnership(FileInfo file) + { + // Activate necessary admin privileges to make changes without NTFS perms + AddPrivilege("SeRestorePrivilege"); // Necessary to set Owner Permissions + AddPrivilege("SeBackupPrivilege"); // Necessary to bypass Traverse Checking + AddPrivilege("SeTakeOwnershipPrivilege"); // Necessary to override FilePermissions + + // Get access control + FileSecurity security = file.GetAccessControl(); + SecurityIdentifier? cu = WindowsIdentity.GetCurrent().User; + + // Set owner to current user + security.SetOwner(cu); + security.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.Modify, AccessControlType.Allow)); + + // Update the Access Control on the original WMVCORE.dll + file.SetAccessControl(security); + } +#pragma warning restore CA1416 // Validate platform compatibility + + + public static bool TryTakeOwnership(FileInfo file, out Exception? exception) + { + try + { + TakeOwnership(file); + + exception = null; + return true; + } + catch (Exception ex) + { + exception = ex; + return false; + } + } } } diff --git a/ZuneModdingHelper/MainWindow.xaml.cs b/ZuneModdingHelper/MainWindow.xaml.cs index 7296876..ac306a0 100644 --- a/ZuneModdingHelper/MainWindow.xaml.cs +++ b/ZuneModdingHelper/MainWindow.xaml.cs @@ -6,13 +6,9 @@ using Microsoft.AppCenter.Analytics; using Microsoft.WindowsAPICodePack.Dialogs; using Newtonsoft.Json.Linq; using OwlCore.AbstractUI.Models; -using OwlCore.AbstractUI.ViewModels; using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; -using System.Linq; -using System.Threading.Tasks; using System.Windows; using System.Windows.Navigation; using ZuneModCore;