-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18f5c9e
commit 853a5a5
Showing
7 changed files
with
2,547 additions
and
6 deletions.
There are no files selected for viewing
Submodule ProtoDef
updated
4 files
+4 −0 | doc/datatypes/conditional.md | |
+1 −1 | schemas/conditional.json | |
+9 −0 | test/conditional.json | |
+2 −1 | test/datatype_tests_schema.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
diff --git a/src/compiler.js b/src/compiler.js | ||
index 82856a2..82ed7b3 100644 | ||
--- a/src/compiler.js | ||
+++ b/src/compiler.js | ||
@@ -32,6 +32,12 @@ class ProtoDefCompiler { | ||
this.sizeOfCompiler.addProtocol(protocolData, path) | ||
} | ||
|
||
+ addVariable (key, val) { | ||
+ this.readCompiler.addContextType(key, val) | ||
+ this.writeCompiler.addContextType(key, val) | ||
+ this.sizeOfCompiler.addContextType(key, val) | ||
+ } | ||
+ | ||
compileProtoDefSync (options = { printCode: false }) { | ||
const sizeOfCode = this.sizeOfCompiler.generate() | ||
const writeCode = this.writeCompiler.generate() | ||
@@ -70,6 +76,12 @@ class CompiledProtodef { | ||
return writeFn(value, buffer, cursor) | ||
} | ||
|
||
+ setVariable (key, val) { | ||
+ this.sizeOfCtx[key] = val | ||
+ this.readCtx[key] = val | ||
+ this.writeCtx[key] = val | ||
+ } | ||
+ | ||
sizeOf (value, type) { | ||
const sizeFn = this.sizeOfCtx[type] | ||
if (!sizeFn) { throw new Error('missing data type: ' + type) } | ||
diff --git a/src/datatypes/compiler-conditional.js b/src/datatypes/compiler-conditional.js | ||
index b817923..9d956be 100644 | ||
--- a/src/datatypes/compiler-conditional.js | ||
+++ b/src/datatypes/compiler-conditional.js | ||
@@ -10,7 +10,8 @@ module.exports = { | ||
let code = `switch (${compare}) {\n` | ||
for (const key in struct.fields) { | ||
let val = key | ||
- if (isNaN(val) && val !== 'true' && val !== 'false') val = `"${val}"` | ||
+ if (val.startsWith('/')) val = 'ctx.' + val.slice(1) // Root context variable | ||
+ else if (isNaN(val) && val !== 'true' && val !== 'false') val = `"${val}"` | ||
code += compiler.indent(`case ${val}: return ` + compiler.callType(struct.fields[key])) + '\n' | ||
} | ||
code += compiler.indent('default: return ' + compiler.callType(struct.default ? struct.default : 'void')) + '\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"main": [ | ||
"container", | ||
[ | ||
{ | ||
"name": "packet", | ||
"type": [ | ||
"switch", | ||
{ | ||
"compareTo": "name", | ||
"fields": { | ||
"color_palette": "ColorPalette", | ||
"draw_text": "DrawText" | ||
}, | ||
"default": "void" | ||
} | ||
] | ||
} | ||
] | ||
], | ||
"ColorPalette": [ | ||
"container", | ||
[ | ||
{ | ||
"name": "palette", | ||
"type": [ | ||
"array", | ||
{ | ||
"countType": "i32", | ||
"type": "string" | ||
} | ||
] | ||
} | ||
] | ||
], | ||
"DrawText": [ | ||
"container", | ||
[ | ||
{ | ||
"name": "color", | ||
"type": "i32" | ||
}, | ||
{ | ||
"name": "opacity", | ||
"type": [ | ||
"switch", | ||
{ | ||
"compareTo": "color", | ||
"fields": { | ||
"/color_transparent": "void" | ||
}, | ||
"default": "u8" | ||
} | ||
] | ||
} | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
main: | ||
packet: name ? | ||
if color_palette: ColorPalette | ||
if draw_text: DrawText | ||
|
||
ColorPalette: | ||
palette: string[]i32 | ||
|
||
DrawText: | ||
color: i32 | ||
opacity: color ? | ||
if /color_transparent: void | ||
default: u8 |
Oops, something went wrong.