forked from Shirakumo/trial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset.lisp
243 lines (194 loc) · 9.35 KB
/
asset.lisp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#|
This file is a part of trial
(c) 2017 Shirakumo http://tymoon.eu (shinmera@tymoon.eu)
Author: Nicolas Hafner <shinmera@tymoon.eu>
|#
(in-package #:org.shirakumo.fraf.trial)
(defclass placeholder-resource (resource)
((generator :initform (error "GENERATOR required."))
(name :initform T)))
(defmethod print-object ((resource placeholder-resource) stream)
(let ((asset (generator resource)))
(print-unreadable-object (resource stream :type T)
(format stream "~a/~a[~a]" (name (pool asset)) (name asset) (name resource)))))
(defmethod allocated-p ((resource placeholder-resource)) NIL)
(defmethod allocate ((resource placeholder-resource))
(load (generator resource))
(cond ((typep resource 'placeholder-resource)
(error "Loading the asset~% ~a~%did not generate the resource~% ~a"
(generator resource) resource))
(T
;; We should have been change class'd by now, so re-call.
(allocate resource))))
(defmethod unload ((resource placeholder-resource)))
(defmethod dependencies ((resource placeholder-resource))
(list (generator resource)))
(defclass asset (resource-generator)
((pool :initform NIL :accessor pool)
(name :initform NIL :accessor name)
(input :initarg :input :accessor input)
(loaded-p :initform NIL :accessor loaded-p)
(generation-arguments :initform () :initarg :generation-arguments :accessor generation-arguments)))
(defgeneric load (asset))
(defgeneric reload (asset))
(defgeneric unload (asset))
(defgeneric list-resources (asset))
(defgeneric coerce-asset-input* (asset input))
(defun // (pool asset &optional (resource T))
(resource (asset pool asset) resource))
(define-compiler-macro // (&whole whole pool asset &optional (resource T) &environment env)
;; We can do this because an asset's generated resources must be updated in place.
(if (and (constantp pool env)
(constantp asset env))
(if (constantp resource env)
`(load-time-value (resource (asset ,pool ,asset) ,resource))
`(resource (asset ,pool ,asset) ,resource))
whole))
(defmethod shared-initialize :after ((asset asset) slots &key pool name)
(check-type name symbol)
(when name
(setf (name asset) name))
(when pool
(setf (pool asset) (etypecase pool
(symbol (find-pool pool T))
(pool pool))))
(setf (asset pool name) asset))
(defmethod reinitialize-instance :after ((asset asset) &key)
(when (loaded-p asset)
(reload asset)))
(defmethod update-instance-for-different-class :around ((previous asset) (current asset) &key)
(cond ((loaded-p previous)
(unload previous)
(call-next-method)
(load current))
(T
(call-next-method))))
(defmethod print-object ((asset asset) stream)
(print-unreadable-object (asset stream :type T)
(format stream "~a/~a" (name (pool asset)) (name asset))))
(defmethod resource ((asset asset) id)
(error "The asset~% ~a~%does not hold a resource named~% ~a"
asset id))
(defmethod reload ((asset asset))
(when (and (loaded-p asset) *context*)
(with-context (*context*)
(deallocate asset)
(loop for resource in (enlist (apply #'generate-resources asset (input* asset) (generation-arguments asset)))
do (dolist (dependency (dependencies resource))
(allocate dependency))
(allocate resource)))))
(defmethod load ((asset asset))
(apply #'generate-resources asset (input* asset) (generation-arguments asset)))
(defmethod load :around ((asset asset))
(unless (loaded-p asset)
(v:trace :trial.asset "Loading ~a/~a" (name (pool asset)) (name asset))
(call-next-method)))
(defmethod generate-resources :after ((asset asset) input &key)
(setf (loaded-p asset) T))
(defmethod unload :around ((asset asset))
(when (loaded-p asset)
(v:trace :trial.asset "Unloading ~a/~a" (name (pool asset)) (name asset))
(call-next-method)))
(defmethod unload :after ((asset asset))
(setf (loaded-p asset) NIL))
(defmethod deallocate :after ((asset asset))
(setf (loaded-p asset) NIL))
(defmethod coerce-asset-input ((asset asset) (input (eql T)))
(coerce-asset-input asset (input asset)))
(defmethod coerce-asset-input ((asset asset) thing)
thing)
(defmethod coerce-asset-input ((asset asset) (path pathname))
(pool-path (pool asset) path))
(defmethod coerce-asset-input ((asset asset) (list list))
(loop for item in list collect (coerce-asset-input asset item)))
(defmethod input* ((asset asset))
(coerce-asset-input asset (input asset)))
(defmethod register-generation-observer :after (observer (asset asset))
(when (loaded-p asset)
(observe-generation observer asset (list-resources asset))))
(defmacro define-asset ((pool name) type input &rest options)
(check-type pool symbol)
(check-type name symbol)
(check-type type symbol)
`(ensure-instance (asset ',pool ',name NIL) ',type
:input ,input
:name ',name
:pool ',pool
:generation-arguments (list ,@options)))
(trivial-indent:define-indentation define-asset (4 6 4 &body))
(defun pathname-asset-name (path &key ignore-directory)
(flet ((rep (regex replace source)
(cl-ppcre:regex-replace-all regex source replace)))
(let ((name (rep "[ _\\-.]+" "-" (pathname-name path)))
(dirs (unless ignore-directory (rest (pathname-directory path)))))
(format NIL "~:@(~{~a/~}~a~)" dirs name))))
(defun generate-assets-from-path (pool type pathname &key (package *package*) attributes ignore-directory debug exclude)
(let ((base (pool-path pool #p""))
(default-options (rest (find T attributes :key #'first)))
(exclude (enlist exclude)))
(loop for path in (directory (pool-path pool pathname))
unless (loop for exclusion in exclude
thereis (pathname-match-p path exclusion))
collect (let* ((path (enough-namestring path base))
(name (intern (pathname-asset-name path :ignore-directory ignore-directory) package))
(options (append (rest (find name attributes :key #'first)) default-options)))
(if debug
(print `(define-asset (,pool ,name) ,type
,(pathname path)
,@options))
(ensure-instance (asset pool name NIL) type
:input (pathname path)
:name name
:pool pool
:generation-arguments options))))))
(defmacro define-assets-from-path ((pool type pathname &rest args) &body attributes)
`(generate-assets-from-path ',pool ',type ,pathname
:attributes (list ,@(loop for (name . args) in attributes
collect `(list ',name ,@args)))
:package ,*package*
,@args))
(defclass single-resource-asset (asset)
((resource)))
(defmethod initialize-instance :after ((asset single-resource-asset) &key)
(setf (slot-value asset 'resource) (make-instance 'placeholder-resource :generator asset)))
(defmethod resource ((asset single-resource-asset) (id (eql T)))
(slot-value asset 'resource))
(defmethod list-resources ((asset single-resource-asset))
(list (resource asset T)))
(defmethod unload ((asset single-resource-asset))
(unload (resource asset T)))
(defmethod deallocate ((asset single-resource-asset))
(when (allocated-p (resource asset T))
(deallocate (resource asset T)))
(change-class (resource asset T) 'placeholder-resource :generator asset))
(defclass multi-resource-asset (asset)
((resources :initform (make-hash-table :test 'equal))))
(defmethod resource ((asset multi-resource-asset) id)
(let ((table (slot-value asset 'resources)))
(or (gethash id table)
(setf (gethash id table)
(make-instance 'placeholder-resource :generator asset)))))
(defmethod list-resources ((asset multi-resource-asset))
(loop for resource being the hash-values of (slot-value asset 'resources)
collect resource))
(defmethod unload ((asset multi-resource-asset))
(loop for resource being the hash-values of (slot-value asset 'resources)
do (unload resource)))
(defmethod deallocate ((asset multi-resource-asset))
(loop for name being the hash-keys of (slot-value asset 'resources)
for resource being the hash-values of (slot-value asset 'resources)
do (when (allocated-p resource)
(deallocate resource))
(change-class resource 'placeholder-resource :name name :generator asset)))
(defclass file-input-asset (asset)
())
(defmethod shared-initialize :after ((asset file-input-asset) slots &key &allow-other-keys)
(let ((file (input* asset)))
(unless (probe-file file)
(alexandria:simple-style-warning "Input file~% ~s~%for asset~% ~s~%does not exist." file asset))))
(defmethod compile-resources ((asset asset) (source (eql T)) &rest args &key &allow-other-keys)
(when (typep asset 'compiled-generator)
(apply #'compile-resources asset (input* asset) args)))
(defmethod compile-resources ((all (eql T)) (source (eql T)) &rest args &key &allow-other-keys)
(dolist (pool (list-pools))
(apply #'compile-resources pool source args)))