migrate all caravan data into mod-data#597
Conversation
PennyJim
left a comment
There was a problem hiding this comment.
Because ModData felt simple enough to judge the merits of, I decided to take a glance at it.
Besides side notes I have about how things are done that I can't be confident in with my lack of experience with pY code. I mostly think there NEEDS to be data validation on what mods try and pass through the ModData.
People will make mistakes in their code. You want to catch it as early as you can so it can't lie in wait until the worst moment to cause a crash or god forbid silently corrupt the contents of storage until its too late.
| -- Create a helper function to safely fetch the data on-demand | ||
| function P.Get_caravan_prototypes() | ||
| local caravan_prototypes = prototypes.mod_data["pyanadons"].data["caravan_prototypes"] | ||
| if not caravan_prototypes then | ||
|
|
||
| return nil | ||
| end | ||
| return caravan_prototypes | ||
| end | ||
|
|
||
| function P.Get_caravan_data() | ||
| local caravan_prototypes = prototypes.mod_data["pyanadons"].data["caravan_data"] | ||
| if not caravan_prototypes then | ||
| game.print("no caravan data!") | ||
| return nil | ||
| end | ||
| return caravan_prototypes | ||
| end | ||
|
|
There was a problem hiding this comment.
prototypes is available in "main chunk" of runtime
This means instead of replacing every instance of the object caravan_prototypes with a function call
You can just instead have the old file that returns caravan_prototypes do this
return prototypes.mod_data["pyanadons"].data["caravan_prototypes"]But rather than passing it directly, I'd dedicate that file to data integrity checking
You can trust internal stuff to be right, and it's your fault if you're wrong
But you CANNOT trust outside sources.
| caravan_prototypes["caravan-turd"] = caravan_prototypes["caravan"] | ||
| caravan_prototypes["fluidavan-turd"] = caravan_prototypes["fluidavan"] | ||
| caravan_prototypes["flyavan-turd"] = caravan_prototypes["flyavan"] | ||
| caravan_prototypes["nukavan-turd"] = caravan_prototypes["nukavan"] | ||
| caravan_prototypes["fluidflyavan-turd"] = caravan_prototypes["fluidflyavan"] | ||
| caravan_prototypes["caravan-turd"].placeable_by = "caravan-turd" | ||
| caravan_prototypes["fluidavan-turd"].placeable_by = "fluidavan-turd" | ||
| caravan_prototypes["flyavan-turd"].placeable_by = "flyavan-turd" | ||
| caravan_prototypes["nukavan-turd"].placeable_by = "nukavan-turd" | ||
| caravan_prototypes["fluidflyavan-turd"].placeable_by = "fluidflyavan" |
There was a problem hiding this comment.
Based on the removed section over here:
https://github.com/pyanodon/pyalienlife/pull/597/changes#diff-0e0ac10ae6e1c14e8d1978c87135ef30db3fe5a90b485d8a9a35c20a3c871eb6L385-L394
This seems like not your fault. Regardless, this looks incredibly wrong to me
Tables are passed by reference, so when you're modifying caravan_prototypes["caravan-turd"] you should also be modifying caravan_prototypes["caravan"]
You'll want to wrap the initial assignments with something like util.copy
caravan_prototypes["caravan-turd"] = util.copy(caravan_prototypes["caravan"])
caravan_prototypes["fluidavan-turd"] = util.copy(caravan_prototypes["fluidavan"])
caravan_prototypes["flyavan-turd"] = util.copy(caravan_prototypes["flyavan"])
caravan_prototypes["nukavan-turd"] = util.copy(caravan_prototypes["nukavan"])
caravan_prototypes["fluidflyavan-turd"] = util.copy(caravan_prototypes["fluidflyavan"])| Caravan.all_actions = { | ||
| ["outpost"] = table.invert { | ||
| "time-passed", | ||
| "store-food", | ||
| "store-specific-food", | ||
| "fill-inventory", | ||
| "empty-inventory", | ||
| "load-caravan", | ||
| "unload-caravan", | ||
| "load-target", | ||
| "unload-target", | ||
| "circuit-condition", | ||
| "circuit-condition-static" | ||
| }, | ||
| ["outpost-fluid"] = table.invert{ | ||
| "time-passed", | ||
| "fill-tank", | ||
| "empty-tank", | ||
| "circuit-condition", | ||
| "circuit-condition-static", | ||
| "fill-tank-until-caravan-has", | ||
| "fill-tank-until-target-has", | ||
| "empty-tank-until-caravan-has", | ||
| "empty-tank-until-target-has", | ||
| }, | ||
| ["character"] = table.invert { | ||
| "time-passed", | ||
| "store-food", | ||
| "store-specific-food", | ||
| "fill-inventory", | ||
| "empty-inventory", | ||
| "load-caravan", | ||
| "unload-caravan", | ||
| "load-target", | ||
| "unload-target", | ||
| "empty-autotrash" | ||
| }, | ||
| ["unit"] = table.invert { | ||
| "time-passed", | ||
| "store-food", | ||
| "store-specific-food", | ||
| "fill-inventory", | ||
| "empty-inventory", | ||
| "load-caravan", | ||
| "unload-caravan", | ||
| "load-target", | ||
| "unload-target", | ||
| }, | ||
| ["cargo-wagon"] = table.invert { | ||
| "time-passed", | ||
| "fill-inventory", | ||
| "empty-inventory", | ||
| "load-caravan", | ||
| "unload-caravan", | ||
| "load-target", | ||
| "unload-target", | ||
| }, | ||
| ["car"] = table.invert { | ||
| "time-passed", | ||
| "fill-inventory", | ||
| "empty-inventory", | ||
| "load-caravan", | ||
| "unload-caravan", | ||
| "load-target", | ||
| "unload-target", | ||
| }, | ||
| ["spider-vehicle"] = table.invert { | ||
| "time-passed", | ||
| "fill-inventory", | ||
| "empty-inventory", | ||
| "load-caravan", | ||
| "unload-caravan", | ||
| "load-target", | ||
| "unload-target", | ||
| }, | ||
| ["electric-pole"] = table.invert { | ||
| "time-passed", | ||
| "circuit-condition", | ||
| "circuit-condition-static" | ||
| }, | ||
| ["default"] = table.invert { | ||
| "time-passed" | ||
| } | ||
| } |
There was a problem hiding this comment.
Take this comment as someone who hasn't seen any of the actual functional code.
But this looks like it should be static to me. Something listed as all_actions looks like a source of truth, rather than something meant to be modified.
I mean this as in it probably shouldn't be within the ModData, and instead just next to where the mod data is fetched in runtime.
Someone more knowledgeable about this system can confirm or deny this
| local prototype = Utils.Get_caravan_prototypes()[entity.name] | ||
| local protos = Utils.Get_caravan_prototypes() | ||
| for k,_ in pairs(protos) do | ||
| game.print(k) | ||
| end | ||
| game.print(entity.name) | ||
| game.print(serpent.block(prototype)) |
There was a problem hiding this comment.
This looks like debug code to me. While I don't know the consensus of the community on debug code making it into the repository, I'd personally always wrap it in a debugger check to prevent it from ever accidentally spamming users.
if debugadapter then
-- Debug code
endBut what really caught my eye about this, is the fact that you do
local prototype = Utils.Get_caravan_prototypes()[entity.name]
local protos = Utils.Get_caravan_prototypes()Seeing a local of protos made right after the same thing was fetch made me sweat before I realized it part of the debug code and likely to be ripped out anyways.
| local P = {} | ||
|
|
||
| -- Create a helper function to safely fetch the data on-demand | ||
| function P.Get_caravan_prototypes() |
There was a problem hiding this comment.
Why is the function capitalized? The other functions in utils aren't.
|
I'll be writing up a mod-data package to the standards and specifications of the rest of the codebase. Thank you for your efforts, but please wait for an official implementation. |
Dirty but working atm
Example of 3rd party mod code to add caravan:
user would need to create a prototype def according to how it is in AL
(need the item, recipe entity also )
Still need to deal with the fluidvan name regex matches for the gui builders