Debian Codegraph @

src/scene/x3d/x3dnodes_load.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-2022 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 {$ifdef read_interface}
18
19 { Container tracking VRML/X3D node and prototype names during parsing.
20 Used by both classic and XML VRML/X3D readers. }
21 TX3DReaderNames = class(TX3DReader)
22 strict private
23 FNodes: TX3DNodeNames;
24 FPrototypes: TX3DPrototypeNames;
25 FImported: TX3DNodeNames;
26 FExported: TX3DExportList;
27 FImportable: TX3DImportableNames;
28 procedure CommonCreate(const AAutoRemoveNodes: boolean);
29 public
30 constructor Create(const AAutoRemoveNodes: boolean;
31 const ABaseUrl: String;
32 const AVersion: TX3DVersion);
33 constructor CreateCopy(const AAutoRemoveNodes: boolean;
34 Source: TX3DReader);
35 destructor Destroy; override;
36
37 { Extract names, before destructing this object.
38 This method can be used only right before calling the destructor.
39 It copies the prototype, exported, imported names list (names visible
40 from the outside), and sets them to @nil (to avoid releasing them
41 at destruction). }
42 procedure ExtractNames(out APrototypes: TX3DPrototypeNames;
43 out AExported: TX3DExportList;
44 out AImported: TX3DNodeNames);
45
46 { Current namespace for DEF/USE.
47
48 This is a list without duplicates with all
49 currently known node names. Objects[] of this list point to
50 actual TX3DNode instances. If many instances had the same NodeName,
51 only the last instance will be referenced here, following VRML spec
52 (last DEF takes precedence).
53
54 Internal notes: ParseNode doesn't modify this, only TX3DNode.Parse
55 can do this. }
56 property Nodes: TX3DNodeNames read FNodes;
57
58 { Current namespace of PROTO names. }
59 property Prototypes: TX3DPrototypeNames read FPrototypes;
60
61 { Currently IMPORTed nodes.
62
63 The nodes on this list are "bound" to their aliases,
64 as this is the name under which they are visible in the current namespace.
65 Alias is the identifier after the "AS" keyword in the "IMPORT" declaration
66 (or, if no "AS xxx" clause was present, then alias is just the name
67 under which node was exported). }
68 property Imported: TX3DNodeNames read FImported;
69
70 { Currently EXPORTed nodes from this scene. }
71 property Exported: TX3DExportList read FExported;
72
73 { Currently loaded Inlines with importable nodes.
74
75 The mechanism is that when you load an Inline node, the resulting
76 "Exported" nodes (from the namespace within the Inline) get added
77 to this "Importable" list. Then the "IMPORT" clause in this
78 namespace can make "Importable" nodes into actually "Imported".
79
80 This is a list with strings representing Inline node names
81 (there's no way to IMPORT from unnamed Inline nodes).
82 Objects[] of this list are instances of TX3DNodeNames
83 corresponding to exported names within the inline. }
84 property Importable: TX3DImportableNames read FImportable;
85
86 { Set E.ExportedNode based on ExportedNodeName,
87 and modify @link(Exported) list to handle the export. }
88 procedure DoExport(const E: TX3DExport; const ExportedNodeName: String);
89 { modify @link(Imported) list to handle the export. }
90 procedure DoImport(const I: TX3DImport);
91
92 { Find node bound to given name. Looks in @link(Nodes) and @link(Imported).
93 Returns @nil if none. }
94 function Bound(const Name: string; out NodeFinished: Boolean;
95 const WarningIfImported: Boolean): TX3DNode;
96 end;
97
98 {$endif}
99
100 {$ifdef read_implementation}
101
102 { TX3DReaderNames ----------------------------------------------------------------- }
103
104 constructor TX3DReaderNames.Create(const AAutoRemoveNodes: boolean;
105 const ABaseUrl: String; const AVersion: TX3DVersion);
106 begin
107 inherited Create(ABaseUrl, AVersion);
108 CommonCreate(AAutoRemoveNodes);
109 end;
110
111 constructor TX3DReaderNames.CreateCopy(const AAutoRemoveNodes: boolean;
112 Source: TX3DReader);
113 begin
114 inherited CreateCopy(Source);
115 CommonCreate(AAutoRemoveNodes);
116 end;
117
118 procedure TX3DReaderNames.CommonCreate(const AAutoRemoveNodes: boolean);
119 begin
120 FNodes := TX3DNodeNames.Create(AAutoRemoveNodes);
121 FPrototypes := TX3DPrototypeNames.Create;
122 FImported := TX3DNodeNames.Create(AAutoRemoveNodes);
123 FExported := TX3DExportList.Create(false); // do not own TX3DExport instances, they are owned by TX3DNode.Exports
124 FImportable := TX3DImportableNames.Create;
125 end;
126
127 destructor TX3DReaderNames.Destroy;
128 begin
129 FreeAndNil(FNodes);
130 FreeAndNil(FPrototypes);
131 FreeAndNil(FImported);
132 FreeAndNil(FExported);
133 FreeAndNil(FImportable);
134 inherited;
135 end;
136
137 procedure TX3DReaderNames.ExtractNames(out APrototypes: TX3DPrototypeNames;
138 out AExported: TX3DExportList;
139 out AImported: TX3DNodeNames);
140 begin
141 APrototypes := FPrototypes;
142 AExported := FExported;
143 AImported := FImported;
144
145 FPrototypes := nil;
146 FExported := nil;
147 FImported := nil;
148 end;
149
150 procedure TX3DReaderNames.DoExport(const E: TX3DExport; const ExportedNodeName: String);
151 var
152 IgnoreNodeFinished: boolean;
153 begin
154 E.ExportedNode := Bound(ExportedNodeName, IgnoreNodeFinished, true);
155 if E.ExportedNode = nil then
156 begin
157 WritelnWarning('VRML/X3D', Format('Exported node name "%s" not found', [ExportedNodeName]));
158 Exit;
159 end;
160
161 Exported.Add(E);
162 end;
163
164 procedure TX3DReaderNames.DoImport(const I: TX3DImport);
165 var
166 ImportedNames: TX3DNodeNames;
167 ImportedNamesIndex: Integer;
168 ImportedNode: TX3DNode;
169 IgnoreNodeFinished: boolean;
170 begin
171 ImportedNamesIndex := Importable.IndexOf(I.InlineNodeName);
172 if ImportedNamesIndex = -1 then
173 begin
174 WritelnWarning('VRML/X3D', Format('Inline node name "%s" not found (or nothing was EXPORTed from it), cannot IMPORT', [I.InlineNodeName]));
175 Exit;
176 end;
177
178 ImportedNames := Importable.Objects[ImportedNamesIndex] as TX3DNodeNames;
179
180 ImportedNode := ImportedNames.Bound(I.ImportedNodeName, IgnoreNodeFinished);
181 if ImportedNode = nil then
182 begin
183 WritelnWarning('VRML/X3D', Format('Imported node name "%s" not found in inline "%s"', [I.ImportedNodeName, I.InlineNodeName]));
184 Exit;
185 end;
186
187 Imported.Bind(ImportedNode, true, I.ImportedNodeAlias);
188 end;
189
190 function TX3DReaderNames.Bound(const Name: string; out NodeFinished: Boolean;
191 const WarningIfImported: Boolean): TX3DNode;
192 begin
193 Result := Nodes.Bound(Name, NodeFinished);
194 if Result = nil then
195 begin
196 Result := Imported.Bound(Name, NodeFinished);
197 if WarningIfImported and (Result <> nil) then
198 WritelnLog('Reusing imported node "%s". This is not allowed by the X3D specification, but is supported by Castle Game Engine', [
199 Name
200 ]);
201 end;
202 end;
203
204 {$endif}