From 4b98f53881144b63cfe4318b6ad94c728af47800 Mon Sep 17 00:00:00 2001 From: Elias Fierke Date: Sat, 16 May 2026 15:09:38 +0200 Subject: [PATCH] [chore:] logging for AddressPatch.cs --- Tasks/AddressPatch.cs | 112 +++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 45 deletions(-) diff --git a/Tasks/AddressPatch.cs b/Tasks/AddressPatch.cs index 6b9f7e9..08f2484 100644 --- a/Tasks/AddressPatch.cs +++ b/Tasks/AddressPatch.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text; +using Logof_Client; public class AddressPatch { @@ -58,61 +59,82 @@ public class AddressPatch public static AddressPatch Import(Uri filename) { - var patch = new AddressPatch(); - - // Alle Zeilen aus der Datei laden - var lines = File.ReadAllLines(filename.LocalPath); - - // Alle Properties der Klasse (Strings und bools) - var properties = typeof(AddressPatch).GetProperties(BindingFlags.Public | BindingFlags.Instance); - - // Nur die Properties, die mit _is enden (also die String-Werte) - var stringProps = properties.Where(p => p.PropertyType == typeof(string) && p.Name.EndsWith("_is")); - - foreach (var prop in stringProps) + try { - // Beispiel: prop.Name = "name_is" - var baseName = prop.Name.Substring(0, prop.Name.Length - 3); // "name" - - // In der Datei wird nach "name:" gesucht (ohne _is) - var line = lines.FirstOrDefault(l => l.StartsWith(baseName + ":")); - if (line != null) - { - // Wert extrahieren (alles nach dem Doppelpunkt) - var value = line.Substring(line.IndexOf(':') + 1).Trim(); - - // Wert im Patch-Objekt setzen - prop.SetValue(patch, value); - - // Passendes has_ Feld aktivieren, z.B. "has_name" - var hasProp = properties.FirstOrDefault(p => p.Name == "has_" + baseName); - if (hasProp != null && hasProp.PropertyType == typeof(bool)) hasProp.SetValue(patch, true); - } + var patch = new AddressPatch(); + + // Alle Zeilen aus der Datei laden + var lines = File.ReadAllLines(filename.LocalPath); + + // Alle Properties der Klasse (Strings und bools) + var properties = typeof(AddressPatch).GetProperties(BindingFlags.Public | BindingFlags.Instance); + + // Nur die Properties, die mit _is enden (also die String-Werte) + var stringProps = properties.Where(p => p.PropertyType == typeof(string) && p.Name.EndsWith("_is")); + + foreach (var prop in stringProps) + { + // Beispiel: prop.Name = "name_is" + var baseName = prop.Name.Substring(0, prop.Name.Length - 3); // "name" + + // In der Datei wird nach "name:" gesucht (ohne _is) + var line = lines.FirstOrDefault(l => l.StartsWith(baseName + ":")); + if (line != null) + { + // Wert extrahieren (alles nach dem Doppelpunkt) + var value = line.Substring(line.IndexOf(':') + 1).Trim(); + + // Wert im Patch-Objekt setzen + prop.SetValue(patch, value); + + // Passendes has_ Feld aktivieren, z.B. "has_name" + var hasProp = properties.FirstOrDefault(p => p.Name == "has_" + baseName); + if (hasProp != null && hasProp.PropertyType == typeof(bool)) hasProp.SetValue(patch, true); + } + } + + return patch; + } + catch (Exception ex) + { + Logger.Log($"Error while importing address patch: {ex.Message}", Logger.LogType.Error); } - return patch; + return null; + + } public override string ToString() { - var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); - var stringProps = properties.Where(p => p.PropertyType == typeof(string) && p.Name.EndsWith("_is")); - - var lines = new StringBuilder(); - - foreach (var prop in stringProps) + try { - // passendes has_ Feld - var hasProp = - properties.FirstOrDefault(p => p.Name == "has_" + prop.Name.Substring(0, prop.Name.Length - 3)); - if (hasProp != null && (bool)hasProp.GetValue(this)) - { - var value = (string)prop.GetValue(this); - lines.AppendLine($"{prop.Name} => {value}"); - } + var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); + var stringProps = properties.Where(p => p.PropertyType == typeof(string) && p.Name.EndsWith("_is")); + + var lines = new StringBuilder(); + + foreach (var prop in stringProps) + { + // passendes has_ Feld + var hasProp = + properties.FirstOrDefault(p => p.Name == "has_" + prop.Name.Substring(0, prop.Name.Length - 3)); + if (hasProp != null && (bool)hasProp.GetValue(this)) + { + var value = (string)prop.GetValue(this); + lines.AppendLine($"{prop.Name} => {value}"); + } + } + + return lines.ToString().TrimEnd(); + } + catch (Exception ex) + { + Logger.Log($"Error while parsing: {ex.Message}", Logger.LogType.Error); } - return lines.ToString().TrimEnd(); + return "Error while parsing"; + } } \ No newline at end of file