mirror of
https://github.com/demostf/demo.js
synced 2026-06-04 00:54:14 +02:00
add event type generation for rust
This commit is contained in:
parent
423178da03
commit
23735c266b
1 changed files with 34 additions and 2 deletions
|
|
@ -23,15 +23,24 @@ fs.readFile(argv._[0], function (err, data) {
|
|||
return;
|
||||
}
|
||||
const match = analyser.getBody();
|
||||
const state = demo.getParser().parserState;
|
||||
if (argv['create-event-definitions']) {
|
||||
const definitions = Array.from(match.eventDefinitions.values());
|
||||
const definitions = Array.from(state.eventDefinitions.values());
|
||||
const definition = definitions
|
||||
.map(createEventDefinition)
|
||||
.join('\n\n')
|
||||
+ '\n\n' + createEventDefinitionUnion(definitions) + '\n\n'
|
||||
+ 'export type GameEventType = GameEvent[\'name\'];\n\n'
|
||||
+ createEventTypeMap(definitions) + '\n\n'
|
||||
+ createEventTypeIdMap(match.eventDefinitions) + '\n';
|
||||
+ createEventTypeIdMap(state.eventDefinitions) + '\n';
|
||||
console.log(definition);
|
||||
} else if (argv['create-event-definitions-rs']) {
|
||||
const definitions = Array.from(state.eventDefinitions.values());
|
||||
const definition = 'pub enum GameEvent {'
|
||||
+ definitions
|
||||
.map(createEventDefinitionRS)
|
||||
.join(',\n')
|
||||
+ '\n}';
|
||||
console.log(definition);
|
||||
} else if (argv['event-list']) {
|
||||
echo(Array.from(match.eventDefinitions.values()));
|
||||
|
|
@ -92,6 +101,22 @@ function getEntryTypeDefinition(typeId) {
|
|||
}
|
||||
}
|
||||
|
||||
function getEntryTypeDefinitionRS(typeId) {
|
||||
switch (typeId) {
|
||||
case 1:
|
||||
return 'String';
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
return 'u32';
|
||||
case 6:
|
||||
return 'bool';
|
||||
case 7:
|
||||
return 'null';
|
||||
}
|
||||
}
|
||||
|
||||
function createEventDefinition(definition) {
|
||||
return `
|
||||
export interface ${getEventTypeName(definition.name)}Event {
|
||||
|
|
@ -102,6 +127,13 @@ ${definition.entries.map(entry => ` ${entry.name}: ${getEntryTypeDefinition(ent
|
|||
}`.trim()
|
||||
}
|
||||
|
||||
function createEventDefinitionRS(definition) {
|
||||
return ` #[event_type(name = "${definition.name}")]
|
||||
${getEventTypeName(definition.name)} {
|
||||
${definition.entries.map(entry => `\t\t${entry.name}: ${getEntryTypeDefinitionRS(entry.type)};`).join('\n')}
|
||||
}`
|
||||
}
|
||||
|
||||
function createEventDefinitionUnion(definitions) {
|
||||
return `export type GameEvent = ` +
|
||||
definitions.map(definition => '\t' + getEventTypeName(definition.name) + 'Event')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue