2021-08-23 16:50:18 +02:00
using System ;
using System.Diagnostics ;
using System.IO ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
using Flowframes.IO ;
2022-07-24 22:30:30 +02:00
using Flowframes.Ui ;
2021-08-23 16:50:18 +02:00
namespace Flowframes.Os
{
class StartupChecks
{
static bool IsWin10Or11 ( )
{
2024-08-13 12:04:17 +02:00
string winVer = OsUtils . GetWindowsVer ( ) ;
2021-08-23 16:50:18 +02:00
2024-08-13 12:04:17 +02:00
if ( winVer . IsEmpty ( ) )
return true ; // If it fails, return true for future-proofing
2021-08-23 16:50:18 +02:00
2024-08-13 12:04:17 +02:00
return winVer . Lower ( ) . Contains ( "windows 10" ) | | winVer . Lower ( ) . Contains ( "windows 11" ) ;
2021-08-23 16:50:18 +02:00
}
public static void CheckOs ( )
{
2024-08-13 12:04:17 +02:00
if ( ! File . Exists ( Paths . GetVerPath ( ) ) & & Paths . GetExeDir ( ) . Lower ( ) . Contains ( "\\temp" ) )
2021-08-23 16:50:18 +02:00
{
2024-08-13 12:04:17 +02:00
UiUtils . ShowMessageBox ( "You seem to be running Flowframes out of a compressed archive.\nPlease extract the whole archive first!" , UiUtils . MessageType . Error ) ;
2021-08-23 16:50:18 +02:00
IoUtils . TryDeleteIfExists ( Paths . GetDataPath ( ) ) ;
Application . Exit ( ) ;
}
2024-08-13 15:45:44 +02:00
// Attempt to create an empty new folder in exe dir to check if we have permissions
try
{
string testDir = Path . Combine ( Paths . GetExeDir ( ) , "test.tmp" ) ;
Directory . CreateDirectory ( Path . Combine ( Paths . GetExeDir ( ) , testDir ) ) ;
Directory . Delete ( Path . Combine ( Paths . GetExeDir ( ) , testDir ) ) ;
}
catch ( Exception e )
{
UiUtils . ShowMessageBox ( $"Flowframes does not have permission to write to its own directory!\nPlease move it to a different folder.\n\nCurrent install directory: {Paths.GetExeDir()}" , UiUtils . MessageType . Error ) ;
Application . Exit ( ) ;
}
2024-08-13 12:04:17 +02:00
string winVer = OsUtils . GetWindowsVer ( ) ;
Logger . Log ( $"Running {winVer}" , true ) ;
2021-08-23 16:50:18 +02:00
2024-08-13 12:04:17 +02:00
if ( ! Environment . Is64BitOperatingSystem & & ! Config . GetBool ( "allow32Bit" , false ) )
2021-08-23 16:50:18 +02:00
{
2024-08-13 12:04:17 +02:00
UiUtils . ShowMessageBox ( "This application is not compatible with 32-bit operating systems!" , UiUtils . MessageType . Error ) ;
2021-08-23 16:50:18 +02:00
Application . Exit ( ) ;
}
2024-08-13 12:04:17 +02:00
if ( winVer . IsEmpty ( ) )
2021-08-23 16:50:18 +02:00
return ;
2024-09-03 22:08:38 +02:00
if ( ! winVer . Lower ( ) . Contains ( "windows 10" ) & & ! winVer . Lower ( ) . Contains ( "windows 11" ) & & ! Config . GetBool ( "ignoreIncompatibleOs" , false ) )
2021-08-23 16:50:18 +02:00
{
2024-08-13 12:04:17 +02:00
UiUtils . ShowMessageBox ( $"This application was made for Windows 10/11 and is not officially compatible with {winVer}.\n\n" +
$"Use it at your own risk and do NOT ask for support as long as your are on {winVer}." , UiUtils . MessageType . Warning ) ;
2021-08-23 16:50:18 +02:00
}
}
public static async Task SymlinksCheck ( )
{
if ( ! IsWin10Or11 ( ) )
return ;
bool silent = Config . GetBool ( "silentDevmodeCheck" , true ) ;
string ver = Updater . GetInstalledVer ( ) . ToString ( ) ;
bool symlinksAllowed = Symlinks . SymlinksAllowed ( ) ;
2024-08-13 12:04:17 +02:00
Logger . Log ( $"Symlinks allowed: {symlinksAllowed}" , true ) ;
2021-08-23 16:50:18 +02:00
if ( ! symlinksAllowed & & Config . Get ( Config . Key . askedForDevModeVersion ) ! = ver )
{
if ( ! silent )
{
2022-07-24 22:30:30 +02:00
UiUtils . ShowMessageBox ( "Flowframes will now enable Windows' Developer Mode which is required for video encoding improvements.\n\n" +
"This requires administrator privileges once." , UiUtils . MessageType . Message ) ;
2021-08-23 16:50:18 +02:00
}
Logger . Log ( $"Trying to enable dev mode." , true ) ;
string devmodeBatchPath = Path . Combine ( Paths . GetDataPath ( ) , "devmode.bat" ) ;
File . WriteAllText ( devmodeBatchPath , Properties . Resources . devmode ) ;
Process devmodeProc = OsUtils . NewProcess ( true ) ;
devmodeProc . StartInfo . Arguments = $"/C {devmodeBatchPath.Wrap()}" ;
devmodeProc . Start ( ) ;
while ( ! devmodeProc . HasExited ) await Task . Delay ( 100 ) ;
bool symlinksWorksNow = false ;
for ( int retries = 8 ; retries > 0 ; retries - - )
{
symlinksWorksNow = Symlinks . SymlinksAllowed ( ) ;
if ( symlinksWorksNow )
break ;
await Task . Delay ( 500 ) ;
}
if ( ! symlinksWorksNow )
{
if ( ! silent )
{
2022-07-24 22:30:30 +02:00
UiUtils . ShowMessageBox ( "Failed to enable developer mode - Perhaps you do not have sufficient privileges.\n\n" +
2021-08-23 16:50:18 +02:00
"Without Developer Mode, video encoding will be noticably slower.\n\nYou can still try enabling " +
2022-07-24 22:30:30 +02:00
"it manually in the Windows 10 Settings:\nSettings -> Update & security -> For developers -> Developer mode." , UiUtils . MessageType . Message ) ;
2021-08-23 16:50:18 +02:00
}
Logger . Log ( "Failed to enable dev mode." , true ) ;
Config . Set ( "askedForDevModeVersion" , ver ) ;
}
else
{
Logger . Log ( "Enabled Windows Developer Mode." , silent ) ;
}
IoUtils . TryDeleteIfExists ( devmodeBatchPath ) ;
}
}
}
}