Skip to content

Commit

Permalink
Fixed Vehicle Framework route planner with dev mode off (#408)
Browse files Browse the repository at this point in the history
* Fixed Vehicle Framework route planner with dev mode off

* Fix copy-paste errors
  • Loading branch information
SokyranTheDragon authored Jan 7, 2024
1 parent ceeca2c commit 70dff09
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Source_Referenced/VehicleFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2514,5 +2514,48 @@ private static bool PreTurretTargeterCurrentTurretGetter()
}

#endregion

#region Route Planner

// Route planner has some checks if it should be stopped, like: is it set to active,
// is the world rendered, is the game state ProgramState.Playing, is the time paused,
// and finally - the last 2 are skipped in dev mode. Replace the dev mode check with
// dev mode or MP check, so it'll let people have the route planner open with MP no
// matter the game speed settings.
// We could make a simple prefix and basically replace part of the method, but if
// the method ever ends up getting modified we'd have to do so as well... it'll be
// more future-proof if we just replace the specific check.

private static bool ReplacedDevModeCheck() => Prefs.DevMode || MP.IsInMultiplayer;

[MpCompatTranspiler(typeof(VehicleRoutePlanner), nameof(VehicleRoutePlanner.ShouldStop), methodType: MethodType.Getter)]

Check failure on line 2531 in Source_Referenced/VehicleFramework.cs

View workflow job for this annotation

GitHub Actions / Build workshop

Argument 1: cannot convert from 'System.Type' to 'string'

Check failure on line 2531 in Source_Referenced/VehicleFramework.cs

View workflow job for this annotation

GitHub Actions / Build workshop

Argument 1: cannot convert from 'System.Type' to 'string'
private static IEnumerable<CodeInstruction> LogResult(IEnumerable<CodeInstruction> instr, MethodBase baseMethod)
{
var target = AccessTools.DeclaredPropertyGetter(typeof(Prefs), nameof(Prefs.DevMode));
var replacement = AccessTools.DeclaredMethod(typeof(VehicleFramework), nameof(ReplacedDevModeCheck));
var replacedCount = 0;

foreach (var ci in instr)
{
if (ci.Calls(target))
{
ci.opcode = OpCodes.Call;
ci.operand = replacement;

replacedCount++;
}

yield return ci;
}

const int expected = 1;
if (replacedCount != expected)
{
var name = (baseMethod.DeclaringType?.Namespace).NullOrEmpty() ? baseMethod.Name : $"{baseMethod.DeclaringType!.Name}:{baseMethod.Name}";
Log.Warning($"Patched incorrect number of Prefs.DevMode calls (replaced {replacedCount}, expected {expected}) for method {name}");
}
}

#endregion
}
}

0 comments on commit 70dff09

Please sign in to comment.