Exemple #1
0
 /// <summary>
 /// Disposes the <see cref="RIFFFileReader" /> and the underlying stream.
 /// </summary>
 /// <param name="disposing">
 /// True to release both managed and unmanaged resources; false to release only unmanaged
 /// resources.
 /// </param>
 protected virtual void Dispose(bool disposing)
 {
     if (!_disposed)
     {
         lock (_lockObj)
         {
             if (_binaryFile != null && _closeBinaryFile)
             {
                 _binaryFile.Dispose();
                 _binaryFile = null;
             }
         }
     }
     _disposed = true;
 }
Exemple #2
0
        static void Test2()
        {
            //unzip disk IO test
            File.Delete(@"z:\test.dta");
            Stopwatch sw = new Stopwatch();

            const int Count = 100;

            var ef     = new BinaryFile(@"z:\test.dta", true);
            var writer = ef.GetWriter();
            var reader = ef.GetReader();

            Console.WriteLine("SmartGzip : " + ef.EnableSmartGzip);
            Console.WriteLine("Count : " + Count);

            var str = "                                                                                                          ";

            //string str = "testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest";
            str += str; str += str; str += str; str += str; str += str; str += str;
            str += str; str += str; str += str; str += str; str += str; str += str;
            long length = 123;

            sw.Start();
            for (int i = 0; i < Count; i++)
            {
                writer.Write(0, str, out length);
            }
            sw.Stop();
            Console.WriteLine("write : " + sw.ElapsedMilliseconds + "ms");
            Console.WriteLine("write : " + (Count / (sw.ElapsedMilliseconds + 1) * length + " KB/s"));
            sw.Restart();
            reader.Position = 0;

            string n = string.Empty;

            for (int i = 0; i < Count; i++)
            {
                n = reader.ReadString(0, length);
            }
            sw.Stop();
            ef.Dispose();

            Console.WriteLine("read : " + sw.ElapsedMilliseconds + "ms");
            Console.WriteLine("read : " + (Count / (sw.ElapsedMilliseconds + 1) * length + " KB/s"));
            Console.WriteLine(length);
            Console.WriteLine(length * Count / 1024 + "KB");
            Console.WriteLine(str.Length * Count / 1024 + "KB");
        }
Exemple #3
0
        // Group: File Functions
        // __________________________________________________________________________


        /* Function: LoadBinaryFile
         * Loads the information in <SearchIndex.nd> and returns whether it was successful.  If not all the out parameters will still
         * return objects, they will just be empty.
         */
        public static bool LoadBinaryFile(Path filename, out StringTable <IDObjects.NumberSet> prefixTopicIDs)
        {
            prefixTopicIDs = new StringTable <IDObjects.NumberSet>(KeySettingsForPrefixes);

            BinaryFile binaryFile = new BinaryFile();
            bool       result     = true;

            try
            {
                if (binaryFile.OpenForReading(filename, "2.0") == false)
                {
                    result = false;
                }
                else
                {
                    // [String: Prefix]
                    // [NumberSet: Prefix Topic IDs]
                    // ...
                    // [String: null]

                    for (;;)
                    {
                        string prefix = binaryFile.ReadString();

                        if (prefix == null)
                        {
                            break;
                        }

                        IDObjects.NumberSet topicIDs = binaryFile.ReadNumberSet();
                        prefixTopicIDs.Add(prefix, topicIDs);
                    }
                }
            }
            catch
            { result = false; }
            finally
            { binaryFile.Dispose(); }

            if (result == false)
            {
                prefixTopicIDs.Clear();
            }

            return(result);
        }
