it300

map_download

Apr 17th, 2019 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.72 KB | None | 0 0
  1. -- Map Downloader 2.0 by Devieth
  2. -- For SAPP 10.x.x
  3.  
  4. -- HAC2 repo URL
  5. hac2_repo = 'http://maps.halonet.net/' -- HAC2 Repo
  6. -- If you have another source for these files feel free to use it.  `halopc.com` may not be up forever.
  7. wget_dl = 'http://halopc.com/download/wget.exe'
  8. unzip_dl = 'http://halopc.com/download/unzip.exe'
  9. -- Sapp API min version.
  10. api_version = '1.10.0.0'
  11. script = 'map_download.lua: \t'
  12.  
  13. function OnScriptLoad()
  14.     register_callback(cb['EVENT_COMMAND'], 'OnEventCommand')
  15.     -- Assign the repo to use.
  16.     map_repo = hac2_repo
  17.     -- Get the EXE Path
  18.     exe_path = read_string(read_dword(sig_scan('0000BE??????005657C605') + 0x3))
  19.     -- Split the string
  20.     local t = tokenizestring(exe_path, '\\')
  21.     -- Create the maps_folder_path
  22.     exe_folder_path = string.sub(exe_path, 0, string.len(exe_path) - string.len(t[#t]))
  23.     maps_folder_path = string.sub(exe_path, 0, string.len(exe_path) - string.len(t[#t]))..'maps\\'
  24.     -- Download unzip.
  25.     get_required_files()
  26. end
  27.  
  28. function OnScriptUnload() end
  29.  
  30. function tokenizestring(input_string, delimiter)
  31.     if delimiter == nil then delimiter = '%s' end
  32.     local t = {}
  33.     for str in string.gmatch(input_string, '([^'..delimiter..']+)') do
  34.         t[#t+1] = str
  35.     end
  36.     return t
  37. end
  38.  
  39. function rcon_return(Enviroment, PlayerIndex, Message, Color)
  40.     local Compatable_Message = string.gsub(tostring(Message), '|t', '   ')
  41.     if Color == nil then Color = 14 end
  42.     if Enviroment == 0 then
  43.         cprint(Compatable_Message, Color)
  44.     elseif Enviroment == 1 then
  45.         rprint(PlayerIndex, Message)
  46.     elseif Enviroment == 2 then
  47.         say(PlayerIndex, Compatable_Message)
  48.     end
  49. end
  50.  
  51. function download_file(url, path, filename, powershell)
  52.     if filename == nil then filename = '' end
  53.     if powershell then
  54.         os.execute("powershell (New-Object System.Net.WebClient).DownloadFile('"..url.."','"..path..filename.."')")
  55.     else
  56.         local file = assert(io.popen('wget -O "'..filename..'" -c "'..url..'"'))
  57.         file:close()
  58.         local file = io.open(path..filename)
  59.         if file then
  60.             local content_size = string.len(file:read('*all'))
  61.             file:close()
  62.             if content_size > 0 then
  63.                 return true
  64.             else
  65.                 os.remove(filename)
  66.                 return false
  67.             end
  68.         end
  69.         return false
  70.     end
  71.     cprint(script.."Download complete! Saving file to: "..path..filename, 14)
  72. end
  73.  
  74. function get_required_files()
  75.     local file = io.open('unzip.exe', 'r')
  76.     if not file then
  77.         cprint(script..'unzip.exe not present, attempting to download...', 4)
  78.         download_file(unzip_dl, exe_folder_path, 'unzip.exe', true)
  79.     else
  80.         cprint(script..'unzip.exe found!',14)
  81.         file:close()
  82.     end
  83.     local file = io.open('wget.exe', 'r')
  84.     if not file then
  85.         cprint(script..'wget.exe not present, attempting to download...', 4)
  86.         download_file(wget_dl, exe_folder_path, 'wget.exe', true)
  87.     else
  88.         cprint(script..'wget.exe found!',14)
  89.         file:close()
  90.     end
  91. end
  92.  
  93. function OnEventCommand(PlayerIndex, Command, Enviroment, Password)
  94.     local t = tokenizestring(string.lower(string.gsub(Command, '"', '')), ' ')
  95.     if get_var(PlayerIndex, '$lvl') ~= '-1' or tonumber(Enviroment) == 0 then
  96.         -- Hi-Jack SAPP's command.
  97.         if t[1] == 'map_download' then
  98.             -- Make sure they actually used a name
  99.             if t[2] then
  100.                 -- Download the map
  101.                 local map_name = t[2]
  102.                 -- Check if they already have the map, or wget is going to hold the server hostage.
  103.                 local file = io.open(maps_folder_path..map_name..'.map')
  104.                 if  file then
  105.                     file:close()
  106.                     rcon_return(tonumber(Enviroment), PlayerIndex, script..map_name.. " is already installed!")
  107.                     return false
  108.                 end
  109.                 -- Download the map.
  110.                 rcon_return(tonumber(Enviroment), PlayerIndex, script.."WARNING: Server may lag or disconnect durring download!")
  111.                 local downloaded = download_file(hac2_repo..'map_download.php?map='..map_name, exe_folder_path, map_name..'.zip')
  112.                 if downloaded then
  113.                     local unzip = assert(io.popen('unzip '..map_name..' -d "'..maps_folder_path..'"'))
  114.                     unzip:close()
  115.                     -- Check to make sure the file was unziped.
  116.                     local file = io.open(maps_folder_path..map_name..'.map')
  117.                     if file then
  118.                         file:close()
  119.                         execute_command("map_load "..map_name)
  120.                         rcon_return(tonumber(Enviroment), PlayerIndex, script.."Download of "..map_name.. " complete!")
  121.                         -- Delete the .zip file.
  122.                         os.remove(map_name..'.zip')
  123.                     else
  124.                         rcon_return(tonumber(Enviroment), PlayerIndex, script..'Error: Failed to extract '..map_name..'.zip', 4)
  125.                     end
  126.                 else
  127.                     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)
  128.                 end
  129.             else
  130.                 rcon_return(tonumber(Enviroment), PlayerIndex, script..'Error: Please enter a map name.', 4)
  131.             end
  132.             return false
  133.         end
  134.     end
  135. end
  136.  
Add Comment
Please, Sign In to add comment