kaibochan

com.lua

Mar 9th, 2023 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.42 KB | None | 0 0
  1. local function sendError(sentFrom, errorMessage, protocol)
  2.     rednet.send(sentFrom, errorMessage, protocol)
  3. end
  4.  
  5. --true values:  "true",  1
  6. --false values: "false", 0
  7. --note that nil is not a false value, it remains nil
  8. function tobooleanstrict(variable)
  9.     local boolean
  10.    
  11.     if type(variable) == "string" then
  12.         if variable == "true" then
  13.             boolean = true
  14.         elseif variable == "false" then
  15.             boolean = false
  16.         end
  17.     elseif type(variable) == "number" then
  18.         if variable == 1 then
  19.             boolean = true
  20.         elseif variable == 0 then
  21.             boolean = false
  22.         end
  23.     end
  24.  
  25.     return boolean
  26. end
  27.  
  28. function validateParameters(actions, sentFrom, action, parameters, protocol)
  29.     --number of parameters does not match
  30.     local numParametersExpected = #actions[action]["parameters"]
  31.     local numParametersGiven = #parameters
  32.  
  33.     if numParametersExpected ~= numParametersGiven then
  34.         local errorMessage = "error \"Error: "..action.." expected "..numParametersExpected.." parameters, recieved "..numParametersGiven.."\""
  35.         sendError(sentFrom, errorMessage, protocol)
  36.         return
  37.     end
  38.  
  39.     --parameter types do not match
  40.     for index, parameter in ipairs(actions[action]["parameters"]) do
  41.         local expectedType = parameter["type"]
  42.         local parameterName = parameter["name"]
  43.         local castedParameter
  44.  
  45.         --parameters is indexed with value 1 since they are removed
  46.         --at the end of the loop, later parameters will shift down
  47.         if expectedType == "number" then
  48.             castedParameter = tonumber(parameters[1])
  49.         elseif expectedType == "boolean" then
  50.             castedParameter = tobooleanstrict(parameters[1])
  51.         elseif expectedType == "string" then
  52.             castedParameter = parameters[1]
  53.         elseif expectedType == "table" then
  54.             castedParameter = textutils.unserialise(parameters[1])
  55.         end
  56.  
  57.         if not castedParameter then
  58.             local errorMessage = "error \"Error: "..action.." expected type "..expectedType.." for parameter "..parameterName.."\""
  59.             sendError(sentFrom, errorMessage, protocol)
  60.             return
  61.         end
  62.  
  63.         --replace indexed parameter string value with a named
  64.         --key, value pair of correct type
  65.         --remove at position 1 since the other parameters will shuffle
  66.         --downwards whenever the bottom one is removed
  67.         table.remove(parameters, 1)
  68.         parameters[parameterName] = castedParameter
  69.     end
  70.  
  71.     return parameters
  72. end
  73.  
  74. --group together words that have quotations around them
  75. --then remove quotations
  76. function groupQuotedParameters(parameters)
  77.     local joinWords = false
  78.     local foundEndQuote = false
  79.     local quotingTable = false
  80.  
  81.     local joinedParameters = {}
  82.     local insertIndex = 1
  83.  
  84.     for index, parameter in ipairs(parameters) do
  85.         --if word begins with a quotation mark, begin joining
  86.         if not quotingTable and parameter:sub(1,1) == "\"" then
  87.             --remove quote at beginning of word
  88.             parameter = parameter:sub(2, #parameter)
  89.             joinWords = true
  90.  
  91.             --if quoted text is the beginning of a table: "{, set quotingTable flag
  92.             --ignore further quotations until we encounter the end of the quoted table: }"
  93.             if parameter:sub(1,1) == "{" then
  94.                 quotingTable = true
  95.             end
  96.         end
  97.  
  98.         --quotations are ignored while quoting a table
  99.         --}" appears at the end of a quoted table
  100.         if parameter:sub(#parameter, #parameter) == "\"" and not quotingTable
  101.         or parameter:sub(#parameter - 1, #parameter) == "}\"" then
  102.             --remove quotation mark at end of word
  103.             parameter = parameter:sub(1, #parameter - 1)
  104.             foundEndQuote = true
  105.             quotingTable = false
  106.         end
  107.  
  108.         --if joining words and words have already been added, concatenate
  109.         --otherwise, insert parameter into joinedParameters table
  110.         if joinWords and joinedParameters[insertIndex] then
  111.             joinedParameters[insertIndex] = joinedParameters[insertIndex].." "..parameter
  112.         else
  113.             table.insert(joinedParameters, insertIndex, parameter)
  114.         end
  115.  
  116.         --if word ends with a quotation mark, stop joining
  117.         if foundEndQuote then
  118.             foundEndQuote = false
  119.             joinWords = false
  120.         end
  121.  
  122.         --if not joining words then increment insertion point
  123.         if not joinWords then
  124.             insertIndex = insertIndex + 1
  125.         end
  126.     end
  127.  
  128.     return joinedParameters
  129. end
  130.  
  131. function parseMessage(actions, sentFrom, message, protocol)
  132.     local words = {}
  133.     for w in message:gmatch("%S+") do
  134.         table.insert(words, w)
  135.     end
  136.  
  137.     --action does not exist
  138.     if not actions[words[1]] then
  139.         sendError(sentFrom, "error \"Command invalid: "..words[1].."\"", protocol)
  140.         return
  141.     end
  142.  
  143.     --compose message into an action and its parameters
  144.     local action = words[1]
  145.     local parameters = {}
  146.     for index = 2, #words do
  147.         table.insert(parameters, words[index])
  148.     end
  149.  
  150.     --group quoted parameters together, such as "oak log"
  151.     parameters = groupQuotedParameters(parameters)
  152.  
  153.     --validate and store casted parameters
  154.     parameters = validateParameters(actions, sentFrom, action, parameters, protocol)
  155.  
  156.     return action, parameters
  157. end
Advertisement
Add Comment
Please, Sign In to add comment