Example #1
0
        public async Task Load(IFileSystem fileSystem, UPath filePath, LoadContext loadContext)
        {
            Stream incStream;
            Stream datStream;

            if (filePath.GetExtensionWithDot() == ".inc")
            {
                if (!fileSystem.FileExists(filePath.GetDirectory() / "pack.dat"))
                {
                    throw new FileNotFoundException("pack.dat not found.");
                }

                incStream = await fileSystem.OpenFileAsync(filePath);

                datStream = await fileSystem.OpenFileAsync(filePath.GetDirectory() / "pack.dat");
            }
            else
            {
                if (!fileSystem.FileExists(filePath.GetDirectory() / "pack.inc"))
                {
                    throw new FileNotFoundException("pack.inc not found.");
                }

                incStream = await fileSystem.OpenFileAsync(filePath.GetDirectory() / "pack.inc");

                datStream = await fileSystem.OpenFileAsync(filePath);
            }

            Files = _aatri.Load(incStream, datStream, AAPackSupport.GetVersion(loadContext.DialogManager));
        }
Example #2
0
        public async Task Load(IFileSystem fileSystem, UPath filePath, LoadContext loadContext)
        {
            Stream hpiStream;
            Stream hpbStream;

            if (filePath.GetExtensionWithDot() == ".HPI")
            {
                var hpbName = filePath.GetNameWithoutExtension() + ".HPB";

                if (!fileSystem.FileExists(filePath.GetDirectory() / hpbName))
                {
                    throw new FileNotFoundException($"{hpbName} not found.");
                }

                hpiStream = await fileSystem.OpenFileAsync(filePath);

                hpbStream = await fileSystem.OpenFileAsync(filePath.GetDirectory() / hpbName);
            }
            else
            {
                var hpiName = filePath.GetNameWithoutExtension() + ".HPI";

                if (!fileSystem.FileExists(filePath.GetDirectory() / hpiName))
                {
                    throw new FileNotFoundException($"{hpiName} not found.");
                }

                hpiStream = await fileSystem.OpenFileAsync(filePath.GetDirectory() / hpiName);

                hpbStream = await fileSystem.OpenFileAsync(filePath);
            }

            Files = _hpiHpb.Load(hpiStream, hpbStream);
        }
Example #3
0
        public async Task Load(IFileSystem fileSystem, UPath filePath, LoadContext loadContext)
        {
            Stream imgStream;
            Stream ddtStream;

            if (filePath.GetExtensionWithDot() == ".IMG")
            {
                var ddtPath = filePath.GetDirectory() / (filePath.GetNameWithoutExtension() + ".DDT");
                if (!fileSystem.FileExists(ddtPath))
                {
                    throw new FileNotFoundException($"{ddtPath.GetName()} not found.");
                }

                imgStream = await fileSystem.OpenFileAsync(filePath);

                ddtStream = await fileSystem.OpenFileAsync(ddtPath);
            }
            else
            {
                var imgPath = filePath.GetDirectory() / (filePath.GetNameWithoutExtension() + ".IMG");
                if (!fileSystem.FileExists(imgPath))
                {
                    throw new FileNotFoundException($"{imgPath.GetName()} not found.");
                }

                imgStream = await fileSystem.OpenFileAsync(imgPath);

                ddtStream = await fileSystem.OpenFileAsync(filePath);
            }

            Files = _ddtImg.Load(ddtStream, imgStream);
        }
Example #4
0
        public Task Save(IFileSystem fileSystem, UPath savePath, SaveContext saveContext)
        {
            Stream lstStream;
            Stream arcStream;

            var lstName = $"{savePath.GetNameWithoutExtension()}.irlst";
            var arcName = $"{savePath.GetNameWithoutExtension()}.irarc";

            switch (savePath.GetExtensionWithDot())
            {
            case ".irlst":
                lstStream = fileSystem.OpenFile(savePath.GetDirectory() / lstName, FileMode.Create);
                arcStream = fileSystem.OpenFile(savePath.GetDirectory() / arcName, FileMode.Create);
                break;

            default:
                lstStream = fileSystem.OpenFile(savePath.GetDirectory() / lstName, FileMode.Create);
                arcStream = fileSystem.OpenFile(savePath.GetDirectory() / arcName, FileMode.Create);
                break;
            }

            _irarc.Save(lstStream, arcStream, Files);

            return(Task.CompletedTask);
        }
