Skip to content

Commit

Permalink
Basic console commands in cedserver
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Feb 6, 2025
1 parent ff0283f commit 7ec689c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
35 changes: 32 additions & 3 deletions Server/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace CentrED.Server;

public class Application
{
private static CEDServer _cedServer;

Check warning on line 8 in Server/Application.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Server, -p:PublishSingleFile=true --self-contained false)

Non-nullable field '_cedServer' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 8 in Server/Application.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Server, -p:PublishSingleFile=true --self-contained false)

Non-nullable field '_cedServer' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 8 in Server/Application.cs

View workflow job for this annotation

GitHub Actions / build (macos-13, Server, -p:PublishSingleFile=true --self-contained false)

Non-nullable field '_cedServer' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
public static void Main(string[] args)
{
var assemblyName = Assembly.GetExecutingAssembly().GetName();
Expand All @@ -16,9 +17,14 @@ public static void Main(string[] args)
try
{
var config = ConfigRoot.Init(args);
var server = new CEDServer(config);
AppDomain.CurrentDomain.ProcessExit += (_, _) => server.Save();
server.Run();
_cedServer = new CEDServer(config);
AppDomain.CurrentDomain.ProcessExit += (_, _) => _cedServer.Save();
new Thread(HandleConsoleInput)
{
IsBackground = true,
Name = "Console Input"
}.Start();
_cedServer.Run();
}
catch (Exception e)
{
Expand All @@ -36,4 +42,27 @@ public static string GetCurrentExecutable()
{
return AppDomain.CurrentDomain.FriendlyName;
}

public static async void HandleConsoleInput()

Check warning on line 46 in Server/Application.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Server, -p:PublishSingleFile=true --self-contained false)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 46 in Server/Application.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Server, -p:PublishSingleFile=true --self-contained false)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 46 in Server/Application.cs

View workflow job for this annotation

GitHub Actions / build (macos-13, Server, -p:PublishSingleFile=true --self-contained false)

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
while (true)
{
string input;
try
{
input = Console.ReadLine()?.Trim();

Check warning on line 53 in Server/Application.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Server, -p:PublishSingleFile=true --self-contained false)

Converting null literal or possible null value to non-nullable type.

Check warning on line 53 in Server/Application.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Server, -p:PublishSingleFile=true --self-contained false)

Converting null literal or possible null value to non-nullable type.

Check warning on line 53 in Server/Application.cs

View workflow job for this annotation

GitHub Actions / build (macos-13, Server, -p:PublishSingleFile=true --self-contained false)

Converting null literal or possible null value to non-nullable type.
}
catch(Exception e)
{
Console.WriteLine("Console input error!");
Console.WriteLine(e);
return;
}
if (string.IsNullOrEmpty(input))
{
continue;
}
_cedServer.PushCommand(input);
}
}
}
47 changes: 46 additions & 1 deletion Server/CEDServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class CEDServer : ILogging, IDisposable
private readonly ConcurrentQueue<NetState<CEDServer>> _connectedQueue = new();
private readonly ConcurrentQueue<NetState<CEDServer>> _toDispose = new();
private readonly ConcurrentQueue<NetState<CEDServer>> _flushPending = new();

private readonly ConcurrentQueue<string> _commandQueue = new();

public DateTime StartTime = DateTime.Now;
private DateTime _lastFlush = DateTime.Now;
Expand Down Expand Up @@ -170,6 +172,7 @@ public void Run()

AutoSave();
AutoBackup();
ProcessCommands();

Thread.Sleep(1);
} while (!Quit);
Expand Down Expand Up @@ -289,14 +292,56 @@ private void Backup()
Directory.Move(backupDir, $"{Config.AutoBackup.Directory}/Backup{i + 1}");
}
backupDir = $"{Config.AutoBackup.Directory}/Backup1";
Directory.CreateDirectory(backupDir);

Landscape.Backup(backupDir);

Send(new ServerStatePacket(ServerState.Running));
LogInfo("Automatic backup finished.");
}

public void PushCommand(string command)
{
_commandQueue.Enqueue(command);
}

private void ProcessCommands()
{
while (_commandQueue.TryDequeue(out var command))
{
try
{
var parts = command.Split(' ', 2);
switch (parts)
{
case ["save"]:
Console.Write("Saving...");
Landscape.Flush();
Console.WriteLine("Done");
break;
case ["save", string dir]:
Console.Write($"Saving to {dir}...");
Landscape.Backup(dir);
Console.WriteLine("Done");
break;
default: PrintHelp(); break;
}
;
}
catch (Exception e)
{
LogError($"Error processing command: {command}");
LogError(e.ToString());
}
}
}

private void PrintHelp()
{
Console.WriteLine("Supported commands:");
Console.WriteLine("save");
Console.WriteLine("save <dir>");
}

public void Dispose()
{
Listener.Dispose();
Expand Down
1 change: 1 addition & 0 deletions Server/Map/ServerLandscape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ public void Flush()

public void Backup(string backupDir)
{
Directory.CreateDirectory(backupDir);
foreach (var fs in new[] { _map, _staidx, _statics })
{
FileInfo fi = new FileInfo(fs.Name);
Expand Down

0 comments on commit 7ec689c

Please sign in to comment.