Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework FixNullMotes patch #489

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
67 changes: 67 additions & 0 deletions Source/Client/Patches/Determinism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,71 @@ static void Postfix(UndercaveMapComponent __instance)
}
}

[HarmonyPatch(typeof(MoteMaker), nameof(MoteMaker.MakeStaticMote))]
[HarmonyPatch([typeof(Vector3), typeof(Map), typeof(ThingDef), typeof(float), typeof(bool), typeof(float)])]
static class FixNullMotes
{
// Make sure that motes will (almost) always spawn. We skip based to player-specific
// data, and instead only allow the only fully deterministic checks (location is in map bounds).
// We skip checks to current map and current mote counter saturation, as those may vary between players.

static void Prefix(ref bool makeOffscreen)
{
// Skip a call to GenView.ShouldSpawnMotesAt, forcing
// each call to check GenGrid.InBounds instead.
// ShouldSpawnMotesAt would check for bounds, but also
// include a current map check as well, which we don't want.
makeOffscreen = true;
}

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instr)
{
var targetCall = AccessTools.DeclaredPropertyGetter(typeof(MoteCounter), nameof(MoteCounter.Saturated));

foreach (var ci in instr)
{
yield return ci;

// Add "& false" to any call to MoteCounter.Saturated to get a deterministic result.
// Not a perfect solution performance-wise, but will prevent desyncs due to
// MoteMaker.MakeStaticMote being non-deterministic without this change.
if (ci.Calls(targetCall))
{
yield return new CodeInstruction(OpCodes.Ldc_I4_0);
yield return new CodeInstruction(OpCodes.And);
}
}
}
}

[HarmonyPatch(typeof(Building_BioferriteHarvester), nameof(Building_BioferriteHarvester.SpawnSetup))]
static class AlwaysRebuildBioferriteHarvesterCables
{
static void Postfix(Building_BioferriteHarvester __instance)
{
// After reloading the game the initialize method will generally be called
// during rendering, ensure it's called on respawning as well.
if (!__instance.initalized)
{
// We need to call ExecuteWhenFinished, as it will crash otherwise.
LongEventHandler.ExecuteWhenFinished(__instance.Initialize);
}
}
}

[HarmonyPatch(typeof(Building_Electroharvester), nameof(Building_Electroharvester.SpawnSetup))]
static class AlwaysRebuildElectroharvesterCables
{
static void Postfix(Building_Electroharvester __instance)
{
// After reloading the game the initialize method will generally be called
// during rendering, ensure it's called on respawning as well.
if (!__instance.initalized)
{
// We need to call ExecuteWhenFinished, as it will crash otherwise.
LongEventHandler.ExecuteWhenFinished(__instance.Initialize);
}
}
}

}
25 changes: 0 additions & 25 deletions Source/Client/Patches/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,31 +426,6 @@ internal static void Choose(QuestPart_Choice part, int index)
}
}

[HarmonyPatch(typeof(MoteMaker), nameof(MoteMaker.MakeStaticMote))]
[HarmonyPatch(new[] {typeof(Vector3), typeof(Map), typeof(ThingDef), typeof(float), typeof(bool), typeof(float)})]
static class FixNullMotes
{
static Dictionary<Type, Mote> cache = new();

static void Postfix(ThingDef moteDef, ref Mote __result) {
if (__result != null) return;

if (moteDef.mote.needsMaintenance) return;

var thingClass = moteDef.thingClass;

if (cache.TryGetValue(thingClass, out Mote value)) {
__result = value;
} else {
__result = (Mote) Activator.CreateInstance(thingClass);

cache.Add(thingClass, __result);
}

__result.def = moteDef;
}
}

[HarmonyPatch(typeof(DiaOption), nameof(DiaOption.Activate))]
static class NodeTreeDialogSync
{
Expand Down
Loading