speed up item sync

This commit is contained in:
GenZmeY 2023-10-08 20:20:17 +03:00
parent 9ab02e29a1
commit 2b45b7ae56
Signed by: GenZmeY
GPG Key ID: 424DA4BC3CB2CF39
6 changed files with 192 additions and 131 deletions

View File

@ -14,6 +14,7 @@ var private config bool bOfficialWeaponsList;
var private KFGameInfo KFGI; var private KFGameInfo KFGI;
var private KFGameReplicationInfo KFGRI; var private KFGameReplicationInfo KFGRI;
var private Array<class<KFWeaponDefinition> > WeapDefs;
var private Array<class<KFWeaponDefinition> > RemoveItems; var private Array<class<KFWeaponDefinition> > RemoveItems;
var private Array<LTI_RepInfo> RepInfos; var private Array<LTI_RepInfo> RepInfos;
var private bool ReadyToSync; var private bool ReadyToSync;
@ -149,13 +150,15 @@ private function PostInit()
CfgRemoveItems.default.bDLC, CfgRemoveItems.default.bDLC,
LogLevel); LogLevel);
WeapDefs = Trader.static.GetTraderWeapDefs(KFGRI, LogLevel);
ReadyToSync = true; ReadyToSync = true;
foreach RepInfos(RepInfo) foreach RepInfos(RepInfo)
{ {
if (RepInfo.PendingSync) if (RepInfo.PendingSync)
{ {
RepInfo.ServerSync(); RepInfo.Replicate(WeapDefs);
} }
} }
} }
@ -189,19 +192,13 @@ public function bool CreateRepInfo(Controller C)
if (RepInfo == None) return false; if (RepInfo == None) return false;
RepInfo.PrepareSync( RepInfo.PrepareSync(Self, LogLevel);
Self,
LogLevel,
RemoveItems,
CfgRemoveItems.default.bAll,
CfgRemoveItems.default.bHRG,
CfgRemoveItems.default.bDLC);
RepInfos.AddItem(RepInfo); RepInfos.AddItem(RepInfo);
if (ReadyToSync) if (ReadyToSync)
{ {
RepInfo.ServerSync(); RepInfo.Replicate(WeapDefs);
} }
else else
{ {

View File

@ -16,13 +16,17 @@ var private localized String IncompatibleGRIWarning;
var const String SecondsShortDefault; var const String SecondsShortDefault;
var private localized String SecondsShort; var private localized String SecondsShort;
var const String PleaseWaitDefault;
var private localized String PleaseWait;
enum E_LTI_LocalMessageType enum E_LTI_LocalMessageType
{ {
LTI_SyncItems, LTI_SyncItems,
LTI_WaitingGRI, LTI_WaitingGRI,
LTI_IncompatibleGRI, LTI_IncompatibleGRI,
LTI_IncompatibleGRIWarning, LTI_IncompatibleGRIWarning,
LTI_SecondsShort LTI_SecondsShort,
LTI_PleaseWait
}; };
public static function String GetLocalizedString( public static function String GetLocalizedString(
@ -50,6 +54,9 @@ public static function String GetLocalizedString(
case LTI_SecondsShort: case LTI_SecondsShort:
return (default.SecondsShort != "" ? default.SecondsShort : default.SecondsShortDefault); return (default.SecondsShort != "" ? default.SecondsShort : default.SecondsShortDefault);
case LTI_PleaseWait:
return (default.PleaseWait != "" ? default.PleaseWait : default.PleaseWaitDefault);
} }
return ""; return "";
@ -62,4 +69,5 @@ defaultproperties
IncompatibleGRIDefault = "Incompatible GRI:" IncompatibleGRIDefault = "Incompatible GRI:"
IncompatibleGRIWarningDefault = "You can enter the game, but the trader may not work correctly."; IncompatibleGRIWarningDefault = "You can enter the game, but the trader may not work correctly.";
SecondsShortDefault = "s" SecondsShortDefault = "s"
PleaseWaitDefault = "Please wait"
} }

View File

@ -1,19 +1,23 @@
class LTI_RepInfo extends ReplicationInfo; class LTI_RepInfo extends ReplicationInfo;
const CAPACITY = 64; // max: 128
const Trader = class'Trader'; const Trader = class'Trader';
const LocalMessage = class'LTI_LocalMessage'; const LocalMessage = class'LTI_LocalMessage';
struct ReplicationStruct
{
var int Size;
var int Transfered;
var class<KFWeaponDefinition> Items[CAPACITY];
var int Length;
};
var public bool PendingSync; var public bool PendingSync;
var private LTI LTI; var private LTI LTI;
var private E_LogLevel LogLevel; var private E_LogLevel LogLevel;
var private Array<class<KFWeaponDefinition> > RemoveItems;
var private bool ReplaceMode;
var private bool RemoveHRG;
var private bool RemoveDLC;
var private int Recieved;
var private int SyncSize;
var private KFPlayerController KFPC; var private KFPlayerController KFPC;
var private KFGFxWidget_PartyInGame PartyInGameWidget; var private KFGFxWidget_PartyInGame PartyInGameWidget;
@ -27,10 +31,13 @@ var private int NotificationPercent;
var private int WaitingGRI; var private int WaitingGRI;
var private int WaitingGRILimit; var private int WaitingGRILimit;
var private ReplicationStruct RepData;
var private Array<class<KFWeaponDefinition> > RepArray;
replication replication
{ {
if (bNetInitial && Role == ROLE_Authority) if (bNetInitial && Role == ROLE_Authority)
LogLevel, ReplaceMode, RemoveHRG, RemoveDLC, SyncSize; LogLevel;
} }
public simulated function bool SafeDestroy() public simulated function bool SafeDestroy()
@ -40,23 +47,113 @@ public simulated function bool SafeDestroy()
return (bPendingDelete || bDeleteMe || Destroy()); return (bPendingDelete || bDeleteMe || Destroy());
} }
public function PrepareSync( public function PrepareSync(LTI _LTI, E_LogLevel _LogLevel)
LTI _LTI,
E_LogLevel _LogLevel,
Array<class<KFWeaponDefinition> > _RemoveItems,
bool _ReplaceMode,
bool _RemoveHRG,
bool _RemoveDLC)
{ {
`Log_Trace(); `Log_Trace();
LTI = _LTI; LTI = _LTI;
LogLevel = _LogLevel; LogLevel = _LogLevel;
RemoveItems = _RemoveItems; }
ReplaceMode = _ReplaceMode;
RemoveHRG = _RemoveHRG; public function Replicate(const out Array<class<KFWeaponDefinition> > WeapDefs)
RemoveDLC = _RemoveDLC; {
SyncSize = RemoveItems.Length; `Log_Trace();
RepArray = WeapDefs;
RepData.Size = RepArray.Length;
if (WorldInfo.NetMode == NM_StandAlone)
{
Progress(RepArray.Length, RepArray.Length);
return;
}
Sync();
}
private reliable server function Sync()
{
local int LocalIndex;
local int GlobalIndex;
`Log_Trace();
LocalIndex = 0;
GlobalIndex = RepData.Transfered;
while (LocalIndex < CAPACITY && GlobalIndex < RepData.Size)
{
RepData.Items[LocalIndex++] = RepArray[GlobalIndex++];
}
if (RepData.Transfered == GlobalIndex) return; // Finished
RepData.Transfered = GlobalIndex;
RepData.Length = LocalIndex;
Send(RepData);
Progress(RepData.Transfered, RepData.Size);
}
private reliable client function Send(ReplicationStruct RD)
{
local int LocalIndex;
`Log_Trace();
for (LocalIndex = 0; LocalIndex < RD.Length; LocalIndex++)
{
RepArray.AddItem(RD.Items[LocalIndex]);
}
Progress(RD.Transfered, RD.Size);
Sync();
}
private simulated function Progress(int Value, int Size)
{
`Log_Trace();
`Log_Debug("Replicated:" @ Value @ "/" @ Size);
if (ROLE < ROLE_Authority)
{
NotifyProgress(Value, Size);
if (Value >= Size) Finished();
}
}
private simulated function Finished()
{
local KFGameReplicationInfo KFGRI;
`Log_Trace();
if (WorldInfo.GRI == None && WaitingGRI++ < WaitingGRILimit)
{
`Log_Debug("Finished: Waiting GRI" @ WaitingGRI);
NotifyWaitingGRI();
SetTimer(1.0f, false, nameof(Finished));
return;
}
KFGRI = KFGameReplicationInfo(WorldInfo.GRI);
if (KFGRI != None)
{
`Log_Debug("Finished: Trader.static.OverwriteTraderItems");
Trader.static.OverwriteTraderItems(KFGRI, RepArray, LogLevel);
`Log_Info("Trader items successfully synchronized!");
}
else
{
`Log_Error("Incompatible Replication info:" @ String(WorldInfo.GRI));
NotifyIncompatibleGRI();
}
ShowReadyButton();
ClientCleanup();
} }
private simulated function KFPlayerController GetKFPC() private simulated function KFPlayerController GetKFPC()
@ -147,6 +244,8 @@ private simulated function ShowReadyButton()
{ {
`Log_Trace(); `Log_Trace();
ClearTimer(nameof(KeepNotification));
if (CheckPartyInGameWidget()) if (CheckPartyInGameWidget())
{ {
Notification.SetVisible(false); Notification.SetVisible(false);
@ -172,41 +271,6 @@ private simulated function UpdateNotification(String Title, String Left, String
} }
} }
private reliable client function ClientSync(class<KFWeaponDefinition> WeapDef)
{
`Log_Trace();
if (WeapDef == None)
{
`Log_Fatal("WeapDef is:" @ WeapDef);
Cleanup();
ConsoleCommand("Disconnect");
SafeDestroy();
return;
}
if (!IsTimerActive(nameof(KeepNotification)))
{
SetTimer(0.1f, true, nameof(KeepNotification));
}
RemoveItems.AddItem(WeapDef);
Recieved = RemoveItems.Length;
NotificationHeaderText = "-" @ WeapDef.static.GetItemName();
NotificationLeftText = LocalMessage.static.GetLocalizedString(LogLevel, LTI_SyncItems);
NotificationRightText = Recieved @ "/" @ SyncSize;
if (SyncSize != 0)
{
NotificationPercent = (float(Recieved) / float(SyncSize)) * 100;
}
`Log_Debug("ClientSync: -" @ String(WeapDef) @ NotificationRightText);
ServerSync();
}
private simulated function KeepNotification() private simulated function KeepNotification()
{ {
HideReadyButton(); HideReadyButton();
@ -217,86 +281,61 @@ private simulated function KeepNotification()
NotificationPercent); NotificationPercent);
} }
private simulated reliable client function ClientSyncFinished() private simulated function ClientCleanup()
{ {
local KFGameReplicationInfo KFGRI; ServerCleanup();
`Log_Trace();
if (WorldInfo.GRI == None && WaitingGRI++ < WaitingGRILimit)
{
`Log_Debug("ClientSyncFinished: Waiting GRI" @ WaitingGRI);
NotificationHeaderText = LocalMessage.static.GetLocalizedString(LogLevel, LTI_WaitingGRI);
NotificationLeftText = String(WaitingGRI) $ LocalMessage.static.GetLocalizedString(LogLevel, LTI_SecondsShort);
NotificationRightText = "";
NotificationPercent = 0;
SetTimer(1.0f, false, nameof(ClientSyncFinished));
return;
}
NotificationHeaderText = "";
NotificationLeftText = "";
NotificationRightText = "";
NotificationPercent = 0;
KFGRI = KFGameReplicationInfo(WorldInfo.GRI);
if (KFGRI != None)
{
`Log_Debug("ClientSyncFinished: Trader.static.ModifyTrader");
Trader.static.ModifyTrader(KFGRI, RemoveItems, ReplaceMode, RemoveHRG, RemoveDLC, LogLevel);
}
else
{
`Log_Error("Incompatible Replication info:" @ String(WorldInfo.GRI));
WriteToChatLocalized(
LTI_IncompatibleGRI,
class'KFLocalMessage'.default.InteractionColor,
WorldInfo.GRI == None ? "None" : String(WorldInfo.GRI.class));
WriteToChatLocalized(
LTI_IncompatibleGRIWarning,
class'KFLocalMessage'.default.InteractionColor);
}
ClearTimer(nameof(KeepNotification));
ShowReadyButton();
Cleanup();
SafeDestroy(); SafeDestroy();
} }
private reliable server function Cleanup() private reliable server function ServerCleanup()
{ {
`Log_Trace(); `Log_Trace();
`Log_Debug("Cleanup"); `Log_Debug("Cleanup");
if (!LTI.DestroyRepInfo(Controller(Owner))) if (!LTI.DestroyRepInfo(GetKFPC()))
{ {
`Log_Debug("Cleanup (forced)"); `Log_Debug("Cleanup (forced)");
SafeDestroy(); SafeDestroy();
} }
} }
public reliable server function ServerSync() private simulated function NotifyWaitingGRI()
{ {
`Log_Trace(); if (!IsTimerActive(nameof(KeepNotification)))
PendingSync = false;
if (bPendingDelete || bDeleteMe) return;
if (SyncSize <= Recieved || WorldInfo.NetMode == NM_StandAlone)
{ {
`Log_Debug("ServerSync: Finished"); SetTimer(0.1f, true, nameof(KeepNotification));
ClientSyncFinished();
} }
else
NotificationHeaderText = LocalMessage.static.GetLocalizedString(LogLevel, LTI_WaitingGRI);
NotificationLeftText = String(WaitingGRI) $ LocalMessage.static.GetLocalizedString(LogLevel, LTI_SecondsShort);
NotificationRightText = LocalMessage.static.GetLocalizedString(LogLevel, LTI_PleaseWait);
NotificationPercent = 0;
KeepNotification();
}
private simulated function NotifyProgress(int Value, int Size)
{
if (!IsTimerActive(nameof(KeepNotification)))
{ {
if (Recieved < RemoveItems.Length) SetTimer(0.1f, true, nameof(KeepNotification));
{
`Log_Debug("ServerSync[-]:" @ (Recieved + 1) @ "/" @ SyncSize @ RemoveItems[Recieved]);
ClientSync(RemoveItems[Recieved++]);
}
} }
NotificationHeaderText = LocalMessage.static.GetLocalizedString(LogLevel, LTI_SyncItems);
NotificationLeftText = Value @ "/" @ Size;
NotificationRightText = LocalMessage.static.GetLocalizedString(LogLevel, LTI_PleaseWait);
NotificationPercent = (float(Value) / float(Size)) * 100;
KeepNotification();
}
private simulated function NotifyIncompatibleGRI()
{
WriteToChatLocalized(
LTI_IncompatibleGRI,
class'KFLocalMessage'.default.InteractionColor,
String(WorldInfo.GRI.class));
WriteToChatLocalized(
LTI_IncompatibleGRIWarning,
class'KFLocalMessage'.default.InteractionColor);
} }
defaultproperties defaultproperties
@ -306,9 +345,8 @@ defaultproperties
bSkipActorPropertyReplication = false bSkipActorPropertyReplication = false
PendingSync = false PendingSync = false
Recieved = 0
NotificationPercent = 0 NotificationPercent = 0
WaitingGRI = 0 WaitingGRI = 0
WaitingGRILimit = 15 WaitingGRILimit = 30
} }

