Skip to content

Commit

Permalink
feat: New WithTechType overload and explicit cast from Enum to EnumBu…
Browse files Browse the repository at this point in the history
…ilder<Enum> (#457)

* Added WithTechType overload without languages

This overload is especially useful when you're adding language lines before creating this tech type, so you don't have to supply `null` for displayName and the description and make the call unnecessarily longer.

* Added explicit cast from Enum to EnumBuilder<Enum>
  • Loading branch information
Metious committed Sep 4, 2023
1 parent 607f513 commit 20084f4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Nautilus/Assets/PrefabInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@ namespace Nautilus.Assets;
/// <param name="TechType">The <see cref="TechType"/> of the corresponding item.</param>
public record struct PrefabInfo(string ClassID, string PrefabFileName, TechType TechType)
{
/// <summary>
/// Constructs a new <see cref="PrefabInfo"/> instance with automatically set <see cref="PrefabFileName"/> and <see cref="TechType"/>.
/// </summary>
/// <remarks>This overload does not do anything about the language side of this prefab (DisplayName and Description) and assumes that the entries were already added.<br/>
/// For the DisplayName, the language line must be "{ClassID}".<br/>
/// For the Description, the language line must be "Tooltip_{ClassID}" instead.</remarks>
/// <param name="classId">The class identifier used for the <see cref="PrefabIdentifier"/> component whenever applicable.</param>
/// <param name="unlockAtStart">Whether this tech type should be unlocked on game start or not. Default to <see langword="true"/>.</param>
/// <param name="techTypeOwner">The assembly that owns the created tech type. The name of this assembly will be shown in the PDA.</param>
/// <returns>An instance of the constructed <see cref="PrefabInfo"/>.</returns>
/// <seealso cref="WithTechType(string, string, string, string, bool, System.Reflection.Assembly)"/>
/// <seealso cref="LanguageHandler.SetLanguageLine"/>
/// <seealso cref="LanguageHandler.RegisterLocalizationFolder"/>
/// <seealso cref="LanguageHandler.RegisterLocalization"/>
public static PrefabInfo WithTechType(string classId, bool unlockAtStart = true, Assembly techTypeOwner = null)
{
techTypeOwner ??= Assembly.GetCallingAssembly();
techTypeOwner = techTypeOwner == Assembly.GetExecutingAssembly()
? ReflectionHelper.CallingAssemblyByStackTrace()
: techTypeOwner;
return new PrefabInfo
(
classId,
classId + "Prefab",
EnumHandler.AddEntry<TechType>(classId, techTypeOwner).WithPdaInfo(null, null, unlockAtStart: unlockAtStart)
);
}

/// <summary>
/// Constructs a new <see cref="PrefabInfo"/> instance with automatically set <see cref="PrefabFileName"/> and <see cref="TechType"/>.
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion Nautilus/Handlers/Enums/EnumBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,28 @@ private EnumBuilder()
/// <summary>
/// Converts an EnumBuilder to its corresponding enum object.
/// </summary>
/// <param name="enumBuilder">The Enum Builder</param>
/// <param name="enumBuilder">The Enum Builder.</param>
/// <returns>The enum object equivalent to this instance.</returns>
public static implicit operator TEnum(EnumBuilder<TEnum> enumBuilder)
{
return enumBuilder.Value;
}

/// <summary>
/// Converts an Enum object to EnumBuilder.
/// </summary>
/// <param name="enum">The Enum object.</param>
/// <returns>The constructed enum builder for the specified enum object.</returns>
public static explicit operator EnumBuilder<TEnum>(TEnum @enum)
{
var enumBuilder = new EnumBuilder<TEnum>
{
_enumValue = @enum
};

return enumBuilder;
}

/// <summary>
/// Converts the value of this instance to a <see cref="string"/>.
/// </summary>
Expand Down

0 comments on commit 20084f4

Please sign in to comment.