From 7deb42f38b084c95e39feabef13796b066a075c8 Mon Sep 17 00:00:00 2001
From: DEATHB4DEFEAT <77995199+DEATHB4DEFEAT@users.noreply.github.com>
Date: Wed, 2 Oct 2024 16:25:24 -0700
Subject: [PATCH] Add LocalizedPrototype Type (#747)
# Description
Adds a new Type for Prototypes to inherit instead of IPrototype with
standard localization formats and lambda shortcuts to them.
---
---
.../Prototypes/LocalizedPrototype.cs | 35 +++++++++++++++++++
1 file changed, 35 insertions(+)
create mode 100644 Content.Shared/Prototypes/LocalizedPrototype.cs
diff --git a/Content.Shared/Prototypes/LocalizedPrototype.cs b/Content.Shared/Prototypes/LocalizedPrototype.cs
new file mode 100644
index 00000000000..acdd5fc180f
--- /dev/null
+++ b/Content.Shared/Prototypes/LocalizedPrototype.cs
@@ -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}";
+
+ /// The localization string for the name of this prototype
+ public string NameLoc => ToLocalizationString("name");
+ /// The localized string for the name of prototype
+ public string Name => Loc.GetString(NameLoc);
+
+ ///
+ /// Returns an Loc string using the as the 'property'.
+ /// Given `desc` it will return `this-prototype-PrototypeId-desc`.
+ ///
+ 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);
+ }
+}