forked from Shirakumo/trial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture.lisp
168 lines (139 loc) · 5.35 KB
/
capture.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
#|
This file is a part of trial
(c) 2016 Shirakumo http://tymoon.eu (shinmera@tymoon.eu)
Author: Nicolas Hafner <shinmera@tymoon.eu>
|#
(in-package #:org.shirakumo.fraf.trial)
(defmacro define-event-type-serializer (type &body slots)
`(define-object-type-serializer ,type (lambda (&rest args) (apply #'make-event ',type args))
,@slots))
(define-type-serializer frame-count-change
(lambda (s) (nibbles:read-ub32/le s))
(lambda (v s) (nibbles:write-ub32/le v s)))
(define-event-type-serializer key-press
(key keyword)
(repeat-p boolean))
(define-event-type-serializer key-release
(key keyword)
(repeat-p boolean))
(define-event-type-serializer text-entered
(text string)
(replace-p boolean))
(define-event-type-serializer mouse-press
(pos vec2)
(button keyword))
(define-event-type-serializer mouse-release
(pos vec2)
(button keyword))
(define-event-type-serializer mouse-scroll
(pos vec2)
(delta single-float))
(define-event-type-serializer mouse-move
(pos vec2)
(old-pos vec2))
(define-event-type-serializer gamepad-press
(device gamepad:device)
(button keyword))
(define-event-type-serializer gamepad-release
(device gamepad:device)
(button keyword))
(define-event-type-serializer gamepad-move
(device gamepad:device)
(axis keyword)
(old-pos single-float)
(pos single-float))
(defun capture-pathname ()
(make-pathname :name (format NIL "capture ~a" (format-timestring :as :filename)) :type "dat"))
(defvar *capture-header* "TRIAL-CAPTURE")
(defclass capture (unit listener)
((name :initform 'capture)
(capture-stream :initform NIL :accessor capture-stream)
(file :initarg :file :initform (capture-pathname) :accessor file)
(fc :initform NIL :accessor fc)))
(defmethod start ((capture capture))
(when (capture-stream capture)
(error "Already started."))
(let ((stream (open (file capture)
:element-type '(unsigned-byte 8)
:direction :io)))
(setf (capture-stream capture) stream)
(loop for c across *capture-header*
do (write-byte (char-code c) stream))
(nibbles:write-ub32/le 0 stream))
capture)
(defmethod stop ((capture capture))
;; Finalize the length
(let ((stream (capture-stream capture)))
(when stream
(file-position stream (length *capture-header*))
(nibbles:write-ub32/le (car (fc capture)) stream)
(close (capture-stream capture))
(setf (capture-stream capture) NIL)))
capture)
(defmethod active-p ((capture capture))
(not (null (capture-stream capture))))
(defmethod handle ((event tick) (capture capture))
(unless (fc capture)
(setf (fc capture) (cons -1 0)))
(setf (cdr (fc capture)) (fc event)))
(defmethod handle ((event event) (capture capture))
(let ((stream (capture-stream capture))
(struct (serialize-info (type-of event)))
(fc (fc capture)))
(when (and struct fc)
(unless (= (car fc) (cdr fc))
(let ((info (serialize-info 'frame-count-change)))
(write-byte (serialize-info-id info) stream)
(funcall (serialize-info-writer info) (cdr (fc capture)) stream))
(setf (car fc) (cdr fc)))
(write-byte (serialize-info-id struct) stream)
(funcall (serialize-info-writer struct) event stream))))
(defun start-capture (scene &optional (file (capture-pathname)))
(register (start (make-instance 'capture :file file)) scene))
(defun stop-capture (scene)
(stop (deregister (unit 'capture scene) scene)))
(defclass replay (unit listener)
((name :initform 'replay)
(file :initarg :file :accessor file)
(capture-stream :initform NIL :accessor capture-stream)
(buffer :initform NIL :accessor buffer)
(duration :initform NIL :accessor duration)
(fc :initform (cons NIL NIL) :accessor fc)))
(defmethod start ((replay replay))
(let ((stream (open (file replay) :element-type '(unsigned-byte 8) :direction :input)))
(loop for c across *capture-header*
do (assert (char= c (code-char (read-byte stream)))))
(setf (capture-stream replay) stream)
(setf (duration replay) (nibbles:read-ub32/le stream))
(when (= 0 (duration replay))
(v:warn :trial.capture "Capture file was not finalized.")
(setf (duration replay) most-positive-fixnum))
(let ((fc (funcall (serialize-info-reader (gethash (read-byte stream) +type-serialize-info+)) stream)))
(setf (fc replay) (cons fc fc)))
replay))
(defmethod stop ((replay replay))
(when (capture-stream replay)
(close (capture-stream replay))
(setf (capture-stream replay) NIL))
replay)
(defmethod handle ((ev tick) (replay replay))
(let ((fc (fc replay)))
(incf (car fc))
(when (= (duration replay) (car fc))
(v:info :trial.capture "Capture replay completed.")
(setf (cdr fc) most-positive-fixnum)
(stop replay)
(deregister replay (scene +main+)))
(when (< (cdr fc) (car fc))
(let ((stream (capture-stream replay)))
(loop for object = (funcall (serialize-info-reader (gethash (read-byte stream) +type-serialize-info+)) stream)
do (etypecase object
(integer
(setf (cdr fc) object)
(return))
(event
(handle object +main+))))))))
(defun start-replay (file scene)
(register (start (make-instance 'replay :file file)) scene))
(defun stop-replay (scene)
(stop (deregister (unit 'replay scene) scene)))