fix: Cleans up core code (#1187)

**Only one functional change**
* Fixes a bug in LogFactory where `Warning` is being logged as `Information`

Non-functional changes:
* Updates/Fixes copyright headers
* Removes namespace scopes for core files.

View with [whitespace off](https://github.com/modernuo/ModernUO/pull/1187/files?w=1).
This commit is contained in:
Kamron Batman 2022-10-10 21:47:08 -07:00 committed by GitHub
parent 0138d40bda
commit f268d5d4e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
262 changed files with 28527 additions and 28646 deletions

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019-2021 - ModernUO Development Team *
* Copyright 2019-2022 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Persistence.cs *
* *
@ -18,112 +18,111 @@ using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Server
namespace Server;
public class Persistence
{
public class Persistence
public const int DefaultPriority = 100;
private static readonly SortedSet<RegistryEntry> _registry = new(new RegistryEntryComparer());
public static void Register(
string name,
Action serializer,
Action<string> snapshotWriter,
Action<string> deserializer,
int priority = DefaultPriority
)
{
public const int DefaultPriority = 100;
private static readonly SortedSet<RegistryEntry> _registry = new(new RegistryEntryComparer());
public static void Register(
string name,
Action serializer,
Action<string> snapshotWriter,
Action<string> deserializer,
int priority = DefaultPriority
)
{
_registry.Add(
new RegistryEntry
{
Name = name,
Priority = priority,
Serialize = serializer,
WriteSnapshot = snapshotWriter,
Deserialize = deserializer
}
);
}
public static void Unregister(string name) => _registry.RemoveWhere(entry => entry.Name == name);
public static void Load(string path)
{
// This should probably not be parallel since Mobiles must be loaded before Items
foreach (var entry in _registry)
_registry.Add(
new RegistryEntry
{
entry.Deserialize(path);
Name = name,
Priority = priority,
Serialize = serializer,
WriteSnapshot = snapshotWriter,
Deserialize = deserializer
}
}
);
}
public static void Serialize()
public static void Unregister(string name) => _registry.RemoveWhere(entry => entry.Name == name);
public static void Load(string path)
{
// This should probably not be parallel since Mobiles must be loaded before Items
foreach (var entry in _registry)
{
Parallel.ForEach(_registry, entry => entry.Serialize());
}
public static void WriteSnapshot(string path)
{
foreach (var entry in _registry)
{
entry.WriteSnapshot(path);
}
}
public record RegistryEntry
{
public string Name { get; init; }
public int Priority { get; init; }
public Action Serialize { get; init; } // Serializing to memory buffers
public Action<string> WriteSnapshot { get; init; }
public Action<string> Deserialize { get; init; }
}
internal class RegistryEntryComparer : IComparer<RegistryEntry>
{
public int Compare(RegistryEntry x, RegistryEntry y)
{
if (x == y)
{
return 0;
}
if (x == null)
{
return 1;
}
if (y == null)
{
return -1;
}
// First sort by priority
var cmp = x.Priority.CompareTo(y.Priority);
// Then alphabetically. We won't allow the same entry (by name) twice in the SortedSet
return cmp != 0 ? cmp : x.Name?.CompareOrdinal(y.Name) ?? -1;
}
}
public static void TraceException(Exception ex)
{
try
{
using var op = new StreamWriter("save-errors.log", true);
op.WriteLine("# {0}", Core.Now);
op.WriteLine(ex);
op.WriteLine();
op.WriteLine();
}
catch
{
// ignored
}
Console.WriteLine(ex);
entry.Deserialize(path);
}
}
public static void Serialize()
{
Parallel.ForEach(_registry, entry => entry.Serialize());
}
public static void WriteSnapshot(string path)
{
foreach (var entry in _registry)
{
entry.WriteSnapshot(path);
}
}
public record RegistryEntry
{
public string Name { get; init; }
public int Priority { get; init; }
public Action Serialize { get; init; } // Serializing to memory buffers
public Action<string> WriteSnapshot { get; init; }
public Action<string> Deserialize { get; init; }
}
internal class RegistryEntryComparer : IComparer<RegistryEntry>
{
public int Compare(RegistryEntry x, RegistryEntry y)
{
if (x == y)
{
return 0;
}
if (x == null)
{
return 1;
}
if (y == null)
{
return -1;
}
// First sort by priority
var cmp = x.Priority.CompareTo(y.Priority);
// Then alphabetically. We won't allow the same entry (by name) twice in the SortedSet
return cmp != 0 ? cmp : x.Name?.CompareOrdinal(y.Name) ?? -1;
}
}
public static void TraceException(Exception ex)
{
try
{
using var op = new StreamWriter("save-errors.log", true);
op.WriteLine("# {0}", Core.Now);
op.WriteLine(ex);
op.WriteLine();
op.WriteLine();
}
catch
{
// ignored
}
Console.WriteLine(ex);
}
}