Make slot forms with minimal (quote)ing (resolves #6)

This commit is contained in:
~keith 2022-01-04 19:26:29 +00:00
parent 372044fc20
commit 1a154a9a56
Signed by: keith
GPG key ID: 5BEBEEAB2C73D520
5 changed files with 34 additions and 3 deletions

View file

@ -73,7 +73,8 @@
(:documentation "Base class for entities in the game.")) (:documentation "Base class for entities in the game."))
(defmethod make-load-form ((this actor) &optional environment) (defmethod make-load-form ((this actor) &optional environment)
(make-load-form-saving-slots this :environment environment)) ;(make-load-form-saving-slots this :environment environment)
(make-generic-load-form this :environment environment))
(defmethod scene ((this actor)) (defmethod scene ((this actor))
"The scene containing this actor." "The scene containing this actor."

View file

@ -18,7 +18,8 @@
(:documentation "Base class for components attached to game entities.")) (:documentation "Base class for components attached to game entities."))
(defmethod make-load-form ((this component) &optional environment) (defmethod make-load-form ((this component) &optional environment)
(make-load-form-saving-slots this :environment environment)) ;(make-load-form-saving-slots this :environment environment)
(make-generic-load-form this :environment environment))
(defmethod actor ((this component)) (defmethod actor ((this component))
"The actor this component belongs to." "The actor this component belongs to."

View file

@ -24,6 +24,7 @@
deref-sus-pointer deref-sus-pointer
referize pointerize referize pointerize
referize-setf pointerize-setf referize-setf pointerize-setf
make-generic-load-form
generate-load-forms generate-load-forms
dump-scene dump-actors dump-scene dump-actors
load-resume-scene load-resume-actors load-resume-scene load-resume-actors

View file

@ -23,7 +23,8 @@
(:documentation "A scene containing game entities.")) (:documentation "A scene containing game entities."))
(defmethod make-load-form ((this scene) &optional environment) (defmethod make-load-form ((this scene) &optional environment)
(make-load-form-saving-slots this :environment environment)) ;(make-load-form-saving-slots this :environment environment)
(make-generic-load-form this :environment environment))
(defmethod print-object ((this scene) stream) (defmethod print-object ((this scene) stream)
(print-unreadable-object (this stream :type t :identity t) (print-unreadable-object (this stream :type t :identity t)

View file

@ -159,6 +159,33 @@
x))) x)))
)) ))
(defun make-generic-load-form (obj &key environment)
"Make a generic load form that works for most objects."
(declare (ignore environment))
(values
;; cons form
`(sb-kernel::new-instance ,(class-name (class-of obj)))
;; init form
(loop for slot in (sb-mop:class-slots (class-of obj))
for slot-name = (sb-mop:slot-definition-name slot)
collect slot-name into slots
collect (make-value-init-form (slot-value obj slot-name))
into values
finally (return `(sb-pcl::set-slots ,obj ,slots ,@values)))
))
(defun make-value-init-form (x)
(typecase x
;; One case for both T and NIL/()
(boolean x)
;; this will fail with circular lists. don't serialize circular lists.
(list (if (cdr (last x))
`(cons ,(make-value-init-form (car x))
,(make-value-init-form (cdr x)))
(cons 'list (loop for y in x collect (make-value-init-form y)))))
(symbol `(quote ,x))
(t x)))
(defun generate-load-forms (obj &key (prune t) (nice-syms nil)) (defun generate-load-forms (obj &key (prune t) (nice-syms nil))
"Generate code that restores the current state of obj." "Generate code that restores the current state of obj."
(declare (type boolean prune nice-syms)) (declare (type boolean prune nice-syms))