Exemple #4
0
        static void Test3()
        {
            File.Delete(@"z:\te.ed");
            var       ef     = new BinaryFile(@"z:\te.ed");
            Stopwatch sw     = new Stopwatch();
            var       writer = ef.GetWriter();
            var       reader = ef.GetReader();
            int       w      = sizeof(int);
            int       count  = 200000;
            int       l      = w * count;

            sw.Start();
            writer.Write(1, count);
            sw.Stop();

            Console.WriteLine(sw.ElapsedMilliseconds);

            var x = new FileInfo(@"z:\te.ed");

            ef.Dispose();

            Console.WriteLine(x.Length == l);
        }
        /* Function: Load
         * Loads the information in <BuildState.nd> and returns whether it was successful.  If not the function will return an empty
         * BuildState object.
         */
        public bool Load(Path filename, out BuildState buildState, out UnprocessedChanges unprocessedChanges)
        {
            // Since we're creating new objects only we have access to them right now and thus we don't need to use Lock() and
            // Unlock().
            buildState         = new BuildState();
            unprocessedChanges = new UnprocessedChanges();

            BinaryFile binaryFile = new BinaryFile();
            bool       result     = true;

            try
            {
                if (binaryFile.OpenForReading(filename, "2.1") == false)
                {
                    result = false;
                }
                else
                {
                    // [Byte: Need to Build Frame Page (0 or 1)]
                    // [Byte: Need to Build Main Style Files (0 or 1)]
                    // [Byte: Need to Build Menu (0 or 1)]
                    // [Byte: Need to Build Main Search Files (0 or 1)]

                    unprocessedChanges.framePage       = (binaryFile.ReadByte() == 1);
                    unprocessedChanges.mainStyleFiles  = (binaryFile.ReadByte() == 1);
                    unprocessedChanges.menu            = (binaryFile.ReadByte() == 1);
                    unprocessedChanges.mainSearchFiles = (binaryFile.ReadByte() == 1);

                    // [NumberSet: Source File IDs to Rebuild]
                    // [NumberSet: Class IDs to Rebuild]
                    // [NumberSet: Image File IDs to Rebuild]
                    // [NumberSet: Style File IDs to Rebuild]
                    // [NumberSet: Source File IDs with Content]
                    // [NumberSet: Class IDs with Content]
                    // [NumberSet: Used Image File IDs]
                    // [NumberSet: Unchanged Image File Use Check IDs]

                    unprocessedChanges.sourceFiles.ReadFrom(binaryFile);
                    unprocessedChanges.classes.ReadFrom(binaryFile);
                    unprocessedChanges.imageFiles.ReadFrom(binaryFile);
                    unprocessedChanges.styleFiles.ReadFrom(binaryFile);
                    buildState.sourceFilesWithContent.ReadFrom(binaryFile);
                    buildState.classesWithContent.ReadFrom(binaryFile);
                    buildState.usedImageFiles.ReadFrom(binaryFile);
                    unprocessedChanges.unchangedImageFileUseChecks.ReadFrom(binaryFile);

                    // [StringSet: Search Prefixes to Rebuild]
                    // [StringSet: Folders to Check for Deletion]

                    unprocessedChanges.searchPrefixes.ReadFrom(binaryFile);
                    unprocessedChanges.possiblyEmptyFolders.ReadFrom(binaryFile);

                    // [String: Menu Data File Type] [NumberSet: Menu Data File Numbers]
                    // [String: Menu Data File Type] [NumberSet: Menu Data File Numbers]
                    // ...
                    // [String: null]

                    string menuDataFileType = binaryFile.ReadString();

                    while (menuDataFileType != null)
                    {
                        Hierarchy hierarchy;

                        if (menuDataFileType == "files")
                        {
                            hierarchy = Hierarchy.File;
                        }
                        else if (menuDataFileType == "classes")
                        {
                            hierarchy = Hierarchy.Class;
                        }
                        else if (menuDataFileType == "database")
                        {
                            hierarchy = Hierarchy.Database;
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }

                        NumberSet menuDataFileNumbers = binaryFile.ReadNumberSet();
                        buildState.usedMenuDataFiles.Add(hierarchy, menuDataFileNumbers);

                        menuDataFileType = binaryFile.ReadString();
                    }
                }
            }
            catch
            { result = false; }
            finally
            { binaryFile.Dispose(); }

            if (result == false)
            {
                buildState         = new BuildState();
                unprocessedChanges = new UnprocessedChanges();
            }

            return(result);
        }
