コード例 #1
0
        public static bool SetUserImage(string gameId, Bitmap image, string sourceFile)
        {
            var game = Games[gameId];

            if (GameImageCache.TryRemove(gameId, out Bitmap oldImage))
            {
                var imageFormat   = Ujeby.Common.Tools.Graphics.GetImageFormat(new FileInfo(sourceFile).Extension);
                var imageLocation = GameDataProvider.SaveGameImage(game.Title, image, imageFormat);

                var newGameImage = new GameImage
                {
                    LocalFilename = imageLocation,
                    SourceUrl     = sourceFile,
                };

                var oldUserData = game.CustomData <UserData>();
                if (oldUserData != null)
                {
                    oldUserData.Image = newGameImage;
                }
                else
                {
                    game.Data = game.Data.Concat(new GameData[] { new UserData(game.Title)
                                                                  {
                                                                      Image = newGameImage
                                                                  } }).ToArray();
                }

                return(UpdateGameImageCache(gameId, newGameImage));
            }

            return(false);
        }
コード例 #2
0
        /// <summary>
        /// update game image in cache
        /// </summary>
        /// <param name="gameId"></param>
        /// <param name="gameImage"></param>
        /// <returns></returns>
        static bool UpdateGameImageCache(string gameId, GameImage gameImage)
        {
            try
            {
                if (gameImage != null)
                {
                    if (!File.Exists(gameImage.LocalFilename) && gameImage.SourceUrl != null)
                    {
                        DownloadGameImage(gameImage);
                    }

                    if (File.Exists(gameImage.LocalFilename))
                    {
                        Bitmap imageFromFile;
                        // existing game image file
                        using (Stream stream = File.OpenRead(gameImage.LocalFilename))
                            imageFromFile = (Bitmap)System.Drawing.Bitmap.FromStream(stream);

                        // removed game
                        if (Games[gameId].Removed)
                        {
                            imageFromFile = (Bitmap)Ujeby.Common.Tools.Graphics.MakeGrayscale3(imageFromFile);
                        }

                        // update game image in cache
                        GameImageCache.AddOrUpdate(gameId, imageFromFile, (key, oldValue) => imageFromFile);

                        return(true);
                    }
                }

                // image not found
                if (ImageCache.TryGetValue(DefaultImage.NotFound.ToString(), out Bitmap notFoundImage))
                {
                    GameImageCache.AddOrUpdate(gameId, notFoundImage, (key, oldValue) => notFoundImage);
                }

                return(true);
            }
            catch (Exception ex)
            {
                Log.WriteLine($"UpdateGameImageCache({ Games[gameId].Title }, { gameImage })");
                Log.WriteLine(ex.ToString());

                return(false);
            }
        }
コード例 #3
0
        /// <summary>
        /// load existing game images to cache
        /// </summary>
        /// <returns></returns>
        static bool LoadGameImages()
        {
            using (var tb = new TimedBlock($"LoadGameImages({ Games.Count })"))
            {
                try
                {
                    GameImageCache.Clear();

                    Parallel.ForEach(Games.Keys, gameId =>
                    {
                        UpdateGameImageCache(gameId, Games[gameId].Image);
                    });

                    return(true);
                }
                catch (Exception ex)
                {
                    Log.WriteLine(ex.ToString());
                }
            }

            return(false);
        }