public static bool IsCompressed(NDSFile file)
 => file.Name.ToLower().EndsWith("lz.bin");
 public VCSChange(SerializationInfo info, StreamingContext context)
 {
     RollbackPatch = (XDeltaPatch)info.GetValue(nameof(RollbackPatch), typeof(XDeltaPatch));
     ParentCommit  = (VCSCommit)info.GetValue(nameof(ParentCommit), typeof(VCSCommit));
     ParentFile    = (NDSFile)info.GetValue(nameof(ParentFile), typeof(NDSFile));
 }
Exemple #3
0
        public void Initialize()
        {
            Dictionary <ushort, NDSDirectory> directories = new Dictionary <ushort, NDSDirectory>();
            Dictionary <ushort, NDSFile>      files       = new Dictionary <ushort, NDSFile>();

            // The maximum address we can access while reading metaentries.
            uint dirEntriesAddrLimit = BitConverter.ToUInt32(RawFNT.Slice(0, sizeof(uint)).GetAsArrayCopy(), startIndex: 0);
            // We create a dictionary for children before setting them because folders/files may be out of order
            // and parents may not exist before referencing them.
            var childrenStructure = new Dictionary <ushort, List <ushort> >();

            // Everything before this entry in the FAT is an overlay file.
            ushort firstDataFileID = BitConverter.ToUInt16(RawFNT.Slice(sizeof(uint), sizeof(ushort)).GetAsArrayCopy(), startIndex: 0);

            // 0xFFFF is just a fictional ID used for convenience.
            RootOverlayDirectory = new NDSDirectory(this, 0xFFFF, "overlay");
            for (ushort overlayID = 0; overlayID < firstDataFileID; overlayID++)
            {
                RootOverlayDirectory.ChildrenFiles.Add(new NDSFile(this, overlayID, $"overlay_{overlayID}")
                {
                    Parent = RootOverlayDirectory
                });
            }

            for (int currentRelativeAddress = 0; currentRelativeAddress < dirEntriesAddrLimit; currentRelativeAddress += DirectoryEntrySize)
            {
                ByteSlice dirEntry = RawFNT.Slice(currentRelativeAddress, DirectoryEntrySize);
                // The address of the first named entry inside of this directory.
                uint namedEntryAddress = BitConverter.ToUInt32(dirEntry.Slice(0, sizeof(uint)).GetAsArrayCopy(), startIndex: 0);
                // The ID of the first file in this directory.
                ushort firstFileID = BitConverter.ToUInt16(dirEntry.Slice(sizeof(uint), sizeof(ushort)).GetAsArrayCopy(), startIndex: 0);
                // The ID of the parent of this directory. If folder is root, this will not start with 0xF000.
                ushort parentDirID = BitConverter.ToUInt16(dirEntry.Slice(sizeof(uint) + sizeof(ushort), sizeof(ushort)).GetAsArrayCopy(), startIndex: 0);
                // The ID of the directory that all these files belong to.
                ushort containerDirID = (ushort)(currentRelativeAddress / DirectoryEntrySize + EntryDirectoryFlag);

                ByteSlice namedEntry = RawFNT.Slice((int)namedEntryAddress, RawFNT.Size);
                readNamedEntry(namedEntry, firstFileID);

                void readNamedEntry(ByteSlice namedEntry, ushort fileID)
                {
                    if (namedEntry[0] == 0)
                    {
                        return;
                    }

                    bool   isDirectory = (namedEntry[0] & 0b10000000) > 0;
                    byte   nameLength  = (byte)(namedEntry[0] & 0b01111111);
                    string name        = Encoding.UTF8.GetString(namedEntry.Slice(1, nameLength).GetAsArrayCopy());

                    if (isDirectory)
                    {
                        ushort entryID = BitConverter.ToUInt16(namedEntry.Slice(1 + nameLength, sizeof(ushort)).GetAsArrayCopy(), startIndex: 0);
                        namedEntry.SliceEnd = namedEntry.SliceStart + 1 + nameLength + sizeof(ushort);
                        var directory = new NDSDirectory(this, entryID, name);
                        directories.Add(entryID, directory);

                        // If the parent is not a directory, then this entry represents the root directory (/data/)
                        if ((parentDirID & EntryDirectoryFlag) == 0)
                        {
                            RootDataDirectory = directory;
                        }
                        else
                        {
                            childrenStructure.TryGetValue(containerDirID, out var parent);
                            if (parent == null)
                            {
                                childrenStructure.Add(containerDirID, new List <ushort>()
                                {
                                    entryID
                                });
                            }
                            else
                            {
                                parent.Add(entryID);
                            }
                        }
                    }
                    else
                    {
                        namedEntry.SliceEnd = namedEntry.SliceStart + 1 + nameLength;
                        var file = new NDSFile(this, fileID, name);
                        if (files.ContainsKey(fileID))
                        {
                            files[fileID] = file;
                            Console.WriteLine($"[NDSFilesystem.Initialize] The file {fileID} was repeated!");
                        }
                        else
                        {
                            files.Add(fileID, file);
                        }

                        childrenStructure.TryGetValue(containerDirID, out var parent);
                        if (parent == null)
                        {
                            childrenStructure.Add(containerDirID, new List <ushort>()
                            {
                                fileID
                            });
                        }
                        else
                        {
                            parent.Add(fileID);
                        }
                    }

                    // Read next entry
                    readNamedEntry(RawFNT.Slice(namedEntry.SliceEnd - RawFNT.SliceStart, RawFNT.Size), (ushort)(fileID + 1));
                }
            }

            foreach (var parentRelations in childrenStructure)
            {
                directories.TryGetValue(parentRelations.Key, out var parent);
                if (parent == null)
                {
                    Console.WriteLine($"[NDSFilesystem.Initialize] Parent 0x{parentRelations.Key:X} does not exist as a directory!! (First child: {files[parentRelations.Value.First()].Name})");
                    continue;
                }
                else
                {
                    foreach (var child in parentRelations.Value)
                    {
                        if ((child & EntryDirectoryFlag) > 0)
                        {
                            parent.ChildrenDirectories.Add(directories[child]);
                            directories[child].Parent = parent;
                        }
                        else
                        {
                            parent.ChildrenFiles.Add(files[child]);
                            files[child].Parent = parent;
                        }
                    }
                }
            }
        }
 public VCSChange(VCSCommit parentCommit, NDSFile parentFile, XDeltaPatch rollbackPatch)
 {
     RollbackPatch = rollbackPatch;
     ParentCommit  = parentCommit;
     ParentFile    = parentFile;
 }