Module:Test

From RimWorld Wiki
Jump to navigation Jump to search

This module is used for development.

Purpose

This module is used to query information from the uploaded and parsed game files.

Its main purpose is to populate the infoboxes.

Usage

A note on the order of named parameters. All of the parameters that look like ...=... are called named parameters and their order is not important (this is true for all templates).

query

{{#invoke:Test|query|<def ID>[|...|][|tag|][|sibling=...]}}

The work-horse. Output varies based on use:

If only the <def ID> parameter is set, it will show the whole Def in the log.
If simple values are queried it will return them.
If lists are queried it will return nothing but call {{#vardefine}} on all the simple values within it. What got defined can be seen in the page's log.

Named parameters:

<def ID>
This parameter identifies the Def so it is mandatory. It can take two forms, if both are defined then defName takes preference.
defName=<defName>
<defName> (case sensitive) should be replaced with the actual defName of a Def.
label=<label>
<label> (case insensitive) should be replaced with the actual label of a Def.
[sibling=...] (optional) (case sensitive)
Allows querying for something if we know its sibling's value (works only for values at the moment).

Anonymous parameters:

[|...|] (optional) (case sensitive)
Anonymous paramaters before the last one ([tag]) are here to help uniquely identify it. If the [tag] is already unique within a Def tree, then these additional parameters are not needed.
[|tag|] (optional) (case sensitive)
The final anonymous parameter defines what is to be queried.

count

{{#invoke:Test|count|<def ID>[|...|][|tag|][|sibling=...]}}

Parameters are the same as for query. It's basically a wrapped up query that behaves a bit differently.

The difference is in how it handles lists. If a list is queried, unlike query, it will return the length of the list.

How-to

Take a look at a Def

{{#invoke:Test|query|label=desert}}

Lua error at line 123: attempt to call field 'getinfo' (a nil value).

Data is in the log.

Retrieve a simple value

{{#invoke:Test|query|defName=Caribou|description}}

Lua error at line 123: attempt to call field 'getinfo' (a nil value).

Dealing with lists

{{#invoke:Test|query|defName=Mech_Scyther|tools}}

Lua error at line 123: attempt to call field 'getinfo' (a nil value).

When a list is retrieved there will be no output but the log will contain a list of defined variables.

For convenience the list is reprinted here:

tools_1_linkedBodyPartsGroup = LeftBlade
tools_1_cooldownTime = 2
tools_1_label = left blade
tools_1_DPS = 10
tools_1_power = 20
tools_1_capacities_1 = Cut
tools_1_capacities_2 = Stab
tools_2_linkedBodyPartsGroup = RightBlade
tools_2_cooldownTime = 2
tools_2_label = right blade
tools_2_DPS = 10
tools_2_power = 20
tools_2_capacities_1 = Cut
tools_2_capacities_2 = Stab
tools_3_linkedBodyPartsGroup = HeadAttackTool
tools_3_capacities_1 = Blunt
tools_3_label = head
tools_3_DPS = 4.5
tools_3_chanceFactor = 0.2
tools_3_power = 9
tools_3_cooldownTime = 2

All of the above can be accessed with the use of {{#var:...}}.

{{#var:tools_1_DPS}}

DPS is not a normal member of this table but has been added with Lua. Let's call it a virtual field.

Retrieve something if a sibling is known

{{#invoke:Test|query|label=guinea pig|minAge|sibling=AnimalAdult}}

Lua error at line 123: attempt to call field 'getinfo' (a nil value).


---------------
-- load data --
---------------

-- wiki --
local Biomes = mw.loadData('Module:Test/data/biomes')
--local Buildings = mw.loadData('Module:Test/data/buildings')
local Races = mw.loadData('Module:Test/data/races')

-- dev --
--local Biomes = loadfile("./data/BiomeDefs.lua")()
--local Buildings = loadfile("./data/ThingDefs_Buildings.lua")()
--local Races = loadfile("./data/ThingDefs_Races.lua")()

local data = {}
data["Biomes"] = Biomes
--data["Buildings"] = Buildings
data["Races"] = Races

-----------------------------
-- small utility functions --
-----------------------------

-- returns the value (or nothing) found by first occurrence of a key within a table
local function search_table_recursive(key, table)
  for k, v in pairs(table) do
    if k == key then return v
    elseif type(v) == "table" then
      local found = search_table_recursive(key, v)
      if found then return found end
    end
  end
end

-- returns the immediate parent table (or nothing) of an element specified by a key and its value
local function find_parent_table(key, value, table)
  for k, v in pairs(table) do
    if k == key and v == value then return table
    elseif type(v) == "table" then
      local found = find_parent_table(key, value, v)
      if found then return found end
    end
  end
end

local function getParentName(defTable)
  return defTable["ParentName"]
end

-- '#' operator seems to work for numerically indexed tables so it can be used instead
-- this count function counts all the keys (of any type)
local function count(table)
  if type(table) ~= "table" then
    return "count(table): argument #1 is not a table"
  else
    local length = 0;
    for i, v in pairs(table) do
      length = length + 1
    end
    return length
  end
end

-- "delimiter" must be a single character or the removal of the final delimiter won't work
-- it's simpler this way but it could be reworked to enable multi-byte delimiters
local function format_csv_string(simple_table, delimiter)
  local list = ""

    for k,v in pairs(simple_table) do
      list = list .. v .. delimiter
    end
    if string.sub(list, -1) == delimiter then
      list = string.sub(list, 1, -2)
    end
    return list
end

local function vardefine(var_name, var_value)
  local fName = debug.getinfo(1,"n").name
  assert(var_name, string.format("bad argument #1 to '%s' (argument missing, name of variable to define)", fName))
  assert(var_name == "string", string.format("bad argument #1 to '%s' (string expected, got %s)", fName, type(var_name)))
  assert(var_value, string.format("bad argument #2 to '%s' (argument missing, value to assign to variable)", fName))
  assert(var_value == "string" or var_value == "number", string.format("bad argument #2 to '%s' (string or number expected, got %s)", fName, type(var_value)))

  frame:callParserFunction('#vardefine', var_name, var_value)
end

------------------
-- search logic --
------------------

local function find_defTable(def)
  for k1,v1 in pairs(data) do
    if type(v1) == "table" then
      for k2,v2 in pairs(v1) do
        if k2 == def then return v2 end
      end
    end
  end
end

local function searchParentDef(key, defTable)
  local ParentName = getParentName(defTable)
  if not ParentName then return nil end
  local parentDefTable = search_table_recursive(ParentName, data)
  if not parentDefTable then return nil end

  local found = search_table_recursive(key, parentDefTable)
  if found then return found
  else
    found = searchParentDef(key, parentDefTable)
    if found then return found end
  end
end

--------------------------------
-- publicly exposed functions --
--------------------------------

local p = {}

function p.query(frame)
  local fName = debug.getinfo(1,"n").name
  assert(frame.args[1], string.format("bad argument #1 to '%s' (argument missing, def)", fName))
  assert(frame.args[2], string.format("bad argument #2 to '%s' (argument missing)", fName))

  local defTable = find_defTable(frame.args[1], data)
  if not defTable then
    return "'" .. frame.args[1] .. "' not found"
  end

  local found = search_table_recursive(frame.args[2], defTable)
  if not found then
    found = searchParentDef(frame.args[2], defTable)
    if not found then
      return "'" .. frame.args[2] .. "'" .. " not found in '" .. frame.args[1] .. "'"
    end
  end

  -- multi-step query
  for i,v in ipairs(frame.args) do
    if i > 2 then
      found = search_table_recursive(v, found)
    end
  end

  return found
end

-------------
-- logging --
-------------

mw.log("Module:DefInfo:os.clock() " .. os.clock()*1000 .. " ms")
--print("Module:DefInfo:os.clock() " .. os.clock()*1000 .. " ms")

return p