-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrip-sprites.scm
89 lines (71 loc) · 2.6 KB
/
rip-sprites.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(define filename "pokegra-hg.narc")
(define outdir "scmtest")
(define dirs '(("back/female" "back/female/shiny")
("back" "back/shiny")
("female" "shiny/female")
("" "shiny")))
(define (join-path . paths)
(string-join paths "/"))
(define (flatten lst)
(if (null? lst)
lst
(append (car lst) (flatten (cdr lst)))))
(define ENOENT 2)
(define EEXIST 17)
(define (maptail f lst)
(if (null? lst) '()
(cons (f lst) (maptail f (cdr lst)))))
(define (mkdir-if-not-exist dir)
(catch 'system-error
(lambda () (mkdir dir) #t)
(lambda (key subr msg args data)
(define code (list-ref data 0))
(if (eq? code EEXIST)
#f
(throw key subr msg args data)))))
(define (mkdir-components path)
(let* ((components (reverse! (string-split path #\/)))
(paths (reverse! (maptail (lambda (x) (apply join-path (reverse x)))
components))))
(for-each mkdir-if-not-exist paths)))
(define (mkdir-all dir)
(catch 'system-error
(lambda () (mkdir-if-not-exist dir))
(lambda (key subr msg args data)
(define code (list-ref data 0))
(if (eq? code ENOENT)
(mkdir-components dir)
(throw key subr msg args data)))))
(define (mkdirs dirs)
(for-each mkdir-all dirs))
(define (narc-maybe-load-file narc i . args)
(if (= 0 (narc-get-file-size narc i))
#f
(apply narc-load-file narc i args)))
(define (rip-sprites narc)
(define (rip-pokemon n)
(let* ((base (* n 6))
(sprites (map (lambda (i) (narc-maybe-load-file narc (+ base i) 'NCGR))
(iota 4)))
(palettes (list (narc-load-file narc (+ base 4) 'NCLR)
(narc-load-file narc (+ base 5) 'NCLR)))
(image (make-image)))
(define (rip-sprite ncgr dirs)
(if ncgr (begin
(ncgr-decrypt-pt ncgr)
(image-set-pixels-from-ncgr image ncgr)
(for-each (lambda (nclr d)
(define outfile (format #f "~a/~a/~a.png" outdir d n))
(image-set-palette-from-nclr image nclr)
(image-save-png image outfile))
palettes
dirs))))
(for-each rip-sprite sprites dirs)))
(let* ((count (narc-file-count narc))
(n (floor (/ count 6))))
(map rip-pokemon (cdr (iota n)))))
; -------------------------
(mkdirs (map (lambda (p) (join-path outdir p))
(flatten dirs)))
(let ((narc (load-narc filename)))
(rip-sprites narc))