some code cleanup for removable

This commit is contained in:
Alexander Weber 2017-12-12 16:28:49 +01:00
parent 97045a6c09
commit c1c9be971c
3 changed files with 67 additions and 70 deletions

11
API.md
View File

@ -41,7 +41,8 @@ Usable from node functions, from apps or outsite
- `mtos:set_app(appname)` - Start/Enable/navigate to appname. If no appname given the launcher is called
- `mtos:get_theme(theme)`- Get theme data current or requested (theme parameter is optional)
- `mtos:set_theme(theme)`- Activate theme
- `mtos:get_node_inventory()` - Access to the item in node inventory (low-level)
- `mtos:get_removable_data()` - Access to the item in node inventory (low-level)
- `mtos:set_removable_data()`- Store changes on low-level removable data
## App Definition
`laptop.register_app(internal_shortname, { definitiontable })` - add a new app or view
@ -78,3 +79,11 @@ Definitiontable:
`function laptop.get_theme(theme_name)`
- `theme:get_button(area, prefix, code, text)` get a themed [prefix]_button in area 'x,y;w,h' with code an text
- `theme:get_label(pos, text)` get a themed label text starting at pos 'x,y'
## Low-level Removable data
`data = mtos:get_removable_data()`
- `label` - Meda label. Item name by default
- `def` - Registered item definition (read-only)
- `inv` - node inventory
- `stack` - The item stack
- `meta` - Stack metadata

View File

@ -1,31 +1,3 @@
local function get_item_data(mtos)
local stack = mtos:get_node_inventory():get_stack("main", 1)
if stack then
local def = stack:get_definition()
if def and def.name ~= "" then
local data = {
def = def,
stack = stack,
meta = stack:get_meta()
}
data.label = data.meta:get_string("description")
if not data.label or data.label == "" then
data.label = def.description
end
return data
end
end
end
local function set_item_data(mtos, data)
if data.label ~= data.def.description then
data.meta:set_string("description", data.label)
end
mtos:get_node_inventory():set_stack("main", 1, data.stack)
end
laptop.register_app("removable", {
app_name = "Removable storage",
@ -39,7 +11,7 @@ laptop.register_app("removable", {
"listring[nodemeta:"..mtos.pos.x..','..mtos.pos.y..','..mtos.pos.z..";main]" ..
"listring[current_player;main]"
local idata = get_item_data(mtos)
local idata = mtos:get_removable_data()
if idata then
formspec = formspec .. mtos.theme:get_label('0,1.2', idata.def.description)..
"field[2,0.7;4,1;label;Label:;"..idata.label.."]"..
@ -65,12 +37,12 @@ laptop.register_app("removable", {
end,
receive_fields_func = function(app, mtos, sender, fields)
local idata = get_item_data(mtos)
local idata = mtos:get_removable_data()
if idata then
if fields.set_label then
idata.label = fields.label
end
set_item_data(mtos, idata)
mtos:set_removable_data()
end
end,
})

92
os.lua
View File

@ -7,13 +7,6 @@ laptop.class_lib.os = os_class
local mod_storage = minetest.get_mod_storage()
-- Setup internal inventory slot
function os_class:get_node_inventory()
local inv = self.meta:get_inventory()
inv:set_size("main", 1) -- 1 disk supported
return inv
end
-- Swap the node
function os_class:swap_node(new_node_name)
local node = minetest.get_node(self.pos)
@ -51,27 +44,6 @@ function os_class:set_infotext(infotext)
self.meta:set_string('infotext', infotext)
end
-- Save the data
function os_class:save()
self.meta:set_string('laptop_appdata', minetest.serialize(self.appdata))
if self.cloud_store then
for store, value in pairs(self.cloud_store) do
mod_storage:set_string(store, minetest.serialize(value))
end
self.cloud_store = nil
end
if self.removable_store then
local stack = self:get_node_inventory():get_stack("main", 1)
if stack then
for store, value in pairs(self.removable_store) do
stack:get_meta():set_string(store, minetest.serialize(value))
end
end
self.removable_store = nil
end
end
-- Get given or current theme
function os_class:get_theme(theme)
local theme_sel = theme or self.appdata.os.theme
@ -156,6 +128,43 @@ function os_class:pass_to_app(method, reshow, sender, ...)
return ret
end
-- Get Low-Level access to inventory slot
function os_class:get_removable_data()
self.removable_data = nil
local inv = self.meta:get_inventory()
inv:set_size("main", 1) -- 1 disk supported
local stack = inv:get_stack("main", 1)
if stack then
local def = stack:get_definition()
if def and def.name ~= "" then
local data = {
inv = inv,
def = def,
stack = stack,
meta = stack:get_meta()
}
data.label = data.meta:get_string("description")
if not data.label or data.label == "" then
data.label = def.description
end
self.removable_data = data
return data
end
end
end
-- Store data to inventory slot item (low-level)
function os_class:set_removable_data()
if self.removable_data then
local data = self.removable_data
if data.label ~= data.def.description then
data.meta:set_string("description", data.label)
end
data.inv:set_stack("main", 1, data.stack)
end
end
-- Get mod storage as (=internet / cloud)
function os_class:connect_to_cloud(store_name)
self.cloud_store = self.cloud_store or {}
@ -164,18 +173,25 @@ function os_class:connect_to_cloud(store_name)
return self.cloud_store[store_name]
end
-- Get item storage as (=floppy/usb)
function os_class:connect_to_removable(store_name)
local stack = self:get_node_inventory():get_stack("main", 1)
if not stack then
self.removable_store = nil
return nil
-- Save the data
function os_class:save()
self.meta:set_string('laptop_appdata', minetest.serialize(self.appdata))
if self.cloud_store then
for store, value in pairs(self.cloud_store) do
mod_storage:set_string(store, minetest.serialize(value))
end
self.cloud_store = nil
end
self.removable_store = self.removable_store or {}
self.removable_store[store_name] = self.removable_store[store_name] or
minetest.deserialize(stack:get_meta():get_string(store_name))
return self.removable_store[store_name]
if self.removable_store then
local data = self:get_removable_data()
if data then
for store, value in pairs(self.removable_store) do
data.meta:set_string(store, minetest.serialize(value))
end
end
self.removable_store = nil
end
end
-----------------------------------------------------