Skip to content

Commit

Permalink
Corrected possible crash "Environment variable path temp" 😉
Browse files Browse the repository at this point in the history
  • Loading branch information
Inestic committed Jun 30, 2022
1 parent ee20fc0 commit 2f24b3d
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 69 deletions.
3 changes: 2 additions & 1 deletion SophiApp/SophiApp/Controls/CheckBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Status}" Value="DISABLED">
<Setter Property="IsEnabled" Value="False" />
<Setter TargetName="BorderCheckBox" Property="Visibility" Value="Hidden" />
<Setter TargetName="BorderCheckBox" Property="BorderBrush" Value="{DynamicResource Brush.Window.Foreground}" />
<Setter TargetName="BorderCheckBox" Property="Background" Value="{DynamicResource Brush.View.Background}" />
<Setter TargetName="PathCheckBox" Property="Visibility" Value="Hidden" />
<Setter Property="Opacity" Value="0.50" />
</DataTrigger>
Expand Down
2 changes: 1 addition & 1 deletion SophiApp/SophiApp/Customisations/CustomisationConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ function MinimizeWindow
};

internal static readonly string _504_MS_STORE_RESET_EXE = $@"{Environment.GetFolderPath(Environment.SpecialFolder.System)}\WSReset.exe";
internal static readonly IEnumerable<string> _700_VOLUME_CACHES_NAMES = new string[] { "Delivery Optimization Files", "Device Driver Packages", "Language Pack", "Previous Installations", "Setup Log Files", "System error memory dump files", "System error minidump files", "Temporary Setup Files", "Update Cleanup", "Windows Defender", "Windows ESD installation files", "Windows Upgrade Log Files" };
internal static readonly IEnumerable<string> _700_VOLUME_CACHES_NAMES = new string[] { "Delivery Optimization Files", "BranchCache", "Device Driver Packages", "Language Pack", "Previous Installations", "Setup Log Files", "System error memory dump files", "System error minidump files", "Temporary Setup Files", "Update Cleanup", "Windows Defender", "Windows ESD installation files", "Windows Upgrade Log Files" };
internal static readonly string _805_EVENT_VIEWS_PATH = $@"{Environment.GetEnvironmentVariable("ProgramData")}\Microsoft\Event Viewer\Views";
internal static readonly string _811_WINDOWS_SANDBOX_EXE = $@"{Environment.GetFolderPath(Environment.SpecialFolder.System)}\WindowsSandbox.exe";
internal static readonly string _922_MS_WORDPAD_EXE = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)}\Windows NT\Accessories\wordpad.exe";
Expand Down
128 changes: 67 additions & 61 deletions SophiApp/SophiApp/Customisations/CustomisationOs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,80 +593,86 @@ public static void _303(bool _)

public static void _307(bool _)
{
var localAppDataTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_LOCAL_APPDATA}\\{TEMP_FOLDER}");
var systemDriveTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_SYSTEM_DRIVE}\\{TEMP_FOLDER}");
var systemRootTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_SYSTEM_ROOT}\\{TEMP_FOLDER}");
var currentTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_TEMP}");
var userName = Environment.UserName;

ServiceHelper.Restart(SERVICE_SPOOLER);
OneDriveHelper.StopProcesses();
FileHelper.CreateDirectory(systemDriveTemp);
FileHelper.DirectoryLazyDelete(systemRootTemp);

if (FileHelper.IsSymbolicLink(currentTemp).Invert())
if (ServiceHelper.ServiceExist(SERVICE_SPOOLER))
{
FileHelper.DirectoryLazyDelete(currentTemp);

if (FileHelper.DirectoryIsEmpty(currentTemp))
{
FileHelper.TryDirectoryDelete(localAppDataTemp);
FileHelper.CreateDirectoryLink(localAppDataTemp, systemDriveTemp);
}
else
var localAppDataTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_LOCAL_APPDATA}\\{TEMP_FOLDER}");
var systemDriveTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_SYSTEM_DRIVE}\\{TEMP_FOLDER}");
var systemRootTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_SYSTEM_ROOT}\\{TEMP_FOLDER}");
var currentTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_TEMP}");
var userName = Environment.UserName;

