Skip to content

Commit

Permalink
#13 Generalize ChoreList (will be extended in following PRs). Disable…
Browse files Browse the repository at this point in the history
… creation chore sync for one using global providers. Improve Unit tests.
  • Loading branch information
zuev93 committed Dec 9, 2023
1 parent 8d33a72 commit d364eca
Show file tree
Hide file tree
Showing 19 changed files with 846 additions and 565 deletions.
23 changes: 13 additions & 10 deletions src/MultiplayerMod.Test/AbstractGameTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using MultiplayerMod.Core.Dependency;
using MultiplayerMod.ModRuntime;
Expand All @@ -9,7 +8,7 @@
using MultiplayerMod.Multiplayer.Objects;
using MultiplayerMod.Test.Environment.Patches;
using MultiplayerMod.Test.Environment.Unity;
using MultiplayerMod.Test.Multiplayer.Commands.Chores;
using MultiplayerMod.Test.Multiplayer.Commands.Chores.Patches;
using NUnit.Framework;
using UnityEngine;

Expand All @@ -19,15 +18,13 @@ public abstract class AbstractGameTest {

private static Harmony harmony = null!;

protected HashSet<Type> patches = typeof(CreateHostChoreTest).Assembly.GetTypes()
.Where(type => type.Namespace == typeof(CreateHostChoreTest).Namespace + ".Patches")
.ToHashSet();
protected readonly HashSet<Type> Patches = new(new[] { typeof(DbPatch) });

[SetUp]
public void SetUp() {
harmony = new Harmony("CreateHostChoreTest");
harmony = new Harmony("AbstractGameTest");
UnityTestRuntime.Install();
PatchesSetup.Install(harmony, patches);
PatchesSetup.Install(harmony, Patches);
SetUpUnityAndGame();
SetupDependencies();
}
Expand Down Expand Up @@ -58,19 +55,23 @@ private static void SetUpUnityAndGame() {
ReportManager.Instance.Awake();
ReportManager.Instance.todaysReport = new ReportManager.DailyReport(ReportManager.Instance);

GlobalChoreProvider.Instance = new GlobalChoreProvider();

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

worldGameObject.AddComponent<MinionGroupProber>().OnPrefabInit();
worldGameObject.AddComponent<GameClock>().OnPrefabInit();
worldGameObject.AddComponent<GlobalChoreProvider>().OnPrefabInit();
}

private static void InitGame(GameObject worldGameObject) {
// From global
Singleton<StateMachineUpdater>.CreateInstance();
Singleton<StateMachineManager>.CreateInstance();
Singleton<CellChangeMonitor>.CreateInstance();

var game = worldGameObject.AddComponent<global::Game>();
global::Game.Instance = game;
game.obj = KObjectManager.Instance.GetOrCreateObject(game.gameObject);

var widthInCells = 40;
var heightInCells = 40;
Expand All @@ -85,6 +86,7 @@ private static void InitGame(GameObject worldGameObject) {
game.travelTubeSystem = new UtilityNetworkTubesManager(widthInCells, heightInCells, 35);
game.gasConduitFlow = new ConduitFlow(ConduitType.Gas, numCells, game.gasConduitSystem, 1f, 0.25f);
game.liquidConduitFlow = new ConduitFlow(ConduitType.Liquid, numCells, game.liquidConduitSystem, 10f, 0.75f);
game.mingleCellTracker = worldGameObject.AddComponent<MingleCellTracker>();

GridSettings.Reset(widthInCells, heightInCells);

Expand All @@ -103,7 +105,8 @@ private static void SetupDependencies() {
new DependencyInfo(nameof(ExecutionContextManager), typeof(ExecutionContextManager), false)
);
dependencyContainer.Register(
new DependencyInfo(nameof(DependencyContainer), typeof(DependencyContainer), false));
new DependencyInfo(nameof(DependencyContainer), typeof(DependencyContainer), false)
);
new Runtime(dependencyContainer);
Runtime.Instance.Dependencies.Get<MultiplayerGame>().Refresh(MultiplayerMode.Host);
Runtime.Instance.Dependencies.Get<ExecutionLevelManager>().EnterOverrideSection(ExecutionLevel.Game);
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 All @@ -21,6 +21,17 @@ private static IEnumerable<CodeInstruction> Component_get_gameObject(IEnumerable
};
}

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch("get_transform")]
private static IEnumerable<CodeInstruction> Component_get_transform(IEnumerable<CodeInstruction> instructions) {
return new List<CodeInstruction> {
new(OpCodes.Ldarg_0), // this
CodeInstruction.Call(typeof(ComponentPatch), nameof(GetTransform)),
new(OpCodes.Ret)
};
}

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch("GetComponentFastPath")]
Expand All @@ -35,4 +46,8 @@ IEnumerable<CodeInstruction> instructions
new(OpCodes.Ret)
};
}

public static Transform GetTransform(Component component) {
return component.gameObject.GetComponent<Transform>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,54 @@ private static IEnumerable<CodeInstruction> GameObject_GetComponent(IEnumerable<

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(GameObject), "get_transform")]
private static IEnumerable<CodeInstruction> GameObject_get_transform(IEnumerable<CodeInstruction> instructions) {
[HarmonyPatch("GetComponentsInternal")]
private static IEnumerable<CodeInstruction> GameObject_GetComponentsInternal(
IEnumerable<CodeInstruction> instructions
) {
return new List<CodeInstruction> {
new(OpCodes.Ldarg_0), // this
CodeInstruction.Call(typeof(GameObjectPatch), nameof(GetTransform)),
new(OpCodes.Ldarg_1), // type
CodeInstruction.Call(typeof(UnityTestRuntime), nameof(UnityTestRuntime.GetComponent)),
new(OpCodes.Ret)
};
}

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch("Internal_AddComponentWithType")]
private static IEnumerable<CodeInstruction> GameObject_Internal_AddComponentWithType(
[HarmonyPatch("GetComponentInChildren", typeof(Type), typeof(bool))]
private static IEnumerable<CodeInstruction> GameObject_GetComponentInChildren(
IEnumerable<CodeInstruction> instructions
) {
return new List<CodeInstruction> {
new(OpCodes.Ldarg_0), // this (go)
new(OpCodes.Ldarg_1), // componentType
CodeInstruction.Call(typeof(UnityTestRuntime), nameof(UnityTestRuntime.AddComponent)),
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
CodeInstruction.Call(typeof(GameObjectPatch), nameof(GetTransform)),
new(OpCodes.Ret)
};
}

[UsedImplicitly]
[HarmonyTranspiler]
[HarmonyPatch(typeof(Time), "get_frameCount")]
private static IEnumerable<CodeInstruction> Time_get_frameCount(IEnumerable<CodeInstruction> instructions) {
[HarmonyPatch("Internal_AddComponentWithType")]
private static IEnumerable<CodeInstruction> GameObject_Internal_AddComponentWithType(
IEnumerable<CodeInstruction> instructions
) {
return new List<CodeInstruction> {
new(
OpCodes.Call,
AccessTools.PropertyGetter(typeof(UnityTestRuntime), nameof(UnityTestRuntime.FrameCount))
),
new(OpCodes.Ldarg_0), // this (go)
new(OpCodes.Ldarg_1), // componentType
CodeInstruction.Call(typeof(UnityTestRuntime), nameof(UnityTestRuntime.AddComponent)),
new(OpCodes.Ret)
};
}
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,29 @@
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 _) {
#pragma warning disable CS0618 // Type or member is obsolete
return new Material("");
#pragma warning restore CS0618 // Type or member is obsolete
}
}
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
Loading

0 comments on commit d364eca

Please sign in to comment.