Initialization: Loading movies and images

This commit is contained in:
AliasAlreadyTaken 2023-05-28 11:37:07 +02:00
parent e97100435f
commit 50f30988f5
4 changed files with 203 additions and 38 deletions

4
dependencies.lua Normal file
View File

@ -0,0 +1,4 @@
yl_cinema.dependencies = {}
yl_cinema.dependencies.json = dofile(yl_cinema.modpath .. "json.lua/json.lua")

View File

@ -24,12 +24,11 @@ yl_cinema.information.additional = "This mod implements #9 of the Small Tasks: A
dofile(yl_cinema.modpath .. "dependencies.lua") dofile(yl_cinema.modpath .. "dependencies.lua")
dofile(yl_cinema.modpath .. "config.lua") dofile(yl_cinema.modpath .. "config.lua")
dofile(yl_cinema.modpath .. "setup.lua") dofile(yl_cinema.modpath .. "setup.lua")
--dofile(yl_cinema.modpath .. "privs.lua")
dofile(yl_cinema.modpath .. "internal.lua") dofile(yl_cinema.modpath .. "internal.lua")
dofile(yl_cinema.modpath .. "api.lua") --dofile(yl_cinema.modpath .. "api.lua")
dofile(yl_cinema.modpath .. "initialize.lua") dofile(yl_cinema.modpath .. "initialize.lua")
dofile(yl_cinema.modpath .. "features.lua") --dofile(yl_cinema.modpath .. "features.lua")
dofile(yl_cinema.modpath .. "chatcommands.lua") --dofile(yl_cinema.modpath .. "chatcommands.lua")
local mod_end_time = (core.get_us_time() - mod_start_time) / 1000000 local mod_end_time = (core.get_us_time() - mod_start_time) / 1000000
core.log("action", "[MOD] yl_cinema loaded in [" .. mod_end_time .. "s]") core.log("action", "[MOD] yl_cinema loaded in [" .. mod_end_time .. "s]")

29
initialize.lua Normal file
View File

@ -0,0 +1,29 @@
local function load_movies()
local movie_save_path = yl_cinema.worldpath .. yl_cinema.settings.save_path
local movies, loaded_count, total_count = yl_cinema.load_movies(movie_save_path)
if loaded_count ~= total_count then
yl_cinema.warn(loaded_count .. "/" .. total_count .. " movies loaded.")
else
yl_cinema.action(loaded_count .. "/" .. total_count .. " movies loaded.")
end
return movies
end
local function load_images(movies)
for movie_id, _ in pairs(movies) do
local loaded_count, total_count = yl_cinema.load_images_of_movie(movie_id)
if loaded_count ~= total_count then
yl_cinema.warn(loaded_count .. "/" .. total_count .. " images loaded for movie " .. movie_id)
else
yl_cinema.action(loaded_count .. "/" .. total_count .. " images loaded for movie " .. movie_id)
end
end
end
local function run_each_serverstart()
local movies = load_movies()
core.after(0, load_images, movies)
yl_cinema.movies = movies
end
run_each_serverstart()

View File