Example #5
0
        public async Task Load(IFileSystem fileSystem, UPath filePath, LoadContext loadContext)
        {
            Stream dataStream;
            Stream indexStream;

            switch (filePath.GetName())
            {
            case "mcb1.bln":
                dataStream = await fileSystem.OpenFileAsync(filePath);

                indexStream = await fileSystem.OpenFileAsync(filePath.GetDirectory() / "mcb0.bln");

                break;

            default:
                indexStream = await fileSystem.OpenFileAsync(filePath);

                dataStream = await fileSystem.OpenFileAsync(filePath.GetDirectory() / "mcb1.bln");

                break;
            }

            if (dataStream == null || indexStream == null)
            {
                throw new InvalidOperationException("This is no Bln archive.");
            }

            Files = _bln.Load(indexStream, dataStream);
        }
Example #6
0
        public async Task Load(IFileSystem fileSystem, UPath filePath, LoadContext loadContext)
        {
            Stream lstStream;
            Stream arcStream;

            if (filePath.GetExtensionWithDot() == ".irlst")
            {
                var arcName = $"{filePath.GetNameWithoutExtension()}.irarc";

                if (!fileSystem.FileExists(filePath.GetDirectory() / arcName))
                {
                    throw new FileNotFoundException($"{ arcName } not found.");
                }

                lstStream = await fileSystem.OpenFileAsync(filePath);

                arcStream = await fileSystem.OpenFileAsync(filePath.GetDirectory() / arcName);
            }
            else
            {
                var lstName = $"{filePath.GetNameWithoutExtension()}.irlst";

                if (!fileSystem.FileExists(filePath.GetDirectory() / lstName))
                {
                    throw new FileNotFoundException($"{lstName} not found.");
                }

                lstStream = await fileSystem.OpenFileAsync(filePath.GetDirectory() / lstName);

                arcStream = await fileSystem.OpenFileAsync(filePath);
            }

            Files = _irarc.Load(lstStream, arcStream);
        }
Example #7
0
        public static void SetPath(this IFileRef self, FileEntry file)
        {
            UPath value = file.Path;

            self.dir      = "" + value.GetDirectory();
            self.fileName = value.GetName();
        }
Example #8
0
        public void TestExtensions()
        {
            {
                var path = new UPath("/a/b/c/d.txt");
                Assert.Equal(new UPath("/a/b/c"), path.GetDirectory());
                Assert.Equal("d.txt", path.GetName());
                Assert.Equal("d", path.GetNameWithoutExtension());
                Assert.Equal(".txt", path.GetExtensionWithDot());
                var newPath = path.ChangeExtension(".zip");
                Assert.Equal("/a/b/c/d.zip", newPath.FullName);
                Assert.Equal(new UPath("a/b/c/d.txt"), path.ToRelative());
                Assert.Equal(path, path.AssertAbsolute());
                Assert.Throws <ArgumentNullException>(() => new UPath().AssertNotNull());
                Assert.Throws <ArgumentException>(() => new UPath("not_absolute").AssertAbsolute());
            }

            {
                var path = new UPath("d.txt");
                Assert.Equal(UPath.Empty, path.GetDirectory());
                Assert.Equal("d.txt", path.GetName());
                Assert.Equal("d", path.GetNameWithoutExtension());
                Assert.Equal(".txt", path.GetExtensionWithDot());
                var newPath = path.ChangeExtension(".zip");
                Assert.Equal("d.zip", newPath.FullName);
                Assert.Equal(new UPath("d.txt"), path.ToRelative());
            }
        }
