Removed obselete functions, changed download bars a bit. renamed files correctly

This commit is contained in:
cake
2025-11-30 08:09:58 +01:00
parent cab13874d8
commit a9181d2592
24 changed files with 357 additions and 197 deletions

View File

@@ -24,6 +24,15 @@ public class DownloadUi : WindowMediatorSubscriberBase
private readonly PairProcessingLimiter _pairProcessingLimiter;
private readonly ConcurrentDictionary<GameObjectHandler, bool> _uploadingPlayers = new();
private readonly Dictionary<GameObjectHandler, Vector2> _smoothed = [];
private readonly Dictionary<GameObjectHandler, DownloadSpeedTracker> _downloadSpeeds = new();
private sealed class DownloadSpeedTracker
{
public long LastBytes;
public double LastTime;
public double SpeedBytesPerSecond;
}
private bool _notificationDismissed = true;
private int _lastDownloadStateHash = 0;
@@ -96,117 +105,52 @@ public class DownloadUi : WindowMediatorSubscriberBase
{
if (_configService.Current.ShowTransferWindow)
{
var limiterSnapshot = _pairProcessingLimiter.GetSnapshot();
try
DrawDownloadSummaryBox();
if (_configService.Current.ShowUploading)
{
if (_fileTransferManager.IsUploading)
const int transparency = 100;
foreach (var player in _uploadingPlayers.Select(p => p.Key).ToList())
{
var currentUploads = _fileTransferManager.GetCurrentUploadsSnapshot();
var totalUploads = currentUploads.Count;
var screenPos = _dalamudUtilService.WorldToScreen(player.GetGameObject());
if (screenPos == Vector2.Zero) continue;
var doneUploads = currentUploads.Count(c => c.IsTransferred);
var totalUploaded = currentUploads.Sum(c => c.Transferred);
var totalToUpload = currentUploads.Sum(c => c.Total);
try
{
using var _ = _uiShared.UidFont.Push();
var uploadText = "Uploading";
var textSize = ImGui.CalcTextSize(uploadText);
UiSharedService.DrawOutlinedFont($"▲", ImGuiColors.DalamudWhite, new Vector4(0, 0, 0, 255), 1);
ImGui.SameLine();
var xDistance = ImGui.GetCursorPosX();
UiSharedService.DrawOutlinedFont($"Compressing+Uploading {doneUploads}/{totalUploads}",
ImGuiColors.DalamudWhite, new Vector4(0, 0, 0, 255), 1);
ImGui.NewLine();
ImGui.SameLine(xDistance);
UiSharedService.DrawOutlinedFont(
$"{UiSharedService.ByteToString(totalUploaded, addSuffix: false)}/{UiSharedService.ByteToString(totalToUpload)}",
ImGuiColors.DalamudWhite, new Vector4(0, 0, 0, 255), 1);
if (_currentDownloads.Any()) ImGui.Separator();
}
}
catch
{
_logger.LogDebug("Error drawing upload progress");
}
try
{
// Check if download notifications are enabled (not set to TextOverlay)
var useNotifications = _configService.Current.UseLightlessNotifications
? _configService.Current.LightlessDownloadNotification != NotificationLocation.TextOverlay
: _configService.Current.UseNotificationsForDownloads;
if (useNotifications)
{
// Use notification system
if (_currentDownloads.Any())
{
UpdateDownloadNotificationIfChanged(limiterSnapshot);
_notificationDismissed = false;
}
else if (!_notificationDismissed)
{
Mediator.Publish(new LightlessNotificationDismissMessage("pair_download_progress"));
_notificationDismissed = true;
_lastDownloadStateHash = 0;
}
}
else
{
// Use text overlay
if (limiterSnapshot.IsEnabled)
{
var queueColor = limiterSnapshot.Waiting > 0 ? ImGuiColors.DalamudYellow : ImGuiColors.DalamudGrey;
var queueText = $"Pair queue {limiterSnapshot.InFlight}/{limiterSnapshot.Limit}";
queueText += limiterSnapshot.Waiting > 0 ? $" ({limiterSnapshot.Waiting} waiting, {limiterSnapshot.Remaining} free)" : $" ({limiterSnapshot.Remaining} free)";
UiSharedService.DrawOutlinedFont(queueText, queueColor, new Vector4(0, 0, 0, 255), 1);
ImGui.NewLine();
}
else
{
UiSharedService.DrawOutlinedFont("Pair apply limiter disabled", ImGuiColors.DalamudGrey, new Vector4(0, 0, 0, 255), 1);
ImGui.NewLine();
}
foreach (var item in _currentDownloads.ToList())
{
var dlSlot = item.Value.Count(c => c.Value.DownloadStatus == DownloadStatus.WaitingForSlot);
var dlQueue = item.Value.Count(c => c.Value.DownloadStatus == DownloadStatus.WaitingForQueue);
var dlProg = item.Value.Count(c => c.Value.DownloadStatus == DownloadStatus.Downloading);
var dlDecomp = item.Value.Count(c => c.Value.DownloadStatus == DownloadStatus.Decompressing);
var totalFiles = item.Value.Sum(c => c.Value.TotalFiles);
var transferredFiles = item.Value.Sum(c => c.Value.TransferredFiles);
var totalBytes = item.Value.Sum(c => c.Value.TotalBytes);
var transferredBytes = item.Value.Sum(c => c.Value.TransferredBytes);
UiSharedService.DrawOutlinedFont($"▼", ImGuiColors.DalamudWhite, new Vector4(0, 0, 0, 255), 1);
ImGui.SameLine();
var xDistance = ImGui.GetCursorPosX();
var drawList = ImGui.GetBackgroundDrawList();
UiSharedService.DrawOutlinedFont(
$"{item.Key.Name} [W:{dlSlot}/Q:{dlQueue}/P:{dlProg}/D:{dlDecomp}]",
ImGuiColors.DalamudWhite, new Vector4(0, 0, 0, 255), 1);
ImGui.NewLine();
ImGui.SameLine(xDistance);
UiSharedService.DrawOutlinedFont(
$"{transferredFiles}/{totalFiles} ({UiSharedService.ByteToString(transferredBytes, addSuffix: false)}/{UiSharedService.ByteToString(totalBytes)})",
ImGuiColors.DalamudWhite, new Vector4(0, 0, 0, 255), 1);
drawList,
uploadText,
screenPos with { X = screenPos.X - textSize.X / 2f - 1, Y = screenPos.Y - textSize.Y / 2f - 1 },
UiSharedService.Color(255, 255, 0, transparency),
UiSharedService.Color(0, 0, 0, transparency),
2
);
}
catch
{
_logger.LogDebug("Error drawing upload progress");
}
}
}
catch
{
_logger.LogDebug("Error drawing download progress");
}
}
if (_configService.Current.ShowTransferBars)
{
const int transparency = 100;
const int dlBarBorder = 3;
const float rounding = 6f;
var shadowOffset = new Vector2(2, 2);
foreach (var transfer in _currentDownloads.ToList())
{
var transferKey = transfer.Key;
var rawPos = _dalamudUtilService.WorldToScreen(transferKey.GetGameObject());
//If RawPos is zero, remove it from smoothed dictionary
if (rawPos == Vector2.Zero)
{
@@ -214,43 +158,66 @@ public class DownloadUi : WindowMediatorSubscriberBase
continue;
}
//Smoothing out the movement and fix jitter around the position.
Vector2 screenPos = _smoothed.TryGetValue(transferKey, out var lastPos) ? (rawPos - lastPos).Length() < 4f ? lastPos : rawPos : rawPos;
_smoothed[transferKey] = screenPos;
Vector2 screenPos = _smoothed.TryGetValue(transferKey, out var lastPos)
? (rawPos - lastPos).Length() < 4f ? lastPos : rawPos
: rawPos;
_smoothed[transferKey] = screenPos;
var totalBytes = transfer.Value.Sum(c => c.Value.TotalBytes);
var transferredBytes = transfer.Value.Sum(c => c.Value.TransferredBytes);
var maxDlText = $"{UiSharedService.ByteToString(totalBytes, addSuffix: false)}/{UiSharedService.ByteToString(totalBytes)}";
var textSize = _configService.Current.TransferBarsShowText ? ImGui.CalcTextSize(maxDlText) : new Vector2(10, 10);
var textSize = _configService.Current.TransferBarsShowText
? ImGui.CalcTextSize(maxDlText)
: new Vector2(10, 10);
int dlBarHeight = _configService.Current.TransferBarsHeight > ((int)textSize.Y + 5) ? _configService.Current.TransferBarsHeight : (int)textSize.Y + 5;
int dlBarWidth = _configService.Current.TransferBarsWidth > ((int)textSize.X + 10) ? _configService.Current.TransferBarsWidth : (int)textSize.X + 10;
int dlBarHeight = _configService.Current.TransferBarsHeight > ((int)textSize.Y + 5)
? _configService.Current.TransferBarsHeight
: (int)textSize.Y + 5;
int dlBarWidth = _configService.Current.TransferBarsWidth > ((int)textSize.X + 10)
? _configService.Current.TransferBarsWidth
: (int)textSize.X + 10;
var dlBarStart = new Vector2(screenPos.X - dlBarWidth / 2f, screenPos.Y - dlBarHeight / 2f);
var dlBarEnd = new Vector2(screenPos.X + dlBarWidth / 2f, screenPos.Y + dlBarHeight / 2f);
// Precompute rects
var outerStart = new Vector2(dlBarStart.X - dlBarBorder - 1, dlBarStart.Y - dlBarBorder - 1);
var outerEnd = new Vector2(dlBarEnd.X + dlBarBorder + 1, dlBarEnd.Y + dlBarBorder + 1);
var borderStart = new Vector2(dlBarStart.X - dlBarBorder, dlBarStart.Y - dlBarBorder);
var borderEnd = new Vector2(dlBarEnd.X + dlBarBorder, dlBarEnd.Y + dlBarBorder);
var drawList = ImGui.GetBackgroundDrawList();
drawList.AddRectFilled(
dlBarStart with { X = dlBarStart.X - dlBarBorder - 1, Y = dlBarStart.Y - dlBarBorder - 1 },
dlBarEnd with { X = dlBarEnd.X + dlBarBorder + 1, Y = dlBarEnd.Y + dlBarBorder + 1 },
UiSharedService.Color(0, 0, 0, transparency), 1);
drawList.AddRectFilled(dlBarStart with { X = dlBarStart.X - dlBarBorder, Y = dlBarStart.Y - dlBarBorder },
dlBarEnd with { X = dlBarEnd.X + dlBarBorder, Y = dlBarEnd.Y + dlBarBorder },
UiSharedService.Color(220, 220, 220, transparency), 1);
drawList.AddRectFilled(dlBarStart, dlBarEnd,
UiSharedService.Color(0, 0, 0, transparency), 1);
//Shadow, background, border, bar background
drawList.AddRectFilled(outerStart + shadowOffset, outerEnd + shadowOffset, UiSharedService.Color(0, 0, 0, transparency / 2), rounding + 2);
drawList.AddRectFilled(outerStart, outerEnd, UiSharedService.Color(0, 0, 0, transparency), rounding + 2);
drawList.AddRectFilled(borderStart, borderEnd, UiSharedService.Color(220, 220, 220, transparency), rounding);
drawList.AddRectFilled(dlBarStart, dlBarEnd, UiSharedService.Color(0, 0, 0, transparency), rounding);
var dlProgressPercent = transferredBytes / (double)totalBytes;
drawList.AddRectFilled(dlBarStart,
dlBarEnd with { X = dlBarStart.X + (float)(dlProgressPercent * dlBarWidth) },
UiSharedService.Color(UIColors.Get("LightlessPurple")));
var progressEndX = dlBarStart.X + (float)(dlProgressPercent * dlBarWidth);
var progressEnd = new Vector2(progressEndX, dlBarEnd.Y);
drawList.AddRectFilled(
dlBarStart,
progressEnd,
UiSharedService.Color(UIColors.Get("LightlessPurple")),
rounding
);
if (_configService.Current.TransferBarsShowText)
{
var downloadText = $"{UiSharedService.ByteToString(transferredBytes, addSuffix: false)}/{UiSharedService.ByteToString(totalBytes)}";
UiSharedService.DrawOutlinedFont(drawList, downloadText,
UiSharedService.DrawOutlinedFont(
drawList,
downloadText,
screenPos with { X = screenPos.X - textSize.X / 2f - 1, Y = screenPos.Y - textSize.Y / 2f - 1 },
UiSharedService.Color(255, 255, 255, transparency),
UiSharedService.Color(0, 0, 0, transparency), 1);
UiSharedService.Color(0, 0, 0, transparency),
1
);
}
}
@@ -269,20 +236,203 @@ public class DownloadUi : WindowMediatorSubscriberBase
var textSize = ImGui.CalcTextSize(uploadText);
var drawList = ImGui.GetBackgroundDrawList();
UiSharedService.DrawOutlinedFont(drawList, uploadText,
UiSharedService.DrawOutlinedFont(
drawList,
uploadText,
screenPos with { X = screenPos.X - textSize.X / 2f - 1, Y = screenPos.Y - textSize.Y / 2f - 1 },
UiSharedService.Color(255, 255, 0, transparency),
UiSharedService.Color(0, 0, 0, transparency), 2);
UiSharedService.Color(0, 0, 0, transparency),
2
);
}
catch
{
_logger.LogDebug("Error drawing upload progress");
_logger.LogDebug("Error drawing upload progress");
}
}
}
}
}
private void DrawDownloadSummaryBox()
{
if (!_currentDownloads.Any())
return;
const int transparency = 150;
const float padding = 6f;
const float spacingY = 2f;
const float minBoxWidth = 320f;
var now = ImGui.GetTime();
int totalFiles = 0;
int transferredFiles = 0;
long totalBytes = 0;
long transferredBytes = 0;
var perPlayer = new List<(string Name, int TransferredFiles, int TotalFiles, long TransferredBytes, long TotalBytes, double SpeedBytesPerSecond)>();
foreach (var transfer in _currentDownloads.ToList())
{
var handler = transfer.Key;
var statuses = transfer.Value.Values;
var playerTotalFiles = statuses.Sum(s => s.TotalFiles);
var playerTransferredFiles = statuses.Sum(s => s.TransferredFiles);
var playerTotalBytes = statuses.Sum(s => s.TotalBytes);
var playerTransferredBytes = statuses.Sum(s => s.TransferredBytes);
totalFiles += playerTotalFiles;
transferredFiles += playerTransferredFiles;
totalBytes += playerTotalBytes;
transferredBytes += playerTransferredBytes;
double speed = 0;
if (playerTotalBytes > 0)
{
if (!_downloadSpeeds.TryGetValue(handler, out var tracker))
{
tracker = new DownloadSpeedTracker
{
LastBytes = playerTransferredBytes,
LastTime = now,
SpeedBytesPerSecond = 0
};
_downloadSpeeds[handler] = tracker;
}
var dt = now - tracker.LastTime;
var dBytes = playerTransferredBytes - tracker.LastBytes;
if (dt > 0.1 && dBytes >= 0)
{
var instant = dBytes / dt;
tracker.SpeedBytesPerSecond = tracker.SpeedBytesPerSecond <= 0
? instant
: tracker.SpeedBytesPerSecond * 0.8 + instant * 0.2;
}
tracker.LastTime = now;
tracker.LastBytes = playerTransferredBytes;
speed = tracker.SpeedBytesPerSecond;
}
perPlayer.Add((
handler.Name,
playerTransferredFiles,
playerTotalFiles,
playerTransferredBytes,
playerTotalBytes,
speed
));
}
foreach (var handler in _downloadSpeeds.Keys.ToList())
{
if (!_currentDownloads.ContainsKey(handler))
_downloadSpeeds.Remove(handler);
}
if (totalFiles == 0 || totalBytes == 0)
return;
var drawList = ImGui.GetBackgroundDrawList();
var windowPos = ImGui.GetWindowPos();
var headerText = $"Downloading {transferredFiles}/{totalFiles} files";
var bytesText = $"{UiSharedService.ByteToString(transferredBytes, addSuffix: false)}/{UiSharedService.ByteToString(totalBytes)}";
var totalSpeed = perPlayer.Sum(p => p.SpeedBytesPerSecond);
var speedText = totalSpeed > 0
? $"{UiSharedService.ByteToString((long)totalSpeed)}/s"
: "Calculating lightspeed...";
var headerSize = ImGui.CalcTextSize(headerText);
var bytesSize = ImGui.CalcTextSize(bytesText);
var speedSize = ImGui.CalcTextSize(speedText);
float contentWidth = headerSize.X;
if (bytesSize.X > contentWidth) contentWidth = bytesSize.X;
if (speedSize.X > contentWidth) contentWidth = speedSize.X;
foreach (var p in perPlayer)
{
var playerSpeedText = p.SpeedBytesPerSecond > 0
? $"{UiSharedService.ByteToString((long)p.SpeedBytesPerSecond)}/s"
: "-";
var line = $"{p.Name}: {p.TransferredFiles}/{p.TotalFiles} " +
$"({UiSharedService.ByteToString(p.TransferredBytes, addSuffix: false)}/{UiSharedService.ByteToString(p.TotalBytes)}) " +
$"@ {playerSpeedText}";
var lineSize = ImGui.CalcTextSize(line);
if (lineSize.X > contentWidth)
contentWidth = lineSize.X;
}
var boxWidth = contentWidth + padding * 2;
if (boxWidth < minBoxWidth)
boxWidth = minBoxWidth;
var lineHeight = ImGui.GetTextLineHeight();
var numTextLines = 3 + perPlayer.Count;
var barHeight = lineHeight * 0.8f;
var boxHeight = padding * 3 + barHeight + numTextLines * (lineHeight + spacingY);
var origin = windowPos;
var boxMin = origin;
var boxMax = origin + new Vector2(boxWidth, boxHeight);
drawList.AddRectFilled(boxMin, boxMax, UiSharedService.Color(0, 0, 0, transparency), 5f);
drawList.AddRect(boxMin, boxMax, UiSharedService.Color(220, 220, 220, transparency), 5f);
// Progress bar
var cursor = boxMin + new Vector2(padding, padding);
var barMin = cursor;
var barMax = new Vector2(boxMin.X + boxWidth - padding, cursor.Y + barHeight);
var progress = (float)transferredBytes / totalBytes;
drawList.AddRectFilled(barMin, barMax, UiSharedService.Color(40, 40, 40, transparency), 3f);
drawList.AddRectFilled(
barMin,
new Vector2(barMin.X + (barMax.X - barMin.X) * progress, barMax.Y),
UiSharedService.Color(UIColors.Get("LightlessPurple")),
3f
);
cursor.Y = barMax.Y + padding;
// Header
UiSharedService.DrawOutlinedFont(drawList, headerText, cursor, UiSharedService.Color(255, 255, 255, transparency), UiSharedService.Color(0, 0, 0, transparency), 1);
cursor.Y += lineHeight + spacingY;
// Bytes
UiSharedService.DrawOutlinedFont(drawList, bytesText, cursor, UiSharedService.Color(255, 255, 255, transparency), UiSharedService.Color(0, 0, 0, transparency), 1);
cursor.Y += lineHeight + spacingY;
// Total speed WIP
UiSharedService.DrawOutlinedFont(drawList, speedText, cursor, UiSharedService.Color(200, 255, 200, transparency), UiSharedService.Color(0, 0, 0, transparency), 1);
cursor.Y += lineHeight * 1.4f;
// Per-player lines
foreach (var p in perPlayer.OrderByDescending(p => p.TotalBytes))
{
var playerSpeedText = p.SpeedBytesPerSecond > 0
? $"{UiSharedService.ByteToString((long)p.SpeedBytesPerSecond)}/s"
: "-";
var line = $"{p.Name}: {p.TransferredFiles}/{p.TotalFiles} " +
$"({UiSharedService.ByteToString(p.TransferredBytes, addSuffix: false)}/{UiSharedService.ByteToString(p.TotalBytes)}) " +
$"@ {playerSpeedText}";
UiSharedService.DrawOutlinedFont(drawList, line, cursor, UiSharedService.Color(255, 255, 255, transparency), UiSharedService.Color(0, 0, 0, transparency), 1);
cursor.Y += lineHeight + spacingY;
}
}
public override bool DrawConditions()
{
if (_uiShared.EditTrackerPosition) return true;