Skip to content

Commit 758c8d2

Browse files
authored
Merge pull request #50 from KSP2Community/dev
Time Warp Thrust Fix
2 parents 8751b0f + 2523a2d commit 758c8d2

File tree

7 files changed

+194
-5
lines changed

7 files changed

+194
-5
lines changed

.github/workflows/release.yml

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
name: Upload release
22

3+
env:
4+
SPACEDOCK_MOD_ID: 3301
5+
SPACEDOCK_MOD_URL: "https://spacedock.info/mod/3301/Community%20Fixes#changelog" # The URL of your mod on SpaceDock"
6+
FORUM_TOPIC_URL: "https://forum.kerbalspaceprogram.com/topic/..." # The URL of your mod's forum topic
7+
FORUM_TOPIC_TITLE: "Community Fixes [v{version} for KSP2 v{ksp2_version}]" # The title of your mod's forum topic
8+
DISCORD_MOD_THREAD_ID: "1183537268565229648" # The thread ID of the mod's Discord post
9+
DISCORD_USERNAME: "Community Fixes" # The username of the bot that will post to Discord
10+
DISCORD_AVATAR_URL: "https://i.imgur.com/M4SUSaf.png" # The Image URL of the bot's avatar. Not required.
11+
312
on:
413
release:
514
types: [ "published" ]
@@ -11,23 +20,29 @@ jobs:
1120
steps:
1221
- name: Checkout repository
1322
uses: actions/checkout@v4
23+
with:
24+
lfs: true
1425

1526
- name: Download NuGet
1627
id: download-nuget
17-
run: |
18-
sudo curl -o /usr/local/bin/nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
28+
run: sudo curl -o /usr/local/bin/nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
1929

2030
- name: Install jq
2131
uses: dcarbone/[email protected]
2232

