DDraceNetwork Docs
skins.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef GAME_CLIENT_COMPONENTS_SKINS_H
4#define GAME_CLIENT_COMPONENTS_SKINS_H
5
6#include <base/lock.h>
7
9
11#include <game/client/skin.h>
12
13#include <chrono>
14#include <string_view>
15#include <unordered_map>
16
17class CHttpRequest;
18
19class CSkins : public CComponent
20{
21public:
22 CSkins();
23
24 typedef std::function<void()> TSkinLoadedCallback;
25
26 int Sizeof() const override { return sizeof(*this); }
27 void OnInit() override;
28 void OnShutdown() override;
29
30 void Refresh(TSkinLoadedCallback &&SkinLoadedCallback);
31 std::chrono::nanoseconds LastRefreshTime() const { return m_LastRefreshTime; }
32
33 const std::unordered_map<std::string_view, std::unique_ptr<CSkin>> &GetSkinsUnsafe() const { return m_Skins; }
34 const CSkin *FindOrNullptr(const char *pName, bool IgnorePrefix = false);
35 const CSkin *Find(const char *pName);
36
37 void RandomizeSkin(int Dummy);
38
39 static bool IsVanillaSkin(const char *pName);
40
41 constexpr static const char *VANILLA_SKINS[] = {"bluekitty", "bluestripe", "brownbear",
42 "cammo", "cammostripes", "coala", "default", "limekitty",
43 "pinky", "redbopp", "redstripe", "saddo", "toptri",
44 "twinbop", "twintri", "warpaint", "x_ninja", "x_spec"};
45
46private:
47 class CSkinDownloadJob : public IJob
48 {
49 public:
50 CSkinDownloadJob(CSkins *pSkins, const char *pName);
52
53 bool Abort() override REQUIRES(!m_Lock);
54
56
57 protected:
58 void Run() override REQUIRES(!m_Lock);
59
60 private:
64 std::shared_ptr<CHttpRequest> m_pGetRequest;
66 };
67
69 {
70 private:
72
73 public:
74 std::shared_ptr<CSkinDownloadJob> m_pDownloadJob = nullptr;
75
76 CLoadingSkin(CLoadingSkin &&Other) = default;
77 CLoadingSkin(const char *pName);
79
80 bool operator<(const CLoadingSkin &Other) const;
81 bool operator<(const char *pOther) const;
82 bool operator==(const char *pOther) const;
83
84 CLoadingSkin &operator=(CLoadingSkin &&Other) = default;
85
86 const char *Name() const { return m_aName; }
87 };
88
89 std::unordered_map<std::string_view, std::unique_ptr<CSkin>> m_Skins;
90
91 std::unordered_map<std::string_view, std::unique_ptr<CLoadingSkin>> m_LoadingSkins;
92 std::chrono::nanoseconds m_LastRefreshTime;
93
96
97 const CSkin *LoadSkin(const char *pName, const char *pPath, int DirType);
98 const CSkin *LoadSkin(const char *pName, CImageInfo &Info);
99 const CSkin *FindImpl(const char *pName);
100 static int SkinScan(const char *pName, int IsDir, int DirType, void *pUser);
101};
102#endif
Definition: component.h:20
Definition: http.h:53
Definition: image.h:12
Definition: lock.h:88
Definition: skins.h:69
const char * Name() const
Definition: skins.h:86
CLoadingSkin & operator=(CLoadingSkin &&Other)=default
CLoadingSkin(CLoadingSkin &&Other)=default
Definition: skins.h:48
void Run() override REQUIRES(!m_Lock)
Definition: skins.cpp:469
CImageInfo & ImageInfo()
Definition: skins.h:55
CImageInfo m_ImageInfo
Definition: skins.h:65
CSkinDownloadJob(CSkins *pSkins, const char *pName)
Definition: skins.cpp:441
bool Abort() override REQUIRES(!m_Lock)
Definition: skins.cpp:453
CSkins * m_pSkins
Definition: skins.h:61
~CSkinDownloadJob()
Definition: skins.cpp:448
char m_aName[MAX_SKIN_LENGTH]
Definition: skins.h:62
std::shared_ptr< CHttpRequest > m_pGetRequest
Definition: skins.h:64
CLock m_Lock
Definition: skins.h:63
Definition: skins.h:20
const CSkin * LoadSkin(const char *pName, const char *pPath, int DirType)
Definition: skins.cpp:119
const std::unordered_map< std::string_view, std::unique_ptr< CSkin > > & GetSkinsUnsafe() const
Definition: skins.h:33
char m_aEventSkinPrefix[MAX_SKIN_LENGTH]
Definition: skins.h:95
static bool IsVanillaSkin(const char *pName)
Definition: skins.cpp:41
CSkin m_PlaceholderSkin
Definition: skins.h:94
const CSkin * FindOrNullptr(const char *pName, bool IgnorePrefix=false)
Definition: skins.cpp:338
void OnShutdown() override
Definition: skins.cpp:300
std::unordered_map< std::string_view, std::unique_ptr< CSkin > > m_Skins
Definition: skins.h:89
CSkins()
Definition: skins.cpp:21
const CSkin * FindImpl(const char *pName)
Definition: skins.cpp:361
int Sizeof() const override
Definition: skins.h:26
void OnInit() override
Definition: skins.cpp:282
std::function< void()> TSkinLoadedCallback
Definition: skins.h:24
void RandomizeSkin(int Dummy)
Definition: skins.cpp:399
static constexpr const char * VANILLA_SKINS[]
Definition: skins.h:41
const CSkin * Find(const char *pName)
Definition: skins.cpp:324
std::unordered_map< std::string_view, std::unique_ptr< CLoadingSkin > > m_LoadingSkins
Definition: skins.h:91
static int SkinScan(const char *pName, int IsDir, int DirType, void *pUser)
Definition: skins.cpp:53
void Refresh(TSkinLoadedCallback &&SkinLoadedCallback)
Definition: skins.cpp:305
std::chrono::nanoseconds m_LastRefreshTime
Definition: skins.h:92
std::chrono::nanoseconds LastRefreshTime() const
Definition: skins.h:31
Definition: jobs.h:20
@ MAX_SKIN_LENGTH
Definition: protocol.h:99
bool operator==(const SHA256_DIGEST &that, const SHA256_DIGEST &other)
Definition: hash.h:36
#define REQUIRES(...)
Definition: lock.h:32
Definition: skin.h:15
bool operator<(const ColorRGBA &Left, const ColorRGBA &Right)
Definition: tileart.cpp:8