From eb4e3ac0707b5508bea18cf221e919c1c781303d Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sun, 6 Jun 2021 17:03:05 -0700 Subject: [PATCH] fix(core): Fixes various issues with codegen (#646) - [X] Fixes bad `ReadEnum` by size - [X] Fixes array, list, and set not handling null values properly. - It will be up to the user (for now) to null out empty lists using `[AfterDeserialization]`. Convenience may be added later. - [X] Fixes errors with `dotnet clean` and non-empty generation folder - [X] Fixes bad field indexes on `Account.cs` causing `tags` to not be serialized/deserialized. - This was caused by a duplicate entry. Don't have protection against this _yet_. --- .../Rules/ArrayMigrationRule.cs | 12 ++++++++---- .../Rules/HashSetMigrationRule.cs | 11 ++++++++--- .../Rules/ListMigrationRule.cs | 14 ++++++++++---- Projects/Server/Serialization/IGenericReader.cs | 4 ++-- Projects/Server/Server.csproj | 3 ++- Projects/UOContent/Accounting/Account.cs | 12 ++++++------ .../Migrations/Server.Accounting.Account.v2.json | 9 +++++++++ Projects/UOContent/UOContent.csproj | 3 ++- 8 files changed, 47 insertions(+), 21 deletions(-) diff --git a/Projects/SerializationGenerator/SerializableMigration/Rules/ArrayMigrationRule.cs b/Projects/SerializationGenerator/SerializableMigration/Rules/ArrayMigrationRule.cs index adb26d87e..32383c1b8 100644 --- a/Projects/SerializationGenerator/SerializableMigration/Rules/ArrayMigrationRule.cs +++ b/Projects/SerializationGenerator/SerializableMigration/Rules/ArrayMigrationRule.cs @@ -101,14 +101,18 @@ namespace SerializableMigration var arrayElementRuleArguments = new string[ruleArguments.Length - 2]; Array.Copy(ruleArguments, 2, arrayElementRuleArguments, 0, ruleArguments.Length - 2); - var propertyIndex = $"{property.Name}Index"; - source.AppendLine($"{indent}writer.Write({property.Name}.Length);"); - source.AppendLine($"{indent}for (var {propertyIndex} = 0; {propertyIndex} < {property.Name}.Length; {propertyIndex}++)"); + var propertyName = property.Name; + var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}"; + var propertyIndex = $"{propertyVarPrefix}Index"; + var propertyLength = $"{propertyVarPrefix}Length"; + source.AppendLine($"{indent}var {propertyLength} = {property.Name}?.Length ?? 0;"); + source.AppendLine($"{indent}writer.Write({propertyLength});"); + source.AppendLine($"{indent}for (var {propertyIndex} = 0; {propertyIndex} < {propertyLength}; {propertyIndex}++)"); source.AppendLine($"{indent}{{"); var serializableArrayElement = new SerializableProperty { - Name = $"{property.Name}[{propertyIndex}]", + Name = $"{property.Name}![{propertyIndex}]", Type = ruleArguments[0], Rule = arrayElementRule.RuleName, RuleArguments = arrayElementRuleArguments diff --git a/Projects/SerializationGenerator/SerializableMigration/Rules/HashSetMigrationRule.cs b/Projects/SerializationGenerator/SerializableMigration/Rules/HashSetMigrationRule.cs index bf7fe8f99..ee3964a9e 100644 --- a/Projects/SerializationGenerator/SerializableMigration/Rules/HashSetMigrationRule.cs +++ b/Projects/SerializationGenerator/SerializableMigration/Rules/HashSetMigrationRule.cs @@ -116,9 +116,13 @@ namespace SerializableMigration var propertyName = property.Name; var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}"; var propertyEntry = $"{propertyVarPrefix}Entry"; - source.AppendLine($"{indent}writer.Write({property.Name}.Count);"); - source.AppendLine($"{indent}foreach (var {propertyEntry} in {property.Name});"); + var propertyCount = $"{propertyVarPrefix}Count"; + source.AppendLine($"{indent}var {propertyCount} = {property.Name}?.Count ?? 0;"); + source.AppendLine($"{indent}writer.Write({propertyCount});"); + source.AppendLine($"{indent}if ({propertyCount} > 0)"); source.AppendLine($"{indent}{{"); + source.AppendLine($"{indent} foreach (var {propertyEntry} in {property.Name}!)"); + source.AppendLine($"{indent} {{"); var serializableSetElement = new SerializableProperty { @@ -128,8 +132,9 @@ namespace SerializableMigration RuleArguments = setElementRuleArguments }; - setElementRule.GenerateSerializationMethod(source, $"{indent} ", serializableSetElement); + setElementRule.GenerateSerializationMethod(source, $"{indent} ", serializableSetElement); + source.AppendLine($"{indent} }}"); source.AppendLine($"{indent}}}"); } } diff --git a/Projects/SerializationGenerator/SerializableMigration/Rules/ListMigrationRule.cs b/Projects/SerializationGenerator/SerializableMigration/Rules/ListMigrationRule.cs index 57b89add3..e22808f54 100644 --- a/Projects/SerializationGenerator/SerializableMigration/Rules/ListMigrationRule.cs +++ b/Projects/SerializationGenerator/SerializableMigration/Rules/ListMigrationRule.cs @@ -114,10 +114,15 @@ namespace SerializableMigration Array.Copy(ruleArguments, 2, listElementRuleArguments, 0, ruleArguments.Length - 2); var propertyName = property.Name; - var propertyEntry = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}Entry"; - source.AppendLine($"{indent}writer.Write({propertyName}.Count);"); - source.AppendLine($"{indent}foreach (var {propertyEntry} in {propertyName})"); + var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}"; + var propertyEntry = $"{propertyVarPrefix}Entry"; + var propertyCount = $"{propertyVarPrefix}Count"; + source.AppendLine($"{indent}var {propertyCount} = {property.Name}?.Count ?? 0;"); + source.AppendLine($"{indent}writer.Write({propertyCount});"); + source.AppendLine($"{indent}if ({propertyCount} > 0)"); source.AppendLine($"{indent}{{"); + source.AppendLine($"{indent} foreach (var {propertyEntry} in {property.Name}!)"); + source.AppendLine($"{indent} {{"); var serializableListElement = new SerializableProperty { @@ -127,8 +132,9 @@ namespace SerializableMigration RuleArguments = listElementRuleArguments }; - listElementRule.GenerateSerializationMethod(source, $"{indent} ", serializableListElement); + listElementRule.GenerateSerializationMethod(source, $"{indent} ", serializableListElement); + source.AppendLine($"{indent} }}"); source.AppendLine($"{indent}}}"); } } diff --git a/Projects/Server/Serialization/IGenericReader.cs b/Projects/Server/Serialization/IGenericReader.cs index 6bc7a6da1..9f8617c85 100644 --- a/Projects/Server/Serialization/IGenericReader.cs +++ b/Projects/Server/Serialization/IGenericReader.cs @@ -82,12 +82,12 @@ namespace Server var num = ReadShort(); return *(T*)# } - case 3: + case 4: { var num = ReadEncodedInt(); return *(T*)# } - case 4: + case 8: { var num = ReadLong(); return *(T*)# diff --git a/Projects/Server/Server.csproj b/Projects/Server/Server.csproj index fb8e297fa..83ecf639e 100755 --- a/Projects/Server/Server.csproj +++ b/Projects/Server/Server.csproj @@ -34,7 +34,8 @@ - + + diff --git a/Projects/UOContent/Accounting/Account.cs b/Projects/UOContent/Accounting/Account.cs index cf2da5115..578b9b1f5 100644 --- a/Projects/UOContent/Accounting/Account.cs +++ b/Projects/UOContent/Accounting/Account.cs @@ -64,17 +64,17 @@ namespace Server.Accounting [SerializableField(10, setter: "private")] private List _comments; - [SerializableField(10, setter: "private")] + [SerializableField(11, setter: "private")] private List _tags; - [SerializableField(11)] + [SerializableField(12)] private IPAddress[] _loginIPs; /// /// List of IP addresses for restricted access. '*' wildcard supported. If the array contains zero entries, all IP addresses /// are allowed. /// - [SerializableField(12)] + [SerializableField(13)] private string[] _ipRestrictions; private TimeSpan _totalGameTime; @@ -83,7 +83,7 @@ namespace Server.Accounting /// Gets the total game time of this account, also considering the game time of characters /// that have been deleted. /// - [SerializableField(13)] + [SerializableField(14)] public TimeSpan TotalGameTime { get @@ -105,7 +105,7 @@ namespace Server.Accounting } } - [SerializableField(14)] + [SerializableField(15)] [SerializableFieldAttr("[CommandProperty(AccessLevel.Administrator)]")] private string _email; @@ -345,8 +345,8 @@ namespace Server.Accounting _totalGold = reader.ReadInt(); _totalPlat = reader.ReadInt(); - _mobiles = new Mobile[7]; var length = reader.ReadInt(); + _mobiles = new Mobile[length]; for (int i = 0; i < length; i++) { _mobiles[i] = reader.ReadEntity(); diff --git a/Projects/UOContent/Migrations/Server.Accounting.Account.v2.json b/Projects/UOContent/Migrations/Server.Accounting.Account.v2.json index 80f2af2a0..607cb92ca 100644 --- a/Projects/UOContent/Migrations/Server.Accounting.Account.v2.json +++ b/Projects/UOContent/Migrations/Server.Accounting.Account.v2.json @@ -74,6 +74,15 @@ "SerializationMethodSignatureMigrationRule" ] }, + { + "name": "Tags", + "type": "System.Collections.Generic.List\u003CServer.Accounting.AccountTag\u003E", + "rule": "ListMigrationRule", + "ruleArguments": [ + "Server.Accounting.AccountTag", + "SerializationMethodSignatureMigrationRule" + ] + }, { "name": "LoginIPs", "type": "System.Net.IPAddress[]", diff --git a/Projects/UOContent/UOContent.csproj b/Projects/UOContent/UOContent.csproj index 86258b188..d5ca9c443 100755 --- a/Projects/UOContent/UOContent.csproj +++ b/Projects/UOContent/UOContent.csproj @@ -30,7 +30,8 @@ - + +