Debian Codegraph @

src/scene/x3d/x3dnodes_importexport.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-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 {$ifdef read_interface}
18
19 TX3DImport = class(TX3DFileItem)
20 public
21 InlineNodeName, ImportedNodeName, ImportedNodeAlias: string;
22
23 procedure Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames);
24
25 { Parse the IMPORT declaration (XML encoding).
26 Given Element here must have TagName = 'IMPORT'. }
27 procedure ParseXML(Element: TDOMElement; Reader: TX3DReaderNames);
28
29 procedure SaveToStream(Writer: TX3DWriter); override;
30 function DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DImport;
31 end;
32
33 TX3DExport = class(TX3DFileItem)
34 strict private
35 FExportedNode: TX3DNode;
36 procedure DestructionNotification(const Node: TX3DNode);
37 procedure SetExportedNode(const Value: TX3DNode);
38 public
39 { Name under which the EXPORTed node is available in outer files,
40 i.e. IMPORT statements should refer to this alias.
41 If empty, just use ExportedNode.X3DName. }
42 ExportedAlias: String;
43
44 destructor Destroy; override;
45
46 { Exported node.
47 Note that we store node reference, not just ExportedNodeName,
48 this way renaming the ExportedNode (e.g. by RenameUniquely during saving)
49 is properly reflected in wrtitten EXPORT statement. }
50 property ExportedNode: TX3DNode read FExportedNode write SetExportedNode;
51
52 procedure Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames);
53
54 { Parse the EXPORT declaration (XML encoding).
55 Given Element here must have TagName = 'EXPORT'. }
56 procedure ParseXML(Element: TDOMElement; Reader: TX3DReaderNames);
57
58 procedure SaveToStream(Writer: TX3DWriter); override;
59 function DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DExport;
60 end;
61
62 TX3DExportList = class({$ifdef FPC}specialize{$endif} TObjectList<TX3DExport>)
63 public
64 function DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DExportList;
65 end;
66
67 {$endif read_interface}
68
69 {$ifdef read_implementation}
70
71 { TX3DImport ---------------------------------------------------------------- }
72
73 procedure TX3DImport.Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames);
74 begin
75 Lexer.NextToken;
76 Lexer.CheckTokenIs(vtName, 'Inline node name');
77 InlineNodeName := Lexer.TokenX3DName;
78
79 Lexer.NextToken;
80 Lexer.CheckTokenIs(vtPeriod);
81
82 Lexer.NextToken;
83 Lexer.CheckTokenIs(vtName, 'imported node name');
84 ImportedNodeName := Lexer.TokenX3DName;
85
86 Lexer.NextToken;
87 if Lexer.TokenIsKeyword(vkAS) then
88 begin
89 Lexer.NextToken;
90 Lexer.CheckTokenIs(vtName, 'alias for imported node name');
91 ImportedNodeAlias := Lexer.TokenX3DName;
92
93 Lexer.NextToken;
94 end else
95 ImportedNodeAlias := ImportedNodeName;
96
97 Reader.DoImport(Self);
98 end;
99
100 procedure TX3DImport.ParseXML(Element: TDOMElement; Reader: TX3DReaderNames);
101 begin
102 if not XMLAttributeX3DName(Element, 'inlineDEF', InlineNodeName) then
103 begin
104 WritelnWarning('VRML/X3D', 'Missing IMPORT "inlineDEF" attribute');
105 Exit;
106 end;
107
108 if not XMLAttributeX3DName(Element, 'importedDEF', ImportedNodeName) then
109 begin
110 WritelnWarning('VRML/X3D', 'Missing IMPORT "importedDEF" attribute, looking for older "exportedDEF"');
111 if not XMLAttributeX3DName(Element, 'exportedDEF', ImportedNodeName) then
112 begin
113 WritelnWarning('VRML/X3D', 'Missing IMPORT attribute: neighter "importedDEF" nor older "exportedDEF" found');
114 Exit;
115 end;
116 end;
117
118 if not XMLAttributeX3DName(Element, 'AS', ImportedNodeAlias) then
119 ImportedNodeAlias := ImportedNodeName;
120
121 Reader.DoImport(Self);
122 end;
123
124 procedure TX3DImport.SaveToStream(Writer: TX3DWriter);
125 begin
126 case Writer.Encoding of
127 xeClassic:
128 begin
129 Writer.WriteIndent('IMPORT ' + EncodeX3DName(InlineNodeName) + '.' + EncodeX3DName(ImportedNodeName));
130 if ImportedNodeName <> ImportedNodeAlias then
131 Writer.Write(' AS ' + EncodeX3DName(ImportedNodeAlias));
132 Writer.Writeln('');
133 end;
134 xeXML:
135 begin
136 Writer.WriteIndent(Format('<IMPORT inlineDEF=%s importedDEF=%s',
137 [ StringToX3DXml(EncodeX3DName(InlineNodeName)),
138 StringToX3DXml(EncodeX3DName(ImportedNodeName)) ]));
139 if ImportedNodeName <> ImportedNodeAlias then
140 Writer.Write(' AS=' + StringToX3DXml(EncodeX3DName(ImportedNodeAlias)));
141 Writer.Writeln(' />');
142 end;
143 {$ifndef COMPILER_CASE_ANALYSIS}
144 else raise EInternalError.Create('TX3DImport.SaveToStream Encoding?');
145 {$endif}
146 end;
147 end;
148
149 function TX3DImport.DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DImport;
150 begin
151 Result := TX3DImport.Create;
152 Result.InlineNodeName := InlineNodeName;
153 Result.ImportedNodeName := ImportedNodeName;
154 Result.ImportedNodeAlias := ImportedNodeAlias;
155 end;
156
157 { TX3DExport ---------------------------------------------------------------- }
158
159 procedure TX3DExport.SetExportedNode(const Value: TX3DNode);
160 begin
161 if FExportedNode <> Value then
162 begin
163 if FExportedNode <> nil then
164 FExportedNode.RemoveDestructionNotification({$ifdef FPC}@{$endif} DestructionNotification);
165 FExportedNode := Value;
166 if FExportedNode <> nil then
167 FExportedNode.AddDestructionNotification({$ifdef FPC}@{$endif} DestructionNotification);
168 end;
169 end;
170
171 procedure TX3DExport.DestructionNotification(const Node: TX3DNode);
172 begin
173 { If FExportedNode is freed, make sure to set our FExportedNode to @nil.
174 Testcase:
175 - open some glTF model in castle-model-viewer
176 - select shape in castle-model-viewer
177 - remove a shape using castle-model-viewer "Edit -> Remove Shape..."
178 - save result to X3D using castle-model-viewer "File -> Save..."
179 Without TX3DExport.DestructionNotification, this would crash, as TX3DExport
180 would contain invalid FExportedNode pointer at saving time. }
181
182 if FExportedNode = Node then
183 FExportedNode := nil;
184 end;
185
186 destructor TX3DExport.Destroy;
187 begin
188 ExportedNode := nil; // make sure to unregister DestructionNotification
189 inherited;
190 end;
191
192 procedure TX3DExport.Parse(Lexer: TX3DLexer; Reader: TX3DReaderNames);
193 var
194 ExportedNodeName: String;
195 begin
196 Lexer.NextToken;
197 Lexer.CheckTokenIs(vtName, 'exported node name');
198 ExportedNodeName := Lexer.TokenX3DName;
199
200 Lexer.NextToken;
201 if Lexer.TokenIsKeyword(vkAS) then
202 begin
203 Lexer.NextToken;
204 Lexer.CheckTokenIs(vtName, 'alias for exported node name');
205 ExportedAlias := Lexer.TokenX3DName;
206
207 Lexer.NextToken;
208 end else
209 ExportedAlias := '';
210
211 Reader.DoExport(Self, ExportedNodeName);
212 end;
213
214 procedure TX3DExport.ParseXML(Element: TDOMElement; Reader: TX3DReaderNames);
215 var
216 ExportedNodeName: String;
217 begin
218 if not XMLAttributeX3DName(Element, 'localDEF', ExportedNodeName) then
219 begin
220 WritelnWarning('VRML/X3D', 'Missing EXPORT "localDEF" attribute');
221 Exit;
222 end;
223
224 if not XMLAttributeX3DName(Element, 'AS', ExportedAlias) then
225 ExportedAlias := '';
226
227 Reader.DoExport(Self, ExportedNodeName);
228 end;
229
230 procedure TX3DExport.SaveToStream(Writer: TX3DWriter);
231 begin
232 if ExportedNode = nil then
233 begin
234 WritelnWarning('Cannot save EXPORT statement to X3D: Exported node not set');
235 Exit;
236 end;
237
238 if ExportedNode.X3DName = '' then
239 begin
240 WritelnWarning('Cannot save EXPORT statement to X3D: Exported node %s has empty name', [ExportedNode.NiceName]);
241 Exit;
242 end;
243
244 case Writer.Encoding of
245 xeClassic:
246 begin
247 Writer.WriteIndent('EXPORT ' + EncodeX3DName(ExportedNode.X3DName));
248 if (ExportedAlias <> '') and
249 (ExportedNode.X3DName <> ExportedAlias) then
250 Writer.Write(' AS ' + EncodeX3DName(ExportedAlias));
251 Writer.Writeln('');
252 end;
253 xeXML:
254 begin
255 Writer.WriteIndent('<EXPORT localDEF=' + StringToX3DXml(EncodeX3DName(ExportedNode.X3DName)));
256 if (ExportedAlias <> '') and
257 (ExportedNode.X3DName <> ExportedAlias) then
258 Writer.Write(' AS=' + StringToX3DXml(EncodeX3DName(ExportedAlias)));
259 Writer.Writeln(' />');
260 end;
261 {$ifndef COMPILER_CASE_ANALYSIS}
262 else raise EInternalError.Create('TX3DExport.SaveToStream Encoding?');
263 {$endif}
264 end;
265 end;
266
267 function TX3DExport.DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DExport;
268 begin
269 { Use CopyState to reuse existing TX3DExport reference.
270
271 Useful, because:
272
273 TX3DRootNode.ExportedNodes, which is TX3DExportList,
274 carries references to the same TX3DExport instances that are also
275 listed in TX3DNode.Exports.
276 So TX3DExportList is created with OwnsObjects=false,
277 because it should not own TX3DExport instances.
278 All TX3DExport instances should be on TX3DNode.Exports list, which already
279 owns them.
280 And TX3DExportList should only have additional references to them.
281
282 This is what happens when reading the file in normal case, without DeepCopy.
283
284 In TX3DExportList.DeepCopy, for consistency,
285 also TX3DExportList is created with OwnsObjects=false.
286 So it should also carry the same references (not have new TX3DExport instances).
287 }
288
289 if not CopyState.ExportOriginalToCopy.TryGetValue(Self, Result) then
290 begin
291 Result := TX3DExport.Create;
292 Result.ExportedNode := CopyState.DeepCopy(ExportedNode);
293 Result.ExportedAlias := ExportedAlias;
294
295 CopyState.ExportOriginalToCopy.Add(Self, Result);
296 end;
297 end;
298
299 { TX3DExportList ------------------------------------------------------------- }
300
301 function TX3DExportList.DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DExportList;
302 var
303 I: Integer;
304 begin
305 Result := TX3DExportList.Create(false); // do not own TX3DExport instances, they are owned by TX3DNode.Exports
306 Result.Count := Count;
307 for I := 0 to Count - 1 do
308 Result[I] := Items[I].DeepCopy(CopyState);
309 end;
310
311 {$endif read_implementation}