mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-13 08:34:08 +02:00
Make hardware acceleration setting use a new system config file
Closes #123
This commit is contained in:
parent
9f415b11b5
commit
6a379bc2cd
66
Configuration/SystemConfig.cs
Normal file
66
Configuration/SystemConfig.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace TweetDuck.Configuration{
|
||||
sealed class SystemConfig{
|
||||
public static readonly bool IsHardwareAccelerationSupported = File.Exists(Path.Combine(Program.ProgramPath, "libEGL.dll")) &&
|
||||
File.Exists(Path.Combine(Program.ProgramPath, "libGLESv2.dll"));
|
||||
|
||||
public bool HardwareAcceleration{
|
||||
get => hardwareAcceleration && IsHardwareAccelerationSupported;
|
||||
set => hardwareAcceleration = value;
|
||||
}
|
||||
|
||||
private readonly string file;
|
||||
|
||||
private bool hardwareAcceleration;
|
||||
|
||||
private SystemConfig(string file){
|
||||
this.file = file;
|
||||
|
||||
HardwareAcceleration = true;
|
||||
}
|
||||
|
||||
private void WriteToStream(Stream stream){
|
||||
stream.WriteByte((byte)(HardwareAcceleration ? 1 : 0));
|
||||
}
|
||||
|
||||
private void ReadFromStream(Stream stream){
|
||||
HardwareAcceleration = stream.ReadByte() > 0;
|
||||
}
|
||||
|
||||
public bool Save(){
|
||||
try{
|
||||
string directory = Path.GetDirectoryName(file);
|
||||
if (directory == null)return false;
|
||||
|
||||
Directory.CreateDirectory(directory);
|
||||
|
||||
using(Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None)){
|
||||
WriteToStream(stream);
|
||||
}
|
||||
|
||||
return true;
|
||||
}catch(Exception e){
|
||||
Program.Reporter.HandleException("Configuration Error", "Could not save the system configuration file.", true, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static SystemConfig Load(string file){
|
||||
SystemConfig config = new SystemConfig(file);
|
||||
|
||||
try{
|
||||
using(Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)){
|
||||
config.ReadFromStream(stream);
|
||||
}
|
||||
}catch(FileNotFoundException){
|
||||
}catch(DirectoryNotFoundException){
|
||||
}catch(Exception e){
|
||||
Program.Reporter.HandleException("Configuration Error", "Could not open the system configuration file. If you continue, you will lose system specific configuration such as Hardware Acceleration.", true, e);
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ public bool Export(ExportFileFlags flags){
|
||||
try{
|
||||
using(CombinedFileStream stream = new CombinedFileStream(new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))){
|
||||
if (flags.HasFlag(ExportFileFlags.Config)){
|
||||
stream.WriteFile("config", Program.ConfigFilePath);
|
||||
stream.WriteFile("config", Program.UserConfigFilePath);
|
||||
}
|
||||
|
||||
if (flags.HasFlag(ExportFileFlags.PluginData)){
|
||||
@ -101,7 +101,7 @@ public bool Import(ExportFileFlags flags){
|
||||
switch(entry.KeyName){
|
||||
case "config":
|
||||
if (flags.HasFlag(ExportFileFlags.Config)){
|
||||
entry.WriteToFile(Program.ConfigFilePath);
|
||||
entry.WriteToFile(Program.UserConfigFilePath);
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -19,7 +19,13 @@ public TabSettingsAdvanced(Action<string> reinjectBrowserCSS, PluginManager plug
|
||||
this.reinjectBrowserCSS = reinjectBrowserCSS;
|
||||
this.plugins = plugins;
|
||||
|
||||
checkHardwareAcceleration.Checked = HardwareAcceleration.IsEnabled;
|
||||
if (SystemConfig.IsHardwareAccelerationSupported){
|
||||
checkHardwareAcceleration.Checked = Program.SystemConfig.HardwareAcceleration;
|
||||
}
|
||||
else{
|
||||
checkHardwareAcceleration.Enabled = false;
|
||||
checkHardwareAcceleration.Checked = false;
|
||||
}
|
||||
|
||||
BrowserCache.CalculateCacheSize(bytes => this.InvokeSafe(() => {
|
||||
if (bytes == -1L){
|
||||
@ -56,28 +62,9 @@ private void btnClearCache_Click(object sender, EventArgs e){
|
||||
}
|
||||
|
||||
private void checkHardwareAcceleration_CheckedChanged(object sender, EventArgs e){
|
||||
bool succeeded = false;
|
||||
|
||||
if (checkHardwareAcceleration.Checked){
|
||||
if (HardwareAcceleration.CanEnable){
|
||||
succeeded = HardwareAcceleration.Enable();
|
||||
}
|
||||
else{
|
||||
MessageBox.Show("Cannot enable hardware acceleration, the libraries libEGL.dll and libGLESv2.dll could not be restored.", Program.BrandName+" Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
else{
|
||||
succeeded = HardwareAcceleration.Disable();
|
||||
}
|
||||
|
||||
if (succeeded){
|
||||
PromptRestart();
|
||||
}
|
||||
else{
|
||||
checkHardwareAcceleration.CheckedChanged -= checkHardwareAcceleration_CheckedChanged;
|
||||
checkHardwareAcceleration.Checked = HardwareAcceleration.IsEnabled;
|
||||
checkHardwareAcceleration.CheckedChanged += checkHardwareAcceleration_CheckedChanged;
|
||||
}
|
||||
Program.SystemConfig.HardwareAcceleration = checkHardwareAcceleration.Checked;
|
||||
Program.SystemConfig.Save();
|
||||
PromptRestart();
|
||||
}
|
||||
|
||||
private void btnEditCefArgs_Click(object sender, EventArgs e){
|
||||
|
@ -1,51 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace TweetDuck.Core.Utils{
|
||||
static class HardwareAcceleration{
|
||||
private static readonly string LibEGL = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libEGL.dll");
|
||||
private static readonly string LibGLES = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libGLESv2.dll");
|
||||
|
||||
private static readonly string DisabledLibEGL = LibEGL+".bak";
|
||||
private static readonly string DisabledLibGLES = LibGLES+".bak";
|
||||
|
||||
public static bool IsEnabled => File.Exists(LibEGL) && File.Exists(LibGLES);
|
||||
public static bool CanEnable => File.Exists(DisabledLibEGL) && File.Exists(DisabledLibGLES);
|
||||
|
||||
public static bool Enable(){
|
||||
if (IsEnabled)return false;
|
||||
|
||||
try{
|
||||
File.Move(DisabledLibEGL, LibEGL);
|
||||
File.Move(DisabledLibGLES, LibGLES);
|
||||
return true;
|
||||
}catch{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Disable(){
|
||||
if (!IsEnabled)return false;
|
||||
|
||||
try{
|
||||
if (File.Exists(DisabledLibEGL)){
|
||||
File.Delete(DisabledLibEGL);
|
||||
}
|
||||
|
||||
if (File.Exists(DisabledLibGLES)){
|
||||
File.Delete(DisabledLibGLES);
|
||||
}
|
||||
}catch{
|
||||
// woops
|
||||
}
|
||||
|
||||
try{
|
||||
File.Move(LibEGL, DisabledLibEGL);
|
||||
File.Move(LibGLES, DisabledLibGLES);
|
||||
return true;
|
||||
}catch{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
13
Program.cs
13
Program.cs
@ -29,7 +29,8 @@ static class Program{
|
||||
public static readonly string ProgramPath = AppDomain.CurrentDomain.BaseDirectory;
|
||||
public static readonly string StoragePath = IsPortable ? Path.Combine(ProgramPath, "portable", "storage") : GetDataStoragePath();
|
||||
|
||||
public static readonly string ConfigFilePath = Path.Combine(StoragePath, "TD_UserConfig.cfg");
|
||||
public static readonly string UserConfigFilePath = Path.Combine(StoragePath, "TD_UserConfig.cfg");
|
||||
public static readonly string SystemConfigFilePath = Path.Combine(StoragePath, "TD_SystemConfig.cfg");
|
||||
public static readonly string PluginDataPath = Path.Combine(StoragePath, "TD_Plugins");
|
||||
public static readonly string PluginConfigFilePath = Path.Combine(StoragePath, "TD_PluginConfig.cfg");
|
||||
private static readonly string ErrorLogFilePath = Path.Combine(StoragePath, "TD_Log.txt");
|
||||
@ -45,6 +46,7 @@ static class Program{
|
||||
private static bool HasCleanedUp;
|
||||
|
||||
public static UserConfig UserConfig { get; private set; }
|
||||
public static SystemConfig SystemConfig { get; private set; }
|
||||
public static Reporter Reporter { get; private set; }
|
||||
|
||||
public static event EventHandler UserConfigReplaced;
|
||||
@ -123,6 +125,7 @@ private static void Main(){
|
||||
}
|
||||
|
||||
ReloadConfig();
|
||||
SystemConfig = SystemConfig.Load(SystemConfigFilePath);
|
||||
|
||||
if (Arguments.HasFlag(Arguments.ArgImportCookies)){
|
||||
ExportManager.ImportCookies();
|
||||
@ -148,7 +151,7 @@ private static void Main(){
|
||||
|
||||
CommandLineArgsParser.ReadCefArguments(UserConfig.CustomCefArgs).ToDictionary(settings.CefCommandLineArgs);
|
||||
|
||||
if (!HardwareAcceleration.IsEnabled){
|
||||
if (!SystemConfig.HardwareAcceleration){
|
||||
settings.CefCommandLineArgs["disable-gpu"] = "1";
|
||||
settings.CefCommandLineArgs["disable-gpu-vsync"] = "1";
|
||||
}
|
||||
@ -219,14 +222,14 @@ private static string GetDataStoragePath(){
|
||||
}
|
||||
|
||||
public static void ReloadConfig(){
|
||||
UserConfig = UserConfig.Load(ConfigFilePath);
|
||||
UserConfig = UserConfig.Load(UserConfigFilePath);
|
||||
UserConfigReplaced?.Invoke(UserConfig, new EventArgs());
|
||||
}
|
||||
|
||||
public static void ResetConfig(){
|
||||
try{
|
||||
File.Delete(ConfigFilePath);
|
||||
File.Delete(UserConfig.GetBackupFile(ConfigFilePath));
|
||||
File.Delete(UserConfigFilePath);
|
||||
File.Delete(UserConfig.GetBackupFile(UserConfigFilePath));
|
||||
}catch(Exception e){
|
||||
Reporter.HandleException("Configuration Reset Error", "Could not delete configuration files to reset the settings.", true, e);
|
||||
return;
|
||||
|
@ -70,6 +70,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Configuration\Arguments.cs" />
|
||||
<Compile Include="Configuration\LockManager.cs" />
|
||||
<Compile Include="Configuration\SystemConfig.cs" />
|
||||
<Compile Include="Configuration\UserConfig.cs" />
|
||||
<Compile Include="Core\Bridge\PropertyBridge.cs" />
|
||||
<Compile Include="Core\Controls\ControlExtensions.cs" />
|
||||
@ -253,7 +254,6 @@
|
||||
</Compile>
|
||||
<Compile Include="Core\Utils\BrowserCache.cs" />
|
||||
<Compile Include="Core\Utils\BrowserUtils.cs" />
|
||||
<Compile Include="Core\Utils\HardwareAcceleration.cs" />
|
||||
<Compile Include="Core\Utils\NativeMethods.cs" />
|
||||
<Compile Include="Updates\Events\UpdateAcceptedEventArgs.cs" />
|
||||
<Compile Include="Updates\Events\UpdateCheckEventArgs.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user