summaryrefslogtreecommitdiff
path: root/BehaviorPacks.lua
diff options
context:
space:
mode:
Diffstat (limited to 'BehaviorPacks.lua')
-rw-r--r--BehaviorPacks.lua26
1 files changed, 26 insertions, 0 deletions
diff --git a/BehaviorPacks.lua b/BehaviorPacks.lua
new file mode 100644
index 0000000..ba6fac5
--- /dev/null
+++ b/BehaviorPacks.lua
@@ -0,0 +1,26 @@
+local GameMod = require "necro.game.data.resource.GameMod"
+local FileIO = require "system.game.FileIO"
+local JSON = require "system.utils.serial.JSON"
+local Utilities = require "system.utils.Utilities"
+
+event.entitySchemaLoadEntity.add("useModData", { order = "overrides", sequence = 1 }, function(ev)
+ for _, mod in pairs(GameMod.listLoadedAssetMods()) do
+ local modPath = GameMod.getAssetModPath(mod)
+ -- only files with certain extensions are loaded as resources, and only
+ -- files loaded as resources can be accessed with FileIO
+ local schemaFile = modPath .. "/schema/" .. ev.entity.name .. ".txt"
+
+ if FileIO.exists(schemaFile) then
+ local data = JSON.decode(FileIO.readFileToString(schemaFile))
+ -- Most EntitySchema events define ev.merge, but apparently entitySchemaLoadEntity
+ -- does not.
+ --
+ -- TODO: Sometimes one may want to merge recursively, such as to edit just some
+ -- fields of a component, but other times, like when editing animations, it may
+ -- be desired to overwrite values (the regular mergeTables may be sufficient).
+ -- Introducing more complex syntax (possibly but not necessarily expressed in
+ -- JSON) could enable dictating these differing needs.
+ Utilities.mergeTables(ev.entity, data)
+ end
+ end
+end)