internal static bool Prefix(ZEventListener __instance, IVisitor visitor) { Mod mod = Mod.Instance; if (!(visitor is NGUIComponentInspector)) { return(true); } NGUIComponentInspector inspector = visitor as NGUIComponentInspector; if (!__instance.eventName_.StartsWith(CustomDataInfo.GetPrefix <MusicTrack>())) { return(true); } visitor.Visit("eventName_", ref __instance.eventName_, false, null); visitor.Visit("delay_", ref __instance.delay_, false, null); var isEditing = inspector.isEditing_; var data = mod.Variables.CachedMusicTrack.GetOrCreate(__instance, () => new MusicTrack()); if (data.LastWrittenData != __instance.eventName_) { data.ReadObject(__instance); data.LastWrittenData = __instance.eventName_; data.EmbedFile = (data.Embedded.Length > 0 ? "Embedded" : ""); data.LastWritten = data.Clone(); } else if (!isEditing) { var anyChanges = false; var old = data.LastWritten; if (data.Name != old.Name || data.DownloadUrl != old.DownloadUrl || data.FileType != old.FileType) { anyChanges = true; } if (data.EmbedFile != old.EmbedFile) { var newRef = data.EmbedFile; if (newRef?.Length == 0) { data.Embedded = new byte[0]; anyChanges = true; } else { try { newRef = newRef.Trim('"', '\''); var extension = Path.GetExtension(newRef); var file = FileEx.ReadAllBytes(newRef); data.Embedded = file ?? throw new Exception("Missing file"); data.FileType = extension; data.DownloadUrl = ""; anyChanges = true; } catch (Exception e) { data.Embedded = new byte[0]; data.FileType = ".mp3"; anyChanges = true; MessageBox.Create($"Failed to embed {newRef} because {e}", "TRACK MUSIC ERROR") .SetButtons(MessageButtons.Ok) .Show(); Mod.Instance.Logger.Error($"Failed to embed {newRef} because {e}"); } } } if (anyChanges) { data.FileLocation = null; data.Attempted = false; data.EmbedFile = (data.Embedded.Length > 0 ? "Embedded" : ""); data.NewVersion(); data.WriteObject(__instance); data.LastWrittenData = __instance.eventName_; data.LastWritten = data.Clone(); var lastTrackName = mod.Variables.CurrentTrackName; if (lastTrackName == old.Name) { mod.SoundPlayer.StopCustomMusic(); } mod.SoundPlayer.DownloadAllTracks(); if (lastTrackName == data.Name || mod.SoundPlayer.GetMusicChoiceValue(G.Sys.LevelEditor_.WorkingSettings_.gameObject, "Level") == data.Name) { mod.SoundPlayer.PlayTrack(data.Name, 0f); } } } visitor.Visit("Name", ref data.Name, null); visitor.Visit("Type", ref data.FileType, null); visitor.Visit("Embed File", ref data.EmbedFile, mod.Variables.MusicTrackOptions); visitor.VisitAction("Select File", () => { var dlgOpen = new System.Windows.Forms.OpenFileDialog { Filter = "Music file (*.mp3, *.wav, *.aiff)|*.mp3;*.wav;*.aiff|All Files (*.*)|*.*", SupportMultiDottedExtensions = true, RestoreDirectory = true, Title = "Select a music file", CheckFileExists = true, CheckPathExists = true }; if (dlgOpen.ShowDialog() == System.Windows.Forms.DialogResult.OK) { data.EmbedFile = dlgOpen.FileName; } }, mod.Variables.MusicTrackButtonOptions); visitor.Visit("Download URL", ref data.DownloadUrl, null); var Error = data.GetError(); if (Error == null) { Error = "None".Colorize(Colors.white); } else { Error = Error.Colorize(Colors.red); } //visitor.Visit("Error", ref Error, null); visitor.VisualLabel($"Error: {Error}".Colorize(Colors.white)); return(false); }
internal static bool Prefix(ZEventListener __instance, IVisitor visitor) { Mod mod = Mod.Instance; if (!(visitor is NGUIComponentInspector)) { return(true); } NGUIComponentInspector inspector = visitor as NGUIComponentInspector; if (!__instance.eventName_.StartsWith(CustomDataInfo.GetPrefix <MusicChoice>())) { return(true); } visitor.Visit("eventName_", ref __instance.eventName_, false, null); visitor.Visit("delay_", ref __instance.delay_, false, null); var isEditing = inspector.isEditing_; var data = mod.Variables.CachedMusicChoice.GetOrCreate(__instance, () => new MusicChoice()); if (data.LastWrittenData != __instance.eventName_) { data.ReadObject(__instance); data.LastWrittenData = __instance.eventName_; data.LastWritten = data.Clone(); } else if (!isEditing) { var anyChanges = false; var old = data.LastWritten; if (data.Choices.Count != old.Choices.Count) { anyChanges = true; } foreach (var newChoice in data.Choices) { if (!old.Choices.ContainsKey(newChoice.Key) || old.Choices[newChoice.Key].Track != newChoice.Value.Track) { anyChanges = true; break; } } if (anyChanges) { data.WriteObject(__instance); data.LastWrittenData = __instance.eventName_; data.LastWritten = data.Clone(); } } for (int index = 0; index < data.Choices.Count; index++) { string key = data.Choices.Keys.ToArray()[index]; var track = data.Choices[key].Track; visitor.Visit($"{key} Track", ref track, null); } return(false); }
internal static bool Prefix(NGUIComponentInspector __instance) { if (__instance.ISerializable_ is ZEventListener listener && listener.eventName_.StartsWith(CustomDataInfo.GetPrefix <MusicChoice>())) { if (G.Sys.LevelEditor_.StartNewToolJobOfType(typeof(RemoveMusicChoiceTool), false) is RemoveMusicChoiceTool removeTool) { var ser = __instance.iSerializables_; removeTool.SetComponents(ser.Cast <Component>().ToArray()); } return(false); } return(true); }