Exemple #1
0
        public static PackageExplorer Load(string path)
        {
            var package = new PackageExplorer
            {
                path = path
            };

            using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Read, true))
                {
                    var entry = archive.GetEntry("Main.bmft");

                    byte[] buffer = new byte[entry.Length];
                    using (var zipStream = entry.Open())
                    {
                        zipStream.Read(buffer, 0, (int)entry.Length);
                        string json = Encoding.UTF8.GetString(buffer);
                    }

                    foreach (var projectEntry in archive.Entries)
                    {
                        var resource = new PackageResource(package, projectEntry);
                        package.Resources.Add(resource);
                    }
                }
                fileStream.Close();
            }

            return(package);
        }
Exemple #2
0
        internal void Add(PackageResource asset)
        {
            if (resources == null)
            {
                resources = new Dictionary <string, PackageResource>();
            }

            resources.Add(asset.FullName, asset);
        }
        public static PackageExplorer Load(string packagePath)
        {
            var packageFileInfo = new FileInfo(packagePath);

            using var fileStream = new FileStream(packagePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            using var archive    = new ZipArchive(fileStream, ZipArchiveMode.Read, true);

            var package = new PackageExplorer
            {
                PackagePath    = packagePath,
                CompressedSize = packageFileInfo.Length
            };

            var definitionEntry    = archive.GetEntry("definition.json");
            var definitionDocument = LoadJsonDocument <PackageDefinition>(definitionEntry);

            var tagsEntry    = archive.GetEntry("tags.json");
            var tagsDocument = LoadJsonDocument <IReadOnlyDictionary <string, IReadOnlyList <string> > >(tagsEntry);
            var tags         = new Dictionary <string, IResourceCollection>();

            var rootDirectiory = new PackageDirectory();

            foreach (var projectEntry in archive.Entries)
            {
                if (!projectEntry.FullName.StartsWith("data/"))
                {
                    continue;
                }

                var resource = new PackageResource(package, projectEntry);
                package.resources.Add(resource);

                foreach (var tagCategory in tagsDocument)
                {
                    if (tagCategory.Value.Contains(resource.FullName))
                    {
                        if (!tags.TryGetValue(tagCategory.Key, out var taggedResourcesCollection))
                        {
                            taggedResourcesCollection = new PackageResourceCollection();
                            tags[tagCategory.Key]     = taggedResourcesCollection;
                        }

                        var taggedResources = (PackageResourceCollection)taggedResourcesCollection;

                        taggedResources.Add(resource);
                        resource.Tags.tags.Add(tagCategory.Key);
                    }
                }
            }

            package.tags       = new PackageTagsCollection(tags);
            package.definition = definitionDocument;

            return(package);
        }
Exemple #4
0
        public PackageAsset(PackageExplorer package, string root, ZipArchiveEntry[] entries)
        {
            Package = package;
            Root    = root;

            PackageResources = new PackageResource[entries.Length];
            for (int i = 0; i < entries.Length; i++)
            {
                PackageResources[i] = new PackageResource(package, entries[i]);
            }
        }
		public bool TryGetResource(string key, out PackageResource value)
		{
			return resources.TryGetValue(key, out value);
		}
		internal void Add(string key, PackageResource asset)
		{
			resources.Add(key, asset);
		}
        public PackageResourceTags(IReadOnlyDictionary <string, IReadOnlyList <string> > tagsDocument, PackageResource resource)
        {
            var tags = new List <string>();

            foreach (var tagCollection in tagsDocument)
            {
                if (tagCollection.Value.Contains(resource.FullName))
                {
                    tags.Add(tagCollection.Key);
                }
            }

            this.tags = tags;
        }
Exemple #8
0
 internal void Add(PackageResource asset)
 {
     resources.Add(asset.FullName, asset);
 }
Exemple #9
0
        public static async Task <PackageExplorer> LoadAsync(IReadOnlyArchiveDirectory source)
        {
            var definitionEntry    = source.Files.GetFile("definition.json");
            var definitionDocument = LoadJsonDocument <PackageDefinition>(definitionEntry);

            var tagsEntry    = source.Files.GetFile("tags.json");
            var tagsDocument = LoadJsonDocument <IReadOnlyDictionary <string, IReadOnlyList <string> > >(tagsEntry);
            var tags         = new Dictionary <string, IResourceCollection>();

            var rootDirectiory = new PackageDirectory("", "", null);

            var packageExplorer = new PackageExplorer
            {
                Source        = source,
                RootDirectory = rootDirectiory,
                Definition    = definitionDocument
            };

            void ImportDirectory(IReadOnlyArchiveDirectory directory, PackageDirectory packageDirectory)
            {
                foreach (var childDirectory in directory.Directories)
                {
                    ImportDirectory(childDirectory,
                                    new PackageDirectory(childDirectory.Name, childDirectory.FullName, packageDirectory));
                }

                foreach (var file in directory.Files)
                {
                    if (!file.FullName.EndsWith(".pkgmeta"))
                    {
                        continue;
                    }
                    var contentEntry = directory.Files.GetFile(file.Name.Substring(0, file.Name.Length - 8));

                    var metadataModel = LoadJsonDocument <PackageResourceMetadataModel>(file);

                    var resource = new PackageResource(packageExplorer, packageDirectory, contentEntry, metadataModel);

                    packageExplorer.Resources.Add(resource.FullName, resource);
                    packageDirectory.Resources.Add(resource.Name, resource);

                    foreach (var tagCategory in tagsDocument)
                    {
                        if (tagCategory.Value.Contains(resource.FullName))
                        {
                            if (!tags.TryGetValue(tagCategory.Key, out var taggedResourcesCollection))
                            {
                                taggedResourcesCollection = new PackageResourceCollection();
                                tags[tagCategory.Key]     = taggedResourcesCollection;
                            }

                            var taggedResources = (PackageResourceCollection)taggedResourcesCollection;

                            taggedResources.Add(resource.FullName, resource);
                            resource.Tags.tags.Add(tagCategory.Key);
                        }
                    }
                }
            }

            ImportDirectory(source, rootDirectiory);

            packageExplorer.Tags = new PackageTagsCollection(tags);

            return(packageExplorer);
        }
Exemple #10
0
        public static async Task <PackageExplorer> LoadAsync(IReadOnlyArchive archive)
        {
            var definitionEntry    = archive.Files.GetFile("definition.json");
            var definitionDocument = LoadJsonDocument <PackageDefinition>(definitionEntry);

            var tagsEntry    = archive.Files.GetFile("tags.json");
            var tagsDocument = LoadJsonDocument <IReadOnlyDictionary <string, IReadOnlyList <string> > >(tagsEntry);
            var tags         = new Dictionary <string, IResourceCollection>();

            var rootDirectiory = new PackageDirectory("", "", null);

            PackageDirectory ForPath(string path)
            {
                int currentIndex     = 5;
                var currentDirectory = rootDirectiory;

                while (true)
                {
                    int nextIndex = path.IndexOf('/', currentIndex);
                    if (nextIndex == -1)
                    {
                        break;
                    }
                    string segment = path.Substring(currentIndex, nextIndex - currentIndex);

                    bool found = false;
                    foreach (var directory in currentDirectory.Directories)
                    {
                        if (directory.Name == segment)
                        {
                            currentDirectory = directory;
                            found            = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        var newDirectory = new PackageDirectory(segment, path.Substring(5, nextIndex - 5), currentDirectory);
                        currentDirectory.Directories.Add(newDirectory);
                        currentDirectory = newDirectory;
                    }

                    currentIndex = nextIndex + 1;
                }

                return(currentDirectory);
            }

            var package = new PackageExplorer
            {
                Archive       = archive,
                RootDirectory = rootDirectiory,
                Definition    = definitionDocument
            };

            foreach (var metadataEntry in archive.Files)
            {
                /*
                 * if (!packageEntry.FullName.StartsWith("data/")
                 || packageEntry.FullName.EndsWith(".pkgmeta"))
                 ||{
                 ||     continue;
                 ||}
                 ||var metadataEntry = archive.Files.GetFile($"{packageEntry.FullName}.pkgmeta");
                 */

                if (!metadataEntry.FullName.EndsWith(".pkgmeta"))
                {
                    continue;
                }
                var contentEntry = archive.Files.GetFile(metadataEntry.FullName.Substring(0, metadataEntry.FullName.Length - 8));

                var metadataModel = LoadJsonDocument <PackageResourceMetadataModel>(metadataEntry);

                var forPath = ForPath(contentEntry.FullName);

                var resource = new PackageResource(package, forPath, contentEntry, metadataModel);

                package.Resources.Add(resource.FullName, resource);
                forPath.Resources.Add(resource.Name, resource);

                foreach (var tagCategory in tagsDocument)
                {
                    if (tagCategory.Value.Contains(resource.FullName))
                    {
                        if (!tags.TryGetValue(tagCategory.Key, out var taggedResourcesCollection))
                        {
                            taggedResourcesCollection = new PackageResourceCollection();
                            tags[tagCategory.Key]     = taggedResourcesCollection;
                        }

                        var taggedResources = (PackageResourceCollection)taggedResourcesCollection;

                        taggedResources.Add(resource.FullName, resource);
                        resource.Tags.tags.Add(tagCategory.Key);
                    }
                }
            }

            package.Tags = new PackageTagsCollection(tags);

            return(package);
        }
Exemple #11
0
        public static async Task <PackageExplorer> LoadAsync(IReadOnlyArchiveDirectory source)
        {
            var definitionEntry    = source.Files.GetFile("definition.json");
            var definitionDocument = LoadJsonDocument <PackageDefinition>(definitionEntry);

            var tagsEntry    = source.Files.GetFile("tags.json");
            var tagsDocument = LoadJsonDocument <IReadOnlyDictionary <string, IReadOnlyList <string> > >(tagsEntry);
            var tags         = new Dictionary <string, IResourceCollection>();

            var rootDirectiory = new PackageDirectory("", null);

            var packageExplorer = new PackageExplorer
            {
                Source        = source,
                RootDirectory = rootDirectiory,
                Definition    = definitionDocument
            };

            var resourcesDirectory = source.Directories.GetDirectory("resources");
            var contentsDirectory  = source.Directories.GetDirectory("contents");

            // Does this package contain any resources?
            if (resourcesDirectory != null)
            {
                foreach (var resourceFile in resourcesDirectory.Files)
                {
                    var metadataModel = LoadJsonDocument <PackageResourceMetadataModel>(resourceFile);

                    // Directory
                    string[] elements = metadataModel.FullName
                                        .Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    var parentDirectory = rootDirectiory;
                    for (int i = 0; i < elements.Length - 1; i++)
                    {
                        string element = elements[i];

                        PackageDirectory findDirectory = null;
                        foreach (var directory in parentDirectory.Directories)
                        {
                            if (directory.Name == element)
                            {
                                findDirectory = directory;
                                break;
                            }
                        }
                        if (findDirectory == null)
                        {
                            findDirectory = new PackageDirectory(element, parentDirectory);
                            parentDirectory.Directories.Add(findDirectory);
                        }
                        parentDirectory = findDirectory;
                    }

                    // Resource
                    var contentFile = contentsDirectory.Files.GetFile(metadataModel.ContentId);
                    var resource    = new PackageResource(packageExplorer, parentDirectory, contentFile, metadataModel);

                    packageExplorer.Resources.Add(resource.FullName, resource);
                    parentDirectory.Resources.Add(resource.Name, resource);

                    // Tags
                    foreach (var tagCategory in tagsDocument)
                    {
                        if (tagCategory.Value.Contains(resource.FullName))
                        {
                            if (!tags.TryGetValue(tagCategory.Key, out var taggedResourcesCollection))
                            {
                                taggedResourcesCollection = new PackageResourceCollection();
                                tags[tagCategory.Key]     = taggedResourcesCollection;
                            }

                            var taggedResources = (PackageResourceCollection)taggedResourcesCollection;

                            taggedResources.Add(resource.FullName, resource);
                            resource.Tags.tags.Add(tagCategory.Key);
                        }
                    }
                }
            }

            packageExplorer.Tags = new PackageTagsCollection(tags);

            return(packageExplorer);
        }