Created
August 26, 2018 12:11
-
-
Save 0x61726b/1e52fd5d30cf152e2770093898315973 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UpdateManager : MonoSingleton<UpdateManager> | |
{ | |
private string baseUrl = "http://localhost:9876/api/v1/"; | |
private List<LCAPIAssetListResult> remoteAssetList = new List<LCAPIAssetListResult>(); | |
void Awake() | |
{ | |
StartCoroutine(GetAssetListFromServer()); | |
// AssetBundle pipeline: | |
// Put some asset in DynamicAssets folder | |
// Make it an asset bundle in editor | |
// Build asset bundle | |
// Put the asset bundle in StreamingAssets folder | |
} | |
IEnumerator StartUpdating() | |
{ | |
Debug.Log("Start updating..."); | |
string assetData = ObscuredPrefs.GetString("AssetData"); | |
var localAssetList = JsonConvert.DeserializeObject<List<AssetPlayerPrefModel>>(assetData); | |
if (localAssetList == null || (localAssetList.Count == 0)) | |
{ | |
localAssetList = new List<AssetPlayerPrefModel>(); | |
} | |
// First, load all dynamic assets that are included in the project | |
var streamingAssetsDir = new DirectoryInfo(Application.streamingAssetsPath); | |
foreach (var fileInfo in streamingAssetsDir.GetFiles()) | |
{ | |
Debug.Log($"Found file in streaming assets: {fileInfo.Name}{fileInfo.Extension}"); | |
if(fileInfo.Extension == ".meta") | |
continue; | |
// Get local version of this file | |
var localAsset = localAssetList.Find(x => x.Name == fileInfo.Name); | |
if (localAsset == null) | |
{ | |
Debug.LogWarning($"Asset {fileInfo.Name} was not found in player prefs."); | |
// When its not found, add it | |
// Assume its version 0 | |
localAsset = new AssetPlayerPrefModel() { Name = fileInfo.Name, Uri = $"{baseUrl}AssetDownload/{fileInfo.Name}", Version = 0 }; | |
localAssetList.Add(localAsset); | |
} | |
// Get remote version of this file | |
var remoteAsset = remoteAssetList.Find(x => x.Name == fileInfo.Name); | |
if (remoteAsset == null) | |
{ | |
Debug.LogError($"Asset {fileInfo.Name} was not found on remote server."); | |
continue; | |
} | |
// Compare versions with local/remote | |
if (localAsset.Version != remoteAsset.Version) | |
{ | |
Debug.Log($"Version mismatch on asset {localAsset.Name}. Local: {localAsset.Version} Remote: {remoteAsset.Version}"); | |
using (var www = WWW.LoadFromCacheOrDownload(localAsset.Uri, remoteAsset.Version)) | |
{ | |
yield return www; | |
if (!string.IsNullOrEmpty(www.error)) | |
{ | |
Debug.Log($"Error while downloading/caching asset {localAsset.Name}. Error: {www.error}"); | |
yield return null; | |
} | |
Debug.Log($"Asset {localAsset.Name}(ver {remoteAsset.Version}) successfully updated and cached."); | |
localAsset.Version = remoteAsset.Version; | |
} | |
} | |
else | |
{ | |
Debug.Log($"Asset {localAsset.Name} is up-to-date!"); | |
} | |
} | |
Debug.Log("Saving asset data..."); | |
SaveAssets(localAssetList); | |
Debug.Log("Update complete!"); | |
AuthManager.Instance.OnAssetUpdateComplete(); | |
} | |
IEnumerator GetAssetListFromServer() | |
{ | |
Debug.Log("Get file list..."); | |
var request = UnityWebRequest.Get($"{baseUrl}Asset"); | |
request.downloadHandler = new DownloadHandlerBuffer(); | |
yield return request.SendWebRequest(); | |
if (request.isNetworkError || request.isHttpError) | |
{ | |
Debug.Log("Error requesting asset list from server!"); | |
yield break; | |
} | |
string result = request.downloadHandler.text; | |
Debug.Log(result); | |
var remoteAssets = JsonConvert.DeserializeObject<LCAPIAssetList>(result); | |
if (remoteAssets == null) | |
{ | |
Debug.LogError("Error retrieving asset list from server!"); | |
yield break; | |
} | |
Debug.Log($"Got remote assets: {remoteAssets.Result.Count} assets."); | |
this.remoteAssetList = remoteAssets.Result; | |
StartCoroutine(StartUpdating()); | |
} | |
void SaveAssets(List<AssetPlayerPrefModel> list) | |
{ | |
string serializedAssets = JsonConvert.SerializeObject(list); | |
ObscuredPrefs.SetString("AssetData", serializedAssets); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment