forked from mcclure/emu-coop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.lua
94 lines (81 loc) · 2.61 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
-- UTILITIES
function nonempty(s) return s and s ~= "" end
function nonzero(s) return s and s ~= 0 end
-- TICKER
local currentError = nil
local currentMessages = List()
local MESSAGE_DURATION = 600
-- Set the current message (optionally, make it an error)
function message(msg, isError)
if isError then
currentError = msg
else
currentMessages:put({msg, life=MESSAGE_DURATION})
end
if msg then print(msg) end -- Double to the Lua log
end
function statusMessage(msg)
message(msg, true)
end
function errorMessage(msg)
message(msg and "Error: " .. msg, true)
end
-- Version string tools
function parseVersion(s)
local split = stringx.split(s)
if not split[1] then return null end
local split2 = stringx.split(split[1], ".")
if not (split2[1] and split2[2]) then return null end
local variant = {}
for x=2,#split do table.insert(variant, split[x]) end
return {major=split2[1], minor=split2[2], patch=split2[3], variant=variant}
end
-- The idea is supposed to be: Same major version and equal or greater minor version are "compatible".
-- Patch versions are ignored.
-- "beta" versions are only compatible with other beta versions.
function parsedVersionMatches(mine, theirs, exact)
if not (mine and theirs) then -- Blank == wildcard
return false
end
local mineMajor = tonumber(mine.major)
local mineMinor = tonumber(mine.minor)
local theirsMajor = tonumber(theirs.major)
local theirsMinor = tonumber(theirs.minor)
if not theirsMajor or not theirsMinor or
(theirsMajor > mineMajor) or -- FIXME: This is not how semver works but all versions are currently 1.x so whatever
(exact and theirsMajor < mineMajor) or
(theirsMajor == mineMajor and theirsMinor > mineMinor) or
(exact and theirsMinor < mineMinor) then
return false
end
if tablex.find(theirs.variant, "beta") and not tablex.find(mine.variant, "beta") then
return false
end
return true
end
function versionMatches(mine, theirs, exact) -- If exact is false allow "downgrades"
mine = mine and parseVersion(mine)
theirs = theirs and parseVersion(theirs)
return parsedVersionMatches(mine, theirs, exact)
end
-- Callback to print the current error message
function printMessage()
local msg = null
if currentError then
msg = currentError
else
while currentMessages:len() > 0 do
local messageRecord = currentMessages[#currentMessages]
if messageRecord.life <= 0 then
currentMessages:pop()
else
msg = messageRecord[1]
messageRecord.life = messageRecord.life - 1
break
end
end
end
if msg then
gui.text(5, 254-40, msg)
end
end