expose custom config to plugins

add support for some basic game parameters to be set via custom config
This commit is contained in:
zznty
2023-06-29 14:08:45 +07:00
parent ed694ae95b
commit 3be524d169
10 changed files with 81 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Torch.API.Managers;
using Torch.API.Session;
using VRage.Game.ModAPI;
@@ -26,6 +27,11 @@ namespace Torch.API
/// </summary>
ITorchConfig Config { get; }
/// <summary>
/// Extended Configuration for the current instance.
/// </summary>
IConfiguration Configuration { get; }
/// <inheritdoc cref="IPluginManager"/>
[Obsolete]
IPluginManager Plugins { get; }

View File

@@ -17,6 +17,7 @@
<ItemGroup>
<PackageReference Include="JorgeSerrano.Json.JsonSnakeCaseNamingPolicy" Version="0.9.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="NLog" Version="5.1.0" />
<PackageReference Include="NuGet.Commands" Version="6.4.0" />
<PackageReference Include="NuGet.DependencyResolver.Core" Version="6.4.0" />

View File

@@ -8,6 +8,15 @@
"resolved": "0.9.0",
"contentHash": "xCqODS+wzpUXNtg4bMMvXG5PLbP0iTwRzRn2R+zWHKm83E6tbV2bCagawXp1EnZeNpd5OXpMxehulZWns8efzQ=="
},
"Microsoft.Extensions.Configuration.Binder": {
"type": "Direct",
"requested": "[7.0.4, )",
"resolved": "7.0.4",
"contentHash": "8+XPvJnHZsYgHOQlcMuQe7QNF5KdVKHH1F/wW3nd8/u81Gk/XFAYMDP0Lpz18h7/AM95M662vvqMorcYxCBB4w==",
"dependencies": {
"Microsoft.Extensions.Configuration.Abstractions": "7.0.0"
}
},
"NLog": {
"type": "Direct",
"requested": "[5.1.0, )",
@@ -64,6 +73,19 @@
"resolved": "6.0.0",
"contentHash": "UcSjPsst+DfAdJGVDsu346FX0ci0ah+lw3WRtn18NUwEqRt70HaOQ7lI72vy3+1LxtqI3T5GWwV39rQSrCzAeg=="
},
"Microsoft.Extensions.Configuration.Abstractions": {
"type": "Transitive",
"resolved": "7.0.0",
"contentHash": "f34u2eaqIjNO9YLHBz8rozVZ+TcFiFs0F3r7nUJd7FRkVSxk8u4OpoK226mi49MwexHOR2ibP9MFvRUaLilcQQ==",
"dependencies": {
"Microsoft.Extensions.Primitives": "7.0.0"
}
},
"Microsoft.Extensions.Primitives": {
"type": "Transitive",
"resolved": "7.0.0",
"contentHash": "um1KU5kxcRp3CNuI8o/GrZtD4AIOXDk+RLsytjZ9QPok3ttLUelLKpilVPuaFT3TFjOhSibUAso0odbOaCDj3Q=="
},
"Newtonsoft.Json": {
"type": "Transitive",
"resolved": "13.0.1",

View File

@@ -85,9 +85,9 @@ namespace Torch.Server
return true;
}
public void Run()
public void Run(IConfiguration configuration)
{
_server = new TorchServer(Config, ApplicationContext.Current.InstanceDirectory.FullName, ApplicationContext.Current.InstanceName);
_server = new TorchServer(Config, ApplicationContext.Current.InstanceDirectory.FullName, ApplicationContext.Current.InstanceName, configuration);
if (ApplicationContext.Current.IsService || Config.NoGui)
{

View File

@@ -39,7 +39,7 @@ namespace Torch.Server
context.GameBinariesDirectory.FullName);
#endif
initializer.Run();
initializer.Run(configuration);
}
private static void SetupLogging(IApplicationContext context, IConfiguration configuration)

View File

