Debian Codegraph @

tools/internal/x3d-nodes-to-pascal/code/generateprocessors.pas

raw
1 // -*- compile-command: "castle-engine compile && cd .. && ./run.sh" -*-
2 {
3 Copyright 2015-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Processors that do the actual job of reading X3D nodes
18 specification and generating output. }
19 unit GenerateProcessors;
20
21 interface
22
23 uses SysUtils, Generics.Collections, Classes,
24 CastleClassUtils, CastleStringUtils, CastleTimeUtils, CastleLog,
25 CastleColors, CastleUtils, CastleApplicationProperties, CastleFilesUtils,
26 CastleDownload;
27
28 type
29 { X3D field/event access type.
30 Copies from X3DNodes, not used, this is a litle more comfortable.
31 Because when the x3d-nodes-to-pascal is tested, the X3DNodes unit
32 is sometimes temporarily broken:) }
33 TX3DAccessType = (atInputOnly, atOutputOnly, atInitializeOnly, atInputOutput);
34
35 EInvalidSpecificationFile = class(Exception);
36
37 TX3DNodeInformationList = class;
38 TX3DFieldInformationList = class;
39
40 TX3DNodeInformation = class
41 X3DType: String;
42 Ancestors: TX3DNodeInformationList;
43 { DefaultContainerField for XML encoding.
44 Leave empty to use ancestor's DefaultContainerField value. }
45 DefaultContainerField: String;
46 { All fields within this node. }
47 Fields: TX3DFieldInformationList;
48 Vrml1: Boolean;
49 constructor Create;
50 destructor Destroy; override;
51 function PascalType(const ForceAsFunctionality: boolean = false): String;
52 function IsFunctionality: boolean;
53 function IsAbstract: boolean;
54 end;
55
56 TX3DNodeInformationList = class(specialize TObjectList<TX3DNodeInformation>)
57 function PascalTypesList: String;
58 end;
59
60 TX3DFieldInformation = class
61 X3DType: String;
62 X3DName, PascalName, PascalNamePrefixed: String;
63 X3DAccessType: String;
64 EnumType, EnumNames, EnumDefault: String;
65 DefaultValue, Comment: String;
66 AllowedChildrenNodes: TX3DNodeInformationList;
67 NotSlim: Boolean;
68 ChangeAlways: String; //< Value of ChangeAlways for this field, as String
69 Range: String; //< Allowed field values, in format specific to given field type
70 Documentation: String;
71 SetterBefore: String;
72 { Set TSFFloat/TMFFloat.Angle (to make it treated as an angle for UNIT conversions. }
73 Angle: Boolean;
74 WeakLink: Boolean;
75 Vrml1EnumNames: String;
76 Vrml1BitMaskNames: String;
77 constructor Create;
78 destructor Destroy; override;
79 function AccessType: TX3DAccessType;
80 function PascalClass: String;
81
82 { Type of a helper property to get and set this field.
83 It can be something simple like 'Single' for SFFloat.
84 Can be '' if no helper property. }
85 function PascalHelperType: String;
86
87 { Types of a helper public setter method to set this field. }
88 procedure PascalSetterTypes(const Names: TCastleStringList);
89
90 { Field is of SFNode or MFNode type. }
91 function IsNode: boolean;
92
93 { Pascal preprocessor conditional expression to include/exclude this field.
94 These methods either return empty string, or a line terminated by NL. }
95 function ConditionsBegin: String;
96 function ConditionsEnd: String;
97
98 { Field is SFString / MFString with a strictly limited set of values. }
99 function IsEnumString: Boolean;
100
101 { Field type is numeric, and range indicates it must be >= 0. }
102 function MustBeNonnegative: Boolean;
103
104 { Call when the field is completely parsed, e.g. Range is known. }
105 procedure Finished;
106 end;
107
108 TX3DFieldInformationList = class(specialize TObjectList<TX3DFieldInformation>)
109 end;
110
111 TProcessor = class abstract
112 public
113 procedure ProcessFile(const InputFileName: String);
114 procedure NodeBegin(const Node: TX3DNodeInformation); virtual;
115 procedure NodeField(const Node: TX3DNodeInformation;
116 const Field: TX3DFieldInformation); virtual;
117 procedure NodeEnd(const Node: TX3DNodeInformation); virtual;
118 procedure ComponentEnd(const ComponentName: String); virtual;
119 end;
120
121 THelperProcessor = class(TProcessor)
122 strict private
123 OutputPrivateInterface, OutputPublicInterface,
124 OutputImplementation, OutputCreateImplementation: String;
125 public
126 procedure NodeBegin(const Node: TX3DNodeInformation); override;
127 procedure NodeField(const Node: TX3DNodeInformation;
128 const Field: TX3DFieldInformation); override;
129 procedure NodeEnd(const Node: TX3DNodeInformation); override;
130 end;
131
132 var
133 Verbose: Boolean;
134
135 { When will the .inc files be generated.
136 May but doesn't have to end with PathDelim. }
137 OutputPath: String = '../../../src/scene/x3d/auto_generated_node_helpers/';
138
139 implementation
140
141 uses DateUtils;
142
143 function DocToPascal(const S: String): String;
144 begin
145 if Pos('}', S) <> 0 then
146 raise Exception.Create('Node documentation cannot use "}" character (Pascal comment closing char) for now');
147 Result := Trim(S);
148 end;
149
150 procedure WritelnVerbose(const S: String);
151 begin
152 if Verbose then
153 Writeln(ErrOutput, 'VERBOSE NOTE: ' + S);
154 end;
155
156 procedure AddAutoGeneratedField(
157 const Node: TX3DNodeInformation;
158 const Field: TX3DFieldInformation;
159 var OutputInterface, OutputImplementation: String);
160 var
161 FieldImplementationComment, AllowedChildrenNodesStr: String;
162 FieldConfigure, FieldExposed, FieldDefaultValue: String;
163 begin
164 FieldConfigure := '';
165
166 if Field.AccessType in [atInputOnly, atOutputOnly] then
167 begin
168 if Node.IsFunctionality then
169 begin
170 OutputInterface +=
171 Field.ConditionsBegin +
172 ' { ' + DocToPascal(Field.Documentation) + ' }' + NL +
173 ' // property ' + Field.PascalNamePrefixed + ': ' + Field.PascalClass + ';' + NL +
174 Field.ConditionsEnd;
175 end else
176 begin
177 OutputInterface +=
178 NL +
179 Field.ConditionsBegin +
180 ' strict private F' + Field.PascalNamePrefixed + ': ' + Field.PascalClass + ';' + NL +
181 ' { ' + DocToPascal(Field.Documentation) + ' }' + NL +
182 ' public property ' + Field.PascalNamePrefixed + ': ' + Field.PascalClass + ' read F' + Field.PascalNamePrefixed + ';' + NL +
183 Field.ConditionsEnd;
184
185 OutputImplementation +=
186 NL +
187 Field.ConditionsBegin +
188 ' F' + Field.PascalNamePrefixed + ' := ' + Field.PascalClass + '.Create(Self, ''' + Field.X3DName + ''', ' + LowerCase(BoolToStr(Field.AccessType = atInputOnly, true)) + ');' + NL +
189 ' AddEvent(F' + Field.PascalNamePrefixed + ');' + NL +
190 Field.ConditionsEnd;
191 end;
192 end else
193 begin
194 if Node.IsFunctionality then
195 begin
196 OutputInterface +=
197 Field.ConditionsBegin +
198 ' { ' + DocToPascal(Field.Documentation) + ' }' + NL +
199 ' // property ' + Field.PascalNamePrefixed + ': ' + Field.PascalClass + ';' + NL +
200 Field.ConditionsEnd;
201 end else
202 begin
203 OutputInterface +=
204 NL +
205 Field.ConditionsBegin +
206 ' strict private F' + Field.PascalNamePrefixed + ': ' + Field.PascalClass + ';' + NL +
207 ' { Internal wrapper for property @code(' + Field.PascalName + '). This wrapper API may change, we advise to access simpler @code(' + Field.PascalName + ') instead, if it is defined (TODO: for now, some field types do not have a simpler counterpart). }' + NL +
208 ' public property ' + Field.PascalNamePrefixed + ': ' + Field.PascalClass + ' read F' + Field.PascalNamePrefixed + ';' + NL +
209 Field.ConditionsEnd;
210
211 FieldConfigure += ' ' + Field.PascalNamePrefixed + '.ChangeAlways := ' + Field.ChangeAlways + ';' + NL;
212 if Field.Angle then
213 FieldConfigure += ' ' + Field.PascalNamePrefixed + '.Angle := true;' + NL;
214 if Field.MustBeNonnegative then
215 FieldConfigure += ' ' + Field.PascalNamePrefixed + '.MustBeNonnegative := true;' + NL;
216 if Field.WeakLink then
217 FieldConfigure += ' ' + Field.PascalNamePrefixed + '.WeakLink := true;' + NL;
218 if Field.SetterBefore <> '' then
219 FieldConfigure += ' ' + Field.PascalNamePrefixed + '.OnBeforeValueChange := {$ifdef FPC}@{$endif}' + Field.SetterBefore + ';' + NL;
220
221 FieldExposed := BoolToStr(Field.AccessType = atInputOutput, true);
222
223 if Field.Comment <> '' then
224 FieldImplementationComment := ' { X3D specification comment: ' + SReplaceChars(Field.Comment, ['{', '}'], '_') + ' }' + NL
225 else
226 FieldImplementationComment := '';
227
228 if Field.IsNode then
229 begin
230 { range: X3DUrlObject means that we limit the node by TUrlFunctionality,
231 and TMFNode.Create accepts only single argument (not a list) in this case.
232 For now just hardcode this case. }
233 if (Field.AllowedChildrenNodes.Count = 1) and
234 (Field.AllowedChildrenNodes[0].X3DType = 'X3DUrlObject') then
235 AllowedChildrenNodesStr := Field.AllowedChildrenNodes.PascalTypesList
236 else
237 AllowedChildrenNodesStr := '[' + Field.AllowedChildrenNodes.PascalTypesList + ']';
238 OutputImplementation +=
239 NL +
240 Field.ConditionsBegin +
241 ' F' + Field.PascalNamePrefixed + ' := ' + Field.PascalClass + '.Create(Self, ' + FieldExposed + ', ''' + Field.X3DName + ''', ' + AllowedChildrenNodesStr + ');' + NL +
242 FieldConfigure +
243 ' AddField(F' + Field.PascalNamePrefixed + ');' + NL +
244 FieldImplementationComment +
245 Field.ConditionsEnd;
246 end else
247 begin
248 { calculate FieldDefaultValue }
249 if Field.X3DType = 'SFEnum' then
250 FieldDefaultValue := '@' + Field.Vrml1EnumNames + ', Ord(High(' + Field.Vrml1EnumNames + ')) + 1, ' + Field.DefaultValue
251 else
252 if Field.X3DType = 'SFBitMask' then
253 FieldDefaultValue := Field.Vrml1BitMaskNames + ', ' + Field.DefaultValue
254 else
255 if Field.IsEnumString then
256 FieldDefaultValue := '@' + Field.EnumNames + ', Ord(High(' + Field.EnumNames + ')) + 1, Ord(' + Field.EnumDefault + ')'
257 else
258 FieldDefaultValue := Field.DefaultValue;
259
260 OutputImplementation +=
261 NL +
262 Field.ConditionsBegin +
263 ' F' + Field.PascalNamePrefixed + ' := ' + Field.PascalClass + '.Create(Self, ' + FieldExposed + ', ''' + Field.X3DName + ''', ' + FieldDefaultValue + ');' + NL +
264 FieldConfigure +
265 ' AddField(F' + Field.PascalNamePrefixed + ');' + NL +
266 FieldImplementationComment +
267 Field.ConditionsEnd;
268 end;
269 end;
270 end;
271 end;
272
273 { TX3DNodeInformation -------------------------------------------------------- }
274
275 constructor TX3DNodeInformation.Create;
276 begin
277 inherited;
278 Ancestors := TX3DNodeInformationList.Create;
279 Fields := TX3DFieldInformationList.Create;
280 end;
281
282 destructor TX3DNodeInformation.Destroy;
283 begin
284 FreeAndNil(Ancestors);
285 FreeAndNil(Fields);
286 inherited;
287 end;
288
289 function TX3DNodeInformation.IsFunctionality: boolean;
290 begin
291 Result := IsSuffix('Object', X3DType);
292 end;
293
294 function TX3DNodeInformation.IsAbstract: boolean;
295 begin
296 Result := IsPrefix('X3D', X3DType) or IsPrefix('AbstractVrml1', X3DType);
297 end;
298
299 function TX3DNodeInformation.PascalType(const ForceAsFunctionality: boolean): String;
300 begin
301 Result := X3DType;
302
303 { If this looks like a TXxxNode, then assume it is already a Pascal name spelled explicitly
304 in txt file. }
305 if IsPrefix('T', X3DType, false) and
306 (
307 IsSuffix('Node', X3DType, false) or
308 IsSuffix('Node_1', X3DType, false) or
309 IsSuffix('Node_2', X3DType, false)
310 ) then
311 Exit;
312
313 if ForceAsFunctionality or IsFunctionality then
314 begin
315 // change 'X3DUrlObject' into 'TUrlFunctionality'
316 Result := PrefixRemove('X3D', Result, true);
317 // Result := SuffixRemove('Node', Result, true); // not needed
318 Result := SuffixRemove('Object', Result, true);
319 Result := 'T' + Result + 'Functionality';
320 end else
321 begin
322 // replace X3D / AbstractVrml1 prefix with Abstract prefix
323 if IsAbstract then
324 begin
325 Result := PrefixRemove('X3D', Result, true);
326 Result := PrefixRemove('AbstractVrml1', Result, true);
327 Result := 'Abstract' + Result;
328 end;
329
330 // always end with Node suffix
331 Result := SuffixRemove('Node', Result, true);
332 Result := Result + 'Node';
333
334 // avoid TAbstractMetadataObjectNode, make it just TAbstractMetadataNode
335 if IsSuffix('ObjectNode', Result) then
336 Result := SuffixRemove('ObjectNode', Result, true) + 'Node';
337
338 // add _1 to VRML 1 nodes, to not conflict with node names from VRML 2 / X3D
339 if Vrml1 then
340 Result := Result + '_1';
341
342 Result := 'T' + Result;
343 end;
344 end;
345
346 { TX3DNodeInformationList ---------------------------------------------------- }
347
348 function TX3DNodeInformationList.PascalTypesList: String;
349 var
350 I: Integer;
351 begin
352 Result := '';
353 if Count <> 0 then
354 begin
355 for I := 0 to Count - 2 do
356 Result += Items[I].PascalType + ', ';
357 Result += Items[Count - 1].PascalType;
358 end;
359 end;
360
361 { TX3DFieldInformation ------------------------------------------------------- }
362
363 constructor TX3DFieldInformation.Create;
364 begin
365 inherited;
366 AllowedChildrenNodes := TX3DNodeInformationList.Create;
367 ChangeAlways := 'chVisibleNonGeometry'; // default
368 end;
369
370 destructor TX3DFieldInformation.Destroy;
371 begin
372 FreeAndNil(AllowedChildrenNodes);
373 inherited;
374 end;
375
376 function TX3DFieldInformation.AccessType: TX3DAccessType;
377 begin
378 if X3DAccessType = '[]' then
379 AccessType := atInitializeOnly
380 else
381 if X3DAccessType = '[in]' then
382 AccessType := atInputOnly
383 else
384 if X3DAccessType = '[out]' then
385 AccessType := atOutputOnly
386 else
387 if X3DAccessType = '[in,out]' then
388 AccessType := atInputOutput
389 else
390 raise EInvalidSpecificationFile.Create('Unrecognized field access type "' + X3DAccessType + '"');
391 end;
392
393 function TX3DFieldInformation.IsNode: boolean;
394 begin
395 Result := (X3DType = 'SFNode') or (X3DType = 'MFNode');
396 end;
397
398 function TX3DFieldInformation.PascalClass: String;
399 begin
400 if AccessType in [atInputOnly, atOutputOnly] then
401 Result := 'T' + X3DType + 'Event'
402 else
403 if (X3DType = 'SFString') and IsEnumString then
404 Result := 'TSFStringEnum'
405 else
406 if (X3DType = 'MFString') and IsEnumString then
407 Result := 'TMFStringEnum' // for things like TFontStyleNode.Justify
408 else
409 Result := 'T' + X3DType;
410 end;
411
412 function TX3DFieldInformation.PascalHelperType: String;
413 begin
414 if (X3DType = 'SFString') and IsEnumString and (EnumType <> '') then
415 Result := EnumType
416 else
417 if X3DType = 'SFFloat' then
418 Result := 'Single'
419 else
420 if X3DType = 'SFDouble' then
421 Result := 'Double'
422 else
423 if X3DType = 'SFTime' then
424 Result := 'TFloatTime'
425 else
426 if X3DType = 'SFVec2f' then
427 Result := 'TVector2'
428 else
429 if X3DType = 'SFVec3f' then
430 Result := 'TVector3'
431 else
432 if X3DType = 'SFVec4f' then
433 Result := 'TVector4'
434 else
435 if X3DType = 'SFVec2d' then
436 Result := 'TVector2Double'
437 else
438 if X3DType = 'SFVec3d' then
439 Result := 'TVector3Double'
440 else
441 if X3DType = 'SFVec4d' then
442 Result := 'TVector4Double'
443 else
444 if X3DType = 'SFInt32' then
445 Result := 'Integer'
446 else
447 if X3DType = 'SFBool' then
448 Result := 'Boolean'
449 else
450 if X3DType = 'SFRotation' then
451 Result := 'TVector4'
452 else
453 if X3DType = 'SFColor' then
454 Result := 'TCastleColorRGB'
455 else
456 if X3DType = 'SFColorRGBA' then
457 Result := 'TCastleColor'
458 else
459 if X3DType = 'SFString'then
460 Result := 'String'
461 else
462 if X3DType = 'SFMatrix3f' then
463 Result := 'TMatrix3'
464 else
465 if X3DType = 'SFMatrix4f' then
466 Result := 'TMatrix4'
467 else
468 if X3DType = 'SFMatrix3d' then
469 Result := 'TMatrix3Double'
470 else
471 if X3DType = 'SFMatrix4d' then
472 Result := 'TMatrix4Double'
473 else
474 // if X3DType = 'SFNode' then // these are converted to Pascal using AllowedPascalClass
475 // Result := 'TXxx'
476 // else
477 Result := '';
478 end;
479
480 procedure TX3DFieldInformation.PascalSetterTypes(const Names: TCastleStringList);
481 begin
482 if (X3DType = 'MFString') and IsEnumString and (EnumType <> '') then
483 begin
484 Names.Add('array of ' + EnumType);
485 Names.Add('T' + EnumType + 'List');
486 end else
487 if X3DType = 'MFFloat' then
488 begin
489 Names.Add('array of Single');
490 Names.Add('TSingleList');
491 end else
492 if X3DType = 'MFDouble' then
493 begin
494 Names.Add('array of Double');
495 Names.Add('TDoubleList');
496 end else
497 if X3DType = 'MFTime' then
498 begin
499 Names.Add('array of TFloatTime');
500 Names.Add('TDoubleList');
501 end else
502 if X3DType = 'MFVec2f' then
503 begin
504 Names.Add('array of TVector2');
505 Names.Add('TVector2List');
506 end else
507 if X3DType = 'MFVec3f' then
508 begin
509 Names.Add('array of TVector3');
510 Names.Add('TVector3List');
511 end else
512 if X3DType = 'MFVec4f' then
513 begin
514 Names.Add('array of TVector4');
515 Names.Add('TVector4List');
516 end else
517 if X3DType = 'MFVec2d' then
518 begin
519 Names.Add('array of TVector2Double');
520 Names.Add('TVector2DoubleList');
521 end else
522 if X3DType = 'MFVec3d' then
523 begin
524 Names.Add('array of TVector3Double');
525 Names.Add('TVector3DoubleList');
526 end else
527 if X3DType = 'MFVec4d' then
528 begin
529 Names.Add('array of TVector4Double');
530 Names.Add('TVector4DoubleList');
531 end else
532 if X3DType = 'MFInt32' then
533 begin
534 Names.Add('array of Int32');
535 Names.Add('TInt32List');
536 end else
537 if X3DType = 'MFBool' then
538 begin
539 Names.Add('array of boolean');
540 Names.Add('TBooleanList');
541 end else
542 if X3DType = 'MFRotation' then
543 begin
544 Names.Add('array of TVector4');
545 Names.Add('TVector4List');
546 end else
547 if X3DType = 'MFColor' then
548 begin
549 Names.Add('array of TCastleColorRGB');
550 Names.Add('TVector3List');
551 end else
552 if X3DType = 'MFColorRGBA' then
553 begin
554 Names.Add('array of TCastleColor');
555 Names.Add('TVector4List');
556 end else
557 if X3DType = 'MFString' then
558 begin
559 Names.Add('array of string');
560 Names.Add('TCastleStringList');
561 end else
562 if X3DType = 'MFMatrix3f' then
563 begin
564 Names.Add('array of TMatrix3');
565 Names.Add('TMatrix3List');
566 end else
567 if X3DType = 'MFMatrix4f' then
568 begin
569 Names.Add('array of TMatrix4');
570 Names.Add('TMatrix4List');
571 end else
572 if X3DType = 'MFMatrix3d' then
573 begin
574 Names.Add('array of TMatrix3Double');
575 Names.Add('TMatrix3DoubleList');
576 end else
577 if X3DType = 'MFMatrix4d' then
578 begin
579 Names.Add('array of TMatrix4Double');
580 Names.Add('TMatrix4DoubleList');
581 end;
582 end;
583
584 function TX3DFieldInformation.ConditionsBegin: String;
585 begin
586 if NotSlim then
587 Result := '{$ifndef CASTLE_SLIM_NODES}' + NL
588 else
589 Result := '';
590 end;
591
592 function TX3DFieldInformation.ConditionsEnd: String;
593 begin
594 if NotSlim then
595 Result := '{$endif not CASTLE_SLIM_NODES}' + NL
596 else
597 Result := '';
598 end;
599
600 function TX3DFieldInformation.IsEnumString: Boolean;
601 begin
602 { TODO: MFString is not yet detected as IsEnumString,
603 as our TMFStringEnum is not yet implemented. }
604 Result :=
605 ((X3DType = 'SFString') {or (X3DType = 'MFString')}) and
606 IsPrefix('["', Range, false) and
607 (IsSuffix('"]', Range, false) or
608 IsSuffix('...]', Range, false));
609 end;
610
611 procedure TX3DFieldInformation.Finished;
612 var
613 I: Integer;
614 AllowedChildrenNodesSplitted: TCastleStringList;
615 AllowedChildren: TX3DNodeInformation;
616 begin
617 if IsNode and
618 (AccessType in [atInitializeOnly, atInputOutput]) then
619 begin
620 { Although NULL is sensible only for SFNode and [] is sensible only
621 for MFNode, X3D specification switches them in many places
622 --- too many to fix them, it's easier to just ignore
623 the difference here. }
624 if (DefaultValue <> 'NULL') and
625 (DefaultValue <> '[]') then
626 raise EInvalidSpecificationFile.CreateFmt('Invalid default SFNode / MFNode value for "%s": %s', [
627 X3DName,
628 DefaultValue
629 ]);
630
631 if IsPrefix('[', Range, false) or
632 IsSuffix(']', Range, false) then
633 raise EInvalidSpecificationFile.Create('Do not surround SFNode / MFNode range in [...] anymore: ' + X3DName);
634
635 { in case of SFNode / MFNode, convert the Range into
636 NodeAllowedChildren }
637 AllowedChildrenNodesSplitted := CreateTokens(Range, WhiteSpaces + [',', '|']);
638 try
639 for I := 0 to AllowedChildrenNodesSplitted.Count - 1 do
640 begin
641 AllowedChildren := TX3DNodeInformation.Create;
642 AllowedChildren.X3DType := AllowedChildrenNodesSplitted[I];
643 AllowedChildrenNodes.Add(AllowedChildren);
644 end;
645 finally FreeAndNil(AllowedChildrenNodesSplitted) end;
646 end;
647 end;
648
649 function TX3DFieldInformation.MustBeNonnegative: Boolean;
650 begin
651 Result :=
652 (
653 (X3DType = 'SFFloat') or
654 (X3DType = 'SFLong') or
655 (X3DType = 'SFInt32')
656 ) and (
657 (Range = '(0,Inf)') or
658 (Range = '[0,Inf)') or
659 (Range = '(0,1)') or
660 (Range = '[0,1)') or
661 (Range = '(0,1]') or
662 (Range = '[0,1]')
663 );
664 end;
665
666 { TProcessor ----------------------------------------------------------------- }
667
668 procedure TProcessor.ProcessFile(const InputFileName: String);
669
670 { Parse field information.
671 Note that the Line parameters should receive the LineWithComment value.
672 Node contents are read-only in this routine. }
673 procedure ParseField(const Field: TX3DFieldInformation;
674 const Node: TX3DNodeInformation; const Line: String);
675 var
676 SeekPos, I: Integer;
677 begin
678 SeekPos := 1;
679
680 Field.X3DType := NextToken(Line, SeekPos, WhiteSpaces);
681 if Field.X3DType = '' then
682 raise EInvalidSpecificationFile.Create('Missing X3D field type on line: ' + Line);
683
684 Field.X3DAccessType := NextToken(Line, SeekPos, WhiteSpaces);
685 if Field.X3DAccessType = '' then
686 raise EInvalidSpecificationFile.Create('Missing X3D field access type on line: ' + Line);
687
688 Field.X3DName := NextToken(Line, SeekPos, WhiteSpaces);
689 if Field.X3DName = '' then
690 raise EInvalidSpecificationFile.Create('Missing X3D field name on line: ' + Line);
691
692 Field.PascalName := Field.X3DName;
693 { rename some field names to avoid collisions }
694 if Field.PascalName = 'on' then
695 Field.PascalName := 'IsOn'
696 else
697 if (Field.PascalName = 'type') and
698 ( (Node.X3DType = 'ShaderPart') or
699 (Node.X3DType = 'EffectPart') or
700 (Node.X3DType = 'ShaderProgram') ) then
701 Field.PascalName := 'ShaderType'
702 else
703 if Field.PascalName = 'name' then
704 Field.PascalName := 'NameField'
705 else
706 if Field.PascalName = 'string' then
707 Field.PascalName := 'Text' { standard Pascal name for such properties };
708 Field.PascalName[1] := UpCase(Field.PascalName[1]);
709
710 Field.PascalNamePrefixed := Field.X3DName;
711 Field.PascalNamePrefixed[1] := UpCase(Field.PascalNamePrefixed[1]);
712 if Field.AccessType in [atInputOnly, atOutputOnly] then
713 Field.PascalNamePrefixed := 'Event' + Field.PascalNamePrefixed
714 else
715 Field.PascalNamePrefixed := 'Fd' + Field.PascalNamePrefixed;
716
717 { Parsing field's default value is a bit tricky.
718 We can't just parse the field using parsing routines in TX3DField unit,
719 as we want to preserve expressions like "Pi/2" unevaluated.
720
721 For MF fields, it's enough to parse to matching "]" character.
722 For SF fields (or MF fields with single init value, i.e. not starting
723 with "["), the field type determines the token count for default value.
724
725 In case of problems, the unparsed field's value will be in Field.Comment. }
726
727 // skip whitespace, to make following test for '[' useful
728 while SCharIs(Line, SeekPos, WhiteSpaces) do
729 Inc(SeekPos);
730
731 if SCharIs(Line, SeekPos, '[') then
732 { parse from [ to matching ] }
733 begin
734 Field.DefaultValue := '[';
735 Inc(SeekPos);
736 while (SeekPos <= Length(Line)) and (Line[SeekPos] <> ']') do
737 begin
738 Field.DefaultValue += Line[SeekPos];
739 Inc(SeekPos);
740 end;
741 Field.DefaultValue += ']';
742 Inc(SeekPos);
743 end else
744 { parse from " to matching " }
745 if SCharIs(Line, SeekPos, '"') then
746 begin
747 Field.DefaultValue := '"';
748 Inc(SeekPos);
749 while (SeekPos <= Length(Line)) and (Line[SeekPos] <> '"') do
750 begin
751 Field.DefaultValue += Line[SeekPos];
752 Inc(SeekPos);
753 end;
754 Field.DefaultValue += '"';
755 Inc(SeekPos);
756 end else
757 if (Field.X3DType = 'SFVec2f') or
758 (Field.X3DType = 'MFVec2f') then
759 begin
760 Field.DefaultValue := 'Vector2(' + NextToken(Line, SeekPos, WhiteSpaces);
761 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces) + ')';
762 end else
763 if (Field.X3DType = 'SFVec2d') or
764 (Field.X3DType = 'MFVec2d') then
765 begin
766 Field.DefaultValue := 'Vector2Double(' + NextToken(Line, SeekPos, WhiteSpaces);
767 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces) + ')';
768 end else
769 if (Field.X3DType = 'SFColor') or
770 (Field.X3DType = 'SFVec3f') or
771 (Field.X3DType = 'MFColor') or
772 (Field.X3DType = 'MFVec3f') then
773 begin
774 Field.DefaultValue := 'Vector3(' + NextToken(Line, SeekPos, WhiteSpaces);
775 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
776 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces) + ')';
777 end else
778 if (Field.X3DType = 'SFVec3d') or
779 (Field.X3DType = 'MFVec3d') then
780 begin
781 Field.DefaultValue := 'Vector3Double(' + NextToken(Line, SeekPos, WhiteSpaces);
782 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
783 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces) + ')';
784 end else
785 if (Field.X3DType = 'SFColorRGBA') or
786 (Field.X3DType = 'SFVec4f') or
787 (Field.X3DType = 'MFColorRGBA') or
788 (Field.X3DType = 'MFVec4f') then
789 begin
790 Field.DefaultValue := 'Vector4(' + NextToken(Line, SeekPos, WhiteSpaces);
791 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
792 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
793 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces) + ')';
794 end else
795 if (Field.X3DType = 'SFVec4d') or
796 (Field.X3DType = 'MFVec4d') then
797 begin
798 Field.DefaultValue := 'Vector4Double(' + NextToken(Line, SeekPos, WhiteSpaces);
799 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
800 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
801 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces) + ')';
802 end else
803 if (Field.X3DType = 'SFMatrix3f') or
804 (Field.X3DType = 'SFMatrix3d') or
805 (Field.X3DType = 'MFMatrix3f') or
806 (Field.X3DType = 'MFMatrix3d') then
807 begin
808 Field.DefaultValue := 'Matrix3(';
809
810 if NextTokenOnce(Line, SeekPos, WhiteSpaces) = 'identity' then
811 begin
812 case Field.X3DType of
813 'SFMatrix3f': Field.DefaultValue := 'TMatrix3.Identity';
814 'SFMatrix3d': Field.DefaultValue := 'TMatrix3Double.Identity';
815 end;
816
817 // just to advance SeekPos
818 NextToken(Line, SeekPos, WhiteSpaces);
819 end else
820 begin
821 for I := 1 to 2 do
822 begin
823 Field.DefaultValue += ' Vector3(' + NextToken(Line, SeekPos, WhiteSpaces);
824 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
825 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces) + '),' + NL;
826 end;
827
828 Field.DefaultValue += ' Vector3(' + NextToken(Line, SeekPos, WhiteSpaces);
829 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
830 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces) + '));';
831 end;
832 end else
833 if (Field.X3DType = 'SFMatrix4f') or
834 (Field.X3DType = 'SFMatrix') or // VRML 1 matrix type
835 (Field.X3DType = 'SFMatrix4d') or
836 (Field.X3DType = 'MFMatrix4f') or
837 (Field.X3DType = 'MFMatrix4d') then
838 begin
839 Field.DefaultValue := 'Matrix4(';
840
841 if NextTokenOnce(Line, SeekPos, WhiteSpaces) = 'identity' then
842 begin
843 case Field.X3DType of
844 'SFMatrix4f': Field.DefaultValue := 'TMatrix4.Identity';
845 'SFMatrix': Field.DefaultValue := 'TMatrix4.Identity';
846 'SFMatrix4d': Field.DefaultValue := 'TMatrix4Double.Identity';
847 end;
848
849 // just to advance SeekPos
850 NextToken(Line, SeekPos, WhiteSpaces);
851 end else
852 begin
853 for I := 1 to 3 do
854 begin
855 Field.DefaultValue += ' Vector4(' + NextToken(Line, SeekPos, WhiteSpaces);
856 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
857 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
858 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces) + '),' + NL;
859 end;
860
861 Field.DefaultValue += ' Vector4(' + NextToken(Line, SeekPos, WhiteSpaces);
862 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
863 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
864 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces) + '));';
865 end;
866 end else
867 if (Field.X3DType = 'SFRotation') or
868 (Field.X3DType = 'MFRotation') then
869 begin
870 Field.DefaultValue := 'Vector3(' + NextToken(Line, SeekPos, WhiteSpaces);
871 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
872 Field.DefaultValue += ', ' + NextToken(Line, SeekPos, WhiteSpaces);
873 Field.DefaultValue += '), ' + NextToken(Line, SeekPos, WhiteSpaces);
874 end else
875 begin
876 Field.DefaultValue := NextToken(Line, SeekPos, WhiteSpaces);
877 end;
878
879 StringReplaceAllVar(Field.DefaultValue, '"', '''', false);
880 { make booleans lowecase, I like them more lowercase }
881 StringReplaceAllVar(Field.DefaultValue, 'FALSE', 'false', false);
882 StringReplaceAllVar(Field.DefaultValue, 'TRUE', 'true', false);
883
884 Field.Comment := Trim(SEnding(Line, SeekPos));
885
886 // cut off initial '# ' from Field.Comment
887 if SCharIs(Field.Comment, 1, '#') then
888 Field.Comment := Trim(SEnding(Field.Comment, 2));
889
890 if Field.Comment <> '' then
891 raise EInvalidSpecificationFile.CreateFmt('Comments at field lines are not allowed, as they usually indicate you should split this into separate lines for change,not-slim etc.: %s.%s', [
892 Node.X3DType,
893 Field.X3DName
894 ]);
895 end;
896
897 { Generate output and free given Node. }
898 procedure FinishPreviousNode(var Node: TX3DNodeInformation);
899 var
900 Field: TX3DFieldInformation;
901 begin
902 if Node <> nil then
903 begin
904 NodeBegin(Node);
905 for Field in Node.Fields do
906 begin
907 Field.Finished;
908 NodeField(Node, Field);
909 end;
910 NodeEnd(Node);
911 end;
912 FreeAndNil(Node);
913 end;
914
915 var
916 Node: TX3DNodeInformation;
917 LastField: TX3DFieldInformation; // last field processed (in the current Node)
918
919 procedure ValidatePerNodeCommand(const Command: String);
920 begin
921 if Node = nil then
922 raise EInvalidSpecificationFile.CreateFmt('"%s" can only be used within a node', [Command]);
923 if LastField <> nil then
924 raise EInvalidSpecificationFile.CreateFmt('"%s" can only be used before any fields are declared', [Command]);
925 end;
926
927 procedure ValidatePerFieldCommand(const Command: String);
928 begin
929 if Node = nil then
930 raise EInvalidSpecificationFile.CreateFmt('"%s" can only be used within a node', [Command]);
931 if LastField = nil then
932 raise EInvalidSpecificationFile.CreateFmt('"%s" can only be used when a field was declared earlier', [Command]);
933 end;
934
935 { Simple approach to read multiline string from text file.
936 Assumes that it must end with a line with only """ (optionally surrounded by whitespaces,
937 but nothing more, not even comments). }
938 function ReadMultilineString(const F: TTextReader): String;
939 var
940 S: String;
941 begin
942 Result := '';
943 repeat
944 if F.Eof then
945 raise EInvalidSpecificationFile.Create('Unexpected end of file in the middle of multiline string (""")');
946 S := F.Readln;
947 if Trim(S) = '"""' then
948 Exit;
949 { Note: do not Trim(S) below, to not break indent in @longCode
950 used within X3D fields docs, like BlendingSort,
951 file:///home/michalis/sources/castle-engine/castle-engine/doc/reference/X3DNodes.TNavigationInfoNode.html#BlendingSort }
952 Result := Result + S + NL;
953 until false;
954 end;
955
956 var
957 F: TTextReader;
958 PosComment, I: Integer;
959 Tokens: TCastleStringList;
960 Line, LineWithComment: String;
961 Ancestor: TX3DNodeInformation;
962 begin
963 Node := nil;
964
965 WritelnVerbose('Processing ' + InputFileName);
966 F := TTextReader.Create(InputFileName);
967 try
968 while not F.Eof do
969 begin
970 LineWithComment := F.Readln;
971 Line := LineWithComment;
972 { remove comments }
973 PosComment := Pos('#', Line);
974 if PosComment <> 0 then
975 SetLength(Line, PosComment - 1);
976 { avoid empty lines (after comment removal) }
977 if Trim(Line) <> '' then
978 begin
979 Tokens := CreateTokens(Line);
980 try
981 { node start }
982 if (Tokens.Count >= 2) and
983 (Tokens[Tokens.Count - 1] = '{') and
984 ((Tokens.Count = 2) or (Tokens[1] = ':')) then
985 begin
986 FinishPreviousNode(Node);
987 Node := TX3DNodeInformation.Create;
988 Node.X3DType := Tokens[0];
989 for I := 2 to Tokens.Count - 2 do
990 begin
991 Ancestor := TX3DNodeInformation.Create;
992 Ancestor.X3DType := SuffixRemove(',', Tokens[I], true);
993 Node.Ancestors.Add(Ancestor);
994 end;
995 LastField := nil; // reset at node start
996 end else
997
998 { node end }
999 if (Tokens.Count = 1) and
1000 (Tokens[0] = '}') then
1001 begin
1002 FinishPreviousNode(Node);
1003 end else
1004
1005 { per-node commands }
1006 if (Tokens.Count = 2) and
1007 (Tokens[0] = 'default-container-field:') then
1008 begin
1009 ValidatePerNodeCommand('default-container-field');
1010 Node.DefaultContainerField := Tokens[1];
1011 end else
1012
1013 if (Tokens.Count = 1) and
1014 (Tokens[0] = 'vrml1') then
1015 begin
1016 ValidatePerNodeCommand('vrml1');
1017 Node.Vrml1 := true;
1018 end else
1019
1020 { per-field commands }
1021 if (Tokens.Count = 1) and
1022 (Tokens[0] = 'not-slim') then
1023 begin
1024 ValidatePerFieldCommand('not-slim');
1025 LastField.NotSlim := true;
1026 end else
1027
1028 if (Tokens.Count = 1) and
1029 (Tokens[0] = 'angle') then
1030 begin
1031 ValidatePerFieldCommand('angle');
1032 LastField.Angle := true;
1033 end else
1034
1035 if (Tokens.Count = 1) and
1036 (Tokens[0] = 'weak-link') then
1037 begin
1038 ValidatePerFieldCommand('weak-link');
1039 LastField.WeakLink := true;
1040 end else
1041
1042 if (Tokens.Count = 2) and
1043 (Tokens[0] = 'change:') then
1044 begin
1045 ValidatePerFieldCommand('change');
1046 LastField.ChangeAlways := Tokens[1];
1047 end else
1048
1049 if Tokens[0] = 'range:' then
1050 begin
1051 ValidatePerFieldCommand('range');
1052 LastField.Range := Trim(PrefixRemove('range:', Trim(Line), false));
1053 end else
1054
1055 if (Tokens.Count = 4) and
1056 (Tokens[0] = 'enumerated-type:') then
1057 begin
1058 ValidatePerFieldCommand('enumerated-type');
1059 if not LastField.IsEnumString then
1060 raise EInvalidSpecificationFile.CreateFmt('enumerated-type only available for SFString / MFString fields detected as enumerated, but used with %s', [
1061 LastField.X3DName
1062 ]);
1063 LastField.EnumType := Tokens[1];
1064 LastField.EnumNames := Tokens[2];
1065 LastField.EnumDefault := Tokens[3];
1066 end else
1067
1068 if (Tokens.Count = 2) and
1069 (Tokens[0] = 'enumerated-type-vrml1:') then
1070 begin
1071 ValidatePerFieldCommand('enumerated-type-vrml1');
1072 if LastField.X3DType <> 'SFEnum' then
1073 raise EInvalidSpecificationFile.CreateFmt('enumerated-type-vrml1 only available for SFEnum fields, but used with %s', [
1074 LastField.X3DName
1075 ]);
1076 LastField.Vrml1EnumNames := Tokens[1];
1077 end else
1078
1079 if Tokens[0] = 'bit-mask-vrml1:' then
1080 begin
1081 ValidatePerFieldCommand('bit-mask-vrml1');
1082 if LastField.X3DType <> 'SFBitMask' then
1083 raise EInvalidSpecificationFile.CreateFmt('bit-mask-vrml1 only available for SFBitMask fields, but used with %s', [
1084 LastField.X3DName
1085 ]);
1086 LastField.Vrml1BitMaskNames := Trim(PrefixRemove('bit-mask-vrml1:', Trim(Line), false));
1087 end else
1088
1089 if Tokens[0] = 'doc:' then
1090 begin
1091 ValidatePerFieldCommand('doc');
1092 LastField.Documentation := Trim(PrefixRemove('doc:', Trim(Line), false));
1093 if LastField.Documentation = '"""' then
1094 LastField.Documentation := ReadMultilineString(F);
1095 end else
1096
1097 if Tokens[0] = 'setter-before:' then
1098 begin
1099 ValidatePerFieldCommand('setter-before');
1100 LastField.SetterBefore := Trim(PrefixRemove('setter-before:', Trim(Line), false));
1101 end else
1102
1103 { field/event inside node }
1104 begin
1105 if Node = nil then
1106 raise EInvalidSpecificationFile.Create('Found line that declares a field/event (as it was not recognized as anything else) but we are not inside any node ' + LineWithComment);
1107 LastField := TX3DFieldInformation.Create;
1108 Node.Fields.Add(LastField);
1109 ParseField(LastField, Node, LineWithComment);
1110 end;
1111 finally FreeAndNil(Tokens) end;
1112 end;
1113 end;
1114 finally FreeAndNil(F) end;
1115
1116 FreeAndNil(Node);
1117
1118 ComponentEnd(DeleteFileExt(ExtractFileName(InputFileName)));
1119 end;
1120
1121 procedure TProcessor.NodeBegin(const Node: TX3DNodeInformation);
1122 begin
1123 end;
1124
1125 procedure TProcessor.NodeField(const Node: TX3DNodeInformation;
1126 const Field: TX3DFieldInformation);
1127 begin
1128 end;
1129
1130 procedure TProcessor.NodeEnd(const Node: TX3DNodeInformation);
1131 begin
1132 end;
1133
1134 procedure TProcessor.ComponentEnd(const ComponentName: String);
1135 begin
1136 end;
1137
1138 { THelperProcessor ----------------------------------------------------------- }
1139
1140 procedure THelperProcessor.NodeBegin(const Node: TX3DNodeInformation);
1141 begin
1142 OutputPublicInterface +=
1143 { CreateNode needs some doc, to avoid using template
1144 "Automatically generated node properties."
1145 as accidental documentation comment. }
1146 ' { Create node fields and events. }' + NL +
1147 ' procedure CreateNode; override;' + NL +
1148 ' class function ClassX3DType: String; override;' + NL;
1149
1150 if Node.Vrml1 then
1151 begin
1152 OutputPublicInterface +=
1153 ' class function ForVRMLVersion(const Version: TX3DVersion): Boolean; override;' + NL;
1154 OutputCreateImplementation +=
1155 'class function ' + Node.PascalType + '.ForVRMLVersion(const Version: TX3DVersion): Boolean;' + NL +
1156 'begin' + NL +
1157 ' Result := Version.Major <= 1; // parse this node only in VRML 1.0 and Inventor files' + NL +
1158 'end;' + NL +
1159 NL;
1160 end;
1161
1162 OutputCreateImplementation +=
1163 'class function ' + Node.PascalType + '.ClassX3DType: String;' + NL +
1164 'begin' + NL +
1165 ' Result := ''' + Node.X3DType + ''';' + NL +
1166 'end;' + NL +
1167 NL;
1168
1169 OutputCreateImplementation +=
1170 'procedure ' + Node.PascalType + '.CreateNode;' + NL +
1171 'begin' + NL +
1172 ' inherited;' + NL;
1173 end;
1174
1175 procedure THelperProcessor.NodeField(const Node: TX3DNodeInformation;
1176 const Field: TX3DFieldInformation);
1177 var
1178 AllowedPascalClass, SetterType: String;
1179 SetterTypes: TCastleStringList;
1180 begin
1181 AddAutoGeneratedField(Node, Field, OutputPublicInterface, OutputCreateImplementation);
1182
1183 if Field.IsEnumString and (Field.EnumType = '') then
1184 Exit;
1185 if (Field.X3DName = 'solid') or
1186 (Field.X3DName = 'linetype') or
1187 (Field.X3DName = 'bboxSize') or // this is accounted already by writing out bboxCenter as public BBox
1188
1189 (Node.X3DType + '.' + Field.X3DName = 'Viewpoint.position') or
1190 (Node.X3DType + '.' + Field.X3DName = 'OrthoViewpoint.position') or
1191 (Node.X3DType + '.' + Field.X3DName = 'GeoViewpoint.position') or
1192 (Node.X3DType + '.' + Field.X3DName = 'TextureProperties.magnificationFilter') or
1193 (Node.X3DType + '.' + Field.X3DName = 'TextureProperties.minificationFilter') or
1194 (Node.X3DType + '.' + Field.X3DName = 'HAnimHumanoid.skinCoord') or
1195 (Node.X3DType + '.' + Field.X3DName = 'FontStyle.family') or // in both VRML = 1 and >= 2, these have special helpers
1196 (Node.Vrml1 and (Node.X3DType + '.' + Field.X3DName = 'FontStyle.style')) or // in VRML = 1, this has special helpers
1197 ((not Node.Vrml1) and (Node.X3DType + '.' + Field.X3DName = 'FontStyle.justify')) or // in VRML >= 2, this has special helpers
1198 // ScreenFontStyle does not have better helpers yet, but it will, just like FontStyle
1199 (Node.X3DType + '.' + Field.X3DName = 'ScreenFontStyle.family') or
1200 (Node.X3DType + '.' + Field.X3DName = 'ScreenFontStyle.justify') or
1201 // These are deprecated, and a bit confusing (these are arrays that should have 0 or 1 items, to override "orientation")
1202 (Node.X3DType + '.' + Field.X3DName = 'X3DViewpointNode.direction') or
1203 (Node.X3DType + '.' + Field.X3DName = 'X3DViewpointNode.up') or
1204
1205 false // keep this line, to allow easily rearranging lines above
1206 then
1207 begin
1208 WritelnVerbose('Not processing, this field has special implementation: ' + Node.X3DType + '.' + Field.X3DName);
1209 Exit;
1210 end;
1211 if (Node.X3DType = 'X3DFogObject') or
1212 (Node.X3DType = 'X3DPickableObject') or
1213 (Node.X3DType = 'LOD') then
1214 begin
1215 WritelnVerbose('Not processing, this entire node has special implementation: ' + Node.X3DType);
1216 Exit;
1217 end;
1218 if (Field.X3DAccessType <> '[in,out]') and
1219 (Field.X3DAccessType <> '[]') then
1220 begin
1221 { We don't generate any special helpers for events now, and they don't seem necessary. }
1222 // too verbose, and this is normal -- not an alarming situation
1223 //WritelnVerbose('Getters / setters are only generated for fields (inputOutput or initializeOnly) now, omitting event: ' + Field.X3DName);
1224 Exit;
1225 end;
1226
1227 if Field.IsNode then
1228 begin
1229 if Field.AllowedChildrenNodes.Count = 0 then
1230 raise Exception.CreateFmt('SFNode field %s.%s has nothing allowed', [
1231 Node.X3DType,
1232 Field.X3DName
1233 ]);
1234 { TODO: Allow to get/set when IsFunctionality too? }
1235 if not Field.AllowedChildrenNodes[0].IsFunctionality then
1236 begin
1237 if Field.AllowedChildrenNodes.Count = 1 then
1238 AllowedPascalClass := Field.AllowedChildrenNodes[0].PascalType
1239 else
1240 AllowedPascalClass := 'TX3DNode';
1241 if Field.X3DType = 'SFNode' then
1242 begin
1243 OutputPrivateInterface +=
1244 Field.ConditionsBegin +
1245 ' function Get' + Field.PascalName + ': ' + AllowedPascalClass + ';' + NL +
1246 ' procedure Set' + Field.PascalName + '(const Value: ' + AllowedPascalClass + ');' + NL +
1247 Field.ConditionsEnd;
1248 OutputPublicInterface +=
1249 Field.ConditionsBegin +
1250 ' { ' + DocToPascal(Field.Documentation) + ' }' + NL +
1251 ' property ' + Field.PascalName + ': ' + AllowedPascalClass + ' read Get' + Field.PascalName + ' write Set' + Field.PascalName + ';' + NL +
1252 Field.ConditionsEnd;
1253 OutputImplementation +=
1254 Field.ConditionsBegin +
1255 'function ' + Node.PascalType + '.Get' + Field.PascalName + ': ' + AllowedPascalClass + ';' + NL +
1256 'begin' + NL +
1257 ' if ' + Field.PascalNamePrefixed + '.Value is ' + AllowedPascalClass + ' then' + NL +
1258 ' Result := ' + AllowedPascalClass + '(' + Field.PascalNamePrefixed + '.Value)' + NL +
1259 ' else' + NL +
1260 ' Result := nil;' + NL +
1261 'end;' + NL +
1262 NL +
1263 'procedure ' + Node.PascalType + '.Set' + Field.PascalName + '(const Value: ' + AllowedPascalClass + ');' + NL +
1264 'begin' + NL +
1265 ' ' + Field.PascalNamePrefixed + '.Send(Value);' + NL +
1266 'end;' + NL +
1267 NL +
1268 Field.ConditionsEnd;
1269 end else
1270 if Field.X3DType = 'MFNode' then
1271 begin
1272 OutputPublicInterface +=
1273 Field.ConditionsBegin +
1274 ' { ' + DocToPascal(Field.Documentation) + ' }' + NL +
1275 ' procedure Set' + Field.PascalName + '(const Value: array of ' + AllowedPascalClass + ');' + NL +
1276 Field.ConditionsEnd;
1277 OutputImplementation +=
1278 Field.ConditionsBegin +
1279 'procedure ' + Node.PascalType + '.Set' + Field.PascalName + '(const Value: array of ' + AllowedPascalClass + ');' + NL +
1280 'var' + NL +
1281 ' L: Integer;' + NL +
1282 ' A: array of TX3DNode;' + NL +
1283 'begin' + NL +
1284 Iff(Field.SetterBefore <> '', ' ' + Field.SetterBefore + '(Value);' + NL, '') +
1285 ' L := High(Value) + 1;' + NL +
1286 ' SetLength(A, L);' + NL +
1287 ' if L > 0 then' + NL +
1288 ' Move(Value[0], A[0], L * SizeOf(' + AllowedPascalClass + '));' + NL +
1289 ' ' + Field.PascalNamePrefixed + '.Send(A);' + NL +
1290 'end;' + NL +
1291 NL +
1292 Field.ConditionsEnd;
1293 end;
1294 end;
1295 end else
1296 if Field.X3DName = 'bboxCenter' then
1297 begin
1298 { Define BBox property that gets/sets both bboxCenter and bboxSize at the same time }
1299 OutputPrivateInterface +=
1300 Field.ConditionsBegin +
1301 ' function GetBBox: TBox3D;' + NL +
1302 ' procedure SetBBox(const Value: TBox3D);' + NL +
1303 Field.ConditionsEnd;
1304 OutputPublicInterface +=
1305 Field.ConditionsBegin +
1306 ' { ' + DocToPascal(Field.Documentation) + ' }' + NL +
1307 ' property BBox: TBox3D read GetBBox write SetBBox;' + NL +
1308 Field.ConditionsEnd;
1309 OutputImplementation +=
1310 Field.ConditionsBegin +
1311 'function ' + Node.PascalType + '.GetBBox: TBox3D;' + NL +
1312 'begin' + NL +
1313 ' Result := TBox3D.FromCenterSize(FdBBoxCenter.Value, FdBBoxSize.Value);' + NL +
1314 'end;' + NL +
1315 NL +
1316 'procedure ' + Node.PascalType + '.SetBBox(const Value: TBox3D);' + NL +
1317 'var' + NL +
1318 ' ValueCenter, ValueSize: TVector3;' + NL +
1319 'begin' + NL +
1320 Iff(Field.SetterBefore <> '', ' ' + Field.SetterBefore + '(Value);' + NL, '') +
1321 ' Value.ToCenterSize(ValueCenter, ValueSize);' + NL +
1322 ' FdBBoxCenter.Send(ValueCenter);' + NL +
1323 ' FdBBoxSize.Send(ValueSize);' + NL +
1324 'end;' + NL +
1325 NL +
1326 Field.ConditionsEnd;
1327 end else
1328 begin
1329 if Field.PascalHelperType <> '' then
1330 begin
1331 OutputPrivateInterface +=
1332 Field.ConditionsBegin +
1333 ' function Get' + Field.PascalName + ': ' + Field.PascalHelperType + ';' + NL +
1334 ' procedure Set' + Field.PascalName + '(const Value: ' + Field.PascalHelperType + ');' + NL +
1335 Field.ConditionsEnd;
1336 OutputPublicInterface +=
1337 Field.ConditionsBegin +
1338 ' { ' + DocToPascal(Field.Documentation) + ' }' + NL +
1339 ' property ' + Field.PascalName + ': ' + Field.PascalHelperType + ' read Get' + Field.PascalName + ' write Set' + Field.PascalName + ';' + NL +
1340 Field.ConditionsEnd;
1341 OutputImplementation +=
1342 Field.ConditionsBegin +
1343 'function ' + Node.PascalType + '.Get' + Field.PascalName + ': ' + Field.PascalHelperType + ';' + NL +
1344 'begin' + NL +
1345 Iff(Field.IsEnumString,
1346 ' Result := ' + Field.EnumType + '(' + Field.PascalNamePrefixed + '.EnumValue);',
1347 ' Result := ' + Field.PascalNamePrefixed + '.Value;') + NL +
1348 'end;' + NL +
1349 NL +
1350 'procedure ' + Node.PascalType + '.Set' + Field.PascalName + '(const Value: ' + Field.PascalHelperType + ');' + NL +
1351 'begin' + NL +
1352 Iff(Field.SetterBefore <> '', ' ' + Field.SetterBefore + '(Value);' + NL, '') +
1353 Iff(Field.IsEnumString,
1354 ' ' + Field.PascalNamePrefixed + '.SendEnumValue(Ord(Value));',
1355 ' ' + Field.PascalNamePrefixed + '.Send(Value);') + NL +
1356 'end;' + NL +
1357 NL +
1358 Field.ConditionsEnd;
1359 end;
1360
1361 SetterTypes := TCastleStringList.Create;
1362 try
1363 Field.PascalSetterTypes(SetterTypes);
1364 for SetterType in SetterTypes do
1365 begin
1366 OutputPublicInterface +=
1367 Field.ConditionsBegin +
1368 ' { ' + DocToPascal(Field.Documentation) + ' }' + NL +
1369 ' procedure Set' + Field.PascalName + '(const Value: ' + SetterType + ');' +
1370 Iff(SetterTypes.Count > 1,' overload;', '') + NL + // Delphi need overload when we have more than one setter wit hte same name
1371 Field.ConditionsEnd;
1372 OutputImplementation +=
1373 Field.ConditionsBegin +
1374 'procedure ' + Node.PascalType + '.Set' + Field.PascalName + '(const Value: ' + SetterType + ');' + NL +
1375 'begin' + NL +
1376 Iff(Field.SetterBefore <> '', ' ' + Field.SetterBefore + '(Value);' + NL, '') +
1377 ' ' + Field.PascalNamePrefixed + '.Send(Value);' + NL +
1378 'end;' + NL +
1379 NL +
1380 Field.ConditionsEnd;
1381 end;
1382 finally FreeAndNil(SetterTypes) end;
1383 end;
1384 end;
1385
1386 function CopyrightYears: String;
1387 const
1388 YearBegin = '2015-';
1389 var
1390 BuildDate: TDateTime;
1391 SourceDateEpoch: String;
1392 begin
1393 { Look at SOURCE_DATE_EPOCH to support reproducible builds,
1394 https://wiki.debian.org/ReproducibleBuilds/TimestampsProposal
1395 https://reproducible-builds.org/specs/source-date-epoch/ }
1396 SourceDateEpoch := GetEnvironmentVariable('SOURCE_DATE_EPOCH');
1397 if SourceDateEpoch = '' then
1398 BuildDate := Now
1399 else
1400 BuildDate := UnixToDateTime(StrToInt(SourceDateEpoch));
1401 Result := YearBegin + IntToStr(YearOf(BuildDate));
1402 end;
1403
1404 procedure THelperProcessor.NodeEnd(const Node: TX3DNodeInformation);
1405
1406 procedure GenerateOutput(const OutputInterface, OutputImplementation: String);
1407 var
1408 OutputFileName: String;
1409 begin
1410 OutputFileName := InclPathDelim(OutputPath) +
1411 'x3dnodes_' + LowerCase(Node.X3DType) +
1412 Iff(Node.Vrml1, '_1', '') + '.inc';
1413
1414 StringToFile(OutputFileName,
1415 '{ -*- buffer-read-only: t -*-' + NL +
1416 '' + NL +
1417 ' Copyright ' + CopyrightYears + ' Michalis Kamburelis.' + NL +
1418 '' + NL +
1419 ' This file is part of "Castle Game Engine".' + NL +
1420 '' + NL +
1421 ' "Castle Game Engine" is free software; see the file COPYING.txt,' + NL +
1422 ' included in this distribution, for details about the copyright.' + NL +
1423 '' + NL +
1424 ' "Castle Game Engine" is distributed in the hope that it will be useful,' + NL +
1425 ' but WITHOUT ANY WARRANTY; without even the implied warranty of' + NL +
1426 ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.' + NL +
1427 '' + NL +
1428 ' ----------------------------------------------------------------------------' + NL +
1429 '}' + NL +
1430 '' + NL +
1431 '{ Automatically generated node properties.' + NL +
1432 '' + NL +
1433 ' Do not edit this file manually!' + NL +
1434 ' To add new properties:' + NL +
1435 ' - add them to the text files in tools/internal/x3d-nodes-to-pascal/nodes-specification/ ,' + NL +
1436 ' - and regenerate include files by running x3d-nodes-to-pascal }' + NL +
1437 '' + NL +
1438 '{$ifdef read_interface}' + NL +
1439 NL +
1440 // 'type' + NL +
1441 OutputInterface +
1442 '{$endif read_interface}' + NL +
1443 '' + NL +
1444 '{$ifdef read_implementation}' + NL +
1445 NL +
1446 OutputImplementation +
1447 '{$endif read_implementation}' + NL
1448 );
1449 end;
1450
1451 begin
1452 if Node.DefaultContainerField <> '' then
1453 OutputCreateImplementation +=
1454 NL +
1455 ' DefaultContainerField := ''' + Node.DefaultContainerField + ''';' + NL;
1456 OutputCreateImplementation +=
1457 'end;' + NL +
1458 NL;
1459 OutputImplementation += OutputCreateImplementation;
1460
1461 if (OutputPrivateInterface = '') or
1462 (OutputPublicInterface = '') or
1463 (OutputImplementation = '') then
1464 begin
1465 // too verbose, and this is normal -- not an alarming situation
1466 // WritelnVerbose('Node does not have any helpers (for now), generating empty include file: ' + Node.X3DType);
1467 end;
1468
1469 if OutputPrivateInterface <> '' then
1470 OutputPrivateInterface := ' strict private' + NL + OutputPrivateInterface;
1471 if OutputPublicInterface <> '' then
1472 OutputPublicInterface := ' public' + NL + OutputPublicInterface;
1473
1474 // no helpers for functionalities, for now
1475 if not Node.IsFunctionality then
1476 GenerateOutput(
1477 // ' ' + Node.PascalType + 'Helper = class helper for ' + Node.PascalType + NL +
1478 OutputPrivateInterface +
1479 OutputPublicInterface +
1480 // ' end;' + NL +
1481 NL,
1482 '{ ' + Node.PascalType + ' ----------------------------------------------- }' + NL +
1483 NL +
1484 OutputImplementation);
1485
1486 OutputPrivateInterface := '';
1487 OutputPublicInterface := '';
1488 OutputImplementation := '';
1489 OutputCreateImplementation := '';
1490 end;
1491
1492 end.