Skip to content

Commit 4949982

Browse files
committed
Plugin system working, Windows service and command system in progress
1 parent e0a4ee9 commit 4949982

24 files changed

Lines changed: 352 additions & 132 deletions

‎NLog.config‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
44

55
<targets>
6-
<target name="logfile" layout="${longdate} [${level:uppercase=true}] ${message}" xsi:type="File" fileName="Torch.log" />
7-
<target name="console" layout="${longdate} [${level:uppercase=true}] ${message}" xsi:type="ColoredConsole" />
6+
<target name="logfile" layout="${longdate} [${level:uppercase=true}] ${logger}: ${message}" xsi:type="File" fileName="Torch.log" />
7+
<target name="console" layout="${longdate} [${level:uppercase=true}] ${logger}: ${message}" xsi:type="ColoredConsole" />
88
</targets>
99

1010
<rules>
1111
<logger name="*" minlevel="Info" writeTo="logfile" />
1212
<logger name="*" minlevel="Info" writeTo="console" />
1313
</rules>
14+
1415
</nlog>

‎TestPlugin/Commands.cs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Torch.Commands;
7+
8+
namespace TestPlugin
9+
{
10+
[Category("admin", "tools")]
11+
public class Commands : CommandModule
12+
{
13+
[Command("Ban", "Bans a player from the game")]
14+
public void Ban()
15+
{
16+
Context.Torch.Multiplayer.SendMessage("Boop!");
17+
}
18+
19+
[Command("Unban", "Unbans a player from the game")]
20+
public void Unban()
21+
{
22+
Context.Torch.Multiplayer.SendMessage("Beep!");
23+
}
24+
}
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace TestPlugin
1111
{
12+
[Plugin("Test Plugin", "1.3.3.7", "fed85d8d-8a29-4ab0-9869-4ad121f99d04")]
1213
public class Plugin : TorchPluginBase
1314
{
1415
/// <inheritdoc />

‎TestPlugin/TestPlugin.csproj‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
</Reference>
4545
</ItemGroup>
4646
<ItemGroup>
47-
<Compile Include="Class1.cs" />
47+
<Compile Include="Commands.cs" />
48+
<Compile Include="Plugin.cs" />
4849
<Compile Include="Properties\AssemblyInfo.cs" />
4950
</ItemGroup>
5051
<ItemGroup>

‎Torch.API/PluginAttribute.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ namespace Torch.API
99
public class PluginAttribute : Attribute
1010
{
1111
public string Name { get; }
12-
public bool Reloadable { get; }
12+
public Version Version { get; }
13+
public Guid Guid { get; }
1314

14-
public PluginAttribute(string name, bool reloadable = false)
15+
public PluginAttribute(string name, string version, string guid)
1516
{
1617
Name = name;
17-
Reloadable = reloadable;
18+
Version = Version.Parse(version);
19+
Guid = Guid.Parse(guid);
1820
}
1921
}
2022
}

‎Torch.API/Torch.API.csproj‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<ItemGroup>
3535
<Reference Include="NLog">
3636
<HintPath>..\packages\NLog.4.4.1\lib\net45\NLog.dll</HintPath>
37+
<Private>True</Private>
3738
</Reference>
3839
<Reference Include="PresentationCore" />
3940
<Reference Include="PresentationFramework" />
@@ -52,10 +53,12 @@
5253
<Reference Include="System.Xml" />
5354
<Reference Include="VRage">
5455
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.dll</HintPath>
56+
<Private>False</Private>
5557
</Reference>
5658
<Reference Include="VRage.Library, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
5759
<SpecificVersion>False</SpecificVersion>
5860
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Library.dll</HintPath>
61+
<Private>False</Private>
5962
</Reference>
6063
</ItemGroup>
6164
<ItemGroup>

‎Torch.Client/Torch.Client.csproj‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@
4343
</Reference>
4444
<Reference Include="Sandbox.Game">
4545
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.Game.dll</HintPath>
46+
<Private>False</Private>
4647
</Reference>
4748
<Reference Include="Sandbox.Graphics, Version=0.1.6108.20417, Culture=neutral, processorArchitecture=AMD64">
4849
<SpecificVersion>False</SpecificVersion>
4950
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.Graphics.dll</HintPath>
51+
<Private>False</Private>
5052
</Reference>
5153
<Reference Include="SpaceEngineers.Game">
5254
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\SpaceEngineers.Game.dll</HintPath>
55+
<Private>False</Private>
5356
</Reference>
5457
<Reference Include="System" />
5558
<Reference Include="System.Data" />
@@ -64,26 +67,33 @@
6467
</Reference>
6568
<Reference Include="VRage">
6669
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.dll</HintPath>
70+
<Private>False</Private>
6771
</Reference>
6872
<Reference Include="VRage.Game">
6973
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Game.dll</HintPath>
74+
<Private>False</Private>
7075
</Reference>
7176
<Reference Include="VRage.Input, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
7277
<SpecificVersion>False</SpecificVersion>
7378
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Input.dll</HintPath>
79+
<Private>False</Private>
7480
</Reference>
7581
<Reference Include="VRage.Library">
7682
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Library.dll</HintPath>
83+
<Private>False</Private>
7784
</Reference>
7885
<Reference Include="VRage.Math, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
7986
<SpecificVersion>False</SpecificVersion>
8087
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Math.dll</HintPath>
88+
<Private>False</Private>
8189
</Reference>
8290
<Reference Include="VRage.Render">
8391
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Render.dll</HintPath>
92+
<Private>False</Private>
8493
</Reference>
8594
<Reference Include="VRage.Render11">
8695
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Render11.dll</HintPath>
96+
<Private>False</Private>
8797
</Reference>
8898
<Reference Include="WindowsBase" />
8999
<Reference Include="PresentationCore" />
@@ -123,13 +133,18 @@
123133
<ProjectReference Include="..\Torch.API\Torch.API.csproj">
124134
<Project>{fba5d932-6254-4a1e-baf4-e229fa94e3c2}</Project>
125135
<Name>Torch.API</Name>
136+
<Private>True</Private>
126137
</ProjectReference>
127138
<ProjectReference Include="..\Torch\Torch.csproj">
128139
<Project>{7E01635C-3B67-472E-BCD6-C5539564F214}</Project>
129140
<Name>Torch</Name>
141+
<Private>True</Private>
130142
</ProjectReference>
131143
</ItemGroup>
132144
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
145+
<PropertyGroup>
146+
<PostBuildEvent>copy "$(SolutionDir)NLog.config" "$(TargetDir)"</PostBuildEvent>
147+
</PropertyGroup>
133148
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
134149
Other similar extension points exist, see Microsoft.Common.targets.
135150
<Target Name="BeforeBuild">

‎Torch.Server/Program.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6+
using System.Reflection;
67
using System.ServiceProcess;
78
using System.Text;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using System.Windows;
1112
using System.Windows.Threading;
13+
using NLog;
1214
using Torch;
1315
using Torch.API;
1416

@@ -17,6 +19,7 @@ namespace Torch.Server
1719
public static class Program
1820
{
1921
private static ITorchServer _server;
22+
private static Logger _log = LogManager.GetLogger("Torch");
2023

2124
public static void Main(string[] args)
2225
{
@@ -29,6 +32,39 @@ public static void Main(string[] args)
2932
return;
3033
}
3134

35+
if (args.FirstOrDefault() == "-svcinstall")
36+
{
37+
var runArgs = string.Join(" ", args.Skip(1));
38+
_log.Info($"Installing Torch as a service with arguments '{runArgs}'");
39+
var startInfo = new ProcessStartInfo
40+
{
41+
FileName = "sc.exe",
42+
Arguments = $"create Torch binPath=\"{Assembly.GetExecutingAssembly().Location} {runArgs}\"",
43+
CreateNoWindow = true,
44+
UseShellExecute = true,
45+
Verb = "runas"
46+
};
47+
Process.Start(startInfo).WaitForExit();
48+
_log.Info("Torch service installed");
49+
return;
50+
}
51+
52+
if (args.FirstOrDefault() == "-svcuninstall")
53+
{
54+
_log.Info("Uninstalling Torch service");
55+
var startInfo = new ProcessStartInfo
56+
{
57+
FileName = "sc.exe",
58+
Arguments = "delete Torch",
59+
CreateNoWindow = true,
60+
UseShellExecute = true,
61+
Verb = "runas"
62+
};
63+
Process.Start(startInfo).WaitForExit();
64+
_log.Info("Torch service uninstalled");
65+
return;
66+
}
67+
3268
_server = new TorchServer();
3369
_server.Init();
3470
_server.Start();

‎Torch.Server/Torch.Server.csproj‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<Reference Include="HavokWrapper, Version=1.0.6051.28726, Culture=neutral, processorArchitecture=AMD64">
4747
<SpecificVersion>False</SpecificVersion>
4848
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\HavokWrapper.dll</HintPath>
49+
<Private>False</Private>
4950
</Reference>
5051
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
5152
<HintPath>..\packages\NLog.4.4.1\lib\net45\NLog.dll</HintPath>
@@ -54,21 +55,26 @@
5455
<Reference Include="Sandbox.Common, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
5556
<SpecificVersion>False</SpecificVersion>
5657
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\Sandbox.Common.dll</HintPath>
58+
<Private>False</Private>
5759
</Reference>
5860
<Reference Include="Sandbox.Game, Version=0.1.6101.33378, Culture=neutral, processorArchitecture=AMD64">
5961
<SpecificVersion>False</SpecificVersion>
6062
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\Sandbox.Game.dll</HintPath>
63+
<Private>False</Private>
6164
</Reference>
6265
<Reference Include="SpaceEngineers.Game, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
6366
<SpecificVersion>False</SpecificVersion>
6467
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\SpaceEngineers.Game.dll</HintPath>
68+
<Private>False</Private>
6569
</Reference>
6670
<Reference Include="SpaceEngineersDedicated">
6771
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\SpaceEngineersDedicated.exe</HintPath>
72+
<Private>False</Private>
6873
</Reference>
6974
<Reference Include="SteamSDK, Version=0.0.0.0, Culture=neutral, processorArchitecture=AMD64">
7075
<SpecificVersion>False</SpecificVersion>
7176
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\SteamSDK.dll</HintPath>
77+
<Private>False</Private>
7278
</Reference>
7379
<Reference Include="System" />
7480
<Reference Include="System.Configuration.Install" />
@@ -88,22 +94,27 @@
8894
<Reference Include="VRage, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
8995
<SpecificVersion>False</SpecificVersion>
9096
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.dll</HintPath>
97+
<Private>False</Private>
9198
</Reference>
9299
<Reference Include="VRage.Dedicated, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
93100
<SpecificVersion>False</SpecificVersion>
94101
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\VRage.Dedicated.dll</HintPath>
102+
<Private>False</Private>
95103
</Reference>
96104
<Reference Include="VRage.Game, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
97105
<SpecificVersion>False</SpecificVersion>
98106
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\VRage.Game.dll</HintPath>
107+
<Private>False</Private>
99108
</Reference>
100109
<Reference Include="VRage.Input, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
101110
<SpecificVersion>False</SpecificVersion>
102111
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Input.dll</HintPath>
112+
<Private>False</Private>
103113
</Reference>
104114
<Reference Include="VRage.Library, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
105115
<SpecificVersion>False</SpecificVersion>
106116
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Library.dll</HintPath>
117+
<Private>False</Private>
107118
</Reference>
108119
<Reference Include="WindowsBase" />
109120
<Reference Include="PresentationCore" />
@@ -167,14 +178,12 @@
167178
<ProjectReference Include="..\Torch.API\Torch.API.csproj">
168179
<Project>{fba5d932-6254-4a1e-baf4-e229fa94e3c2}</Project>
169180
<Name>Torch.API</Name>
170-
</ProjectReference>
171-
<ProjectReference Include="..\Torch.Launcher\Torch.Launcher.csproj">
172-
<Project>{19292801-5B9C-4EE0-961F-0FA37B3A6C3D}</Project>
173-
<Name>Torch.Launcher</Name>
181+
<Private>True</Private>
174182
</ProjectReference>
175183
<ProjectReference Include="..\Torch\Torch.csproj">
176184
<Project>{7e01635c-3b67-472e-bcd6-c5539564f214}</Project>
177185
<Name>Torch</Name>
186+
<Private>True</Private>
178187
</ProjectReference>
179188
</ItemGroup>
180189
<ItemGroup>
@@ -207,6 +216,9 @@
207216
<Resource Include="SpaceEngineers.ico" />
208217
</ItemGroup>
209218
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
219+
<PropertyGroup>
220+
<PostBuildEvent>copy "$(SolutionDir)NLog.config" "$(TargetDir)"</PostBuildEvent>
221+
</PropertyGroup>
210222
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
211223
Other similar extension points exist, see Microsoft.Common.targets.
212224
<Target Name="BeforeBuild">

‎Torch.Server/TorchServer.cs‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ public class TorchServer : TorchBase, ITorchServer
2727
{
2828
public Thread GameThread { get; private set; }
2929
public bool IsRunning { get; private set; }
30+
public bool IsService { get; set; }
3031

3132
public event Action SessionLoading;
3233

33-
private readonly ManualResetEvent _stopHandle = new ManualResetEvent(false);
34+
private readonly AutoResetEvent _stopHandle = new AutoResetEvent(false);
3435

3536
internal TorchServer()
3637
{
@@ -85,19 +86,31 @@ public override void Start()
8586
MySandboxGame.IsDedicated = true;
8687
Environment.SetEnvironmentVariable("SteamAppId", MyPerServerSettings.AppId.ToString());
8788

88-
Reflection.InvokeStaticMethod(typeof(DedicatedServer), "RunMain", "Torch", null, true);
89+
Log.Trace("Invoking RunMain");
90+
try { Reflection.InvokeStaticMethod(typeof(DedicatedServer), "RunMain", "Torch", null, IsService, true); }
91+
catch (Exception e)
92+
{
93+
Log.Error(e);
94+
throw;
95+
}
96+
Log.Trace("RunMain completed");
97+
IsRunning = false;
8998
}
9099

91100
/// <summary>
92101
/// Stop the server.
93102
/// </summary>
94103
public override void Stop()
95104
{
96-
if (Thread.CurrentThread.ManagedThreadId != GameThread?.ManagedThreadId)
105+
if (!IsRunning)
106+
Log.Error("Server is already stopped");
107+
108+
if (Thread.CurrentThread.ManagedThreadId != GameThread?.ManagedThreadId && MySandboxGame.Static.IsRunning)
97109
{
98110
Log.Info("Requesting server stop.");
99111
MySandboxGame.Static.Invoke(Stop);
100-
_stopHandle.WaitOne();
112+
_stopHandle.WaitOne(10000);
113+
Log.Error("Server stop timed out.");
101114
return;
102115
}
103116

0 commit comments

Comments
 (0)