Skip to content

Instantly share code, notes, and snippets.

@0x61726b
Created August 22, 2018 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x61726b/145c46c3f8e2ea24287d877230481379 to your computer and use it in GitHub Desktop.
Save 0x61726b/145c46c3f8e2ea24287d877230481379 to your computer and use it in GitHub Desktop.
bundle updater
void Awake()
{
StartCoroutine(DictionaryUpdater());
}
IEnumerator DictionaryUpdater()
{
string dictData = ObscuredPrefs.GetString("DictData");
var dictionaries = JsonConvert.DeserializeObject<List<DictionaryPlayerPrefModel>>(dictData);
// Download from scratch
if (dictionaries == null)
{
var dictList = new List<DictionaryPlayerPrefModel>();
// Download turkish
yield return StartCoroutine(DownloadDictionary("tr", (DictionaryPlayerPrefModel data) =>
{
dictList.Add(data);
}));
if (dictList.Count == 0)
{
Debug.Log("Update error");
// TODO handle error
}
else
{
ObscuredPrefs.SetString("DictData", JsonConvert.SerializeObject(dictList));
}
}
else
{
foreach (var dictionaryPlayerPrefModel in dictionaries)
{
int version = dictionaryPlayerPrefModel.Version;
string language = dictionaryPlayerPrefModel.Language;
UnityWebRequest request = UnityWebRequest.Get($"{baseUrl}WordDictionary/{language}");
request.downloadHandler = new DownloadHandlerBuffer();
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
yield break;
}
string response = request.downloadHandler.text;
var deserializedResponse = JsonConvert.DeserializeObject<LCAPIQueryDictionaryResponse>(response);
if (deserializedResponse != null)
{
// Compare versions
if (version == deserializedResponse.Version)
{
Debug.Log($"Dictionary {language} is up-to-date!");
}
else
{
Debug.Log($"Dictionary {language} needs to be updated! Version {deserializedResponse.Version} vs {version}");
yield return StartCoroutine(DownloadDictionary(language, (DictionaryPlayerPrefModel data) =>
{
dictionaryPlayerPrefModel.Version = data.Version;
}));
}
}
}
ObscuredPrefs.SetString("DictData", JsonConvert.SerializeObject(dictionaries));
}
}
IEnumerator DownloadDictionary(string language, DictionaryDownloadCompleteDelegate callback)
{
UnityWebRequest request = UnityWebRequest.Get($"{baseUrl}WordDictionary/{language}");
request.downloadHandler = new DownloadHandlerBuffer();
yield return request.SendWebRequest();
// To-do, try again
if (request.isNetworkError || request.isHttpError)
{
yield break;
}
string response = request.downloadHandler.text;
if (request.responseCode == 200)
{
var deserializedResponse = JsonConvert.DeserializeObject<LCAPIQueryDictionaryResponse>(response);
if (deserializedResponse != null)
{
string assetBundleUrl = deserializedResponse.Result.Url;
int bundleVersion = deserializedResponse.Version;
using (var www = WWW.LoadFromCacheOrDownload(assetBundleUrl, bundleVersion))
{
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log(www.error);
yield return null;
}
DictionaryPlayerPrefModel dictSave = new DictionaryPlayerPrefModel()
{
Language = "tr",
Version = bundleVersion
};
callback?.Invoke(dictSave);
}
}
}
yield return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment