Debian Codegraph @

src/scene/x3d/x3dnodes_nodesmanager.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2002-2018 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 { TNodesManager ------------------------------------------------------------ }
20
21 type
22 { }
23 ENodesManagerError = class(EX3DError);
24 ENodeClassRegisterError = class(ENodesManagerError);
25
26 { Manages X3D non-abstract node classes, that can be used in X3D files.
27 Use only through a singleton @link(NodesManager). }
28 TNodesManager = class
29 strict private
30 { Strings[] is ClassX3DType. Objects[] is the actual class
31 (typecast to TX3DNodeClass is safe). }
32 FRegistered: TStringList;
33 function GetRegistered(Index: Integer): TX3DNodeClass;
34 public
35 constructor Create;
36 destructor Destroy; override;
37
38 { Make the given node class known to the parser and other routines.
39 We associate the node class with it's TX3DNode.ClassX3DType
40 (make sure it's not empty).
41
42 It is OK to register two different node classes with the same node.
43 For example, VRML 1.0 TConeNode_1 class and VRML 2.0/X3D TConeNode_2
44 class both have a name 'Cone' (and will be correctly chosen during parsing).
45 But you cannot register two times the same NodeClass.
46
47 @groupBegin }
48 procedure RegisterNodeClass(NodeClass: TX3DNodeClass);
49 procedure RegisterNodeClasses(const NodeClasses: array of TX3DNodeClass);
50 { @groupEnd }
51
52 { Unregisters given node class, removing it from our table.
53
54 @raises(ENodesManagerError if NodeClass.ClassX3DType = ''
55 (so it cannot be even registered), or if
56 ((NodeClass was not registered) and ErrorIfNotRegistered)) }
57 procedure UnRegisterNodeClass(NodeClass: TX3DNodeClass;
58 ErrorIfNotRegistered: boolean = true);
59
60 { Return node class for a given name. This method is the main purpose
61 of TNodesManager: to map node names into node classes.
62
63 Searches in nodes registered by RegisterNodeClass and such.
64 During searching, looks not only for matching node name,
65 but also at matching VRML/X3D version, checking
66 @code(ForVRMLVersion(Version)).
67
68 Returns @nil when not found. }
69 function X3DTypeToClass(const AX3DType: string;
70 const Version: TX3DVersion): TX3DNodeClass;
71
72 { Return class that matches given URL. This is useful for EXTERNROTOs.
73 Checks also @code(ForVRMLVersion(Version)).
74 Returns @nil if not found. }
75 function URNToClass(const URN: string;
76 const Version: TX3DVersion): TX3DNodeClass;
77
78 { Enumerate all registered classes, from Registered[0] to
79 Registered[RegisteredCount - 1].
80
81 @groupBegin }
82 property Registered [Index: Integer]: TX3DNodeClass read GetRegistered;
83 function RegisteredCount: Cardinal;
84 { @groupEnd }
85 end;
86
87 var
88 { Singleton to manage X3D non-abstract node classes, that can be used in X3D files.
89
90 In normal circumstances, this is the only
91 instance of TNodesManager class ever created. It is created / destroyed
92 in this unit's initialization / finalization. }
93 NodesManager: TNodesManager;
94
95 {$endif read_interface}
96
97 {$ifdef read_implementation}
98
99 { TNodesManager ------------------------------------------------------------ }
100
101 constructor TNodesManager.Create;
102 begin
103 inherited;
104 FRegistered := TStringListCaseSens.Create;
105 end;
106
107 destructor TNodesManager.Destroy;
108 begin
109 FRegistered.Free;
110 inherited;
111 end;
112
113 procedure TNodesManager.RegisterNodeClass(NodeClass: TX3DNodeClass);
114 begin
115 if NodeClass.ClassX3DType = '' then
116 raise ENodesManagerError.Create('Class '+NodeClass.ClassName+' has '+
117 'empty ClassX3DType so it cannot be registered in TNodesManager');
118
119 if FRegistered.IndexOfObject(TObject(Pointer(NodeClass))) <> -1 then
120 raise ENodesManagerError.Create('Class '+NodeClass.ClassName+
121 ' was already registered in TNodesManager');
122
123 FRegistered.AddObject(NodeClass.ClassX3DType, TObject(Pointer(NodeClass)));
124 end;
125
126 procedure TNodesManager.RegisterNodeClasses(
127 const NodeClasses: array of TX3DNodeClass);
128 var
129 I: Integer;
130 begin
131 for i := 0 to High(NodeClasses) do RegisterNodeClass(NodeClasses[i]);
132 end;
133
134 procedure TNodesManager.UnRegisterNodeClass(NodeClass: TX3DNodeClass;
135 ErrorIfNotRegistered: boolean);
136 var i: Integer;
137 begin
138 if NodeClass.ClassX3DType = '' then
139 raise ENodesManagerError.Create('Class '+NodeClass.ClassName+' has '+
140 'empty ClassX3DType so it cannot be unregistered (or even registered) '+
141 'in TNodesManager');
142
143 i := FRegistered.IndexOfObject(TObject(Pointer(NodeClass)));
144 if i <> - 1 then
145 FRegistered.Delete(i) else
146 if ErrorIfNotRegistered then
147 ENodesManagerError.Create('Node class "' + NodeClass.ClassName +
148 '" was not registered, so you cannot unregister it');
149 end;
150
151 function TNodesManager.X3DTypeToClass(const AX3DType: string;
152 const Version: TX3DVersion): TX3DNodeClass;
153 var
154 I: Integer;
155 begin
156 for I := 0 to FRegistered.Count - 1 do
157 begin
158 Result := TX3DNodeClass(FRegistered.Objects[I]);
159 if (FRegistered[I] = AX3DType) and
160 Result.ForVRMLVersion(Version) then
161 Exit;
162 end;
163 Result := nil;
164 end;
165
166 function TNodesManager.URNToClass(const URN: string;
167 const Version: TX3DVersion): TX3DNodeClass;
168
169 { Does this node name match the given URN. This is used when searching for an built-in
170 implementation of a node declared in X3D using the EXTERNPROTO.
171
172 In the past, this was using a virtual URNMatching implementation at each node,
173 which tried to be smart:
174 - use CGE prefix only for CGE extensions
175 - use X3D only for X3D nodes (version >= 3)
176 - use VRML 97 only for VRML 97 nodes (version 2)
177 etc.
178
179 Ultimately, this was causing a lot of maintenance burden,
180 and implementation of URNMatching was often untested and slightly incorrect,
181 and was useless.
182
183 - Standard URNs for nodes are not widely used,
184 - and most modern formats are just X3D anyway,
185 - and we can check ForVRMLVersion.
186 So we can implement them simpler. }
187 function URNMatching(const URN, ClassX3DType: String): Boolean;
188 begin
189 Result :=
190 (URN = 'urn:castle-engine.io:node:' + ClassX3DType) or
191 (URN = 'urn:castle-engine.sourceforge.io:node:' + ClassX3DType) or
192 (URN = 'urn:castle-engine.sourceforge.net:node:' + ClassX3DType) or
193 (URN = 'urn:vrmlengine.sourceforge.net:node:' + ClassX3DType) or
194
195 { URNs used to indicate standard VRML / X3D nodes.
196
197 Funny thing, I actually didn't found anywhere a definite official
198 statement that they are using such-and-such URNs.
199
200 X3D specification refers to RFC
201 [http://www.ietf.org/rfc/rfc3541.txt?number=3541] which, basically,
202 just says "we like URNs and we'll use them" and nothing more.
203 Same thing for VRML 97 spec
204 [http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/extensions.html].
205 There is no precise answer e.g. what URN should be used to
206 Indicate some standard VRML 97 / X3D node.
207
208 I constructed URNs below looking at examples in the RFC,
209 annotated by a funny line "The following examples are not
210 guaranteed to be real. They are presented for pedagogical reasons only." }
211 (URN = 'urn:web3d:vrml97:node:' + ClassX3DType) or
212 (URN = 'urn:web3d:x3d:node:' + ClassX3DType) or
213
214 { URN to indicate BitManagement nodes. This should work, according to
215 http://www.bitmanagement.com/developer/contact/examples/layer/index.html
216 example EXTERNPROTO. }
217 (URN = 'urn:inet:bitmanagement.de:node:' + ClassX3DType);
218 end;
219
220 var
221 I: Integer;
222 begin
223 for I := 0 to FRegistered.Count - 1 do
224 begin
225 Result := TX3DNodeClass(FRegistered.Objects[I]);
226 if URNMatching(URN, Result.ClassX3DType) and
227 Result.ForVRMLVersion(Version) then
228 Exit;
229 end;
230 Result := nil;
231 end;
232
233 function TNodesManager.GetRegistered(Index: Integer): TX3DNodeClass;
234 begin
235 Result := TX3DNodeClass(FRegistered.Objects[Index]);
236 end;
237
238 function TNodesManager.RegisteredCount: Cardinal;
239 begin
240 Result := FRegistered.Count;
241 end;
242
243 {$endif read_implementation}