feat: Adds tidy option for serialization. Codegens ballotbox. Fixes pooled timer leaking (#681)
* Fixes pooled timer leaking * Fixes `[dumptimers` command so it outputs properly, adds spacing, and stacktraces * Adds `[Tidy]` for serializing Lists. This will remove deleted entities during world save before serializing the list. * Adds helpers for managing Lists/Sets/Dictionaries ### New API ```cs // Creates the list if it is null, then adds Utility.Add(ref list, value); Utility.Add(ref set, value); Utility.Add(ref dict, key, value); // Nulls the variable if the count is zero Utility.Remove(ref list, value); Utility.Remove(ref set, value); Utility.Remove(ref dict, key); // Marks entity as dirty in addition to doing the action entity.Add(list, value); // Marks entity as dirty, and will create list if it doesn't exist entity.Add(ref list, value); // Marks entity as dirty in addition to doing the action entity.Remove(list, value); // Marks entity as dirty, and will null the list count is zero entity.Remove(ref list, value); ``` ### Updates to [dumptimers <img width="825" alt="Screen Shot 2021-08-14 at 2 55 10 AM" src="https://user-images.githubusercontent.com/3953314/129442449-ccf7fe14-29d6-4f3f-9366-c8eb7b9828a7.png">
This commit is contained in:
parent
84cbd52a2a
commit
360143478a
53 changed files with 627 additions and 464 deletions
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SerializationGenerator;
|
||||
|
|
@ -52,11 +53,18 @@ namespace SerializableMigration
|
|||
parentSymbol
|
||||
);
|
||||
|
||||
var extraOptions = "";
|
||||
if (attributes.Any(a => a.IsTidy(compilation)))
|
||||
{
|
||||
extraOptions += "@Tidy";
|
||||
}
|
||||
|
||||
var length = serializableSetType.RuleArguments.Length;
|
||||
ruleArguments = new string[length + 2];
|
||||
ruleArguments[0] = setTypeSymbol.ToDisplayString();
|
||||
ruleArguments[1] = serializableSetType.Rule;
|
||||
Array.Copy(serializableSetType.RuleArguments, 0, ruleArguments, 2, length);
|
||||
ruleArguments = new string[length + 3];
|
||||
ruleArguments[0] = extraOptions;
|
||||
ruleArguments[1] = setTypeSymbol.ToDisplayString();
|
||||
ruleArguments[2] = serializableSetType.Rule;
|
||||
Array.Copy(serializableSetType.RuleArguments, 0, ruleArguments, 3, length);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -71,9 +79,12 @@ namespace SerializableMigration
|
|||
}
|
||||
|
||||
var ruleArguments = property.RuleArguments;
|
||||
var setElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
|
||||
var setElementRuleArguments = new string[ruleArguments.Length - 2];
|
||||
Array.Copy(ruleArguments, 2, setElementRuleArguments, 0, ruleArguments.Length - 2);
|
||||
var hasExtraOptions = ruleArguments[0] == "" || ruleArguments[0].StartsWith("@", StringComparison.Ordinal);
|
||||
var argumentsOffset = hasExtraOptions ? 1 : 0;
|
||||
|
||||
var setElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1 + argumentsOffset]];
|
||||
var setElementRuleArguments = new string[ruleArguments.Length - 2 - argumentsOffset];
|
||||
Array.Copy(ruleArguments, 2 + argumentsOffset, setElementRuleArguments, 0, ruleArguments.Length - 2 - argumentsOffset);
|
||||
|
||||
var propertyName = property.Name;
|
||||
var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}";
|
||||
|
|
@ -81,16 +92,16 @@ namespace SerializableMigration
|
|||
var propertyEntry = $"{propertyVarPrefix}Entry";
|
||||
var propertyCount = $"{propertyVarPrefix}Count";
|
||||
|
||||
source.AppendLine($"{indent}{ruleArguments[0]} {propertyEntry};");
|
||||
source.AppendLine($"{indent}{ruleArguments[argumentsOffset]} {propertyEntry};");
|
||||
source.AppendLine($"{indent}var {propertyCount} = reader.ReadInt();");
|
||||
source.AppendLine($"{indent}{property.Name} = new System.Collections.Generic.HashSet<{ruleArguments[0]}>({propertyCount});");
|
||||
source.AppendLine($"{indent}{property.Name} = new System.Collections.Generic.HashSet<{ruleArguments[argumentsOffset]}>({propertyCount});");
|
||||
source.AppendLine($"{indent}for (var {propertyIndex} = 0; i < {propertyCount}; {propertyIndex}++)");
|
||||
source.AppendLine($"{indent}{{");
|
||||
|
||||
var serializableSetElement = new SerializableProperty
|
||||
{
|
||||
Name = propertyEntry,
|
||||
Type = ruleArguments[0],
|
||||
Type = ruleArguments[argumentsOffset],
|
||||
Rule = setElementRule.RuleName,
|
||||
RuleArguments = setElementRuleArguments
|
||||
};
|
||||
|
|
@ -111,14 +122,23 @@ namespace SerializableMigration
|
|||
}
|
||||
|
||||
var ruleArguments = property.RuleArguments;
|
||||
var setElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1]];
|
||||
var setElementRuleArguments = new string[ruleArguments.Length - 2];
|
||||
Array.Copy(ruleArguments, 2, setElementRuleArguments, 0, ruleArguments.Length - 2);
|
||||
var hasExtraOptions = ruleArguments[0] == "" || ruleArguments[0].StartsWith("@", StringComparison.Ordinal);
|
||||
var shouldTidy = hasExtraOptions && ruleArguments[0].Contains("@Tidy");
|
||||
var argumentsOffset = hasExtraOptions ? 1 : 0;
|
||||
|
||||
var setElementRule = SerializableMigrationRulesEngine.Rules[ruleArguments[1 + argumentsOffset]];
|
||||
var setElementRuleArguments = new string[ruleArguments.Length - 2 - argumentsOffset];
|
||||
Array.Copy(ruleArguments, 2 + argumentsOffset, setElementRuleArguments, 0, ruleArguments.Length - 2 - argumentsOffset);
|
||||
|
||||
var propertyName = property.Name;
|
||||
var propertyVarPrefix = $"{char.ToLower(propertyName[0])}{propertyName.Substring(1, propertyName.Length - 1)}";
|
||||
var propertyEntry = $"{propertyVarPrefix}Entry";
|
||||
var propertyCount = $"{propertyVarPrefix}Count";
|
||||
|
||||
if (shouldTidy)
|
||||
{
|
||||
source.AppendLine($"{indent}{property.Name}?.Tidy();");
|
||||
}
|
||||
source.AppendLine($"{indent}var {propertyCount} = {property.Name}?.Count ?? 0;");
|
||||
source.AppendLine($"{indent}writer.Write({propertyCount});");
|
||||
source.AppendLine($"{indent}if ({propertyCount} > 0)");
|
||||
|
|
@ -129,7 +149,7 @@ namespace SerializableMigration
|
|||
var serializableSetElement = new SerializableProperty
|
||||
{
|
||||
Name = propertyEntry,
|
||||
Type = ruleArguments[0],
|
||||
Type = ruleArguments[argumentsOffset],
|
||||
Rule = setElementRule.RuleName,
|
||||
RuleArguments = setElementRuleArguments
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue