Skip to content

Commit

Permalink
fix: Fixes serialization and dirty tracking conveniences (#1627)
Browse files Browse the repository at this point in the history
### Summary
- Fixes issues with non-_ISerializable_ objects having bad serialization (Skill/Stat. Mods)
- Fixes issues with MarkDirty and namespaces: modernuo/SerializationGenerator#29
- Fixes missing dirty tracking for some data structures.
- Fixes cascading MarkDirty for non-serializable types that have owners that are also non-serializable types.

### New Codegenned API
When a data structure (Array, List, Dictionary, HashSet, etc) is source generated for serialization, new methods are added which will handle dirty tracking. Use these instead of the built in methods for that data structure.

_All Data Structures_
```cs
public void Clear<PropertyName>();
```

_Lists and Sets_
```cs
public void AddTo<PropertyName>(V value);
public void RemoveFrom<PropertyName>(V value);
```

_Lists_
```cs
public void InsertInto<PropertyName>(int index, V value);
public void RemoveFrom<PropertyName>At(int index);
```

_Dictionaries_
```cs
public void AddTo<PropertyName>(T key, V value);
public void RemoveFrom<PropertyName>(T key);
public void ReplaceIn<PropertyName>(T key, V value);
```

Over time, they will be cleaned up and more variants added such as `bool RemoveFrom<PropertyName>(T key, out V value)`
  • Loading branch information
kamronbatman authored Dec 3, 2023
1 parent 3a0d4b1 commit b7882df
Show file tree
Hide file tree
Showing 25 changed files with 64 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"modernuoschemagenerator": {
"version": "2.9.0",
"version": "2.10.8",
"commands": [
"ModernUOSchemaGenerator"
]
Expand Down
2 changes: 1 addition & 1 deletion Projects/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageReference Include="Zlib.Bindings" Version="1.11.0" />

<PackageReference Include="ModernUO.Serialization.Annotations" Version="2.9.1" />
<PackageReference Include="ModernUO.Serialization.Generator" Version="2.9.2" />
<PackageReference Include="ModernUO.Serialization.Generator" Version="2.10.8" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="Migrations/*.v*.json" />
Expand Down
4 changes: 2 additions & 2 deletions Projects/UOContent/Engines/Bulk Orders/Books/BulkOrderBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ public override int GetTotal(TotalType type)

public void AddEntry(IBOBEntry entry)
{
this.Add(Entries, entry);
AddToEntries(entry);
InvalidateProperties();
}

public void RemoveEntry(IBOBEntry entry)
{
this.Remove(Entries, entry);
RemoveFromEntries(entry);
InvalidateProperties();
}

Expand Down
12 changes: 6 additions & 6 deletions Projects/UOContent/Engines/CannedEvil/ChampionSpawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public void OnSlice()
AwardArtifact((Champion as BaseChampion)?.GetArtifact());
}

this.Clear(_damageEntries);
ClearDamageEntries();

if (_platform != null)
{
Expand Down Expand Up @@ -570,7 +570,7 @@ public void OnSlice()
((Corpse)m.Corpse).BeginDecay(TimeSpan.FromMinutes(1));
}

this.RemoveAt(_creatures, i);
RemoveFromCreaturesAt(i);
--i;
++_kills;

Expand Down Expand Up @@ -776,7 +776,7 @@ public void Respawn()
// Allow creatures to turn into Paragons at Ilshenar champions.
m.OnBeforeSpawn(loc, Map);

this.Add(_creatures, m);
AddToCreatures(m);
m.MoveToWorld(loc, Map);

if (m is BaseCreature bc)
Expand Down Expand Up @@ -1114,7 +1114,7 @@ public override void OnAfterDelete()
_redSkulls[i].Delete();
}

this.Clear(_redSkulls);
ClearRedSkulls();
}

if (_whiteSkulls != null)
Expand All @@ -1124,7 +1124,7 @@ public override void OnAfterDelete()
_whiteSkulls[i].Delete();
}

this.Clear(_whiteSkulls);
ClearWhiteSkulls();
}

DeleteCreatures();
Expand Down Expand Up @@ -1161,7 +1161,7 @@ public void DeleteCreatures()
}
}

this.Clear(_creatures);
ClearCreatures();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Projects/UOContent/Engines/ConPVP/Games/BombingRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ private void Remake()
ac.Delete();
}

this.Clear(Components);
ClearComponents();

