modded class ERR_GameMode
{
 void ERR_GameMode(IEntitySource src, IEntity parent)
 {
  if (!GetGame() || Replication.IsServer()) return;
  if (!GetPrefabData() || GetPrefabData().GetPrefabName() != "{8FB1559AE3F29722}Prefabs/ERR/TerrainConnector.et") return;
  GetGame().ERR_ClientConstructorDrain(this);
 }
}
modded class ArmaReforgerScripted
{
 protected ref array<IEntity> m_ERRClientOldManagers = {};
 bool m_ERRClientDetected;
 protected SCR_EntityCatalogManagerComponent m_ERROldCatalog;
 protected EntityID m_ERROldLoadoutId;
 protected SCR_AdditionalGameModeSettingsComponent m_ERROld0;
 protected SCR_PerceivedFactionManagerComponent m_ERROld1;
 protected SCR_PlayerListedAdminManagerComponent m_ERROld2;
 protected SCR_IdentityManagerComponent m_ERROld3;

 void ERR_ClientCaptureOriginal(SCR_BaseGameMode mode)
 {
  if (Replication.IsServer() || ERR_GameMode.Cast(mode)) return;
  m_ERROldCatalog = SCR_EntityCatalogManagerComponent.GetInstance();
  if (GetLoadoutManager()) m_ERROldLoadoutId = GetLoadoutManager().GetID();
  m_ERROld0 = SCR_AdditionalGameModeSettingsComponent.GetInstance();
  m_ERROld1 = SCR_PerceivedFactionManagerComponent.GetInstance();
  m_ERROld2 = SCR_PlayerListedAdminManagerComponent.GetInstance();
  m_ERROld3 = SCR_IdentityManagerComponent.GetInstance();

  m_ERRClientOldManagers = {mode, GetLoadoutManager(), GetFactionManager(), GetPerceptionManager(), GetChat()};
  ChimeraWorld chimera = ChimeraWorld.CastFrom(GetWorld());
  if (chimera) m_ERRClientOldManagers.Insert(chimera.GetRadioManager());
  PrintFormat("ERR CONSTRUCTOR: retained original managers=%1", m_ERRClientOldManagers.Count());
 }
 override void UnregisterLoadoutManager(SCR_LoadoutManager instance)
 {
  if (m_ERRClientDetected && instance && instance.GetID() == m_ERROldLoadoutId && instance != GetLoadoutManager())
  {
   Print("ERR CONSTRUCTOR: completed original loadout unregister");
   return;
  }
  super.UnregisterLoadoutManager(instance);
 }
 override protected void OnBeforeWorldCleanup()
 {
  m_ERRClientDetected = false;
  m_ERRClientOldManagers.Clear();
  m_ERROldCatalog = null;
  m_ERROld0 = null;
  m_ERROld1 = null;
  m_ERROld2 = null;
  m_ERROld3 = null;
  m_ERROldLoadoutId = EntityID.INVALID;
  super.OnBeforeWorldCleanup();
 }
 void ERR_ClientConstructorDrain(ERR_GameMode incoming)
 {
  if (m_ERRClientOldManagers.IsEmpty()) return;
  m_ERRClientDetected = true;
  SCR_BaseGameMode mode = SCR_BaseGameMode.Cast(m_ERRClientOldManagers[0]);
  PrintFormat("ERR CONSTRUCTOR: drain retained mode=%1 incoming=%2", mode, incoming);
  if (mode) mode.OnGameEnd();
  if (m_DataCollectorComponent && m_DataCollectorComponent.GetOwner() == mode) m_DataCollectorComponent = null;
  SCR_EntityCatalogManagerComponent.ERR_ReleaseClientCatalog(m_ERROldCatalog);
  m_DataCollectorComponent = null;
  SCR_AdditionalGameModeSettingsComponent.ERR_ReleaseClientTerrain(m_ERROld0);
  SCR_PerceivedFactionManagerComponent.ERR_ReleaseClientTerrain(m_ERROld1);
  SCR_PlayerListedAdminManagerComponent.ERR_ReleaseClientTerrain(m_ERROld2);
  SCR_IdentityManagerComponent.ERR_ReleaseClientTerrain(m_ERROld3);

  foreach (IEntity manager : m_ERRClientOldManagers) if (manager && manager != incoming) delete manager;
  m_ERRClientOldManagers.Clear();
  PrintFormat("ERR CONSTRUCTOR: drained native mode=%1 factions=%2 loadouts=%3", GetGameMode(), GetFactionManager(), GetLoadoutManager());
 }
}
modded class SCR_EntityCatalogManagerComponent
{
 static void ERR_ReleaseClientCatalog(SCR_EntityCatalogManagerComponent expected)
 {
  if (s_Instance == expected) s_Instance = null;
 }
 protected bool m_ERRWaitingForFaction;
 void ERR_CompleteFactionInit()
 {
  if (!m_ERRWaitingForFaction || !GetGame().GetFactionManager()) return;
  m_ERRWaitingForFaction = false;
  ClearEventMask(GetOwner(), EntityEvent.FRAME);
  super.EOnInit(GetOwner());
  Print("ERR CONSTRUCTOR: catalog completed during faction initialization");
 }
 override void EOnInit(IEntity owner)
 {
  if (!Replication.IsServer() && ERR_GameMode.Cast(owner) && !GetGame().GetFactionManager())
  {
   m_ERRWaitingForFaction = true;
   SetEventMask(owner, EntityEvent.FRAME);
   return;
  }
  super.EOnInit(owner);
 }
 override void EOnFrame(IEntity owner, float timeSlice)
 {
  if (!m_ERRWaitingForFaction || !GetGame().GetFactionManager()) return;
  m_ERRWaitingForFaction = false;
  ClearEventMask(owner, EntityEvent.FRAME);
  super.EOnInit(owner);
  Print("ERR CONSTRUCTOR: catalog initialized after factions");
 }
}
modded class SCR_DelegateFactionManagerComponent
{
 override void OnFactionsInit(array<Faction> factions)
 {
  if (!Replication.IsServer() && GetGame().m_ERRClientDetected)
  {
   SCR_EntityCatalogManagerComponent catalog = SCR_EntityCatalogManagerComponent.GetInstance();
   if (catalog) catalog.ERR_CompleteFactionInit();
   SCR_IdentityManagerComponent identity = SCR_IdentityManagerComponent.GetInstance();
   if (identity) identity.ERR_CompleteTerrainFactionInit();
   return;
  }
  super.OnFactionsInit(factions);
 }
}
modded class SCR_EditableFactionComponent
{
 override bool RplSave(ScriptBitWriter writer)
 {
  writer.WriteInt(m_iFactionIndex);
  return super.RplSave(writer);
 }
 override bool RplLoad(ScriptBitReader reader)
 {
  int initialIndex;
  if (!reader.ReadInt(initialIndex)) return false;
  if (GetGame().m_ERRClientDetected && !m_ScrFaction)
  {
   PrintFormat("ERR CONSTRUCTOR: delegate RplLoad wireIndex=%1", initialIndex);
   SetFactionIndexBroadcast(initialIndex);
  }
  return super.RplLoad(reader);
 }
}