Example #9
0
        /// <inheritdoc/>
        public override void SetBlockStates(
            HashDigest <SHA256> blockHash,
            IImmutableDictionary <string, IValue> states)
        {
            var serialized = new Bencodex.Types.Dictionary(
                states.ToImmutableDictionary(
                    kv => (IKey)(Text)kv.Key,
                    kv => kv.Value
                    )
                );

            UPath path    = StatePath(blockHash);
            UPath dirPath = path.GetDirectory();

            CreateDirectoryRecursively(_states, dirPath);

            var codec = new Codec();

            using Stream file = _states.CreateFile(path);
            if (_compress)
            {
                using var deflate = new DeflateStream(file, CompressionLevel.Fastest, true);
                codec.Encode(serialized, deflate);
            }
            else
            {
                codec.Encode(serialized, file);
            }

            _statesCache.AddOrUpdate(blockHash, states);
        }
Example #10
0
        public IEnumerable <UPath> EnumeratePaths(UPath path, string searchPattern, SearchOption searchOption,
                                                  SearchTarget searchTarget)
        {
            var search = SearchPattern.Parse(ref path, ref searchPattern);

            var hashset = new HashSet <UPath>();

            // ReSharper disable once LoopCanBePartlyConvertedToQuery
            foreach (var entry in this.archive.Entries)
            {
                var p = new UPath('/' + entry.FullName);
                if (searchTarget == SearchTarget.Both || searchTarget == SearchTarget.File)
                {
                    if (p.IsInDirectory(path, searchOption == SearchOption.AllDirectories) && search.Match(p))
                    {
                        hashset.Add(p);
                    }
                }

                if (searchTarget != SearchTarget.Both && searchTarget != SearchTarget.Directory)
                {
                    continue;
                }
                p = p.GetDirectory();
                if (p.IsInDirectory(path, searchOption == SearchOption.AllDirectories) && search.Match(p))
                {
                    hashset.Add(p);
                }
            }

            return(hashset);
        }
Example #11
0
        /// <inheritdoc />
        protected override Stream OpenFileImpl(UPath path, FileMode mode, FileAccess access,
                                               FileShare share)
        {
            if (IsWithinSpecialDirectory(path))
            {
                throw new UnauthorizedAccessException($"The access to `{path}` is denied");
            }

            // Create directory if not existing
            var directory = path.GetDirectory();

            if (!DirectoryExists(directory))
            {
                CreateDirectory(directory);
            }

            // Open file
            Stream file;

            if (mode == FileMode.Create || mode == FileMode.CreateNew)
            {
                file = File.Open(ConvertPathToInternal(path), mode);
            }
            else
            {
                file = File.Open(ConvertPathToInternal(path), mode, access, share);
            }
            StreamManager.Register(file);

            GetOrCreateDispatcher().RaiseOpened(path);

            return(file);
        }
Example #12
0
 private static void CreateDirectoryRecursively(IFileSystem fs, UPath path)
 {
     if (!fs.DirectoryExists(path))
     {
         CreateDirectoryRecursively(fs, path.GetDirectory());
         fs.CreateDirectory(path);
     }
 }
Example #13
0
 private void AddChangedDirectory(UPath path)
 {
     while (path != UPath.Root && !path.IsEmpty)
     {
         _changedDirectories.Add(path);
         path = path.GetDirectory();
     }
 }
Example #14
0
        public async Task Load(IFileSystem fileSystem, UPath filePath, LoadContext loadContext)
        {
            var fileStream = await fileSystem.OpenFileAsync(filePath);

            var apkFilePaths = fileSystem.EnumerateFiles(filePath.GetDirectory(), "*.apk");
            var apkStreams   = apkFilePaths.Select(x => fileSystem.OpenFile(x)).ToArray();

            Files = _arc.Load(fileStream, apkStreams);
        }
