// Copyright (C) Stichting Deltares 2025. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Deltares.Dam.Data.License;
using Deltares.Dam.Data.Registry;
using Deltares.Dam.Data.Version;
using Deltares.Dam.Forms;
using Deltares.Geometry.Forms;
using Deltares.Geotechnics.Forms;
using Deltares.Standard;
using Deltares.Standard.Forms.DExpress;
using Deltares.Standard.Reflection;
namespace Deltares.Dam.Application
{
internal class Program : IProgramInfo
{
private const string dAuthFeature = "DGS_26_00";
private const string BetaString = "(BETA)";
// ReSharper disable ConvertToConstant.Local
// ReSharper disable RedundantDefaultFieldInitializer
private static readonly bool IsReleaseVersion = true;
// ReSharper restore RedundantDefaultFieldInitializer
// ReSharper restore ConvertToConstant.Local
///
/// The main entry point for the application.
///
[STAThread]
private static void Main()
{
#if DEBUG
RunInDebugMode();
#else
RunInReleaseMode();
#endif
}
private static void Start()
{
VerifyInstallation();
DamLicense.CheckoutLicense(dAuthFeature, info.AssemblyVersion);
// Parameters are used for splash screen
var mainForm = new MainForm(typeof(Program),
new Bitmap(typeof(DamPlugin).Assembly.GetManifestResourceStream("Deltares.Dam.Forms.Resources.DAMSplash.jpg")), false);
if (!IsReleaseVersion)
{
mainForm.TextChanged += MainFormTextChanged;
mainForm.Disposed += (s, o) => mainForm.TextChanged -= MainFormTextChanged;
}
// register plugins
mainForm.Register(new GeometryPlugin());
mainForm.Register(new GeotechnicsPlugin());
mainForm.Register(new DamPlugin());
// run
System.Windows.Forms.Application.Run(mainForm);
DamLicense.CheckinLicense();
}
///
/// Check if the application is properly installed by verifying the required versions
/// of Common Files and DS_Flex in the registry.
/// If not, show an error message and exit the application.
///
private static void VerifyInstallation()
{
const string requiredVersionCommonFiles = "26.1";
const string registryPathCommonFiles = "SOFTWARE\\WOW6432Node\\Deltares\\";
const string registryKeyCommonFiles = "Common Version";
const string requiredVersionDsFlex = "7.9";
const string registryPathDsFlex = "SOFTWARE\\WOW6432Node\\WL | Delft Hydraulics\\DS_Flex\\";
const string registryKeyDsFlex = "InstallerVersion";
var errorMessages = new List();
// Check version of Common Files
string versionCommonFiles = RegistryReader.GetRegistryValueFromLocalMachine(registryPathCommonFiles, registryKeyCommonFiles);
if (string.IsNullOrEmpty(versionCommonFiles))
{
errorMessages.Add("Common Files not found.");
}
else if (!VersionComparer.IsVersionGreaterOrEqual(versionCommonFiles, requiredVersionCommonFiles))
{
errorMessages.Add($"Common files version is outdated: {versionCommonFiles}. Required version is {requiredVersionCommonFiles} or higher.");
}
// Check version of DS_Flex
string versionDsFlex = RegistryReader.GetRegistryValueFromLocalMachine(registryPathDsFlex, registryKeyDsFlex);
if (string.IsNullOrEmpty(versionDsFlex))
{
errorMessages.Add("DS_Flex not found.");
}
else if (!VersionComparer.IsVersionGreaterOrEqual(versionDsFlex, requiredVersionDsFlex))
{
errorMessages.Add($"DS_Flex version is outdated: {versionDsFlex}. Required version is {requiredVersionDsFlex} or higher.");
}
if (errorMessages.Count > 0)
{
var errorMessage = new StringBuilder();
errorMessage.AppendLine($"The application cannot be started because it is not properly installed. Please install Common Files {requiredVersionCommonFiles} or higher.");
foreach (string msg in errorMessages)
{
errorMessage.AppendLine(msg);
}
MessageBox.Show(errorMessage.ToString(), "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
}
private static void MainFormTextChanged(object sender, EventArgs eventArgs)
{
var mainForm = sender as MainForm;
if (mainForm == null || mainForm.Text.Contains(BetaString) || IsReleaseVersion)
{
return;
}
mainForm.TextChanged -= MainFormTextChanged;
mainForm.Text = mainForm.Text + " " + BetaString;
mainForm.TextChanged += MainFormTextChanged;
}
private static void Initialize()
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
}
#if DEBUG
private static void RunInDebugMode()
{
Initialize();
Start();
}
#else
private static void RunInReleaseMode()
{
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
Initialize();
try
{
Start();
}
catch (Exception ex)
{
HandleException(ex);
}
}
#endif
private static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
HandleException(e.ExceptionObject as Exception);
}
private static void HandleException(Exception ex)
{
if (ex == null)
{
return;
}
// Show message box
var exceptionMessage = new StringBuilder();
if (ex.InnerException != null)
{
exceptionMessage.Append(ex.InnerException.Message);
exceptionMessage.AppendLine();
}
exceptionMessage.Append("An unhandled error occurred. The application is terminating. For more information see your trace log.");
LocalizedMessageBox.ShowException(String.Concat(exceptionMessage.ToString(), ": ", ex.Message, ex.StackTrace));
System.Windows.Forms.Application.Exit();
}
#region IProgramInfo
private static readonly AssemblyInfoHelper info = new AssemblyInfoHelper(typeof(Program));
public string LicenseType
{
get
{
return DamLicense.DamLicenseType.ToString();
}
}
bool IProgramInfo.IsReleaseVersion
{
get
{
return IsReleaseVersion;
}
}
string IProgramInfo.Company
{
get
{
return info.Company;
}
}
public string Phone
{
get
{
return info.Phone;
}
}
public string Email
{
get
{
return info.Email;
}
}
string IProgramInfo.Copyright
{
get
{
return info.Copyright;
}
}
string IProgramInfo.Product
{
get
{
return info.Product + (!IsReleaseVersion ? " " + BetaString : string.Empty);
}
}
string IProgramInfo.Title
{
get
{
return info.Title;
}
}
string IProgramInfo.AssemblyVersion
{
get
{
return info.AssemblyVersion;
}
}
#endregion
}
}