Post data editing during loading.

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
User avatar
rk84
Filter Inserter
Filter Inserter
Posts: 556
Joined: Wed Feb 13, 2013 9:15 am
Contact:

Post data editing during loading.

Post by rk84 »

Possible sollution to those that want to edit new extensions to data.raw after mod has executed it's own data.lua.
From mod's prespective there data that is loaded before (pre-data) and data that is loaded after mod is done (post-data). Sometime ago I was puzzled by mod loading and I was thinking how influence in mod loading order. I decided to ignore loading order, do pre-data edits normally and change dataloader to handle possible post-data edits. There is example with key parts below, that I have tried successfully. :)

Possible risks:
  • Calling data.extend in procedure
  • Adding your procedure first and using data.extend after for pre-data edits.
Possible improvements:
  • metamethods to catch entries to data.raw?

Code: Select all

-- data.lua
...
require("styles.icons") -- Extending/editing prototype library the normal way

require("postprocessor") -- Overwrites part of dataloader(If not already called)

require("styles.posticons") -- Sets procedure for future loaded prototypes
processor behind procedures

Code: Select all

-- postprocessor.lua
-- Mod defined loading procedures are stored in to this
procedures = {}

-- Overwriting data.extend to take use of procedures
function data.extend(self, otherdata)
  for _, e in ipairs(otherdata) do
    if not e.type or not e.name then
      error("Missing name or type in the following prototype definition " .. serpent.block(e))
    end
    local t = self.raw[e.type]
    if t == nil then
      t = {}
      self.raw[e.type] = t
    end
    procedures:checkPrototype(e)
    t[e.name] = e
  end
end

--For adding new procedure
function procedures.add(func)
  table.insert(procedures, func)
end

-- Utilize given procedures
function procedures.checkPrototype(self, prototype)
  for _, f in ipairs(self) do
    f(prototype)
  end
end
Adding new procedure

Code: Select all

-- styles.posticons.lua
-- check stack_size to find "item" kind of type
-- create icon style for item with icon

procedures.add(function(prototype)
  if prototype.stack_size and prototype.icon then
    local style =
    {
      type = "button_style",
      parent = "tm-icon-style",
      default_graphical_set =
      {
        type = "monolith",
        top_monolith_border = 1,
        right_monolith_border = 1,
        bottom_monolith_border = 1,
        left_monolith_border = 1,
        monolith_image =
        {
          filename = prototype.icon,
          width = 32,
          height = 32
        }
      }
    }
    data.raw["gui-style"].default["tm-icon-"..prototype.name] = style
  end
end)
Test mode
Searching Flashlight
[WIP]Fluid handling expansion
[WIP]PvP gamescript
[WIP]Rocket Express
Autofill: The torch has been pass to Nexela

Post Reply

Return to “Modding discussion”