Skip to content
This repository has been archived by the owner on Sep 30, 2019. It is now read-only.

Commit

Permalink
Added better song parsing error handling. Fixed bug where you'd somet…
Browse files Browse the repository at this point in the history
…imes get stuck while playing a song.
  • Loading branch information
xyonico committed May 8, 2018
1 parent b0ee6d4 commit eab4dd3
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 47 deletions.
11 changes: 0 additions & 11 deletions README.md
Expand Up @@ -87,14 +87,3 @@ All possible environmentNames:
}
]
```
___

## Troubleshooting (for the old injector version)
BSSI can be finicky at times and stubborn with injecting.

Steps to help you avoid crashing:
* Wait at least 5 seconds after starting Beat Saber before injecting.
* Put your hand/face over the Oculus Face Sensor so the black mirrored screen on your desktop shows the game before launching BSSI.
* Make sure there are no 'ghost processes' running in task manager labelled 'BSSI' & 'MonoJunkie'; if there is, end the task.
* Open Beat Saber before opening BSSI, running as administrator for both is preferred.
* Do a clean install of BSSI if crashing persists.
30 changes: 30 additions & 0 deletions SongLoaderPlugin/CustomLevelStaticData.cs
@@ -0,0 +1,30 @@
using System.Linq;

namespace SongLoaderPlugin
{
/// There's a bug in Beat Saber, where
/// if you pause a song mid-playthrough,
/// and go back to menu, start playing another song,
/// the new level difficulty isn't updated throughout all the game managers.
/// So when you play a custom song that only has Expert difficulty,
/// pause and go play another custom song that only has Easy difficulty,
/// you're going to have problems.
/// So here instead of returning null when it can't find any difficulty level,
/// We're going to return the first difficulty level.

public class CustomLevelStaticData : LevelStaticData
{
public override DifficultyLevel GetDifficultyLevel(Difficulty difficulty)
{
foreach (var difficultyLevel in _difficultyLevels)
{
if (difficultyLevel.difficulty == difficulty)
{
return difficultyLevel;
}
}

return difficultyLevels.First();
}
}
}
3 changes: 1 addition & 2 deletions SongLoaderPlugin/CustomSongInfo.cs
Expand Up @@ -45,8 +45,7 @@ public string GetIdentifier()
}

var hash = Utils.CreateMD5(combinedJson);
levelId = hash + "\0" + string.Join("\0", new[] {songName, songSubName, authorName, beatsPerMinute.ToString()});
Console.WriteLine("ID=" + levelId);
levelId = hash + "" + string.Join("", new[] {songName, songSubName, authorName, beatsPerMinute.ToString()}) + "";
return levelId;
}
}
Expand Down
2 changes: 1 addition & 1 deletion SongLoaderPlugin/Plugin.cs
Expand Up @@ -23,7 +23,7 @@ public void OnApplicationStart()

public void OnApplicationQuit()
{
PlayerPrefs.SetInt("lbPatched", 0);
PlayerPrefs.DeleteKey("lbPatched");
}

public void OnLevelWasLoaded(int level)
Expand Down
81 changes: 48 additions & 33 deletions SongLoaderPlugin/SongLoader.cs
Expand Up @@ -19,7 +19,6 @@ public class SongLoader : MonoBehaviour

private MainGameSceneSetupData _sceneSetupData;
private LeaderboardScoreUploader _leaderboardScoreUploader;
private GameplayMode _prevMode;

public static void OnLoad()
{
Expand Down Expand Up @@ -50,16 +49,11 @@ private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene scene)
_sceneSetupData = ReflectionUtil.GetPrivateField<MainGameSceneSetupData>(sceneSetup, "_mainGameSceneSetupData");
var currentCustomSong = CustomSongs.FirstOrDefault(x => x.levelId == _sceneSetupData.levelId);
if (currentCustomSong == null) return;
_prevMode = _sceneSetupData.gameplayMode;
//if (PlayerPrefs.GetInt("lbPatched", 0) == 1) return;
//ReflectionUtil.SetPrivateField(_sceneSetupData, "gameplayMode", GameplayMode.PartyStandard);
}
else
{
//We're probably in menu
if (_sceneSetupData == null) return;
//Log("Resetting to previous gameplay mode " + _prevMode);
//ReflectionUtil.SetPrivateProperty(_sceneSetupData, "gameplayMode", _prevMode);

}
}

Expand All @@ -75,8 +69,7 @@ public void RefreshSongs()
var songs = RetrieveSongs();

var gameScenesManager = Resources.FindObjectsOfTypeAll<GameScenesManager>().FirstOrDefault();

var levelsData = new List<LevelStaticData>();

var gameDataModel = PersistentSingleton<GameDataModel>.instance;
var oldData = gameDataModel.gameStaticData.worldsData[0].levelsData.ToList();

Expand All @@ -92,10 +85,10 @@ public void RefreshSongs()
var id = song.GetIdentifier();
CustomSongs.Add(song);

LevelStaticData newLevel = null;
CustomLevelStaticData newLevel = null;
try
{
newLevel = ScriptableObject.CreateInstance<LevelStaticData>();
newLevel = ScriptableObject.CreateInstance<CustomLevelStaticData>();
}
catch (Exception e)
{
Expand All @@ -122,45 +115,58 @@ public void RefreshSongs()
{
var newDiffLevel = new LevelStaticData.DifficultyLevel();

var difficulty = diffLevel.difficulty.ToEnum(LevelStaticData.Difficulty.Normal);
ReflectionUtil.SetPrivateField(newDiffLevel, "_difficulty", difficulty);
ReflectionUtil.SetPrivateField(newDiffLevel, "_difficultyRank", diffLevel.difficultyRank);

if (!File.Exists(song.path + "/" + diffLevel.jsonPath))
{
Log("Couldn't find difficulty json " + song.path + "/" + diffLevel.jsonPath);
continue;
}

var newSongLevelData = ScriptableObject.CreateInstance<SongLevelData>();
var json = File.ReadAllText(song.path + "/" + diffLevel.jsonPath);
try
{
newSongLevelData.LoadFromJson(json);
var difficulty = diffLevel.difficulty.ToEnum(LevelStaticData.Difficulty.Normal);
ReflectionUtil.SetPrivateField(newDiffLevel, "_difficulty", difficulty);
ReflectionUtil.SetPrivateField(newDiffLevel, "_difficultyRank", diffLevel.difficultyRank);

if (!File.Exists(song.path + "/" + diffLevel.jsonPath))
{
Log("Couldn't find difficulty json " + song.path + "/" + diffLevel.jsonPath);
continue;
}

var newSongLevelData = ScriptableObject.CreateInstance<SongLevelData>();
var json = File.ReadAllText(song.path + "/" + diffLevel.jsonPath);
try
{
newSongLevelData.LoadFromJson(json);
}
catch (Exception e)
{
Log("Error while parsing " + song.path + "/" + diffLevel.jsonPath);
Log(e.ToString());
continue;
}

ReflectionUtil.SetPrivateField(newDiffLevel, "_songLevelData", newSongLevelData);
StartCoroutine(LoadAudio("file://" + song.path + "/" + diffLevel.audioPath, newDiffLevel,
"_audioClip"));
difficultyLevels.Add(newDiffLevel);
}
catch (Exception e)
{
Log("Error while parsing " + song.path + "/" + diffLevel.jsonPath);
Log(e.ToString());
Log("Error parsing difficulty level in song: " + song.path);
Log(e.Message);
continue;
}

ReflectionUtil.SetPrivateField(newDiffLevel, "_songLevelData", newSongLevelData);
StartCoroutine(LoadAudio("file://" + song.path + "/" + diffLevel.audioPath, newDiffLevel, "_audioClip"));
difficultyLevels.Add(newDiffLevel);
}

if (difficultyLevels.Count == 0) continue;

ReflectionUtil.SetPrivateField(newLevel, "_difficultyLevels", difficultyLevels.ToArray());
newLevel.OnEnable();
levelsData.Add(newLevel);
oldData.Add(newLevel);
}

oldData.AddRange(levelsData);
ReflectionUtil.SetPrivateField(gameDataModel.gameStaticData.worldsData[0], "_levelsData", oldData.ToArray());
SongsLoaded.Invoke();
}

private void RemoveCustomScores()
{
if (PlayerPrefs.HasKey("lbPatched")) return;
_leaderboardScoreUploader = FindObjectOfType<LeaderboardScoreUploader>();
if (_leaderboardScoreUploader == null) return;
var scores =
Expand Down Expand Up @@ -221,7 +227,16 @@ private List<CustomSongInfo> RetrieveSongs()
}

var infoText = File.ReadAllText(songPath + "/info.json");
var songInfo = JsonUtility.FromJson<CustomSongInfo>(infoText);
CustomSongInfo songInfo;
try
{
songInfo = JsonUtility.FromJson<CustomSongInfo>(infoText);
}
catch (Exception e)
{
Log("Error parsing song: " + songPath);
continue;
}
songInfo.path = songPath;

//Here comes SimpleJSON to the rescue when JSONUtility can't handle an array.
Expand Down
1 change: 1 addition & 0 deletions SongLoaderPlugin/SongLoaderPlugin.csproj
Expand Up @@ -71,6 +71,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CustomLevelStaticData.cs" />
<Compile Include="CustomSongInfo.cs" />
<Compile Include="ReflectionUtil.cs" />
<Compile Include="SimpleJSON.cs" />
Expand Down

0 comments on commit eab4dd3

Please sign in to comment.