Skip to content

Commit

Permalink
Added Mod Requirement Attribute for IConfigurators (#86)
Browse files Browse the repository at this point in the history
* Added Mod Requirement Attribute for IConfigurators

* Added Has to ModRepository and changed modDependencies description
  • Loading branch information
TobbyTheBobby authored Sep 15, 2023
1 parent d2aef4b commit 20c5f0a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,26 @@ public void AddConfiguratorsFromAssemblyToRepository(Assembly assembly)
IEnumerable<Type> configuratorTypes = assembly.GetTypesByAttribute<ConfiguratorAttribute>();
ImmutableArray<IConfigurator> configurators = ValidateAndCreateConfigurators(configuratorTypes).ToImmutableArray();

_configuratorRepository.AddRange(SceneEntrypoint.MainMenu, GetConfiguratorWithSceneFlag(configurators, SceneEntrypoint.MainMenu));
_configuratorRepository.AddRange(SceneEntrypoint.InGame, GetConfiguratorWithSceneFlag(configurators, SceneEntrypoint.InGame));
_configuratorRepository.AddRange(SceneEntrypoint.MapEditor, GetConfiguratorWithSceneFlag(configurators, SceneEntrypoint.MapEditor));
var filteredConfigurators = FilterConfiguratorsWithMissingRequiredModDependencies(configurators).ToImmutableArray();

_configuratorRepository.AddRange(SceneEntrypoint.MainMenu, GetConfiguratorWithSceneFlag(filteredConfigurators, SceneEntrypoint.MainMenu));
_configuratorRepository.AddRange(SceneEntrypoint.InGame, GetConfiguratorWithSceneFlag(filteredConfigurators, SceneEntrypoint.InGame));
_configuratorRepository.AddRange(SceneEntrypoint.MapEditor, GetConfiguratorWithSceneFlag(filteredConfigurators, SceneEntrypoint.MapEditor));
}

/// <summary>
/// Configurator filter based on defined mod dependencies
/// </summary>
/// <param name="configurators">validated configurators</param>
/// <returns>Filtered configurators that have all their dependencies active</returns>
private IEnumerable<IConfigurator> FilterConfiguratorsWithMissingRequiredModDependencies(IEnumerable<IConfigurator> configurators)
{
return configurators.Where(configurator =>
{
var attribute = configurator.GetType().GetCustomAttribute<RequiredModDependencies>();

return attribute == null || attribute.ModDependencies.All(modDependency => _modRepository.Has(modDependency));
});
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions Core/TimberApi.Core/ModLoaderSystem/ModRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public bool TryGetByAssembly(Assembly assembly, out IMod mod)
return _assemblyModDirectory.TryGetValue(assembly, out mod!);
}

public bool Has(string uniqueId)
{
return _uniqueIdModDirectory.TryGetKey(uniqueId, out _);
}

[Inject]
public void InjectDependencies(ModLoader modLoader)
{
Expand Down
27 changes: 27 additions & 0 deletions Core/TimberApi/ConfiguratorSystem/RequiredModDependencies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace TimberApi.ConfiguratorSystem
{
/// <summary>
/// Attribute to register mod dependencies for a configurator
/// Attribute should be placed on a `IConfigurator` class with a Configurator attribute, optional
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class RequiredModDependencies : Attribute
{
/// <summary>
/// Required ModIds to install the configurator
/// </summary>
public string[] ModDependencies;

/// <summary>
/// Specify which mods are required to install the configurator.
/// Requires ALL mods to be active.
/// </summary>
/// <param name="modDependencies">Unique mod id's that are required to be active to install the configurator</param>
public RequiredModDependencies(params string[] modDependencies)
{
ModDependencies = modDependencies;
}
}
}
2 changes: 2 additions & 0 deletions Core/TimberApi/ModSystem/IModRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface IModRepository
bool TryGetByUniqueId(string uniqueId, out IMod mod);

bool TryGetByAssembly(Assembly assembly, out IMod mod);

bool Has(string uniqueId);
}
}

0 comments on commit 20c5f0a

Please sign in to comment.