Exemple #1
0
        public void ParseDiskFile(byte[] data)
        {
            using (var ms = new MemoryStream(data))
            {
                ParseHeader(ms);     // 3 bytes
                ParseDiskHeader(ms); // 6 bytes
                BankName      = ms.ReadNullTerminatedString(BANK_NAME_LENGTH);
                BankName2     = ms.ReadNullTerminatedString(BANK_NAME2_LENGTH);
                CompanyName   = ms.ReadNullTerminatedString(COMPANY_NAME_LENGTH);
                CompanyUrl    = ms.ReadNullTerminatedString(COMPANY_URL_LENGTH);
                NumParameters = ParseNumParameters(ms);

                for (var i = 0; i < NUM_DISK_PRESETS; i++)
                {
                    var workaround = false;
                    var preset     = new SpirePreset(NumParameters);

                    if (i + 1 == NUM_DISK_PRESETS)
                    {
                        workaround = true;
                    }

                    preset.ParseFromBank(ms, workaround);

                    Presets.Add(preset);
                }

                Debug.WriteLine($"Current offset: {ms.Position}, total length: {ms.Length}");
            }
        }
Exemple #2
0
        public void FillMissingParametersFrom(SpirePreset preset)
        {
            for (var i = NumParameters; i < preset.NumParameters; i++)
            {
                SetParameter(i, preset.GetParameter(i));
            }

            NumParameters = preset.NumParameters;
        }
Exemple #3
0
        /// <summary>
        /// Creates a copy of the program and sets the name to "init".
        /// </summary>
        /// <returns></returns>
        public SpirePreset CreateBlank()
        {
            var sp = new SpirePreset {
                ProgramName = "init"
            };

            StuffAfterProgramName.CopyTo(sp.StuffAfterProgramName, 0);
            ProgramData.CopyTo(sp.ProgramData, 0);

            return(sp);
        }
Exemple #4
0
        public int?FindPresetIndex(SpirePreset preset)
        {
            foreach (var p in Presets)
            {
                if (p.ProgramData.SequenceEqual(preset.ProgramData))
                {
                    return(Presets.IndexOf(p));
                }
            }

            return(null);
        }
Exemple #5
0
        public byte[] GenerateMemoryBank(SpirePreset preset, SpireJsonConfig config = null)
        {
            PrepareForExport(preset);

            using (var ms = new MemoryStream())
            {
                ms.Write(SBF_HEADER);
                ms.Write(preset.NumParameters);

                preset.Export(ms, true);
                ms.Write(SelectedProgramIndex);

                // Original bank data
                for (var i = 0; i < NUM_DISK_PRESETS; i++)
                {
                    Presets[i].Export(ms);
                }

                // Modified bank data
                for (var i = 0; i < NUM_DISK_PRESETS; i++)
                {
                    if (i == SelectedProgramIndex)
                    {
                        // Export the actual preset
                        preset.Export(ms);
                    }
                    else
                    {
                        Presets[i].Export(ms);
                    }
                }

                if (config != null)
                {
                    ms.WriteByte(0x00);
                    ms.WriteByte(0x7C);

                    var json      = JsonConvert.SerializeObject(config);
                    var jsonBytes = Encoding.UTF8.GetBytes(json);
                    ms.Write(jsonBytes, 0, jsonBytes.Length);
                    ms.WriteByte(0x00);
                }
                return(ms.ToArray());
            }
        }
Exemple #6
0
        private void PrepareForExport(SpirePreset preset)
        {
            var fp = new SpirePreset();

            fp.ParseDiskFile(VendorResources.RevealSound_Spire_Blank);

            preset.FillMissingParametersFrom(fp);
            if (Presets.Count == 0)
            {
                // Generate blank bank
                for (var i = 0; i < NUM_DISK_PRESETS; i++)
                {
                    Presets.Add(preset.CreateBlank());
                }
            }

            if (Presets.Count != NUM_DISK_PRESETS)
            {
                throw new SpireException($"The number of presets is expected to be {NUM_DISK_PRESETS}, but " +
                                         $"the actual number is {Presets.Count}. This is either a bug in " +
                                         $"PresetMagician, a corrupt preset bank or an unknown format. Please report " +
                                         "this as a bug and include the affected file(s).");
            }

            var idx = FindPresetIndex(preset);

            if (idx != null)
            {
                SetSelectedProgramIndex((int)idx);
            }
            else
            {
                SetSelectedProgramIndex(0);
                Presets[0] = preset;
            }

            foreach (var p in Presets)
            {
                p.FillMissingParametersFrom(fp);
            }
        }