working on multiplayer

This commit is contained in:
2025-06-24 14:45:45 +02:00
parent b1050f627b
commit 5a813f212c
67 changed files with 499 additions and 127 deletions

View File

@@ -4,19 +4,18 @@ using System.Threading;
using Cysharp.Threading.Tasks;
using R3;
using RebootKit.Engine.Foundation;
using RebootKit.Engine.Multiplayer;
using RebootKit.Engine.Services.Console;
using RebootKit.Engine.Services.GameMode;
using RebootKit.Engine.Services.Input;
using RebootKit.Engine.Services.Simulation;
using RebootKit.Engine.Steam;
using Unity.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using Assert = UnityEngine.Assertions.Assert;
using Logger = RebootKit.Engine.Foundation.Logger;
// RR
// Game
// GameMode
namespace RebootKit.Engine.Main {
public interface IGame : IDisposable {
UniTask InitAsync(CancellationToken cancellationToken);
@@ -33,56 +32,60 @@ namespace RebootKit.Engine.Main {
[ConfigVar("con.write_log", 1, "Enables writing game log to console output")]
static ConfigVar s_writeLogToConsole;
static EngineConfigAsset s_engineConfigAsset;
internal static EngineConfigAsset EngineConfig;
static DisposableBag s_disposableBag;
static DisposableBag s_servicesBag;
static ConsoleService s_consoleService;
static GameModesService s_gameModesService;
static InputService s_inputService;
static WorldService s_worldService;
public static ConsoleService Console => s_consoleService;
public static InputService Input => s_inputService;
public static WorldService World => s_worldService;
public static GameModesService GameModes => s_gameModesService;
public static ConsoleService Console { get; private set; }
public static InputService Input { get; private set; }
public static WorldService World { get; private set; }
public static GameModesService GameModes { get; private set; }
public static Camera MainCamera { get; internal set; }
static IGame s_game;
public static async UniTask InitAsync(EngineConfigAsset configAsset, CancellationToken cancellationToken) {
// Core
internal static async UniTask InitAsync(EngineConfigAsset configAsset, CancellationToken cancellationToken) {
Assert.IsNotNull(configAsset, "Config asset is required");
Assert.IsNotNull(configAsset.gameAsset, "Game asset is required");
s_engineConfigAsset = configAsset;
EngineConfig = configAsset;
s_Logger.Info("Initializing");
s_servicesBag = new DisposableBag();
s_disposableBag = new DisposableBag();
s_Logger.Debug("Registering core services");
s_consoleService = CreateService(s_engineConfigAsset.coreServices.consoleService);
s_inputService = CreateService(s_engineConfigAsset.coreServices.inputService);
s_worldService = CreateService(s_engineConfigAsset.coreServices.worldService);
s_gameModesService = CreateService<GameModesService>();
Console = CreateService(EngineConfig.coreServices.consoleService);
Input = CreateService(EngineConfig.coreServices.inputService);
World = CreateService(EngineConfig.coreServices.worldService);
GameModes = CreateService<GameModesService>();
await InitializeAssetsAsync(cancellationToken);
await SteamManager.InitializeAsync(cancellationToken);
if (SteamManager.IsInitialized) {
s_networkTransport = SteamManager.NetworkTransport;
}
s_Logger.Debug("Creating game");
s_game = s_engineConfigAsset.gameAsset.CreateGame();
s_game = EngineConfig.gameAsset.CreateGame();
await s_game.InitAsync(cancellationToken);
}
public static void Shutdown() {
internal static void Shutdown() {
SteamManager.Shutdown();
s_Logger.Info("Shutting down");
s_servicesBag.Dispose();
s_disposableBag.Dispose();
}
public static void Run() {
internal static void Run() {
s_game.Run();
#if UNITY_EDITOR
@@ -145,12 +148,13 @@ namespace RebootKit.Engine.Main {
// Game API
public static void StartGameMode(GameModeAsset gameMode, WorldConfig world) {
if (gameMode is null) {
throw new ArgumentNullException(nameof(gameMode));
if (!IsClient() || !IsHost()) {
s_Logger.Error("Cannot start game mode: you must be connected to a server and be the host");
return;
}
s_Logger.Info($"Starting game mode: {gameMode.name} in world: {world.name}");
s_gameModesService.Start(gameMode, world);
GameModes.Start(gameMode, world);
}
public static TGame Game<TGame>() where TGame : IGame {
@@ -182,17 +186,17 @@ namespace RebootKit.Engine.Main {
public static void Log(string message) {
Debug.Log(message);
s_consoleService?.WriteToOutput(message);
Console?.WriteToOutput(message);
}
public static void LogWarning(string message) {
Debug.LogWarning(message);
s_consoleService?.WriteToOutput(message);
Console?.WriteToOutput(message);
}
public static void LogError(string message) {
Debug.LogError(message);
s_consoleService?.WriteToOutput(message);
Console?.WriteToOutput(message);
}
// CVar API
@@ -228,5 +232,48 @@ namespace RebootKit.Engine.Main {
ConfigVarsContainer.Register(cvar);
return cvar;
}
// Network API
static GameLobby s_gameLobby;
static INetworkTransport s_networkTransport;
public static bool IsHost() {
return s_networkTransport.IsServer();
}
public static bool IsClient() {
return s_networkTransport.IsClient();
}
public static int GetPing() {
return -1;
}
public static void HostServer(bool offline = false) {
s_networkTransport.StartServer();
}
public static void ConnectToLobby() {
s_networkTransport.Connect(Steamworks.SteamNetworkingSockets.Identity.SteamId);
}
public static void Disconnect() {
s_networkTransport.Disconnect();
}
internal static void OnConnected(GameLobby lobby) {
}
internal static void OnDisconnected() {
}
internal static void OnServerDataReceived(byte[] data) {
s_Logger.Debug($"[SERVER] Data received: {data.Length} bytes");
}
internal static void OnClientDataReceived(byte[] data) {
s_Logger.Debug($"[CLIENT] Data received: {data.Length} bytes");
}
}
}