From aab3a05d6f097be942d695e50382555a8a0b385b Mon Sep 17 00:00:00 2001 From: LemADEC Date: Fri, 29 Jul 2016 02:52:13 +0200 Subject: [PATCH] Fixed LUA scripts crash with invalid data file Added example to initial help message Added help message on termination Added explicit limits on negative movement --- .../warpdriveEnanReactorCore/startup | 5 +++- .../warpdriveMainframe/startup | 30 ++++++++++++------- .../warpdriveShipController/startup | 30 ++++++++++++------- .../warpdriveWeaponController/startup | 5 +++- .../warpdriveEnanReactorCore/autorun.lua | 5 +++- .../warpdriveShipController/autorun.lua | 30 ++++++++++++------- .../warpdriveWeaponController/autorun.lua | 5 +++- 7 files changed, 73 insertions(+), 37 deletions(-) diff --git a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveEnanReactorCore/startup b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveEnanReactorCore/startup index e6c8208d..94ed2938 100644 --- a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveEnanReactorCore/startup +++ b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveEnanReactorCore/startup @@ -426,6 +426,7 @@ function data_read() local file = fs.open("shipdata.txt", "r") data = textutils.unserialize(file.readAll()) file.close() + if data == nil then data = {}; end end if data.reactor_mode == nil then data.reactor_mode = 0; end if data.reactor_rate == nil then data.reactor_rate = 100; end @@ -874,6 +875,7 @@ function connections_page() WriteLn("") SetColorTitle() WriteLn("Please refer to below menu for keyboard controls") + WriteLn("For example, press 1 to access Reactor page") end -- peripheral boot up @@ -946,4 +948,5 @@ if monitors ~= nil then end end SetCursorPos(1, 1) -Write("") +WriteLn("Program terminated") +WriteLn("Type startup to restart it") diff --git a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveMainframe/startup b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveMainframe/startup index 0fe582da..89168d3b 100644 --- a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveMainframe/startup +++ b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveMainframe/startup @@ -703,6 +703,7 @@ function data_read() local file = fs.open("shipdata.txt", "r") data = textutils.unserialize(file.readAll()) file.close() + if data == nil then data = {}; end end if data.core_summon == nil then data.core_summon = false; end if data.reactor_mode == nil then data.reactor_mode = 0; end @@ -902,28 +903,33 @@ function core_page_setMovement() SetColorDefault() SetCursorPos(1, 3) - core_movement[1] = core_page_setDistanceAxis(2, "Front", core_movement[1], math.abs(core_front + core_back + 1)) - core_movement[2] = core_page_setDistanceAxis(3, "Up" , core_movement[2], math.abs(core_up + core_down + 1)) - core_movement[3] = core_page_setDistanceAxis(4, "Right", core_movement[3], math.abs(core_left + core_right + 1)) + core_movement[1] = core_page_setDistanceAxis(2, "Forward" , "Front", "Back", core_movement[1], math.abs(core_front + core_back + 1)) + core_movement[2] = core_page_setDistanceAxis(3, "Vertical", "Up" , "Down", core_movement[2], math.abs(core_up + core_down + 1)) + core_movement[3] = core_page_setDistanceAxis(4, "Lateral" , "Right", "Left", core_movement[3], math.abs(core_left + core_right + 1)) core_movement = { ship.movement(core_movement[1], core_movement[2], core_movement[3]) } end -function core_page_setDistanceAxis(line, axis, userEntry, shipLength) +function core_page_setDistanceAxis(line, axis, positive, negative, userEntry, shipLength) local maximumDistance = shipLength + 127 if core_isInHyper and line ~= 3 then maximumDistance = shipLength + 127 * 100 end + SetCursorPos(1, line + 1) + Write(positive .. " is " .. ( shipLength + 1) .. ", maximum is " .. maximumDistance .. " ") + SetCursorPos(1, line + 2) + Write(negative .. " is " .. (-shipLength - 1) .. ", maximum is " .. -maximumDistance .. " ") repeat SetCursorPos(1, line) - Write(axis .. " (min " .. (shipLength + 1) .. ", max " .. maximumDistance .. "): ") + Write(axis .. " movement: ") userEntry = readInputNumber(userEntry) - if userEntry == 0 then - return userEntry - end - if math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance then + if userEntry ~= 0 and (math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance) then ShowWarning("Wrong distance. Try again.") end - until math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance + until userEntry == 0 or (math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance) + SetCursorPos(1, line + 1) + ClearLine() + SetCursorPos(1, line + 2) + ClearLine() return userEntry end @@ -2058,6 +2064,7 @@ function connections_page() WriteLn("") SetColorTitle() WriteLn("Please refer to below menu for keyboard controls") + WriteLn("For example, press 1 to access Reactor page") end -- peripheral boot up @@ -2161,4 +2168,5 @@ if monitors ~= nil then end end SetCursorPos(1, 1) -Write("") +WriteLn("Program terminated") +WriteLn("Type startup to restart it") diff --git a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveShipController/startup b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveShipController/startup index bf237825..bc362e17 100644 --- a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveShipController/startup +++ b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveShipController/startup @@ -422,6 +422,7 @@ function data_read() local file = fs.open("shipdata.txt", "r") data = textutils.unserialize(file.readAll()) file.close() + if data == nil then data = {}; end end if data.core_summon == nil then data.core_summon = false; end end @@ -609,28 +610,33 @@ function core_page_setMovement() SetColorDefault() SetCursorPos(1, 3) - core_movement[1] = core_page_setDistanceAxis(2, "Front", core_movement[1], math.abs(core_front + core_back + 1)) - core_movement[2] = core_page_setDistanceAxis(3, "Up" , core_movement[2], math.abs(core_up + core_down + 1)) - core_movement[3] = core_page_setDistanceAxis(4, "Right", core_movement[3], math.abs(core_left + core_right + 1)) + core_movement[1] = core_page_setDistanceAxis(2, "Forward" , "Front", "Back", core_movement[1], math.abs(core_front + core_back + 1)) + core_movement[2] = core_page_setDistanceAxis(3, "Vertical", "Up" , "Down", core_movement[2], math.abs(core_up + core_down + 1)) + core_movement[3] = core_page_setDistanceAxis(4, "Lateral" , "Right", "Left", core_movement[3], math.abs(core_left + core_right + 1)) core_movement = { ship.movement(core_movement[1], core_movement[2], core_movement[3]) } end -function core_page_setDistanceAxis(line, axis, userEntry, shipLength) +function core_page_setDistanceAxis(line, axis, positive, negative, userEntry, shipLength) local maximumDistance = shipLength + 127 if core_isInHyper and line ~= 3 then maximumDistance = shipLength + 127 * 100 end + SetCursorPos(1, line + 1) + Write(positive .. " is " .. ( shipLength + 1) .. ", maximum is " .. maximumDistance .. " ") + SetCursorPos(1, line + 2) + Write(negative .. " is " .. (-shipLength - 1) .. ", maximum is " .. -maximumDistance .. " ") repeat SetCursorPos(1, line) - Write(axis .. " (min " .. (shipLength + 1) .. ", max " .. maximumDistance .. "): ") + Write(axis .. " movement: ") userEntry = readInputNumber(userEntry) - if userEntry == 0 then - return userEntry - end - if math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance then + if userEntry ~= 0 and (math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance) then ShowWarning("Wrong distance. Try again.") end - until math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance + until userEntry == 0 or (math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance) + SetCursorPos(1, line + 1) + ClearLine() + SetCursorPos(1, line + 2) + ClearLine() return userEntry end @@ -900,6 +906,7 @@ function connections_page() WriteLn("") SetColorTitle() WriteLn("Please refer to below menu for keyboard controls") + WriteLn("For example, press 1 to access Ship core page") end -- peripheral boot up @@ -982,4 +989,5 @@ if monitors ~= nil then end end SetCursorPos(1, 1) -Write("") +WriteLn("Program terminated") +WriteLn("Type startup to restart it") diff --git a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveWeaponController/startup b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveWeaponController/startup index 635a88db..cb16c73e 100644 --- a/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveWeaponController/startup +++ b/src/main/resources/assets/warpdrive/lua.ComputerCraft/warpdriveWeaponController/startup @@ -444,6 +444,7 @@ function data_read() local file = fs.open("shipdata.txt", "r") data = textutils.unserialize(file.readAll()) file.close() + if data == nil then data = {}; end end if data.laser_batteries == nil then data.laser_batteries = {}; end laser_batteries = data.laser_batteries @@ -1329,6 +1330,7 @@ function connections_page() WriteLn("") SetColorTitle() WriteLn("Please refer to below menu for keyboard controls") + WriteLn("For example, press 1 to access Laser batteries page") end -- peripheral boot up @@ -1405,4 +1407,5 @@ if monitors ~= nil then end end SetCursorPos(1, 1) -Write("") +WriteLn("Program terminated") +WriteLn("Type startup to restart it") diff --git a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveEnanReactorCore/autorun.lua b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveEnanReactorCore/autorun.lua index a66fdd41..e0673cb4 100644 --- a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveEnanReactorCore/autorun.lua +++ b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveEnanReactorCore/autorun.lua @@ -378,6 +378,7 @@ function data_read() data = serialization.unserialize(rawData) end file:close() + if data == nil then data = {}; end end if data.reactor_mode == nil then data.reactor_mode = 0; end if data.reactor_rate == nil then data.reactor_rate = 100; end @@ -814,6 +815,7 @@ function connections_page() WriteLn("") SetColorTitle() WriteLn("Please refer to below menu for keyboard controls") + WriteLn("For example, press 1 to access Reactor page") end -- peripheral boot up @@ -881,4 +883,5 @@ until abort SetMonitorColorFrontBack(0xFFFFFF, 0x000000) term.clear() SetCursorPos(1, 1) -Write("") +WriteLn("Program terminated") +WriteLn("Type reboot to restart it") diff --git a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveShipController/autorun.lua b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveShipController/autorun.lua index 699710f4..4c39905e 100644 --- a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveShipController/autorun.lua +++ b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveShipController/autorun.lua @@ -374,6 +374,7 @@ function data_read() data = serialization.unserialize(rawData) end file:close() + if data == nil then data = {}; end end if data.core_summon == nil then data.core_summon = false; end end @@ -562,28 +563,33 @@ function core_page_setMovement() SetColorDefault() SetCursorPos(1, 3) - core_movement[1] = core_page_setDistanceAxis(2, "Front", core_movement[1], math.abs(core_front + core_back + 1)) - core_movement[2] = core_page_setDistanceAxis(3, "Up" , core_movement[2], math.abs(core_up + core_down + 1)) - core_movement[3] = core_page_setDistanceAxis(4, "Right", core_movement[3], math.abs(core_left + core_right + 1)) + core_movement[1] = core_page_setDistanceAxis(2, "Forward" , "Front", "Back", core_movement[1], math.abs(core_front + core_back + 1)) + core_movement[2] = core_page_setDistanceAxis(3, "Vertical", "Up" , "Down", core_movement[2], math.abs(core_up + core_down + 1)) + core_movement[3] = core_page_setDistanceAxis(4, "Lateral" , "Right", "Left", core_movement[3], math.abs(core_left + core_right + 1)) core_movement = { ship.movement(core_movement[1], core_movement[2], core_movement[3]) } end -function core_page_setDistanceAxis(line, axis, userEntry, shipLength) +function core_page_setDistanceAxis(line, axis, positive, negative, userEntry, shipLength) local maximumDistance = shipLength + 127 if core_isInHyper and line ~= 3 then maximumDistance = shipLength + 127 * 100 end + SetCursorPos(1, line + 1) + Write(positive .. " is " .. ( shipLength + 1) .. ", maximum is " .. maximumDistance .. " ") + SetCursorPos(1, line + 2) + Write(negative .. " is " .. (-shipLength - 1) .. ", maximum is " .. -maximumDistance .. " ") repeat SetCursorPos(1, line) - Write(axis .. " (min " .. (shipLength + 1) .. ", max " .. maximumDistance .. "): ") + Write(axis .. " movement: ") userEntry = readInputNumber(userEntry) - if userEntry == 0 then - return userEntry - end - if math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance then + if userEntry ~= 0 and (math.abs(userEntry) <= shipLength or math.abs(userEntry) > maximumDistance) then ShowWarning("Wrong distance. Try again.") end - until math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance + until userEntry == 0 or (math.abs(userEntry) > shipLength and math.abs(userEntry) <= maximumDistance) + SetCursorPos(1, line + 1) + ClearLine() + SetCursorPos(1, line + 2) + ClearLine() return userEntry end @@ -847,6 +853,7 @@ function connections_page() WriteLn("") SetColorTitle() WriteLn("Please refer to below menu for keyboard controls") + WriteLn("For example, press 1 to access Ship core page") end -- peripheral boot up @@ -924,4 +931,5 @@ end SetMonitorColorFrontBack(0xFFFFFF, 0x000000) term.clear() SetCursorPos(1, 1) -Write("") +WriteLn("Program terminated") +WriteLn("Type reboot to restart it") diff --git a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveWeaponController/autorun.lua b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveWeaponController/autorun.lua index 8f2a1108..6cc368ed 100644 --- a/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveWeaponController/autorun.lua +++ b/src/main/resources/assets/warpdrive/lua.OpenComputers/warpdriveWeaponController/autorun.lua @@ -396,6 +396,7 @@ function data_read() data = serialization.unserialize(rawData) end file:close() + if data == nil then data = {}; end end if data.laser_batteries == nil then data.laser_batteries = {}; end laser_batteries = data.laser_batteries @@ -1259,6 +1260,7 @@ function connections_page() WriteLn("") SetColorTitle() WriteLn("Please refer to below menu for keyboard controls") + WriteLn("For example, press 1 to access Laser batteries page") end -- peripheral boot up @@ -1330,4 +1332,5 @@ until abort SetMonitorColorFrontBack(0xFFFFFF, 0x000000) term.clear() SetCursorPos(1, 1) -Write("") +WriteLn("Program terminated") +WriteLn("Type reboot to restart it")