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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
#ifndef NAMETOOBJECTMAP_H
#define NAMETOOBJECTMAP_H
#include <string>
#include <map>
#include "Runtime/BaseClasses/BaseObject.h"
#if UNITY_EDITOR
#include "Editor/Src/BuildPipeline/BuildSerialization.h"
#endif
using std::map;
using std::string;
using std::pair;
static inline bool IsBuiltinResourceObject(Object* o)
{
// built-in resources have this flag set
if (o->TestHideFlag(Object::kHideAndDontSave))
return true;
// Resources from builtin_extra only have "not editable" flag set, so the above check doesn't catch them.
// In the editor (where this matters mostly when building resource files), catch that by detecting
// if the object came from any built-in resources file.
#if UNITY_EDITOR
if (IsAnyDefaultResourcesObject(o->GetInstanceID()))
return true;
#endif
return false;
}
template<class Type, class ObjectToName, class NameToObject>
class NameToObjectMap
{
private:
typedef typename ObjectToName::iterator ObjectToNameIterator;
typedef typename NameToObject::iterator NameToObjectIterator;
ObjectToName m_ObjectToName;
NameToObject m_NameToObject;
Object* m_ObjectToDirty;
public:
DECLARE_SERIALIZE (NameToObjectMap)
void SetObjectToDirty (Object* dirty) { m_ObjectToDirty = dirty; }
void Add (const string& name, PPtr<Type> o)
{
Remove (o);
m_ObjectToName.insert (make_pair (o, name));
m_NameToObject.insert (make_pair (name, o));
AssertIf (m_NameToObject.size () != m_ObjectToName.size ());
m_ObjectToDirty->SetDirty ();
}
bool Remove (PPtr<Type> object)
{
AssertIf (m_NameToObject.size () != m_ObjectToName.size ());
int oldSize = m_NameToObject.size ();
{
pair<NameToObjectIterator, NameToObjectIterator> range = make_pair(m_NameToObject.begin(), m_NameToObject.end());
NameToObjectIterator i, next;
for (i=range.first;i!=range.second;i=next)
{
next = i; next++;
if (i->second == object)
{
m_NameToObject.erase (i);
}
}
}
{
pair<ObjectToNameIterator, ObjectToNameIterator> range;
range = m_ObjectToName.equal_range (object);
m_ObjectToName.erase(range.first, range.second);
}
m_ObjectToDirty->SetDirty ();
AssertIf (m_NameToObject.size () != m_ObjectToName.size ());
return oldSize != m_NameToObject.size ();
}
Type* Find (const string& name)
{
// Get all with name 'name'
pair<NameToObjectIterator, NameToObjectIterator> range;
range = m_NameToObject.equal_range (name);
NameToObjectIterator i, next;
Type* found = NULL;
// Then find the first that is loaded, those that can't be loaded
// are removed.
for (i=range.first;i!=range.second;i=next)
{
next = i; next++;
Type* o = i->second;
if (o)
{
// When there are two shaders one builtin resource and one normal shader
// Then we want the one in the project folder not the builtin one. So people can override shaders
// At some point we should try to get the ordering of shader includes better defined!
if (found && IsBuiltinResourceObject(o))
continue;
found = o;
}
}
return found;
}
std::vector<PPtr<Type> > GetAllObjects ()
{
std::vector<PPtr<Type> > objects;
for (NameToObjectIterator i=m_NameToObject.begin ();i!=m_NameToObject.end ();i++)
{
objects.push_back(i->second);
}
return objects;
}
const NameToObject& GetAll ()
{
return m_NameToObject;
}
private:
void Rebuild ()
{
// Rebuild name -> object
m_NameToObject.clear ();
ObjectToNameIterator i;
for (i=m_ObjectToName.begin ();i != m_ObjectToName.end ();i++)
m_NameToObject.insert (make_pair (i->second, i->first));
}
};
template<class Type, class ObjectToName, class NameToObject>
template<class TransferFunction>
void NameToObjectMap<Type, ObjectToName, NameToObject>::Transfer (TransferFunction& transfer)
{
TRANSFER (m_ObjectToName);
if (transfer.IsReading ())
Rebuild ();
}
#endif
|