Example #15
0
        /// <inheritdoc cref="BaseStore.PutTxExecution(Libplanet.Tx.TxFailure)"/>
        public override void PutTxExecution(TxFailure txFailure)
        {
            UPath path    = TxExecutionPath(txFailure);
            UPath dirPath = path.GetDirectory();

            CreateDirectoryRecursively(_txExecutions, dirPath);
            using Stream f =
                      _txExecutions.OpenFile(path, System.IO.FileMode.OpenOrCreate, FileAccess.Write);
            Codec.Encode(SerializeTxExecution(txFailure), f);
        }
Example #16
0
        public async Task Load(IFileSystem fileSystem, UPath filePath, LoadContext loadContext)
        {
            var segStream = await fileSystem.OpenFileAsync(filePath);

            var binStream = await fileSystem.OpenFileAsync(filePath.ChangeExtension(".BIN"));

            var sizeName   = filePath.GetDirectory() / filePath.GetNameWithoutExtension() + "SIZE.BIN";
            var sizeStream = fileSystem.FileExists(sizeName) ? await fileSystem.OpenFileAsync(sizeName) : null;

            Files = _arc.Load(segStream, binStream, sizeStream);
        }
Example #17
0
        public Task Save(IFileSystem fileSystem, UPath savePath, SaveContext saveContext)
        {
            Stream texStream;
            Stream texListStream;

            if (savePath.GetName() == "textures")
            {
                texStream     = fileSystem.OpenFile(savePath, FileMode.Create, FileAccess.Write);
                texListStream = fileSystem.OpenFile(savePath.GetDirectory() / "texture_table", FileMode.Create, FileAccess.Write);
            }
            else
            {
                texStream     = fileSystem.OpenFile(savePath.GetDirectory() / "textures", FileMode.Create, FileAccess.Write);
                texListStream = fileSystem.OpenFile(savePath, FileMode.Create, FileAccess.Write);
            }

            _arc.Save(texStream, texListStream, Files);

            return(Task.CompletedTask);
        }
Example #18
0
        public async Task Load(IFileSystem fileSystem, UPath filePath, LoadContext loadContext)
        {
            Stream texStream;
            Stream texListStream;

            if (filePath.GetName() == "textures")
            {
                texStream = await fileSystem.OpenFileAsync(filePath);

                texListStream = await fileSystem.OpenFileAsync(filePath.GetDirectory() / "texture_table");
            }
            else
            {
                texStream = await fileSystem.OpenFileAsync(filePath.GetDirectory() / "textures");

                texListStream = await fileSystem.OpenFileAsync(filePath);
            }

            Files = _arc.Load(texStream, texListStream);
        }
Example #19
0
        public Task Save(IFileSystem fileSystem, UPath savePath, SaveContext saveContext)
        {
            var texPath = savePath;
            var pltPath = $"{savePath.GetDirectory()}/../Palettes(NW4R)/{savePath.GetName()}";

            var texStream = fileSystem.OpenFile(texPath, FileMode.Create, FileAccess.Write);
            var pltStream = Images[0].ImageInfo.HasPaletteInformation ? fileSystem.OpenFile(pltPath, FileMode.Create, FileAccess.Write) : null;

            _img.Save(texStream, pltStream, Images[0].ImageInfo);

            return(Task.CompletedTask);
        }
Example #20
0
        public Task Save(IFileSystem fileSystem, UPath savePath, SaveContext saveContext)
        {
            var segStream = fileSystem.OpenFile(savePath, FileMode.Create, FileAccess.Write);
            var binStream = fileSystem.OpenFile(savePath.ChangeExtension(".BIN"), FileMode.Create, FileAccess.Write);

            var sizeName   = savePath.GetDirectory() / savePath.GetNameWithoutExtension() + "SIZE.BIN";
            var sizeStream = Files.Any(x => x.UsesCompression) ? fileSystem.OpenFile(sizeName, FileMode.Create, FileAccess.Write) : null;

            _arc.Save(segStream, binStream, sizeStream, Files);

            return(Task.CompletedTask);
        }
