[chore:] logging for AddressPatch.cs
This commit is contained in:
+64
-42
@@ -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"
|
||||
var patch = new AddressPatch();
|
||||
|
||||
// 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();
|
||||
// Alle Zeilen aus der Datei laden
|
||||
var lines = File.ReadAllLines(filename.LocalPath);
|
||||
|
||||
// Wert im Patch-Objekt setzen
|
||||
prop.SetValue(patch, value);
|
||||
// Alle Properties der Klasse (Strings und bools)
|
||||
var properties = typeof(AddressPatch).GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
|
||||
// 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);
|
||||
}
|
||||
// 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";
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user