Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.5

0.11.5 / src / script_support.lucb

## Base helpers for Script node builds: a stable content key, keeping a built tool, and
## the bytes a cache folder holds.
import c
import interop
import strings
from luce_std import files

func mix(state: u64, value: u64, multiplier: u64) -> u64:
    let scrambled = (state ^ value) *% multiplier
    return scrambled ^ (scrambled >> 29)

## A stable content key: 128 bits of two independent 64-bit mixes of the text's bytes,
## as 32 hex digits. It names a build directory, so it must be the same in every run and
## on every machine (Luce's `hash()` promises neither).
pub func text_key(text: str) -> interop.Owned[str]!:
    var a: u64 = 0xCBF29CE484222325
    var b: u64 = 0x84222325CBF29CE4
    for byte in text.bytes:
        a = mix(a, (u64)byte, 0x100000001B3)
        b = mix(b, (u64)byte + 0x9E, 0x9E3779B97F4A7C15)
    a = mix(a, (u64)text.length, 0xFF51AFD7ED558CCD)
    b = mix(b, (u64)text.length, 0xC4CEB9FE1A85EC53)
    var digits: u8[32]
    let hex = "0123456789abcdef"
    for at in 0..<16:
        digits[at] = hex.bytes[(usize)((a >> (u64)(60 - at * 4)) & 15)]
        digits[16 + at] = hex.bytes[(usize)((b >> (u64)(60 - at * 4)) & 15)]
    return try interop.copy_text((str)digits[0..<32])

## Copy the tool at `source` to `destination` (replaced atomically), executable.
pub func keep_tool(source: str, destination: str) -> !:
    let from_text = try strings.copy(source)
    defer strings.release(from_text)
    let to_text = try strings.copy(destination)
    defer strings.release(to_text)
    _ = try files.copy_file((c.str)from_text, (c.str)to_text, replace = true, permissions = 493)

## Sums the sizes of regular files a walk visits.
struct Sizes: files.WalkVisitor:
    var total: i64

    pub func visit(path: str, info: files.Metadata, depth: usize) -> bool!:
        _ = path
        _ = depth
        if info.kind == files.FileKind.regular: self.total += info.size
        return true

## Bytes of the regular files under `folder` (0 when it is missing).
pub func folder_bytes(folder: str) -> i64:
    let path = strings.copy(folder) catch failure:
        return 0
    defer strings.release(path)
    var sizes = Sizes(total = 0)
    files.walk_with((c.str)path, &sizes) catch failure:
        return sizes.total
    return sizes.total