-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description Adds a new Type for Prototypes to inherit instead of IPrototype with standard localization formats and lambda shortcuts to them. ---
- Loading branch information
1 parent
83bc824
commit 7deb42f
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Linq; | ||
using Robust.Shared.Prototypes; | ||
|
||
namespace Content.Shared.Prototypes; | ||
|
||
public abstract class LocalizedPrototype : IPrototype | ||
{ | ||
[IdDataField] | ||
public string ID { get; } = default!; | ||
|
||
public const string LocFormat = "{0}-{1}-{2}"; | ||
|
||
/// <summary>The localization string for the name of this prototype</summary> | ||
public string NameLoc => ToLocalizationString("name"); | ||
/// <summary>The localized string for the name of prototype</summary> | ||
public string Name => Loc.GetString(NameLoc); | ||
|
||
/// <summary> | ||
/// Returns an Loc string using the <see cref="field"/> as the 'property'. | ||
/// Given `desc` it will return `this-prototype-PrototypeId-desc`. | ||
/// </summary> | ||
public string ToLocalizationString(string field) | ||
{ | ||
// Get the ID of the proto Type | ||
var type = | ||
((PrototypeAttribute?) Attribute.GetCustomAttribute(GetType(), typeof(PrototypeAttribute)))?.Type | ||
?? GetType().Name.Remove(GetType().Name.Length - 9); | ||
// Lowercase the first letter | ||
type = char.ToLowerInvariant(type[0]) + type[1..]; | ||
// Replace every uppercase letter with a dash and the lowercase letter | ||
type = type.Aggregate("", (current, c) => current + (char.IsUpper(c) ? "-" + char.ToLowerInvariant(c) : c.ToString())); | ||
|
||
return string.Format(LocFormat, type, ID, field); | ||
} | ||
} |