Exemplo n.º 1
0
        public static async Task <string> GetThumbByBeatmapDbId(BeatmapDataModel dataModel)
        {
            if (_appDbOperator.GetMapThumb(dataModel.BeatmapDbId, out var path) && path != null)
            {
                return(path);
            }

            var osuFilePath = dataModel.InOwnDb
                ? Path.Combine(Domain.CustomSongPath, dataModel.FolderName, dataModel.BeatmapFileName)
                : Path.Combine(Domain.OsuSongPath, dataModel.FolderName, dataModel.BeatmapFileName);

            if (!File.Exists(osuFilePath))
            {
                return(null);
            }

            //lock (CountLock)
            //{
            //    _thumbCount++;
            //}

            var osuFile = await OSharp.Beatmap.OsuFile.ReadFromFileAsync(@"\\?\" + osuFilePath, options =>
            {
                options.IncludeSection("Events");
                options.IgnoreSample();
                options.IgnoreStoryboard();
            })
                          .ConfigureAwait(false);

            var guidStr = Guid.NewGuid().ToString();

            //lock (CountLock)
            //{
            //    _thumbCount--;
            //}

            var sourceBgFile = osuFile.Events?.BackgroundInfo?.Filename;

            if (string.IsNullOrWhiteSpace(sourceBgFile))
            {
                _appDbOperator.SetMapThumb(dataModel.BeatmapDbId, null);
                return(null);
            }

            var sourceBgPath = dataModel.InOwnDb
                ? Path.Combine(Domain.CustomSongPath, dataModel.FolderName, sourceBgFile)
                : Path.Combine(Domain.OsuSongPath, dataModel.FolderName, sourceBgFile);

            if (!File.Exists(sourceBgPath))
            {
                //_appDbOperator.SetMapThumb(dataModel.BeatmapDbId, null);
                return(null);
            }

            ResizeImageAndSave(sourceBgPath, guidStr, height: 200);
            _appDbOperator.SetMapThumb(dataModel.BeatmapDbId, guidStr);
            return(guidStr);
        }
Exemplo n.º 2
0
        public static async Task <string> GetThumbByBeatmapDbId(BeatmapDataModel dataModel)
        {
            return(await Task.Run(async() =>
            {
                await Lock.WaitAsync();
                try
                {
                    if (_appDbOperator.GetMapThumb(dataModel.BeatmapDbId, out var path) && path != null)
                    {
                        if (File.Exists(path))
                        {
                            return path;
                        }
                    }

                    var folder = dataModel.GetFolder(out var isFromDb, out var freePath);
                    var osuFilePath = isFromDb ? Path.Combine(folder, dataModel.BeatmapFileName) : freePath;

                    if (!File.Exists(osuFilePath))
                    {
                        return null;
                    }

                    var osuFile = await OsuFile.ReadFromFileAsync(osuFilePath, options =>
                    {
                        options.IncludeSection("Events");
                        options.IgnoreSample();
                        options.IgnoreStoryboard();
                    })
                                  .ConfigureAwait(false);
                    if (!osuFile.ReadSuccess)
                    {
                        return null;
                    }

                    var guidStr = Guid.NewGuid().ToString();

                    var sourceBgFile = osuFile.Events?.BackgroundInfo?.Filename;
                    if (string.IsNullOrWhiteSpace(sourceBgFile))
                    {
                        _appDbOperator.SetMapThumb(dataModel.BeatmapDbId, null);
                        return null;
                    }

                    var sourceBgPath = Path.Combine(folder, sourceBgFile);

                    if (!File.Exists(sourceBgPath))
                    {
                        //_appDbOperator.SetMapThumb(dataModel.BeatmapDbId, null);
                        return null;
                    }

                    ResizeImageAndSave(sourceBgPath, guidStr, height: 200);
                    _appDbOperator.SetMapThumb(dataModel.BeatmapDbId, guidStr);
                    return guidStr;
                }
                catch (Exception ex)
                {
                    Logger.Error("Error while creating beatmap thumb cache: {0}", dataModel.GetIdentity());
                    return default;
                }
                finally
                {
                    Lock.Release();
                }
            }));
        }