/
namedtargets.cs
90 lines (72 loc) · 2.16 KB
/
namedtargets.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Improved versions of setNTObjectName and clearNTObjectName with much
// higher performance. Required to fix lag when clearing named bricks.
// -------------------------------------------------------------------
function SimObject::setNTObjectName(%this, %name)
{
%this = %this.getId();
%name = getSafeVariableName(trim(%name));
if(%name $= "")
{
%this.clearNTObjectName();
%this.setName("");
return;
}
//Names must start with a _ to prevent overwriting real objects
if(getSubStr(%name, 0, 1) !$= "_")
%name = "_" @ %name;
if(%this.getName() $= %name)
return;
if(isObject(%name) && !(%name.getType() & $TypeMasks::FxBrickAlwaysObjectType))
{
error("ERROR: SimObject::setNTObjectName() - Non-Brick object named \"" @ %name @ "\" already exists!");
return;
}
%this.clearNTObjectName();
%group = %this.getGroup();
%count = %group.NTObjectCount[%name] | 0;
if(!%count)
%group.addNTName(%name);
//Add a reverse lookup to remove the name much faster
%group.NTObject[%name, %count] = %this;
%group.NTObjectIndex[%name, %this] = %count;
%group.NTObjectCount[%name] = %count + 1;
%this.setName(%name);
}
function SimObject::clearNTObjectName(%this)
{
%this = %this.getId();
%group = %this.getGroup();
if(!isObject(%group))
return;
%oldName = %this.getName();
if(%oldName $= "")
return;
%index = %group.NTObjectIndex[%oldName, %this];
%count = %group.NTObjectCount[%oldName];
if(%group.NTObject[%oldName, %index] == %this)
{
//Reverse lookup works, use fast version
%lastObj = %group.NTObject[%oldName, %count - 1];
%group.NTObject[%oldName, %index] = %lastObj;
%group.NTObject[%oldName, %count - 1] = "";
%group.NTObjectIndex[%oldName, %lastObj] = %index;
%group.NTObjectIndex[%oldName, %this] = "";
%group.NTObjectCount[%oldName]--;
}
else
{
//Reverse lookup failed, use old and slow version
for(%i = 0; %i < %count; %i++)
{
if(%group.NTObject[%oldName, %i] == %this)
{
%group.NTObject[%oldName, %i] = %group.NTObject[%oldName, %count - 1];
%group.NTObject[%oldName, %count - 1] = "";
%group.NTObjectCount[%oldName]--;
break;
}
}
}
if(!%group.NTObjectCount[%oldName])
%group.removeNTName(%oldName);
}