gravitowl

store

Dec 21st, 2024 (edited)
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. local baseURL = "https://raw.githubusercontent.com/MentaalAchtergesteld/CC-SCRIPTS/refs/heads/main/";
  2. local storeURL = baseURL .. "store.json";
  3. local scriptsURL = baseURL .. "scripts/";
  4. local libURL = baseURL .. "lib/";
  5.  
  6. local screenWidth, screenHeight = term.getSize();
  7.  
  8. local function fetchScripts()
  9. print("Fetching list of available scripts...")
  10. local response = http.get(storeURL);
  11. if not response then
  12. error("Failed to fetch script list.");
  13. end
  14.  
  15. local content = response.readAll();
  16. response.close();
  17.  
  18. local decoded = textutils.unserializeJSON(content);
  19. if not decoded then
  20. error("Failed to decode JSON data.");
  21. end
  22.  
  23. return decoded;
  24. end
  25.  
  26. local function isInstalled(scriptName)
  27. return fs.exists("/" .. scriptName .. ".lua");
  28. end
  29.  
  30. local function installScript(script)
  31. print(string.format("[INFO] Installing %s...", script.name));
  32. local response = http.get(scriptsURL .. script.file);
  33. if not response then
  34. error("Failed to download %s", script.name);
  35. end
  36.  
  37. local content = response.readAll();
  38. response.close();
  39.  
  40. local file = fs.open("/" .. script.name .. ".lua", "w");
  41. file.write(content);
  42. file.close();
  43.  
  44. print(string.format("Installed %s!", script.name));
  45. end
  46.  
  47. local function updateScript(script)
  48. if not isInstalled(script.name) then
  49. print("Script is not installed.");
  50. return;
  51. end
  52.  
  53. print(string.format("Updating %s...", script.name));
  54. installScript(script);
  55. end
  56.  
  57. local function removeScript(script)
  58. print(string.format("Removing %s...", script.name));
  59.  
  60. if isInstalled(script.name) then
  61. fs.delete("/" .. script.name .. ".lua");
  62. print(string.format("Removed %s.", script.name));
  63. else
  64. print("Script is not installed.");
  65. end
  66. end
  67.  
  68. local function drawScriptsList(scripts, selectedIndex)
  69. term.clear();
  70. term.setCursorPos(1,1);
  71. print("=== Script Store ===");
  72.  
  73. for i=1, math.min(screenHeight - 3, #scripts) do
  74. local scriptIndex = i + (selectedIndex - 1);
  75. if scriptIndex > #scripts then break end
  76.  
  77. local script = scripts[scriptIndex];
  78. if scriptIndex == selectedIndex then
  79. term.setTextColor(colors.lightGray);
  80. else
  81. term.setTextColor(colors.white);
  82. end
  83.  
  84. print(string.format("[%d] %s", scriptIndex, script.name));
  85. end
  86.  
  87. term.setTextColor(colors.white);
  88.  
  89. term.setCursorPos(1, screenHeight);
  90. term.write("[Q] Quit");
  91. end
  92.  
  93. local function drawScriptDetails(script)
  94. term.clear();
  95. term.setCursorPos(1, 1);
  96. print("=== Script Details ===");
  97. print("Name: " .. script.name);
  98. print("Description: " .. script.description);
  99. print();
  100. print("[I] Install | [U] Update | [R] Remove | [B] Back");
  101. end
  102.  
  103. local function main()
  104. local scripts = fetchScripts();
  105. local selectedIndex = 1;
  106.  
  107. local running = true;
  108.  
  109. local UIStates = {
  110. ScriptList = 1,
  111. ScriptDetail = 2
  112. }
  113.  
  114. local state = UIStates.ScriptList;
  115.  
  116. while running do
  117. if state == UIStates.ScriptList then
  118. drawScriptsList(scripts, selectedIndex);
  119. local event, key = os.pullEvent("key");
  120.  
  121. if key == keys.up then
  122. selectedIndex = math.max(selectedIndex - 1, 1);
  123. elseif key == keys.down then
  124. selectedIndex = math.min(selectedIndex + 1, #scripts);
  125. elseif key == keys.enter then
  126. state = UIStates.ScriptDetail;
  127. elseif key == keys.q then
  128. break;
  129. end
  130. elseif state == UIStates.ScriptDetail then
  131. local script = scripts[selectedIndex];
  132. drawScriptDetails(script);
  133.  
  134. local event, char = os.pullEvent("char");
  135. if char == "i" then
  136. installScript(script);
  137. elseif char == "u" then
  138. updateScript(script);
  139. elseif char == "r" then
  140. removeScript(script);
  141. elseif char == "b" then
  142. state = UIStates.ScriptList;
  143. end
  144. end
  145.  
  146. sleep(0.1);
  147. end
  148.  
  149. print("Quitting store.");
  150. sleep(0.5);
  151. term.clear();
  152. term.setCursorPos(1, 1);
  153. end
  154.  
  155. main();
Advertisement
Add Comment
Please, Sign In to add comment