Moved SaveGameStatus to seperate file, guarded against null callbacks and added documentation

This commit is contained in:
Alexander Qvist-Hellum
2017-07-07 00:34:45 +02:00
parent 1251b945bc
commit 8ab16c3d30
6 changed files with 37 additions and 14 deletions

View File

@@ -25,6 +25,10 @@ namespace Torch.API
bool IsOnGameThread(); bool IsOnGameThread();
void Start(); void Start();
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); void Save(long callerId);
void Init(); void Init();
T GetManager<T>() where T : class, IManager; T GetManager<T>() where T : class, IManager;

View File

@@ -222,11 +222,17 @@ namespace Torch.Server
State = ServerState.Stopped; State = ServerState.Stopped;
} }
/// <inheritdoc/>
public override void Save(long callerId) public override void Save(long callerId)
{ {
base.SaveGameAsync((statusCode) => SaveCompleted(statusCode, 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) private void SaveCompleted(SaveGameStatus statusCode, long callerId)
{ {
switch (statusCode) switch (statusCode)

View File

@@ -67,7 +67,11 @@ 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.")] [Command("save", "Saves the game.")]
public void Save() public void Save()
{ {

13
Torch/SaveGameStatus.cs Normal file
View 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
};
}

View File

@@ -144,6 +144,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" />

View File

@@ -31,14 +31,6 @@ using VRage.Utils;
namespace Torch namespace Torch
{ {
public enum SaveGameStatus : byte
{
Success = 0,
SaveInProgress = 1,
GameNotReady = 2,
TimedOut = 3
};
/// <summary> /// <summary>
/// Base class for code shared between the Torch client and server. /// Base class for code shared between the Torch client and server.
/// </summary> /// </summary>
@@ -123,11 +115,11 @@ namespace Torch
if (!MySandboxGame.IsGameReady) if (!MySandboxGame.IsGameReady)
{ {
callback(SaveGameStatus.GameNotReady); callback?.Invoke(SaveGameStatus.GameNotReady);
} }
else if(MyAsyncSaving.InProgress) else if(MyAsyncSaving.InProgress)
{ {
callback(SaveGameStatus.SaveInProgress); callback?.Invoke(SaveGameStatus.SaveInProgress);
} }
else else
{ {
@@ -142,10 +134,12 @@ namespace Torch
await Task.Run(() => await Task.Run(() =>
{ {
if (e.WaitOne(60000)) if (e.WaitOne(60000))
callback(SaveGameStatus.Success); {
return; callback?.Invoke(SaveGameStatus.Success);
return;
}
callback(SaveGameStatus.TimedOut); callback?.Invoke(SaveGameStatus.TimedOut);
}).ConfigureAwait(false); }).ConfigureAwait(false);
} }
} }
@@ -292,6 +286,7 @@ namespace Torch
pluginList.Add(this); pluginList.Add(this);
} }
/// <inheritdoc/>
public virtual void Save(long callerId) public virtual void Save(long callerId)
{ {