Exemple #6
0
        /* Function: Load
         * Loads the information in <Config.nd> and returns whether it was successful.  If not all the out parameters will still
         * return objects, they will just be empty.
         */
        public bool Load(Path filename, out Config.ProjectInfo projectInfo, out List <Style> styles, out List <FileSourceInfo> fileSourceInfoList)
        {
            projectInfo        = new Config.ProjectInfo();
            styles             = new List <Style>();
            fileSourceInfoList = new List <FileSourceInfo>();

            BinaryFile binaryFile = new BinaryFile();
            bool       result     = true;

            try
            {
                if (binaryFile.OpenForReading(filename, "2.2") == false)
                {
                    result = false;
                }
                else
                {
                    // [String: Project Title or null]
                    // [String: Project Subtitle or null]
                    // [String: Project Copyright or null]
                    // [String: Project Timestamp Code or null]

                    projectInfo.Title = binaryFile.ReadString();
                    projectInfo.TitlePropertyLocation = Config.PropertySource.PreviousRun;

                    projectInfo.Subtitle = binaryFile.ReadString();
                    projectInfo.SubtitlePropertyLocation = Config.PropertySource.PreviousRun;

                    projectInfo.Copyright = binaryFile.ReadString();
                    projectInfo.CopyrightPropertyLocation = Config.PropertySource.PreviousRun;

                    projectInfo.TimestampCode = binaryFile.ReadString();
                    projectInfo.TimestampCodePropertyLocation = Config.PropertySource.PreviousRun;


                    // [String: Style Path]
                    //    (properties)
                    // [String: Style Path]
                    // ...
                    // [String: null]

                    string stylePath = binaryFile.ReadString();

                    while (stylePath != null)
                    {
                        Style style;

                        if (stylePath.EndsWith(".css", StringComparison.OrdinalIgnoreCase))
                        {
                            style = new Styles.CSSOnly(stylePath);
                        }
                        else
                        {
                            style = new Styles.Advanced(stylePath);
                        }

                        styles.Add(style);


                        // [String: Inherit] ... [String: null]

                        string inheritStatement = binaryFile.ReadString();

                        while (inheritStatement != null)
                        {
                            // Find the name in the list of styles so we can connect the objects together properly.  There should only
                            // be one style per name so we can just compare by name.  Also, this list is stored in the order in which
                            // they must be applied, which means inherited styles will appear before the ones that inherit from them,
                            // so we can search the list we've built so far instead of waiting until they're all loaded.
                            Style matchingStyle = null;

                            for (int i = 0; i < styles.Count; i++)
                            {
                                if (string.Compare(inheritStatement, styles[i].Name, StringComparison.OrdinalIgnoreCase) == 0)
                                {
                                    matchingStyle = styles[i];
                                    break;
                                }
                            }

                            // If there's no match just add it as null.
                            style.AddInheritedStyle(inheritStatement, Config.PropertySource.PreviousRun, matchingStyle);

                            inheritStatement = binaryFile.ReadString();
                        }


                        // [String: OnLoad] [Byte: Page Type] ... [String: null]

                        string onLoadStatement = binaryFile.ReadString();

                        while (onLoadStatement != null)
                        {
                            Engine.Styles.PageType pageType = (Engine.Styles.PageType)binaryFile.ReadByte();
                            style.AddOnLoad(onLoadStatement, Config.PropertySource.PreviousRun, pageType);

                            onLoadStatement = binaryFile.ReadString();
                        }


                        // [String: Link] [Byte: Page Type] ... [String: null]

                        string linkStatement = binaryFile.ReadString();

                        while (linkStatement != null)
                        {
                            Engine.Styles.PageType pageType = (Engine.Styles.PageType)binaryFile.ReadByte();
                            style.AddLinkedFile(linkStatement, Config.PropertySource.PreviousRun, pageType);

                            linkStatement = binaryFile.ReadString();
                        }


                        // [String: Home Page or null]

                        string homePage = binaryFile.ReadString();

                        if (homePage != null)
                        {
                            style.SetHomePage(homePage, Config.PropertySource.PreviousRun);
                        }


                        // Next style path
                        stylePath = binaryFile.ReadString();
                    }

                    projectInfo.StyleName = styles[styles.Count - 1].Name;
                    projectInfo.StyleNamePropertyLocation = Config.PropertySource.PreviousRun;


                    // [Int32: Source FileSource Number] [String: Source FileSource UniqueIDString]
                    // [Int32: Source FileSource Number] [String: Source FileSource UniqueIDString]
                    // ...
                    // [Int32: 0]

                    FileSourceInfo fileSourceInfo = new FileSourceInfo();
                    fileSourceInfo.Type = Files.InputType.Source;

                    for (;;)
                    {
                        fileSourceInfo.Number = binaryFile.ReadInt32();

                        if (fileSourceInfo.Number == 0)
                        {
                            break;
                        }

                        fileSourceInfo.UniqueIDString = binaryFile.ReadString();
                        fileSourceInfoList.Add(fileSourceInfo);
                    }

                    // [Int32: Image FileSource Number] [String: Image FileSource UniqueIDString]
                    // [Int32: Image FileSource Number] [String: Image FileSource UniqueIDString]
                    // ...
                    // [Int32: 0]

                    fileSourceInfo.Type = Files.InputType.Image;

                    for (;;)
                    {
                        fileSourceInfo.Number = binaryFile.ReadInt32();

                        if (fileSourceInfo.Number == 0)
                        {
                            break;
                        }

                        fileSourceInfo.UniqueIDString = binaryFile.ReadString();
                        fileSourceInfoList.Add(fileSourceInfo);
                    }
                }
            }
            catch
            { result = false; }
            finally
            { binaryFile.Dispose(); }

            if (result == false)
            {
                projectInfo = new Config.ProjectInfo();
                styles.Clear();
                fileSourceInfoList.Clear();
            }

            return(result);
        }
 public void Dispose()
 {
     BinaryFile?.Dispose();
 }
