/
itchy.lua
181 lines (181 loc) · 5.31 KB
/
itchy.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
local thread
thread = function(...)
require("love.thread")
require("love.timer")
local timer
do
local _obj_0 = love
thread, timer = _obj_0.thread, _obj_0.timer
end
local http = require("socket.http")
local ok, libcurl = pcall(function()
return require("luajit-request")
end)
if not (ok) then
ok, libcurl = pcall(function()
return require("lib.luajit-request")
end)
if not (ok) then
libcurl = nil
end
end
local request
request = function(data)
local result = { }
if not (libcurl) then
if data.luajit_request then
ok, libcurl = pcall(function()
return require(data.luajit_request)
end)
if not (ok) then
libcurl = nil
end
end
end
if libcurl then
local response = libcurl.send(data.url or "https://api.itch.io/wharf/latest?target=" .. tostring(data.target) .. "&channel_name=" .. tostring(data.channel))
result.body = response.body
result.status = response.code
else
if not (data.proxy) then
result.message = "Could not load libcurl."
return nil, result
end
result.body, result.status = http.request(data.url or tostring(data.proxy) .. "/get/https://api.itch.io/wharf/latest?target=" .. tostring(data.target) .. "&channel_name=" .. tostring(data.channel))
if not (result.body) then
result.message = "socket.http.request error: " .. tostring(result.status)
result.status = nil
return nil, result
end
end
return true, result
end
local check
check = function(data)
local send = thread.getChannel(data.thread_channel or "itchy")
local exponential_backoff = 1
while true do
local _continue_0 = false
repeat
do
local result = { }
if not (data.url or data.target) then
result.message = "'target' or 'url' be must be defined!"
send:push(result)
return false
end
ok, result = request(data)
if not (ok) then
send:push(result)
return false
end
result.version = result.body:match('%s*{%s*"latest"%s*:%s*"(.+)"%s*}%s*')
result.version = tonumber(result.version) or result.version
if data.version then
result.latest = result.version == data.version
end
if result.status ~= 200 and (not result.version) then
result.message = "unknown, error getting latest version: HTTP " .. tostring(result.status) .. ", trying again in " .. tostring(exponential_backoff) .. " seconds..."
send:push(result)
timer.sleep(exponential_backoff)
exponential_backoff = exponential_backoff * 2
if exponential_backoff > 10 * 60 then
exponential_backoff = 10 * 60
end
_continue_0 = true
break
elseif result.latest ~= nil then
if result.latest then
result.message = tostring(result.version) .. ", you have the latest vesion"
else
result.message = tostring(result.version) .. ", there is a newer version available!"
end
else
result.message = result.version
end
send:push(result)
return true
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
end
local start
start = function(data)
if data.proxy == nil and (not data.url) then
data.proxy = "http://insecure-proxy.tangentfox.com"
end
if not (data.channel) then
require("love.system")
local os = love.system.getOS()
local _exp_0 = os
if "OS X" == _exp_0 then
data.channel = "osx"
elseif "Windows" == _exp_0 then
data.channel = "win32"
elseif "Linux" == _exp_0 then
data.channel = "linux"
elseif "Android" == _exp_0 then
data.channel = "android"
elseif "iOS" == _exp_0 then
data.channel = "ios"
else
data.channel = os
end
end
check(data)
if data.interval then
while true do
timer.sleep(data.interval)
check(data)
end
end
end
return start(...)
end
if arg == nil then
return thread(...)
end
local thread_data = love.filesystem.newFileData(string.dump(thread), "itchy version checker")
local counter = 1
local configs, results = { }, { }
local default_data
local itchy = {
check_version = function(self, data)
if not (default_data) then
default_data = data
end
if (not data.thread_channel) and next(configs) then
data.thread_channel = "itchy-" .. tostring(counter)
counter = counter + 1
end
configs[data] = data
return love.thread.newThread(thread_data):start(data)
end,
new_version = function(self, data)
if data == nil then
data = default_data
end
if data and configs[data] then
local channel = love.thread.getChannel(data.thread_channel or "itchy")
if channel:getCount() > 0 then
results[data] = channel:demand()
end
return results[data]
end
end,
kill_version_checker = function(self, data)
if data == nil then
data = default_data
end
configs[data] = nil
results[data] = nil
if data == default_data then
default_data = nil
end
end
}
return itchy