KF2-LootedTraderInventory/LTI/Classes/LTI_RepInfo.uc

397 lines
8.4 KiB
Ucode
Raw Normal View History

2023-05-14 00:22:31 +00:00
class LTI_RepInfo extends ReplicationInfo;
2023-10-08 17:20:17 +00:00
const CAPACITY = 64; // max: 128
2023-05-14 00:22:31 +00:00
const Trader = class'Trader';
const LocalMessage = class'LTI_LocalMessage';
2023-10-08 17:20:17 +00:00
struct ReplicationStruct
{
var int Size;
var int Transfered;
var class<KFWeaponDefinition> Items[CAPACITY];
var int Length;
};
2023-05-14 00:22:31 +00:00
var public bool PendingSync;
var private LTI LTI;
var private E_LogLevel LogLevel;
var private GameReplicationInfo GRI;
2023-05-14 00:22:31 +00:00
var private KFPlayerController KFPC;
var private KFGFxWidget_PartyInGame PartyInGameWidget;
var private GFxObject Notification;
var private String NotificationHeaderText;
var private String NotificationLeftText;
var private String NotificationRightText;
var private int NotificationPercent;
var private int WaitingGRI;
var private int WaitingGRIThreshold;
2023-05-14 00:47:20 +00:00
var private int WaitingGRILimit;
2023-05-14 00:22:31 +00:00
2023-10-08 17:20:17 +00:00
var private ReplicationStruct RepData;
var private Array<class<KFWeaponDefinition> > RepArray;
2023-05-14 00:22:31 +00:00
replication
{
if (bNetInitial && Role == ROLE_Authority)
2023-10-08 17:20:17 +00:00
LogLevel;
2023-05-14 00:22:31 +00:00
}
public simulated function bool SafeDestroy()
{
`Log_Trace();
return (bPendingDelete || bDeleteMe || Destroy());
}
2023-10-08 17:20:17 +00:00
public function PrepareSync(LTI _LTI, E_LogLevel _LogLevel)
2023-05-14 00:22:31 +00:00
{
`Log_Trace();
LTI = _LTI;
LogLevel = _LogLevel;
2023-10-08 17:20:17 +00:00
}
public function Replicate(const out Array<class<KFWeaponDefinition> > WeapDefs)
{
`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 (GetGRI(WaitingGRI > WaitingGRIThreshold) == None && WaitingGRI++ < WaitingGRILimit)
2023-10-08 17:20:17 +00:00
{
`Log_Debug("Finished: Waiting GRI" @ WaitingGRI);
NotifyWaitingGRI();
SetTimer(1.0f, false, nameof(Finished));
return;
}
KFGRI = KFGameReplicationInfo(GRI);
2023-10-08 17:20:17 +00:00
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 Game Replication info:" @ String(GRI));
if (GRI == None)
{
NotifyNoneGRI();
}
else
{
NotifyIncompatibleGRI();
}
2023-10-08 17:20:17 +00:00
}
ShowReadyButton();
ClientCleanup();
2023-05-14 00:22:31 +00:00
}
private simulated function GameReplicationInfo GetGRI(optional bool ForcedSearch = false)
{
`Log_Trace();
if (GRI == None)
{
GRI = WorldInfo.GRI;
}
if (GRI == None && ForcedSearch)
{
foreach WorldInfo.DynamicActors(class'GameReplicationInfo', GRI) break;
}
if (WorldInfo.GRI == None && GRI != None)
{
`Log_Warn("Force initialization of WorldInfo.GRI" @ String(GRI));
WorldInfo.GRI = GRI;
}
return GRI;
}
2023-05-14 00:22:31 +00:00
private simulated function KFPlayerController GetKFPC()
{
`Log_Trace();
if (KFPC != None) return KFPC;
KFPC = KFPlayerController(Owner);
if (KFPC == None && ROLE < ROLE_Authority)
{
KFPC = KFPlayerController(GetALocalPlayerController());
}
return KFPC;
}
2023-05-14 00:47:20 +00:00
public reliable client function WriteToChatLocalized(
E_LTI_LocalMessageType LMT,
optional String HexColor,
optional String String1,
optional String String2,
optional String String3)
{
`Log_Trace();
WriteToChat(LocalMessage.static.GetLocalizedString(LogLevel, LMT, String1, String2, String3), HexColor);
}
public reliable client function WriteToChat(String Message, optional String HexColor)
{
local KFGFxHudWrapper HUD;
`Log_Trace();
if (GetKFPC() == None) return;
if (KFPC.MyGFxManager.PartyWidget != None && KFPC.MyGFxManager.PartyWidget.PartyChatWidget != None)
{
KFPC.MyGFxManager.PartyWidget.PartyChatWidget.SetVisible(true);
KFPC.MyGFxManager.PartyWidget.PartyChatWidget.AddChatMessage(Message, HexColor);
}
HUD = KFGFxHudWrapper(KFPC.myHUD);
if (HUD != None && HUD.HUDMovie != None && HUD.HUDMovie.HudChatBox != None)
{
HUD.HUDMovie.HudChatBox.AddChatMessage(Message, HexColor);
}
}
2023-05-14 00:22:31 +00:00
private simulated function SetPartyInGameWidget()
{
`Log_Trace();
if (GetKFPC() == None) return;
if (KFPC.MyGFxManager == None) return;
if (KFPC.MyGFxManager.PartyWidget == None) return;
PartyInGameWidget = KFGFxWidget_PartyInGame(KFPC.MyGFxManager.PartyWidget);
Notification = PartyInGameWidget.Notification;
}
private simulated function bool CheckPartyInGameWidget()
{
`Log_Trace();
if (PartyInGameWidget == None)
{
SetPartyInGameWidget();
}
return (PartyInGameWidget != None);
}
private simulated function HideReadyButton()
{
`Log_Trace();
if (CheckPartyInGameWidget())
{
PartyInGameWidget.SetReadyButtonVisibility(false);
}
}
private simulated function ShowReadyButton()
{
`Log_Trace();
2023-10-08 17:20:17 +00:00
ClearTimer(nameof(KeepNotification));
2023-05-14 00:22:31 +00:00
if (CheckPartyInGameWidget())
{
Notification.SetVisible(false);
PartyInGameWidget.SetReadyButtonVisibility(true);
PartyInGameWidget.UpdateReadyButtonText();
PartyInGameWidget.UpdateReadyButtonVisibility();
}
}
private simulated function UpdateNotification(String Title, String Left, String Right, int Percent)
{
`Log_Trace();
if (CheckPartyInGameWidget() && Notification != None)
{
Notification.SetString("itemName", Title);
Notification.SetFloat("percent", Percent);
Notification.SetInt("queue", 0);
Notification.SetString("downLoading", Left);
Notification.SetString("remaining", Right);
Notification.SetObject("notificationInfo", Notification);
Notification.SetVisible(true);
}
}
private simulated function KeepNotification()
{
HideReadyButton();
UpdateNotification(
NotificationHeaderText,
NotificationLeftText,
NotificationRightText,
NotificationPercent);
}
2023-10-08 17:20:17 +00:00
private simulated function ClientCleanup()
2023-05-14 00:22:31 +00:00
{
`Log_Debug("Cleanup");
2023-10-08 17:20:17 +00:00
ServerCleanup();
2023-05-14 00:22:31 +00:00
SafeDestroy();
}
2023-10-08 17:20:17 +00:00
private reliable server function ServerCleanup()
2023-05-14 00:22:31 +00:00
{
`Log_Trace();
`Log_Debug("Cleanup");
2023-10-08 17:20:17 +00:00
if (!LTI.DestroyRepInfo(GetKFPC()))
2023-05-14 00:22:31 +00:00
{
`Log_Debug("Cleanup (forced)");
SafeDestroy();
}
}
2023-10-08 17:20:17 +00:00
private simulated function NotifyWaitingGRI()
2023-05-14 00:22:31 +00:00
{
2023-10-08 17:20:17 +00:00
if (!IsTimerActive(nameof(KeepNotification)))
2023-05-14 00:22:31 +00:00
{
2023-10-08 17:20:17 +00:00
SetTimer(0.1f, true, nameof(KeepNotification));
2023-05-14 00:22:31 +00:00
}
2023-10-08 17:20:17 +00:00
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)))
2023-05-14 00:22:31 +00:00
{
2023-10-08 17:20:17 +00:00
SetTimer(0.1f, true, nameof(KeepNotification));
2023-05-14 00:22:31 +00:00
}
2023-10-08 17:20:17 +00:00
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(GRI.class));
2023-10-08 17:20:17 +00:00
WriteToChatLocalized(
LTI_IncompatibleGRIWarning,
class'KFLocalMessage'.default.InteractionColor);
2023-05-14 00:22:31 +00:00
}
private simulated function NotifyNoneGRI()
{
WriteToChatLocalized(
LTI_NoneGRI,
class'KFLocalMessage'.default.InteractionColor);
WriteToChatLocalized(
LTI_NoneGRIWarning,
class'KFLocalMessage'.default.InteractionColor);
}
2023-05-14 00:22:31 +00:00
defaultproperties
{
bAlwaysRelevant = false
bOnlyRelevantToOwner = true
bSkipActorPropertyReplication = false
PendingSync = false
NotificationPercent = 0
WaitingGRI = 0
WaitingGRIThreshold = 10
WaitingGRILimit = 20
2023-05-14 00:22:31 +00:00
}