@@ -8,6 +8,7 @@ using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Diagnostics.Runtime;
using Microsoft.Extensions.Configuration;
using NLog;
using PropertyChanged;
using Sandbox;
@@ -46,8 +47,9 @@ namespace Torch.Server
//Here to trigger rebuild
/// <inheritdoc />
public TorchServer(ITorchConfig config, string instancePath, string instanceName) : base(config)
public TorchServer(ITorchConfig config, string instancePath, string instanceName, IConfiguration configuration) : base(config)
{
Configuration = configuration;
InstancePath = instancePath;
InstanceName = instanceName;
DedicatedInstance = new InstanceManager(this);
@@ -136,6 +138,8 @@ namespace Torch.Server
Log.Info($"Initialized server '{InstanceName}' at '{InstancePath}'");
}
public override IConfiguration Configuration { get; }
/// <inheritdoc />
public override void Start()
{

View File

@@ -601,6 +601,7 @@
"type": "Project",
"dependencies": {
"JorgeSerrano.Json.JsonSnakeCaseNamingPolicy": "[0.9.0, )",
"Microsoft.Extensions.Configuration.Binder": "[7.0.4, )",
"NLog": "[5.1.0, )",
"NuGet.Commands": "[6.4.0, )",
"NuGet.DependencyResolver.Core": "[6.4.0, )",

View File

@@ -5,6 +5,7 @@ using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using NLog;
using Sandbox;
using Sandbox.Game;
@@ -59,6 +60,8 @@ namespace Torch
/// <inheritdoc />
public ITorchConfig Config { get; protected set; }
public abstract IConfiguration Configuration { get; }
/// <inheritdoc />
public SemanticVersioning.Version TorchVersion { get; }
public string InstancePath { get; protected init;}

View File

@@ -8,6 +8,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Havok;
using Microsoft.Extensions.Configuration;
using NLog;
using NLog.Fluent;
using Sandbox;
@@ -30,6 +31,7 @@ using VRage.Dedicated;
using VRage.EOS;
using VRage.FileSystem;
using VRage.Game;
using VRage.Game.ModAPI;
using VRage.Game.ObjectBuilder;
using VRage.Game.SessionComponents;
using VRage.GameServices;
@@ -169,8 +171,11 @@ namespace Torch
MyInitializer.InvokeBeforeRun(_appSteamId, _appName, MyVRage.Platform.System.GetRootPath(), _userDataPath);
_log.Info("Loading Dedicated Config");
// object created in SpaceEngineersGame.SetupPerGameSettings()
MySandboxGame.ConfigDedicated.Load();
ApplyConfiguration(MySandboxGame.ConfigDedicated);
MyPlatformGameSettings.CONSOLE_COMPATIBLE = MySandboxGame.ConfigDedicated.ConsoleCompatibility;
//Type.GetType("VRage.Steam.MySteamService, VRage.Steam").GetProperty("IsActive").GetSetMethod(true).Invoke(service, new object[] {SteamAPI.Init()});
@@ -302,6 +307,18 @@ namespace Torch
MyGlobalTypeMetadata.Static.Init(false);
}
private void ApplyConfiguration(IMyConfigDedicated dedicated)
{
var config = _torch.Configuration.GetSection("DedicatedServer");
dedicated.ServerPort = config.GetValue("Port", dedicated.ServerPort);
dedicated.ConsoleCompatibility = config.GetValue("ConsoleCompatibility", dedicated.ConsoleCompatibility);
dedicated.IP = config.GetValue("Ip", dedicated.IP);
dedicated.WorldName = config.GetValue("WorldName", dedicated.WorldName);
dedicated.ServerName = config.GetValue("ServerName", dedicated.ServerName);
dedicated.ServerDescription = config.GetValue("ServerDescription", dedicated.ServerDescription);
}
private void Destroy()
{
_game.Dispose();

View File

@@ -145,6 +145,27 @@
"resolved": "3.3.3",
"contentHash": "j/rOZtLMVJjrfLRlAMckJLPW/1rze9MT1yfWqSIbUPGRu1m1P0fuo9PmqapwsmePfGB5PJrudQLvmUOAMF0DqQ=="
},
"Microsoft.Extensions.Configuration.Abstractions": {
"type": "Transitive",
"resolved": "7.0.0",
"contentHash": "f34u2eaqIjNO9YLHBz8rozVZ+TcFiFs0F3r7nUJd7FRkVSxk8u4OpoK226mi49MwexHOR2ibP9MFvRUaLilcQQ==",
"dependencies": {
"Microsoft.Extensions.Primitives": "7.0.0"
}
},
"Microsoft.Extensions.Configuration.Binder": {
"type": "Transitive",
"resolved": "7.0.4",
"contentHash": "8+XPvJnHZsYgHOQlcMuQe7QNF5KdVKHH1F/wW3nd8/u81Gk/XFAYMDP0Lpz18h7/AM95M662vvqMorcYxCBB4w==",
"dependencies": {
"Microsoft.Extensions.Configuration.Abstractions": "7.0.0"
}
},
"Microsoft.Extensions.Primitives": {
"type": "Transitive",
"resolved": "7.0.0",
"contentHash": "um1KU5kxcRp3CNuI8o/GrZtD4AIOXDk+RLsytjZ9QPok3ttLUelLKpilVPuaFT3TFjOhSibUAso0odbOaCDj3Q=="
},
"Microsoft.Xaml.Behaviors.Wpf": {
"type": "Transitive",
"resolved": "1.1.31",
@@ -383,6 +404,7 @@
"type": "Project",
"dependencies": {
"JorgeSerrano.Json.JsonSnakeCaseNamingPolicy": "[0.9.0, )",
"Microsoft.Extensions.Configuration.Binder": "[7.0.4, )",
"NLog": "[5.1.0, )",
"NuGet.Commands": "[6.4.0, )",
"NuGet.DependencyResolver.Core": "[6.4.0, )",