Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ allow_defined_top = true
max_line_length = false
codes = true

-- Ideally reenable these warnings later
redefined = false

ignore = {
"512" -- Loop can be executed at most once.
}
-- Something to think about in the future
-- max_cyclomatic_complexity = 10

Expand Down
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: python # Can use any language here, but if it's not 'python'
# it becomes necessary to pass '--user' to pip when installing hererocks.
sudo: false # Use container-based infrastructure.

env:
- LUA="lua 5.1"

before_install:
- pip install hererocks
- hererocks env --$LUA -rlatest # Use latest LuaRocks, install into 'env' directory.
- source env/bin/activate # Add directory with all installed binaries to PATH.
- luarocks install luacheck

#install:
# - luarocks make # Install the rock, assuming there is a rockspec
# # in the root of the repository.

script:
# '--enable 1' means 'global = true'
# IDEs tend to have issues with 'global = true', as they work on a per-file basis
# this allows us to use the same .luacheckrc for both travis and IDEs
# (Disabled for now)
# - luacheck LuaMenu --enable 1
- luacheck LuaMenu
30 changes: 15 additions & 15 deletions LuaMenu/Addons/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-- compat-5.1 if using Lua 5.0
--
-- CHANGELOG
-- 0.9.20 Introduction of local Lua functions for private functions (removed _ function prefix).
-- 0.9.20 Introduction of local Lua functions for private functions (removed _ function prefix).
-- Fixed Lua 5.1 compatibility issues.
-- Introduced json.null to have null values in associative arrays.
-- encode() performance improvement (more than 50%) through table.concat rather than ..
Expand Down Expand Up @@ -65,19 +65,19 @@ function json.encode (v)
if v==nil then
return "null"
end

local vtype = type(v)

-- Handle strings
if vtype == 'string' then
return '"' .. encodeString(v) .. '"' -- Need to handle encoding in string
end

-- Handle booleans
if vtype=='number' or vtype=='boolean' then
return tostring(v)
end

-- Handle tables
if vtype=='table' then
local rval = {}
Expand All @@ -100,12 +100,12 @@ function json.encode (v)
return '{' .. table.concat(rval,',') .. '}'
end
end

-- Handle null values
if vtype=='function' and v==null then
return 'null'
end

assert(false,'encode attempt to encode unsupported type ' .. vtype .. ':' .. tostring(v))
end