2333
- name: Build the solution
34+
run: dotnet build "CommunityFixes.sln" -c Release
35+
36+
- name: Extract current version
37+
id: get-version
2438
run: |
2539
version=$(jq -r '.version' plugin_template/swinfo.json)
2640
echo "Version is $version"
27-
dotnet build "CommunityFixes.sln" -c Release
41+
echo "version=$version" >> $GITHUB_ENV
2842
echo "release_filename=CommunityFixes-$version.zip" >> $GITHUB_ENV
2943
echo "zip=$(ls -1 dist/CommunityFixes-*.zip | head -n 1)" >> $GITHUB_ENV
3044
echo "upload_url=$(wget -qO- https://api.github.com/repos/$GITHUB_REPOSITORY/releases | jq '.[0].upload_url' | tr -d \")" >> $GITHUB_ENV
45+
wget -qO- https://api.github.com/repos/$GITHUB_REPOSITORY/releases | jq -r '.[0].body' > ./changelog.md
3146
3247
- name: Upload zip to release
3348
uses: shogo82148/[email protected]
@@ -38,3 +53,48 @@ jobs:
3853
asset_path: ${{ env.zip }}
3954
asset_name: ${{ env.release_filename }}
4055
asset_content_type: application/zip
56+
57+
- name: Add Mask
58+
run: echo "::add-mask::${{ secrets.SPACEDOCK_PASSWORD }}"
59+
60+
- name: Update mod on SpaceDock
61+
uses: KSP2Community/[email protected]
62+
with:
63+
username: ${{ secrets.SPACEDOCK_USER }}
64+
password: ${{ secrets.SPACEDOCK_PASSWORD }}
65+
game_id: 22407
66+
mod_id: ${{ env.SPACEDOCK_MOD_ID }}
67+
version: ${{ env.version }}
68+
zipball: ${{ env.zip }}
69+
changelog: ./changelog.md
70+
71+
- name: Update Forum topic
72+
uses: Kerbalight/ksp2-forum-post-action@latest
73+
with:
74+
username: ${{ secrets.KSP_FORUM_USERNAME }}
75+
password: ${{ secrets.KSP_FORUM_PASSWORD }}
76+
forum_topic_url: ${{ env.FORUM_TOPIC_URL }}
77+
forum_topic_title: ${{ env.FORUM_TOPIC_TITLE }}
78+
spacedock_url: ${{ env.SPACEDOCK_MOD_URL}}
79+
version: ${{ env.version }}
80+
changelog: ./changelog.md
81+
82+
- name: Prepare message for Discord
83+
shell: bash
84+
run: |
85+
echo -e "## Release v${version}\n" > ./content.md
86+
cat ./changelog.md >> ./content.md
87+
{
88+
echo 'discord_message<<EOF'
89+
cat ./content.md
90+
echo EOF
91+
} >> "$GITHUB_ENV"
92+
93+
- name: Publish update to Discord
94+
uses: tsickert/[email protected]
95+
with:
96+
webhook-url: ${{ secrets.DISCORD_KMS_WEBHOOK_URL }}
97+
content: ${{ env.discord_message }}
98+
thread-id: ${{ env.DISCORD_MOD_THREAD_ID }}
99+
username: ${{ env.DISCORD_USERNAME }}
100+
avatar-url: ${{ env.DISCORD_AVATAR_URL }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ _UpgradeReport_Files/
3434
**/.idea/**/contentModel.xml
3535
**/.idea/**/modules.xml
3636

37+
# Rider plugins
38+
**/.idea/**/copilot/
39+
3740
# VS Code files
3841
.vscode/*
3942
!.vscode/settings.json

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This project aims to bring together community bug fixes for Kerbal Space Program
1515
- **Stock Mission Fix** by [Cheese](https://github.com/cheese3660) - Fixes the incorrect completion conditions of some stock missions.
1616
- **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.
1717
- **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)".
18+
- **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.
1819

1920
## Planned fixes
2021
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.

plugin_template/swinfo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "Community Fixes",
66
"description": "Community project that aims to bring together bug fixes for KSP 2.",
77
"source": "https://github.com/KSP2Community/CommunityFixes",
8-
"version": "0.12.0",
8+
"version": "0.13.0",
99
"version_check": "https://raw.githubusercontent.com/KSP2Community/CommunityFixes/main/plugin_template/swinfo.json",
1010
"ksp2_version": {
1111
"min": "0.2.1",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Reflection.Emit;
2+
using HarmonyLib;
3+
using KSP.Sim.impl;
4+
using SpaceWarp.API.Logging;
5+
6+
namespace CommunityFixes.Fix.TimeWarpThrustFix;
7+
8+
[Fix("Fixes time warp thrust rounding error.")]
9+
public class TimeWarpThrustFix : BaseFix
10+
{
11+
private static ILogger _logger;
12+
13+
public override void OnInitialized()
14+
{
15+
_logger = Logger;
16+
HarmonyInstance.PatchAll(typeof(TimeWarpThrustFix));
17+
}
18+
19+
private static bool CheckNeedRoundingError(double positionError, double velocityError)
20+
{
21+
var needError = positionError >= 0.01 || velocityError >= 0.01;
22+
if (needError)
23+
{
24+
_logger.LogDebug($"pos_err={positionError}, vel_err={velocityError}");
25+
}
26+
27+
return false;
28+
}
29+
30+
[HarmonyPatch(typeof(VesselComponent), nameof(VesselComponent.HandleOrbitalPhysicsUnderThrustStart))]
31+
[HarmonyTranspiler]
32+
public static IEnumerable<CodeInstruction> VesselComponent_HandleOrbitalPhysicsUnderThrustStart(
33+
IEnumerable<CodeInstruction> bodyToReplace
34+
)
35+
{
36+
var propGetSqrMagnitude = typeof(Vector3d).GetProperty("sqrMagnitude")!.GetGetMethod();
37+
var res = TranspilerHelper.Replace(
38+
bodyToReplace,
39+
[
40+
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Ldloca_S },
41+
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Call, Operand = propGetSqrMagnitude },
42+
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Ldc_R8 },
43+
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Bge_Un },
44+
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Ldloca_S },
45+
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Call, Operand = propGetSqrMagnitude },
46+
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Ldc_R8 },
47+
new TranspilerHelper.ILLookupKey { OpCode = OpCodes.Bge_Un },
48+
],
49+
oldBody =>
50+
{
51+
var checkNeedRoundingError = CheckNeedRoundingError;
52+
var instructions = oldBody.ToArray();
53+
return
54+
[
55+
instructions[0], instructions[1],
56+
instructions[4], instructions[5],
57+
new CodeInstruction(OpCodes.Call, checkNeedRoundingError.Method),
58+
new CodeInstruction(OpCodes.Ldc_I4_1),
59+
instructions[7], new CodeInstruction(OpCodes.Nop),
60+
];
61+
},
62+
1..1
63+
);
64+
65+
return res;
66+
}
67+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Reflection.Emit;
2+
using HarmonyLib;
3+
using JetBrains.Annotations;
4+
5+
namespace CommunityFixes.Fix.TimeWarpThrustFix;
6+
7+
internal static class TranspilerHelper
8+
{
9+
public sealed class ILLookupKey
10+
{
11+
public OpCode OpCode { get; set; }
12+
[CanBeNull] public object Operand { get; set; }
13+
}
14+
15+
public static IEnumerable<CodeInstruction> Replace(
16+
IEnumerable<CodeInstruction> oldBody,
17+
ILLookupKey[] lookupKeys,
18+
Func<IReadOnlyCollection<CodeInstruction>, IEnumerable<CodeInstruction>> repl,
19+
Range expectedTimes
20+
)
21+
{
22+
var queue = new Queue<CodeInstruction>(lookupKeys.Length);
23+
var foundCount = 0;
24+
25+
foreach (var instruction in oldBody)
26+
{
27+
queue.Enqueue(instruction);
28+
if (queue.Count < lookupKeys.Length) continue;
29+
30+
if (queue.Zip(lookupKeys, (instr, lookup) =>
31+
instr.opcode == lookup.OpCode &&
32+
(lookup.Operand is null || Equals(instr.operand, lookup.Operand))
33+
).All(b => b))
34+
{
35+
foundCount++;
36+
foreach (var modInstr in repl(queue))
37+
{
38+
yield return modInstr;
39+
}
40+
41+
queue.Clear();
42+
continue;
43+
}
44+
45+
yield return queue.Dequeue();
46+
}
47+
48+
foreach (var instr in queue)
49+
{
50+
yield return instr;
51+
}
52+
53+
if (foundCount < expectedTimes.Start.Value || foundCount > expectedTimes.End.Value)
54+
{
55+
throw new InvalidOperationException($"Found expected IL {foundCount} times, instead of {expectedTimes}");
56+
}
57+
}
58+
}

src/CommunityFixes/Fix/VesselLandedState/VesselLandedStateFix.cs renamed to src/CommunityFixes/Fix/VesselLandedStateFix/VesselLandedStateFix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using KSP.Sim.impl;
22

3-
namespace CommunityFixes.Fix.VesselLandedState;
3+
namespace CommunityFixes.Fix.VesselLandedStateFix;
44

55
[Fix("Vessel Landed State")]
66
public class VesselLandedStateFix: BaseFix

0 commit comments

Comments
 (0)