View File

@ -75,9 +75,7 @@ public static simulated function ModifyTrader(
{ {
local KFGFxObject_TraderItems TraderItems; local KFGFxObject_TraderItems TraderItems;
local STraderItem Item; local STraderItem Item;
local class<KFWeaponDefinition> WeapDef;
local Array<class<KFWeaponDefinition> > WeapDefs; local Array<class<KFWeaponDefinition> > WeapDefs;
local int MaxItemID;
`Log_TraceStatic(); `Log_TraceStatic();
@ -99,13 +97,33 @@ public static simulated function ModifyTrader(
WeapDefs.Sort(ByPrice); WeapDefs.Sort(ByPrice);
OverwriteTraderItems(KFGRI, WeapDefs, LogLevel);
}
public static simulated function OverwriteTraderItems(
KFGameReplicationInfo KFGRI,
const out Array<class<KFWeaponDefinition> > WeapDefs,
E_LogLevel LogLevel)
{
local KFGFxObject_TraderItems TraderItems;
local STraderItem Item;
local class<KFWeaponDefinition> WeapDef;
local int MaxItemID;
`Log_TraceStatic();
TraderItems = GetTraderItems(KFGRI, LogLevel);
TraderItems.SaleItems.Length = 0; TraderItems.SaleItems.Length = 0;
MaxItemID = 0; MaxItemID = 0;
`Log_Debug("Trader Items:");
foreach WeapDefs(WeapDef) foreach WeapDefs(WeapDef)
{ {
Item.WeaponDef = WeapDef; Item.WeaponDef = WeapDef;
Item.ItemID = ++MaxItemID; Item.ItemID = MaxItemID++;
TraderItems.SaleItems.AddItem(Item); TraderItems.SaleItems.AddItem(Item);
`Log_Debug("[" $ MaxItemID $ "]" @ String(WeapDef));
} }
TraderItems.SetItemsInfo(TraderItems.SaleItems); TraderItems.SetItemsInfo(TraderItems.SaleItems);

Binary file not shown.

Binary file not shown.