Skip to content

Commit

Permalink
Fixed race condition error on GameObjectFactory (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tariksavas authored Jul 21, 2024
1 parent 8af1c57 commit de8f223
Showing 1 changed file with 123 additions and 42 deletions.
165 changes: 123 additions & 42 deletions Runtime/Core/Factory/GameObjectFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,121 +9,202 @@ public class GameObjectFactory : Factory
public TFactorable Create<TFactorable>(TFactorable prefab)
where TFactorable : Component, IFactorable
{
TFactorable createdObject = Object.Instantiate(prefab);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab);

createdObject.InjectToFields(_container);

if (createdObject.TryGetComponent(out MonoInjecter monoInjecter))
if (properties.PrefabWasActive)
{
monoInjecter.Injected = true;
properties.CreatedObject.gameObject.SetActive(true);
}

return createdObject;
return properties.CreatedObject;
}

public TFactorable Create<TFactorable, TParam1>(TFactorable prefab, TParam1 param1)
where TFactorable : Component, IFactorable<TParam1>
{
TFactorable createdObject = Create(prefab);
createdObject.InitializeFactory(param1);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab);
properties.CreatedObject.InitializeFactory(param1);

return createdObject;
if (properties.PrefabWasActive)
{
properties.CreatedObject.gameObject.SetActive(true);
}

return properties.CreatedObject;
}

public TFactorable Create<TFactorable, TParam1, TParam2>(TFactorable prefab, TParam1 param1, TParam2 param2)
where TFactorable : Component, IFactorable<TParam1, TParam2>
{
TFactorable createdObject = Create(prefab);
createdObject.InitializeFactory(param1, param2);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab);
properties.CreatedObject.InitializeFactory(param1, param2);

if (properties.PrefabWasActive)
{
properties.CreatedObject.gameObject.SetActive(true);
}

return createdObject;
return properties.CreatedObject;
}

public TFactorable Create<TFactorable, TParam1, TParam2, TParam3>(TFactorable prefab, TParam1 param1, TParam2 param2, TParam3 param3)
where TFactorable : Component, IFactorable<TParam1, TParam2, TParam3>
{
TFactorable createdObject = Create(prefab);
createdObject.InitializeFactory(param1, param2, param3);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab);
properties.CreatedObject.InitializeFactory(param1, param2, param3);

if (properties.PrefabWasActive)
{
properties.CreatedObject.gameObject.SetActive(true);
}

return createdObject;
return properties.CreatedObject;
}

public TFactorable Create<TFactorable, TParam1, TParam2, TParam3, TParam4>(TFactorable prefab, TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4)
where TFactorable : Component, IFactorable<TParam1, TParam2, TParam3, TParam4>
{
TFactorable createdObject = Create(prefab);
createdObject.InitializeFactory(param1, param2, param3, param4);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab);
properties.CreatedObject.InitializeFactory(param1, param2, param3, param4);

if (properties.PrefabWasActive)
{
properties.CreatedObject.gameObject.SetActive(true);
}

return createdObject;
return properties.CreatedObject;
}

public TFactorable Create<TFactorable, TParam1, TParam2, TParam3, TParam4, TParam5>(TFactorable prefab, TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5)
where TFactorable : Component, IFactorable<TParam1, TParam2, TParam3, TParam4, TParam5>
{
TFactorable createdObject = Create(prefab);
createdObject.InitializeFactory(param1, param2, param3, param4, param5);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab);
properties.CreatedObject.InitializeFactory(param1, param2, param3, param4, param5);

return createdObject;
if (properties.PrefabWasActive)
{
properties.CreatedObject.gameObject.SetActive(true);
}

return properties.CreatedObject;
}

public TFactorable Create<TFactorable>(TFactorable prefab, Transform parent)
where TFactorable : Component, IFactorable
{
TFactorable createdObject = Object.Instantiate(prefab, parent);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab, parent);

createdObject.InjectToFields(_container);

if (createdObject.TryGetComponent(out MonoInjecter monoInjecter))
if (properties.PrefabWasActive)
{
monoInjecter.Injected = true;
properties.CreatedObject.gameObject.SetActive(true);
}

return createdObject;
return properties.CreatedObject;
}