ServiceHelper.TryRestart(SERVICE_SPOOLER);
OneDriveHelper.StopProcesses();
FileHelper.CreateDirectory(systemDriveTemp);
FileHelper.DirectoryLazyDelete(systemRootTemp);

if (FileHelper.IsSymbolicLink(currentTemp).Invert())
{
ScheduledTaskHelper.RegisterLogonTask(name: _307_SYMBOLIC_LINK_TASK, description: null, execute: POWERSHELL_EXE, arg: _307_TASK_ARGS, username: userName);
FileHelper.DirectoryLazyDelete(currentTemp);

if (FileHelper.DirectoryIsEmpty(currentTemp))
{
FileHelper.TryDirectoryDelete(localAppDataTemp);
FileHelper.CreateDirectoryLink(localAppDataTemp, systemDriveTemp);
}
else
{
ScheduledTaskHelper.RegisterLogonTask(name: _307_SYMBOLIC_LINK_TASK, description: null, execute: POWERSHELL_EXE, arg: _307_TASK_ARGS, username: userName);
}
}
}

Environment.SetEnvironmentVariable(TMP, systemDriveTemp, EnvironmentVariableTarget.User);
Environment.SetEnvironmentVariable(TMP, systemDriveTemp, EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable(TMP, systemDriveTemp, EnvironmentVariableTarget.Process);
RegHelper.SetValue(RegistryHive.CurrentUser, ENVIRONMENT, TMP, _307_SYSTEM_DRIVE_TEMP, RegistryValueKind.ExpandString);
Environment.SetEnvironmentVariable(TEMP, systemDriveTemp, EnvironmentVariableTarget.User);
Environment.SetEnvironmentVariable(TEMP, systemDriveTemp, EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable(TEMP, systemDriveTemp, EnvironmentVariableTarget.Process);
RegHelper.SetValue(RegistryHive.CurrentUser, ENVIRONMENT, TEMP, _307_SYSTEM_DRIVE_TEMP, RegistryValueKind.ExpandString);
RegHelper.SetValue(RegistryHive.LocalMachine, SESSION_MANAGER_ENVIRONMENT, TMP, systemDriveTemp, RegistryValueKind.ExpandString);
RegHelper.SetValue(RegistryHive.LocalMachine, SESSION_MANAGER_ENVIRONMENT, TEMP, systemDriveTemp, RegistryValueKind.ExpandString);
Environment.SetEnvironmentVariable(TMP, systemDriveTemp, EnvironmentVariableTarget.User);
Environment.SetEnvironmentVariable(TMP, systemDriveTemp, EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable(TMP, systemDriveTemp, EnvironmentVariableTarget.Process);
RegHelper.SetValue(RegistryHive.CurrentUser, ENVIRONMENT, TMP, _307_SYSTEM_DRIVE_TEMP, RegistryValueKind.ExpandString);
Environment.SetEnvironmentVariable(TEMP, systemDriveTemp, EnvironmentVariableTarget.User);
Environment.SetEnvironmentVariable(TEMP, systemDriveTemp, EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable(TEMP, systemDriveTemp, EnvironmentVariableTarget.Process);
RegHelper.SetValue(RegistryHive.CurrentUser, ENVIRONMENT, TEMP, _307_SYSTEM_DRIVE_TEMP, RegistryValueKind.ExpandString);
RegHelper.SetValue(RegistryHive.LocalMachine, SESSION_MANAGER_ENVIRONMENT, TMP, systemDriveTemp, RegistryValueKind.ExpandString);
RegHelper.SetValue(RegistryHive.LocalMachine, SESSION_MANAGER_ENVIRONMENT, TEMP, systemDriveTemp, RegistryValueKind.ExpandString);
}
}

public static void _308(bool _)
{
var localAppDataTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_LOCAL_APPDATA}\\{TEMP_FOLDER}");
var systemRootTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_SYSTEM_ROOT}\\{TEMP_FOLDER}");
var currentTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_TEMP}");
var userName = Environment.UserName;

ServiceHelper.Restart(SERVICE_SPOOLER);
OneDriveHelper.StopProcesses();
FileHelper.DirectoryLazyDelete(localAppDataTemp);
FileHelper.CreateDirectory(systemRootTemp, localAppDataTemp);

