tirbofish/dropbear
main / crates / magna-carta / src / generator / jvm.rs · 5791 bytes
crates/magna-carta/src/generator/jvm.rs
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
use crate::ScriptManifest;
use crate::generator::Generator;
use chrono::Utc;
use std::collections::HashMap;
use std::fmt::Write;
pub struct KotlinJVMGenerator;
impl Generator for KotlinJVMGenerator {
fn generate(&self, manifest: &ScriptManifest) -> anyhow::Result<String> {
let mut output = String::new();
let local_time = Utc::now();
writeln!(
output,
"// Auto-generated by dropbear-engine with the magna-carta parser on {} UTC",
local_time.format("%Y-%m-%d %H:%M:%S")
)?;
writeln!(output)?;
writeln!(output, "package com.dropbear.decl")?;
writeln!(output)?;
let mut imported_packages = std::collections::HashSet::new();
for item in manifest.items() {
if let Some(last_dot) = item.fqcn().rfind('.') {
let package = &item.fqcn()[..last_dot];
imported_packages.insert(package.to_string());
}
}
for package in &imported_packages {
writeln!(output, "import {}.*", package)?;
}
writeln!(output)?;
let mut tag_map: HashMap<String, Vec<String>> = HashMap::new();
for item in manifest.items() {
let simple_name = item.simple_name();
if item.tags().is_empty() {
continue;
}
for tag in item.tags() {
tag_map
.entry(tag.clone())
.or_default()
.push(simple_name.to_string());
}
}
writeln!(output, "object RunnableRegistry {{")?;
writeln!(
output,
" private val tagRegistry = mutableMapOf<String, MutableList<() -> com.dropbear.ecs.System>>()"
)?;
writeln!(output)?;
writeln!(output, " init {{")?;
writeln!(output, " registerStaticScripts()")?;
writeln!(output, " }}")?;
writeln!(output)?;
writeln!(output, " private fun registerStaticScripts() {{")?;
for (tag, classes) in &tag_map {
writeln!(output, " // Tag: {}", tag)?;
for class in classes {
writeln!(
output,
" tagRegistry.computeIfAbsent(\"{}\") {{ mutableListOf() }}.add(::{})",
tag, class
)?;
}
}
writeln!(output, " }}")?;
writeln!(output)?;
writeln!(output, " @Synchronized")?;
writeln!(output, " fun reload() {{")?;
writeln!(output, " tagRegistry.clear()")?;
writeln!(output, " registerStaticScripts()")?;
writeln!(output, " }}")?;
writeln!(output)?;
writeln!(
output,
" fun getScriptFactories(tag: String): List<() -> com.dropbear.ecs.System> {{"
)?;
writeln!(
output,
" return tagRegistry[tag]?.toList() ?: emptyList()"
)?;
writeln!(output, " }}")?;
writeln!(output)?;
writeln!(
output,
" fun instantiateScripts(tag: String): List<com.dropbear.ecs.System> {{"
)?;
writeln!(
output,
" return getScriptFactories(tag).map {{ it() }}"
)?;
writeln!(output, " }}")?;
writeln!(output)?;
writeln!(output, " fun getAllTags(): Set<String> {{")?;
writeln!(output, " return tagRegistry.keys.toSet()")?;
writeln!(output, " }}")?;
writeln!(output, "}}")?;
writeln!(output)?;
writeln!(output, "object ComponentManager {{")?;
writeln!(
output,
" private val instances: MutableMap<String, com.dropbear.ecs.NativeComponent> = mutableMapOf()"
)?;
writeln!(output)?;
writeln!(
output,
" private fun registerOne(component: com.dropbear.ecs.NativeComponent) {{"
)?;
writeln!(output, " component.register()")?;
writeln!(
output,
" instances[component.fullyQualifiedTypeName] = component"
)?;
writeln!(output, " }}")?;
writeln!(output)?;
writeln!(output, " @Synchronized")?;
writeln!(output, " fun registerAll() {{")?;
for component in manifest.components() {
writeln!(
output,
" registerOne({simple}())",
simple = component.simple_name(),
)?;
}
writeln!(output, " }}")?;
writeln!(output)?;
writeln!(
output,
" fun updateKotlinComponent(fqcn: String, entityId: Long, dt: Double) {{"
)?;
writeln!(output, " val instance = instances[fqcn] ?: return")?;
writeln!(
output,
" val engine = com.dropbear.DropbearEngine(com.dropbear.DropbearEngine.native)"
)?;
writeln!(output, " instance.setCurrentEntity(entityId)")?;
writeln!(output, " instance.updateComponent(engine, dt)")?;
writeln!(output, " instance.clearCurrentEntity()")?;
writeln!(output, " }}")?;
writeln!(output)?;
writeln!(output, " fun inspectKotlinComponent(fqcn: String) {{")?;
writeln!(output, " val instance = instances[fqcn] ?: return")?;
writeln!(
output,
" val engine = com.dropbear.DropbearEngine(com.dropbear.DropbearEngine.native)"
)?;
writeln!(output, " instance.inspect(engine)")?;
writeln!(output, " }}")?;
writeln!(output, "}}")?;
Ok(output)
}
}