Модуль:Сущность/data: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 5: | Строка 5: | ||
local dpOk, dpModule = pcall(require, "Module:GetField") | local dpOk, dpModule = pcall(require, "Module:GetField") | ||
local dp = dpOk and dpModule or nil | local dp = dpOk and dpModule or nil | ||
local ucfirst = dp and dp.ucfirst or function(s) | |||
if not s or s == "" then return s end | |||
return string.upper(s:sub(1, 1)) .. (s:sub(2) or "") | |||
end | |||
local switchModeRegistry = {} | local switchModeRegistry = {} | ||
| Строка 26: | Строка 31: | ||
end | end | ||
local function | local function load_cached(page) | ||
if dp then | |||
return dp.loadCachedData(page) | |||
end | |||
local moduleName = JsonPaths.get(page) | local moduleName = JsonPaths.get(page) | ||
local ok, data = pcall(mw.loadData, moduleName) | local ok, data = pcall(mw.loadData, moduleName) | ||
| Строка 47: | Строка 55: | ||
end | end | ||
local function | local function load_entity_components(entityId) | ||
if not | if not entityId or entityId == "" then | ||
return | return {} | ||
end | |||
if dp then | |||
local entity = dp.loadEntityData(entityId) | |||
if entity then | |||
return dp.collectEntityComponents(entity) | |||
end | |||
end | |||
local page = "prototype/Entity/" .. entityId .. ".json" | |||
local moduleName = JsonPaths.get(page) | |||
local ok, data = pcall(mw.loadData, moduleName) | |||
if not ok or type(data) ~= "table" then | |||
return {} | |||
end | |||
if type(data.components) ~= "table" then | |||
return {} | |||
end | |||
local out = {} | |||
local seen = {} | |||
for k in pairs(data.components) do | |||
if type(k) == "string" then | |||
local name = k | |||
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 ~= "" and not seen[name] then | |||
seen[name] = true | |||
out[#out + 1] = name | |||
end | |||
end | |||
end | |||
table.sort(out) | |||
return out | |||
end | |||
p.loadEntityData = dp and dp.loadEntityData or nil | |||
p.collectEntityComponents = dp and dp.collectEntityComponents or nil | |||
p.loadEntityComponents = load_entity_components | |||
p.entityHasComponent = function(entityOrId, compName) | |||
if not compName or compName == "" then | |||
return false | |||
end | |||
local entity | |||
if type(entityOrId) == "string" then | |||
entity = dp and dp.loadEntityData(entityOrId) | |||
if not entity then | |||
return false | |||
end | |||
else | |||
entity = entityOrId | |||
end | |||
if dp then | |||
return dp.entityHasComponent(entity, compName) | |||
end | |||
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 | end | ||
| Строка 100: | Строка 167: | ||
end | end | ||
local function make_source(kind, name | local function make_source(kind, name, tplPath) | ||
return { kind = kind, name = name | return { kind = kind, name = name, tplPath = tplPath } | ||
end | end | ||
| Строка 302: | Строка 369: | ||
end | end | ||
return "{{ | return "{{карточка/сущность|" .. table.concat(parts, "|") .. "}}" | ||
end | end | ||
| Строка 598: | Строка 665: | ||
end | end | ||
local function collect_entity_sets(id | local function collect_entity_sets(id, prototypeStoreDefs, componentWhitelist, componentBlacklist, prototypeWhitelist, prototypeBlacklist) | ||
local foundComponents, foundPrototypes = {}, {} | local foundComponents, foundPrototypes = {}, {} | ||
local compList = | local compList = load_entity_components(id) | ||
if type(compList) == "table" then | if type(compList) == "table" then | ||
for _, v in ipairs(compList) do | for _, v in ipairs(compList) do | ||
if type(v) == "string" then | if type(v) == "string" and v ~= "" then | ||
foundComponents[v] = true | foundComponents[v] = true | ||
end | end | ||
| Строка 611: | Строка 677: | ||
end | end | ||
local protoStore = prototypeStoreDefs and prototypeStoreDefs[id] | |||
if type(protoStore) == "table" then | |||
if | for protoName in pairs(protoStore) do | ||
if type(protoName) == "string" and protoName ~= "" then | |||
foundPrototypes[protoName] = true | |||
foundPrototypes[ | |||
end | end | ||
end | end | ||
end | end | ||
apply_entity_set_filters(foundComponents, parse_csv_set(componentWhitelist), parse_csv_set(componentBlacklist)) | apply_entity_set_filters(foundComponents, parse_csv_set(componentWhitelist), parse_csv_set(componentBlacklist)) | ||
| Строка 749: | Строка 814: | ||
local prototypeBlacklist = frame.args.prototypeBlacklist or frame.args.prototypeblacklist or "" | local prototypeBlacklist = frame.args.prototypeBlacklist or frame.args.prototypeblacklist or "" | ||
local prototypeStoreDefs = load_cached("prototype_store.json") | |||
local prototypeStoreDefs = | if not prototypeStoreDefs then | ||
if | |||
return false | return false | ||
end | end | ||
local foundComponents, foundPrototypes = collect_entity_sets(id | local foundComponents, foundPrototypes = collect_entity_sets(id, prototypeStoreDefs, | ||
componentWhitelist, componentBlacklist, prototypeWhitelist, prototypeBlacklist) | componentWhitelist, componentBlacklist, prototypeWhitelist, prototypeBlacklist) | ||
| Строка 770: | Строка 834: | ||
local function processEntity(kind, name, isStore) | local function processEntity(kind, name, isStore) | ||
local | local tplPath = ucfirst(kind) .. "/" .. name | ||
if isStore then | if isStore then | ||
tplPath = tplPath .. "/store" | tplPath = tplPath .. "/store" | ||
| Строка 804: | Строка 867: | ||
id = id, | id = id, | ||
extra = extra, | extra = extra, | ||
source = make_source(kind, name | source = make_source(kind, name, tplPath), | ||
priority = resolve_priority(parsed) | priority = resolve_priority(parsed) | ||
}) | }) | ||
| Строка 820: | Строка 883: | ||
end | end | ||
local componentStoreDefs = | local componentStoreDefs = load_cached("component_store.json") | ||
if type(componentStoreDefs) == "table" and (not anyEntityWhitelist or compHasWhitelist) then | if type(componentStoreDefs) == "table" and (not anyEntityWhitelist or compHasWhitelist) then | ||
local compStore = componentStoreDefs[id] | local compStore = componentStoreDefs[id] | ||
| Строка 1010: | Строка 1073: | ||
id = "", | id = "", | ||
extra = "", | extra = "", | ||
source = make_source("" | source = make_source("", tplPath, tplPath), | ||
priority = 1 | priority = 1 | ||
}, nil, true) | }, nil, true) | ||