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
|
|
@ -1492,5 +1492,159 @@ namespace Server
|
|||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static string GetTimeStamp() => Core.Now.ToString("yyyy-MM-dd-HH-mm-ss");
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Add<T>(ref List<T> list, T value)
|
||||
{
|
||||
list ??= new List<T>();
|
||||
list.Add(value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Add<T>(ref HashSet<T> set, T value)
|
||||
{
|
||||
set ??= new HashSet<T>();
|
||||
set.Add(value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Add<K, V>(ref Dictionary<K, V> dict, K key, V value)
|
||||
{
|
||||
dict ??= new Dictionary<K, V>();
|
||||
dict.Add(key, value);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Remove<T>(ref List<T> list, T value)
|
||||
{
|
||||
if (list != null)
|
||||
{
|
||||
var removed = list.Remove(value);
|
||||
|
||||
if (list.Count == 0)
|
||||
{
|
||||
list = null;
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Remove<T>(ref HashSet<T> set, T value)
|
||||
{
|
||||
if (set != null)
|
||||
{
|
||||
var removed = set.Remove(value);
|
||||
|
||||
if (set.Count == 0)
|
||||
{
|
||||
set = null;
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Remove<K, V>(ref Dictionary<K, V> dict, K key)
|
||||
{
|
||||
if (dict != null)
|
||||
{
|
||||
var removed = dict.Remove(key);
|
||||
|
||||
if (dict.Count == 0)
|
||||
{
|
||||
dict = null;
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Remove<K, V>(ref Dictionary<K, V> dict, K key, out V value)
|
||||
{
|
||||
if (dict != null)
|
||||
{
|
||||
var removed = dict.Remove(key, out value);
|
||||
|
||||
if (dict.Count == 0)
|
||||
{
|
||||
dict = null;
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Replace<T>(ref List<T> list, T oldValue, T newValue)
|
||||
{
|
||||
if (oldValue != null && newValue != null)
|
||||
{
|
||||
var index = list?.IndexOf(oldValue) ?? -1;
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
list![index] = newValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
Add(ref list, newValue);
|
||||
}
|
||||
}
|
||||
else if (oldValue != null)
|
||||
{
|
||||
Remove(ref list, oldValue);
|
||||
}
|
||||
else if (newValue != null)
|
||||
{
|
||||
Add(ref list, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Replace<K, V>(ref Dictionary<K, V> dict, K key, V oldValue, V newValue)
|
||||
{
|
||||
if (newValue != null)
|
||||
{
|
||||
Add(ref dict, key, newValue);
|
||||
}
|
||||
else if (oldValue != null)
|
||||
{
|
||||
Remove(ref dict, key);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Clear<T>(ref List<T> list)
|
||||
{
|
||||
list.Clear();
|
||||
list = null;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Clear<T>(ref HashSet<T> set)
|
||||
{
|
||||
set.Clear();
|
||||
set = null;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Clear<K, V>(ref Dictionary<K, V> dict)
|
||||
{
|
||||
dict.Clear();
|
||||
dict = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue