~mil/mepo

81a5698e594fcebcaf02c310141eb6c668fcb388 — Miles Alan a month ago 403fd1d
Move tile_cache_url to be stored in utilprefs instead of TileCache
4 files changed, 27 insertions(+), 15 deletions(-)

M src/TileCache.zig
M src/api/prefset_n.zig
M src/api/prefset_t.zig
M src/util/utilprefs.zig
M src/TileCache.zig => src/TileCache.zig +8 -10
@@ 51,7 51,6 @@ expiry_seconds: i32 = -1,
queue_lifo_ui: datastructure.QueueHashMap(types.XYZ, void),
queue_lifo_bg: datastructure.QueueHashMap(types.XYZ, void),
renderer: ?*sdl.SDL_Renderer = null,
source_url: [:0]const u8 = undefined,
surface_map: datastructure.QueueHashMap(types.XYZ, *sdl.SDL_Surface),
texture_map: datastructure.EvictionHashMap(types.XYZ, *sdl.SDL_Texture, config.MaxTextures),
transfer_map: datastructure.QueueHashMap(types.XYZ, *TransferDatum),


@@ 135,12 134,12 @@ pub fn set_cache_url(tile_cache: *@This(), url: [:0]const u8) !void {
    tile_cache.queue_lifo_ui.clearAndFree();
    tile_cache.queue_lifo_bg.clearAndFree();

    // Set new URL
    tile_cache.allocator.free(tile_cache.source_url);
    tile_cache.source_url = try tile_cache.allocator.dupeZ(u8, url);
    try utilprefs.set_t(tile_cache.allocator, "tile_cache_url", url);
}

