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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
#include "UnityPrefix.h"
#if ENABLE_SCRIPTING
#include "MonoScript.h"
#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h"
#include "Runtime/Serialize/TransferUtility.h"
#include "MonoManager.h"
#include "Runtime/Scripting/Backend/ScriptingBackendApi.h"
#include "MonoScriptCache.h"
#include "MonoBehaviour.h"
#if UNITY_FLASH || UNITY_WINRT
#include "Runtime/Scripting/Backend/ScriptingTypeRegistry.h"
#endif
const char* kBuildLogicGraphDllFile = "Temp/StagingArea/Data/Managed/LogicGraphs.dll";
string GetGeneratedLogicGraphsDllName()
{
return GetLastPathNameComponent(kBuildLogicGraphDllFile);
}
using namespace std;
#if UNITY_EDITOR
static UInt32 ComputeTypeTreeHashForScriptClass (MonoScript* script)
{
if (script->GetClass () == NULL
|| !IsValidScriptType (script->GetScriptType ())
|| script->IsEditorScript ())
return 0;
// We don't want to call the constructor as that may run user code and
// may have all kinds of side-effects. All we need is an instance for
// running the TypeTree generation/serialization code on.
ScriptingObjectPtr instance = scripting_object_new (script->GetClass ());
if (!instance)
return 0;
// Create a temporary MonoBehaviour that we can run ProxyTransfer on.
MonoBehaviour* dummyInstance = NEW_OBJECT (MonoBehaviour);
dummyInstance->Reset ();
dummyInstance->HackSetAwakeWasCalled ();
// Connect MonoBehaviour to script class and to the object instance
// we have created.
dummyInstance->SetScript (script, instance);
// Generate type tree.
TypeTree typeTree;
GenerateTypeTree (*dummyInstance, &typeTree);
DestroySingleObject (dummyInstance);
// Compute hash of tree.
return HashTypeTree (typeTree);
}
#endif // UNITY_EDITOR
MonoScript::MonoScript (MemLabelId label, ObjectCreationMode mode)
: Super(label, mode)
{
m_PropertiesHash = 0;
m_ScriptCache = NULL;
m_IsEditorScript = false;
m_ExecutionOrder = 0;
}
MonoScript::~MonoScript ()
{
CleanupScriptCache();
}
UInt32 MonoScript::GetPropertiesHash ()
{
// In the editor, compute the property hash on demand based on the type
// tree generated by the MonoBehaviour serialization code.
#if UNITY_EDITOR
if (m_PropertiesHash == 0)
m_PropertiesHash = ComputeTypeTreeHashForScriptClass (this);
#endif
return m_PropertiesHash;
}
UnityStr MonoScript::GetScriptFullClassName () const
{
if (m_Namespace.empty())
return m_ClassName;
else
return m_Namespace + "." + m_ClassName;
}
ScriptingClassPtr MonoScript::GetClass ()
{
if (m_ScriptCache)
return m_ScriptCache->klass;
else
return NULL;
}
ScriptingMethodPtr MonoScript::FindMethod (const char* name)
{
if (m_ScriptCache)
return ::FindMethod(*m_ScriptCache, name);
else
return NULL;
}
MonoScriptType MonoScript::GetScriptType() const
{
if (m_ScriptCache == NULL)
return kScriptTypeNotInitialized;
else
return m_ScriptCache->scriptType;
}
void MonoScript::CleanupScriptCache ()
{
if (m_ScriptCache != NULL)
{
const_cast<MonoScriptCache*> (m_ScriptCache)->Release();
m_ScriptCache = NULL;
}
}
#if ENABLE_SCRIPTING
void MonoScript::Rebuild (ScriptingTypePtr klass)
{
CleanupScriptCache ();
#if UNITY_EDITOR
m_PropertiesHash = 0;
#endif
m_ScriptCache = CreateMonoScriptCache (klass, m_IsEditorScript, this);
}
#endif
void MonoScript::RebuildFromAwake()
{
#if ENABLE_MONO
Rebuild (GetMonoManager().GetMonoClassWithAssemblyName (GetScriptClassName (), GetNameSpace(), GetAssemblyName ()));
#elif UNITY_FLASH || UNITY_WINRT
ScriptingTypePtr t = GetScriptingTypeRegistry().GetType(GetNameSpace().c_str(), GetScriptClassName().c_str());
Rebuild(t);
#endif
}
// Used by WinRT & Flash
UnityStr MonoScript::GetScriptName()
{
UnityStr s = GetNameSpace();
if (s.size() > 0)
s += "." + GetScriptClassName();
else
s = GetScriptClassName();
return s;
}
void MonoScript::AwakeFromLoad (AwakeFromLoadMode awakeMode)
{
Super::AwakeFromLoad (awakeMode);
if ((awakeMode & kDidLoadThreaded) == 0)
{
RebuildFromAwake();
}
}
void MonoScript::AwakeFromLoadThreaded ()
{
Super::AwakeFromLoadThreaded();
RebuildFromAwake();
}
template<class TransferFunction>
void MonoScript::TransferPropertiesHash (TransferFunction& transfer)
{
#if UNITY_EDITOR
if (transfer.IsWritingGameReleaseData())
{
// Force building the properties hash
GetPropertiesHash();
}
#endif
// When reading or writing for game release
// Transfer properties hash
if (transfer.IsSerializingForGameRelease ())
transfer.Transfer (m_PropertiesHash, "m_PropertiesHash", kNotEditableMask);
}
#if UNITY_EDITOR
// Temporarily (When building the player during the write operation).
// Sets the correct assembly of the MonoScript, and restores it afterwards.
struct RemapAssemblyDuringBuild
{
MonoScript* m_Script;
string m_AssemblyName;
string m_Namespace;
string m_ClassName;
RemapAssemblyDuringBuild (MonoScript& inScript, bool logicGraphRelease)
: m_Script (NULL)
{
if (!logicGraphRelease)
return;
m_Script = &inScript;
m_AssemblyName = m_Script->GetAssemblyName();
m_Namespace = m_Script->GetNameSpace();
m_ClassName = m_Script->GetScriptClassName();
m_Script->m_AssemblyName = GetGeneratedLogicGraphsDllName();
m_Script->m_Namespace = "";
m_Script->m_ClassName = m_Script->m_EditorGraphData->GetName();
}
~RemapAssemblyDuringBuild ()
{
if (m_Script == NULL)
return;
m_Script->m_AssemblyName = m_AssemblyName;
m_Script->m_Namespace = m_Namespace;
m_Script->m_ClassName = m_ClassName;
}
};
#endif
template<class TransferFunction>
void MonoScript::Transfer (TransferFunction& transfer)
{
// Don't transfer script class since we already transfer the script property ourselves.
Super::Super::Transfer (transfer);
transfer.SetVersion(4);
#if UNITY_EDITOR
bool logicGraphRelease = transfer.IsWritingGameReleaseData() && !m_EditorGraphData.IsNull();
RemapAssemblyDuringBuild remap (*this, logicGraphRelease);
if (!transfer.IsSerializingForGameRelease ())
{
transfer.Transfer (m_Script, "m_Script", kHideInEditorMask);
transfer.Transfer (m_DefaultReferences, "m_DefaultReferences", kHideInEditorMask);
transfer.Transfer (m_Icon, "m_Icon", kNoTransferFlags);
TRANSFER (m_EditorGraphData);
}
#endif
transfer.Transfer (m_ExecutionOrder, "m_ExecutionOrder", kNotEditableMask);
TransferPropertiesHash (transfer);
transfer.Transfer (m_ClassName, "m_ClassName", kNotEditableMask);
transfer.Transfer (m_Namespace, "m_Namespace", kNotEditableMask);
transfer.Transfer (m_AssemblyName, "m_AssemblyName", kNotEditableMask);
transfer.Transfer (m_IsEditorScript, "m_IsEditorScript", kHideInEditorMask);
// AssemblyIdentifier has been removed and is now simply the dll name
if (transfer.IsVersionSmallerOrEqual(1))
{
transfer.Transfer (m_AssemblyName, "m_AssemblyIdentifier", kNotEditableMask);
if (m_AssemblyName == "Unity Engine Special")
{
m_AssemblyName = "UnityEngine.dll";
m_Namespace = "UnityEngine";
}
else if (m_AssemblyName == "Unity Editor Special")
{
m_AssemblyName = "UnityEditor.dll";
m_Namespace = "UnityEditor";
}
else
{
m_AssemblyName = "Assembly - " + m_AssemblyName + ".dll";
}
}
#if UNITY_EDITOR
// In 2.x Assemblies compiled by Unity had spaces in the name.
// In 3.0 we are removing all spaces, since this makes loading assemblies easier and fixes issue on xbox where the filename is too long.
// ("Assembly - UnityScript.dll" -> "Assembly-UnityScript.dll")
if (transfer.IsVersionSmallerOrEqual(2))
{
if (BeginsWith(m_AssemblyName, "Assembly - "))
replace_string(m_AssemblyName, " ", "");
}
#endif
}
bool MonoScript::ShouldIgnoreInGarbageDependencyTracking ()
{
// MonoScript can have referenced
// But it's ok to ignore them eg. references to default references, icons and editor graph
return true;
}
void MonoScript::Init (const ScriptString& script, const string& className, const std::string& nameSpace, const string& identifier, bool isEditorScript)
{
SetScript(script);
m_ClassName = className;
m_Namespace = nameSpace;
m_AssemblyName = identifier;
m_PropertiesHash = 0;
m_IsEditorScript = isEditorScript;
SetDirty ();
}
#if ENABLE_SCRIPTING
void MonoScript::Init (MonoClass* scriptType)
{
m_ClassName = scripting_class_get_name(scriptType);
m_Namespace = scripting_class_get_namespace(scriptType);
m_PropertiesHash = 0;
#if ENABLE_MONO
MonoImage* image = mono_class_get_image(scriptType);
MonoAssemblyName name;
bool success = mono_assembly_fill_assembly_name(image, &name);
if (success)
m_AssemblyName = mono_stringify_assembly_name(&name);
#endif
m_IsEditorScript = 0;
SetDirty ();
}
#endif
std::string MonoScript::GetNameSpace ()const
{
return m_Namespace;
}
#if ENABLE_SCRIPTING
MonoScript* CreateMonoScriptFromScriptingType(ScriptingTypePtr klass)
{
MonoScript* result = NEW_OBJECT(MonoScript);
result->Reset ();
result->Init(klass);
GetMonoScriptManager().RegisterRuntimeScript (*result);
result->AwakeFromLoad(kInstantiateOrCreateFromCodeAwakeFromLoad);
return result;
}
#endif
void MonoScript::SetExecutionOrder (SInt32 executionOrder)
{
m_ExecutionOrder = executionOrder;
SetDirty();
}
#if UNITY_EDITOR
bool MonoScript::IsBuiltinScript() const
{
return m_AssemblyName == "UnityEngine.dll" || m_AssemblyName == "UnityEditor.dll";
}
void MonoScript::SetIcon (PPtr<Object> icon)
{
m_Icon = icon;
// We do not call SetDirty() since MonoScript is generated data (MonoImporter holds the icon state)
}
PPtr<Object> MonoScript::GetIcon () const
{
return m_Icon;
}
void MonoScript::SetEditorGraphData(Object* data)
{
if (m_EditorGraphData != PPtr<Object> (data))
{
m_EditorGraphData = data;
SetDirty();
}
}
PPtr<Object> MonoScript::GetEditorGraphData()
{
return m_EditorGraphData;
}
bool MonoScript::GetScriptTypeWasJustCreatedFromComponentMenu ()
{
if (m_ScriptCache)
return m_ScriptCache->scriptTypeWasJustCreatedFromComponentMenu;
else
return false;
}
void MonoScript::SetScriptTypeWasJustCreatedFromComponentMenu ()
{
if (m_ScriptCache == NULL)
Rebuild(NULL);
const_cast<MonoScriptCache*> (m_ScriptCache)->scriptTypeWasJustCreatedFromComponentMenu = true;
}
std::string BuildScriptClassId(const std::string& assembly, const std::string& ns, const std::string& klass)
{
return assembly + ":" + ns + ":" + klass;
}
void GetScriptClassIdComponents(const std::string& scriptClassId, std::string& assembly, std::string& ns, std::string& klass)
{
std::vector<std::string> parts;
Split(scriptClassId, ":", parts);
assembly = parts[0];
AssertIf(parts.size() != 2 && parts.size() != 3);
// empty namespace
if (parts.size() == 2)
{
ns.clear();
klass = parts[1];
}
else if (parts.size() == 3)
{
ns = parts[1];
klass = parts[2];
}
}
#endif
#if ENABLE_SCRIPTING
IMPLEMENT_CLASS (MonoScript)
IMPLEMENT_OBJECT_SERIALIZE (MonoScript)
#endif
#endif
|