Skip to content

Commit

Permalink
Merge pull request #593 from caioavidal/remove-autofac
Browse files Browse the repository at this point in the history
Migrate Autofac to native Dependency Injection Framework
  • Loading branch information
MUN1Z authored Dec 17, 2024
2 parents 44d4c64 + 10e6bbb commit 25ae4e7
Show file tree
Hide file tree
Showing 69 changed files with 664 additions and 601 deletions.
2 changes: 0 additions & 2 deletions data/extensions/Spells/Commands/InfoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
using NeoServer.Game.Common;
using NeoServer.Game.Common.Contracts.Creatures;
using NeoServer.Game.Common.Contracts.Items;
using NeoServer.Game.Common.Contracts.Items.Types;
using NeoServer.Game.Common.Creatures;
using NeoServer.Game.Items;
using NeoServer.Networking.Packets.Outgoing;
using NeoServer.Server.Common.Contracts;
using NeoServer.Server.Configurations;
using NeoServer.Server.Helpers;
Expand Down
2 changes: 0 additions & 2 deletions data/extensions/Spells/Commands/NpcCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using NeoServer.Game.Common.Contracts.World.Tiles;
using NeoServer.Game.Common.Location;
using NeoServer.Game.Creatures.Factories;
using NeoServer.Game.Creatures.Monster;
using NeoServer.Game.World.Map;
using NeoServer.Game.World.Models.Spawns;

namespace NeoServer.Extensions.Spells.Commands;

Expand Down
2 changes: 0 additions & 2 deletions data/extensions/Spells/Commands/SummonCreator.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using NeoServer.Game.Combat.Spells;
using NeoServer.Game.Common;
using NeoServer.Game.Common.Contracts.Creatures;
using NeoServer.Game.Common.Contracts.Services;
using NeoServer.Game.Common.Contracts.World.Tiles;
using NeoServer.Game.Common.Location;
using NeoServer.Game.Creatures.Factories;
using NeoServer.Game.World.Map;
using NeoServer.Server.Helpers;

namespace NeoServer.Extensions.Spells.Commands;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
using NeoServer.Game.Common.Contracts.Creatures;
using NeoServer.Game.Common.Location.Structs;
using NeoServer.Server.Common.Contracts;
using NeoServer.Server.Common.Contracts.Tasks;
using NeoServer.Server.Tasks;

namespace NeoServer.Server.Commands.Movements;

