Skip to content

Commit

Permalink
Merge pull request #50 from KSP2Community/dev
Browse files Browse the repository at this point in the history
Time Warp Thrust Fix
  • Loading branch information
jan-bures authored Mar 10, 2024
2 parents 8751b0f + 2523a2d commit 758c8d2
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 5 deletions.
66 changes: 63 additions & 3 deletions .github/workflows/release.yml
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" ]
Expand All @@ -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]
Expand All @@ -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 }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ _UpgradeReport_Files/
**/.idea/**/contentModel.xml
**/.idea/**/modules.xml

# Rider plugins
**/.idea/**/copilot/

# VS Code files
.vscode/*
!.vscode/settings.json
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This project aims to bring together community bug fixes for Kerbal Space Program
- **Stock Mission Fix** by [Cheese](https://github.com/cheese3660) - Fixes the incorrect completion conditions of some stock missions.
- **Resource Manager UI Fix** by [munix](https://github.com/jan-bures) - Fixes the Resource Manager bug where moving a tank from the right pane back to the left pane caused it to duplicate.
- **Decoupled Craft Name Fix** by [munix](https://github.com/jan-bures) - Decoupled and docked/undocked vessels get names based on the original vessels instead of "Default Name" and "(Combined)".
- **Time Warp Thrust Fix** by [SunSerega](https://github.com/SunSerega) - Fixes the bug where thrust under time warp was sometimes not working despite draining fuel.

## Planned fixes
To see what fixes are planned to be implemented, you can visit the [Issues page](https://github.com/KSP2Community/CommunityFixes/issues) on the project's GitHub.
Expand Down
2 changes: 1 addition & 1 deletion plugin_template/swinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Community Fixes",
"description": "Community project that aims to bring together bug fixes for KSP 2.",
"source": "https://github.com/KSP2Community/CommunityFixes",
"version": "0.12.0",
"version": "0.13.0",
"version_check": "https://raw.githubusercontent.com/KSP2Community/CommunityFixes/main/plugin_template/swinfo.json",
"ksp2_version": {
"min": "0.2.1",
Expand Down
67 changes: 67 additions & 0 deletions src/CommunityFixes/Fix/TimeWarpThrustFix/TimeWarpThrustFix.cs
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 src/CommunityFixes/Fix/TimeWarpThrustFix/TranspilerHelper.cs
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}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using KSP.Sim.impl;

namespace CommunityFixes.Fix.VesselLandedState;
namespace CommunityFixes.Fix.VesselLandedStateFix;

[Fix("Vessel Landed State")]
public class VesselLandedStateFix: BaseFix
Expand Down

0 comments on commit 758c8d2

Please sign in to comment.