| 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 |
{ } |
| 20 |
TX3DNodeNameRec = record |
| 21 |
Node: TX3DNode; |
| 22 |
Name: string; |
| 23 |
Finished: boolean; |
| 24 |
function DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DNodeNameRec; |
| 25 |
end; |
| 26 |
PX3DNodeNameRec = ^TX3DNodeNameRec; |
| 27 |
|
| 28 |
{ List to track node names while parsing VRML/X3D file. } |
| 29 |
TX3DNodeNames = class({$ifdef FPC}specialize{$endif} TStructList<TX3DNodeNameRec>) |
| 30 |
strict private |
| 31 |
FAutoRemove: boolean; |
| 32 |
procedure DestructionNotification(const Node: TX3DNode); |
| 33 |
function IndexOfNode(const Node: TX3DNode): Integer; |
| 34 |
function IndexOfName(const Name: string): Integer; |
| 35 |
public |
| 36 |
constructor Create(const AAutoRemove: boolean); |
| 37 |
destructor Destroy; override; |
| 38 |
|
| 39 |
{ If @true (determined at construction time), |
| 40 |
then destroyed nodes will be automatically removed from this list. |
| 41 |
This allows you to safely destroy node instances during this |
| 42 |
objects lifetime, without worrying that some dangling pointers remain |
| 43 |
on this list. |
| 44 |
|
| 45 |
Internally, this is done by registering |
| 46 |
itself for AnyNodeDestructionNotifications. } |
| 47 |
property AutoRemove: boolean read FAutoRemove; |
| 48 |
|
| 49 |
{ Associate given node with it's own name. |
| 50 |
|
| 51 |
If not NodeFinished, then we understand that we're parsing / saving |
| 52 |
the node's contents now. If NodeFinished, then we know the node |
| 53 |
contents are fully parsed / saved now. This information helps |
| 54 |
us to detect cycles in X3D DEF/USE graph. |
| 55 |
For now, we just disallow such cycles. Still, we allow |
| 56 |
ROUTEs from inside the node, so calling with NodeFinished = false |
| 57 |
is still useful for parsing. } |
| 58 |
procedure Bind(const Node: TX3DNode; const NodeFinished: boolean); overload; |
| 59 |
procedure Bind(const Node: TX3DNode; const NodeFinished: boolean; const BindToName: string); overload; |
| 60 |
|
| 61 |
{ Find node bound to given name. @nil if none. } |
| 62 |
function Bound(const Name: string; out NodeFinished: boolean): TX3DNode; overload; |
| 63 |
|
| 64 |
{ Node is during reading, judging from Bind calls with NodeFinished=@false |
| 65 |
(when node reading starts) and with NodeFinished=@true (when node reading |
| 66 |
finishes). } |
| 67 |
function NodeDuringReading(const Node: TX3DNode): Boolean; |
| 68 |
|
| 69 |
{ Check is Node bound in the current namespace. |
| 70 |
@false means that node is not within this namespace, |
| 71 |
possibly it's name was hidden by other node with the same name. |
| 72 |
|
| 73 |
Doesn't check is Node bound to it's name (Node.Name) or something |
| 74 |
else. So this assumes that node can only be bound (if at all) |
| 75 |
only to it's own name, which is true during parsing |
| 76 |
(when nothing can change in the middle of parsing). } |
| 77 |
function Bound(Node: TX3DNode): boolean; overload; |
| 78 |
|
| 79 |
function DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DNodeNames; |
| 80 |
end; |
| 81 |
|
| 82 |
TX3DPrototypeNames = class(TStringListCaseSens) |
| 83 |
public |
| 84 |
procedure Bind(Proto: TX3DPrototypeBase); |
| 85 |
|
| 86 |
{ Find proto bound to given name. @nil if none. } |
| 87 |
function Bound(const Name: string): TX3DPrototypeBase; |
| 88 |
end; |
| 89 |
|
| 90 |
TX3DImportableNames = class(TStringListCaseSens) |
| 91 |
public |
| 92 |
destructor Destroy; override; |
| 93 |
|
| 94 |
{ Bind Exported names to given Inline node name. |
| 95 |
Exported instance becomes owner by this TX3DImportableNames instance. |
| 96 |
InlineName must be <> '' here. } |
| 97 |
procedure Bind(const InlineName: string; Exported: TX3DNodeNames); |
| 98 |
end; |
| 99 |
|
| 100 |
{$endif read_interface} |
| 101 |
|
| 102 |
{$ifdef read_implementation} |
| 103 |
|
| 104 |
{ TX3DNodeNameRec ------------------------------------------------------------ } |
| 105 |
|
| 106 |
function TX3DNodeNameRec.DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DNodeNameRec; |
| 107 |
begin |
| 108 |
Result.Node := CopyState.DeepCopy(Node); |
| 109 |
Result.Name := Name; |
| 110 |
Result.Finished := Finished; |
| 111 |
end; |
| 112 |
|
| 113 |
{ TX3DNodeNames ----------------------------------------------------------- } |
| 114 |
|
| 115 |
constructor TX3DNodeNames.Create(const AAutoRemove: boolean); |
| 116 |
begin |
| 117 |
inherited Create; |
| 118 |
FAutoRemove := AAutoRemove; |
| 119 |
if AutoRemove then |
| 120 |
AnyNodeDestructionNotifications.Add({$ifdef FPC}@{$endif} DestructionNotification); |
| 121 |
end; |
| 122 |
|
| 123 |
destructor TX3DNodeNames.Destroy; |
| 124 |
begin |
| 125 |
{ This may happen after X3DNodes unit finalization |
| 126 |
(e.g. simplest_vrml_browser_with_shadow_volumes demo_models/shadow_volumes/stonehenge.wrl, |
| 127 |
where TX3DRootNode with some ExportedNames is freed from CastleWindow |
| 128 |
unit finalization, because Application owns Window that owns Scene). |
| 129 |
So secure from AnyNodeDestructionNotifications being nil. } |
| 130 |
if AutoRemove and (AnyNodeDestructionNotifications <> nil) then |
| 131 |
AnyNodeDestructionNotifications.Remove({$ifdef FPC}@{$endif} DestructionNotification); |
| 132 |
inherited; |
| 133 |
end; |
| 134 |
|
| 135 |
function TX3DNodeNames.IndexOfNode(const Node: TX3DNode): Integer; |
| 136 |
begin |
| 137 |
for Result := 0 to Count - 1 do |
| 138 |
if L[Result].Node = Node then |
| 139 |
Exit; |
| 140 |
Result := -1; |
| 141 |
end; |
| 142 |
|
| 143 |
function TX3DNodeNames.IndexOfName(const Name: string): Integer; |
| 144 |
begin |
| 145 |
for Result := 0 to Count - 1 do |
| 146 |
if L[Result].Name = Name then |
| 147 |
Exit; |
| 148 |
Result := -1; |
| 149 |
end; |
| 150 |
|
| 151 |
function TX3DNodeNames.NodeDuringReading(const Node: TX3DNode): Boolean; |
| 152 |
var |
| 153 |
I: Integer; |
| 154 |
begin |
| 155 |
for I := 0 to Count - 1 do |
| 156 |
if L[I].Node = Node then |
| 157 |
Exit(not L[I].Finished); |
| 158 |
Result := false; // Node reading not even started |
| 159 |
end; |
| 160 |
|
| 161 |
procedure TX3DNodeNames.DestructionNotification(const Node: TX3DNode); |
| 162 |
var |
| 163 |
I: Integer; |
| 164 |
begin |
| 165 |
I := IndexOfNode(Node); |
| 166 |
if I >= 0 then |
| 167 |
Delete(I); |
| 168 |
end; |
| 169 |
|
| 170 |
procedure TX3DNodeNames.Bind(const Node: TX3DNode; const NodeFinished: boolean; const BindToName: string); |
| 171 |
var |
| 172 |
I: Integer; |
| 173 |
P: PX3DNodeNameRec; |
| 174 |
begin |
| 175 |
if BindToName <> '' then |
| 176 |
begin |
| 177 |
I := IndexOfName(BindToName); |
| 178 |
if I <> -1 then |
| 179 |
begin |
| 180 |
P := PX3DNodeNameRec(Ptr(I)); |
| 181 |
{ Warn about duplicate node names. |
| 182 |
Avoid warning for TX3DPrototypeNode, as right now we knowingly |
| 183 |
override names when instantiating node. |
| 184 |
Testcase: demo-models/x3d/anchor_test.x3dv } |
| 185 |
if (P^.Node <> Node) and |
| 186 |
(not (P^.Node is TX3DPrototypeNode)) and |
| 187 |
(not (Node is TX3DPrototypeNode)) then |
| 188 |
begin |
| 189 |
WritelnWarning('Node name "%s" used multiple times for different nodes (%s and %s). This is not allowed in X3D.' + NL + |
| 190 |
' Possible reasons:' + NL + |
| 191 |
' 1. You opened an X3D model generated by older CGE tools, that didn''t force node names unique when input had non-unique names. Regenerate this X3D model using latest CGE tools.' + NL + |
| 192 |
' 2. You opened a VRML model with non-unique names. Non-unique names are valid in VRML, but we recommend to upgrade all VRML models to X3D and make names unique.', [ |
| 193 |
BindToName, |
| 194 |
P^.Node.X3DType, |
| 195 |
Node.X3DType |
| 196 |
]); |
| 197 |
end; |
| 198 |
end else |
| 199 |
P := PX3DNodeNameRec(Add); |
| 200 |
P^.Node := Node; |
| 201 |
P^.Name := BindToName; |
| 202 |
P^.Finished := NodeFinished; |
| 203 |
end; |
| 204 |
end; |
| 205 |
|
| 206 |
procedure TX3DNodeNames.Bind(const Node: TX3DNode; const NodeFinished: boolean); |
| 207 |
begin |
| 208 |
Bind(Node, NodeFinished, Node.X3DName); |
| 209 |
end; |
| 210 |
|
| 211 |
function TX3DNodeNames.Bound(const Name: string; out NodeFinished: boolean): TX3DNode; |
| 212 |
var |
| 213 |
I: Integer; |
| 214 |
begin |
| 215 |
I := IndexOfName(Name); |
| 216 |
if I <> -1 then |
| 217 |
begin |
| 218 |
Result := L[I].Node; |
| 219 |
NodeFinished := L[I].Finished; |
| 220 |
end else |
| 221 |
Result := nil; |
| 222 |
end; |
| 223 |
|
| 224 |
function TX3DNodeNames.Bound(Node: TX3DNode): boolean; |
| 225 |
begin |
| 226 |
Result := IndexOfNode(Node) <> -1; |
| 227 |
end; |
| 228 |
|
| 229 |
function TX3DNodeNames.DeepCopy(const CopyState: TX3DNodeDeepCopyState): TX3DNodeNames; |
| 230 |
var |
| 231 |
I: Integer; |
| 232 |
begin |
| 233 |
Result := TX3DNodeNames.Create(AutoRemove); |
| 234 |
Result.Count := Count; |
| 235 |
for I := 0 to Count - 1 do |
| 236 |
Result.Items[I] := Items[I].DeepCopy(CopyState); |
| 237 |
end; |
| 238 |
|
| 239 |
{ TX3DPrototypeNames -------------------------------------------------------- } |
| 240 |
|
| 241 |
procedure TX3DPrototypeNames.Bind(Proto: TX3DPrototypeBase); |
| 242 |
var |
| 243 |
I: Integer; |
| 244 |
begin |
| 245 |
I := IndexOf(Proto.X3DName); |
| 246 |
if I <> - 1 then |
| 247 |
Objects[I] := Proto else |
| 248 |
AddObject(Proto.X3DName, Proto); |
| 249 |
end; |
| 250 |
|
| 251 |
function TX3DPrototypeNames.Bound(const Name: string): TX3DPrototypeBase; |
| 252 |
var |
| 253 |
I: Integer; |
| 254 |
begin |
| 255 |
I := IndexOf(Name); |
| 256 |
if I <> -1 then |
| 257 |
Result := Objects[I] as TX3DPrototypeBase else |
| 258 |
Result := nil; |
| 259 |
end; |
| 260 |
|
| 261 |
{ TX3DImportableNames ------------------------------------------------------- } |
| 262 |
|
| 263 |
destructor TX3DImportableNames.Destroy; |
| 264 |
var |
| 265 |
I: Integer; |
| 266 |
begin |
| 267 |
for I := 0 to Count - 1 do |
| 268 |
Objects[I].Free; |
| 269 |
inherited; |
| 270 |
end; |
| 271 |
|
| 272 |
procedure TX3DImportableNames.Bind(const InlineName: string; Exported: TX3DNodeNames); |
| 273 |
var |
| 274 |
I: Integer; |
| 275 |
begin |
| 276 |
I := IndexOf(InlineName); |
| 277 |
if I = -1 then |
| 278 |
AddObject(InlineName, Exported) else |
| 279 |
begin |
| 280 |
Objects[I].Free; |
| 281 |
Objects[I] := Exported; |
| 282 |
end; |
| 283 |
end; |
| 284 |
|
| 285 |
{$endif read_implementation} |