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

Нет описания правки
Нет описания правки
Строка 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 function load_data_page(page)
    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 = {}
Строка 30: Строка 39:
         return dp.loadCachedData(page)
         return dp.loadCachedData(page)
     end
     end
     local moduleName = JsonPaths.get(page)
     return load_data_page(page)
    local ok, data = pcall(mw.loadData, moduleName)
    if not ok then
        return nil
    end
    return data
end
end


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


local function load_entity_components(entityId)
local function normalize_component_name(name)
    if type(name) ~= "string" or name == "" then
        return nil
    end
    if dp then
        return dp.normalizeComponentName(name)
    end
    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
 
function p.loadEntityComponents(entityId)
     if not entityId or entityId == "" then
     if not entityId or entityId == "" then
         return {}
         return {}
Строка 60: Строка 82:
         end
         end
     end
     end
     local page = "prototype/Entity/" .. entityId .. ".json"
     local data = load_data_page("prototype/Entity/" .. entityId .. ".json")
    local moduleName = JsonPaths.get(page)
     if type(data) ~= "table" or type(data.components) ~= "table" then
    local ok, data = pcall(mw.loadData, moduleName)
     if not ok or type(data) ~= "table" then
        return {}
    end
    if type(data.components) ~= "table" then
         return {}
         return {}
     end
     end
Строка 72: Строка 89:
     local seen = {}
     local seen = {}
     for k in pairs(data.components) do
     for k in pairs(data.components) do
         if type(k) == "string" then
         local name = normalize_component_name(k)
            local name = k
        if name and not seen[name] then
            if name:sub(1, 5) == "type:" then name = name:sub(6)
            seen[name] = true
            elseif name:sub(1, 6) == "!type:" then name = name:sub(7)
            out[#out + 1] = name
            end
            if name ~= "" and not seen[name] then
                seen[name] = true
                out[#out + 1] = name
            end
         end
         end
     end
     end
Строка 87: Строка 99:
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: Строка 113:
         entity = entityOrId
         entity = entityOrId
     end
     end
     if dp then
     if dp then
         return dp.entityHasComponent(entity, compName)
         return dp.entityHasComponent(entity, compName)
     end
     end
     if type(entity) ~= "table" or type(entity.components) ~= "table" then
     if type(entity) ~= "table" or type(entity.components) ~= "table" then
         return false
         return false
     end
     end
     return entity.components[compName] ~= nil
     return entity.components[compName] ~= nil
         or entity.components["type:" .. compName] ~= nil
         or entity.components["type:" .. compName] ~= nil
Строка 663: Строка 676:
     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