123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- using BeatLyrics.Common;
- using BeatLyrics.Common.Models;
- using IllusionPlugin;
- using Newtonsoft.Json;
- using SongCore.Utilities;
- using System;
- using System.Collections;
- using System.IO;
- using System.Linq;
- using TMPro;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- namespace BeatLyrics
- {
- /// <summary>
- /// Entry point of the plugin.
- /// </summary>
- public sealed class Plugin : IPlugin
- {
- public string Name => "Beat Lyrics";
- public string Version => "0.0.0.1";
- private LyricsComponent _lastUsedLyricsComponent;
- public void OnApplicationStart()
- {
- SceneManager.activeSceneChanged += OnActiveSceneChanged;
- }
- public void OnApplicationQuit()
- {
- SceneManager.activeSceneChanged -= OnActiveSceneChanged;
- }
- #region Unused
- public void OnLevelWasInitialized(int level)
- {
- }
- public void OnLevelWasLoaded(int level)
- {
- }
- public void OnUpdate()
- {
- }
- public void OnFixedUpdate()
- {
- }
- #endregion Unused
- public void OnActiveSceneChanged(Scene _, Scene newScene)
- {
- if (newScene.name == "GameCore")
- {
- _lastUsedLyricsComponent = newScene.GetRootGameObjects()[0].AddComponent<LyricsComponent>();
- }
- else
- {
- if (_lastUsedLyricsComponent != null)
- {
- //TODO: cleanup
- }
- }
- }
- }
- public class LyricsComponent : MonoBehaviour
- {
- private static readonly Quaternion Rot = Quaternion.Euler(new Vector3(0, 0, 0));
- private static readonly Vector3 Pos = new Vector3(0, 1.5f, 15f);
- private static readonly float FontSize = 25;
- private static readonly Color32 FontColor = new Color32(255, 255, 255, 64);
- private static readonly float OutlineWidth = 0.4f;
- private static readonly Color32 OutlineColor = new Color32(255, 0, 0, 255);
- //TODO: OutLine
- //TODO: Select Font
- private readonly AudioTimeSyncController _audioTime;
- private readonly GameObject _clockCanvas = null;
- private readonly TextMeshProUGUI _text = null;
- private GameplayCoreSceneSetupData _data;
- private LyricFile _lyricFile;
- private string _lyricFileName;
- private string _debugInfo;
- public LyricsComponent()
- {
- _audioTime = Resources.FindObjectsOfTypeAll<AudioTimeSyncController>().FirstOrDefault();
- _clockCanvas = new GameObject();
- _clockCanvas.AddComponent<Canvas>();
- _clockCanvas.name = "BeatLyrics Canvas";
- _clockCanvas.transform.position = Pos;
- _clockCanvas.transform.rotation = Rot;
- _clockCanvas.transform.localScale = new Vector3(0.02f, 0.02f, 1.0f);
- var textObj = new GameObject();
- textObj.transform.SetParent(_clockCanvas.transform);
- textObj.transform.localPosition = Vector3.zero;
- textObj.transform.localRotation = Quaternion.identity;
- textObj.transform.localScale = Vector3.one;
- _text = textObj.AddComponent<TextMeshProUGUI>();
- _text.alignment = TextAlignmentOptions.Center;
- _text.fontSize = FontSize;
- _text.enableWordWrapping = false;
- _text.color = FontColor;
- _text.name = "Lyrics Text";
- _text.text = "BeatLyrics";
- //text.font=
- }
- public IEnumerator Start()
- {
- var sceneSetup = FindObjectOfType<GameplayCoreSceneSetup>();
- _data = (GameplayCoreSceneSetupData)sceneSetup?.GetField("_sceneSetupData");
- //HACK: NJS Cheat
- if (_data != null) //TODO: Create New Plugin - OR - Load from Config
- {
- try
- {
- _data.difficultyBeatmap.SetField("_noteJumpMovementSpeed", 17);
- }
- catch (Exception e)
- {
- System.Diagnostics.Debug.Print(e.ToString());
- }
- }
- try
- {
- LoadLrc();
- }
- catch (Exception e)
- {
- _debugInfo = "Error Load Lrc:" + e.Message;
- }
- yield break;
- }
- private void LoadLrc()
- {
- if (_data?.difficultyBeatmap.level.levelID != null)
- {
- var beatLyricsDir = DirProvider.BeatLyricsDir;
- if (beatLyricsDir != null)
- {
- var path = GetSetFilePath(_data.difficultyBeatmap.level.levelID);
- if (File.Exists(path))
- {
- var jsonFileName = File.ReadAllText(path);
- var jsonFilePath = Path.Combine(beatLyricsDir, _data.difficultyBeatmap.level.levelID, jsonFileName);
- if (File.Exists(jsonFilePath))
- {
- var json = File.ReadAllText(jsonFilePath);
- _lyricFile = JsonConvert.DeserializeObject<LyricFile>(json);
- _lyricFileName = Path.GetFileNameWithoutExtension(jsonFileName);
- }
- else
- {
- _debugInfo += "json file not found";
- }
- }
- else
- {
- _debugInfo += "set.txt file not found";
- }
- }
- else
- {
- _debugInfo += "failure to get EnvVar";
- }
- }
- }
- public void Update()
- {
- var milliseconds = _audioTime?.songTime * 1000 ?? 0;
- if (_lyricFile != null)
- {
- if (milliseconds < 1)
- {
- _text.text = _lyricFileName;
- }
- else
- {
- //Show LRC
- _text.text = _lyricFile.Main?.FirstOrDefault(p => p.TimeMs <= milliseconds && p.TimeMs + p.DurationMs >= milliseconds)?.Text
- + Environment.NewLine + _lyricFile.Subtitle?.FirstOrDefault(p => p.TimeMs <= milliseconds && p.TimeMs + p.DurationMs >= milliseconds)?.Text;
- }
- }
- else if (milliseconds < 1)
- {
- var level = _data?.difficultyBeatmap.level;
- _text.text = "此处应有歌词,但是未能加载" +
- $"{Environment.NewLine}{level?.levelID}" +
- $"{Environment.NewLine}{_debugInfo}";
- }
- else
- {
- _text.text = "";
- }
- }
- public static string GetSetFilePath(string levelId)
- {
- return Path.Combine(DirProvider.BeatLyricsDir, levelId + DirProvider.SuffixSet);
- }
- }
- }
|