forked from your-land/yl_cinema
194 lines
5.8 KiB
Lua
194 lines
5.8 KiB
Lua
-- 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
|
|
|
|
local function warn(text)
|
|
core.log("warning", "[yl_cinema] " .. text)
|
|
end
|
|
|
|
local function action(text)
|
|
core.log("action", "[yl_cinema] " .. text)
|
|
end
|
|
|
|
-- 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
|
|
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 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 function read_json_file(path)
|
|
local file = io.open(path, "r")
|
|
if not file then
|
|
return false, "Error opening file: " .. path
|
|
end
|
|
|
|
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_id, movie in pairs(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
|
|
if #results > 0 then
|
|
return true, results
|
|
else
|
|
return false, results
|
|
end
|
|
end
|
|
|
|
-- Public functions wrap the private ones, so they can be exchanged easily
|
|
|
|
function yl_cinema.load(filename, ...)
|
|
return read_json_file(filename, ...)
|
|
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
|
|
|
|
function yl_cinema.get_movie(movie_id, ...)
|
|
return get_movie(movie_id, ...)
|
|
end
|
|
|
|
yl_cinema.action = action
|
|
yl_cinema.warn = warn |