Skip to content

Commit 77e8055

Browse files
committed
Added some comments
1 parent 1720f03 commit 77e8055

File tree

1 file changed

+32
-10
lines changed
  • src/main/java/org/maxgamer/quickshop/util/language/text/distributions/crowdin

1 file changed

+32
-10
lines changed

src/main/java/org/maxgamer/quickshop/util/language/text/distributions/crowdin/CrowdinOTA.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import okhttp3.Request;
1111
import okhttp3.Response;
1212
import org.apache.commons.codec.digest.DigestUtils;
13+
import org.apache.commons.lang.Validate;
1314
import org.bukkit.configuration.file.YamlConfiguration;
1415
import org.jetbrains.annotations.NotNull;
1516
import org.jetbrains.annotations.Nullable;
@@ -48,12 +49,20 @@ public CrowdinOTA(QuickShop plugin) {
4849
.build();
4950

5051
}
52+
/**
53+
* Getting the Crowdin distribution manifest
54+
* @return The distribution manifest
55+
*/
5156

5257
@Nullable
5358
public Manifest getManifest() {
5459
return JsonUtil.getGson().fromJson(getManifestJson(), Manifest.class);
5560
}
5661

62+
/**
63+
* Getting the Crowdin distribution manifest json
64+
* @return The distribution manifest json
65+
*/
5766
@Nullable
5867
public String getManifestJson() {
5968
String url = CROWDIN_OTA_HOST + "manifest.json";
@@ -79,6 +88,11 @@ public String getManifestJson() {
7988
return data;
8089
}
8190

91+
/**
92+
* Getting crowdin language mapping (crowdin code -> minecraft code)
93+
* Can be set on Crowdin platform
94+
* @return The language mapping
95+
*/
8296
public Map<String, String> genLanguageMapping() {
8397
if (getManifestJson() == null) {
8498
return new HashMap<>();
@@ -98,6 +112,10 @@ public Map<String, String> genLanguageMapping() {
98112
return mapping;
99113
}
100114

115+
/**
116+
* Getting all languages available on crowdin, so we can use that as the key to read language mapping.
117+
* @return The languages available
118+
*/
101119
@Override
102120
@NotNull
103121
public List<String> getAvailableLanguages() {
@@ -132,39 +150,43 @@ public List<String> getAvailableFiles() {
132150
@NotNull
133151
public String getFile(String fileCrowdinPath, String crowdinLocale, boolean forceFlush) throws Exception {
134152
Manifest manifest = getManifest();
135-
if (manifest == null) {
136-
throw new IllegalStateException("Failed to get project manifest");
137-
}
138-
if (!manifest.getFiles().contains(fileCrowdinPath)) {
139-
throw new IllegalArgumentException("The file " + fileCrowdinPath + " not exists on Crowdin");
140-
}
141-
if (manifest.getCustomLanguages() != null && !manifest.getCustomLanguages().contains(crowdinLocale)) {
142-
throw new IllegalArgumentException("The locale " + crowdinLocale + " not exists on Crowdin");
143-
}
153+
// Validate
154+
Validate.notNull(manifest,"Manifest cannot be null");
155+
Validate.isTrue(manifest.getFiles().contains(fileCrowdinPath),"Requested file not exists on Crowdin");
156+
Validate.notNull(manifest.getCustomLanguages(),"Crowdin custom languages payload incorrect");
157+
Validate.isTrue(manifest.getCustomLanguages().contains(crowdinLocale),"Requested locale "+crowdinLocale+" not exists on Crowdin");
158+
// Post path (replaced with locale code)
144159
String postProcessingPath = fileCrowdinPath.replace("%locale%", crowdinLocale);
160+
// Create path hash to store the file
145161
String pathHash = DigestUtils.sha1Hex(postProcessingPath);
162+
// Reading metadata
146163
File metadataFile = new File(Util.getCacheFolder(), "i18n.metadata");
147164
YamlConfiguration cacheMetadata = YamlConfiguration.loadConfiguration(metadataFile);
165+
// Reading cloud timestamp
148166
long localeTimestamp = cacheMetadata.getLong(pathHash + ".timestamp");
167+
// Reading locale cache
149168
File cachedDataFile = new File(Util.getCacheFolder(), pathHash);
150169
String data = null;
170+
// Getting local cache
151171
if (cachedDataFile.exists()) {
152172
Util.debugLog("Reading data from local cache: " + cachedDataFile.getCanonicalPath());
153173
data = Util.readToString(cachedDataFile);
154174
}
155175
// invalidate cache, flush it
176+
// force flush required OR local cache not exists OR outdated
156177
if (forceFlush || data == null || localeTimestamp != manifest.getTimestamp()) {
157178
String url = CROWDIN_OTA_HOST + "content" + fileCrowdinPath.replace("%locale%", crowdinLocale);
158179
Util.debugLog("Reading data from remote server: " + url);
159180
try (Response response = client.newCall(new Request.Builder().get().url(url).build()).execute()) {
160181
val body = response.body();
161182
if (body == null) {
162-
throw new OTAException(response.code(), ""); // Returns empty string
183+
throw new OTAException(response.code(), ""); // Returns empty string (failed to getting content)
163184
}
164185
data = body.string();
165186
if (response.code() != 200) {
166187
throw new OTAException(response.code(), data);
167188
}
189+
// save to local cache file
168190
Files.write(cachedDataFile.toPath(), data.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
169191
} catch (IOException e) {
170192
plugin.getLogger().log(Level.WARNING, "Failed to download manifest.json, multi-language system may won't work");

0 commit comments

Comments
 (0)