Skip to content

Commit b685002

Browse files
committed
Preserve compatibility with SpaceWarp 1.0.1
1 parent 675882c commit b685002

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

MicroEngineerProject/MicroEngineer/MicroEngineerMod.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public override void OnInitialized()
6161
if (MicroWindows.Find(w => w.MainWindow == MainWindow.StageInfoOAB) == null)
6262
InitializeStageInfoOABWindow();
6363

64+
// Preserve backward compatibility with SpaceWarp 1.0.1
65+
if (MicroUtility.IsModOlderThan("SpaceWarp", 1, 1, 0))
66+
MicroStyles.SetStylesForOldSpaceWarpSkin();
67+
6468
Appbar.RegisterAppButton(
6569
"Micro Engineer",
6670
"BTN-MicroEngineerBtn",

MicroEngineerProject/MicroEngineer/MicroStyles.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,13 @@ public static void DrawHorizontalLine(float height)
214214
/// Draws a white horizontal line accross the container it's put in with height of 1 px
215215
/// </summary>
216216
public static void DrawHorizontalLine() { MicroStyles.DrawHorizontalLine(1); }
217+
218+
internal static void SetStylesForOldSpaceWarpSkin()
219+
{
220+
SectionToggleStyle = new GUIStyle(SpaceWarpUISkin.toggle)
221+
{
222+
margin = new RectOffset(0, 30, 0, 5)
223+
};
224+
}
217225
}
218226
}

MicroEngineerProject/MicroEngineer/MicroUtility.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using BepInEx.Logging;
1111
using KSP.Messages;
1212
using KSP.Sim.DeltaV;
13+
using BepInEx.Bootstrap;
14+
using SpaceWarp.API.Mods;
1315

1416
namespace MicroMod
1517
{
@@ -257,6 +259,62 @@ internal static bool ToggleGameInputOnControlInFocus(bool gameInputState, bool s
257259
return false;
258260
}
259261
}
262+
263+
internal static (int major, int minor, int patch)? GetModVersion(string modId)
264+
{
265+
var plugin = Chainloader.Plugins?.OfType<BaseSpaceWarpPlugin>().ToList().FirstOrDefault(p => p.SpaceWarpMetadata.ModID.ToLowerInvariant() == modId.ToLowerInvariant());
266+
string versionString = plugin?.SpaceWarpMetadata?.Version;
267+
268+
string[] versionNumbers = versionString?.Split(new char[] { '.' }, 3);
269+
270+
if (versionNumbers != null && versionNumbers.Length >= 1)
271+
{
272+
int majorVersion = 0;
273+
int minorVersion = 0;
274+
int patchVersion = 0;
275+
276+
if (versionNumbers.Length >= 1)
277+
int.TryParse(versionNumbers[0], out majorVersion);
278+
if (versionNumbers.Length >= 2)
279+
int.TryParse(versionNumbers[1], out minorVersion);
280+
if (versionNumbers.Length == 3)
281+
int.TryParse(versionNumbers[2], out patchVersion);
282+
283+
return (majorVersion, minorVersion, patchVersion);
284+
}
285+
else return null;
286+
}
287+
288+
/// <summary>
289+
/// Check if installed mod is older than the specified version
290+
/// </summary>
291+
/// <param name="modId">SpaceWarp mod ID</param>
292+
/// <param name="major">Specified major version (X.0.0)</param>
293+
/// <param name="minor">Specified minor version (0.X.0)</param>
294+
/// <param name="patch">Specified patch version (0.0.X)</param>
295+
/// <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>
296+
internal static bool IsModOlderThan (string modId, int major, int minor, int patch)
297+
{
298+
var modVersion = MicroUtility.GetModVersion(modId);
299+
300+
if (!modVersion.HasValue || modVersion.Value == (0, 0, 0))
301+
return false;
302+
303+
if (modVersion.Value.Item1 < major)
304+
return true;
305+
else if (modVersion.Value.Item1 > major)
306+
return false;
307+
308+
if (modVersion.Value.Item2 < minor)
309+
return true;
310+
else if (modVersion.Value.Item2 > minor)
311+
return false;
312+
313+
if (modVersion.Value.Item3 < patch)
314+
return true;
315+
else
316+
return false;
317+
}
260318
}
261319

262320
public static class AeroForces

0 commit comments

Comments
 (0)