fix(codegen): Adds manual dirty checking (#621)

- Adds `[ManualDirtyChecking]` for anyone that adds it themselves.
- Adds detection of `[Serializable]` and `[ManualDirtyChecking]` at startup to help identify scripts that need migration.
This commit is contained in:
Kamron Batman 2021-05-27 00:54:21 -07:00 committed by GitHub
parent 90dc5e742c
commit 81e4087acc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 79 additions and 53 deletions

View file

@ -63,8 +63,6 @@ namespace Server.Guilds
[CommandProperty(AccessLevel.Counselor)]
public Serial Serial { get; }
bool ISerializable.UseDirtyChecking => false;
long ISerializable.SavePosition { get; set; } = -1;
BufferWriter ISerializable.SaveBuffer { get; set; }

View file

@ -36,25 +36,24 @@ namespace Server
public class Entity : IEntity
{
public Entity(Serial serial, Point3D loc, Map map)
public Entity(Serial serial, Point3D loc, Map map) : this(serial)
{
Serial = serial;
Location = loc;
Map = map;
Deleted = false;
}
public Entity(Serial serial) => Serial = serial;
public void SetTypeRef(Type type)
{
}
bool ISerializable.UseDirtyChecking => false;
long ISerializable.SavePosition { get; set; } = -1;
BufferWriter ISerializable.SaveBuffer { get; set; }
public int TypeRef { get; } = -1;
public int TypeRef => -1;
public Serial Serial { get; }

View file

@ -783,8 +783,6 @@ namespace Server
AddNameProperties(list);
}
public virtual bool UseDirtyChecking => false;
long ISerializable.SavePosition { get; set; } = -1;
BufferWriter ISerializable.SaveBuffer { get; set; }

View file

@ -24,6 +24,7 @@ using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Server.Buffers;
using Server.Json;
using Server.Logging;
using Server.Network;
@ -232,7 +233,7 @@ namespace Server
public static bool EJ => Expansion >= Expansion.EJ;
public static string FindDataFile(string path, bool throwNotFound = true, bool warnNotFound = false)
public static string FindDataFile(string path, bool throwNotFound = true)
{
string fullPath = null;
@ -248,16 +249,9 @@ namespace Server
fullPath = null;
}
if (fullPath == null && (throwNotFound || warnNotFound))
if (fullPath == null && throwNotFound)
{
Utility.PushColor(ConsoleColor.Red);
Console.WriteLine($"Data: {path} was not found");
Console.WriteLine("Make sure modernuo.json is properly configured");
Utility.PopColor();
if (throwNotFound)
{
throw new FileNotFoundException($"Data: {path} was not found");
}
throw new FileNotFoundException($"Data: {path} was not found");
}
return fullPath;
@ -561,49 +555,57 @@ namespace Server
private static void VerifyType(Type type)
{
var isItem = type.IsSubclassOf(typeof(Item));
if (!isItem && !type.IsSubclassOf(typeof(Mobile)))
if (!type.IsAssignableTo(typeof(ISerializable)) || type.IsInterface || type.IsAbstract)
{
return;
}
if (isItem)
if (type.IsSubclassOf(typeof(Item)))
{
Interlocked.Increment(ref _itemCount);
}
else
else if (!type.IsSubclassOf(typeof(Mobile)))
{
Interlocked.Increment(ref _mobileCount);
}
StringBuilder warningSb = null;
ValueStringBuilder errors = new ValueStringBuilder();
try
{
if (World.DirtyTrackingEnabled)
{
var manualDirtyCheckingAttribute = type.GetCustomAttribute<ManualDirtyCheckingAttribute>(false);
var codeGennedAttribute = type.GetCustomAttribute<SerializableAttribute>(false);
if (manualDirtyCheckingAttribute == null && codeGennedAttribute == null)
{
errors.AppendLine(" - No property tracking (dirty checking)");
}
}
if (type.GetConstructor(_serialTypeArray) == null)
{
warningSb = new StringBuilder();
warningSb.AppendLine(" - No serialization constructor");
errors.AppendLine(" - No serialization constructor");
}
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.DeclaredOnly;
if (type.GetMethod("Serialize", bindingFlags) == null)
{
warningSb ??= new StringBuilder();
warningSb.AppendLine(" - No Serialize() method");
errors.AppendLine(" - No Serialize() method");
}
if (type.GetMethod("Deserialize", bindingFlags) == null)
{
warningSb ??= new StringBuilder();
warningSb.AppendLine(" - No Deserialize() method");
errors.AppendLine(" - No Deserialize() method");
}
if (warningSb?.Length > 0)
if (errors.Length > 0)
{
Console.WriteLine("Warning: {0}\n{1}", type, warningSb);
Utility.PushColor(ConsoleColor.Red);
Console.WriteLine($"{type}\n{errors.ToString()}");
Utility.PopColor();
}
}
catch

View file

@ -2521,8 +2521,6 @@ namespace Server
AddNameProperties(list);
}
public virtual bool UseDirtyChecking => false;
long ISerializable.SavePosition { get; set; } = -1;
BufferWriter ISerializable.SaveBuffer { get; set; }

View file

@ -20,10 +20,6 @@ namespace Server
{
public interface ISerializable
{
// Make sure all properties that will be serialized are calling `MarkDirty()` when they get modified.
// This should be done manually or via code gen through SerializedField attribute.
// This attribute should be virtual for any base serializalbe type (Item, Mobile, etc) that can be opt-in per type.
bool UseDirtyChecking { get; }
long SavePosition { get; protected set; }
BufferWriter SaveBuffer { get; protected internal set; }
int TypeRef { get; }
@ -43,7 +39,7 @@ namespace Server
public void InitializeSaveBuffer(byte[] buffer)
{
SaveBuffer = new BufferWriter(buffer, true);
if (UseDirtyChecking)
if (World.DirtyTrackingEnabled)
{
SavePosition = SaveBuffer.Position;
}
@ -67,7 +63,7 @@ namespace Server
SaveBuffer.Seek(0, SeekOrigin.Begin);
Serialize(SaveBuffer);
if (UseDirtyChecking)
if (World.DirtyTrackingEnabled)
{
SavePosition = SaveBuffer.Position;
}

View file

@ -0,0 +1,28 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2021 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: ManualDirtyChecking.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/>. *
*************************************************************************/
using System;
namespace Server
{
/// <summary>
/// Indicates that the applied class has dirty checking. This is necessary for classes that are not code genned.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class ManualDirtyCheckingAttribute : Attribute
{
}
}

View file

@ -3,11 +3,13 @@ using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Server.Logging;
namespace Server
{
public class TileMatrix
{
private static readonly ILogger logger = LogFactory.GetLogger(typeof(TileMatrix));
private static readonly List<TileMatrix> _instances = new();
private readonly StaticTile[][][][][] _staticTiles;
@ -59,15 +61,9 @@ namespace Server
_map = owner;
#if DEBUG
const bool warnNotFound = true;
#else
const bool warnNotFound = false;
#endif
if (fileIndex != 0x7F)
{
var mapPath = Core.FindDataFile($"map{fileIndex}.mul", false, warnNotFound);
var mapPath = Core.FindDataFile($"map{fileIndex}.mul", false);
if (mapPath != null)
{
@ -75,29 +71,41 @@ namespace Server
}
else
{
mapPath = Core.FindDataFile($"map{fileIndex}LegacyMUL.uop", false, warnNotFound);
mapPath = Core.FindDataFile($"map{fileIndex}LegacyMUL.uop", false);
if (mapPath != null)
{
MapStream = new FileStream(mapPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
_mapIndex = new UOPIndex(MapStream);
}
else
{
logger.Warning($"map{fileIndex}.mul was not found.");
}
}
var indexPath = Core.FindDataFile($"staidx{fileIndex}.mul", false, warnNotFound);
var indexPath = Core.FindDataFile($"staidx{fileIndex}.mul", false);
if (indexPath != null)
{
IndexStream = new FileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
IndexReader = new BinaryReader(IndexStream);
}
else
{
logger.Warning($"staidx{fileIndex}.mul was not found.");
}
var staticsPath = Core.FindDataFile($"statics{fileIndex}.mul", false, warnNotFound);
var staticsPath = Core.FindDataFile($"statics{fileIndex}.mul", false);
if (staticsPath != null)
{
DataStream = new FileStream(staticsPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
else
{
logger.Warning($"statics{fileIndex}.mul was not found.");
}
}
_emptyStaticBlock = new StaticTile[8][][];

View file

@ -48,6 +48,7 @@ namespace Server
private static string _tempSavePath; // Path to the temporary folder for the save
private static string _savePath; // Path to "Saves" folder
public const bool DirtyTrackingEnabled = false;
public const uint ItemOffset = 0x40000000;
public const uint MaxItemSerial = 0x7FFFFFFF;
public const uint MaxMobileSerial = ItemOffset - 1;

View file

@ -265,8 +265,6 @@ namespace Server.Accounting
}
}
bool ISerializable.UseDirtyChecking => false;
long ISerializable.SavePosition { get; set; }
BufferWriter ISerializable.SaveBuffer { get; set; }