Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 150: | Строка 150: | ||
end | end | ||
end | end | ||
end | |||
local function ucfirst(s) | |||
if not s or s == "" then return s end | |||
return string.upper(s:sub(1, 1)) .. (s:sub(2) or "") | |||
end | |||
local function normalizeComponentName(name) | |||
if type(name) ~= "string" or name == "" then | |||
return nil | |||
end | |||
name = mw.text.trim(name) | |||
if name:sub(1, 5) == "type:" then | |||
name = name:sub(6) | |||
elseif name:sub(1, 6) == "!type:" then | |||
name = name:sub(7) | |||
end | |||
if name == "" then | |||
return nil | |||
end | |||
return name | |||
end | |||
local function split_data_page_path(pagePath) | |||
local kind, name = string.match(pagePath or "", "^(component)/([^/]+)%.json$") | |||
if not kind then | |||
kind, name = string.match(pagePath or "", "^(prototype)/([^/]+)%.json$") | |||
end | |||
if not kind then | |||
return nil, nil | |||
end | |||
return kind, ucfirst(name) | |||
end | |||
local function get_component_from_entity(entity, componentName) | |||
if type(entity) ~= "table" or type(entity.components) ~= "table" then | |||
return nil | |||
end | |||
return entity.components["type:" .. componentName] | |||
or entity.components[componentName] | |||
or entity.components["!type:" .. componentName] | |||
end | |||
local function load_specific_entry(pagePath, id, allowDefault) | |||
local kind, name = split_data_page_path(pagePath) | |||
if not kind or not id or id == "" then | |||
return nil | |||
end | |||
if kind ~= "component" then | |||
return nil | |||
end | |||
local base | |||
local defaultData = load_cached_data(kind .. "/" .. name .. "/defaultFields.json") | |||
if type(defaultData) == "table" then | |||
base = deep_copy(defaultData) | |||
end | |||
local entityPage = "prototype/Entity/" .. id .. ".json" | |||
local entity = load_cached_data(entityPage) | |||
local specific = get_component_from_entity(entity, name) | |||
if type(specific) ~= "table" then | |||
if allowDefault == false then | |||
return nil | |||
end | |||
return base | |||
end | |||
if base then | |||
deep_merge(base, specific) | |||
return base | |||
end | |||
return specific | |||
end | end | ||
| Строка 191: | Строка 268: | ||
local function load_entry(id, pagePath, data, allowDefault) | local function load_entry(id, pagePath, data, allowDefault) | ||
local specificPageEntry = load_specific_entry(pagePath, id, allowDefault) | |||
if specificPageEntry ~= nil then | |||
return specificPageEntry | |||
end | |||
data = data or load_cached_data(pagePath) | data = data or load_cached_data(pagePath) | ||
if allowDefault == nil then | if allowDefault == nil then | ||
| Строка 663: | Строка 745: | ||
return out | return out | ||
end | end | ||
| Строка 735: | Строка 780: | ||
for _, idKey in ipairs(ids) do | for _, idKey in ipairs(ids) do | ||
local entry = resolve_entry_from_data(data, idKey | local entry = resolve_entry_from_data(data, idKey) | ||
if type(entry) == "table" and entry_matches_path(entry, parsedPath, target, searchType) then | if type(entry) == "table" and entry_matches_path(entry, parsedPath, target, searchType) then | ||
matches[#matches + 1] = idKey | matches[#matches + 1] = idKey | ||
| Строка 759: | Строка 804: | ||
if mode ~= "component" and mode ~= "tag" then | if mode ~= "component" and mode ~= "tag" then | ||
return " | return "" | ||
end | end | ||
local targets = split_csv_list(rawList) | local targets = split_csv_list(rawList) | ||
if #targets == 0 then | if #targets == 0 then | ||
return " | return "" | ||
end | end | ||
| Строка 771: | Строка 816: | ||
pagePath = "component.json" | pagePath = "component.json" | ||
else | else | ||
pagePath = " | pagePath = "tag.json" | ||
end | end | ||
local data = load_cached_data(pagePath) | local data = load_cached_data(pagePath) | ||
if not data or type(data) ~= "table" then | if not data or type(data) ~= "table" then | ||
return " | return "" | ||
end | end | ||
local ids = collect_id_keys(data) | local ids = collect_id_keys(data) | ||
if #ids == 0 then | if #ids == 0 then | ||
return " | return "" | ||
end | end | ||
| Строка 788: | Строка 833: | ||
end) | end) | ||
local function | local function entry_has_all_targets(entry, wanted) | ||
if type(entry) ~= "table" then | if type(entry) ~= "table" then | ||
return false | return false | ||
| Строка 796: | Строка 841: | ||
for _, v in ipairs(entry) do | for _, v in ipairs(entry) do | ||
set[tostring(v)] = true | set[tostring(v)] = true | ||
end | end | ||
| Строка 840: | Строка 855: | ||
for _, idKey in ipairs(ids) do | for _, idKey in ipairs(ids) do | ||
local entry = resolve_entry_from_data(data, idKey | local entry = resolve_entry_from_data(data, idKey) | ||
if | if entry_has_all_targets(entry, targets) then | ||
matches[#matches + 1] = idKey | |||
end | end | ||
end | end | ||
| Строка 857: | Строка 866: | ||
end | end | ||
return " | return "" | ||
end | end | ||
| Строка 910: | Строка 919: | ||
if searchType == "path" then | if searchType == "path" then | ||
for _, idKey in ipairs(ids) do | for _, idKey in ipairs(ids) do | ||
local entry = resolve_entry_from_data(data, idKey | local entry = resolve_entry_from_data(data, idKey) | ||
entryCache[idKey] = entry | entryCache[idKey] = entry | ||
if type(entry) == "table" and entry_has_any_nonempty_path(entry, parsedPath) then | if type(entry) == "table" and entry_has_any_nonempty_path(entry, parsedPath) then | ||
| Строка 919: | Строка 928: | ||
local target = tostring(searchValue) | local target = tostring(searchValue) | ||
for _, idKey in ipairs(ids) do | for _, idKey in ipairs(ids) do | ||
local entry = resolve_entry_from_data(data, idKey | local entry = resolve_entry_from_data(data, idKey) | ||
entryCache[idKey] = entry | entryCache[idKey] = entry | ||
if type(entry) == "table" and entry_matches_path(entry, parsedPath, target, searchType) then | if type(entry) == "table" and entry_matches_path(entry, parsedPath, target, searchType) then | ||
| Строка 933: | Строка 942: | ||
local out = {} | local out = {} | ||
for _, idKey in ipairs(matches) do | for _, idKey in ipairs(matches) do | ||
local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs, entryCache[idKey], | local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs, entryCache[idKey], true) | ||
if tpl ~= "" then | if tpl ~= "" then | ||
out[#out + 1] = tpl | out[#out + 1] = tpl | ||
| Строка 996: | Строка 1005: | ||
local out = {} | local out = {} | ||
for _, id in ipairs(ids) do | for _, id in ipairs(ids) do | ||
local entry = resolve_entry_from_data(data, id | local entry = resolve_entry_from_data(data, id) | ||
if entry ~= nil then | if entry ~= nil then | ||
local tpl = build_tpl(id, pagePath, tplPath, data, tplArgs, entry, true) | local tpl = build_tpl(id, pagePath, tplPath, data, tplArgs, entry, true) | ||
| Строка 1038: | Строка 1047: | ||
return "" | return "" | ||
end | |||
local function load_entity_data(entityId) | |||
if entityId == "" then | |||
return nil | |||
end | |||
local pagePath = "prototype/Entity/" .. entityId .. ".json" | |||
return load_cached_data(pagePath) | |||
end | |||
local function entity_has_component(entity, compName) | |||
if type(entity) ~= "table" or type(entity.components) ~= "table" then | |||
return false | |||
end | |||
return entity.components[compName] ~= nil | |||
or entity.components["type:" .. compName] ~= nil | |||
or entity.components["!type:" .. compName] ~= nil | |||
end | |||
local function collect_entity_components(entity) | |||
if type(entity) ~= "table" or type(entity.components) ~= "table" then | |||
return {} | |||
end | |||
local out = {} | |||
local seen = {} | |||
local comps = entity.components | |||
if #comps > 0 then | |||
for _, v in ipairs(comps) do | |||
local name = normalizeComponentName(v) | |||
if name and not seen[name] then | |||
seen[name] = true | |||
out[#out + 1] = name | |||
end | |||
end | |||
else | |||
for k in pairs(comps) do | |||
local name = normalizeComponentName(k) | |||
if name and not seen[name] then | |||
seen[name] = true | |||
out[#out + 1] = name | |||
end | |||
end | |||
end | |||
table.sort(out) | |||
return out | |||
end | end | ||
| Строка 1049: | Строка 1108: | ||
end | end | ||
local | local entity = load_entity_data(entityId) | ||
if not | if not entity then | ||
return "false" | return "false" | ||
end | end | ||
return entity_has_component(entity, compName) and "true" or "false" | |||
end | end | ||
| Строка 1077: | Строка 1121: | ||
if entityId == "" then | if entityId == "" then | ||
return " | return "" | ||
end | end | ||
local | local entity = load_entity_data(entityId) | ||
if not | if not entity then | ||
return " | return "" | ||
end | end | ||
local out = collect_entity_components(entity) | |||
local ok, json = pcall(mw.text.jsonEncode, out) | local ok, json = pcall(mw.text.jsonEncode, out) | ||
if ok and json then | if ok and json then | ||
| Строка 1102: | Строка 1135: | ||
end | end | ||
return " | return "" | ||
end | end | ||
| Строка 1178: | Строка 1211: | ||
for idKey in pairs(idsTable) do | for idKey in pairs(idsTable) do | ||
local entry = resolve_entry_from_data(data, idKey | local entry = resolve_entry_from_data(data, idKey) | ||
if entry ~= nil then | if entry ~= nil then | ||
local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs, entry, true) | local tpl = build_tpl(idKey, pagePath, tplPath, data, tplArgs, entry, true) | ||