Exemplo n.º 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);
        }
Exemplo n.º 2
0
        internal PackageResourceContent(PackageExplorer packageExplorer, string contentKey, IReadOnlyArchiveFile zipArchiveEntry)
        {
            this.packageExplorer = packageExplorer;
            this.contentKey      = contentKey;

            CompressedSize   = 0;
            UncompressedSize = 0;
        }
Exemplo n.º 3
0
        public PackageResource(PackageExplorer package, ZipArchiveEntry packageEntry)
        {
            Package    = package;
            PackageKey = packageEntry.FullName;

            CompressedSize   = packageEntry.CompressedLength;
            UncompressedSize = packageEntry.Length;
        }
Exemplo n.º 4
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);
                    }

                    string pathPrefix  = null;
                    var    pathEntries = new List <ZipArchiveEntry>();
                    foreach (var projectEntry in archive.Entries)
                    {
                        int pathPrefixIndex = projectEntry.FullName.IndexOf('/');
                        if (pathPrefixIndex == -1)
                        {
                            Console.WriteLine("Not adding \"" + projectEntry.FullName + "\" as an item.");
                            continue;
                        }
                        string newPathIndex = projectEntry.FullName.Substring(0, pathPrefixIndex);

                        if (pathPrefix == null)
                        {
                            pathPrefix = newPathIndex;
                        }

                        if (pathPrefix != newPathIndex)
                        {
                            var asset = new PackageAsset(package, pathPrefix, pathEntries.ToArray());
                            pathEntries.Clear();
                            package.Assets.Add(asset);
                            pathPrefix = newPathIndex;
                        }
                        pathEntries.Add(projectEntry);
                    }
                    if (pathEntries.Count != 0)
                    {
                        var asset = new PackageAsset(package, pathPrefix, pathEntries.ToArray());
                        package.Assets.Add(asset);
                    }
                }
                fileStream.Close();
            }

            return(package);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
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]);
            }
        }
Exemplo n.º 7
0
        internal PackageResource(PackageExplorer package, ZipArchiveEntry packageEntry, IReadOnlyDictionary <string, IReadOnlyList <string> > tagsDocument)
        {
            this.package = package;
            Name         = packageEntry.Name;
            FullName     = packageEntry.FullName;

            CompressedSize   = packageEntry.CompressedLength;
            UncompressedSize = packageEntry.Length;

            Tags = new PackageResourceTags(tagsDocument, this);
        }
        internal PackageResourceDependencies(PackageExplorer packageExplorer, PackageResourceMetadataModel metadataModel)
        {
            this.packageExplorer = packageExplorer;
            dependencies         = new List <PackageResourceDependency>();

            if (metadataModel.Dependencies != null)
            {
                foreach (var importerDependency in metadataModel.Dependencies)
                {
                    dependencies.Add(
                        new PackageResourceDependency(packageExplorer, importerDependency));
                }
            }
        }
Exemplo n.º 9
0
        internal PackageResource(PackageExplorer packageExplorer, PackageDirectory directory, IReadOnlyArchiveFile contentEntry, PackageResourceMetadataModel metadataModel)
        {
            Explorer     = packageExplorer;
            Directory    = directory;
            Dependencies = new PackageResourceDependencies(packageExplorer, metadataModel);
            Tags         = new PackageResourceTags();

            Name     = metadataModel.Name;
            FullName = metadataModel.FullName;

            Content = new PackageResourceContent(contentEntry);

            int dotIndex = contentEntry.FullName.LastIndexOf('.');

            Extension = dotIndex != -1
                                ? contentEntry.FullName.Substring(dotIndex)
                                : null;
        }
Exemplo n.º 10
0
        internal PackageResource(PackageExplorer explorer, ZipArchiveEntry packageEntry)
        {
            Tags     = new PackageResourceTags();
            Explorer = explorer;
            Name     = packageEntry.Name;

            string withoutData = packageEntry.FullName.Substring(5);

            FullName = withoutData;

            int dotIndex = packageEntry.FullName.LastIndexOf('.');

            Extension = dotIndex != -1
                                ? packageEntry.FullName.Substring(dotIndex)
                                : null;

            CompressedSize   = packageEntry.CompressedLength;
            UncompressedSize = packageEntry.Length;
        }
Exemplo n.º 11
0
        public PackageResourceDependency(PackageExplorer packageExplorer, PackageResourceMetadataDependencyModel dependencyModel)
        {
            this.packageExplorer = packageExplorer;

            Key = dependencyModel.Resource;
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
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);
        }