Add async session save method, random tweaks

This commit is contained in:
John Gross
2017-02-06 01:22:35 -08:00
parent d5159dc72a
commit ff3cdfd099
6 changed files with 45 additions and 18 deletions

View File

@@ -13,8 +13,7 @@ namespace Torch.API
event Action<IChatItem> MessageReceived;
Dictionary<ulong, IPlayer> Players { get; }
List<IChatItem> Chat { get; }
void SendMessage(string message);
void SendMessage(string message, long playerId, string author = "Server", string font = MyFontEnum.Blue);
void SendMessage(string message, string author = "Server", long playerId = 0, string font = MyFontEnum.Blue);
void KickPlayer(ulong id);
void BanPlayer(ulong id, bool banned = true);
IMyPlayer GetPlayerBySteamId(ulong id);

View File

@@ -27,6 +27,7 @@ namespace Torch.API
public interface ITorchServer : ITorchBase
{
bool IsRunning { get; }
string InstancePath { get; }
}
public interface ITorchClient : ITorchBase

View File

@@ -39,9 +39,9 @@ namespace Torch.Server
return;
}
string configName = args.Length > 0 ? args[0] : "TorchConfig.xml";
var configName = args.FirstOrDefault() ?? "TorchConfig.xml";
var configPath = Path.Combine(Directory.GetCurrentDirectory(), configName);
var options = new ServerConfig("Torch");
ServerConfig options;
if (File.Exists(configName))
{
_log.Info($"Loading config {configPath}");
@@ -50,6 +50,7 @@ namespace Torch.Server
else
{
_log.Info($"Generating default config at {configPath}");
options = new ServerConfig();
options.SaveTo(configPath);
}

View File

@@ -16,7 +16,6 @@ namespace Torch.Server
public string InstancePath { get; set; }
public string InstanceName { get; set; }
//public string SaveName { get; set; }
public int Autosave { get; set; }
public bool AutoRestart { get; set; }
@@ -30,7 +29,6 @@ namespace Torch.Server
public static ServerConfig LoadFrom(string path)
{
_log.Info($"Loading config from '{path}'");
try
{
var serializer = new XmlSerializer(typeof(ServerConfig));
@@ -43,14 +41,13 @@ namespace Torch.Server
}
catch (Exception e)
{
Console.WriteLine(e);
_log.Error(e);
return null;
}
}
public bool SaveTo(string path)
{
_log.Info($"Saving config to '{path}'");
try
{
var serializer = new XmlSerializer(typeof(ServerConfig));
@@ -62,7 +59,7 @@ namespace Torch.Server
}
catch (Exception e)
{
Console.WriteLine(e);
_log.Error(e);
return false;
}
}

View File

@@ -74,15 +74,10 @@ namespace Torch.Managers
return p;
}
public void SendMessage(string message)
{
SendMessage(message, 0);
}
/// <summary>
/// Send a message in chat.
/// </summary>
public void SendMessage(string message, long playerId, string author = "Server", string font = MyFontEnum.Red)
public void SendMessage(string message, string author = "Server", long playerId = 0, string font = MyFontEnum.Red)
{
var msg = new ScriptedChatMsg {Author = author, Font = font, Target = playerId, Text = message};
MyMultiplayerBase.SendScriptedChatMessage(ref msg);

View File

@@ -10,6 +10,8 @@ using System.Threading.Tasks;
using NLog;
using Sandbox;
using Sandbox.Game;
using Sandbox.Game.Multiplayer;
using Sandbox.Game.Screens.Helpers;
using Sandbox.Game.World;
using Sandbox.ModAPI;
using SpaceEngineers.Game;
@@ -23,10 +25,9 @@ namespace Torch
public abstract class TorchBase : ITorchBase
{
/// <summary>
/// Dirty hack because *keen*
/// Use only if absolutely necessary.
/// Hack because *keen*.
/// Use only if necessary, prefer dependency injection.
/// </summary>
[Obsolete]
public static ITorchBase Instance { get; private set; }
protected static Logger Log { get; } = LogManager.GetLogger("Torch");
public Version TorchVersion { get; protected set; }
@@ -54,6 +55,37 @@ namespace Torch
Multiplayer = new MultiplayerManager(this);
}
public async Task SaveGameAsync()
{
Log.Info("Saving game");
if (MySandboxGame.IsGameReady && !MyAsyncSaving.InProgress && Sync.IsServer && !(MySession.Static.LocalCharacter?.IsDead ?? true))
{
using (var e = new AutoResetEvent(false))
{
MyAsyncSaving.Start(() =>
{
MySector.ResetEyeAdaptation = true;
e.Set();
});
await Task.Run(() =>
{
if (!e.WaitOne(60000))
{
Log.Error("Save failed!");
Multiplayer.SendMessage("Save timed out!", author: "Error");
}
});
}
}
else
{
Log.Error("Cannot save");
}
}
#region Game Actions
/// <summary>
/// Invokes an action on the game thread.
/// </summary>
@@ -117,6 +149,8 @@ namespace Torch
throw new TimeoutException("The game action timed out.");
}
#endregion
public virtual void Init()
{
Debug.Assert(!_init, "Torch instance is already initialized.");