Модуль:Сущность/data: различия между версиями

Нет описания правки
Нет описания правки
 
(не показаны 4 промежуточные версии этого же участника)
Строка 2: Строка 2:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local JsonPaths = require('Module:JsonPaths')
local JsonPaths = require('Module:JsonPaths')
local dp = require( "Module:GetField")


local dpOk, dpModule = pcall(require, "Module:GetField")
local function load_data_page(page)
local dp = dpOk and dpModule or nil
    local moduleName = JsonPaths.get(page)
    local ok, data = pcall(mw.loadData, moduleName)
    if not ok or type(data) ~= "table" then
        return nil
    end
    return data
end


local switchModeRegistry = {}
local switchModeRegistry = {}
Строка 24: Строка 31:
         end
         end
     end
     end
end
local function load_cached(page)
    if dp then
        return dp.loadCachedData(page)
    end
    local moduleName = JsonPaths.get(page)
    local ok, data = pcall(mw.loadData, moduleName)
    if not ok then
        return nil
    end
    return data
end
end


Строка 50: Строка 45:
end
end


local function load_entity_components(entityId)
function p.loadEntityComponents(entityId)
     if not entityId or entityId == "" then
     if not entityId or entityId == "" then
         return {}
         return {}
     end
     end
     if dp then
     local entity = dp.loadEntityData(entityId)
        local entity = dp.loadEntityData(entityId)
    if entity then
        if entity then
        return dp.collectEntityComponents(entity)
            return dp.collectEntityComponents(entity)
        end
     end
     end
     local page = "prototype/Entity/" .. entityId .. ".json"
     return {}
    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
end


p.loadEntityData = dp and dp.loadEntityData or nil
function p.entityHasComponent(entityOrId, compName)
p.collectEntityComponents = dp and dp.collectEntityComponents or nil
p.loadEntityComponents = load_entity_components
p.entityHasComponent = function(entityOrId, compName)
     if not compName or compName == "" then
     if not compName or compName == "" then
         return false
         return false
     end
     end
     local entity
     local entity
     if type(entityOrId) == "string" then
     if type(entityOrId) == "string" then
         entity = dp and dp.loadEntityData(entityOrId)
         entity = dp and dp.loadEntityData(entityOrId) or load_data_page("prototype/Entity/" .. entityOrId .. ".json")
         if not entity then
         if not entity then
             return false
             return false
Строка 103: Строка 70:
         entity = entityOrId
         entity = entityOrId
     end
     end
     if dp then
 
        return dp.entityHasComponent(entity, compName)
     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


Строка 663: Строка 623:
     local foundComponents, foundPrototypes = {}, {}
     local foundComponents, foundPrototypes = {}, {}


     local compList = load_entity_components(id)
     local compList = p.loadEntityComponents(id)
     if type(compList) == "table" then
     if type(compList) == "table" then
         for _, v in ipairs(compList) do
         for _, v in ipairs(compList) do
Строка 809: Строка 769:
     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 = dp.loadCachedData("prototype_store.json")
     if not prototypeStoreDefs then
     if not prototypeStoreDefs then
         return false
         return false
Строка 829: Строка 789:


local function processEntity(kind, name, isStore)
local function processEntity(kind, name, isStore)
local tplPath = kind .. "/" .. name
local tplPath = kind .. "/" .. dp.ucfirst(name)
if isStore then
if isStore then
tplPath = tplPath .. "/store"
tplPath = tplPath .. "/store"
Строка 854: Строка 814:
         local paramNames = get_template_params(tplPath, content)
         local paramNames = get_template_params(tplPath, content)
         if dp then
         if dp then
             local dataPage = tplPath .. ".json"
             local dataPage = kind .. "/" .. dp.ucfirst(name) .. ".json"
             extra = get_selective_extra(id, dataPage, paramNames)
             extra = get_selective_extra(id, dataPage, paramNames)
         end
         end
Строка 878: Строка 838:
     end
     end


     local componentStoreDefs = load_cached("component_store.json")
     local componentStoreDefs = dp.loadCachedData("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]