Пример #1
0
        protected override void EnsureLoaded()
        {
            if (NameLookup != null)
            {
                return;
            }

            lock (_lockRoot)
            {
                if (NameLookup != null)
                {
                    return;
                }

                var lookup = new Dictionary <string, T>(StringComparer.OrdinalIgnoreCase);

                if (CSScriptLoaderHelpers.LoadScripts && Directory.Exists(BasePath))
                {
                    foreach (var file in Directory.EnumerateFiles(
                                 BasePath, FilePattern, SearchOption.TopDirectoryOnly))
                    {
                        try
                        {
                            var instance = CSScriptLoaderHelpers.LoadFile <T>(file);
                            lookup[instance.Name] = instance;
                        }
                        catch (Exception e)
                        {
                            throw new InvalidOperationException(
                                      $"Error while loading {typeof(T).Name} '{Path.GetFileName(file)}'\r\n", e);
                        }
                    }
                }
                else
                {
                    foreach (var field in DataType.GetFields(BindingFlags.Public | BindingFlags.Static)
                             .Where(f => typeof(T).GetTypeInfo().IsAssignableFrom(f.FieldType)))
                    {
                        var instance = (T)field.GetValue(null);
                        lookup[instance.Name] = instance;
                    }
                }

                NameLookup = lookup;
            }
        }
 public SingleCSScriptLoader(string relativePath, Type dataType) : base(relativePath, dataType)
 {
     _name       = new T().Name;
     FilePattern = CSScriptLoaderHelpers.GetScriptFilename(_name) + CSScriptLoaderHelpers.ScriptExtension;
 }