From 13c523b84959c24261539d2f26ee8b970117fef8 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Tue, 7 Jul 2020 00:33:22 +0200 Subject: [PATCH 1/9] Add lib --- kai/lib.lua | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 kai/lib.lua diff --git a/kai/lib.lua b/kai/lib.lua new file mode 100644 index 0000000..7d8b45e --- /dev/null +++ b/kai/lib.lua @@ -0,0 +1,76 @@ +-- Do not lose state when reset (chunkloading etc) +state = (function () + local path = ".state" + local state_table = {} + + if fs.exists(path) then + local f = fs.open(path, "r") + state_table = textutils.unserialize(f.readAll()) + f.close() + end + + return setmetatable({}, { + __index = state_table, + + __newindex = function(self, k, v) + rawset(state_table, k, v) + local f = fs.open(path, "w") + f.write(textutils.serialize(state_table)) + f.close() + end, + }) +end)() + +--[[ + orientation: + 0 = South (+z) + 1 = West (-x) + 2 = North (-z) + 3 = East (+x) +]] + +function move(raw_move, position_update) + return function() + success, err = raw_move() + if not success then + return false, err + end + position_update() + end +end + +return { + state = state, + + fwd = move(turtle.forward, function() + if state.o == 1 or state.o == 3 then + state.x = state.x + ((state.o == 1) and -1 or 1) + else + state.z = state.z + ((state.o == 2) and -1 or 1) + end + end), + + back = move(turtle.back, function() + if state.o == 1 or state.o == 3 then + state.x = state.x + ((state.o == 1 and 1 or -1)) + else + state.z = state.z + ((state.o == 2) and 1 or -1) + end + end), + + up = move(turtle.up, function() + state.y = state.y + 1 + end), + + down = move(turtle.down, function() + state.y = state.y - 1 + end), + + left = move(turtle.turnLeft, function() + state.o = (state.o - 1) % 4 + end), + + right = move(turtle.turnRight, function() + state.o = (state.o + 1) % 4 + end), +} \ No newline at end of file From 5ac9658c3aebce6fbc8f4d32eb0fdc76fc33e5e9 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Tue, 7 Jul 2020 00:58:47 +0200 Subject: [PATCH 2/9] Update lib --- kai/lib.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kai/lib.lua b/kai/lib.lua index 7d8b45e..602567a 100644 --- a/kai/lib.lua +++ b/kai/lib.lua @@ -21,6 +21,10 @@ state = (function () }) end)() +if state.x == nil or state.y == nil or state.z == nil or state.o == nil then + print("[WARNING] position is not set") +end + --[[ orientation: 0 = South (+z) @@ -45,7 +49,7 @@ return { fwd = move(turtle.forward, function() if state.o == 1 or state.o == 3 then state.x = state.x + ((state.o == 1) and -1 or 1) - else + elseif state.o == 0 or state.o == 2 then state.z = state.z + ((state.o == 2) and -1 or 1) end end), @@ -53,7 +57,7 @@ return { back = move(turtle.back, function() if state.o == 1 or state.o == 3 then state.x = state.x + ((state.o == 1 and 1 or -1)) - else + elseif state.o == 0 or state.o == 2 then state.z = state.z + ((state.o == 2) and 1 or -1) end end), From b54c2f2257441d87979c3c9565ce62785a0abe5a Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Tue, 7 Jul 2020 02:30:49 +0200 Subject: [PATCH 3/9] Update lib --- kai/lib.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kai/lib.lua b/kai/lib.lua index 602567a..42e3ca4 100644 --- a/kai/lib.lua +++ b/kai/lib.lua @@ -22,7 +22,12 @@ state = (function () end)() if state.x == nil or state.y == nil or state.z == nil or state.o == nil then - print("[WARNING] position is not set") + printError("[lib] position is not set") + printError("[lib] defaulting to 0, 0, 0, +z") + state.x = 0 + state.y = 0 + state.z = 0 + state.o = 0 end --[[ @@ -33,6 +38,10 @@ end 3 = East (+x) ]] +function getOrientation() + return ({"+z", "-x", "-z", "+x"})[1 + state.o] +end + function move(raw_move, position_update) return function() success, err = raw_move() @@ -40,12 +49,15 @@ function move(raw_move, position_update) return false, err end position_update() + return true end end return { state = state, + getOrientation = getOrientation, + fwd = move(turtle.forward, function() if state.o == 1 or state.o == 3 then state.x = state.x + ((state.o == 1) and -1 or 1) From c01b9304b9210daf18b8bf8dac073aa5dc707b38 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Tue, 7 Jul 2020 04:10:10 +0200 Subject: [PATCH 4/9] git gud --- kai/gitgud.lua | 132 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 kai/gitgud.lua diff --git a/kai/gitgud.lua b/kai/gitgud.lua new file mode 100644 index 0000000..495cb1c --- /dev/null +++ b/kai/gitgud.lua @@ -0,0 +1,132 @@ +--[[ + +Usages: +> gitgud sync +download file from repo + +> gitgud delete +delete downloaded file + +> gitgud +re-download all synced files + +> gitgud run +fetch and exec file from repo + +]] + +if not http then error("http is not enabled") end + +local function get(url) + print("GET " .. url) + local response, err = http.get(url) + if not response then + printError(err) + return + end + + local status, err = response.getResponseCode() + if status ~= 200 then + printError(status .. " " .. err) + return + end + print(status .. " " .. err) + + local result = response.readAll() + response.close() + return result +end + +function ensure_dotlua(path) + if string.match(path, "%.lua$") then + return path + else + return path .. ".lua" + end +end + +local function gitea_url(path) + return "https://gitea.leafbla.de/dominic/turtles/raw/branch/master/".. path +end + +local function download_file(repo_path, local_path) + local result = get(gitea_url(repo_path)) + if not result then + error("download failed") + end + + local f = fs.open(local_path, "w") + f.write(result) + f.close() +end + +-- keep track of downloaded files +local storage_path = ".gitgud" + +local storage = {} +if fs.exists(storage_path) then + local f = fs.open(storage_path, "r") + storage = textutils.unserialize(f.readAll()) + f.close() +end + +local function update_storage() + local f = fs.open(storage_path, "w") + f.write(textutils.serialize(storage)) + f.close() +end + +local args = {...} + +if #args == 0 then + + for local_path, repo_path in pairs(storage) do + download_file(repo_path, local_path) + end + +elseif args[1] == "sync" then + local usage = "gitgud sync " + local repo_path = args[2] or error(usage) + local local_path = args[3] or error(usage) + + repo_path = ensure_dotlua(repo_path) + local_path = ensure_dotlua(local_path) + + storage[local_path] = repo_path + update_storage() + + download_file(repo_path, local_path) + +elseif args[1] == "delete" then + local usage = "gitgud delete " + local local_path = args[2] or error(usage) + + local_path = ensure_dotlua(local_path) + + storage[local_path] = nil + update_storage() + + fs.delete(local_path) + +elseif args[1] == "run" then + local usage = "gitgud run [args...]" + local repo_path = args[2] or error(usage) + + repo_path = ensure_dotlua(repo_path) + + local result = get(gitea_url(repo_path)) + if not result then + error("download failed") + end + + local func, err = load(result, repo_path, "t", _ENV) + if not func then + printError(err) + return + end + local success, err = pcall(func, select(3, ...)) + if not success then + printError(err) + end + +end \ No newline at end of file From 8485ae5f32fa62aab8b6d479bc18e7e20ee6a3f2 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Tue, 7 Jul 2020 04:27:58 +0200 Subject: [PATCH 5/9] git even gudder --- kai/gitgud.lua | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/kai/gitgud.lua b/kai/gitgud.lua index 495cb1c..6bd7e80 100644 --- a/kai/gitgud.lua +++ b/kai/gitgud.lua @@ -18,19 +18,16 @@ fetch and exec file from repo if not http then error("http is not enabled") end local function get(url) - print("GET " .. url) local response, err = http.get(url) if not response then - printError(err) - return + return nil, err end local status, err = response.getResponseCode() if status ~= 200 then - printError(status .. " " .. err) + return nil, (status .. " " .. err) return end - print(status .. " " .. err) local result = response.readAll() response.close() @@ -50,14 +47,19 @@ local function gitea_url(path) end local function download_file(repo_path, local_path) - local result = get(gitea_url(repo_path)) + write("Downloading " .. repo_path .. " as " .. local_path .. "... ") + local result, err = get(gitea_url(repo_path)) if not result then - error("download failed") + print() + printError(err) + return end local f = fs.open(local_path, "w") f.write(result) f.close() + + print("OK") end -- keep track of downloaded files @@ -79,11 +81,14 @@ end local args = {...} if #args == 0 then + print("Syncing " .. #storage .. " files.") for local_path, repo_path in pairs(storage) do download_file(repo_path, local_path) end + print("Done.") + elseif args[1] == "sync" then local usage = "gitgud sync " local repo_path = args[2] or error(usage) @@ -114,9 +119,13 @@ elseif args[1] == "run" then repo_path = ensure_dotlua(repo_path) - local result = get(gitea_url(repo_path)) + write("Downloading " .. repo_path .. "... ") + local result, err = get(gitea_url(repo_path)) if not result then - error("download failed") + print() + printError(err) + else + print("OK") end local func, err = load(result, repo_path, "t", _ENV) From 1b0a016222bb4c299b9db895c4ed002e9ddfcb05 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Tue, 7 Jul 2020 04:30:41 +0200 Subject: [PATCH 6/9] Fix --- kai/gitgud.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/kai/gitgud.lua b/kai/gitgud.lua index 6bd7e80..9b963b4 100644 --- a/kai/gitgud.lua +++ b/kai/gitgud.lua @@ -26,7 +26,6 @@ local function get(url) local status, err = response.getResponseCode() if status ~= 200 then return nil, (status .. " " .. err) - return end local result = response.readAll() From 8355faffad41a5582ff0ddf9965ba89075facaa5 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Tue, 7 Jul 2020 04:36:33 +0200 Subject: [PATCH 7/9] git goodest --- kai/gitgud.lua | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/kai/gitgud.lua b/kai/gitgud.lua index 9b963b4..d1aa8f3 100644 --- a/kai/gitgud.lua +++ b/kai/gitgud.lua @@ -51,7 +51,7 @@ local function download_file(repo_path, local_path) if not result then print() printError(err) - return + return false end local f = fs.open(local_path, "w") @@ -59,6 +59,7 @@ local function download_file(repo_path, local_path) f.close() print("OK") + return true end -- keep track of downloaded files @@ -80,13 +81,24 @@ end local args = {...} if #args == 0 then - print("Syncing " .. #storage .. " files.") - for local_path, repo_path in pairs(storage) do - download_file(repo_path, local_path) + -- Lua being Lua, of course #storage won't work + local count = 0 + for _ in pairs(storage) do + count = count + 1 end - print("Done.") + print("Syncing " .. count .. " files.") + + local success_count = 0 + for local_path, repo_path in pairs(storage) do + local success = download_file(repo_path, local_path) + if success then + success_count = success_count + 1 + end + end + + print("Done. " .. success_count .. " out of " .. count .. "files synced.") elseif args[1] == "sync" then local usage = "gitgud sync " @@ -137,4 +149,8 @@ elseif args[1] == "run" then printError(err) end +else + print("Unknown command " .. args[1]) + print("Try \"sync\", \"delete\" or \"run\"") + end \ No newline at end of file From a5eb903c9046bdb4d2d174e22a893fe20576c3c7 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Tue, 7 Jul 2020 04:37:35 +0200 Subject: [PATCH 8/9] Fix --- kai/gitgud.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kai/gitgud.lua b/kai/gitgud.lua index d1aa8f3..5f848f4 100644 --- a/kai/gitgud.lua +++ b/kai/gitgud.lua @@ -98,7 +98,7 @@ if #args == 0 then end end - print("Done. " .. success_count .. " out of " .. count .. "files synced.") + print("Done. " .. success_count .. " out of " .. count .. " files synced.") elseif args[1] == "sync" then local usage = "gitgud sync " From b3f659b2b7fc48b295503b985fc502275af3fd62 Mon Sep 17 00:00:00 2001 From: Kai Vogelgesang Date: Tue, 7 Jul 2020 13:23:52 +0200 Subject: [PATCH 9/9] Fix --- kai/gitgud.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kai/gitgud.lua b/kai/gitgud.lua index 5f848f4..2aa2d0f 100644 --- a/kai/gitgud.lua +++ b/kai/gitgud.lua @@ -33,7 +33,7 @@ local function get(url) return result end -function ensure_dotlua(path) +local function ensure_dotlua(path) if string.match(path, "%.lua$") then return path else