Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / src / file_identity.lucb

## Content identity of an external geometry file for the import cache: size,
## modification time and a word-wise hash of its bytes. Reading the file is
## far cheaper than parsing it, and catches edits that keep size and mtime.
import c
from luce_std import files
import strings
import memory

func mix(state: u64, value: u64) -> u64:
    let scrambled = state ^ (value *% 0x9E3779B97F4A7C15)
    return ((scrambled << 29) | (scrambled >> 35)) *% 5 +% 0x52DCE729

pub func file_identity(path: str) -> i64!:
    let native = try strings.copy(path)
    defer strings.release(native)
    let info = try files.metadata((c.str)native)
    let file = try files.read((c.str)native, limit = 4294967296)
    defer file.release()
    let data = file.value
    var state = mix(mix(mix(0x243F6A8885A308D3, (u64)info.size), (u64)info.modified.seconds), (u64)info.modified.nanoseconds)
    var at: usize = 0
    while at + 8 <= data.length:
        state = mix(state, memory.read[u64](&data[at]))
        at += 8
    while at < data.length:
        state = mix(state, (u64)data[at])
        at += 1
    return (i64)(state ^ (state >> 31))