コード例 #1
0
ファイル: LoadSave.cs プロジェクト: DJuttmann/SM3E
        // Write all data to the Rom.
        public void Save()
        {
            if (!ProjectLoaded)
            {
                return;
            }

            ReallocateAll();
            RepointAll();
            List <Data> objects = CurrentRom.AllData;

            objects.Sort((x, y) => x.StartAddressPC - y.StartAddressPC);

            Stream output  = new FileStream(ProjectPath + RomFileName, FileMode.Create);
            int    address = 0;

            for (int n = 0; n < objects.Count; n++)
            {
                if (address < objects [n].StartAddressPC)
                {
                    CurrentRom.Seek(address);
                    CurrentRom.WriteToFile(output, objects [n].StartAddressPC - address);
                    address = objects [n].StartAddressPC;
                }
                if (address == objects [n].StartAddressPC)
                {
                    int  oldAddress  = address;
                    long oldPosition = output.Position;
                    objects [n].WriteToROM(output, ref address);
                    if (address - oldAddress != output.Position - oldPosition)
                    {
                        Logging.WriteLine("Write error: incorrect # bytes written for " +
                                          objects [n].GetType().ToString() + " at " +
                                          Tools.IntToHex(objects [n].StartAddressPC) +
                                          " - should be " + (address - oldAddress) +
                                          " instead of " + (output.Position - oldPosition));
                    }
                    if (address != output.Position)
                    {
                        Logging.WriteLine("Desync: " + address + " ~ " + output.Position);
                    }
                }
                else
                {
                    Logging.WriteLine("Write error: Invalid address for " +
                                      objects [n].GetType().ToString() + " at " +
                                      Tools.IntToHex(objects [n].StartAddressPC) +
                                      " - output stream already at " + Tools.IntToHex(address));
                }
            }

            // Round address up to multiple of bank size and write remaining data up to there.
            int roundedSize = Math.Max(CurrentRom.Data.Length, (address + 0x7FFF) & ~0x7FFF);

            if (address < roundedSize)
            {
                CurrentRom.Seek(address);
                CurrentRom.WriteToFile(output, roundedSize - address);
            }
            output.Close();

            WriteProjectFileXml();
            ChangesMade = false;
            ProjectSaved?.Invoke(this, null);
        }