mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
feature: Weaver adds 'bool Weaved()' to each NetworkBehaviour, which can be checked at runtime #3523
This commit is contained in:
parent
1d7af1b750
commit
d5664ab202
@ -1351,5 +1351,10 @@ public virtual void OnStartAuthority() {}
|
|||||||
|
|
||||||
/// <summary>Stop event, only called for objects the client has authority over.</summary>
|
/// <summary>Stop event, only called for objects the client has authority over.</summary>
|
||||||
public virtual void OnStopAuthority() {}
|
public virtual void OnStopAuthority() {}
|
||||||
|
|
||||||
|
// Weaver injects this into inheriting classes to return true.
|
||||||
|
// allows runtime & tests to check if a type was weaved.
|
||||||
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||||
|
public virtual bool Weaved() => false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,20 +205,29 @@ public static bool WriteArguments(ILProcessor worker, Writers writers, Logger Lo
|
|||||||
}
|
}
|
||||||
|
|
||||||
#region mark / check type as processed
|
#region mark / check type as processed
|
||||||
public const string ProcessedFunctionName = "MirrorProcessed";
|
public const string ProcessedFunctionName = "Weaved";
|
||||||
|
|
||||||
// by adding an empty MirrorProcessed() function
|
// check if the type has a "Weaved" function already
|
||||||
public static bool WasProcessed(TypeDefinition td)
|
public static bool WasProcessed(TypeDefinition td)
|
||||||
{
|
{
|
||||||
return td.GetMethod(ProcessedFunctionName) != null;
|
return td.GetMethod(ProcessedFunctionName) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add the Weaved() function which returns true.
|
||||||
|
// can be called at runtime and from tests to check if weaving succeeded.
|
||||||
public void MarkAsProcessed(TypeDefinition td)
|
public void MarkAsProcessed(TypeDefinition td)
|
||||||
{
|
{
|
||||||
if (!WasProcessed(td))
|
if (!WasProcessed(td))
|
||||||
{
|
{
|
||||||
MethodDefinition versionMethod = new MethodDefinition(ProcessedFunctionName, MethodAttributes.Private, weaverTypes.Import(typeof(void)));
|
// add a function:
|
||||||
|
// public override bool MirrorProcessed() { return true; }
|
||||||
|
// ReuseSlot means 'override'.
|
||||||
|
MethodDefinition versionMethod = new MethodDefinition(
|
||||||
|
ProcessedFunctionName,
|
||||||
|
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.ReuseSlot,
|
||||||
|
weaverTypes.Import(typeof(bool)));
|
||||||
ILProcessor worker = versionMethod.Body.GetILProcessor();
|
ILProcessor worker = versionMethod.Body.GetILProcessor();
|
||||||
|
worker.Emit(OpCodes.Ldc_I4_1);
|
||||||
worker.Emit(OpCodes.Ret);
|
worker.Emit(OpCodes.Ret);
|
||||||
td.Methods.Add(versionMethod);
|
td.Methods.Add(versionMethod);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user