Exemple #8
0
        static void Test1()
        {
            File.Delete("zip.txt");
            File.Delete("unzip.txt");

            //Console.WriteLine(ef.EnableSmartGzip);

            var smartText = "";

            Random rand = new Random();

            while (smartText.Length < 10000)
            {
                smartText += rand.Next(16).ToString("x");
            }

            while (smartText.Length < 1000000)
            {
                smartText += smartText;
            }
            Console.WriteLine("Test1  Start");
            Console.WriteLine("String.Length = " + smartText.Length);

            Console.WriteLine("-------zip--------");

            var ef1     = new BinaryFile("zip.txt", true, FileShare.ReadWrite);
            var ef2     = new BinaryFile("unzip.txt", false, FileShare.ReadWrite);
            var reader1 = ef1.GetReader();
            var writer1 = ef1.GetWriter();
            var reader2 = ef2.GetReader();
            var writer2 = ef2.GetWriter();

            Console.WriteLine("SmartGzip : " + ef1.EnableSmartGzip);
            Stopwatch sw = new Stopwatch();

            sw.Start();
            writer1.Write(smartText);
            Console.WriteLine("raw = read : " + (reader1.ReadString() == smartText));
            writer1.Write(12);
            sw.Stop();
            Console.WriteLine("zip Time : " + sw.ElapsedMilliseconds + "ms");
            Console.WriteLine("zip Size : " + writer1.BaseWriter.BaseStream.Seek(0, SeekOrigin.End) / 1024 + "KB");

            Console.WriteLine("-------zip--------");
            Console.WriteLine();
            Console.WriteLine("------unzip-------");
            Console.WriteLine("SmartGzip : " + ef2.EnableSmartGzip);
            sw.Reset();
            sw.Start();
            writer2.Write(smartText);
            Console.WriteLine("raw = read : " + (reader2.ReadString() == smartText));
            writer2.Write(12);
            sw.Stop();
            Console.WriteLine("unzip Time : " + sw.ElapsedMilliseconds + "ms");
            Console.WriteLine("unzip Size : " + writer2.BaseWriter.BaseStream.Seek(0, SeekOrigin.End) / 1024 + "KB");

            ef1.Dispose();
            ef2.Dispose();

            File.Delete("zip.txt");
            File.Delete("unzip.txt");
            Console.WriteLine("------unzip-------");
            Console.WriteLine("Test1  End");
        }
