From 3a196e18bffced0e75c54c08251b6d297a31da53 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Fri, 9 Jul 2021 15:51:31 -0400 Subject: [PATCH] Implement CRF robot importer and import framework --- NPort/API/SimpleImporter.cs | 2 +- NPort/Importers/RobocraftFactoryImporter.cs | 2 +- NPort/Installer/Updater.cs | 56 +++++++++++++++++++++ NPort/Installer/Utility.cs | 15 ++++++ NPort/NPort.csproj | 18 +++---- NPort/NPortPlugin.cs | 26 ++++++++-- 6 files changed, 101 insertions(+), 18 deletions(-) create mode 100644 NPort/Installer/Updater.cs create mode 100644 NPort/Installer/Utility.cs diff --git a/NPort/API/SimpleImporter.cs b/NPort/API/SimpleImporter.cs index c7ea643..e88fd21 100644 --- a/NPort/API/SimpleImporter.cs +++ b/NPort/API/SimpleImporter.cs @@ -30,7 +30,7 @@ namespace NPort.API Block placedBlock = Block.PlaceNew(type, position); placedBlock.Color = colour; placedBlock.Rotation = rotation; - //placedBlock.Material = material; + placedBlock.Material = material; } public abstract bool SimpleImport(string[] args); diff --git a/NPort/Importers/RobocraftFactoryImporter.cs b/NPort/Importers/RobocraftFactoryImporter.cs index 4faccb4..6c49ef2 100644 --- a/NPort/Importers/RobocraftFactoryImporter.cs +++ b/NPort/Importers/RobocraftFactoryImporter.cs @@ -49,7 +49,7 @@ namespace NPort.Importers float3 rotation = TranslateBlockRotation(cube.orientation); float3 position = new float3(cube.x - BAY_CENTER, cube.y - 8f , cube.z - BAY_CENTER); BlockColor colour = QuantizeToBlockColor(cube.colour); - PlaceBlock(BlockIDs.Cube, position * 0.2f, rotation, colour, BlockMaterial.SteelBodywork); + PlaceBlock(BlockIDs.Cube, position * 0.2f, rotation, colour, BlockMaterial.RigidSteel); //break; } return true; diff --git a/NPort/Installer/Updater.cs b/NPort/Installer/Updater.cs new file mode 100644 index 0000000..610da5c --- /dev/null +++ b/NPort/Installer/Updater.cs @@ -0,0 +1,56 @@ +using System.Security.Cryptography; +using System.IO; + +namespace NPort.Installer +{ + // if I could statically link libfj inside this DLL, this would not need to exist + public class Updater + { + internal static void CheckForUpdate() + { + // calculate checksums + string newChecksum; + using (Stream f = Utility.GetEmbeddedLibFj()) + { + newChecksum = Checksum(f); + } + + string oldChecksum = ""; + if (File.Exists(Utility.LIBFJ_INSTALL_PATH)) + { + using (Stream f = File.OpenRead(Utility.LIBFJ_INSTALL_PATH)) + { + oldChecksum = Checksum(f); + } + } + + if (newChecksum != oldChecksum) + { + // update required + using (Stream outfile = File.Create(Utility.LIBFJ_INSTALL_PATH)) + { + using (Stream resource = Utility.GetEmbeddedLibFj()) + { + resource.CopyTo(outfile); + } + } + } + } + + internal static void Remove() + { + if (File.Exists(Utility.LIBFJ_INSTALL_PATH)) + { + File.Delete(Utility.LIBFJ_INSTALL_PATH); + } + } + + private static string Checksum(Stream stream) + { + using (MD5 md5 = MD5.Create()) + { + return System.Convert.ToBase64String(md5.ComputeHash(stream)); + } + } + } +} \ No newline at end of file diff --git a/NPort/Installer/Utility.cs b/NPort/Installer/Utility.cs new file mode 100644 index 0000000..429b3d7 --- /dev/null +++ b/NPort/Installer/Utility.cs @@ -0,0 +1,15 @@ +using System.IO; +using System.Reflection; + +namespace NPort.Installer +{ + public static class Utility + { + public const string LIBFJ_RESOURCE_NAME = "NPort.libfj.dll"; + + public const string LIBFJ_INSTALL_PATH = "TechbloxPreview_Data/Plugins/x86_64/libfj.dll"; + + public static Stream GetEmbeddedLibFj() => + Assembly.GetExecutingAssembly().GetManifestResourceStream(LIBFJ_RESOURCE_NAME); + } +} \ No newline at end of file diff --git a/NPort/NPort.csproj b/NPort/NPort.csproj index 34f0986..0e36852 100644 --- a/NPort/NPort.csproj +++ b/NPort/NPort.csproj @@ -485,10 +485,6 @@ ..\ref\TechbloxPreview_Data\Managed\RobocraftX.GUI.Inventory.dll ..\..\ref\TechbloxPreview_Data\Managed\RobocraftX.GUI.Inventory.dll - - ..\ref\TechbloxPreview_Data\Managed\RobocraftX.GUI.RemoveBlock.dll - ..\..\ref\TechbloxPreview_Data\Managed\RobocraftX.GUI.RemoveBlock.dll - ..\ref\TechbloxPreview_Data\Managed\RobocraftX.GUI.ScaleGhost.dll ..\..\ref\TechbloxPreview_Data\Managed\RobocraftX.GUI.ScaleGhost.dll @@ -653,10 +649,6 @@ ..\ref\TechbloxPreview_Data\Managed\Techblox.InputCapture.dll ..\..\ref\TechbloxPreview_Data\Managed\Techblox.InputCapture.dll - - ..\ref\TechbloxPreview_Data\Managed\Techblox.MouseCursor.dll - ..\..\ref\TechbloxPreview_Data\Managed\Techblox.MouseCursor.dll - ..\ref\TechbloxPreview_Data\Managed\Techblox.SwitchAnimation.dll ..\..\ref\TechbloxPreview_Data\Managed\Techblox.SwitchAnimation.dll @@ -1082,9 +1074,13 @@ ..\..\ref\TechbloxPreview_Data\Managed\VisualProfiler.dll - - - + + + + + + + diff --git a/NPort/NPortPlugin.cs b/NPort/NPortPlugin.cs index 231b0e1..279d6d8 100644 --- a/NPort/NPortPlugin.cs +++ b/NPort/NPortPlugin.cs @@ -1,4 +1,5 @@ -using System.Reflection; +using System.Linq; +using System.Reflection; using IllusionPlugin; using TechbloxModdingAPI.Utility; @@ -14,11 +15,19 @@ namespace NPort public override string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString(); + private bool _isEditingCache = false; + // called when Gamecraft shuts down public override void OnApplicationQuit() { // Shutdown this mod - Logging.LogDebug($"{Name} has shutdown"); + string[] args = System.Environment.GetCommandLineArgs(); + if (args.Contains("-nport-uninstall") || args.Contains("-unfuckify")) + { + Installer.Updater.Remove(); + } + + Logging.LogDebug($"{Name} has shutdown"); // Shutdown the Gamecraft modding API last Main.Shutdown(); @@ -27,12 +36,16 @@ namespace NPort // called when Gamecraft starts up public override void OnApplicationStart() { - // Initialize the Gamecraft modding API first + // Initialize the Gamecraft modding API first Main.Init(); // check out the modding API docs here: https://mod.exmods.org/ // Initialize this mod - + Installer.Updater.CheckForUpdate(); // install/update libfj.dll + TechbloxModdingAPI.App.Game.Enter += (sender, args) => { this._isEditingCache = true; }; + TechbloxModdingAPI.App.Game.Edit += (sender, args) => { this._isEditingCache = true; }; + TechbloxModdingAPI.App.Game.Simulate += (sender, args) => { this._isEditingCache = false; }; + TechbloxModdingAPI.App.Game.Exit += (sender, args) => { this._isEditingCache = false; }; Logging.MetaLog($"{Name} has started up"); #if DEBUG // TEST @@ -53,7 +66,8 @@ namespace NPort private string[] _argNames = { }; public override void OnGUI() - { + { + if (!this._isEditingCache) return; #if DEBUG // testing if (GUILayout.Button("Get PIG!!! OINK!")) @@ -67,6 +81,8 @@ namespace NPort _selectedItem = GUILayout.Toolbar(_selectedItem, API.ImportRegistry.Names); if (_selectedItem >= 0) { + // show help message + GUILayout.Box(API.ImportRegistry.Help(_selectedItem), GUILayout.MinWidth(128f), GUILayout.ExpandHeight(true)); // get arguments for selected importer _argNames = API.ImportRegistry.Args(_selectedItem); // grow/shrink _argCache when needed