z_mirror/flib

git clone https://git.raiguard.me/z_mirror/flib

Caleb HeuerFix flib_locale.of using derived type for fallback string6ae3ed1

trunk
3.1 KiB78 linesraw
1local flib_prototypes = require("__flib__.prototypes")
2
3--- Provides utilities for deducing the localised names of various prototypes in the prototype stage.
4--- Inspired by Rusty's Locale Utilities: https://github.com/theRustyKnife/rusty-locale
5--- @class flib_locale
6local flib_locale = {}
7
8--- Returns the localised name of the given prototype.
9--- @overload fun(prototype: data.PrototypeBase): data.LocalisedString
10--- @overload fun(base_type: string, name: string): data.LocalisedString
11function flib_locale.of(prototype, name)
12  -- In this case, `prototype` is actually `base_type`.
13  if type(prototype) == "string" then
14    return flib_locale.of(flib_prototypes.get(prototype, name) --[[@as data.PrototypeBase]])
15  end
16  if prototype.type == "recipe" then
17    return flib_locale.of_recipe(prototype --[[@as data.RecipePrototype]])
18  elseif defines.prototypes.item[prototype.type] then
19    return flib_locale.of_item(prototype --[[@as data.ItemPrototype]])
20  else
21    return prototype.localised_name or { flib_prototypes.get_base_type(prototype.type) .. "-name." .. prototype.name }
22  end
23end
24
25--- Returns the localised name of the given item.
26--- @param item data.ItemPrototype
27--- @return data.LocalisedString
28function flib_locale.of_item(item)
29  if not defines.prototypes.item[item.type] then
30    error("Given prototype is not an item: " .. serpent.block(item))
31  end
32  if item.localised_name then
33    return item.localised_name
34  end
35  local type_name = "item"
36  --- @type data.PrototypeBase?
37  local prototype
38  if item.place_result then
39    type_name = "entity"
40    prototype = flib_prototypes.get("entity", item.place_result) --[[@as data.PrototypeBase]]
41  elseif item.place_as_equipment_result then
42    type_name = "equipment"
43    prototype = flib_prototypes.get("equipment", item.place_as_equipment_result) --[[@as data.PrototypeBase]]
44  elseif item.place_as_tile then
45    local tile_prototype = data.raw.tile[item.place_as_tile.result]
46    -- Tiles with variations don't have a localised name
47    if tile_prototype and tile_prototype.localised_name then
48      prototype = tile_prototype
49      type_name = "tile"
50    end
51  end
52  return prototype and prototype.localised_name or { type_name .. "-name." .. item.name }
53end
54
55--- Returns the localised name of the given recipe.
56--- @param recipe data.RecipePrototype
57--- @return data.LocalisedString
58function flib_locale.of_recipe(recipe)
59  if recipe.type ~= "recipe" then
60    error("Given prototype is not an recipe: " .. serpent.block(recipe))
61  end
62  if recipe.localised_name then
63    return recipe.localised_name
64  end
65  local main_product = recipe.main_product -- LuaLS gets confused if we don't assign to a local.
66  if main_product == "" then
67    return { "recipe-name." .. recipe.name }
68  elseif main_product and main_product == recipe.name then
69    return flib_locale.of_item(flib_prototypes.get("item", main_product))
70  end
71  local results = recipe.results
72  if results and #results == 1 and results[1].name == recipe.name then
73    return flib_locale.of_item(flib_prototypes.get("item", results[1].name))
74  end
75  return { "recipe-name." .. recipe.name }
76end
77
78return flib_locale