Expand Down Expand Up @@ -137,7 +137,7 @@ function json.decode(s, startPos)
if curChar==[["]] or curChar==[[']] then
return decode_scanString(s,startPos)
end

-- Otherwise, it must be a constant
return decode_scanConstant(s,startPos)
end
Expand Down Expand Up @@ -199,14 +199,14 @@ function decode_scanComment(s, startPos)
assert( string.sub(s,startPos,startPos+1)=='/*', "decode_scanComment called but comment does not start at position " .. startPos)
local endPos = string.find(s,'*/',startPos+2)
assert(endPos~=nil, "Unterminated comment in string at " .. startPos)
return endPos+2
return endPos+2
end

--- Scans for given constants: true, false or null
-- Returns the appropriate Lua type, and the position of the next character to read.
-- @param s The string being scanned.
-- @param startPos The position in the string at which to start scanning.
-- @return object, int The object (true, false or nil) and the position at which the next character should be
-- @return object, int The object (true, false or nil) and the position at which the next character should be
-- scanned.
function decode_scanConstant(s, startPos)
local consts = { ["true"] = true, ["false"] = false, ["null"] = nil }
Expand Down Expand Up @@ -298,7 +298,7 @@ function decode_scanString(s,startPos)
local stringLen = string.len(s)
repeat
local curChar = string.sub(s,endPos,endPos)
if not escaped then
if not escaped then
if curChar==[[\]] then
escaped = true
else
Expand All @@ -314,7 +314,7 @@ function decode_scanString(s,startPos)
local stringValue = 'return ' .. string.sub(s, startPos, endPos-1)
local stringEval = loadstring(stringValue)
assert(stringEval, 'Failed to load string [ ' .. stringValue .. '] in JSON4Lua.decode_scanString at position ' .. startPos .. ' : ' .. endPos)
return stringEval(), endPos
return stringEval(), endPos
end

--- Scans a JSON string skipping all whitespace from the current start position.
Expand Down Expand Up @@ -348,7 +348,7 @@ function encodeString(s)
s = string.gsub(s,"'","\\'")
s = string.gsub(s,'\n','\\n')
s = string.gsub(s,'\t','\\t')
return s
return s
end

-- Determines whether the given Lua type is an array or a table / dictionary.
Expand All @@ -358,7 +358,7 @@ end
-- @param t The table to evaluate as an array
-- @return boolean, number True if the table can be represented as an array, false otherwise. If true,
-- the second returned value is the maximum
-- number of indexed elements in the array.
-- number of indexed elements in the array.
function isArray(t)

--
Expand All @@ -372,7 +372,7 @@ function isArray(t)
return (array_count == table_count), array_count

--[[
-- Next we count all the elements, ensuring that any non-indexed elements are not-encodable
-- Next we count all the elements, ensuring that any non-indexed elements are not-encodable
-- (with the possible exception of 'n')
local maxIndex = 0
for k,v in base.pairs(t) do
Expand All @@ -398,6 +398,6 @@ end
-- @return boolean True if the object should be JSON encoded, false if it should be ignored.
function isEncodable(o)
local t = type(o)
return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null)
return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null)
end

6 changes: 3 additions & 3 deletions LuaMenu/Addons/tablefunctions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ function Spring.Utilities.TableToString(data, key)
end
if dataType == "string" then
local cleaned = data:gsub("\n", "\\n"):gsub("\r", "\\r"):gsub("\t", "\\t"):gsub("\a", "\\a"):gsub("\v", "\\v"):gsub("\"", "\\\"")
return key .. [[="]] .. cleaned .. [["]]
return key .. [[="]] .. cleaned .. [["]]
elseif dataType == "number" then
return key .. "=" .. data
return key .. "=" .. data
elseif dataType == "boolean" then
return key .. "=" .. ((data and "true") or "false")
elseif dataType == "table" then
Expand Down Expand Up @@ -175,7 +175,7 @@ function Spring.Utilities.CustomKeyToUsefulTable(dataRaw)
dataRaw = string.gsub(dataRaw, '_', '=')
dataRaw = Spring.Utilities.Base64Decode(dataRaw)
local dataFunc, err = loadstring("return " .. dataRaw)
if dataFunc then
if dataFunc then
local success, usefulTable = pcall(dataFunc)
if success then
if collectgarbage then
Expand Down
56 changes: 28 additions & 28 deletions LuaMenu/Addons/timeFunctions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ local function FixTimeOutOfBounds(timeTable)
timeTable[i + 1] = timeTable[i + 1] + 1
end
end

repeat
local updated = false
-- Overflow
local daysInThisMonth = monthDays[timeTable[5]] or 31
if timeTable[5] == 2 and IsLeapYear(timeTable[6]) then
daysInThisMonth = 29
end

if timeTable[4] > daysInThisMonth then -- Some bases are one index and some are zero indexed FFS!!
timeTable[4] = timeTable[4] - daysInThisMonth
timeTable[5] = timeTable[5] + 1
Expand All @@ -86,7 +86,7 @@ local function FixTimeOutOfBounds(timeTable)
timeTable[6] = timeTable[6] + 1
updated = true
end

-- Underflow
local daysInLastMonth = monthDays[(timeTable[5] - 2)%12 + 1] or 31
if timeTable[4] < 1 then
Expand All @@ -100,7 +100,7 @@ local function FixTimeOutOfBounds(timeTable)
updated = true
end
until (not updated)

return timeTable
end

Expand All @@ -112,15 +112,15 @@ function Spring.Utilities.FormatTime(seconds, includeSeconds)
if seconds < 0 then
return (includeSeconds and "0s") or "0m"
end

local hours = math.floor(seconds/3600)
local minutes = math.floor(seconds/60)%60
local seconds = math.floor(seconds)%60

--Spring.Echo("pastTime", pastTime[1], pastTime[2], pastTime[3], pastTime[4], "pastSeconds", pastSeconds)
--Spring.Echo("currentTime", currentTime[1], currentTime[2], currentTime[3], currentTime[4], "currentSeconds", currentSeconds)
--Spring.Echo("seconds", seconds)

local timeText = ""
if hours > 0 then
timeText = timeText .. hours .. "h "
Expand All @@ -131,7 +131,7 @@ function Spring.Utilities.FormatTime(seconds, includeSeconds)
if includeSeconds then
timeText = timeText .. seconds .. "s "
end

return timeText
end

Expand All @@ -150,10 +150,10 @@ function Spring.Utilities.ArchaicFormatDate(timeTable, translator)
else
daySuffix = "th"
end

local timeSuffix = (timeTable[3] < 12 and "AM") or "PM"
local hour = (timeTable[3] - 1)%12 + 1

local stringToFormat = "%d" .. daySuffix .. " of " .. translator("month_" .. timeTable[5]) .. " at " .. "%d:%02d " .. timeSuffix
local timeString = string.format(stringToFormat, timeTable[4], hour, timeTable[2])
return timeString
Expand Down Expand Up @@ -209,7 +209,7 @@ function Spring.Utilities.GetTimeToPast(pastTimeString, includeSeconds)
-- Always assume that the past time is one day behind.
currentSeconds = currentSeconds + 86400
end

return Spring.Utilities.FormatTime(currentSeconds - pastSeconds, includeSeconds)
end

Expand All @@ -225,16 +225,16 @@ function Spring.Utilities.GetTimeDifferenceTable(targetTime, currentTime)
tonumber(os.date("!%m")), -- Month
tonumber(os.date("!%Y")), -- Year
}

for i = 1, #targetTime do
if not (targetTime[i] and currentTime[i]) then
return false
end
end

-- Order times.
local before, after = currentTime, targetTime

local targetInTheFuture = true
for i = 6, 1, -1 do
if before[i] ~= after[i] then
Expand All @@ -245,22 +245,22 @@ function Spring.Utilities.GetTimeDifferenceTable(targetTime, currentTime)
break
end
end

-- Add days based on year difference.
local year = before[6]
while before[6] < after[6] do
after[4] = after[4] + DaysInAYear(before[6])
before[6] = before[6] + 1
end

-- Convert month to day.
before[4] = before[4] + MonthToDays(before)
after[4] = after[4] + MonthToDays(after)

-- Clear year and month now that conversion to day is complete.
after[5] = 0
after[6] = 0

-- Do subtraction with unbounded days.
for i = 1, 4 do
after[i] = after[i] - before[i]
Expand All @@ -269,7 +269,7 @@ function Spring.Utilities.GetTimeDifferenceTable(targetTime, currentTime)
after[i + 1] = after[i + 1] - 1
end
end

return after, targetInTheFuture
end

Expand Down Expand Up @@ -304,7 +304,7 @@ function Spring.Utilities.TimeStringToTable(timeString)
return false
end
end

return timeTable
end

Expand All @@ -316,7 +316,7 @@ function Spring.Utilities.GetTimeDifference(targetTimeString, otherTime)
if otherTime then
otherTime = Spring.Utilities.TimeStringToTable(otherTime)
end

local difference, targetInTheFuture = Spring.Utilities.GetTimeDifferenceTable(targetTime, otherTime)
local timeText, isNow = Spring.Utilities.FormatRelativeTime(difference, targetInTheFuture)
return timeText, targetInTheFuture, isNow
Expand All @@ -331,23 +331,23 @@ function Spring.Utilities.UtcToLocal(utcTimeString)
tonumber(os.date("%m")), -- Month
tonumber(os.date("%Y")), -- Year
}

