[chore:] logging for AddressPatch.cs
This commit is contained in:
+67
-45
@@ -3,6 +3,7 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Logof_Client;
|
||||||
|
|
||||||
public class AddressPatch
|
public class AddressPatch
|
||||||
{
|
{
|
||||||
@@ -58,61 +59,82 @@ public class AddressPatch
|
|||||||
|
|
||||||
public static AddressPatch Import(Uri filename)
|
public static AddressPatch Import(Uri filename)
|
||||||
{
|
{
|
||||||
var patch = new AddressPatch();
|
try
|
||||||
|
|
||||||
// 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 patch = new AddressPatch();
|
||||||
var baseName = prop.Name.Substring(0, prop.Name.Length - 3); // "name"
|
|
||||||
|
// Alle Zeilen aus der Datei laden
|
||||||
// In der Datei wird nach "name:" gesucht (ohne _is)
|
var lines = File.ReadAllLines(filename.LocalPath);
|
||||||
var line = lines.FirstOrDefault(l => l.StartsWith(baseName + ":"));
|
|
||||||
if (line != null)
|
// Alle Properties der Klasse (Strings und bools)
|
||||||
{
|
var properties = typeof(AddressPatch).GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||||
// Wert extrahieren (alles nach dem Doppelpunkt)
|
|
||||||
var value = line.Substring(line.IndexOf(':') + 1).Trim();
|
// Nur die Properties, die mit _is enden (also die String-Werte)
|
||||||
|
var stringProps = properties.Where(p => p.PropertyType == typeof(string) && p.Name.EndsWith("_is"));
|
||||||
// Wert im Patch-Objekt setzen
|
|
||||||
prop.SetValue(patch, value);
|
foreach (var prop in stringProps)
|
||||||
|
{
|
||||||
// Passendes has_ Feld aktivieren, z.B. "has_name"
|
// Beispiel: prop.Name = "name_is"
|
||||||
var hasProp = properties.FirstOrDefault(p => p.Name == "has_" + baseName);
|
var baseName = prop.Name.Substring(0, prop.Name.Length - 3); // "name"
|
||||||
if (hasProp != null && hasProp.PropertyType == typeof(bool)) hasProp.SetValue(patch, true);
|
|
||||||
}
|
// 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()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
try
|
||||||
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 properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||||
var hasProp =
|
var stringProps = properties.Where(p => p.PropertyType == typeof(string) && p.Name.EndsWith("_is"));
|
||||||
properties.FirstOrDefault(p => p.Name == "has_" + prop.Name.Substring(0, prop.Name.Length - 3));
|
|
||||||
if (hasProp != null && (bool)hasProp.GetValue(this))
|
var lines = new StringBuilder();
|
||||||
{
|
|
||||||
var value = (string)prop.GetValue(this);
|
foreach (var prop in stringProps)
|
||||||
lines.AppendLine($"{prop.Name} => {value}");
|
{
|
||||||
}
|
// 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";
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user