-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.rkt
More file actions
376 lines (286 loc) · 10.7 KB
/
main.rkt
File metadata and controls
376 lines (286 loc) · 10.7 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#lang at-exp racket
(provide once-upon-a-time
extra-unreal-command-line-args
multiplayer
server-ip-address
demo-aether
current-file-name ;helps with simple mods
codespells-workspace
(all-from-out racket
codespells-server
codespells-server/unreal-js/unreal-client
codespells-runes codespells/demo-aether)
define-runtime-path
spawn-mod-blueprint ;should this file be providing this?
#%module-begin)
(require codespells/demo-aether
codespells-runes
codespells-server/unreal-js/util
(rename-in racket [#%module-begin #%old-module-begin]))
(module reader syntax/module-reader
codespells/main)
(define server-ip-address (make-parameter "127.0.0.1"))
(define multiplayer (make-parameter #f))
(define extra-unreal-command-line-args (make-parameter ""))
(define (once-upon-a-time #:aether aether #:world world)
(displayln "Starting World and Aether")
(world)
(parameterize ([running-as-multiplayer-server? (eq? 'server (multiplayer))])
(aether))
(let loop ()
(sleep 1)
(loop)))
;Should move dl to utility (outside of CodeSpells)
(provide dl)
(define (find-dl-size url)
(local-require net/http-easy)
(/
(string->number (bytes->string/utf-8 (response-headers-ref (head url) 'Content-Length)))
1000000))
(define (dl from to [size-in-megabytes (find-dl-size from)])
(local-require net/url)
(define the-url (string->url from))
(define in (get-pure-port the-url))
(define out (open-output-file to))
(thread (λ () (listen-for-progress in 0 size-in-megabytes)))
(copy-port in out)
(close-output-port out))
(define (listen-for-progress in last-percent-complete total-metabytes)
(sync (port-progress-evt in))
(unless (port-closed? in)
(define-values [line col pos] (port-next-location in))
;pos is the byte position
(define percent-complete (* 100 (exact->inexact (/ pos (* total-metabytes 1000000)))))
;(displayln percent-complete)
(if (> percent-complete (+ 1 last-percent-complete))
(begin
(printf "World download progress: ~a%\n"
(~r (min percent-complete 100)
#:precision 2))
(listen-for-progress in percent-complete total-metabytes))
(listen-for-progress in last-percent-complete total-metabytes))))
;TODO: Probably time to move this world stuff to a new file, if not a codespells-worlds repo
; Essentially a new module evolving below this line...
(provide demo-world)
(require racket/runtime-path
codespells-server
codespells-server/unreal-js/unreal-client)
(define-runtime-path js-runtime "./js/on-start.js")
(define codespells-workspace (make-parameter (current-directory)))
(define (demo-world)
(fetch-and-run-world
"https://codespells-org.s3.amazonaws.com/WorldTemplates/demo-world/0.0/CodeSpellsDemoWorld.zip"
"CodeSpellsDemoWorld"
"Minimal_Default"
))
;TODO: Move to new package
(provide temple-world)
(define (temple-world)
(fetch-and-run-world
"https://codespells-org.s3.amazonaws.com/WorldTemplates/temple-world/0.0/TempleWorld.zip"
"TempleWorld"
"DemoMap3"))
;TODO: Move to new package
(provide village-world)
(define (village-world)
(fetch-and-run-world
"https://codespells-org.s3.amazonaws.com/WorldTemplates/village-world/0.0/VillageWorld.zip"
"VillageWorld"
"AdvancedVillagePack_Showcase"))
;TODO: Move to new package
(provide polar-facility-world)
(define (polar-facility-world)
(fetch-and-run-world
"https://codespells-org.s3.amazonaws.com/WorldTemplates/polar-facility-world/0.0/PolarFacilityWorld.zip"
"PolarFacilityWorld"
"PolarFacilityMap"))
;TODO: Move to new package
(provide voxel-world)
(define (voxel-world)
(fetch-and-run-world
"https://codespells-org.s3.amazonaws.com/WorldTemplates/voxel-world/0.0/VoxelWorld.zip"
"VoxelWorld"
"VoxelWorld"
))
;TODO: Move to new package
(provide log-cabin-world)
(define (log-cabin-world)
(fetch-and-run-world
"https://codespells-org.s3.amazonaws.com/WorldTemplates/log-cabin-world/0.0/LogCabinWorld.zip"
"LogCabinWorld"
"Demonstration_Map"))
;TODO: Move to new package
(provide forest-world)
(define (forest-world)
(fetch-and-run-world
"https://codespells-org.s3.amazonaws.com/WorldTemplates/forest-world/0.0/ForestWorld.zip"
"ForestWorld"
"Demo_Scene"))
;TODO: Move to new package
(provide cave-world)
(define (cave-world)
(fetch-and-run-world
"https://codespells-org.s3.amazonaws.com/WorldTemplates/cave-world/0.0/CaveWorld.zip"
"CaveWorld"
"LV_Soul_Cave"))
(provide arena-world)
(define (arena-world)
(fetch-and-run-world
"https://codespells-org.s3.amazonaws.com/WorldTemplates/arena-world/0.0/ArenaWorld.zip"
"ArenaWorld"
"DemoMap1"))
(define (fetch-and-run-world world-installation-source world-name [map-name #f])
(local-require file/unzip net/sendurl)
(define zip-file-name (last (string-split world-installation-source "/")))
(define world-installation-target (build-path (codespells-workspace) world-name))
(lambda ()
(displayln (~a "Starting World: " world-name))
(when (and
(not (file-exists? (build-path (codespells-workspace) zip-file-name)))
(not (directory-exists? world-installation-target)))
(displayln "Downloading world zip file...")
(dl world-installation-source
(build-path (codespells-workspace) zip-file-name)
))
(when (not (directory-exists? world-installation-target))
(displayln "Unzipping")
(unzip (build-path (codespells-workspace) zip-file-name))
(rename-file-or-directory
(build-path (current-directory) world-name)
world-installation-target))
(copy-file js-runtime
(build-path (codespells-workspace)
world-name
world-name
"Content"
"Scripts"
"on-start.js")
#t)
(define multiplayer-command-line
(if (not (multiplayer))
""
(if (not map-name)
(error (~a "No map name is set for " world-name))
(if (eq? (multiplayer) 'client)
(server-ip-address)
(~a map-name "?listen")))))
(define exe (~a (~s (~a(build-path world-installation-target (~a world-name ".exe"))))
" " multiplayer-command-line
" -unreal-server=" (unreal-server-port)
" -codespells-server=" (codespells-server-port)
" " (extra-unreal-command-line-args)
))
(displayln (~a "Running " exe)) ;Assume Windows for now
(thread (thunk (system exe)))
))
;Another module shaping up here
(provide define-classic-rune
define-classic-rune-lang
spawn-this-mod-blueprint
(all-from-out 2htdp/image))
(require racket/runtime-path
syntax/parse/define
(for-syntax racket/syntax racket/path racket/list)
2htdp/image)
(define-syntax (define-classic-rune stx)
(syntax-parse stx
[(_ head #:background bg #:foreground fg lines ...)
#:with name (car (syntax-e #'head))
#:with name-rune (format-id stx "~a-rune" #'name)
#:with name-rune-binding (format-id stx "~a-rune-binding" #'name)
#`(begin
(provide name name-rune)
(define head
lines ...)
(define (name-rune)
(svg-rune-description
(rune-background
#:color bg
(rune-image fg))))
(define name-rune-binding
(html-rune 'name (name-rune)))
)]))
(define-syntax (define-classic-rune-lang stx)
(syntax-parse stx
[(_ name #:eval-from eval-from (rune-names ...))
#:with rune-bindings (map (lambda (n) (format-id stx "~a-rune-binding" n))
(syntax->list #'(rune-names ...))) ;(format-id stx "~a-rune-binding" 'hello)
#`(begin
(provide name)
(define (name #:with-paren-runes? [with-paren-runes? #f])
(local-require codespells-runes)
(rune-lang eval-from ;#,(syntax-source stx)
(rune-list #:with-paren-runes? with-paren-runes?
#,@#'rune-bindings
))))]))
(require (for-syntax codespells/modding/lib racket/format))
(define-for-syntax (this-unreal-mod-location stx)
;Hmmm. This doesn't work when we do `raco exe`, still references the old path
(build-path (path-only (syntax-source stx))
"BuildUnreal"
"WindowsNoEditor"
(~a (racket-id->unreal-id
(find-pkg-root-dir (path-only (syntax-source stx)))))
"Content"
"Paks"))
(define-for-syntax (this-unreal-mod-name stx)
(~a (racket-id->unreal-id
(find-pkg-root-dir (path-only (syntax-source stx))))))
;TODO: Could check at compile time that this BP exists...
; Could tell them what to do in the Unreal project...
(define-syntax (#%module-begin stx)
(syntax-parse stx
[(_ things ...)
#`(#%old-module-begin
things ...
)]))
(define-syntax (spawn-this-mod-blueprint stx)
(syntax-parse stx
[(_ name)
#`(spawn-mod-blueprint
#,(datum->syntax stx 'MyPak)
#,(datum->syntax stx 'mod-name)
name)
#;
#`(spawn-mod-blueprint
#,(this-unreal-mod-location stx)
#,(this-unreal-mod-name stx)
name)
]))
(define-syntax (current-file-name stx)
(datum->syntax stx 'main.rkt)
#;
#`#,(syntax-source stx))
(provide require-mod)
(require syntax/parse/define
(for-syntax racket/syntax))
(define-syntax (require-mod stx)
(syntax-parse stx
[(_ name)
#:with name: (format-id stx "~a:" #'name)
#'(begin
(provide (all-from-out name))
(require
(prefix-in name: (only-in name my-mod-lang))
(except-in name my-mod-lang)
))]))
(provide mod)
(define-syntax (mod stx)
(syntax-parse stx
[(mod the-mod-name
#:pak-folder
pak-folder-name)
#`(begin
(module mod-info racket
(define mod-name "MyMod")
;(define-runtime-path main.rkt "main.rkt")
;(define-runtime-path MyPak pak-folder-name)
)
(require 'mod-info)
)
#;
(datum->syntax stx `(begin
(define mod-name ,(syntax->datum #'the-mod-name))
(define-runtime-path main.rkt "main.rkt")
(define-runtime-path MyPak ,(syntax->datum #'pak-folder-name))
))]))