Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions Source/Common/Syncing/Worker/SyncWorkerEntry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Multiplayer.API;
Expand All @@ -12,6 +13,7 @@
delegate void SyncWorkerDelegateNoReturn(SyncWorker sync, ref object? obj);

public Type type;
public MethodInfo addGenericMethod;
public bool shouldConstruct;
private List<SyncWorkerDelegate> syncWorkers;
private List<SyncWorkerEntry> subclasses = [];
Expand All @@ -19,16 +21,25 @@

public int SyncWorkerCount => syncWorkers.Count;

public SyncWorkerEntry(Type type, bool shouldConstruct = false)

Check warning on line 24 in Source/Common/Syncing/Worker/SyncWorkerEntry.cs

View workflow job for this annotation

GitHub Actions / Builds

Non-nullable field 'addGenericMethod' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 24 in Source/Common/Syncing/Worker/SyncWorkerEntry.cs

View workflow job for this annotation

GitHub Actions / Builds

Non-nullable field 'addGenericMethod' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 24 in Source/Common/Syncing/Worker/SyncWorkerEntry.cs

View workflow job for this annotation

GitHub Actions / Builds

Non-nullable field 'addGenericMethod' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
this.type = type;
syncWorkers = new List<SyncWorkerDelegate>();
this.shouldConstruct = shouldConstruct;

if (type != typeof(object)) {
var delegateType = typeof(SyncWorkerDelegate<>).MakeGenericType(type);
addGenericMethod = typeof(SyncWorkerEntry)
.GetMethods()
.First(m => m.Name == nameof(Add) && m.IsGenericMethodDefinition)
.MakeGenericMethod(type);
}
}

public SyncWorkerEntry(SyncWorkerEntry other)
{
type = other.type;
addGenericMethod = other.addGenericMethod;
syncWorkers = other.syncWorkers;
subclasses = other.subclasses;
shouldConstruct = other.shouldConstruct;
Expand All @@ -41,12 +52,21 @@
{
if (method.ReturnType == typeof(void))
{
var func = (SyncWorkerDelegateNoReturn)Delegate.CreateDelegate(typeof(SyncWorkerDelegateNoReturn), method);
Add((SyncWorker sync, ref object obj) =>
if (type == typeof(object)) {
SyncWorkerDelegateNoReturn func = (SyncWorkerDelegateNoReturn)Delegate.CreateDelegate(typeof(SyncWorkerDelegateNoReturn), method);
Add((SyncWorker sync, ref object? obj) =>
{
func(sync, ref obj);
return true;
}, true);
return;
}
else
{
func(sync, ref obj);
return true;
}, true);
var delegateType = typeof(SyncWorkerDelegate<>).MakeGenericType(this.type);
var func = Delegate.CreateDelegate(delegateType, method);
typeof(SyncWorkerEntry).GetMethods().First(m => m.Name == nameof(Add) && m.IsGenericMethod).MakeGenericMethod(type).Invoke(this, [func]);
}
}
else
{
Expand All @@ -58,7 +78,7 @@
{
Add((SyncWorker sync, ref object? obj) => {
var obj2 = (T?) obj;
func(sync, ref obj2);

Check warning on line 81 in Source/Common/Syncing/Worker/SyncWorkerEntry.cs

View workflow job for this annotation

GitHub Actions / Builds

Possible null reference assignment.

Check warning on line 81 in Source/Common/Syncing/Worker/SyncWorkerEntry.cs

View workflow job for this annotation

GitHub Actions / Builds

Possible null reference assignment.

Check warning on line 81 in Source/Common/Syncing/Worker/SyncWorkerEntry.cs

View workflow job for this annotation

GitHub Actions / Builds

Possible null reference assignment.
obj = obj2;
return true;
});
Expand Down Expand Up @@ -92,8 +112,8 @@

public void Add(SyncWorkerEntry other)
{
SyncWorkerEntry newEntry = Add(other.type, other, other.shouldConstruct);

Check warning on line 115 in Source/Common/Syncing/Worker/SyncWorkerEntry.cs

View workflow job for this annotation

GitHub Actions / Builds

Converting null literal or possible null value to non-nullable type.
newEntry.subclasses = other.subclasses;

Check warning on line 116 in Source/Common/Syncing/Worker/SyncWorkerEntry.cs

View workflow job for this annotation

GitHub Actions / Builds

Dereference of a possibly null reference.
}

public SyncWorkerEntry? Add(Type toAddType, bool toAddConstruct = false)
Expand Down
Loading