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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
#include "UnityPrefix.h"
#include "AssetBundle.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "Runtime/Serialize/PersistentManager.h"
#include "Runtime/Misc/ResourceManager.h"
#if UNITY_EDITOR
#include "Editor/Src/Utility/RuntimeClassHashing.h"
#endif
#if ENABLE_WWW
#include "PlatformDependent/CommonWebPlugin/UnityWebStream.h"
#endif
#if ENABLE_CACHING
#include "Runtime/Misc/CachingManager.h"
#endif
using namespace std;
IMPLEMENT_CLASS (AssetBundle)
IMPLEMENT_OBJECT_SERIALIZE (AssetBundle)
AssetBundle::AssetBundle (MemLabelId label, ObjectCreationMode mode)
: Super(label, mode)
{
#if ENABLE_WWW
m_UnityWebStream = NULL;
#endif
#if ENABLE_CACHING
m_CachedUnityWebStream = NULL;
#endif
m_UncompressedFileInfo = NULL;
m_RuntimeCompatibility = CURRENT_RUNTIME_COMPATIBILITY_VERSION;
// Mark as kDontSave, so that AssetBundles are not unloaded on level loads
SetHideFlags(Object::kDontSave);
}
template<class TransferFunc>
void AssetBundle::AssetInfo::Transfer (TransferFunc& transfer)
{
TRANSFER(preloadIndex);
TRANSFER(preloadSize);
TRANSFER(asset);
}
template<class TransferFunc>
void AssetBundleScriptInfo::Transfer (TransferFunc& transfer)
{
TRANSFER (className);
TRANSFER (nameSpace);
TRANSFER (assemblyName);
TRANSFER (hash);
}
template<class TransferFunc>
void AssetBundle::Transfer (TransferFunc& transfer)
{
Super::Transfer (transfer);
transfer.SetVersion (3);
AssertIf (transfer.GetFlags() & kPerformUnloadDependencyTracking);
if (transfer.IsReading ())
m_RuntimeCompatibility = 0;
if (transfer.IsOldVersion(1))
{
multimap<UnityStr, PPtr<Object> > oldContainer;
transfer.Transfer(oldContainer, "m_Container");
PPtr<Object> mainAsset;
transfer.Transfer(mainAsset, "m_MainAsset");
m_Container.clear();
for (multimap<UnityStr, PPtr<Object> >::iterator i=oldContainer.begin();i != oldContainer.end();i++)
{
AssetInfo info;
info.preloadIndex = 0;
info.preloadSize = 0;
info.asset = i->second;
m_Container.insert(make_pair(i->first, info));
}
m_MainAsset.preloadIndex = 0;
m_MainAsset.preloadSize = 0;
m_MainAsset.asset = mainAsset;
}
else
{
transfer.Transfer(m_PreloadTable, "m_PreloadTable");
transfer.Transfer(m_Container, "m_Container");
transfer.Transfer(m_MainAsset, "m_MainAsset");
transfer.Transfer(m_ScriptCompatibility, "m_ScriptCompatibility");
transfer.Transfer(m_ClassCompatibility, "m_ClassCompatibility");
if (!transfer.IsOldVersion (2))
transfer.Transfer (m_RuntimeCompatibility, "m_RuntimeCompatibility");
}
}
void AssetBundle::DebugPrintContents ()
{
for (iterator i=m_Container.begin();i != m_Container.end();i++)
{
printf_console("- %s\n", i->first.c_str());
}
}
AssetBundle::range AssetBundle::GetPathRange (const string& path)
{
// if (m_Container.equal_range(ToLower(path)).first == m_Container.equal_range(ToLower(path)).second)
// {
// printf_console(("Failed loading " + path + "\n***********\n").c_str());
// DebugPrintContents();
// }
return m_Container.equal_range(ToLower(path));
}
AssetBundle::range AssetBundle::GetAll ()
{
return make_pair (m_Container.begin(), m_Container.end());
}
Object* AssetBundle::GetImpl (int classID, const string& path)
{
range r = GetPathRange(path);
for (iterator i=r.first;i != r.second;i++)
{
Object* obj = i->second.asset;
if (obj && obj->IsDerivedFrom(classID))
return obj;
}
//printf_console(("Failed loading " + path + "\n***********\n").c_str());
//DebugPrintContents();
return NULL;
}
AssetBundle::~AssetBundle ()
{
#if ENABLE_WWW
if (m_UnityWebStream)
m_UnityWebStream->Release();
#endif
#if ENABLE_CACHING
UNITY_DELETE(m_CachedUnityWebStream, kMemFile);
#endif
// Don't kill of m_UncompressedFileInfo here due to weird
// logic in UnloadAssetBundle() that will access the UncompressedFileInfo
// even after the AssetBundle has been deleted through DestroyAllAtPath().
}
bool AssetBundle::ShouldIgnoreInGarbageDependencyTracking ()
{
return true;
}
#if UNITY_EDITOR
void AssetBundle::AddScriptCompatibilityInfo (std::string const& className, std::string const& nameSpace, std::string const& assembly, UInt32 hash)
{
m_ScriptCompatibility.push_back (AssetBundleScriptInfo (className, nameSpace, assembly, hash));
}
void AssetBundle::FillHashTableForRuntimeClasses (std::vector<SInt32> const& classIds, TransferInstructionFlags transferFlags)
{
vector_map<int, UInt32> hashes;
CalculateHashForClasses (hashes, classIds, transferFlags);
m_ClassCompatibility.resize (hashes.size ());
std::copy (hashes.begin (), hashes.end (), m_ClassCompatibility.begin ());
}
AssetBundle* GetEditorAssetBundle ()
{
int instanceID = GetPersistentManager().GetInstanceIDFromPathAndFileID(kEditorResourcePath, 1);
PPtr<AssetBundle> res (instanceID);
return res;
}
#endif
|