preload weapon models

This commit is contained in:
GenZmeY 2022-07-08 06:03:30 +03:00
parent 559eae0298
commit e5662242db
3 changed files with 178 additions and 86 deletions

View File

@ -67,7 +67,7 @@ private function PreInit()
{ {
LogLevel = LL_Info; LogLevel = LL_Info;
bPreloadContent = true; bPreloadContent = true;
bForcePreloadContent = true; bForcePreloadContent = false;
UnlockDLC = false; UnlockDLC = false;
SaveConfig(); SaveConfig();
} }

View File

@ -14,10 +14,18 @@ var private bool ForcePreloadContent;
var private int Recieved; var private int Recieved;
var private int SyncSize; var private int SyncSize;
var private int Preloaded;
var private KFPlayerController KFPC;
var private KFPawn KFP;
var private KFInventoryManager KFIM;
var private KFGFxWidget_PartyInGame PartyInGameWidget; var private KFGFxWidget_PartyInGame PartyInGameWidget;
var private GFxObject Notification; var private GFxObject Notification;
var private class<Weapon> PreloadWeaponClass;
var private float PreloadWeaponTime;
replication replication
{ {
if (bNetInitial && Role == ROLE_Authority) if (bNetInitial && Role == ROLE_Authority)
@ -40,7 +48,9 @@ public function PrepareSync(
bool _PreloadContent, bool _PreloadContent,
bool _ForcePreloadContent) bool _ForcePreloadContent)
{ {
CTI = _CTI; `Log_Trace(`Location);
CTI = _CTI;
LogLevel = _LogLevel; LogLevel = _LogLevel;
RemoveItems = _RemoveItems; RemoveItems = _RemoveItems;
AddItems = _AddItems; AddItems = _AddItems;
@ -50,28 +60,68 @@ public function PrepareSync(
SyncSize = RemoveItems.Length + AddItems.Length; SyncSize = RemoveItems.Length + AddItems.Length;
} }
private simulated function PlayerController GetPlayerController() private simulated function KFPlayerController GetKFPC()
{ {
local PlayerController PC; `Log_Trace(`Location);
PC = PlayerController(Owner); if (KFPC != None) return KFPC;
if (PC == None && ROLE < ROLE_Authority) KFPC = KFPlayerController(Owner);
if (KFPC == None && ROLE < ROLE_Authority)
{ {
PC = GetALocalPlayerController(); KFPC = KFPlayerController(GetALocalPlayerController());
} }
return PC; return KFPC;
}
private simulated function KFPawn GetKFP()
{
local Pawn P;
`Log_Trace(`Location);
if (KFP != None) return KFP;
if (GetKFPC() != None)
{
P = GetKFPC().Pawn;
if (P != None)
{
KFP = KFPawn(P);
}
}
return KFP;
}
private simulated function KFInventoryManager GetKFIM()
{
local InventoryManager IM;
`Log_Trace(`Location);
if (KFIM != None) return KFIM;
if (GetKFP() != None)
{
IM = GetKFP().InvManager;
if (IM != None)
{
KFIM = KFInventoryManager(IM);
}
}
return KFIM;
} }
private simulated function SetPartyInGameWidget() private simulated function SetPartyInGameWidget()
{ {
local KFPlayerController KFPC;
`Log_Trace(`Location); `Log_Trace(`Location);
KFPC = KFPlayerController(GetPlayerController()); if (GetKFPC() == None) return;
if (KFPC == None) return;
if (KFPC.MyGFxManager == None) return; if (KFPC.MyGFxManager == None) return;
if (KFPC.MyGFxManager.PartyWidget == None) return; if (KFPC.MyGFxManager.PartyWidget == None) return;
@ -81,6 +131,8 @@ private simulated function SetPartyInGameWidget()
private simulated function bool CheckPartyInGameWidget() private simulated function bool CheckPartyInGameWidget()
{ {
`Log_Trace(`Location);
if (PartyInGameWidget == None) if (PartyInGameWidget == None)
{ {
SetPartyInGameWidget(); SetPartyInGameWidget();
@ -89,8 +141,10 @@ private simulated function bool CheckPartyInGameWidget()
return (PartyInGameWidget != None); return (PartyInGameWidget != None);
} }
private simulated function HideReadyButton() private unreliable client function HideReadyButton()
{ {
`Log_Trace(`Location);
if (CheckPartyInGameWidget()) if (CheckPartyInGameWidget())
{ {
PartyInGameWidget.SetReadyButtonVisibility(false); PartyInGameWidget.SetReadyButtonVisibility(false);
@ -99,6 +153,8 @@ private simulated function HideReadyButton()
private simulated function ShowReadyButton() private simulated function ShowReadyButton()
{ {
`Log_Trace(`Location);
if (CheckPartyInGameWidget()) if (CheckPartyInGameWidget())
{ {
Notification.SetVisible(false); Notification.SetVisible(false);
@ -108,8 +164,10 @@ private simulated function ShowReadyButton()
} }
} }
private reliable client function UpdateNotification(String Title, String Downloading, String Remainig, int Percent) private unreliable client function UpdateNotification(String Title, String Downloading, String Remainig, int Percent)
{ {
`Log_Trace(`Location);
if (CheckPartyInGameWidget() && Notification != None) if (CheckPartyInGameWidget() && Notification != None)
{ {
Notification.SetString("itemName", Title); Notification.SetString("itemName", Title);
@ -151,7 +209,7 @@ private reliable client function ClientSync(class<KFWeaponDefinition> WeapDef, o
Recieved = RemoveItems.Length + AddItems.Length; Recieved = RemoveItems.Length + AddItems.Length;
UpdateNotification( UpdateNotification(
"Sync items, please wait...", "Sync trader items, please wait...",
Remove ? "-" : "+" @ Repl(String(WeapDef), "KFWeapDef_", ""), Remove ? "-" : "+" @ Repl(String(WeapDef), "KFWeapDef_", ""),
Recieved @ "/" @ SyncSize, Recieved @ "/" @ SyncSize,
(float(Recieved) / float(SyncSize)) * 100); (float(Recieved) / float(SyncSize)) * 100);
@ -159,17 +217,13 @@ private reliable client function ClientSync(class<KFWeaponDefinition> WeapDef, o
ServerSync(); ServerSync();
} }
private simulated reliable client function SyncFinished() private simulated reliable client function ClientSyncFinished()
{ {
local KFGameReplicationInfo KFGRI; local KFGameReplicationInfo KFGRI;
`Log_Trace(`Location); `Log_Trace(`Location);
if (WorldInfo == None || WorldInfo.GRI == None) ClearTimer(nameof(WaitForPreloadWeapon));
{
SetTimer(1.0f, false, nameof(SyncFinished));
return;
}
KFGRI = KFGameReplicationInfo(WorldInfo.GRI); KFGRI = KFGameReplicationInfo(WorldInfo.GRI);
if (KFGRI == None) if (KFGRI == None)
@ -198,14 +252,11 @@ public reliable server function ServerSync()
{ {
if (ForcePreloadContent) if (ForcePreloadContent)
{ {
PreloadContentWorkaround(); ServerPreloadWeaponWorkaround();
} }
else
SyncFinished();
if (!CTI.DestroyRepLink(Controller(Owner)))
{ {
SafeDestroy(); ServerSyncFinished();
} }
} }
else else
@ -221,92 +272,132 @@ public reliable server function ServerSync()
} }
} }
private function PreloadContentWorkaround() private function ServerSyncFinished()
{ {
local PlayerController PC; `Log_Trace(`Location);
local Pawn P;
local KFInventoryManager KFIM; ClientSyncFinished();
local class<Weapon> CW;
local Weapon W; if (!CTI.DestroyRepLink(Controller(Owner)))
local int Index; {
local DroppedPickup DP; SafeDestroy();
local float Time; }
}
private reliable server function ServerPreloadWeaponWorkaround()
{
local class<Weapon> WC;
`Log_Trace(`Location); `Log_Trace(`Location);
HideReadyButton();
PC = GetPlayerController();
if (PC == None) RemovePreloadWeapon();
{
SetTimer(0.1f, false, nameof(PreloadContentWorkaround));
return;
}
P = PC.Pawn;
if (P == None)
{
SetTimer(0.1f, false, nameof(PreloadContentWorkaround));
return;
}
KFIM = KFInventoryManager(P.InvManager);
if (KFIM == None)
{
SetTimer(0.1f, false, nameof(PreloadContentWorkaround));
return;
}
KFIM.bInfiniteWeight = true; if (AddItems.Length <= Preloaded)
Time = WorldInfo.TimeSeconds - 1.0f;
for (Index = 0; Index < AddItems.Length; Index++)
{ {
HideReadyButton(); ServerSyncFinished();
UpdateNotification( }
"Game isn't frozen, Don't panic", else
"Preload content:", {
Index @ "/" @ AddItems.Length, WC = class<Weapon> (DynamicLoadObject(AddItems[Preloaded++].default.WeaponClassPath, class'Class'));
(float(Index) / float(AddItems.Length)) * 100); if (WC != None)
CW = class<Weapon> (DynamicLoadObject(AddItems[Index].default.WeaponClassPath, class'Class'));
if (CW != None && Weapon(P.FindInventoryType(CW)) == None)
{ {
P.CreateInventory(CW); PreloadWeaponTime = WorldInfo.TimeSeconds - 1.0f;
PreloadWeaponClass = WC;
ClientPreloadWeapon(WC);
if (!AddPreloadWeapon(WC))
{
ServerPreloadWeaponWorkaround();
}
} }
} }
}
private reliable server function bool AddPreloadWeapon(class<Weapon> WC)
{
local Weapon W;
`Log_Trace(`Location);
if (GetKFIM() == None || WC == None) return false;
KFIM.bInfiniteWeight = true;
W = Weapon(KFP.FindInventoryType(WC));
if (W == None)
{
W = Weapon(KFP.CreateInventory(WC));
}
if (W != None)
{
KFIM.SetCurrentWeapon(W);
}
KFIM.bInfiniteWeight = false;
if (W == None) `Log_Warn("Can't preload" @ WC @ "for some reason (skip)");
return (W != None);
}
private function RemovePreloadWeapon()
{
local DroppedPickup DP;
local Weapon W;
`Log_Trace(`Location);
if (GetKFIM() == None || PreloadWeaponClass == None) return;
HideReadyButton();
UpdateNotification("Cleanup", "", "", 0);
foreach KFIM.InventoryActors(class'Weapon', W) foreach KFIM.InventoryActors(class'Weapon', W)
{ {
if (W != None) if (W != None && W.class == PreloadWeaponClass)
{ {
KFIM.PendingWeapon = W;
KFIM.ChangedWeapon();
if (W.CanThrow()) if (W.CanThrow())
{ {
P.TossInventory(W); KFP.TossInventory(W);
W.Destroy(); W.Destroy();
} }
} }
} }
HideReadyButton();
UpdateNotification("Cleanup", "", "", 0);
foreach WorldInfo.DynamicActors(class'DroppedPickup', DP) foreach WorldInfo.DynamicActors(class'DroppedPickup', DP)
{ {
if (DP.Instigator == P && DP.CreationTime > Time) if (DP.Instigator == KFP && DP.CreationTime > PreloadWeaponTime)
{ {
DP.Destroy(); DP.Destroy();
} }
} }
}
private reliable client function ClientPreloadWeapon(class<Weapon> WC)
{
`Log_Trace(`Location);
KFIM.bInfiniteWeight = false; Preloaded++;
PreloadWeaponClass = WC;
SetTimer(0.5f, false, nameof(WaitForPreloadWeapon));
}
private simulated function WaitForPreloadWeapon()
{
`Log_Trace(`Location);
`Log_Info("Force Preload Finished (" $ PC.PlayerReplicationInfo.PlayerName $ ")"); HideReadyButton();
UpdateNotification(
"Preload weapon models, please wait...",
Repl(String(PreloadWeaponClass), "KFWeap_", ""),
Preloaded @ "/" @ AddItems.Length,
(float(Preloaded) / float(AddItems.Length)) * 100);
if (GetKFIM() != None
&& KFIM.Instigator.Weapon != None
&& KFIM.Instigator.Weapon.Class == PreloadWeaponClass)
{
ServerPreloadWeaponWorkaround();
}
else
{
SetTimer(0.5f, false, nameof(WaitForPreloadWeapon));
}
} }
defaultproperties defaultproperties
@ -317,4 +408,5 @@ defaultproperties
PendingSync = false PendingSync = false
Recieved = 0 Recieved = 0
Preloaded = 0
} }

View File

@ -70,7 +70,7 @@ public static simulated function PreloadWeapon(class<KFWeaponDefinition> WeapDef
if (KFW != None) if (KFW != None)
{ {
// This doesn't seem to have any effect right now, // This doesn't seem to have any effect right now,
// so we're forced to use a workaround: CTI_RepInfo.PreloadContentWorkaround() // so we're forced to use a workaround: CTI_RepInfo.ServerPreloadWeaponWorkaround()
// But I still leave it for the future // But I still leave it for the future
// in the hope that someday we can preload weapon models using this function // in the hope that someday we can preload weapon models using this function
class'KFWeapon'.static.TriggerAsyncContentLoad(KFW); class'KFWeapon'.static.TriggerAsyncContentLoad(KFW);