From 8c4ff002b0804b5033d54a1d920e5aae7539e7ec Mon Sep 17 00:00:00 2001 From: Digi Date: Sun, 26 Jun 2022 10:36:04 +0300 Subject: [PATCH] Initial version --- .gitattributes | 2 + .gitignore | 398 +++++++++++++++++++++++++++++++++++++ PluginReloader.cs | 171 ++++++++++++++++ PluginReloader.csproj | 133 +++++++++++++ PluginReloader.sln | 31 +++ Properties/AssemblyInfo.cs | 36 ++++ README.md | 9 + 7 files changed, 780 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 PluginReloader.cs create mode 100644 PluginReloader.csproj create mode 100644 PluginReloader.sln create mode 100644 Properties/AssemblyInfo.cs create mode 100644 README.md diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..426d76d --- /dev/null +++ b/.gitignore @@ -0,0 +1,398 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files +*.ncb +*.aps + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +*.sln.iml diff --git a/PluginReloader.cs b/PluginReloader.cs new file mode 100644 index 0000000..a9f618b --- /dev/null +++ b/PluginReloader.cs @@ -0,0 +1,171 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using Sandbox.ModAPI; +using VRage.FileSystem; +using VRage.Game; +using VRage.Input; +using VRage.Plugins; +using VRage.Utils; + +namespace Digi.PluginReloader +{ + public class PluginReloaderPlugin : IPlugin, IHandleInputPlugin + { + const string PluginsPath = "DynamicPlugins"; // relative to appdata folder + + object GameInstance; + + readonly List Plugins = new List(); + + public void Init(object gameInstance) + { + GameInstance = gameInstance; + + MyLog.Default.WriteLine("PluginReloader initialized, press Ctrl+Alt+L ingame to reload plugins"); + MyLog.Default.Flush(); + + LoadPlugins(); + } + + public void Dispose() + { + UnloadPlugins(); + GameInstance = null; + } + + public void Update() + { + try + { + if(MyInput.Static == null) + return; + + if(MyInput.Static.IsNewKeyPressed(MyKeys.L) && MyInput.Static.IsAnyAltKeyPressed() && MyInput.Static.IsAnyCtrlKeyPressed()) + { + UnloadPlugins(); + LoadPlugins(); + + // I don't really know what I'm doing here xD + GC.Collect(); + GC.WaitForFullGCComplete(); + GC.Collect(); + } + + foreach(IPlugin plugin in Plugins) + { + try + { + plugin.Update(); + } + catch(Exception e) + { + LogError(e); + } + } + } + catch(Exception e) + { + LogError(e); + } + } + + public void HandleInput() + { + try + { + foreach(IPlugin plugin in Plugins) + { + try + { + IHandleInputPlugin hip = plugin as IHandleInputPlugin; + hip?.HandleInput(); + } + catch(Exception e) + { + LogError(e); + } + } + } + catch(Exception e) + { + LogError(e); + } + } + + void LoadPlugins() + { + string dir = Path.Combine(MyFileSystem.UserDataPath, PluginsPath); + + if(!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + return; + } + + string[] fileNames = Directory.GetFiles(dir, "*.dll", SearchOption.TopDirectoryOnly); + + foreach(string filePath in fileNames) + { + byte[] bytes = File.ReadAllBytes(filePath); + Assembly assembly = Assembly.Load(bytes); + CreateInstances(assembly); + } + } + + void CreateInstances(Assembly assembly) + { + foreach(Type type in assembly.GetTypes()) + { + try + { + if(type.IsAbstract) + continue; + + if(!type.GetInterfaces().Contains(typeof(IPlugin))) + continue; + + if(type.Name == nameof(PluginReloaderPlugin)) + continue; + + IPlugin plugin = (IPlugin)assembly.CreateInstance(type.FullName); + plugin.Init(GameInstance); + Plugins.Add(plugin); + + string msg = $"PluginReloader loaded plugin: {plugin}"; + MyAPIGateway.Utilities?.ShowNotification(msg, 2000, MyFontEnum.Green); + MyLog.Default.WriteLine(msg); + } + catch(Exception ex) + { + LogError($"Cannot create instance of '{type.FullName}': {ex.ToString()}"); + } + } + } + + void UnloadPlugins() + { + foreach(IPlugin plugin in Plugins) + { + plugin.Dispose(); + } + + Plugins.Clear(); + } + + void LogError(string text) + { + string message = $"PluginReloader error: {text}"; + MyLog.Default.WriteLine(message); + MyAPIGateway.Utilities?.ShowNotification(message, 10000, MyFontEnum.Red); + } + + void LogError(Exception e) + { + MyLog.Default.WriteLine($"PluginReloader error: {e.ToString()}"); + MyAPIGateway.Utilities?.ShowNotification($"PluginReloader error: {e.Message}", 10000, MyFontEnum.Red); + } + } +} diff --git a/PluginReloader.csproj b/PluginReloader.csproj new file mode 100644 index 0000000..b2c4e2a --- /dev/null +++ b/PluginReloader.csproj @@ -0,0 +1,133 @@ + + + + + Debug + AnyCPU + {DE28D939-6429-4E3D-BD38-7D5BDD0C2258} + Library + Properties + PluginReloader + PluginReloader + v4.8 + 512 + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + x64 + 6 + + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + prompt + MinimumRecommendedRules.ruleset + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 6 + prompt + MinimumRecommendedRules.ruleset + + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.Common.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.Game.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.Graphics.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.RenderDirect.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\SpaceEngineers.Game.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\SpaceEngineers.ObjectBuilders.dll + False + + + + + + + + + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Audio.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Game.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Input.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Library.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Math.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Render.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Render11.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Scripting.dll + False + + + C:\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Steam.dll + False + + + + + + + + \ No newline at end of file diff --git a/PluginReloader.sln b/PluginReloader.sln new file mode 100644 index 0000000..4e5fca8 --- /dev/null +++ b/PluginReloader.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27004.2010 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PluginReloader", "PluginReloader.csproj", "{69591029-510B-408E-8FD0-BEFDC46E45C8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {69591029-510B-408E-8FD0-BEFDC46E45C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {69591029-510B-408E-8FD0-BEFDC46E45C8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {69591029-510B-408E-8FD0-BEFDC46E45C8}.Debug|x64.ActiveCfg = Debug|x64 + {69591029-510B-408E-8FD0-BEFDC46E45C8}.Debug|x64.Build.0 = Debug|x64 + {69591029-510B-408E-8FD0-BEFDC46E45C8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {69591029-510B-408E-8FD0-BEFDC46E45C8}.Release|Any CPU.Build.0 = Release|Any CPU + {69591029-510B-408E-8FD0-BEFDC46E45C8}.Release|x64.ActiveCfg = Release|x64 + {69591029-510B-408E-8FD0-BEFDC46E45C8}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1E5B475B-621C-4046-A264-8C19499D37AE} + EndGlobalSection +EndGlobal diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..de4b125 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Plugin Reloader")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Plugin Reloader")] +[assembly: AssemblyCopyright("Copyright © Digi")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("de28d939-6429-4e3d-bd38-7d5bdd0c2258")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/README.md b/README.md new file mode 100644 index 0000000..0de33ad --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Plugin Reloader + +This is primarily for developing plugins as I doubt plugins out there expect to be disposed while game is still running or initialized while a world is already running. + +It will use %appdata%/SpaceEngineers/DynamicPlugins/ folder to load&reload from. + +In-game use Ctrl+Alt+L to trigger a reload of the plugins from that folder. + +It does not mess with any plugins loaded by other means (game, plugin loader, etc). \ No newline at end of file