public void GetBundleCRCInformationFromServer() { Debug.Log("AssetBundleInformationManager: GetBundleCRCInformation"); try { var request = (HttpWebRequest)HttpWebRequest.Create(AssetBundleManager.Instance.BundleServerURL + "assetbundles.crc"); request.Timeout = 5000; //5 secs timeout Debug.Log("Request URI (CRC): " + request.RequestUri.AbsoluteUri); using (var response = (HttpWebResponse)request.GetResponse()) { m_realtimeToPollAgain += m_realtimeToPollAgain; // Take longer each time we call to prevent over polling. if ((int)response.StatusCode == 200) { Debug.Log("AssetBundleInformationManager: LoadCRCInformation Server"); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string strResponse = reader.ReadToEnd(); ParseCRCInformation(strResponse, m_serverAssetBundleCRCInformation); //SERVER CRC INFO: // Compare server CRC's with local CRC's for changes. foreach (var newServerCrc in m_serverAssetBundleCRCInformation) { foreach (AssetBundleCRCInfo localCrc in m_localAssetBundleCRCInformation) { if (localCrc.m_name == newServerCrc.m_name) { //this is an asset bundle we already know about from local crc info if (localCrc.m_fileSizeBytes != newServerCrc.m_fileSizeBytes || localCrc.m_CRC != newServerCrc.m_CRC) { Debug.Log("AssetBundleInformationManager::GetBundleCRCInformationFromServer - Detected a new version of the bundle from the server. Wiping the current file and downloading the new one."); //the server has a file that's different from the one we have here. Wipe any copy of this file, but don't count it as a failure to load AssetBundleLocalFileChecked.Invoke(this, new AssetBundleLocalFileCheckedEventArgs(localCrc.m_name, false, 0, newServerCrc.m_fileSizeBytes)); AssetBundleDownloadCorruptedOrBroken.Invoke(this, new AssetBundleDeleteAssetBundleFileEventArgs(localCrc.m_name, null)); AssetBundleRevokePermision.Invoke(this, new AssetBundleEventArgs(localCrc.m_name)); //no point keeping old crc info for a file we never had/couldn't load. Simply replace the crc info int index = m_localAssetBundleCRCInformation.IndexOf(localCrc); m_localAssetBundleCRCInformation[index] = newServerCrc; } //otherwise it didn't change, and we can simply continue goto localCrcFound; } } //this code is skipped if the file was found in the known assetbundle list //unknown asset bundle, add it to the list m_localAssetBundleCRCInformation.Add(newServerCrc); AssetBundleLocalFileChecked.Invoke(this, new AssetBundleLocalFileCheckedEventArgs(newServerCrc.m_name, false, 0, newServerCrc.m_fileSizeBytes)); AssetBundleStateChanged.Invoke(this, new AssetBundleStateChangedEventArgs(newServerCrc.m_name, AssetBundleDownloadState.WaitingManualPermission, null)); localCrcFound :; } WriteCRCInfoToDisk(); reader.Close(); HasLoadedServerCRCInfo = true; m_AssetBundleCRCInformationThread = null; } else { Debug.LogError("AssetBundleInformationManager: GetBundleCRCInformation Failed With Response: " + response.StatusCode); m_AssetBundleCRCInformationThread = null; } response.Close(); } } catch (WebException e) { if (AssetBundleManager.Instance.CurrentReachability == NetworkReachability.NotReachable) { Debug.LogWarning("No internet access, cannot get asset bundle data"); return; } m_realtimeToPollAgain += m_realtimeToPollAgain; Debug.LogException(e); Debug.LogError("AssetBundleInformationManager: GetBundleCRCInformationFromServer WebException " + e.ToString()); m_AssetBundleCRCInformationThread = null; } catch (ThreadAbortException) { m_realtimeToPollAgain += m_realtimeToPollAgain; Debug.LogWarning("AssetBundleInformationManager: GetBundleCRCInformationFromServer Thread aborted. Assuming intentionally."); } catch (Exception e) { m_realtimeToPollAgain += m_realtimeToPollAgain; Debug.LogException(e); Debug.LogError("AssetBundleInformationManager: GetBundleCRCInformationFromServer Exception " + e.ToString()); } }
public void CheckLocalAssetBundle(AssetBundleCRCInfo localCrc, int localCrcIndex, AssetBundleCRCInfo?serverCrc) { bool isInGame = AssetBundleManager.Instance.IsInGame; string fullFileName = AssetBundleManager.Instance.CurrentVersionDownloadLocation + "/" + localCrc.m_name; string tempFileName = fullFileName + ".incomplete"; FileInfo fullAssetFileInfo = new FileInfo(fullFileName); if (fullAssetFileInfo.Exists) { //check filesize first if (fullAssetFileInfo.Length == localCrc.m_fileSizeBytes) { if (isInGame) { //the file is the correct size, but CRC is slow, so we'll delay the CRC //don't do this now! return; } Debug.Log("AssetBundleInformationManager::CheckLocalAssetBundle : Completed file: " + localCrc.m_name); //sweet, same length, it's complete, now to check CRC uint crc = AssetBundleUtils.GenerateCRC32FromFile(fullFileName); if (crc == localCrc.m_CRC) { //it's the right file, untampered. //it's loadable. Debug.Log("AssetBundleInformationManager::CheckLocalAssetBundle : Local Asset Bundle " + localCrc.m_name + " is Loadable. SUCCESS"); AssetBundleLocalFileChecked.Invoke(this, new AssetBundleLocalFileCheckedEventArgs(localCrc.m_name, true, localCrc.m_fileSizeBytes, localCrc.m_fileSizeBytes)); AssetBundleStateChanged.Invoke(this, new AssetBundleStateChangedEventArgs(localCrc.m_name, AssetBundleDownloadState.Loadable, null)); AssetBundleRevokePermision.Invoke(this, new AssetBundleEventArgs(localCrc.m_name)); } else { //tampered? or damaged? Delete it. Debug.LogError("AssetBundleInformationManager::CheckLocalAssetBundle : Local Asset Bundle " + localCrc.m_name + " failed the CRC check. Deleting."); AssetBundleDownloadCorruptedOrBroken.Invoke(this, new AssetBundleDeleteAssetBundleFileEventArgs(localCrc.m_name, "FAILED_LOCAL_CRC_CHECK")); } } else { //it's not an incomplete dl, and it's either too big or too small, not sure how that could happen, wipe it and force a redownload //no point crcing as the bytes are definitely different. Debug.LogError("AssetBundleInformationManager: Local Asset Bundle " + localCrc.m_name + " failed the LENGTH check. How!? Deleting."); AssetBundleDownloadCorruptedOrBroken.Invoke(this, new AssetBundleDeleteAssetBundleFileEventArgs(localCrc.m_name, "FAILED_LOCAL_LENGTH_CHECK")); } } FileInfo tempAssetFileInfo = new FileInfo(tempFileName); if (tempAssetFileInfo.Exists) { Debug.Log("AssetBundleInformationManager::CheckLocalAssetBundle : Temporary File: " + localCrc.m_name); if (isInGame) { if (serverCrc.HasValue && tempAssetFileInfo.Length < serverCrc.Value.m_fileSizeBytes) { // We don't know what this file is, but we'll assume it's a partially downloaded NEW asset bundle and put it back in the queue Debug.Log("AssetBundleInformationManager::CheckLocalAssetBundle : Temporary Asset Bundle " + localCrc.m_name + " failed CRC. It's small enough to feasibly be a partially-downloaded new asset bundle. Download continuing."); AssetBundleLocalFileChecked.Invoke(this, new AssetBundleLocalFileCheckedEventArgs(serverCrc.Value.m_name, false, tempAssetFileInfo.Length, serverCrc.Value.m_fileSizeBytes)); AssetBundleStateChanged.Invoke(this, new AssetBundleStateChangedEventArgs(serverCrc.Value.m_name, AssetBundleDownloadState.Queued, null)); } return; } else { uint crc = AssetBundleUtils.GenerateCRC32FromFile(tempFileName); bool crcMatchesLocal = localCrc.m_CRC == crc; bool crcMatchesServer = serverCrc.HasValue && serverCrc.Value.m_CRC == crc; if (crcMatchesLocal || crcMatchesServer) { // Crc matches either! if (!crcMatchesLocal) { // The recently completed ".incomplete" file will now be moved to the final destination (oooh). // Thus, the local CRC info needs to match the new crc. m_localAssetBundleCRCInformation[localCrcIndex] = serverCrc.Value; } // The file is loadable, so move it to the right file name. Yay! Debug.Log("AssetBundleInformationManager::CheckLocalAssetBundle : Temporary Asset Bundle " + localCrc.m_name + " matched a CRC check and is now loadable. SUCCESS." + "\nServer: " + crcMatchesServer + ", Local: " + crcMatchesLocal); AssetBundleFinishedAndRequiresMoving.Invoke(this, new AssetBundleEventArgs(localCrc.m_name)); } else if (serverCrc.HasValue && tempAssetFileInfo.Length < serverCrc.Value.m_fileSizeBytes) { // We don't know what this file is, but we'll assume it's a partially downloaded NEW asset bundle. Debug.Log("AssetBundleInformationManager::CheckLocalAssetBundle : Temporary Asset Bundle " + localCrc.m_name + " failed CRC. It's small enough to feasibly be a partially-downloaded new asset bundle. Download continuing."); AssetBundleLocalFileChecked.Invoke(this, new AssetBundleLocalFileCheckedEventArgs(serverCrc.Value.m_name, false, tempAssetFileInfo.Length, serverCrc.Value.m_fileSizeBytes)); AssetBundleStateChanged.Invoke(this, new AssetBundleStateChangedEventArgs(serverCrc.Value.m_name, AssetBundleDownloadState.Queued, null)); } else if (HasLoadedServerCRCInfo) { // It failed all CRC AND we have got all server CRC's. Thus, this file must be corrupted. Debug.LogError("AssetBundleInformationManager::CheckLocalAssetBundle : Local Asset Bundle " + localCrc.m_name + " does not match any CRC, and we have all server CRC's. Assumed corrupted."); AssetBundleDownloadCorruptedOrBroken.Invoke(this, new AssetBundleDeleteAssetBundleFileEventArgs(localCrc.m_name, "FAILED_SERVER_CRC_CHECK")); } else { // We don't know if this is a new version, so no checks are worth doing on this until we have server info Debug.Log("AssetBundleInformationManager::CheckLocalAssetBundle : Local Asset Bundle " + localCrc.m_name + " does not match local CRC and we do not currently have the server CRC's. " + "It's probably a new bundle in a server CRC that we haven't downloaded for this play session yet. We'll queue it for download, pending the server CRC's."); AssetBundleLocalFileChecked.Invoke(this, new AssetBundleLocalFileCheckedEventArgs(localCrc.m_name, false, tempAssetFileInfo.Length, localCrc.m_fileSizeBytes)); AssetBundleStateChanged.Invoke(this, new AssetBundleStateChangedEventArgs(localCrc.m_name, AssetBundleDownloadState.WaitingManualPermission, null)); } } } // CRC checking complete. Phew. tempAssetFileInfo.Refresh(); fullAssetFileInfo.Refresh(); if (!tempAssetFileInfo.Exists && !fullAssetFileInfo.Exists) { // No file left after CRC checks, so the only thing to do is begin downloading afresh. AssetBundleLocalFileChecked.Invoke(this, new AssetBundleLocalFileCheckedEventArgs(localCrc.m_name, false, 0, localCrc.m_fileSizeBytes)); AssetBundleStateChanged.Invoke(this, new AssetBundleStateChangedEventArgs(localCrc.m_name, AssetBundleDownloadState.WaitingManualPermission, null)); } }