-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NPE was caused by some events coming from non KMonoBehaviour - StateMachine.Instance. Add reference mechanism for the type as well.
- Loading branch information
Showing
10 changed files
with
124 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
MultiplayerMod/Game/Mechanics/Objects/StateMachineEventsArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using MultiplayerMod.Multiplayer.Objects.Reference; | ||
|
||
namespace MultiplayerMod.Game.Mechanics.Objects; | ||
|
||
public record StateMachineEventsArgs( | ||
StateMachineReference Target, | ||
Type MethodType, | ||
string MethodName, | ||
object[] Args | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
MultiplayerMod/Multiplayer/Objects/Reference/StateMachineReference.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
|
||
namespace MultiplayerMod.Multiplayer.Objects.Reference; | ||
|
||
[Serializable] | ||
public class StateMachineReference { | ||
|
||
private ComponentReference<StateMachineController> ControllerReference { get; set; } | ||
private Type StateMachineType { get; set; } | ||
|
||
public StateMachineReference( | ||
ComponentReference<StateMachineController> controllerReference, | ||
Type stateMachineType | ||
) { | ||
ControllerReference = controllerReference; | ||
StateMachineType = stateMachineType; | ||
} | ||
|
||
public StateMachine.Instance? GetInstance() => ControllerReference.GetComponent().GetSMI(StateMachineType); | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
MultiplayerMod/Multiplayer/Objects/StateMachineReferenceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Reflection; | ||
using MultiplayerMod.Multiplayer.Objects.Reference; | ||
|
||
namespace MultiplayerMod.Multiplayer.Objects; | ||
|
||
public static class StateMachineReferenceExtensions { | ||
|
||
public static StateMachineReference GetReference(this StateMachine.Instance instance) => new( | ||
GetStateMachineController(instance).GetReference(), | ||
instance.GetType() | ||
); | ||
|
||
// `controller` field is defined in StateMachine<,,,>.GenericInstance. However cast is impossible due to unknown | ||
// generic argument types. So reflection is the most handy way to get its value :( | ||
private static StateMachineController GetStateMachineController(StateMachine.Instance instance) => | ||
(StateMachineController) instance.GetType() | ||
.GetField("controller", BindingFlags.Instance | BindingFlags.NonPublic)! | ||
.GetValue(instance); | ||
} |