Skip to content

Commit

Permalink
Show localized book title/author in tooltip and info popup
Browse files Browse the repository at this point in the history
Also works in book shelves like those in Mages Guild.
  • Loading branch information
Interkarma committed Nov 30, 2022
1 parent 6a97e93 commit 84af0c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Assets/Scripts/Game/Items/DaggerfallUnityItemMCP.cs
Expand Up @@ -153,9 +153,15 @@ public override string ArmourMod()

public override string BookAuthor()
{ // %ba
BookFile bookFile = new BookFile();

// Get book author from localized book file as first preference
string name = GameManager.Instance.ItemHelper.GetBookFileName(parent.message);
LocalizedBook localizedBook = new LocalizedBook();
if (localizedBook.OpenLocalizedBookFile(name))
return localizedBook.Author;

// Fallback to legacy book data
BookFile bookFile = new BookFile();
if (name != null)
{
if (!BookReplacement.TryImportBook(name, bookFile))
Expand Down
16 changes: 16 additions & 0 deletions Assets/Scripts/Game/Items/ItemHelper.cs
Expand Up @@ -61,6 +61,7 @@ public class ItemHelper
readonly Dictionary<int, ImageData> itemImages = new Dictionary<int, ImageData>();
readonly Dictionary<InventoryContainerImages, ImageData> containerImages = new Dictionary<InventoryContainerImages, ImageData>();
readonly Dictionary<int, String> bookIDNameMapping = new Dictionary<int, String>();
readonly Dictionary<int, String> localizedBookIDNameMapping = new Dictionary<int, string>();

public delegate bool ItemUseHandler(DaggerfallUnityItem item, ItemCollection collection);
Dictionary<int, ItemUseHandler> itemUseHandlers = new Dictionary<int, ItemUseHandler>();
Expand Down Expand Up @@ -550,6 +551,21 @@ public static ArtifactsSubTypes GetArtifactSubType(string itemShortName)
/// <returns>The title of the bookd or defaultBookName if no name was found.</returns>
public string GetBookTitle(int id, string defaultBookTitle)
{
// Get cached localized book title if previously read
if (localizedBookIDNameMapping.ContainsKey(id))
return localizedBookIDNameMapping[id];

// Get book title from localized book file as first preference
// Localized title will be cached so file is only read once
string filename = GetBookFileName(id);
LocalizedBook localizedBook = new LocalizedBook();
if (localizedBook.OpenLocalizedBookFile(filename))
{
localizedBookIDNameMapping.Add(id, localizedBook.Title);
return localizedBook.Title;
}

// Fallback to legacy data
string title;
return bookIDNameMapping.TryGetValue(id, out title) ? title : defaultBookTitle;
}
Expand Down

0 comments on commit 84af0c6

Please sign in to comment.