-
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 pull request #50 from KSP2Community/dev
Time Warp Thrust Fix
- Loading branch information
Showing
7 changed files
with
194 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
name: Upload release | ||
|
||
env: | ||
SPACEDOCK_MOD_ID: 3301 | ||
SPACEDOCK_MOD_URL: "https://spacedock.info/mod/3301/Community%20Fixes#changelog" # The URL of your mod on SpaceDock" | ||
FORUM_TOPIC_URL: "https://forum.kerbalspaceprogram.com/topic/..." # The URL of your mod's forum topic | ||
FORUM_TOPIC_TITLE: "Community Fixes [v{version} for KSP2 v{ksp2_version}]" # The title of your mod's forum topic | ||
DISCORD_MOD_THREAD_ID: "1183537268565229648" # The thread ID of the mod's Discord post | ||
DISCORD_USERNAME: "Community Fixes" # The username of the bot that will post to Discord | ||
DISCORD_AVATAR_URL: "https://i.imgur.com/M4SUSaf.png" # The Image URL of the bot's avatar. Not required. | ||
|
||
on: | ||
release: | ||
types: [ "published" ] | ||
|
@@ -11,23 +20,29 @@ jobs: | |
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
with: | ||
lfs: true | ||
|
||
- name: Download NuGet | ||
id: download-nuget | ||
run: | | ||
sudo curl -o /usr/local/bin/nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe | ||
run: sudo curl -o /usr/local/bin/nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe | ||
|
||
- name: Install jq | ||
uses: dcarbone/[email protected] | ||
|
||
- name: Build the solution | ||
run: dotnet build "CommunityFixes.sln" -c Release | ||
|
||
- name: Extract current version | ||
id: get-version | ||
run: | | ||
version=$(jq -r '.version' plugin_template/swinfo.json) | ||
echo "Version is $version" | ||
dotnet build "CommunityFixes.sln" -c Release | ||
echo "version=$version" >> $GITHUB_ENV | ||
echo "release_filename=CommunityFixes-$version.zip" >> $GITHUB_ENV | ||
echo "zip=$(ls -1 dist/CommunityFixes-*.zip | head -n 1)" >> $GITHUB_ENV | ||
echo "upload_url=$(wget -qO- https://api.github.com/repos/$GITHUB_REPOSITORY/releases | jq '.[0].upload_url' | tr -d \")" >> $GITHUB_ENV | ||
wget -qO- https://api.github.com/repos/$GITHUB_REPOSITORY/releases | jq -r '.[0].body' > ./changelog.md | ||
- name: Upload zip to release | ||
uses: shogo82148/[email protected] | ||
|
@@ -38,3 +53,48 @@ jobs: | |
asset_path: ${{ env.zip }} | ||
asset_name: ${{ env.release_filename }} | ||
asset_content_type: application/zip | ||
|
||
- name: Add Mask | ||
run: echo "::add-mask::${{ secrets.SPACEDOCK_PASSWORD }}" | ||
|
||
- name: Update mod on SpaceDock | ||
uses: KSP2Community/[email protected] | ||
with: | ||
username: ${{ secrets.SPACEDOCK_USER }} | ||
password: ${{ secrets.SPACEDOCK_PASSWORD }} | ||
game_id: 22407 | ||
mod_id: ${{ env.SPACEDOCK_MOD_ID }} | ||
version: ${{ env.version }} | ||
zipball: ${{ env.zip }} | ||
changelog: ./changelog.md | ||
|
||
- name: Update Forum topic | ||
uses: Kerbalight/ksp2-forum-post-action@latest | ||
with: | ||
username: ${{ secrets.KSP_FORUM_USERNAME }} | ||
password: ${{ secrets.KSP_FORUM_PASSWORD }} | ||
forum_topic_url: ${{ env.FORUM_TOPIC_URL }} | ||
forum_topic_title: ${{ env.FORUM_TOPIC_TITLE }} | ||
spacedock_url: ${{ env.SPACEDOCK_MOD_URL}} | ||
version: ${{ env.version }} | ||
changelog: ./changelog.md | ||
|
||
- name: Prepare message for Discord | ||
shell: bash | ||
run: | | ||
echo -e "## Release v${version}\n" > ./content.md | ||
cat ./changelog.md >> ./content.md | ||
{ | ||
echo 'discord_message<<EOF' | ||
cat ./content.md | ||
echo EOF | ||
} >> "$GITHUB_ENV" | ||
- name: Publish update to Discord | ||
uses: tsickert/[email protected] | ||
with: | ||
webhook-url: ${{ secrets.DISCORD_KMS_WEBHOOK_URL }} | ||
content: ${{ env.discord_message }} | ||
thread-id: ${{ env.DISCORD_MOD_THREAD_ID }} | ||
username: ${{ env.DISCORD_USERNAME }} | ||
avatar-url: ${{ env.DISCORD_AVATAR_URL }} |
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
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