@needle-tools/needle-component-compiler 3.0.0-alpha.1 → 3.0.0-alpha.1.999f08a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,307 +1,307 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.CSharpWriter = void 0;
19
- var base_compiler_1 = require("../base-compiler");
20
- var BaseWriter_1 = require("../BaseWriter");
21
- var allowDebugLogs = false;
22
- /** Inline type dictionary: TypeScript type name → fully-qualified C# type name */
23
- var TYPE_MAP = {
24
- // Primitives
25
- "number": "float",
26
- "boolean": "bool",
27
- "string": "string",
28
- "any": "object",
29
- "object": "UnityEngine.Object",
30
- "void": "void",
31
- // Unity base types
32
- "Behaviour": "UnityEngine.MonoBehaviour",
33
- "MonoBehaviour": "UnityEngine.MonoBehaviour",
34
- "Component": "UnityEngine.Component",
35
- "ScriptableObject": "UnityEngine.ScriptableObject",
36
- // GameObjects / Transforms
37
- "GameObject": "UnityEngine.GameObject",
38
- "Object3D": "UnityEngine.Transform",
39
- "Transform": "UnityEngine.Transform",
40
- "RectTransform": "UnityEngine.RectTransform",
41
- "AssetReference": "UnityEngine.Transform",
42
- "THREE.Object3D": "UnityEngine.Transform",
43
- // Math types
44
- "Vector2": "UnityEngine.Vector2",
45
- "Vector3": "UnityEngine.Vector3",
46
- "Vector4": "UnityEngine.Vector4",
47
- "Quaternion": "UnityEngine.Quaternion",
48
- "Color": "UnityEngine.Color",
49
- "RGBAColor": "UnityEngine.Color",
50
- "THREE.Color": "UnityEngine.Color",
51
- "THREE.Vector2": "UnityEngine.Vector2",
52
- "THREE.Vector3": "UnityEngine.Vector3",
53
- "THREE.Vector4": "UnityEngine.Vector4",
54
- "Matrix4": "UnityEngine.Matrix4x4",
55
- "Box3": "UnityEngine.Bounds",
56
- // Rendering
57
- "Renderer": "UnityEngine.Renderer",
58
- "Material": "UnityEngine.Material",
59
- "Mesh": "UnityEngine.Mesh",
60
- "Texture": "UnityEngine.Texture",
61
- "Texture2D": "UnityEngine.Texture2D",
62
- "RenderTexture": "UnityEngine.RenderTexture",
63
- // Audio
64
- "AudioClip": "UnityEngine.AudioClip",
65
- "AudioSource": "UnityEngine.AudioSource",
66
- // Animation
67
- "Animator": "UnityEngine.Animator",
68
- "Animation": "UnityEngine.Animation",
69
- "AnimationClip": "UnityEngine.AnimationClip",
70
- // Events
71
- "EventList": "UnityEngine.Events.UnityEvent",
72
- "UnityEvent": "UnityEngine.Events.UnityEvent",
73
- // Physics / Colliders
74
- "ICollider": "UnityEngine.Collider",
75
- // UI
76
- "Text": "UnityEngine.UI.Text",
77
- "Image": "UnityEngine.UI.Image",
78
- "RawImage": "UnityEngine.UI.RawImage",
79
- // Camera / Light
80
- "Camera": "UnityEngine.Camera",
81
- "Light": "UnityEngine.Light",
82
- // Timeline / Playables
83
- "SignalAsset": "UnityEngine.Timeline.SignalAsset",
84
- "PlayableDirector": "UnityEngine.Playables.PlayableDirector",
85
- // Miscellaneous
86
- "Rigidbody": "UnityEngine.Rigidbody",
87
- "Map": "System.Collections.Generic.Dictionary",
88
- };
89
- /** Lifecycle method name mappings: camelCase TS → PascalCase C# */
90
- var LIFECYCLE_METHODS = {
91
- "awake": "Awake",
92
- "start": "Start",
93
- "onEnable": "OnEnable",
94
- "onDisable": "OnDisable",
95
- "update": "Update",
96
- "lateUpdate": "LateUpdate",
97
- "fixedUpdate": "FixedUpdate",
98
- "onDestroy": "OnDestroy",
99
- "onCollisionEnter": "OnCollisionEnter",
100
- "onCollisionExit": "OnCollisionExit",
101
- "onCollisionStay": "OnCollisionStay",
102
- "onTriggerEnter": "OnTriggerEnter",
103
- "onTriggerExit": "OnTriggerExit",
104
- "onTriggerStay": "OnTriggerStay",
105
- "onApplicationQuit": "OnApplicationQuit",
106
- "onBecameVisible": "OnBecameVisible",
107
- "onBecameInvisible": "OnBecameInvisible",
108
- "onGUI": "OnGUI",
109
- "onValidate": "OnValidate",
110
- "reset": "Reset",
111
- };
112
- /** UnityEditor types that need #if UNITY_EDITOR wrapping */
113
- var EDITOR_TYPES = new Set([
114
- "UnityEditor", "AnimatorController", "EditorApplication",
115
- "EditorUtility", "SerializedObject", "SerializedProperty",
116
- ]);
117
- function isEditorType(typeName) {
118
- return EDITOR_TYPES.has(typeName) || typeName.startsWith("UnityEditor.");
119
- }
120
- var CSharpWriter = /** @class */ (function (_super) {
121
- __extends(CSharpWriter, _super);
122
- function CSharpWriter() {
123
- return _super !== null && _super.apply(this, arguments) || this;
124
- }
125
- CSharpWriter.prototype.resolveCSharpTypeName = function (typescriptTypeName) {
126
- var _a, _b;
127
- if (!typescriptTypeName)
128
- return undefined;
129
- // Strip THREE. prefix for lookup fallback
130
- var stripped = typescriptTypeName.startsWith("THREE.") ? typescriptTypeName.substring(6) : typescriptTypeName;
131
- return (_b = (_a = TYPE_MAP[typescriptTypeName]) !== null && _a !== void 0 ? _a : TYPE_MAP[stripped]) !== null && _b !== void 0 ? _b : typescriptTypeName;
132
- };
133
- CSharpWriter.prototype.startNewType = function (filePath, typeName, baseTypes, comments) {
134
- var _a;
135
- if (comments === null || comments === void 0 ? void 0 : comments.includes("@dont-generate-component"))
136
- return false;
137
- // @type override: first comment matching "@type <T>" overrides base class
138
- var resolvedBase = "UnityEngine.MonoBehaviour";
139
- var typeOverride = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@type "); });
140
- if (typeOverride) {
141
- resolvedBase = typeOverride.substring("@type ".length).trim();
142
- }
143
- else if (baseTypes === null || baseTypes === void 0 ? void 0 : baseTypes.length) {
144
- var first = baseTypes[0];
145
- resolvedBase = (_a = this.resolveCSharpTypeName(first)) !== null && _a !== void 0 ? _a : "UnityEngine.MonoBehaviour";
146
- }
147
- // Abstract modifier
148
- var isAbstract = comments === null || comments === void 0 ? void 0 : comments.includes("@abstract");
149
- var modifiers = isAbstract ? "abstract partial" : "partial";
150
- this.writer.writeLine("// NEEDLE_CODEGEN_START");
151
- this.writer.writeLine("// auto generated code - do not edit directly");
152
- this.writer.writeLine("");
153
- this.writer.writeLine("#pragma warning disable");
154
- this.writer.writeLine("");
155
- this.writer.beginBlock("namespace Needle.Typescript.GeneratedComponents\n{");
156
- this.writer.beginBlock("public ".concat(modifiers, " class ").concat(typeName, " : ").concat(resolvedBase, "\n{"));
157
- };
158
- CSharpWriter.prototype.endNewType = function (filePath, typeName) {
159
- this.writer.endBlock("}");
160
- this.writer.endBlock("}");
161
- this.writer.writeLine("");
162
- this.writer.writeLine("// NEEDLE_CODEGEN_END");
163
- this.writeCSharpScheme(filePath, typeName);
164
- };
165
- CSharpWriter.prototype.writeMember = function (visibility, name, isArray, type, initialValue, comments) {
166
- var _a;
167
- if (allowDebugLogs)
168
- console.log("[CSharp] writeMember", name, type, comments);
169
- // @nonSerialized → skip entirely
170
- if (comments === null || comments === void 0 ? void 0 : comments.some(function (c) { return c.startsWith("@nonSerialized"); }))
171
- return;
172
- // @type override
173
- var typeOverride = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@type "); });
174
- var csharpType = typeOverride
175
- ? typeOverride.substring("@type ".length).trim()
176
- : ((_a = this.resolveCSharpTypeName(type)) !== null && _a !== void 0 ? _a : type);
177
- // Determine visibility string
178
- var vis = "public";
179
- if (visibility === base_compiler_1.Visibility.Private || visibility === base_compiler_1.Visibility.Protected) {
180
- vis = visibility === base_compiler_1.Visibility.Protected ? "protected" : "private";
181
- }
182
- // @serializeField → add attribute + force private becomes serialized
183
- var serializeField = comments === null || comments === void 0 ? void 0 : comments.some(function (c) { return c.startsWith("@serializeField") || c.startsWith("@serializable"); });
184
- // @tooltip
185
- var tooltipComment = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@tooltip "); });
186
- var tooltip = tooltipComment ? tooltipComment.substring("@tooltip ".length).trim().replace(/^["']|["']$/g, "") : null;
187
- // @contextmenu
188
- var contextMenuComment = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@contextmenu "); });
189
- var contextMenu = contextMenuComment ? contextMenuComment.substring("@contextmenu ".length).trim() : null;
190
- // @ifdef
191
- var ifdefComment = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@ifdef "); });
192
- var ifdef = ifdefComment ? ifdefComment.substring("@ifdef ".length).trim() : null;
193
- // Skip private/protected fields unless @serializeField
194
- if (visibility !== base_compiler_1.Visibility.Public && !serializeField)
195
- return;
196
- // Resolve assignment
197
- var assignment = "";
198
- if (initialValue !== undefined && initialValue !== null) {
199
- if (initialValue === "null") {
200
- // keep null for reference types, skip for value types
201
- }
202
- else if (csharpType === "float") {
203
- assignment = " = ".concat(initialValue, "f");
204
- }
205
- else if (csharpType === "bool") {
206
- assignment = " = ".concat(initialValue.toLowerCase());
207
- }
208
- else if (csharpType === "string") {
209
- assignment = " = \"".concat(initialValue, "\"");
210
- }
211
- else if (initialValue.startsWith("new ")) {
212
- // constructor expression already prefixed with new
213
- var resolved = this.resolveNewExpression(initialValue);
214
- assignment = " = ".concat(resolved);
215
- }
216
- else {
217
- assignment = " = ".concat(initialValue);
218
- }
219
- }
220
- // Array suffix
221
- if (isArray)
222
- csharpType += "[]";
223
- // Editor type wrapping
224
- var needsEditorWrap = isEditorType(csharpType.split("[")[0].split("<")[0]);
225
- if (ifdef)
226
- this.writer.writeLine("#ifdef ".concat(ifdef));
227
- if (needsEditorWrap)
228
- this.writer.writeLine("#if UNITY_EDITOR");
229
- // Emit attributes
230
- if (tooltip)
231
- this.writer.writeLine("[UnityEngine.Tooltip(\"".concat(tooltip, "\")]"));
232
- if (contextMenu)
233
- this.writer.writeLine("[UnityEngine.ContextMenu(\"".concat(contextMenu, "\")]"));
234
- if (serializeField && visibility !== base_compiler_1.Visibility.Public) {
235
- this.writer.writeLine("[UnityEngine.SerializeField]");
236
- }
237
- // Emit field
238
- var varName = "@".concat(name);
239
- this.writer.writeLine("".concat(vis, " ").concat(csharpType, " ").concat(varName).concat(assignment, ";"));
240
- if (needsEditorWrap)
241
- this.writer.writeLine("#endif");
242
- if (ifdef)
243
- this.writer.writeLine("#endif");
244
- };
245
- CSharpWriter.prototype.writeMethod = function (visibility, name, returnType, args, comments) {
246
- var _this = this;
247
- var _a, _b;
248
- if (allowDebugLogs)
249
- console.log("[CSharp] writeMethod", name, returnType);
250
- // @contextmenu on method → add attribute
251
- var contextMenuComment = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@contextmenu "); });
252
- var contextMenu = contextMenuComment ? contextMenuComment.substring("@contextmenu ".length).trim() : null;
253
- // Only emit public methods and lifecycle methods (which are always public)
254
- var lifecycleName = LIFECYCLE_METHODS[name];
255
- if (visibility !== base_compiler_1.Visibility.Public && !lifecycleName)
256
- return;
257
- // Lifecycle methods get PascalCase; regular public methods keep their name as-is
258
- var methodName = lifecycleName !== null && lifecycleName !== void 0 ? lifecycleName : name;
259
- // Build parameter list — inline object types (e.g. { x: number }) resolve to "object"
260
- var paramList = (_a = args === null || args === void 0 ? void 0 : args.map(function (arg) {
261
- var _a, _b;
262
- var csharpType;
263
- if (!arg.type || arg.type.includes("{")) {
264
- csharpType = "object";
265
- }
266
- else {
267
- csharpType = (_b = (_a = _this.resolveCSharpTypeName(arg.type)) !== null && _a !== void 0 ? _a : arg.type) !== null && _b !== void 0 ? _b : "object";
268
- }
269
- return "".concat(csharpType, " @").concat(arg.name);
270
- }).join(", ")) !== null && _a !== void 0 ? _a : "";
271
- var csReturnType = returnType
272
- ? ((_b = this.resolveCSharpTypeName(returnType)) !== null && _b !== void 0 ? _b : returnType)
273
- : "void";
274
- if (contextMenu)
275
- this.writer.writeLine("[UnityEngine.ContextMenu(\"".concat(contextMenu, "\")]"));
276
- this.writer.writeLine("public void ".concat(methodName, "(").concat(paramList, ") {}"));
277
- };
278
- CSharpWriter.prototype.writeNewTypeExpression = function (typeName, args) {
279
- // no-op for C# output
280
- };
281
- // ── Helpers ────────────────────────────────────────────────────────────
282
- /** Converts camelCase to PascalCase for method names */
283
- CSharpWriter.prototype.toPascalCase = function (name) {
284
- return name.charAt(0).toUpperCase() + name.slice(1);
285
- };
286
- /** Resolve a "new Type(args)" expression: map TS types to C# qualified types */
287
- CSharpWriter.prototype.resolveNewExpression = function (expr) {
288
- var _a;
289
- // expr is already resolved: "new UnityEngine.Vector2(1, 0.5)" → "new UnityEngine.Vector2(1f, 0.5f)"
290
- var match = expr.match(/^new\s+([\w.]+)\s*\((.*)\)$/s);
291
- if (!match)
292
- return expr;
293
- var typeName = match[1], argsStr = match[2];
294
- var csharpType = (_a = this.resolveCSharpTypeName(typeName)) !== null && _a !== void 0 ? _a : typeName;
295
- // Convert numeric literals to float
296
- var csharpArgs = argsStr.replace(/(\d+\.?\d*)/g, function (n) {
297
- return n.includes(".") ? "".concat(n, "f") : "".concat(n, "f");
298
- });
299
- return "new ".concat(csharpType, "(").concat(csharpArgs, ")");
300
- };
301
- /** Override writeScheme to use .cs extension instead of .component.json */
302
- CSharpWriter.prototype.writeCSharpScheme = function (processingFilePath, component) {
303
- var res = this.sink.flush(component + ".cs", this.writer.flush());
304
- };
305
- return CSharpWriter;
306
- }(BaseWriter_1.BaseWriter));
307
- exports.CSharpWriter = CSharpWriter;
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.CSharpWriter = void 0;
19
+ var base_compiler_1 = require("../base-compiler");
20
+ var BaseWriter_1 = require("../BaseWriter");
21
+ var allowDebugLogs = false;
22
+ /** Inline type dictionary: TypeScript type name → fully-qualified C# type name */
23
+ var TYPE_MAP = {
24
+ // Primitives
25
+ "number": "float",
26
+ "boolean": "bool",
27
+ "string": "string",
28
+ "any": "object",
29
+ "object": "UnityEngine.Object",
30
+ "void": "void",
31
+ // Unity base types
32
+ "Behaviour": "UnityEngine.MonoBehaviour",
33
+ "MonoBehaviour": "UnityEngine.MonoBehaviour",
34
+ "Component": "UnityEngine.Component",
35
+ "ScriptableObject": "UnityEngine.ScriptableObject",
36
+ // GameObjects / Transforms
37
+ "GameObject": "UnityEngine.GameObject",
38
+ "Object3D": "UnityEngine.Transform",
39
+ "Transform": "UnityEngine.Transform",
40
+ "RectTransform": "UnityEngine.RectTransform",
41
+ "AssetReference": "UnityEngine.Transform",
42
+ "THREE.Object3D": "UnityEngine.Transform",
43
+ // Math types
44
+ "Vector2": "UnityEngine.Vector2",
45
+ "Vector3": "UnityEngine.Vector3",
46
+ "Vector4": "UnityEngine.Vector4",
47
+ "Quaternion": "UnityEngine.Quaternion",
48
+ "Color": "UnityEngine.Color",
49
+ "RGBAColor": "UnityEngine.Color",
50
+ "THREE.Color": "UnityEngine.Color",
51
+ "THREE.Vector2": "UnityEngine.Vector2",
52
+ "THREE.Vector3": "UnityEngine.Vector3",
53
+ "THREE.Vector4": "UnityEngine.Vector4",
54
+ "Matrix4": "UnityEngine.Matrix4x4",
55
+ "Box3": "UnityEngine.Bounds",
56
+ // Rendering
57
+ "Renderer": "UnityEngine.Renderer",
58
+ "Material": "UnityEngine.Material",
59
+ "Mesh": "UnityEngine.Mesh",
60
+ "Texture": "UnityEngine.Texture",
61
+ "Texture2D": "UnityEngine.Texture2D",
62
+ "RenderTexture": "UnityEngine.RenderTexture",
63
+ // Audio
64
+ "AudioClip": "UnityEngine.AudioClip",
65
+ "AudioSource": "UnityEngine.AudioSource",
66
+ // Animation
67
+ "Animator": "UnityEngine.Animator",
68
+ "Animation": "UnityEngine.Animation",
69
+ "AnimationClip": "UnityEngine.AnimationClip",
70
+ // Events
71
+ "EventList": "UnityEngine.Events.UnityEvent",
72
+ "UnityEvent": "UnityEngine.Events.UnityEvent",
73
+ // Physics / Colliders
74
+ "ICollider": "UnityEngine.Collider",
75
+ // UI
76
+ "Text": "UnityEngine.UI.Text",
77
+ "Image": "UnityEngine.UI.Image",
78
+ "RawImage": "UnityEngine.UI.RawImage",
79
+ // Camera / Light
80
+ "Camera": "UnityEngine.Camera",
81
+ "Light": "UnityEngine.Light",
82
+ // Timeline / Playables
83
+ "SignalAsset": "UnityEngine.Timeline.SignalAsset",
84
+ "PlayableDirector": "UnityEngine.Playables.PlayableDirector",
85
+ // Miscellaneous
86
+ "Rigidbody": "UnityEngine.Rigidbody",
87
+ "Map": "System.Collections.Generic.Dictionary",
88
+ };
89
+ /** Lifecycle method name mappings: camelCase TS → PascalCase C# */
90
+ var LIFECYCLE_METHODS = {
91
+ "awake": "Awake",
92
+ "start": "Start",
93
+ "onEnable": "OnEnable",
94
+ "onDisable": "OnDisable",
95
+ "update": "Update",
96
+ "lateUpdate": "LateUpdate",
97
+ "fixedUpdate": "FixedUpdate",
98
+ "onDestroy": "OnDestroy",
99
+ "onCollisionEnter": "OnCollisionEnter",
100
+ "onCollisionExit": "OnCollisionExit",
101
+ "onCollisionStay": "OnCollisionStay",
102
+ "onTriggerEnter": "OnTriggerEnter",
103
+ "onTriggerExit": "OnTriggerExit",
104
+ "onTriggerStay": "OnTriggerStay",
105
+ "onApplicationQuit": "OnApplicationQuit",
106
+ "onBecameVisible": "OnBecameVisible",
107
+ "onBecameInvisible": "OnBecameInvisible",
108
+ "onGUI": "OnGUI",
109
+ "onValidate": "OnValidate",
110
+ "reset": "Reset",
111
+ };
112
+ /** UnityEditor types that need #if UNITY_EDITOR wrapping */
113
+ var EDITOR_TYPES = new Set([
114
+ "UnityEditor", "AnimatorController", "EditorApplication",
115
+ "EditorUtility", "SerializedObject", "SerializedProperty",
116
+ ]);
117
+ function isEditorType(typeName) {
118
+ return EDITOR_TYPES.has(typeName) || typeName.startsWith("UnityEditor.");
119
+ }
120
+ var CSharpWriter = /** @class */ (function (_super) {
121
+ __extends(CSharpWriter, _super);
122
+ function CSharpWriter() {
123
+ return _super !== null && _super.apply(this, arguments) || this;
124
+ }
125
+ CSharpWriter.prototype.resolveCSharpTypeName = function (typescriptTypeName) {
126
+ var _a, _b;
127
+ if (!typescriptTypeName)
128
+ return undefined;
129
+ // Strip THREE. prefix for lookup fallback
130
+ var stripped = typescriptTypeName.startsWith("THREE.") ? typescriptTypeName.substring(6) : typescriptTypeName;
131
+ return (_b = (_a = TYPE_MAP[typescriptTypeName]) !== null && _a !== void 0 ? _a : TYPE_MAP[stripped]) !== null && _b !== void 0 ? _b : typescriptTypeName;
132
+ };
133
+ CSharpWriter.prototype.startNewType = function (filePath, typeName, baseTypes, comments) {
134
+ var _a;
135
+ if (comments === null || comments === void 0 ? void 0 : comments.includes("@dont-generate-component"))
136
+ return false;
137
+ // @type override: first comment matching "@type <T>" overrides base class
138
+ var resolvedBase = "UnityEngine.MonoBehaviour";
139
+ var typeOverride = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@type "); });
140
+ if (typeOverride) {
141
+ resolvedBase = typeOverride.substring("@type ".length).trim();
142
+ }
143
+ else if (baseTypes === null || baseTypes === void 0 ? void 0 : baseTypes.length) {
144
+ var first = baseTypes[0];
145
+ resolvedBase = (_a = this.resolveCSharpTypeName(first)) !== null && _a !== void 0 ? _a : "UnityEngine.MonoBehaviour";
146
+ }
147
+ // Abstract modifier
148
+ var isAbstract = comments === null || comments === void 0 ? void 0 : comments.includes("@abstract");
149
+ var modifiers = isAbstract ? "abstract partial" : "partial";
150
+ this.writer.writeLine("// NEEDLE_CODEGEN_START");
151
+ this.writer.writeLine("// auto generated code - do not edit directly");
152
+ this.writer.writeLine("");
153
+ this.writer.writeLine("#pragma warning disable");
154
+ this.writer.writeLine("");
155
+ this.writer.beginBlock("namespace Needle.Typescript.GeneratedComponents\n{");
156
+ this.writer.beginBlock("public ".concat(modifiers, " class ").concat(typeName, " : ").concat(resolvedBase, "\n{"));
157
+ };
158
+ CSharpWriter.prototype.endNewType = function (filePath, typeName) {
159
+ this.writer.endBlock("}");
160
+ this.writer.endBlock("}");
161
+ this.writer.writeLine("");
162
+ this.writer.writeLine("// NEEDLE_CODEGEN_END");
163
+ this.writeCSharpScheme(filePath, typeName);
164
+ };
165
+ CSharpWriter.prototype.writeMember = function (visibility, name, isArray, type, initialValue, comments) {
166
+ var _a;
167
+ if (allowDebugLogs)
168
+ console.log("[CSharp] writeMember", name, type, comments);
169
+ // @nonSerialized → skip entirely
170
+ if (comments === null || comments === void 0 ? void 0 : comments.some(function (c) { return c.startsWith("@nonSerialized"); }))
171
+ return;
172
+ // @type override
173
+ var typeOverride = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@type "); });
174
+ var csharpType = typeOverride
175
+ ? typeOverride.substring("@type ".length).trim()
176
+ : ((_a = this.resolveCSharpTypeName(type)) !== null && _a !== void 0 ? _a : type);
177
+ // Determine visibility string
178
+ var vis = "public";
179
+ if (visibility === base_compiler_1.Visibility.Private || visibility === base_compiler_1.Visibility.Protected) {
180
+ vis = visibility === base_compiler_1.Visibility.Protected ? "protected" : "private";
181
+ }
182
+ // @serializeField → add attribute + force private becomes serialized
183
+ var serializeField = comments === null || comments === void 0 ? void 0 : comments.some(function (c) { return c.startsWith("@serializeField") || c.startsWith("@serializable"); });
184
+ // @tooltip
185
+ var tooltipComment = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@tooltip "); });
186
+ var tooltip = tooltipComment ? tooltipComment.substring("@tooltip ".length).trim().replace(/^["']|["']$/g, "") : null;
187
+ // @contextmenu
188
+ var contextMenuComment = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@contextmenu "); });
189
+ var contextMenu = contextMenuComment ? contextMenuComment.substring("@contextmenu ".length).trim() : null;
190
+ // @ifdef
191
+ var ifdefComment = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@ifdef "); });
192
+ var ifdef = ifdefComment ? ifdefComment.substring("@ifdef ".length).trim() : null;
193
+ // Skip private/protected fields unless @serializeField
194
+ if (visibility !== base_compiler_1.Visibility.Public && !serializeField)
195
+ return;
196
+ // Resolve assignment
197
+ var assignment = "";
198
+ if (initialValue !== undefined && initialValue !== null) {
199
+ if (initialValue === "null") {
200
+ // keep null for reference types, skip for value types
201
+ }
202
+ else if (csharpType === "float") {
203
+ assignment = " = ".concat(initialValue, "f");
204
+ }
205
+ else if (csharpType === "bool") {
206
+ assignment = " = ".concat(initialValue.toLowerCase());
207
+ }
208
+ else if (csharpType === "string") {
209
+ assignment = " = \"".concat(initialValue, "\"");
210
+ }
211
+ else if (initialValue.startsWith("new ")) {
212
+ // constructor expression already prefixed with new
213
+ var resolved = this.resolveNewExpression(initialValue);
214
+ assignment = " = ".concat(resolved);
215
+ }
216
+ else {
217
+ assignment = " = ".concat(initialValue);
218
+ }
219
+ }
220
+ // Array suffix
221
+ if (isArray)
222
+ csharpType += "[]";
223
+ // Editor type wrapping
224
+ var needsEditorWrap = isEditorType(csharpType.split("[")[0].split("<")[0]);
225
+ if (ifdef)
226
+ this.writer.writeLine("#ifdef ".concat(ifdef));
227
+ if (needsEditorWrap)
228
+ this.writer.writeLine("#if UNITY_EDITOR");
229
+ // Emit attributes
230
+ if (tooltip)
231
+ this.writer.writeLine("[UnityEngine.Tooltip(\"".concat(tooltip, "\")]"));
232
+ if (contextMenu)
233
+ this.writer.writeLine("[UnityEngine.ContextMenu(\"".concat(contextMenu, "\")]"));
234
+ if (serializeField && visibility !== base_compiler_1.Visibility.Public) {
235
+ this.writer.writeLine("[UnityEngine.SerializeField]");
236
+ }
237
+ // Emit field
238
+ var varName = "@".concat(name);
239
+ this.writer.writeLine("".concat(vis, " ").concat(csharpType, " ").concat(varName).concat(assignment, ";"));
240
+ if (needsEditorWrap)
241
+ this.writer.writeLine("#endif");
242
+ if (ifdef)
243
+ this.writer.writeLine("#endif");
244
+ };
245
+ CSharpWriter.prototype.writeMethod = function (visibility, name, returnType, args, comments) {
246
+ var _this = this;
247
+ var _a, _b;
248
+ if (allowDebugLogs)
249
+ console.log("[CSharp] writeMethod", name, returnType);
250
+ // @contextmenu on method → add attribute
251
+ var contextMenuComment = comments === null || comments === void 0 ? void 0 : comments.find(function (c) { return c.startsWith("@contextmenu "); });
252
+ var contextMenu = contextMenuComment ? contextMenuComment.substring("@contextmenu ".length).trim() : null;
253
+ // Only emit public methods and lifecycle methods (which are always public)
254
+ var lifecycleName = LIFECYCLE_METHODS[name];
255
+ if (visibility !== base_compiler_1.Visibility.Public && !lifecycleName)
256
+ return;
257
+ // Lifecycle methods get PascalCase; regular public methods keep their name as-is
258
+ var methodName = lifecycleName !== null && lifecycleName !== void 0 ? lifecycleName : name;
259
+ // Build parameter list — inline object types (e.g. { x: number }) resolve to "object"
260
+ var paramList = (_a = args === null || args === void 0 ? void 0 : args.map(function (arg) {
261
+ var _a, _b;
262
+ var csharpType;
263
+ if (!arg.type || arg.type.includes("{")) {
264
+ csharpType = "object";
265
+ }
266
+ else {
267
+ csharpType = (_b = (_a = _this.resolveCSharpTypeName(arg.type)) !== null && _a !== void 0 ? _a : arg.type) !== null && _b !== void 0 ? _b : "object";
268
+ }
269
+ return "".concat(csharpType, " @").concat(arg.name);
270
+ }).join(", ")) !== null && _a !== void 0 ? _a : "";
271
+ var csReturnType = returnType
272
+ ? ((_b = this.resolveCSharpTypeName(returnType)) !== null && _b !== void 0 ? _b : returnType)
273
+ : "void";
274
+ if (contextMenu)
275
+ this.writer.writeLine("[UnityEngine.ContextMenu(\"".concat(contextMenu, "\")]"));
276
+ this.writer.writeLine("public void ".concat(methodName, "(").concat(paramList, ") {}"));
277
+ };
278
+ CSharpWriter.prototype.writeNewTypeExpression = function (typeName, args) {
279
+ // no-op for C# output
280
+ };
281
+ // ── Helpers ────────────────────────────────────────────────────────────
282
+ /** Converts camelCase to PascalCase for method names */
283
+ CSharpWriter.prototype.toPascalCase = function (name) {
284
+ return name.charAt(0).toUpperCase() + name.slice(1);
285
+ };
286
+ /** Resolve a "new Type(args)" expression: map TS types to C# qualified types */
287
+ CSharpWriter.prototype.resolveNewExpression = function (expr) {
288
+ var _a;
289
+ // expr is already resolved: "new UnityEngine.Vector2(1, 0.5)" → "new UnityEngine.Vector2(1f, 0.5f)"
290
+ var match = expr.match(/^new\s+([\w.]+)\s*\((.*)\)$/s);
291
+ if (!match)
292
+ return expr;
293
+ var typeName = match[1], argsStr = match[2];
294
+ var csharpType = (_a = this.resolveCSharpTypeName(typeName)) !== null && _a !== void 0 ? _a : typeName;
295
+ // Convert numeric literals to float
296
+ var csharpArgs = argsStr.replace(/(\d+\.?\d*)/g, function (n) {
297
+ return n.includes(".") ? "".concat(n, "f") : "".concat(n, "f");
298
+ });
299
+ return "new ".concat(csharpType, "(").concat(csharpArgs, ")");
300
+ };
301
+ /** Override writeScheme to use .cs extension instead of .component.json */
302
+ CSharpWriter.prototype.writeCSharpScheme = function (processingFilePath, component) {
303
+ var res = this.sink.flush(component + ".cs", this.writer.flush());
304
+ };
305
+ return CSharpWriter;
306
+ }(BaseWriter_1.BaseWriter));
307
+ exports.CSharpWriter = CSharpWriter;