Files
cms_thermo_active/Client/Program.cs
T

183 lines
5.8 KiB
C#

using Chromium;
using Chromium.Event;
using Chromium.WebBrowser;
using Chromium.WebBrowser.Event;
using Client.Config;
using Client.Utils;
using CMS_Client.View;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CMS_Client
{
public static class Program
{
//Set the Mutef of GUID
static Mutex CmsStepMutex = new Mutex(true, "{66fa29db-925a-402b-a4c7-d3d780fb1bc3}");
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#region MAIN_METHOD
[STAThread]
static void Main(string[] args)
{
//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 (!CmsStepMutex.WaitOne(TimeSpan.Zero, true))
ShowAlarmAndClose("Only one istance of " + Application.ProductName + " can be executed!");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Run the Loading Form
Application.Run(new OpeningForm());
//Run the Main-Browser Form
Application.Run(new MainForm());
CfxRuntime.Shutdown();
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);
}
}
#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("--high-dpi-support=1");
}
//Method called Before Cef Initialization
static void Chromium_OnBeforeCfxInitialize(OnBeforeCfxInitializeEventArgs e)
{
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;
}
//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
private static void ShowAlarmAndClose(string Message)
{
MessageBox.Show(Message,
Application.ProductName,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1
);
Environment.Exit(-1);
}
#endregion
}
}