upload
This commit is contained in:
commit
1c7a8cf25f
579 files changed
+83196
No files matched your search
@@ -0,0 +1,186 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using VibranceHud.Customize;
|
||||
using VibranceHud.Monitors;
|
||||
using VibranceHud.Zoom;
|
||||
|
||||
namespace VibranceHud.Startup;
|
||||
|
||||
public static class ColdStartRestore
|
||||
{
|
||||
public static bool ShouldResetTheTaskbar(bool lastRunDied, bool aStyleWasTurnedOn, bool styleStillWanted, bool styleStillAllowed)
|
||||
{
|
||||
if (lastRunDied & aStyleWasTurnedOn)
|
||||
{
|
||||
return !(styleStillWanted & styleStillAllowed);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void PutTheScreenBack(Action gammaLinear, Action magIdentity, Action driverBaseline, Action taskbarNormal, bool resetTaskbar)
|
||||
{
|
||||
Guarded(gammaLinear);
|
||||
Guarded(magIdentity);
|
||||
Guarded(driverBaseline);
|
||||
if (resetTaskbar)
|
||||
{
|
||||
Guarded(taskbarNormal);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RunIfTheLastRunDied(AppSettings settings, SettingsStore store)
|
||||
{
|
||||
RunIfTheLastRunDied(DirtyExitMarker.Shared, settings, () =>
|
||||
{
|
||||
store.Save(settings);
|
||||
}, () => RecoveryGamma(settings, () =>
|
||||
{
|
||||
store.Save(settings);
|
||||
}), () => new MagOverlay().HeldForTheGoodbye(), TrayApplicationContext.CreateVibranceController, TaskbarNormal, null, ZoomEmergency.Reset);
|
||||
}
|
||||
|
||||
internal static void RunIfTheLastRunDied(DirtyExitMarker marker, AppSettings settings, Action save, Func<IDisposable> openGammaRamp, Func<IDisposable> openMagnifier, Func<IVibranceController> openDriver, Action taskbarNormal, Func<IReadOnlyList<ColourMonitor>>? monitorInventory = null, Action? zoomIdentity = null)
|
||||
{
|
||||
if (!marker.ClaimPreviousRunWasDirty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
PutTheScreenBack(() =>
|
||||
{
|
||||
if (settings.GammaRestorePending)
|
||||
{
|
||||
openGammaRamp().Dispose();
|
||||
AppSettings appSettings = settings;
|
||||
Dictionary<string, ushort[]> ownedMonitorGamma = settings.OwnedMonitorGamma;
|
||||
appSettings.GammaRestorePending = ownedMonitorGamma != null && ownedMonitorGamma.Count > 0;
|
||||
save();
|
||||
}
|
||||
}, () =>
|
||||
{
|
||||
openMagnifier().Dispose();
|
||||
zoomIdentity?.Invoke();
|
||||
}, () =>
|
||||
{
|
||||
if (!settings.NvapiRestorePending)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (settings.OwnedMonitorVibrance != null)
|
||||
{
|
||||
MonitorSelection selection = new MonitorSelection(settings, monitorInventory ?? new Func<IReadOnlyList<ColourMonitor>>(ColourMonitorInventory.Read));
|
||||
new SelectedVibranceController(openDriver(), selection, settings, save).RestoreOwned();
|
||||
settings.NvapiRestorePending = settings.OwnedMonitorVibrance.Count > 0;
|
||||
save();
|
||||
return;
|
||||
}
|
||||
if (settings.NvapiRestoreLevels.Count > 0 || DriverVibranceBaseline.RestoreOnExit(settings.NvapiRestoreLevel).HasValue)
|
||||
{
|
||||
IVibranceController driver = openDriver();
|
||||
int? driverVibranceWeLastWrote = settings.DriverVibranceWeLastWrote;
|
||||
if (driverVibranceWeLastWrote.HasValue)
|
||||
{
|
||||
int valueOrDefault = driverVibranceWeLastWrote.GetValueOrDefault();
|
||||
if (OutsideDriverChange.HoldsALevelOtherThan(driver, valueOrDefault))
|
||||
{
|
||||
goto IL_0131;
|
||||
}
|
||||
}
|
||||
int? driverVibranceWeLastWrote2 = DriverVibranceBaseline.RestoreEveryDisplay(driver, settings.NvapiRestoreLevels, settings.NvapiRestoreLevel);
|
||||
if (driverVibranceWeLastWrote2.HasValue)
|
||||
{
|
||||
settings.DriverVibranceWeLastWrote = driverVibranceWeLastWrote2;
|
||||
}
|
||||
}
|
||||
goto IL_0131;
|
||||
IL_0131:
|
||||
settings.NvapiRestorePending = false;
|
||||
save();
|
||||
}, taskbarNormal, ShouldResetTheTaskbar(lastRunDied: true, AStyleWasTurnedOn(settings), CustomizeService.ShouldReapplyTaskbarStyle(settings), styleStillAllowed: true));
|
||||
}
|
||||
|
||||
public static void LeavingBeforeTheTray(AppSettings settings)
|
||||
{
|
||||
LeavingBeforeTheTray(DirtyExitMarker.Shared, settings, TaskbarNormal);
|
||||
}
|
||||
|
||||
internal static void LeavingBeforeTheTray(DirtyExitMarker marker, AppSettings settings, Action taskbarNormal)
|
||||
{
|
||||
if (ShouldResetTheTaskbar(marker.ClaimPreviousRunWasDirty(), AStyleWasTurnedOn(settings), CustomizeService.ShouldReapplyTaskbarStyle(settings), styleStillAllowed: false))
|
||||
{
|
||||
Guarded(taskbarNormal);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool AStyleWasTurnedOn(AppSettings settings)
|
||||
{
|
||||
return CustomizeService.StyleFromName(settings.TaskbarStyleName) != TaskbarStyle.Normal;
|
||||
}
|
||||
|
||||
private static void TaskbarNormal()
|
||||
{
|
||||
CustomizeService.ApplyTaskbarStyle(new TaskbarSettings
|
||||
{
|
||||
Style = TaskbarStyle.Normal
|
||||
});
|
||||
}
|
||||
|
||||
public static Action GammaLinearNow(AppSettings settings, SettingsStore store)
|
||||
{
|
||||
return () =>
|
||||
{
|
||||
RecoveryGamma(settings, () =>
|
||||
{
|
||||
store.Save(settings);
|
||||
}).Dispose();
|
||||
AppSettings appSettings = settings;
|
||||
Dictionary<string, ushort[]> ownedMonitorGamma = settings.OwnedMonitorGamma;
|
||||
appSettings.GammaRestorePending = ownedMonitorGamma != null && ownedMonitorGamma.Count > 0;
|
||||
};
|
||||
}
|
||||
|
||||
public static Action DriverBaselineNow(AppSettings settings, SettingsStore store)
|
||||
{
|
||||
return () =>
|
||||
{
|
||||
if (settings.OwnedMonitorVibrance != null)
|
||||
{
|
||||
MonitorSelection selection = new MonitorSelection(settings, ColourMonitorInventory.Read);
|
||||
new SelectedVibranceController(TrayApplicationContext.CreateVibranceController(), selection, settings, () =>
|
||||
{
|
||||
store.Save(settings);
|
||||
}).RestoreOwned();
|
||||
}
|
||||
else if (settings.NvapiRestoreLevels.Count > 0 || DriverVibranceBaseline.RestoreOnExit(settings.NvapiRestoreLevel).HasValue)
|
||||
{
|
||||
DriverVibranceBaseline.RestoreEveryDisplay(TrayApplicationContext.CreateVibranceController(), settings.NvapiRestoreLevels, settings.NvapiRestoreLevel);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static MultiMonitorGammaRamp RecoveryGamma(AppSettings settings, Action save)
|
||||
{
|
||||
MultiMonitorGammaRamp multiMonitorGammaRamp = new MultiMonitorGammaRamp((string _) => (100, 0));
|
||||
if (settings.OwnedMonitorGamma != null)
|
||||
{
|
||||
multiMonitorGammaRamp.Selection = new MonitorSelection(settings, ColourMonitorInventory.Read);
|
||||
multiMonitorGammaRamp.AdoptRecoveryBaselines(settings.OwnedMonitorGamma);
|
||||
multiMonitorGammaRamp.DeviceReleased = (string id) =>
|
||||
{
|
||||
settings.OwnedMonitorGamma.Remove(id);
|
||||
save();
|
||||
};
|
||||
}
|
||||
return multiMonitorGammaRamp;
|
||||
}
|
||||
|
||||
private static void Guarded(Action call)
|
||||
{
|
||||
try
|
||||
{
|
||||
call();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Threading;
|
||||
|
||||
namespace VibranceHud.Startup;
|
||||
|
||||
internal static class CrashHandBack
|
||||
{
|
||||
private static MultiMonitorGammaRamp? _ramp;
|
||||
|
||||
internal static void Register(MultiMonitorGammaRamp ramp)
|
||||
{
|
||||
Volatile.Write(ref _ramp, ramp);
|
||||
}
|
||||
|
||||
internal static void Forget()
|
||||
{
|
||||
Volatile.Write(ref _ramp, null);
|
||||
}
|
||||
|
||||
internal static void Run()
|
||||
{
|
||||
MultiMonitorGammaRamp multiMonitorGammaRamp = Interlocked.Exchange(ref _ramp, null);
|
||||
if (multiMonitorGammaRamp == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
multiMonitorGammaRamp.Dispose();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace VibranceHud.Startup;
|
||||
|
||||
public sealed class DirtyExitMarker
|
||||
{
|
||||
private readonly Func<bool> _exists;
|
||||
|
||||
private readonly Action _write;
|
||||
|
||||
private readonly Action _delete;
|
||||
|
||||
private bool _asked;
|
||||
|
||||
private bool _wasDirty;
|
||||
|
||||
private bool _cleared;
|
||||
|
||||
public static DirtyExitMarker Shared { get; set; } = Inert();
|
||||
|
||||
public DirtyExitMarker(Func<bool> exists, Action write, Action delete)
|
||||
{
|
||||
_exists = exists;
|
||||
_write = write;
|
||||
_delete = delete;
|
||||
}
|
||||
|
||||
public bool ClaimPreviousRunWasDirty()
|
||||
{
|
||||
if (_asked)
|
||||
{
|
||||
return _wasDirty;
|
||||
}
|
||||
_asked = true;
|
||||
try
|
||||
{
|
||||
_wasDirty = _exists();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_wasDirty = false;
|
||||
}
|
||||
try
|
||||
{
|
||||
_write();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
return _wasDirty;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
if (!_asked || _cleared)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_cleared = true;
|
||||
try
|
||||
{
|
||||
_delete();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public static DirtyExitMarker Inert()
|
||||
{
|
||||
return new DirtyExitMarker(() => false, () =>
|
||||
{
|
||||
}, () =>
|
||||
{
|
||||
});
|
||||
}
|
||||
|
||||
public static DirtyExitMarker InLocalAppData()
|
||||
{
|
||||
string path = Path.Combine(AppFolders.Local, "running.marker");
|
||||
return new DirtyExitMarker(() => File.Exists(path), () =>
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
||||
File.WriteAllText(path, DateTime.UtcNow.ToString("O"));
|
||||
}, () =>
|
||||
{
|
||||
File.Delete(path);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using VibranceHud.Monitors;
|
||||
|
||||
namespace VibranceHud.Startup;
|
||||
|
||||
internal static class RestoreMonitorSwitch
|
||||
{
|
||||
internal const string Switch = "--restore-monitor";
|
||||
|
||||
internal const int DeadlineMs = 10000;
|
||||
|
||||
internal static int? TryRun(string[] args, Action? work = null, int timeoutMs = 10000)
|
||||
{
|
||||
if (args == null || !args.Any((string a) => string.Equals(a, "--restore-monitor", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
try
|
||||
{
|
||||
Action job = work ?? new Action(RealWork);
|
||||
Thread thread = new Thread(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
job();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
});
|
||||
thread.IsBackground = true;
|
||||
thread.Name = "restore-monitor";
|
||||
thread.Start();
|
||||
thread.Join(timeoutMs);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static void RealWork()
|
||||
{
|
||||
SettingsStore settingsStore = SettingsStore.Default();
|
||||
AppSettings appSettings = settingsStore.Load();
|
||||
MonitorPanicRestore.Run(appSettings, settingsStore, new Dxva2MonitorWriter(), MonitorProbe.Probe, ColdStartRestore.GammaLinearNow(appSettings, settingsStore), ColdStartRestore.DriverBaselineNow(appSettings, settingsStore));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Security;
|
||||
|
||||
namespace VibranceHud.Startup;
|
||||
|
||||
public static class StartupFailure
|
||||
{
|
||||
public const string MonitorSentence = "Make sure at least one monitor is connected and try again.";
|
||||
|
||||
public const string CarriedOn = "PlexusX hit a problem, but it is still running.\n\nWhat you just did may not have worked. If PlexusX acts strange, close it and open it again.";
|
||||
|
||||
public const string HadToClose = "PlexusX hit a problem and had to close.\n\nOpen PlexusX again. If this keeps happening, email [email protected].";
|
||||
|
||||
public const string StuckCopyQuestion = "PlexusX is already open, but it is stuck.\n\nThis can happen when a game or your graphics card crashes.\n\nClose the stuck copy and start PlexusX again?";
|
||||
|
||||
public const string StuckCopyWouldNotClose = "PlexusX couldn't close the stuck copy.\n\nPress Ctrl+Shift+Esc to open Task Manager. Pick PlexusX, press End task, then start PlexusX again.";
|
||||
|
||||
public const string StuckCopyClosed = "The stuck copy is closed. Start PlexusX again.";
|
||||
|
||||
private const string Opening = "PlexusX couldn't start.";
|
||||
|
||||
private const string Closing = "If this keeps happening, email [email protected].";
|
||||
|
||||
public static string Describe(Exception? ex)
|
||||
{
|
||||
if (ex is StartupStepFailed startupStepFailed)
|
||||
{
|
||||
if (startupStepFailed.Step == "setting up the monitors attached to this PC" && !IsAFileThatWouldNotLoad(startupStepFailed.InnerException))
|
||||
{
|
||||
return "PlexusX couldn't start.\n\nPlexusX could not set up the monitors attached to this PC. Make sure at least one monitor is connected and try again.\n\nIf this keeps happening, email [email protected].";
|
||||
}
|
||||
return "PlexusX couldn't start.\n\nIt stopped while " + startupStepFailed.Step + ". " + Because(startupStepFailed.InnerException) + "\n\nIf this keeps happening, email [email protected].";
|
||||
}
|
||||
return "PlexusX couldn't start.\n\n" + Because(ex) + "\n\nIf this keeps happening, email [email protected].";
|
||||
}
|
||||
|
||||
public static Exception? Detail(Exception? ex)
|
||||
{
|
||||
if (!(ex is StartupStepFailed { InnerException: not null } startupStepFailed))
|
||||
{
|
||||
return ex;
|
||||
}
|
||||
return startupStepFailed.InnerException;
|
||||
}
|
||||
|
||||
private static bool IsAFileThatWouldNotLoad(Exception? ex)
|
||||
{
|
||||
if (ex is FileNotFoundException || ex is FileLoadException || ex is BadImageFormatException || ex is TypeLoadException)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static string Because(Exception? ex)
|
||||
{
|
||||
if (!(ex is UnauthorizedAccessException) && !(ex is SecurityException))
|
||||
{
|
||||
if (!(ex is FileNotFoundException) && !(ex is FileLoadException) && !(ex is DirectoryNotFoundException) && !(ex is BadImageFormatException) && !(ex is TypeLoadException))
|
||||
{
|
||||
if (!(ex is IOException))
|
||||
{
|
||||
if (ex is OutOfMemoryException)
|
||||
{
|
||||
return "This PC ran out of memory. Closing something else and trying again usually works.";
|
||||
}
|
||||
return "Something PlexusX needs did not work.";
|
||||
}
|
||||
return "A file PlexusX needs could not be read.";
|
||||
}
|
||||
return FlavourWording.MissingFileBecause(FlavourPolicy.Current);
|
||||
}
|
||||
return "Windows did not give PlexusX permission to do this.";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace VibranceHud.Startup;
|
||||
|
||||
public static class StartupStep
|
||||
{
|
||||
public const string TheMonitors = "setting up the monitors attached to this PC";
|
||||
|
||||
public const string YourSettings = "reading your settings";
|
||||
|
||||
public const string YourLicence = "checking your licence";
|
||||
|
||||
public const string TheStartupList = "remembering whether PlexusX starts with Windows";
|
||||
|
||||
public const string TheBuildCheck = "checking this copy of PlexusX";
|
||||
|
||||
public const string TheFirstRunWizard = "showing the first-run setup";
|
||||
|
||||
public const string TheScreenAfterACrash = "putting your screen back after PlexusX was closed unexpectedly";
|
||||
|
||||
public const string TheRequestChannel = "listening for links, a second launch and the installer";
|
||||
|
||||
public const string TheSecondLaunch = "handing over to the PlexusX that is already running";
|
||||
|
||||
public const string TheTaskbarPin = "pointing your taskbar pin at this PlexusX";
|
||||
|
||||
internal const string SkippedLogName = "startup-skipped.log";
|
||||
|
||||
internal static Action<string, Exception>? Recorder;
|
||||
|
||||
public static bool Skippable(string step, Action work)
|
||||
{
|
||||
if (work == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
work();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Record(step, ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Required(string step, Action work)
|
||||
{
|
||||
try
|
||||
{
|
||||
work();
|
||||
}
|
||||
catch (StartupStepFailed)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception inner)
|
||||
{
|
||||
throw new StartupStepFailed(step, inner);
|
||||
}
|
||||
}
|
||||
|
||||
public static T Required<T>(string step, Func<T> work)
|
||||
{
|
||||
try
|
||||
{
|
||||
return work();
|
||||
}
|
||||
catch (StartupStepFailed)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception inner)
|
||||
{
|
||||
throw new StartupStepFailed(step, inner);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Record(string step, Exception ex)
|
||||
{
|
||||
Action<string, Exception> recorder = Recorder;
|
||||
if (recorder != null)
|
||||
{
|
||||
recorder(step, ex);
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
File.AppendAllText(Path.Combine(CrashLog.CrashDirectory, "startup-skipped.log"), DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ") + " skipped: " + step + " - " + CrashLog.Redact(ex.GetType().Name + ": " + ex.Message) + Environment.NewLine);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace VibranceHud.Startup;
|
||||
|
||||
public sealed class StartupStepFailed : Exception
|
||||
{
|
||||
public string Step { get; }
|
||||
|
||||
public StartupStepFailed(string step, Exception inner)
|
||||
: base("PlexusX failed while " + step + ".", inner)
|
||||
{
|
||||
Step = step ?? "";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user