Editing Module:Test

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
DefInfo = {}
+
---------------
local wiki = {}
+
-- load data --
 +
---------------
  
local function runTime()
+
-- wiki --
  return string.format("%i", os.clock() * 1000)
+
local Biomes = mw.loadData('Module:Test/data/biomes')
end
+
--local Buildings = mw.loadData('Module:Test/data/buildings')
 +
local Races = mw.loadData('Module:Test/data/races')
  
------------------------------------------------------------------
+
-- dev --
-- deal with differences between MediaWiki and dev environments --
+
--local Biomes = loadfile("./data/BiomeDefs.lua")()
------------------------------------------------------------------
+
--local Buildings = loadfile("./data/ThingDefs_Buildings.lua")()
 +
--local Races = loadfile("./data/ThingDefs_Races.lua")()
  
if mw then
+
local data = {}
 +
data["Biomes"] = Biomes
 +
--data["Buildings"] = Buildings
 +
data["Races"] = Races
  
  log = mw.log
+
-----------------------------
  logObject = mw.logObject
+
-- small utility functions --
 +
-----------------------------
  
  local timeDataStart = runTime()
+
-- returns the value (or nothing) found by first occurrence of a key within a table
 
+
local function search_table_recursive(key, table)
  Data  = mw.loadData('Module:Test/data')
+
   for k, v in pairs(table) do
 
+
    if k == key then return v
  local timeDataEnd = runTime()
+
    elseif type(v) == "table" then
   log(string.format('@%ims, data loaded in %ims', timeDataEnd, timeDataEnd - timeDataStart))
+
      local found = search_table_recursive(key, v)
 
+
      if found then return found end
  Util  = require("Module:Test/lib/util")
+
     end
  Search = require("Module:Test/lib/search")
 
  VF    = require("Module:Test/data/virtual")
 
 
 
  log(string.format('@%ims, modules loaded', runTime()))
 
 
 
else
 
 
 
  logDevStore = {}
 
 
 
  log = function(str)
 
     table.insert(logDevStore, str)
 
 
   end
 
   end
 +
end
  
  logObject = function(obj, prefix)
+
-- returns the immediate parent table (or nothing) of an element specified by a key and its value
     if prefix then
+
local function find_parent_table(key, value, table)
      assert(type(prefix) == "string")
+
  for k, v in pairs(table) do
       table.insert(logDevStore, prefix .. " = " .. Inspect(obj))
+
     if k == key and v == value then return table
    else
+
    elseif type(v) == "table" then
       table.insert(logDevStore, Inspect(obj))
+
       local found = find_parent_table(key, value, v)
 +
       if found then return found end
 
     end
 
     end
 
   end
 
   end
 
  function pp(tbl, title) -- pretty print tables
 
    Util.hl(title)
 
    print(Inspect(tbl))
 
  end
 
 
  local timeDataStart = runTime()
 
 
  Data    = require "data/data"
 
 
  local timeDataEnd = runTime()
 
  log(string.format('@%ims, data loaded in %ims', timeDataEnd, timeDataEnd - timeDataStart))
 
 
  Util    = require "lib/util"
 
  Search  = require "lib/search"
 
  Inspect = require "lib/inspect"
 
  VF      = require "data/virtual"
 
 
  log(string.format('@%ims, modules loaded', runTime()))
 
 
 
end
 
end
  
-----------------------
+
local function getParentName(defTable)
-- private functions --
+
   return defTable["ParentName"]
-----------------------
 
 
 
function DefInfo.vardefine(name, value, frame)
 
  assert(name, "vardefine: missing argument #1 (variable to definePrefix)")
 
  assert(type(name) == "string", string.format("vardefine: bad argument #1 (string expected, got %s)", type(name)))
 
  assert(value, "vardefine: missing argument #2 (value to assign)")
 
   assert(type(value) == "string" or type(value) == "number" or type(value) =="boolean", string.format("vardefine: bad argument #2 (string, number or boolean expected, got %s)", type(value)))
 
  assert(frame, "vardefine: 'frame' missing")
 
  frame:callParserFunction('#vardefine', name, value)
 
 
end
 
end
  
function DefInfo.expandDef(def, runMe)
+
-- '#' operator seems to work for numerically indexed tables so it can be used instead
   if not runMe then return nil end
+
-- this count function counts all the keys (of any type)
   local vFuncs = VF
+
local function count(table)
  for fName,func in pairs(vFuncs) do
+
   if type(table) ~= "table" then
    if func(def) then
+
    return "count(table): argument #1 is not a table"
       log(string.format('@%ims, expandDef: %s expanded with %s', runTime(), def.defName, fName))
