public void BlockData()
        {
            byte[] testData = new byte[(512 * 1024) + 21];
            for (int i = 0; i < testData.Length; ++i)
            {
                testData[i] = (byte)(i % 33);
            }

            MemoryStream fsImage = new MemoryStream();

            SquashFileSystemBuilder builder = new SquashFileSystemBuilder();

            builder.AddFile(@"file", new MemoryStream(testData));
            builder.Build(fsImage);

            SquashFileSystemReader reader = new SquashFileSystemReader(fsImage);

            using (Stream fs = reader.OpenFile("file", FileMode.Open))
            {
                byte[] buffer  = new byte[(512 * 1024) + 1024];
                int    numRead = fs.Read(buffer, 0, buffer.Length);

                Assert.Equal(testData.Length, numRead);
                for (int i = 0; i < testData.Length; ++i)
                {
                    Assert.Equal(testData[i], buffer[i] /*, "Data differs at index " + i*/);
                }
            }
        }
Ejemplo n.º 2
0
        internal static NodeInfo GetNodeInfo(FileInfo fi)
        {
            var ni = new NodeInfo();

            ni.FullName = fi.FullName;
            using (FileStream isoStream = File.OpenRead(fi.FullName))
            {
                SquashFileSystemReader readCart = new SquashFileSystemReader(isoStream);

                using (Stream entryFs = readCart.OpenFile("title.png", FileMode.Open))
                {
                    ni.BoxArt = Image.FromStream(entryFs);
                }
                using (Stream entryFs = readCart.OpenFile("cartridge.xml", FileMode.Open))
                {
                    byte[] bytes = new byte[entryFs.Length];
                    entryFs.Position = 0;
                    entryFs.Read(bytes, 0, (int)entryFs.Length);
                    string data = Encoding.ASCII.GetString(bytes);

                    Match m = Regex.Match(data, @"<title>\s*(.+?)\s*</title>");
                    ni.Title = m.Success ? m.Groups[1].Value : "No Title Found";
                    m        = Regex.Match(data, @"<desc>\s*(.+?)\s*</desc>");
                    ni.Desc  = m.Success ? m.Groups[1].Value : "No Desc Found";
                }

                var emuFiles = readCart.GetFiles("emu");
                if (emuFiles.Length > 0)
                {
                    ni.Core = Path.GetFileName(emuFiles[0]);
                }
            }
            return(ni);
        }
        public void Detect()
        {
            MemoryStream ms = new MemoryStream(new byte[1000]);

            Assert.False(SquashFileSystemReader.Detect(ms));

            ms = new MemoryStream(new byte[10]);
            Assert.False(SquashFileSystemReader.Detect(ms));

            MemoryStream            emptyFs = new MemoryStream();
            SquashFileSystemBuilder builder = new SquashFileSystemBuilder();

            builder.Build(emptyFs);
            Assert.True(SquashFileSystemReader.Detect(emptyFs));
        }
        public void CreateDirs()
        {
            MemoryStream fsImage = new MemoryStream();

            SquashFileSystemBuilder builder = new SquashFileSystemBuilder();

            builder.AddFile(@"\adir\anotherdir\file", new MemoryStream(new byte[] { 1, 2, 3, 4 }));
            builder.Build(fsImage);

            SquashFileSystemReader reader = new SquashFileSystemReader(fsImage);

            Assert.True(reader.DirectoryExists(@"adir"));
            Assert.True(reader.DirectoryExists(@"adir\anotherdir"));
            Assert.True(reader.FileExists(@"adir\anotherdir\file"));
        }
        public void SingleFile()
        {
            MemoryStream fsImage = new MemoryStream();

            SquashFileSystemBuilder builder = new SquashFileSystemBuilder();

            builder.AddFile("file", new MemoryStream(new byte[] { 1, 2, 3, 4 }));
            builder.Build(fsImage);

            SquashFileSystemReader reader = new SquashFileSystemReader(fsImage);

            Assert.Equal(1, reader.GetFileSystemEntries("\\").Length);
            Assert.Equal(4, reader.GetFileLength("file"));
            Assert.True(reader.FileExists("file"));
            Assert.False(reader.DirectoryExists("file"));
            Assert.False(reader.FileExists("otherfile"));
        }
        public void FragmentData()
        {
            MemoryStream fsImage = new MemoryStream();

            SquashFileSystemBuilder builder = new SquashFileSystemBuilder();

            builder.AddFile(@"file", new MemoryStream(new byte[] { 1, 2, 3, 4 }));
            builder.Build(fsImage);

            SquashFileSystemReader reader = new SquashFileSystemReader(fsImage);

            using (Stream fs = reader.OpenFile("file", FileMode.Open))
            {
                byte[] buffer  = new byte[100];
                int    numRead = fs.Read(buffer, 0, 100);

                Assert.Equal(4, numRead);
                Assert.Equal(1, buffer[0]);
                Assert.Equal(2, buffer[1]);
                Assert.Equal(3, buffer[2]);
                Assert.Equal(4, buffer[3]);
            }
        }
        public void Defaults()
        {
            MemoryStream fsImage = new MemoryStream();

            SquashFileSystemBuilder builder = new SquashFileSystemBuilder();

            builder.AddFile(@"file", new MemoryStream(new byte[] { 1, 2, 3, 4 }));
            builder.AddDirectory(@"dir");

            builder.DefaultUser                 = 1000;
            builder.DefaultGroup                = 1234;
            builder.DefaultFilePermissions      = UnixFilePermissions.OwnerAll;
            builder.DefaultDirectoryPermissions = UnixFilePermissions.GroupAll;

            builder.AddFile("file2", new MemoryStream());
            builder.AddDirectory("dir2");

            builder.Build(fsImage);

            SquashFileSystemReader reader = new SquashFileSystemReader(fsImage);

            Assert.Equal(0, reader.GetUnixFileInfo("file").UserId);
            Assert.Equal(0, reader.GetUnixFileInfo("file").GroupId);
            Assert.Equal(UnixFilePermissions.OwnerRead | UnixFilePermissions.OwnerWrite | UnixFilePermissions.GroupRead | UnixFilePermissions.GroupWrite, reader.GetUnixFileInfo("file").Permissions);

            Assert.Equal(0, reader.GetUnixFileInfo("dir").UserId);
            Assert.Equal(0, reader.GetUnixFileInfo("dir").GroupId);
            Assert.Equal(UnixFilePermissions.OwnerAll | UnixFilePermissions.GroupRead | UnixFilePermissions.GroupExecute | UnixFilePermissions.OthersRead | UnixFilePermissions.OthersExecute, reader.GetUnixFileInfo("dir").Permissions);

            Assert.Equal(1000, reader.GetUnixFileInfo("file2").UserId);
            Assert.Equal(1234, reader.GetUnixFileInfo("file2").GroupId);
            Assert.Equal(UnixFilePermissions.OwnerAll, reader.GetUnixFileInfo("file2").Permissions);

            Assert.Equal(1000, reader.GetUnixFileInfo("dir2").UserId);
            Assert.Equal(1234, reader.GetUnixFileInfo("dir2").GroupId);
            Assert.Equal(UnixFilePermissions.GroupAll, reader.GetUnixFileInfo("dir2").Permissions);
        }
