Skip to content

Commit bf1d31a

Browse files
committed
temp push changes
1 parent c94bdd6 commit bf1d31a

File tree

5 files changed

+423
-0
lines changed

5 files changed

+423
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.maxgamer.quickshop.util.language.game;
2+
3+
public class GameLanguageManager {
4+
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.maxgamer.quickshop.util.language.game.distributions;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.List;
6+
7+
public interface Distribution {
8+
@NotNull List<String> getAvailableLanguages();
9+
10+
@NotNull List<String> getAvailableFiles();
11+
12+
@NotNull String getFile(String fileCrowdinPath, String crowdinLocale) throws Exception;
13+
14+
@NotNull String getFile(String fileCrowdinPath, String crowdinLocale, boolean forceFlush) throws Exception;
15+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.maxgamer.quickshop.util.language.game.distributions;
2+
3+
import com.google.common.cache.Cache;
4+
import com.google.common.cache.CacheBuilder;
5+
import com.google.gson.JsonElement;
6+
import lombok.val;
7+
import okhttp3.OkHttpClient;
8+
import okhttp3.Request;
9+
import okhttp3.Response;
10+
import org.apache.commons.lang.StringUtils;
11+
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
13+
import org.maxgamer.quickshop.QuickShop;
14+
import org.maxgamer.quickshop.util.JsonUtil;
15+
import org.maxgamer.quickshop.util.ReflectFactory;
16+
import org.maxgamer.quickshop.util.Util;
17+
import org.maxgamer.quickshop.util.language.game.distributions.bean.GameManifest;
18+
import org.maxgamer.quickshop.util.language.game.distributions.bean.VersionManifest;
19+
import org.maxgamer.quickshop.util.mojangapi.MojangApiMirror;
20+
21+
import java.io.IOException;
22+
import java.util.ArrayList;
23+
import java.util.Collections;
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.concurrent.TimeUnit;
27+
import java.util.logging.Level;
28+
29+
public class MojangDistribution {
30+
protected final Cache<String, String> requestCachePool = CacheBuilder.newBuilder()
31+
.initialCapacity(1)
32+
.expireAfterWrite(7, TimeUnit.DAYS)
33+
.recordStats()
34+
.build();
35+
private final QuickShop plugin;
36+
private final OkHttpClient client;
37+
private final MojangApiMirror mirror;
38+
39+
public MojangDistribution(QuickShop plugin, MojangApiMirror mirror) {
40+
this.plugin = plugin;
41+
this.mirror = mirror;
42+
Util.getCacheFolder().mkdirs();
43+
this.client = new OkHttpClient.Builder()
44+
.build();
45+
46+
}
47+
48+
@Nullable
49+
public VersionManifest getVersionManifest() {
50+
String url = mirror.getLauncherMetaRoot()+"/mc/game/version_manifest.json";
51+
String data;
52+
if (requestCachePool.getIfPresent(url) != null)
53+
return JsonUtil.standard().fromJson(requestCachePool.getIfPresent(url),VersionManifest.class);
54+
if (!grabIntoCaches(url)) return null;
55+
return JsonUtil.standard().fromJson(requestCachePool.getIfPresent(url),VersionManifest.class);
56+
}
57+
58+
59+
@Nullable
60+
public GameManifest getGameManifest(VersionManifest versionManifest, String gameVersion){
61+
for (VersionManifest.VersionsDTO version : versionManifest.getVersions()) {
62+
if(version.getId().equals(gameVersion)){
63+
String url = version.getUrl();
64+
if (!grabIntoCaches(url)) return null;
65+
return JsonUtil.standard().fromJson(requestCachePool.getIfPresent(url),GameManifest.class);
66+
}
67+
}
68+
return null;
69+
}
70+
@NotNull
71+
public List<String> getAvailableLanguages(){
72+
List<String> languages = new ArrayList<>();
73+
VersionManifest versionManifest = getVersionManifest();
74+
if(versionManifest == null)
75+
return Collections.emptyList();
76+
GameManifest gameManifest = getGameManifest(versionManifest, ReflectFactory.getServerVersion());
77+
if(gameManifest == null)
78+
return Collections.emptyList();
79+
if(!grabIntoCaches(gameManifest.getAssetIndex().getUrl()))
80+
return Collections.emptyList();
81+
String versionMapping = requestCachePool.getIfPresent(gameManifest.getAssetIndex().getUrl());
82+
if(versionMapping == null)
83+
return Collections.emptyList();
84+
for (Map.Entry<String, JsonElement> objects : JsonUtil.parser().parse(versionMapping).getAsJsonObject().get("objects").getAsJsonObject().entrySet()) {
85+
if(objects.getKey().startsWith("minecraft/lang/")){
86+
languages.add(StringUtils.substringBetween("minecraft/lang/",".json"));
87+
}
88+
}
89+
return languages;
90+
}
91+
92+
93+
public boolean grabIntoCaches(String url) {
94+
String data;
95+
try (Response response = client.newCall(new Request.Builder().get().url(url).build()).execute()) {
96+
val body = response.body();
97+
if (body == null) return true;
98+
data = body.string();
99+
if (response.code() != 200) {
100+
plugin.getLogger().warning("Couldn't get manifest: " + response.code() + ", please report to QuickShop!");
101+
return false;
102+
}
103+
requestCachePool.put(url, data);
104+
} catch (IOException e) {
105+
plugin.getLogger().log(Level.WARNING, "Failed to download manifest.json, multi-language system won't work");
106+
return false;
107+
}
108+
return true;
109+
}
110+
}
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
package org.maxgamer.quickshop.util.language.game.distributions.bean;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.util.List;
8+
9+
@NoArgsConstructor
10+
@Data
11+
public class GameManifest {
12+
13+
@JsonProperty("arguments")
14+
private ArgumentsDTO arguments;
15+
@JsonProperty("assetIndex")
16+
private AssetIndexDTO assetIndex;
17+
@JsonProperty("assets")
18+
private String assets;
19+
@JsonProperty("complianceLevel")
20+
private Integer complianceLevel;
21+
@JsonProperty("downloads")
22+
private DownloadsDTO downloads;
23+
@JsonProperty("id")
24+
private String id;
25+
@JsonProperty("javaVersion")
26+
private JavaVersionDTO javaVersion;
27+
@JsonProperty("libraries")
28+
private List<LibrariesDTO> libraries;
29+
@JsonProperty("logging")
30+
private LoggingDTO logging;
31+
@JsonProperty("mainClass")
32+
private String mainClass;
33+
@JsonProperty("minimumLauncherVersion")
34+
private Integer minimumLauncherVersion;
35+
@JsonProperty("releaseTime")
36+
private String releaseTime;
37+
@JsonProperty("time")
38+
private String time;
39+
@JsonProperty("type")
40+
private String type;
41+
42+
@NoArgsConstructor
43+
@Data
44+
public static class ArgumentsDTO {
45+
@JsonProperty("game")
46+
private List<String> game;
47+
@JsonProperty("jvm")
48+
private List<JvmDTO> jvm;
49+
50+
@NoArgsConstructor
51+
@Data
52+
public static class JvmDTO {
53+
@JsonProperty("rules")
54+
private List<RulesDTO> rules;
55+
@JsonProperty("value")
56+
private List<String> value;
57+
58+
@NoArgsConstructor
59+
@Data
60+
public static class RulesDTO {
61+
@JsonProperty("action")
62+
private String action;
63+
@JsonProperty("os")
64+
private OsDTO os;
65+
66+
@NoArgsConstructor
67+
@Data
68+
public static class OsDTO {
69+
@JsonProperty("name")
70+
private String name;
71+
}
72+
}
73+
}
74+
}
75+
76+
@NoArgsConstructor
77+
@Data
78+
public static class AssetIndexDTO {
79+
@JsonProperty("id")
80+
private String id;
81+
@JsonProperty("sha1")
82+
private String sha1;
83+
@JsonProperty("size")
84+
private Integer size;
85+
@JsonProperty("totalSize")
86+
private Integer totalSize;
87+
@JsonProperty("url")
88+
private String url;
89+
}
90+
91+
@NoArgsConstructor
92+
@Data
93+
public static class DownloadsDTO {
94+
@JsonProperty("client")
95+
private ClientDTO client;
96+
@JsonProperty("client_mappings")
97+
private ClientMappingsDTO clientMappings;
98+
@JsonProperty("server")
99+
private ServerDTO server;
100+
@JsonProperty("server_mappings")
101+
private ServerMappingsDTO serverMappings;
102+
103+
@NoArgsConstructor
104+
@Data
105+
public static class ClientDTO {
106+
@JsonProperty("sha1")
107+
private String sha1;
108+
@JsonProperty("size")
109+
private Integer size;
110+
@JsonProperty("url")
111+
private String url;
112+
}
113+
114+
@NoArgsConstructor
115+
@Data
116+
public static class ClientMappingsDTO {
117+
@JsonProperty("sha1")
118+
private String sha1;
119+
@JsonProperty("size")
120+
private Integer size;
121+
@JsonProperty("url")
122+
private String url;
123+
}
124+
125+
@NoArgsConstructor
126+
@Data
127+
public static class ServerDTO {
128+
@JsonProperty("sha1")
129+
private String sha1;
130+
@JsonProperty("size")
131+
private Integer size;
132+
@JsonProperty("url")
133+
private String url;
134+
}
135+
136+
@NoArgsConstructor
137+
@Data
138+
public static class ServerMappingsDTO {
139+
@JsonProperty("sha1")
140+
private String sha1;
141+
@JsonProperty("size")
142+
private Integer size;
143+
@JsonProperty("url")
144+
private String url;
145+
}
146+
}
147+
148+
@NoArgsConstructor
149+
@Data
150+
public static class JavaVersionDTO {
151+
@JsonProperty("component")
152+
private String component;
153+
@JsonProperty("majorVersion")
154+
private Integer majorVersion;
155+
}
156+
157+
@NoArgsConstructor
158+
@Data
159+
public static class LoggingDTO {
160+
@JsonProperty("client")
161+
private ClientDTO client;
162+
163+
@NoArgsConstructor
164+
@Data
165+
public static class ClientDTO {
166+
@JsonProperty("argument")
167+
private String argument;
168+
@JsonProperty("file")
169+
private FileDTO file;
170+
@JsonProperty("type")
171+
private String type;
172+
173+
@NoArgsConstructor
174+
@Data
175+
public static class FileDTO {
176+
@JsonProperty("id")
177+
private String id;
178+
@JsonProperty("sha1")
179+
private String sha1;
180+
@JsonProperty("size")
181+
private Integer size;
182+
@JsonProperty("url")
183+
private String url;
184+
}
185+
}
186+
}
187+
188+
@NoArgsConstructor
189+
@Data
190+
public static class LibrariesDTO {
191+
@JsonProperty("downloads")
192+
private DownloadsDTO downloads;
193+
@JsonProperty("name")
194+
private String name;
195+
@JsonProperty("rules")
196+
private List<RulesDTO> rules;
197+
@JsonProperty("natives")
198+
private NativesDTO natives;
199+
@JsonProperty("extract")
200+
private ExtractDTO extract;
201+
202+
@NoArgsConstructor
203+
@Data
204+
public static class DownloadsDTO {
205+
@JsonProperty("artifact")
206+
private ArtifactDTO artifact;
207+
208+
@NoArgsConstructor
209+
@Data
210+
public static class ArtifactDTO {
211+
@JsonProperty("path")
212+
private String path;
213+
@JsonProperty("sha1")
214+
private String sha1;
215+
@JsonProperty("size")
216+
private Integer size;
217+
@JsonProperty("url")
218+
private String url;
219+
}
220+
}
221+
222+
@NoArgsConstructor
223+
@Data
224+
public static class NativesDTO {
225+
@JsonProperty("osx")
226+
private String osx;
227+
}
228+
229+
@NoArgsConstructor
230+
@Data
231+
public static class ExtractDTO {
232+
@JsonProperty("exclude")
233+
private List<String> exclude;
234+
}
235+
236+
@NoArgsConstructor
237+
@Data
238+
public static class RulesDTO {
239+
@JsonProperty("action")
240+
private String action;
241+
@JsonProperty("os")
242+
private OsDTO os;
243+
244+
@NoArgsConstructor
245+
@Data
246+
public static class OsDTO {
247+
@JsonProperty("name")
248+
private String name;
249+
}
250+
}
251+
}
252+
}

0 commit comments

Comments
 (0)