From: AeonLucid Date: Mon, 19 Oct 2020 10:22:45 +0000 (+0200) Subject: Rename Impostor.Server.Api to Impostor.Api X-Git-Tag: v1.2.2~96^2~69 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=4be31d870857cf471fc4709c8b484f74c369a34a;p=rhonda%2Fimpostor.git Rename Impostor.Server.Api to Impostor.Api --- diff --git a/src/Impostor.Api/Events/Attributes/EventListenerAttribute.cs b/src/Impostor.Api/Events/Attributes/EventListenerAttribute.cs new file mode 100644 index 0000000..c8b9360 --- /dev/null +++ b/src/Impostor.Api/Events/Attributes/EventListenerAttribute.cs @@ -0,0 +1,39 @@ +using System; + +namespace Impostor.Server.Events +{ + [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] + public class EventListenerAttribute : Attribute + { + public EventListenerAttribute(EventPriority priority = EventPriority.Normal) + { + Priority = priority; + } + + public EventListenerAttribute(Type @event, EventPriority priority = EventPriority.Normal) + { + Priority = priority; + Event = @event; + } + + /// + /// The priority of the event listener. + /// + public EventPriority Priority { get; set; } + + /// + /// The events that the listener is listening to. + /// + public Type? Event { get; set; } + + /// + /// If set to true, the listener will be called regardless of the . + /// + public bool IgnoreCancelled { get; set; } + + /// + /// The order of the priority. + /// + public int PriorityOrder { get; set; } = 100; + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Events/EventPriority.cs b/src/Impostor.Api/Events/EventPriority.cs new file mode 100644 index 0000000..dbf506e --- /dev/null +++ b/src/Impostor.Api/Events/EventPriority.cs @@ -0,0 +1,12 @@ +namespace Impostor.Server.Events +{ + public enum EventPriority + { + Lowest = 0, + Low = 1, + Normal = 2, + High = 3, + Highest = 4, + Monitor = 5 + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Events/Game/GameCreatedEvent.cs b/src/Impostor.Api/Events/Game/GameCreatedEvent.cs new file mode 100644 index 0000000..a916227 --- /dev/null +++ b/src/Impostor.Api/Events/Game/GameCreatedEvent.cs @@ -0,0 +1,22 @@ +using Impostor.Server.Games; + +namespace Impostor.Server.Events +{ + /// + /// Called whenever a new is created. + /// + public sealed class GameCreatedEvent : IGameEvent + { + /// + /// Initializes a new instance of the class. + /// + /// Instance of the game. + public GameCreatedEvent(IGame game) + { + Game = game; + } + + /// + public IGame Game { get; } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Events/Game/GameDestroyedEvent.cs b/src/Impostor.Api/Events/Game/GameDestroyedEvent.cs new file mode 100644 index 0000000..5d32143 --- /dev/null +++ b/src/Impostor.Api/Events/Game/GameDestroyedEvent.cs @@ -0,0 +1,22 @@ +using Impostor.Server.Games; + +namespace Impostor.Server.Events +{ + /// + /// Called whenever a new is destroyed. + /// + public sealed class GameDestroyedEvent : IGameEvent + { + /// + /// Initializes a new instance of the class. + /// + /// Instance of the game. + public GameDestroyedEvent(IGame game) + { + Game = game; + } + + /// + public IGame Game { get; } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Events/Game/IGameEvent.cs b/src/Impostor.Api/Events/Game/IGameEvent.cs new file mode 100644 index 0000000..8d77c4b --- /dev/null +++ b/src/Impostor.Api/Events/Game/IGameEvent.cs @@ -0,0 +1,9 @@ +using Impostor.Server.Games; + +namespace Impostor.Server.Events +{ + public interface IGameEvent : IEvent + { + IGame Game { get; } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Events/Game/PlayerJoinedGameEvent.cs b/src/Impostor.Api/Events/Game/PlayerJoinedGameEvent.cs new file mode 100644 index 0000000..0a47f64 --- /dev/null +++ b/src/Impostor.Api/Events/Game/PlayerJoinedGameEvent.cs @@ -0,0 +1,18 @@ +using Impostor.Server.Games; +using Impostor.Server.Net; + +namespace Impostor.Server.Events +{ + public class PlayerJoinedGameEvent : IGameEvent + { + public PlayerJoinedGameEvent(IGame game, IClientPlayer player) + { + Game = game; + Player = player; + } + + public IGame Game { get; } + + public IClientPlayer Player { get; } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Events/Game/PlayerLeftGameEvent.cs b/src/Impostor.Api/Events/Game/PlayerLeftGameEvent.cs new file mode 100644 index 0000000..e17c333 --- /dev/null +++ b/src/Impostor.Api/Events/Game/PlayerLeftGameEvent.cs @@ -0,0 +1,21 @@ +using Impostor.Server.Games; +using Impostor.Server.Net; + +namespace Impostor.Server.Events +{ + public class PlayerLeftGameEvent : IGameEvent + { + public PlayerLeftGameEvent(IGame game, IClientPlayer player, bool isBan) + { + Game = game; + Player = player; + IsBan = isBan; + } + + public IGame Game { get; } + + public IClientPlayer Player { get; } + + public bool IsBan { get; } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Events/IEvent.cs b/src/Impostor.Api/Events/IEvent.cs new file mode 100644 index 0000000..e9b3505 --- /dev/null +++ b/src/Impostor.Api/Events/IEvent.cs @@ -0,0 +1,6 @@ +namespace Impostor.Server.Events +{ + public interface IEvent + { + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Events/IEventCancelable.cs b/src/Impostor.Api/Events/IEventCancelable.cs new file mode 100644 index 0000000..75e2364 --- /dev/null +++ b/src/Impostor.Api/Events/IEventCancelable.cs @@ -0,0 +1,10 @@ +namespace Impostor.Server.Events +{ + public interface IEventCancelable : IEvent + { + /// + /// True if the event was cancelled. + /// + bool IsCancelled { get; set; } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Events/IEventListener.cs b/src/Impostor.Api/Events/IEventListener.cs new file mode 100644 index 0000000..3e08c36 --- /dev/null +++ b/src/Impostor.Api/Events/IEventListener.cs @@ -0,0 +1,6 @@ +namespace Impostor.Server.Events +{ + public interface IEventListener + { + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Events/Managers/IEventManager.cs b/src/Impostor.Api/Events/Managers/IEventManager.cs new file mode 100644 index 0000000..11ae01d --- /dev/null +++ b/src/Impostor.Api/Events/Managers/IEventManager.cs @@ -0,0 +1,44 @@ +using System; +using System.Threading.Tasks; + +namespace Impostor.Server.Events.Managers +{ + public interface IEventManager + { + /// + /// Register a temporary event listener. + /// + /// Event callback. + /// Disposable that unregisters the callback from the event manager. + /// Type of the event. + IDisposable Register(Func callback) + where TEvent : IEvent; + + /// + /// Register a temporary event listener. + /// + /// Event listener. + /// Middleware between the events, which can be used to swap to the correct thread dispatcher. + /// Disposable that unregisters the callback from the event manager. + /// Type of the event listener. + IDisposable RegisterListener(TListener listener, Func, Task>? invoker = null) + where TListener : IEventListener; + + /// + /// Returns true if an event with the type is registered. + /// + /// True if the is registered. + /// Type of the event. + bool IsRegistered() + where TEvent : IEvent; + + /// + /// Call all the event listeners for the type . + /// + /// The event argument. + /// Type of the event. + /// A representing the asynchronous operation. + ValueTask CallAsync(TEvent @event) + where TEvent : IEvent; + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Exceptions/ImpostorException.cs b/src/Impostor.Api/Exceptions/ImpostorException.cs new file mode 100644 index 0000000..9e9b8f7 --- /dev/null +++ b/src/Impostor.Api/Exceptions/ImpostorException.cs @@ -0,0 +1,24 @@ +using System; +using System.Runtime.Serialization; + +namespace Impostor.Server +{ + public class ImpostorException : Exception + { + public ImpostorException() + { + } + + protected ImpostorException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + + public ImpostorException(string? message) : base(message) + { + } + + public ImpostorException(string? message, Exception? innerException) : base(message, innerException) + { + } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Games/Extensions/GameManagerExtensions.cs b/src/Impostor.Api/Games/Extensions/GameManagerExtensions.cs new file mode 100644 index 0000000..db173c2 --- /dev/null +++ b/src/Impostor.Api/Games/Extensions/GameManagerExtensions.cs @@ -0,0 +1,14 @@ +using System.Linq; +using Impostor.Server.Games.Managers; +using Impostor.Shared.Innersloth.Data; + +namespace Impostor.Server.Games +{ + public static class GameManagerExtensions + { + public static int GetGameCount(this IGameManager manager, MapFlags map) + { + return manager.Games.Count(game => map.HasFlag((MapFlags)(1 << game.Options.MapId))); + } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Games/GameCode.cs b/src/Impostor.Api/Games/GameCode.cs new file mode 100644 index 0000000..0d1fd37 --- /dev/null +++ b/src/Impostor.Api/Games/GameCode.cs @@ -0,0 +1,74 @@ +using System; +using Impostor.Shared.Innersloth; + +namespace Impostor.Server.Games +{ + public readonly struct GameCode : IEquatable + { + public GameCode(int value) + { + Value = value; + Code = GameCodeParser.IntToGameName(value); + } + + public GameCode(string code) + { + Value = GameCodeParser.GameNameToInt(code); + Code = code; + } + + public string Code { get; } + + public int Value { get; } + + public static implicit operator string(GameCode code) => code.Code; + + public static implicit operator int(GameCode code) => code.Value; + + public static implicit operator GameCode(string code) => From(code); + + public static implicit operator GameCode(int value) => From(value); + + public static bool operator ==(GameCode left, GameCode right) + { + return left.Equals(right); + } + + public static bool operator !=(GameCode left, GameCode right) + { + return !left.Equals(right); + } + + public static GameCode Create() + { + return new GameCode(GameCodeParser.GenerateCode(6)); + } + + public static GameCode From(int value) => new GameCode(value); + + public static GameCode From(string value) => new GameCode(value); + + /// + public bool Equals(GameCode other) + { + return Code == other.Code && Value == other.Value; + } + + /// + public override bool Equals(object? obj) + { + return obj is GameCode other && Equals(other); + } + + /// + public override int GetHashCode() + { + return HashCode.Combine(Code, Value); + } + + public override string ToString() + { + return Code; + } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Games/GameJoinError.cs b/src/Impostor.Api/Games/GameJoinError.cs new file mode 100644 index 0000000..c4d7c6d --- /dev/null +++ b/src/Impostor.Api/Games/GameJoinError.cs @@ -0,0 +1,48 @@ +namespace Impostor.Server.Games +{ + public enum GameJoinError + { + /// + /// No error occured while joining the game. + /// + None, + + /// + /// The client is not registered in the client manager. + /// + InvalidClient, + + /// + /// The client has been banned from the game. + /// + Banned, + + /// + /// The game is full. + /// + GameFull, + + /// + /// The limbo state of the player is incorrect. + /// + InvalidLimbo, + + /// + /// The game is already started. + /// + GameStarted, + + /// + /// The game has been destroyed. + /// + GameDestroyed, + + /// + /// Custom error by a plugin. + /// + /// + /// A custom message can be set in . + /// + Custom, + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Games/GameJoinResult.cs b/src/Impostor.Api/Games/GameJoinResult.cs new file mode 100644 index 0000000..40babeb --- /dev/null +++ b/src/Impostor.Api/Games/GameJoinResult.cs @@ -0,0 +1,48 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using Impostor.Server.Net; + +namespace Impostor.Server.Games +{ + public readonly struct GameJoinResult + { + private GameJoinResult(GameJoinError error, string? message = null, IClientPlayer? player = null) + { + Error = error; + Message = message; + Player = player; + } + + public GameJoinError Error { get; } + + public bool IsSuccess => Error == GameJoinError.None; + + public bool IsCustomError => Error == GameJoinError.Custom; + + [MemberNotNullWhen(true, nameof(IsCustomError))] + public string? Message { get; } + + [MemberNotNullWhen(true, nameof(IsSuccess))] + public IClientPlayer? Player { get; } + + public static GameJoinResult CreateCustomError(string message) + { + return new GameJoinResult(GameJoinError.Custom, message); + } + + public static GameJoinResult CreateSuccess(IClientPlayer player) + { + return new GameJoinResult(GameJoinError.None, player: player); + } + + public static GameJoinResult FromError(GameJoinError error) + { + if (error == GameJoinError.Custom) + { + throw new InvalidOperationException($"Custom errors should provide a message, use {nameof(CreateCustomError)} instead."); + } + + return new GameJoinResult(error); + } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Games/IGame.cs b/src/Impostor.Api/Games/IGame.cs new file mode 100644 index 0000000..dcd25a2 --- /dev/null +++ b/src/Impostor.Api/Games/IGame.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using System.Net; +using Impostor.Server.Net; +using Impostor.Server.Net.Messages; +using Impostor.Shared.Innersloth; +using Impostor.Shared.Innersloth.Data; + +namespace Impostor.Server.Games +{ + public interface IGame + { + GameOptionsData Options { get; } + + GameCode Code { get; } + + GameStates GameState { get; } + + IEnumerable Players { get; } + + IPEndPoint PublicIp { get; } + + int PlayerCount { get; } + + IClientPlayer Host { get; } + + bool IsPublic { get; } + + IDictionary Items { get; } + + int HostId { get; } + + IGameMessageWriter CreateMessage(MessageType type); + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Games/Managers/IGameManager.cs b/src/Impostor.Api/Games/Managers/IGameManager.cs new file mode 100644 index 0000000..d089772 --- /dev/null +++ b/src/Impostor.Api/Games/Managers/IGameManager.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace Impostor.Server.Games.Managers +{ + public interface IGameManager + { + IEnumerable Games { get; } + + IGame? Find(GameCode code); + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Impostor.Api.csproj b/src/Impostor.Api/Impostor.Api.csproj new file mode 100644 index 0000000..0e72d47 --- /dev/null +++ b/src/Impostor.Api/Impostor.Api.csproj @@ -0,0 +1,24 @@ + + + + net5.0 + Impostor.Server + enable + None + ProjectRules.ruleset + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + \ No newline at end of file diff --git a/src/Impostor.Api/Impostor.Api.csproj.DotSettings b/src/Impostor.Api/Impostor.Api.csproj.DotSettings new file mode 100644 index 0000000..2df7445 --- /dev/null +++ b/src/Impostor.Api/Impostor.Api.csproj.DotSettings @@ -0,0 +1,8 @@ + + True + True + True + True + True + True + True \ No newline at end of file diff --git a/src/Impostor.Api/Net/Extensions/GameMessageWriterExtensions.cs b/src/Impostor.Api/Net/Extensions/GameMessageWriterExtensions.cs new file mode 100644 index 0000000..d995b5a --- /dev/null +++ b/src/Impostor.Api/Net/Extensions/GameMessageWriterExtensions.cs @@ -0,0 +1,41 @@ +using System; +using System.Threading.Tasks; +using Impostor.Server.Net.Messages; + +namespace Impostor.Server.Net +{ + public static class GameMessageWriterExtensions + { + public static ValueTask SendToAllExceptAsync(this IGameMessageWriter writer, LimboStates states, int? id) + { + return id.HasValue + ? writer.SendToAllExceptAsync(id.Value, states) + : writer.SendToAllAsync(states); + } + + public static ValueTask SendToAllExceptAsync(this IGameMessageWriter writer, LimboStates states, IClient client) + { + if (client == null) + { + throw new ArgumentNullException(nameof(client)); + } + + return writer.SendToAllExceptAsync(client.Id, states); + } + + public static ValueTask SendToAsync(this IGameMessageWriter writer, IClient client) + { + if (client == null) + { + throw new ArgumentNullException(nameof(client)); + } + + return writer.SendToAsync(client.Id); + } + + public static ValueTask SendToAsync(this IGameMessageWriter writer, IClientPlayer player) + { + return SendToAsync(writer, player.Client); + } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/IClient.cs b/src/Impostor.Api/Net/IClient.cs new file mode 100644 index 0000000..0c508c4 --- /dev/null +++ b/src/Impostor.Api/Net/IClient.cs @@ -0,0 +1,60 @@ +using System.Collections.Generic; + +namespace Impostor.Server.Net +{ + /// + /// Represents a connected game client. + /// + public interface IClient + { + /// + /// Gets or sets the unique ID of the client. + /// + /// + /// This ID is generated when the client is registered in the client manager and should not be used + /// to store persisted data. + /// + int Id { get; set; } + + /// + /// Gets the name that was provided by the player in the client. + /// + /// + /// The name is provided by the player and should not be used to store persisted data. + /// + string Name { get; } + + /// + /// Gets the connection of the client. + /// + /// + /// Null when the client was not registered by the matchmaker. + /// + IConnection? Connection { get; } + + /// + /// Gets a value indicating whether the client is a bot. + /// + bool IsBot { get; } + + /// + /// Gets a key/value collection that can be used to share data between messages. + /// + /// + /// + /// The stored data will not be saved. + /// After the connection has been closed all data will be lost. + /// + /// + /// Note that the values will not be disposed after the connection has been closed. + /// This has to be implemented by the plugin. + /// + /// + IDictionary Items { get; } + + /// + /// Gets or sets the current game data of the . + /// + IClientPlayer? Player { get; } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/IClientPlayer.cs b/src/Impostor.Api/Net/IClientPlayer.cs new file mode 100644 index 0000000..4e4252f --- /dev/null +++ b/src/Impostor.Api/Net/IClientPlayer.cs @@ -0,0 +1,30 @@ +using System.Threading.Tasks; +using Impostor.Server.Games; + +namespace Impostor.Server.Net +{ + /// + /// Represents a player in . + /// + public interface IClientPlayer + { + /// + /// Gets the client that belongs to the player. + /// + IClient Client { get; } + + /// + /// Gets the game where the belongs to. + /// + IGame Game { get; } + + /// + /// Gets or sets the current limbo state of the player. + /// + LimboStates Limbo { get; set; } + + ValueTask KickAsync(); + + ValueTask BanAsync(); + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/IConnection.cs b/src/Impostor.Api/Net/IConnection.cs new file mode 100644 index 0000000..f619dfa --- /dev/null +++ b/src/Impostor.Api/Net/IConnection.cs @@ -0,0 +1,38 @@ +using System.Net; +using Impostor.Server.Games; +using Impostor.Server.Net.Messages; + +namespace Impostor.Server.Net +{ + /// + /// Represents the connection of the client. + /// + public interface IConnection + { + /// + /// Gets the IP endpoint of the client. + /// + IPEndPoint EndPoint { get; } + + /// + /// Gets a value indicating whether the client is connected to the server. + /// + bool IsConnected { get; } + + /// + /// Gets the client of the connection. + /// + IClient? Client { get; } + + /// + /// Create a message writer that can be send to the connection. + /// + /// + /// Be aware when implementing a custom connection handler that this method is not called when a message + /// is being send in . + /// + /// Type of the message. + /// Message writer for the current connection. + IConnectionMessageWriter CreateMessage(MessageType messageType); + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/LimboStates.cs b/src/Impostor.Api/Net/LimboStates.cs new file mode 100644 index 0000000..1812f5a --- /dev/null +++ b/src/Impostor.Api/Net/LimboStates.cs @@ -0,0 +1,13 @@ +using System; + +namespace Impostor.Server.Net +{ + [Flags] + public enum LimboStates + { + PreSpawn = 1, + NotLimbo = 2, + WaitingForHost = 4, + All = PreSpawn | NotLimbo | WaitingForHost + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/Manager/IClientManager.cs b/src/Impostor.Api/Net/Manager/IClientManager.cs new file mode 100644 index 0000000..6405a65 --- /dev/null +++ b/src/Impostor.Api/Net/Manager/IClientManager.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Impostor.Server.Net.Manager +{ + public interface IClientManager + { + IEnumerable Clients { get; } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/Messages/IConnectionMessageWriter.cs b/src/Impostor.Api/Net/Messages/IConnectionMessageWriter.cs new file mode 100644 index 0000000..453eb7e --- /dev/null +++ b/src/Impostor.Api/Net/Messages/IConnectionMessageWriter.cs @@ -0,0 +1,21 @@ +using System.Threading.Tasks; + +namespace Impostor.Server.Net.Messages +{ + /// + /// Represents the message writer for . + /// + public interface IConnectionMessageWriter : IMessageWriter + { + /// + /// Gets the connection where the message writer belongs to. + /// + public IConnection Connection { get; } + + /// + /// Sends the message to the . + /// + /// Task. + ValueTask SendAsync(); + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/Messages/IGameMessageWriter.cs b/src/Impostor.Api/Net/Messages/IGameMessageWriter.cs new file mode 100644 index 0000000..d1d769c --- /dev/null +++ b/src/Impostor.Api/Net/Messages/IGameMessageWriter.cs @@ -0,0 +1,33 @@ +using System.Threading.Tasks; +using Impostor.Server.Games; + +namespace Impostor.Server.Net.Messages +{ + /// + /// Represents the message writer for . + /// + public interface IGameMessageWriter : IMessageWriter + { + /// + /// Send the message to all players. + /// + /// Required limbo state of the player. + /// A representing the asynchronous operation. + ValueTask SendToAllAsync(LimboStates states = LimboStates.NotLimbo); + + /// + /// Send the message to all players except one. + /// + /// The player to exclude from sending the message. + /// Required limbo state of the player. + /// A representing the asynchronous operation. + ValueTask SendToAllExceptAsync(int senderId, LimboStates states = LimboStates.NotLimbo); + + /// + /// Send a message to a specific player. + /// + /// ID of the client. + /// A representing the asynchronous operation. + ValueTask SendToAsync(int id); + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/Messages/IMessage.cs b/src/Impostor.Api/Net/Messages/IMessage.cs new file mode 100644 index 0000000..854a0d2 --- /dev/null +++ b/src/Impostor.Api/Net/Messages/IMessage.cs @@ -0,0 +1,9 @@ +namespace Impostor.Server.Net.Messages +{ + public interface IMessage + { + MessageType Type { get; } + + IMessageReader CreateReader(); + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/Messages/IMessageReader.cs b/src/Impostor.Api/Net/Messages/IMessageReader.cs new file mode 100644 index 0000000..aeefcc6 --- /dev/null +++ b/src/Impostor.Api/Net/Messages/IMessageReader.cs @@ -0,0 +1,61 @@ +using System; + +namespace Impostor.Server.Net.Messages +{ + public interface IMessageReader + { + /// + /// Gets the tag of the message. + /// + byte Tag { get; } + + /// + /// Gets the buffer of the message. + /// + ReadOnlyMemory Buffer { get; } + + /// + /// Gets the current position of the reader. + /// + int Position { get; } + + /// + /// Gets the length of the buffer. + /// + int Length { get; } + + IMessageReader ReadMessage(); + + bool ReadBoolean(); + + sbyte ReadSByte(); + + byte ReadByte(); + + ushort ReadUInt16(); + + short ReadInt16(); + + uint ReadUInt32(); + + int ReadInt32(); + + float ReadSingle(); + + string ReadString(); + + ReadOnlyMemory ReadBytesAndSize(); + + ReadOnlyMemory ReadBytes(int length); + + int ReadPackedInt32(); + + uint ReadPackedUInt32(); + + void CopyTo(IMessageWriter writer); + + IMessageReader Slice(int start); + + IMessageReader Slice(int start, int length); + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/Messages/IMessageWriter.cs b/src/Impostor.Api/Net/Messages/IMessageWriter.cs new file mode 100644 index 0000000..add3154 --- /dev/null +++ b/src/Impostor.Api/Net/Messages/IMessageWriter.cs @@ -0,0 +1,113 @@ +using System; +using System.Net; +using Impostor.Server.Games; + +namespace Impostor.Server.Net.Messages +{ + /// + /// Base message writer. + /// + public interface IMessageWriter : IDisposable + { + /// + /// Writes a boolean to the message. + /// + /// Value to write. + void Write(bool value); + + /// + /// Writes a sbyte to the message. + /// + /// Value to write. + void Write(sbyte value); + + /// + /// Writes a byte to the message. + /// + /// Value to write. + void Write(byte value); + + /// + /// Writes a short to the message. + /// + /// Value to write. + void Write(short value); + + /// + /// Writes an ushort to the message. + /// + /// Value to write. + void Write(ushort value); + + /// + /// Writes an uint to the message. + /// + /// Value to write. + void Write(uint value); + + /// + /// Writes an int to the message. + /// + /// Value to write. + void Write(int value); + + /// + /// Writes a float to the message. + /// + /// Value to write. + void Write(float value); + + /// + /// Writes a string to the message. + /// + /// Value to write. + void Write(string value); + + /// + /// Writes a to the message. + /// + /// Value to write. + void Write(IPAddress value); + + /// + /// Writes an packed int to the message. + /// + /// Value to write. + void WritePacked(int value); + + /// + /// Writes an packed uint to the message. + /// + /// Value to write. + void WritePacked(uint value); + + /// + /// Writes raw bytes to the message. + /// + /// Bytes to write. + void Write(ReadOnlyMemory data); + + /// + /// Writes a game code to the message. + /// + /// Value to write. + void Write(GameCode value); + + /// + /// Starts a new message. + /// + /// Message flag header. + void StartMessage(byte typeFlag); + + /// + /// Mark the end of the message. + /// + void EndMessage(); + + /// + /// Clear the message writer. + /// + /// New type of the message. + void Clear(MessageType type); + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Net/Messages/MessageType.cs b/src/Impostor.Api/Net/Messages/MessageType.cs new file mode 100644 index 0000000..a350cb6 --- /dev/null +++ b/src/Impostor.Api/Net/Messages/MessageType.cs @@ -0,0 +1,29 @@ +namespace Impostor.Server.Net.Messages +{ + /// + /// Specifies how a message should be sent between connections. + /// + public enum MessageType + { + /// + /// Requests unreliable delivery with no fragmentation. + /// + /// + /// Sending data using unreliable delivery means that data is not guaranteed to arrive at it's destination nor is + /// it guaranteed to arrive only once. However, unreliable delivery can be faster than other methods and it + /// typically requires a smaller number of protocol bytes than other methods. There is also typically less + /// processing involved and less memory needed as packets are not stored once sent. + /// + Unreliable, + + /// + /// Requests data be sent reliably but with no fragmentation. + /// + /// + /// Sending data reliably means that data is guaranteed to arrive and to arrive only once. Reliable delivery + /// typically requires more processing, more memory (as packets need to be stored in case they need resending), + /// a larger number of protocol bytes and can be slower than unreliable delivery. + /// + Reliable, + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Plugins/IPlugin.cs b/src/Impostor.Api/Plugins/IPlugin.cs new file mode 100644 index 0000000..e5f8645 --- /dev/null +++ b/src/Impostor.Api/Plugins/IPlugin.cs @@ -0,0 +1,20 @@ +using System.Threading.Tasks; +using Impostor.Server.Events; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace Impostor.Server.Plugins +{ + public interface IPlugin : IEventListener + { + ValueTask EnableAsync(); + + ValueTask DisableAsync(); + + ValueTask ReloadAsync(); + + void ConfigureHost(IHostBuilder host); + + void ConfigureServices(IServiceCollection services); + } +} \ No newline at end of file diff --git a/src/Impostor.Api/Plugins/PluginBase.cs b/src/Impostor.Api/Plugins/PluginBase.cs new file mode 100644 index 0000000..5ef6cf3 --- /dev/null +++ b/src/Impostor.Api/Plugins/PluginBase.cs @@ -0,0 +1,32 @@ +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace Impostor.Server.Plugins +{ + public class PluginBase : IPlugin + { + public virtual ValueTask EnableAsync() + { + return default; + } + + public virtual ValueTask DisableAsync() + { + return default; + } + + public virtual ValueTask ReloadAsync() + { + return default; + } + + public virtual void ConfigureHost(IHostBuilder host) + { + } + + public virtual void ConfigureServices(IServiceCollection services) + { + } + } +} \ No newline at end of file diff --git a/src/Impostor.Api/ProjectRules.ruleset b/src/Impostor.Api/ProjectRules.ruleset new file mode 100644 index 0000000..a21a8d4 --- /dev/null +++ b/src/Impostor.Api/ProjectRules.ruleset @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Impostor.Plugins.Debugger/Impostor.Plugins.Debugger.csproj b/src/Impostor.Plugins.Debugger/Impostor.Plugins.Debugger.csproj index 1852d26..084174e 100644 --- a/src/Impostor.Plugins.Debugger/Impostor.Plugins.Debugger.csproj +++ b/src/Impostor.Plugins.Debugger/Impostor.Plugins.Debugger.csproj @@ -6,7 +6,7 @@ - + \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/Attributes/EventListenerAttribute.cs b/src/Impostor.Server.Api/Events/Attributes/EventListenerAttribute.cs deleted file mode 100644 index c8b9360..0000000 --- a/src/Impostor.Server.Api/Events/Attributes/EventListenerAttribute.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; - -namespace Impostor.Server.Events -{ - [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] - public class EventListenerAttribute : Attribute - { - public EventListenerAttribute(EventPriority priority = EventPriority.Normal) - { - Priority = priority; - } - - public EventListenerAttribute(Type @event, EventPriority priority = EventPriority.Normal) - { - Priority = priority; - Event = @event; - } - - /// - /// The priority of the event listener. - /// - public EventPriority Priority { get; set; } - - /// - /// The events that the listener is listening to. - /// - public Type? Event { get; set; } - - /// - /// If set to true, the listener will be called regardless of the . - /// - public bool IgnoreCancelled { get; set; } - - /// - /// The order of the priority. - /// - public int PriorityOrder { get; set; } = 100; - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/EventPriority.cs b/src/Impostor.Server.Api/Events/EventPriority.cs deleted file mode 100644 index dbf506e..0000000 --- a/src/Impostor.Server.Api/Events/EventPriority.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Impostor.Server.Events -{ - public enum EventPriority - { - Lowest = 0, - Low = 1, - Normal = 2, - High = 3, - Highest = 4, - Monitor = 5 - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/Game/GameCreatedEvent.cs b/src/Impostor.Server.Api/Events/Game/GameCreatedEvent.cs deleted file mode 100644 index a916227..0000000 --- a/src/Impostor.Server.Api/Events/Game/GameCreatedEvent.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Impostor.Server.Games; - -namespace Impostor.Server.Events -{ - /// - /// Called whenever a new is created. - /// - public sealed class GameCreatedEvent : IGameEvent - { - /// - /// Initializes a new instance of the class. - /// - /// Instance of the game. - public GameCreatedEvent(IGame game) - { - Game = game; - } - - /// - public IGame Game { get; } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/Game/GameDestroyedEvent.cs b/src/Impostor.Server.Api/Events/Game/GameDestroyedEvent.cs deleted file mode 100644 index 5d32143..0000000 --- a/src/Impostor.Server.Api/Events/Game/GameDestroyedEvent.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Impostor.Server.Games; - -namespace Impostor.Server.Events -{ - /// - /// Called whenever a new is destroyed. - /// - public sealed class GameDestroyedEvent : IGameEvent - { - /// - /// Initializes a new instance of the class. - /// - /// Instance of the game. - public GameDestroyedEvent(IGame game) - { - Game = game; - } - - /// - public IGame Game { get; } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/Game/IGameEvent.cs b/src/Impostor.Server.Api/Events/Game/IGameEvent.cs deleted file mode 100644 index 8d77c4b..0000000 --- a/src/Impostor.Server.Api/Events/Game/IGameEvent.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Impostor.Server.Games; - -namespace Impostor.Server.Events -{ - public interface IGameEvent : IEvent - { - IGame Game { get; } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/Game/PlayerJoinedGameEvent.cs b/src/Impostor.Server.Api/Events/Game/PlayerJoinedGameEvent.cs deleted file mode 100644 index 0a47f64..0000000 --- a/src/Impostor.Server.Api/Events/Game/PlayerJoinedGameEvent.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Impostor.Server.Games; -using Impostor.Server.Net; - -namespace Impostor.Server.Events -{ - public class PlayerJoinedGameEvent : IGameEvent - { - public PlayerJoinedGameEvent(IGame game, IClientPlayer player) - { - Game = game; - Player = player; - } - - public IGame Game { get; } - - public IClientPlayer Player { get; } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/Game/PlayerLeftGameEvent.cs b/src/Impostor.Server.Api/Events/Game/PlayerLeftGameEvent.cs deleted file mode 100644 index e17c333..0000000 --- a/src/Impostor.Server.Api/Events/Game/PlayerLeftGameEvent.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Impostor.Server.Games; -using Impostor.Server.Net; - -namespace Impostor.Server.Events -{ - public class PlayerLeftGameEvent : IGameEvent - { - public PlayerLeftGameEvent(IGame game, IClientPlayer player, bool isBan) - { - Game = game; - Player = player; - IsBan = isBan; - } - - public IGame Game { get; } - - public IClientPlayer Player { get; } - - public bool IsBan { get; } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/IEvent.cs b/src/Impostor.Server.Api/Events/IEvent.cs deleted file mode 100644 index e9b3505..0000000 --- a/src/Impostor.Server.Api/Events/IEvent.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Impostor.Server.Events -{ - public interface IEvent - { - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/IEventCancelable.cs b/src/Impostor.Server.Api/Events/IEventCancelable.cs deleted file mode 100644 index 75e2364..0000000 --- a/src/Impostor.Server.Api/Events/IEventCancelable.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Impostor.Server.Events -{ - public interface IEventCancelable : IEvent - { - /// - /// True if the event was cancelled. - /// - bool IsCancelled { get; set; } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/IEventListener.cs b/src/Impostor.Server.Api/Events/IEventListener.cs deleted file mode 100644 index 3e08c36..0000000 --- a/src/Impostor.Server.Api/Events/IEventListener.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Impostor.Server.Events -{ - public interface IEventListener - { - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Events/Managers/IEventManager.cs b/src/Impostor.Server.Api/Events/Managers/IEventManager.cs deleted file mode 100644 index 11ae01d..0000000 --- a/src/Impostor.Server.Api/Events/Managers/IEventManager.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace Impostor.Server.Events.Managers -{ - public interface IEventManager - { - /// - /// Register a temporary event listener. - /// - /// Event callback. - /// Disposable that unregisters the callback from the event manager. - /// Type of the event. - IDisposable Register(Func callback) - where TEvent : IEvent; - - /// - /// Register a temporary event listener. - /// - /// Event listener. - /// Middleware between the events, which can be used to swap to the correct thread dispatcher. - /// Disposable that unregisters the callback from the event manager. - /// Type of the event listener. - IDisposable RegisterListener(TListener listener, Func, Task>? invoker = null) - where TListener : IEventListener; - - /// - /// Returns true if an event with the type is registered. - /// - /// True if the is registered. - /// Type of the event. - bool IsRegistered() - where TEvent : IEvent; - - /// - /// Call all the event listeners for the type . - /// - /// The event argument. - /// Type of the event. - /// A representing the asynchronous operation. - ValueTask CallAsync(TEvent @event) - where TEvent : IEvent; - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Exceptions/ImpostorException.cs b/src/Impostor.Server.Api/Exceptions/ImpostorException.cs deleted file mode 100644 index 9e9b8f7..0000000 --- a/src/Impostor.Server.Api/Exceptions/ImpostorException.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Runtime.Serialization; - -namespace Impostor.Server -{ - public class ImpostorException : Exception - { - public ImpostorException() - { - } - - protected ImpostorException(SerializationInfo info, StreamingContext context) : base(info, context) - { - } - - public ImpostorException(string? message) : base(message) - { - } - - public ImpostorException(string? message, Exception? innerException) : base(message, innerException) - { - } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Games/Extensions/GameManagerExtensions.cs b/src/Impostor.Server.Api/Games/Extensions/GameManagerExtensions.cs deleted file mode 100644 index db173c2..0000000 --- a/src/Impostor.Server.Api/Games/Extensions/GameManagerExtensions.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.Linq; -using Impostor.Server.Games.Managers; -using Impostor.Shared.Innersloth.Data; - -namespace Impostor.Server.Games -{ - public static class GameManagerExtensions - { - public static int GetGameCount(this IGameManager manager, MapFlags map) - { - return manager.Games.Count(game => map.HasFlag((MapFlags)(1 << game.Options.MapId))); - } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Games/GameCode.cs b/src/Impostor.Server.Api/Games/GameCode.cs deleted file mode 100644 index 0d1fd37..0000000 --- a/src/Impostor.Server.Api/Games/GameCode.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using Impostor.Shared.Innersloth; - -namespace Impostor.Server.Games -{ - public readonly struct GameCode : IEquatable - { - public GameCode(int value) - { - Value = value; - Code = GameCodeParser.IntToGameName(value); - } - - public GameCode(string code) - { - Value = GameCodeParser.GameNameToInt(code); - Code = code; - } - - public string Code { get; } - - public int Value { get; } - - public static implicit operator string(GameCode code) => code.Code; - - public static implicit operator int(GameCode code) => code.Value; - - public static implicit operator GameCode(string code) => From(code); - - public static implicit operator GameCode(int value) => From(value); - - public static bool operator ==(GameCode left, GameCode right) - { - return left.Equals(right); - } - - public static bool operator !=(GameCode left, GameCode right) - { - return !left.Equals(right); - } - - public static GameCode Create() - { - return new GameCode(GameCodeParser.GenerateCode(6)); - } - - public static GameCode From(int value) => new GameCode(value); - - public static GameCode From(string value) => new GameCode(value); - - /// - public bool Equals(GameCode other) - { - return Code == other.Code && Value == other.Value; - } - - /// - public override bool Equals(object? obj) - { - return obj is GameCode other && Equals(other); - } - - /// - public override int GetHashCode() - { - return HashCode.Combine(Code, Value); - } - - public override string ToString() - { - return Code; - } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Games/GameJoinError.cs b/src/Impostor.Server.Api/Games/GameJoinError.cs deleted file mode 100644 index c4d7c6d..0000000 --- a/src/Impostor.Server.Api/Games/GameJoinError.cs +++ /dev/null @@ -1,48 +0,0 @@ -namespace Impostor.Server.Games -{ - public enum GameJoinError - { - /// - /// No error occured while joining the game. - /// - None, - - /// - /// The client is not registered in the client manager. - /// - InvalidClient, - - /// - /// The client has been banned from the game. - /// - Banned, - - /// - /// The game is full. - /// - GameFull, - - /// - /// The limbo state of the player is incorrect. - /// - InvalidLimbo, - - /// - /// The game is already started. - /// - GameStarted, - - /// - /// The game has been destroyed. - /// - GameDestroyed, - - /// - /// Custom error by a plugin. - /// - /// - /// A custom message can be set in . - /// - Custom, - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Games/GameJoinResult.cs b/src/Impostor.Server.Api/Games/GameJoinResult.cs deleted file mode 100644 index 40babeb..0000000 --- a/src/Impostor.Server.Api/Games/GameJoinResult.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Diagnostics.CodeAnalysis; -using Impostor.Server.Net; - -namespace Impostor.Server.Games -{ - public readonly struct GameJoinResult - { - private GameJoinResult(GameJoinError error, string? message = null, IClientPlayer? player = null) - { - Error = error; - Message = message; - Player = player; - } - - public GameJoinError Error { get; } - - public bool IsSuccess => Error == GameJoinError.None; - - public bool IsCustomError => Error == GameJoinError.Custom; - - [MemberNotNullWhen(true, nameof(IsCustomError))] - public string? Message { get; } - - [MemberNotNullWhen(true, nameof(IsSuccess))] - public IClientPlayer? Player { get; } - - public static GameJoinResult CreateCustomError(string message) - { - return new GameJoinResult(GameJoinError.Custom, message); - } - - public static GameJoinResult CreateSuccess(IClientPlayer player) - { - return new GameJoinResult(GameJoinError.None, player: player); - } - - public static GameJoinResult FromError(GameJoinError error) - { - if (error == GameJoinError.Custom) - { - throw new InvalidOperationException($"Custom errors should provide a message, use {nameof(CreateCustomError)} instead."); - } - - return new GameJoinResult(error); - } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Games/IGame.cs b/src/Impostor.Server.Api/Games/IGame.cs deleted file mode 100644 index dcd25a2..0000000 --- a/src/Impostor.Server.Api/Games/IGame.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using System.Net; -using Impostor.Server.Net; -using Impostor.Server.Net.Messages; -using Impostor.Shared.Innersloth; -using Impostor.Shared.Innersloth.Data; - -namespace Impostor.Server.Games -{ - public interface IGame - { - GameOptionsData Options { get; } - - GameCode Code { get; } - - GameStates GameState { get; } - - IEnumerable Players { get; } - - IPEndPoint PublicIp { get; } - - int PlayerCount { get; } - - IClientPlayer Host { get; } - - bool IsPublic { get; } - - IDictionary Items { get; } - - int HostId { get; } - - IGameMessageWriter CreateMessage(MessageType type); - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Games/Managers/IGameManager.cs b/src/Impostor.Server.Api/Games/Managers/IGameManager.cs deleted file mode 100644 index d089772..0000000 --- a/src/Impostor.Server.Api/Games/Managers/IGameManager.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Collections.Generic; - -namespace Impostor.Server.Games.Managers -{ - public interface IGameManager - { - IEnumerable Games { get; } - - IGame? Find(GameCode code); - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Impostor.Server.Api.csproj b/src/Impostor.Server.Api/Impostor.Server.Api.csproj deleted file mode 100644 index 0e72d47..0000000 --- a/src/Impostor.Server.Api/Impostor.Server.Api.csproj +++ /dev/null @@ -1,24 +0,0 @@ - - - - net5.0 - Impostor.Server - enable - None - ProjectRules.ruleset - - - - - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - \ No newline at end of file diff --git a/src/Impostor.Server.Api/Impostor.Server.Api.csproj.DotSettings b/src/Impostor.Server.Api/Impostor.Server.Api.csproj.DotSettings deleted file mode 100644 index 2df7445..0000000 --- a/src/Impostor.Server.Api/Impostor.Server.Api.csproj.DotSettings +++ /dev/null @@ -1,8 +0,0 @@ - - True - True - True - True - True - True - True \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/Extensions/GameMessageWriterExtensions.cs b/src/Impostor.Server.Api/Net/Extensions/GameMessageWriterExtensions.cs deleted file mode 100644 index d995b5a..0000000 --- a/src/Impostor.Server.Api/Net/Extensions/GameMessageWriterExtensions.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Threading.Tasks; -using Impostor.Server.Net.Messages; - -namespace Impostor.Server.Net -{ - public static class GameMessageWriterExtensions - { - public static ValueTask SendToAllExceptAsync(this IGameMessageWriter writer, LimboStates states, int? id) - { - return id.HasValue - ? writer.SendToAllExceptAsync(id.Value, states) - : writer.SendToAllAsync(states); - } - - public static ValueTask SendToAllExceptAsync(this IGameMessageWriter writer, LimboStates states, IClient client) - { - if (client == null) - { - throw new ArgumentNullException(nameof(client)); - } - - return writer.SendToAllExceptAsync(client.Id, states); - } - - public static ValueTask SendToAsync(this IGameMessageWriter writer, IClient client) - { - if (client == null) - { - throw new ArgumentNullException(nameof(client)); - } - - return writer.SendToAsync(client.Id); - } - - public static ValueTask SendToAsync(this IGameMessageWriter writer, IClientPlayer player) - { - return SendToAsync(writer, player.Client); - } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/IClient.cs b/src/Impostor.Server.Api/Net/IClient.cs deleted file mode 100644 index 0c508c4..0000000 --- a/src/Impostor.Server.Api/Net/IClient.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System.Collections.Generic; - -namespace Impostor.Server.Net -{ - /// - /// Represents a connected game client. - /// - public interface IClient - { - /// - /// Gets or sets the unique ID of the client. - /// - /// - /// This ID is generated when the client is registered in the client manager and should not be used - /// to store persisted data. - /// - int Id { get; set; } - - /// - /// Gets the name that was provided by the player in the client. - /// - /// - /// The name is provided by the player and should not be used to store persisted data. - /// - string Name { get; } - - /// - /// Gets the connection of the client. - /// - /// - /// Null when the client was not registered by the matchmaker. - /// - IConnection? Connection { get; } - - /// - /// Gets a value indicating whether the client is a bot. - /// - bool IsBot { get; } - - /// - /// Gets a key/value collection that can be used to share data between messages. - /// - /// - /// - /// The stored data will not be saved. - /// After the connection has been closed all data will be lost. - /// - /// - /// Note that the values will not be disposed after the connection has been closed. - /// This has to be implemented by the plugin. - /// - /// - IDictionary Items { get; } - - /// - /// Gets or sets the current game data of the . - /// - IClientPlayer? Player { get; } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/IClientPlayer.cs b/src/Impostor.Server.Api/Net/IClientPlayer.cs deleted file mode 100644 index 4e4252f..0000000 --- a/src/Impostor.Server.Api/Net/IClientPlayer.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System.Threading.Tasks; -using Impostor.Server.Games; - -namespace Impostor.Server.Net -{ - /// - /// Represents a player in . - /// - public interface IClientPlayer - { - /// - /// Gets the client that belongs to the player. - /// - IClient Client { get; } - - /// - /// Gets the game where the belongs to. - /// - IGame Game { get; } - - /// - /// Gets or sets the current limbo state of the player. - /// - LimboStates Limbo { get; set; } - - ValueTask KickAsync(); - - ValueTask BanAsync(); - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/IConnection.cs b/src/Impostor.Server.Api/Net/IConnection.cs deleted file mode 100644 index f619dfa..0000000 --- a/src/Impostor.Server.Api/Net/IConnection.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Net; -using Impostor.Server.Games; -using Impostor.Server.Net.Messages; - -namespace Impostor.Server.Net -{ - /// - /// Represents the connection of the client. - /// - public interface IConnection - { - /// - /// Gets the IP endpoint of the client. - /// - IPEndPoint EndPoint { get; } - - /// - /// Gets a value indicating whether the client is connected to the server. - /// - bool IsConnected { get; } - - /// - /// Gets the client of the connection. - /// - IClient? Client { get; } - - /// - /// Create a message writer that can be send to the connection. - /// - /// - /// Be aware when implementing a custom connection handler that this method is not called when a message - /// is being send in . - /// - /// Type of the message. - /// Message writer for the current connection. - IConnectionMessageWriter CreateMessage(MessageType messageType); - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/LimboStates.cs b/src/Impostor.Server.Api/Net/LimboStates.cs deleted file mode 100644 index 1812f5a..0000000 --- a/src/Impostor.Server.Api/Net/LimboStates.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace Impostor.Server.Net -{ - [Flags] - public enum LimboStates - { - PreSpawn = 1, - NotLimbo = 2, - WaitingForHost = 4, - All = PreSpawn | NotLimbo | WaitingForHost - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/Manager/IClientManager.cs b/src/Impostor.Server.Api/Net/Manager/IClientManager.cs deleted file mode 100644 index 6405a65..0000000 --- a/src/Impostor.Server.Api/Net/Manager/IClientManager.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; - -namespace Impostor.Server.Net.Manager -{ - public interface IClientManager - { - IEnumerable Clients { get; } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/Messages/IConnectionMessageWriter.cs b/src/Impostor.Server.Api/Net/Messages/IConnectionMessageWriter.cs deleted file mode 100644 index 453eb7e..0000000 --- a/src/Impostor.Server.Api/Net/Messages/IConnectionMessageWriter.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Threading.Tasks; - -namespace Impostor.Server.Net.Messages -{ - /// - /// Represents the message writer for . - /// - public interface IConnectionMessageWriter : IMessageWriter - { - /// - /// Gets the connection where the message writer belongs to. - /// - public IConnection Connection { get; } - - /// - /// Sends the message to the . - /// - /// Task. - ValueTask SendAsync(); - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/Messages/IGameMessageWriter.cs b/src/Impostor.Server.Api/Net/Messages/IGameMessageWriter.cs deleted file mode 100644 index d1d769c..0000000 --- a/src/Impostor.Server.Api/Net/Messages/IGameMessageWriter.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Threading.Tasks; -using Impostor.Server.Games; - -namespace Impostor.Server.Net.Messages -{ - /// - /// Represents the message writer for . - /// - public interface IGameMessageWriter : IMessageWriter - { - /// - /// Send the message to all players. - /// - /// Required limbo state of the player. - /// A representing the asynchronous operation. - ValueTask SendToAllAsync(LimboStates states = LimboStates.NotLimbo); - - /// - /// Send the message to all players except one. - /// - /// The player to exclude from sending the message. - /// Required limbo state of the player. - /// A representing the asynchronous operation. - ValueTask SendToAllExceptAsync(int senderId, LimboStates states = LimboStates.NotLimbo); - - /// - /// Send a message to a specific player. - /// - /// ID of the client. - /// A representing the asynchronous operation. - ValueTask SendToAsync(int id); - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/Messages/IMessage.cs b/src/Impostor.Server.Api/Net/Messages/IMessage.cs deleted file mode 100644 index 854a0d2..0000000 --- a/src/Impostor.Server.Api/Net/Messages/IMessage.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Impostor.Server.Net.Messages -{ - public interface IMessage - { - MessageType Type { get; } - - IMessageReader CreateReader(); - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/Messages/IMessageReader.cs b/src/Impostor.Server.Api/Net/Messages/IMessageReader.cs deleted file mode 100644 index aeefcc6..0000000 --- a/src/Impostor.Server.Api/Net/Messages/IMessageReader.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; - -namespace Impostor.Server.Net.Messages -{ - public interface IMessageReader - { - /// - /// Gets the tag of the message. - /// - byte Tag { get; } - - /// - /// Gets the buffer of the message. - /// - ReadOnlyMemory Buffer { get; } - - /// - /// Gets the current position of the reader. - /// - int Position { get; } - - /// - /// Gets the length of the buffer. - /// - int Length { get; } - - IMessageReader ReadMessage(); - - bool ReadBoolean(); - - sbyte ReadSByte(); - - byte ReadByte(); - - ushort ReadUInt16(); - - short ReadInt16(); - - uint ReadUInt32(); - - int ReadInt32(); - - float ReadSingle(); - - string ReadString(); - - ReadOnlyMemory ReadBytesAndSize(); - - ReadOnlyMemory ReadBytes(int length); - - int ReadPackedInt32(); - - uint ReadPackedUInt32(); - - void CopyTo(IMessageWriter writer); - - IMessageReader Slice(int start); - - IMessageReader Slice(int start, int length); - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/Messages/IMessageWriter.cs b/src/Impostor.Server.Api/Net/Messages/IMessageWriter.cs deleted file mode 100644 index add3154..0000000 --- a/src/Impostor.Server.Api/Net/Messages/IMessageWriter.cs +++ /dev/null @@ -1,113 +0,0 @@ -using System; -using System.Net; -using Impostor.Server.Games; - -namespace Impostor.Server.Net.Messages -{ - /// - /// Base message writer. - /// - public interface IMessageWriter : IDisposable - { - /// - /// Writes a boolean to the message. - /// - /// Value to write. - void Write(bool value); - - /// - /// Writes a sbyte to the message. - /// - /// Value to write. - void Write(sbyte value); - - /// - /// Writes a byte to the message. - /// - /// Value to write. - void Write(byte value); - - /// - /// Writes a short to the message. - /// - /// Value to write. - void Write(short value); - - /// - /// Writes an ushort to the message. - /// - /// Value to write. - void Write(ushort value); - - /// - /// Writes an uint to the message. - /// - /// Value to write. - void Write(uint value); - - /// - /// Writes an int to the message. - /// - /// Value to write. - void Write(int value); - - /// - /// Writes a float to the message. - /// - /// Value to write. - void Write(float value); - - /// - /// Writes a string to the message. - /// - /// Value to write. - void Write(string value); - - /// - /// Writes a to the message. - /// - /// Value to write. - void Write(IPAddress value); - - /// - /// Writes an packed int to the message. - /// - /// Value to write. - void WritePacked(int value); - - /// - /// Writes an packed uint to the message. - /// - /// Value to write. - void WritePacked(uint value); - - /// - /// Writes raw bytes to the message. - /// - /// Bytes to write. - void Write(ReadOnlyMemory data); - - /// - /// Writes a game code to the message. - /// - /// Value to write. - void Write(GameCode value); - - /// - /// Starts a new message. - /// - /// Message flag header. - void StartMessage(byte typeFlag); - - /// - /// Mark the end of the message. - /// - void EndMessage(); - - /// - /// Clear the message writer. - /// - /// New type of the message. - void Clear(MessageType type); - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Net/Messages/MessageType.cs b/src/Impostor.Server.Api/Net/Messages/MessageType.cs deleted file mode 100644 index a350cb6..0000000 --- a/src/Impostor.Server.Api/Net/Messages/MessageType.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Impostor.Server.Net.Messages -{ - /// - /// Specifies how a message should be sent between connections. - /// - public enum MessageType - { - /// - /// Requests unreliable delivery with no fragmentation. - /// - /// - /// Sending data using unreliable delivery means that data is not guaranteed to arrive at it's destination nor is - /// it guaranteed to arrive only once. However, unreliable delivery can be faster than other methods and it - /// typically requires a smaller number of protocol bytes than other methods. There is also typically less - /// processing involved and less memory needed as packets are not stored once sent. - /// - Unreliable, - - /// - /// Requests data be sent reliably but with no fragmentation. - /// - /// - /// Sending data reliably means that data is guaranteed to arrive and to arrive only once. Reliable delivery - /// typically requires more processing, more memory (as packets need to be stored in case they need resending), - /// a larger number of protocol bytes and can be slower than unreliable delivery. - /// - Reliable, - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Plugins/IPlugin.cs b/src/Impostor.Server.Api/Plugins/IPlugin.cs deleted file mode 100644 index e5f8645..0000000 --- a/src/Impostor.Server.Api/Plugins/IPlugin.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Threading.Tasks; -using Impostor.Server.Events; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace Impostor.Server.Plugins -{ - public interface IPlugin : IEventListener - { - ValueTask EnableAsync(); - - ValueTask DisableAsync(); - - ValueTask ReloadAsync(); - - void ConfigureHost(IHostBuilder host); - - void ConfigureServices(IServiceCollection services); - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/Plugins/PluginBase.cs b/src/Impostor.Server.Api/Plugins/PluginBase.cs deleted file mode 100644 index 5ef6cf3..0000000 --- a/src/Impostor.Server.Api/Plugins/PluginBase.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace Impostor.Server.Plugins -{ - public class PluginBase : IPlugin - { - public virtual ValueTask EnableAsync() - { - return default; - } - - public virtual ValueTask DisableAsync() - { - return default; - } - - public virtual ValueTask ReloadAsync() - { - return default; - } - - public virtual void ConfigureHost(IHostBuilder host) - { - } - - public virtual void ConfigureServices(IServiceCollection services) - { - } - } -} \ No newline at end of file diff --git a/src/Impostor.Server.Api/ProjectRules.ruleset b/src/Impostor.Server.Api/ProjectRules.ruleset deleted file mode 100644 index a21a8d4..0000000 --- a/src/Impostor.Server.Api/ProjectRules.ruleset +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/Impostor.Server/Impostor.Server.csproj b/src/Impostor.Server/Impostor.Server.csproj index a7612c8..e045ac0 100644 --- a/src/Impostor.Server/Impostor.Server.csproj +++ b/src/Impostor.Server/Impostor.Server.csproj @@ -21,8 +21,8 @@ + - diff --git a/src/Impostor.sln b/src/Impostor.sln index 72a1536..41694cb 100644 --- a/src/Impostor.sln +++ b/src/Impostor.sln @@ -15,7 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "server", "server", "{F2B205 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Impostor.Server", "Impostor.Server\Impostor.Server.csproj", "{1B0390AF-A4F3-4FE4-B093-708B0135C0B3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Impostor.Server.Api", "Impostor.Server.Api\Impostor.Server.Api.csproj", "{E096A7D7-D693-4A13-A526-38CC574D84F8}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Impostor.Api", "Impostor.Api\Impostor.Api.csproj", "{E096A7D7-D693-4A13-A526-38CC574D84F8}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "patcher", "patcher", "{94FAED42-15BB-40CF-BE64-5D5C3B4ABC8F}" EndProject @@ -126,7 +126,6 @@ Global {D16B8DE9-8EE2-4F6A-9352-305D5AB0233D} = {56DD9707-D811-4056-9E2C-8A9CC2479B07} {804CF172-0C87-4423-9688-BD97D549891E} = {94FAED42-15BB-40CF-BE64-5D5C3B4ABC8F} {1B0390AF-A4F3-4FE4-B093-708B0135C0B3} = {F2B205ED-4250-412E-9992-B11B7D6CE136} - {E096A7D7-D693-4A13-A526-38CC574D84F8} = {F2B205ED-4250-412E-9992-B11B7D6CE136} {7C3EB599-2292-4532-B280-D5BED1094DD4} = {94FAED42-15BB-40CF-BE64-5D5C3B4ABC8F} {82B36C4C-4EBD-49B7-A2DB-0B3308FBEF69} = {94FAED42-15BB-40CF-BE64-5D5C3B4ABC8F} {ECBCAA3B-B974-41CF-AFFC-6F5AA4C42FA7} = {36AA9913-E6EA-4A6C-90E6-2FD3CC2E3124}