198 lines
5.3 KiB
Lua
Raw Normal View History

-----------------------------------------------------
-- Operating System class
-----------------------------------------------------
local os_class = {}
os_class.__index = os_class
2017-12-05 23:15:55 +01:00
laptop.class_lib.os = os_class
local mod_storage = minetest.get_mod_storage()
2017-12-11 22:07:47 +01:00
-- 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)
node.name = new_node_name
minetest.swap_node(self.pos, node)
end
-- Power on the system and start the launcher
function os_class:power_on(new_node_name)
if new_node_name then
self:swap_node(new_node_name)
end
2017-12-05 23:15:55 +01:00
self:set_app() --launcher
end
-- Power on the system / and resume last running app
function os_class:resume(new_node_name)
if new_node_name then
self:swap_node(new_node_name)
end
self:set_app(self.appdata.os.current_app)
end
-- Power off the system
function os_class:power_off(new_node_name)
if new_node_name then
self:swap_node(new_node_name)
end
self.meta:set_string('formspec', "")
2017-11-24 22:11:45 +01:00
self:save()
end
-- Set infotext for system
function os_class:set_infotext(infotext)
self.meta:set_string('infotext', infotext)
end
-- Save the data
2017-11-23 17:17:59 +01:00
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
2017-12-12 09:16:24 +01:00
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
2017-11-23 17:17:59 +01:00
end
-- Get given or current theme
function os_class:get_theme(theme)
local theme_sel = theme or self.appdata.os.theme
return laptop.get_theme(theme_sel)
end
-- Set current theme
function os_class:set_theme(theme)
if laptop.themes[theme] then
self.appdata.os.theme = theme
self.theme = self:get_theme()
self:save()
end
end
2017-12-05 23:15:55 +01:00
-- Add app to stack (before starting new)
function os_class:appstack_add(appname)
table.insert(self.appdata.os.stack, appname)
end
2017-11-24 22:11:45 +01:00
2017-12-05 23:15:55 +01:00
-- Get last app from stack
function os_class:appstack_pop()
local ret
if #self.appdata.os.stack > 0 then
ret = self.appdata.os.stack[#self.appdata.os.stack]
table.remove(self.appdata.os.stack, #self.appdata.os.stack)
2017-11-23 17:17:59 +01:00
end
2017-12-05 23:15:55 +01:00
return ret
end
-- Free stack
function os_class:appstack_free()
self.appdata.os.stack = {}
end
2017-12-05 23:15:55 +01:00
-- Get new app instance
function os_class:get_app(name)
local template = laptop.apps[name]
if not template then
return
end
2017-12-05 23:15:55 +01:00
local app = setmetatable(table.copy(template), laptop.class_lib.app)
app.name = name
app.os = self
return app
end
-- Activate the app
function os_class:set_app(appname)
local launcher = self.hwdef.custom_launcher or "launcher"
2017-12-05 23:15:55 +01:00
local newapp = appname or launcher
if newapp == launcher then
self:appstack_free()
elseif self.appdata.os.current_app and
self.appdata.os.current_app ~= launcher and
self.appdata.os.current_app ~= newapp then
self:appstack_add(self.appdata.os.current_app)
2017-12-05 23:15:55 +01:00
end
2017-12-05 23:15:55 +01:00
self.appdata.os.current_app = newapp
local app = self:get_app(newapp)
local formspec = app:get_formspec()
if formspec ~= false then
self.meta:set_string('formspec', formspec)
end
2017-11-23 17:17:59 +01:00
self:save()
end
2017-12-05 23:15:55 +01:00
-- Handle input processing
function os_class:pass_to_app(method, reshow, sender, ...)
local appname = self.appdata.os.current_app or self.hwdef.custom_launcher or "launcher"
2017-12-05 23:15:55 +01:00
local app = self:get_app(appname)
local ret = app:receive_data(method, reshow, sender, ...)
self.appdata.os.last_player = sender:get_player_name()
if self.appdata.os.current_app == appname and reshow then
local formspec = app:get_formspec()
if formspec ~= false then
self.meta:set_string('formspec', formspec)
end
end
self:save()
return ret
2017-11-23 17:17:59 +01:00
end
-- Get mod storage as (=internet / cloud)
function os_class:connect_to_cloud(store_name)
self.cloud_store = self.cloud_store or {}
self.cloud_store[store_name] = self.cloud_store[store_name] or
minetest.deserialize(mod_storage:get_string(store_name)) or {}
return self.cloud_store[store_name]
end
2017-12-12 09:16:24 +01:00
-- 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
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]
end
-----------------------------------------------------
-- Get Operating system object
-----------------------------------------------------
function laptop.os_get(pos)
local self = setmetatable({}, os_class)
2017-11-23 17:17:59 +01:00
self.__index = os_class
self.pos = pos
self.node = minetest.get_node(pos)
self.hwdef = laptop.node_config[self.node.name]
2017-11-23 17:17:59 +01:00
self.meta = minetest.get_meta(pos)
self.appdata = minetest.deserialize(self.meta:get_string('laptop_appdata')) or {}
self.appdata.launcher = self.appdata.launcher or {}
self.appdata.os = self.appdata.os or {}
self.appdata.os.stack = self.appdata.os.stack or {}
self.theme = self:get_theme()
2017-11-23 17:17:59 +01:00
return self
end