panraven Grandmaster Cheater Reputation: 61 Joined: 01 Oct 2008 Posts: 958
|
Posted: Fri May 22, 2015 1:48 pm Post subject: |
|
|
Saw these before got deleted...
rahat wrote: | hmmm,
ran into one short problem with "var = cmdHttpGet([[tiny-url/ammo.txt]])". That, if for some reason host is down, trainer doesn't load while throwing error.
Is there any way to modify it a bit, so if cmdHttpGet function does not get to specified url OR returning output is other then "return{*" then it should check for another url.
Thnx in advance |
...so new code:
Code: | function exist(f) return type(f)=='string' and os.rename(f,f) and true or false end
function readFile(f,mode) local r = io.open(f,mode or 'rb') local ret = r:read('*all') r:close() return ret end
function cmdHttpGet(file,url,...)
assert(type(url)=='string','has to specific url string')
local useTmp,ok,response,result = type(file)~='string'
if useTmp then file = os.tmpname() end
local cmd = string.format([[powershell -command "& { (New-Object Net.WebClient).DownloadFile('%s', '%s') }"]],url,file)
ok,cmd = pcall(io.popen,cmd,'r')
if not ok then return nil,cmd end -- cmd is err msg if not ok else the command pipe line object
response = cmd:read('*all')
local requestGood = response:len() == 0 -- assume the powershell return nothing on CONSOLE if the download is success
cmd:close()
if requestGood then
ok,response = pcall(readFile,file)
result = ok and response or nil
else -- web error
if select('#',...)>0 then result,response = cmdHttpGet(file,...) end -- try next url if provided
end
if useTmp and exist(file) then os.remove(file) end -- clean tmp file
return result,response
end
-- test start
function test()
local r,errmsg = cmdHttpGet(nil,[[http://cheapengine.net]],[[http://cheatengine.org]])
if r ~= nil then
print(type(r),#r)
else
print('error:',errmsg)
end
end
test()
-- test end
-- seperate manual online data loading and default local file loading
function getDataOnline(filename)
return cmdHttpGet(filename,[[your]],[[url]],[[list]])
end
function getDataLocal(filename)
if exist(filename) then
local ok,result = pcall(readFile,filename)
if ok then return result else return nil, result end
end
return getDataOnline(filename)
end |
Last 2 functions is to address Rydian's concern, that probably the trainer should minimize online access.
For example, a [mauanl online load] button is made to load online data only when necessary, while the trainer should load data from a local file by default.
On error handling, these functions should return a string if success, or nil with 2nd return value as error message on something wrong (see test example).
|
|