forked from Devieth/Halo-Lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_download.lua
135 lines (127 loc) · 4.72 KB
/
map_download.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
-- Map Downloader 2.0 by Devieth
-- For SAPP 10.x.x
-- HAC2 repo URL
hac2_repo = 'http://maps.halonet.net/' -- HAC2 Repo
-- If you have another source for these files feel free to use it. `halopc.com` may not be up forever.
wget_dl = 'http://halopc.com/download/wget.exe'
unzip_dl = 'http://halopc.com/download/unzip.exe'
-- Sapp API min version.
api_version = '1.10.0.0'
script = 'map_download.lua: \t'
function OnScriptLoad()
register_callback(cb['EVENT_COMMAND'], 'OnEventCommand')
-- Assign the repo to use.
map_repo = hac2_repo
-- Get the EXE Path
exe_path = read_string(read_dword(sig_scan('0000BE??????005657C605') + 0x3))
-- Split the string
local t = tokenizestring(exe_path, '\\')
-- Create the maps_folder_path
exe_folder_path = string.sub(exe_path, 0, string.len(exe_path) - string.len(t[#t]))
maps_folder_path = string.sub(exe_path, 0, string.len(exe_path) - string.len(t[#t]))..'maps\\'
-- Download unzip.
get_required_files()
end
function OnScriptUnload() end
function tokenizestring(input_string, delimiter)
if delimiter == nil then delimiter = '%s' end
local t = {}
for str in string.gmatch(input_string, '([^'..delimiter..']+)') do
t[#t+1] = str
end
return t
end
function rcon_return(Enviroment, PlayerIndex, Message, Color)
local Compatable_Message = string.gsub(tostring(Message), '|t', ' ')
if Color == nil then Color = 14 end
if Enviroment == 0 then
cprint(Compatable_Message, Color)
elseif Enviroment == 1 then
rprint(PlayerIndex, Message)
elseif Enviroment == 2 then
say(PlayerIndex, Compatable_Message)
end
end
function download_file(url, path, filename, powershell)
if filename == nil then filename = '' end
if powershell then
os.execute("powershell (New-Object System.Net.WebClient).DownloadFile('"..url.."','"..path..filename.."')")
else
local file = assert(io.popen('wget -O "'..filename..'" -c "'..url..'"'))
file:close()
local file = io.open(path..filename)
if file then
local content_size = string.len(file:read('*all'))
file:close()
if content_size > 0 then
return true
else
os.remove(filename)
return false
end
end
return false
end
cprint(script.."Download complete! Saving file to: "..path..filename, 14)
end
function get_required_files()
local file = io.open('unzip.exe', 'r')
if not file then
cprint(script..'unzip.exe not present, attempting to download...', 4)
download_file(unzip_dl, exe_folder_path, 'unzip.exe', true)
else
cprint(script..'unzip.exe found!',14)
file:close()
end
local file = io.open('wget.exe', 'r')
if not file then
cprint(script..'wget.exe not present, attempting to download...', 4)
download_file(wget_dl, exe_folder_path, 'wget.exe', true)
else
cprint(script..'wget.exe found!',14)
file:close()
end
end
function OnEventCommand(PlayerIndex, Command, Enviroment, Password)
local t = tokenizestring(string.lower(string.gsub(Command, '"', '')), ' ')
if get_var(PlayerIndex, '$lvl') ~= '-1' or tonumber(Enviroment) == 0 then
-- Hi-Jack SAPP's command.
if t[1] == 'map_download' then
-- Make sure they actually used a name
if t[2] then
-- Download the map
local map_name = t[2]
-- Check if they already have the map, or wget is going to hold the server hostage.
local file = io.open(maps_folder_path..map_name..'.map')
if file then
file:close()
rcon_return(tonumber(Enviroment), PlayerIndex, script..map_name.. " is already installed!")
return false
end
-- Download the map.
rcon_return(tonumber(Enviroment), PlayerIndex, script.."WARNING: Server may lag or disconnect durring download!")
local downloaded = download_file(hac2_repo..'map_download.php?map='..map_name, exe_folder_path, map_name..'.zip')
if downloaded then
local unzip = assert(io.popen('unzip '..map_name..' -d "'..maps_folder_path..'"'))
unzip:close()
-- Check to make sure the file was unziped.
local file = io.open(maps_folder_path..map_name..'.map')
if file then
file:close()
execute_command("map_load "..map_name)
rcon_return(tonumber(Enviroment), PlayerIndex, script.."Download of "..map_name.. " complete!")
-- Delete the .zip file.
os.remove(map_name..'.zip')
else
rcon_return(tonumber(Enviroment), PlayerIndex, script..'Error: Failed to extract '..map_name..'.zip', 4)
end
else
rcon_return(tonumber(Enviroment), PlayerIndex, script..'Error: Failed to download '..map_name..'\n'..script..'Please check your spelling or the hac2 repo if the map exist.', 4)
end
else
rcon_return(tonumber(Enviroment), PlayerIndex, script..'Error: Please enter a map name.', 4)
end
return false
end
end
end