diff --git a/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll b/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll index 24baa458..ffbba4cf 100644 Binary files a/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll and b/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll differ diff --git a/Source/Restock/InstallChecker.cs b/Source/Restock/InstallChecker.cs new file mode 100644 index 00000000..13e1c453 --- /dev/null +++ b/Source/Restock/InstallChecker.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using UnityEngine; + +namespace Restock +{ + [KSPAddon(KSPAddon.Startup.Instantly, true)] + public class InstallChecker : MonoBehaviour + { + public readonly IEnumerable UNEXPECTED_URLS = Array.AsReadOnly(new[] { + "Squad/Parts/FuelTank/fuelTankT100", + "Squad/Parts/FuelTank/fuelTankT200", + "Squad/Parts/FuelTank/fuelTankT400", + "Squad/Parts/FuelTank/fuelTankT800", + }); + + private void Start() + { + string[] errorMessages = CheckInstallLocation().Concat(CheckUnexpectedUrls()).ToArray(); + + if (errorMessages.Length > 0) CreateWarningDialog(errorMessages); + } + + private IEnumerable CheckInstallLocation() + { + UrlDir gameData = GameDatabase.Instance.root.children.First(dir => dir.type == UrlDir.DirectoryType.GameData); + AssemblyLoader.LoadedAssembly loadedAssembly = AssemblyLoader.loadedAssemblies.GetByAssembly(Assembly.GetExecutingAssembly()); + + if (loadedAssembly == null) + { + Debug.LogError("[Restock] Error checking install location - could not find loaded assembly"); + yield break; + } + + UrlDir assemblyDir = GetDirectory(gameData, loadedAssembly.url); + + if (assemblyDir == null) + { + Debug.LogError("[Restock] Error checking install location - could not find assembly url directory: " + loadedAssembly.url); + yield break; + } + + string observedInstallPath = Path.GetFullPath(assemblyDir.parent.path); + string expectedInstallPath = Path.GetFullPath(Path.Combine(KSPUtil.ApplicationRootPath, Path.Combine("GameData", "ReStock"))); + + if (observedInstallPath != expectedInstallPath) + { + Debug.LogError($"[Restock] Install found at '{observedInstallPath}'"); + yield return $"Expected Restock to be installed at\n{expectedInstallPath}\nbut actually installed at\n{observedInstallPath}"; + } + } + + private IEnumerable CheckUnexpectedUrls() + { + UrlDir gameData = GameDatabase.Instance.root.children.First(dir => dir.type == UrlDir.DirectoryType.GameData); + foreach (string unexpectedUrl in UNEXPECTED_URLS) + { + if (!(gameData.GetDirectory(unexpectedUrl) is UrlDir unexpectedDir)) continue; + Debug.LogError("[ReStock] Found unexpected directory " + unexpectedDir.path); + yield return $"Found unexpected directory, likely left over from an older version of KSP:\n" + unexpectedDir.path; + } + } + + private void CreateWarningDialog(params string[] allMessages) + { + PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), + new Vector2(0.5f, 0.5f), + new MultiOptionDialog( + "RestockInstallWarning", + $"Restock has detected installation issues:\n\n{string.Join("\n\n", allMessages)}", + "Restock - installation issues detected", + HighLogic.UISkin, + new Rect(0.5f, 0.5f, 500f, 60f), + new DialogGUIFlexibleSpace(), + new DialogGUIHorizontalLayout( + new DialogGUIFlexibleSpace(), + new DialogGUIButton("OK", delegate () { }, 140.0f, 30.0f, true), + new DialogGUIFlexibleSpace() + ) + ), + true, + HighLogic.UISkin); + } + + private UrlDir GetDirectory(UrlDir baseUrl, string url) + { + string[] splitUrl = url.Trim().Split('/'); + + UrlDir currentDir = baseUrl; + foreach (string dirName in splitUrl) + { + currentDir = currentDir.children.FirstOrDefault(dir => dir.name == dirName); + if (currentDir == null) return null; + } + return currentDir; + } + } +} diff --git a/Source/Restock/Restock.csproj b/Source/Restock/Restock.csproj index 9989e844..69ebd600 100644 --- a/Source/Restock/Restock.csproj +++ b/Source/Restock/Restock.csproj @@ -41,6 +41,7 @@ +