Skip to content

Commit

Permalink
Merge pull request #18 from Falki-git/main
Browse files Browse the repository at this point in the history
Preserve compatibility with SpaceWarp 1.0.1
  • Loading branch information
Falki-git authored Apr 4, 2023
2 parents 3fd9425 + b685002 commit 7067fb0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
6 changes: 5 additions & 1 deletion MicroEngineerProject/MicroEngineer/MicroEngineerMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public override void OnInitialized()
if (MicroWindows.Find(w => w.MainWindow == MainWindow.StageInfoOAB) == null)
InitializeStageInfoOABWindow();

// Preserve backward compatibility with SpaceWarp 1.0.1
if (MicroUtility.IsModOlderThan("SpaceWarp", 1, 1, 0))
MicroStyles.SetStylesForOldSpaceWarpSkin();

Appbar.RegisterAppButton(
"Micro Engineer",
"BTN-MicroEngineerBtn",
Expand Down Expand Up @@ -186,7 +190,7 @@ private void OnGUI()
GUI.skin = MicroStyles.SpaceWarpUISkin;

MicroUtility.RefreshGameManager();
if (MicroUtility.GameState.GameState == GameState.VehicleAssemblyBuilder)
if (MicroUtility.GameState?.GameState == GameState.VehicleAssemblyBuilder)
OnGUI_OAB();
else
OnGUI_Flight();
Expand Down
8 changes: 8 additions & 0 deletions MicroEngineerProject/MicroEngineer/MicroStyles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,13 @@ public static void DrawHorizontalLine(float height)
/// Draws a white horizontal line accross the container it's put in with height of 1 px
/// </summary>
public static void DrawHorizontalLine() { MicroStyles.DrawHorizontalLine(1); }

internal static void SetStylesForOldSpaceWarpSkin()
{
SectionToggleStyle = new GUIStyle(SpaceWarpUISkin.toggle)
{
margin = new RectOffset(0, 30, 0, 5)
};
}
}
}
58 changes: 58 additions & 0 deletions MicroEngineerProject/MicroEngineer/MicroUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using BepInEx.Logging;
using KSP.Messages;
using KSP.Sim.DeltaV;
using BepInEx.Bootstrap;
using SpaceWarp.API.Mods;

namespace MicroMod
{
Expand Down Expand Up @@ -257,6 +259,62 @@ internal static bool ToggleGameInputOnControlInFocus(bool gameInputState, bool s
return false;
}
}

internal static (int major, int minor, int patch)? GetModVersion(string modId)
{
var plugin = Chainloader.Plugins?.OfType<BaseSpaceWarpPlugin>().ToList().FirstOrDefault(p => p.SpaceWarpMetadata.ModID.ToLowerInvariant() == modId.ToLowerInvariant());
string versionString = plugin?.SpaceWarpMetadata?.Version;

string[] versionNumbers = versionString?.Split(new char[] { '.' }, 3);

if (versionNumbers != null && versionNumbers.Length >= 1)
{
int majorVersion = 0;
int minorVersion = 0;
int patchVersion = 0;

if (versionNumbers.Length >= 1)
int.TryParse(versionNumbers[0], out majorVersion);
if (versionNumbers.Length >= 2)
int.TryParse(versionNumbers[1], out minorVersion);
if (versionNumbers.Length == 3)
int.TryParse(versionNumbers[2], out patchVersion);

return (majorVersion, minorVersion, patchVersion);
}
else return null;
}

/// <summary>
/// Check if installed mod is older than the specified version
/// </summary>
/// <param name="modId">SpaceWarp mod ID</param>
/// <param name="major">Specified major version (X.0.0)</param>
/// <param name="minor">Specified minor version (0.X.0)</param>
/// <param name="patch">Specified patch version (0.0.X)</param>
/// <returns>True = installed mod is older. False = installed mod has the same version or it's newer or version isn't declared or version declared is gibberish that cannot be parsed</returns>
internal static bool IsModOlderThan (string modId, int major, int minor, int patch)
{
var modVersion = MicroUtility.GetModVersion(modId);

if (!modVersion.HasValue || modVersion.Value == (0, 0, 0))
return false;

if (modVersion.Value.Item1 < major)
return true;
else if (modVersion.Value.Item1 > major)
return false;

if (modVersion.Value.Item2 < minor)
return true;
else if (modVersion.Value.Item2 > minor)
return false;

if (modVersion.Value.Item3 < patch)
return true;
else
return false;
}
}

public static class AeroForces
Expand Down

0 comments on commit 7067fb0

Please sign in to comment.