Compare commits
5 Commits
13a0d4dbaf
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
448e0354ea
|
|||
|
2edb549156
|
|||
|
37c404603d
|
|||
|
3bd305dc10
|
|||
|
9f3771cea9
|
75
core.lua
75
core.lua
@@ -43,19 +43,21 @@ function Core:read()
|
||||
if not self then
|
||||
error("No instance!")
|
||||
end
|
||||
|
||||
|
||||
if not self.ws then
|
||||
return false, "Websocket not initiated";
|
||||
end
|
||||
|
||||
return true, textutils.unserialiseJSON(self.ws.receive())
|
||||
local data, success = self.ws.receive()
|
||||
|
||||
return true, textutils.unserialiseJSON(data)
|
||||
end
|
||||
|
||||
function Core:download(file)
|
||||
if not self then
|
||||
error("No instance!")
|
||||
end
|
||||
|
||||
|
||||
if not self.ws then
|
||||
return false, "Websocket not initiated";
|
||||
end
|
||||
@@ -65,15 +67,15 @@ function Core:download(file)
|
||||
local success, data = self:read();
|
||||
|
||||
if not success then
|
||||
return false, "Failed to read: "+data;
|
||||
return false, "Failed to read: "..data;
|
||||
end
|
||||
|
||||
if not data.type then
|
||||
return false, "Invalid package: "+textutils.serialiseJSON(data);
|
||||
return false, "Invalid package: "..textutils.serialiseJSON(data);
|
||||
end
|
||||
|
||||
if not (data.type == "FILE_INFO") then
|
||||
return false, "Invalid package type: "+textutils.serialiseJSON(data);
|
||||
return false, "Invalid package type: "..textutils.serialiseJSON(data);
|
||||
end
|
||||
|
||||
if not data.exists then
|
||||
@@ -85,20 +87,73 @@ function Core:download(file)
|
||||
local success, data = self:read();
|
||||
|
||||
if not success then
|
||||
return false, "Failed to read: "+data;
|
||||
return false, "Failed to read: "..data;
|
||||
end
|
||||
|
||||
if not data.type then
|
||||
return false, "Invalid package: "+textutils.serialiseJSON(data);
|
||||
return false, "Invalid package: "..textutils.serialiseJSON(data);
|
||||
end
|
||||
|
||||
if not data.type == "FILE_DATA" then
|
||||
return false, "Invalid type: "+textutils.serialiseJSON(data)
|
||||
return false, "Invalid type: "..textutils.serialiseJSON(data)
|
||||
end
|
||||
|
||||
if fs.exists(file) then
|
||||
fs.delete(file);
|
||||
end
|
||||
|
||||
local fileStream = io.open(file, "w");
|
||||
fileStream:write(data.fileContent);
|
||||
|
||||
if not fileStream then
|
||||
return false, "Failed to open fileStream"
|
||||
end
|
||||
|
||||
fileStream:write(data.data);
|
||||
fileStream:close();
|
||||
|
||||
return true, "Success"
|
||||
end
|
||||
|
||||
function Core:tick()
|
||||
if not self then
|
||||
error("No instance!")
|
||||
end
|
||||
|
||||
if not self.ws then
|
||||
return false, "Not initilized"
|
||||
end
|
||||
|
||||
self:send({type = "TICK"});
|
||||
|
||||
local success, data = self:read();
|
||||
|
||||
if not success then
|
||||
return false, "Failed to do anything: "..data;
|
||||
end
|
||||
|
||||
print("Server expects Action: "..textutils.serialiseJSON(data))
|
||||
end
|
||||
|
||||
function Core:disconnect()
|
||||
if not self then
|
||||
error("No instance!")
|
||||
end
|
||||
|
||||
if not self.ws then
|
||||
return "Not initilized"
|
||||
end
|
||||
|
||||
self.ws.close();
|
||||
end
|
||||
|
||||
function Core:setLoop(loop)
|
||||
self.loop = loop;
|
||||
end
|
||||
|
||||
function Core:start()
|
||||
while true do
|
||||
if self.loop then
|
||||
self.loop();
|
||||
end
|
||||
end
|
||||
end
|
||||
9
downloadCore.lua
Normal file
9
downloadCore.lua
Normal file
@@ -0,0 +1,9 @@
|
||||
require("core")
|
||||
|
||||
local core = Core.new();
|
||||
|
||||
core:connect();
|
||||
|
||||
core:download("core.lua");
|
||||
|
||||
core:disconnect();
|
||||
Reference in New Issue
Block a user