[EXAMPLE] Dynamic Loading of RSO Configs directly from the mods

Replaces resource spawning system, so that the distances between resources are much bigger. Railway is needed then.

Moderators: orzelek, Dark

AlienX
Fast Inserter
Fast Inserter
Posts: 105
Joined: Wed May 17, 2017 7:13 pm
Contact:

[EXAMPLE] Dynamic Loading of RSO Configs directly from the mods

Post by AlienX »

Hi,
While I love RSO, i find it a bit irritating that people have to badger you in order to add their configs for their new mods, however I have found a simple elegant solution to the problem.

Instead of people having to badger you to do it, why not make RSO dynamically call each mod, and request the configuration for their ores?

Well, that's what I have done and it works very well.

I simply added this code to the end of your mainconfig.lua:

Code: Select all

-- Dynamically load config from other mods
	for name,version in pairs(game.active_mods) do
		local interfaceName = "rsoconfig-" .. name

		if ( remote.interfaces[interfaceName] ~= nil and remote.interfaces[interfaceName]["getConfig"] ~= nil ) then
			log("RSO -> Mod " .. name .. " configuration interface found, calling it now")
			local modConfigRequest = remote.call(interfaceName, "getConfig")
			if ( modConfigRequest ~= nil ) then
				for key, configElement in pairs(modConfigRequest) do
					config[key] = configElement
				end
			end
		else
			log("RSO -> Mod " .. name .. " does not have a configuration interface")
		end
	end

Then, i added a remote interface to my mod:

Code: Select all

function getRsoConfig()
    local config = {}
    config["ax-matter-ore"] = {
		type="resource-ore",
		
		allotment=40,
		spawns_per_region={min=1, max=1},
		richness=6000,
		size={min=10, max=15},
		min_amount=300,

		starting={richness=8000, size=25, probability=1},
	}
	
	config["ax-liquid-matter"] = {
		type="resource-liquid",
		minimum_amount=240000,
		allotment=70,
		spawns_per_region={min=1, max=2},
		richness={min=240000, max=400000},
		size={min=2, max=5},
		
		starting={richness=400000, size=2, probability=1},
		
		multi_resource_chance=0.20,
		multi_resource={
			["coal"] = 4,
			["uranium-ore"] = 1,
		}
	}
    return config
end

remote.add_interface("rsoconfig-aix_matter", {getConfig = function() return getRsoConfig() end})
This way, the mod creators themselves could add their own interface, with the name of "rsoconfig-MODNAME", as long as their handle is called getConfig RSO will pick it up dynamically when a new level loads.

I hope that you'll consider adding something like this into your mod, as it really saves you a whole lot of time, plus it's possible for mod creators to change RSO's config when they release a new version of their mod, or even a brand new one.
orzelek
Smart Inserter
Smart Inserter
Posts: 3923
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: [EXAMPLE] Dynamic Loading of RSO Configs directly from the mods

Post by orzelek »

It was considered few times.
And you are missing one significant problem - that config would need to be validated somehow.

And if it's not then any error in it will land with bug report on RSO or the mod in question.

It can be done easily as you can see - and maybe in reverse way aka mod would call RSO interface to register the config. That way RSO wouldn't need to scan the list of mods every time it needs to redo the config.

Issue still remains how to ensure that every mod that replies with the config has a valid config that is correct for current version of RSO.
AlienX
Fast Inserter
Fast Inserter
Posts: 105
Joined: Wed May 17, 2017 7:13 pm
Contact:

Re: [EXAMPLE] Dynamic Loading of RSO Configs directly from the mods

Post by AlienX »

This is true, but some simple nil checks on the data validation stage could be done, if any data validation fails then ignore the whole mod, and report it in the log file, or a message on the screen like you do already.
AlienX
Fast Inserter
Fast Inserter
Posts: 105
Joined: Wed May 17, 2017 7:13 pm
Contact:

Re: [EXAMPLE] Dynamic Loading of RSO Configs directly from the mods

Post by AlienX »

What about something like this?

Totally untested mind you, just an example.

Code: Select all


