| 550 | #if USE_CSHARP |
| 551 | |
| 552 | DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_Create1(MTypeObject* type) |
| 553 | { |
| 554 | // Peek class for that type (handle generic class cases) |
| 555 | if (!type) |
| 556 | DebugLog::ThrowArgumentNull("type"); |
| 557 | MType* mType = INTERNAL_TYPE_OBJECT_GET(type); |
| 558 | const MTypes mTypeType = MCore::Type::GetType(mType); |
| 559 | if (mTypeType == MTypes::GenericInst) |
| 560 | { |
| 561 | LOG(Error, "Generic scripts are not supported."); |
| 562 | return nullptr; |
| 563 | } |
| 564 | MClass* typeClass = MCore::Type::GetClass(mType); |
| 565 | if (typeClass == nullptr) |
| 566 | { |
| 567 | LOG(Error, "Invalid type."); |
| 568 | return nullptr; |
| 569 | } |
| 570 | |
| 571 | // Get the assembly with that class |
| 572 | auto module = ManagedBinaryModule::FindModule(typeClass); |
| 573 | if (module == nullptr) |
| 574 | { |
| 575 | LOG(Error, "Cannot find scripting assembly for type \'{0}\'.", String(typeClass->GetFullName())); |
| 576 | return nullptr; |
| 577 | } |
| 578 | |
| 579 | // Try to find the scripting type for this class |
| 580 | int32 typeIndex; |
| 581 | if (!module->ClassToTypeIndex.TryGet(typeClass, typeIndex)) |
| 582 | { |
| 583 | LOG(Error, "Cannot spawn objects of type \'{0}\'.", String(typeClass->GetFullName())); |
| 584 | return nullptr; |
| 585 | } |
| 586 | const ScriptingType& scriptingType = module->Types[typeIndex]; |
| 587 | |
| 588 | // Create unmanaged object |
| 589 | const ScriptingObjectSpawnParams params(Guid::New(), ScriptingTypeHandle(module, typeIndex)); |
| 590 | ScriptingObject* obj = scriptingType.Script.Spawn(params); |
| 591 | if (obj == nullptr) |
| 592 | { |
| 593 | LOG(Error, "Failed to spawn object of type \'{0}\'.", String(typeClass->GetFullName())); |
| 594 | return nullptr; |
| 595 | } |
| 596 | |
| 597 | // Set default name for actors |
| 598 | if (auto* actor = dynamic_cast<Actor*>(obj)) |
| 599 | { |
| 600 | actor->SetName(String(typeClass->GetName())); |
| 601 | } |
| 602 | |
| 603 | // Create managed object |
| 604 | MObject* managedInstance = obj->GetOrCreateManagedInstance(); |
| 605 | if (managedInstance == nullptr) |
| 606 | { |
| 607 | LOG(Error, "Cannot create managed instance for type \'{0}\'.", String(typeClass->GetFullName())); |
| 608 | Delete(obj); |
| 609 | } |
nothing calls this directly
no test coverage detected