Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Nickel/Framework/Content/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ IModDataHandler modDataHandler
var cards = new CardManager(currentModLoadPhaseProvider, loggerProvider, vanillaModManifest);
var artifacts = new ArtifactManager(currentModLoadPhaseProvider, loggerProvider, vanillaModManifest);
var characters = new CharacterManager(currentModLoadPhaseProvider, loggerProvider, eventManager, sprites, audio, decks, statuses, cards, vanillaModManifest, modLoaderModManifest);
var parts = new PartManager(enumCasePool, currentModLoadPhaseProvider);
var parts = new PartManager(enumCasePool, currentModLoadPhaseProvider, vanillaModManifest);
var ships = new ShipManager(currentModLoadPhaseProvider, vanillaModManifest);
var cardTraits = new CardTraitManager(loggerProvider, vanillaModManifest, modLoaderModManifest, modDataHandler);
var enemies = new EnemyManager(currentModLoadPhaseProvider, loggerProvider, vanillaModManifest);
Expand All @@ -90,6 +90,7 @@ internal void InjectLocalizations(string locale, Dictionary<string, string> loca
this.Cards.InjectLocalizations(locale, localizations);
this.Artifacts.InjectLocalizations(locale, localizations);
this.Characters.InjectLocalizations(locale, localizations);
this.Parts.InjectLocalizations(locale, localizations);
this.Ships.InjectLocalizations(locale, localizations);
this.Enemies.InjectLocalizations(locale, localizations);
}
Expand Down
50 changes: 40 additions & 10 deletions Nickel/Framework/Content/PartManager.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Nickel;

internal sealed class PartManager
{
private readonly IModManifest VanillaModManifest;
private readonly AfterDbInitManager<PartTypeEntry> PartTypeManager;
private readonly AfterDbInitManager<PartEntry> PartInstanceManager;
private readonly EnumCasePool EnumCasePool;
private readonly Dictionary<string, PartTypeEntry> UniqueNameToPartTypeEntry = [];
private readonly Dictionary<string, PartEntry> UniqueNameToPartInstanceEntry = [];
private readonly Dictionary<string, PType> VanillaPartTypes;

public PartManager(EnumCasePool enumCasePool, Func<ModLoadPhaseState> currentModLoadPhaseProvider)
public PartManager(EnumCasePool enumCasePool, Func<ModLoadPhaseState> currentModLoadPhaseProvider, IModManifest vanillaModManifest)
{
this.VanillaModManifest = vanillaModManifest;
this.PartTypeManager = new(currentModLoadPhaseProvider, Inject);
this.PartInstanceManager = new(currentModLoadPhaseProvider, Inject);
this.EnumCasePool = enumCasePool;

ArtifactRewardPatches.OnGetBlockedArtifacts += this.OnGetBlockedArtifacts;

this.VanillaPartTypes = Enum.GetValues<PType>().ToDictionary(v => Enum.GetName(v)!, v => v);
}

private void OnGetBlockedArtifacts(object? _, ArtifactRewardPatches.GetBlockedArtifactsEventArgs e)
Expand All @@ -33,7 +38,10 @@ private void OnGetBlockedArtifacts(object? _, ArtifactRewardPatches.GetBlockedAr
}

internal void InjectQueuedEntries()
=> this.PartInstanceManager.InjectQueuedEntries();
{
this.PartInstanceManager.InjectQueuedEntries();
this.PartTypeManager.InjectQueuedEntries();
}

public IPartTypeEntry RegisterPartType(IModManifest owner, string name, PartTypeConfiguration configuration)
{
Expand All @@ -59,20 +67,42 @@ public IPartEntry RegisterPart(IModManifest owner, string name, PartConfiguratio
return entry;
}

public bool TryGetPartTypeByUniqueName(string uniqueName, [MaybeNullWhen(false)] out IPartTypeEntry entry)
public IPartTypeEntry? LookupPartTypeByUniqueName(string uniqueName)
{
entry = null;
return this.UniqueNameToPartTypeEntry.TryGetValue(uniqueName, out var typedEntry);
if (this.UniqueNameToPartTypeEntry.TryGetValue(uniqueName, out var entry))
return entry;

if (this.VanillaPartTypes.TryGetValue(uniqueName, out var partType))
{
entry = new PartTypeEntry(this.VanillaModManifest, uniqueName, partType, new()
{
Name = _ => Loc.T($"part.{uniqueName}.name"),
Description = _ => Loc.T($"part.{uniqueName}.desc"),
});

this.UniqueNameToPartTypeEntry[uniqueName] = entry;
return entry;
}

return null;
}

public bool TryGetPartByUniqueName(string uniqueName, [MaybeNullWhen(false)] out IPartEntry entry)
internal void InjectLocalizations(string locale, Dictionary<string, string> localizations)
{
entry = null;
return this.UniqueNameToPartInstanceEntry.TryGetValue(uniqueName, out var typedEntry);
foreach (var entry in this.UniqueNameToPartTypeEntry.Values)
InjectLocalization(locale, localizations, entry);
}

private static void Inject(PartTypeEntry entry)
{
=> InjectLocalization(DB.currentLocale.locale, DB.currentLocale.strings, entry);

private static void InjectLocalization(string locale, Dictionary<string, string> localizations, PartTypeEntry entry)
{
var key = entry.PartType.Key();
if (entry.Configuration.Name.Localize(locale) is { } name)
localizations[$"part.{key}.name"] = name;
if (entry.Configuration.Description.Localize(locale) is { } description)
localizations[$"part.{key}.desc"] = description;
}

private static void Inject(PartEntry entry)
Expand Down
3 changes: 3 additions & 0 deletions Nickel/Framework/Implementations/Content/ModShips.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public IReadOnlyDictionary<string, IPartEntry> RegisteredParts
public IShipEntry? LookupByUniqueName(string uniqueName)
=> shipManagerProvider().LookupByUniqueName(uniqueName);

public IPartTypeEntry? LookupPartTypeByUniqueName(string uniqueName)
=> partManagerProvider().LookupPartTypeByUniqueName(uniqueName);

public IShipEntry RegisterShip(string name, ShipConfiguration configuration)
{
var entry = shipManagerProvider().RegisterShip(modManifest, name, configuration);
Expand Down
7 changes: 7 additions & 0 deletions Nickel/Interfaces/Content/IModShips.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public interface IModShips
/// <returns>An entry, or <c>null</c> if the unique name does not match any known ships.</returns>
IShipEntry? LookupByUniqueName(string uniqueName);

/// <summary>
/// Lookup a ship part type (<see cref="PType"/>) entry by its full <see cref="IModOwned.UniqueName"/>.
/// </summary>
/// <param name="uniqueName">The unique name to retrieve an entry for.</param>
/// <returns>An entry, or <c>null</c> if the unique name does not match any known part types.</returns>
IPartTypeEntry? LookupPartTypeByUniqueName(string uniqueName);

/// <summary>
/// Register a new <see cref="StarterShip"/>.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Nickel/Models/Content/PartTypeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace Nickel;
/// </summary>
public readonly struct PartTypeConfiguration
{
/// <summary>A localization provider for the name of the <see cref="PType"/>.</summary>
public SingleLocalizationProvider? Name { get; init; }

/// <summary>A localization provider for the description of the <see cref="PType"/>.</summary>
public SingleLocalizationProvider? Description { get; init; }

/// <summary>The artifact types exclusive to this part type.</summary>
public IReadOnlySet<Type>? ExclusiveArtifactTypes { get; init; }
}
Loading