mirror of
https://github.com/MrOkiDoki/BattleBit-Community-Server-API.git
synced 2025-01-09 11:17:30 -03:00
Merge branch 'MrOkiDoki:main' into main
This commit is contained in:
commit
b02c568603
6 changed files with 113 additions and 61 deletions
27
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
27
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
|
@ -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.
|
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
|
@ -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.
|
|
@ -731,11 +731,9 @@ namespace BattleBitAPI.Server
|
|||
}
|
||||
|
||||
// ---- Static ----
|
||||
public static TGameServer CreateInstance<TGameServer>(Internal @internal) where TGameServer : GameServer<TPlayer>
|
||||
public static void SetInstance(GameServer<TPlayer> server, Internal @internal)
|
||||
{
|
||||
TGameServer gameServer = (TGameServer)Activator.CreateInstance(typeof(TGameServer));
|
||||
gameServer.mInternal = @internal;
|
||||
return gameServer;
|
||||
server.mInternal = @internal;
|
||||
}
|
||||
|
||||
// ---- Internal ----
|
||||
|
|
|
@ -235,11 +235,9 @@ namespace BattleBitAPI
|
|||
}
|
||||
|
||||
// ---- Static ----
|
||||
public static TPlayer CreateInstance<TPlayer>(Player<TPlayer>.Internal @internal) where TPlayer : Player<TPlayer>
|
||||
public static void SetInstance(TPlayer player, Player<TPlayer>.Internal @internal)
|
||||
{
|
||||
TPlayer player = (TPlayer)Activator.CreateInstance(typeof(TPlayer));
|
||||
player.mInternal = @internal;
|
||||
return player;
|
||||
}
|
||||
|
||||
// ---- Overrides ----
|
||||
|
|
|
@ -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
|
|||
/// <remarks>
|
||||
/// GameServer: Game server that has been just created.<br/>
|
||||
/// </remarks>
|
||||
public Func<GameServer<TPlayer>, Task> OnCreatingGameServerInstance { get; set; }
|
||||
public Func<TGameServer> OnCreatingGameServerInstance { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Fired when a new instance of player instance created.
|
||||
|
@ -89,7 +90,7 @@ namespace BattleBitAPI.Server
|
|||
/// <remarks>
|
||||
/// TPlayer: The player instance that was created<br/>
|
||||
/// </remarks>
|
||||
public Func<TPlayer, Task> OnCreatingPlayerInstance { get; set; }
|
||||
public Func<TPlayer> 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<ulong, (TPlayer, Player<TPlayer>.Internal)>(1024 * 16);
|
||||
}
|
||||
|
||||
public TGameServer GetServerInstance(ulong hash, out bool isNew, out GameServer<TPlayer>.Internal @internal)
|
||||
public TGameServer GetServerInstance(ulong hash, out GameServer<TPlayer>.Internal @internal, Func<TGameServer> createFunc)
|
||||
{
|
||||
lock (mGameServerInstances)
|
||||
{
|
||||
if (mGameServerInstances.TryGetValue(hash, out var data))
|
||||
{
|
||||
@internal = data.Item2;
|
||||
isNew = false;
|
||||
return data.Item1;
|
||||
}
|
||||
|
||||
@internal = new GameServer<TPlayer>.Internal();
|
||||
TGameServer gameServer = GameServer<TPlayer>.CreateInstance<TGameServer>(@internal);
|
||||
GameServer<TPlayer> server;
|
||||
|
||||
isNew = true;
|
||||
mGameServerInstances.Add(hash, (gameServer, @internal));
|
||||
return gameServer;
|
||||
if (createFunc != null)
|
||||
server = createFunc();
|
||||
else
|
||||
server = Activator.CreateInstance<GameServer<TPlayer>>();
|
||||
|
||||
GameServer<TPlayer>.SetInstance(server, @internal);
|
||||
|
||||
mGameServerInstances.Add(hash, ((TGameServer)server, @internal));
|
||||
return (TGameServer)server;
|
||||
}
|
||||
}
|
||||
public TPlayer GetPlayerInstance(ulong steamID, out bool isNew, out Player<TPlayer>.Internal @internal)
|
||||
public TPlayer GetPlayerInstance(ulong steamID, out Player<TPlayer>.Internal @internal, Func<TPlayer> createFunc)
|
||||
{
|
||||
lock (this.mPlayerInstances)
|
||||
{
|
||||
if (this.mPlayerInstances.TryGetValue(steamID, out var player))
|
||||
{
|
||||
isNew = false;
|
||||
@internal = player.Item2;
|
||||
return player.Item1;
|
||||
}
|
||||
|
||||
@internal = new Player<TPlayer>.Internal();
|
||||
var pplayer = Player<TPlayer>.CreateInstance(@internal);
|
||||
|
||||
isNew = true;
|
||||
mPlayerInstances.Add(steamID, (pplayer, @internal));
|
||||
return pplayer;
|
||||
Player<TPlayer> pplayer;
|
||||
|
||||
if (createFunc != null)
|
||||
pplayer = createFunc();
|
||||
else
|
||||
pplayer = Activator.CreateInstance<TPlayer>();
|
||||
Player<TPlayer>.SetInstance((TPlayer)pplayer, @internal);
|
||||
|
||||
mPlayerInstances.Add(steamID, ((TPlayer)pplayer, @internal));
|
||||
return (TPlayer)pplayer;
|
||||
}
|
||||
}
|
||||
public Player<TPlayer>.Internal GetPlayerInternals(ulong steamID)
|
||||
|
|
51
Program.cs
51
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<MyPlayer, MyGameServer>();
|
||||
listener.OnGameServerConnecting += OnGameServerConnecting;
|
||||
listener.OnValidateGameServerToken += OnValidateGameServerToken;
|
||||
listener.OnCreatingGameServerInstance += OnCreatingGameServerInstance;
|
||||
listener.OnCreatingPlayerInstance += OnCreatingPlayerInstance;
|
||||
listener.Start(29294);
|
||||
|
||||
Thread.Sleep(-1);
|
||||
}
|
||||
|
||||
private static async Task<bool> 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<bool> OnGameServerConnecting(IPAddress arg)
|
||||
private static MyGameServer OnCreatingGameServerInstance()
|
||||
{
|
||||
await Console.Out.WriteLineAsync(arg.ToString() + " connecting");
|
||||
return true;
|
||||
return new MyGameServer("mysecretDBpass");
|
||||
}
|
||||
|
||||
}
|
||||
class MyPlayer : Player<MyPlayer>
|
||||
{
|
||||
private string mydb;
|
||||
public MyPlayer(string mydb)
|
||||
{
|
||||
this.mydb = mydb;
|
||||
}
|
||||
|
||||
public override async Task OnSpawned()
|
||||
{
|
||||
}
|
||||
}
|
||||
class MyGameServer : GameServer<MyPlayer>
|
||||
{
|
||||
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<OnPlayerSpawnArguments> 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)
|
||||
|
|
Loading…
Reference in a new issue