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 static bool IsSpecialSkin(const char *pName);
41
42 constexpr static const char *VANILLA_SKINS[] = {"bluekitty", "bluestripe", "brownbear",
43 "cammo", "cammostripes", "coala", "default", "limekitty",
44 "pinky", "redbopp", "redstripe", "saddo", "toptri",
45 "twinbop", "twintri", "warpaint", "x_ninja", "x_spec"};
46
47private:
48 class CSkinDownloadJob : public IJob
49 {
50 public:
51 CSkinDownloadJob(CSkins *pSkins, const char *pName);
53
54 bool Abort() override REQUIRES(!m_Lock);
55
57
58 protected:
59 void Run() override REQUIRES(!m_Lock);
60
61 private:
65 std::shared_ptr<CHttpRequest> m_pGetRequest;
67 };
68
70 {
71 private:
73
74 public:
75 std::shared_ptr<CSkinDownloadJob> m_pDownloadJob = nullptr;
76
77 CLoadingSkin(CLoadingSkin &&Other) = default;
78 CLoadingSkin(const char *pName);
80
81 bool operator<(const CLoadingSkin &Other) const;
82 bool operator<(const char *pOther) const;
83 bool operator==(const char *pOther) const;
84
85 CLoadingSkin &operator=(CLoadingSkin &&Other) = default;
86
87 const char *Name() const { return m_aName; }
88 };
89
90 std::unordered_map<std::string_view, std::unique_ptr<CSkin>> m_Skins;
91
92 std::unordered_map<std::string_view, std::unique_ptr<CLoadingSkin>> m_LoadingSkins;
93 std::chrono::nanoseconds m_LastRefreshTime;
94
97
98 const CSkin *LoadSkin(const char *pName, const char *pPath, int DirType);
99 const CSkin *LoadSkin(const char *pName, CImageInfo &Info);
100 const CSkin *FindImpl(const char *pName);
101 static int SkinScan(const char *pName, int IsDir, int DirType, void *pUser);
102};
103#endif
Definition: component.h:20
Definition: http.h:53
Definition: image.h:12
Definition: lock.h:88
Definition: skins.h:70
const char * Name() const
Definition: skins.h:87
CLoadingSkin & operator=(CLoadingSkin &&Other)=default
CLoadingSkin(CLoadingSkin &&Other)=default
Definition: skins.h:49
void Run() override REQUIRES(!m_Lock)
Definition: skins.cpp:480
CImageInfo & ImageInfo()
Definition: skins.h:56
CImageInfo m_ImageInfo
Definition: skins.h:66
CSkinDownloadJob(CSkins *pSkins, const char *pName)
Definition: skins.cpp:452
bool Abort() override REQUIRES(!m_Lock)
Definition: skins.cpp:464
CSkins * m_pSkins
Definition: skins.h:62
~CSkinDownloadJob()
Definition: skins.cpp:459
char m_aName[MAX_SKIN_LENGTH]
Definition: skins.h:63
std::shared_ptr< CHttpRequest > m_pGetRequest
Definition: skins.h:65
CLock m_Lock
Definition: skins.h:64
Definition: skins.h:20
const CSkin * LoadSkin(const char *pName, const char *pPath, int DirType)
Definition: skins.cpp:124
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:96
static bool IsVanillaSkin(const char *pName)
Definition: skins.cpp:41
CSkin m_PlaceholderSkin
Definition: skins.h:95
const CSkin * FindOrNullptr(const char *pName, bool IgnorePrefix=false)
Definition: skins.cpp:343
void OnShutdown() override
Definition: skins.cpp:305
std::unordered_map< std::string_view, std::unique_ptr< CSkin > > m_Skins
Definition: skins.h:90
CSkins()
Definition: skins.cpp:21
const CSkin * FindImpl(const char *pName)
Definition: skins.cpp:366
int Sizeof() const override
Definition: skins.h:26
void OnInit() override
Definition: skins.cpp:287
std::function< void()> TSkinLoadedCallback
Definition: skins.h:24
void RandomizeSkin(int Dummy)
Definition: skins.cpp:404
static bool IsSpecialSkin(const char *pName)
Definition: skins.cpp:46
static constexpr const char * VANILLA_SKINS[]
Definition: skins.h:42
const CSkin * Find(const char *pName)
Definition: skins.cpp:329
std::unordered_map< std::string_view, std::unique_ptr< CLoadingSkin > > m_LoadingSkins
Definition: skins.h:92
static int SkinScan(const char *pName, int IsDir, int DirType, void *pUser)
Definition: skins.cpp:58
void Refresh(TSkinLoadedCallback &&SkinLoadedCallback)
Definition: skins.cpp:310
std::chrono::nanoseconds m_LastRefreshTime
Definition: skins.h:93
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