Example #21
0
        public Task Save(IFileSystem fileSystem, UPath savePath, SaveContext saveContext)
        {
            Stream dataOutput;
            Stream indexOutput;

            switch (savePath.GetName())
            {
            case "mcb1.bln":
                dataOutput  = fileSystem.OpenFile(savePath, FileMode.Create);
                indexOutput = fileSystem.OpenFile(savePath.GetDirectory() / "mcb0.bln", FileMode.Create);
                break;

            default:
                indexOutput = fileSystem.OpenFile(savePath, FileMode.Create);
                dataOutput  = fileSystem.OpenFile(savePath.GetDirectory() / "mcb1.bln", FileMode.Create);
                break;
            }

            _bln.Save(indexOutput, dataOutput, Files);

            return(Task.CompletedTask);
        }
Example #22
0
        public Task Save(IFileSystem fileSystem, UPath savePath, SaveContext saveContext)
        {
            Stream incStream;
            Stream datStream;

            switch (savePath.GetExtensionWithDot())
            {
            case ".inc":
                incStream = fileSystem.OpenFile(savePath.GetDirectory() / "pack.inc", FileMode.Create);
                datStream = fileSystem.OpenFile(savePath.GetDirectory() / savePath.GetNameWithoutExtension() + ".dat", FileMode.Create);
                break;

            default:
                incStream = fileSystem.OpenFile(savePath.GetDirectory() / savePath.GetNameWithoutExtension() + ".inc", FileMode.Create);
                datStream = fileSystem.OpenFile(savePath.GetDirectory() / "pack.dat", FileMode.Create);
                break;
            }

            _aatri.Save(incStream, datStream, Files);

            return(Task.CompletedTask);
        }
Example #23
0
        public async Task Load(IFileSystem fileSystem, UPath filePath, LoadContext loadContext)
        {
            var texPath = filePath;
            var pltPath = $"{filePath.GetDirectory()}/../Palettes(NW4R)/{filePath.GetName()}";

            var texStream = await fileSystem.OpenFileAsync(texPath);

            var pltStream = fileSystem.FileExists(pltPath) ? await fileSystem.OpenFileAsync(pltPath) : null;

            Images = new List <IKanvasImage> {
                new KanvasImage(EncodingDefinition, _img.Load(texStream, pltStream))
            };
        }
Example #24
0
        private void AssertMountName(UPath name)
        {
            name.AssertAbsolute();
            if (name == UPath.Root)
            {
                throw new ArgumentException("The mount name cannot be a `/` root filesystem", nameof(name));
            }

            if (name.GetDirectory() != UPath.Root)
            {
                throw new ArgumentException("The mount name cannot contain subpath and must contain only a root path e.g `/mount`", nameof(name));
            }
        }
Example #25
0
        public IArchiveFileInfo Add(Stream fileData, UPath filePath)
        {
            // Determine extension hash
            var extensionHash = Regex.IsMatch(filePath.GetExtensionWithDot(), @"\.[\da-fA-F]{8}") ?
                                uint.Parse(filePath.GetExtensionWithDot().Substring(1), NumberStyles.HexNumber) :
                                MtArcSupport.DetermineExtensionHash(filePath.GetExtensionWithDot());

            // Create entry
            IMtEntry entry;

            switch (_platform)
            {
            case MtArcPlatform.Switch:
                entry = new MtEntrySwitch
                {
                    ExtensionHash = extensionHash,
                    FileName      = (filePath.GetDirectory() / filePath.GetNameWithoutExtension()).FullName,
                    decompSize    = (int)fileData.Length
                };
                break;

            case MtArcPlatform.LittleEndian:
            case MtArcPlatform.BigEndian:
                entry = new MtEntry
                {
                    ExtensionHash = extensionHash,
                    FileName      = (filePath.GetDirectory() / filePath.GetNameWithoutExtension()).FullName,
                    decompSize    = (int)fileData.Length,
                };
                break;

            default:
                throw new InvalidOperationException();
            }

            // Create ArchiveFileInfo
            return(CreateAfi(fileData, filePath.FullName, entry, _platform));
        }
