generated from your-land/yl_template
Initialization: Loading movies and images
This commit is contained in:
parent
e97100435f
commit
50f30988f5
4
dependencies.lua
Normal file
4
dependencies.lua
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
yl_cinema.dependencies = {}
|
||||
|
||||
yl_cinema.dependencies.json = dofile(yl_cinema.modpath .. "json.lua/json.lua")
|
7
init.lua
7
init.lua
@ -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 .. "config.lua")
|
||||
dofile(yl_cinema.modpath .. "setup.lua")
|
||||
--dofile(yl_cinema.modpath .. "privs.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 .. "features.lua")
|
||||
dofile(yl_cinema.modpath .. "chatcommands.lua")
|
||||
--dofile(yl_cinema.modpath .. "features.lua")
|
||||
--dofile(yl_cinema.modpath .. "chatcommands.lua")
|
||||
|
||||
local mod_end_time = (core.get_us_time() - mod_start_time) / 1000000
|
||||
core.log("action", "[MOD] yl_cinema loaded in [" .. mod_end_time .. "s]")
|
||||
|
29
initialize.lua
Normal file
29
initialize.lua
Normal 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()
|
201
internal.lua
201
internal.lua
@ -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 debug = true
|
||||
|
||||
local function say(text)
|
||||
if yl_template.debug then
|
||||
core.log("action", "[MOD] yl_template : " .. text)
|
||||
end
|
||||
local function warn(text)
|
||||
core.log("warning", "[yl_cinema] " .. text)
|
||||
end
|
||||
|
||||
local function save_path(file)
|
||||
return yl_template.worldpath .. file .. ".json"
|
||||
local function action(text)
|
||||
core.log("action", "[yl_cinema] " .. text)
|
||||
end
|
||||
|
||||
local function save_json(filename, content)
|
||||
if type(filename) ~= "string" or type(content) ~= "table" then
|
||||
-- JSON decoder
|
||||
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
|
||||
end
|
||||
local savepath = save_path(filename)
|
||||
local savecontent = minetest.write_json(content)
|
||||
return minetest.safe_file_write(savepath, savecontent)
|
||||
return {{recipe[1], recipe[2], recipe[3]}, {recipe[4], recipe[5], recipe[6]}, {recipe[7], recipe[8], recipe[9]}}
|
||||
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 savepath = save_path(filename)
|
||||
local function path_of_image(movie_id, name)
|
||||
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")
|
||||
if err then
|
||||
return {}
|
||||
end
|
||||
io.input(file)
|
||||
local savecontent = io.read()
|
||||
local content = minetest.parse_json(savecontent)
|
||||
io.close(file)
|
||||
|
||||
if type(content) ~= "table" then
|
||||
content = {}
|
||||
local function read_json_file(path)
|
||||
local file = io.open(path, "r")
|
||||
if not file then
|
||||
return false, "Error opening file: " .. path
|
||||
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
|
||||
|
||||
-- Public functions wrap the private ones, so they can be exchanged easily
|
||||
|
||||
function yl_template.load(filename, ...)
|
||||
return load_json(filename, ...)
|
||||
function yl_cinema.load(filename, ...)
|
||||
return read_json_file(filename, ...)
|
||||
end
|
||||
|
||||
function yl_template.save(filename, content, ...)
|
||||
return save_json(filename, content, ...)
|
||||
end
|
||||
---
|
||||
-- Loads the metadata for all movies in the given directory.
|
||||
--
|
||||
-- @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
|
Loading…
Reference in New Issue
Block a user