if (FileHelper.IsSymbolicLink(currentTemp).Invert())
if (ServiceHelper.ServiceExist(SERVICE_SPOOLER))
{
FileHelper.DirectoryLazyDelete(currentTemp);
var localAppDataTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_LOCAL_APPDATA}\\{TEMP_FOLDER}");
var systemRootTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_SYSTEM_ROOT}\\{TEMP_FOLDER}");
var currentTemp = Environment.ExpandEnvironmentVariables($"{ENVIRONMENT_TEMP}");
var userName = Environment.UserName;

if (Directory.Exists(currentTemp) && FileHelper.DirectoryIsEmpty(currentTemp))
{
FileHelper.TryDirectoryDelete(currentTemp);
}
else
ServiceHelper.TryRestart(SERVICE_SPOOLER);
OneDriveHelper.StopProcesses();
FileHelper.DirectoryLazyDelete(localAppDataTemp);
FileHelper.CreateDirectory(systemRootTemp, localAppDataTemp);

if (FileHelper.IsSymbolicLink(currentTemp).Invert())
{
ScheduledTaskHelper.RegisterLogonTask(name: _308_TEMPORARY_TASK, description: null, execute: POWERSHELL_EXE, arg: _308_TASK_ARGS, username: userName);
FileHelper.DirectoryLazyDelete(currentTemp);

if (Directory.Exists(currentTemp) && FileHelper.DirectoryIsEmpty(currentTemp))
{
FileHelper.TryDirectoryDelete(currentTemp);
}
else
{
ScheduledTaskHelper.RegisterLogonTask(name: _308_TEMPORARY_TASK, description: null, execute: POWERSHELL_EXE, arg: _308_TASK_ARGS, username: userName);
}
}
}

