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

Нет описания правки
мНет описания правки
Строка 274: Строка 274:


     local componentDefs = load_module_data("component.json")
     local componentDefs = load_module_data("component.json")
     local prototypeDefs = load_module_data("prototype.json")
     local prototypeStoreDefs = load_module_data("prototype_store.json")
     if not componentDefs or not prototypeDefs then return "" end
    local componentStoreDefs = load_module_data("component_store.json") or {}
     if not componentDefs then return "" end
 
    local foundComponents = {}
    local foundComponentStores = {}
    local foundPrototypeStores = {}


    local foundComponents, foundPrototypes = {}, {}
     local compList = componentDefs[id]
     local compList = componentDefs[id]
     if type(compList) == "table" then
     if type(compList) == "table" then
Строка 287: Строка 291:
     end
     end


     local protoEntry = prototypeDefs[id]
     local compStoreEntry = componentStoreDefs[id]
    if type(protoEntry) == "table" then
    if type(compStoreEntry) == "table" then
        for protoName, _ in pairs(protoEntry) do
        for compName, entityList in pairs(compStoreEntry) do
            if type(protoName) == "string" then
            if type(compName) == "string" and type(entityList) == "table" and #entityList > 0 then
                foundPrototypes[protoName] = true
                foundComponentStores[compName] = entityList
            end
        end
    end
 
    if type(prototypeStoreDefs) == "table" then
        local protoEntry = prototypeStoreDefs[id]
        if type(protoEntry) == "table" then
            for protoName, entityList in pairs(protoEntry) do
                if type(protoName) == "string" and type(entityList) == "table" and #entityList > 0 then
                    foundPrototypeStores[protoName] = entityList
                end
             end
             end
         end
         end
Строка 301: Строка 316:
             if componentDefs[n] ~= nil then
             if componentDefs[n] ~= nil then
                 foundComponents[n] = true
                 foundComponents[n] = true
             elseif prototypeDefs[n] ~= nil then
             end
                 foundPrototypes[n] = true
            if componentStoreDefs[n] and type(componentStoreDefs[n]) == "table" then
                for compName, entityList in pairs(componentStoreDefs[n]) do
                    if type(compName) == "string" and type(entityList) == "table" and #entityList > 0 then
                        foundComponentStores[compName] = entityList
                    end
                end
            end
            if prototypeStoreDefs and prototypeStoreDefs[n] and type(prototypeStoreDefs[n]) == "table" then
                 for protoName, entityList in pairs(prototypeStoreDefs[n]) do
                    if type(protoName) == "string" and type(entityList) == "table" and #entityList > 0 then
                        foundPrototypeStores[protoName] = entityList
                    end
                end
             end
             end
         end
         end
Строка 310: Строка 337:
         for item in string.gmatch(ignoreComponents, "[^,]+") do
         for item in string.gmatch(ignoreComponents, "[^,]+") do
             local name = trim(item)
             local name = trim(item)
             if name ~= "" then foundComponents[name] = nil end
             if name ~= "" then
                foundComponents[name] = nil
                foundComponentStores[name] = nil
            end
         end
         end
     end
     end
Строка 316: Строка 346:
         for item in string.gmatch(ignorePrototypes, "[^,]+") do
         for item in string.gmatch(ignorePrototypes, "[^,]+") do
             local name = trim(item)
             local name = trim(item)
             if name ~= "" then foundPrototypes[name] = nil end
             if name ~= "" then foundPrototypeStores[name] = nil end
         end
         end
     end
     end
Строка 327: Строка 357:
     local ok, dp = pcall(require, "Module:GetField")
     local ok, dp = pcall(require, "Module:GetField")
     local errors = {}
     local errors = {}
     local function processEntity(kind, name)
 
     local function processEntity(kind, name, isStore, storeEntities)
         local pathName = lcfirst(name)
         local pathName = lcfirst(name)
         local tplPath = kind .. "/" .. pathName
         local tplPath = kind .. "/" .. pathName .. (isStore and "/store" or "")
         local content = load_template_content(tplPath)
         local content = load_template_content(tplPath)
         if not content then
         if not content then
