-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathversion.lua
More file actions
291 lines (246 loc) · 10.5 KB
/
Copy pathversion.lua
File metadata and controls
291 lines (246 loc) · 10.5 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
Version = { }
-- Version Format: YYYY-MM-DD
local http = require('socket.http')
local json = require('json')
local curatedReleaseList = { }
local apiURL = 'https://api.github.com/repos/RaraProjects/rest/releases'
local mainBranchURL = 'https://github.com/RaraProjects/rest/archive/refs/heads/main.zip'
local maxReleaseCount = 3
local versionDataPulled = false
local recentDownload = nil
local hasTestRelease = false
-- ------------------------------------------------------------------------------------------------------
-- Gets asset data from the Github JSON results.
-- ------------------------------------------------------------------------------------------------------
---@param release table
---@return table
-- ------------------------------------------------------------------------------------------------------
local getAssetData = function(release)
if type(release.assets) ~= 'table' then
return { }
end
if release.assets[1] then
return
{
downloadURL = release.assets[1].browser_download_url,
downloadCount = release.assets[1].download_count,
updateTime = release.assets[1].updated_at,
}
end
return { }
end
-- ------------------------------------------------------------------------------------------------------
-- Create a list of most recent release and recent pre-releases.
-- ------------------------------------------------------------------------------------------------------
---@param allReleases table
---@return table
-- ------------------------------------------------------------------------------------------------------
local function buildUpdateList(allReleases)
local list = {}
local prereleaseCount = 0
local stableRelease
-- Check for most recent release.
for _, release in ipairs(allReleases) do
if release.downloadURL and not release.isPrerelease then
stableRelease = release
break
end
end
if stableRelease then
table.insert(list, stableRelease)
end
-- Check for recent pre-releases.
for _, release in ipairs(allReleases) do
if release.downloadURL and release.isPrerelease and prereleaseCount < maxReleaseCount then
table.insert(list, release)
prereleaseCount = prereleaseCount + 1
if prereleaseCount >= maxReleaseCount then
break
end
end
end
return list
end
-- ------------------------------------------------------------------------------------------------------
-- Convert the Github string format to YYYY-MM-DD.
-- ------------------------------------------------------------------------------------------------------
---@param isoDateTime string
---@return string
-- ------------------------------------------------------------------------------------------------------
local convertDateString = function(isoDateTime)
if isoDateTime and type(isoDateTime) == 'string' then
return isoDateTime:sub(1, 10)
end
return isoDateTime
end
-- ------------------------------------------------------------------------------------------------------
-- Try to launch a web URL in the browser.
-- ------------------------------------------------------------------------------------------------------
---@param url string
-- ------------------------------------------------------------------------------------------------------
local openUrl = function(url)
Ashita.Chat.Echo(string.format('Opening URL: %s', url))
if not url then
return
end
if os.getenv('OS') == 'Windows_NT' then
os.execute(string.format('start %s', url))
end
end
-- ------------------------------------------------------------------------------------------------------
-- Try to launch a Windows directory in the file explorer.
-- ------------------------------------------------------------------------------------------------------
---@param path string
-- ------------------------------------------------------------------------------------------------------
local openFolder = function(path)
if not path then
return
end
path = path:gsub('/', '\\')
Ashita.Chat.Echo(string.format('Opening Directory: %s', path))
os.execute(string.format('explorer %s', path))
end
-- ------------------------------------------------------------------------------------------------------
-- Shows the first version update screen.
-- ------------------------------------------------------------------------------------------------------
local uiGetGitHubData = function()
if versionDataPulled == false then
UI.Text('Clicking this button will...')
UI.Text('- Check Github for recent Rest releases.')
UI.Text('- Likely cause a very short system stutter.')
UI.Text('- Show you a table of Rest versions (in game).')
UI.Text('- NOT download files to your computer.')
UI.Text('')
if UI.Button('Check for new Rest releases.') then
Version.PullGithubData()
versionDataPulled = true
end
end
end
-- ------------------------------------------------------------------------------------------------------
-- Basic install instructions.
-- ------------------------------------------------------------------------------------------------------
local basicInstallInstructions = function()
UI.Text('1. Click Addon Folder button above to open your addons folder.')
UI.Text('2. Move your rest.zip file to addons folder.')
UI.Text('3. Unzip rest.zip.')
UI.Text('4. You can overwrite the rest files.')
end
-- ------------------------------------------------------------------------------------------------------
-- Instructions that show after a download has taken place.
-- ------------------------------------------------------------------------------------------------------
local uiInstructions = function()
if recentDownload then
UI.Text('')
UI.Text(string.format('You downloaded: %s! Next steps...', recentDownload))
basicInstallInstructions()
elseif hasTestRelease then
UI.Text('')
UI.Text('Brave soul! You downloaded the cutting edge development. Next steps...')
basicInstallInstructions()
UI.Text('5. If something breaks (badly) fall back to a release or pre-release.')
UI.Text('6. Leave a comment in the Rest Discord or Github Issues page.')
end
end
-- ------------------------------------------------------------------------------------------------------
-- Displays the version table.
-- ------------------------------------------------------------------------------------------------------
Version.Populate = function()
uiGetGitHubData()
if versionDataPulled == false then
return
end
UI.Text(string.format('Your version is: %s.', addon.version))
if UI.Button('Ashita Addon Folder - Quick Access') then
openFolder(string.format('%saddons\\', AshitaCore:GetInstallPath()))
end
UI.Text('')
UI.Text('Clicking...')
UI.Text('- Doc: Open release info on Github website.')
UI.Text('- Get: DOWNLOAD the release .zip file.')
UI.Text('')
local showReleaseColor = true
local colFlags = bit.bor(ImGuiTableColumnFlags_None)
local tableFlags = bit.bor(
ImGuiTableFlags_SizingFixedFit,
ImGuiTableFlags_Resizable,
ImGuiTableFlags_PadOuterX,
ImGuiTableFlags_Borders,
ImGuiTableFlags_NoHostExtendX
)
if UI.BeginTable('Versions', 6, tableFlags) then
UI.TableSetupColumn('Version', colFlags)
UI.TableSetupColumn('Pre-release?', colFlags)
UI.TableSetupColumn('Update Date', colFlags)
UI.TableSetupColumn('Downloads', colFlags)
UI.TableSetupColumn('Info', colFlags)
UI.TableSetupColumn('DL', colFlags)
UI.TableHeadersRow()
for _, release in ipairs(curatedReleaseList) do
UI.TableNextColumn() UI.Text(string.format('%s', release.version))
UI.TableNextColumn() UI.Text(string.format('%s', release.isPrerelease and 'Yes' or ''))
UI.TableNextColumn() UI.Text(string.format('%s', convertDateString(release.updateTime)))
UI.TableNextColumn() UI.Text(string.format('%s', release.downloadCount))
UI.TableNextColumn()
UI.PushID(string.format('Doc %s', release.version))
if UI.SmallButton('Doc') then
openUrl(release.htmlURL)
end
UI.PopID()
UI.TableNextColumn()
UI.PushID(string.format('Get %s', release.version))
if UI.SmallButton('Get') then
openUrl(release.downloadURL)
recentDownload = release.version
hasTestRelease = false
end
UI.PopID()
if showReleaseColor then
Window.TableRowColor(1)
showReleaseColor = false
else
Window.TableRowColor(0)
end
end
UI.EndTable()
end
UI.Text('')
UI.Text('Feel free to try the newest unreleased features. It\'s what I use.')
if UI.Button('Download - UNRELEASED EXPERIMENTAL - May Break!') then
openUrl(mainBranchURL)
recentDownload = nil
hasTestRelease = true
end
uiInstructions()
end
-- ------------------------------------------------------------------------------------------------------
-- Pull the release JSON data via Github API.
-- ------------------------------------------------------------------------------------------------------
Version.PullGithubData = function()
local releaseData = { }
local body, status = http.request(apiURL)
if status ~= 200 or not body then
Ashita.Chat.Echo('Unable to reach GitHub.')
return
end
local apiReleases = json.decode(body)
local tagName
for _, release in ipairs(apiReleases) do
tagName = release.tag_name
if tagName then
local assetData = getAssetData(release)
table.insert(releaseData,
{
version = tagName,
isPrerelease = release.prerelease or false,
isDateTag = true,
downloadURL = assetData.downloadURL,
downloadCount = assetData.downloadCount,
updateTime = assetData.updateTime,
htmlURL = release.html_url,
notes = release.body,
})
end
end
curatedReleaseList = buildUpdateList(releaseData)
end