+
   else
 +
    local length = 0;
 +
    for i, v in pairs(table) do
 +
       length = length + 1
 
     end
 
     end
 +
    return length
 
   end
 
   end
 
end
 
end
  
function DefInfo.mergeParents(baseDef, ignoreKeys)
+
-- "delimiter" must be a single character or the removal of the final delimiter won't work
   local ancestorIDs = {}
+
-- it's simpler this way but it could be reworked to enable multi-byte delimiters
  local mergedDef = {}
+
local function format_csv_string(simple_table, delimiter)
  local def = baseDef
+
   local list = ""
  
  while def._.ParentName do
+
    for k,v in pairs(simple_table) do
    local parentID = def._.DefCategory .. ":" .. def._.ParentName
+
      list = list .. v .. delimiter
     table.insert(ancestorIDs, parentID)
+
    end
    def = Data[parentID]
+
     if string.sub(list, -1) == delimiter then
  end
+
      list = string.sub(list, 1, -2)
 
+
     end
  ancestorIDs = Util.table.reverse(ancestorIDs)
+
    return list
  table.insert(ancestorIDs, baseDef._.DefCategory .. ":" .. baseDef.defName)
 
 
 
  for _,parentID in ipairs(ancestorIDs) do
 
     Util.table.overwrite(mergedDef, Data[parentID], ignoreKeys)
 
  end
 
 
 
  return mergedDef
 
 
end
 
end
  
function DefInfo.getDef(defID, expandVF)
+
local function vardefine(var_name, var_value)
   if expandVF ~= false then expandVF = true end
+
   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))
   local ignoreKeys = {"Abstract", "Name", "ParentName"}
+
   assert(var_name == "string", string.format("bad argument #1 to '%s' (string expected, got %s)", fName, type(var_name)))
   local baseDef
+
   assert(var_value, string.format("bad argument #2 to '%s' (argument missing, value to assign to variable)", fName))
  local def
+
  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)))
 
 
  if not defID then return nil end
 
 
 
  for _,def in pairs(Data) do
 
    if def.defName == defID then
 
      baseDef = def
 
      break
 
    elseif string.upper(def.label or '') == string.upper(defID) then
 
      baseDef = def
 
      break
 
    end
 
  end
 
 
 
  if not baseDef then return nil end
 
  
   def = DefInfo.mergeParents(baseDef, ignoreKeys)
+
   frame:callParserFunction('#vardefine', var_name, var_value)
 
 
  DefInfo.expandDef(def, expandVF)
 
 
 
  return def
 
 
end
 
end
  
local function setPrefix(tbl, parentKey)
+
------------------
  local mt = getmetatable(tbl) or {}
+
-- search logic --
 +
------------------
  
   for k,v in pairs(tbl) do
+
local function find_defTable(def)
    local prefix = parentKey .. "_" .. k
+
   for k1,v1 in pairs(data) do
     if type(v) == 'table' then
+
     if type(v1) == "table" then
       setPrefix(v, prefix)
+
       for k2,v2 in pairs(v1) do
    else
+
        if k2 == def then return v2 end
      mt[k] = prefix
+
       end
    end
 
  end
 
 
 
  setmetatable(tbl, mt)
 
end
 
 
 
local function definePrefixed(tbl, frame)
 
  for k,v in pairs(tbl) do
 
    if type(v) ~= 'table' then
 
      local mt = getmetatable(tbl)
 
      log(string.format('%s = %s', mt[k], tostring(v)))
 
      if mw then DefInfo.vardefine(mt[k], v, frame) end
 
    else
 
       definePrefixed(v, frame)
 
 
     end
 
     end
 
   end
 
   end
 
end
 
end
  
----------------------
+
local function searchParentDef(key, defTable)
-- public interface --
+
  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
  
function wiki.count(frame)
+
   local found = search_table_recursive(key, parentDefTable)
   local query = wiki.query(frame)
+
   if found then return found
   if type(wiki.queried) == 'table' then -- WARNING: checks a variable that is set in wiki.query (ugly)
+
  else
     return Util.table.count(wiki.queried)
+
    found = searchParentDef(key, parentDefTable)
 +
     if found then return found end
 
   end
 
   end
 
end
 
end
  
function wiki.query(frame)
+
--------------------------------
 +
-- publicly exposed functions --
 +
--------------------------------
  
  local argLen = Util.table.count(frame.args, "number") -- #frame.args won't work as expected, check the doc
+
local p = {}
  
   if not frame.args['defName'] and not frame.args['label'] then
