speed up item synchronization and add trader items limit

This commit is contained in:
GenZmeY 2023-09-19 22:31:08 +03:00
parent 37921be744
commit 97294a4117
Signed by: GenZmeY
GPG Key ID: 424DA4BC3CB2CF39
6 changed files with 227 additions and 169 deletions

View File

@ -1,7 +1,7 @@
class CTI extends Info class CTI extends Info
config(CTI); config(CTI);
const LatestVersion = 3; const LatestVersion = 4;
const CfgRemoveItems = class'RemoveItems'; const CfgRemoveItems = class'RemoveItems';
const CfgAddItems = class'AddItems'; const CfgAddItems = class'AddItems';
@ -22,10 +22,12 @@ var private config E_LogLevel LogLevel;
var private config String UnlockDLC; var private config String UnlockDLC;
var private config bool bPreloadContent; var private config bool bPreloadContent;
var private config bool bOfficialWeaponsList; var private config bool bOfficialWeaponsList;
var private config bool bDisableItemLimitCheck;
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<class<KFWeaponDefinition> > AddItems; var private Array<class<KFWeaponDefinition> > AddItems;
@ -33,8 +35,6 @@ var private Array<CTI_RepInfo> RepInfos;
var private bool ReadyToSync; var private bool ReadyToSync;
var private Array<S_PreloadContent> PreloadContent;
public simulated function bool SafeDestroy() public simulated function bool SafeDestroy()
{ {
`Log_Trace(); `Log_Trace();
@ -95,6 +95,8 @@ private function PreInit()
bOfficialWeaponsList = false; bOfficialWeaponsList = false;
case 2: case 2:
case 3:
bDisableItemLimitCheck = false;
case MaxInt: case MaxInt:
`Log_Info("Config updated to version" @ LatestVersion); `Log_Info("Config updated to version" @ LatestVersion);
@ -129,7 +131,7 @@ private function PreInit()
if (!Unlocker.static.IsValidTypeUnlockDLC(UnlockDLC, LogLevel)) if (!Unlocker.static.IsValidTypeUnlockDLC(UnlockDLC, LogLevel))
{ {
`Log_Warn("Wrong 'UnlockDLC' (" $ UnlockDLC $ "), return to default value (False)"); `Log_Warn("Wrong 'UnlockDLC' value (" $ UnlockDLC $ "), return to default value (False)");
UnlockDLC = "False"; UnlockDLC = "False";
SaveConfig(); SaveConfig();
} }
@ -189,34 +191,35 @@ private function PostInit()
CfgRemoveItems.default.bAll, CfgRemoveItems.default.bAll,
CfgRemoveItems.default.bHRG, CfgRemoveItems.default.bHRG,
CfgRemoveItems.default.bDLC, CfgRemoveItems.default.bDLC,
bDisableItemLimitCheck,
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);
} }
} }
} }
private function Preload(Array<class<KFWeaponDefinition> > Content) private function Preload(const out Array<class<KFWeaponDefinition> > Content)
{ {
local Array<class<KFWeapon> > OfficialWeapons; local Array<S_PreloadContent> PreloadContent;
local S_PreloadContent SPC; local S_PreloadContent SPC;
`Log_Trace(); `Log_Trace();
OfficialWeapons = Trader.static.GetTraderWeapons();
foreach Content(SPC.KFWD) foreach Content(SPC.KFWD)
{ {
SPC.KFWC = class<KFWeapon> (DynamicLoadObject(SPC.KFWD.default.WeaponClassPath, class'Class')); SPC.KFWC = class<KFWeapon> (DynamicLoadObject(SPC.KFWD.default.WeaponClassPath, class'Class'));
if (SPC.KFWC != None) if (SPC.KFWC != None)
{ {
if (OfficialWeapons.Find(SPC.KFWC) != INDEX_NONE) if (SPC.KFWC.GetPackageName() == 'CTI' || SPC.KFWC.GetPackageName() == 'KFGameContent')
{ {
`Log_Debug("Skip preload:" @ SPC.KFWD.GetPackageName() $ "." $ SPC.KFWD); `Log_Debug("Skip preload:" @ SPC.KFWD.GetPackageName() $ "." $ SPC.KFWD);
continue; continue;
@ -271,26 +274,19 @@ public function bool CreateRepInfo(Controller C)
`Log_Trace(); `Log_Trace();
if (C == None) return false; if (C == None || KFPlayerController(C) == None) return false;
RepInfo = Spawn(class'CTI_RepInfo', C); RepInfo = Spawn(class'CTI_RepInfo', C);
if (RepInfo == None) return false; if (RepInfo == None) return false;
RepInfo.PrepareSync( RepInfo.PrepareSync(Self, KFPlayerController(C), LogLevel);
Self,
LogLevel,
RemoveItems,
AddItems,
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_CTI_LocalMessageType enum E_CTI_LocalMessageType
{ {
CTI_SyncItems, CTI_SyncItems,
CTI_WaitingGRI, CTI_WaitingGRI,
CTI_IncompatibleGRI, CTI_IncompatibleGRI,
CTI_IncompatibleGRIWarning, CTI_IncompatibleGRIWarning,
CTI_SecondsShort CTI_SecondsShort,
CTI_PleaseWait
}; };
public static function String GetLocalizedString( public static function String GetLocalizedString(
@ -50,6 +54,9 @@ public static function String GetLocalizedString(
case CTI_SecondsShort: case CTI_SecondsShort:
return (default.SecondsShort != "" ? default.SecondsShort : default.SecondsShortDefault); return (default.SecondsShort != "" ? default.SecondsShort : default.SecondsShortDefault);
case CTI_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,21 +1,23 @@
class CTI_RepInfo extends ReplicationInfo; class CTI_RepInfo extends ReplicationInfo;
const CAPACITY = 64; // max: 128
const Trader = class'Trader'; const Trader = class'Trader';
const LocalMessage = class'CTI_LocalMessage'; const LocalMessage = class'CTI_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 CTI CTI; var private CTI CTI;
var private E_LogLevel LogLevel; var private E_LogLevel LogLevel;
var private Array<class<KFWeaponDefinition> > RemoveItems;
var private Array<class<KFWeaponDefinition> > AddItems;
var private bool ReplaceMode;
var private bool RemoveHRG;
var private bool RemoveDLC;
var private bool PreloadContent;
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;
@ -29,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()
@ -42,25 +47,116 @@ public simulated function bool SafeDestroy()
return (bPendingDelete || bDeleteMe || Destroy()); return (bPendingDelete || bDeleteMe || Destroy());
} }
public function PrepareSync( public function Replicate(const out Array<class<KFWeaponDefinition> > WeapDefs)
CTI _CTI, {
E_LogLevel _LogLevel, `Log_Trace();
Array<class<KFWeaponDefinition> > _RemoveItems,
Array<class<KFWeaponDefinition> > _AddItems, RepArray = WeapDefs;
bool _ReplaceMode, RepData.Size = RepArray.Length;
bool _RemoveHRG,
bool _RemoveDLC) 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();
}
public function PrepareSync(CTI _CTI, KFPlayerController _KFPC, E_LogLevel _LogLevel)
{ {
`Log_Trace(); `Log_Trace();
CTI = _CTI; CTI = _CTI;
KFPC = _KFPC;
LogLevel = _LogLevel; LogLevel = _LogLevel;
RemoveItems = _RemoveItems; }
AddItems = _AddItems;
ReplaceMode = _ReplaceMode; private simulated function Progress(int Value, int Size)
RemoveHRG = _RemoveHRG; {
RemoveDLC = _RemoveDLC; `Log_Trace();
SyncSize = RemoveItems.Length + AddItems.Length;
`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();
Cleanup();
SafeDestroy();
} }
private simulated function KFPlayerController GetKFPC() private simulated function KFPlayerController GetKFPC()
@ -151,6 +247,8 @@ private simulated function ShowReadyButton()
{ {
`Log_Trace(); `Log_Trace();
ClearTimer(nameof(KeepNotification));
if (CheckPartyInGameWidget()) if (CheckPartyInGameWidget())
{ {
Notification.SetVisible(false); Notification.SetVisible(false);
@ -176,48 +274,6 @@ private simulated function UpdateNotification(String Title, String Left, String
} }
} }
private reliable client function ClientSync(class<KFWeaponDefinition> WeapDef, optional bool Remove = false)
{
`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));
}
if (Remove)
{
RemoveItems.AddItem(WeapDef);
}
else
{
AddItems.AddItem(WeapDef);
}
Recieved = RemoveItems.Length + AddItems.Length;
NotificationHeaderText = (Remove ? "-" : "+") @ WeapDef.static.GetItemName();
NotificationLeftText = LocalMessage.static.GetLocalizedString(LogLevel, CTI_SyncItems);
NotificationRightText = Recieved @ "/" @ SyncSize;
if (SyncSize != 0)
{
NotificationPercent = (float(Recieved) / float(SyncSize)) * 100;
}
`Log_Debug("ClientSync:" @ (Remove ? "-" : "+") @ String(WeapDef) @ NotificationRightText);
ServerSync();
}
private simulated function KeepNotification() private simulated function KeepNotification()
{ {
HideReadyButton(); HideReadyButton();
@ -228,91 +284,55 @@ private simulated function KeepNotification()
NotificationPercent); NotificationPercent);
} }
private simulated reliable client function ClientSyncFinished()
{
local KFGameReplicationInfo KFGRI;
`Log_Trace();
if (WorldInfo.GRI == None && WaitingGRI++ < WaitingGRILimit)
{
`Log_Debug("ClientSyncFinished: Waiting GRI" @ WaitingGRI);
NotificationHeaderText = LocalMessage.static.GetLocalizedString(LogLevel, CTI_WaitingGRI);
NotificationLeftText = String(WaitingGRI) $ LocalMessage.static.GetLocalizedString(LogLevel, CTI_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, AddItems, ReplaceMode, RemoveHRG, RemoveDLC, LogLevel);
}
else
{
`Log_Error("Incompatible Replication info:" @ String(WorldInfo.GRI));
WriteToChatLocalized(
CTI_IncompatibleGRI,
class'KFLocalMessage'.default.InteractionColor,
WorldInfo.GRI == None ? "None" : String(WorldInfo.GRI.class));
WriteToChatLocalized(
CTI_IncompatibleGRIWarning,
class'KFLocalMessage'.default.InteractionColor);
}
ClearTimer(nameof(KeepNotification));
ShowReadyButton();
Cleanup();
SafeDestroy();
}
private reliable server function Cleanup() private reliable server function Cleanup()
{ {
`Log_Trace(); `Log_Trace();
`Log_Debug("Cleanup"); `Log_Debug("Cleanup");
if (!CTI.DestroyRepInfo(Controller(Owner))) if (!CTI.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)))
{
SetTimer(0.1f, true, nameof(KeepNotification));
}
PendingSync = false; NotificationHeaderText = LocalMessage.static.GetLocalizedString(LogLevel, CTI_WaitingGRI);
NotificationLeftText = String(WaitingGRI) $ LocalMessage.static.GetLocalizedString(LogLevel, CTI_SecondsShort);
NotificationRightText = LocalMessage.static.GetLocalizedString(LogLevel, CTI_PleaseWait);
NotificationPercent = 0;
KeepNotification();
}
if (bPendingDelete || bDeleteMe) return; private simulated function NotifyProgress(int Value, int Size)
{
if (!IsTimerActive(nameof(KeepNotification)))
{
SetTimer(0.1f, true, nameof(KeepNotification));
}
if (SyncSize <= Recieved || WorldInfo.NetMode == NM_StandAlone) NotificationHeaderText = LocalMessage.static.GetLocalizedString(LogLevel, CTI_SyncItems);
{ NotificationLeftText = Value @ "/" @ Size;
`Log_Debug("ServerSync: Finished"); NotificationRightText = LocalMessage.static.GetLocalizedString(LogLevel, CTI_PleaseWait);
ClientSyncFinished(); NotificationPercent = (float(Value) / float(Size)) * 100;
KeepNotification();
} }
else
private simulated function NotifyIncompatibleGRI()
{ {
if (Recieved < RemoveItems.Length) WriteToChatLocalized(
{ CTI_IncompatibleGRI,
`Log_Debug("ServerSync[-]:" @ (Recieved + 1) @ "/" @ SyncSize @ RemoveItems[Recieved]); class'KFLocalMessage'.default.InteractionColor,
ClientSync(RemoveItems[Recieved++], true); String(WorldInfo.GRI.class));
} WriteToChatLocalized(
else CTI_IncompatibleGRIWarning,
{ class'KFLocalMessage'.default.InteractionColor);
`Log_Debug("ServerSync[+]:" @ (Recieved + 1) @ "/" @ SyncSize @ AddItems[Recieved - RemoveItems.Length]);
ClientSync(AddItems[Recieved++ - RemoveItems.Length], false);
}
}
} }
defaultproperties defaultproperties
@ -322,9 +342,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

@ -1,6 +1,11 @@
class Trader extends Object class Trader extends Object
abstract; abstract;
// Bug:
// The wrong weapon is purchased if the index is greater than 256 😡
// Some greedy guy saved 3 bytes for no reason again
const ITEMS_LIMIT = 256;
private delegate int ByPrice(class<KFWeaponDefinition> A, class<KFWeaponDefinition> B) private delegate int ByPrice(class<KFWeaponDefinition> A, class<KFWeaponDefinition> B)
{ {
return A.default.BuyPrice > B.default.BuyPrice ? -1 : 0; return A.default.BuyPrice > B.default.BuyPrice ? -1 : 0;
@ -88,19 +93,18 @@ public static function Array<class<KFWeaponDefinition> > GetTraderWeapDefsDLC(KF
public static simulated function ModifyTrader( public static simulated function ModifyTrader(
KFGameReplicationInfo KFGRI, KFGameReplicationInfo KFGRI,
Array<class<KFWeaponDefinition> > RemoveItems, const out Array<class<KFWeaponDefinition> > RemoveItems,
Array<class<KFWeaponDefinition> > AddItems, const out Array<class<KFWeaponDefinition> > AddItems,
bool ReplaceMode, bool ReplaceMode,
bool RemoveHRG, bool RemoveHRG,
bool RemoveDLC, bool RemoveDLC,
bool bDisableItemLimitCheck,
E_LogLevel LogLevel) E_LogLevel LogLevel)
{ {
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 Index; local int Index;
local int MaxItemID;
`Log_TraceStatic(); `Log_TraceStatic();
@ -128,13 +132,44 @@ public static simulated function ModifyTrader(
WeapDefs.Sort(ByPrice); WeapDefs.Sort(ByPrice);
if (!bDisableItemLimitCheck && WeapDefs.Length > ITEMS_LIMIT)
{
`Log_Warn("The total number of items has reached the limit (" $ ITEMS_LIMIT $ ")," @ (WeapDefs.Length - ITEMS_LIMIT) @ "items will not be added.");
`Log_Warn("Excluded items:");
for (Index = ITEMS_LIMIT; Index < WeapDefs.Length; ++Index)
{
`Log_Warn("[" $ Index + 1 $ "]" @ String(WeapDefs[Index]));
}
WeapDefs.Length = ITEMS_LIMIT;
}
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);
@ -142,7 +177,7 @@ public static simulated function ModifyTrader(
KFGRI.TraderItems = TraderItems; KFGRI.TraderItems = TraderItems;
} }
private static function bool WeaponClassIsUnique(String WeaponClassPath, Array<class<KFWeaponDefinition> > WeapDefs, E_LogLevel LogLevel) private static function bool WeaponClassIsUnique(String WeaponClassPath, const out Array<class<KFWeaponDefinition> > WeapDefs, E_LogLevel LogLevel)
{ {
local class<KFWeaponDefinition> WeapDef; local class<KFWeaponDefinition> WeapDef;

Binary file not shown.

Binary file not shown.