TerraForge3D  2.3.1
3D Terrain And Landscape Generator
SupportersTribute.cpp
1#define JSON_NOEXCEPTION
2
3#include "Misc/SupportersTribute.h"
4
5#include "Base/Texture2D.h"
6#include "Platform.h"
7
8#include <Utils.h>
9#include <imgui.h>
10#include <vector>
11#include <string>
12#include <json.hpp>
13
14
15void SupportersTribute::LoadstargazersData(nlohmann::json &data)
16{
17 bool isNetWorkConnected = IsNetWorkConnected();
18
19 for (nlohmann::json item : data)
20 {
21 GitHubData st;
22 st.name = item["login"];
23
24 if (!PathExist(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "github_avatars"))
25 {
26 MkDir(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "github_avatars");
27 }
28
29 if (!FileExists(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "github_avatars" PATH_SEPARATOR + st.name + "_" + std::string(item["node_id"])) && isNetWorkConnected)
30 {
31 std::string urlFull = item["avatar_url"];
32 std::string baseURL = urlFull.substr(0, 37);
33 std::string pathURL = urlFull.substr(38);
34 DownloadFile(baseURL, pathURL, GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "github_avatars" PATH_SEPARATOR + st.name + "_" + std::string(item["node_id"]));
35 }
36
37 st.avatar = new Texture2D(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "github_avatars" PATH_SEPARATOR + st.name + "_" + std::string(item["node_id"]));
38 stargazers.push_back(st);
39 }
40}
41
42void SupportersTribute::LoadcontributorsData(nlohmann::json &data)
43{
44 bool isNetWorkConnected = IsNetWorkConnected();
45
46 for (nlohmann::json item : data)
47 {
48 GitHubData st;
49 st.name = item["login"];
50
51 if (!PathExist(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "github_avatars"))
52 {
53 MkDir(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "github_avatars");
54 }
55
56 if (!FileExists(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "github_avatars" PATH_SEPARATOR + st.name + "_" + std::string(item["node_id"])) && isNetWorkConnected)
57 {
58 std::string urlFull = item["avatar_url"];
59 std::string baseURL = urlFull.substr(0, 37);
60 std::string pathURL = urlFull.substr(38);
61 DownloadFile(baseURL, pathURL, GetExecutableDir() + PATH_SEPARATOR
62 "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR
63 "github_avatars" PATH_SEPARATOR + st.name + "_" +
64 std::string(item["node_id"]));
65 }
66
67 st.avatar = new Texture2D(GetExecutableDir() + PATH_SEPARATOR "Data"
68 PATH_SEPARATOR "cache" PATH_SEPARATOR "github_avatars"
69 PATH_SEPARATOR + st.name + "_" +
70 std::string(item["node_id"]));
71 contributors.push_back(st);
72 }
73}
74
75
76SupportersTribute::SupportersTribute()
77{
78 if (IsNetWorkConnected() && (!FileExists(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "stargazers.terr3dcache") || rand() % 5 == 0))
79 {
80 Log("Internet Connection is Live!\nFetching Latest Supporters data.");
81 {
82 std::string repoDataStr = FetchURL("https://api.github.com", "/repos/Jaysmito101/TerraForge3D");
83 nlohmann::json repoData = nlohmann::json::parse(repoDataStr);
84 int stargazerCount = repoData["stargazers_count"];
85 int perPage = 30;
86 int lastPage = stargazerCount / perPage;
87 std::string stargazersRawData = FetchURL("https://api.github.com", "/repos/Jaysmito101/TerraForge3D/stargazers?per_page=30&page=" + std::to_string(lastPage));
88 SaveToFile(GetExecutableDir() + PATH_SEPARATOR +"Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "stargazers.terr3dcache", stargazersRawData);
89 nlohmann::json stargazersData = nlohmann::json::parse(stargazersRawData);
90 LoadstargazersData(stargazersData);
91 }
92 {
93 std::string contributorsRawData = FetchURL("https://api.github.com", "/repos/Jaysmito101/TerraForge3D/contributors");
94 SaveToFile(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "contributors.terr3dcache", contributorsRawData);
95 nlohmann::json contributorsData = nlohmann::json::parse(contributorsRawData);
96 LoadcontributorsData(contributorsData);
97 }
98 }
99
100 else
101 {
102 bool tmp = false;
103 Log("Trying to load cached data.");
104
105 if (FileExists(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "stargazers.terr3dcache"))
106 {
107 Log("Found Stargazers Cached Data!");
108 std::string stargazersRawData = ReadShaderSourceFile(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "stargazers.terr3dcache", &tmp);
109 nlohmann::json stargazersData = nlohmann::json::parse(stargazersRawData);
110 LoadstargazersData(stargazersData);
111 }
112
113 else
114 {
115 Log("Stargazers Cached Data not found!");
116 }
117
118 if (FileExists(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "contributors.terr3dcache"))
119 {
120 Log("Found Contributors Cached Data!");
121 std::string contributorsRawData = ReadShaderSourceFile(GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "contributors.terr3dcache", &tmp);
122 nlohmann::json contributorsData = nlohmann::json::parse(contributorsRawData);
123 LoadcontributorsData(contributorsData);
124 }
125
126 else
127 {
128 Log("Contributors Cached Data not found!");
129 }
130 }
131}
132
133SupportersTribute::~SupportersTribute()
134{
135 for(auto &st : stargazers)
136 {
137 if(st.avatar)
138 {
139 delete st.avatar;
140 }
141 }
142
143 for(auto &co : contributors)
144 {
145 if(co.avatar)
146 {
147 delete co.avatar;
148 }
149 }
150}
151
152void SupportersTribute::ShowSettings(bool *pOpen)
153{
154 ImGui::Begin("Supporters", pOpen, ImGuiWindowFlags_NoResize);
155 ImGui::SetWindowSize(ImVec2(250, 250));
156 ImGui::Separator();
157 ImGui::Text("Contributors");
158
159 for (GitHubData st : contributors)
160 {
161 uint32_t avTexId = 0;
162
163 if (st.avatar)
164 {
165 avTexId = st.avatar->GetRendererID();
166 }
167
168 ImGui::Image((ImTextureID)avTexId, ImVec2(30, 30));
169 ImGui::SameLine();
170 ImGui::Text(st.name.c_str());
171 }
172
173 ImGui::Separator();
174 ImGui::Separator();
175 ImGui::Text("Stargazers");
176
177 for (GitHubData st : stargazers)
178 {
179 uint32_t avTexId = 0;
180
181 if (st.avatar)
182 {
183 avTexId = st.avatar->GetRendererID();
184 }
185
186 ImGui::Image((ImTextureID)avTexId, ImVec2(30, 30));
187 ImGui::SameLine();
188 ImGui::Text(st.name.c_str());
189 }
190
191 ImGui::Separator();
192 ImGui::End();
193}
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json parse(InputType &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true, const bool ignore_comments=false)
deserialize from a compatible input
Definition: json.hpp:24605
a class to store JSON values
Definition: json.hpp:17860