Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates and fixes for Beat Saber 1.39.0 #81

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion BeatSaberCinema/BeatSaberCinema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
<Reference Include="AdditionalContentModel.Interfaces" Publicize="true">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\AdditionalContentModel.Interfaces.dll</HintPath>
</Reference>
<Reference Include="BeatmapCore" Publicize="true">
<Reference Include="BeatSaber.BeatmapEditor" Publicize="true">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\BeatSaber.BeatmapEditor.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="BeatmapCore" Publicize="true">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\BeatmapCore.dll</HintPath>
<Private>False</Private>
</Reference>
Expand Down
2 changes: 1 addition & 1 deletion BeatSaberCinema/Environment/EnvironmentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private static void Reset()
PlaybackController.Instance.VideoPlayer.screenController.Screens.RemoveRange(1, PlaybackController.Instance.VideoPlayer.screenController.Screens.Count - 1);


if (Util.IsModInstalled("_Heck"))
if (InstalledMods.Heck)
{
var types = new[] {"Chroma.GameObjectTrackController", "Chroma.Lighting.EnvironmentEnhancement.GameObjectTrackController"};

Expand Down
9 changes: 1 addition & 8 deletions BeatSaberCinema/Harmony/Patches/WaitForChromaPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,7 @@ private static IEnumerator WaitThenStart()

//Turns out CustomPlatforms runs even later and undoes some of the scene modifications Cinema does. Waiting for a specific duration is more of a temporary fix.
//TODO Find a better way to implement this. The problematic coroutine in CustomPlatforms is CustomFloorPlugin.EnvironmentHider+<InternalHideObjectsForPlatform>
if (Util.IsModInstalled("CustomPlatforms"))
{
yield return new WaitForSeconds(0.75f);
}
else
{
yield return new WaitForSeconds(0.05f);
}
yield return new WaitForSeconds(InstalledMods.CustomPlatforms ? 0.75f : 0.05f);

EnvironmentController.ModifyGameScene(PlaybackController.Instance.VideoConfig);
}
Expand Down
29 changes: 17 additions & 12 deletions BeatSaberCinema/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ public void OnApplicationStart()
private static void OnMenuSceneLoadedFresh(ScenesTransitionSetupDataSO scenesTransition)
{
PlaybackController.Create();
VideoMenu.AddTab();
SettingsUI.CreateMenu();
SongPreviewPlayerController.Init();

if (Util.IsModInstalled("BetterSongList", "0.3.2") && !_filterAdded)
if (VideoMenu.Instance != null)
{
AddBetterSongListFilter();
VideoMenu.RemoveTab();
}
VideoMenu.AddTab();

SettingsUI.CreateMenu();
SongPreviewPlayerController.Init();
AddBetterSongListFilter();
}

[OnEnable]
Expand All @@ -76,7 +78,7 @@ public void OnEnable()
}

//No need to index maps if the filter isn't going to be applied anyway
if (Util.IsModInstalled("BetterSongList", "0.3.2"))
if (InstalledMods.BetterSongList)
{
Loader.SongsLoadedEvent += VideoLoader.IndexMaps;
}
Expand All @@ -96,7 +98,7 @@ public void OnDisable()
//TODO Destroying and re-creating the PlaybackController messes up the VideoMenu without any exceptions in the log. Investigate.
//PlaybackController.Destroy();

VideoMenu.Instance?.RemoveTab();
VideoMenu.RemoveTab();
EnvironmentController.Disable();
VideoLoader.StopFileSystemWatcher();
Collections.DeregisterCapability(CAPABILITY);
Expand All @@ -114,13 +116,16 @@ private void RemoveHarmonyPatches()