Exemple #9
0
        // Group: File Functions
        // __________________________________________________________________________


        /* Function: LoadBinaryFile
         * Loads the information in <BuildState.nd> and returns whether it was successful.  If not the function will return an empty
         * BuildState object.
         */
        public static bool LoadBinaryFile(Path filename, out HTMLBuildState buildState)
        {
            buildState = new HTMLBuildState(createEmptyObjects: false);

            BinaryFile binaryFile = new BinaryFile();
            bool       result     = true;

            try
            {
                if (binaryFile.OpenForReading(filename, "2.0") == false)
                {
                    result = false;
                }
                else
                {
                    // [Byte: Need to Build Frame Page (0 or 1)]
                    // [Byte: Need to Build Main Style Files (0 or 1)]
                    // [Byte: Need to Build Menu (0 or 1)]
                    // [Byte: Need to Build Prefix Index (0 or 1)]

                    buildState.needToBuildFramePage      = (binaryFile.ReadByte() == 1);
                    buildState.needToBuildMainStyleFiles = (binaryFile.ReadByte() == 1);
                    buildState.needToBuildMenu           = (binaryFile.ReadByte() == 1);
                    buildState.needToBuildPrefixIndex    = (binaryFile.ReadByte() == 1);

                    // [NumberSet: Source File IDs to Rebuild]
                    // [NumberSet: Class IDs to Rebuild]
                    // [NumberSet: Source File IDs with Content]
                    // [NumberSet: Class IDs with Content]
                    // [StringSet: Prefixes to Rebuild]
                    // [StringSet: Folders to Check for Deletion]

                    buildState.sourceFilesToRebuild      = binaryFile.ReadNumberSet();
                    buildState.classFilesToRebuild       = binaryFile.ReadNumberSet();
                    buildState.sourceFilesWithContent    = binaryFile.ReadNumberSet();
                    buildState.classFilesWithContent     = binaryFile.ReadNumberSet();
                    buildState.prefixesToRebuild         = binaryFile.ReadStringSet();
                    buildState.foldersToCheckForDeletion = binaryFile.ReadStringSet(Config.Manager.KeySettingsForPaths);

                    // [String: Menu Data File Type] [NumberSet: Menu Data File Numbers]
                    // [String: Menu Data File Type] [NumberSet: Menu Data File Numbers]
                    // ...
                    // [String: null]

                    buildState.usedMenuDataFiles = new StringTable <IDObjects.NumberSet>();
                    string menuDataFileType = binaryFile.ReadString();

                    while (menuDataFileType != null)
                    {
                        IDObjects.NumberSet menuDataFileNumbers = binaryFile.ReadNumberSet();
                        buildState.usedMenuDataFiles.Add(menuDataFileType, menuDataFileNumbers);

                        menuDataFileType = binaryFile.ReadString();
                    }
                }
            }
            catch
            { result = false; }
            finally
            { binaryFile.Dispose(); }

            if (result == false)
            {
                buildState = new HTMLBuildState(createEmptyObjects: true);
            }

            return(result);
        }