Example #26
0
        private static bool IsWithinSpecialDirectory(UPath path)
        {
            if (!IsOnWindows)
            {
                return(false);
            }

            var parentDirectory = path.GetDirectory();

            return(path == PathDrivePrefixOnWindows ||
                   path == UPath.Root ||
                   parentDirectory == PathDrivePrefixOnWindows ||
                   parentDirectory == UPath.Root);
        }
Example #27
0
        /// <summary>
        /// Creates a <see cref="MemoryFileSystem"/> based on the given <see cref="Stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> to add to the file system.</param>
        /// <param name="streamName">The path of the stream in the file system.</param>
        /// <param name="streamManager">The stream manager for this file system.</param>
        /// <returns>The created <see cref="IFileSystem"/> for this stream.</returns>
        public static IFileSystem CreateMemoryFileSystem(Stream stream, UPath streamName, IStreamManager streamManager)
        {
            // 1. Create file system
            var fileSystem = new MemoryFileSystem(streamManager);
            var directory  = streamName.GetDirectory();

            if (!directory.IsEmpty && !fileSystem.DirectoryExists(streamName.GetDirectory()))
            {
                fileSystem.CreateDirectory(streamName.GetDirectory());
            }

            var createdStream = fileSystem.OpenFile(streamName, FileMode.CreateNew, FileAccess.Write);

            // 2. Copy data
            var bkPos = stream.Position;

            stream.Position = 0;
            stream.CopyTo(createdStream);
            stream.Position        = bkPos;
            createdStream.Position = 0;
            createdStream.Close();

            return(fileSystem);
        }
Example #28
0
        public Task Save(IFileSystem fileSystem, UPath savePath, SaveContext saveContext)
        {
            Stream imgStream;
            Stream ddtStream;

            switch (savePath.GetExtensionWithDot())
            {
            case ".IMG":
                var ddtPath = savePath.GetDirectory() / (savePath.GetNameWithoutExtension() + ".DDT");
                imgStream = fileSystem.OpenFile(savePath, FileMode.Create);
                ddtStream = fileSystem.OpenFile(ddtPath, FileMode.Create);
                break;

            default:
                var imgPath = savePath.GetDirectory() / (savePath.GetNameWithoutExtension() + ".IMG");
                imgStream = fileSystem.OpenFile(imgPath, FileMode.Create);
                ddtStream = fileSystem.OpenFile(savePath, FileMode.Create);
                break;
            }

            _ddtImg.Save(ddtStream, imgStream, Files);

            return(Task.CompletedTask);
        }
Example #29
0
        /// <inheritdoc cref="BaseStore.SetBlockPerceivedTime(BlockHash, DateTimeOffset)"/>
        public override void SetBlockPerceivedTime(
            BlockHash blockHash,
            DateTimeOffset perceivedTime
            )
        {
            UPath path = BlockPath(blockHash);

            if (!_blockPerceptions.FileExists(path))
            {
                UPath dirPath = path.GetDirectory();
                CreateDirectoryRecursively(_blockPerceptions, dirPath);
                _blockPerceptions.WriteAllBytes(path, new byte[0]);
            }

            _blockPerceptions.SetLastWriteTime(path, perceivedTime.LocalDateTime);
        }
Example #30
0
        private void SaveFileAs()
        {
            var sfd = new SaveFileDialog
            {
                InitialDirectory = _openedFile.GetDirectory().FullName,
                FileName         = _openedFile.GetName()
            };

            if (sfd.ShowDialog() != DialogResult.OK)
            {
                MessageBox.Show("An error occurred when selecting a save path.", "Save Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            SaveFile(sfd.FileName);
        }