2021-04-25 22:59:36 -05:00
using OwlCore.AbstractUI.Models ;
using System ;
using System.Collections.Generic ;
2021-05-30 23:48:48 -05:00
using System.Diagnostics ;
2021-04-25 22:59:36 -05:00
using System.IO ;
2021-05-26 11:32:53 -05:00
using System.Security.AccessControl ;
using System.Security.Principal ;
2021-04-25 22:59:36 -05:00
using System.Threading.Tasks ;
2021-05-26 11:32:53 -05:00
using ZuneModCore.Win32 ;
2021-04-25 22:59:36 -05:00
namespace ZuneModCore.Mods
{
public class VideoSyncMod : Mod
{
2021-05-06 04:53:13 -05:00
private readonly string WMVCORE_PATH = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . System ), "WMVCORE.dll" );
2021-04-26 05:09:48 -05:00
2021-04-25 22:59:36 -05:00
public override string Title => "Fix Video Sync" ;
public override string Description =>
"Resolves \"Error C00D11CD\" when attempting to sync video to a Zune device using Windows 10 1607 (Anniversary Update) or newer" ;
2021-04-26 18:05:19 -05:00
public override string Author => "ส็็็Codix#4833" ;
2021-04-25 22:59:36 -05:00
public override string Id => nameof ( VideoSyncMod );
2021-04-26 04:28:43 -05:00
public override AbstractUIElementGroup ? OptionsUI => null ;
2021-04-25 22:59:36 -05:00
2021-05-26 11:32:53 -05:00
#pragma warning disable CA1416 // Validate platform compatibility
2021-04-26 04:28:43 -05:00
public override Task < string? > Apply ()
2021-04-25 22:59:36 -05:00
{
2021-05-30 23:48:48 -05:00
// Make a backup of the original file
FileVersionInfo wmvDllVersionInfo = FileVersionInfo . GetVersionInfo ( WMVCORE_PATH );
if ( Version . Parse ( wmvDllVersionInfo . ProductVersion !) <= new Version ( 12 , 0 , 10586 , 0 ))
File . Copy ( WMVCORE_PATH , Path . Combine ( StorageDirectory , "WMVCORE.original.dll" ), true );
2021-05-26 11:32:53 -05:00
// Get the working WMVCORE.dll
string sourcePath = Path . Combine (
Path . GetDirectoryName ( System . Reflection . Assembly . GetExecutingAssembly (). Location )!, "Resources\\WMVCORE.dll" );
// Get the original WMVCORE.dll
FileInfo wmvDllInfo = new ( WMVCORE_PATH );
2021-04-26 05:09:48 -05:00
try
{
2021-05-26 11:32:53 -05:00
// 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
2021-04-26 04:28:43 -05:00
2021-05-26 11:32:53 -05:00
// Get access control
FileSecurity security = wmvDllInfo . GetAccessControl ();
SecurityIdentifier ? cu = WindowsIdentity . GetCurrent (). User ;
if ( cu == null )
return Task . FromResult < string? >( "Failed to set permissions on WMVCORE.dll, current user was null" );
2021-04-26 05:18:40 -05:00
2021-05-26 11:32:53 -05:00
// 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 );
2021-05-06 05:06:43 -05:00
2021-04-26 05:09:48 -05:00
// Copy the pre-Anniversary Update WMVCORE.dll
2021-05-06 05:06:43 -05:00
File . Copy ( sourcePath , WMVCORE_PATH , true );
2021-04-26 05:09:48 -05:00
return Task . FromResult < string? >( null );
}
2021-05-26 11:32:53 -05:00
catch ( IOException )
{
return Task . FromResult < string? >( $"Unable to replace '{wmvDllInfo.FullName}'. Verify that Zune and Windows Media Player are not running and try again." );
}
2021-04-26 05:09:48 -05:00
catch ( Exception ex )
{
return Task . FromResult ( ex . Message )!;
}
2021-04-25 22:59:36 -05:00
}
2021-05-26 11:32:53 -05:00
#pragma warning restore CA1416 // Validate platform compatibility
2021-04-25 22:59:36 -05:00
2021-04-26 04:28:43 -05:00
public override Task < string? > Reset ()
2021-04-25 22:59:36 -05:00
{
2021-04-26 17:02:21 -05:00
try
{
// Copy backup to application folder
File . Copy ( Path . Combine ( StorageDirectory , "WMVCORE.original.dll" ), WMVCORE_PATH , true );
2021-04-26 05:09:48 -05:00
2021-04-26 17:02:21 -05:00
return Task . FromResult < string? >( null );
}
catch ( Exception ex )
{
return Task . FromResult ( ex . Message )!;
}
2021-04-25 22:59:36 -05:00
}
public override IReadOnlyList < Type >? DependentMods => null ;
}
}