Plugin system working, Windows service and command system in progress

This commit is contained in:
John Gross
2017-01-05 02:49:43 -08:00
parent e0a4ee9ce0
commit 4949982fa8
24 changed files with 352 additions and 132 deletions

View File

@@ -3,12 +3,13 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfile" layout="${longdate} [${level:uppercase=true}] ${message}" xsi:type="File" fileName="Torch.log" />
<target name="console" layout="${longdate} [${level:uppercase=true}] ${message}" xsi:type="ColoredConsole" />
<target name="logfile" layout="${longdate} [${level:uppercase=true}] ${logger}: ${message}" xsi:type="File" fileName="Torch.log" />
<target name="console" layout="${longdate} [${level:uppercase=true}] ${logger}: ${message}" xsi:type="ColoredConsole" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
<logger name="*" minlevel="Info" writeTo="console" />
</rules>
</nlog>

25
TestPlugin/Commands.cs Normal file
View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Torch.Commands;
namespace TestPlugin
{
[Category("admin", "tools")]
public class Commands : CommandModule
{
[Command("Ban", "Bans a player from the game")]
public void Ban()
{
Context.Torch.Multiplayer.SendMessage("Boop!");
}
[Command("Unban", "Unbans a player from the game")]
public void Unban()
{
Context.Torch.Multiplayer.SendMessage("Beep!");
}
}
}

View File

