Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-bures committed Aug 10, 2024
2 parents 35be585 + aca4784 commit 49bfb17
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 3 deletions.
39 changes: 38 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ 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:
Expand Down Expand Up @@ -60,4 +66,35 @@ jobs:
mod_id: ${{ env.SPACEDOCK_MOD_ID }}
version: ${{ env.version }}
zipball: ${{ env.zip }}
changelog: ./changelog.md
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/discord-webhook@v5.3.0
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
10 changes: 10 additions & 0 deletions src/CommunityFixes/Fix/SaveLoadDateTimeFix/SaveLoadDateTimeFix.cs
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));
}
}
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 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 49bfb17

Please sign in to comment.