using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DisplLanguage = RunNxUpdate.Languages.Language;
using EngelLocation = RunNxUpdate.Locations.Location;
namespace RunNxUpdate
{
class Program
{
#region Version und Copyright
// Version und Copyright
static string programName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
static string programVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
static string copyright = GenerateCopyright();
string icon = Application.StartupPath + "\\Info.bmp";
//Assembly Datum und Zeit
static DateTime value = AssemblyDateTime();
string date = value.ToShortDateString();
string time = value.ToLongTimeString();
private static DateTime AssemblyDateTime()
{
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
return buildDateTime;
}
private static string CurrentYear()
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
private static string StartYear()
{
string startYear = "";
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
{
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
while (startYear.StartsWith(" "))
{
startYear = startYear.Remove(0, 1);
}
startYear = startYear.Remove(startYear.IndexOf(" "));
return startYear;
}
else
{
DateTime dtn = DateTime.Now;
return dtn.Year.ToString();
}
}
private static string GenerateCopyright()
{
string startYear = StartYear();
string currentYear = CurrentYear();
if (startYear == currentYear)
{
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
else
{
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace(startYear, String.Concat(startYear, "-", currentYear));
}
}
#endregion
#region Programm-Info
///
/// Contains the name of the program file
///
public static string ProgramName { private set; get; }
///
/// Contains the version of the program file
///
public static string ProgramVersion { private set; get; }
///
/// Contains the copyright notice
///
public static string Copyright { private set; get; }
private static void SetProgramInfo()
{
//Name, Version und Copyright setzen
ProgramName = programName;
ProgramVersion = programVersion;
Copyright = copyright;
}
#endregion
//Globale Variablen
public static string UpdateDir { get; set; }
public static string UpdateNo { get; set; }
public static Languages.Language Language { get; set; }
//--- Möglicher Aufruf aus einem Script heraus:
//----- set UPDATEFOLDER=M:\ug\NX12_install\nx-12.0.2.mp14
//----- set UPDATEDATE=20210222
//----- call M:\ug\Tools\RunNxUpdate\RunNxUpdate.exe -updatefolder=%UPDATEFOLDER% -updateNr=engel_%UPDATEDATE%
static void Main(string[] args)
{
SetProgramInfo();
//Variablen vorbelegen falls kein Wert gesetzt wurde
UpdateDir = "";
UpdateNo = "";
try
{
//Übergabeparameter auswerten
new Program().Transfer(args);
//Sprache setzen
new Program().SetLanguage();
//Programm starten
new Program().Start(UpdateDir, UpdateNo, "run_ugsupdate.exe");
}
catch (UpdateFilesNotExistException)
{
MessageBox.Show(String.Concat(AppResources.msgText001, Environment.NewLine, AppResources.msgText000),
AppResources.msgHeadline001,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
catch (UpdateException)
{
MessageBox.Show(String.Concat(AppResources.msgText002, Environment.NewLine, AppResources.msgText000),
AppResources.msgHeadline001,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
catch (EnvironmentFileException)
{
MessageBox.Show(String.Concat(AppResources.msgText003, Environment.NewLine, AppResources.msgText000),
AppResources.msgHeadline001,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
catch (VersionFileIOException)
{
MessageBox.Show(String.Concat(AppResources.msgText004, Environment.NewLine, AppResources.msgText000),
AppResources.msgHeadline001,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
catch (IOException)
{
MessageBox.Show(String.Concat(AppResources.msgText005, Environment.NewLine, AppResources.msgText000),
AppResources.msgHeadline001,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
catch (Exception)
{
MessageBox.Show(String.Concat(AppResources.msgText006, Environment.NewLine, AppResources.msgText000),
AppResources.msgHeadline001,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
}
private void Transfer(string[] args)
{
foreach (var element in args)
{
if (element.ToUpper().StartsWith("-UPDATEFOLDER"))
{
UpdateDir = element.Remove(0, element.LastIndexOf("=") + 1);
if (!Directory.Exists(UpdateDir))
{
throw new UpdateFilesNotExistException();
}
}
if (element.ToUpper().StartsWith("-UPDATENR"))
{
UpdateNo = element.Remove(0, element.LastIndexOf("=") + 1);
}
}
}
private void SetLanguage()
{
Language = DisplLanguage.English;
Languages.SetLanguage(DisplLanguage.English);
//Wenn der Standort 'EA' oder 'EAZ' ist dann ist die Sprache deutsch
EngelLocation location = Locations.GetOwnLocation();
if (location == 0)
{
Application.Exit();
}
else if (location == EngelLocation.EA || location == EngelLocation.EAZ)
{
Language = DisplLanguage.German;
Languages.SetLanguage(DisplLanguage.German);
}
}
private void Start(string folder, string updateNo, string batchFile)
{
NxVersion nxVer = new NxVersion();
nxVer.GetNxVersion(Environment.GetEnvironmentVariable("UGII_BASE_DIR"));
NxVersion currentVersion = GetCurrentVersion(folder); //Zu installierende Version
string installedVersion = nxVer.NXpatch; //Installierte Version
int installationStatus = nxVer.Compare(currentVersion);
switch (installationStatus)
{
case 0:
VersionEqual(updateNo);
break;
case 1:
InstalledVersionNewer();
break;
case 2:
InstalledVersionOlder(currentVersion.NXpatch.ToString(), folder, updateNo);
break;
default:
ErrorOccurred();
break;
}
}
private NxVersion GetCurrentVersion(string folder)
{
NxVersion nxVer = new NxVersion();
nxVer.GetNxVersion(Environment.GetEnvironmentVariable("UGII_BASE_DIR"));
string nxPatch = folder.Remove(0, folder.LastIndexOf("\\") + 1).ToUpper().Replace("-", " ");
if (nxPatch.Contains("MP0"))
{
nxPatch = nxPatch.Replace("MP0", "MP");
}
string nxPatchTemp = nxPatch.Remove(nxPatch.LastIndexOf(".") + 1);
string mp = nxPatch.Remove(0, nxPatch.LastIndexOf("MP"));
nxPatch = String.Concat(nxPatchTemp, nxVer.NXphase, " ", mp);
return nxVer.ConvertStringPatchToNxVersionPatch(nxPatch);
}
private void VersionEqual(string nxVersion)
{
//Ist die aktuelle Version gleich der installierten Version und das Programm
//'RunNxUpdate' wurde gestartet, dann fehlt die Versionsdatei in 'C:\CAD\Update'.
//Es ist kein Update notwendig, aber die Versionsdatei muss geschrieben werden.
SetVersionFile(nxVersion);
}
private void InstalledVersionNewer()
{
MessageBox.Show(String.Concat(AppResources.msgText007, Environment.NewLine, AppResources.msgText000),
AppResources.msgHeadline002,
MessageBoxButtons.OK,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1);
}
private void InstalledVersionOlder(string currentVersion, string folder, string nxVersion)
{
if (IsStartDate(nxVersion))
{
DialogResult result = MessageBox.Show(String.Concat(AppResources.msgText008, currentVersion, ".", Environment.NewLine, AppResources.msgText009, Environment.NewLine, AppResources.msgText010, Environment.NewLine, Environment.NewLine, AppResources.msgText011),
AppResources.msgHeadline002,
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1);
if (result == DialogResult.Yes)
{
//Update ausführen
RunUpdate(folder, nxVersion);
}
}
}
private void ErrorOccurred()
{
MessageBox.Show(String.Concat(AppResources.msgText002, Environment.NewLine, AppResources.msgText000),
AppResources.msgHeadline001,
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
private void RunUpdate(string folder, string nxVersion)
{
while (CheckIfAProcessIsRunning("ugraf"))
{
MessageBox.Show(AppResources.msgText012,
AppResources.msgHeadline003,
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
}
string fileName = String.Concat(folder, "\\run_ugsupdate.exe");
try
{
Update(fileName);
}
catch (Exception)
{
throw new UpdateException();
}
SetVersionFile(nxVersion);
}
private bool CheckIfAProcessIsRunning(string processname)
{
return Process.GetProcessesByName(processname).Length > 0;
}
private void Update(string fileName)
{
Process P = new Process();
P.StartInfo.FileName = fileName;
// Hier können noch Argumente übergeben werden
//P.StartInfo.Arguments = "";
P.Start();
P.WaitForExit();
}
private void SetVersionFile(string nxVersion)
{
string fileName = "";
string userName = "";
string computerName = "";
try
{
fileName = String.Concat("C:\\CAD\\Update\\", nxVersion, ".txt");
userName = Environment.GetEnvironmentVariable("USERNAME");
computerName = Environment.GetEnvironmentVariable("COMPUTERNAME");
}
catch (Exception)
{
throw new EnvironmentFileException();
}
if (userName == null)
{
userName = "noUSERNAME";
}
if (computerName == null)
{
computerName = "noCOMPUTERNAME";
}
DateTime now = DateTime.Now;
string dateTime = now.ToString();
try
{
File.WriteAllText(fileName, String.Concat(userName, " ", computerName, " ", dateTime));
}
catch (Exception)
{
throw new VersionFileIOException();
}
}
private bool IsStartDate(string updateNo)
{
//Updatedatum ermitteln
string updateDate = updateNo.Replace("engel_", "");
DateTime dtud = new DateTime(Convert.ToUInt16(updateDate.Remove(4)), Convert.ToUInt16(updateDate.Remove(0, 4).Remove(2)), Convert.ToUInt16(updateDate.Remove(0, 6)));
//Aktuelles Datum ermitteln
DateTime dtcd = DateTime.Now;
//Ist das aktuelle Datum gleich oder neuer als das Updatedatum, dann gib 'true' sonst 'false' zurück
if (dtcd >= dtud)
{
return true;
}
else
{
return false;
}
}
}
#region Exceptions
class UpdateFilesNotExistException : ApplicationException
{
public UpdateFilesNotExistException()
{ }
public UpdateFilesNotExistException(string message) : base(message)
{ }
public UpdateFilesNotExistException(string message, Exception inner) : base(message, inner)
{ }
}
class UpdateException : ApplicationException
{
public UpdateException()
{ }
public UpdateException(string message) : base(message)
{ }
public UpdateException(string message, Exception inner) : base(message, inner)
{ }
}
class EnvironmentFileException : ApplicationException
{
public EnvironmentFileException()
{ }
public EnvironmentFileException(string message) : base(message)
{ }
public EnvironmentFileException(string message, Exception inner) : base(message, inner)
{ }
}
class VersionFileIOException : ApplicationException
{
public VersionFileIOException()
{ }
public VersionFileIOException(string message) : base(message)
{ }
public VersionFileIOException(string message, Exception inner) : base(message, inner)
{ }
}
#endregion
}