Exemplo n.º 1
0
        public static void LoadAsset([NotNull] Folder root, [NotNull] ModAsset asset, [CanBeNull] Folder bundledFiles)
        {
            FileTree data;

            if (asset.Origin.Scheme == "dvmod" && asset.Origin.Host == "current-archive")
            {
                if (bundledFiles is null)
                {
                    throw new InvalidOperationException("reference ro current-archive from standalone mod info");
                }
                data = bundledFiles.GetPath(asset.Origin.AbsolutePath.TrimStart('/')) ?? throw new FileNotFoundException("referenced file was not found", asset.Origin.ToString());
            }
            else
            {
                var stream = DownloadManager.Download(asset.Origin) ?? throw new FileNotFoundException("unable to download asset", asset.Origin.ToString());
                data = new File("", FileData.FromStream(stream), null);
            }

            if (asset.Processor == ModAssetProcessor.Archive && data is File file)
            {
                data = ArchiveExtractor.Read(file.Data, asset.ArchivePath);
                if (data is Folder fo)
                {
                    Load(fo);
                    if (string.IsNullOrEmpty(asset.Path))
                    {
                        root.AddItems(fo.Children);
                    }
                }
            }

            if (!string.IsNullOrEmpty(asset.Path))
            {
                if (data is Folder fo)
                {
                    var c = root.PathAddFolder(asset.Path);
                    c.AddItems(fo.Children);
                    while (c != null && c.Processor == ModAssetProcessor.None)
                    {
                        c.Processor = ModAssetProcessor.MergeFolder;
                        c           = c.Parent;
                    }
                }
                else if (data is File fi)
                {
                    data = root.PathAddFile(asset.Path, fi.FileData);
                }
            }
            else if (data is File)
            {
                throw new InvalidOperationException("file assets must specify a folder");
            }

            if (asset.Processor != ModAssetProcessor.Archive)
            {
                data.Processor = asset.Processor;
            }
            if (data.Processor == ModAssetProcessor.None)
            {
                data.Processor = data is File ? ModAssetProcessor.Copy : ModAssetProcessor.MergeFolder;
            }
            data = data.Parent;
            while (data != null && data.Processor == ModAssetProcessor.None)
            {
                data.Processor = ModAssetProcessor.MergeFolder;
                data           = data.Parent;
            }
        }