Skip to content

armgit / WacOS

Public Imported from GitHub
0

A new style desktop shell for Windows 11: menu bar, Dock, Finder, Spotlight, Control Center and Liquid Glass, in native C++.

Rust
5.3 KB
Raw Blame
// Gera, a partir dos MESMOS arquivos do C++ (só leitura de src\comum):
//  - idioma_gen.rs: os 11 dicionários pt -> idioma (idioma_<código>.inc, pares { L"pt", L"tr" })
//  - mac27_2x.bin / mac27_1x.bin: os botões da janela do macOS 27 (semaforo_mac27.inc, BGRA pré-multiplicado)
use std::fmt::Write as _;
use std::path::PathBuf;

const LINGUAS: [&str; 12] = ["en", "es", "fr", "de", "it", "ja", "zh", "ko", "ru", "pl", "tr", "vi"];

/// Lê um literal L"..." do C a partir de `i` (aponta para o L). Devolve (texto, posição depois das aspas).
fn literal(b: &[char], mut i: usize) -> (String, usize) {
    assert!(b[i] == 'L' && b[i + 1] == '"');
    i += 2;
    let mut s = String::new();
    while b[i] != '"' {
        if b[i] == '\\' {
            let c = b[i + 1];
            i += 2;
            match c {
                'n' => s.push('\n'),
                't' => s.push('\t'),
                'r' => s.push('\r'),
                '"' => s.push('"'),
                '\\' => s.push('\\'),
                '\'' => s.push('\''),
                'u' => {
                    let h: String = b[i..i + 4].iter().collect();
                    s.push(char::from_u32(u32::from_str_radix(&h, 16).unwrap()).unwrap());
                    i += 4;
                }
                'x' => {
                    let mut h = String::new();
                    while b[i].is_ascii_hexdigit() {
                        h.push(b[i]);
                        i += 1;
                    }
                    s.push(char::from_u32(u32::from_str_radix(&h, 16).unwrap()).unwrap());
                }
                outro => panic!("escape desconhecido \\{outro}"),
            }
        } else {
            s.push(b[i]);
            i += 1;
        }
    }
    (s, i + 1)
}

/// Pares { L"a" L"b"..., L"c" } do arquivo (literais vizinhos se juntam, como no C)
fn pares(texto: &str) -> Vec<(String, String)> {
    let b: Vec<char> = texto.chars().collect();
    let mut out = Vec::new();
    let mut i = 0;
    let mut campos: Vec<String> = Vec::new();
    let mut atual: Option<String> = None;
    let mut dentro = false;
    while i < b.len() {
        let c = b[i];
        if !dentro {
            if c == '/' && i + 1 < b.len() && b[i + 1] == '/' {
                while i < b.len() && b[i] != '\n' {
                    i += 1;
                }
                continue;
            }
            if c == '{' {
                dentro = true;
                campos.clear();
                atual = None;
            }
            i += 1;
            continue;
        }
        if c == 'L' && i + 1 < b.len() && b[i + 1] == '"' {
            let (s, j) = literal(&b, i);
            atual.get_or_insert_with(String::new).push_str(&s);
            i = j;
            continue;
        }
        if c == ',' {
            if let Some(a) = atual.take() {
                campos.push(a);
            }
        } else if c == '}' {
            if let Some(a) = atual.take() {
                campos.push(a);
            }
            if campos.len() == 2 {
                out.push((campos[0].clone(), campos[1].clone()));
            }
            dentro = false;
        }
        i += 1;
    }
    out
}

fn main() {
    let raiz = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("..").join("..").join("src").join("comum");
    let out = PathBuf::from(std::env::var("OUT_DIR").unwrap());

    let mut gen = String::new();
    for l in LINGUAS {
        let p = raiz.join(format!("idioma_{l}.inc"));
        println!("cargo:rerun-if-changed={}", p.display());
        let t = std::fs::read_to_string(&p).unwrap();
        let t = t.trim_start_matches('\u{feff}');
        let mut v = pares(t);
        // complemento: traduções que entraram no main do C++ depois do ponto do porte (rust-teste/comum/idioma)
        let extra = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()).join("idioma").join(format!("idioma_{l}.inc"));
        println!("cargo:rerun-if-changed={}", extra.display());
        if let Ok(x) = std::fs::read_to_string(&extra) {
            let novos = pares(x.trim_start_matches('\u{feff}'));
            v.retain(|(a, _)| !novos.iter().any(|(n, _)| n == a));
            v.extend(novos);
        }
        writeln!(gen, "pub static T_{}: &[(&str, &str)] = &[", l.to_uppercase()).unwrap();
        for (a, b) in v {
            writeln!(gen, "    ({a:?}, {b:?}),").unwrap();
        }
        gen.push_str("];\n");
    }
    std::fs::write(out.join("idioma_gen.rs"), gen).unwrap();

    // semaforo_mac27.inc: dois arrays de bytes (5 x 36x36x4 e 5 x 18x18x4)
    let p = raiz.join("semaforo_mac27.inc");
    println!("cargo:rerun-if-changed={}", p.display());
    let t = std::fs::read_to_string(&p).unwrap();
    let parte = |nome: &str| -> Vec<u8> {
        let ini = t.find(&format!("{nome}[")).unwrap();
        let ab = t[ini..].find('{').unwrap() + ini;
        let fim = t[ab..].find("};").unwrap() + ab;
        t[ab..fim]
            .split(|c: char| !c.is_ascii_digit())
            .filter(|s| !s.is_empty())
            .map(|s| s.parse::<u16>().unwrap() as u8)
            .collect()
    };
    let a = parte("MAC27_LIGHTS");
    let b = parte("MAC27_LIGHTS_1X");
    assert_eq!(a.len(), 5 * 36 * 36 * 4);
    assert_eq!(b.len(), 5 * 18 * 18 * 4);
    std::fs::write(out.join("mac27_2x.bin"), a).unwrap();
    std::fs::write(out.join("mac27_1x.bin"), b).unwrap();
    println!("cargo:rerun-if-changed=build.rs");
}