pub fn set_cache_dir(tile_cache: *@This(), path: [:0]const u8) !void {
    //try utilprefs.set_t(tile_cache.allocator, "tile_cache_url", url);

    const expanded_path = try utilfile.wordexp_filepath(tile_cache.allocator, path);
    defer tile_cache.allocator.free(expanded_path);
    try std.fs.cwd().makePath(expanded_path);


@@ 256,7 255,7 @@ pub fn tile_ui_retreive_or_queue(tile_cache: *@This(), coords: types.XYZ) !TileD

        // Load tile to surface
        {
            const png = try png_path(tile_cache.allocator, tile_cache.source_url, coords);
            const png = try png_path(tile_cache.allocator, utilprefs.get("tile_cache_url").t.?, coords);
            defer tile_cache.allocator.free(png);
            file_cached_png_opt = cache_dir.readFileAlloc(tile_cache.allocator, png, 500000) catch null;
            if (file_cached_png_opt) |file_cached_png| {


@@ 308,10 307,10 @@ fn curl_add_to_multi_and_register_transfer(tile_cache: *@This(), coords: types.X
    try tile_cache.transfer_map.put(coords, transfer_datum);

    var tile_url = url: {
        var url = try tile_cache.allocator.alloc(u8, tile_cache.source_url.len + (3 * 10));
        var url = try tile_cache.allocator.alloc(u8, utilprefs.get("tile_cache_url").t.?.len + (3 * 10));
        if (cstdio.sprintf(
            &url[0],
            tile_cache.source_url,
            utilprefs.get("tile_cache_url").t.?,
            @intCast(c_int, coords.x),
            @intCast(c_int, coords.y),
            @intCast(c_int, coords.z),


@@ 426,7 425,7 @@ fn download_loop_transfer_complete(tile_cache: *@This(), msg: *curl.CURLMsg) !vo

                if (tile_cache.cache_dir) |cache_dir| {
                    // Save to FS
                    const path = try png_path(tile_cache.allocator, tile_cache.source_url, coords);
                    const path = try png_path(tile_cache.allocator, utilprefs.get("tile_cache_url").t.?, coords);
                    try cache_dir.writeFile(path, datum_array);
                }
                if (tile_cache.transfer_map.get(coords).?.load_to_texture) {


@@ 482,7 481,7 @@ fn png_path(allocator: std.mem.Allocator, source: []const u8, coords: types.XYZ)
/// to creation time & expiry seconds setting
fn tile_exists_in_fs_and_non_expired(tile_cache: *@This(), coords: types.XYZ) !bool {
    if (tile_cache.cache_dir) |cache_dir| {
        const png = try png_path(tile_cache.allocator, tile_cache.source_url, coords);
        const png = try png_path(tile_cache.allocator, utilprefs.get("tile_cache_url").t.?, coords);
        defer tile_cache.allocator.free(png);
        const tile_file = cache_dir.openFile(png, .{}) catch return false;
        defer tile_file.close();


@@ 518,7 517,6 @@ pub fn init(allocator: std.mem.Allocator) anyerror!@This() {
        return @as(@This(), .{
            .allocator = allocator,
            .curl_multi = curl.curl_multi_init().?,
            .source_url = try allocator.dupeZ(u8, ""),
            .queue_lifo_ui = datastructure.QueueHashMap(types.XYZ, void).init(allocator),
            .queue_lifo_bg = datastructure.QueueHashMap(types.XYZ, void).init(allocator),
            .surface_map = datastructure.QueueHashMap(types.XYZ, *sdl.SDL_Surface).init(allocator),

M src/api/prefset_n.zig => src/api/prefset_n.zig +1 -2
@@ 21,7 21,6 @@ fn execute(mepo: *Mepo, args: [types.MepoFnNargs]types.MepoArg) !void {

    if (std.mem.eql(u8, "tile_cache_network", name)) {
        mepo.tile_cache.set_network(value == 1);
    } else {
        utilprefs.set_n(name, value);
    }
    utilprefs.set_n(name, value);
}

M src/api/prefset_t.zig => src/api/prefset_t.zig +3 -1
@@ 2,6 2,7 @@ const Mepo = @import("../Mepo.zig");
const types = @import("../types.zig");
const std = @import("std");
const utildbg = @import("../util/utildbg.zig");
const utilprefs = @import("../util/utilprefs.zig");

pub const spec = .{
    .name = "prefset_t",


@@ 18,7 19,8 @@ fn execute(mepo: *Mepo, args: [types.MepoFnNargs]types.MepoArg) !void {
    const name = args[0].Text;
    const value = args[1].Text;

    // Tilecache
    // Under the hood uses utilprefs.set_t
    // but unlike numerical updates, these generally need 'syncing' fns
    if (std.mem.eql(u8, "tile_cache_dir", name)) {
        try mepo.tile_cache.set_cache_dir(value);
    } else if (std.mem.eql(u8, "tile_cache_url", name)) {

M src/util/utilprefs.zig => src/util/utilprefs.zig +15 -2
@@ 1,7 1,7 @@
const std = @import("std");

const PrefValueEnum = enum { b, t, f, u };
const PrefValueUnion = union(PrefValueEnum) { b: bool, t: ?[]const u8, f: f64, u: u8 };
const PrefValueUnion = union(PrefValueEnum) { b: bool, t: ?[:0]const u8, f: f64, u: u8 };
const PrefMapping = struct {
    name: []const u8,
    value: PrefValueUnion,


@@ 124,7 124,20 @@ pub fn set_n(prefname: []const u8, value: f64) void {
                    pref.value = .{ .f = value };
                },
                .t => {},
                .v => {},
            }
        }
    }
}

pub fn set_t(allocator: std.mem.Allocator, prefname: []const u8, value: []const u8) !void {
    for (prefs_mapping) |*pref| {
        if (std.mem.eql(u8, pref.name, prefname)) {
            switch (pref.value) {
                .t => {
                    if (pref.value.t) |heap_allocated_text| allocator.free(heap_allocated_text);
                    pref.value.t = try allocator.dupeZ(u8, value);
                },
                else => {}
            }
        }
    }