// stairs
AddComponent(new AddonComponent(0x74D), -1, +1, -5);
Expand Down
12 changes: 6 additions & 6 deletions Projects/UOContent/Engines/Doom/GauntletSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ public virtual int ComputeTrapCount()
return area / 100;
}

public virtual void ClearTraps()
public virtual void RemoveAllTraps()
{
for (var i = 0; i < Traps.Count; ++i)
{
Traps[i].Delete();
}

Traps.Clear();
ClearTraps();
}

public virtual void SpawnTrap()
Expand Down Expand Up @@ -303,19 +303,19 @@ public virtual int ComputeSpawnCount()
return Math.Max((playerCount + PlayersPerSpawn - 1) / PlayersPerSpawn, 1);
}

public virtual void ClearCreatures()
public virtual void RemoveAllCreatures()
{
for (var i = 0; i < Creatures.Count; ++i)
{
Creatures[i].Delete();
}

Creatures.Clear();
ClearCreatures();
}

public virtual void FullSpawn()
{
ClearCreatures();
RemoveAllCreatures();

var count = ComputeSpawnCount();

Expand All @@ -324,7 +324,7 @@ public virtual void FullSpawn()
Spawn();
}

ClearTraps();
RemoveAllTraps();

count = ComputeTrapCount();

Expand Down
19 changes: 5 additions & 14 deletions Projects/UOContent/Items/Addons/BallotBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ public BallotBox() : base(0x9A8)
[SerializableField(2, setter: "private")]
private List<Mobile> _no;

public void ClearTopic()
{
Topic = Array.Empty<string>();

ClearVotes();
}

public void AddLineToTopic(string line)
{
if (Topic.Length >= MaxTopicLines)
Expand All @@ -59,11 +52,8 @@ public void AddLineToTopic(string line)

public void ClearVotes()
{
if (Yes.Count > 0 || No.Count > 0)
{
this.Clear(_yes);
this.Clear(_no);
}
ClearYes();
ClearNo();
}

public bool IsOwner(Mobile from)
Expand Down Expand Up @@ -197,6 +187,7 @@ public override void OnResponse(NetState sender, RelayInfo info)
if (isOwner)
{
m_Box.ClearTopic();
m_Box.ClearVotes();

from.SendLocalizedMessage(
500370,
Expand Down Expand Up @@ -228,7 +219,7 @@ public override void OnResponse(NetState sender, RelayInfo info)
}
else
{
m_Box.Add(m_Box._yes, from);
m_Box.AddToYes(from);
from.SendLocalizedMessage(500373); // Your vote has been registered.
}
}
Expand All @@ -245,7 +236,7 @@ public override void OnResponse(NetState sender, RelayInfo info)
}
else
{
m_Box.Add(m_Box._no, from);
m_Box.AddToNo(from);
from.SendLocalizedMessage(500373); // Your vote has been registered.
}
}
Expand Down
2 changes: 1 addition & 1 deletion Projects/UOContent/Items/Addons/BaseAddon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void AddComponent(AddonComponent c, int x, int y, int z)
return;
}

this.Add(Components, c);
AddToComponents(c);

c.Addon = this;
c.Offset = new Point3D(x, y, z);
Expand Down
2 changes: 1 addition & 1 deletion Projects/UOContent/Items/Addons/BaseAddonContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public void AddComponent(AddonContainerComponent c, int x, int y, int z)
return;
}

this.Add(Components, c);
AddToComponents(c);

c.Addon = this;
c.Offset = new Point3D(x, y, z);
Expand Down
4 changes: 2 additions & 2 deletions Projects/UOContent/Items/Addons/SolenAntHole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void SpawnAnt()

public void SpawnAnt(BaseCreature ant)
{
this.Add(_spawned, ant);
AddToSpawned(ant);

var map = Map;
var p = Location;
Expand All @@ -143,7 +143,7 @@ public bool SpawnKilled()
{
if (!_spawned[i].Alive || _spawned[i].Deleted)
{
this.RemoveAt(_spawned, i);
RemoveFromSpawnedAt(i);
}
}

Expand Down
16 changes: 8 additions & 8 deletions Projects/UOContent/Items/Aquarium/Aquarium.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public virtual void KillFish(int amount)
LiveCreatures = Math.Max(LiveCreatures - 1, 0);

// An unfortunate accident has left a creature floating upside-down. It is starting to smell.
this.Add(_events, 1074366);
AddToEvents(1074366);
}
}

