Skip to content
Closed
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
2 changes: 1 addition & 1 deletion docs/engine/front-end.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ Each change is written to `starlancer.ini` at once, in the section `[OpenReliant

- GET MODS in white, centred on (320, 95).
- The framed list from (45, 136), with the catalogue's mods grouped by their top category, the first folder of their category path: a heading row for each group, in the large font and blue as the controls tab's headings are, with the group's name in capitals (`- SHIPS`), then the group's mods sorted by their full path and their name, indented by 14 pixels, each as its name and version from the index. A click on a heading folds the group, which hides its mods and turns the heading's `-` into a `+`; another click unfolds it. Mods without a category come last, under OTHER MODS. A mod that needs a newer OpenReliant is red; otherwise the selected mod is white, a mod installed in an older version is gold, and an installed mod is dimmed. While the repositories are read, the list says READING THE CATALOG. If none can be read, the list says CAN'T READ THE CATALOG and why, in red. If one of several can't be read, its name and the reason are in red above the list, ending at the panel's right edge where the mods screen's RESTART TO APPLY is, and the others' mods are listed. An empty catalogue says THE CATALOG OFFERS NO MODS. The screen lists at most 127 mods, since each mod and each group takes one of the list's 255 rows.
- The framed panel from (401, 136), which says CHOOSE A MOD while nothing is selected, and otherwise shows the selected mod's thumbnail, downloaded when the mod is selected and fitted into the same box as on the mods screen; its name; right under it, its status line, in gold: INSTALLED with the installed version, BY HAND where the settings file doesn't record the install, and UPDATE TO with the catalogue's version where it is newer, INSTALLED BY HAND AS A FOLDER: INSTALL WON'T REPLACE IT where a folder mod of the name is in the `mods` folder, DOWNLOADING with the percentage, CANCELING once CANCEL was pressed, or INSTALLED: RESTART TO APPLY; in red: NEEDS OPENRELIANT with the version, or why the install failed. Then its version and author; its full category, as IN SHIPS / FIGHTERS / ALLIANCE; the repository that lists it, as FROM openreliant-mods; the OpenReliant it needs, in red when it is newer than this one; its size and its description, which takes the lines left. During a download, a progress bar at the bottom of the panel, measured against the size the catalogue gives.
- The framed panel from (401, 136), which says CHOOSE A MOD while nothing is selected, and otherwise shows the selected mod's thumbnail, downloaded when the mod is selected and fitted into the same box as on the mods screen; its name; right under it, its status line, in gold: INSTALLED with the installed version, BY HAND where the settings file doesn't record the install, and UPDATE TO with the catalogue's version where it is newer, INSTALLED BY HAND AS A FOLDER: INSTALL WON'T REPLACE IT where a folder mod of the name is in the `mods` folder, DOWNLOADING with the percentage, CANCELING once CANCEL was pressed, or INSTALLED: RESTART TO APPLY; in red: NEEDS OPENRELIANT with the version, or why the install failed. Then its version and author; its full category, as IN SHIPS / FIGHTERS / ALLIANCE; the repository that lists it, as FROM openreliant-mods; the OpenReliant it needs, in red when it is newer than this one; its size and its description, which takes the lines left. During a download, a progress bar at the bottom of the panel, measured against the size the catalogue gives, and under the panel, above the buttons, two lines in blue: how much has arrived of the size, as 64.3 MB OF 153.7 MB, then the speed over the last 3 seconds and the time left at that speed, as 6.1 MB/S, 2 MIN LEFT, once the download has run for a second.
- The settings screen's buttons: OK and MAIN MENU in the left column. In the right column, INSTALL (or UPDATE when an older version is installed) where RESET DEFAULTS is, dimmed when there is nothing to do, and RELOAD where CANCEL CHANGES is, which reads the repositories again and tries again the thumbnails that failed. While a mod downloads, one download runs at a time and the catalogue can't change under it, so RELOAD is dimmed, and INSTALL reads CANCEL while the downloading mod is chosen and is dimmed while another is. CANCEL stops the download, which deletes its part file; the panel says CANCELING until the download's thread has stopped, then shows the mod as it was before. OK and Escape go back to the mods screen, which rereads the `mods` folder if a mod was installed.

The repositories `starlancer.ini` names ([Repositories](../guide/modding.md#repositories)) are read one after another into one catalogue, where the first repository to list an id wins, and the catalogue is kept until RELOAD or until the front end closes. A mod without the keys it needs (`id`, `archive`, `sha256`, `size`), whose id can't name a mod, or whose size is over 4294967295 bytes, the most a HOG archive's 32-bit offsets reach, is skipped with a line in the log, which escapes the id; an index in a newer format fails with THE CATALOG NEEDS A NEWER OPENRELIANT. Every download goes through [`web.zig`](../../src/engine/web.zig), on a thread of its own with its own HTTP client, and over HTTPS: `http` is changed to `https` before each request and each redirect, which the download follows itself, five at most; any other scheme is refused. Each request sends `OpenReliant` as its user agent. The player's own machine (`localhost` and the names under it, `127.0.0.0/8` and `0.0.0.0` in every form the system's resolver reads, such as `127.1`, and `::1`, `::` and the IPv4 addresses mapped into IPv6) is read only for a repository whose URL names it, and for the thumbnails and archives its index links, over plain HTTP. Anywhere else, a link to the player's machine, or a redirect to it from another host, fails with THE CATALOG POINTS AT THIS COMPUTER, so that a repository on the web can't make OpenReliant send requests to the services on the player's machine. A host name that the system's resolver turns into the player's own address isn't caught, since only the link itself is checked, but such a link is read over HTTPS, so a service there gets the start of a secure connection that it can't complete, never a request. A failure says what happened: the server's name can't be found, a secure connection can't be made, the system's certificates can't be loaded, too many redirects, a bad redirect, and so on. An index or a thumbnail gives up after 60 seconds, or after 15 seconds without anything arriving, and an archive after 30 seconds without anything arriving, with THE SERVER DOESN'T ANSWER. An index is read up to 4 MiB and a thumbnail up to 2 MiB; an archive is read up to the size the catalogue gives, and a download that goes past it, or that the server announces at another size, stops at once. INSTALL downloads the archive into `<id>.hog.part` in the `mods` folder, hashing the bytes on their way to the file. Once the archive is complete and matches the catalogue's digest, the digest is written as `<id>.hog.sha256.part`, the archive's part is renamed to `<id>.hog`, replacing an older copy, and the checksum file's part is renamed to `<id>.hog.sha256`; then the install is recorded in `[OpenReliantInstalledMods]` with the repository's name, so that the loader refuses the archive without its checksum file from then on. A download that fails or doesn't match leaves nothing behind; if the checksum file can't be put in place, the archive is removed again, with the checksum file of the copy it replaced. Leaving the screen lets a download continue. CANCEL stops it: its thread cancels the request, deletes the archive's part file and ends, and only then does the screen let the install go, so that an install of the same mod started right after can't run into the part file being deleted. Closing the front end lets go of a download without waiting, so that quitting is never held up, not even by a lookup of a host's address, which nothing can interrupt: the download's thread cancels it and ends on its own, deleting an archive's part file, unless OpenReliant has ended first, in which case the next install of the mod replaces the part file. The mods screen uses the catalogue too, to say which of its mods have an update: with CHECK FOR UPDATES on ([Newer releases](../guide/configuration.md#newer-releases)), entering it starts this reading in the background, once per run, and each of its passes takes in what has arrived, as this screen's passes do; with it off, nothing is read, but a catalogue this screen has read serves all the same. Opened from the mods screen, GET MODS chooses the mod the mods screen had chosen, unfolding its group, where the catalogue lists it. The GET MODS button on the mods screen uses the settings screen's shapes, at (45, 116) with its label to its right, and is shown only when the settings file names a repository.
Expand Down
5 changes: 3 additions & 2 deletions docs/guide/modding.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ leaves nothing in the `mods` folder, and the panel says CANCELING for a moment w
stops. Leaving the screen lets a download go on. OK goes back to the mods screen, which now lists
the installed mods. Like every change on the mods screen, they take effect at the next start.

The catalogue and the downloads run in the background, so the menus keep working, and the panel
shows the download's progress. Besides the check for a newer release ([Newer
The catalogue and the downloads run in the background, so the menus keep working. During a
download, the panel shows how much has arrived, the speed over the last few seconds and the time
left, and the log gives the average speed once the mod is installed. Besides the check for a newer release ([Newer
releases](configuration.md#newer-releases)), OpenReliant connects to the web only for this screen:
to read the repositories, and to download the mods you install. Everything is downloaded over HTTPS:
a plain `http` link is changed to `https` before it is requested, and so is every redirect, so
Expand Down
22 changes: 16 additions & 6 deletions src/engine/game/bigfile/catalogue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,21 @@ pub const Install = struct {
install.id = id;
}

/// The download's state. Takes its answer in once it has arrived, logging how it ended.
/// The download's state. Takes its answer in once it has arrived, logging how it ended, with
/// the average speed of a download that succeeded, which helps compare it with a browser's.
pub fn state(install: *Install) State {
// The progress goes with the answer, so it is read first.
const received, const elapsed = if (install.download.progress()) |measured|
.{ measured.received.load(.monotonic), measured.elapsed.load(.monotonic) }
else
.{ 0, 0 };
if (install.download.take()) |answer| {
if (answer) |_| {
log.info("installed the mod {s} in the mods folder", .{install.id.?});
const megabytes = @as(f64, @floatFromInt(received)) / (1024 * 1024);
const seconds = @as(f64, @floatFromInt(elapsed)) / std.time.ns_per_s;
log.info("installed the mod {s} in the mods folder: {d:.1} MB in {d:.1} s, {d:.1} MB/s", .{
install.id.?, megabytes, seconds, if (seconds > 0) megabytes / seconds else 0,
});
install.done = true;
} else |err| if (err == error.Canceled) {
log.info("the download of the mod {s} was cancelled", .{install.id.?});
Expand All @@ -708,10 +718,10 @@ pub const Install = struct {
return .idle;
}

/// The share of the archive received so far, while it downloads.
pub fn fraction(install: Install) ?f32 {
const progress = install.download.progress() orelse return null;
return progress.fraction();
/// How far the download has got, while it runs: the bytes received, the archive's size, the
/// recent speed and the time left.
pub fn transfer(install: Install) ?*const web.Progress {
return install.download.progress();
}

/// Asks the download to stop (`web.Download.cancel`), while it runs. The install stays
Expand Down
81 changes: 76 additions & 5 deletions src/engine/game/interface/mod_catalogue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//! MISSIONS, ...) under headings that fold and unfold, with the chosen mod's thumbnail, version,
//! author, category, repository, size and description in the panel next to it. INSTALL downloads
//! the chosen mod's archive into the `mods` folder, checks it against the catalogue's digest and
//! records in `starlancer.ini` which repository it came from, showing the progress in the panel.
//! records in `starlancer.ini` which repository it came from. The panel shows the download's
//! progress, speed and time left.
//! UPDATE does the same for a mod that is installed in an older version. RELOAD reads the
//! repositories again. The indexes and the downloads run on other threads, so the screen keeps
//! running while they come in. The repositories are read one after another, and one download
Expand Down Expand Up @@ -120,6 +121,15 @@ const bar_height = 8;
const bar_inside = 2;
const bar_at: [2]i32 = .{ details_frame.at[0] + details_inside, details_frame.at[1] + frame_height - details_inside - bar_height };
const bar_width = details_lines.width;
/// Where the download's two lines start, under the panel, so that they never cover the mod's
/// details: how much has arrived, then the speed and the time left. They end above the buttons.
const transfer_at: [2]i32 = .{ details_frame.at[0], details_frame.at[1] + frame_height + transfer_gap };
const transfer_gap = 4;

comptime {
const transfer_end = transfer_at[1] + 2 * details_lines.height;
std.debug.assert(transfer_end <= settings.Button.reset_defaults.rect().y);
}

/// The buttons, in the settings screen's places: INSTALL (or UPDATE, or CANCEL while the chosen
/// mod downloads) where RESET DEFAULTS is, and RELOAD where CANCEL CHANGES is.
Expand Down Expand Up @@ -905,7 +915,25 @@ pub const ModCatalogue = struct {
shown.most = @min(description_lines.most, @as(usize, @intCast(@max(room, 1))));
try canvas.wrapped(font, .{ x, y }, description, canvas_module.blue, .left, shown);
}
if (downloading) screen.drawBar(canvas);
if (downloading) {
try screen.drawTransfer(canvas);
screen.drawBar(canvas);
}
}

/// Draws the download's lines under the panel: how much has arrived of how much, then the
/// recent speed and the time left, once they are known.
fn drawTransfer(screen: ModCatalogue, canvas: Canvas) canvas_module.Error!void {
if (screen.install.cancelling()) return;
const progress = screen.install.transfer() orelse return;
const font = canvas.fonts.small;
var buffer: [name_buffer]u8 = undefined;
const received = progress.received.load(.monotonic);
try canvas.wrapped(font, transfer_at, receivedText(&buffer, received, progress.total.load(.monotonic)), canvas_module.blue, .left, details_lines);
const speed = progress.speed.load(.monotonic);
if (speed == 0) return;
const below: [2]i32 = .{ transfer_at[0], transfer_at[1] + details_lines.height };
try canvas.wrapped(font, below, speedText(&buffer, speed, progress.secondsLeft()), canvas_module.blue, .left, details_lines);
}

/// The status line of the mod at `index` in the catalogue, written into `buffer`: the download's
Expand All @@ -918,8 +946,9 @@ pub const ModCatalogue = struct {
const entry = screen.entries()[index];
if (screen.installing == index) {
if (screen.install.cancelling()) return .{ .text = "CANCELING", .colour = canvas_module.gold };
const text = if (screen.install.fraction()) |fraction|
std.mem.print(buffer, "DOWNLOADING {d}%", .{@as(u32, @intFromFloat(fraction * 100))}) catch "DOWNLOADING"
const fraction = if (screen.install.transfer()) |progress| progress.fraction() else null;
const text = if (fraction) |share|
std.mem.print(buffer, "DOWNLOADING {d}%", .{@as(u32, @intFromFloat(share * 100))}) catch "DOWNLOADING"
else
"DOWNLOADING";
return .{ .text = text, .colour = canvas_module.gold };
Expand Down Expand Up @@ -953,7 +982,8 @@ pub const ModCatalogue = struct {
/// Draws the progress bar at the bottom of the panel, filled to the download's progress.
fn drawBar(screen: ModCatalogue, canvas: Canvas) void {
canvas.box(bar_at, .{ bar_width, bar_height });
const fraction = screen.install.fraction() orelse return;
const progress = screen.install.transfer() orelse return;
const fraction = progress.fraction() orelse return;
const filled: i32 = @intFromFloat(@round(fraction * @as(f32, @floatFromInt(bar_width - 2 * bar_inside))));
if (filled <= 0) return;
canvas.wipe(.{ bar_at[0] + bar_inside, bar_at[1] + bar_inside }, .{ bar_at[0] + bar_inside + filled - 1, bar_at[1] + bar_height - bar_inside - 1 }, canvas_module.gold);
Expand Down Expand Up @@ -1019,6 +1049,35 @@ fn sizeText(buffer: []u8, bytes: u64) []const u8 {
return std.mem.print(buffer, "{d} KB", .{(bytes + kilobyte / 2) / kilobyte}) catch "";
}

/// How much of a download has arrived, written into `buffer`: `64.3 MB OF 153.7 MB`, or only
/// what has arrived where the size is unknown (`total` is 0).
fn receivedText(buffer: *[name_buffer]u8, received: u64, total: u64) []const u8 {
var have: [name_buffer]u8 = undefined;
const arrived = sizeText(&have, received);
if (total == 0) return std.mem.print(buffer, "{s}", .{arrived}) catch arrived;
var whole: [name_buffer]u8 = undefined;
return std.mem.print(buffer, "{s} OF {s}", .{ arrived, sizeText(&whole, total) }) catch arrived;
}

/// A download's speed in bytes per second and the seconds left, where they are known, written
/// into `buffer`: `6.1 MB/S, 2 MIN LEFT`.
fn speedText(buffer: *[name_buffer]u8, speed: u64, seconds_left: ?u64) []const u8 {
var size: [name_buffer]u8 = undefined;
const per_second = sizeText(&size, speed);
const seconds = seconds_left orelse return std.mem.print(buffer, "{s}/S", .{per_second}) catch "";
var time: [name_buffer]u8 = undefined;
return std.mem.print(buffer, "{s}/S, {s} LEFT", .{ per_second, timeText(&time, seconds) }) catch "";
}

/// A time left as the panel shows it, written into `buffer`: in seconds under a minute, else in
/// minutes, rounded up, with the hours from an hour on: `45 S`, `2 MIN`, `1 H 5 MIN`.
fn timeText(buffer: []u8, seconds: u64) []const u8 {
if (seconds < std.time.s_per_min) return std.mem.print(buffer, "{d} S", .{seconds}) catch "";
const minutes = std.math.divCeil(u64, seconds, std.time.s_per_min) catch unreachable;
if (minutes < 60) return std.mem.print(buffer, "{d} MIN", .{minutes}) catch "";
return std.mem.print(buffer, "{d} H {d} MIN", .{ minutes / 60, minutes % 60 }) catch "";
}

/// Where the name of the `place`th visible row starts.
fn nameAt(place: usize) [2]i32 {
return .{ name_x, first_row + @as(i32, @intCast(place)) * row_spacing };
Expand Down Expand Up @@ -1377,6 +1436,18 @@ test "the mods screen asks which version the catalogue offers for a mod, and GET
try std.testing.expectEqual(1, screen.chosen.?);
}

test "the download's lines" {
var buffer: [name_buffer]u8 = undefined;
try std.testing.expectEqualStrings("64.3 MB OF 153.7 MB", receivedText(&buffer, 67_423_436, 161_166_131));
try std.testing.expectEqualStrings("512 KB", receivedText(&buffer, 512 * 1024, 0));
try std.testing.expectEqualStrings("6.1 MB/S, 2 MIN LEFT", speedText(&buffer, 6_396_314, 95));
try std.testing.expectEqualStrings("850 KB/S", speedText(&buffer, 850 * 1024, null));
try std.testing.expectEqualStrings("45 S", timeText(&buffer, 45));
try std.testing.expectEqualStrings("1 MIN", timeText(&buffer, 60));
try std.testing.expectEqualStrings("2 MIN", timeText(&buffer, 61));
try std.testing.expectEqualStrings("1 H 5 MIN", timeText(&buffer, 3900));
}

test sizeText {
var buffer: [32]u8 = undefined;
try std.testing.expectEqualStrings("30.8 MB", sizeText(&buffer, 32297124));
Expand Down
Loading
Loading