Exemple #10
0
        /* Function: Load
         * Loads the information in <Config.nd> and returns whether it was successful.  If not all the out parameters will still
         * return objects, they will just be empty.
         */
        public bool Load(Path filename, out List <Style> styles, out List <FileSourceInfo> fileSourceInfoList)
        {
            styles             = new List <Style>();
            fileSourceInfoList = new List <FileSourceInfo>();

            BinaryFile binaryFile = new BinaryFile();
            bool       result     = true;

            try
            {
                if (binaryFile.OpenForReading(filename, "2.0") == false)
                {
                    result = false;
                }
                else
                {
                    // [String: Style Path]
                    // [String: Style Path]
                    // ...
                    // [String: null]

                    string stylePath = binaryFile.ReadString();

                    while (stylePath != null)
                    {
                        if (stylePath.EndsWith(".css", StringComparison.OrdinalIgnoreCase))
                        {
                            styles.Add(new Styles.CSSOnly(stylePath));
                        }
                        else
                        {
                            styles.Add(new Styles.Advanced(stylePath));
                        }

                        stylePath = binaryFile.ReadString();
                    }

                    // [Int32: Source FileSource Number] [String: Source FileSource UniqueIDString]
                    // [Int32: Source FileSource Number] [String: Source FileSource UniqueIDString]
                    // ...
                    // [Int32: 0]

                    FileSourceInfo fileSourceInfo = new FileSourceInfo();
                    fileSourceInfo.Type = Files.InputType.Source;

                    for (;;)
                    {
                        fileSourceInfo.Number = binaryFile.ReadInt32();

                        if (fileSourceInfo.Number == 0)
                        {
                            break;
                        }

                        fileSourceInfo.UniqueIDString = binaryFile.ReadString();
                        fileSourceInfoList.Add(fileSourceInfo);
                    }

                    // [Int32: Image FileSource Number] [String: Image FileSource UniqueIDString]
                    // [Int32: Image FileSource Number] [String: Image FileSource UniqueIDString]
                    // ...
                    // [Int32: 0]

                    fileSourceInfo.Type = Files.InputType.Image;

                    for (;;)
                    {
                        fileSourceInfo.Number = binaryFile.ReadInt32();

                        if (fileSourceInfo.Number == 0)
                        {
                            break;
                        }

                        fileSourceInfo.UniqueIDString = binaryFile.ReadString();
                        fileSourceInfoList.Add(fileSourceInfo);
                    }
                }
            }
            catch
            { result = false; }
            finally
            { binaryFile.Dispose(); }

            if (result == false)
            {
                styles.Clear();
                fileSourceInfoList.Clear();
            }

            return(result);
        }
        /// <summary>
        /// Loads a ROM from the given path if supported
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileSystem"></param>
        /// <returns>The ROM that was loaded, or null if the given path is not supported</returns>
        public static async Task <IModTarget?> LoadRom(string path, IFileSystem fileSystem)
        {
            if (fileSystem.FileExists(path))
            {
                bool is3dsRom;

                BinaryFile?file        = null;
                bool       disposeFile = true;
                try
                {
                    file     = new BinaryFile(path, fileSystem);
                    is3dsRom = await ThreeDsRom.IsThreeDsRom(file).ConfigureAwait(false);

                    if (is3dsRom)
                    {
                        var rom = await ThreeDsRom.Load(file).ConfigureAwait(false);

                        // To-do: check title ID to ensure it's a PSMD ROM
                        // For now, just assume it's supported by our class

                        disposeFile = false;
                        return(new PsmdRom(rom));
                    }
                    else
                    {
                        return(null);
                    }
                }
                finally
                {
                    if (disposeFile && file != null)
                    {
                        file.Dispose();
                    }
                }
            }
            else if (fileSystem.DirectoryExists(path))
            {
                // Is it a Switch ROM or a 3DS ROM?
                // To-do: check title ID to ensure it's the correct type of ROM
                if (fileSystem.FileExists(Path.Combine(path, "ExeFS", "code.bin")))
                {
                    // It's a 3DS ROM
                    return(new PsmdRom(path, fileSystem));
                }
                else if (fileSystem.FileExists(Path.Combine(path, "exefs", "main")))
                {
                    // It's a Switch ROM
                    return(new RtdxRom(path, fileSystem));
                }
                else
                {
                    // We couldn't find the executable and can make no conclusion
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }