Skip to content

Commit

Permalink
Add all prefab types to DI systems to be able to patch/register
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPurple6411 committed Jan 14, 2023
1 parent ce70725 commit 9a5ab6d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
23 changes: 11 additions & 12 deletions Example mod/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private const string
/// A simple SaveDataCache implementation, intended to save the players current position to disk.
/// </summary>
[FileName("player_position")]
internal class SaveData : SaveDataCache
internal class SaveData: SaveDataCache
{
public Vector3 PlayerPosition { get; set; }
}
Expand All @@ -45,23 +45,22 @@ public void Awake()
var builder = PrefabManager.CreateBuilder(this);
// Add
builder.Assets.AddCustomPrefab(new NuclearBattery());
builder.Assets.AddService<IGreeting, SayHi>();
// Run
builder.Build();
// Get Service
builder.GetService<IGreeting>().Greetings();


new EasyBattery("nuke2", "Nuclear Battery 2.0", "OOP Battery Test", true)
var nuke2 = new EasyBattery("nuke2", "Nuclear Battery 2.0", "OOP Battery Test", true)
{
PluginFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
PowerCapacity = 69420,
AddToFabricator = true,
Parts = new List<TechType>() { TechType.Copper, TechType.AcidMushroom, TechType.AcidMushroom, TechType.AcidMushroom },
}.Patch();
};


builder.Assets.AddPrefab(nuke2);
builder.Assets.AddService<IGreeting, SayHi>();

// Run
builder.Build();
// Get Service
builder.GetService<IGreeting>().Greetings();

LogSource = base.Logger;

Expand Down Expand Up @@ -170,7 +169,7 @@ public enum CustomChoice { One, Two, Three }
/// (defaults to "config") and an optional subfolder for the config file to reside in.</para>
/// </summary>
[Menu("SMLHelper Example Mod")]
public class Config : ConfigFile
public class Config: ConfigFile
{
/// <summary>
/// <para>A <see cref="ChoiceAttribute"/> is represented by a group of options where only one can be selected at a time,
Expand Down Expand Up @@ -319,7 +318,7 @@ private void MyGenericValueChangedEvent<T>(ConfigOptionEventArgs<T> e)
ExampleMod.LogSource.LogInfo("Generic value changed!");
ExampleMod.LogSource.LogInfo($"{e.Id}: {e.GetType()}");

switch (e)
switch(e)
{
case KeybindChangedEventArgs keybindChangedEventArgs:
ExampleMod.LogSource.LogInfo(KeyCodeUtils.KeyCodeToString(keybindChangedEventArgs.Value));
Expand Down
32 changes: 21 additions & 11 deletions SMLHelper/Assets/DependencyInjection/AssetBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,30 @@ public void Build()
}
}

private void RegisterPrefab(CustomPrefab customPrefab)
private void RegisterPrefab(ModPrefabRoot modPrefabRoot)
{
PrefabHandler.RegisterPrefab(customPrefab);

foreach (var (position, angles) in customPrefab.CoordinatedSpawns ?? Enumerable.Empty<Spawnable.SpawnLocation>())
switch(modPrefabRoot)
{
CoordinatedSpawnsHandler.RegisterCoordinatedSpawn(new SpawnInfo(customPrefab.PrefabInfo.ClassID, position, angles));
case Spawnable spawnable:
spawnable.Patch();
break;
case CustomPrefab customPrefab:
PrefabHandler.RegisterPrefab(customPrefab);
foreach(var (position, angles) in customPrefab.CoordinatedSpawns ?? Enumerable.Empty<Spawnable.SpawnLocation>())
{
CoordinatedSpawnsHandler.RegisterCoordinatedSpawn(new SpawnInfo(customPrefab.PrefabInfo.ClassID, position, angles));
}

if(customPrefab.BiomesToSpawnIn != null)
LootDistributionHandler.AddLootDistributionData(customPrefab.PrefabInfo.ClassID, customPrefab.PrefabInfo.PrefabPath, customPrefab.BiomesToSpawnIn);

if(customPrefab.Recipe != null)
CraftDataHandler.SetTechData(customPrefab.PrefabInfo.TechType, customPrefab.Recipe);
break;
case ModPrefab modPrefab:
PrefabHandler.RegisterPrefab(modPrefab);
break;
}

if (customPrefab.BiomesToSpawnIn != null)
LootDistributionHandler.AddLootDistributionData(customPrefab.PrefabInfo.ClassID, customPrefab.PrefabInfo.PrefabPath, customPrefab.BiomesToSpawnIn);

if (customPrefab.Recipe != null)
CraftDataHandler.SetTechData(customPrefab.PrefabInfo.TechType, customPrefab.Recipe);
}

private object ResolveParam(ParameterInfo param)
Expand Down
10 changes: 5 additions & 5 deletions SMLHelper/Assets/DependencyInjection/AssetCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class AssetCollection : IAssetCollection
{
private readonly List<AssetDescriptor> _descriptors = new();
private readonly List<CustomPrefab> _prefabs = new();
private readonly List<ModPrefabRoot> _prefabs = new();

public void AddDescriptor(AssetDescriptor item)
{
Expand All @@ -18,12 +18,12 @@ public List<AssetDescriptor> GetAssetDescriptors()
return _descriptors;
}

public void AddCustomPrefab(CustomPrefab customPrefab)
public void AddCustomPrefab(ModPrefabRoot customPrefab)
{
_prefabs.Add(customPrefab);
}

public List<CustomPrefab> GetCustomPrefabs()
public List<ModPrefabRoot> GetCustomPrefabs()
{
return _prefabs;
}
Expand All @@ -34,6 +34,6 @@ public interface IAssetCollection
void AddDescriptor(AssetDescriptor descriptor);
List<AssetDescriptor> GetAssetDescriptors();

void AddCustomPrefab(CustomPrefab customPrefab);
List<CustomPrefab> GetCustomPrefabs();
void AddCustomPrefab(ModPrefabRoot customPrefab);
List<ModPrefabRoot> GetCustomPrefabs();
}
4 changes: 2 additions & 2 deletions SMLHelper/Assets/DependencyInjection/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public static IAssetCollection AddService<TService>(this IAssetCollection assets


public static IAssetCollection AddPrefab<TCustomPrefab>(this IAssetCollection assets)
where TCustomPrefab : CustomPrefab, new()
where TCustomPrefab : ModPrefabRoot, new()
{
var prefab = new TCustomPrefab();
assets.AddCustomPrefab(prefab);
return assets;
}

public static IAssetCollection AddPrefab(this IAssetCollection assets, CustomPrefab customPrefab)
public static IAssetCollection AddPrefab(this IAssetCollection assets, ModPrefabRoot customPrefab)
{
assets.AddCustomPrefab(customPrefab);
return assets;
Expand Down

0 comments on commit 9a5ab6d

Please sign in to comment.