1#ifndef GAME_CLIENT_COMPONENTS_COMMUNITY_ICONS_H
2#define GAME_CLIENT_COMPONENTS_COMMUNITY_ICONS_H
3
4#include <base/hash.h>
5
6#include <engine/graphics.h>
7#include <engine/serverbrowser.h>
8#include <engine/shared/jobs.h>
9
10#include <game/client/component.h>
11#include <game/client/ui_rect.h>
12
13#include <optional>
14
15class IHttpRequest;
16
17class CCommunityIcon
18{
19 friend class CCommunityIcons;
20
21private:
22 char m_aCommunityId[CServerInfo::MAX_COMMUNITY_ID_LENGTH];
23 SHA256_DIGEST m_Sha256;
24 IGraphics::CTextureHandle m_OrgTexture;
25 IGraphics::CTextureHandle m_GreyTexture;
26};
27
28class CCommunityIcons : public CComponentInterfaces
29{
30public:
31 const CCommunityIcon *Find(const char *pCommunityId);
32 void Render(const CCommunityIcon *pIcon, CUIRect Rect, bool Active);
33 void Load();
34 void Update();
35 void Shutdown();
36
37private:
38 class CAbstractCommunityIconJob
39 {
40 protected:
41 CCommunityIcons *m_pCommunityIcons;
42 char m_aCommunityId[CServerInfo::MAX_COMMUNITY_ID_LENGTH];
43 char m_aPath[IO_MAX_PATH_LENGTH];
44 int m_StorageType;
45 bool m_Success = false;
46 SHA256_DIGEST m_Sha256;
47
48 CAbstractCommunityIconJob(CCommunityIcons *pCommunityIcons, const char *pCommunityId, int StorageType);
49
50 public:
51 const char *CommunityId() const { return m_aCommunityId; }
52 bool Success() const { return m_Success; }
53 const SHA256_DIGEST &Sha256() const { return m_Sha256; }
54 virtual ~CAbstractCommunityIconJob() = default;
55 };
56
57 class CCommunityIconLoadJob : public IJob, public CAbstractCommunityIconJob
58 {
59 CImageInfo m_ImageInfo;
60 CImageInfo m_ImageInfoGrayscale;
61
62 protected:
63 void Run() override;
64
65 public:
66 CCommunityIconLoadJob(CCommunityIcons *pCommunityIcons, const char *pCommunityId, int StorageType);
67 ~CCommunityIconLoadJob() override;
68
69 CImageInfo &ImageInfo() { return m_ImageInfo; }
70 CImageInfo &ImageInfoGrayscale() { return m_ImageInfoGrayscale; }
71 };
72
73 class CCommunityIconDownloadJob : public CAbstractCommunityIconJob
74 {
75 std::shared_ptr<IHttpRequest> m_pHttpRequest;
76
77 public:
78 CCommunityIconDownloadJob(CCommunityIcons *pCommunityIcons, const char *pCommunityId, const char *pUrl, const SHA256_DIGEST &Sha256);
79 std::shared_ptr<IHttpRequest> HttpRequest() { return m_pHttpRequest; }
80 };
81
82 std::vector<CCommunityIcon> m_vCommunityIcons;
83 std::deque<std::shared_ptr<CCommunityIconLoadJob>> m_CommunityIconLoadJobs;
84 std::deque<std::shared_ptr<CCommunityIconDownloadJob>> m_CommunityIconDownloadJobs;
85 std::optional<SHA256_DIGEST> m_CommunityIconsInfoSha256;
86 static int FileScan(const char *pName, int IsDir, int DirType, void *pUser);
87 bool LoadFile(const char *pPath, int DirType, CImageInfo &Info, CImageInfo &InfoGrayscale, SHA256_DIGEST &Sha256);
88 void LoadFinish(const char *pCommunityId, CImageInfo &Info, CImageInfo &InfoGrayscale, const SHA256_DIGEST &Sha256);
89};
90
91#endif
92