Compare commits

..

7 Commits

Author SHA1 Message Date
zznty
6bcd2ea58e windows moment 2023-01-24 16:27:31 +07:00
zznty
b8a06f7bd7 publish nuget package on release 2023-01-24 16:25:10 +07:00
zznty
d44b1a592c rewrite torch mod because casitard broke original 2023-01-24 16:09:19 +07:00
zznty
d7d556d2f2 fix for restart on crash too 2023-01-12 23:37:43 +07:00
zznty
850b313269 fix updates again 2023-01-12 23:29:37 +07:00
zznty
fe985e1a9c fix restart again 2023-01-12 23:20:44 +07:00
zznty
aeea192860 exit on restart 2023-01-09 22:02:54 +07:00
13 changed files with 75 additions and 298 deletions

View File

@@ -67,3 +67,9 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with: with:
id: ${{ steps.create_release.outputs.id }} id: ${{ steps.create_release.outputs.id }}
- run: dotnet pack -c Release ./Torch.API/Torch.API.csproj -o pack --include-symbols -p:SymbolPackageFormat=snupkg --no-build
- run: dotnet pack -c Release ./Torch/Torch.csproj -o pack --include-symbols -p:SymbolPackageFormat=snupkg --no-build
- run: dotnet pack -c Release ./Torch.Server/Torch.Server.csproj -o pack --include-symbols -p:SymbolPackageFormat=snupkg -p:Version=${{ env.TORCH_VERSION }} --no-build
- run: dotnet nuget push ./pack/*.nupkg -s github

View File

@@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Torch.Mod.Messages
{
/// <summary>
/// shim to store incoming message data
/// </summary>
internal class IncomingMessage : MessageBase
{
public IncomingMessage()
{
}
public override void ProcessClient()
{
throw new Exception();
}
public override void ProcessServer()
{
throw new Exception();
}
}
}

View File

@@ -32,11 +32,6 @@ namespace Torch.Mod.Messages
public override void ProcessClient() public override void ProcessClient()
{ {
if (TorchModCore.Debug)
{
MyAPIGateway.Utilities.ShowMessage("Torch", $"Joining server {Address} with delay {Delay}");
}
if (Delay <= 0) if (Delay <= 0)
{ {
MyAPIGateway.Multiplayer.JoinServer(Address); MyAPIGateway.Multiplayer.JoinServer(Address);

View File

@@ -1,9 +1,4 @@
using System; using ProtoBuf;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProtoBuf;
namespace Torch.Mod.Messages namespace Torch.Mod.Messages
{ {
@@ -17,36 +12,9 @@ namespace Torch.Mod.Messages
[ProtoContract] [ProtoContract]
public abstract class MessageBase public abstract class MessageBase
{ {
[ProtoMember(101)]
public ulong SenderId; public ulong SenderId;
public abstract void ProcessClient(); public abstract void ProcessClient();
public abstract void ProcessServer(); public abstract void ProcessServer();
//members below not serialized, they're just metadata about the intended target(s) of this message
internal MessageTarget TargetType;
internal ulong Target;
internal ulong[] Ignore;
internal byte[] CompressedData;
}
public enum MessageTarget
{
/// <summary>
/// Send to Target
/// </summary>
Single,
/// <summary>
/// Send to Server
/// </summary>
Server,
/// <summary>
/// Send to all Clients (only valid from server)
/// </summary>
AllClients,
/// <summary>
/// Send to all except those steam ID listed in Ignore
/// </summary>
AllExcept,
} }
} }

View File

@@ -1,161 +1,50 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Sandbox.ModAPI; using Sandbox.ModAPI;
using Torch.Mod.Messages; using Torch.Mod.Messages;
using VRage; using VRage.Game.Components;
using VRage.Collections;
using VRage.Game.ModAPI; using VRage.Game.ModAPI;
using VRage.Network; #if TORCH
using VRage.Utils; using Torch.Utils;
using Task = ParallelTasks.Task; using VRage.Library.Collections;
using System.Reflection;
#endif
namespace Torch.Mod namespace Torch.Mod
{ {
public static class ModCommunication [MySessionComponentDescriptor(MyUpdateOrder.AfterSimulation)]
public class ModCommunication : MySessionComponentBase
{ {
public const ushort NET_ID = 4352; public const ulong MOD_ID = 2915950488;
private static bool _closing = false; private const ushort CHANNEL = 7654;
private static BlockingCollection<MessageBase> _processing;
private static MyConcurrentPool<IncomingMessage> _messagePool;
private static List<IMyPlayer> _playerCache;
public static void Register() public override void BeforeStart()
{ {
MyLog.Default.WriteLineAndConsole("TORCH MOD: Registering mod communication."); base.BeforeStart();
_processing = new BlockingCollection<MessageBase>(new ConcurrentQueue<MessageBase>()); MyAPIGateway.Multiplayer.RegisterSecureMessageHandler(CHANNEL, MessageHandler);
_playerCache = new List<IMyPlayer>();
_messagePool = new MyConcurrentPool<IncomingMessage>(8);
MyAPIGateway.Multiplayer.RegisterMessageHandler(NET_ID, MessageHandler);
//background thread to handle de/compression and processing
_closing = false;
MyAPIGateway.Parallel.StartBackground(DoProcessing);
MyLog.Default.WriteLineAndConsole("TORCH MOD: Mod communication registered successfully.");
} }
public static void Unregister() private void MessageHandler(ushort channel, byte[] data, ulong sender, bool fromServer)
{ {
MyLog.Default.WriteLineAndConsole("TORCH MOD: Unregistering mod communication."); if (!fromServer)
MyAPIGateway.Multiplayer?.UnregisterMessageHandler(NET_ID, MessageHandler); return;
_processing?.CompleteAdding();
_closing = true; var message = MyAPIGateway.Utilities.SerializeFromBinary<MessageBase>(data);
//_task.Wait(); message.SenderId = sender;
if (MyAPIGateway.Multiplayer.IsServer) message.ProcessServer();
else message.ProcessClient();
} }
private static void MessageHandler(byte[] bytes)
{
var m = _messagePool.Get();
m.CompressedData = bytes;
#if TORCH #if TORCH
m.SenderId = MyEventContext.Current.Sender.Value; [ReflectedMethodInfo(typeof(MyAPIUtilities), "VRage.Game.ModAPI.IMyUtilities.SerializeToBinary")]
#endif private static MethodInfo _serializeMethod = null!;
_processing.Add(m); private static readonly CacheList<IMyPlayer> Players = new();
}
public static void DoProcessing() private static byte[] Serialize(MessageBase message)
{ {
while (!_closing) return (byte[])_serializeMethod.MakeGenericMethod(message.GetType())
{ .Invoke(MyAPIGateway.Utilities, new object[] { message });
try
{
MessageBase m;
try
{
m = _processing.Take();
}
catch
{
continue;
}
MyLog.Default.WriteLineAndConsole($"Processing message: {m.GetType().Name}");
if (m is IncomingMessage) //process incoming messages
{
MessageBase i;
try
{
var o = MyCompression.Decompress(m.CompressedData);
m.CompressedData = null;
_messagePool.Return((IncomingMessage)m);
i = MyAPIGateway.Utilities.SerializeFromBinary<MessageBase>(o);
}
catch (Exception ex)
{
MyLog.Default.WriteLineAndConsole($"TORCH MOD: Failed to deserialize message! {ex}");
continue;
}
if (TorchModCore.Debug)
MyAPIGateway.Utilities.ShowMessage("Torch", $"Received message of type {i.GetType().Name}");
if (MyAPIGateway.Multiplayer.IsServer)
i.ProcessServer();
else
i.ProcessClient();
}
else //process outgoing messages
{
if (TorchModCore.Debug)
MyAPIGateway.Utilities.ShowMessage("Torch", $"Sending message of type {m.GetType().Name}");
var b = MyAPIGateway.Utilities.SerializeToBinary(m);
m.CompressedData = MyCompression.Compress(b);
switch (m.TargetType)
{
case MessageTarget.Single:
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, m.Target);
break;
case MessageTarget.Server:
MyAPIGateway.Multiplayer.SendMessageToServer(NET_ID, m.CompressedData);
break;
case MessageTarget.AllClients:
MyAPIGateway.Players.GetPlayers(_playerCache);
foreach (var p in _playerCache)
{
if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId)
continue;
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId);
}
break;
case MessageTarget.AllExcept:
MyAPIGateway.Players.GetPlayers(_playerCache);
foreach (var p in _playerCache)
{
if (p.SteamUserId == MyAPIGateway.Multiplayer.MyId || m.Ignore.Contains(p.SteamUserId))
continue;
MyAPIGateway.Multiplayer.SendMessageTo(NET_ID, m.CompressedData, p.SteamUserId);
}
break;
default:
throw new Exception();
}
_playerCache.Clear();
}
}
catch (Exception ex)
{
MyLog.Default.WriteLineAndConsole($"TORCH MOD: Exception occurred in communication thread! {ex}");
}
}
MyLog.Default.WriteLineAndConsole("TORCH MOD: INFO: Communication thread shut down successfully! THIS IS NOT AN ERROR");
//exit signal received. Clean everything and GTFO
_processing?.Dispose();
_processing = null;
_messagePool?.Clean();
_messagePool = null;
_playerCache = null;
} }
public static void SendMessageTo(MessageBase message, ulong target) public static void SendMessageTo(MessageBase message, ulong target)
@@ -163,12 +52,7 @@ namespace Torch.Mod
if (!MyAPIGateway.Multiplayer.IsServer) if (!MyAPIGateway.Multiplayer.IsServer)
throw new Exception("Only server can send targeted messages"); throw new Exception("Only server can send targeted messages");
if (_closing) MyAPIGateway.Multiplayer.SendMessageTo(CHANNEL, Serialize(message), target);
return;
message.Target = target;
message.TargetType = MessageTarget.Single;
_processing.Add(message);
} }
public static void SendMessageToClients(MessageBase message) public static void SendMessageToClients(MessageBase message)
@@ -176,11 +60,7 @@ namespace Torch.Mod
if (!MyAPIGateway.Multiplayer.IsServer) if (!MyAPIGateway.Multiplayer.IsServer)
throw new Exception("Only server can send targeted messages"); throw new Exception("Only server can send targeted messages");
if (_closing) MyAPIGateway.Multiplayer.SendMessageToOthers(CHANNEL, Serialize(message));
return;
message.TargetType = MessageTarget.AllClients;
_processing.Add(message);
} }
public static void SendMessageExcept(MessageBase message, params ulong[] ignoredUsers) public static void SendMessageExcept(MessageBase message, params ulong[] ignoredUsers)
@@ -188,21 +68,20 @@ namespace Torch.Mod
if (!MyAPIGateway.Multiplayer.IsServer) if (!MyAPIGateway.Multiplayer.IsServer)
throw new Exception("Only server can send targeted messages"); throw new Exception("Only server can send targeted messages");
if (_closing) using var players = Players;
return; MyAPIGateway.Multiplayer.Players.GetPlayers(players, player => !ignoredUsers.Contains(player.SteamUserId));
message.TargetType = MessageTarget.AllExcept; var data = Serialize(message);
message.Ignore = ignoredUsers; foreach (var player in players)
_processing.Add(message); {
MyAPIGateway.Multiplayer.SendMessageTo(CHANNEL, data, player.SteamUserId);
}
} }
public static void SendMessageToServer(MessageBase message) public static void SendMessageToServer(MessageBase message)
{ {
if (_closing) throw new NotSupportedException();
return; }
#endif
message.TargetType = MessageTarget.Server;
_processing.Add(message);
}
} }
} }

View File

@@ -9,13 +9,11 @@
<Import_RootNamespace>Torch.Mod</Import_RootNamespace> <Import_RootNamespace>Torch.Mod</Import_RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Messages\IncomingMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\JoinServerMessage.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Messages\JoinServerMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\NotificationMessage.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Messages\NotificationMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\DialogMessage.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Messages\DialogMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\MessageBase.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Messages\MessageBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\VoxelResetMessage.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Messages\VoxelResetMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ModCommunication.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ModCommunication.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TorchModCore.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,51 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sandbox.ModAPI;
using VRage.Game.Components;
namespace Torch.Mod
{
[MySessionComponentDescriptor(MyUpdateOrder.AfterSimulation)]
public class TorchModCore : MySessionComponentBase
{
public const ulong MOD_ID = 2722000298;
private static bool _init;
public static bool Debug;
public override void UpdateAfterSimulation()
{
if (_init)
return;
_init = true;
ModCommunication.Register();
MyAPIGateway.Utilities.MessageEntered += Utilities_MessageEntered;
}
private void Utilities_MessageEntered(string messageText, ref bool sendToOthers)
{
if (messageText == "@!debug")
{
Debug = !Debug;
MyAPIGateway.Utilities.ShowMessage("Torch", $"Debug: {Debug}");
sendToOthers = false;
}
}
protected override void UnloadData()
{
try
{
MyAPIGateway.Utilities.MessageEntered -= Utilities_MessageEntered;
ModCommunication.Unregister();
}
catch
{
//session unloading, don't care
}
}
}
}

View File

@@ -146,7 +146,7 @@ namespace Torch.Server.Managers
{ {
DedicatedConfig.Mods.Clear(); DedicatedConfig.Mods.Clear();
//remove the Torch mod to avoid running multiple copies of it //remove the Torch mod to avoid running multiple copies of it
DedicatedConfig.SelectedWorld.WorldConfiguration.Mods.RemoveAll(m => m.PublishedFileId == TorchModCore.MOD_ID); DedicatedConfig.SelectedWorld.WorldConfiguration.Mods.RemoveAll(m => m.PublishedFileId == ModCommunication.MOD_ID);
foreach (var m in DedicatedConfig.SelectedWorld.WorldConfiguration.Mods) foreach (var m in DedicatedConfig.SelectedWorld.WorldConfiguration.Mods)
DedicatedConfig.Mods.Add(new ModItemInfo(m)); DedicatedConfig.Mods.Add(new ModItemInfo(m));
Task.Run(() => DedicatedConfig.UpdateAllModInfosAsync()); Task.Run(() => DedicatedConfig.UpdateAllModInfosAsync());
@@ -161,7 +161,7 @@ namespace Torch.Server.Managers
{ {
DedicatedConfig.Mods.Clear(); DedicatedConfig.Mods.Clear();
//remove the Torch mod to avoid running multiple copies of it //remove the Torch mod to avoid running multiple copies of it
DedicatedConfig.SelectedWorld.WorldConfiguration.Mods.RemoveAll(m => m.PublishedFileId == TorchModCore.MOD_ID); DedicatedConfig.SelectedWorld.WorldConfiguration.Mods.RemoveAll(m => m.PublishedFileId == ModCommunication.MOD_ID);
foreach (var m in DedicatedConfig.SelectedWorld.WorldConfiguration.Mods) foreach (var m in DedicatedConfig.SelectedWorld.WorldConfiguration.Mods)
DedicatedConfig.Mods.Add(new ModItemInfo(m)); DedicatedConfig.Mods.Add(new ModItemInfo(m));
Task.Run(() => DedicatedConfig.UpdateAllModInfosAsync()); Task.Run(() => DedicatedConfig.UpdateAllModInfosAsync());

View File

@@ -3,11 +3,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Microsoft.Diagnostics.Runtime; using Microsoft.Diagnostics.Runtime;
using NLog; using NLog;
using PropertyChanged; using PropertyChanged;
@@ -21,8 +20,6 @@ using Torch.API.Session;
using Torch.Commands; using Torch.Commands;
using Torch.Managers.PatchManager; using Torch.Managers.PatchManager;
using Torch.Mod; using Torch.Mod;
using Torch.Mod.Messages;
using Torch.Patches;
using Torch.Server.Commands; using Torch.Server.Commands;
using Torch.Server.Managers; using Torch.Server.Managers;
using Torch.Utils; using Torch.Utils;
@@ -211,7 +208,7 @@ namespace Torch.Server
Environment.Exit(0); Environment.Exit(0);
#endif #endif
var exe = Assembly.GetExecutingAssembly().Location.Replace("dll", "exe"); var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe");
var args = Environment.GetCommandLineArgs(); var args = Environment.GetCommandLineArgs();
@@ -229,6 +226,8 @@ namespace Torch.Server
} }
Process.Start(exe, $"--waitForPid {Environment.ProcessId} --tempAutostart true {string.Join(" ", args)}"); Process.Start(exe, $"--waitForPid {Environment.ProcessId} --tempAutostart true {string.Join(" ", args)}");
Environment.Exit(0);
}) })
{ {
Name = "Restart thread" Name = "Restart thread"
@@ -243,14 +242,12 @@ namespace Torch.Server
case TorchSessionState.Unloading: case TorchSessionState.Unloading:
_watchdog?.Dispose(); _watchdog?.Dispose();
_watchdog = null; _watchdog = null;
ModCommunication.Unregister();
break; break;
case TorchSessionState.Loaded: case TorchSessionState.Loaded:
_multiplayerManagerDedicated = CurrentSession.Managers.GetManager<MultiplayerManagerDedicated>(); _multiplayerManagerDedicated = CurrentSession.Managers.GetManager<MultiplayerManagerDedicated>();
_multiplayerManagerDedicated.PlayerJoined += MultiplayerManagerDedicatedOnPlayerJoined; _multiplayerManagerDedicated.PlayerJoined += MultiplayerManagerDedicatedOnPlayerJoined;
_multiplayerManagerDedicated.PlayerLeft += MultiplayerManagerDedicatedOnPlayerLeft; _multiplayerManagerDedicated.PlayerLeft += MultiplayerManagerDedicatedOnPlayerLeft;
CurrentSession.Managers.GetManager<CommandManager>().RegisterCommandModule(typeof(WhitelistCommands)); CurrentSession.Managers.GetManager<CommandManager>().RegisterCommandModule(typeof(WhitelistCommands));
ModCommunication.Register();
break; break;
case TorchSessionState.Loading: case TorchSessionState.Loading:
case TorchSessionState.Unloaded: case TorchSessionState.Unloaded:

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Threading; using System.Threading;
using NLog; using NLog;
using VRage; using VRage;
@@ -31,7 +32,7 @@ internal class UnhandledExceptionHandler
{ {
Console.WriteLine("Restarting in 5 seconds."); Console.WriteLine("Restarting in 5 seconds.");
Thread.Sleep(5000); Thread.Sleep(5000);
var exe = typeof(Program).Assembly.Location; var exe = Path.Combine(AppContext.BaseDirectory, "Torch.Server.exe");
Process.Start(exe, $"-waitForPid {Environment.ProcessId} {_config}"); Process.Start(exe, $"-waitForPid {Environment.ProcessId} {_config}");
} }

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using NLog; using NLog;
@@ -9,7 +10,7 @@ using Torch.API;
namespace Torch.Managers namespace Torch.Managers
{ {
public class FilesystemManager : Manager public partial class FilesystemManager : Manager
{ {
private static readonly Logger _log = LogManager.GetCurrentClassLogger(); private static readonly Logger _log = LogManager.GetCurrentClassLogger();
/// <summary> /// <summary>
@@ -51,19 +52,27 @@ namespace Torch.Managers
} }
} }
/// <summary>
/// Move the given file (if it exists) to a temporary directory that will be cleared the next time the application starts.
/// </summary>
public void SoftDelete(string path, string file) public void SoftDelete(string path, string file)
{ {
string source = Path.Combine(path, file); var source = Path.Combine(path, file);
if (!File.Exists(source)) if (!File.Exists(source))
return; return;
var rand = Path.GetRandomFileName();
var dest = Path.Combine(TempDirectory, rand); var tempFilePath = Path.Combine(path, file + ".old");
File.Move(source, rand); if (File.Exists(tempFilePath))
string rsource = Path.Combine(path, rand); File.Delete(tempFilePath);
File.Move(rsource, dest);
} var errorCode = Rename(source, tempFilePath);
if (Marshal.GetExceptionForHR(errorCode) is { } exception)
throw exception;
}
[LibraryImport("msvcrt", SetLastError = true, EntryPoint = "rename")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
private static partial int Rename(
[MarshalAs(UnmanagedType.LPWStr)]
string oldpath,
[MarshalAs(UnmanagedType.LPWStr)]
string newpath);
} }
} }

View File

@@ -50,7 +50,7 @@ namespace Torch.Session
{ {
_overrideMods = new Dictionary<ulong, MyObjectBuilder_Checkpoint.ModItem>(); _overrideMods = new Dictionary<ulong, MyObjectBuilder_Checkpoint.ModItem>();
if (Torch.Config.UgcServiceType == UGCServiceType.Steam) if (Torch.Config.UgcServiceType == UGCServiceType.Steam)
_overrideMods.Add(TorchModCore.MOD_ID, ModItemUtils.Create(TorchModCore.MOD_ID)); _overrideMods.Add(ModCommunication.MOD_ID, ModItemUtils.Create(ModCommunication.MOD_ID));
} }
/// <inheritdoc/> /// <inheritdoc/>

View File

@@ -13,6 +13,7 @@
<Platforms>AnyCPU</Platforms> <Platforms>AnyCPU</Platforms>
<EnableWindowsTargeting>true</EnableWindowsTargeting> <EnableWindowsTargeting>true</EnableWindowsTargeting>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile> <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<DefineConstants>TRACE;TORCH</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="$(Configuration) == 'Release'"> <PropertyGroup Condition="$(Configuration) == 'Release'">