Skip to content

Commit

Permalink
#13 Turn on creation sync for dynamic chores.
Browse files Browse the repository at this point in the history
This PR is a part of enabling sync of dynamic chores.
  • Loading branch information
zuev93 committed Dec 7, 2023
1 parent 78e3c37 commit 40189af
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 99 deletions.
3 changes: 3 additions & 0 deletions src/MultiplayerMod.Test/AbstractGameTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ private static void SetUpUnityAndGame() {

StateMachineDebuggerSettings._Instance = new StateMachineDebuggerSettings();
StateMachineDebuggerSettings._Instance.Initialize();

MinionGroupProber.Instance = worldGameObject.AddComponent<MinionGroupProber>();
MinionGroupProber.Instance.OnPrefabInit();
}

private static void InitGame(GameObject worldGameObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ApplicationPatch {

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(Application), "get_isPlaying")]
[HarmonyPatch("get_isPlaying")]
private static IEnumerable<CodeInstruction> Application_get_isPlaying(IEnumerable<CodeInstruction> instructions) {
return new List<CodeInstruction> {
new(OpCodes.Ldc_I4_1), // true
Expand All @@ -22,8 +22,10 @@ private static IEnumerable<CodeInstruction> Application_get_isPlaying(IEnumerabl

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(Application), "get_streamingAssetsPath")]
private static IEnumerable<CodeInstruction> Application_get_streamingAssetsPath(IEnumerable<CodeInstruction> instructions) {
[HarmonyPatch("get_streamingAssetsPath")]
private static IEnumerable<CodeInstruction> Application_get_streamingAssetsPath(
IEnumerable<CodeInstruction> instructions
) {
return new List<CodeInstruction> {
new(OpCodes.Ldstr, ""), // ""
new(OpCodes.Ret)
Expand All @@ -32,8 +34,10 @@ private static IEnumerable<CodeInstruction> Application_get_streamingAssetsPath(

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(Application), "get_persistentDataPath")]
private static IEnumerable<CodeInstruction> Application_persistentDataPath(IEnumerable<CodeInstruction> instructions) {
[HarmonyPatch("get_persistentDataPath")]
private static IEnumerable<CodeInstruction> Application_persistentDataPath(
IEnumerable<CodeInstruction> instructions
) {
return new List<CodeInstruction> {
new(OpCodes.Ldstr, ""), // ""
new(OpCodes.Ret)
Expand All @@ -42,7 +46,7 @@ private static IEnumerable<CodeInstruction> Application_persistentDataPath(IEnum

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(Application), "get_consoleLogPath")]
[HarmonyPatch("get_consoleLogPath")]
private static IEnumerable<CodeInstruction> Application_consoleLogPath(IEnumerable<CodeInstruction> instructions) {
return new List<CodeInstruction> {
new(OpCodes.Ldstr, ""), // ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ComponentPatch {

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(Component), "get_gameObject")]
[HarmonyPatch("get_gameObject")]
private static IEnumerable<CodeInstruction> Component_get_gameObject(IEnumerable<CodeInstruction> instructions) {
return new List<CodeInstruction> {
new(OpCodes.Ldarg_0), // this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,22 @@ private static IEnumerable<CodeInstruction> GameObject_GetComponent(IEnumerable<

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(GameObject), "get_transform")]
[HarmonyPatch("GetComponentInChildren", typeof(Type), typeof(bool))]
private static IEnumerable<CodeInstruction> GameObject_GetComponentInChildren(
IEnumerable<CodeInstruction> instructions
) {
return new List<CodeInstruction> {
new(OpCodes.Ldarg_0), // this
new(OpCodes.Ldarg_1), // type
new(OpCodes.Ldarg_2), // includeInactive
CodeInstruction.Call(typeof(UnityTestRuntime), nameof(UnityTestRuntime.GetComponentInChildren)),
new(OpCodes.Ret)
};
}

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch("get_transform")]
private static IEnumerable<CodeInstruction> GameObject_get_transform(IEnumerable<CodeInstruction> instructions) {
return new List<CodeInstruction> {
new(OpCodes.Ldarg_0), // this
Expand All @@ -88,19 +103,6 @@ IEnumerable<CodeInstruction> instructions
};
}

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(Time), "get_frameCount")]
private static IEnumerable<CodeInstruction> Time_get_frameCount(IEnumerable<CodeInstruction> instructions) {
return new List<CodeInstruction> {
new(
OpCodes.Call,
AccessTools.PropertyGetter(typeof(UnityTestRuntime), nameof(UnityTestRuntime.FrameCount))
),
new(OpCodes.Ret)
};
}

public static Transform GetTransform(GameObject gameObject) {
return gameObject.GetComponent<Transform>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Reflection.Emit;
using HarmonyLib;
using JetBrains.Annotations;
using UnityEngine;

namespace MultiplayerMod.Test.Environment.Unity.Patches.Unity;

[UsedImplicitly]
[HarmonyPatch(typeof(Material))]
public class MaterialPatch {

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch("CreateWithString")]
private static IEnumerable<CodeInstruction> Material_CreateWithString(IEnumerable<CodeInstruction> instructions) {
return new List<CodeInstruction> {
new(OpCodes.Ret)
};
}

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch("GetFirstPropertyNameIdByAttribute")]
private static IEnumerable<CodeInstruction> Material_GetFirstPropertyNameIdByAttribute(
IEnumerable<CodeInstruction> instructions
) {
return new List<CodeInstruction> {
new(OpCodes.Ldc_I4_0),
new(OpCodes.Ret)
};
}

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch("SetColorImpl")]
private static IEnumerable<CodeInstruction> Material_SetColorImpl(IEnumerable<CodeInstruction> instructions) {
return new List<CodeInstruction> {
new(OpCodes.Ret)
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Reflection.Emit;
using HarmonyLib;
using JetBrains.Annotations;
using UnityEngine;

namespace MultiplayerMod.Test.Environment.Unity.Patches.Unity;

[UsedImplicitly]
[HarmonyPatch(typeof(Renderer))]
public class RendererPatch {

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch("get_material")]
private static IEnumerable<CodeInstruction> Renderer_get_material(IEnumerable<CodeInstruction> instructions) {
return new List<CodeInstruction> {
new(OpCodes.Ldarg_0), // this
CodeInstruction.Call(typeof(RendererPatch), nameof(CreateMaterial)),
new(OpCodes.Ret)
};
}

public static Material CreateMaterial(Renderer _) {
return new Material("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TimePatch {

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(Time), "get_frameCount")]
[HarmonyPatch("get_frameCount")]
private static IEnumerable<CodeInstruction> Time_get_frameCount(IEnumerable<CodeInstruction> instructions) {
return new List<CodeInstruction> {
new(
Expand All @@ -22,5 +22,4 @@ private static IEnumerable<CodeInstruction> Time_get_frameCount(IEnumerable<Code
new(OpCodes.Ret)
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TransformPatch {

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(Transform), "get_position_Injected")]
[HarmonyPatch("get_position_Injected")]
private static IEnumerable<CodeInstruction> Transform_get_position_Injected(
IEnumerable<CodeInstruction> instructions
) {
Expand Down
3 changes: 3 additions & 0 deletions src/MultiplayerMod.Test/Environment/Unity/UnityTestRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public static Component GetComponent(GameObject gameObject, Type type) {
Objects[gameObject].SingleOrDefault(component => type.IsAssignableFrom(component.GetType()));
}

public static Component? GetComponentInChildren(GameObject gameObject, Type type, bool includeInactive) =>
GetComponent(gameObject, type);

public static unsafe void GetComponentFastPath(GameObject gameObject, Type type, IntPtr oneFurtherThanResultValue) {
var component = GetComponent(gameObject, type);

Expand Down
118 changes: 96 additions & 22 deletions src/MultiplayerMod.Test/Game/Chores/ChoreEventsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ public class ChoreEventsTest : AbstractGameTest {

target = createGameObject().GetComponent<KMonoBehaviour>();
target.gameObject.AddComponent<ChoreProvider>();
target.gameObject.AddComponent<KPrefabID>();
target.gameObject.AddComponent<MeshRenderer>();
target.gameObject.AddComponent<Prioritizable>();

var sensors = target.gameObject.AddComponent<Sensors>();
sensors.Add(new SafeCellSensor(sensors));

gameObject = createGameObject();
gameObject.AddComponent<Pickupable>();
gameObject.AddComponent<PrimaryElement>();

choreType = Db.Get().ChoreTypes.Astronaut;
kPrefabID = createGameObject().AddComponent<KPrefabID>();
kPrefabID.gameObject.AddComponent<SkillPerkMissingComplainer>(); // required for RancherChore
Expand All @@ -49,15 +59,14 @@ public void TestEventFiring(System.Action createNewChore, Type choreType, Func<o

// ReSharper disable ObjectCreationAsStatement
private static object[][] GetTestArgs() {
// var cellCallback = new Func<MoveChore.StatesInstance, int>(_ => 5);
var cellCallback = new Func<MoveChore.StatesInstance, int>(_ => 5);

var testArgs = new[] {
new object[] {
new System.Action(() => new AttackChore(target, gameObject)),
typeof(AttackChore),
new Func<object[]>(() => new object[] { target, gameObject })
},

new object[] {
new System.Action(() => new DeliverFoodChore(target)),
typeof(DeliverFoodChore),
Expand All @@ -74,11 +83,6 @@ private static object[][] GetTestArgs() {
new Func<object[]>(() => new object[] { choreType, target })
},
// new object[] {
// new System.Action(() => new EmoteChore(target, choreType, emote)),
// typeof(EmoteChore),
// new Func<object?[]>(() => new object?[] { target, choreType, emote, 1, null })
// },
// new object[] {
// new System.Action(() => new EquipChore(target)),
// typeof(EquipChore),
// new Func<object[]>(() => new object[] { target })
Expand All @@ -88,11 +92,6 @@ private static object[][] GetTestArgs() {
// typeof(FixedCaptureChore),
// new Func<object[]>(() => new object[] { kPrefabID })
// },
// new object[] {
// new System.Action(() => new MoveChore(target, choreType, cellCallback)),
// typeof(MoveChore),
// new Func<object[]>(() => new object[] { target, choreType, cellCallback, false })
// },
new object[] {
new System.Action(() => new MoveToQuarantineChore(target, target)),
typeof(MoveToQuarantineChore),
Expand Down Expand Up @@ -151,15 +150,6 @@ private static object[][] GetTestArgs() {
typeof(SighChore),
new Func<object[]>(() => new object[] { target })
},
// new object[] {
// new System.Action(
// () => new StressEmoteChore(target, choreType, new HashedString(1), null, KAnim.PlayMode.Loop, null)
// ),
// typeof(StressEmoteChore),
// new Func<object?[]>(
// () => new object?[] { target, choreType, new HashedString(1), null, KAnim.PlayMode.Loop, null }
// )
// },
new object[] {
new System.Action(() => new StressIdleChore(target)),
typeof(StressIdleChore),
Expand Down Expand Up @@ -206,8 +196,92 @@ private static object[][] GetTestArgs() {
// }
// )
// }
new object[] {
new System.Action(() => new AggressiveChore(target)),
typeof(AggressiveChore),
new Func<object?[]>(() => new object?[] { target, null })
},
new object[] {
new System.Action(() => new BeIncapacitatedChore(target)),
typeof(BeIncapacitatedChore),
new Func<object[]>(() => new object[] { target })
},
new object[] {
new System.Action(() => new BingeEatChore(target)),
typeof(BingeEatChore),
new Func<object?[]>(() => new object?[] { target, null })
},
new object[] {
new System.Action(() => new EatChore(target)),
typeof(EatChore),
new Func<object[]>(() => new object[] { target })
},
new object[] {
new System.Action(() => new EmoteChore(target, choreType, emote)),
typeof(EmoteChore),
new Func<object?[]>(() => new object?[] { target, choreType, emote, 1, null })
},
new object[] {
new System.Action(() => new EntombedChore(target, "override")),
typeof(EntombedChore),
new Func<object[]>(() => new object[] { target, "override" })
},
// new object[] {
// new System.Action(() => new FetchAreaChore(new Chore.Precondition.Context())),
// typeof(FetchAreaChore),
// new Func<object[]>(() => new object[] { new Chore.Precondition.Context() })
// },
new object[] {
new System.Action(() => new FleeChore(target, gameObject)),
typeof(FleeChore),
new Func<object[]>(() => new object[] { target, gameObject })
},
new object[] {
new System.Action(() => new FoodFightChore(target, gameObject)),
typeof(FoodFightChore),
new Func<object[]>(() => new object[] { target, gameObject })
},
new object[] {
new System.Action(() => new MoveChore(target, choreType, cellCallback)),
typeof(MoveChore),
new Func<object[]>(() => new object[] { target, choreType, cellCallback, false })
},
new object[] {
new System.Action(() => new MournChore(target)),
typeof(MournChore),
new Func<object[]>(() => new object[] { target })
},
new object[] {
new System.Action(() => new MovePickupableChore(target, gameObject, null)),
typeof(MovePickupableChore),
new Func<object?[]>(() => new object?[] { target, gameObject, null })
},
new object[] {
new System.Action(() => new MoveToSafetyChore(target)),
typeof(MoveToSafetyChore),
new Func<object[]>(() => new object[] { target })
},
new object[] {
new System.Action(() => new RecoverBreathChore(target)),
typeof(RecoverBreathChore),
new Func<object[]>(() => new object[] { target })
},
new object[] {
new System.Action(() => new SleepChore(choreType, target, gameObject, false, false)),
typeof(SleepChore),
new Func<object[]>(() => new object[] { choreType, target, gameObject, false, false })
},
new object[] {
new System.Action(
() => new StressEmoteChore(target, choreType, new HashedString(1), null, KAnim.PlayMode.Loop, null)
),
typeof(StressEmoteChore),
new Func<object?[]>(
() => new object?[] { target, choreType, new HashedString(1), null, KAnim.PlayMode.Loop, null }
)
},
};
Assert.AreEqual(ChoreList.DeterministicChores.Count, testArgs.Length);
Assert.AreEqual(ChoreList.SupportedChores.Count, testArgs.Length);
return testArgs;
}
}
Loading

0 comments on commit 40189af

Please sign in to comment.