/
EvilBeaver
/
OneScript
Обзор
Документация
Войти
/
EvilBeaver
/
OneScript
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/OneScript.StandardLibrary/NativeApi/NativeApiFactory.cs
52 строки
2 KB
Andrey Ovsiankin
Обновлены неймспейсы по ScriptConstructorAttribute
30 июл 2021, 17:16
30 июл 2021, 17:16
b9c3b75
Код
Авторство
О чём код?
/*---------------------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------------------------------------------------------*/ using System; using System.Collections.Generic; using OneScript.Types; using OneScript.Contexts; using ScriptEngine.Machine; namespace OneScript.StandardLibrary.NativeApi { /// <summary> /// Фабрика, осуществляющая регистрацию библиотеки внешних /// компонент Native API и создания экземпляров компонент. /// </summary> class NativeApiFactory { public static bool Register(string filepath, string identifier, ITypeManager typeManager) { if (_libraries.ContainsKey(identifier)) return false; var library = new NativeApiLibrary(filepath, identifier, typeManager); if (library.Loaded) _libraries.Add(identifier, library); return library.Loaded; } private static readonly Dictionary<string, NativeApiLibrary> _libraries = new Dictionary<string, NativeApiLibrary>(); internal static void Initialize() { foreach (var item in _libraries) item.Value.Dispose(); _libraries.Clear(); } [ScriptConstructor] public static IValue Constructor(TypeActivationContext context) { var typeName = context.TypeName; var separator = new char[] { '.' }; var names = typeName.Split(separator, StringSplitOptions.RemoveEmptyEntries); if (names.Length == 3 && _libraries.TryGetValue(names[1], out NativeApiLibrary library)) return library.CreateComponent(context.TypeManager, default, typeName, names[2]); throw new NotImplementedException(); } } }