-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dev' into dev
- Loading branch information
Showing
9 changed files
with
216 additions
and
3 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
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
10 changes: 10 additions & 0 deletions
10
src/CommunityFixes/Fix/SaveLoadDateTimeFix/SaveLoadDateTimeFix.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,10 @@ | ||
namespace CommunityFixes.Fix.STFUFix; | ||
|
||
[Fix("Save/Load Date/Time Fix")] | ||
public class SaveLoadDateTimeFix : BaseFix | ||
{ | ||
public override void OnInitialized() | ||
{ | ||
HarmonyInstance.PatchAll(typeof(SaveLoadDateTimeFix_Patch)); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/CommunityFixes/Fix/SaveLoadDateTimeFix/SaveLoadDateTimeFix_Patch.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,37 @@ | ||
using HarmonyLib; | ||
using KSP.Game; | ||
using System.Globalization; | ||
using UnityEngine.UI; | ||
|
||
namespace CommunityFixes.Fix.STFUFix; | ||
|
||
internal class SaveLoadDateTimeFix_Patch | ||
{ | ||
[HarmonyPatch(typeof(SaveLoadDialogFileEntry), nameof(SaveLoadDialogFileEntry.Initialize), new Type[] { typeof(ExtendedSaveFileInfo), typeof(bool), typeof(bool) })] | ||
[HarmonyPrefix] | ||
public static void SaveLoadDialogFileEntry_Initialize(SaveLoadDialogFileEntry __instance, ExtendedSaveFileInfo fileInfo, bool loading, bool isLastPlayed) | ||
{ | ||
CultureInfo.DefaultThreadCurrentCulture = Thread.CurrentThread.CurrentUICulture; | ||
} | ||
|
||
[HarmonyPatch(typeof(SaveLoadDialog), nameof(SaveLoadDialog.UpdateLoadMenuGameInformation), new Type[] { typeof(ExtendedSaveFileInfo), typeof(Image) })] | ||
[HarmonyPrefix] | ||
public static void SaveLoadDialog_UpdateLoadMenuGameInformation(SaveLoadDialog __instance, ExtendedSaveFileInfo fileInfo, Image thumnailScreenshot) | ||
{ | ||
CultureInfo.DefaultThreadCurrentCulture = Thread.CurrentThread.CurrentUICulture; | ||
} | ||
|
||
[HarmonyPatch(typeof(CampaignLoadMenu), nameof(CampaignLoadMenu.UpdateLoadMenuGameInformation), new Type[] { typeof(ExtendedSaveFileInfo), typeof(Image) })] | ||
[HarmonyPrefix] | ||
public static void CampaignLoadMenu_UpdateLoadMenuGameInformation(CampaignLoadMenu __instance, ExtendedSaveFileInfo fileInfo, Image thumnailScreenshot) | ||
{ | ||
CultureInfo.DefaultThreadCurrentCulture = Thread.CurrentThread.CurrentUICulture; | ||
} | ||
|
||
[HarmonyPatch(typeof(CampaignTileEntry), nameof(CampaignTileEntry.Initialize), new Type[] { typeof(ExtendedSaveFileInfo), typeof(CampaignLoadMenu), typeof(CampaignMenu) })] | ||
[HarmonyPrefix] | ||
public static void CampaignTileEntry_UpdateLoadMenuGameInformation(CampaignTileEntry __instance, ExtendedSaveFileInfo fileInfo, CampaignLoadMenu loadMenu, CampaignMenu campaignMenu) | ||
{ | ||
CultureInfo.DefaultThreadCurrentCulture = Thread.CurrentThread.CurrentUICulture; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/CommunityFixes/Fix/TimeWarpThrustFix/TimeWarpThrustFix.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,67 @@ | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using KSP.Sim.impl; | ||
using SpaceWarp.API.Logging; | ||
|
||
namespace CommunityFixes.Fix.TimeWarpThrustFix; | ||
|
||
[Fix("Fixes time warp thrust rounding error.")] | ||
public class TimeWarpThrustFix : BaseFix | ||
{ | ||
private static ILogger _logger; | ||
|
||
public override void OnInitialized() | ||
{ | ||
_logger = Logger; | ||
HarmonyInstance.PatchAll(typeof(TimeWarpThrustFix)); | ||
} | ||
|
||
private static bool CheckNeedRoundingError(double positionError, double velocityError) | ||
{ | ||
var needError = positionError >= 0.01 || velocityError >= 0.01; | ||
if (needError) | ||
{ | ||
_logger.LogDebug($"pos_err={positionError}, vel_err={velocityError}"); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
[HarmonyPatch(typeof(VesselComponent), nameof(VesselComponent.HandleOrbitalPhysicsUnderThrustStart))] | ||
[HarmonyTranspiler] | ||
public static IEnumerable<CodeInstruction> VesselComponent_HandleOrbitalPhysicsUnderThrustStart( | ||
IEnumerable<CodeInstruction> bodyToReplace | ||
) | ||
{ | ||
var propGetSqrMagnitude = typeof(Vector3d).GetProperty("sqrMagnitude")!.GetGetMethod(); | ||
var res = TranspilerHelper.Replace( | ||
bodyToReplace, | ||
[ | ||
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Ldloca_S }, | ||
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Call, Operand = propGetSqrMagnitude }, | ||
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Ldc_R8 }, | ||
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Bge_Un }, | ||
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Ldloca_S }, | ||
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Call, Operand = propGetSqrMagnitude }, | ||
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Ldc_R8 }, | ||
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Bge_Un }, | ||
], | ||
oldBody => | ||
{ | ||
var checkNeedRoundingError = CheckNeedRoundingError; | ||
var instructions = oldBody.ToArray(); | ||
return | ||
[ | ||
instructions[0], instructions[1], | ||
instructions[4], instructions[5], | ||
new CodeInstruction(OpCodes.Call, checkNeedRoundingError.Method), | ||
new CodeInstruction(OpCodes.Ldc_I4_1), | ||
instructions[7], new CodeInstruction(OpCodes.Nop), | ||
]; | ||
}, | ||
1..1 | ||
); | ||
|
||
return res; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/CommunityFixes/Fix/TimeWarpThrustFix/TranspilerHelper.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,58 @@ | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using JetBrains.Annotations; | ||
|
||
namespace CommunityFixes.Fix.TimeWarpThrustFix; | ||
|
||
internal static class TranspilerHelper | ||
{ | ||
public sealed class ILLookupKey | ||
{ | ||
public OpCode OpCode { get; set; } | ||
[CanBeNull] public object Operand { get; set; } | ||
} | ||
|
||
public static IEnumerable<CodeInstruction> Replace( | ||
IEnumerable<CodeInstruction> oldBody, | ||
ILLookupKey[] lookupKeys, | ||
Func<IReadOnlyCollection<CodeInstruction>, IEnumerable<CodeInstruction>> repl, | ||
Range expectedTimes | ||
) | ||
{ | ||
var queue = new Queue<CodeInstruction>(lookupKeys.Length); | ||
var foundCount = 0; | ||
|
||
foreach (var instruction in oldBody) | ||
{ | ||
queue.Enqueue(instruction); | ||
if (queue.Count < lookupKeys.Length) continue; | ||
|
||
if (queue.Zip(lookupKeys, (instr, lookup) => | ||
instr.opcode == lookup.OpCode && | ||
(lookup.Operand is null || Equals(instr.operand, lookup.Operand)) | ||
).All(b => b)) | ||
{ | ||
foundCount++; | ||
foreach (var modInstr in repl(queue)) | ||
{ | ||
yield return modInstr; | ||
} | ||
|
||
queue.Clear(); | ||
continue; | ||
} | ||
|
||
yield return queue.Dequeue(); | ||
} | ||
|
||
foreach (var instr in queue) | ||
{ | ||
yield return instr; | ||
} | ||
|
||
if (foundCount < expectedTimes.Start.Value || foundCount > expectedTimes.End.Value) | ||
{ | ||
throw new InvalidOperationException($"Found expected IL {foundCount} times, instead of {expectedTimes}"); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...VesselLandedState/VesselLandedStateFix.cs → ...selLandedStateFix/VesselLandedStateFix.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