Add GitHub plugin updater, refactor game update hook to TorchBase instead of PluginManager, fix world config not applying
This commit is contained in:
@@ -1,40 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using NLog;
|
||||
using Torch;
|
||||
using Sandbox;
|
||||
using Sandbox.Engine.Analytics;
|
||||
using Sandbox.Engine.Multiplayer;
|
||||
using Sandbox;
|
||||
using Sandbox.Engine.Utils;
|
||||
using Sandbox.Game;
|
||||
using Sandbox.Game.Gui;
|
||||
using Sandbox.Game.World;
|
||||
using SpaceEngineers.Game;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Xml.Serialization;
|
||||
using SteamSDK;
|
||||
using Torch.API;
|
||||
using VRage.Dedicated;
|
||||
using VRage.FileSystem;
|
||||
using VRage.Game;
|
||||
using VRage.Game.ModAPI;
|
||||
using VRage.Game.ObjectBuilder;
|
||||
using VRage.Game.SessionComponents;
|
||||
using VRage.Profiler;
|
||||
using VRage.Plugins;
|
||||
|
||||
namespace Torch.Server
|
||||
{
|
||||
public class TorchServer : TorchBase, ITorchServer
|
||||
{
|
||||
public Thread GameThread { get; private set; }
|
||||
public bool IsRunning { get; private set; }
|
||||
public TorchConfig Config { get; }
|
||||
public ServerState State { get; private set; }
|
||||
public string InstanceName => Config?.InstanceName;
|
||||
public string InstancePath => Config?.InstancePath;
|
||||
|
||||
@@ -49,7 +36,7 @@ namespace Torch.Server
|
||||
{
|
||||
base.Init();
|
||||
|
||||
Log.Info($"Init server instance '{Config.InstanceName}' at path '{Config.InstancePath}'");
|
||||
Log.Info($"Init server '{Config.InstanceName}' at '{Config.InstancePath}'");
|
||||
|
||||
MyFakes.ENABLE_INFINARIO = false;
|
||||
MyPerGameSettings.SendLogToKeen = false;
|
||||
@@ -60,21 +47,19 @@ namespace Torch.Server
|
||||
MySessionComponentExtDebug.ForceDisable = true;
|
||||
MyPerServerSettings.AppId = 244850;
|
||||
MyFinalBuildConstants.APP_VERSION = MyPerGameSettings.BasicGameInfo.GameVersion;
|
||||
//MyGlobalTypeMetadata.Static.Init();
|
||||
|
||||
//TODO: Allows players to filter servers for Torch in the server browser. Need to init Steam before this
|
||||
//SteamServerAPI.Instance.GameServer.SetKeyValue("SM", "Torch");
|
||||
}
|
||||
MyPlugins.RegisterGameAssemblyFile(MyPerGameSettings.GameModAssembly);
|
||||
MyPlugins.RegisterGameObjectBuildersAssemblyFile(MyPerGameSettings.GameModObjBuildersAssembly);
|
||||
MyPlugins.RegisterSandboxAssemblyFile(MyPerGameSettings.SandboxAssembly);
|
||||
MyPlugins.RegisterSandboxGameAssemblyFile(MyPerGameSettings.SandboxGameAssembly);
|
||||
MyPlugins.Load();
|
||||
|
||||
public void SetConfig(IMyConfigDedicated config)
|
||||
{
|
||||
MySandboxGame.ConfigDedicated = config;
|
||||
}
|
||||
MyGlobalTypeMetadata.Static.Init();
|
||||
MyInitializer.InvokeBeforeRun(
|
||||
MyPerServerSettings.AppId,
|
||||
MyPerServerSettings.GameDSName,
|
||||
InstancePath, DedicatedServer.AddDateToLog);
|
||||
|
||||
public void Start(IMyConfigDedicated config)
|
||||
{
|
||||
SetConfig(config);
|
||||
Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -82,26 +67,35 @@ namespace Torch.Server
|
||||
/// </summary>
|
||||
public override void Start()
|
||||
{
|
||||
if (IsRunning)
|
||||
if (State > 0)
|
||||
throw new InvalidOperationException("Server is already running.");
|
||||
|
||||
Config.Save();
|
||||
IsRunning = true;
|
||||
State = ServerState.Starting;
|
||||
Log.Info("Starting server.");
|
||||
|
||||
var runInternal = typeof(DedicatedServer).GetMethod("RunInternal", BindingFlags.Static | BindingFlags.NonPublic);
|
||||
|
||||
MySandboxGame.IsDedicated = true;
|
||||
Environment.SetEnvironmentVariable("SteamAppId", MyPerServerSettings.AppId.ToString());
|
||||
|
||||
Log.Trace("Invoking RunMain");
|
||||
try { Reflection.InvokeStaticMethod(typeof(DedicatedServer), "RunMain", Config.InstanceName, Config.InstancePath, false, true); }
|
||||
catch (Exception e)
|
||||
VRage.Service.ExitListenerSTA.OnExit += delegate { MySandboxGame.Static?.Exit(); };
|
||||
|
||||
do
|
||||
{
|
||||
Log.Error("Error running server.");
|
||||
Log.Error(e);
|
||||
throw;
|
||||
}
|
||||
Log.Trace("RunMain completed");
|
||||
IsRunning = false;
|
||||
runInternal.Invoke(null, null);
|
||||
} while (MySandboxGame.IsReloading);
|
||||
|
||||
MyInitializer.InvokeAfterRun();
|
||||
State = ServerState.Stopped;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Init(object gameInstance)
|
||||
{
|
||||
base.Init(gameInstance);
|
||||
State = ServerState.Running;
|
||||
SteamServerAPI.Instance.GameServer.SetKeyValue("SM", "Torch");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -109,7 +103,7 @@ namespace Torch.Server
|
||||
/// </summary>
|
||||
public override void Stop()
|
||||
{
|
||||
if (!IsRunning)
|
||||
if (State == ServerState.Stopped)
|
||||
Log.Error("Server is already stopped");
|
||||
|
||||
if (Thread.CurrentThread.ManagedThreadId != GameThread?.ManagedThreadId && MySandboxGame.Static.IsRunning)
|
||||
@@ -125,31 +119,14 @@ namespace Torch.Server
|
||||
|
||||
//Unload all the static junk.
|
||||
//TODO: Finish unloading all server data so it's in a completely clean state.
|
||||
VRage.FileSystem.MyFileSystem.Reset();
|
||||
MyFileSystem.Reset();
|
||||
VRage.Input.MyGuiGameControlsHelpers.Reset();
|
||||
VRage.Input.MyInput.UnloadData();
|
||||
//CleanupProfilers();
|
||||
|
||||
Log.Info("Server stopped.");
|
||||
_stopHandle.Set();
|
||||
IsRunning = false;
|
||||
State = ServerState.Stopped;
|
||||
}
|
||||
|
||||
/*
|
||||
private string GetInstancePath(bool isService = false, string instanceName = "Torch")
|
||||
{
|
||||
if (isService)
|
||||
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), MyPerServerSettings.GameDSName, instanceName);
|
||||
|
||||
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), MyPerServerSettings.GameDSName);
|
||||
}*/
|
||||
|
||||
/*
|
||||
private void CleanupProfilers()
|
||||
{
|
||||
typeof(MyRenderProfiler).GetField("m_threadProfiler", BindingFlags.Static | BindingFlags.NonPublic).SetValue(null, null);
|
||||
typeof(MyRenderProfiler).GetField("m_gpuProfiler", BindingFlags.Static | BindingFlags.NonPublic).SetValue(null, null);
|
||||
(typeof(MyRenderProfiler).GetField("m_threadProfilers", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null) as List<MyProfiler>).Clear();
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user