コード例 #1
0
        public static void CompressMMFiles()
        {
            /// Re-Compressing the files back into a compressed rom is the most expensive job during seed creation.
            /// To speed up, we compress files in parallel with a sorted list to reduce idle threads at the end.

            var startTime = DateTime.Now;

            // sorting the list with .Where().ToList() => OrderByDescending().ToList only takes (~ 0.400 miliseconds) on Isghj's computer
            var sortedCompressibleFiles = RomData.MMFileList.Where(file => file.IsCompressed && file.WasEdited).ToList();

            sortedCompressibleFiles = sortedCompressibleFiles.OrderByDescending(file => file.Data.Length).ToList();

            // Debug.WriteLine($" sort the list with Sort() : [{(DateTime.Now).Subtract(startTime).TotalMilliseconds} (ms)]");

            // lower priority so that the rando can't lock a badly scheduled CPU by using 100%
            var previousThreadPriority = Thread.CurrentThread.Priority;

            Thread.CurrentThread.Priority = ThreadPriority.Lowest;
            // yaz0 encode all of the files for the rom
            Parallel.ForEach(sortedCompressibleFiles.AsParallel().AsOrdered(), file =>
            {
                //var yazTime = DateTime.Now;
                file.Data = Yaz.EncodeAndCopy(file.Data);
                //Debug.WriteLine($" size: [{file.Data.Length}] time to complete compression : [{(DateTime.Now).Subtract(yazTime).TotalMilliseconds} (ms)]");
            });
            // this thread is borrowed, we don't want it to always be the lowest priority, return to previous state
            Thread.CurrentThread.Priority = previousThreadPriority;

            Debug.WriteLine($" compress all files time : [{(DateTime.Now).Subtract(startTime).TotalMilliseconds} (ms)]");
        }
コード例 #2
0
        public static byte[] BuildROM()
        {
            // yaz0 encode all of the files for the rom
            Parallel.ForEach(RomData.MMFileList, file =>
            {
                if (file.IsCompressed && file.WasEdited)
                {
                    // lower priority so that the rando can't lock a badly scheduled CPU by using 100%
                    var previousThreadPriority    = Thread.CurrentThread.Priority;
                    Thread.CurrentThread.Priority = ThreadPriority.Lowest;
                    file.Data = Yaz.EncodeAndCopy(file.Data);
                    // this thread is borrowed, we don't want it to always be the lowest priority, return to previous state
                    Thread.CurrentThread.Priority = previousThreadPriority;
                }
            });
            byte[] ROM     = new byte[0x2000000];
            int    ROMAddr = 0;

            // write all files to rom
            for (int i = 0; i < RomData.MMFileList.Count; i++)
            {
                if (RomData.MMFileList[i].Cmp_Addr == -1)
                {
                    continue;
                }
                RomData.MMFileList[i].Cmp_Addr = ROMAddr;
                int fileLength = RomData.MMFileList[i].Data.Length;
                if (RomData.MMFileList[i].IsCompressed)
                {
                    RomData.MMFileList[i].Cmp_End = ROMAddr + fileLength;
                }
                if (ROMAddr + fileLength > ROM.Length) // rom too small
                {
                    // assuming the largest file isn't the last one, we still want some extra space for further files
                    //  padding will reduce the requirements for further resizes
                    int    expansionIncrementSize = 0x40000; // 1mb might be too large, not sure if there is a hardware compatiblity issue here
                    int    expansionLength        = (((ROMAddr + fileLength - ROM.Length) / expansionIncrementSize) + 1) * expansionIncrementSize;
                    byte[] newROM = new byte[ROM.Length + expansionLength];
                    Buffer.BlockCopy(ROM, 0, newROM, 0, ROM.Length);
                    Buffer.BlockCopy(new byte[expansionLength], 0, newROM, ROM.Length, expansionLength);
                    ROM = newROM;
                    Debug.WriteLine("*** Expanding rom to size 0x" + ROM.Length.ToString("X2") + "***");
                }

                ReadWriteUtils.Arr_Insert(RomData.MMFileList[i].Data, 0, fileLength, ROM, ROMAddr);
                ROMAddr += fileLength;
            }
            SequenceUtils.UpdateBankInstrumentPointers(ROM);
            UpdateFileTable(ROM);
            SignROM(ROM);
            FixCRC(ROM);

            return(ROM);
        }