|
| 1 | +using OpenLoco.Dat.Data; |
| 2 | +using OpenLoco.Dat.FileParsing; |
| 3 | +using OpenLoco.Gui.Models; |
| 4 | +using ReactiveUI.Fody.Helpers; |
| 5 | +using System.IO; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace OpenLoco.Gui.ViewModels |
| 9 | +{ |
| 10 | + public class MusicViewModel : BaseLocoFileViewModel |
| 11 | + { |
| 12 | + public MusicViewModel(FileSystemItem currentFile, ObjectEditorModel model) |
| 13 | + : base(currentFile, model) |
| 14 | + => Load(); |
| 15 | + |
| 16 | + public override void Load() |
| 17 | + { |
| 18 | + var (header, data) = SawyerStreamReader.LoadWavFile(CurrentFile.Filename); |
| 19 | + SoundViewModel = new SoundViewModel( |
| 20 | + GetDisplayName(CurrentFile.DisplayName), |
| 21 | + header, |
| 22 | + data); |
| 23 | + } |
| 24 | + |
| 25 | + string GetDisplayName(string filename) |
| 26 | + { |
| 27 | + if (OriginalDataFiles.Music.TryGetValue(CurrentFile.DisplayName, out var musicName)) |
| 28 | + { |
| 29 | + return $"{musicName} ({CurrentFile.DisplayName})"; |
| 30 | + } |
| 31 | + |
| 32 | + if (OriginalDataFiles.MiscellaneousTracks.TryGetValue(CurrentFile.DisplayName, out var miscTrackName)) |
| 33 | + { |
| 34 | + return $"{miscTrackName} ({CurrentFile.DisplayName})"; |
| 35 | + } |
| 36 | + |
| 37 | + return CurrentFile.DisplayName; |
| 38 | + } |
| 39 | + |
| 40 | + [Reactive] |
| 41 | + public SoundViewModel SoundViewModel { get; set; } |
| 42 | + |
| 43 | + public override void Save() |
| 44 | + { |
| 45 | + var savePath = CurrentFile.FileLocation == FileLocation.Local |
| 46 | + ? Path.Combine(Model.Settings.DataDirectory, CurrentFile.Filename) |
| 47 | + : Path.Combine(Model.Settings.DownloadFolder, Path.ChangeExtension(CurrentFile.DisplayName, ".dat")); |
| 48 | + |
| 49 | + Logger?.Info($"Saving music to {savePath}"); |
| 50 | + var bytes = SawyerStreamWriter.SaveMusicToDat(SoundViewModel.Header, SoundViewModel.Data); |
| 51 | + File.WriteAllBytes(savePath, bytes); |
| 52 | + } |
| 53 | + |
| 54 | + public override void SaveAs() |
| 55 | + { |
| 56 | + var saveFile = Task.Run(async () => await PlatformSpecific.SaveFilePicker(PlatformSpecific.DatFileTypes)).Result; |
| 57 | + if (saveFile == null) |
| 58 | + { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + var savePath = saveFile.Path.LocalPath; |
| 63 | + Logger?.Info($"Saving music to {savePath}"); |
| 64 | + var bytes = SawyerStreamWriter.SaveMusicToDat(SoundViewModel.Header, SoundViewModel.Data); |
| 65 | + File.WriteAllBytes(savePath, bytes); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments