Skip to content
Draft
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
20 changes: 0 additions & 20 deletions SlipeServer.Console/Logic/ServerTestLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ public class ServerTestLogic
private readonly WeaponConfigurationService weaponConfigurationService;
private readonly GameWorld gameWorld;
private readonly IElementIdGenerator elementIdGenerator;
private Resource? testResource;
private Resource? secondTestResource;
private Resource? thirdTestResource;

private readonly Random random = new();
private RadarArea? RadarArea { get; set; }
Expand Down Expand Up @@ -184,14 +181,6 @@ private void SetupTestLogic()

private void SetupTestElements()
{
this.testResource = this.resourceProvider.GetResource("TestResource");
this.secondTestResource = this.resourceProvider.GetResource("SecondTestResource");
this.secondTestResource.NoClientScripts[$"{this.secondTestResource!.Name}/testfile.lua"] =
Encoding.UTF8.GetBytes("outputChatBox(\"I AM A NOT CACHED MESSAGE\")");
this.secondTestResource.NoClientScripts[$"blabla.lua"] = new byte[] { };

this.thirdTestResource = this.resourceProvider.GetResource("MetaXmlTestResource");

new WorldObject(321, new Vector3(5, 0, 3)).AssociateWith(this.server);
new Water(new Vector3[]
{
Expand Down Expand Up @@ -868,11 +857,6 @@ void Player_Disconnected(Player sender, PlayerQuitEventArgs e)
}
};

this.commandService.AddCommand("latent").Triggered += (source, args) =>
{
this.luaService.TriggerLatentEvent("Slipe.Test.ClientEvent", this.testResource!, this.root, 1, this.root, 50, "STRING");
};

this.commandService.AddCommand("dim").Triggered += (source, args) =>
{
if (args.Arguments.Length > 0)
Expand Down Expand Up @@ -1553,10 +1537,6 @@ private void OnPlayerJoin(CustomPlayer player)
player.Weapons.First(weapon => weapon.Type == WeaponId.Ak47).Ammo = 750;
player.Weapons.First(weapon => weapon.Type == WeaponId.Ak47).AmmoInClip = 25;

this.testResource?.StartFor(player);
this.secondTestResource?.StartFor(player);
this.thirdTestResource?.StartFor(player);

this.HandlePlayerSubscriptions(player);

player.AcInfoReceived += (o, args) =>
Expand Down
80 changes: 74 additions & 6 deletions SlipeServer.Example/Controllers/TestCommandController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using Microsoft.Extensions.Logging;
using SlipeServer.Example.Logic;
using SlipeServer.LuaControllers;
using SlipeServer.LuaControllers.Attributes;
using SlipeServer.Server.ElementCollections;
using SlipeServer.Server.Elements;
using SlipeServer.Server.Enums;
using SlipeServer.Server.Services;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Reflection;

namespace SlipeServer.Example.Controllers;

[CommandController()]
internal class NoAccessAttribute : Attribute;

[CommandController]
public class TestCommandController : BaseCommandController<CustomPlayer>
{
private readonly ChatBox chatBox;
Expand All @@ -29,15 +30,82 @@ public TestCommandController(ChatBox chatBox, IElementCollection elementCollecti
this.logger.LogInformation("Instantiating {type}", typeof(TestController));
}

protected override void Invoke(Action next)
{
try
{
if (this.Context.MethodInfo.GetCustomAttribute<NoAccessAttribute>() != null)
{
this.chatBox.OutputTo(this.Context.Player, $"You can not access command {this.Context.Command}");
} else
{
next();
}
}
catch (Exception ex)
{
this.chatBox.OutputTo(this.Context.Player, $"Failed to execute command {this.Context.Command}");
}
}

protected override async Task InvokeAsync(Func<Task> next)
{
var stopwatch = Stopwatch.StartNew();
await next();
Console.WriteLine("Executed async command in: {0}ms", stopwatch.ElapsedMilliseconds);
}

public void Chat(IEnumerable<string> words)
{
this.chatBox.OutputTo(this.Context.Player, string.Join(' ', words));
}

[NoAccess]
public void NoAccess()
{
this.chatBox.OutputTo(this.Context.Player, "You have accessed command with NoAccess attribute!");
}

public void Oops()
{
throw new Exception("oops");
}

public void Ping()
{
this.chatBox.OutputTo(this.Context.Player, $"Your ping is {this.Context.Player.Client.Ping}.");
}

public void SampleClass(SampleClass sampleClass)
{
this.chatBox.OutputTo(this.Context.Player, $"sampleClass: {sampleClass.Number}");
}

public void FindPlayer(Player player)
{
this.chatBox.OutputTo(this.Context.Player, $"player: {player}");
}

public async Task Async()
{
this.chatBox.OutputTo(this.Context.Player, "Executing command...");
await Task.Delay(1000);
this.chatBox.OutputTo(this.Context.Player, "Command executed!");
}

public async Task AsyncLong()
{
this.chatBox.OutputTo(this.Context.Player, $"Simulating long execution...");
try
{
await Task.Delay(10_000, this.Context.CancellationToken);
this.chatBox.OutputTo(this.Context.Player, "Long command executed!");
}
catch (OperationCanceledException)
{
Console.WriteLine("Failed to execute long async command :(");
}
}

[Command("tp")]
[Command("teleport")]
Expand All @@ -56,7 +124,7 @@ public void GiveWeapon(WeaponId weapon, ushort ammoCount = 100)
this.Context.Player.AddWeapon(weapon, ammoCount, true);
}

[NoCommand()]
[NoCommand]
public void NoCommand()
{
this.chatBox.OutputTo(this.Context.Player, $"This should not run.");
Expand Down
46 changes: 46 additions & 0 deletions SlipeServer.Example/Logic/LuaControllersExampleLogic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using SlipeServer.LuaControllers.Commands;
using SlipeServer.Server.ElementCollections;
using SlipeServer.Server.Elements;
using SlipeServer.Server.Services;

namespace SlipeServer.Example.Logic;

public class SampleClass
{
public int Number { get; set; }
}

public class LuaControllersExampleLogic
{
private readonly IElementCollection elementCollection;
private readonly ChatBox chatBox;

public LuaControllersExampleLogic(LuaControllerArgumentsMapper mapper, IElementCollection elementCollection, ChatBox chatBox)
{
mapper.DefineMap<SampleClass>(arg =>
{
return new SampleClass
{
Number = int.Parse(arg)
};
});
mapper.DefineMap<Player>(arg =>
{
return elementCollection.GetByType<Player>().Where(x => x.Name.Contains(arg)).FirstOrDefault();
});

mapper.ArgumentErrorOccurred += HandleArgumentErrorOccurred;
this.elementCollection = elementCollection;
this.chatBox = chatBox;
}

private void HandleArgumentErrorOccurred(Player player, Exception exception)
{
if (exception is ArgumentOutOfRangeException)
this.chatBox.OutputTo(player, "Too many or too few arguments");
else if (exception is LuaControllerArgumentException ex)
{
this.chatBox.OutputTo(player, $"Error while executing command, argument at index {ex.Index + 1} expected {ex.ParameterInfo.ParameterType}, got '{ex.Argument}'");
}
}
}
52 changes: 52 additions & 0 deletions SlipeServer.Example/Logic/ResourcesExampleLogic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using SlipeServer.Lua;
using SlipeServer.Server;
using SlipeServer.Server.Elements;
using SlipeServer.Server.Resources;
using SlipeServer.Server.Resources.Providers;
using SlipeServer.Server.Services;
using System.Text;

namespace SlipeServer.Example.Logic;

public sealed class ResourcesExampleLogic
{
private readonly ChatBox chatBox;
private readonly CommandService commandService;
private readonly LuaEventService luaEventService;
private readonly Resource? testResource;
private readonly Resource? secondTestResource;
private readonly Resource? thirdTestResource;
private readonly RootElement rootElement;

public ResourcesExampleLogic(MtaServer mtaServer, IResourceProvider resourceProvider, ChatBox chatBox, CommandService commandService, LuaEventService luaEventService)
{
this.chatBox = chatBox;
this.commandService = commandService;
this.luaEventService = luaEventService;
this.rootElement = mtaServer.RootElement;
this.testResource = resourceProvider.GetResource("TestResource");
this.secondTestResource = resourceProvider.GetResource("SecondTestResource");
this.secondTestResource.NoClientScripts[$"{secondTestResource!.Name}/testfile.lua"] =
Encoding.UTF8.GetBytes("outputChatBox(\"I AM A NOT CACHED MESSAGE\")");
this.secondTestResource.NoClientScripts[$"blabla.lua"] = new byte[] { };

this.thirdTestResource = resourceProvider.GetResource("MetaXmlTestResource");

mtaServer.PlayerJoined += HandlePlayerJoined;
}

private void AddCommands()
{
this.commandService.AddCommand("latent").Triggered += (source, args) =>
{
this.luaEventService.TriggerLatentEvent("Slipe.Test.ClientEvent", this.testResource!, this.rootElement, 1, this.rootElement, 50, "STRING");
};
}
private void HandlePlayerJoined(Player player)
{
this.testResource?.StartFor(player);
this.secondTestResource?.StartFor(player);
this.thirdTestResource?.StartFor(player);
this.chatBox.OutputTo(player, "Resources started");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using SlipeServer.Server.Elements;
using SlipeServer.Server.Services;

namespace SlipeServer.Example;
namespace SlipeServer.Example.Logic;

public class ServerExampleLogic
{
Expand Down Expand Up @@ -87,7 +87,7 @@ private void AddVehiclesCommands()

private void AddCommand(string command, Action<Player> callback)
{
this.commandService.AddCommand(command).Triggered += (object? sender, Server.Events.CommandTriggeredEventArgs e) =>
this.commandService.AddCommand(command).Triggered += (sender, e) =>
{
callback(e.Player);
};
Expand Down
5 changes: 4 additions & 1 deletion SlipeServer.Example/ServerBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SlipeServer.LuaControllers;
using SlipeServer.Example.Logic;
using SlipeServer.LuaControllers;
using SlipeServer.Server.ServerBuilders;

namespace SlipeServer.Example;
Expand All @@ -8,6 +9,8 @@ public static class ServerBuilderExtensions
public static ServerBuilder AddExampleLogic(this ServerBuilder builder)
{
builder.AddLogic<ServerExampleLogic>();
builder.AddLogic<LuaControllersExampleLogic>();
builder.AddLogic<ResourcesExampleLogic>();
builder.AddLuaControllers();

return builder;
Expand Down
6 changes: 6 additions & 0 deletions SlipeServer.Example/SlipeServer.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
<Using Include="SlipeServer.Example.Services" />
<Using Include="SlipeServer.Example.LuaValues" />
</ItemGroup>

<ItemGroup>
<None Update="Resources\**\*.lua">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion SlipeServer.Hosting/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SlipeServer.Hosting;
using SlipeServer.Server.ServerBuilders;

namespace SlipeServer.Hosting;

public static class HostBuilderExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SlipeServer.LuaControllers.Attributes;

[AttributeUsage(AttributeTargets.Class)]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class CommandControllerAttribute(bool usesScopedEvents = true) : Attribute
{
public bool UsesScopedCommands { get; } = usesScopedEvents;
Expand Down
6 changes: 2 additions & 4 deletions SlipeServer.LuaControllers/Attributes/NoCommandAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
namespace SlipeServer.LuaControllers.Attributes;

[AttributeUsage(AttributeTargets.Method)]
public class NoCommandAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class NoCommandAttribute : Attribute;
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
namespace SlipeServer.LuaControllers.Attributes;

[AttributeUsage(AttributeTargets.Method)]
public class NoLuaEventAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class NoLuaEventAttribute : Attribute;
Loading