Ejemplo n.º 8
0
        /// <summary>Explodes the UCE file and places individual files in repsective directories (coverart, bezelart, etc.).</summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="overwrite">if set to <c>true</c> [overwrite].</param>
        /// <returns></returns>
        internal static VaultRecordCSV_ViewModel ExplodeUCE(string fileName, string defaultGenre = "Unknown", bool overwrite = false)
        {
            var    vCart            = new VaultRecordCSV_ViewModel();
            string boxArtFileName   = "";
            string bezelArtFileName = "";
            string emulatorFileName = "";
            string romFileName      = "";

            vCart.Genre = defaultGenre;

            using (FileStream isoStream = File.OpenRead(fileName))
            {
                SquashFileSystemReader readCart = new SquashFileSystemReader(isoStream);

                if (readCart.FileExists($"cartridge.xml"))
                {
                    using (Stream entryFs = readCart.OpenFile("cartridge.xml", FileMode.Open))
                    {
                        byte[] bytes = new byte[entryFs.Length];
                        entryFs.Position = 0;
                        entryFs.Read(bytes, 0, (int)entryFs.Length);
                        string data = Encoding.ASCII.GetString(bytes);

                        Match m = Regex.Match(data, @"<title>\s*(.+?)\s*</title>");
                        vCart.Title       = m.Success ? m.Groups[1].Value : "No Title Found";
                        m                 = Regex.Match(data, @"<desc>\s*(.+?)\s*</desc>");
                        vCart.Description = m.Success ? m.Groups[1].Value : "No Desc Found";
                    }

                    if (readCart.FileExists($"exec.sh"))
                    {
                        using (Stream entryFs = readCart.OpenFile("exec.sh", FileMode.Open))
                        {
                            byte[] bytes = new byte[entryFs.Length];
                            entryFs.Position = 0;
                            entryFs.Read(bytes, 0, (int)entryFs.Length);
                            string data = Encoding.ASCII.GetString(bytes);
                            //Parse out Bezel FileName
                            var bezelArtData  = data.Split(new string[] { " /tmp" }, StringSplitOptions.None)[0];
                            var indexOfBoxArt = bezelArtData.kIndexOf("./boxart/", System.Globalization.CompareOptions.IgnoreCase);
                            bezelArtFileName = bezelArtData.Substring(indexOfBoxArt + 9).TrimEnd(new char[] { ' ', '\"' });

                            // /emulator/retroplayer ./emu/fceumm_libretro.so "./roms/Top gun.nes"

                            var emulatorData    = data.Split(new string[] { "./roms" }, StringSplitOptions.None);
                            var indexOfEmulator = emulatorData[0].kIndexOf(" ./emu/", System.Globalization.CompareOptions.IgnoreCase);
                            emulatorFileName       = emulatorData[0].Substring(indexOfEmulator + 7).TrimEnd(new char[] { ' ', '\"' });
                            vCart.EmulatorCoreFile = emulatorFileName;

                            var indexOfROM = emulatorData[1].kIndexOf("\n", System.Globalization.CompareOptions.IgnoreCase);
                            romFileName   = emulatorData[1].Substring(1, indexOfROM - 1).TrimEnd(new char[] { ' ', '\"' });
                            vCart.ROMFile = romFileName;
                        }


                        //Cover Art (Boxart)
                        if (readCart.FileExists($"title.png"))
                        {
                            vCart.CoverArtFile = $"{_Settings.DefaultFolder_BoxArt}\\{vCart.Title.Replace(" ", "_")}_cover.png";
                            if (!File.Exists(vCart.CoverArtFile) || overwrite)
                            {
                                using (Stream bxArtFile = readCart.OpenFile($"title.png", FileMode.Open))
                                {
                                    Image.FromStream(bxArtFile).Save(vCart.CoverArtFile);
                                }
                            }
                            else
                            {
                                throw new Exception($"File {vCart.CoverArtFile} already exists in Vault");
                            }
                        }
                        //Bezel Art
                        if (readCart.FileExists($"boxart\\{bezelArtFileName}"))
                        {
                            vCart.BezelArtFile = $"{_Settings.DefaultFolder_BezelArt}\\{vCart.Title.Replace(" ", "_")}_bezel.png";
                            if (!File.Exists(vCart.BezelArtFile) || overwrite)
                            {
                                using (Stream bzArtFile = readCart.OpenFile($"boxart\\{bezelArtFileName}", FileMode.Open))
                                {
                                    Image.FromStream(bzArtFile).Save(vCart.BezelArtFile);
                                }
                            }
                            else
                            {
                                throw new Exception($"File {vCart.BezelArtFile} already exists in Vault");
                            }
                        }

                        //Emulator
                        if (readCart.FileExists($"emu\\{emulatorFileName}"))
                        {
                            using (Stream emuFile = readCart.OpenFile($"emu\\{emulatorFileName}", FileMode.Open))
                            {
                                vCart.EmulatorCoreFile = $"{_Settings.DefaultFolder_Cores}\\{emulatorFileName}";
                                if (!File.Exists(vCart.EmulatorCoreFile))
                                {
                                    using (var fileStream = File.Create(vCart.EmulatorCoreFile))
                                    {
                                        emuFile.Seek(0, SeekOrigin.Begin);
                                        emuFile.CopyTo(fileStream);
                                    }
                                }
                            }
                        }

                        //ROM
                        if (readCart.FileExists($"roms\\{romFileName}"))
                        {
                            using (Stream romFile = readCart.OpenFile($"roms\\{romFileName}", FileMode.Open))
                            {
                                vCart.ROMFile = $"{_Settings.DefaultFolder_ROMS}\\{romFileName.Replace(" ", "_")}";
                                if (!File.Exists(vCart.ROMFile) || overwrite)
                                {
                                    using (var fileStream = File.Create(vCart.ROMFile))
                                    {
                                        romFile.Seek(0, SeekOrigin.Begin);
                                        romFile.CopyTo(fileStream);
                                    }
                                }
                                else
                                {
                                    throw new Exception($"File {vCart.BezelArtFile} already exists in Vault");
                                }
                            }
                        }
                    } // end if exec.sh exists
                }     // end if cartridge.xml exists
            }

            vCart.Index = -1;
            return(vCart);
        }