3 using FarseerPhysics.Dynamics;
4 using Microsoft.Xna.Framework;
6 using System.Collections.Generic;
7 using System.Diagnostics;
10 using System.Reflection;
11 using System.Threading;
12 using System.Xml.Linq;
13 using MoonSharp.Interpreter;
21 public static readonly
Version Version = Assembly.GetEntryAssembly().GetName().Version;
26 private static World world;
31 if (world ==
null) { world =
new World(
new Vector2(0, -9.82f)); }
34 set { world = value; }
66 private static Stopwatch stopwatch;
68 private static readonly Queue<int> prevUpdateRates =
new Queue<int>();
69 private static int updateCount = 0;
83 FarseerPhysics.Settings.AllowSleep =
true;
84 FarseerPhysics.Settings.ContinuousPhysics =
false;
85 FarseerPhysics.Settings.VelocityIterations = 1;
86 FarseerPhysics.Settings.PositionIterations = 1;
88 Console.WriteLine(
"Loading game settings");
92 if (!
CommandLineArgs.Any(a => a.Trim().Equals(
"-ownerkey", StringComparison.OrdinalIgnoreCase)))
94 Console.WriteLine(
"Initializing SteamManager");
95 SteamManager.Initialize();
97 if (!SteamManager.SteamworksLibExists)
99 Console.WriteLine(
"Initializing EosManager");
100 if (EosInterface.Core.Init(EosInterface.ApplicationCredentials.Server, enableOverlay:
false).TryUnwrapFailure(out var initError))
102 Console.WriteLine($
"EOS failed to initialize: {initError}");
111 Console.WriteLine(
"Initializing GameScreen");
123 ContentPackageManager.Init().Consume();
124 ContentPackageManager.LogEnabledRegularPackageErrors();
132 CheckContentPackage();
138 private void CheckContentPackage()
142 DebugConsole.ThrowErrorLocalized(
143 TextManager.GetWithVariables(
"versionmismatchwarning",
144 (
"[gameversion]",
Version.ToString()),
151 string name =
"Server";
152 int port = NetConfig.DefaultPort;
153 int queryPort = NetConfig.DefaultQueryPort;
154 bool publiclyVisible =
false;
155 string password =
"";
156 bool enableUpnp =
false;
159 Option<int> ownerKey = Option.None;
160 Option<P2PEndpoint> ownerEndpoint = Option.None;
161 IPAddress listenIp = IPAddress.Any;
164 if (doc?.Root ==
null)
166 DebugConsole.AddWarning(
"File \"" +
ServerSettings.
SettingsFile +
"\" not found. Starting the server with default settings.");
176 password = doc.Root.GetAttributeString(
"password",
"");
177 ownerKey = Option<int>.None();
199 DebugConsole.ThrowError($
"Invalid Ip Address '{CommandLineArgs[i + 1]}'.");
232 ownerKey = Option<int>.Some(key);
266 Server.ServerSettings.PlayStyle = playStyle;
269 case "-banafterwrongpassword":
271 Server.ServerSettings.BanAfterWrongPassword = banAfterWrongPassword;
274 case "-karmaenabled":
276 Server.ServerSettings.KarmaEnabled = karmaEnabled;
281 Server.ServerSettings.KarmaPreset = karmaPresetName;
286 if (ServerLanguageOptions.Options.Any(o => o.Identifier == language))
288 Server.ServerSettings.Language = language;
305 Hyper.ComponentModel.HyperTypeDescriptionProvider.Add(typeof(
Character));
306 Hyper.ComponentModel.HyperTypeDescriptionProvider.Add(typeof(
Item));
307 Hyper.ComponentModel.HyperTypeDescriptionProvider.Add(typeof(Items.Components.ItemComponent));
308 Hyper.ComponentModel.HyperTypeDescriptionProvider.Add(typeof(
Hull));
315 double frequency = (double)Stopwatch.Frequency;
316 if (frequency <= 1500)
318 DebugConsole.NewMessage(
"WARNING: Stopwatch frequency under 1500 ticks per second. Expect significant syncing accuracy issues.", Color.Yellow);
321 Stopwatch performanceCounterTimer = Stopwatch.StartNew();
323 stopwatch = Stopwatch.StartNew();
324 long prevTicks = stopwatch.ElapsedTicks;
327 long currTicks = stopwatch.ElapsedTicks;
328 double elapsedTime = Math.Max(currTicks - prevTicks, 0) / frequency;
329 Timing.Accumulator += elapsedTime;
330 if (Timing.Accumulator > Timing.AccumulatorMax)
335 Timing.Accumulator = Timing.Step;
338 CrossThread.ProcessTasks();
340 prevTicks = currTicks;
341 while (Timing.Accumulator >= Timing.Step)
343 performanceCounterTimer.Start();
345 Timing.TotalTime += Timing.Step;
346 Timing.TotalTimeUnpaused += Timing.Step;
347 DebugConsole.Update();
353 if (
Server ==
null) {
break; }
354 SteamManager.Update((
float)Timing.Step);
355 EosInterface.Core.Update();
357 CoroutineManager.Update(paused:
false, (
float)Timing.Step);
360 performanceCounterTimer.Stop();
365 performanceCounterTimer.Reset();
367 Timing.Accumulator -= Timing.Step;
372 if (
Server?.OwnerConnection ==
null)
374 DebugConsole.UpdateCommandLine((
int)(Timing.Accumulator * 800));
378 DebugConsole.Clear();
381 DebugConsole.UpdateCommandLine((
int)(Timing.Accumulator * 800));
384 int frameTime = (int)((stopwatch.ElapsedTicks - prevTicks) / frequency * 1000.0);
385 frameTime = Math.Max(0, frameTime);
387 Thread.Sleep(Math.Max(((
int)(Timing.Step * 1000.0) - frameTime) / 2, 0));
389 if (performanceCounterTimer.ElapsedMilliseconds > 1000)
391 int updateRate = (int)Math.Round(updateCount / (
double)(performanceCounterTimer.ElapsedMilliseconds / 1000.0));
392 prevUpdateRates.Enqueue(updateRate);
393 if (prevUpdateRates.Count >= 10)
395 int avgUpdateRate = (int)prevUpdateRates.Average();
396 if (avgUpdateRate < Timing.FixedUpdateRate * 0.98 && GameSession != null && GameSession.RoundDuration > 1.0)
398 DebugConsole.AddWarning($
"Running slowly ({avgUpdateRate} updates/s)!");
410 prevUpdateRates.Clear();
412 performanceCounterTimer.Restart();
420 SteamManager.ShutDown();
422 SaveUtil.CleanUnnecessarySaveFiles();
424 if (GameSettings.CurrentConfig.SaveDebugConsoleLogs
425 || GameSettings.CurrentConfig.VerboseLogging) { DebugConsole.SaveLogs(); }
426 if (GameAnalyticsManager.SendUserStatistics) { GameAnalyticsManager.ShutDown(); }
433 Timing.Accumulator = 0.0f;
434 stopwatch?.Restart();
435 prevUpdateRates.Clear();
441 return CoroutineManager.StartCoroutine(loader);
readonly Version GameVersion
static void InitCorePrefabs()
static NetLobbyScreen NetLobbyScreen
static ContentPackage VanillaContent
readonly string[] CommandLineArgs
CoroutineHandle ShowLoading(IEnumerable< CoroutineStatus > loader, bool waitKeyHit=true)
static void ResetFrameTime()
static bool IsMultiplayer
static bool IsSingleplayer
static readonly Version Version
static GameScreen GameScreen
static GameSession GameSession
static readonly Screen SubEditorScreen
LuaCsPerformanceCounter PerformanceCounter
ClientPermissions Permissions
NetworkConnection Connection
NetworkConnection OwnerConnection
override IReadOnlyList< Client > ConnectedClients
void Update(float deltaTime)
void StartServer(bool registerToServerList)
void SendConsoleMessage(string txt, Client recipient, Color? color=null)
const string SettingsFile
virtual void Update(double deltaTime)
static void RefreshSavedSubs()
static readonly UnimplementedScreen Instance
static new Option< P2PEndpoint > Parse(string str)