private static void AddBetterSongListFilter()
{
var filter = new HasVideoFilter();
var success = BetterSongList.FilterMethods.Register(filter);
if (success)
if (!InstalledMods.BetterSongList || _filterAdded)
{
_filterAdded = true;
Log.Debug($"Registered {nameof(HasVideoFilter)}");
return;
}

_filterAdded = BetterSongList.FilterMethods.Register(new HasVideoFilter());

if (_filterAdded)
{
Log.Debug($"Registered {nameof(HasVideoFilter)}");
}
else
{
Expand Down
25 changes: 25 additions & 0 deletions BeatSaberCinema/Util/InstalledMods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Linq;
using IPA.Loader;
using HiveVersion = Hive.Versioning.Version;

namespace BeatSaberCinema
{
/// <summary>
/// Declares a set of soft dependencies by whether they are installed.
/// true: is installed / false: is not installed
/// </summary>
internal static class InstalledMods
{
public static bool BetterSongList { get; } = IsModInstalled("BetterSongList", "0.3.2");
public static bool BeatSaberPlaylistsLib { get; } = IsModInstalled("BeatSaberPlaylistsLib", "1.7.0");
public static bool CustomPlatforms { get; } = IsModInstalled("CustomPlatforms");
public static bool MusicVideoPlayer { get; } = IsModInstalled("Music Video Player");
public static bool Heck { get; } = IsModInstalled("_Heck");

private static bool IsModInstalled(string modId, string? minimumVersion = null)
{
return minimumVersion == null ? PluginManager.EnabledPlugins.Any(x => x.Id == modId)
: PluginManager.EnabledPlugins.Any(x => x.Id == modId && x.HVersion >= new HiveVersion(minimumVersion));
}
}
}
69 changes: 69 additions & 0 deletions BeatSaberCinema/Util/PlaylistSongUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using BeatSaberPlaylistsLib.Types;
using Newtonsoft.Json;

namespace BeatSaberCinema
{
public static class PlaylistSongUtils
{
/// <summary>
/// Do not call this method without checking if BeatSaberPlaylistsLib is installed with <see cref="InstalledMods"/>
/// </summary>
public static bool IsPlaylistLevel(this BeatmapLevel? beatmapLevel)
{
return beatmapLevel is PlaylistLevel;
}

/// <summary>
/// Do not call this method without checking if BeatSaberPlaylistsLib is installed with <see cref="InstalledMods"/>
/// </summary>
public static BeatmapLevel? GetLevelFromPlaylistIfAvailable(this BeatmapLevel? beatmapLevel)
{
return beatmapLevel is PlaylistLevel playlistLevel ? playlistLevel.playlistSong.BeatmapLevel ?? beatmapLevel : beatmapLevel;
}

/// <summary>
/// Do not call this method without checking if BeatSaberPlaylistsLib is installed with <see cref="InstalledMods"/>
/// </summary>
public static bool TryGetPlaylistLevelConfig(this BeatmapLevel? beatmapLevel, string levelPath, out VideoConfig? videoConfig)
{
return (videoConfig = beatmapLevel is PlaylistLevel playlistLevel ? playlistLevel.TryLoadConfig(levelPath) : null) != null;
}

/// <summary>
/// Do not call this method without checking if BeatSaberPlaylistsLib is installed with <see cref="InstalledMods"/>
/// </summary>
public static VideoConfig? TryLoadConfig(this PlaylistLevel playlistLevel, string levelPath)
{
var playlistSong = playlistLevel.playlistSong;
if (playlistSong.TryGetCustomData("cinema", out var cinemaData))
{
VideoConfig? videoConfig;
try
{
var json = JsonConvert.SerializeObject(cinemaData);
videoConfig = JsonConvert.DeserializeObject<VideoConfig>(json);
}
catch (Exception e)
{
Log.Error($"Error parsing video json {playlistSong.Name}:");
Log.Error(e);
return null;
}

if (videoConfig == null)
{
Log.Warn($"Deserializing video config for {playlistSong.Name} failed");
return null;
}
videoConfig.LevelDir = levelPath;
videoConfig.UpdateDownloadState();

return videoConfig;
}

Log.Error($"No config exists for {playlistSong.Name}:");
return null;
}
}
}
5 changes: 0 additions & 5 deletions BeatSaberCinema/Util/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ public static string ShortenFilename(string path, string s)
return s;
}

public static bool IsModInstalled(string modName, string? minimumVersion = null)
{
return IPA.Loader.PluginManager.EnabledPlugins.Any(x => x.Id == modName && (minimumVersion == null || x.HVersion >= new Hive.Versioning.Version(minimumVersion)));
}

public static Texture? LoadPNGFromResources(string resourcePath)
{
var fileData = BeatSaberMarkupLanguage.Utilities.GetResource(Assembly.GetExecutingAssembly(), resourcePath);
Expand Down
85 changes: 9 additions & 76 deletions BeatSaberCinema/Video/VideoLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Threading;
using System.Threading.Tasks;
using BeatmapEditor3D.DataModels;
using BeatSaberPlaylistsLib.Types;
using IPA.Utilities.Async;
using Newtonsoft.Json;
using SongCore;
Expand All @@ -25,7 +24,6 @@ public static class VideoLoader
internal const string WIP_MAPS_FOLDER = "CustomWIPLevels";
private const string CONFIG_FILENAME = "cinema-video.json";
private const string CONFIG_FILENAME_MVP = "video.json";
private const string MOD_ID_MVP = "Music Video Player";

private static FileSystemWatcher? _fileSystemWatcher;
public static event Action<VideoConfig?>? ConfigChanged;
Expand Down Expand Up @@ -196,6 +194,7 @@ public static void RemoveConfigFromCache(BeatmapLevel level)

config.LevelDir = GetLevelPath(level);
config.bundledConfig = true;
Log.Debug("Loaded from bundled configs");
return config;
}

Expand Down Expand Up @@ -333,15 +332,14 @@ public static async Task<EntitlementStatus> GetEntitlementForLevel(BeatmapLevel
return videoConfig;
}

public static VideoConfig? GetConfigForLevel(BeatmapLevel? level, bool isPlaylistSong = false)
public static VideoConfig? GetConfigForLevel(BeatmapLevel? level)
{
var playlistSong = level;
if (isPlaylistSong)
if (InstalledMods.BeatSaberPlaylistsLib)
{
level = GetBeatmapLevelFromPlaylistSong(level);
level = level.GetLevelFromPlaylistIfAvailable();
}

if (playlistSong == null || level == null)
if (level == null)
{
return null;
}
Expand All @@ -361,7 +359,7 @@ public static async Task<EntitlementStatus> GetEntitlementForLevel(BeatmapLevel
if (Directory.Exists(levelPath))
{
videoConfig = LoadConfig(GetConfigPath(levelPath));
if (videoConfig == null && !Util.IsModInstalled(MOD_ID_MVP))
if (videoConfig == null && !InstalledMods.MusicVideoPlayer)
{
//Back compatiblity with MVP configs, but only if MVP is not installed
videoConfig = LoadConfig(Path.Combine(levelPath, CONFIG_FILENAME_MVP));
Expand All @@ -372,39 +370,12 @@ public static async Task<EntitlementStatus> GetEntitlementForLevel(BeatmapLevel
Log.Debug($"Path does not exist: {levelPath}");
}

if (videoConfig == null && isPlaylistSong && PlaylistSongHasConfig(playlistSong))
if (InstalledMods.BeatSaberPlaylistsLib && videoConfig == null && level.TryGetPlaylistLevelConfig(levelPath, out var playlistConfig))
{
videoConfig = LoadConfigFromPlaylistSong(playlistSong, levelPath);
videoConfig = playlistConfig;
}

if (videoConfig == null)
{
videoConfig = GetConfigFromBundledConfigs(level);
if (videoConfig == null)
{
return videoConfig;
}
Log.Debug("Loaded from bundled configs");
}

return videoConfig;
}

private static bool PlaylistSongHasConfig(BeatmapLevel level)
{
var playlistLevel = level as PlaylistLevel;
return playlistLevel?.playlistSong.TryGetCustomData("cinema", out _) ?? false;
}

public static BeatmapLevel? GetBeatmapLevelFromPlaylistSong(BeatmapLevel? level)
{
BeatmapLevel? unwrappedLevel = null!;
if (level is PlaylistLevel playlistLevel)
{
unwrappedLevel = playlistLevel.playlistSong.BeatmapLevel;
}

return unwrappedLevel ?? level;
return videoConfig ?? GetConfigFromBundledConfigs(level);
}

[Obsolete("Obsolete")]
Expand Down Expand Up @@ -573,44 +544,6 @@ public static bool DeleteConfig(VideoConfig videoConfig, BeatmapLevel level)
return videoConfig;
}

private static VideoConfig? LoadConfigFromPlaylistSong(BeatmapLevel beatmapLevel, string levelPath)
{
if (!(beatmapLevel is PlaylistLevel playlistLevel))
{
return null;
}

var playlistSong = playlistLevel.playlistSong;
if (playlistSong.TryGetCustomData("cinema", out var cinemaData))
{
VideoConfig? videoConfig;
try
{
var json = JsonConvert.SerializeObject(cinemaData);
videoConfig = JsonConvert.DeserializeObject<VideoConfig>(json);
}
catch (Exception e)
{
Log.Error($"Error parsing video json {playlistSong.Name}:");
Log.Error(e);
return null;
}

if (videoConfig == null)
{
Log.Warn($"Deserializing video config for {playlistSong.Name} failed");
return null;
}
videoConfig.LevelDir = levelPath;
videoConfig.UpdateDownloadState();

return videoConfig;
}

Log.Error($"No config exists for {playlistSong.Name}:");
return null;
}

private static IEnumerable<BundledConfig> LoadBundledConfigs()
{
var buffer = BeatSaberMarkupLanguage.Utilities.GetResource(Assembly.GetExecutingAssembly(), "BeatSaberCinema.Resources.configs.json");
Expand Down
Loading