22
33import com .google .common .cache .Cache ;
44import com .google .common .cache .CacheBuilder ;
5+ import com .google .gson .JsonElement ;
6+ import com .google .gson .JsonParser ;
7+ import com .google .gson .JsonPrimitive ;
58import org .apache .commons .codec .digest .DigestUtils ;
9+ import org .apache .commons .lang .StringUtils ;
610import org .bukkit .configuration .file .YamlConfiguration ;
711import org .jetbrains .annotations .NotNull ;
812import org .jetbrains .annotations .Nullable ;
1620import java .io .File ;
1721import java .io .IOException ;
1822import java .net .URL ;
19- import java .util . Collections ;
20- import java .util .List ;
23+ import java .nio . charset . StandardCharsets ;
24+ import java .util .* ;
2125import java .util .concurrent .TimeUnit ;
2226
2327public class CrowdinOTA implements Distribution {
2428 protected static final String CROWDIN_OTA_HOST = "https://distributions.crowdin.net/daf1a8db40f132ce157c457xrm4/" ;
25- protected final Cache <String , String > requestCachePool = CacheBuilder .newBuilder ()
29+ protected final Cache <String , byte [] > requestCachePool = CacheBuilder .newBuilder ()
2630 .expireAfterWrite (7 , TimeUnit .DAYS )
2731 .recordStats ()
2832 .build ();
@@ -33,40 +37,73 @@ public CrowdinOTA(QuickShop plugin) {
3337 Util .getCacheFolder ().mkdirs ();
3438 }
3539
36- private @ Nullable String requestWithCache (String url ) {
37- String data = requestCachePool .getIfPresent (url );
40+ private byte [] requestWithCache (@ NotNull String url , @ Nullable File saveTo ) {
41+ byte [] data = requestCachePool .getIfPresent (url );
3842 if (data == null ) {
3943 try {
40- data = HttpRequest .get (new URL (url ))
44+ HttpRequest . BufferedResponse response = HttpRequest .get (new URL (url ))
4145 .execute ()
4246 .expectResponseCode (200 )
43- .returnContent ()
44- .asString ("UTF-8" );
47+ .returnContent ();
48+ if (saveTo != null )
49+ response .saveContent (saveTo );
50+ return response .asBytes ();
4551 } catch (IOException e ) {
4652 e .printStackTrace ();
4753 return null ;
54+ } catch (InterruptedException e ) {
55+ e .printStackTrace ();
4856 }
49- if (data != null )
50- requestCachePool .put (url , data );
57+ return null ;
5158 }
5259 return data ;
5360 }
5461
5562 @ Nullable
5663 public Manifest getManifest () {
5764 String url = CROWDIN_OTA_HOST + "manifest.json" ;
58- String data = requestWithCache (url );
59- if (data == null )
65+ String data = new String ( requestWithCache (url , null ), StandardCharsets . UTF_8 );
66+ if (StringUtils . isEmpty ( data ) )
6067 return null ;
6168 return JsonUtil .getGson ().fromJson (data , Manifest .class );
6269 }
6370
71+ @ Nullable
72+ public String getManifestJson () {
73+ String url = CROWDIN_OTA_HOST + "manifest.json" ;
74+ String data = new String (requestWithCache (url ,null ), StandardCharsets .UTF_8 );
75+ if (StringUtils .isEmpty (data ))
76+ return null ;
77+ return data ;
78+ }
79+
80+ public Map <String , String > genLanguageMapping () {
81+ if (getManifestJson () == null )
82+ return new HashMap <>();
83+ Map <String , String > mapping = new HashMap <>();
84+ JsonElement parser = new JsonParser ().parse (getManifestJson ());
85+ for (Map .Entry <String , JsonElement > set : parser .getAsJsonObject ().getAsJsonObject ("language_mapping" ).entrySet ()) {
86+ if (!set .getValue ().isJsonObject ())
87+ continue ;
88+ JsonPrimitive object = set .getValue ().getAsJsonObject ().getAsJsonPrimitive ("locale" );
89+ if (object == null )
90+ continue ;
91+ mapping .put (set .getKey (), object .getAsString ());
92+ }
93+ return mapping ;
94+ }
95+
6496 @ NotNull
6597 public List <String > getAvailableLanguages () {
6698 Manifest manifest = getManifest ();
6799 if (manifest == null )
68100 return Collections .emptyList ();
69- return manifest .getLanguages ();
101+ List <String > languages = new ArrayList <>();
102+ Map <String , String > mapping = genLanguageMapping ();
103+ for (String language : manifest .getLanguages ()) {
104+ languages .add (mapping .getOrDefault (language , language ));
105+ }
106+ return languages ;
70107 }
71108
72109 @ NotNull
@@ -79,11 +116,11 @@ public List<String> getAvailableFiles() {
79116
80117 @ Override
81118 public @ NotNull String getFile (String fileCrowdinPath , String crowdinLocale ) throws Exception {
82- return getFile (fileCrowdinPath ,crowdinLocale ,false );
119+ return getFile (fileCrowdinPath , crowdinLocale , false );
83120 }
84121
85122 @ NotNull
86- public String getFile (String fileCrowdinPath , String crowdinLocale , boolean forceFlush )throws Exception {
123+ public String getFile (String fileCrowdinPath , String crowdinLocale , boolean forceFlush ) throws Exception {
87124 Manifest manifest = getManifest ();
88125 if (manifest == null )
89126 throw new IllegalStateException ("Failed to get project manifest" );
@@ -98,18 +135,20 @@ public String getFile(String fileCrowdinPath, String crowdinLocale, boolean forc
98135 long localeTimestamp = cacheMetadata .getLong (pathHash + ".timestamp" );
99136 File cachedDataFile = new File (Util .getCacheFolder (), pathHash );
100137 String data = null ;
101- if (cachedDataFile .exists ()){
138+ if (cachedDataFile .exists ()) {
102139 data = Util .readToString (cachedDataFile );
103140 }
104141 // invalidate cache, flush it
105- if (forceFlush || data == null ||localeTimestamp != manifest .getTimestamp ()) {
106- String url = CROWDIN_OTA_HOST + "content/ " + fileCrowdinPath .replace ("%locale%" , postProcessingPath );
107- data = requestWithCache (url );
108- if (data == null )
109- throw new IOException ("Couldn't download translation from remote server" );
142+ if (forceFlush || data == null || localeTimestamp != manifest .getTimestamp ()) {
143+ String url = CROWDIN_OTA_HOST + "content" + fileCrowdinPath .replace ("%locale%" , crowdinLocale );
144+ byte [] bin = requestWithCache (url , cachedDataFile );
145+ if (bin == null )
146+ throw new IOException ("Couldn't download translation from remote server. If you see any error like \" 404 Not Found \" , please report it to QuickShop. " );
110147 // update cache index
148+ data = Util .readToString (cachedDataFile );
111149 cacheMetadata .set (pathHash + ".timestamp" , manifest .getTimestamp ());
112150 cacheMetadata .save (metadataFile );
151+ return new String (bin , StandardCharsets .UTF_8 );
113152 }
114153// if (data == null) {
115154// cacheMetadata.set(pathHash, null);
0 commit comments