Skip to content

Commit

Permalink
This is a disaster. Revert "Revert "Server now has a config file.. an…
Browse files Browse the repository at this point in the history
…d the file is in .gitignore""

This reverts commit 144f765.
  • Loading branch information
soccermitchy committed Dec 18, 2016
1 parent 144f765 commit 56aef03
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,4 @@ ModelManifest.xml
*.csproj
*.csproj
*.csproj
gtaserver.core/serverSettings.xml
59 changes: 49 additions & 10 deletions gtaserver.core/Program.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
using System;
using System.Threading;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;

namespace GTAServer
{
public class Program
{
public static ServerConfiguration GameServerConfiguration;
private static ILogger _logger;

public static void Main(string[] args)
{
Util.LoggerFactory = new LoggerFactory()
#if DEBUG
.AddConsole(LogLevel.Trace)
.AddDebug(); // this adds stuff to VS debug console
#else
.AddConsole()
.AddConsole();
#endif
.AddDebug();
var logger = Util.LoggerFactory.CreateLogger<Program>();

_logger = Util.LoggerFactory.CreateLogger<Program>();
#if DEBUG
logger.LogWarning("Note - This build is a debug build. Please do not share this build and report any issues to Mitchell Monahan (@wolfmitchell)");
logger.LogWarning("Furthermore, debug builds will not announce themselves to the master server, regardless of the AnnounceSelf config option.");
logger.LogWarning("To help bring crashes to the attention of the server owner and make sure they are reported to me, error catching has been disabled in this build.");
_logger.LogWarning("Note - This build is a debug build. Please do not share this build and report any issues to Mitchell Monahan (@wolfmitchell)");
_logger.LogWarning("Furthermore, debug builds will not announce themselves to the master server, regardless of the AnnounceSelf config option.");
_logger.LogWarning("To help bring crashes to the attention of the server owner and make sure they are reported to me, error catching has been disabled in this build.");
#endif
logger.LogInformation("Server preparing to start...");
_logger.LogInformation("Reading server configuration...");
GameServerConfiguration = LoadServerConfiguration("serverSettings.xml");
_logger.LogInformation("Configuration loaded...");

_logger.LogInformation("Server preparing to start...");

var gameServer = new GameServer(4699, "GTAServer .NET Core Test", "freeroam");
var gameServer = new GameServer(GameServerConfiguration.Port, GameServerConfiguration.ServerName,
GameServerConfiguration.GamemodeName)
{
Password = GameServerConfiguration.Password,
MasterServer = GameServerConfiguration.PrimaryMasterServer,
AnnounceSelf = GameServerConfiguration.AnnounceSelf,
AllowNicknames = GameServerConfiguration.AllowNicknames,
AllowOutdatedClients = GameServerConfiguration.AllowOutdatedClients,
};

logger.LogInformation("Server starting...");
_logger.LogInformation("Server starting...");
gameServer.Start();

logger.LogInformation("Starting server main loop, ready to accept connections.");
_logger.LogInformation("Starting server main loop, ready to accept connections.");
while (true)
{
#if DEBUG
Expand All @@ -46,5 +65,25 @@ public static void Main(string[] args)
Thread.Sleep(1);
}
}

public static ServerConfiguration LoadServerConfiguration(string path)
{
var ser = new XmlSerializer(typeof(ServerConfiguration));

ServerConfiguration cfg = null;
if (File.Exists(path))
{
using (var stream = File.OpenRead(path)) cfg = (ServerConfiguration)ser.Deserialize(stream);
using (
var stream = new FileStream(path, File.Exists(path) ? FileMode.Truncate : FileMode.Create,
FileAccess.ReadWrite)) ser.Serialize(stream, cfg);
}
else
{
_logger.LogInformation("No config found, creating a new one");
using (var stream = File.OpenWrite(path)) ser.Serialize(stream, cfg = new ServerConfiguration());
}
return cfg;
}
}
}
21 changes: 21 additions & 0 deletions gtaserver.core/ServerConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;

namespace GTAServer
{
public class ServerConfiguration
{
public int Port { get; set; } = 4499;
public int MaxClients { get; set; } = 16;
public string GamemodeName { get; set; } = "freeroam";
public string ServerName { get; set; } = "GTACoOp Server";
public string Password { get; set; } = "";
public string PrimaryMasterServer { get; set; } = "https://gtamaster.nofla.me";
public bool AnnounceSelf { get; set; } = false;
public bool AllowNicknames { get; set; } = true;
public bool AllowOutdatedClients { get; set; } = false;
}
}

0 comments on commit 56aef03

Please sign in to comment.