Environment.SetEnvironmentVariable(TMP, localAppDataTemp, EnvironmentVariableTarget.User);
Environment.SetEnvironmentVariable(TMP, systemRootTemp, EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable(TMP, localAppDataTemp, EnvironmentVariableTarget.Process);
RegHelper.SetValue(RegistryHive.CurrentUser, ENVIRONMENT, TMP, _308_APPDATA_TEMP, RegistryValueKind.ExpandString);
Environment.SetEnvironmentVariable(TEMP, localAppDataTemp, EnvironmentVariableTarget.User);
Environment.SetEnvironmentVariable(TEMP, systemRootTemp, EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable(TEMP, localAppDataTemp, EnvironmentVariableTarget.Process);
RegHelper.SetValue(RegistryHive.CurrentUser, ENVIRONMENT, TEMP, _308_APPDATA_TEMP, RegistryValueKind.ExpandString);
RegHelper.SetValue(RegistryHive.LocalMachine, SESSION_MANAGER_ENVIRONMENT, TMP, systemRootTemp, RegistryValueKind.ExpandString);
RegHelper.SetValue(RegistryHive.LocalMachine, SESSION_MANAGER_ENVIRONMENT, TEMP, systemRootTemp, RegistryValueKind.ExpandString);
Environment.SetEnvironmentVariable(TMP, localAppDataTemp, EnvironmentVariableTarget.User);
Environment.SetEnvironmentVariable(TMP, systemRootTemp, EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable(TMP, localAppDataTemp, EnvironmentVariableTarget.Process);
RegHelper.SetValue(RegistryHive.CurrentUser, ENVIRONMENT, TMP, _308_APPDATA_TEMP, RegistryValueKind.ExpandString);
Environment.SetEnvironmentVariable(TEMP, localAppDataTemp, EnvironmentVariableTarget.User);
Environment.SetEnvironmentVariable(TEMP, systemRootTemp, EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable(TEMP, localAppDataTemp, EnvironmentVariableTarget.Process);
RegHelper.SetValue(RegistryHive.CurrentUser, ENVIRONMENT, TEMP, _308_APPDATA_TEMP, RegistryValueKind.ExpandString);
RegHelper.SetValue(RegistryHive.LocalMachine, SESSION_MANAGER_ENVIRONMENT, TMP, systemRootTemp, RegistryValueKind.ExpandString);
RegHelper.SetValue(RegistryHive.LocalMachine, SESSION_MANAGER_ENVIRONMENT, TEMP, systemRootTemp, RegistryValueKind.ExpandString);
}
}

public static void _309(bool IsChecked) => RegHelper.SetValue(RegistryHive.LocalMachine,
Expand Down
8 changes: 5 additions & 3 deletions SophiApp/SophiApp/Customisations/CustomisationStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,11 @@ public static bool _358() => DotNetHelper.IsInstalled("windowsdesktop-runtime-6.
: throw new FileNotExistException("windowsdesktop-runtime-6.*-win-x64.exe");

public static bool _500() => HttpHelper.IsOnline
? UwpHelper.PackageExist(UWP_MS_WIN_PHOTOS)
? UwpHelper.PackageExist(_500_UWP_HEVC_VIDEO)
: throw new UwpAppFoundException(UWP_MS_WIN_PHOTOS)
? OsHelper.GetBuild() < 22517
? UwpHelper.PackageExist(UWP_MS_WIN_PHOTOS)
? UwpHelper.PackageExist(_500_UWP_HEVC_VIDEO)
: throw new UwpAppFoundException(UWP_MS_WIN_PHOTOS)
: throw new WindowsBuildNotSupportedException()
: throw new NoInternetConnectionException();

public static bool _501() => UwpHelper.PackageExist(UWP_MS_CORTANA)
Expand Down
7 changes: 7 additions & 0 deletions SophiApp/SophiApp/Helpers/ExceptionsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ public WddmMinimalVersionException(string minimumVersion, string currentVersion)
}
}

internal class WindowsBuildNotSupportedException : Exception
{
public WindowsBuildNotSupportedException() : base("This OS build is not supported")
{
}
}

internal class WindowsCapabilityNotInstalledException : Exception
{
public WindowsCapabilityNotInstalledException(string name) : base($"The Windows capability {name} wasn't installed in this OS")
Expand Down
15 changes: 15 additions & 0 deletions SophiApp/SophiApp/Helpers/ServiceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ internal static void Restart(string serviceName)

internal static bool ServiceExist(string serviceName) => ServiceController.GetServices().Any(service => service.ServiceName == serviceName);

internal static void TryRestart(string serviceName)
{
try
{
var service = Get(serviceName);
if (service.StartType != ServiceStartMode.Disabled)
{
Restart(serviceName);
}
}
catch (Exception)
{
}
}

public static void SetStartMode(ServiceController svc, ServiceStartMode mode)
{
var scManagerHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
Expand Down
2 changes: 1 addition & 1 deletion SophiApp/SophiApp/Localizations/IT.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@
<system:String x:Key="Localization.Customizations.RebootRequired">riavvia il computer dopo aver Applicato le impostazioni</system:String>
<system:String x:Key="Localization.Loading">Leggendo le impostazioni correnti...</system:String>
<system:String x:Key="WindowsTerminal.OpenIsAdmin">Apri in Windows Terminal</system:String>
</ResourceDictionary>
</ResourceDictionary>
4 changes: 2 additions & 2 deletions SophiApp/SophiApp/Localizations/TR.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<system:String x:Key="Localization.CleanupTask.NotificationTask.Run">Çalıştır</system:String>
<system:String x:Key="Localization.Build.Name">[Helen]</system:String>
<system:String x:Key="Localization.About.MadeWith">Windows&#174;</system:String>
<system:String x:Key="Localization.About.OfWindows">ile hazırlandı. </system:String>
<system:String x:Key="Localization.About.OfWindows">ile hazırlandı.</system:String>
<system:String x:Key="Localization.Settings.GitHub">GitHub</system:String>
<system:String x:Key="Localization.Settings.Themes">Tema</system:String>
<system:String x:Key="Localization.Settings.Themes.Dark">Karanlık</system:String>
Expand Down Expand Up @@ -96,4 +96,4 @@
<system:String x:Key="Localization.Customizations.RebootRequired">Ayarları uyguladıktan sonra lütfen bilgisayarınızı yeniden başlatın</system:String>
<system:String x:Key="Localization.Loading">Geçerli ayarlar okunuyor...</system:String>
<system:String x:Key="WindowsTerminal.OpenIsAdmin">Windows Terminalinde Aç</system:String>
</ResourceDictionary>
</ResourceDictionary>

0 comments on commit 2f24b3d

Please sign in to comment.