public GameObject CreateTrack() { GameObject gameObject = Resource.LoadPrefabInstance("Group", true); gameObject.GetComponent <CustomName>().customName_ = "Music Track"; var component = gameObject.AddComponent <ZEventListener>(); var track = new MusicTrack() { Name = "Unknown" }; track.NewVersion(); track.WriteObject(component); gameObject.ForEachILevelEditorListener(delegate(ILevelEditorListener listener) { listener.LevelEditorStart(true); }); MonoBehaviour[] components = gameObject.GetComponents <MonoBehaviour>(); foreach (MonoBehaviour monoBehaviour in components) { monoBehaviour.enabled = false; } LevelEditor editor = G.Sys.LevelEditor_; editor.AddGameObjectSilent(ref objectHandle, gameObject, null); return(gameObject); }
static void Postfix(ZEventListener __instance, IVisitor visitor) { if (!__instance.eventName_.StartsWith(CustomDataInfo.GetPrefix <MusicTrack>())) { return; } CachedMusicTrack.GetOr(__instance, () => MusicTrack.FromObject(__instance)); }
static bool Prefix(UnityEngine.GameObject gameObject, ref string __result) { if (gameObject == null) { return(true); } var component = gameObject.GetComponent <ZEventListener>(); if (component == null) { return(true); } if (component.eventName_.StartsWith(CustomDataInfo.GetPrefix <MusicTrack>())) { var track = Entry.CachedMusicTrack.GetOr(component, () => MusicTrack.FromObject(component)); if (track == null) { __result = "Music Track?"; } __result = $"Music Track: {track.Name}"; return(false); } return(true); }
public static string DownloadTrack(MusicTrack track, string levelPath) { var err = track.GetError(); if (err != null) { return(null); } if (track.FileLocation != null || track.Attempted) { return(track.FileLocation); } track.Attempted = true; var trackPath = $"{levelPath}.{track.FileName}{track.FileType}"; var statePath = $"{levelPath}.{track.FileName}.musicstate"; var upToDate = false; try { if (FileEx.Exists(statePath) && FileEx.Exists(trackPath)) { var stateStr = FileEx.ReadAllText(statePath); if (stateStr == track.Version) { upToDate = true; } } } catch (Exception e) { UnityEngine.Debug.Log($"Failed to read music state file {statePath} because {e}"); return(null); } if (upToDate) { track.FileLocation = trackPath; return(trackPath); } if (track.Embedded.Length > 0) { try { FileEx.WriteAllBytes(trackPath, track.Embedded); track.FileLocation = trackPath; } catch (Exception e) { UnityEngine.Debug.Log($"Failed to write track {trackPath} (embed) because {e}"); return(null); } } else if (!string.IsNullOrEmpty(track.DownloadUrl)) { var request = UnityEngine.Networking.UnityWebRequest.Get(track.DownloadUrl); byte[] file = new byte[0]; var stopwatch = System.Diagnostics.Stopwatch.StartNew(); var operation = request.Send(); while (!operation.isDone) { if (stopwatch.ElapsedMilliseconds > MaxMusicDownloadTimeMilli) { request.Abort(); UnityEngine.Debug.Log($"Failed to download {track.Name}: it took too long!"); break; } else if (request.downloadedBytes >= (ulong)MaxMusicDownloadSizeBytes) { request.Abort(); UnityEngine.Debug.Log($"Failed to download {track.Name}: it is too big!"); break; } } stopwatch.Stop(); if (operation.isDone) { if (request.isError) { UnityEngine.Debug.Log($"Failed to download {track.Name}: Error {request.error}"); } else { file = request.downloadHandler.data; } } request.Dispose(); if (file.Length > 0) { try { FileEx.WriteAllBytes(trackPath, file); track.FileLocation = trackPath; } catch (Exception e) { UnityEngine.Debug.Log($"Failed to write track {trackPath} (download) because {e}"); return(null); } } else { UnityEngine.Debug.Log($"Failed to download {track.Name}: no data!"); } } else { UnityEngine.Debug.Log($"Impossible state when saving custom music track {track.Name}"); } try { FileEx.WriteAllText(statePath, track.Version); } catch (Exception e) { UnityEngine.Debug.Log($"Failed to write music state file {statePath} because {e}"); } return(trackPath); }