@ -1,53 +1,186 @@
-- The functions and variables in this file are only for use in the mod itself.
-- Those that do real work should be local and wrapped in public functions
-- Minetest connector
local json = yl_cinema.dependencies.json
-- The functions and variables in this file are only for use in the mod itself. Those that do real work should be local and wrapped in public functions local function warn(text)
core.log("warning", "[yl_cinema] " .. text)
local debug = true
local function say(text)
if yl_template.debug then
core.log("action", "[MOD] yl_template : " .. text)
end
end end
local function save_path(file) local function action(text)
return yl_template.worldpath .. file .. ".json" core.log("action", "[yl_cinema] " .. text)
end end
local function save_json(filename, content) -- JSON decoder
if type(filename) ~= "string" or type(content) ~= "table" then local function parse_json(json_str)
return json.decode(json_str)
end
local function get_files_in_directory(folderpath)
return core.get_dir_list(folderpath, true)
end
-- internal functions
local function mediacallback(name)
local target_name = name or ""
action("mediacallback: File pushed to player " .. target_name)
end
local function string_to_recipe(s)
local recipe = string.split(s, ",")
if #recipe ~= 9 then
return false return false
end end
local savepath = save_path(filename) return {{recipe[1], recipe[2], recipe[3]}, {recipe[4], recipe[5], recipe[6]}, {recipe[7], recipe[8], recipe[9]}}
local savecontent = minetest.write_json(content)
return minetest.safe_file_write(savepath, savecontent)
end end
local function path_of_movie(movie_id)
return yl_cinema.worldpath .. yl_cinema.settings.save_path .. DIR_DELIM .. movie_id .. DIR_DELIM .. movie_id ..
".json"
end
local function load_json(filename) -- returns the saved dialog local function path_of_image(movie_id, name)
local savepath = save_path(filename) return yl_cinema.worldpath .. yl_cinema.settings.save_path .. DIR_DELIM .. movie_id .. DIR_DELIM .. "textures" ..
DIR_DELIM .. name
end
local file, err = io.open(savepath, "r") local function read_json_file(path)
if err then local file = io.open(path, "r")
return {} if not file then
end return false, "Error opening file: " .. path
io.input(file)
local savecontent = io.read()
local content = minetest.parse_json(savecontent)
io.close(file)
if type(content) ~= "table" then
content = {}
end end
return content local content = file:read("*all")
file:close()
if not content then
return false, "Error reading file: " .. path
end
return true, parse_json(content)
end
local function get_movie(movie_id)
return yl_cinema.movies[movie_id] or {}
end
local function load_image(path)
local options = {
filepath = path
}
return core.dynamic_add_media(options, mediacallback)
end
local function load_images_of_movie(movie_id)
local movie = get_movie(movie_id)
local loaded_count = 0
for _, file in ipairs(movie.files) do
local path = path_of_image(movie_id, file)
local success = load_image(path)
if success then
loaded_count = loaded_count + 1
end
end
return loaded_count, #movie.files
end
local function load_movie(movie_id)
local file_path = path_of_movie(movie_id)
local success, movie_data = read_json_file(file_path)
local movie = "Error loading movie metadata from file " .. dump(file_path) .. ": " .. dump(movie_data)
if success then
movie = movie_data
else
warn(movie)
end
return success, movie
end
local function load_movies(movies_directory)
local movies = {}
local loaded_count = 0
local total_count = 0
local movie_files = get_files_in_directory(movies_directory)
for _, movie_id in ipairs(movie_files) do
local success, movie = load_movie(movie_id)
if success then
movies[movie_id] = movie
loaded_count = loaded_count + 1
end
end
return movies, loaded_count, #movie_files
end
local function search_movies(movies, search_term)
local results = {}
for _, movie in ipairs(movies) do
if string.match(movie.id, search_term) or string.match(movie.name, search_term) or
string.match(movie.description, search_term) then
table.insert(results, movie.id)
end
end
return results
end end
-- Public functions wrap the private ones, so they can be exchanged easily -- Public functions wrap the private ones, so they can be exchanged easily
function yl_template.load(filename, ...) function yl_cinema.load(filename, ...)
return load_json(filename, ...) return read_json_file(filename, ...)
end end
function yl_template.save(filename, content, ...) ---
return save_json(filename, content, ...) -- Loads the metadata for all movies in the given directory.
end --
-- @param movies_directory (string) The directory to search for movie metadata files.
-- @return A table of movies, where each movie is represented as a table with the following fields:
-- - id (string): The ID of the movie.
-- - name (string): The name of the movie.
-- - description (string): A description of the movie.
-- - title_texture (string): The path to the texture to use as the movie's title.
-- - replay (boolean): Whether the movie can be replayed.
-- - pages (table): A table of pages for the movie. Each page is represented as a table with the following fields:
-- - order (number): The order in which the page should be displayed.
-- - texturename (string): The path to the texture to use for the page.
-- - caption (string): The caption to display for the page.
-- - captionposx (number): The x position of the caption.
-- - captionposy (number): The y position of the caption.
-- - duration (number): The duration to display the page in seconds.
-- @return The number of successfully loaded movies.
-- @return The total number of movies attempted to load.
--
function yl_cinema.load_movies(movies_directory, ...)
return load_movies(movies_directory, ...)
end
---
-- Searches the list of movies for a given search term.
--
-- @param movies (table) The list of movies to search.
-- @param searchterm (string) The search term to match against.
-- @return A table of movieids that match the search term.
--
-- Each movie is represented as a table with the following fields:
-- - id (string): The ID of the movie.
-- - name (string): The name of the movie.
-- - description (string): A description of the movie.
--
function yl_cinema.search_movies(movies, search_term, ...)
return search_movies(movies, search_term, ...)
end
function yl_cinema.path_of_movie(movie_id, ...)
return path_of_movie(movie_id, ...)
end
function yl_cinema.load_images_of_movie(movie_id, ...)
return load_images_of_movie(movie_id, ...)
end
yl_cinema.action = action
yl_cinema.warn = warn