7 [
Attribute(
"", UIWidgets.ResourceNamePicker,
"Individual editor manager",
"et")]
8 private ResourceName m_EditorManagerPrefab;
10 [
Attribute(
"", UIWidgets.ResourceNamePicker,
"Server entity prefab",
"et")]
11 private ResourceName m_EditorServerPrefab;
14 private ref array<ref SCR_EditorModePrefab> m_ModePrefabs;
16 [
Attribute(
"300",
desc:
"When player disconnects, editor modes will be restored if the player reconnects with this timeout (in seconds).")]
17 protected int m_iRestoreOnReconnectTimeout;
20 private ref map<int, SCR_EditorManagerEntity> m_aEditorEntities;
23 private ref SCR_SortedArray<SCR_EditorModePrefab> m_ModePrefabsSorted =
new SCR_SortedArray<SCR_EditorModePrefab>;
24 private ref array<ref SCR_EditorManagerDisconnectData> m_DisconnectedModes;
28 ref ScriptInvoker Event_OnEditorManagerCreatedServer =
new ScriptInvoker;
32 ref ScriptInvoker Event_OnEditorManagerDeletedServer =
new ScriptInvoker;
36 ref ScriptInvoker Event_OnEditorManagerInitOwner =
new ScriptInvoker;
40 ref ScriptInvoker Event_OnEditorManagerPing =
new ScriptInvoker;
49 if (RplSession.Mode() == RplMode.Client || (m_SettingsEntity && m_SettingsEntity.IsEditorDisabled()))
52 if (m_aEditorEntities && m_aEditorEntities.Contains(playerID))
54 Debug.Error2(
"SCR_EditorManagerCore.CreateEditorManager()",
string.Format(
"Attempt for duplicate registration of editor manager for playerID = %1!", playerID));
58 PlayerController playerController =
GetGame().GetPlayerManager().GetPlayerController(playerID);
59 if (!playerController)
61 Debug.Error2(
"SCR_EditorManagerCore.CreateEditorManager()",
string.Format(
"Cannot create editor manager entity, PlayerController for playerID = %1 does not exist yet!", playerID));
66 RplIdentity playerRplID = playerController.GetRplIdentity();
69 for (
int i = 0; i < m_aEditorEntities.Count(); i++)
71 PlayerController compareController =
GetGame().GetPlayerManager().GetPlayerController(m_aEditorEntities.GetKey(i));
72 if (compareController && compareController.GetRplIdentity() == playerRplID)
74 Debug.Error2(
"SCR_EditorManagerCore.CreateEditorManager()",
string.Format(
"Cannot register editor manager for playerID = %1 because manager for playerID = %2 is already registered on the same machine!", playerID, m_aEditorEntities.GetKey(i)));
80 ResourceName prefab = m_EditorManagerPrefab;
82 prefab = m_SettingsEntity.GetPrefab(m_EditorManagerPrefab);
87 Debug.Error2(
"SCR_EditorManagerCore.CreateEditorManager()",
string.Format(
"Prefab %1 is not SCR_EditorManagerEntity!", prefab.GetPath()));
93 if (playerRplID != RplIdentity.Local())
95 RplComponent rpl = RplComponent.Cast(editorManager.FindComponent(RplComponent));
98 Debug.Error2(
"SCR_EditorManagerCore.CreateEditorManager()",
string.Format(
"Prefab %1 is missing RplComponent component!", prefab.GetPath()));
102 rpl.Give(playerRplID);
104 m_aEditorEntities.Insert(playerID, editorManager);
107 editorManager.InitServer(playerID);
108 Event_OnEditorManagerCreatedServer.Invoke(editorManager);
112 array<EPlayerRole> allRoles = {};
115 if (
GetGame().GetPlayerManager().HasPlayerRole(playerID, allRoles[i]))
116 playerRoles |= allRoles[i];
118 OnPlayerRoleChange(playerID, playerRoles);
124 if (m_DisconnectedModes)
127 WorldTimestamp currentTime = world.GetServerTimestamp();
128 for (
int i = m_DisconnectedModes.Count() - 1; i >= 0; i--)
130 if (currentTime.Greater(m_DisconnectedModes[i].m_Time.PlusSeconds(m_iRestoreOnReconnectTimeout)))
133 m_DisconnectedModes.Remove(i);
135 else if (m_DisconnectedModes[i].
m_iPlayerID == playerID)
138 modes = m_DisconnectedModes[i].m_Modes;
139 m_DisconnectedModes.Remove(i);
142 if (m_DisconnectedModes.IsEmpty())
143 m_DisconnectedModes =
null;
149 editorManager.PostInitServer();
151 return editorManager;
155 protected void DeleteEditorManager(
int playerID)
157 if (RplSession.Mode() == RplMode.Client)
160 if (m_aEditorEntities.Contains(playerID))
165 if (!m_DisconnectedModes)
166 m_DisconnectedModes = {};
175 SCR_Enum.HasFlag(modes, prefab.GetMode())
180 persistentModes |= prefab.GetMode();
184 if (persistentModes != 0)
187 m_aEditorEntities.Remove(playerID);
188 Event_OnEditorManagerDeletedServer.Invoke(editorManager);
189 delete editorManager;
196 int GetEditorEntities(out notnull array<SCR_EditorManagerEntity> outEditorEntities)
198 outEditorEntities.Clear();
199 if (m_aEditorEntities)
201 for (
int i = 0; i < m_aEditorEntities.Count(); i++)
205 outEditorEntities.Insert(editorManager);
208 return outEditorEntities.Count();
218 Print(
"Attempting to define multiple instances of SCR_EditorManagerEntity!", LogLevel.ERROR);
239 if (m_aEditorEntities && m_aEditorEntities.Contains(playerID))
240 return m_aEditorEntities.Get(playerID);
250 if (Replication.IsClient())
252 Debug.Error2(
"SCR_EditorManagerCore.GetEditorServer()",
"Editor server entity is available only on server!");
255 return m_ServerEntity;
264 if (m_SettingsEntity)
267 m_SettingsEntity = entity;
275 return m_SettingsEntity;
284 array<SCR_EditorModePrefab> prefabs = {};
285 GetBaseModePrefabs(prefabs);
288 if (modePrefab.GetMode() == modeType)
302 array<SCR_EditorModePrefab> prefabs = {};
303 for (
int i, count = GetBaseModePrefabs(prefabs, flags, coreOnly); i < count; i++)
305 modes |= prefabs[i].GetMode();
315 int GetBaseModePrefabs(out notnull array<SCR_EditorModePrefab> outPrefabs,
EEditorModeFlag flags = -1,
bool coreOnly =
false)
318 bool isOverride = !coreOnly && m_SettingsEntity && m_SettingsEntity.GetBaseModes(defaultModes);
320 int count = m_ModePrefabs.Count();
326 if (
SCR_Enum.HasFlag(defaultModes, prefab.GetMode()))
327 outPrefabs.Insert(prefab);
332 if (flags == -1 ||
SCR_Enum.HasPartialFlag(prefab.GetFlags(), flags))
333 outPrefabs.Insert(prefab);
336 return outPrefabs.Count();
344 int GetBaseModePrefabs(out notnull SCR_SortedArray<SCR_EditorModePrefab> outPrefabs,
EEditorModeFlag flags = -1,
bool coreOnly =
false)
347 bool isOverride = !coreOnly && m_SettingsEntity && m_SettingsEntity.GetBaseModes(defaultModes);
350 int count = m_ModePrefabsSorted.Count();
351 for (
int i; i < count; i++)
353 prefab = m_ModePrefabsSorted[i];
355 (!isOverride ||
SCR_Enum.HasFlag(defaultModes, prefab.GetMode()))
356 && (flags == -1 ||
SCR_Enum.HasPartialFlag(prefab.GetFlags(), flags))
359 outPrefabs.Insert(m_ModePrefabsSorted.GetOrder(i), prefab);
362 return outPrefabs.Count();
373 if (modeEntity.GetMode() == modeType)
374 return modeEntity.GetInfo();
384 protected void OnPlayerRegistered(
int playerID)
387 CreateEditorManager(playerID);
390 IEntity controlledEntity =
GetGame().GetPlayerManager().GetPlayerControlledEntity(playerID);
391 if (controlledEntity)
392 OnPlayerSpawn(playerID, controlledEntity);
396 protected void OnPlayerDisconnected(
int playerID)
399 DeleteEditorManager(playerID);
403 protected void OnPlayerSpawn(
int playerID, IEntity controlledEntity)
409 editorManager.AutoInit();
414 protected void OnPlayerKilled(
int playerId, IEntity playerEntity, IEntity killerEntity, notnull
Instigator killer)
422 protected void OnPlayerDeleted(
int playerID, IEntity player)
424 OnPlayerKilled(playerID, player,
null,
Instigator.CreateInstigator(
null));
428 protected void OnPlayerRoleChange(
int playerId,
EPlayerRole roleFlags)
441 if (
SCR_Global.IsAdminRole(roleFlags) && Replication.IsRunning())
452 void OnPlayFromCameraPos()
458 PlayerController playerController =
GetGame().GetPlayerController();
459 if (playerController)
461 int playerID = playerController.GetPlayerId();
464 CreateEditorManager(playerID);
468 GetGame().GetCallqueue().CallLater(OnPlayFromCameraPos, 1);
474 protected void OnReconnectOnPreviousCharacter(notnull
SCR_ReconnectData reconnectData)
476 int playerID = reconnectData.m_iPlayerId;
478 IEntity controlledEntity =
GetGame().GetPlayerManager().GetPlayerControlledEntity(playerID);
479 if (controlledEntity)
480 OnPlayerSpawn(playerID, controlledEntity);
488 override void OnGameStart()
491 if (RplSession.Mode() == RplMode.Client)
498 if (!m_SettingsEntity)
501 m_SettingsEntity.SetBaseModes(GetBaseModes(
EEditorModeFlag.DEFAULT,
true),
false);
506 if (m_aEditorEntities)
509 m_aEditorEntities =
new map<int, SCR_EditorManagerEntity>();
516 gameMode.GetOnPlayerRegistered().Insert(OnPlayerRegistered);
517 gameMode.GetOnPostCompPlayerDisconnected().Insert(OnPlayerDisconnected);
518 gameMode.GetOnPlayerSpawned().Insert(OnPlayerSpawn);
519 gameMode.GetOnPlayerKilled().Insert(OnPlayerKilled);
520 gameMode.GetOnPlayerDeleted().Insert(OnPlayerDeleted);
521 gameMode.GetOnPlayerRoleChange().Insert(OnPlayerRoleChange);
526 PlayerController controller =
GetGame().GetPlayerController();
527 if (controller && controller.GetPlayerId() > 0)
529 OnPlayerRegistered(controller.GetPlayerId());
545 override void OnGameEnd()
548 m_aEditorEntities =
null;
549 Event_OnEditorManagerCreatedServer =
new ScriptInvoker;
550 Event_OnEditorManagerDeletedServer =
new ScriptInvoker;
551 Event_OnEditorManagerInitOwner =
new ScriptInvoker;
561 m_ModePrefabsSorted.Insert(prefabData.GetInfo().GetOrder(), prefabData);
571 Event_OnEditorManagerCreatedServer =
null;
572 Event_OnEditorManagerDeletedServer =
null;
573 Event_OnEditorManagerInitOwner =
null;
590 m_Time = world.GetServerTimestamp();