function validateConfigElement(configElement)	
	-- Check element against values that must exist
	if ( configElement.type == nil ) then return {result=false, message="type must be set"} end
	if ( configElement.allotment == nil ) then return {result=false, message="allotment must be set"} end
	if ( configElement.spawns_per_region == nil ) then return {result=false, message="spawns_per_region must be set"} end
	if ( configElement.richness == nil ) then return {result=false, message="richness must be set"} end
	if ( configElement.size == nil ) then return {result=false, message="size must be set"} end

	-- Check variable types
	if ( type(configElement.type) ~= "string" ) then return {result=false, message="type is not a string" end
	if ( type(configElement.spawns_per_region) == "table" ) then
		if ( configElement.spawns_per_region.min == nil or configElement.spawns_per_region.max == nil ) then
			return {result=false, message="invalid spawns_per_region"}
		end
	else
		return return {result=false, message="spawns_per_region is not a table"}
	end

	-- Check dynamic tables, they could be anything.
	if ( configElement.multi_resource ~= nil ) then
		if ( configElement.multi_resource_chance == nil ) then return {result=false, message="multi_resource_chance not set when multi_resource exists"}
		for key, value in configElement.multi_resource do
			-- Sanity check the ent prototypes given, and that the value is a integer
			if ( game.entity_prototypes[key] == nil or type(value) ~= "number" )
				return {result=false, message="multi_resource " .. key .. " either has no entity, or the value is not a number"}
			end
		end
	end

	return {result=true,message=nil}
end


	-- Dynamically load config from other mods
	for name,version in pairs(game.active_mods) do
		local interfaceName = "rsoconfig-" .. name

		if ( remote.interfaces[interfaceName] ~= nil and remote.interfaces[interfaceName]["getConfig"] ~= nil ) then
			log("RSO -> Mod " .. name .. " configuration interface found, calling it now")
			local modConfigRequest = remote.call(interfaceName, "getConfig")
			if ( modConfigRequest ~= nil ) then
				for key, configElement in pairs(modConfigRequest) do
					local validation = validateConfigElement(configElement)
					if ( validation.result == true ) then
						config[key] = configElement
					else
						if ( validation.message ~= nil ) then
							log("Unable to import config from " .. name .. ", message was: " .. validation.message)
						end
					end
				end
			end
		else
			log("RSO -> Mod " .. name .. " does not have a configuration interface")
		end
	end
orzelek
Smart Inserter
Smart Inserter
Posts: 3923
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: [EXAMPLE] Dynamic Loading of RSO Configs directly from the mods

Post by orzelek »

Thats what I didn't really want to do... might be because I'm not keeping track of what is allowed in configs anywhere. I didn't really change it much from what was in the mod and taking time to write it all up somewhere did not happen.

It might happen some day.. but not yet.
Teaching everyone to make configs is pretty difficult - not every mod author will come with ready config like you :D

You can find thread below from Eradicator - he started on creating validator for configs. I don't remeber how finished it was.
AlienX
Fast Inserter
Fast Inserter
Posts: 105
Joined: Wed May 17, 2017 7:13 pm
Contact:

Re: [EXAMPLE] Dynamic Loading of RSO Configs directly from the mods

Post by AlienX »

This is a shame bud, but i can understand your side of things indeed.
Hopefully one day you can shove the responsibility to the mod creators to configure RSO, instead of yourself having to do a lot of work :)

However, as you have the entity prototype and autoplace settings available to you during the control.lua part, why not dynamically create a config yourself for missing ores based off the prototypes parameters?

** Edit **
Also, are you able to merge posts, perhaps merge mine with that other guys original post, no point in having two topics talking about the same thing.
orzelek
Smart Inserter
Smart Inserter
Posts: 3923
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: [EXAMPLE] Dynamic Loading of RSO Configs directly from the mods

Post by orzelek »

AlienX wrote: Wed Apr 03, 2019 9:10 pm This is a shame bud, but i can understand your side of things indeed.
Hopefully one day you can shove the responsibility to the mod creators to configure RSO, instead of yourself having to do a lot of work :)

However, as you have the entity prototype and autoplace settings available to you during the control.lua part, why not dynamically create a config yourself for missing ores based off the prototypes parameters?

** Edit **
Also, are you able to merge posts, perhaps merge mine with that other guys original post, no point in having two topics talking about the same thing.

I did think few times about using autoplace and try to do some magic on it to have RSO configs. I couldn't find nice way to do that - RSO config has simply more info and allows for different setups of ores.
And in 0.17 autoplace looks like hundred table horror when accessed in game stage.. not touching that :D

I might be able to merge topics here... I'm not going to try it I think might explode something.
Post Reply

Return to “Resource Spawner Overhaul”