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