@@ -9,6 +9,7 @@ using VRage.Plugins;
namespace TestPlugin
{
[Plugin("Test Plugin", "1.3.3.7", "fed85d8d-8a29-4ab0-9869-4ad121f99d04")]
public class Plugin : TorchPluginBase
{
/// <inheritdoc />

View File

@@ -44,7 +44,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Commands.cs" />
<Compile Include="Plugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

View File

@@ -9,12 +9,14 @@ namespace Torch.API
public class PluginAttribute : Attribute
{
public string Name { get; }
public bool Reloadable { get; }
public Version Version { get; }
public Guid Guid { get; }
public PluginAttribute(string name, bool reloadable = false)
public PluginAttribute(string name, string version, string guid)
{
Name = name;
Reloadable = reloadable;
Version = Version.Parse(version);
Guid = Guid.Parse(guid);
}
}
}

View File

@@ -34,6 +34,7 @@
<ItemGroup>
<Reference Include="NLog">
<HintPath>..\packages\NLog.4.4.1\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
@@ -52,10 +53,12 @@
<Reference Include="System.Xml" />
<Reference Include="VRage">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Library, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Library.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>

View File

@@ -43,13 +43,16 @@
</Reference>
<Reference Include="Sandbox.Game">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sandbox.Graphics, Version=0.1.6108.20417, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.Graphics.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SpaceEngineers.Game">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\SpaceEngineers.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
@@ -64,26 +67,33 @@
</Reference>
<Reference Include="VRage">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Game">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Input, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Input.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Library">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Library.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Math, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Math.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Render">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Render.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Render11">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Render11.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
@@ -123,13 +133,18 @@
<ProjectReference Include="..\Torch.API\Torch.API.csproj">
<Project>{fba5d932-6254-4a1e-baf4-e229fa94e3c2}</Project>
<Name>Torch.API</Name>
<Private>True</Private>
</ProjectReference>
<ProjectReference Include="..\Torch\Torch.csproj">
<Project>{7E01635C-3B67-472E-BCD6-C5539564F214}</Project>
<Name>Torch</Name>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>copy "$(SolutionDir)NLog.config" "$(TargetDir)"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@@ -3,12 +3,14 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using NLog;
using Torch;
using Torch.API;
@@ -17,6 +19,7 @@ namespace Torch.Server
public static class Program
{
private static ITorchServer _server;
private static Logger _log = LogManager.GetLogger("Torch");
public static void Main(string[] args)
{
@@ -29,6 +32,39 @@ namespace Torch.Server
return;
}
if (args.FirstOrDefault() == "-svcinstall")
{
var runArgs = string.Join(" ", args.Skip(1));
_log.Info($"Installing Torch as a service with arguments '{runArgs}'");
var startInfo = new ProcessStartInfo
{
FileName = "sc.exe",
Arguments = $"create Torch binPath=\"{Assembly.GetExecutingAssembly().Location} {runArgs}\"",
CreateNoWindow = true,
UseShellExecute = true,
Verb = "runas"
};
Process.Start(startInfo).WaitForExit();
_log.Info("Torch service installed");
return;
}
if (args.FirstOrDefault() == "-svcuninstall")
{
_log.Info("Uninstalling Torch service");
var startInfo = new ProcessStartInfo
{
FileName = "sc.exe",
Arguments = "delete Torch",
CreateNoWindow = true,
UseShellExecute = true,
Verb = "runas"
};
Process.Start(startInfo).WaitForExit();
_log.Info("Torch service uninstalled");
return;
}
_server = new TorchServer();
_server.Init();
_server.Start();

View File

@@ -46,6 +46,7 @@
<Reference Include="HavokWrapper, Version=1.0.6051.28726, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\HavokWrapper.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.4.1\lib\net45\NLog.dll</HintPath>
@@ -54,21 +55,26 @@
<Reference Include="Sandbox.Common, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\Sandbox.Common.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sandbox.Game, Version=0.1.6101.33378, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\Sandbox.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SpaceEngineers.Game, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\SpaceEngineers.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SpaceEngineersDedicated">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\SpaceEngineersDedicated.exe</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SteamSDK, Version=0.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\SteamSDK.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
@@ -88,22 +94,27 @@
<Reference Include="VRage, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Dedicated, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\VRage.Dedicated.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Game, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\VRage.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Input, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Input.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Library, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Library.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
@@ -167,14 +178,12 @@
<ProjectReference Include="..\Torch.API\Torch.API.csproj">
<Project>{fba5d932-6254-4a1e-baf4-e229fa94e3c2}</Project>
<Name>Torch.API</Name>
</ProjectReference>
<ProjectReference Include="..\Torch.Launcher\Torch.Launcher.csproj">
<Project>{19292801-5B9C-4EE0-961F-0FA37B3A6C3D}</Project>
<Name>Torch.Launcher</Name>
<Private>True</Private>
</ProjectReference>
<ProjectReference Include="..\Torch\Torch.csproj">
<Project>{7e01635c-3b67-472e-bcd6-c5539564f214}</Project>
<Name>Torch</Name>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
@@ -207,6 +216,9 @@
<Resource Include="SpaceEngineers.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>copy "$(SolutionDir)NLog.config" "$(TargetDir)"</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@@ -27,10 +27,11 @@ namespace Torch.Server
{
public Thread GameThread { get; private set; }
public bool IsRunning { get; private set; }
public bool IsService { get; set; }
public event Action SessionLoading;
private readonly ManualResetEvent _stopHandle = new ManualResetEvent(false);
private readonly AutoResetEvent _stopHandle = new AutoResetEvent(false);
internal TorchServer()
{
@@ -85,7 +86,15 @@ namespace Torch.Server
MySandboxGame.IsDedicated = true;
Environment.SetEnvironmentVariable("SteamAppId", MyPerServerSettings.AppId.ToString());
Reflection.InvokeStaticMethod(typeof(DedicatedServer), "RunMain", "Torch", null, true);
Log.Trace("Invoking RunMain");
try { Reflection.InvokeStaticMethod(typeof(DedicatedServer), "RunMain", "Torch", null, IsService, true); }
catch (Exception e)
{
Log.Error(e);
throw;
}
Log.Trace("RunMain completed");
IsRunning = false;
}
/// <summary>
@@ -93,11 +102,15 @@ namespace Torch.Server
/// </summary>
public override void Stop()
{
if (Thread.CurrentThread.ManagedThreadId != GameThread?.ManagedThreadId)
if (!IsRunning)
Log.Error("Server is already stopped");
if (Thread.CurrentThread.ManagedThreadId != GameThread?.ManagedThreadId && MySandboxGame.Static.IsRunning)
{
Log.Info("Requesting server stop.");
MySandboxGame.Static.Invoke(Stop);
_stopHandle.WaitOne();
_stopHandle.WaitOne(10000);
Log.Error("Server stop timed out.");
return;
}

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;
using NLog;
using Torch.API;
namespace Torch.Server
@@ -11,7 +12,8 @@ namespace Torch.Server
class TorchService : ServiceBase
{
public const string Name = "Torch (SEDS)";
private readonly ITorchServer _server = new TorchServer();
private readonly TorchServer _server = new TorchServer();
private static Logger _log = LogManager.GetLogger("Torch");
public TorchService()
{
@@ -29,7 +31,9 @@ namespace Torch.Server
{
base.OnStart(args);
_server.Init();
_server.Start();
_server.RunArgs = args;
_server.IsService = true;
Task.Run(() => _server.Start());
}
/// <inheritdoc />

View File

@@ -15,6 +15,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Torch.Launcher", "Torch.Lau
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestPlugin", "TestPlugin\TestPlugin.csproj", "{ABD18A6C-F638-44E9-8E55-DEDEA321C600}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7AD02A71-1D4C-48F9-A8C1-789A5512424F}"
ProjectSection(SolutionItems) = preProject
NLog.config = NLog.config
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU

View File

@@ -1,12 +1,56 @@
using System;
using System.Linq;
using System.Reflection;
using Torch.API;
using VRage.Game.ModAPI;
namespace Torch.Commands
{
public class Command
{
public CommandModule Module;
public string Name;
public string[] Path;
public Action<CommandContext> Invoke;
public string Name { get; }
public string Description { get; }
public string HelpText { get; }
public Type Module { get; }
public string[] Path { get; }
public ITorchPlugin Plugin { get; }
private readonly MethodInfo _method;
public Command(ITorchPlugin plugin, MethodInfo commandMethod)
{
Plugin = plugin;
var commandAttribute = commandMethod.GetCustomAttribute<CommandAttribute>();
if (commandAttribute == null)
throw new TypeLoadException($"Method does not have a {nameof(CommandAttribute)}");
if (!commandMethod.DeclaringType.IsSubclassOf(typeof(CommandModule)))
throw new TypeLoadException($"Command {commandMethod.Name}'s declaring type {commandMethod.DeclaringType.FullName} is not a subclass of {nameof(CommandModule)}");
var moduleAttribute = commandMethod.DeclaringType.GetCustomAttribute<CategoryAttribute>();
_method = commandMethod;
Module = commandMethod.DeclaringType;
var path = commandAttribute.Path;
if (moduleAttribute != null)
{
var modPath = moduleAttribute.Path;
var comPath = commandAttribute.Path;
path = new string[modPath.Length + comPath.Length];
modPath.CopyTo(path, 0);
comPath.CopyTo(path, modPath.Length);
}
Path = path;
Name = commandAttribute.Name;
Description = commandAttribute.Description;
HelpText = commandAttribute.HelpText;
}
public void Invoke(CommandContext context)
{
var moduleInstance = (CommandModule)Activator.CreateInstance(Module);
moduleInstance.Context = context;
_method.Invoke(moduleInstance, new object[0]);
}
}
}

View File

@@ -5,14 +5,16 @@ namespace Torch.Commands
public class CommandAttribute : Attribute
{
public string Name { get; }
public string Description { get; }
public string HelpText { get; }
public string[] Path { get; }
/// <summary>
/// Specifies a command to add to the command tree.
/// </summary>
/// <param name="name"></param>
public CommandAttribute(string name)
public CommandAttribute(string name, string description = "", string helpText = "", params string[] path)
{
Name = name;
Description = description;
HelpText = helpText;
Path = path.Length > 0 ? path : new [] {name.ToLower()};
}
}
}

View File

@@ -1,20 +1,26 @@
using System.Linq;
using System.Text.RegularExpressions;
using Torch.API;
using VRage.Game.ModAPI;
namespace Torch.Commands
{
public struct CommandContext
{
public string[] Args;
public ulong SteamId;
public ITorchPlugin Plugin { get; }
public ITorchBase Torch { get; }
public IMyPlayer Player { get; }
public string[] Args { get; }
/// <summary>
/// Splits the argument by single words and quoted blocks.
/// </summary>
/// <returns></returns>
public CommandContext(ulong steamId, params string[] args)
public CommandContext(ITorchBase torch, ITorchPlugin plugin, IMyPlayer player, params string[] args)
{
SteamId = steamId;
Torch = torch;
Plugin = plugin;
Player = player;
Args = args;
/*

View File

@@ -1,23 +1,50 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using NLog;
using Torch.API;
using VRage.Game.ModAPI;
namespace Torch.Commands
{
public class CommandSystem
public class PermissionGroup
{
public ITorchBase Server { get; }
public List<ulong> Members { get; }
public List<Permission> Permissions { get; }
}
public class PermissionUser
{
public ulong SteamID { get; }
public List<PermissionGroup> Groups { get; }
public List<Permission> Permissions { get; }
}
public class Permission
{
public string[] Path { get; }
public bool Allow { get; }
}
public class CommandManager
{
public ITorchBase Torch { get; }
public char Prefix { get; set; }
public Dictionary<string, Command> Commands { get; } = new Dictionary<string, Command>();
private Logger _log = LogManager.GetLogger(nameof(CommandManager));
public CommandSystem(ITorchBase server, char prefix = '/')
public CommandManager(ITorchBase torch, char prefix = '/')
{
Server = server;
Torch = torch;
Prefix = prefix;
}
public bool HasPermission(ulong steamId, Command command)
{
return true;
}
public bool IsCommand(string command)
{
return command.Length > 1 && command[0] == Prefix;
@@ -31,37 +58,14 @@ namespace Torch.Commands
if (!type.IsSubclassOf(typeof(CommandModule)))
continue;
var module = (CommandModule)Activator.CreateInstance(type);
module.Server = Server;
module.Plugin = plugin;
foreach (var method in type.GetMethods())
{
var commandAttrib = method.GetCustomAttribute<CommandAttribute>();
if (commandAttrib == null)
continue;
if (Commands.ContainsKey(commandAttrib.Name))
{
Console.WriteLine($"[ERROR]: Command \"{method.Name}\" is already registered!");
continue;
}
var parameters = method.GetParameters();
if (parameters.Length != 1 || parameters[0].ParameterType != typeof(CommandContext))
{
//TODO: log invalid command
Console.WriteLine($"[ERROR]: Command \"{method.Name}\" has the wrong signature! Must take one CommandContext parameter.");
continue;
}
var command = new Command
{
Module = module,
Name = commandAttrib.Name,
Invoke = c => method.Invoke(module, new object[] {c})
};
Commands.Add(command.Name, command);
var command = new Command(plugin, method);
_log.Info($"Registering command '{string.Join(".", command.Path)}' from plugin '{plugin.Name}'");
}
}
}
@@ -79,15 +83,6 @@ namespace Torch.Commands
string arg = "";
if (command.Length > cmdNameEnd + 1)
arg = command.Substring(cmdNameEnd + 1);
var context = new CommandContext
{
//Args = arg,
SteamId = steamId
};
Commands[cmdName].Invoke(context);
}
}
}

View File

@@ -4,7 +4,6 @@ namespace Torch.Commands
{
public class CommandModule
{
public ITorchPlugin Plugin { get; set; }
public ITorchBase Server { get; set; }
public CommandContext Context { get; internal set; }
}
}

View File

@@ -37,14 +37,14 @@ namespace Torch.Commands
return true;
}
public InvokeResult Invoke(ulong steamId, string[] command)
public Command ParseCommand(ulong steamId, string[] command, out string[] args)
{
args = new string[0];
var root = command.First();
if (!RootNodes.ContainsKey(root))
return InvokeResult.NoCommand;
return null;
var node = RootNodes[root];
var args = new string[0];
for (var i = 1; i < command.Length; i++)
{
var current = command[i];
@@ -59,13 +59,9 @@ namespace Torch.Commands
}
if (!node.IsCommand)
return InvokeResult.NoCommand;
return null;
//check permission here
var context = new CommandContext(steamId, args);
node.Command.Invoke(context);
return InvokeResult.Success;
return node.Command;
}
private class Node

View File

@@ -22,6 +22,7 @@ using SteamSDK;
using Torch.API;
using Torch.ViewModels;
using VRage.Game;
using VRage.Game.ModAPI;
using VRage.Library.Collections;
using VRage.Network;
using VRage.Utils;
@@ -42,9 +43,9 @@ namespace Torch
public List<IChatItem> Chat { get; } = new List<IChatItem>();
public Dictionary<ulong, IPlayer> Players { get; } = new Dictionary<ulong, IPlayer>();
public Player LocalPlayer { get; private set; }
private readonly ITorchBase _torch;
private static Logger _log = LogManager.GetLogger("Torch");
private Dictionary<MyPlayer.PlayerId, MyPlayer> _onlinePlayers;
internal MultiplayerManager(ITorchBase torch)
{
@@ -64,6 +65,12 @@ namespace Torch
});
}
public IMyPlayer GetPlayerBySteamId(ulong steamId)
{
_onlinePlayers.TryGetValue(new MyPlayer.PlayerId(steamId), out MyPlayer p);
return p;
}
/// <summary>
/// Send a message in chat.
/// </summary>
@@ -82,6 +89,8 @@ namespace Torch
MyMultiplayer.Static.ClientLeft += OnClientLeft;
MySession.Static.Players.PlayerRequesting += OnPlayerRequesting;
_onlinePlayers = MySession.Static.Players.GetPrivateField<Dictionary<MyPlayer.PlayerId, MyPlayer>>("m_players");
//TODO: Move these with the methods?
RemoveHandlers();
SteamServerAPI.Instance.GameServer.ValidateAuthTicketResponse += ValidateAuthTicketResponse;

View File

@@ -13,6 +13,7 @@ using NLog;
using Sandbox;
using Sandbox.ModAPI;
using Torch.API;
using Torch.Commands;
using VRage.Plugins;
using VRage.Collections;
using VRage.Library.Collections;
@@ -22,16 +23,18 @@ namespace Torch
public class PluginManager : IPluginManager
{
private readonly ITorchBase _torch;
private static Logger _log = LogManager.GetLogger("Torch");
private static Logger _log = LogManager.GetLogger(nameof(PluginManager));
public const string PluginDir = "Plugins";
private readonly List<ITorchPlugin> _plugins = new List<ITorchPlugin>();
private readonly PluginUpdater _updater;
private readonly CommandManager _commands;
public PluginManager(ITorchBase torch)
{
_torch = torch;
_updater = new PluginUpdater(this);
_commands = new CommandManager(_torch);
if (!Directory.Exists(PluginDir))
Directory.CreateDirectory(PluginDir);
@@ -67,6 +70,7 @@ namespace Torch
/// </summary>
public void LoadPlugins()
{
_log.Info("Loading plugins");
var pluginsPath = Path.Combine(Directory.GetCurrentDirectory(), PluginDir);
var dlls = Directory.GetFiles(pluginsPath, "*.dll", SearchOption.AllDirectories);
foreach (var dllPath in dlls)
@@ -78,9 +82,22 @@ namespace Torch
{
if (type.GetInterfaces().Contains(typeof(ITorchPlugin)))
{
var plugin = (ITorchPlugin)Activator.CreateInstance(type);
_log.Info($"Loading plugin {plugin.Name} ({plugin.Version})");
_plugins.Add(plugin);
try
{
var plugin = (ITorchPlugin)Activator.CreateInstance(type);
if (plugin.Id == default(Guid))
throw new TypeLoadException($"Plugin '{type.FullName}' is missing a {nameof(PluginAttribute)}");
_log.Info($"Loading plugin {plugin.Name} ({plugin.Version})");
_plugins.Add(plugin);
_commands.RegisterPluginCommands(plugin);
}
catch (Exception e)
{
_log.Error($"Error loading plugin '{type.FullName}'");
throw;
}
}
}
}

View File

@@ -4,14 +4,15 @@ using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using NLog;
namespace Torch
{
public static class Reflection
{
//private static readonly Logger Log = LogManager.GetLogger( "BaseLog" );
private static readonly Logger Log = LogManager.GetLogger("Reflection");
public static bool HasMethod(Type objectType, string methodName, Type[] argTypes = null)
public static bool HasMethod(Type type, string methodName, Type[] argTypes = null)
{
try
{
@@ -20,27 +21,27 @@ namespace Torch
if (argTypes == null)
{
var methodInfo = objectType.GetMethod(methodName);
var methodInfo = type.GetMethod(methodName);
if (methodInfo == null)
methodInfo = objectType.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (methodInfo == null && objectType.BaseType != null)
methodInfo = objectType.BaseType.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
methodInfo = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (methodInfo == null && type.BaseType != null)
methodInfo = type.BaseType.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (methodInfo == null)
{
//Log.Error( "Failed to find method '" + methodName + "' in type '" + objectType.FullName + "'" );
Log.Error( "Failed to find method '" + methodName + "' in type '" + type.FullName + "'" );
return false;
}
}
else
{
MethodInfo method = objectType.GetMethod(methodName, argTypes);
MethodInfo method = type.GetMethod(methodName, argTypes);
if (method == null)
method = objectType.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy, Type.DefaultBinder, argTypes, null);
if (method == null && objectType.BaseType != null)
method = objectType.BaseType.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy, Type.DefaultBinder, argTypes, null);
method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy, Type.DefaultBinder, argTypes, null);
if (method == null && type.BaseType != null)
method = type.BaseType.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy, Type.DefaultBinder, argTypes, null);
if (method == null)
{
//Log.Error( "Failed to find method '" + methodName + "' in type '" + objectType.FullName + "'" );
Log.Error( "Failed to find method '" + methodName + "' in type '" + type.FullName + "'" );
return false;
}
}
@@ -53,60 +54,58 @@ namespace Torch
}
catch (Exception ex)
{
//Log.Error( "Failed to find method '" + methodName + "' in type '" + objectType.FullName + "': " + ex.Message );
//Log.Error( ex );
Log.Error( "Failed to find method '" + methodName + "' in type '" + type.FullName + "': " + ex.Message );
Log.Error( ex );
return false;
}
}
public static bool HasField(Type objectType, string fieldName)
public static bool HasField(Type type, string fieldName)
{
try
{
if (string.IsNullOrEmpty(fieldName))
return false;
var field = objectType.GetField(fieldName);
var field = type.GetField(fieldName);
if (field == null)
field = objectType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (field == null)
field = objectType.BaseType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
field = type.BaseType.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (field == null)
{
//Log.Error( "Failed to find field '" + fieldName + "' in type '" + objectType.FullName + "'" );
Log.Error("Failed to find field '{0}' in type '{1}'", fieldName, type.FullName);
return false;
}
return true;
}
catch (Exception ex)
{
//Log.Error( "Failed to find field '" + fieldName + "' in type '" + objectType.FullName + "': " + ex.Message );
//Log.Error( ex );
Log.Error(ex, "Failed to find field '{0}' in type '{1}'", fieldName, type.FullName);
return false;
}
}
public static bool HasProperty(Type objectType, string propertyName)
public static bool HasProperty(Type type, string propertyName)
{
try
{
if (string.IsNullOrEmpty(propertyName))
return false;
var prop = objectType.GetProperty(propertyName);
var prop = type.GetProperty(propertyName);
if (prop == null)
prop = objectType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
prop = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (prop == null)
prop = objectType.BaseType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
prop = type.BaseType.GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (prop == null)
{
//Log.Error( "Failed to find property '" + propertyName + "' in type '" + objectType.FullName + "'" );
Log.Error("Failed to find property '{0}' in type '{1}'", propertyName, type.FullName);
return false;
}
return true;
}
catch (Exception ex)
{
//Log.Error( "Failed to find property '" + propertyName + "' in type '" + objectType.FullName + "': " + ex.Message );
//Log.Error( ex );
Log.Error(ex, "Failed to find property '{0}' in type '{1}'", propertyName, type.FullName);
return false;
}
}
@@ -115,9 +114,18 @@ namespace Torch
{
var method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
if (method == null)
throw new TypeLoadException($"Method {methodName} not found in static class {type.FullName}");
{
Log.Error($"Method {methodName} not found in static class {type.FullName}");
return null;
}
return method.Invoke(null, args);
}
public static T GetPrivateField<T>(this object obj, string fieldName)
{
var type = obj.GetType();
return (T)type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(obj);
}
}
}

View File

@@ -39,33 +39,41 @@
<Reference Include="Sandbox.Common, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.Common.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sandbox.Game, Version=0.1.6108.20542, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sandbox.Graphics, Version=0.1.6108.20417, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\Sandbox.Graphics.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SpaceEngineers.Game, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\SpaceEngineers.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SpaceEngineers.ObjectBuilders, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\SpaceEngineers.ObjectBuilders.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SpaceEngineers.ObjectBuilders.XmlSerializers, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\SpaceEngineers.ObjectBuilders.XmlSerializers.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SpaceEngineersDedicated">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\DedicatedServer64\SpaceEngineersDedicated.exe</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="SteamSDK, Version=0.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\SteamSDK.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
@@ -78,49 +86,61 @@
<Reference Include="VRage, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Audio, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Audio.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Game, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Game.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Game.XmlSerializers, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Game.XmlSerializers.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Input, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Input.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Library, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Library.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Math">
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Math.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Native, Version=0.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Native.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.OpenVRWrapper, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.OpenVRWrapper.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Render, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Render.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Render11, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Render11.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="VRage.Scripting, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files (x86)\Steam\steamapps\common\SpaceEngineers\Bin64\VRage.Scripting.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="WindowsBase" />
</ItemGroup>
@@ -130,7 +150,7 @@
<Compile Include="Commands\CommandAttribute.cs" />
<Compile Include="Commands\CommandModule.cs" />
<Compile Include="Commands\CommandContext.cs" />
<Compile Include="Commands\CommandSystem.cs" />
<Compile Include="Commands\CommandManager.cs" />
<Compile Include="Commands\CommandTree.cs" />
<Compile Include="Managers\ChatManager.cs" />
<Compile Include="Managers\NetworkManager\NetworkHandlerBase.cs" />
@@ -157,6 +177,7 @@
<ProjectReference Include="..\Torch.API\Torch.API.csproj">
<Project>{fba5d932-6254-4a1e-baf4-e229fa94e3c2}</Project>
<Name>Torch.API</Name>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>

View File

@@ -113,12 +113,19 @@ namespace Torch
{
Debug.Assert(!_init, "Torch instance is already initialized.");
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
_init = true;
MyScriptCompiler.Static.AddConditionalCompilationSymbols("TORCH");
MyScriptCompiler.Static.AddReferencedAssemblies(typeof(ITorchBase).Assembly.Location);
MyScriptCompiler.Static.AddReferencedAssemblies(typeof(TorchBase).Assembly.Location);
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Log.Fatal((Exception)e.ExceptionObject);
}
public abstract void Start();
public abstract void Stop();
}

View File

@@ -7,6 +7,7 @@ using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NLog;
using Torch.API;
namespace Torch
@@ -17,28 +18,25 @@ namespace Torch
public Version Version { get; }
public string Name { get; }
public ITorchBase Torch { get; private set; }
private static Logger _log = LogManager.GetCurrentClassLogger();
protected TorchPluginBase()
{
var asm = Assembly.GetCallingAssembly();
var id = asm.GetCustomAttribute<GuidAttribute>()?.Value;
if (id == null)
throw new InvalidOperationException($"{asm.FullName} has no Guid attribute.");
Id = new Guid(id);
var ver = asm.GetCustomAttribute<AssemblyVersionAttribute>()?.Version;
if (ver == null)
throw new InvalidOperationException($"{asm.FullName} has no AssemblyVersion attribute.");
Version = new Version(ver);
var name = asm.GetCustomAttribute<AssemblyTitleAttribute>()?.Title;
if (name == null)
throw new InvalidOperationException($"{asm.FullName} has no AssemblyTitle attribute.");
Name = name;
var type = GetType();
var pluginInfo = type.GetCustomAttribute<PluginAttribute>();
if (pluginInfo == null)
{
_log.Warn($"Plugin {type.FullName} has no PluginAttribute");
Name = type.FullName;
Version = new Version(0, 0, 0, 0);
Id = default(Guid);
}
else
{
Name = pluginInfo.Name;
Version = pluginInfo.Version;
Id = pluginInfo.Guid;
}
}
public virtual void Init(ITorchBase torch)