Строка 343: Строка 374:
         end
         end
         local parsed = getTemplateMeta(frame, tplPath)
         local parsed = getTemplateMeta(frame, tplPath)
         for _, sw in ipairs(switches) do
        local dataPage = (kind .. "/" .. pathName) .. ".json"
        local entities = isStore and storeEntities or { id }
        local switchesToProcess = isStore and { "card" } or switches
 
         for _, sw in ipairs(switchesToProcess) do
             local keys = parsed[sw] or {}
             local keys = parsed[sw] or {}
             for _, key in ipairs(keys) do
             for _, key in ipairs(keys) do
Строка 357: Строка 392:
                         table.insert(switchKeyOrder[sw], key)
                         table.insert(switchKeyOrder[sw], key)
                     end
                     end
                     local extra = ""
                     for _, entityId in ipairs(entities) do
                    if ok and dp and dp.flattenField then
                        local extra = ""
                        local dataPage = tplPath .. ".json"
                        if ok and dp and dp.flattenField then
                        extra = dp.flattenField({ args = { id, dataPage } })
                            extra = dp.flattenField({ args = { entityId, dataPage } }) or ""
                    end
                        end
                    local tplStr
                        local tplStr
                    local tplLabelStr, tplContentStr
                        local tplLabelStr, tplContentStr
                    if sw == "card" then
                        if sw == "card" then
                        tplLabelStr = makeTplCall(tplPath, "cardLabel", key, id, extra)
                            tplLabelStr = makeTplCall(tplPath, "cardLabel", key, entityId, extra)
                        tplContentStr = makeTplCall(tplPath, "cardContent", key, id, extra)
                            tplContentStr = makeTplCall(tplPath, "cardContent", key, entityId, extra)
                    else
                        else
                        tplStr = makeTplCall(tplPath, sw, key, id, extra)
                            tplStr = makeTplCall(tplPath, sw, key, entityId, extra)
                    end
                        end
                    local priority = 1
                        local priority = 1
                    if parsed and parsed.priority ~= nil then
                        if parsed and parsed.priority ~= nil then
                        if type(parsed.priority) == "number" then
                            if type(parsed.priority) == "number" then
                             priority = parsed.priority
                                priority = parsed.priority
                             else
                                local pnum = tonumber(parsed.priority)
                                if pnum then priority = pnum end
                            end
                        end
                        local entry
                        if sw == "card" then
                            entry = {
                                tplLabel = tplLabelStr,
                                tplContent = tplContentStr,
                                cardTag = parsed.cardTag,
                                source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
                                priority = priority,
                                idx = #switchKeyToTemplates[sw][key] + 1
                            }
                         else
                         else
                             local pnum = tonumber(parsed.priority)
                             entry = {
                             if pnum then priority = pnum end
                                tpl = tplStr,
                                source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
                                priority = priority,
                                idx = #switchKeyToTemplates[sw][key] + 1
                             }
                         end
                         end
                        table.insert(switchKeyToTemplates[sw][key], entry)
                     end
                     end
                    local entry
                    if sw == "card" then
                        entry = {
                            tplLabel = tplLabelStr,
                            tplContent = tplContentStr,
                            cardTag = parsed.cardTag,
                            source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
                            priority = priority,
                            idx = #switchKeyToTemplates[sw][key] + 1
                        }
                    else
                        entry = {
                            tpl = tplStr,
                            source = { kind = kind, name = name, pathName = pathName, tplPath = tplPath },
                            priority = priority,
                            idx = #switchKeyToTemplates[sw][key] + 1
                        }
                    end
                    table.insert(switchKeyToTemplates[sw][key], entry)
                 end
                 end
             end
             end
Строка 403: Строка 439:
     end
     end


    local items = {}
     for compName, _ in pairs(foundComponents) do
     for compName, _ in pairs(foundComponents) do table.insert(items, { kind = "component", name = compName }) end
        processEntity("component", compName, false)
     for protoName, _ in pairs(foundPrototypes) do table.insert(items, { kind = "prototype", name = protoName }) end
    end
     for _, it in ipairs(items) do processEntity(it.kind, it.name) end
     for compName, entityList in pairs(foundComponentStores) do
        processEntity("component", compName, true, entityList)
    end
     for protoName, entityList in pairs(foundPrototypeStores) do
        processEntity("prototype", protoName, true, entityList)
    end


     local out = {}
     local out = {}