+
function p.query(frame)
    logObject(frame.args, string.format('query @ %ims: missing an identifying argument (defName or label)\nframe.args', runTime()))
+
  local fName = debug.getinfo(1,"n").name
    return nil
+
   assert(frame.args[1], string.format("bad argument #1 to '%s' (argument missing, def)", fName))
  end
+
  assert(frame.args[2], string.format("bad argument #2 to '%s' (argument missing)", fName))
  
   local def = DefInfo.getDef(frame.args['defName']) or DefInfo.getDef(frame.args['label'])
+
   local defTable = find_defTable(frame.args[1], data)
 
+
   if not defTable then
   if not def then
+
     return "'" .. frame.args[1] .. "' not found"
     logObject(frame.args, string.format("query @ %ims: Def not found\nframe.args", runTime()))
 
    return nil
 
 
   end
 
   end
  
   if def and argLen == 0 then
+
  local found = search_table_recursive(frame.args[2], defTable)
     logObject(def, string.format("['%s:%s'] @ %ims", def._.DefCategory, def.defName, runTime()))
+
   if not found then
     return nil
+
     found = searchParentDef(frame.args[2], defTable)
 +
    if not found then
 +
      return "'" .. frame.args[2] .. "'" .. " not found in '" .. frame.args[1] .. "'"
 +
     end
 
   end
 
   end
  
   local processedDef = def
+
   -- multi-step query
 
+
   for i,v in ipairs(frame.args) do
   for i,arg in ipairs(frame.args) do -- arguments
+
     if i > 2 then
    arg = tonumber(arg) or arg -- frame.args are always strings on MediaWiki so convert back the numbers
+
       found = search_table_recursive(v, found)
 
 
     if i == argLen and frame.args["sibling"] then
 
       processedDef = Search.find({nil, frame.args["sibling"]} , processedDef)
 
      if not processedDef then
 
        logObject(frame.args, string.format("query @ %ims: bad argument 'sibling' ('%s' not found')\nframe.args", runTime(), frame.args["sibling"]))
 
        return nil
 
      else
 
        processedDef = Search.meta.parent.table[arg]
 
        if not processedDef then
 
          logObject(frame.args, string.format("query @ %ims: bad argument #%i ('%s' is not a sibling of '%s')", runTime(), i, arg, frame.args["sibling"]))
 
          return nil
 
        end
 
      end
 
 
     end
 
     end
 
    if i < argLen or i == argLen and not frame.args["sibling"] then
 
      processedDef = Search.find(arg, processedDef)
 
      if not processedDef then
 
        logObject(frame.args, string.format("query @ %ims: bad argument #%i ('%s' not found)\nframe.args", runTime(), i, frame.args[i]))
 
        return nil
 
      else
 
        if type(processedDef) ~= 'table' and i < argLen then
 
          log(string.format("query @ %ims: warning Def ['%s'] argument #%i ('%s' returns a value, all extra arguments ignored)", runTime(), def['label'], i, frame.args[i]))
 
          return processedDef
 
        end
 
      end
 
    end
 
 
  end -- for arguments
 
 
  if type(processedDef) == "table" then
 
    log(string.format("@%ims, query: table vardefined", runTime()))
 
    setPrefix(processedDef, frame.args[argLen])
 
    definePrefixed(processedDef, frame)
 
    wiki.queried = processedDef -- WARNING: sets a variable that is used in another function wiki.count (ugly)
 
    return nil
 
 
   end
 
   end
  
  log(string.format("@%ims, query: %s printed", runTime(), type(processedDef)))
+
   return found
   return processedDef
 
 
end
 
end
  
------------------------------------
+
-------------
-- simulate MediaWiki environment --
+
-- logging --
------------------------------------
+
-------------
  
if not mw then
+
mw.log("Module:DefInfo:os.clock() " .. os.clock()*1000 .. " ms")
  local simframe = { ["args"] = {} }
+
--print("Module:DefInfo:os.clock() " .. os.clock()*1000 .. " ms")
  simframe.args['label'] = 'ancient cryptosleep casket'
 
--~  simframe.args[1] = 'verbs'
 
--~  simframe.args[2] = 'label'
 
  wiki.query(simframe)
 
end
 
  
if not mw then
+
return p
  Util.hl("DefInfo log")
 
  for _,v in ipairs(logDevStore) do
 
    print(v)
 
  end
 
end
 
 
 
------------
 
-- return --
 
------------
 
 
 
if mw then
 
  return wiki
 
else
 
  return DefInfo
 
end
 

Please note that all contributions to RimWorld Wiki are considered to be released under the CC BY-SA 3.0 (see RimWorld Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)