modernuo/Projects/Server/Guild.cs

121 lines
3.4 KiB
C#
Raw Permalink Normal View History

feat: Source generated Serialization/Deserialization (#550) ### Features * Fully abstracts serialization by using compile-time attributes. * Supports serializing the following: - Primitives (integers, strings, etc) - IP Addresses - BigDecimal - DateTime, Delta DateTimes - TimeSpan - Server.Race - Server.Map - Point2D, Point3D, Rect2D, Rect3D - Existing/New `ISerializable` references - Lists/Sets of serializable types - Type with a `Serialize` method and constructor that takes an `IGenericReader` * Supports forward-only migration * Supports existing RunUO deserialization for older versions by changing to the following signature: - `public void OldDeserialize(IGenericReader reader, int version)` - Must remove deserializing the version since this is already done * Supports serializing from private fields or custom made properties. * Types do not require inheriting Item/Mobile. Code gen will fully create `ISerializable` information. - This is not recommended yet, since it requires wiring to `Persistence` which will cause lots of unresolved symbol errors until code gen is built. ### Example ```cs using System.Collections.Generic; namespace Server.Items { [Serializable(1)] public partial class TestItem1 : Item { [SerializableField(1)] [SerializableFieldAttr("[CommandProperty(AccessLevel.Administrator)]")] private List<Item> _someProperty; private void Deserialize(IGenericReader reader, int version) { } } } ``` Generates this: ```cs namespace Server.Items { public partial class TestItem1 { #pragma warning disable 0414 private const int _version = 1; #pragma warning restore 0414 [CommandProperty(AccessLevel.Administrator)] public System.Collections.Generic.List<Server.Item> SomeProperty { get => _someProperty; set { if (value != _someProperty) { ((ISerializable)this).MarkDirty(); _someProperty = value; } } } public TestItem1(Serial serial) : base(serial) { } public override void Serialize(IGenericWriter writer) { var savePosition = ((Server.ISerializable)this).SavePosition; if (savePosition > -1) { writer.Seek(savePosition, System.IO.SeekOrigin.Begin); return; } writer.WriteEncodedInt(_version); writer.Write(_someProperty); } public override void Deserialize(IGenericReader reader) { var version = reader.ReadEncodedInt(); if (version < 1) { OldDeserialize(reader, version); ((Server.ISerializable)this).MarkDirty(); return; } SomeProperty = reader.ReadEntityList<Server.Item>(); } } } ``` And this: ```json { "version": 1, "type": "TestItem1", "properties": [ { "name": "SomeProperty", "type": "System.Collections.Generic.List\u003CServer.Item\u003E", "rule": "ListMigrationRule", "ruleArguments": [ "Server.Item", "SerializableInterfaceMigrationRule" ] } ] } ```
2021-05-23 21:06:23 -07:00
/*************************************************************************
* ModernUO *
* Copyright 2019-2024 - ModernUO Development Team *
feat: Source generated Serialization/Deserialization (#550) ### Features * Fully abstracts serialization by using compile-time attributes. * Supports serializing the following: - Primitives (integers, strings, etc) - IP Addresses - BigDecimal - DateTime, Delta DateTimes - TimeSpan - Server.Race - Server.Map - Point2D, Point3D, Rect2D, Rect3D - Existing/New `ISerializable` references - Lists/Sets of serializable types - Type with a `Serialize` method and constructor that takes an `IGenericReader` * Supports forward-only migration * Supports existing RunUO deserialization for older versions by changing to the following signature: - `public void OldDeserialize(IGenericReader reader, int version)` - Must remove deserializing the version since this is already done * Supports serializing from private fields or custom made properties. * Types do not require inheriting Item/Mobile. Code gen will fully create `ISerializable` information. - This is not recommended yet, since it requires wiring to `Persistence` which will cause lots of unresolved symbol errors until code gen is built. ### Example ```cs using System.Collections.Generic; namespace Server.Items { [Serializable(1)] public partial class TestItem1 : Item { [SerializableField(1)] [SerializableFieldAttr("[CommandProperty(AccessLevel.Administrator)]")] private List<Item> _someProperty; private void Deserialize(IGenericReader reader, int version) { } } } ``` Generates this: ```cs namespace Server.Items { public partial class TestItem1 { #pragma warning disable 0414 private const int _version = 1; #pragma warning restore 0414 [CommandProperty(AccessLevel.Administrator)] public System.Collections.Generic.List<Server.Item> SomeProperty { get => _someProperty; set { if (value != _someProperty) { ((ISerializable)this).MarkDirty(); _someProperty = value; } } } public TestItem1(Serial serial) : base(serial) { } public override void Serialize(IGenericWriter writer) { var savePosition = ((Server.ISerializable)this).SavePosition; if (savePosition > -1) { writer.Seek(savePosition, System.IO.SeekOrigin.Begin); return; } writer.WriteEncodedInt(_version); writer.Write(_someProperty); } public override void Deserialize(IGenericReader reader) { var version = reader.ReadEncodedInt(); if (version < 1) { OldDeserialize(reader, version); ((Server.ISerializable)this).MarkDirty(); return; } SomeProperty = reader.ReadEntityList<Server.Item>(); } } } ``` And this: ```json { "version": 1, "type": "TestItem1", "properties": [ { "name": "SomeProperty", "type": "System.Collections.Generic.List\u003CServer.Item\u003E", "rule": "ListMigrationRule", "ruleArguments": [ "Server.Item", "SerializableInterfaceMigrationRule" ] } ] } ```
2021-05-23 21:06:23 -07:00
* Email: hi@modernuo.com *
* File: Guild.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;
2020-08-25 18:53:35 -07:00
using System.Collections.Generic;
namespace Server.Guilds;
public enum GuildType
2020-08-25 18:53:35 -07:00
{
Regular,
Chaos,
Order
}
public abstract class BaseGuild : ISerializable
{
protected BaseGuild()
2020-08-25 18:53:35 -07:00
{
Serial = World.NewGuild;
World.AddGuild(this);
2020-08-25 18:53:35 -07:00
}
protected BaseGuild(Serial serial) => Serial = serial;
2020-08-25 18:53:35 -07:00
public abstract string Abbreviation { get; set; }
public abstract string Name { get; set; }
public abstract GuildType Type { get; set; }
public abstract bool Disbanded { get; }
public abstract void Delete();
2020-08-25 18:53:35 -07:00
public bool Deleted => Disbanded;
2020-08-25 18:53:35 -07:00
[IgnoreDupe]
[CommandProperty(AccessLevel.Counselor)]
public Serial Serial { get; }
2020-08-25 18:53:35 -07:00
[IgnoreDupe]
[CommandProperty(AccessLevel.GameMaster, readOnly: true)]
public DateTime Created { get; set; } = Core.Now;
public byte SerializedThread { get; set; }
public int SerializedPosition { get; set; }
public int SerializedLength { get; set; }
public abstract void Serialize(IGenericWriter writer);
public abstract void Deserialize(IGenericReader reader);
public abstract void OnDelete(Mobile mob);
2020-08-25 18:53:35 -07:00
public static BaseGuild FindByName(string name)
{
foreach (var g in World.Guilds.Values)
{
if (g.Name.InsensitiveEquals(name))
{
return g;
}
}
2020-08-25 18:53:35 -07:00
return null;
}
public static BaseGuild FindByAbbrev(string abbr)
{
foreach (var g in World.Guilds.Values)
{
if (g.Abbreviation.InsensitiveEquals(abbr))
{
return g;
}
}
2020-08-25 18:53:35 -07:00
return null;
}
2020-08-25 18:53:35 -07:00
public static HashSet<BaseGuild> Search(string find)
{
var words = find.ToLower().Split(' ');
var results = new HashSet<BaseGuild>();
2020-08-25 18:53:35 -07:00
foreach (var g in World.Guilds.Values)
{
var name = g.Name;
bool all = true;
foreach (var t in words)
{
if (name.InsensitiveIndexOf(t) == -1)
2020-09-12 15:31:21 -07:00
{
all = false;
break;
2020-09-12 15:31:21 -07:00
}
2020-08-25 18:53:35 -07:00
}
if (all)
{
results.Add(g);
}
2020-08-25 18:53:35 -07:00
}
return results;
2020-08-25 18:53:35 -07:00
}
public override string ToString() => $"{Serial} \"{Name} [{Abbreviation}]\"";
2020-08-25 18:53:35 -07:00
}