KF2-CustomTraderInventory/CTI/Classes/CTI.uc

316 lines
6.3 KiB
Ucode
Raw Normal View History

2022-07-05 13:09:48 +00:00
class CTI extends Info
config(CTI);
const LatestVersion = 2;
2022-07-05 13:09:48 +00:00
const CfgRemoveItems = class'RemoveItems';
const CfgAddItems = class'AddItems';
const CfgOfficialWeapons = class'OfficialWeapons';
const Trader = class'Trader';
const Unlocker = class'Unlocker';
2022-07-05 13:09:48 +00:00
2022-07-10 02:49:38 +00:00
struct S_PreloadContent
{
var class<KFWeaponDefinition> KFWD;
var class<KFWeapon> KFWC;
var KFWeapon KFW;
var KFW_Access KFWA;
};
2022-07-05 13:09:48 +00:00
var private config int Version;
var private config E_LogLevel LogLevel;
var private config String UnlockDLC;
2022-07-08 18:18:58 +00:00
var private config bool bPreloadContent;
var private config bool bOfficialWeaponsList;
2022-07-05 13:09:48 +00:00
var private KFGameInfo KFGI;
var private KFGameReplicationInfo KFGRI;
var private Array<class<KFWeaponDefinition> > RemoveItems;
var private Array<class<KFWeaponDefinition> > AddItems;
var private Array<CTI_RepInfo> RepInfos;
var private bool ReadyToSync;
2022-07-10 02:49:38 +00:00
var private Array<S_PreloadContent> PreloadContent;
2022-07-05 13:09:48 +00:00
public simulated function bool SafeDestroy()
{
`Log_Trace();
2022-07-05 13:09:48 +00:00
return (bPendingDelete || bDeleteMe || Destroy());
}
public event PreBeginPlay()
{
`Log_Trace();
2022-07-05 13:09:48 +00:00
`Log_Debug("PreBeginPlay readyToSync" @ ReadyToSync);
if (WorldInfo.NetMode == NM_Client)
{
`Log_Fatal("NetMode == NM_Client, Destroy...");
SafeDestroy();
return;
}
Super.PreBeginPlay();
PreInit();
}
public event PostBeginPlay()
{
`Log_Trace();
2022-07-05 13:09:48 +00:00
if (bPendingDelete || bDeleteMe) return;
Super.PostBeginPlay();
PostInit();
}
private function PreInit()
{
`Log_Trace();
2022-07-05 13:09:48 +00:00
if (Version == `NO_CONFIG)
{
LogLevel = LL_Info;
bPreloadContent = true;
UnlockDLC = "False";
2022-07-05 13:09:48 +00:00
SaveConfig();
}
CfgRemoveItems.static.InitConfig(Version, LatestVersion);
CfgAddItems.static.InitConfig(Version, LatestVersion);
switch (Version)
{
case `NO_CONFIG:
`Log_Info("Config created");
2022-07-11 12:56:21 +00:00
case 1:
bOfficialWeaponsList = false;
2022-07-05 13:09:48 +00:00
case MaxInt:
`Log_Info("Config updated to version" @ LatestVersion);
2022-07-05 13:09:48 +00:00
break;
case LatestVersion:
`Log_Info("Config is up-to-date");
break;
default:
`Log_Warn("The config version is higher than the current version (are you using an old mutator?)");
`Log_Warn("Config version is" @ Version @ "but current version is" @ LatestVersion);
`Log_Warn("The config version will be changed to" @ LatestVersion);
break;
}
CfgOfficialWeapons.static.Update(bOfficialWeaponsList);
2022-07-05 13:09:48 +00:00
if (LatestVersion != Version)
{
Version = LatestVersion;
SaveConfig();
}
if (LogLevel == LL_WrongLevel)
{
LogLevel = LL_Info;
`Log_Warn("Wrong 'LogLevel', return to default value");
SaveConfig();
}
`Log_Base("LogLevel:" @ LogLevel);
if (!Unlocker.static.IsValidTypeUnlockDLC(UnlockDLC, LogLevel))
{
`Log_Warn("Wrong 'UnlockDLC' (" $ UnlockDLC $ "), return to default value (False)");
UnlockDLC = "False";
SaveConfig();
}
2022-07-05 13:09:48 +00:00
RemoveItems = CfgRemoveItems.static.Load(LogLevel);
AddItems = CfgAddItems.static.Load(LogLevel);
}
private function PostInit()
{
2022-07-14 06:55:58 +00:00
local CTI_RepInfo RepInfo;
2022-07-05 13:09:48 +00:00
`Log_Trace();
2022-07-05 13:09:48 +00:00
if (WorldInfo == None || WorldInfo.Game == None)
{
SetTimer(1.0f, false, nameof(PostInit));
return;
}
KFGI = KFGameInfo(WorldInfo.Game);
if (KFGI == None)
{
`Log_Fatal("Incompatible gamemode:" @ WorldInfo.Game);
SafeDestroy();
return;
}
if (KFGI.GameReplicationInfo == None)
{
SetTimer(1.0f, false, nameof(PostInit));
return;
}
KFGRI = KFGameReplicationInfo(KFGI.GameReplicationInfo);
if (KFGRI == None)
{
`Log_Fatal("Incompatible Replication info:" @ KFGI.GameReplicationInfo);
SafeDestroy();
return;
}
if (Unlocker.static.UnlockDLC(KFGI, KFGRI, UnlockDLC, RemoveItems, AddItems, LogLevel))
{
`Log_Info("DLC unlocked");
}
2022-07-05 13:09:48 +00:00
if (bPreloadContent)
{
2022-07-11 00:54:08 +00:00
Preload(AddItems);
2022-07-05 13:09:48 +00:00
}
Trader.static.ModifyTrader(KFGRI, RemoveItems, AddItems, CfgRemoveItems.default.bAll, LogLevel);
2022-07-05 13:09:48 +00:00
ReadyToSync = true;
2022-07-14 06:55:58 +00:00
foreach RepInfos(RepInfo)
2022-07-05 13:09:48 +00:00
{
2022-07-14 06:55:58 +00:00
if (RepInfo.PendingSync)
2022-07-05 13:09:48 +00:00
{
2022-07-14 06:55:58 +00:00
RepInfo.ServerSync();
2022-07-05 13:09:48 +00:00
}
}
}
2022-07-11 00:54:08 +00:00
private function Preload(Array<class<KFWeaponDefinition> > Content)
2022-07-10 02:49:38 +00:00
{
local Array<class<KFWeapon> > OfficialWeapons;
2022-07-10 02:49:38 +00:00
local S_PreloadContent SPC;
`Log_Trace();
OfficialWeapons = Trader.static.GetTraderWeapons();
2022-07-10 02:49:38 +00:00
foreach Content(SPC.KFWD)
{
SPC.KFWC = class<KFWeapon> (DynamicLoadObject(SPC.KFWD.default.WeaponClassPath, class'Class'));
if (SPC.KFWC != None)
{
if (OfficialWeapons.Find(SPC.KFWC) != INDEX_NONE)
{
`Log_Debug("Skip preload:" @ SPC.KFWD.GetPackageName() $ "." $ SPC.KFWD);
continue;
}
2022-07-10 02:49:38 +00:00
SPC.KFW = KFGI.Spawn(SPC.KFWC);
if (SPC.KFW == None)
{
`Log_Warn("Spawn failed:" @ SPC.KFWD.default.WeaponClassPath);
continue;
}
SPC.KFWA = new (SPC.KFW) class'KFW_Access';
if (SPC.KFWA == None)
{
`Log_Warn("Spawn failed:" @ SPC.KFWD.default.WeaponClassPath @ "KFW_Access");
continue;
}
PreloadContent.AddItem(SPC);
}
}
foreach PreloadContent(SPC)
{
2022-07-11 00:54:08 +00:00
SPC.KFWA.KFW_StartLoadWeaponContent();
2022-07-10 02:49:38 +00:00
}
`Log_Info("Preloaded" @ PreloadContent.Length @ "weapon models");
2022-07-10 02:49:38 +00:00
}
2022-07-05 13:09:48 +00:00
public function NotifyLogin(Controller C)
{
`Log_Trace();
2022-07-05 13:09:48 +00:00
2022-08-24 16:26:18 +00:00
if (!CreateRepInfo(C))
{
`Log_Error("Can't create RepInfo for:" @ C);
}
2022-07-05 13:09:48 +00:00
}
public function NotifyLogout(Controller C)
{
`Log_Trace();
2022-07-05 13:09:48 +00:00
DestroyRepInfo(C);
2022-07-05 13:09:48 +00:00
}
2022-07-14 06:55:58 +00:00
public function bool CreateRepInfo(Controller C)
2022-07-05 13:09:48 +00:00
{
2022-07-14 06:55:58 +00:00
local CTI_RepInfo RepInfo;
2022-07-05 13:09:48 +00:00
`Log_Trace();
2022-07-05 13:09:48 +00:00
if (C == None) return false;
2022-07-14 06:55:58 +00:00
RepInfo = Spawn(class'CTI_RepInfo', C);
2022-07-05 13:09:48 +00:00
2022-07-14 06:55:58 +00:00
if (RepInfo == None) return false;
2022-07-05 13:09:48 +00:00
2022-07-14 06:55:58 +00:00
RepInfo.PrepareSync(
2022-07-05 13:09:48 +00:00
Self,
LogLevel,
RemoveItems,
AddItems,
2022-07-11 00:54:08 +00:00
CfgRemoveItems.default.bAll);
2022-07-05 13:09:48 +00:00
2022-07-14 06:55:58 +00:00
RepInfos.AddItem(RepInfo);
2022-07-05 13:09:48 +00:00
if (ReadyToSync)
{
2022-07-14 06:55:58 +00:00
RepInfo.ServerSync();
2022-07-05 13:09:48 +00:00
}
else
{
2022-07-14 06:55:58 +00:00
RepInfo.PendingSync = true;
2022-07-05 13:09:48 +00:00
}
return true;
}
2022-07-14 06:55:58 +00:00
public function bool DestroyRepInfo(Controller C)
2022-07-05 13:09:48 +00:00
{
2022-07-14 06:55:58 +00:00
local CTI_RepInfo RepInfo;
2022-07-05 13:09:48 +00:00
`Log_Trace();
2022-07-05 13:09:48 +00:00
if (C == None) return false;
2022-07-14 06:55:58 +00:00
foreach RepInfos(RepInfo)
2022-07-05 13:09:48 +00:00
{
2022-07-14 06:55:58 +00:00
if (RepInfo.Owner == C)
2022-07-05 13:09:48 +00:00
{
2022-07-14 06:55:58 +00:00
RepInfos.RemoveItem(RepInfo);
RepInfo.SafeDestroy();
2022-07-05 13:09:48 +00:00
return true;
}
}
return false;
}
DefaultProperties
{
ReadyToSync = false
}