read the save, the seed and the worldgen assets
The engine's save classes are closed, so the protobuf field numbers were read off the data and matched to the map sizes TerraGenConfig gives. vsmv dump renders what the save stored, as the reference the generator will be diffed against. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
116
src/json.sex
Normal file
116
src/json.sex
Normal file
@@ -0,0 +1,116 @@
|
||||
;;; Loads the game's asset JSON. The game reads it with Newtonsoft,
|
||||
;;; which takes comments, unquoted keys and trailing commas; cJSON
|
||||
;;; takes none of them. So each file is first rewritten into strict
|
||||
;;; JSON, line for line, and then handed to cJSON.
|
||||
|
||||
(pub include "cJSON.h")
|
||||
(include stdio.h stdlib.h string.h ctype.h)
|
||||
|
||||
(fn ident-start-char ((c char)) bool
|
||||
(return (|| (isalpha (cast c u8)) (== c #\_) (== c #\$))))
|
||||
|
||||
(fn ident-char ((c char)) bool
|
||||
(return (|| (ident-start-char c) (isdigit (cast c u8)))))
|
||||
|
||||
;;; Drops a comma that was the last thing written before a closing
|
||||
;;; bracket: `[1, 2, ]' -> `[1, 2 ]'.
|
||||
(fn drop-trailing-comma ((out (* char)) (n size-t)) size-t
|
||||
(var k size-t n)
|
||||
(while (&& (> k 0) (isspace (cast [out (- k 1)] u8)))
|
||||
(-= k 1))
|
||||
(if (&& (> k 0) (== [out (- k 1)] #\,))
|
||||
(= [out (- k 1)] #\space))
|
||||
(return n))
|
||||
|
||||
;;; {code: "x", // note
|
||||
;;; values: [1, 2,],}
|
||||
;;; -> {"code": "x",
|
||||
;;; "values": [1, 2 ] }
|
||||
;;; Newlines are kept, so cJSON's error offsets map back to source lines.
|
||||
(pub fn json-strict ((src (* const char))) (* char)
|
||||
(var out (* char) (malloc (+ (* 3 (strlen src)) 1)))
|
||||
(var n size-t 0)
|
||||
(var p (* const char) src)
|
||||
(while (* p)
|
||||
(var c char (* p))
|
||||
(if (== c #\")
|
||||
(do
|
||||
(= [out n] c) (+= n 1) (+= p 1)
|
||||
(while (&& (* p) (!= (* p) #\"))
|
||||
(if (&& (== (* p) #\\) [p 1])
|
||||
(do (= [out n] (* p)) (+= n 1) (+= p 1)))
|
||||
(= [out n] (* p)) (+= n 1) (+= p 1))
|
||||
(if (* p)
|
||||
(do (= [out n] (* p)) (+= n 1) (+= p 1))))
|
||||
(&& (== c #\/) (== [p 1] #\/))
|
||||
(while (&& (* p) (!= (* p) #\newline))
|
||||
(+= p 1))
|
||||
(&& (== c #\/) (== [p 1] #\*))
|
||||
(do
|
||||
(+= p 2)
|
||||
(while (&& (* p) (! (&& (== (* p) #\*) (== [p 1] #\/))))
|
||||
(if (== (* p) #\newline)
|
||||
(do (= [out n] #\newline) (+= n 1)))
|
||||
(+= p 1))
|
||||
(if (* p) (+= p 2)))
|
||||
(|| (== c #\]) (== c #\}))
|
||||
(do
|
||||
(= n (drop-trailing-comma out n))
|
||||
(= [out n] c) (+= n 1) (+= p 1))
|
||||
(ident-start-char c)
|
||||
(do
|
||||
(var start (* const char) p)
|
||||
(while (ident-char (* p))
|
||||
(+= p 1))
|
||||
(var len size-t (cast (- p start) size-t))
|
||||
(var bare bool (|| (&& (== len 4) (|| (== (strncmp start "true" 4) 0)
|
||||
(== (strncmp start "null" 4) 0)))
|
||||
(&& (== len 5) (== (strncmp start "false" 5) 0))))
|
||||
(if (! bare)
|
||||
(do (= [out n] #\") (+= n 1)))
|
||||
(memcpy (+ out n) start len)
|
||||
(+= n len)
|
||||
(if (! bare)
|
||||
(do (= [out n] #\") (+= n 1))))
|
||||
(do
|
||||
(= [out n] c) (+= n 1) (+= p 1))))
|
||||
(= [out n] 0)
|
||||
(return out))
|
||||
|
||||
(fn read-file ((path (* const char))) (* char)
|
||||
(var f (* FILE) (fopen path "rb"))
|
||||
(if (! f)
|
||||
(return NULL))
|
||||
(fseek f 0 SEEK-END)
|
||||
(var len long (ftell f))
|
||||
(fseek f 0 SEEK-SET)
|
||||
(var text (* char) (malloc (+ len 1)))
|
||||
(var got size-t (fread text 1 len f))
|
||||
(fclose f)
|
||||
(= [text got] 0)
|
||||
(return text))
|
||||
|
||||
(fn line-of ((text (* const char)) (at (* const char))) int
|
||||
(var line int 1)
|
||||
(for (var p (* const char) text) (< p at) (++ p)
|
||||
(if (== (* p) #\newline)
|
||||
(++ line)))
|
||||
(return line))
|
||||
|
||||
;;; The parsed file, or NULL after saying what went wrong on stderr.
|
||||
;;; The caller frees it with cJSON-Delete.
|
||||
(pub fn json-load ((path (* const char))) (* cJSON)
|
||||
(var text (* char) (read-file path))
|
||||
(if (! text)
|
||||
(do
|
||||
(fprintf stderr "%s: cannot read\n" path)
|
||||
(return NULL)))
|
||||
(var strict (* char) (json-strict text))
|
||||
(free text)
|
||||
(var end (* const char) NULL)
|
||||
(var json (* cJSON) (cJSON-ParseWithOpts strict (& end) 1))
|
||||
(if (! json)
|
||||
(fprintf stderr "%s:%d: not JSON even after relaxing it\n"
|
||||
path (line-of strict end)))
|
||||
(free strict)
|
||||
(return json))
|
||||
Reference in New Issue
Block a user