modded class SCR_BaseGameMode
{
 override void EOnInit(IEntity owner)
 {
  super.EOnInit(owner);
  GetGame().ERR_ClientCaptureOriginal(this);
 }
}

modded class SCR_AdditionalGameModeSettingsComponent
{
 static void ERR_ReleaseClientTerrain(SCR_AdditionalGameModeSettingsComponent expected)
 {
  if (s_Instance == expected) s_Instance = null;
 }
}

modded class SCR_PerceivedFactionManagerComponent
{
 static void ERR_ReleaseClientTerrain(SCR_PerceivedFactionManagerComponent expected)
 {
  if (s_Instance == expected) s_Instance = null;
 }
}

modded class SCR_PlayerListedAdminManagerComponent
{
 static void ERR_ReleaseClientTerrain(SCR_PlayerListedAdminManagerComponent expected)
 {
  if (m_Instance == expected) m_Instance = null;
 }
}

modded class SCR_IdentityManagerComponent
{
 protected bool m_ERRWaitingForTerrainFaction;
 override void EOnInit(IEntity owner)
 {
  if (!Replication.IsServer() && GetGame().m_ERRClientDetected && ERR_GameMode.Cast(owner) && !GetGame().GetFactionManager())
  {
   m_ERRWaitingForTerrainFaction = true;
   return;
  }
  super.EOnInit(owner);
 }
 void ERR_CompleteTerrainFactionInit()
 {
  if (!m_ERRWaitingForTerrainFaction || !GetGame().GetFactionManager()) return;
  m_ERRWaitingForTerrainFaction = false;
  super.EOnInit(GetOwner());
 }

 static void ERR_ReleaseClientTerrain(SCR_IdentityManagerComponent expected)
 {
  if (m_sInstance == expected) m_sInstance = null;
 }
}
