Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using SlipeServer.Server;
using SlipeServer.Server.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;

Expand All @@ -19,22 +20,30 @@ public XmlConfigurationProvider(string fileName)

ushort result;

var startupResources = new List<StartupResource>();

foreach (XmlNode node in xmlConfig.FirstChild.ChildNodes)
{
switch (node.Name)
{
case "serverName":
case "servername":
this.Configuration.ServerName = node.InnerText;
break;
case "host":
this.Configuration.Host = node.InnerText;
case "serverip":
if(node.InnerText == "auto" || node.InnerText == "any")
{
this.Configuration.Host = "";
} else
{
this.Configuration.Host = node.InnerText;
}
break;
case "port":
case "serverport":
if (ushort.TryParse(node.InnerText, out result))
this.Configuration.Port = result;

break;
case "maxPlayers":
case "maxplayers":
if (ushort.TryParse(node.InnerText, out result))
this.Configuration.MaxPlayerCount = result;

Expand All @@ -43,19 +52,19 @@ public XmlConfigurationProvider(string fileName)
this.Configuration.Password = node.InnerText;
break;

case "httpPort":
case "httpport":
this.Configuration.HttpPort = ushort.Parse(node.InnerText);
break;

case "httpUrl":
case "httpurl":
this.Configuration.HttpUrl = node.InnerText;
break;

case "httpHost":
case "httphost":
this.Configuration.HttpHost = node.InnerText;
break;

case "httpConnectionsPerClient":
case "httpmaxconnectionsperclient":
this.Configuration.HttpConnectionsPerClient = int.Parse(node.InnerText);
break;

Expand Down Expand Up @@ -102,7 +111,29 @@ public XmlConfigurationProvider(string fileName)
case "IsVoiceEnabled":
this.Configuration.IsVoiceEnabled = bool.Parse(node.InnerText);
break;

case "resource":
var startupResource = new StartupResource();
foreach (XmlAttribute item in node.Attributes)
{
switch (item.Name)
{
case "src":
startupResource.Name = item.Value;
break;
case "startup":
startupResource.Start = item.Value == "1" || item.Value == "true";
break;
case "protected":
startupResource.Protected = item.Value == "1" || item.Value == "true";
break;
}
}
startupResources.Add(startupResource);
break;
}
}

this.Configuration.StartupResources = startupResources.ToArray();
}
}
9 changes: 9 additions & 0 deletions SlipeServer.Console/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
print("md5 test: ", md5("qwerty") == "D8578EDF8458CE06FBC5BB76A58C5CA4");
print("sha256 test: ", sha256("qwerty") == "65E84BE33532FB784C48129675F9EFF3A682B27168C0EA744B2CF58EE02337C5");
print("custom definitions:",add(2,2), substract(2,2))

-- Begin blips
local blip = createBlip(100, 0,0, 51, 0, 0, 0, 255);
print("blip color:", getBlipColor(blip))
print("blip icon:", getBlipIcon(blip))
print("blip ordering:", getBlipOrdering(blip))
print("blip size:", getBlipSize(blip))
print("blip visible distance:", getBlipVisibleDistance(blip))
-- End blips
end
createExplosion(-10, 0,0, 4);

Expand Down
2 changes: 1 addition & 1 deletion SlipeServer.Hosting/SlipeServer.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
33 changes: 33 additions & 0 deletions SlipeServer.Legacy/CustomConsoleFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Logging.Abstractions;

namespace SlipeServer.Legacy;

public class CustomConsoleFormatter : ConsoleFormatter
{
private readonly SimpleConsoleFormatterOptions formatterOptions;

public CustomConsoleFormatter(IOptionsMonitor<SimpleConsoleFormatterOptions> options)
: base("customFormatter")
{
this.formatterOptions = new SimpleConsoleFormatterOptions
{
TimestampFormat = "[HH:mm:ss] "
};
}

public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter)
{
if (textWriter == null)
{
throw new ArgumentNullException(nameof(textWriter));
}

var timestamp = DateTime.Now.ToString(this.formatterOptions.TimestampFormat);
textWriter.Write(timestamp);
textWriter.Write(logEntry.Formatter(logEntry.State, logEntry.Exception));
textWriter.Write(Environment.NewLine);
}
}
38 changes: 38 additions & 0 deletions SlipeServer.Legacy/InteractiveServerConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace SlipeServer.Legacy;

public delegate void ConsoleCommandCallback(string arguments);

public class InteractiveServerConsole
{
private readonly Dictionary<string, ConsoleCommandCallback> commands = [];

public IEnumerable<string> Commands => commands.Keys;

public InteractiveServerConsole()
{

}

public bool AddCommand(string command, ConsoleCommandCallback callback)
{
if (this.commands.ContainsKey(command))
return false;

this.commands[command] = callback;
return true;
}

public void ExecuteCommand(string command)
{
var splt = command.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);

var commandName = splt[0];
if (this.commands.TryGetValue(commandName, out var callback)){

if (splt.Length == 2)
callback(splt[1]);
else
callback("");
}
}
}
192 changes: 192 additions & 0 deletions SlipeServer.Legacy/LegacyConsoleCommandsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
using Microsoft.Extensions.Logging;
using SlipeServer.Scripting;
using SlipeServer.Server.Resources;
using SlipeServer.Server.Resources.Providers;
using SlipeServer.Server.Services;

