From 7ea982c903335d0aa77a946c3208e14fe6af26b2 Mon Sep 17 00:00:00 2001 From: Westin Miller Date: Sat, 25 Nov 2017 17:44:50 -0800 Subject: [PATCH] Future methods for loading/unloading sessions. Fix reflected manager to work with instance methods on internal types. --- Torch/Utils/Reflected/ReflectedManager.cs | 6 +++- Torch/VRageGame.cs | 42 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Torch/Utils/Reflected/ReflectedManager.cs b/Torch/Utils/Reflected/ReflectedManager.cs index 08eb4a1..f23fa2a 100644 --- a/Torch/Utils/Reflected/ReflectedManager.cs +++ b/Torch/Utils/Reflected/ReflectedManager.cs @@ -237,8 +237,12 @@ namespace Torch.Utils argExp[i] = Expression.Convert(paramExp[i + 1], invokeTypes[i]); else argExp[i] = paramExp[i + 1]; + Debug.Assert(methodInstance.DeclaringType != null); + Expression instanceExp = paramExp[0].Type != methodInstance.DeclaringType + ? Expression.Convert(paramExp[0], methodInstance.DeclaringType) + : (Expression) paramExp[0]; field.SetValue(null, - Expression.Lambda(Expression.Call(paramExp[0], methodInstance, argExp), paramExp) + Expression.Lambda(Expression.Call(instanceExp, methodInstance, argExp), paramExp) .Compile()); _log.Trace($"Reflecting field {field.DeclaringType?.FullName}#{field.Name} with {methodInstance.DeclaringType?.FullName}#{methodInstance.Name}"); } diff --git a/Torch/VRageGame.cs b/Torch/VRageGame.cs index e43b8ee..3da6e83 100644 --- a/Torch/VRageGame.cs +++ b/Torch/VRageGame.cs @@ -10,13 +10,16 @@ using Havok; using NLog; using NLog.Fluent; using Sandbox; +using Sandbox.Engine.Multiplayer; using Sandbox.Engine.Networking; using Sandbox.Engine.Platform.VideoMode; using Sandbox.Game; +using Sandbox.Game.World; using SpaceEngineers.Game; using SpaceEngineers.Game.GUI; using Torch.Utils; using VRage; +using VRage.Audio; using VRage.FileSystem; using VRage.Game; using VRage.Game.SessionComponents; @@ -35,6 +38,17 @@ namespace Torch #pragma warning disable 649 [ReflectedGetter(Name = "m_plugins", Type = typeof(MyPlugins))] private static readonly Func> _getVRagePluginList; + + [ReflectedGetter(Name = "Static", TypeName = "Sandbox.Game.Audio.MyMusicController, Sandbox.Game")] + private static readonly Func _getMusicControllerStatic; + + + [ReflectedSetter(Name = "Static", TypeName = "Sandbox.Game.Audio.MyMusicController, Sandbox.Game")] + private static readonly Action _setMusicControllerStatic; + + + [ReflectedMethod(Name = "Unload", TypeName = "Sandbox.Game.Audio.MyMusicController, Sandbox.Game")] + private static readonly Action _musicControllerUnload; #pragma warning restore 649 private readonly TorchBase _torch; @@ -210,6 +224,34 @@ namespace Torch } } + private void LoadSession(string sessionPath) + { + // ? + MySessionLoader.LoadSingleplayerSession(sessionPath); + } + + private void UnloadSession() + { + if (MySession.Static != null) + { + MySession.Static.Unload(); + MySession.Static = null; + } + { + var musicCtl = _getMusicControllerStatic(); + if (musicCtl != null) + { + _musicControllerUnload(musicCtl); + _setMusicControllerStatic(null); + MyAudio.Static.MusicAllowed = true; + } + } + if (MyMultiplayer.Static != null) + { + MyMultiplayer.Static.Dispose(); + } + } + private void DoStop() { ParallelTasks.Parallel.Scheduler.WaitForTasksToFinish(TimeSpan.FromSeconds(10.0));