diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..fead26f --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,27 @@ +--- +name: Bug report +about: Create a report to help us improve +title: "[Bug]" +labels: bug +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Screenshots** +If applicable, add screenshots to help explain your problem. + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..11eb268 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: "[Feature Request]" +labels: enhancement +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/BattleBitAPI/Server/GameServer.cs b/BattleBitAPI/Server/GameServer.cs index 6b92ed7..904d689 100644 --- a/BattleBitAPI/Server/GameServer.cs +++ b/BattleBitAPI/Server/GameServer.cs @@ -731,11 +731,9 @@ namespace BattleBitAPI.Server } // ---- Static ---- - public static TGameServer CreateInstance(Internal @internal) where TGameServer : GameServer + public static void SetInstance(GameServer server, Internal @internal) { - TGameServer gameServer = (TGameServer)Activator.CreateInstance(typeof(TGameServer)); - gameServer.mInternal = @internal; - return gameServer; + server.mInternal = @internal; } // ---- Internal ---- diff --git a/BattleBitAPI/Server/Player.cs b/BattleBitAPI/Server/Player.cs index d16c0b7..dabe927 100644 --- a/BattleBitAPI/Server/Player.cs +++ b/BattleBitAPI/Server/Player.cs @@ -235,11 +235,9 @@ namespace BattleBitAPI } // ---- Static ---- - public static TPlayer CreateInstance(Player.Internal @internal) where TPlayer : Player + public static void SetInstance(TPlayer player, Player.Internal @internal) { - TPlayer player = (TPlayer)Activator.CreateInstance(typeof(TPlayer)); player.mInternal = @internal; - return player; } // ---- Overrides ---- diff --git a/BattleBitAPI/Server/ServerListener.cs b/BattleBitAPI/Server/ServerListener.cs index 4177d6c..2c1773b 100644 --- a/BattleBitAPI/Server/ServerListener.cs +++ b/BattleBitAPI/Server/ServerListener.cs @@ -1,6 +1,7 @@ using System.Net; using System.Net.Sockets; using System.Numerics; +using System.Runtime.CompilerServices; using BattleBitAPI.Common; using BattleBitAPI.Common.Extentions; using BattleBitAPI.Networking; @@ -80,7 +81,7 @@ namespace BattleBitAPI.Server /// /// GameServer: Game server that has been just created.
///
- public Func, Task> OnCreatingGameServerInstance { get; set; } + public Func OnCreatingGameServerInstance { get; set; } /// /// Fired when a new instance of player instance created. @@ -89,7 +90,7 @@ namespace BattleBitAPI.Server /// /// TPlayer: The player instance that was created
///
- public Func OnCreatingPlayerInstance { get; set; } + public Func OnCreatingPlayerInstance { get; set; } // --- Private --- private TcpListener mSocket; @@ -381,7 +382,7 @@ namespace BattleBitAPI.Server } var hash = ((ulong)gamePort << 32) | (ulong)ip.ToUInt(); - server = this.mInstanceDatabase.GetServerInstance(hash, out bool isNew, out resources); + server = this.mInstanceDatabase.GetServerInstance(hash, out resources, this.OnCreatingGameServerInstance); resources.Set( this.mExecutePackage, this.mGetPlayerInternals, @@ -555,7 +556,8 @@ namespace BattleBitAPI.Server wearings.Read(readStream); } - TPlayer player = mInstanceDatabase.GetPlayerInstance(steamid, out bool isNewClient, out var playerInternal); + + TPlayer player = mInstanceDatabase.GetPlayerInstance(steamid, out var playerInternal, this.OnCreatingPlayerInstance); playerInternal.SteamID = steamid; playerInternal.Name = username; playerInternal.IP = new IPAddress(ipHash); @@ -580,25 +582,11 @@ namespace BattleBitAPI.Server playerInternal._Modifications.Read(readStream); } - //Call new instance callback if needed. - if (isNewClient) - { - if (this.OnCreatingPlayerInstance != null) - this.OnCreatingPlayerInstance(player); - } - resources.AddPlayer(player); } //Send accepted notification. networkStream.WriteByte((byte)NetworkCommuncation.Accepted); - - if (isNew) - { - if (this.OnCreatingGameServerInstance != null) - this.OnCreatingGameServerInstance(server); - } - } } } @@ -716,7 +704,7 @@ namespace BattleBitAPI.Server Squads squad = (Squads)stream.ReadInt8(); GameRole role = (GameRole)stream.ReadInt8(); - TPlayer player = mInstanceDatabase.GetPlayerInstance(steamID, out bool isNewClient, out var playerInternal); + TPlayer player = mInstanceDatabase.GetPlayerInstance(steamID, out var playerInternal, this.OnCreatingPlayerInstance); playerInternal.SteamID = steamID; playerInternal.Name = username; playerInternal.IP = new IPAddress(ip); @@ -729,12 +717,6 @@ namespace BattleBitAPI.Server //Start from default. playerInternal._Modifications.Reset(); - if (isNewClient) - { - if (this.OnCreatingPlayerInstance != null) - this.OnCreatingPlayerInstance(player); - } - resources.AddPlayer(player); player.OnConnected(); server.OnPlayerConnected(player); @@ -1299,42 +1281,52 @@ namespace BattleBitAPI.Server this.mPlayerInstances = new Dictionary.Internal)>(1024 * 16); } - public TGameServer GetServerInstance(ulong hash, out bool isNew, out GameServer.Internal @internal) + public TGameServer GetServerInstance(ulong hash, out GameServer.Internal @internal, Func createFunc) { lock (mGameServerInstances) { if (mGameServerInstances.TryGetValue(hash, out var data)) { @internal = data.Item2; - isNew = false; return data.Item1; } @internal = new GameServer.Internal(); - TGameServer gameServer = GameServer.CreateInstance(@internal); + GameServer server; - isNew = true; - mGameServerInstances.Add(hash, (gameServer, @internal)); - return gameServer; + if (createFunc != null) + server = createFunc(); + else + server = Activator.CreateInstance>(); + + GameServer.SetInstance(server, @internal); + + mGameServerInstances.Add(hash, ((TGameServer)server, @internal)); + return (TGameServer)server; } } - public TPlayer GetPlayerInstance(ulong steamID, out bool isNew, out Player.Internal @internal) + public TPlayer GetPlayerInstance(ulong steamID, out Player.Internal @internal, Func createFunc) { lock (this.mPlayerInstances) { if (this.mPlayerInstances.TryGetValue(steamID, out var player)) { - isNew = false; @internal = player.Item2; return player.Item1; } @internal = new Player.Internal(); - var pplayer = Player.CreateInstance(@internal); - isNew = true; - mPlayerInstances.Add(steamID, (pplayer, @internal)); - return pplayer; + Player pplayer; + + if (createFunc != null) + pplayer = createFunc(); + else + pplayer = Activator.CreateInstance(); + Player.SetInstance((TPlayer)pplayer, @internal); + + mPlayerInstances.Add(steamID, ((TPlayer)pplayer, @internal)); + return (TPlayer)pplayer; } } public Player.Internal GetPlayerInternals(ulong steamID) diff --git a/Program.cs b/Program.cs index 9c041c0..bb6f8b5 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,7 @@ using BattleBitAPI; using BattleBitAPI.Common; using BattleBitAPI.Server; +using System.Linq.Expressions; using System.Net; using System.Numerics; using System.Threading.Channels; @@ -11,55 +12,71 @@ class Program static void Main(string[] args) { var listener = new ServerListener(); - listener.OnGameServerConnecting += OnGameServerConnecting; - listener.OnValidateGameServerToken += OnValidateGameServerToken; + listener.OnCreatingGameServerInstance += OnCreatingGameServerInstance; + listener.OnCreatingPlayerInstance += OnCreatingPlayerInstance; listener.Start(29294); Thread.Sleep(-1); } - private static async Task OnValidateGameServerToken(IPAddress ip, ushort gameport, string sentToken) + private static MyPlayer OnCreatingPlayerInstance() { - await Console.Out.WriteLineAsync(ip + ":" + gameport + " sent " + sentToken); - return true; + return new MyPlayer("asdasd"); } - private static async Task OnGameServerConnecting(IPAddress arg) + private static MyGameServer OnCreatingGameServerInstance() { - await Console.Out.WriteLineAsync(arg.ToString() + " connecting"); - return true; + return new MyGameServer("mysecretDBpass"); } - } class MyPlayer : Player { + private string mydb; + public MyPlayer(string mydb) + { + this.mydb = mydb; + } + public override async Task OnSpawned() { } } class MyGameServer : GameServer { + private string myDbConnection; + public MyGameServer(string mySecretDBConnection) + { + this.myDbConnection = mySecretDBConnection; + } + public override async Task OnConnected() { ForceStartGame(); ServerSettings.PlayerCollision = true; } - public override async Task OnDisconnected() - { - await Console.Out.WriteLineAsync("Disconnected: "+ this.TerminationReason); - } - public override async Task OnTick() { - base.ServerSettings.PlayerCollision = true; - foreach (var item in AllPlayers) - item.Modifications.CanSuicide = true; } + public override async Task OnPlayerSpawning(MyPlayer player, OnPlayerSpawnArguments request) + { + request.Wearings.Eye = "Eye_Zombie_01"; + request.Wearings.Face = "Face_Zombie_01"; + request.Wearings.Face = "Hair_Zombie_01"; + request.Wearings.Skin = "Zombie_01"; + request.Wearings.Uniform = "ANY_NU_Uniform_Zombie_01"; + request.Wearings.Head = "ANV2_Universal_Zombie_Helmet_00_A_Z"; + request.Wearings.Belt = "ANV2_Universal_All_Belt_Null"; + request.Wearings.Backbag = "ANV2_Universal_All_Backpack_Null"; + request.Wearings.Chest = "ANV2_Universal_All_Armor_Null"; + return request; + } public override async Task OnPlayerConnected(MyPlayer player) { await Console.Out.WriteLineAsync("Connected: " + player); + player.Modifications.CanSpectate = true; + player.Modifications.CanDeploy = false; } public override async Task OnPlayerSpawned(MyPlayer player)