2020-09-12 00:05:38 -07:00
|
|
|
/*************************************************************************
|
|
|
|
|
* ModernUO *
|
2024-06-23 10:51:20 -07:00
|
|
|
* Copyright 2019-2024 - ModernUO Development Team *
|
2020-09-12 00:05:38 -07:00
|
|
|
* Email: hi@modernuo.com *
|
|
|
|
|
* File: Main.cs *
|
|
|
|
|
* *
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
*************************************************************************/
|
2020-08-25 18:53:35 -07:00
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2024-11-01 21:21:10 -07:00
|
|
|
using System.Globalization;
|
2020-08-25 18:53:35 -07:00
|
|
|
using System.IO;
|
2021-10-16 10:48:58 -07:00
|
|
|
using System.Linq;
|
2020-08-25 18:53:35 -07:00
|
|
|
using System.Reflection;
|
2022-10-27 23:27:22 -07:00
|
|
|
using System.Runtime.CompilerServices;
|
2020-08-25 18:53:35 -07:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
2021-02-14 18:30:53 -08:00
|
|
|
using System.Text.Json;
|
2020-08-25 18:53:35 -07:00
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2024-06-02 15:02:47 -07:00
|
|
|
using Server.Compression;
|
2020-08-25 18:53:35 -07:00
|
|
|
using Server.Json;
|
2021-04-20 08:43:54 +02:00
|
|
|
using Server.Logging;
|
2020-08-25 18:53:35 -07:00
|
|
|
using Server.Network;
|
2023-08-01 04:46:49 +01:00
|
|
|
using Server.Text;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
namespace Server;
|
|
|
|
|
|
|
|
|
|
public static class Core
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
private static readonly ILogger logger = LogFactory.GetLogger(typeof(Core));
|
2021-04-20 08:43:54 +02:00
|
|
|
|
2023-11-25 10:11:24 -08:00
|
|
|
private static bool _performProcessKill;
|
|
|
|
|
private static bool _restartOnKill;
|
2023-10-03 00:42:49 -07:00
|
|
|
private static bool _performSnapshot;
|
2024-06-23 10:51:20 -07:00
|
|
|
private static string _snapshotPath;
|
2022-10-10 21:47:08 -07:00
|
|
|
private static bool _crashed;
|
|
|
|
|
private static string _baseDirectory;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private static bool? _isRunningFromXUnit;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private static int _itemCount;
|
|
|
|
|
private static int _mobileCount;
|
|
|
|
|
public static EventLoopContext LoopContext { get; set; }
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private static readonly Type[] _serialTypeArray = { typeof(Serial) };
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
|
|
|
|
public static readonly bool IsDarwin = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
|
|
|
|
public static readonly bool IsFreeBSD = RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD);
|
|
|
|
|
public static readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || IsFreeBSD;
|
|
|
|
|
public static readonly bool IsBSD = IsDarwin || IsFreeBSD;
|
|
|
|
|
public static readonly bool Unix = IsBSD || IsLinux;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private const string AssembliesConfiguration = "Data/assemblies.json";
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2021-01-18 21:05:11 -08:00
|
|
|
#nullable enable
|
2022-10-10 21:47:08 -07:00
|
|
|
// TODO: Find a way to get rid of this
|
|
|
|
|
public static bool IsRunningFromXUnit
|
|
|
|
|
{
|
|
|
|
|
get
|
2021-01-18 21:05:11 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
if (_isRunningFromXUnit != null)
|
2021-01-18 21:05:11 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
return _isRunningFromXUnit.Value;
|
|
|
|
|
}
|
2021-01-18 21:05:11 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
|
|
|
|
|
{
|
|
|
|
|
if (a.FullName.InsensitiveStartsWith("xunit"))
|
2021-01-18 21:05:11 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
_isRunningFromXUnit = true;
|
|
|
|
|
return true;
|
2021-01-18 21:05:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
|
|
|
|
|
_isRunningFromXUnit = false;
|
|
|
|
|
return false;
|
2021-01-18 21:05:11 -08:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2021-02-05 14:09:57 -08:00
|
|
|
#nullable restore
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2024-05-30 23:34:03 -07:00
|
|
|
public static Assembly ApplicationAssembly { get; set; }
|
2022-10-10 21:47:08 -07:00
|
|
|
public static Assembly Assembly { get; set; }
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
// Assembly file version
|
|
|
|
|
public static Version Version => new(ThisAssembly.AssemblyFileVersion);
|
2020-09-02 17:50:57 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static Process Process { get; private set; }
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static Thread Thread { get; private set; }
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2023-03-09 21:00:17 -08:00
|
|
|
private static long _firstTick;
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private static long _tickCount;
|
2021-03-13 01:32:04 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private static DateTime _now;
|
2021-01-25 21:06:46 -08:00
|
|
|
|
2024-09-11 00:55:18 -07:00
|
|
|
public static long TickCount => _tickCount;
|
2021-03-13 01:32:04 -08:00
|
|
|
|
2024-09-11 00:55:18 -07:00
|
|
|
public static DateTime Now => _now;
|
2021-09-17 00:15:16 -07:00
|
|
|
|
2024-09-11 00:55:18 -07:00
|
|
|
public static long Uptime => TickCount - _firstTick;
|
2023-03-09 21:00:17 -08:00
|
|
|
|
2023-10-30 18:05:32 -07:00
|
|
|
private static long _cycleIndex;
|
|
|
|
|
private static readonly double[] _cyclesPerSecond = new double[128];
|
|
|
|
|
|
|
|
|
|
public static double CyclesPerSecond => _cyclesPerSecond[_cycleIndex];
|
2021-09-17 00:15:16 -07:00
|
|
|
|
2023-10-30 18:05:32 -07:00
|
|
|
public static double AverageCPS => _cyclesPerSecond.Average();
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static string BaseDirectory
|
|
|
|
|
{
|
|
|
|
|
get
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
if (_baseDirectory == null)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
try
|
2020-09-11 09:46:15 +04:00
|
|
|
{
|
2024-05-30 23:34:03 -07:00
|
|
|
_baseDirectory = ApplicationAssembly.Location;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (_baseDirectory.Length > 0)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
_baseDirectory = Path.GetDirectoryName(_baseDirectory);
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2020-09-11 09:46:15 +04:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
_baseDirectory = "";
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
|
|
|
|
|
return _baseDirectory;
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static CancellationTokenSource ClosingTokenSource { get; } = new();
|
2020-09-12 00:05:38 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool Closing => ClosingTokenSource.IsCancellationRequested;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static int GlobalUpdateRange { get; set; } = 18;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static int GlobalMaxUpdateRange { get; set; } = 24;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static int ScriptItems => _itemCount;
|
|
|
|
|
public static int ScriptMobiles => _mobileCount;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static Expansion Expansion { get; set; }
|
|
|
|
|
public static bool T2A => Expansion >= Expansion.T2A;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool UOR => Expansion >= Expansion.UOR;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool UOTD => Expansion >= Expansion.UOTD;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool LBR => Expansion >= Expansion.LBR;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool AOS => Expansion >= Expansion.AOS;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool SE => Expansion >= Expansion.SE;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool ML => Expansion >= Expansion.ML;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool SA => Expansion >= Expansion.SA;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool HS => Expansion >= Expansion.HS;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool TOL => Expansion >= Expansion.TOL;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static bool EJ => Expansion >= Expansion.EJ;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static string FindDataFile(string path, bool throwNotFound = true)
|
|
|
|
|
{
|
|
|
|
|
string fullPath = null;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
foreach (var p in ServerConfiguration.DataDirectories)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
fullPath = Path.Combine(p, path);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (IsLinux && !File.Exists(fullPath))
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
var fi = new FileInfo(fullPath);
|
|
|
|
|
if (fi.Directory != null && Directory.Exists(fi.Directory.FullName))
|
2020-09-11 09:46:15 +04:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
fullPath = fi.Directory.EnumerateFiles(
|
|
|
|
|
fi.Name,
|
|
|
|
|
new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive }
|
|
|
|
|
).FirstOrDefault()?.FullName;
|
2020-09-11 09:46:15 +04:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (File.Exists(fullPath))
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
break;
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
fullPath = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fullPath == null && throwNotFound)
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException($"Data: {path} was not found");
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
return fullPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<string> FindDataFileByPattern(string pattern)
|
|
|
|
|
{
|
|
|
|
|
var options = new EnumerationOptions { MatchCasing = MatchCasing.CaseInsensitive };
|
|
|
|
|
foreach (var p in ServerConfiguration.DataDirectories)
|
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
if (Directory.Exists(p))
|
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
foreach (var file in Directory.EnumerateFiles(p, pattern, options))
|
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
yield return file;
|
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
2022-05-08 21:24:32 -07:00
|
|
|
|
2023-11-25 10:11:24 -08:00
|
|
|
public static void Kill(bool restart = false)
|
|
|
|
|
{
|
|
|
|
|
_restartOnKill = restart;
|
2024-06-06 21:20:22 -07:00
|
|
|
_performProcessKill = true;
|
2023-11-25 10:11:24 -08:00
|
|
|
}
|
|
|
|
|
|
2024-05-30 23:34:03 -07:00
|
|
|
public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
|
|
|
|
Console.WriteLine(e.IsTerminating ? "Error:" : "Warning:");
|
|
|
|
|
Console.WriteLine(e.ExceptionObject);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (e.IsTerminating)
|
|
|
|
|
{
|
|
|
|
|
_crashed = true;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
var close = false;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var args = new ServerCrashedEventArgs(e.ExceptionObject as Exception);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
EventSink.InvokeServerCrashed(args);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
close = args.Close;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (!close)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("This exception is fatal, press return to exit");
|
2024-04-05 15:07:43 -07:00
|
|
|
ConsoleInputHandler.ReadLine();
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
|
2024-06-06 21:20:22 -07:00
|
|
|
DoKill();
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!Closing)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
HandleClosed();
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private static void Console_CancelKeyPressed(object sender, ConsoleCancelEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var keypress = e.SpecialKey switch
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
ConsoleSpecialKey.ControlBreak => "CTRL+BREAK",
|
|
|
|
|
_ => "CTRL+C"
|
|
|
|
|
};
|
2020-09-12 00:05:38 -07:00
|
|
|
|
2022-11-13 00:36:23 -08:00
|
|
|
logger.Information("Detected {Key} pressed.", keypress);
|
2022-10-10 21:47:08 -07:00
|
|
|
e.Cancel = true;
|
|
|
|
|
Kill();
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-25 10:11:24 -08:00
|
|
|
internal static void DoKill(bool restart = false)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
|
|
|
|
if (Closing)
|
|
|
|
|
{
|
|
|
|
|
return;
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
HandleClosed();
|
|
|
|
|
|
|
|
|
|
if (restart)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2024-05-30 23:34:03 -07:00
|
|
|
try
|
2020-09-11 09:46:15 +04:00
|
|
|
{
|
2024-05-30 23:34:03 -07:00
|
|
|
logger.Information("Restarting");
|
|
|
|
|
if (IsWindows)
|
|
|
|
|
{
|
2024-09-11 00:55:18 -07:00
|
|
|
using var process = Process.Start("dotnet", $"{ApplicationAssembly.Location}");
|
2024-05-30 23:34:03 -07:00
|
|
|
}
|
|
|
|
|
else
|
2020-09-12 00:05:38 -07:00
|
|
|
{
|
2024-06-06 21:20:22 -07:00
|
|
|
using var process = new Process();
|
|
|
|
|
process.StartInfo = new ProcessStartInfo
|
2020-09-12 00:05:38 -07:00
|
|
|
{
|
2024-06-06 21:20:22 -07:00
|
|
|
FileName = "dotnet",
|
2024-09-11 00:55:18 -07:00
|
|
|
Arguments = $"{ApplicationAssembly.Location}",
|
2024-06-06 21:20:22 -07:00
|
|
|
UseShellExecute = true
|
2024-05-30 23:34:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
process.Start();
|
|
|
|
|
}
|
|
|
|
|
logger.Information("Restart done");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error(e, "Restart failed");
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
|
2024-06-06 21:20:22 -07:00
|
|
|
Environment.Exit(0);
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private static void HandleClosed()
|
|
|
|
|
{
|
|
|
|
|
ClosingTokenSource.Cancel();
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
logger.Information("Shutting down");
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
World.WaitForWriteCompletion();
|
2023-10-09 00:22:59 -07:00
|
|
|
World.ExitSerializationThreads();
|
2024-09-19 16:54:49 -07:00
|
|
|
PingServer.Shutdown();
|
|
|
|
|
TcpServer.Shutdown();
|
2023-10-09 00:22:59 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (!_crashed)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
EventSink.InvokeShutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 00:55:18 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static long GetTimestamp() => 1000L * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
|
|
|
|
|
|
|
|
|
|
public static void Setup(Assembly applicationAssembly, Process process)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2024-11-01 21:21:10 -07:00
|
|
|
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
|
|
|
|
|
|
2024-05-30 23:34:03 -07:00
|
|
|
Process = process;
|
|
|
|
|
ApplicationAssembly = applicationAssembly;
|
|
|
|
|
Assembly = Assembly.GetAssembly(typeof(Core));
|
|
|
|
|
Thread = Thread.CurrentThread;
|
|
|
|
|
LoopContext = new EventLoopContext();
|
|
|
|
|
SynchronizationContext.SetSynchronizationContext(LoopContext);
|
2024-01-15 08:29:54 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve += AssemblyHandler.AssemblyResolver;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2024-05-30 23:34:03 -07:00
|
|
|
Console.OutputEncoding = Encoding.UTF8;
|
|
|
|
|
Thread.Name = "Core Thread";
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (BaseDirectory.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Directory.SetCurrentDirectory(BaseDirectory);
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
Utility.PushColor(ConsoleColor.Green);
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
"ModernUO - [https://github.com/modernuo/modernuo] Version {0}.{1}.{2}.{3}",
|
|
|
|
|
Version.Major,
|
|
|
|
|
Version.Minor,
|
|
|
|
|
Version.Build,
|
|
|
|
|
Version.Revision
|
|
|
|
|
);
|
|
|
|
|
Utility.PopColor();
|
|
|
|
|
|
|
|
|
|
Utility.PushColor(ConsoleColor.DarkGray);
|
2023-08-09 09:09:26 -07:00
|
|
|
Console.WriteLine(@"Copyright 2019-2023 ModernUO Development Team
|
2020-09-12 15:31:21 -07:00
|
|
|
This program comes with ABSOLUTELY NO WARRANTY;
|
|
|
|
|
This is free software, and you are welcome to redistribute it under certain conditions.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
".TrimMultiline());
|
2022-10-10 21:47:08 -07:00
|
|
|
Utility.PopColor();
|
2020-09-12 15:31:21 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
Console.CancelKeyPress += Console_CancelKeyPressed;
|
2021-08-08 23:42:10 -07:00
|
|
|
|
2024-06-02 15:02:47 -07:00
|
|
|
// LibDeflate is not thread safe, so we need to create a new instance for each thread
|
|
|
|
|
var standard = Deflate.Standard;
|
|
|
|
|
AppDomain.CurrentDomain.ProcessExit += (_, _) => standard.Dispose();
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
ServerConfiguration.Load();
|
2021-08-08 23:42:10 -07:00
|
|
|
|
2022-11-13 00:36:23 -08:00
|
|
|
logger.Information("Running on {Framework}", RuntimeInformation.FrameworkDescription);
|
2020-09-12 15:31:21 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
var assemblyPath = Path.Join(BaseDirectory, AssembliesConfiguration);
|
2020-09-10 23:38:06 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
// Load UOContent.dll
|
|
|
|
|
var assemblyFiles = JsonConfig.Deserialize<List<string>>(assemblyPath)?.ToArray();
|
|
|
|
|
if (assemblyFiles == null)
|
|
|
|
|
{
|
|
|
|
|
throw new JsonException($"Failed to deserialize {assemblyPath}.");
|
|
|
|
|
}
|
2021-02-14 18:30:53 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
for (var i = 0; i < assemblyFiles.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
assemblyFiles[i] = Path.Join(BaseDirectory, "Assemblies", assemblyFiles[i]);
|
|
|
|
|
}
|
2020-09-10 23:38:06 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
AssemblyHandler.LoadAssemblies(assemblyFiles);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
VerifySerialization();
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2024-09-11 00:55:18 -07:00
|
|
|
_now = DateTime.UtcNow;
|
|
|
|
|
_firstTick = _tickCount = GetTimestamp();
|
|
|
|
|
|
|
|
|
|
Timer.Init(_tickCount);
|
2021-07-11 22:42:06 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
AssemblyHandler.Invoke("Configure");
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
TileMatrixLoader.LoadTileMatrix();
|
2021-02-14 16:06:49 -08:00
|
|
|
|
2022-11-21 10:38:20 -08:00
|
|
|
RegionJsonSerializer.LoadRegions();
|
2022-10-10 21:47:08 -07:00
|
|
|
World.Load();
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
AssemblyHandler.Invoke("Initialize");
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
TcpServer.Start();
|
2023-09-03 09:29:27 -07:00
|
|
|
PingServer.Start();
|
2022-10-10 21:47:08 -07:00
|
|
|
EventSink.InvokeServerStarted();
|
|
|
|
|
RunEventLoop();
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static void RunEventLoop()
|
|
|
|
|
{
|
|
|
|
|
try
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2021-09-17 00:15:16 -07:00
|
|
|
#if DEBUG
|
2022-10-10 21:47:08 -07:00
|
|
|
const bool idleCPU = true;
|
2021-09-17 00:15:16 -07:00
|
|
|
#else
|
2023-07-24 04:12:38 +01:00
|
|
|
var idleCPU = ServerConfiguration.GetOrUpdateSetting("core.enableIdleCPU", false);
|
2021-09-17 00:15:16 -07:00
|
|
|
#endif
|
|
|
|
|
|
2023-10-30 18:05:32 -07:00
|
|
|
var cycleCount = _cyclesPerSecond.Length;
|
2024-09-11 00:55:18 -07:00
|
|
|
long last = _tickCount;
|
2022-10-10 21:47:08 -07:00
|
|
|
const int interval = 100;
|
2023-10-30 18:05:32 -07:00
|
|
|
double frequency = Stopwatch.Frequency * interval;
|
2021-09-17 00:15:16 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
int sample = 0;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
while (!Closing)
|
|
|
|
|
{
|
2024-09-11 00:55:18 -07:00
|
|
|
_tickCount = GetTimestamp();
|
2022-10-10 21:47:08 -07:00
|
|
|
_now = DateTime.UtcNow;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
Mobile.ProcessDeltaQueue();
|
|
|
|
|
Item.ProcessDeltaQueue();
|
|
|
|
|
Timer.Slice(_tickCount);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
// Handle networking
|
|
|
|
|
NetState.Slice();
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
// Execute captured post-await methods (like Timer.Pause)
|
|
|
|
|
LoopContext.ExecuteTasks();
|
feat(timers): Adds timer pooling, fixes timer related bugs, and changes timer api (#667)
### Changes/Fixes:
* Adds timer pooling.
* Allows pool to be configurable in ModernUO.json
* Pool replenishes itself asynchronously if depleted.
* Fixes an issue with barkeeps and town criers
* Fixes an issue with incognito buff icons not being removed
* Fixes an issue with polymorph name mod not being removed
* Fixes several places where timers go on forever even after an object is deleted, keeping a reference (memory leak)
* Eliminates the timer for MiningCart altogether.
* Deletes `AcidSlime` since it is a duplicate of `PoolOfAcid`
* Fixes HonorableExecution and standardizes the code for other Bushido moves.
## Changes to the Timer API:
```cs
public class Timer
{
// Creates a timer that will be returned to the pool once execution stops.
public static void StartTimer(Action callback);
public static void StartTimer(TimeSpan delay, Action callback);
public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback);
public static void StartTimer(TimeSpan interval, int count, Action callback);
public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback);
// Creates a timer and returns a token for more control. Requires manual cancellation in order for the timer to be returned to the pool.
// If the token is dereferenced, the timer will be dereferenced too. While not returning a timer to the pool is not considered hazardous, it does defeat the purpose of pooled timers.
public static void StartTimer(Action callback, out TimerExecutionToken token);
public static void StartTimer(TimeSpan delay, Action callback, out TimerExecutionToken token);
public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback, out TimerExecutionToken token);
public static void StartTimer(TimeSpan interval, int count, Action callback, out TimerExecutionToken token);
public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback, out TimerExecutionToken token);
// If you aren't sure how to use the API above, or you don't care about performance, then you can use the old RunUO Timer.DelayCall
public static DelayCallTimer DelayCall(Action callback);
public static DelayCallTimer DelayCall(TimeSpan delay, Action callback);
public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, Action callback);
public static DelayCallTimer DelayCall(TimeSpan interval, int count, Action callback);
public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, int count, Action callback);
}
public struct TimerExecutionToken
{
public bool Running { get; }
public int Index { get; }
public int RemainingCount { get; }
public DateTime Next { get; }
}
```
## When to use `TimerExecutionToken`?
Use tokens when you want to gain the performance benefit of using a pooled timer, but you need one of the following:
* Access to the next time the timer will tick:`token.Next`
* Access to which interval, how many intervals there are, or how many are remaining: `token.Index`, `token.Count`, and `token.RemainingCount`
* Stop a timer manually.
* Determine if the timer is running: `timer.Running`
* See notes below about requirements for using tokens!
## Notes about using the TimerExecutionToken:
When you opt-in to receive a token, you must call `Cancel()` to return the timer. This can be done inside of the callback, or outside of the callback at any time.
If this is not called and your timer is an infinite interval, then you will create a potential memory leak, or null pointer exception in your callback.
If the timer ends and is stopped, but cancel is not called, then the timer will never return to the pool and stay referenced until the token is deleted or cancel is called. (Memory leak)
## Is this thread safe?
No. The ModernUO timer system is not thread safe at all. If you require a thread safe timer system, contact me and I'll help adapt this system. Keep in mind that there is a massive performance hit to make this thread safe when there are literally no use cases for it.
If you need to synchronize execution, meaning you want to execute code from another thread on the core thread. Let's say you have a discord bot that is pushing commands to the game server. Then use `EventLoopContext.Post(SendOrPostCallback callback, object state);`.
2021-08-07 14:33:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
Timer.CheckTimerPool(); // Check for pool depletion so we can async refill it.
|
2021-01-25 21:06:46 -08:00
|
|
|
|
2023-10-03 00:42:49 -07:00
|
|
|
if (_performSnapshot)
|
|
|
|
|
{
|
|
|
|
|
// Return value is the offset that can be used to fix timers that should drift
|
2024-06-23 10:51:20 -07:00
|
|
|
World.Snapshot(_snapshotPath);
|
2024-01-15 08:29:54 -08:00
|
|
|
_performSnapshot = false;
|
2023-10-03 00:42:49 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-25 10:11:24 -08:00
|
|
|
if (_performProcessKill)
|
|
|
|
|
{
|
2024-06-23 10:51:20 -07:00
|
|
|
World.WaitForWriteCompletion();
|
2024-06-06 21:20:22 -07:00
|
|
|
break;
|
2023-11-25 10:11:24 -08:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 00:55:18 -07:00
|
|
|
if (sample++ == interval)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2024-09-11 00:55:18 -07:00
|
|
|
sample = 0;
|
|
|
|
|
var now = GetTimestamp();
|
2023-10-30 18:05:32 -07:00
|
|
|
|
|
|
|
|
var cyclesPerSecond = frequency / (now - last);
|
|
|
|
|
_cyclesPerSecond[_cycleIndex++] = cyclesPerSecond;
|
|
|
|
|
if (_cycleIndex == cycleCount)
|
|
|
|
|
{
|
|
|
|
|
_cycleIndex = 0;
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
last = now;
|
2021-09-17 00:15:16 -07:00
|
|
|
|
2023-10-30 18:05:32 -07:00
|
|
|
if (idleCPU && cyclesPerSecond > 125)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
|
|
|
|
Thread.Sleep(2);
|
2020-12-26 13:53:43 -08:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
catch (Exception e)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
CurrentDomain_UnhandledException(null, new UnhandledExceptionEventArgs(e, true));
|
2024-06-06 21:20:22 -07:00
|
|
|
return;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2024-06-06 21:20:22 -07:00
|
|
|
|
|
|
|
|
DoKill(_restartOnKill);
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2024-06-23 10:51:20 -07:00
|
|
|
internal static void RequestSnapshot(string snapshotPath)
|
|
|
|
|
{
|
|
|
|
|
_performSnapshot = true;
|
|
|
|
|
_snapshotPath = snapshotPath;
|
|
|
|
|
}
|
2023-10-03 00:42:49 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public static void VerifySerialization()
|
|
|
|
|
{
|
|
|
|
|
_itemCount = 0;
|
|
|
|
|
_mobileCount = 0;
|
|
|
|
|
|
|
|
|
|
var callingAssembly = Assembly.GetCallingAssembly();
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
VerifySerialization(callingAssembly);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
foreach (var assembly in AssemblyHandler.Assemblies)
|
|
|
|
|
{
|
|
|
|
|
if (assembly != callingAssembly)
|
2020-09-11 09:46:15 +04:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
VerifySerialization(assembly);
|
2020-09-11 09:46:15 +04:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private static void VerifyType(Type type)
|
|
|
|
|
{
|
|
|
|
|
if (!type.IsAssignableTo(typeof(ISerializable)) || type.IsInterface || type.IsAbstract)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (type.IsSubclassOf(typeof(Item)))
|
|
|
|
|
{
|
|
|
|
|
Interlocked.Increment(ref _itemCount);
|
|
|
|
|
}
|
|
|
|
|
else if (type.IsSubclassOf(typeof(Mobile)))
|
|
|
|
|
{
|
|
|
|
|
Interlocked.Increment(ref _mobileCount);
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
using var errors = ValueStringBuilder.CreateMT();
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (World.DirtyTrackingEnabled)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
var manualDirtyCheckingAttribute = type.GetCustomAttribute<ManualDirtyCheckingAttribute>(false);
|
|
|
|
|
var codeGennedAttribute = type.GetCustomAttribute<ModernUO.Serialization.SerializationGeneratorAttribute>(false);
|
2021-05-27 00:54:21 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (manualDirtyCheckingAttribute == null && codeGennedAttribute == null)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
errors.AppendLine(" - No property tracking (dirty checking)");
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (type.GetConstructor(_serialTypeArray) == null)
|
|
|
|
|
{
|
|
|
|
|
errors.AppendLine(" - No serialization constructor");
|
|
|
|
|
}
|
2021-06-02 23:12:52 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic |
|
|
|
|
|
BindingFlags.Instance | BindingFlags.DeclaredOnly;
|
2021-06-02 23:12:52 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
var hasSerializeMethod = false;
|
|
|
|
|
var hasDeserializeMethod = false;
|
2021-06-02 23:12:52 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
foreach (var method in type.GetMethods(bindingFlags))
|
|
|
|
|
{
|
|
|
|
|
if (method.Name == "Serialize")
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
hasSerializeMethod = true;
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (method.Name == "Deserialize")
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
var parameters = method.GetParameters();
|
|
|
|
|
if (parameters.Length == 1 && parameters[0].ParameterType == typeof(IGenericReader))
|
|
|
|
|
{
|
|
|
|
|
hasDeserializeMethod = true;
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (!hasSerializeMethod)
|
|
|
|
|
{
|
|
|
|
|
errors.AppendLine(" - No Serialize() method");
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
|
|
|
|
|
if (!hasDeserializeMethod)
|
2021-06-02 23:12:52 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
errors.AppendLine(" - No Deserialize() method");
|
2021-06-02 23:12:52 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
|
|
|
|
|
if (errors.Length > 0)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
Utility.PushColor(ConsoleColor.Red);
|
2022-11-13 00:36:23 -08:00
|
|
|
Console.WriteLine($"{type}{Environment.NewLine}{errors.ToString()}");
|
2022-10-10 21:47:08 -07:00
|
|
|
Utility.PopColor();
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
catch (AmbiguousMatchException e)
|
|
|
|
|
{
|
|
|
|
|
// ignored
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Warning: Exception in serialization verification of type {0}", type);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private static void VerifySerialization(Assembly assembly)
|
|
|
|
|
{
|
|
|
|
|
if (assembly != null)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
Parallel.ForEach(assembly.GetTypes(), VerifyType);
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|