Files
2020-10-23 17:09:45 +02:00

183 lines
6.5 KiB
C#

using Client.Config;
using Client.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
using Microsoft.Win32;
using CefSharp.WinForms;
using CefSharp;
namespace Client2020
{
static class Program
{
static Mutex CmsStepClientMutex = new Mutex(true, "{66fa29db-925a-402b-a4c7-d3d780fb1bc3}");
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//Crate General Exception Handler
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(GeneralExMethod);
//Read App Configuration
readConfiguration();
//Initialize Chromium
InitializeCefSettings();
//Check if is already running an instance of this application
if (!CmsStepClientMutex.WaitOne(TimeSpan.Zero, true))
ShowAlarmAndClose("Only one istance of " + Application.ProductName + " can be executed!");
//Check Graphic Card in Energy Saving mode
checkGraphicCard();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Run the Loading Form
Application.Run(new OpeningForm());
//Run the Main-Browser Form
Application.Run(new MainForm());
}
static private void InitializeCefSettings()
{
try
{
if (Config.ConnectionConfig.DeleteCahceFolderOnStartup && Directory.Exists(Constants.BROWSER_CACHE_FOLDER))
{
Directory.Delete(Constants.BROWSER_CACHE_FOLDER, true);
}
}
catch (Exception E)
{
ShowAlarmAndClose(E.Message);
}
//Setup the CEF Folder
var settings = new CefSettings();
if (Config.ClientConfig.RenderingMethod == Constants.Rendering.CPU)
settings.CefCommandLineArgs.Add("--disable-gpu");
if (Config.ClientConfig.EnableTransparent == true)
settings.CefCommandLineArgs.Add("--enable-transparent-visuals");
settings.CefCommandLineArgs.Add("--disable-pinch");
settings.CefCommandLineArgs.Add("--enable-media-stream");
settings.CefCommandLineArgs.Add("--enable-usermedia-screen-capture");
settings.CefCommandLineArgs.Add("--no-proxy-server");
settings.CefCommandLineArgs.Add("--ignore-certificate-errors-spki-list");
settings.CefCommandLineArgs.Add("--ignore-ssl-errors");
CefSharpSettings.FocusedNodeChangedEnabled = true;
CefSharpSettings.LegacyJavascriptBindingEnabled = true;
settings.LogSeverity = LogSeverity.Fatal;
settings.CachePath = Constants.BROWSER_CACHE_FOLDER;
//Initialize Cef
try
{
Cef.Initialize(settings);
}
catch (Exception E)
{
ShowAlarmAndClose(E.Message);
}
}
#region CONFIG_METHODS
//Sub-Method used to read the configuration
static private void readConfiguration()
{
//Read the Config
try
{
ConfigController.ReadStartupConfig();
}
catch (Exception E)
{
ShowAlarmAndClose(E.Message);
}
}
static private void checkGraphicCard()
{
ManagementObjectSearcher VideoCardsQuery = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_VideoController");
ManagementObjectCollection VideoCards = VideoCardsQuery.Get();
//Check if i have more Cards
if (VideoCards.Count > 1)
{
//Prepare List of Cards
String Cardlist = "";
foreach (ManagementObject card in VideoCards)
{
Cardlist = Cardlist + " - " + card["Name"] + "\n";
}
//If is Win 10 check the Registry Key
if (Environment.OSVersion.Version.Major == 10)
{
string keyName = @"HKEY_CURRENT_USER\Software\Microsoft\DirectX\UserGpuPreferences";
string valueName = System.Reflection.Assembly.GetExecutingAssembly().Location;
Object value = Registry.GetValue(keyName, valueName, null);
if (value == null || !value.Equals("GpuPreference=1;"))
{
//code if key Not Exist add it and restart
Registry.SetValue(keyName, valueName, "GpuPreference=1;");
MessageBox.Show("Active has foundthis Graphic Cards:\n\n" + Cardlist + "\nThe graphic configuration has been setted. Press Ok to restart the Application", Application.ProductName);
Application.Restart();
Environment.Exit(0);
}
}
}
}
#endregion
private static void GeneralExMethod(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;
//Create Directory
if (!Directory.Exists(Client.Utils.Constants.CEF_EXCEPTIONLOG_PATH))
Directory.CreateDirectory(Constants.CEF_EXCEPTIONLOG_PATH);
//Log the exception on File
string path = Constants.CEF_EXCEPTIONLOG_PATH + @"\" + DateTime.Now.ToString("yyyy_MM_dd") + @".txt";
using (StreamWriter sw = File.AppendText(path))
sw.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " | Class.Name: " + e.TargetSite.ReflectedType.Name + " | Method.Name: " + e.TargetSite.Name + " | Error: " + e.Message);
}
//Method Used to Show an alarm and close the application
public static void ShowAlarmAndClose(string Message)
{
MessageBox.Show(Message,
Application.ProductName,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1
);
Environment.Exit(-1);
}
}
}