Expand All @@ -563,34 +563,34 @@ public virtual void Evaluate()
else if (m_EvaluateDay)
{
// reset events
this.Clear(_events);
ClearEvents();

// food events
if (
_food.Added < _food.Maintain && _food.State != (int)FoodState.Overfed &&
_food.State != (int)FoodState.Dead ||
_food.Added >= _food.Improve && _food.State == (int)FoodState.Full)
{
this.Add(_events, 1074368); // The tank looks worse than it did yesterday.
AddToEvents(1074368); // The tank looks worse than it did yesterday.
}

if (
_food.Added >= _food.Improve && _food.State != (int)FoodState.Full &&
_food.State != (int)FoodState.Overfed ||
_food.Added < _food.Maintain && _food.State == (int)FoodState.Overfed)
{
this.Add(_events, 1074367); // The tank looks healthier today.
AddToEvents(1074367); // The tank looks healthier today.
}

// water events
if (_water.Added < _water.Maintain && _water.State != (int)WaterState.Dead)
{
this.Add(_events, 1074370); // This tank can use more water.
AddToEvents(1074370); // This tank can use more water.
}

if (_water.Added >= _water.Improve && _water.State != (int)WaterState.Strong)
{
this.Add(_events, 1074369); // The water looks clearer today.
AddToEvents(1074369); // The water looks clearer today.
}

UpdateFoodState();
Expand Down Expand Up @@ -663,7 +663,7 @@ public virtual void Evaluate()

if (AddFish(fish))
{
this.Add(_events, message);
AddToEvents(message);
}
else
{
Expand Down Expand Up @@ -1017,7 +1017,7 @@ public override void OnClick()
Owner.From.PlaySound(0x5A2);
}

m_Aquarium.RemoveAt(m_Aquarium._events, 0);
m_Aquarium.RemoveFromEventsAt(0);
m_Aquarium.InvalidateProperties();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Projects/UOContent/Items/Guilds/Guildstone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public override void OnDoubleClick(Mobile from)
targetState.Leaving = guildState.Leaving;
}

_guild.Remove(_guild.Accepted, from);
_guild.Accepted.Remove(from);
_guild.AddMember(from);

GuildGump.EnsureClosed(from);
Expand Down Expand Up @@ -305,7 +305,7 @@ public void Placement_OnTarget(Mobile from, object targeted)

addon.MoveToWorld(loc, from.Map);

house.Add(house.Addons, addon);
house.Addons.Add(addon);
Delete();
}
else
Expand Down
5 changes: 0 additions & 5 deletions Projects/UOContent/Items/Maps/MapItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,6 @@ public virtual void ChangePin(int index, int x, int y)
}
}

public virtual void ClearPins()
{
Pins.Clear();
}

private void Deserialize(IGenericReader reader, int version)
{
// Version 0 doesn't serialize Facet/Editable, and count is not encoded
Expand Down
4 changes: 2 additions & 2 deletions Projects/UOContent/Items/Misc/CommunicationCrystals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ public override void OnSpeech(SpeechEventArgs e)

public void AddReceiver(ReceiverCrystal receiver)
{
this.Add(Receivers, receiver);
AddToReceivers(receiver);
InvalidateProperties();
}

public void RemoveReceiver(ReceiverCrystal receiver)
{
this.Remove(Receivers, receiver);
RemoveFromReceivers(receiver);
InvalidateProperties();
}

Expand Down
4 changes: 2 additions & 2 deletions Projects/UOContent/Items/Misc/Corpses/Corpse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ public override void OnItemUsed(Mobile from, Item item)
from.CriminalAction(true);
}

this.Add(_looters, from);
AddToLooters(from);

_instancedItems?.Remove(item);

Expand All @@ -695,7 +695,7 @@ public override void OnItemLifted(Mobile from, Item item)
from.CriminalAction(true);
}

this.Add(_looters, from);
AddToLooters(from);

_instancedItems?.Remove(item);
}
Expand Down
4 changes: 2 additions & 2 deletions Projects/UOContent/Items/Misc/FlippableAddonAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private void ClearComponents(Item item)
c.Delete();
}

addon.Clear(addon.Components);
addon.ClearComponents();
}
else if (item is BaseAddonContainer addonContainer)
{
Expand All @@ -134,7 +134,7 @@ private void ClearComponents(Item item)
c.Delete();
}

addonContainer.Clear(addonContainer.Components);
addonContainer.ClearComponents();
}
}
}
Loading

0 comments on commit b7882df

Please sign in to comment.