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