| 157 | } |
| 158 | |
| 159 | bool AnimGraph::InitCustomNode(Node* node) |
| 160 | { |
| 161 | #if USE_CSHARP |
| 162 | // Fetch the node logic controller type |
| 163 | if (node->Values.Count() < 2 || node->Values[0].Type.Type != ValueType::String) |
| 164 | { |
| 165 | LOG(Warning, "Invalid custom node data values."); |
| 166 | return false; |
| 167 | } |
| 168 | const StringView typeName(node->Values[0]); |
| 169 | const StringAnsi typeNameStd = typeName.ToStringAnsi(); |
| 170 | MClass* type = Scripting::FindClass(typeNameStd); |
| 171 | if (type == nullptr) |
| 172 | { |
| 173 | LOG(Warning, "Invalid custom node type {0}.", typeName); |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // Get methods |
| 178 | MMethod* load = type->GetMethod("Load", 1); |
| 179 | MMethod* evaluate = type->GetMethod("Evaluate", 1); |
| 180 | if (load == nullptr) |
| 181 | { |
| 182 | LOG(Warning, "Invalid custom node type {0}. Missing Load method.", typeName); |
| 183 | return false; |
| 184 | } |
| 185 | if (evaluate == nullptr) |
| 186 | { |
| 187 | LOG(Warning, "Invalid custom node type {0}. Missing Evaluate method.", typeName); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | // Initialization can happen on Content Thread so ensure to have runtime attached |
| 192 | MCore::Thread::Attach(); |
| 193 | |
| 194 | // Allocate managed node object (create GC handle to prevent destruction) |
| 195 | MObject* obj = type->CreateInstance(); |
| 196 | const MGCHandle handleGC = MCore::GCHandle::New(obj); |
| 197 | |
| 198 | // Initialize node |
| 199 | InternalInitData initData; |
| 200 | initData.Values = MUtils::ToArray(node->Values, MCore::TypeCache::Object); |
| 201 | initData.BaseModel = BaseModel.GetManagedInstance(); |
| 202 | void* params[1]; |
| 203 | params[0] = &initData; |
| 204 | MObject* exception = nullptr; |
| 205 | load->Invoke(obj, params, &exception); |
| 206 | if (exception) |
| 207 | { |
| 208 | MCore::GCHandle::Free(handleGC); |
| 209 | MException ex(exception); |
| 210 | ex.Log(LogType::Warning, TEXT("AnimGraph")); |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | // Cache node data |
| 215 | auto& data = node->Data.Custom; |
| 216 | data.Evaluate = evaluate; |
no test coverage detected