Skip to content

Commit

Permalink
Separate DisplayName and DisplayNameClean (tModLoader#4052)
Browse files Browse the repository at this point in the history
* Separate DisplayName and DisplayNameClean

* Address feedback, apply to all other suitable situations.

* Update Mod.cs

* Address feedback
  • Loading branch information
JavidPack authored Feb 12, 2024
1 parent 3d164ea commit 2f0f68a
Show file tree
Hide file tree
Showing 21 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected override List<DefinitionOptionElement<BuffDefinition>> GetPassedOption
string modname = "Terraria";

if (option.Type >= BuffID.Count) {
modname = BuffLoader.GetBuff(option.Type).Mod.DisplayName; // or internal name?
modname = BuffLoader.GetBuff(option.Type).Mod.DisplayNameClean; // or internal name?
}

if (modname.IndexOf(ChooserFilterMod.CurrentString, StringComparison.OrdinalIgnoreCase) == -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override List<DefinitionOptionElement<ItemDefinition>> GetPassedOption
string modname = "Terraria";

if (option.Type >= ItemID.Count) {
modname = ItemLoader.GetItem(option.Type).Mod.DisplayName; // or internal name?
modname = ItemLoader.GetItem(option.Type).Mod.DisplayNameClean; // or internal name?
}

if (modname.IndexOf(ChooserFilterMod.CurrentString, StringComparison.OrdinalIgnoreCase) == -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override List<DefinitionOptionElement<NPCDefinition>> GetPassedOptionE
string modname = option.Definition.Mod;

if (option.Type >= NPCID.Count) {
modname = NPCLoader.GetNPC(option.Type).Mod.DisplayName; // or internal name?
modname = NPCLoader.GetNPC(option.Type).Mod.DisplayNameClean; // or internal name?
}

if (modname.IndexOf(ChooserFilterMod.CurrentString, StringComparison.OrdinalIgnoreCase) == -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected override List<DefinitionOptionElement<PrefixDefinition>> GetPassedOpti
string modname = option.Definition.Mod;

if (option.Type >= PrefixID.Count)
modname = PrefixLoader.GetPrefix(option.Type).Mod.DisplayName; // or internal name?
modname = PrefixLoader.GetPrefix(option.Type).Mod.DisplayNameClean; // or internal name?

if (modname.IndexOf(ChooserFilterMod.CurrentString, StringComparison.OrdinalIgnoreCase) == -1)
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected override List<DefinitionOptionElement<ProjectileDefinition>> GetPassed
string modname = option.Definition.Mod;

if (option.Type >= ProjectileID.Count) {
modname = ProjectileLoader.GetProjectile(option.Type).Mod.DisplayName; // or internal name?
modname = ProjectileLoader.GetProjectile(option.Type).Mod.DisplayNameClean; // or internal name?
}

if (!modname.Contains(ChooserFilterMod.CurrentString, StringComparison.OrdinalIgnoreCase))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override List<DefinitionOptionElement<TileDefinition>> GetPassedOption
string modname = "Terraria";

if (option.Type >= TileID.Count) {
modname = TileLoader.GetTile(option.Type).Mod.DisplayName; // or internal name?
modname = TileLoader.GetTile(option.Type).Mod.DisplayNameClean; // or internal name?
}

if (modname.IndexOf(ChooserFilterMod.CurrentString, StringComparison.OrdinalIgnoreCase) == -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ internal void SetMod(Mod mod, ModConfig config = null)

}
else {
throw new Exception($"There are no ModConfig for {mod.DisplayName}, how did this happen?");
throw new Exception($"There are no ModConfig for {mod.DisplayNameClean}, how did this happen?");
}
}

Expand Down
12 changes: 2 additions & 10 deletions patches/tModLoader/Terraria/ModLoader/Config/UI/UIModConfigList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private void PopulateMods()

// Have to sort by display name because normally mods are sorted by internal names
var mods = ModLoader.Mods.ToList();
mods.Sort((x, y) => CleanChatTags(x.DisplayName).CompareTo(CleanChatTags(y.DisplayName)));
mods.Sort((x, y) => x.DisplayNameClean.CompareTo(y.DisplayNameClean));

foreach (var mod in mods) {
if (ConfigManager.Configs.TryGetValue(mod, out _)) {
Expand Down Expand Up @@ -197,7 +197,7 @@ private void PopulateConfigs()

// Have to sort by display name because normally configs are sorted by internal names
// TODO: Support sort by attribute or some other custom ordering then replicate logic in UIModConfig.SetMod too
var sortedConfigs = configs.OrderBy(x => CleanChatTags(x.DisplayName.Value)).ToList();
var sortedConfigs = configs.OrderBy(x => Utils.CleanChatTags(x.DisplayName.Value)).ToList();

foreach (var config in sortedConfigs) {
float indicatorOffset = 20;
Expand Down Expand Up @@ -253,12 +253,4 @@ public override void Draw(SpriteBatch spriteBatch)
UILinkPointNavigator.Shortcuts.BackButtonCommand = 100;
UILinkPointNavigator.Shortcuts.BackButtonGoto = Interface.modsMenuID;
}

// TODO: this should be in utils, also add color parameter
private static string CleanChatTags(string text)
{
return string.Join("", ChatManager.ParseMessage(text, Color.White)
.Where(x => x.GetType() == typeof(TextSnippet))
.Select(x => x.Text));
}
}
2 changes: 2 additions & 0 deletions patches/tModLoader/Terraria/ModLoader/Core/LocalMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal class LocalMod

public string Name => modFile.Name;
public string DisplayName => string.IsNullOrEmpty(properties.displayName) ? Name : properties.displayName;
public readonly string DisplayNameClean; // Suitable for console output, chat tags stripped away.
public Version tModLoaderVersion => properties.buildVersion;

public bool Enabled {
Expand All @@ -25,6 +26,7 @@ public LocalMod(TmodFile modFile, BuildProperties properties)
{
this.modFile = modFile;
this.properties = properties;
DisplayNameClean = Utils.CleanChatTags(DisplayName);
}

public LocalMod(TmodFile modFile) : this(modFile, BuildProperties.ReadModFile(modFile))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Action(CommandCaller caller, string input, string[] args)
caller.Reply(Language.GetTextValue("tModLoader.CommandModListModlist"), Color.Yellow);

foreach (var mod in mods)
caller.Reply(mod.DisplayName);
caller.Reply(caller.CommandType == CommandType.Chat ? mod.DisplayName : mod.DisplayNameClean);
}
}
}
2 changes: 1 addition & 1 deletion patches/tModLoader/Terraria/ModLoader/Mod.Internals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal void Autoload()
while (AsyncLoadQueue.Count > 0)
AsyncLoadQueue.Dequeue().Wait();

ModSourceBestiaryInfoElement = new GameContent.Bestiary.ModSourceBestiaryInfoElement(this, DisplayName);
ModSourceBestiaryInfoElement = new GameContent.Bestiary.ModSourceBestiaryInfoElement(this, DisplayName); // TODO: DisplayName is incorrect, but ModBestiaryInfoElement._displayName usage inconsistent.

if (ContentAutoloadingEnabled) {
var loadableTypes = AssemblyManager.GetLoadableTypes(Code)
Expand Down
6 changes: 6 additions & 0 deletions patches/tModLoader/Terraria/ModLoader/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public partial class Mod
/// The display name of this mod in the Mods menu.
/// </summary>
public string DisplayName { get; internal set; }

private string displayNameClean;
/// <summary>
/// Same as DisplayName, but chat tags are removed. This can be used for more readable logging and console output. It is also useful for code that searches or filters by mod name.
/// </summary>
public string DisplayNameClean => displayNameClean ??= Utils.CleanChatTags(DisplayName);

public AssetRepository Assets { get; private set; }

Expand Down
2 changes: 1 addition & 1 deletion patches/tModLoader/Terraria/ModLoader/ModBossBarStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class ModBossBarStyle : ModType
/// <summary>
/// Controls the name that shows up in the menu selection. If not overridden, it will use this mod's display name.
/// </summary>
public virtual string DisplayName => Mod.DisplayName;
public virtual string DisplayName => Mod.DisplayNameClean;

/// <summary>
/// Return true to skip update code for boss bars. Useful if you want to use your own code for finding out which NPCs to track. Returns false by default.
Expand Down
2 changes: 1 addition & 1 deletion patches/tModLoader/Terraria/ModLoader/ModNPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public virtual void AutoStaticDefaults()
NPCLoader.bannerToItem[Banner] = BannerItem;
}
else if (Banner != 0 || BannerItem != 0) {
Logging.tML.Warn(Language.GetTextValue("tModLoader.LoadWarningBannerOrBannerItemNotSet", Mod.DisplayName, Name));
Logging.tML.Warn(Language.GetTextValue("tModLoader.LoadWarningBannerOrBannerItemNotSet", Mod.Name, Name));
}

if (NPC.lifeMax > 32767 || NPC.boss) {
Expand Down
2 changes: 1 addition & 1 deletion patches/tModLoader/Terraria/ModLoader/MonoModHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ internal static void RemoveAll(Mod mod)

foreach (var asm in AssemblyManager.GetModAssemblies(mod.Name)) {
if (assemblyDetours.TryGetValue(asm, out var list)) {
Logging.tML.Debug($"Unloading {list.ilHooks.Count} IL hooks, {list.detours.Count} detours from {asm.GetName().Name} in {mod.DisplayName}");
Logging.tML.Debug($"Unloading {list.ilHooks.Count} IL hooks, {list.detours.Count} detours from {asm.GetName().Name} in {mod.Name}");

foreach (var detour in list.detours)
if (detour.IsApplied)
Expand Down
4 changes: 2 additions & 2 deletions patches/tModLoader/Terraria/ModLoader/UI/Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ internal static void ServerModMenu(out bool reloadMods)
Console.WriteLine();
var mods = ModOrganizer.FindMods(logDuplicates: true);
for (int k = 0; k < mods.Length; k++) {
Console.Write((k + 1) + "\t\t" + mods[k].DisplayName);
Console.Write((k + 1) + "\t\t" + mods[k].DisplayNameClean);
Console.WriteLine(" (" + (mods[k].Enabled ? "enabled" : "disabled") + ")");
}
if (mods.Length == 0) {
Expand Down Expand Up @@ -648,7 +648,7 @@ private static void EnableDepsRecursive(LocalMod mod, LocalMod[] mods, List<stri
EnableDepsRecursive(dep, mods, missingRefs);
if (!dep.Enabled) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Automatically enabling {dep.DisplayName} required by {mod.DisplayName}");
Console.WriteLine($"Automatically enabling {dep.DisplayNameClean} required by {mod.DisplayNameClean}");
Console.ResetColor();
}
dep.Enabled ^= true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ModDownloadItem(string displayName, string name, Version version, string
{
ModName = name;
DisplayName = displayName;
DisplayNameClean = string.Join("", ChatManager.ParseMessage(displayName, Color.White).Where(x => x.GetType() == typeof(TextSnippet)).Select(x => x.Text));
DisplayNameClean = Utils.CleanChatTags(displayName);
PublishId = new ModPubId_t { m_ModPubId = publishId };
OwnerId = ownerId;

Expand Down
5 changes: 3 additions & 2 deletions patches/tModLoader/Terraria/ModLoader/UI/UILoadMods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public void SetLoadStage(string stageText, int modCount = -1)

private void SetProgressText(string text, string logText = null)
{
Logging.tML.Info(logText ?? text);
if (Main.dedServ) Console.WriteLine(text);
string cleanText = Utils.CleanChatTags(text); // text might have chat tags, most notably mod display names.
Logging.tML.Info(logText != null ? Utils.CleanChatTags(logText) : cleanText);
if (Main.dedServ) Console.WriteLine(cleanText);
else DisplayText = text;
}

Expand Down
2 changes: 1 addition & 1 deletion patches/tModLoader/Terraria/ModLoader/UI/UIModItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public UIModItem(LocalMod mod)
Height.Pixels = 90;
Width.Percent = 1f;
SetPadding(6f);
DisplayNameClean = string.Join("", ChatManager.ParseMessage(_mod.DisplayName, Color.White).Where(x => x.GetType() == typeof(TextSnippet)).Select(x => x.Text));
DisplayNameClean = _mod.DisplayNameClean;
}

public override void OnInitialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ internal static void PublishMod(LocalMod mod, string iconPath)
var values = new NameValueCollection
{
{ "displayname", bp.displayName },
{ "displaynameclean", string.Join("", ChatManager.ParseMessage(bp.displayName, Color.White).Where(x => x.GetType() == typeof(TextSnippet)).Select(x => x.Text)) },
{ "displaynameclean", Utils.CleanChatTags(bp.displayName) },
{ "name", modFile.Name },
{ "version", $"{bp.version}" },
{ "author", bp.author },
Expand Down
9 changes: 9 additions & 0 deletions patches/tModLoader/Terraria/Utils.TML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Terraria.DataStructures;
using Terraria.ModLoader;
using Terraria.ModLoader.UI;
using Terraria.UI;
using Terraria.UI.Chat;
using Terraria.Utilities;

namespace Terraria;
Expand Down Expand Up @@ -221,4 +223,11 @@ public static void LogAndConsoleErrorMessage(string message)
Console.ResetColor();
}
}

internal static string CleanChatTags(string text)
{
return string.Join("", ChatManager.ParseMessage(text, Color.White)
.Where(x => x.GetType() == typeof(TextSnippet))
.Select(x => x.Text));
}
}

0 comments on commit 2f0f68a

Please sign in to comment.