/// <summary> /// 查找指定资源包中的指定资源文件并返回为Byte[] /// </summary> /// /// <param name="fileName"></param> /// <param name="resName"></param> /// <returns></returns> public static byte[] OpenResource(string fileName, string resName) { Stream ins0 = null; DataInputStream dis = null; try { ins0 = Resources.OpenStream(fileName); dis = new DataInputStream(ins0); LPKHeader header = ReadHeader(dis); LPKTable[] fileTable = ReadLPKTable(dis, (int)header.GetTables()); bool find = false; int fileIndex = 0; string innerName = null; for (int i = 0; i < fileTable.Length; i++) { innerName = StringUtils.NewString(fileTable[i].GetFileName()).Trim(); if (innerName.Equals(resName, StringComparison.InvariantCultureIgnoreCase)) { find = true; fileIndex = i; break; } } if (find == false) { throw new Exception("File not found. ( " + fileName + " )"); } else { return(ReadFileFromPak(dis, header, fileTable[fileIndex])); } } catch (Exception ex) { Log.Exception(ex); throw new Exception("File not found. ( " + fileName + " )"); } finally { if (dis != null) { try { dis.Close(); dis = null; } catch (IOException ex) { Log.Exception(ex); } } } }
public static LPKHeader ReadHeader(ArrayByte dis) { LPKHeader header = new LPKHeader(); header.SetPAKIdentity(dis.ReadInt()); byte[] pass = dis.ReadByteArray(LPKHeader.LF_PASSWORD_LENGTH); header.SetPassword(pass); header.SetVersion(dis.ReadFloat()); header.SetTables(dis.ReadLong()); return(header); }
/// <summary> /// 返回LPK文件信息 /// </summary> /// /// <param name="pakFilePath"></param> /// <returns></returns> /// <exception cref="System.Exception"></exception> public static IList <object> GetLPKInfo(string resName) { Stream ins0 = Resources.OpenStream(resName); DataInputStream dis = new DataInputStream(ins0); LPKHeader header = ReadHeader(dis); LPKTable[] fileTable = ReadLPKTable(dis, (int)header.GetTables()); List <object> result = new List <object>(); CollectionUtils.Add(result, header); CollectionUtils.Add(result, fileTable); return(result); }
/// <summary> /// 读取数据流 /// </summary> /// /// <param name="dis"></param> /// <param name="header"></param> /// <param name="fileTable"></param> /// <returns></returns> /// <exception cref="System.Exception"></exception> public static byte[] ReadFileFromPak(DataInputStream dis, LPKHeader header, LPKTable fileTable) { dis.Skip(fileTable.GetOffSet() - OutputOffset(header)); int fileLength = (int)fileTable.GetFileSize(); byte[] fileBuff = new byte[fileLength]; int readLength = dis.Read(fileBuff, 0, fileLength); if (readLength < fileLength) { return(null); } else { MakeBuffer(fileBuff, readLength); return(fileBuff); } }
public static byte[] OpenResource(string fileName, string resName) { try { PAK pak = (PAK)CollectionUtils.Get(pakRes, fileName); Stream ins = Resources.OpenStream(fileName); ArrayByte result = null; if (CACHE) { if (cacheRes == null) { cacheRes = new System.Collections.Generic.Dictionary <string, ArrayByte>( CollectionUtils.INITIAL_CAPACITY); } result = (ArrayByte)CollectionUtils.Get(cacheRes, fileName); if (result == null) { result = new ArrayByte(ins, ArrayByte.LITTLE_ENDIAN); CollectionUtils.Put(cacheRes, fileName, result); } else { result.Reset(ArrayByte.LITTLE_ENDIAN); } } else { result = new ArrayByte(ins, ArrayByte.LITTLE_ENDIAN); } if (pak == null) { pak = new PAK(); LPKHeader header = ReadHeader(result); pak.tables = ReadLPKTable(result, (int)header.GetTables()); pak.head_size = (int)(LPKHeader.Size() + header.GetTables() * LPKTable.Size()); pak.skip = result.Position(); pak.length = result.Length(); CollectionUtils.Put(pakRes, fileName, pak); } else { result.SetPosition(pak.skip); } bool find = false; int fileIndex = 0; string innerName = null; LPKTable[] tables_0 = pak.tables; int size = tables_0.Length; for (int i = 0; i < size; i++) { innerName = tables_0[i].GetFileName(); if (resName.Equals(innerName, System.StringComparison.InvariantCultureIgnoreCase)) { find = true; fileIndex = i; break; } } if (!find) { throw new Exception("File not found. ( " + fileName + " )"); } else { return(ReadFileFromPak(result, pak.head_size, tables_0[fileIndex])); } } catch (Exception) { throw new Exception("File not found. ( " + fileName + " )"); } }
public static LPKHeader ReadHeader(ArrayByte dis) { LPKHeader header = new LPKHeader(); header.SetPAKIdentity(dis.ReadInt()); byte[] pass = dis.ReadByteArray(LPKHeader.LF_PASSWORD_LENGTH); header.SetPassword(pass); header.SetVersion(dis.ReadFloat()); header.SetTables(dis.ReadLong()); return header; }
/// <summary> /// 获得指定头文件的偏移长度 /// </summary> /// /// <param name="header"></param> /// <returns></returns> public static long OutputOffset(LPKHeader header) { return(LPKHeader.Size() + header.GetTables() * LPKTable.Size()); }