minetest-game-adventuretest/mods/handle_schematics/build_chest_add_schems_by_d...

165 lines
5.3 KiB
Lua

local build_chest_add_files_to_menu_from_directory = function( schem, path, entry_name, backup_name, menu_path_list)
-- we need the filename without extension (*.mts, *.we, *.wem)
local schemname = schem;
local i = string.find( schem, '.mts', -4 );
if( i ) then
schemname = string.sub( schem, 1, i-1 );
else
i = string.find( schem, '.we', -3 );
if( i ) then
schemname = string.sub( schem, 1, i-1 );
else
i = string.find( schem, '.wem', -4 );
if( i ) then
schemname = string.sub( schem, 1, i-1 );
else
return;
end
end
end
-- only add known file types
if( not( schemname )) then
return;
end
i = string.find( schemname, 'backup_' );
menu_path_list[ #menu_path_list+1 ] = schemname;
menu_path_list[ #menu_path_list+1 ] = path..schemname;
-- probably one of those automatic backups of the landscape
if( i and i==1 and backup_name) then
menu_path_list[1] = backup_name;
-- normal entry
else
menu_path_list[1] = entry_name;
end
build_chest.add_entry( menu_path_list);
build_chest.add_building( path..schemname, {scm=schemname, typ='nn'});
end
-- search for mods and modpacks containing schematics in any form
local build_chest_check_all_directories_mods_and_modpacks = function( path, menu_title, gamename )
local d2 = minetest.get_dir_list( path..'/mods', true );
for _,modname in pairs( d2 ) do
local d3 = minetest.get_dir_list( path..'/mods/'..modname, true );
for _,subdir in pairs( d3 ) do
if( subdir ~= 'textures' and subdir ~= 'sounds' and subdir ~= 'models' and subdir ~= '.git' and subdir ~= 'locale') then
local d4 = minetest.get_dir_list( path..'/mods/'..modname..'/'..subdir, false );
for _,filename in pairs( d4 ) do
build_chest_add_files_to_menu_from_directory(
filename,
path..'/mods/'..modname..'/'..subdir..'/',
menu_title,
nil,
{'OVERWRITE THIS', gamename, modname} );
end
-- it might be a modpack
d4 = minetest.get_dir_list( path..'/mods/'..modname..'/'..subdir, true );
for _,subsubdir in pairs( d4 ) do
if( subsubdir ~= 'textures' and subsubdir ~= 'sounds' and subsubdir ~= 'models' and subsubdir ~= '.git' and subsubdir ~= 'locale') then
local d5 = minetest.get_dir_list( path..'/mods/'..modname..'/'..subdir..'/'..subsubdir, false );
for _,filename in pairs( d5 ) do
build_chest_add_files_to_menu_from_directory(
filename,
path..'/mods/'..modname..'/'..subdir..'/'..subsubdir..'/',
menu_title,
nil,
-- folders from modpacks get marked with a *..*
{'OVERWRITE THIS', gamename, '*'..subdir..'*'} );
end
end
end
end
end
end
end
local build_chest_check_all_directories = function()
-- find the name of the directory directly above the current worldpath
local worldpath = minetest.get_worldpath();
local p = 1;
local last_found = 1;
while( last_found ) do
p = last_found;
last_found = string.find( worldpath, '/', last_found+1 );
end
-- abort on Windows
if( p == 1 ) then
return;
end
worldpath = string.sub( worldpath, 1, p );
--[[
local p = 1;
while( not( string.find( worldpath, '/', -1*p ))) do
p = p+1;
end
local found = 1;
for p=string.len( worldpath ),1,-1 do
if( p>found
and (string.byte( worldpath, p )=='/'
or string.byte( worldpath, p )=='\\')) then
found = p;
end
end
--]]
worldpath = string.sub( worldpath, 1, string.len( worldpath )-p );
-- locate .mts, .wem and .we files in the worlds/WORLDNAME/schems/* folders
local d1 = minetest.get_dir_list( worldpath, true );
for _,worldname in pairs( d1 ) do
-- get list of subdirectories
local d2 = minetest.get_dir_list( worldpath..'/'..worldname, true );
for _,subdir in pairs( d2 ) do
if( subdir=='schems' ) then
local d3 = minetest.get_dir_list( worldpath..'/'..worldname..'/schems', false );
for _,filename in pairs( d3 ) do
build_chest_add_files_to_menu_from_directory(
filename,
worldpath..'/'..worldname..'/schems/',
'import from world',
'landscape backups',
{'OVERWRITE THIS', worldname });
end
end
end
end
local main_path = string.sub( worldpath, 1, string.len(worldpath)-string.len('/worlds'));
-- search in MODS/* subfolder
build_chest_check_all_directories_mods_and_modpacks( main_path, 'import from mod', 'mods' );
-- search in all GAMES/* folders for mods containing schematics
local game_path = main_path..'/games';
d1 = minetest.get_dir_list( game_path, true );
for _,gamename in pairs( d1 ) do
build_chest_check_all_directories_mods_and_modpacks( game_path..'/'..gamename, 'import from game', gamename );
end
end
-- TODO: hopfefully, security will get more relaxed regarding reading directories in the future
-- if security is enabled, our options to get schematics are a bit limited
if( minetest.setting_getbool( 'secure.enable_security' )) then
local worldpath = minetest.get_worldpath();
local d3 = minetest.get_dir_list( worldpath..'/schems', false );
if( d3 ) then
for _,filename in pairs( d3 ) do
build_chest_add_files_to_menu_from_directory(
filename,
worldpath..'/schems/',
'import from world',
'landscape backups',
{'OVERWRITE THIS', '-current world-' });
end
end
else
-- check worlds, mods and games folders for schematics and add them to the menu
build_chest_check_all_directories()
end