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
47 changes: 40 additions & 7 deletions KISSMultiplayer/lua/ge/extensions/kissmp/ui/download.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ local function bytes_to_mb(bytes)
return (bytes / 1024) / 1024
end

local function format_eta(seconds)
if seconds < 0 then seconds = 0 end
local hours = math.floor(seconds / 3600)
local minutes = math.floor((seconds % 3600) / 60)
local secs = math.floor(seconds % 60)

if hours > 0 then
return string.format("%02d:%02d:%02d", hours, minutes, secs)
end
return string.format("%02d:%02d", minutes, secs)
end

local function draw(gui)
if not kissui.show_download then return end

Expand Down Expand Up @@ -37,19 +49,41 @@ local function draw(gui)
imgui.ProgressBar(download_status.progress, imgui.ImVec2(split_width, 0))

local mod = kissmods.mods[download_status.name]
total_size = total_size + mod.size
downloaded_size = downloaded_size + (mod.size * download_status.progress)
local mod_size = (mod and mod.size) or 0
total_size = total_size + mod_size
downloaded_size = downloaded_size + (mod_size * download_status.progress)
end
end
imgui.EndChild()

total_size = bytes_to_mb(total_size)
downloaded_size = bytes_to_mb(downloaded_size)
local progress = downloaded_size / total_size
local total_size_bytes = network.download_total_bytes or 0
local downloaded_size_bytes = network.downloaded_bytes or 0
if total_size_bytes <= 0 then
total_size_bytes = total_size
downloaded_size_bytes = downloaded_size
end

total_size = bytes_to_mb(total_size_bytes)
downloaded_size = bytes_to_mb(downloaded_size_bytes)
local progress_text = tostring(math.floor(downloaded_size)) .. "MB / " .. tostring(math.floor(total_size)) .. "MB"

local elapsed = 0
if (network.download_start_time or 0) > 0 then
elapsed = socket.gettime() - network.download_start_time
end
if elapsed <= 0 then elapsed = 0.001 end
local progress_speed = downloaded_size / elapsed
local speed_text = tostring(math.floor(progress_speed)) .. "MB/s"

local eta_text = "--:--"
if progress_speed > 0 and downloaded_size < total_size then
local eta_seconds = (total_size - downloaded_size) / progress_speed
eta_text = format_eta(eta_seconds)
end

content_width = imgui.GetWindowContentRegionWidth()
split_width = content_width * 0.495
split_width = content_width * 0.450
progress_text = progress_text .. " (" .. speed_text .. ", ETA " .. eta_text .. ")"
local text_size = imgui.CalcTextSize(progress_text)
local extra_size = split_width - text_size.x

Expand All @@ -60,7 +94,6 @@ local function draw(gui)
end
imgui.SameLine()
if imgui.Button("Cancel###cancel_download", imgui.ImVec2(split_width, -1)) then
network.cancel_download()
kissui.show_download = false
network.disconnect()
end
Expand Down
Loading