Merge pull request #42 from Maldark/master
Add async /save command for admins+ and server console.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -75,6 +75,12 @@ namespace Torch.API
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
void Stop();
|
void Stop();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a save of the game.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="callerId">Id of the player who initiated the save.</param>
|
||||||
|
void Save(long callerId);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initialize the Torch instance.
|
/// Initialize the Torch instance.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@@ -219,5 +219,41 @@ namespace Torch.Server
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public override void Save(long callerId)
|
||||||
|
{
|
||||||
|
base.SaveGameAsync((statusCode) => SaveCompleted(statusCode, callerId));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Callback for when save has finished.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="statusCode">Return code of the save operation</param>
|
||||||
|
/// <param name="callerId">Caller of the save operation</param>
|
||||||
|
private void SaveCompleted(SaveGameStatus statusCode, long callerId)
|
||||||
|
{
|
||||||
|
switch (statusCode)
|
||||||
|
{
|
||||||
|
case SaveGameStatus.Success:
|
||||||
|
Log.Info("Save completed.");
|
||||||
|
Multiplayer.SendMessage("Saved game.", playerId: callerId);
|
||||||
|
break;
|
||||||
|
case SaveGameStatus.SaveInProgress:
|
||||||
|
Log.Error("Save failed, a save is already in progress.");
|
||||||
|
Multiplayer.SendMessage("Save failed, a save is already in progress.", playerId: callerId, font: MyFontEnum.Red);
|
||||||
|
break;
|
||||||
|
case SaveGameStatus.GameNotReady:
|
||||||
|
Log.Error("Save failed, game was not ready.");
|
||||||
|
Multiplayer.SendMessage("Save failed, game was not ready.", playerId: callerId, font: MyFontEnum.Red);
|
||||||
|
break;
|
||||||
|
case SaveGameStatus.TimedOut:
|
||||||
|
Log.Error("Save failed, save timed out.");
|
||||||
|
Multiplayer.SendMessage("Save failed, save timed out.", playerId: callerId, font: MyFontEnum.Red);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -67,5 +67,16 @@ namespace Torch.Commands
|
|||||||
Context.Respond("Stopping server.");
|
Context.Respond("Stopping server.");
|
||||||
Context.Torch.Stop();
|
Context.Torch.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a save of the game.
|
||||||
|
/// Caller id defaults to 0 in the case of triggering the chat command from server.
|
||||||
|
/// </summary>
|
||||||
|
[Command("save", "Saves the game.")]
|
||||||
|
public void Save()
|
||||||
|
{
|
||||||
|
Context.Respond("Saving game.");
|
||||||
|
Context.Torch.Save(Context.Player?.IdentityId ?? 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
13
Torch/SaveGameStatus.cs
Normal file
13
Torch/SaveGameStatus.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
namespace Torch
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Describes the possible outcomes when attempting to save the game progress.
|
||||||
|
/// </summary>
|
||||||
|
public enum SaveGameStatus : byte
|
||||||
|
{
|
||||||
|
Success = 0,
|
||||||
|
SaveInProgress = 1,
|
||||||
|
GameNotReady = 2,
|
||||||
|
TimedOut = 3
|
||||||
|
};
|
||||||
|
}
|
@@ -146,6 +146,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="ChatMessage.cs" />
|
<Compile Include="ChatMessage.cs" />
|
||||||
|
<Compile Include="SaveGameStatus.cs" />
|
||||||
<Compile Include="Collections\KeyTree.cs" />
|
<Compile Include="Collections\KeyTree.cs" />
|
||||||
<Compile Include="Collections\ObservableDictionary.cs" />
|
<Compile Include="Collections\ObservableDictionary.cs" />
|
||||||
<Compile Include="Collections\RollingAverage.cs" />
|
<Compile Include="Collections\RollingAverage.cs" />
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
@@ -123,10 +123,24 @@ namespace Torch
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SaveGameAsync()
|
public bool IsOnGameThread()
|
||||||
|
{
|
||||||
|
return Thread.CurrentThread.ManagedThreadId == MySandboxGame.Static.UpdateThread.ManagedThreadId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SaveGameAsync(Action<SaveGameStatus> callback)
|
||||||
{
|
{
|
||||||
Log.Info("Saving game");
|
Log.Info("Saving game");
|
||||||
if (MySandboxGame.IsGameReady && !MyAsyncSaving.InProgress && Sync.IsServer && !(MySession.Static.LocalCharacter?.IsDead ?? true))
|
|
||||||
|
if (!MySandboxGame.IsGameReady)
|
||||||
|
{
|
||||||
|
callback?.Invoke(SaveGameStatus.GameNotReady);
|
||||||
|
}
|
||||||
|
else if(MyAsyncSaving.InProgress)
|
||||||
|
{
|
||||||
|
callback?.Invoke(SaveGameStatus.SaveInProgress);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
using (var e = new AutoResetEvent(false))
|
using (var e = new AutoResetEvent(false))
|
||||||
{
|
{
|
||||||
@@ -139,17 +153,15 @@ namespace Torch
|
|||||||
await Task.Run(() =>
|
await Task.Run(() =>
|
||||||
{
|
{
|
||||||
if (e.WaitOne(60000))
|
if (e.WaitOne(60000))
|
||||||
|
{
|
||||||
|
callback?.Invoke(SaveGameStatus.Success);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Log.Error("Save failed!");
|
callback?.Invoke(SaveGameStatus.TimedOut);
|
||||||
Multiplayer.SendMessage("Save timed out!", "Error");
|
|
||||||
}).ConfigureAwait(false);
|
}).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
Log.Error("Cannot save");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Game Actions
|
#region Game Actions
|
||||||
@@ -293,7 +305,13 @@ namespace Torch
|
|||||||
pluginList.Add(this);
|
pluginList.Add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc/>
|
||||||
|
public virtual void Save(long callerId)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc
|
||||||
public virtual void Start()
|
public virtual void Start()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user