z_mirror/flib
git clone https://git.raiguard.me/z_mirror/flib
046392c
trunk
1--- Provides utilities for locating and iterating prototypes in `data.raw`. 2--- @class flib_prototypes 3local flib_prototypes ={} 4 5--- Returns a list of all prototypes with the given `base_type`. 6--- @overload fun(base_type: "achievement"): data.AchievementPrototype[] 7--- @overload fun(base_type: "active-trigger"): data.ActiveTriggerPrototype[] 8--- @overload fun(base_type: "airborne-pollutant"): data.AirbornePollutantPrototype[] 9--- @overload fun(base_type: "ambient-sound"): data.AmbientSound[] 10--- @overload fun(base_type: "ammo-category"): data.AmmoCategory[] 11--- @overload fun(base_type: "animation"): data.AnimationPrototype[] 12--- @overload fun(base_type: "asteroid-chunk"): data.AsteroidChunkPrototype[] 13--- @overload fun(base_type: "autoplace-control"): data.AutoplaceSpecification[] 14--- @overload fun(base_type: "burner-usage"): data.BurnerUsagePrototype[] 15--- @overload fun(base_type: "collision-layer"): data.CollisionLayerPrototype[] 16--- @overload fun(base_type: "custom-event"): data.CustomEventPrototype[] 17--- @overload fun(base_type: "custom-input"): data.CustomInputPrototype[] 18--- @overload fun(base_type: "damage-type"): data.DamageType[] 19--- @overload fun(base_type: "decorative"): data.DecorativePrototype[] 20--- @overload fun(base_type: "deliver-category"): data.DeliverCategory[] 21--- @overload fun(base_type: "deliver-impact-combination"): data.DeliverImpactCombination[] 22--- @overload fun(base_type: "editor-controller"): data.EditorControllerPrototype[] 23--- @overload fun(base_type: "entity"): data.EntityPrototype[] 24--- @overload fun(base_type: "equipment"): data.EquipmentPrototype[] 25--- @overload fun(base_type: "equipment-category"): data.EquipmentCategory[] 26--- @overload fun(base_type: "equipment-grid"): data.EquipmentGridPrototype[] 27--- @overload fun(base_type: "fluid"): data.FluidPrototype[] 28--- @overload fun(base_type: "font"): data.FontPrototype[] 29--- @overload fun(base_type: "fuel-category"): data.FuelCategory[] 30--- @overload fun(base_type: "god-controller"): data.GodControllerPrototype[] 31--- @overload fun(base_type: "gui-style"): data.GuiStyle[] 32--- @overload fun(base_type: "impact-category"): data.ImpactCategory[] 33--- @overload fun(base_type: "item"): data.ItemPrototype[] 34--- @overload fun(base_type: "item-group"): data.ItemGroup[] 35--- @overload fun(base_type: "item-subgroup"): data.ItemSubGroup[] 36--- @overload fun(base_type: "map-gen-presets"): data.MapGenPresets[] 37--- @overload fun(base_type: "map-settings"): data.MapSettings[] 38--- @overload fun(base_type: "module-category"): data.ModuleCategory[] 39--- @overload fun(base_type: "mouse-cursor"): data.MouseCursor[] 40--- @overload fun(base_type: "noise-expression"): data.NamedNoiseExpression[] 41--- @overload fun(base_type: "noise-function"): data.NamedNoiseFunction[] 42--- @overload fun(base_type: "particle"): data.ParticlePrototype[] 43--- @overload fun(base_type: "procession"): data.ProcessionPrototype[] 44--- @overload fun(base_type: "procession-layer-inheritance-group"): data.ProcessionLayerInheritanceGroup[] 45--- @overload fun(base_type: "quality"): data.QualityPrototype[] 46--- @overload fun(base_type: "recipe"): data.RecipePrototype[] 47--- @overload fun(base_type: "recipe-category"): data.RecipeCategory[] 48--- @overload fun(base_type: "remote-controller"): data.RemoteControllerPrototype[] 49--- @overload fun(base_type: "resource-category"): data.ResourceCategory[] 50--- @overload fun(base_type: "shortcut"): data.ShortcutPrototype[] 51--- @overload fun(base_type: "sound"): data.SoundPrototype[] 52--- @overload fun(base_type: "space-connection"): data.SpaceConnectionPrototype[] 53--- @overload fun(base_type: "space-location"): data.SpaceLocationPrototype[] 54--- @overload fun(base_type: "spectator-controller"): data.SpectatorControllerPrototype[] 55--- @overload fun(base_type: "sprite"): data.SpritePrototype[] 56--- @overload fun(base_type: "surface"): data.SurfacePrototype[] 57--- @overload fun(base_type: "surface-property"): data.SurfacePropertyPrototype[] 58--- @overload fun(base_type: "technology"): data.TechnologyPrototype[] 59--- @overload fun(base_type: "tile"): data.TilePrototype[] 60--- @overload fun(base_type: "tile-effect"): data.TileEffectDefinition[] 61--- @overload fun(base_type: "tips-and-tricks-item"): data.TipsAndTricksItem[] 62--- @overload fun(base_type: "tips-and-tricks-item-category"): data.TipsAndTricksItemCategory[] 63--- @overload fun(base_type: "trigger-target-type"): data.TriggerTargetType[] 64--- @overload fun(base_type: "trivial-smoke"): data.TrivialSmokePrototype[] 65--- @overload fun(base_type: "tutorial"): data.TutorialDefinition[] 66--- @overload fun(base_type: "utility-constants"): data.UtilityConstants[] 67--- @overload fun(base_type: "utility-sounds"): data.UtilitySounds[] 68--- @overload fun(base_type: "utility-sprites"): data.UtilitySounds[] 69--- @overload fun(base_type: "virtual-signal"): data.VirtualSignalPrototype[] 70function flib_prototypes . all ( base_type ) 71if not base_type then 72error ( "Did not provide a base_type" ) 73end 74if type ( base_type ) ~= "string" then 75error ( "base_type must be a string" ) 76end 77if not defines . prototypes [ base_type ] then 78error ( "'" .. base_type .. "' is not a valid base prototype type" ) 79end 80 81local result ={} 82local result_len =0 83for prototype_type in pairs ( defines . prototypes [ base_type ]) do 84for _ , prototype in pairs ( data . raw [ prototype_type ] or {} ) do 85result_len =result_len + 1 86result [ result_len ] =prototype 87end 88end 89return result 90end 91 92--- Returns the prototype with the given `base_type` and `name`, if it exists. 93--- @overload fun(base_type: "achievement", name: string): data.AchievementPrototype? 94--- @overload fun(base_type: "active-trigger", name: string): data.ActiveTriggerPrototype? 95--- @overload fun(base_type: "airborne-pollutant", name: string): data.AirbornePollutantPrototype? 96--- @overload fun(base_type: "ambient-sound", name: string): data.AmbientSound? 97--- @overload fun(base_type: "ammo-category", name: string): data.AmmoCategory? 98--- @overload fun(base_type: "animation", name: string): data.AnimationPrototype? 99--- @overload fun(base_type: "asteroid-chunk", name: string): data.AsteroidChunkPrototype? 100--- @overload fun(base_type: "autoplace-control", name: string): data.AutoplaceSpecification? 101--- @overload fun(base_type: "burner-usage", name: string): data.BurnerUsagePrototype? 102--- @overload fun(base_type: "collision-layer", name: string): data.CollisionLayerPrototype? 103--- @overload fun(base_type: "custom-event", name: string): data.CustomEventPrototype? 104--- @overload fun(base_type: "custom-input", name: string): data.CustomInputPrototype? 105--- @overload fun(base_type: "damage-type", name: string): data.DamageType? 106--- @overload fun(base_type: "decorative", name: string): data.DecorativePrototype? 107--- @overload fun(base_type: "deliver-category", name: string): data.DeliverCategory? 108--- @overload fun(base_type: "deliver-impact-combination", name: string): data.DeliverImpactCombination? 109--- @overload fun(base_type: "editor-controller", name: string): data.EditorControllerPrototype? 110--- @overload fun(base_type: "entity", name: string): data.EntityPrototype? 111--- @overload fun(base_type: "equipment", name: string): data.EquipmentPrototype? 112--- @overload fun(base_type: "equipment-category", name: string): data.EquipmentCategory? 113--- @overload fun(base_type: "equipment-grid", name: string): data.EquipmentGridPrototype? 114--- @overload fun(base_type: "fluid", name: string): data.FluidPrototype? 115--- @overload fun(base_type: "font", name: string): data.FontPrototype? 116--- @overload fun(base_type: "fuel-category", name: string): data.FuelCategory? 117--- @overload fun(base_type: "god-controller", name: string): data.GodControllerPrototype? 118--- @overload fun(base_type: "gui-style", name: string): data.GuiStyle? 119--- @overload fun(base_type: "impact-category", name: string): data.ImpactCategory? 120--- @overload fun(base_type: "item", name: string): data.ItemPrototype? 121--- @overload fun(base_type: "item-group", name: string): data.ItemGroup? 122--- @overload fun(base_type: "item-subgroup", name: string): data.ItemSubGroup? 123--- @overload fun(base_type: "map-gen-presets", name: string): data.MapGenPresets? 124--- @overload fun(base_type: "map-settings", name: string): data.MapSettings? 125--- @overload fun(base_type: "module-category", name: string): data.ModuleCategory? 126--- @overload fun(base_type: "mouse-cursor", name: string): data.MouseCursor? 127--- @overload fun(base_type: "noise-expression", name: string): data.NamedNoiseExpression? 128--- @overload fun(base_type: "noise-function", name: string): data.NamedNoiseFunction? 129--- @overload fun(base_type: "particle", name: string): data.ParticlePrototype? 130--- @overload fun(base_type: "procession", name: string): data.ProcessionPrototype? 131--- @overload fun(base_type: "procession-layer-inheritance-group", name: string): data.ProcessionLayerInheritanceGroup? 132--- @overload fun(base_type: "quality", name: string): data.QualityPrototype? 133--- @overload fun(base_type: "recipe", name: string): data.RecipePrototype? 134--- @overload fun(base_type: "recipe-category", name: string): data.RecipeCategory? 135--- @overload fun(base_type: "remote-controller", name: string): data.RemoteControllerPrototype? 136--- @overload fun(base_type: "resource-category", name: string): data.ResourceCategory? 137--- @overload fun(base_type: "shortcut", name: string): data.ShortcutPrototype? 138--- @overload fun(base_type: "sound", name: string): data.SoundPrototype? 139--- @overload fun(base_type: "space-connection", name: string): data.SpaceConnectionPrototype? 140--- @overload fun(base_type: "space-location", name: string): data.SpaceLocationPrototype? 141--- @overload fun(base_type: "spectator-controller", name: string): data.SpectatorControllerPrototype? 142--- @overload fun(base_type: "sprite", name: string): data.SpritePrototype? 143--- @overload fun(base_type: "surface", name: string): data.SurfacePrototype? 144--- @overload fun(base_type: "surface-property", name: string): data.SurfacePropertyPrototype? 145--- @overload fun(base_type: "technology", name: string): data.TechnologyPrototype? 146--- @overload fun(base_type: "tile", name: string): data.TilePrototype? 147--- @overload fun(base_type: "tile-effect", name: string): data.TileEffectDefinition? 148--- @overload fun(base_type: "tips-and-tricks-item", name: string): data.TipsAndTricksItem? 149--- @overload fun(base_type: "tips-and-tricks-item-category", name: string): data.TipsAndTricksItemCategory? 150--- @overload fun(base_type: "trigger-target-type", name: string): data.TriggerTargetType? 151--- @overload fun(base_type: "trivial-smoke", name: string): data.TrivialSmokePrototype? 152--- @overload fun(base_type: "tutorial", name: string): data.TutorialDefinition? 153--- @overload fun(base_type: "utility-constants", name: string): data.UtilityConstants? 154--- @overload fun(base_type: "utility-sounds", name: string): data.UtilitySounds? 155--- @overload fun(base_type: "utility-sprites", name: string): data.UtilitySounds? 156--- @overload fun(base_type: "virtual-signal", name: string): data.VirtualSignalPrototype? 157function flib_prototypes . find ( base_type , name ) 158if not base_type then 159error ( "Did not provide a base_type" ) 160end 161if type ( base_type ) ~= "string" then 162error ( "base_type must be a string" ) 163end 164if base_type == "" then 165error ( "base_type must be a non-empty string" ) 166end 167if not defines . prototypes [ base_type ] then 168error ( "'" .. base_type .. "' is not a valid base prototype type" ) 169end 170 171if not name then 172error ( "Did not provide a name" ) 173end 174if type ( name ) ~= "string" then 175error ( "name must be a string" ) 176end 177if name == "" then 178error ( "name must be a non-empty string" ) 179end 180 181for derived_type in pairs ( defines . prototypes [ base_type ]) do 182local prototypes =data . raw [ derived_type ] 183if prototypes then 184local prototype =prototypes [ name ] 185if prototype then 186return prototype 187end 188end 189end 190end 191 192--- Returns the prototype with the given `base_type` and `name`, throwing an error if it doesn't exist. 193--- @overload fun(base_type: "achievement", name: string): data.AchievementPrototype 194--- @overload fun(base_type: "active-trigger", name: string): data.ActiveTriggerPrototype 195--- @overload fun(base_type: "airborne-pollutant", name: string): data.AirbornePollutantPrototype 196--- @overload fun(base_type: "ambient-sound", name: string): data.AmbientSound 197--- @overload fun(base_type: "ammo-category", name: string): data.AmmoCategory 198--- @overload fun(base_type: "animation", name: string): data.AnimationPrototype 199--- @overload fun(base_type: "asteroid-chunk", name: string): data.AsteroidChunkPrototype 200--- @overload fun(base_type: "autoplace-control", name: string): data.AutoplaceSpecification 201--- @overload fun(base_type: "burner-usage", name: string): data.BurnerUsagePrototype 202--- @overload fun(base_type: "collision-layer", name: string): data.CollisionLayerPrototype 203--- @overload fun(base_type: "custom-event", name: string): data.CustomEventPrototype 204--- @overload fun(base_type: "custom-input", name: string): data.CustomInputPrototype 205--- @overload fun(base_type: "damage-type", name: string): data.DamageType 206--- @overload fun(base_type: "decorative", name: string): data.DecorativePrototype 207--- @overload fun(base_type: "deliver-category", name: string): data.DeliverCategory 208--- @overload fun(base_type: "deliver-impact-combination", name: string): data.DeliverImpactCombination 209--- @overload fun(base_type: "editor-controller", name: string): data.EditorControllerPrototype 210--- @overload fun(base_type: "entity", name: string): data.EntityPrototype 211--- @overload fun(base_type: "equipment", name: string): data.EquipmentPrototype 212--- @overload fun(base_type: "equipment-category", name: string): data.EquipmentCategory 213--- @overload fun(base_type: "equipment-grid", name: string): data.EquipmentGridPrototype 214--- @overload fun(base_type: "fluid", name: string): data.FluidPrototype 215--- @overload fun(base_type: "font", name: string): data.FontPrototype 216--- @overload fun(base_type: "fuel-category", name: string): data.FuelCategory 217--- @overload fun(base_type: "god-controller", name: string): data.GodControllerPrototype 218--- @overload fun(base_type: "gui-style", name: string): data.GuiStyle 219--- @overload fun(base_type: "impact-category", name: string): data.ImpactCategory 220--- @overload fun(base_type: "item", name: string): data.ItemPrototype 221--- @overload fun(base_type: "item-group", name: string): data.ItemGroup 222--- @overload fun(base_type: "item-subgroup", name: string): data.ItemSubGroup 223--- @overload fun(base_type: "map-gen-presets", name: string): data.MapGenPresets 224--- @overload fun(base_type: "map-settings", name: string): data.MapSettings 225--- @overload fun(base_type: "module-category", name: string): data.ModuleCategory 226--- @overload fun(base_type: "mouse-cursor", name: string): data.MouseCursor 227--- @overload fun(base_type: "noise-expression", name: string): data.NamedNoiseExpression 228--- @overload fun(base_type: "noise-function", name: string): data.NamedNoiseFunction 229--- @overload fun(base_type: "particle", name: string): data.ParticlePrototype 230--- @overload fun(base_type: "procession", name: string): data.ProcessionPrototype 231--- @overload fun(base_type: "procession-layer-inheritance-group", name: string): data.ProcessionLayerInheritanceGroup 232--- @overload fun(base_type: "quality", name: string): data.QualityPrototype 233--- @overload fun(base_type: "recipe", name: string): data.RecipePrototype 234--- @overload fun(base_type: "recipe-category", name: string): data.RecipeCategory 235--- @overload fun(base_type: "remote-controller", name: string): data.RemoteControllerPrototype 236--- @overload fun(base_type: "resource-category", name: string): data.ResourceCategory 237--- @overload fun(base_type: "shortcut", name: string): data.ShortcutPrototype 238--- @overload fun(base_type: "sound", name: string): data.SoundPrototype 239--- @overload fun(base_type: "space-connection", name: string): data.SpaceConnectionPrototype 240--- @overload fun(base_type: "space-location", name: string): data.SpaceLocationPrototype 241--- @overload fun(base_type: "spectator-controller", name: string): data.SpectatorControllerPrototype 242--- @overload fun(base_type: "sprite", name: string): data.SpritePrototype 243--- @overload fun(base_type: "surface", name: string): data.SurfacePrototype 244--- @overload fun(base_type: "surface-property", name: string): data.SurfacePropertyPrototype 245--- @overload fun(base_type: "technology", name: string): data.TechnologyPrototype 246--- @overload fun(base_type: "tile", name: string): data.TilePrototype 247--- @overload fun(base_type: "tile-effect", name: string): data.TileEffectDefinition 248--- @overload fun(base_type: "tips-and-tricks-item", name: string): data.TipsAndTricksItem 249--- @overload fun(base_type: "tips-and-tricks-item-category", name: string): data.TipsAndTricksItemCategory 250--- @overload fun(base_type: "trigger-target-type", name: string): data.TriggerTargetType 251--- @overload fun(base_type: "trivial-smoke", name: string): data.TrivialSmokePrototype 252--- @overload fun(base_type: "tutorial", name: string): data.TutorialDefinition 253--- @overload fun(base_type: "utility-constants", name: string): data.UtilityConstants 254--- @overload fun(base_type: "utility-sounds", name: string): data.UtilitySounds 255--- @overload fun(base_type: "utility-sprites", name: string): data.UtilitySounds 256--- @overload fun(base_type: "virtual-signal", name: string): data.VirtualSignalPrototype 257function flib_prototypes . get ( base_type , name ) 258local result =flib_prototypes . find ( base_type , name ) 259if not result then 260error ( "Prototype '" .. base_type .. "/" .. name .. "' does not exist." ) 261end 262return result 263end 264 265--- @type table<string, string?> 266local base_type_lookup ={} 267for base_type , derived_types in pairs ( defines . prototypes ) do 268for derived_type in pairs ( derived_types ) do 269base_type_lookup [ derived_type ] =base_type 270end 271end 272 273--- Returns the base type of the given prototype type. 274--- @param derived_type string 275--- @return string 276function flib_prototypes . get_base_type ( derived_type ) 277local base_type =base_type_lookup [ derived_type ] 278if not base_type then 279error ( "'" .. derived_type .. "' is not a valid prototype type." ) 280end 281return base_type 282end 283 284return flib_prototypes