public class WalkToMechanism : IWalkToMechanism
{
private readonly IGameServer game;
private readonly IScheduler _scheduler;

public WalkToMechanism(IGameServer game)
public WalkToMechanism(IScheduler scheduler)
{
this.game = game;
_scheduler = scheduler;
}

public void WalkTo(IPlayer player, Action action, Location toLocation, bool secondChance = false)
Expand All @@ -21,12 +22,12 @@ public void WalkTo(IPlayer player, Action action, Location toLocation, bool seco
{
if (secondChance) return;

Action<ICreature> callBack = _ =>
game.Scheduler.AddEvent(new SchedulerEvent(player.StepDelay,
() => WalkTo(player, action, toLocation, true)));

player.WalkTo(toLocation, callBack);
player.WalkTo(toLocation, CallBack);
return;

void CallBack(ICreature _) =>
_scheduler.AddEvent(
new SchedulerEvent(player.StepDelay, () => WalkTo(player, action, toLocation, true)));
}

action?.Invoke();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NeoServer.Game.Common.Contracts.Creatures;
using NeoServer.Game.Common.Contracts.Items;
using NeoServer.Game.Common.Contracts.World;
using NeoServer.Game.Common.Creatures.Players;
using NeoServer.Game.Common.Helpers;
using NeoServer.Game.Common.Location;
Expand All @@ -12,13 +13,15 @@ namespace NeoServer.Server.Commands.Trade;

public class TradeRequestCommand : ICommand
{
private readonly IGameServer _gameServer;
private readonly IGameCreatureManager _creatureManager;
private readonly SafeTradeSystem _tradeSystem;
private readonly IMap _map;

public TradeRequestCommand(IGameServer gameServer, SafeTradeSystem tradeSystem)
public TradeRequestCommand(SafeTradeSystem tradeSystem, IGameCreatureManager creatureManager, IMap map)
{
_gameServer = gameServer;
_tradeSystem = tradeSystem;
_creatureManager = creatureManager;
_map = map;
}

public void RequestTrade(IPlayer player, TradeRequestPacket packet)
Expand All @@ -28,7 +31,7 @@ public void RequestTrade(IPlayer player, TradeRequestPacket packet)
var item = GetItem(player, packet);
if (item is null) return;

_gameServer.CreatureManager.TryGetPlayer(packet.PlayerId, out var secondPlayer);
_creatureManager.TryGetPlayer(packet.PlayerId, out var secondPlayer);
if (secondPlayer is null) return;

_tradeSystem.Request(player, secondPlayer, item);
Expand All @@ -38,7 +41,7 @@ private IItem GetItem(IPlayer player, TradeRequestPacket packet)
{
if (packet.Location.Type == LocationType.Ground)
{
if (_gameServer.Map[packet.Location] is not { } tile) return null;
if (_map[packet.Location] is not { } tile) return null;
return tile.TopItemOnStack;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NeoServer.Game.Common.Contracts.Creatures;
using NeoServer.Game.Common.Contracts.Items.Types.Usable;
using NeoServer.Game.Common.Contracts.World;
using NeoServer.Game.Common.Contracts.World.Tiles;
using NeoServer.Networking.Packets.Outgoing.Effect;
using NeoServer.Server.Common.Contracts;
Expand All @@ -8,18 +9,19 @@ namespace NeoServer.Server.Events.Items;

public class ItemUsedOnTileEventHandler
{
private readonly IGameServer game;

public ItemUsedOnTileEventHandler(IGameServer game)
private readonly IMap _map;
private readonly IGameCreatureManager _creatureManager;
public ItemUsedOnTileEventHandler(IGameCreatureManager creatureManager, IMap map)
{
this.game = game;
_creatureManager = creatureManager;
_map = map;
}

public void Execute(ICreature usedBy, ITile onTile, IUsableOnTile item)
{
foreach (var spectator in game.Map.GetPlayersAtPositionZone(usedBy.Location))
foreach (var spectator in _map.GetPlayersAtPositionZone(usedBy.Location))
{
if (!game.CreatureManager.GetPlayerConnection(spectator.CreatureId, out var connection)) continue;
if (!_creatureManager.GetPlayerConnection(spectator.CreatureId, out var connection)) continue;

if (item.Metadata.ShootType != default)
connection.OutgoingPackets.Enqueue(new DistanceEffectPacket(usedBy.Location, onTile.Location,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
using Autofac;

using System;
using Microsoft.Extensions.DependencyInjection;
using NeoServer.Game.Combat.Spells;
using NeoServer.Game.Common.Contracts.Items;
using NeoServer.Game.Common.Contracts.Items.Types.Usable;
using NeoServer.Game.Common.Contracts.World;
using NeoServer.Game.Common.Services;
using NeoServer.Game.Items.Events;
using NeoServer.Game.Items.Items.Attributes;
using NeoServer.Game.Items.Items.UsableItems.Runes;
using NeoServer.Game.Systems.SafeTrade;
using NeoServer.Server.Common.Contracts;
using NeoServer.Server.Events.Combat;
using NeoServer.Server.Events.Creature;
using NeoServer.Server.Events.Items;
using NeoServer.Server.Events.Player;
using NeoServer.Server.Events.Player.Trade;
using NeoServer.Server.Events.Server;
Expand All @@ -15,47 +23,60 @@ namespace NeoServer.Server.Events.Subscribers;

public sealed class EventSubscriber
{
private readonly IComponentContext _container;
private readonly IServiceProvider _container;
private readonly IGameServer _gameServer;

private readonly IMap _map;
private readonly SafeTradeSystem _tradeSystem;

public EventSubscriber(IMap map, IGameServer gameServer, IComponentContext container, SafeTradeSystem tradeSystem)
private readonly ItemStartedDecayingEventHandler _itemStartedDecayingEventHandler;
private readonly ItemUsedOnTileEventHandler _itemUsedOnTileEventHandler;
private readonly FieldRuneUsedEventHandler _fieldRuneUsedEventHandler;
private readonly ItemUsedEventHandler _itemUsedEventHandler;
public EventSubscriber(IMap map, IGameServer gameServer, IServiceProvider container, SafeTradeSystem tradeSystem,
ItemStartedDecayingEventHandler itemStartedDecayingEventHandler, ItemUsedOnTileEventHandler itemUsedOnTileEventHandler, FieldRuneUsedEventHandler fieldRuneUsedEventHandler, ItemUsedEventHandler itemUsedEventHandler)
{
_map = map;
_gameServer = gameServer;
_container = container;
_tradeSystem = tradeSystem;
_itemStartedDecayingEventHandler = itemStartedDecayingEventHandler;
_itemUsedOnTileEventHandler = itemUsedOnTileEventHandler;
_fieldRuneUsedEventHandler = fieldRuneUsedEventHandler;
_itemUsedEventHandler = itemUsedEventHandler;
}

public void AttachEvents()
{
_map.OnCreatureAddedOnMap += (creature, cylinder) =>
_container.Resolve<CreatureAddedOnMapEventHandler>().Execute(creature, cylinder);
_container.GetRequiredService<CreatureAddedOnMapEventHandler>().Execute(creature, cylinder);

_map.OnCreatureAddedOnMap += (creature, _) =>
_container.Resolve<PlayerSelfAppearOnMapEventHandler>().Execute(creature);

_map.OnThingRemovedFromTile += _container.Resolve<ThingRemovedFromTileEventHandler>().Execute;
_map.OnCreatureMoved += _container.Resolve<CreatureMovedEventHandler>().Execute;
_map.OnThingMovedFailed += _container.Resolve<InvalidOperationEventHandler>().Execute;
_map.OnThingAddedToTile += _container.Resolve<ThingAddedToTileEventHandler>().Execute;
_map.OnThingUpdatedOnTile += _container.Resolve<ThingUpdatedOnTileEventHandler>().Execute;
_container.GetRequiredService<PlayerSelfAppearOnMapEventHandler>().Execute(creature);

BaseSpell.OnSpellInvoked += _container.Resolve<SpellInvokedEventHandler>().Execute;
_map.OnThingRemovedFromTile += _container.GetRequiredService<ThingRemovedFromTileEventHandler>().Execute;
_map.OnCreatureMoved += _container.GetRequiredService<CreatureMovedEventHandler>().Execute;
_map.OnThingMovedFailed += _container.GetRequiredService<InvalidOperationEventHandler>().Execute;
_map.OnThingAddedToTile += _container.GetRequiredService<ThingAddedToTileEventHandler>().Execute;
_map.OnThingUpdatedOnTile += _container.GetRequiredService<ThingUpdatedOnTileEventHandler>().Execute;

OperationFailService.OnOperationFailed += _container.Resolve<PlayerOperationFailedEventHandler>().Execute;
OperationFailService.OnInvalidOperation += _container.Resolve<PlayerOperationFailedEventHandler>().Execute;
NotificationSenderService.OnNotificationSent += _container.Resolve<NotificationSentEventHandler>().Execute;
_gameServer.OnOpened += _container.Resolve<ServerOpenedEventHandler>().Execute;
BaseSpell.OnSpellInvoked += _container.GetRequiredService<SpellInvokedEventHandler>().Execute;

OperationFailService.OnOperationFailed += _container.GetRequiredService<PlayerOperationFailedEventHandler>().Execute;
OperationFailService.OnInvalidOperation += _container.GetRequiredService<PlayerOperationFailedEventHandler>().Execute;
NotificationSenderService.OnNotificationSent += _container.GetRequiredService<NotificationSentEventHandler>().Execute;
_gameServer.OnOpened += _container.GetRequiredService<ServerOpenedEventHandler>().Execute;

Decayable.OnStarted += _itemStartedDecayingEventHandler.Execute;

IConsumable.OnUsed += _itemUsedEventHandler.Execute;
FieldRune.OnUsedOnTile += _fieldRuneUsedEventHandler.Execute;

AddTradeHandlers();
}

private void AddTradeHandlers()
{
_tradeSystem.OnClosed += _container.Resolve<TradeClosedEventHandler>().Execute;
_tradeSystem.OnTradeRequest += _container.Resolve<TradeRequestedEventHandler>().Execute;
_tradeSystem.OnClosed += _container.GetRequiredService<TradeClosedEventHandler>().Execute;
_tradeSystem.OnTradeRequest += _container.GetRequiredService<TradeRequestedEventHandler>().Execute;
}
}

This file was deleted.

11 changes: 6 additions & 5 deletions src/ApplicationServer/NeoServer.Server.Helpers/IoC.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using Autofac;
using System;
using Microsoft.Extensions.DependencyInjection;

namespace NeoServer.Server.Helpers;

public static class IoC
{
private static IContainer _container;
private static IServiceProvider _container;

public static void Initialize(IContainer container)
public static void Initialize(IServiceProvider container)
{
_container = container;
}

public static T GetInstance<T>()
public static T GetInstance<T>() where T : class
{
return _container.Resolve<T>();
return _container.GetService<T>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="8.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="Serilog" Version="4.2.0" />
</ItemGroup>
Expand All @@ -16,4 +16,8 @@
<ProjectReference Include="..\..\GameWorldSimulator\NeoServer.Game.Common\NeoServer.Game.Common.csproj"/>
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ namespace NeoServer.Server.Configurations;
public enum DatabaseType
{
INMEMORY,
MONGODB,
POSTGRESQL,
MSSQL,
SQLITE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

<ItemGroup>
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" />
<PackageReference Include="Autofac" Version="8.1.1" />
<PackageReference Include="Serilog" Version="4.2.0" />

</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/ApplicationServer/NeoServer.Server/Tasks/Scheduler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Channels;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="8.1.1" />
<PackageReference Include="NLua" Version="1.7.3" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 25ae4e7

Please sign in to comment.