Skip to content

Commit

Permalink
Renamed SetPrefab, added AddOnUnregister event
Browse files Browse the repository at this point in the history
  • Loading branch information
Metious committed Mar 19, 2023
1 parent e3469b5 commit c3d86e0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Example mod/CustomFabricatorExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private void Awake()
PrefabInfo stoneInfo = PrefabInfo.WithTechType("Stone", "Stone", "A good looking stone")
.WithIcon(SpriteManager.Get(TechType.Nickel));
CustomPrefab stone = new CustomPrefab(stoneInfo);
stone.SetPrefab(new CloneTemplate(stone.Info, TechType.Nickel));
stone.SetGameObject(new CloneTemplate(stone.Info, TechType.Nickel));
stone.Register();

/*
Expand Down Expand Up @@ -49,7 +49,7 @@ private void Awake()
{
FabricatorModel = FabricatorTemplate.Model.Workbench
};
customFab.SetPrefab(fabPrefab);
customFab.SetGameObject(fabPrefab);

/*
* This is a string example of how a RecipeData may look like in a json file. TechTypes can be
Expand Down
2 changes: 1 addition & 1 deletion Example mod/CustomPrefabExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void Awake()
/*
* Here we are setting the Titanium clone we created earlier as our item's prefab.
*/
titaniumClone.SetPrefab(cloneTemplate);
titaniumClone.SetGameObject(cloneTemplate);

/*
* Then we added biome spawns for our item.
Expand Down
50 changes: 44 additions & 6 deletions SMLHelper/Assets/CustomPrefab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public interface ICustomPrefab
PrefabInfo Info { get; }

/// <summary>
/// Function which constructs a game object as this prefab.
/// Function which constructs a game object for this prefab.
/// </summary>
PrefabFactoryAsync Prefab { get; }

/// <summary>
/// Function that will be executed after the SMLHelper's basic processing for <see cref="Prefab"/> has been completed.
/// </summary>
Expand Down Expand Up @@ -91,6 +91,12 @@ public interface ICustomPrefab
/// </summary>
/// <param name="onRegisterCallback">The action that will be called.</param>
void AddOnRegister(Action onRegisterCallback);

/// <summary>
/// Adds an action that will be called when this prefab has performed an unregister operation.
/// </summary>
/// <param name="onUnregisterCallback">The action that will be called.</param>
void AddOnUnregister(Action onUnregisterCallback);
}

/// <summary>
Expand All @@ -100,6 +106,7 @@ public class CustomPrefab : ICustomPrefab
{
private readonly Dictionary<Type, Gadget> _gadgets = new();
private readonly List<Action> _onRegister = new();
private readonly List<Action> _onUnregister = new();

private bool _registered;

Expand Down Expand Up @@ -204,29 +211,35 @@ public void AddOnRegister(Action onRegisterCallback)
_onRegister.Add(onRegisterCallback);
}

/// <inheritdoc/>
public void AddOnUnregister(Action onUnregisterCallback)
{
_onUnregister.Add(onUnregisterCallback);
}

/// <summary>
/// Sets a function as the game object constructor of this custom prefab. This is an asynchronous version.
/// </summary>
/// <param name="prefabAsync">The function to set.</param>
public void SetPrefab(Func<IOut<GameObject>, IEnumerator> prefabAsync) => Prefab = obj => prefabAsync(obj);
public void SetGameObject(Func<IOut<GameObject>, IEnumerator> prefabAsync) => Prefab = obj => prefabAsync(obj);

/// <summary>
/// Sets a prefab template as the game object constructor of this custom prefab.
/// </summary>
/// <param name="prefabTemplate">The prefab template object to set.</param>
public void SetPrefab(PrefabTemplate prefabTemplate) => Prefab = prefabTemplate.GetPrefabAsync;
public void SetGameObject(PrefabTemplate prefabTemplate) => Prefab = prefabTemplate.GetPrefabAsync;

/// <summary>
/// Sets a game object as the prefab of this custom prefab.
/// </summary>
/// <param name="prefab">The game object to set.</param>
public void SetPrefab(GameObject prefab) => Prefab = obj => SyncPrefab(obj, prefab);
public void SetGameObject(GameObject prefab) => Prefab = obj => SyncPrefab(obj, prefab);

/// <summary>
/// Sets a function as the game object constructor of this custom prefab. This is a synchronous version.
/// </summary>
/// <param name="prefab">The function to set.</param>
public void SetPrefab(Func<GameObject> prefab) => Prefab = obj => SyncPrefab(obj, prefab?.Invoke());
public void SetGameObject(Func<GameObject> prefab) => Prefab = obj => SyncPrefab(obj, prefab?.Invoke());

/// <summary>
/// Sets a post processor for the <see cref="Prefab"/>. This is an asynchronous version.
Expand Down Expand Up @@ -278,6 +291,31 @@ public void Register()

_registered = true;
}

/// <summary>
/// Unregisters this custom prefab from the game.
/// </summary>
/// <remarks>The class ID reference will be completely erased, however, the TechType instance will remain in the game.</remarks>
public void Unregister()
{
if (!_registered)
return;

if (string.IsNullOrEmpty(Info.ClassID) || string.IsNullOrEmpty(Info.PrefabFileName))
{
InternalLogger.Info($"Prefab '{Info}' is invalid or unknown. Skipping unregister operation.");
return;
}

foreach (var unReg in _onUnregister)
{
unReg?.Invoke();
}

PrefabHandler.Prefabs.UnregisterPrefab(this);

_registered = false;
}

private IEnumerator SyncPrefab(IOut<GameObject> obj, GameObject prefab)
{
Expand Down

0 comments on commit c3d86e0

Please sign in to comment.