@Briarfox - This is the script loaded in http.request :
-- GithubLoader
-- @Author : Hyro Vitaly Protago
-- @Version : 1.0.2
----------------------------------------------------------------------------
------------------------------ How to use ----------------------------------
----------------------------------------------------------------------------
--[[
# Create a codea project and replace the main by :
--
private = "https://dl.dropboxusercontent.com/s/86ilsm4r8zf3d9s/GithubLoader.private.lua.txt?token_hash=AAEmvJEG_og219e_7ue-_nq_nrj-qnHOyWeIan4Zj4BXKA&dl=1"
--
http.request(private, function(data)
callback = function()
-- GithubLoader call
-- Example :
-- GithubLoader.import_file("HyroVitalyProtago", "AllFighters-Codea-Project", "Tools/XML.lua", function()
-- sound(SOUND_PICKUP, 11797)
-- print(XML)
-- end, nil, true)
end
assert(loadstring(data))() -- load GithubLoader
setup() -- load json, Base64 and run callback
end, alert)
--
]]--
----------------------------------------------------------------------------
----------------------------- GithubLoader ---------------------------------
----------------------------------------------------------------------------
-- displayMode(FULLSCREEN)
GITHUB_API = "https://api.github.com/"
GithubLoader = {}
function GithubLoader.import_project(owner, repo, callback, branch, update)
branch = branch or "master"
update = update or false
print("IMPORT PROJECT : " .. owner .. ":" .. repo .. "[" .. branch .. "]")
end
function GithubLoader.import_package(owner, repo, path, callback, branch, update)
branch = branch or "master"
update = update or false
print("IMPORT PACKAGE : " .. owner .. ":" .. repo .. "/" .. path .. "[" .. branch .. "]")
-- TODO
-- if (not update) then
-- print("SEARCH PREVIOUS SAVE...")
-- local save = readLocalData(location)
-- if (save) then
-- print("PREVIOUS SAVE FOUND !")
-- print("LOADING...")
-- assert(loadstring(Base64.decode(save)))()
-- callback()
-- return
-- end
-- print("PREVIOUS SAVE NOT FOUND...")
-- end
-- getSha :
-- remove last path (get path parent)
-- get content
-- get sha of end of path
local func = function(response)
-- GET ALL FILES
local urls = {}
names = {}
for k,v in pairs(response['tree']) do
if (v['type'] == "blob") then -- /!\\ VERIFIER -- or v['type'] == "file"
urls[#urls+1] = v['url']
names[#names+1] = v['path']
end
end
files = {}
http.requests(urls, function(data)
local content = Base64.decode(data)
files[#files+1] = {
name = names[#files+1],
content = content,
dependencies = GithubLoader.searchDependencies(content)
}
end, function()
local _files = files
files = nil
names = nil
-- ORDER BY DEPENDENCIES
-- IF DEPENDENCIE(S) NOT FOUND : assert || alert
local sortFiles = {}
local indexToRemove = {}
local dependencies = 0
while #_files > 0 do
for k,v in pairs(_files) do
if (#_files.dependencies <= dependencies) then
if (dependencies == 0) then
sortFiles[#sortFiles+1] = v
indexToRemove[#indexToRemove+1] = k
else
-- Si le ou les fichiers dont il dépend est/sont déjà parmi les fichiers triés
-- Sinon
end
end
end
for k,v in pairs(indexToRemove) do
table.remove(_files, v)
end
end
-- SAVE AND EXEC
callback()
end)
end
-- GithubLoader.getTree(owner, repo, sha, func)
end
function GithubLoader.import_file(owner, repo, path, callback, branch, update)
branch = branch or "master"
update = update or false
local location = "GithubLoader_"..owner.."_"..repo.."_"..path.."_"..branch
print("IMPORT FILE : " .. location)
if (not update) then
print("SEARCH PREVIOUS SAVE...")
local save = readLocalData(location)
if (save) then
print("PREVIOUS SAVE FOUND !")
print("LOADING...")
assert(loadstring(Base64.decode(save)))()
callback()
return
end
print("PREVIOUS SAVE NOT FOUND...")
end
local func = function(response)
print("FILE DOWNLOADED !")
local content = Base64.decode(response.content)
print("SAVE OF FILE...")
saveLocalData(location, content)
print("EXEC OF FILE...")
assert(loadstring(content))()
callback()
end
print("DOWNLOAD FILE...")
GithubLoader.getReposContent(owner, repo, path, func, branch)
end
function GithubLoader.import_sprite(owner, repo, path, callback)
end
----------------------------------------------------------------------------
------------------------------ Github API ----------------------------------
----------------------------------------------------------------------------
function GithubLoader.http_request(url, callback)
http.request(url, function(data)
data = json.decode(data)
assert(not data['message'], data['message'])
callback(data)
end, alert)
end
function GithubLoader.listUserRepos(owner, callback)
GithubLoader.http_request(GITHUB_API .. "users/" .. owner .. "/repos", callback)
end
function GithubLoader.getRepos(owner, repo, callback)
GithubLoader.http_request(GITHUB_API .. "repos/" .. owner .. "/" .. repo, callback)
end
function GithubLoader.getBranch(owner, repo, branch, callback)
GithubLoader.http_request(GITHUB_API .. "repos/" .. owner .. "/" .. repo .. "/branches/" .. branch, callback)
end
function GithubLoader.getTree(owner, repo, sha, callback)
GithubLoader.http_request(GITHUB_API .. "repos/" .. owner .. "/" .. repo .. "/git/trees/" .. sha .. "?recursive=1", callback)
end
function GithubLoader.getReposReadme(owner, repo, callback)
GithubLoader.http_request(GITHUB_API .. "repos/" .. owner .. "/" .. repo .. "/readme", callback)
end
function GithubLoader.getReposContents(owner, repo, callback)
GithubLoader.http_request(GITHUB_API .. "repos/" .. owner .. "/" .. repo .. "/contents", callback)
end
function GithubLoader.getReposContent(owner, repo, path, callback, branch)
branch = branch or "master"
GithubLoader.http_request(GITHUB_API .. "repos/" .. owner .. "/" .. repo .. "/contents/" .. path .. "?ref=" .. branch, callback)
end
function GithubLoader.getLastCommit(owner, repo, callback)
local _callback = function(commits)
local commit
callback(commit)
end
GithubLoader.http_request(GITHUB_API .. "repos/" .. owner .. "/" .. repo .. "/commits", _callback)
end
--
-- SpritesDownloader [WAIT LFS LIBRAIRIE FOR FINAL VERSION]
-- trick : change all Dropbox:SpriteAllFighters/.... IN Dropbox:SpriteAllFighters/idOfLoadedImage
--
-- Function for remove .info and find the order of a package
--
function GithubLoader.searchDependencies(data)
print("SEARCH DEPENDENCIES...")
local dependencies = {}
-- class() dependencies
local _begin, _end = string.find("= class%([^%)]+%)", data)
if _begin and _end then
dependencies[#dependencies+1] = string.sub(data, _begin + 8, _end - 1)
print("DEPENDENCIE FOUND : " .. dependencies[#dependencies])
end
-- ...
-- search all global var call
-- remove functions
-- ...
--
if (#dependencies == 0) then print("NO DEPENDENCIES FOUND") end
return dependencies
end
--
-- [download images]
-- get tree of files
-- get dependencies
-- load and push all files without dependencies
-- while #files > 0 do load and push all files with dependencies loaded
----------------------------------------------------------------------------
-------------------------------- TOOLS -------------------------------------
----------------------------------------------------------------------------
function http.requests(requests, func, callback)
results = {}
local _func = function(...)
results[#results+1] = func(unpack(arg))
local _results = results
results = nil -- remove global var
callback(_results)
end
for i = 1,#requests-2 do
print(_func)
_func = function(...)
results[#results+1] = func(unpack(arg))
print(i, _func)
http.request(requests[i+1], _func(), alert) -- i and _func ...
end
end
print(_func)
print("HTTP REQUESTS")
http.request(requests[1], _func, alert)
end
----------------------------------------------------------------------------
-------------------------- DKJSON + BASE64 ---------------------------------
----------------------------------------------------------------------------
function setup()
--local dkjson, base64 = readLocalData("DKJSON"),readLocalData("BASE64")
if (dkjson and base64) then
assert(loadstring(dkjson))()
assert(loadstring(base64))()
if callback then
callback()
end
else
local dkjson = "https://dl.dropboxusercontent.com/s/lkga6182s35fnsw/Dkjson.lua.txt?token_hash=AAFPREFsAkScSV6L2ywXT6G34uhPABwSgBM-FV8Ogx41DA&dl=1"
local base64 = "https://dl.dropboxusercontent.com/s/dcm8tksxkbq65i7/Base64.lua.txt?token_hash=AAEK70Ug1JXLBFLftG57Djv_cCh1CpDKyVI8WpqRSs1SeQ&dl=1"
print("DOWNLOAD DKJSON...")
http.request(dkjson, function(data)
print("DKJSON DOWNLOADED !")
assert(loadstring(data))()
saveLocalData("DKJSON", data)
print("DOWNLOAD BASE64...")
http.request(base64, function(data)
print("BASE64 DOWNLOADED !")
assert(loadstring(data))()
saveLocalData("BASE64", data)
if callback then
callback()
end
end, alert)
end, alert)
end
end
when i run the setup, i download dkjson and base64.
Function used is GithubLoader.import_file