public void LoadScoreFile([NotNull] string scoreFilePath, int scoreIndex, float scoreOffset) { var theaterDays = Game.ToBaseGame(); var debug = theaterDays.FindSingleElement <DebugOverlay>(); if (string.IsNullOrEmpty(scoreFilePath)) { if (debug != null) { debug.AddLine("ERROR: Score file is not specified."); } return; } if (!File.Exists(scoreFilePath)) { if (debug != null) { debug.AddLine($"ERROR: Score file <{scoreFilePath}> is missing."); } return; } var scoreFormats = theaterDays.PluginManager.GetPluginsOfType <IScoreFormat>(); if (scoreFormats.Count == 0) { if (debug != null) { debug.AddLine("ERROR: No available score reader."); } return; } var sourceOptions = new ReadSourceOptions { ScoreIndex = scoreIndex }; var compileOptions = new ScoreCompileOptions { GlobalSpeed = 1, Offset = scoreOffset }; var successful = false; RuntimeScore runtimeScore = null; SourceScore sourceScore = null; foreach (var format in scoreFormats) { if (!format.SupportsReadingFileType(scoreFilePath)) { continue; } using (var reader = format.CreateReader()) { Stream fileStream = null; if (reader.IsStreamingSupported) { fileStream = File.Open(scoreFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); } if (!successful) { if (format.CanReadAsSource) { try { sourceScore = reader.ReadSourceScore(fileStream, scoreFilePath, sourceOptions); if (!format.CanBeCompiled) { throw new InvalidOperationException("This format must support compiling source score to runtime score."); } using (var compiler = format.CreateCompiler()) { runtimeScore = compiler.Compile(sourceScore, compileOptions); } successful = true; } catch (Exception ex) { if (debug != null) { debug.AddLine($"An exception is thrown while trying to read the score using <{format.PluginDescription}>: {ex.Message}"); debug.AddLine(ex.StackTrace); } } } } if (!successful) { if (format.CanReadAsCompiled) { try { runtimeScore = reader.ReadCompiledScore(fileStream, scoreFilePath, sourceOptions, compileOptions); successful = true; } catch (Exception ex) { if (debug != null) { debug.AddLine($"An exception is thrown while trying to read the score using <{format.PluginDescription}>: {ex.Message}"); debug.AddLine(ex.StackTrace); } } } } if (successful) { break; } fileStream?.Dispose(); } } if (!successful) { if (debug != null) { debug.AddLine($"ERROR: No score reader can read score file <{scoreFilePath}>."); } } else { _sourceScore = sourceScore; RuntimeScore = runtimeScore; if (debug != null) { debug.AddLine($"Loaded score file: {scoreFilePath}"); } } var noteReactor = theaterDays.FindSingleElement <NoteReactor>(); noteReactor?.RecalculateReactions(); var tapPoints = theaterDays.FindSingleElement <TapPoints>(); tapPoints?.RecalcLayout(); }
public void WriteCompiledScore(Stream stream, string fileName, RuntimeScore score) { throw new NotSupportedException(); }
protected override void OnInitialize() { base.OnInitialize(); var settings = Program.Settings; var scoreFileName = settings.Game.ScoreFile; var debug = Game.AsTheaterDays().FindSingleElement <DebugOverlay>(); if (string.IsNullOrEmpty(scoreFileName)) { if (debug != null) { debug.AddLine("ERROR: Score file is not specified."); } return; } if (!File.Exists(scoreFileName)) { if (debug != null) { debug.AddLine($"ERROR: Score file <{scoreFileName}> is missing."); } return; } if (Program.PluginManager.ScoreFormats.Count == 0) { if (debug != null) { debug.AddLine("ERROR: No available score reader."); } return; } var sourceOptions = new ReadSourceOptions { ScoreIndex = settings.Game.ScoreIndex }; var compileOptions = new ScoreCompileOptions { GlobalSpeed = 1, Offset = settings.Game.ScoreOffset }; var successful = false; RuntimeScore runtimeScore = null; SourceScore sourceScore = null; foreach (var format in Program.PluginManager.ScoreFormats) { if (!format.SupportsReadingFileType(scoreFileName)) { continue; } using (var reader = format.CreateReader()) { using (var fileStream = File.Open(scoreFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { if (!successful) { if (format.CanReadAsSource) { try { sourceScore = reader.ReadSourceScore(fileStream, scoreFileName, sourceOptions); if (!format.CanBeCompiled) { throw new InvalidOperationException("This format must support compiling source score to runtime score."); } using (var compiler = format.CreateCompiler()) { runtimeScore = compiler.Compile(sourceScore, compileOptions); } successful = true; } catch (Exception ex) { if (debug != null) { debug.AddLine($"An exception is thrown while trying to read the score using <{format.PluginDescription}>: {ex.Message}"); debug.AddLine(ex.StackTrace); } } } } if (!successful) { if (format.CanReadAsCompiled) { try { runtimeScore = reader.ReadCompiledScore(fileStream, scoreFileName, sourceOptions, compileOptions); successful = true; } catch (Exception ex) { if (debug != null) { debug.AddLine($"An exception is thrown while trying to read the score using <{format.PluginDescription}>: {ex.Message}"); debug.AddLine(ex.StackTrace); } } } } if (successful) { break; } } } } if (!successful) { if (debug != null) { debug.AddLine($"ERROR: No score reader can read score file <{scoreFileName}>."); } } else { _score = sourceScore; RuntimeScore = runtimeScore; if (debug != null) { debug.AddLine($"Loaded score file: {scoreFileName}"); } } }