Implement Torch API in client
This commit is contained in:
@@ -11,8 +11,9 @@ namespace Torch.API
|
||||
event Action SessionLoaded;
|
||||
IMultiplayer Multiplayer { get; }
|
||||
IPluginManager Plugins { get; }
|
||||
void GameAction(Action action);
|
||||
void BeginGameAction(Action action, Action<object> callback = null, object state = null);
|
||||
void DoGameAction(Action action);
|
||||
Task DoGameActionAsync(Action action);
|
||||
string[] RunArgs { get; set; }
|
||||
void Start();
|
||||
void Stop();
|
||||
void Init();
|
||||
@@ -21,7 +22,6 @@ namespace Torch.API
|
||||
public interface ITorchServer : ITorchBase
|
||||
{
|
||||
bool IsRunning { get; }
|
||||
string[] RunArgs { get; set; }
|
||||
}
|
||||
|
||||
public interface ITorchClient : ITorchBase
|
||||
|
@@ -7,11 +7,11 @@ namespace Torch.Client
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var game = new GameInitializer(args);
|
||||
var client = new TorchClient();
|
||||
|
||||
try
|
||||
{
|
||||
game.TryInit();
|
||||
client.Init();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -19,7 +19,7 @@ namespace Torch.Client
|
||||
return;
|
||||
}
|
||||
|
||||
game.RunGame();
|
||||
client.Start();
|
||||
}
|
||||
}
|
||||
}
|
@@ -86,8 +86,9 @@
|
||||
<Reference Include="PresentationFramework" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="GameInitializer.cs" />
|
||||
<Compile Include="TorchClient.cs" />
|
||||
<Compile Include="TorchConsoleScreen.cs" />
|
||||
<Compile Include="TorchMainMenuScreen.cs" />
|
||||
<Compile Include="TorchSettingsScreen.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs">
|
||||
@@ -114,6 +115,10 @@
|
||||
<AppDesigner Include="Properties\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Torch.API\Torch.API.csproj">
|
||||
<Project>{fba5d932-6254-4a1e-baf4-e229fa94e3c2}</Project>
|
||||
<Name>Torch.API</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Torch\Torch.csproj">
|
||||
<Project>{7E01635C-3B67-472E-BCD6-C5539564F214}</Project>
|
||||
<Name>Torch</Name>
|
||||
|
@@ -6,54 +6,31 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using Sandbox;
|
||||
using Sandbox.Engine.Platform;
|
||||
using Sandbox.Game;
|
||||
using Sandbox.Game.Gui;
|
||||
using Sandbox.Graphics;
|
||||
using Sandbox.Graphics.GUI;
|
||||
using Sandbox.Gui;
|
||||
using SpaceEngineers.Game;
|
||||
using SpaceEngineers.Game.GUI;
|
||||
using Torch.API;
|
||||
using VRage.FileSystem;
|
||||
using VRage.Game;
|
||||
using VRage.Input;
|
||||
using VRage.Utils;
|
||||
using VRageRender;
|
||||
using Game = Sandbox.Engine.Platform.Game;
|
||||
|
||||
namespace Torch.Client
|
||||
{
|
||||
public class GameInitializer
|
||||
class TorchClient : TorchBase, ITorchClient
|
||||
{
|
||||
private MyCommonProgramStartup _startup;
|
||||
private IMyRender _renderer;
|
||||
private const uint APP_ID = 244850;
|
||||
private PluginManager _pluginManager;
|
||||
private readonly string[] _args;
|
||||
private VRageGameServices _services;
|
||||
|
||||
public GameInitializer(string[] args)
|
||||
{
|
||||
_args = args;
|
||||
}
|
||||
|
||||
public void TryInit()
|
||||
public override void Init()
|
||||
{
|
||||
if (!File.Exists("steam_appid.txt"))
|
||||
{
|
||||
Directory.SetCurrentDirectory(Path.GetDirectoryName(typeof(VRage.FastResourceLock).Assembly.Location) + "\\..");
|
||||
}
|
||||
|
||||
//Add myself to the credits because I'm awesome.
|
||||
var credits = new MyCreditsDepartment("Torch Developed By") { Persons = new List<MyCreditsPerson>
|
||||
{
|
||||
new MyCreditsPerson("JIMMACLE"),
|
||||
new MyCreditsPerson("REXXAR"),
|
||||
new MyCreditsPerson("PHOENIXTHESAGE")
|
||||
} };
|
||||
MyPerGameSettings.Credits.Departments.Add(credits);
|
||||
|
||||
SpaceEngineersGame.SetupBasicGameInfo();
|
||||
_startup = new MyCommonProgramStartup(_args);
|
||||
_startup = new MyCommonProgramStartup(RunArgs);
|
||||
if (_startup.PerformReporting())
|
||||
return;
|
||||
|
||||
@@ -73,6 +50,8 @@ namespace Torch.Client
|
||||
_renderer = null;
|
||||
SpaceEngineersGame.SetupPerGameSettings();
|
||||
|
||||
OverrideMenus();
|
||||
|
||||
InitializeRender();
|
||||
|
||||
_services = new VRageGameServices(mySteamService);
|
||||
@@ -84,42 +63,34 @@ namespace Torch.Client
|
||||
MyInitializer.InvokeAfterRun();
|
||||
}
|
||||
|
||||
public void RunGame()
|
||||
private void OverrideMenus()
|
||||
{
|
||||
using (var spaceEngineersGame = new SpaceEngineersGame(_services, _args))
|
||||
var credits = new MyCreditsDepartment("Torch Developed By")
|
||||
{
|
||||
Logger.Write("Starting SE...");
|
||||
spaceEngineersGame.OnGameLoaded += SpaceEngineersGame_OnGameLoaded;
|
||||
MyGuiSandbox.GuiControlCreated += GuiControlCreated;
|
||||
Persons = new List<MyCreditsPerson>
|
||||
{
|
||||
new MyCreditsPerson("JIMMACLE"),
|
||||
new MyCreditsPerson("REXXAR"),
|
||||
new MyCreditsPerson("PHOENIXTHESAGE")
|
||||
}
|
||||
};
|
||||
MyPerGameSettings.Credits.Departments.Insert(0, credits);
|
||||
|
||||
MyPerGameSettings.GUI.MainMenu = typeof(TorchMainMenuScreen);
|
||||
}
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
using (var spaceEngineersGame = new SpaceEngineersGame(_services, RunArgs))
|
||||
{
|
||||
Logger.Write("Starting client...");
|
||||
spaceEngineersGame.Run();
|
||||
}
|
||||
}
|
||||
|
||||
private void GuiControlCreated(object o)
|
||||
public override void Stop()
|
||||
{
|
||||
var menu = o as MyGuiScreenMainMenu;
|
||||
if (menu != null)
|
||||
{
|
||||
var pistonBtn = new MyGuiControlImageButton
|
||||
{
|
||||
Name = "TorchButton",
|
||||
Text = "Torch",
|
||||
HighlightType = MyGuiControlHighlightType.WHEN_CURSOR_OVER,
|
||||
Visible = true,
|
||||
};
|
||||
|
||||
menu.Controls.Add(pistonBtn);
|
||||
}
|
||||
}
|
||||
|
||||
private void SpaceEngineersGame_OnGameLoaded(object sender, EventArgs e)
|
||||
{
|
||||
_pluginManager = new PluginManager();
|
||||
_pluginManager.LoadAllPlugins();
|
||||
|
||||
//Fix Marek's name.
|
||||
MyPerGameSettings.Credits.Departments[1].Persons[0].Name.Clear().Append("MEARK ROAS");
|
||||
MyScreenManager.AddScreen(new TorchConsoleScreen());
|
||||
MySandboxGame.ExitThreadSafe();
|
||||
}
|
||||
|
||||
private void InitializeRender()
|
33
Torch.Client/TorchMainMenuScreen.cs
Normal file
33
Torch.Client/TorchMainMenuScreen.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Sandbox.Graphics;
|
||||
using Sandbox.Graphics.GUI;
|
||||
using Sandbox.Gui;
|
||||
using SpaceEngineers.Game.GUI;
|
||||
using VRage.Game;
|
||||
using VRage.Utils;
|
||||
using VRageMath;
|
||||
|
||||
namespace Torch.Client
|
||||
{
|
||||
public class TorchMainMenuScreen : MyGuiScreenMainMenu
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override void RecreateControls(bool constructor)
|
||||
{
|
||||
base.RecreateControls(constructor);
|
||||
var buttonSize = MyGuiControlButton.GetVisualStyle(MyGuiControlButtonStyleEnum.Default).NormalTexture.MinSizeGui;
|
||||
Vector2 leftButtonPositionOrigin = MyGuiManager.ComputeFullscreenGuiCoordinate(MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_BOTTOM) + new Vector2(buttonSize.X / 2f, 0f);
|
||||
var btn = MakeButton(leftButtonPositionOrigin - 9 * MyGuiConstants.MENU_BUTTONS_POSITION_DELTA, MyStringId.GetOrCompute("Torch"), TorchButtonClicked);
|
||||
Controls.Add(btn);
|
||||
}
|
||||
|
||||
private void TorchButtonClicked(MyGuiControlButton obj)
|
||||
{
|
||||
MyGuiSandbox.AddScreen(new TorchSettingsScreen());
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Sandbox.Graphics;
|
||||
using Sandbox.Graphics.GUI;
|
||||
using VRage.Utils;
|
||||
using VRageMath;
|
||||
@@ -11,18 +12,21 @@ namespace Torch.Client
|
||||
{
|
||||
public class TorchSettingsScreen : MyGuiScreenBase
|
||||
{
|
||||
public override string GetFriendlyName() => "Piston Settings";
|
||||
public override string GetFriendlyName() => "Torch Settings";
|
||||
|
||||
public TorchSettingsScreen() : base(new Vector2(0.5f), null, new Vector2(0.5f), true, null, 0f, 0f)
|
||||
public TorchSettingsScreen() : base(new Vector2(0.5f), null, Vector2.One, true)
|
||||
{
|
||||
this.BackgroundColor = new Vector4(0);
|
||||
RecreateControls(true);
|
||||
}
|
||||
|
||||
public sealed override void RecreateControls(bool constructor)
|
||||
{
|
||||
base.RecreateControls(constructor);
|
||||
AddCaption(MyStringId.GetOrCompute("Piston Settings"));
|
||||
AddCaption(MyStringId.GetOrCompute("Torch Settings"), null, new Vector2(0, 0), 1.2f);
|
||||
var pluginList = new MyGuiControlListbox {VisibleRowsCount = 20};
|
||||
foreach (var name in TorchBase.Instance.Plugins.GetPluginFolders())
|
||||
pluginList.Items.Add(new MyGuiControlListbox.Item(new StringBuilder(name)));
|
||||
Controls.Add(pluginList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -24,7 +24,6 @@ namespace Torch.Server
|
||||
public class TorchServer : TorchBase, ITorchServer
|
||||
{
|
||||
public Thread ServerThread { get; private set; }
|
||||
public string[] RunArgs { get; set; } = new string[0];
|
||||
public bool IsRunning { get; private set; }
|
||||
|
||||
public event Action SessionLoading;
|
||||
|
@@ -16,7 +16,7 @@
|
||||
<WrapPanel>
|
||||
<TextBlock Text="{Binding Time}"/>
|
||||
<TextBlock Text=" "/>
|
||||
<TextBlock Text="{Binding Sender.Name}" FontWeight="Bold"/>
|
||||
<TextBlock Text="{Binding Player.Name}" FontWeight="Bold"/>
|
||||
<TextBlock Text=": "/>
|
||||
<TextBlock Text="{Binding Message}"/>
|
||||
</WrapPanel>
|
||||
|
@@ -53,7 +53,7 @@ namespace Torch.Server
|
||||
|
||||
private void KickButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var player = PlayerList.SelectedItem as Player;
|
||||
var player = PlayerList.SelectedItem as IPlayer;
|
||||
if (player != null)
|
||||
{
|
||||
_server.Multiplayer.KickPlayer(player.SteamId);
|
||||
@@ -62,7 +62,7 @@ namespace Torch.Server
|
||||
|
||||
private void BanButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var player = PlayerList.SelectedItem as Player;
|
||||
var player = PlayerList.SelectedItem as IPlayer;
|
||||
if (player != null)
|
||||
{
|
||||
_server.Multiplayer.BanPlayer(player.SteamId);
|
||||
|
@@ -10,20 +10,12 @@ namespace Torch
|
||||
{
|
||||
public static class Logger
|
||||
{
|
||||
public const string Prefix = "[PISTON]";
|
||||
public const string Prefix = "[TORCH]";
|
||||
|
||||
public static void Write(string message)
|
||||
{
|
||||
var msg = $"{Prefix}: {message}";
|
||||
MySandboxGame.Log.WriteLineAndConsole(msg);
|
||||
if (!MySandboxGame.Log.LogEnabled)
|
||||
{
|
||||
try
|
||||
{
|
||||
MySandboxGame.Log.AppendToClosedLog(msg);
|
||||
}
|
||||
catch (Exception) { }
|
||||
}
|
||||
MyLog.Default.WriteLineAndConsole(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -50,11 +50,11 @@ namespace Torch
|
||||
_torch.SessionLoaded += OnSessionLoaded;
|
||||
}
|
||||
|
||||
public void KickPlayer(ulong steamId) => _torch.BeginGameAction(() => MyMultiplayer.Static.KickClient(steamId));
|
||||
public void KickPlayer(ulong steamId) => _torch.DoGameActionAsync(() => MyMultiplayer.Static.KickClient(steamId));
|
||||
|
||||
public void BanPlayer(ulong steamId, bool banned = true)
|
||||
{
|
||||
_torch.BeginGameAction(() =>
|
||||
_torch.DoGameActionAsync(() =>
|
||||
{
|
||||
MyMultiplayer.Static.BanClient(steamId, banned);
|
||||
if (_gameOwnerIds.ContainsKey(steamId))
|
||||
@@ -179,16 +179,16 @@ namespace Torch
|
||||
//Largely copied from SE
|
||||
private void ValidateAuthTicketResponse(ulong steamID, AuthSessionResponseEnum response, ulong ownerSteamID)
|
||||
{
|
||||
MyLog.Default.WriteLineAndConsole($"{Logger.Prefix} Server ValidateAuthTicketResponse ({response}), owner: {ownerSteamID}");
|
||||
Logger.Write($"Server ValidateAuthTicketResponse ({response}), owner: {ownerSteamID}");
|
||||
|
||||
if (steamID != ownerSteamID)
|
||||
{
|
||||
MyLog.Default.WriteLineAndConsole($"User {steamID} is using a game owned by {ownerSteamID}. Tracking...");
|
||||
Logger.Write($"User {steamID} is using a game owned by {ownerSteamID}. Tracking...");
|
||||
_gameOwnerIds[steamID] = ownerSteamID;
|
||||
|
||||
if (MySandboxGame.ConfigDedicated.Banned.Contains(ownerSteamID))
|
||||
{
|
||||
MyLog.Default.WriteLineAndConsole($"Game owner {ownerSteamID} is banned. Banning and rejecting client {steamID}...");
|
||||
Logger.Write($"Game owner {ownerSteamID} is banned. Banning and rejecting client {steamID}...");
|
||||
UserRejected(steamID, JoinResult.BannedByAdmins);
|
||||
BanPlayer(steamID, true);
|
||||
}
|
||||
|
@@ -156,13 +156,13 @@ namespace Torch
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool DeleteFile(string name);
|
||||
|
||||
public bool UnblockDll(string fileName)
|
||||
{
|
||||
return DeleteFile(fileName + ":Zone.Identifier");
|
||||
}
|
||||
|
||||
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool DeleteFile(string name);
|
||||
}
|
||||
}
|
||||
|
@@ -30,18 +30,18 @@ namespace Torch
|
||||
}
|
||||
else
|
||||
{
|
||||
var SteamAPI = SteamSDK.SteamAPI.Instance;
|
||||
var steamApi = SteamSDK.SteamAPI.Instance;
|
||||
steam.GetField("SteamAPI").SetValue(this, SteamSDK.SteamAPI.Instance);
|
||||
steam.GetProperty("IsActive").GetSetMethod(true).Invoke(this, new object[] { SteamSDK.SteamAPI.Instance != null });
|
||||
|
||||
if (SteamAPI != null)
|
||||
if (steamApi != null)
|
||||
{
|
||||
steam.GetProperty("UserId").GetSetMethod(true).Invoke(this, new object[] { SteamAPI.GetSteamUserId() });
|
||||
steam.GetProperty("UserName").GetSetMethod(true).Invoke(this, new object[] { SteamAPI.GetSteamName() });
|
||||
steam.GetProperty("OwnsGame").GetSetMethod(true).Invoke(this, new object[] { SteamAPI.HasGame() });
|
||||
steam.GetProperty("UserUniverse").GetSetMethod(true).Invoke(this, new object[] { SteamAPI.GetSteamUserUniverse() });
|
||||
steam.GetProperty("BranchName").GetSetMethod(true).Invoke(this, new object[] { SteamAPI.GetBranchName() });
|
||||
SteamAPI.LoadStats();
|
||||
steam.GetProperty("UserId").GetSetMethod(true).Invoke(this, new object[] { steamApi.GetSteamUserId() });
|
||||
steam.GetProperty("UserName").GetSetMethod(true).Invoke(this, new object[] { steamApi.GetSteamName() });
|
||||
steam.GetProperty("OwnsGame").GetSetMethod(true).Invoke(this, new object[] { steamApi.HasGame() });
|
||||
steam.GetProperty("UserUniverse").GetSetMethod(true).Invoke(this, new object[] { steamApi.GetSteamUserUniverse() });
|
||||
steam.GetProperty("BranchName").GetSetMethod(true).Invoke(this, new object[] { steamApi.GetBranchName() });
|
||||
steamApi.LoadStats();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
@@ -11,6 +12,9 @@ namespace Torch
|
||||
{
|
||||
public abstract class TorchBase : ITorchBase
|
||||
{
|
||||
public static ITorchBase Instance { get; private set; }
|
||||
|
||||
public string[] RunArgs { get; set; }
|
||||
public IPluginManager Plugins { get; protected set; }
|
||||
public IMultiplayer Multiplayer { get; protected set; }
|
||||
|
||||
@@ -23,85 +27,54 @@ namespace Torch
|
||||
|
||||
protected TorchBase()
|
||||
{
|
||||
RunArgs = new string[0];
|
||||
Instance = this;
|
||||
Plugins = new PluginManager();
|
||||
Multiplayer = new MultiplayerManager(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes an action on the game thread and blocks until completion
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
public void GameAction(Action action)
|
||||
public void DoGameAction(Action action)
|
||||
{
|
||||
if (action == null)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
if (Thread.CurrentThread == MySandboxGame.Static.UpdateThread)
|
||||
{
|
||||
action();
|
||||
}
|
||||
else
|
||||
{
|
||||
AutoResetEvent e = new AutoResetEvent(false);
|
||||
|
||||
MySandboxGame.Static.Invoke(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
action();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//log
|
||||
}
|
||||
finally
|
||||
{
|
||||
e.Set();
|
||||
}
|
||||
});
|
||||
|
||||
//timeout so we don't accidentally hang the server
|
||||
e.WaitOne(60000);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//we need a logger :(
|
||||
}
|
||||
MySandboxGame.Static.Invoke(action);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues an action for invocation on the game thread and optionally runs a callback on completion
|
||||
/// Invokes an action on the game thread asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="callback"></param>
|
||||
/// <param name="state"></param>
|
||||
public void BeginGameAction(Action action, Action<object> callback = null, object state = null)
|
||||
public Task DoGameActionAsync(Action action)
|
||||
{
|
||||
if (action == null)
|
||||
return;
|
||||
if (Thread.CurrentThread == MySandboxGame.Static.UpdateThread)
|
||||
{
|
||||
Debug.Assert(false, $"{nameof(DoGameActionAsync)} should not be called on the game thread.");
|
||||
action?.Invoke();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
try
|
||||
return Task.Run(() =>
|
||||
{
|
||||
if (Thread.CurrentThread == MySandboxGame.Static.UpdateThread)
|
||||
var e = new AutoResetEvent(false);
|
||||
|
||||
MySandboxGame.Static.Invoke(() =>
|
||||
{
|
||||
action();
|
||||
}
|
||||
else
|
||||
{
|
||||
Task.Run(() =>
|
||||
try
|
||||
{
|
||||
GameAction(action);
|
||||
callback?.Invoke(state);
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// log
|
||||
}
|
||||
action?.Invoke();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//log
|
||||
}
|
||||
finally
|
||||
{
|
||||
e.Set();
|
||||
}
|
||||
});
|
||||
|
||||
if(!e.WaitOne(60000))
|
||||
throw new TimeoutException("The game action timed out.");
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public abstract void Start();
|
||||
|
Reference in New Issue
Block a user