static bool _CheckBundleFileConsistency(AssetBundleMasterFile info, bool calcBundleCRC, out string retError) { retError = null; string path = string.Format(@"{0}/{1}", AssetBundleConfig.m_LocalPath + @"Assets", info.m_fileName); if (!File.Exists(path)) { retError = Translate("patch file nothing"); return(false); } else { if (calcBundleCRC) { FileInfo fileInfo = new FileInfo(path); if (!fileInfo.Exists) { retError = Translate("patch file nothing"); return(false); } using (FileStream inStream = fileInfo.OpenRead()) { byte[] bytes = new byte[fileInfo.Length]; int currRead; int totalRead = 0; while ((currRead = inStream.Read(bytes, totalRead, (int)fileInfo.Length - totalRead)) > 0) { totalRead += currRead; } if (info.m_size != fileInfo.Length) { retError = Translate("patch file different size"); return(false); } if (calcBundleCRC == true && string.IsNullOrEmpty(info.m_hash) == true) { // 해쉬값이 없을 경우에만 예전처럼 CRC 값을 비교해서 사용한다. int crcCompute = CRC32.GetHash(bytes); if (info.m_crc != crcCompute) { retError = Translate("patch file hash different"); return(false); } } } } } return(true); }
static void _SaveBundleFile(AssetBundleMasterFile info, byte[] bytes) { Debug.Log("SaveBundleFile : " + info.GetBundleLocalSaveFilePath()); if (!Directory.Exists(AssetBundleConfig.m_LocalPath)) { Directory.CreateDirectory(AssetBundleConfig.m_LocalPath); } //if (File.Exists(info.GetBundleLocalSaveFilePath())) //{ // File.Delete(info.GetBundleLocalSaveFilePath()); //} using (MemoryStream inStream = new MemoryStream(bytes, 0, bytes.Length)) { inStream.Seek(0, SeekOrigin.Begin); using (Ionic.Zip.ZipFile zip = Ionic.Zip.ZipFile.Read(inStream)) { zip.Password = AssetBundleConfig.passward; zip.ExtractExistingFile = Ionic.Zip.ExtractExistingFileAction.OverwriteSilently; zip.ExtractAll(AssetBundleConfig.m_LocalPath + @"Assets", Ionic.Zip.ExtractExistingFileAction.OverwriteSilently); } } string retError; if (!_CheckBundleFileConsistency(info, false, out retError)) { string msg = "patch file save error :" + @"(" + retError + @")."; throw new Exception(msg); } info.m_downloadCompleted = true; Debug.Log("파일 저장됨 SaveFile : " + info.GetBundleLocalSaveFilePath()); }
IEnumerator _DownLoadBundle(AssetBundleMasterFile info, int index) { _curCoroutineCnt += 1; info.m_downloading = true; _currentProgress = 0; string url = info.GetBundleDownloadUrl(); Debug.Log("Download : " + url); _currentFile = url; UnityWebRequest http = CreateHttp(new Uri(url), (UnityWebRequest webReq) => { if (!string.IsNullOrEmpty(webReq.error)) { _curCoroutineCnt -= 1; info.m_downloading = false; int result = _OnNetworkError(webReq.error); if (result == 2) { webReq.Dispose(); return(true); } else { return(false); } } Debug.Log("Downloading : " + info.m_bundleName + " downloadProgress : " + webReq.downloadProgress); if (webReq.downloadHandler.isDone == false || webReq.downloadProgress < 1.0f) { return(false); } _SaveBundleFile(info, webReq.downloadHandler.data); Debug.Log("m_AssetBundleMgr - currentFile : " + _currentFile + " count : " + _savedBundleFileCnt + "/" + _downloadList.Count); if (_onDownloadUpdate != null) { _onDownloadUpdate(_currentFile, _downloadList.Count, _savedBundleFileCnt); } _currentProgress = 1; _continuousNetworkErrorCnt = 0; _errorDesc = null; _currentFile = string.Empty; _savedBundleFileCnt++; _curCoroutineCnt -= 1; if (_curCoroutineCnt < 0) { _curCoroutineCnt = 0; } if (_savedBundleFileCnt >= _downloadList.Count) { _ChangeState(State.End); } else { _StartDownloadBundleFile(); } webReq.Dispose(); return(true); }); http.SendWebRequest(); Debug.Log("http sendWebRequest : " + http + " url : " + url); yield return(null); }
public T Load <T>(string name) where T : UnityEngine.Object { if (m_AssetBundleManifest == null) { AssetBundleMasterFile manifestfile = m_AssetBundleMasterFileInfo.assetBundleMasterFileDic[@"assets/assetbundlemanifest"]; AssetBundle bundle = AssetBundle.LoadFromFile(manifestfile.GetLoadBundleFilePath()); m_AssetBundleManifest = bundle.LoadAsset(manifestfile.m_bundleName) as AssetBundleManifest; } Debug.Log("Bundle Load Name: " + name); string fullName = m_strResourcePath + name; string strLowerPathName = fullName.ToLower(); string assetbundleKey = strLowerPathName.Substring(0, fullName.LastIndexOf('/')); string bundleName = strLowerPathName.Replace(assetbundleKey + @"/", @""); AssetBundleMasterFile file = null; if (m_AssetBundleMasterFileInfo.assetBundleMasterFileDic.ContainsKey(strLowerPathName)) { file = m_AssetBundleMasterFileInfo.assetBundleMasterFileDic[strLowerPathName]; } else if (m_AssetBundleMasterFileInfo.assetBundleMasterFileDic.ContainsKey(assetbundleKey)) { file = m_AssetBundleMasterFileInfo.assetBundleMasterFileDic[assetbundleKey]; } else if (m_AssetBundleMasterFileInfo.assetBundleMasterFileDic.ContainsKey(string.Format(@"{0}/{1}", strLowerPathName, bundleName))) { file = m_AssetBundleMasterFileInfo.assetBundleMasterFileDic[string.Format(@"{0}/{1}", strLowerPathName, bundleName)]; } if (file == null) { return(null); } //First check if it is in the loaded bundle list if (!m_LoadAssetBundle.ContainsKey(file.m_fileName)) { //Load a bundle because it is not in the list of bundles AssetBundle bundle = AssetBundle.LoadFromFile(file.GetLoadBundleFilePath()); if (bundle != null) { // Add to list m_LoadAssetBundle.Add(file.m_fileName.ToLower(), bundle); if (file.m_bundleName == @"DB") { bundleName = bundleName + @".bytes"; } string[] temp = m_AssetBundleManifest.GetAllDependencies(file.m_fileName.ToLower()); //Load the relevant bundle. for (int i = 0; i < temp.Length; i++) { DependenciesBundleLoad(temp[i]); } T obj = bundle.LoadAsset <T>(bundleName.ToLower()); return(obj); } } else { //Use a bundle already loaded if (m_LoadAssetBundle[file.m_fileName.ToLower()] != null) { if (file.m_bundleName == @"DB") { bundleName = bundleName + @".bytes"; } //디펜던시 확인 string[] temp = m_AssetBundleManifest.GetAllDependencies(file.m_fileName.ToLower()); //Load related bundles for (int i = 0; i < temp.Length; i++) { DependenciesBundleLoad(temp[i]); } T obj = m_LoadAssetBundle[file.m_fileName.ToLower()].LoadAsset <T>(bundleName.ToLower()); return(obj); } } return(null); }