Skip to content

Commit

Permalink
small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hermanest committed Jul 26, 2023
1 parent 4994bd4 commit af52eaa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
6 changes: 5 additions & 1 deletion Source/7_Utils/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ public static IEnumerable<string> GetAllReplayPaths() {
.Concat(Directory.EnumerateFiles(ReplayerCache.CacheDirectory, "*.bsor"));
}

public static string GetAbsoluteReplayPath(string fileName) {
return Path.Combine(replaysFolderPath, fileName);
}

public static bool TryWriteReplay(string fileName, Replay replay) {
try {
var path = Path.Combine(replaysFolderPath, fileName);
var path = GetAbsoluteReplayPath(fileName);
using BinaryWriter file = new(File.Open(path, FileMode.OpenOrCreate), Encoding.UTF8);
ReplayEncoder.Encode(replay, file);
file.Close();
Expand Down
28 changes: 13 additions & 15 deletions Source/7_Utils/ReplayManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -24,7 +25,7 @@ public class ReplayManager : Singleton<ReplayManager>, IReplayManager, IReplayFi

public IReplayHeader? CachedReplay { get; private set; }

private IList<IReplayHeader>? _lastReplayHeaders = new List<IReplayHeader>();
private IList<IReplayHeader>? _lastReplayHeaders;

public async Task<IList<IReplayHeader>?> LoadReplayHeadersAsync(
CancellationToken token,
Expand Down Expand Up @@ -74,26 +75,23 @@ await Task.Run(() => {
Plugin.Log.Warn("OverrideOldReplays is enabled, old replays will be deleted");
var info = replay.info;
if (_lastReplayHeaders is null) await LoadReplayHeadersAsync(token);
foreach (var replayHeader in _lastReplayHeaders!) {
if (replayHeader.ReplayInfo is not { } replayInfo ||
replayInfo.PlayerID != info.playerID ||
replayInfo.SongName != info.songName ||
replayInfo.SongDifficulty != info.difficulty
|| replayInfo.SongMode != info.mode
|| replayInfo.SongHash != info.hash) continue;
foreach (var replayHeader in _lastReplayHeaders!.Where(x =>
x.ReplayInfo is { } i && i.PlayerID == info.playerID
&& i.SongName == info.songName && i.SongDifficulty == info.difficulty
&& i.SongMode == info.mode && i.SongHash == info.hash).ToArray()
) {
Plugin.Log.Info("Deleting old replay: " + Path.GetFileName(replayHeader.FilePath));
((IReplayFileManager)this).DeleteReplay(replayHeader);
}
}

var writeResult = false;
await Task.Run(() => writeResult = TryWriteReplay(path, replay), token);
if (!writeResult) return null;

if (!TryWriteReplay(path, replay)) return null;
replay.info.levelEndType = playEndData.EndType;
var header = new GenericReplayHeader(this, path, replay);
_lastReplayHeaders!.Add(header);
ReplayAddedEvent?.Invoke(header);
var absolutePath = GetAbsoluteReplayPath(path);
var header = new GenericReplayHeader(this, absolutePath, replay);
_lastReplayHeaders?.Add(header);
CachedReplay = header;
ReplayAddedEvent?.Invoke(header);
return header;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/7_Utils/ScoreUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void ProcessReplay(Replay replay, PlayEndData data) {
SaveReplay(replay, data);

static void SaveReplay(Replay replay, PlayEndData data) {
_ = ReplayManager.Instance.SaveReplayAsync(replay, data, default).Result;
_ = ReplayManager.Instance.SaveReplayAsync(replay, data, default);
}
}

Expand Down

0 comments on commit af52eaa

Please sign in to comment.