public TFactorable Create<TFactorable, TParam1>(TFactorable prefab, Transform parent, TParam1 param1)
where TFactorable : Component, IFactorable<TParam1>
{
TFactorable createdObject = Create(prefab, parent);
createdObject.InitializeFactory(param1);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab, parent);
properties.CreatedObject.InitializeFactory(param1);

return createdObject;
if (properties.PrefabWasActive)
{
properties.CreatedObject.gameObject.SetActive(true);
}

return properties.CreatedObject;
}

public TFactorable Create<TFactorable, TParam1, TParam2>(TFactorable prefab, Transform parent, TParam1 param1, TParam2 param2)
where TFactorable : Component, IFactorable<TParam1, TParam2>
{
TFactorable createdObject = Create(prefab, parent);
createdObject.InitializeFactory(param1, param2);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab, parent);
properties.CreatedObject.InitializeFactory(param1, param2);

return createdObject;
if (properties.PrefabWasActive)
{
properties.CreatedObject.gameObject.SetActive(true);
}

return properties.CreatedObject;
}

public TFactorable Create<TFactorable, TParam1, TParam2, TParam3>(TFactorable prefab, Transform parent, TParam1 param1, TParam2 param2, TParam3 param3)
where TFactorable : Component, IFactorable<TParam1, TParam2, TParam3>
{
TFactorable createdObject = Create(prefab, parent);
createdObject.InitializeFactory(param1, param2, param3);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab, parent);
properties.CreatedObject.InitializeFactory(param1, param2, param3);

if (properties.PrefabWasActive)
{
properties.CreatedObject.gameObject.SetActive(true);
}

return createdObject;
return properties.CreatedObject;
}

public TFactorable Create<TFactorable, TParam1, TParam2, TParam3, TParam4>(TFactorable prefab, Transform parent, TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4)
where TFactorable : Component, IFactorable<TParam1, TParam2, TParam3, TParam4>
{
TFactorable createdObject = Create(prefab, parent);
createdObject.InitializeFactory(param1, param2, param3, param4);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab, parent);
properties.CreatedObject.InitializeFactory(param1, param2, param3, param4);

if (properties.PrefabWasActive)
{
properties.CreatedObject.gameObject.SetActive(true);
}

return createdObject;
return properties.CreatedObject;
}

public TFactorable Create<TFactorable, TParam1, TParam2, TParam3, TParam4, TParam5>(TFactorable prefab, Transform parent, TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5)
where TFactorable : Component, IFactorable<TParam1, TParam2, TParam3, TParam4, TParam5>
{
TFactorable createdObject = Create(prefab, parent);
createdObject.InitializeFactory(param1, param2, param3, param4, param5);
CreatedHiddenObjectProperties<TFactorable> properties = CreateHiddenObject(prefab, parent);
properties.CreatedObject.InitializeFactory(param1, param2, param3, param4, param5);

if (properties.PrefabWasActive)
{
properties.CreatedObject.gameObject.SetActive(true);
}

return properties.CreatedObject;
}

public CreatedHiddenObjectProperties<TFactorable> CreateHiddenObject<TFactorable>(TFactorable prefab, Transform parent = null) where TFactorable : Component, IFactorable
{
bool prefabWasActive = prefab.gameObject.activeSelf;

prefab.gameObject.SetActive(false);

TFactorable createdObject = Object.Instantiate(prefab, parent);
createdObject.InjectToFields(_container);

return createdObject;
if (createdObject.TryGetComponent(out MonoInjecter monoInjecter))
{
monoInjecter.Injected = true;
}

if (prefabWasActive)
{
prefab.gameObject.SetActive(true);
}

return new CreatedHiddenObjectProperties<TFactorable>(createdObject, prefabWasActive);
}

public readonly struct CreatedHiddenObjectProperties<TFactorable> where TFactorable : Component, IFactorable
{
public readonly TFactorable CreatedObject;

public readonly bool PrefabWasActive;

public CreatedHiddenObjectProperties(TFactorable createdObject, bool prefabWasActive) : this()
{
CreatedObject = createdObject;
PrefabWasActive = prefabWasActive;
}
}
}
}

0 comments on commit de8f223

Please sign in to comment.