Preserve user profiles in mod storage

This commit is contained in:
Wuzzy 2022-04-20 20:45:20 +02:00
parent 8b08b26720
commit c426ce6809
2 changed files with 55 additions and 8 deletions

View File

@ -126,25 +126,28 @@ or the lacunarity.
A profile is just noise parameters that you can load for later use.
Its a convenience feature.
The drop-down list to the left is the list of all profiles.
This list contains two types of profiles:
This list contains three types of profiles:
* Active noise parameters: Special profile that represents
the currently active noise. Can not be deleted.
* Builtin profiles are the noise parameters
of the official Minetest mapgens, loaded from settings.
Their name always begins with `mg_`
They can not be deleted.
* Custom profiles: These are your profiles, you can
Can not be deleted.
* User profiles: These are your profiles, you can
add and remove them at will. These are named like
“Profile 1”
User profiles will also automatically saved on disk so they
get restored when restarting.
The buttons do the following:
* Add: Save the current noise parameters into a new entry in the list
* Add: Save the current noise parameters into a new user profile
* Load: Load the currently selected profile and replace the
input fields
* Delete: Delete the currently selected profile (not possible
for builtin profiles)
Note: Currently, profiles are temporary. They are **not** saved on shutdown!
* Delete: Delete the currently selected user profile (not possible
for non-user profiles)
##### Noise options
This section roughly says how the noise parameters are “interpreted”. You

View File

@ -1,6 +1,8 @@
local S = minetest.get_translator("perlin_explorer")
local F = minetest.formspec_escape
local mod_storage = minetest.get_mod_storage()
-- If true, the Perlin test nodes will support color
-- (set to false in case of performance problems)
local COLORIZE_NODES = true
@ -131,6 +133,46 @@ for n=1, #noise_settings do
end
end
-- Load user profiles from mod storage
minetest.log("info", "[perlin_explorer] Checking for user profiles in mod storage ...")
if mod_storage:contains("profiles") then
local user_profiles_str = mod_storage:get_string("profiles")
local user_profiles = minetest.deserialize(user_profiles_str)
local loaded = 0
if user_profiles then
for p=1, #user_profiles do
local user_profile = user_profiles[p]
if user_profile and type(user_profile) == "table" and user_profile.noiseparams then
table.insert(np_profiles, {
np_type = "user",
noiseparams = user_profiles[p].noiseparams,
})
loaded = loaded + 1
else
minetest.log("warning", "[perlin_explorer] Malformed user profile in mod storage! Skipping ...")
end
end
end
minetest.log("action", "[perlin_explorer] Loaded "..loaded.." user profile(s) from mod storage")
end
-- Updates the user profiles in the mod stroage.
-- Must be called whenever the user profiles change.
local function update_mod_stored_profiles()
local user_profiles = {}
for p=1, #np_profiles do
local profile = np_profiles[p]
if profile.np_type == "user" then
table.insert(user_profiles, {
noiseparams = table.copy(profile.noiseparams),
})
end
end
local serialized_profiles = minetest.serialize(user_profiles)
mod_storage:set_string("profiles", serialized_profiles)
minetest.log("info", "[perlin_explorer] Profiles in mod storage updated")
end
-- Options are settings that dictate how the noise is supposed
-- to be generated.
local current_options = {}
@ -1409,6 +1451,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return
end
table.remove(np_profiles, profile_to_delete)
update_mod_stored_profiles()
local new_id = math.max(1, profile_to_delete - 1)
show_noise_formspec(player, default_noiseparams, new_id)
return
@ -1492,6 +1535,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
np_type = "user",
noiseparams = noiseparams,
})
update_mod_stored_profiles()
local new_profile = #np_profiles
minetest.log("action", "[perlin_explorer] Perlin noise profile "..new_profile.." added!")
show_noise_formspec(player, noiseparams, new_profile)