/
Scripting.cpp
1132 lines (993 loc) · 35.6 KB
/
Scripting.cpp
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#include "BinaryModule.h"
#include "Scripting.h"
#include "ScriptingType.h"
#include "FlaxEngine.Gen.h"
#include "Engine/Threading/Threading.h"
#include "Engine/Threading/ThreadLocal.h"
#include "Engine/Threading/IRunnable.h"
#include "Engine/Platform/FileSystem.h"
#include "Engine/Platform/File.h"
#include "Engine/Debug/DebugLog.h"
#if USE_EDITOR
#include "Engine/Level/Level.h"
#include "Editor/Scripting/ScriptsBuilder.h"
#endif
#include "ManagedCLR/MAssembly.h"
#include "ManagedCLR/MClass.h"
#include "ManagedCLR/MMethod.h"
#include "ManagedCLR/MDomain.h"
#include "ManagedCLR/MCore.h"
#include "ManagedCLR/MException.h"
#include "Internal/StdTypesContainer.h"
#include "Engine/Core/ObjectsRemovalService.h"
#include "Engine/Core/Types/TimeSpan.h"
#include "Engine/Profiler/ProfilerCPU.h"
#include "Engine/Content/Asset.h"
#include "Engine/Content/Content.h"
#include "Engine/Engine/EngineService.h"
#include "Engine/Engine/Globals.h"
#include "Engine/Graphics/RenderTask.h"
#include "Engine/Platform/MemoryStats.h"
#include "Engine/Serialization/JsonTools.h"
extern void registerFlaxEngineInternalCalls();
class ScriptingService : public EngineService
{
public:
ScriptingService()
: EngineService(TEXT("Scripting"), -20)
{
}
bool Init() override;
void Update() override;
void LateUpdate() override;
void FixedUpdate() override;
void LateFixedUpdate() override;
void Draw() override;
void BeforeExit() override;
void Dispose() override;
};
namespace
{
MDomain* _rootDomain = nullptr;
MDomain* _scriptsDomain = nullptr;
CriticalSection _objectsLocker;
#define USE_OBJECTS_DISPOSE_CRASHES_DEBUGGING 0
#if USE_OBJECTS_DISPOSE_CRASHES_DEBUGGING
struct ScriptingObjectData
{
ScriptingObject* Ptr;
StringAnsi TypeName;
ScriptingObjectData()
{
Ptr = nullptr;
}
ScriptingObjectData(ScriptingObject* ptr)
{
Ptr = ptr;
if (ptr && ptr->GetTypeHandle() && ptr->GetTypeHandle().TypeIndex < ptr->GetTypeHandle().Module->Types.Count())
TypeName = ptr->GetType().Fullname;
}
ScriptingObject* operator->() const
{
return Ptr;
}
explicit operator ScriptingObject*()
{
return Ptr;
}
operator ScriptingObject*() const
{
return Ptr;
}
};
Dictionary<Guid, ScriptingObjectData> _objectsDictionary(1024 * 16);
#else
Dictionary<Guid, ScriptingObject*> _objectsDictionary(1024 * 16);
#endif
bool _isEngineAssemblyLoaded = false;
bool _hasGameModulesLoaded = false;
MMethod* _method_Update = nullptr;
MMethod* _method_LateUpdate = nullptr;
MMethod* _method_FixedUpdate = nullptr;
MMethod* _method_LateFixedUpdate = nullptr;
MMethod* _method_Draw = nullptr;
MMethod* _method_Exit = nullptr;
Dictionary<StringAnsi, BinaryModule*, InlinedAllocation<64>> _nonNativeModules;
#if USE_EDITOR
bool LastBinariesLoadTriggeredCompilation = false;
#endif
}
Delegate<BinaryModule*> Scripting::BinaryModuleLoaded;
Action Scripting::ScriptsLoaded;
Action Scripting::ScriptsUnload;
Action Scripting::ScriptsReloading;
Action Scripting::ScriptsReloaded;
ThreadLocal<Scripting::IdsMappingTable*, PLATFORM_THREADS_LIMIT, true> Scripting::ObjectsLookupIdMapping;
ScriptingService ScriptingServiceInstance;
bool initFlaxEngine();
// Assembly events
void onEngineLoaded(MAssembly* assembly);
void onEngineUnloading(MAssembly* assembly);
bool ScriptingService::Init()
{
const auto startTime = DateTime::NowUTC();
// Initialize managed runtime
if (MCore::LoadEngine())
{
LOG(Fatal, "C# runtime initialization failed.");
return true;
}
// Cache root domain
_rootDomain = MCore::GetRootDomain();
#if USE_SCRIPTING_SINGLE_DOMAIN
// Use single root domain
auto domain = _rootDomain;
#else
// Create Mono domain for scripts
auto domain = MCore::CreateDomain("Scripts Domain");
#endif
domain->SetCurrentDomain(true);
_scriptsDomain = domain;
// Add internal calls
registerFlaxEngineInternalCalls();
// Load assemblies
if (Scripting::Load())
{
LOG(Fatal, "Scripting Engine initialization failed.");
return true;
}
auto endTime = DateTime::NowUTC();
LOG(Info, "Scripting Engine initializated! (time: {0}ms)", (int32)((endTime - startTime).GetTotalMilliseconds()));
return false;
}
#if COMPILE_WITHOUT_CSHARP
#define INVOKE_EVENT(name)
#else
#define INVOKE_EVENT(name) \
if (!_isEngineAssemblyLoaded) return; \
if (_method_##name == nullptr) \
{ \
MClass* mclass = Scripting::GetStaticClass(); \
if (mclass == nullptr) \
{ \
LOG(Fatal, "Missing Scripting class."); \
return; \
} \
_method_##name = mclass->GetMethod("Internal_" #name, 0); \
if (_method_##name == nullptr) \
{ \
LOG(Fatal, "Missing Scripting method " #name "."); \
return; \
} \
} \
MObject* exception = nullptr; \
_method_##name->Invoke(nullptr, nullptr, &exception); \
DebugLog::LogException(exception)
#endif
void ScriptingService::Update()
{
PROFILE_CPU_NAMED("Scripting::Update");
INVOKE_EVENT(Update);
}
void ScriptingService::LateUpdate()
{
PROFILE_CPU_NAMED("Scripting::LateUpdate");
INVOKE_EVENT(LateUpdate);
}
void ScriptingService::FixedUpdate()
{
PROFILE_CPU_NAMED("Scripting::FixedUpdate");
INVOKE_EVENT(FixedUpdate);
}
void ScriptingService::LateFixedUpdate()
{
PROFILE_CPU_NAMED("Scripting::LateFixedUpdate");
INVOKE_EVENT(LateFixedUpdate);
}
void ScriptingService::Draw()
{
PROFILE_CPU_NAMED("Scripting::Draw");
INVOKE_EVENT(Draw);
}
void ScriptingService::BeforeExit()
{
PROFILE_CPU_NAMED("Scripting::BeforeExit");
INVOKE_EVENT(Exit);
}
MDomain* Scripting::GetRootDomain()
{
return _rootDomain;
}
MDomain* Scripting::GetScriptsDomain()
{
return _scriptsDomain;
}
void Scripting::ProcessBuildInfoPath(String& path, const String& projectFolderPath)
{
if (path.IsEmpty())
return;
if (path.StartsWith(TEXT("$(EnginePath)")))
path = Globals::StartupFolder / path.Substring(14);
else if (path.StartsWith(TEXT("$(ProjectPath)")))
path = projectFolderPath / path.Substring(14);
else if (FileSystem::IsRelative(path))
path = projectFolderPath / path;
}
bool Scripting::LoadBinaryModules(const String& path, const String& projectFolderPath)
{
PROFILE_CPU_NAMED("LoadBinaryModules");
LOG(Info, "Loading binary modules from build info file {0}", path);
// Read file contents
Array<byte> fileData;
if (File::ReadAllBytes(path, fileData))
{
LOG(Error, "Failed to read file contents.");
return true;
}
// Parse Json data
rapidjson_flax::Document document;
{
PROFILE_CPU_NAMED("Json.Parse");
document.Parse((char*)fileData.Get(), fileData.Count());
}
if (document.HasParseError())
{
LOG(Error, "Failed to file contents.");
return true;
}
// TODO: validate Name, Platform, Architecture, Configuration from file
// Load all references
auto referencesMember = document.FindMember("References");
if (referencesMember != document.MemberEnd() && referencesMember->value.IsArray())
{
auto& referencesArray = referencesMember->value;
for (rapidjson::SizeType i = 0; i < referencesArray.Size(); i++)
{
auto& reference = referencesArray[i];
String referenceProjectPath = JsonTools::GetString(reference, "ProjectPath", String::Empty);
if (referenceProjectPath == TEXT("$(EnginePath)/Flax.flaxproj"))
continue; // Skip reference to engine
String referencePath = JsonTools::GetString(reference, "Path", String::Empty);
if (referenceProjectPath.IsEmpty() || referencePath.IsEmpty())
{
LOG(Error, "Empty reference.");
return true;
}
ProcessBuildInfoPath(referenceProjectPath, projectFolderPath);
ProcessBuildInfoPath(referencePath, projectFolderPath);
String referenceProjectFolderPath = StringUtils::GetDirectoryName(referenceProjectPath);
if (LoadBinaryModules(referencePath, referenceProjectFolderPath))
{
LOG(Error, "Failed to load reference.");
return true;
}
}
}
// Load all binary modules
auto binaryModulesMember = document.FindMember("BinaryModules");
if (binaryModulesMember != document.MemberEnd() && binaryModulesMember->value.IsArray())
{
auto& binaryModulesArray = binaryModulesMember->value;
for (rapidjson::SizeType i = 0; i < binaryModulesArray.Size(); i++)
{
auto& binaryModule = binaryModulesArray[i];
const auto nameMember = binaryModule.FindMember("Name");
if (nameMember == binaryModule.MemberEnd())
{
LOG(Error, "Failed to process file.");
return true;
}
String name = nameMember->value.GetText();
StringAnsi nameAnsi(nameMember->value.GetString(), nameMember->value.GetStringLength());
String nativePath = JsonTools::GetString(binaryModule, "NativePath", String::Empty);
String managedPath = JsonTools::GetString(binaryModule, "ManagedPath", String::Empty);
ProcessBuildInfoPath(nativePath, projectFolderPath);
ProcessBuildInfoPath(managedPath, projectFolderPath);
LOG(Info, "Loading binary module {0}", name);
// Check if that module has been already registered
BinaryModule* module = BinaryModule::GetModule(nameAnsi);
if (!module)
_nonNativeModules.TryGet(nameAnsi, module);
if (!module)
{
// C++
if (nativePath.HasChars())
{
// Check if this module has been statically linked
auto& staticallyLinkedBinaryModules = StaticallyLinkedBinaryModuleInitializer::GetStaticallyLinkedBinaryModules();
for (auto getter : staticallyLinkedBinaryModules)
{
module = getter();
if (module && module->GetName() == nameAnsi)
break;
module = nullptr;
}
if (!module)
{
// Load library
const auto startTime = DateTime::NowUTC();
#if PLATFORM_ANDROID || PLATFORM_MAC
// On some platforms all native binaries are side-by-side with the app in a different folder
if (!FileSystem::FileExists(nativePath))
{
nativePath = String(StringUtils::GetDirectoryName(Platform::GetExecutableFilePath())) / StringUtils::GetFileName(nativePath);
}
#elif PLATFORM_IOS
// iOS uses Frameworks folder with native binaries
if (!FileSystem::FileExists(nativePath))
{
nativePath = Globals::ProjectFolder / TEXT("Frameworks") / StringUtils::GetFileName(nativePath);
}
#endif
auto library = Platform::LoadLibrary(nativePath.Get());
if (!library)
{
LOG(Error, "Failed to load library '{0}' for binary module {1}.", nativePath, name);
return true;
}
char getBinaryFuncName[512];
StringAnsiView getBinaryFuncNamePrefix("GetBinaryModule");
ASSERT(getBinaryFuncNamePrefix.Length() + nameAnsi.Length() < ARRAY_COUNT(getBinaryFuncName));
Platform::MemoryCopy(getBinaryFuncName, getBinaryFuncNamePrefix.Get(), getBinaryFuncNamePrefix.Length());
Platform::MemoryCopy(getBinaryFuncName + getBinaryFuncNamePrefix.Length(), nameAnsi.Get(), nameAnsi.Length());
*(getBinaryFuncName + getBinaryFuncNamePrefix.Length() + nameAnsi.Length()) = '\0';
const auto getBinaryFunc = (GetBinaryModuleFunc)Platform::GetProcAddress(library, getBinaryFuncName);
if (!getBinaryFunc)
{
Platform::FreeLibrary(library);
LOG(Error, "Failed to setup library '{0}' for binary module {1}.", nativePath, name);
return true;
}
const auto endTime = DateTime::NowUTC();
LOG(Info, "Module {0} loaded in {1}ms", name, (int32)(endTime - startTime).GetTotalMilliseconds());
// Get the binary module
module = getBinaryFunc();
if (!module)
{
Platform::FreeLibrary(library);
LOG(Error, "Failed to get binary module {0}.", name);
return true;
}
((NativeBinaryModule*)module)->Library = library;
}
}
else
{
// Create module if native library is not used
module = New<ManagedBinaryModule>(nameAnsi);
_nonNativeModules.Add(nameAnsi, module);
}
}
#if !COMPILE_WITHOUT_CSHARP
// C#
if (managedPath.HasChars() && !((ManagedBinaryModule*)module)->Assembly->IsLoaded())
{
if (((ManagedBinaryModule*)module)->Assembly->Load(managedPath, nativePath))
{
LOG(Error, "Failed to load C# assembly '{0}' for binary module {1}.", managedPath, name);
return true;
}
}
#endif
BinaryModuleLoaded(module);
}
}
return false;
}
bool Scripting::Load()
{
PROFILE_CPU();
// Note: this action can be called from main thread (due to Mono problems with assemblies actions from other threads)
ASSERT(IsInMainThread());
#if USE_CSHARP
// Load C# core assembly
ManagedBinaryModule* corlib = GetBinaryModuleCorlib();
if (corlib->Assembly->LoadCorlib())
{
LOG(Error, "Failed to load corlib C# assembly.");
return true;
}
// Initialize C# corelib types
{
const auto& corlibClasses = corlib->Assembly->GetClasses();
bool gotAll = true;
#define CACHE_CORLIB_CLASS(var, name) gotAll &= corlibClasses.TryGet(StringAnsiView(name), MCore::TypeCache::var)
CACHE_CORLIB_CLASS(Void, "System.Void");
CACHE_CORLIB_CLASS(Object, "System.Object");
CACHE_CORLIB_CLASS(Byte, "System.Byte");
CACHE_CORLIB_CLASS(Boolean, "System.Boolean");
CACHE_CORLIB_CLASS(SByte, "System.SByte");
CACHE_CORLIB_CLASS(Char, "System.Char");
CACHE_CORLIB_CLASS(Int16, "System.Int16");
CACHE_CORLIB_CLASS(UInt16, "System.UInt16");
CACHE_CORLIB_CLASS(Int32, "System.Int32");
CACHE_CORLIB_CLASS(UInt32, "System.UInt32");
CACHE_CORLIB_CLASS(Int64, "System.Int64");
CACHE_CORLIB_CLASS(UInt64, "System.UInt64");
CACHE_CORLIB_CLASS(IntPtr, "System.IntPtr");
CACHE_CORLIB_CLASS(UIntPtr, "System.UIntPtr");
CACHE_CORLIB_CLASS(Single, "System.Single");
CACHE_CORLIB_CLASS(Double, "System.Double");
CACHE_CORLIB_CLASS(String, "System.String");
#undef CACHE_CORLIB_CLASS
if (!gotAll)
{
LOG(Error, "Failed to load corlib C# assembly.");
for (const auto& e : corlibClasses)
LOG(Info, "Class: {0}", String(e.Value->GetFullName()));
return true;
}
}
#endif
// Load FlaxEngine
const String flaxEnginePath = Globals::BinariesFolder / TEXT("FlaxEngine.CSharp.dll");
auto* flaxEngineModule = (NativeBinaryModule*)GetBinaryModuleFlaxEngine();
if (!flaxEngineModule->Assembly->IsLoaded())
{
if (flaxEngineModule->Assembly->Load(flaxEnginePath))
{
LOG(Error, "Failed to load FlaxEngine C# assembly.");
return true;
}
onEngineLoaded(flaxEngineModule->Assembly);
// Insert type aliases for vector types that don't exist in C++ but are just typedef (properly redirect them to actual types)
// TODO: add support for automatic typedef aliases setup for scripting module to properly lookup type from the alias typename
#if USE_LARGE_WORLDS
flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Vector2"] = flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Double2"];
flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Vector3"] = flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Double3"];
flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Vector4"] = flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Double4"];
#else
flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Vector2"] = flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Float2"];
flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Vector3"] = flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Float3"];
flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Vector4"] = flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Float4"];
#endif
#if USE_CSHARP
flaxEngineModule->ClassToTypeIndex[flaxEngineModule->Assembly->GetClass("FlaxEngine.Vector2")] = flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Vector2"];
flaxEngineModule->ClassToTypeIndex[flaxEngineModule->Assembly->GetClass("FlaxEngine.Vector3")] = flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Vector3"];
flaxEngineModule->ClassToTypeIndex[flaxEngineModule->Assembly->GetClass("FlaxEngine.Vector4")] = flaxEngineModule->TypeNameToTypeIndex["FlaxEngine.Vector4"];
#endif
}
#if USE_EDITOR
// Skip loading game modules in Editor on startup - Editor loads them later during splash screen (eg. after first compilation)
static bool SkipFirstLoad = true;
if (SkipFirstLoad)
{
SkipFirstLoad = false;
return false;
}
// Flax.Build outputs the <target>.Build.json with binary modules to use for game scripting
const Char *target, *platform, *architecture, *configuration;
ScriptsBuilder::GetBinariesConfiguration(target, platform, architecture, configuration);
if (StringUtils::Length(target) == 0)
{
LOG(Info, "Missing EditorTarget in project. Not using game script modules.");
_hasGameModulesLoaded = true;
return false;
}
const String targetBuildInfo = Globals::ProjectFolder / TEXT("Binaries") / target / platform / architecture / configuration / target + TEXT(".Build.json");
// Call compilation if game target build info is missing
if (!FileSystem::FileExists(targetBuildInfo))
{
LOG(Info, "Missing target build info ({0})", targetBuildInfo);
if (LastBinariesLoadTriggeredCompilation)
return false;
LastBinariesLoadTriggeredCompilation = true;
ScriptsBuilder::Compile();
return false;
}
#else
const String targetBuildInfo = Globals::BinariesFolder / TEXT("Game.Build.json");
#endif
// Load game binary modules
if (LoadBinaryModules(targetBuildInfo, Globals::ProjectFolder))
{
LOG(Error, "Failed to load Game assemblies.");
return true;
}
_hasGameModulesLoaded = true;
// End
ScriptsLoaded();
return false;
}
void Scripting::Release()
{
PROFILE_CPU();
// Note: this action can be called from main thread (due to Mono problems with assemblies actions from other threads)
ASSERT(IsInMainThread());
// Fire event
ScriptsUnload();
// Cleanup
ObjectsRemovalService::Flush();
// Cleanup some managed objects
MCore::GC::Collect();
MCore::GC::WaitForPendingFinalizers();
// Release managed objects instances for persistent objects (assets etc.)
_objectsLocker.Lock();
{
for (auto i = _objectsDictionary.Begin(); i.IsNotEnd(); ++i)
{
auto obj = i->Value;
#if USE_OBJECTS_DISPOSE_CRASHES_DEBUGGING
LOG(Info, "[OnScriptingDispose] obj = 0x{0:x}, {1}", (uint64)obj.Ptr, String(obj.TypeName));
#endif
obj->OnScriptingDispose();
}
}
_objectsLocker.Unlock();
// Release assets sourced from game assemblies
const auto flaxModule = GetBinaryModuleFlaxEngine();
for (auto asset : Content::GetAssets())
{
if (asset->GetTypeHandle().Module == flaxModule)
continue;
asset->DeleteObjectNow();
}
auto* flaxEngineModule = (NativeBinaryModule*)GetBinaryModuleFlaxEngine();
onEngineUnloading(flaxEngineModule->Assembly);
// Unload assemblies (from back to front)
{
LOG(Info, "Unloading binary modules");
auto modules = BinaryModule::GetModules();
for (int32 i = modules.Count() - 1; i >= 0; i--)
{
auto module = modules[i];
module->Destroy(false);
}
_nonNativeModules.ClearDelete();
_hasGameModulesLoaded = false;
}
// Cleanup
MCore::GC::Collect();
MCore::GC::WaitForPendingFinalizers();
// Flush objects
ObjectsRemovalService::Flush();
// Switch domain
auto rootDomain = MCore::GetRootDomain();
if (rootDomain)
{
if (!rootDomain->SetCurrentDomain(false))
{
LOG(Error, "Failed to set current domain to root");
}
}
#if !USE_SCRIPTING_SINGLE_DOMAIN
MCore::UnloadDomain("Scripts Domain");
#endif
}
#if USE_EDITOR
void Scripting::Reload(bool canTriggerSceneReload)
{
// By default we allow to call it only from the main thread and when no scene is loaded.
// Otherwise call scene manager to perform clear scripts reload.
// It will call this method back on main thread without scenes loaded, see SceneActionType::ReloadScripts.
if (!IsInMainThread() || Level::IsAnySceneLoaded())
{
if (canTriggerSceneReload)
{
// Call scene to reload scripts
Level::ReloadScriptsAsync();
}
else
{
LOG(Warning, "Cannot reload scene on scripting reload. Flag is not set.");
}
return;
}
PROFILE_CPU();
// Ideally we would call Release and Load but this would also reload Editor objects which we want avoid
// Editor is also referencing assets and other managed objects so we should force reload everything.
// However Reload is called when no scene is loaded.
// Faster path - if no game assembly loaded yet
if (!_hasGameModulesLoaded)
{
// Just load missing assemblies
Load();
return;
}
LOG(Info, "Start user scripts reload");
ScriptsReloading();
// Flush cache (some objects may be deleted after reload start event)
ObjectsRemovalService::Flush();
// Give GC a try to cleanup old user objects and the other mess
MCore::GC::Collect();
MCore::GC::WaitForPendingFinalizers();
// Destroy objects from game assemblies (eg. not released objects that might crash if persist in memory after reload)
const auto flaxModule = GetBinaryModuleFlaxEngine();
_objectsLocker.Lock();
{
for (auto i = _objectsDictionary.Begin(); i.IsNotEnd(); ++i)
{
auto obj = i->Value;
if (obj->GetTypeHandle().Module == flaxModule)
continue;
#if USE_OBJECTS_DISPOSE_CRASHES_DEBUGGING
LOG(Info, "[OnScriptingDispose] obj = 0x{0:x}, {1}", (uint64)obj.Ptr, String(obj.TypeName));
#endif
obj->OnScriptingDispose();
}
}
_objectsLocker.Unlock();
// Release assets sourced from game assemblies
for (auto asset : Content::GetAssets())
{
if (asset->GetTypeHandle().Module == flaxModule)
continue;
asset->DeleteObjectNow();
}
// Unload all game modules
LOG(Info, "Unloading game binary modules");
auto modules = BinaryModule::GetModules();
for (int32 i = modules.Count() - 1; i >= 0; i--)
{
BinaryModule* module = modules[i];
if (module == GetBinaryModuleCorlib() || module == GetBinaryModuleFlaxEngine())
continue;
module->Destroy(true);
}
modules.Clear();
_nonNativeModules.ClearDelete();
_hasGameModulesLoaded = false;
// Release and create a new assembly load context for user assemblies
MCore::ReloadScriptingAssemblyLoadContext();
// Give GC a try to cleanup old user objects and the other mess
MCore::GC::Collect();
MCore::GC::WaitForPendingFinalizers();
// Load all game modules
if (Load())
{
LOG(Error, "User assemblies reload failed.");
}
ScriptsReloaded();
LOG(Info, "End user scripts reload");
}
#endif
MClass* Scripting::FindClass(const StringAnsiView& fullname)
{
if (fullname.IsEmpty())
return nullptr;
PROFILE_CPU();
auto& modules = BinaryModule::GetModules();
for (auto module : modules)
{
auto managedModule = dynamic_cast<ManagedBinaryModule*>(module);
if (managedModule && managedModule->Assembly->IsLoaded())
{
MClass* result = managedModule->Assembly->GetClass(fullname);
if (result != nullptr)
return result;
}
}
return nullptr;
}
ScriptingTypeHandle Scripting::FindScriptingType(const StringAnsiView& fullname)
{
if (fullname.IsEmpty())
return ScriptingTypeHandle();
PROFILE_CPU();
auto& modules = BinaryModule::GetModules();
for (auto module : modules)
{
int32 typeIndex;
if (module->FindScriptingType(fullname, typeIndex))
{
return ScriptingTypeHandle(module, typeIndex);
}
}
return ScriptingTypeHandle();
}
ScriptingObject* Scripting::NewObject(const ScriptingTypeHandle& type)
{
if (!type)
{
LOG(Error, "Invalid type.");
return nullptr;
}
const ScriptingType& scriptingType = type.GetType();
// Create unmanaged object
const ScriptingObjectSpawnParams params(Guid::New(), type);
ScriptingObject* obj = scriptingType.Script.Spawn(params);
if (obj == nullptr)
LOG(Error, "Failed to spawn object of type \'{0}\'.", scriptingType.ToString());
return obj;
}
ScriptingObject* Scripting::NewObject(const MClass* type)
{
if (type == nullptr)
{
LOG(Error, "Invalid type.");
return nullptr;
}
#if USE_CSHARP
// Get the assembly with that class
auto module = ManagedBinaryModule::FindModule(type);
if (module == nullptr)
{
LOG(Error, "Cannot find scripting assembly for type \'{0}\'.", String(type->GetFullName()));
return nullptr;
}
// Try to find the scripting type for this class
int32 typeIndex;
if (!module->ClassToTypeIndex.TryGet(type, typeIndex))
{
LOG(Error, "Cannot spawn objects of type \'{0}\'.", String(type->GetFullName()));
return nullptr;
}
const ScriptingType& scriptingType = module->Types[typeIndex];
// Create unmanaged object
const ScriptingObjectSpawnParams params(Guid::New(), ScriptingTypeHandle(module, typeIndex));
ScriptingObject* obj = scriptingType.Script.Spawn(params);
if (obj == nullptr)
LOG(Error, "Failed to spawn object of type \'{0}\'.", scriptingType.ToString());
return obj;
#else
LOG(Error, "Not supported object creation from Managed class.");
return nullptr;
#endif
}
FLAXENGINE_API ScriptingObject* FindObject(const Guid& id, MClass* type)
{
return Scripting::FindObject(id, type);
}
void ScriptingObjectReferenceBase::OnSet(ScriptingObject* object)
{
auto e = _object;
if (e != object)
{
if (e)
e->Deleted.Unbind<ScriptingObjectReferenceBase, &ScriptingObjectReferenceBase::OnDeleted>(this);
_object = e = object;
if (e)
e->Deleted.Bind<ScriptingObjectReferenceBase, &ScriptingObjectReferenceBase::OnDeleted>(this);
Changed();
}
}
void ScriptingObjectReferenceBase::OnDeleted(ScriptingObject* obj)
{
ASSERT(_object == obj);
_object->Deleted.Unbind<ScriptingObjectReferenceBase, &ScriptingObjectReferenceBase::OnDeleted>(this);
_object = nullptr;
Changed();
}
ScriptingObject* Scripting::FindObject(Guid id, MClass* type)
{
if (!id.IsValid())
return nullptr;
PROFILE_CPU();
// Try to map object id
const auto idsMapping = ObjectsLookupIdMapping.Get();
if (idsMapping)
{
idsMapping->TryGet(id, id);
}
// Try to find it
#if USE_OBJECTS_DISPOSE_CRASHES_DEBUGGING
ScriptingObjectData data;
_objectsLocker.Lock();
_objectsDictionary.TryGet(id, data);
_objectsLocker.Unlock();
auto result = data.Ptr;
#else
ScriptingObject* result = nullptr;
_objectsLocker.Lock();
_objectsDictionary.TryGet(id, result);
_objectsLocker.Unlock();
#endif
if (result)
{
// Check type
if (!type || result->Is(type))
return result;
LOG(Warning, "Found scripting object with ID={0} of type {1} that doesn't match type {2}.", id, String(result->GetType().Fullname), String(type->GetFullName()));
return nullptr;
}
// Check if object can be an asset and try to load it
if (!type)
{
result = Content::LoadAsync<Asset>(id);
if (!result)
LOG(Warning, "Unable to find scripting object with ID={0}", id);
return result;
}
if (type == ScriptingObject::GetStaticClass() || type->IsSubClassOf(Asset::GetStaticClass()))
{
Asset* asset = Content::LoadAsync(id, type);
if (asset)
return asset;
}
LOG(Warning, "Unable to find scripting object with ID={0}. Required type {1}.", id, String(type->GetFullName()));
return nullptr;
}
ScriptingObject* Scripting::TryFindObject(Guid id, MClass* type)
{
if (!id.IsValid())
return nullptr;
PROFILE_CPU();
// Try to map object id
const auto idsMapping = ObjectsLookupIdMapping.Get();
if (idsMapping)
{
idsMapping->TryGet(id, id);
}
// Try to find it
#if USE_OBJECTS_DISPOSE_CRASHES_DEBUGGING
ScriptingObjectData data;
_objectsLocker.Lock();
_objectsDictionary.TryGet(id, data);
_objectsLocker.Unlock();
auto result = data.Ptr;
#else
ScriptingObject* result = nullptr;
_objectsLocker.Lock();
_objectsDictionary.TryGet(id, result);
_objectsLocker.Unlock();
#endif
// Check type
if (result && type && !result->Is(type))
{
result = nullptr;
}
return result;
}
ScriptingObject* Scripting::TryFindObject(MClass* type)
{
if (type == nullptr)
return nullptr;
ScopeLock lock(_objectsLocker);
for (auto i = _objectsDictionary.Begin(); i.IsNotEnd(); ++i)
{
const auto obj = i->Value;
if (obj->GetClass() == type)
return obj;
}
return nullptr;
}
ScriptingObject* Scripting::FindObject(const MObject* managedInstance)
{
if (managedInstance == nullptr)
return nullptr;
PROFILE_CPU();
// TODO: optimize it by reading the unmanagedPtr or _internalId from managed Object property
ScopeLock lock(_objectsLocker);
for (auto i = _objectsDictionary.Begin(); i.IsNotEnd(); ++i)
{
const auto obj = i->Value;
if (obj->GetManagedInstance() == managedInstance)
return obj;
}
return nullptr;
}
void Scripting::OnManagedInstanceDeleted(ScriptingObject* obj)
{
PROFILE_CPU();
ASSERT(obj);
// Validate if object still exists
_objectsLocker.Lock();
if (_objectsDictionary.ContainsValue(obj))
{
#if USE_OBJECTS_DISPOSE_CRASHES_DEBUGGING
LOG(Info, "[OnManagedInstanceDeleted] obj = 0x{0:x}, {1}", (uint64)obj, String(ScriptingObjectData(obj).TypeName));
#endif
obj->OnManagedInstanceDeleted();
}
else
{
//LOG(Warning, "Object finalization called for already removed object (address={0:x})", (uint64)obj);
}
_objectsLocker.Unlock();
}
bool Scripting::HasGameModulesLoaded()
{
return _hasGameModulesLoaded;
}