Files
Samuele E. Locatelli 5f1ca0699a Fixes
2020-10-16 11:05:23 +02:00

259 lines
9.4 KiB
C#

using Active_Client.View;
using Chromium;
using Chromium.Event;
using Chromium.WebBrowser;
using Chromium.WebBrowser.Event;
using Client.Config;
using Client.Utils;
using Microsoft.Win32;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Permissions;
using System.Threading;
using System.Windows.Forms;
namespace Active_Client
{
public static class Program
{
//Set the Mutex of GUID
static Mutex CmsStepClientMutex = new Mutex(true, "{66fa29db-925a-402b-a4c7-d3d780fb1bc3}");
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region MAIN_METHOD
[STAThread]
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
static void Main(string[] args)
{
//Crate General Exception Handler
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(GeneralExMethod);
//Read App Configuration
readConfiguration();
//Initialize Chromium
InitializeCefSettings();
//Check the first argument -> Url
if (args.Count() > 0)
{
// Read ARGS Config
if (!String.IsNullOrWhiteSpace(args[0]))
{
Config.ConnectionConfig.BypassReadConfiguration = true;
Config.ConnectionConfig.StartingUrl = args[0];
}
else
Config.ConnectionConfig.BypassReadConfiguration = false;
}
//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!");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Check Graphic Card in Energy Saving mode
checkGraphicCard();
//Run the Loading Form
Application.Run(new OpeningForm());
//Run the Main-Browser Form
Application.Run(new MainForm());
//Force show Taskbar
NcWindow.ShowTaskBar();
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region CHROMIUMFX_METHODS
//Start Chromium Settings
static private void InitializeCefSettings()
{
//Check if it is present all resources
CheckResourcesFolder();
//Setup the CEF Folder
if (CfxRuntime.PlatformArch == CfxPlatformArch.x64)
CfxRuntime.LibCefDirPath = Constants.CEF_X64_PATH;
else
CfxRuntime.LibCefDirPath = Constants.CEF_X86_PATH;
//Add the event variables
ChromiumWebBrowser.OnBeforeCfxInitialize += Chromium_OnBeforeCfxInitialize;
ChromiumWebBrowser.OnBeforeCommandLineProcessing += Chromium_OnBeforeCommandLineProcessing;
//Initialize Cef
try
{
ChromiumWebBrowser.Initialize();
}
catch (Exception E)
{
ShowAlarmAndClose(E.Message);
}
}
//Method called Before Cef Command-Line Send
static void Chromium_OnBeforeCommandLineProcessing(CfxOnBeforeCommandLineProcessingEventArgs e)
{
if (Config.ClientConfig.RenderingMethod == Constants.Rendering.CPU)
e.CommandLine.AppendSwitch("--disable-gpu");
e.CommandLine.AppendSwitch("--enable-transparent-visuals");
e.CommandLine.AppendSwitch("--disable-pinch");
e.CommandLine.AppendSwitch("--enable-media-stream");
e.CommandLine.AppendSwitch("--enable-usermedia-screen-capture");
e.CommandLine.AppendSwitch("--no-proxy-server");
e.CommandLine.AppendSwitch("--ignore-certificate-errors-spki-list");
e.CommandLine.AppendSwitch("--ignore-certificate-errors");
e.CommandLine.AppendSwitch("--ignore-ssl-errors");
}
//Method called Before Cef Initialization
static void Chromium_OnBeforeCfxInitialize(OnBeforeCfxInitializeEventArgs e)
{
try
{
if (Config.ConnectionConfig.DeleteCahceFolderOnStartup && Directory.Exists(Constants.BROWSER_CACHE_FOLDER))
{
Directory.Delete(Constants.BROWSER_CACHE_FOLDER, true);
}
}
catch (Exception E)
{
ShowAlarmAndClose(E.Message);
}
e.Settings.WindowlessRenderingEnabled = true;
//Path Setup
e.Settings.CachePath = Constants.BROWSER_CACHE_FOLDER;
e.Settings.LocalesDirPath = Constants.CEF_LOCALES_PATH;
e.Settings.ResourcesDirPath = CfxRuntime.LibCefDirPath;
e.Settings.Locale = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
}
//Method called Before Cef Initialization
static void CheckResourcesFolder()
{
if (!Directory.Exists(Constants.CEF_PATH))
ShowAlarmAndClose("CEF Direcory not found");
if (!Directory.Exists(Constants.CEF_LOCALES_PATH))
ShowAlarmAndClose("CEF Locales Direcory not found");
}
#endregion
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region ALARM_METHODS
//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);
}
private static void GeneralExMethod(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;
//Create Directory
if (!Directory.Exists(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);
//Show Taskbar if the App is terminating
if (args.IsTerminating)
NcWindow.ShowTaskBar();
}
#endregion
}
}