/// <summary> /// 遍历目录下的资源文件,生成索引文件 /// </summary> /// <param name="resDir">要遍历的目录</param> private static void GenerateIndexFile(string resDir) { string platName = resDir; if (platName[platName.Length - 1] == '/') { platName = platName.Substring(0, platName.Length - 1); } platName = platName.Substring(platName.LastIndexOf('/') + 1); DirectoryInfo dirInfo = new DirectoryInfo(resDir); var files = dirInfo.GetFiles("*", SearchOption.AllDirectories); List <BundleItem> items = new List <BundleItem>(); foreach (var file in files) { if (file.Extension != ResUtils.BundleExtension && file.Name != platName) { continue; //只处理资源关系文件和特定后缀的文件 } BundleItem item = new BundleItem(); item.m_HashCode = ResUtils.GetFileHash(file.FullName); item.m_FileSize = ResUtils.GetFileSize(file.FullName); item.m_Name = file.FullName.Substring(resDir.Length); items.Add(item); } IdxFile idx = new IdxFile(); string idxContent = IdxFile.SaveString(items, resDir); string filePath = resDir + ResUtils.BundleIndexFileName; File.WriteAllText(filePath, idxContent); Debug.Log("=========Generated index file to .." + filePath); }
/** * Constructor to load dictionary with given path. * @param url Path of one of stardict file or Path of folder contains stardict files * @deprecated using StarDict.loadDict(String url) */ public StarDict(string url) { if (!File.GetAttributes(url).HasFlag(FileAttributes.Directory)) { m_url = Path.Combine(Path.GetDirectoryName(url), Path.GetFileNameWithoutExtension(url)); m_ifoFile = new IfoFile(m_url + ext_info); m_idxFile = new IdxFile(m_url + ext_index, m_ifoFile.WordCount, m_ifoFile.IdxFileSize); m_dictFile = new DictFile(m_url + ext_dict); } else { string[] list = Directory.GetFiles(url); string infoPath = null; string indexPath = null; string dictPath = null; // Build table to mapping the file extension and file name for (int i = list.Length - 1; i >= 0; i--) { if (list[i].EndsWith(ext_info)) infoPath = list[i]; else if (list[i].EndsWith(ext_index)) indexPath = list[i]; else if (list[i].EndsWith(ext_dict)) dictPath = list[i]; } m_ifoFile = new IfoFile(Path.Combine(url, infoPath)); m_idxFile = new IdxFile(Path.Combine(url, indexPath), m_ifoFile.WordCount, m_ifoFile.IdxFileSize); m_dictFile = new DictFile(Path.Combine(url, dictPath)); } if (m_ifoFile.IsLoaded && m_idxFile.IsLoaded) m_available = true; }
/** * Constructor to load dictionary with given path. * @param url Path of one of stardict file or Path of folder contains stardict files * @deprecated using StarDict.loadDict(String url) */ public StarDict(string url) { if (!File.GetAttributes(url).HasFlag(FileAttributes.Directory)) { m_url = Path.Combine(Path.GetDirectoryName(url), Path.GetFileNameWithoutExtension(url)); m_ifoFile = new IfoFile(m_url + ext_info); m_idxFile = new IdxFile(m_url + ext_index, m_ifoFile.WordCount, m_ifoFile.IdxFileSize); m_dictFile = new DictFile(m_url + ext_dict); } else { string[] list = Directory.GetFiles(url); string infoPath = null; string indexPath = null; string dictPath = null; // Build table to mapping the file extension and file name for (int i = list.Length - 1; i >= 0; i--) { if (list[i].EndsWith(ext_info)) { infoPath = list[i]; } else if (list[i].EndsWith(ext_index)) { indexPath = list[i]; } else if (list[i].EndsWith(ext_dict)) { dictPath = list[i]; } } m_ifoFile = new IfoFile(Path.Combine(url, infoPath)); m_idxFile = new IdxFile(Path.Combine(url, indexPath), m_ifoFile.WordCount, m_ifoFile.IdxFileSize); m_dictFile = new DictFile(Path.Combine(url, dictPath)); } if (m_ifoFile.IsLoaded && m_idxFile.IsLoaded) { m_available = true; } }
/// <summary> /// 从服务器得到资源列表并对比出需要更新的包列表 /// </summary> /// <param name="ev">检查完成后回调函数</param> /// <returns></returns> IEnumerator AsyncCheckDownloadingList(ProcessCompleteEvent ev) { //读取本地的idx和apk里的idx文件 Dictionary <string, BundleItem> localBundlesDict = new Dictionary <string, BundleItem>(); string localIndexPath = ResUtils.BundleRootPath + ResUtils.BundleIndexFileName; if (!File.Exists(localIndexPath)) //如果P目录里没有索引文件,去Resources里拷贝一份到P目录 { UnityEngine.Debug.Log("local idx not found, try copy from default"); Directory.CreateDirectory(ResUtils.BundleRootPath); var txt = Resources.Load(ResUtils.BundleIndexFileName.Substring(ResUtils.BundleIndexFileName.IndexOf('.'))) as TextAsset; if (txt != null) { File.WriteAllText(ResUtils.BundleRootPath + ResUtils.BundleIndexFileName, txt.text); } } if (File.Exists(localIndexPath)) { string indexContent = File.ReadAllText(localIndexPath); if (indexContent != null) { IdxFile file = new IdxFile(); List <BundleItem> list = file.Load(indexContent); foreach (var v in list) { localBundlesDict[v.m_Name] = v; } } } else { UnityEngine.Debug.LogWarning("local idx not found"); } //下载网上的idx文件 WWW www = new WWW(mHttpAddress + ResUtils.GetBundleManifestName(Application.platform) + "/" + ResUtils.BundleIndexFileName); yield return(www); if (www.error != null) { UnityEngine.Debug.Log("remote idx read error " + www.error); } mDownloadingList.Clear(); if (www.error == null) { mNewIndexContent = www.text; IdxFile file = new IdxFile(); List <BundleItem> listServer = file.Load(mNewIndexContent); foreach (var v in listServer) { string localHash = null; string netHash = v.m_HashCode; BundleItem localItem = null; if (localBundlesDict.TryGetValue(v.m_Name, out localItem)) { localHash = localItem.m_HashCode; } if (localHash != netHash) { mDownloadingList.Add(v); //网上的资源较新则需要重新下载到本地 } } UnityEngine.Debug.LogFormat("download idx file success! new bundles count {0}, downloading {1}", listServer.Count, mDownloadingList.Count); } else { UnityEngine.Debug.LogFormat("download idx file error! {0}", www.error); } if (ev != null) { ev.Invoke(); } yield return(null); }