|
| 1 | +// Copyright (c) 2025-2026 POWDER-RANGER. All Rights Reserved. |
| 2 | + |
| 3 | +#include "Core/N1NetcodeManager.h" |
| 4 | +#include "Core/N1NetworkClock.h" |
| 5 | +#include "Core/N1PredictionBuffer.h" |
| 6 | +#include "Pipeline/N1ReconciliationEngine.h" |
| 7 | +#include "Pipeline/N1ClientPrediction.h" |
| 8 | +#include "Pipeline/N1ServerAuthority.h" |
| 9 | +#include "Pipeline/N1RollbackEngine.h" |
| 10 | +#include "Pipeline/N1BlendInterpolator.h" |
| 11 | +#include "UE6/N1UE6Compatibility.h" |
| 12 | +#include "Engine/World.h" |
| 13 | + |
| 14 | +// Static singleton storage |
| 15 | +static TWeakObjectPtr<UN1NetcodeManager> GNetcodeManager; |
| 16 | + |
| 17 | +UN1NetcodeManager::UN1NetcodeManager() |
| 18 | +{ |
| 19 | + CurrentMode = EN1NetcodeMode::Standalone; |
| 20 | + CurrentPhase = EN1NetcodePhase::Initializing; |
| 21 | +} |
| 22 | + |
| 23 | +void UN1NetcodeManager::Initialize(const FN1NetcodeConfig& InConfig, EN1NetcodeMode InMode) |
| 24 | +{ |
| 25 | + Config = InConfig; |
| 26 | + CurrentMode = InMode; |
| 27 | + |
| 28 | + UE_LOG(LogN1Netcode, Log, TEXT("Initializing N1 Netcode Manager [Mode: %s]"), |
| 29 | + *UEnum::GetValueAsString(InMode)); |
| 30 | + |
| 31 | + // Create subsystems |
| 32 | + NetworkClock = NewObject<UN1NetworkClock>(this); |
| 33 | + NetworkClock->Initialize(InMode == EN1NetcodeMode::Server || InMode == EN1NetcodeMode::ListenServer); |
| 34 | + |
| 35 | + if (InMode == EN1NetcodeMode::Client || InMode == EN1NetcodeMode::ListenServer) |
| 36 | + { |
| 37 | + auto* Buffer = NewObject<UN1PredictionBuffer>(this); |
| 38 | + int32 BufferFrames = FMath::CeilToInt((Config.MaxAcceptableLatencyMs / 1000.0f) * Config.ClientTickRate) + Config.MaxRollbackFrames; |
| 39 | + Buffer->Initialize(BufferFrames, Config.ClientTickRate); |
| 40 | + |
| 41 | + PredictionSystem = NewObject<UN1ClientPrediction>(this); |
| 42 | + PredictionSystem->Initialize(Buffer, NetworkClock, Config.ClientTickRate); |
| 43 | + |
| 44 | + RollbackEngine = NewObject<UN1RollbackEngine>(this); |
| 45 | + RollbackEngine->Initialize(Config.MaxRollbackFrames); |
| 46 | + |
| 47 | + BlendInterpolator = NewObject<UN1BlendInterpolator>(this); |
| 48 | + BlendInterpolator->Initialize(Config.BlendFrames, EN1BlendCurve::SmoothStep); |
| 49 | + |
| 50 | + ReconciliationEngine = NewObject<UN1ReconciliationEngine>(this); |
| 51 | + ReconciliationEngine->Initialize(Config.RollbackThreshold, Config.MaxRollbackFrames); |
| 52 | + } |
| 53 | + |
| 54 | + if (InMode == EN1NetcodeMode::Server || InMode == EN1NetcodeMode::ListenServer) |
| 55 | + { |
| 56 | + ServerAuthority = NewObject<UN1ServerAuthority>(this); |
| 57 | + ServerAuthority->Initialize(Config.ServerTickRate, Config.InputBufferMs); |
| 58 | + } |
| 59 | + |
| 60 | + // UE6 compatibility check |
| 61 | + auto* UE6Compat = NewObject<UN1UE6Compatibility>(this); |
| 62 | + UE6Compat->Initialize(); |
| 63 | + UE6Compat->LogCompatibilityStatus(); |
| 64 | + |
| 65 | + bInitialized = true; |
| 66 | + SetPhase(EN1NetcodePhase::Active); |
| 67 | + |
| 68 | + UE_LOG(LogN1Netcode, Log, TEXT("N1 Netcode Manager initialized successfully")); |
| 69 | +} |
| 70 | + |
| 71 | +void UN1NetcodeManager::Shutdown() |
| 72 | +{ |
| 73 | + SetPhase(EN1NetcodePhase::ShuttingDown); |
| 74 | + |
| 75 | + PredictionSystem = nullptr; |
| 76 | + ReconciliationEngine = nullptr; |
| 77 | + RollbackEngine = nullptr; |
| 78 | + BlendInterpolator = nullptr; |
| 79 | + ServerAuthority = nullptr; |
| 80 | + NetworkClock = nullptr; |
| 81 | + |
| 82 | + bInitialized = false; |
| 83 | + GNetcodeManager.Reset(); |
| 84 | + |
| 85 | + UE_LOG(LogN1Netcode, Log, TEXT("N1 Netcode Manager shutdown complete")); |
| 86 | +} |
| 87 | + |
| 88 | +void UN1NetcodeManager::Tick(float DeltaTime) |
| 89 | +{ |
| 90 | + if (!bInitialized) return; |
| 91 | + |
| 92 | + // Update network clock |
| 93 | + if (NetworkClock) |
| 94 | + { |
| 95 | + NetworkClock->Tick(DeltaTime); |
| 96 | + } |
| 97 | + |
| 98 | + // Tick client systems |
| 99 | + if (PredictionSystem) |
| 100 | + { |
| 101 | + PredictionSystem->Tick(DeltaTime); |
| 102 | + } |
| 103 | + |
| 104 | + if (BlendInterpolator) |
| 105 | + { |
| 106 | + BlendInterpolator->Tick(DeltaTime); |
| 107 | + } |
| 108 | + |
| 109 | + // Server tick |
| 110 | + if (ServerAuthority) |
| 111 | + { |
| 112 | + ServerAuthority->TickServer(DeltaTime); |
| 113 | + } |
| 114 | + |
| 115 | + // Periodic forced reconciliation |
| 116 | + if (Config.ForcedReconciliationIntervalSec > 0) |
| 117 | + { |
| 118 | + PerformForcedReconciliation(DeltaTime); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +UN1NetcodeManager* UN1NetcodeManager::GetN1Manager(UWorld* World) |
| 123 | +{ |
| 124 | + if (GNetcodeManager.IsValid()) |
| 125 | + { |
| 126 | + return GNetcodeManager.Get(); |
| 127 | + } |
| 128 | + |
| 129 | + if (World) |
| 130 | + { |
| 131 | + UN1NetcodeManager* Manager = NewObject<UN1NetcodeManager>(World); |
| 132 | + GNetcodeManager = Manager; |
| 133 | + return Manager; |
| 134 | + } |
| 135 | + |
| 136 | + return nullptr; |
| 137 | +} |
| 138 | + |
| 139 | +void UN1NetcodeManager::SetPhase(EN1NetcodePhase NewPhase) |
| 140 | +{ |
| 141 | + if (CurrentPhase != NewPhase) |
| 142 | + { |
| 143 | + EN1NetcodePhase OldPhase = CurrentPhase; |
| 144 | + CurrentPhase = NewPhase; |
| 145 | + OnPhaseChanged.Broadcast(NewPhase); |
| 146 | + UE_LOG(LogN1Netcode, Verbose, TEXT("Phase transition: %s -> %s"), |
| 147 | + *UEnum::GetValueAsString(OldPhase), |
| 148 | + *UEnum::GetValueAsString(NewPhase)); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +void UN1NetcodeManager::PerformForcedReconciliation(float DeltaTime) |
| 153 | +{ |
| 154 | + ForcedReconciliationTimer += DeltaTime; |
| 155 | + if (ForcedReconciliationTimer >= Config.ForcedReconciliationIntervalSec) |
| 156 | + { |
| 157 | + ForcedReconciliationTimer = 0.0f; |
| 158 | + UE_LOG(LogN1Netcode, Verbose, TEXT("Performing forced periodic reconciliation")); |
| 159 | + // This would trigger a full-state resync in production |
| 160 | + } |
| 161 | +} |
0 commit comments