namespace SlipeServer.Legacy;

internal sealed class LegacyConsoleCommandsService : IHostedService
{
private readonly ILogger<LegacyConsoleCommandsService> logger;
private readonly InteractiveServerConsole serverConsole;
private readonly IResourceProvider resourceProvider;
private readonly ClientResourceService clientResourceService;
private readonly ServerResourceService serverResourcesService;
private readonly ChatBox chatBox;

public LegacyConsoleCommandsService(ILogger<LegacyConsoleCommandsService> logger, InteractiveServerConsole serverConsole, IResourceProvider resourceProvider, ClientResourceService clientResourceService, ServerResourceService serverResourcesService, ChatBox chatBox)
{
this.logger = logger;
this.serverConsole = serverConsole;
this.resourceProvider = resourceProvider;
this.clientResourceService = clientResourceService;
this.serverResourcesService = serverResourcesService;
this.chatBox = chatBox;
}

private void HandleStartCommand(string name)
{
var serverResource = this.serverResourcesService.GetResource(name);

if(serverResource == null)
{
this.logger.LogInformation("* Syntax: start <resource-name>");
return;
}

this.logger.LogInformation("start: Requested by Console");

try
{
if (this.serverResourcesService.StartResource(name))
{
this.logger.LogInformation("start: Resource '{resourceName}' started", name);
} else
{
this.logger.LogWarning("start: Resource is already running");
}
}
catch(Exception ex)
{
Console.WriteLine(ex);
this.logger.LogError(ex, "Failed to start resource {resourceName}", name);
}
}

private void HandleStopCommand(string name)
{
var resource = this.serverResourcesService.GetResource(name);

if(resource == null)
{
this.logger.LogInformation("* Syntax: stop <resource-name>");
return;
}

this.logger.LogInformation("stop: Requested by Console");
this.logger.LogInformation("stop: Resource stopping");
try
{
this.serverResourcesService.StopResource(name);
}
catch(Exception ex)
{
this.logger.LogError(ex, "Failed to stop resource {resourceName}", name);
}
}

private void HandleRestartCommand(string name)
{
var resource = this.serverResourcesService.GetResource(name);

if(resource == null)
{
this.logger.LogInformation("* Syntax: stop <resource-name>");
return;
}

this.logger.LogInformation("restart: Requested by Console");
this.logger.LogInformation("restart: Resource restarting");
try
{
if (this.serverResourcesService.StopResource(name))
{
this.serverResourcesService.StartResource(name);
} else
{
this.logger.LogWarning("restart: Resource is not running");
}
}
catch(Exception ex)
{
Console.WriteLine(ex);
this.logger.LogError(ex, "Failed to restart resource {resourceName}", name);
}
}

private void HandleRefreshCommand(string name)
{
var resource = this.serverResourcesService.GetResource(name);

if(resource == null)
{
this.logger.LogInformation("* Syntax: stop <resource-name>");
return;
}

try
{
this.resourceProvider.Refresh();
}
catch(Exception ex)
{
this.logger.LogError(ex, "Failed to stop resource {resourceName}", name);
}
}

private void HandleSayCommand(string text)
{
this.logger.LogInformation("CONSOLECHAT: {0}", text);
this.chatBox.Output(text, System.Drawing.Color.FromArgb(223, 149, 232));
}

private void HandleListCommand(string text)
{
int count = 0;
int failedCount = 0;
int runningCount = 0;
this.logger.LogInformation("== Resource list==");
foreach (var resource in this.resourceProvider.GetResources())
{
var resourceState = this.serverResourcesService.GetResourceState(resource.Name);
switch (resourceState)
{
case ResourceState.Loaded:
count++;
this.logger.LogInformation(" {resourceName} STOPPED ({resourcesFilesCount} files)", resource.Name.PadRight(20), resource.Files.Count);
break;
case ResourceState.Running:
runningCount++;
this.logger.LogInformation(" {resourceName} RUNNING", resource.Name.PadRight(20));
break;
}
this.chatBox.Output(text, System.Drawing.Color.FromArgb(223, 149, 232));
}
this.logger.LogInformation("== Resources: {loadedResources} loaded, {failedToStartResources} failed, {runningResources} running ==", count, failedCount, runningCount);
}

private void HandleHelpCommand(string text)
{
this.logger.LogInformation("help [command]");
this.logger.LogInformation("Available commands:");
Console.WriteLine();

var commands = this.serverConsole.Commands.ToArray();
for (int i = 0; i < commands.Length; i += 3)
{
string A = commands[i];
string B = (i + 1 < commands.Length) ? commands[i + 1] : string.Empty;
string C = (i + 2 < commands.Length) ? commands[i + 2] : string.Empty;

Console.WriteLine(string.Format("{0,-25} {1,-25} {2,-25}", A, B, C));
}
}

public Task StartAsync(CancellationToken cancellationToken)
{
this.serverConsole.AddCommand("start", HandleStartCommand);
this.serverConsole.AddCommand("stop", HandleStopCommand);
this.serverConsole.AddCommand("restart", HandleRestartCommand);
this.serverConsole.AddCommand("refresh", HandleRefreshCommand);
this.serverConsole.AddCommand("say", HandleSayCommand);
this.serverConsole.AddCommand("list", HandleListCommand);
this.serverConsole.AddCommand("help", HandleHelpCommand);
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
Loading