This commit is contained in:
Stephan Maier
2024-08-27 08:10:27 +02:00
parent eb5c2fa502
commit 647f938eee
617 changed files with 73086 additions and 7137 deletions

View File

@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FSI.Lib.Helpers;
namespace FSI.BT.Tools.Global.Utilities
{
internal class Admin
{
public static bool CheckSuperAdminRight()
{
if (Vars.GlobalSettings.SuperAdmin == null)
{
return false;
}
System.Security.Principal.WindowsIdentity windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();
if (string.Equals(Lib.DeEncryptString.DeEncrypt.DecryptString(Lib.DeEncryptString.DeEncrypt.DecryptString(Vars.GlobalSettings.SuperAdmin, AppDomain.CurrentDomain.FriendlyName), AppDomain.CurrentDomain.FriendlyName), windowsIdentity.ShortName(), StringComparison.OrdinalIgnoreCase))
return true;
return false;
}
public static bool CheckAdminRight()
{
if (Vars.GlobalSettings.Admins == null)
{
return false;
}
List<Settings.StringValue.IStringValue> users = Vars.GlobalSettings.Admins.ToList();
System.Security.Principal.WindowsIdentity windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();
foreach (var user in users)
{
if (string.Equals(Lib.DeEncryptString.DeEncrypt.DecryptString(user.Value, AppDomain.CurrentDomain.FriendlyName), windowsIdentity.ShortName(), StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
public static bool CheckUserRight()
{
if (Vars.GlobalSettings.Users == null)
{
return false;
}
List<Settings.StringValue.IStringValue> users = Vars.GlobalSettings.Users.ToList();
System.Security.Principal.WindowsIdentity windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();
foreach (var user in users)
{
if (string.Equals(Lib.DeEncryptString.DeEncrypt.DecryptString(user.Value, AppDomain.CurrentDomain.FriendlyName), windowsIdentity.ShortName(), StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
}
}

View File

@@ -0,0 +1,70 @@
// <copyright file="AppRestart.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace FSI.BT.Tools.Global.Utilities
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
internal class AppRestart
{
public static event Action BeforeRestarting;
internal static void ByThreadException()
{
Restart(GetCurrentMethod());
}
internal static void ByAppContextMenu()
{
Restart(GetCurrentMethod());
}
internal static void ByConfigChange()
{
Restart(GetCurrentMethod());
}
internal static void ByMenuButton()
{
Restart(GetCurrentMethod());
}
private static void Restart(string reason)
{
BeforeRestarting?.Invoke();
Log.Info($"Restart by '{reason}'");
Log.Close();
using (Process p = new())
{
string fileName = System.Environment.ProcessPath;
p.StartInfo = new ProcessStartInfo(fileName);
try
{
p.Start();
}
catch (Win32Exception ex)
{
Log.Warn("Restart failed", ex);
}
}
Application.Exit();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static string GetCurrentMethod()
{
StackTrace st = new();
StackFrame sf = st.GetFrame(1);
return sf.GetMethod().Name;
}
}
}

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace FSI.BT.Tools.Global.Utilities
{
internal static class ExtractEmbeddedZip
{
internal static void Extract(string zipName, string destPath)
{
System.IO.Directory.CreateDirectory(destPath); // Erstellt alle fehlenden Verzeichnisse
using Stream _pluginZipResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(zipName);
using ZipArchive zip = new(_pluginZipResourceStream);
zip.ExtractToDirectory(destPath, true);
Vars.Log.Info("Externes Tool \"{0}\" wurde in das Verzeichnis \"{1}\" entpackt", zipName, destPath);
}
}
}

View File

@@ -0,0 +1,199 @@
// <copyright file="Log.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace FSI.BT.Tools.Global.Utilities
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Clearcove.Logging;
internal static class Log
{
private const string LogfileLastExtension = "_last";
private static readonly Logger LogValue = new(string.Empty);
private static readonly List<string> Warnings = new();
private static readonly List<string> Infos = new();
internal static void Initialize()
{
bool warnFailedToSaveLogFile = false;
Exception exceptionWarnFailedToSaveLogFile = new();
if (Global.Vars.SystemTrayMenuSettings.SaveLogFileInApplicationDirectory)
{
try
{
string fileNameToCheckWriteAccess = "CheckWriteAccess";
File.WriteAllText(fileNameToCheckWriteAccess, fileNameToCheckWriteAccess);
File.Delete(fileNameToCheckWriteAccess);
}
catch (Exception ex)
{
Global.Vars.SystemTrayMenuSettings.SaveLogFileInApplicationDirectory = false;
warnFailedToSaveLogFile = true;
exceptionWarnFailedToSaveLogFile = ex;
}
}
bool warnCanNotClearLogfile = false;
Exception exceptionWarnCanNotClearLogfile = new();
string fileNamePath = GetLogFilePath();
FileInfo fileInfo = new(fileNamePath);
string fileNamePathLast = string.Empty;
if (fileInfo.Exists && fileInfo.Length > 2000000)
{
fileNamePathLast = GetLogFilePath(LogfileLastExtension);
try
{
File.Delete(fileNamePathLast);
File.Move(fileNamePath, fileNamePathLast);
}
catch (Exception ex)
{
warnCanNotClearLogfile = true;
exceptionWarnCanNotClearLogfile = ex;
}
}
Logger.Start(fileInfo);
if (warnFailedToSaveLogFile)
{
Warn($"Failed to save log file in application folder {GetLogFilePath()}", exceptionWarnFailedToSaveLogFile);
}
if (warnCanNotClearLogfile)
{
Warn($"Can not clear logfile:'{fileNamePathLast}'", exceptionWarnCanNotClearLogfile);
}
}
internal static void Info(string message)
{
if (!Infos.Contains(message))
{
Infos.Add(message);
LogValue.Info(message);
}
}
internal static void Warn(string message, Exception ex)
{
string warning = $"{message} {ex.ToString().Replace(Environment.NewLine, " ", StringComparison.InvariantCulture)}";
if (!Warnings.Contains(warning))
{
Warnings.Add(warning);
LogValue.Warn(warning);
}
}
internal static void Error(string message, Exception ex)
{
LogValue.Error($"{message}{Environment.NewLine}" +
$"{ex}");
}
internal static string GetLogFilePath(string backup = "")
{
string logFilePath = string.Empty;
if (!Global.Vars.SystemTrayMenuSettings.SaveLogFileInApplicationDirectory)
{
logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), $"FSI.BT.Tools.SystemTrayMenu");
}
return Path.Combine(logFilePath, $"log-{Environment.MachineName}{backup}.txt");
}
internal static void OpenLogFile()
{
string lastLogfile = GetLogFilePath(LogfileLastExtension);
if (File.Exists(lastLogfile))
{
ProcessStart(lastLogfile);
}
ProcessStart(GetLogFilePath());
}
internal static void WriteApplicationRuns()
{
Assembly assembly = Assembly.GetExecutingAssembly();
LogValue.Info($"Application Start " +
assembly.ManifestModule.Name + " | " +
assembly.GetName().Version.ToString() + " | " +
$"ScalingFactor={Scaling.Factor}");
}
internal static void Close()
{
try
{
Logger.ShutDown();
}
catch (Exception ex)
{
Global.Vars.SystemTrayMenuSettings.SaveLogFileInApplicationDirectory = false;
Warn($"Failed to save log file in application folder {GetLogFilePath()}", ex);
}
}
internal static void ProcessStart(
string fileName,
string arguments = "",
bool doubleQuoteArg = false,
string workingDirectory = "",
bool createNoWindow = false,
string resolvedPath = "")
{
if (doubleQuoteArg && !string.IsNullOrEmpty(arguments))
{
arguments = "\"" + arguments + "\"";
}
try
{
using Process p = new()
{
StartInfo = new ProcessStartInfo(fileName)
{
FileName = fileName,
Arguments = arguments,
WorkingDirectory = workingDirectory,
CreateNoWindow = createNoWindow,
UseShellExecute = true,
},
};
p.Start();
}
catch (Win32Exception ex)
{
Warn($"fileName:'{fileName}' arguments:'{arguments}'", ex);
if ((ex.NativeErrorCode == 2 || ex.NativeErrorCode == 1223) &&
(string.IsNullOrEmpty(resolvedPath) || !File.Exists(resolvedPath)))
{
new Thread(ShowProblemWithShortcut).Start();
static void ShowProblemWithShortcut()
{
_ = MessageBox.Show(
Global.Utilities.Translator.GetText("The item that this shortcut refers to has been changed or moved, so this shortcut will no longer work properly."),
Global.Utilities.Translator.GetText("Problem with shortcut link"),
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
}
}
catch (Exception ex)
{
Warn($"fileName:'{fileName}' arguments:'{arguments}'", ex);
}
}
}
}

View File

@@ -0,0 +1,31 @@
// <copyright file="Scaling.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace FSI.BT.Tools.Global.Utilities
{
using System;
using System.Drawing;
internal static class Scaling
{
public static float Factor { get; private set; } = 1;
public static float FactorByDpi { get; private set; } = 1;
public static void Initialize()
{
Factor = Global.Vars.SystemTrayMenuSettings.SizeInPercent / 100f;
}
public static int Scale(int width)
{
return (int)Math.Round(width * Factor, 0, MidpointRounding.AwayFromZero);
}
public static void CalculateFactorByDpi(Graphics graphics)
{
FactorByDpi = graphics.DpiX / 96;
}
}
}

View File

@@ -0,0 +1,36 @@
// <copyright file="Translator.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace FSI.BT.Tools.Global.Utilities
{
using System.Globalization;
using System.Resources;
using FSI.BT.Tools.SystemTrayMenu.UserInterface;
internal static class Translator
{
private static CultureInfo culture;
internal static void Initialize()
{
if (string.IsNullOrEmpty(
Vars.SystemTrayMenuSettings.CurrentCultureInfoName))
{
Vars.SystemTrayMenuSettings.CurrentCultureInfoName = "de";
//Global.Vars.SystemTrayMenuSettings.Save();
}
culture = CultureInfo.CreateSpecificCulture(
Vars.SystemTrayMenuSettings.CurrentCultureInfoName);
}
internal static string GetText(string id)
{
ResourceManager rm = new(
"FSI.BT.Tools.Global.Resources.Languages.lang",
typeof(Menu).Assembly);
return rm.GetString(id, culture);
}
}
}