code wiki / _hdl_build / nx_vn_pack.nx
nx_vn_pack.nx source
↩ module page · 47 lines · 2972 B
1// nx_vn_pack.nx -- make the VN page SELF-CONTAINED: inline the generated background PNG as a base64 data: URI
2// inside vn.html's CSS, replacing url('vn_bg.png'). No separate image file to serve, NO external fetch (the bytes
3// are in the page), still zero-JS / zero-blit / sovereign. The public sites daemon serves the .html fine; this
4// removes the dependency on it serving a separate .png. Reuses our own nx_base64 encoder. license_tier: ORIGINAL expect_exit: 0
5import "nx_syscalls.nx"
6import "nx_base64.nx"
7const K_MAGIC_1024: i64 = 1024
8
9func pw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func pn(v: i64) -> i64 { var m: i64=v; if m==0{sys_write(1,"0" as *u8,1);return 0} let t:*u8=sys_mmap(24); var k:i64=0; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let o:*u8=sys_mmap(24); var i:i64=0; while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 }
11// 1 if buf[i..] starts with needle
12func pmatch(buf: *u8, i: i64, needle: *u8) -> i64 { var j: i64=0; while needle[j]!=(0 as u8){ if buf[i+j]!=needle[j]{return 0} j=j+1 } return 1 }
13func plen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
14
15func main(argc: i64, argv: *i64) -> i64 {
16 let pl: *i64 = sys_mmap(16) as *i64
17 let png: *u8 = sys_read_file("knowledge/staging/game/scene_keeper.png" as *u8, pl)
18 if (png as i64) == 0 { pw("nx_vn_pack: cannot read scene_keeper.png\n" as *u8); sys_exit(1); return 1 }
19 let pnglen: i64 = pl[0]
20 let b64: *u8 = sys_mmap(pnglen*2 + 64)
21 let b64len: i64 = b64_encode(png, pnglen, b64)
22
23 let hl: *i64 = sys_mmap(16) as *i64
24 let html: *u8 = sys_read_file("knowledge/staging/game/vn.html" as *u8, hl)
25 if (html as i64) == 0 { pw("nx_vn_pack: cannot read vn.html\n" as *u8); sys_exit(1); return 1 }
26 let hlen: i64 = hl[0]
27
28 let needle: *u8 = "url('vn_bg.png')" as *u8
29 let nlen: i64 = plen(needle)
30 let out: *u8 = sys_mmap(b64len + hlen + K_MAGIC_1024)
31 var o: i64 = 0; var i: i64 = 0; var done: i64 = 0
32 while i < hlen {
33 if done == 0 { if pmatch(html, i, needle) == 1 {
34 o = o + 0
35 let pre: *u8 = "url('data:image/png;base64," as *u8; var k: i64 = 0; while pre[k]!=(0 as u8){ out[o]=pre[k]; o=o+1; k=k+1 }
36 var j: i64 = 0; while j < b64len { out[o]=b64[j]; o=o+1; j=j+1 }
37 out[o]=39 as u8; o=o+1; out[o]=41 as u8; o=o+1 // ')'
38 i = i + nlen; done = 1
39 } else { out[o]=html[i]; o=o+1; i=i+1 } }
40 else { out[o]=html[i]; o=o+1; i=i+1 }
41 }
42 out[o] = 0 as u8
43 let fd: i64 = sys_openat_wr("knowledge/staging/game/vn.html" as *u8, 420); if fd >= 0 { sys_write(fd, out, o); sys_close(fd) }
44 pw("nx_vn_pack: inlined "); pn(pnglen); pw("B png -> "); pn(b64len); pw("B base64 data-URI; self-contained vn.html = "); pn(o); pw("B\n" as *u8)
45 if done == 0 { pw(" WARN: url('vn_bg.png') not found -- vn.html unchanged\n" as *u8); sys_exit(2); return 2 }
46 sys_exit(0); return 0
47}