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

Add method to get the background color #152

Merged
merged 7 commits into from
May 17, 2023
Merged
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
2 changes: 1 addition & 1 deletion Circle.Game.Tests/CircleTestBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnl
private void load()
{
if (LocalConfig.Get<bool>(CircleSetting.LoadBeatmapsOnStartup))
BeatmapManager.ReloadBeatmaps();
BeatmapManager.LoadBeatmaps();

AddRange(new Drawable[]
{
Expand Down
100 changes: 21 additions & 79 deletions Circle.Game/Beatmaps/BeatmapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Circle.Game.IO.Archives;
using osu.Framework.Logging;
using SharpCompress.Archives;
Expand Down Expand Up @@ -45,11 +46,27 @@ public BeatmapManager(BeatmapStorage beatmaps)
/// <summary>
/// 폴더에 존재하는 비트맵을 로드합니다.
/// </summary>
public void ReloadBeatmaps()
public void LoadBeatmaps()
{
Logger.Log("Loading beatmaps...");
loadedBeatmaps = beatmapStorage.GetBeatmapInfos().ToList();
OnLoadedBeatmaps?.Invoke(loadedBeatmaps);

if (!loadedBeatmaps.Exists(b => b.Equals(CurrentBeatmap)))
ClearCurrentBeatmap();

Logger.Log($"Loaded {loadedBeatmaps.Count} beatmaps!");
}

/// <summary>
/// 폴더에 존재하는 비트맵을 로드합니다.
/// </summary>
public async Task LoadBeatmapsAsync()
{
Logger.Log("Loading beatmaps...");
loadedBeatmaps = await beatmapStorage.GetBeatmapInfosAsync();
OnLoadedBeatmaps?.Invoke(loadedBeatmaps);

if (!loadedBeatmaps.Exists(b => b.Equals(CurrentBeatmap)))
ClearCurrentBeatmap();

Expand All @@ -67,7 +84,7 @@ public void ClearCurrentBeatmap()
CurrentBeatmap = null;
}

public void Import(string path, bool migration = false)
public void Import(string path)
{
string fileName = Path.GetFileNameWithoutExtension(path).Trim(' ');
string beatmap = beatmapStorage.Storage.GetStorageForDirectory(fileName).GetFullPath(string.Empty);
Expand All @@ -77,29 +94,20 @@ public void Import(string path, bool migration = false)
try
{
File.Copy(path, Path.Combine(beatmap, Path.GetFileName(path)), true);
if (!migration)
OnImported?.Invoke(Path.GetFileName(path));
OnImported?.Invoke(Path.GetFileName(path));
}
catch (Exception e)
{
Logger.Log($"Error during import {Path.GetFileName(path)}: {e.Message}");
}
finally
{
if (migration)
File.Delete(path);
}

return;
}

using (var reader = new ZipArchiveReader(File.Open(path, FileMode.Open, FileAccess.Read), Path.GetFileName(path)))
{
reader.Archive.WriteToDirectory(beatmap);
if (migration)
File.Delete(path);
else
OnImported?.Invoke(Path.GetFileName(path));
OnImported?.Invoke(Path.GetFileName(path));
}
}

Expand All @@ -121,23 +129,6 @@ public void Import(Stream stream, string name)
}
}

public void Migrate()
{
foreach (var bi in beatmapStorage.GetBeatmapInfos())
{
if (bi.Directory != @"beatmaps")
continue;

Logger.Log($"Migrating {bi}...");
migrateTracks(bi);
migrateBackgrounds(bi);
Import(bi.BeatmapPath, true);
Logger.Log($"Migrated {bi}!");
}

deleteOldStorages();
}

/// <summary>
/// 비트맵을 파일로 저장합니다.
/// </summary>
Expand All @@ -160,55 +151,6 @@ public bool Delete(BeatmapInfo beatmap, bool deleteResources)
return LoadedBeatmaps.Remove(beatmap);
}

private void migrateTracks(BeatmapInfo info)
{
string songFileName = info.Beatmap.Settings.SongFileName;
string tracks = Path.Combine(beatmapStorage.Storage.GetFullPath("tracks"), songFileName);

if (string.IsNullOrEmpty(songFileName) || !beatmapStorage.Storage.Exists(tracks))
return;

string fileName = Path.GetFileNameWithoutExtension(info.Name)?.Trim(' ');
string beatmap = beatmapStorage.Storage.GetStorageForDirectory(fileName).GetFullPath(string.Empty);

try
{
File.Copy(tracks, Path.Combine(beatmap, songFileName));
}
catch (Exception e)
{
Logger.Log($"Error during migration track {songFileName}: {e.Message}");
}
}

private void migrateBackgrounds(BeatmapInfo info)
{
string bgImage = info.Beatmap.Settings.BgImage;
string backgrounds = Path.Combine(beatmapStorage.Storage.GetFullPath("backgrounds"), bgImage);

if (string.IsNullOrEmpty(bgImage) || !beatmapStorage.Storage.Exists(backgrounds))
return;

string fileName = Path.GetFileNameWithoutExtension(info.Name)?.Trim(' ');
string beatmap = beatmapStorage.Storage.GetStorageForDirectory(fileName).GetFullPath(string.Empty);

try
{
File.Copy(backgrounds, Path.Combine(beatmap, bgImage));
}
catch (Exception e)
{
Logger.Log($"Error during migration background {bgImage}: {e.Message}");
}
}

private void deleteOldStorages()
{
beatmapStorage.Storage.DeleteDirectory(@"backgrounds");
beatmapStorage.Storage.DeleteDirectory(@"beatmaps");
beatmapStorage.Storage.DeleteDirectory(@"tracks");
}

public event Action<(BeatmapInfo oldBeatmap, BeatmapInfo newBeatmap)> OnBeatmapChanged;

public event Action<IList<BeatmapInfo>> OnLoadedBeatmaps;
Expand Down
Loading