This commit is contained in:
zznty
2022-07-21 21:57:27 +07:00
commit bc4546410e
75 changed files with 2709 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
using System.Security.Cryptography;
using System.Text.Json;
using EmbedIO;
using EmbedIO.BearerToken;
using EmbedIO.WebApi;
using Microsoft.IdentityModel.Tokens;
using NLog;
using Torch.API;
using Torch.API.Managers;
using Torch.Managers;
using Torch.Server.Managers;
using TorchRemote.Plugin.Controllers;
using TorchRemote.Plugin.Modules;
using TorchRemote.Plugin.Utils;
using VRage.Game.ModAPI;
namespace TorchRemote.Plugin.Managers;
public class ApiServerManager : Manager
{
private static readonly ILogger Log = LogManager.GetCurrentClassLogger();
private readonly Config _config;
private readonly IWebServer _server;
[Dependency]
private readonly SettingManager _settingManager = null!;
[Dependency]
private readonly InstanceManager _instanceManager = null!;
public ApiServerManager(ITorchBase torchInstance, Config config) : base(torchInstance)
{
_config = config;
if (string.IsNullOrEmpty(_config.SecurityKey))
_config.SecurityKey = CreateSecurityKey();
var apiModule = new WebApiModule("/api/v1", async (context, data) =>
{
try
{
context.Response.ContentType = "application/json";
using var stream = context.OpenResponseStream();
await JsonSerializer.SerializeAsync(stream, data, Statics.SerializerOptions);
}
catch (Exception e)
{
Log.Error(e);
throw HttpException.InternalServerError(e.Message, e.Message);
}
});
var chatModule = new ChatModule("/api/live/chat", true);
Statics.ChatModule = chatModule;
_server = new WebServer(o => o
.WithUrlPrefix(_config.Listener.UrlPrefix)
.WithMicrosoftHttpListener())
.WithLocalSessionManager()
.WithModule(apiModule
.WithController<ServerController>()
.WithController<SettingsController>()
.WithController<WorldsController>()
.WithController<ChatController>())
.WithModule(new LogsModule("/api/live/logs", true))
.WithModule(chatModule)
.WithBearerToken("/api", new SymmetricSecurityKey(Convert.FromBase64String(_config.SecurityKey)), new BasicAuthorizationServerProvider());
}
public override void Attach()
{
base.Attach();
Starter();
Log.Info("Listening on {0}", _config.Listener.UrlPrefix);
//_instanceManager.InstanceLoaded += model => _settingManager.RegisterSetting(model.Model, typeof(IMyConfigDedicated), false);
}
public override void Detach()
{
base.Detach();
_server.Dispose();
}
private async void Starter()
{
await _server.RunAsync();
}
private static string CreateSecurityKey()
{
var aes = Aes.Create();
aes.GenerateIV();
aes.GenerateKey();
return Convert.ToBase64String(aes.Key);
}
}

View File

@@ -0,0 +1,28 @@
using Sandbox.Engine.Multiplayer;
using Torch.API;
using Torch.API.Managers;
using Torch.Managers;
using TorchRemote.Models.Responses;
using TorchRemote.Models.Shared;
using TorchRemote.Plugin.Utils;
namespace TorchRemote.Plugin.Managers;
public class ChatMonitorManager : Manager
{
[Dependency]
private readonly IChatManagerServer _chatManager = null!;
public ChatMonitorManager(ITorchBase torchInstance) : base(torchInstance)
{
}
public override void Attach()
{
base.Attach();
_chatManager.MessageRecieved += ChatManagerOnMessageReceived;
}
private void ChatManagerOnMessageReceived(TorchChatMessage msg, ref bool consumed)
{
Statics.ChatModule.SendChatResponse(new ChatMessageResponse(msg.Author ?? (msg.AuthorSteamId is null ? Torch.Config.ChatName : MyMultiplayer.Static.GetMemberName(msg.AuthorSteamId.Value)),
msg.AuthorSteamId, (ChatChannel)msg.Channel, msg.Message));
}
}

View File

@@ -0,0 +1,77 @@
using System.Collections.Concurrent;
using System.Reflection;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
using NLog;
using Torch.API;
using Torch.Managers;
using Torch.Views;
using TorchRemote.Models.Responses;
using TorchRemote.Plugin.Utils;
using VRage;
namespace TorchRemote.Plugin.Managers;
public class SettingManager : Manager
{
private static readonly ILogger Log = LogManager.GetCurrentClassLogger();
public SettingManager(ITorchBase torchInstance) : base(torchInstance)
{
}
public Guid RegisterSetting(object value, Type type, bool includeOnlyDisplay = true)
{
var properties = type.IsInterface ? type.GetProperties() : type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var settingProperties = properties
.Where(b => !b.HasAttribute<XmlIgnoreAttribute>() &&
!b.HasAttribute<JsonIgnoreAttribute>() &&
(!includeOnlyDisplay ||
b.HasAttribute<DisplayAttribute>()))
.Select(property => new SettingProperty(property.Name,
GetTypeId(property.PropertyType, property.GetValue(value), includeOnlyDisplay),
property.PropertyType, property.GetMethod, property.SetMethod,
property.GetCustomAttribute<DisplayAttribute>() is { } attr ?
new(attr.Name, attr.Description, attr.GroupName, attr.Order, attr.ReadOnly, attr.Enabled) :
null))
.ToArray();
var setting = new Setting(type.Name, type, settingProperties, value);
var id = (type.FullName! + value.GetHashCode()).ToGuid();
Settings.Add(id, setting);
Log.Debug("Registered type {0} with id {1}", type, id);
return id;
}
private Guid GetTypeId(Type type, object value, bool includeOnlyDisplay)
{
if (type == typeof(int) || type == typeof(uint))
return SettingPropertyTypeEnum.Integer;
if (type == typeof(bool))
return SettingPropertyTypeEnum.Boolean;
if (type == typeof(short) ||
type == typeof(ushort) ||
type == typeof(byte) ||
type == typeof(ulong) ||
type == typeof(long) ||
type == typeof(float) ||
type == typeof(double) ||
type == typeof(MyFixedPoint))
return SettingPropertyTypeEnum.Number;
if (type == typeof(string))
return SettingPropertyTypeEnum.String;
if (type == typeof(DateTime))
return SettingPropertyTypeEnum.DateTime;
if (type == typeof(TimeSpan))
return SettingPropertyTypeEnum.TimeSpan;
if (type == typeof(System.Drawing.Color) || type == typeof(VRageMath.Color))
return SettingPropertyTypeEnum.Color;
return RegisterSetting(value, type, includeOnlyDisplay);
}
public IDictionary<Guid, Setting> Settings { get; } = new ConcurrentDictionary<Guid, Setting>();
}
public record Setting(string Name, Type Type, IEnumerable<SettingProperty> Properties, object? Value = null);
public record SettingProperty(string Name, Guid TypeId, Type Type, MethodInfo Getter, MethodInfo? Setter, SettingPropertyDisplayInfo? DisplayInfo);
public record SettingPropertyDisplayInfo(string? Name, string? Description, string? GroupName, int? Order, bool? IsReadOnly, bool? IsEnabled);