Files
RobustToolbox/SS14.Server/BaseServer.cs
Pieter-Jan Briers b5c0202647 Godot (#545)
* bsdiff submodule

* All the other stuff

* Upgrade to 0.2 of SS14.Shared.BSdiff

* Use streams. Even faster!

* All rendering refactors should start with the killing of the SFML submodule. Trust me I've done this before!

* And after that comes the removal of the references in the project files!

Of course, neither of these compile. Meh.

* IoC loads, now to port the entire client over.

* More work

* Trying to debug the null exception.

* More messy WiP code

* More WiP

* Transform components now make Node2Ds inside the scene tree.

* Work on the UI system.

* Redo project configurations to work better.

* More work on the Godot GUI binding.

Added BaseButton and Button bindings, including events for them.

More APIs on Controls like GetChild() and alike.

Ability to instance a Godot PackedScene and have it automatically be wrapped by the SS14 GUI wrapper, thus allowing the usage of Godot's GUI editor while maintaining the good C# API.

* Fix incorrect sRGB profile on bootsplash.png

* LineEdit API.

* Maybe commit the csproj too.

* Exit button works now!

* Change MainMenu root to a normal Control.

* Some stuff messing with window popups.

* Fix popup not scaling down again after housing large text.

* Make popup dialog helper.

* Auto wrap controls that spawn others when not instancing.

* Nice typo me.

* Work getting server connections working.

* Server didn't start anymore due to still trying to load removed zip packs.
* Made MainMenu dump UI elements in shutdown.

* We now successfully connect to the server.

* WE Ingame NOW!

* Basic map rendering works now!

* Camera & Input work.

Though I'm not sure whether the input issues are this piss poor laptop or actual bugs.

* Fix input issues.

KeyHeld() was firing KeyUp() into States, which broke everything.

* WiP Debug console.

UI Works, command processing doesn't.

* Remove some debug logs.

* Fixing focusing issues with the Debug Console.

* In which I copy paste in the DebugConsole code.

* More WiP DebugConsole work.

* Use RichTextLabel for DebugConsole.

* Disable DebugConsole test text.

Also disable context menu on the IP Box because of font size issues.

* ITextureSource for texture wrapping.

* Make resource loading not copy every build.

It now loads from repo root. Release builds are TODO.

* Bsdiff 0.3 for fixed targets.

* Fix iCCP sRGB errors.

* Give full texture paths to prototypes.

* Sprite component old API restored. *shudder*

* Finish sprite rendering.

By hitting delete on basically everything.

* Fixing camera delay with this one weird trick!

The input lag was because the camera lagged, not because the input lagged. I didn't have visualization.

* Use greyshirt as temporary human replacement.

* FPS Counter.

* Fix Windows, re-add space tile def.

Windows broke because Mono on Windows can't use System.IO.Compression.
The GZIP streams use by MsgFullState have been moved to
ICSharpCode.SharpZipLib.GZip. Works fine.

Also re-added the space tile def because everything blows up otherwise I
guess.

* Debug colliders works.

* Highly WiP varied Window code.

* Work on the quit button. Doesn't work yet. Needs state overhaul.

* Do a better job of freeing Godot objects correctly.

* Simplify game states.

States are no longer cached. They're GC'd after shut down. Their creation has been simplified too due to IoCManager.InjectDependencies().

This makes returning to main menu work GUI wise.

* Be less aggressive on resource freeing. Only dispose Mono handles now.

* More work getting quit to work.

* Grids get cleared upon disconnect correctly.

* Disable rendering of the filler space tile.

I'll leave a nice parallax background up to content.

* Oops forgot to stage these.

* Fix issues in the csproj file.

* Make controls have own namespace.

* Chatbox GUI, other stuff.

Compiles but doesn't quite work yet.

* Git fuck you

* Chat works.

* Clicking on a not-control removes focus now.

* Fix an exception.

* Update mono. Use enums instead of constants now!

* Fix window dragging

* Fix chat stealing focus from other LineEdits on MacOS I guess.

* Correctly handle client shutdown & server disconnect.

* Fix error spam on client shutdown probably.

* Tiny amount of lighting code to have access to it from other computers

* Lighting works mostly.

* Godot.Texture.Flags -> FlagsEnum

* More WiP lighting code

* Turns out you can't control custom layers with cameras eh.

* WiP lighting almost works

* Lighting WORKS!

* Lobby thing.

* Some options menu work. I'm gonna try something

* Options menu works.

* More improvements.

* In game options menu works.

* Remove a debug log.

* Fix Window Movement and Drag.

* Huh Godot edited these scenes.

* Forgot to commit projects

* It compiles.

* I never claimed that.

* Update sandbox csprojs a bit.

* Makes sandbox load.

Client goes ingame but dies due to broken map manager networking code.

* Fix grids.

* Fix relogging duplicating entities inside Godot.

* Eyes!

* How about removing the TODO entry.

* Auto fetch bsdiffwrap.

* Update TODO.

* Remove TODO list.

* Tilemaps get cleaned on disconnect now.

* Fix bsdiff submodule HOPEFULLY

* Highly WiP and not compiling or working placement.

Yes this is the best way for me to share code between my computers.

* Fix bsdiff with spaces in path names bloody MSBuild + cmd.

* PLACEMENT KINDA WORKS.

NO RENDERING OR TILES YET.

ALSO I NEED TO REBASE THIS.

* Kill EngineContentPack.zip

* Fix map code and remove sprite components from the server.

* Ok entity placement mostly works.

* Grid lines sorta work but SnapgridCenter is still broke.

* Fix Center Grid Rendering.

* Work getting tile spawns to work. Not quite there yet.

* Fix placement and remove tiledef networking.

It werks!

* Remove SolutionSpecific.targets

Didn't end up being needed.

* Kill off wearableanimatedsprite component states.

* Do not put binaries relative to the solution.

* Remove shaders, reorganize prototypes for content.

* Reimplement SpriteComponent color.

* Correctly set __engine_human DrawDepth.

* Round coordinates passed to Godot to pixels to prevent issues.

* Remove some GUI log spam.

* Resource cache now uses the VFS more-proper but still awfully.

* Fix color reading code on SpriteComponent.

See, e94bf4854b106da51c89eeeab9a609be369f9622 did work.
The problem was all the code it NEEDED to work was broken.

* Step one into trying to fix Travis and AppVeyor.

* Auto download GodotSharp on Travis.

* Let's not make dumb mistakes with the cache directories.

* 2018 and requests still isn't in the stdlib.

* This maybe?

* This maybe?

* AppVeyor time.

* How 'bout we don't download sonarqube at all outside master.

* Try to cache on AppVeyor.

* Fix mac builds maybe.

* Finishing up Godot: cleanup.

* Eh this works too for SS14Loader.

* Remove some dead files

* Make Omnisharp not choke on buildchecker.

* Controls for box containers.

* Remove debug port from project.godot.

* Control and drawing improvements.

Controls are now properly capable of overriding Godot virtuals.
Importantly minsize calculations and stuff like HasPoint.

There is now a system for doing direct drawing on controls.
This is done with a DrawingHandle system.

TextureSource has been renamed to Texture.

Also there's a wrapping for Godot's style boxes now.

* Yeah don't insult omnisharp.

* Stylebox fixes and margin API on controls.

* Hey it compiles!

* Fix things.

* Fix null godot texture conversion.

* Fix client transform parenting.

* Fix movement sticking to north.

* Some updates.

* Work on exports.

* Unstubs client/Godot timing code.

It's mostly implemented now.

* Client unit tests work.

Jesus Christ.

* Let's figure out why AppVeyor hates me.

* Does the remie.

* Update GodotSharp download URL.

* Export templates for the builds server.

* Remove mac export icon.

* TO THE BUILDS SERVER.

* Probably implement the effects system in Godot.

* Fix mouse handling everywhere.

* Fix some CollidableComponent exceptions.

* Effects system works + unshaded option for LAZORS.

* Let's not fuck with Angle yet.

* Make file/line numbers show up on Windows Godot.
2018-04-07 15:24:46 +02:00

416 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using SS14.Server.GameStates;
using SS14.Server.Interfaces;
using SS14.Server.Interfaces.Chat;
using SS14.Server.Interfaces.ClientConsoleHost;
using SS14.Server.Interfaces.GameObjects;
using SS14.Server.Interfaces.GameState;
using SS14.Server.Interfaces.Log;
using SS14.Server.Interfaces.Placement;
using SS14.Server.Interfaces.Player;
using SS14.Server.Interfaces.ServerConsole;
using SS14.Shared;
using SS14.Shared.Configuration;
using SS14.Shared.ContentPack;
using SS14.Shared.GameStates;
using SS14.Shared.Interfaces;
using SS14.Shared.Interfaces.Configuration;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.Interfaces.Map;
using SS14.Shared.Interfaces.Network;
using SS14.Shared.Interfaces.Serialization;
using SS14.Shared.Interfaces.Timing;
using SS14.Shared.Interfaces.Timers;
using SS14.Shared.IoC;
using SS14.Shared.Log;
using SS14.Shared.Network;
using SS14.Shared.Network.Messages;
using SS14.Shared.Prototypes;
using SS14.Shared.Map;
using SS14.Server.Interfaces.Maps;
using SS14.Server.Player;
using SS14.Shared.Enums;
using SS14.Shared.Reflection;
using SS14.Shared.Timing;
namespace SS14.Server
{
/// <summary>
/// The master class that runs the rest of the engine.
/// </summary>
public class BaseServer : IBaseServer
{
[Dependency]
private readonly ICommandLineArgs _commandLine;
[Dependency]
private readonly IConfigurationManager _config;
[Dependency]
private readonly IComponentManager _components;
[Dependency]
private readonly IServerEntityManager _entities;
[Dependency]
private readonly IServerLogManager _log;
[Dependency]
private readonly ISS14Serializer _serializer;
[Dependency]
private readonly IGameTiming _time;
[Dependency]
private readonly IResourceManager _resources;
[Dependency]
private readonly IMapLoader _mapLoader;
[Dependency]
private readonly IMapManager _mapManager;
[Dependency]
private readonly ITimerManager timerManager;
[Dependency]
private readonly IGameStateManager _stateManager;
[Dependency]
private readonly IServerNetManager _network;
private GameLoop _mainLoop;
private ServerRunLevel _runLevel;
private TimeSpan _lastTitleUpdate;
private int _lastReceivedBytes;
private int _lastSentBytes;
/// <inheritdoc />
public ServerRunLevel RunLevel
{
get => _runLevel;
set => OnRunLevelChanged(value);
}
/// <inheritdoc />
public string MapName => _config.GetCVar<string>("game.mapname");
/// <inheritdoc />
public int MaxPlayers => _config.GetCVar<int>("game.maxplayers");
/// <inheritdoc />
public string ServerName => _config.GetCVar<string>("game.hostname");
/// <inheritdoc />
public string Motd => _config.GetCVar<string>("game.welcomemsg");
/// <inheritdoc />
public string GameModeName { get; set; } = string.Empty;
/// <inheritdoc />
public event EventHandler<RunLevelChangedEventArgs> RunLevelChanged;
/// <inheritdoc />
public void Restart()
{
Logger.Info("[SRV] Restarting Server...");
Cleanup();
Start();
}
/// <inheritdoc />
public void Shutdown(string reason = null)
{
if (string.IsNullOrWhiteSpace(reason))
Logger.Log("[SRV] Shutting down...");
else
Logger.Log($"[SRV] {reason}, shutting down...");
_mainLoop.Running = false;
}
/// <inheritdoc />
public bool Start()
{
//Sets up the configMgr
_config.LoadFromFile(_commandLine.ConfigFile);
//Sets up Logging
_config.RegisterCVar("log.path", "logs", CVar.ARCHIVE);
_config.RegisterCVar("log.format", "log_%(date)s-%(time)s.txt", CVar.ARCHIVE);
_config.RegisterCVar("log.level", LogLevel.Information, CVar.ARCHIVE);
var logPath = _config.GetCVar<string>("log.path");
var logFormat = _config.GetCVar<string>("log.format");
var logFilename = logFormat.Replace("%(date)s", DateTime.Now.ToString("yyyyMMdd")).Replace("%(time)s", DateTime.Now.ToString("hhmmss"));
var fullPath = Path.Combine(logPath, logFilename);
if (!Path.IsPathRooted(fullPath))
logPath = PathHelpers.ExecutableRelativeFile(fullPath);
// Create log directory if it does not exist yet.
Directory.CreateDirectory(Path.GetDirectoryName(logPath));
_log.CurrentLevel = _config.GetCVar<LogLevel>("log.level");
_log.LogPath = logPath;
OnRunLevelChanged(ServerRunLevel.Init);
LoadSettings();
var netMan = IoCManager.Resolve<IServerNetManager>();
try
{
netMan.Initialize(true);
netMan.Startup();
}
catch (System.Net.Sockets.SocketException)
{
var port = netMan.Port;
Logger.Log($"Unable to setup networking manager. Check port {port} is not already in use!, shutting down...", LogLevel.Fatal);
Environment.Exit(1);
}
catch (Exception e)
{
Logger.Log($"Unable to setup networking manager. Unknown exception: {e}, shutting down...", LogLevel.Fatal);
Environment.Exit(1);
}
// Set up the VFS
_resources.Initialize();
#if RELEASE
_resources.MountContentDirectory(@"./Resources/");
#else
// Load from the resources dir in the repo root instead.
// It's a debug build so this is fine.
_resources.MountContentDirectory(@"../../Resources/");
_resources.MountContentDirectory(@"Resources/Assemblies", "Assemblies/");
#endif
//mount the engine content pack
// _resources.MountContentPack(@"EngineContentPack.zip");
//mount the default game ContentPack defined in config
// _resources.MountDefaultContentPack();
//identical code in game controller for client
if (!AssemblyLoader.TryLoadAssembly<GameShared>(_resources, $"Content.Shared"))
if (!AssemblyLoader.TryLoadAssembly<GameShared>(_resources, $"Sandbox.Shared"))
Logger.Warning($"[ENG] Could not load any Shared DLL.");
if (!AssemblyLoader.TryLoadAssembly<GameServer>(_resources, $"Content.Server"))
if (!AssemblyLoader.TryLoadAssembly<GameServer>(_resources, $"Sandbox.Server"))
Logger.Warning($"[ENG] Could not load any Server DLL.");
// HAS to happen after content gets loaded.
// Else the content types won't be included.
// TODO: solve this properly.
_serializer.Initialize();
// Initialize Tier 2 services
IoCManager.Resolve<IGameStateManager>().Initialize();
IoCManager.Resolve<IEntityManager>().Initialize();
IoCManager.Resolve<IChatManager>().Initialize();
IoCManager.Resolve<IPlayerManager>().Initialize(MaxPlayers);
IoCManager.Resolve<IMapManager>().Initialize();
IoCManager.Resolve<IPlacementManager>().Initialize();
// Call Init in game assemblies.
AssemblyLoader.BroadcastRunLevel(AssemblyLoader.RunLevel.Init);
IoCManager.Resolve<ITileDefinitionManager>().Initialize();
// because of 'reasons' this has to be called after the last assembly is loaded
// otherwise the prototypes will be cleared
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
prototypeManager.LoadDirectory(@"Prototypes");
prototypeManager.Resync();
var clientConsole = IoCManager.Resolve<IClientConsoleHost>();
clientConsole.Initialize();
var consoleManager = IoCManager.Resolve<IConsoleManager>();
consoleManager.Initialize();
OnRunLevelChanged(ServerRunLevel.PreGame);
return false;
}
private TimeSpan _lastTick;
private TimeSpan _lastKeepUpAnnounce;
/// <inheritdoc />
public void MainLoop()
{
_mainLoop = new GameLoop(_time);
_mainLoop.SleepMode = SleepMode.Delay;
_mainLoop.Tick += (sender, args) => Update(args.DeltaSeconds);
// set GameLoop.Running to false to return from this function.
_mainLoop.Run();
Cleanup();
}
/// <summary>
/// Updates the console window title with performance statistics.
/// </summary>
private void UpdateTitle()
{
// every 1 second update stats in the console window title
if ((_time.RealTime - _lastTitleUpdate).TotalSeconds < 1.0)
return;
var netStats = UpdateBps();
Console.Title = string.Format("FPS: {0:N2} SD: {1:N2}ms | Net: ({2}) | Memory: {3:N0} KiB",
Math.Round(_time.FramesPerSecondAvg, 2),
_time.RealFrameTimeStdDev.TotalMilliseconds,
netStats,
Process.GetCurrentProcess().PrivateMemorySize64 >> 10);
_lastTitleUpdate = _time.RealTime;
}
/// <summary>
/// Loads the server settings from the ConfigurationManager.
/// </summary>
private void LoadSettings()
{
var cfgMgr = IoCManager.Resolve<IConfigurationManager>();
cfgMgr.RegisterCVar("net.tickrate", 66, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER);
cfgMgr.RegisterCVar("game.hostname", "MyServer", CVar.ARCHIVE);
cfgMgr.RegisterCVar("game.mapname", "SavedEntities.xml", CVar.ARCHIVE);
cfgMgr.RegisterCVar("game.maxplayers", 32, CVar.ARCHIVE);
cfgMgr.RegisterCVar("game.type", GameType.Game);
cfgMgr.RegisterCVar("game.welcomemsg", "Welcome to the server!", CVar.ARCHIVE);
_time.TickRate = _config.GetCVar<int>("net.tickrate");
Logger.Info($"[SRV] Name: {ServerName}");
Logger.Info($"[SRV] TickRate: {_time.TickRate}({_time.TickPeriod.TotalMilliseconds:0.00}ms)");
Logger.Info($"[SRV] Map: {MapName}");
Logger.Info($"[SRV] Max players: {MaxPlayers}");
Logger.Info($"[SRV] Welcome message: {Motd}");
}
/// <summary>
/// Switches the run level of the BaseServer to the desired value.
/// </summary>
private void OnRunLevelChanged(ServerRunLevel level)
{
if (level == _runLevel)
return;
Logger.Debug($"[ENG] Runlevel changed to: {level}");
var args = new RunLevelChangedEventArgs(_runLevel, level);
_runLevel = level;
RunLevelChanged?.Invoke(this, args);
// positive edge triggers
switch (level)
{
case ServerRunLevel.PreGame:
_entities.Startup();
break;
}
}
// called right before main loop returns, do all saving/cleanup in here
private void Cleanup()
{
// shut down networking, kicking all players.
_network.Shutdown("Server Shutdown");
// shutdown entities
_entities.Shutdown();
//TODO: This should prob shutdown all managers in a loop.
// remove all maps
if(_runLevel == ServerRunLevel.Game)
{
var mapMgr = IoCManager.Resolve<IMapManager>();
// TODO: Unregister all maps.
mapMgr.DeleteMap(new MapId(1));
}
}
private string UpdateBps()
{
var stats = IoCManager.Resolve<IServerNetManager>().Statistics;
var bps = $"Send: {(stats.SentBytes - _lastSentBytes) >> 10:N0} KiB/s, Recv: {(stats.ReceivedBytes - _lastReceivedBytes) >> 10:N0} KiB/s";
_lastSentBytes = stats.SentBytes;
_lastReceivedBytes = stats.ReceivedBytes;
return bps;
}
private void Update(float frameTime)
{
UpdateTitle();
IoCManager.Resolve<IServerNetManager>().ProcessPackets();
AssemblyLoader.BroadcastUpdate(AssemblyLoader.UpdateLevel.PreEngine, frameTime);
timerManager.UpdateTimers(frameTime);
if (_runLevel >= ServerRunLevel.PreGame)
{
_components.Update(frameTime);
_entities.Update(frameTime);
}
AssemblyLoader.BroadcastUpdate(AssemblyLoader.UpdateLevel.PostEngine, frameTime);
_stateManager.SendGameStateUpdate();
}
}
/// <summary>
/// Enumeration of the run levels of the BaseServer.
/// </summary>
public enum ServerRunLevel
{
Error = 0,
Init,
PreGame,
Game,
PostGame,
MapChange,
}
/// <summary>
/// Type of game currently running.
/// </summary>
public enum GameType
{
MapEditor = 0,
Game,
}
/// <summary>
/// Event arguments for when the RunLevel has changed in the BaseServer.
/// </summary>
public class RunLevelChangedEventArgs : EventArgs
{
/// <summary>
/// RunLevel that the BaseServer switched from.
/// </summary>
public ServerRunLevel OldLevel { get; }
/// <summary>
/// RunLevel that the BaseServers switched to.
/// </summary>
public ServerRunLevel NewLevel { get; }
/// <summary>
/// Constructs a new instance of the class.
/// </summary>
public RunLevelChangedEventArgs(ServerRunLevel oldLevel, ServerRunLevel newLevel)
{
OldLevel = oldLevel;
NewLevel = newLevel;
}
}
}