for i = 1, #localTime do
if not localTime[i] then
return false
end
end

local utcTime = Spring.Utilities.TimeStringToTable(utcTimeString)
if not utcTime then
return false
end

local difference, localInTheFuture = Spring.Utilities.GetTimeDifferenceTable(localTime)
if not difference then
return
end

if localInTheFuture then
for i = 1, 6 do
utcTime[i] = utcTime[i] + difference[i]
Expand All @@ -357,7 +357,7 @@ function Spring.Utilities.UtcToLocal(utcTimeString)
utcTime[i] = utcTime[i] - difference[i]
end
end

return FixTimeOutOfBounds(utcTime)
end

Expand All @@ -378,7 +378,7 @@ function Spring.Utilities.GetCurrentUtc()
tonumber(os.date("!%m")),
tonumber(os.date("!%Y")),
}

return string.format("%04d-%02d-%02dT%02d:%02d:%02d", t[6], t[5], t[4], t[3], t[2], t[1])
end

Expand All @@ -391,6 +391,6 @@ function Spring.Utilities.GetCompactCurrentUtc()
tonumber(os.date("!%m")),
tonumber(os.date("!%Y")),
}

return string.format("%04d%02d%02d_%02d%02d%02d", t[6], t[5], t[4], t[3], t[2], t[1])
end
Loading