-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild_chest_add_schems_by_directory.lua
177 lines (162 loc) · 5.62 KB
/
build_chest_add_schems_by_directory.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
i = string.find( schem, '.schematic', -10 );
if( i ) then
schemname = string.sub( schem, 1, i-1 );
else
return;
end
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 ipairs( d2 ) do
local d3 = minetest.get_dir_list( path..'/mods/'..modname, true );
for _,subdir in ipairs( 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 ipairs( 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 ipairs( 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 ipairs( 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
build_chest.add_files_from_path = function( worldpath, worldname )
-- get list of subdirectories
local path = worldpath..'/'..worldname;
local subdir_list = minetest.get_dir_list( path, true );
if( not( subdir_list )) then
return;
end
for _,subdir in ipairs( subdir_list ) do
if( subdir=='schems' ) then
local file_list = minetest.get_dir_list( path..'/schems', false );
for _,filename in ipairs( file_list ) do
build_chest_add_files_to_menu_from_directory(
filename,
path..'/schems/',
'import from world',
'landscape backups',
{'OVERWRITE THIS', worldname });
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 ipairs( d1 ) do
build_chest.add_files_from_path( worldpath, worldname );
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 ipairs( 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.settings:get( 'secure.enable_security' )) then
if( minetest.get_dir_list ) then
local worldpath = minetest.get_worldpath();
local d3 = minetest.get_dir_list( worldpath..'/schems', false );
if( d3 ) then
for _,filename in ipairs( 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