public bool GetRecordBlocksInfo() { try { var mdictStream = new StreamReader(Filename); mdictStream.BaseStream.Position = RecordBlockInfoStartPosition; RecordBlockInfoList = new List <MdictHelper.Tuple <long, long> >(); int numRecordBlocks = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); int numEntries = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); int recordBlockInfoSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); int recordBlockSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); for (int ml = 0; ml < numRecordBlocks; ml++) { long compressedSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); long decompressedSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); RecordBlockInfoList.Add(new MdictHelper.Tuple <long, long>(compressedSize, decompressedSize)); } RecordListStartPosition = mdictStream.BaseStream.Position; mdictStream.Close(); return(true); } catch { return(false); } }
public bool IgnoreKeys() { try { var mdictStream = new StreamReader(Filename); mdictStream.BaseStream.Position = KeyListStartPosition; int keyBlockInfoCount = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); int keyBlockInfoEntryCount = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); if (float.Parse(Headers["GeneratedByEngineVersion"]) >= 2.0) { MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); } int keyBlockInfoSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); int keyBlockSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); if (float.Parse(Headers["GeneratedByEngineVersion"]) >= 2.0) { mdictStream.BaseStream.Read(new byte[4], 0, 4); } mdictStream.BaseStream.Position += keyBlockInfoSize; mdictStream.BaseStream.Position += keyBlockSize; RecordBlockInfoStartPosition = mdictStream.BaseStream.Position; mdictStream.Close(); return(true); } catch { return(false); } }
public bool GetKeys() { try { var mdictStream = new StreamReader(Filename); mdictStream.BaseStream.Position = KeyListStartPosition; //Get KeyBlockInfoList var keyBlockInfoList = new List <MdictHelper.Tuple <long, long> >(); int keyBlockInfoCount = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); int keyBlockInfoEntryCount = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); if (float.Parse(Headers["GeneratedByEngineVersion"]) >= 2.0) { MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); } int keyBlockInfoSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); int keyBlockSize = MdictHelper.ReadNumber(mdictStream.BaseStream, NumberWidth); if (float.Parse(Headers["GeneratedByEngineVersion"]) >= 2.0) { mdictStream.BaseStream.Read(new byte[4], 0, 4); } var keyBlockInfoBuffer = new byte[keyBlockInfoSize]; mdictStream.BaseStream.Read(keyBlockInfoBuffer, 0, keyBlockInfoBuffer.Length); try { var keyBlockInfoText = BitConverter.ToString(keyBlockInfoBuffer); if (float.Parse(Headers["GeneratedByEngineVersion"]) >= 2.0) { keyBlockInfoText = MdictHelper.UnZipZlibHexString(keyBlockInfoText.Replace("-", "").Substring(NumberWidth * 2)) .Replace("-", ""); } int i = 0, byteWidth = 2, textTerm = 1; if (float.Parse(Headers["GeneratedByEngineVersion"]) < 2.0) { byteWidth = 1; textTerm = 0; } while (i < keyBlockInfoText.Length) { i += NumberWidth * 2; int textHeadSize = int.Parse(keyBlockInfoText.Substring(i, byteWidth * 2), NumberStyles.HexNumber); i += byteWidth * 2; if (Headers["Encoding"] != "UTF-16") { i += (textHeadSize * 2) + (textTerm * 2); } else { i += ((textHeadSize * 2) + (textTerm * 2)) * 2; } int textTailSize = int.Parse(keyBlockInfoText.Substring(i, byteWidth * 2), NumberStyles.HexNumber); i += byteWidth * 2; if (Headers["Encoding"] != "UTF-16") { i += (textTailSize * 2) + (textTerm * 2); } else { i += ((textTailSize * 2) + (textTerm * 2)) * 2; } int keyBlockCompressedSize = int.Parse(keyBlockInfoText.Substring(i, NumberWidth * 2), NumberStyles.HexNumber); i += NumberWidth * 2; int keyBlockDecompressedSize = int.Parse(keyBlockInfoText.Substring(i, NumberWidth * 2), NumberStyles.HexNumber); i += NumberWidth * 2; keyBlockInfoList.Add(new MdictHelper.Tuple <long, long>(keyBlockCompressedSize, keyBlockDecompressedSize)); } } catch { } //Get KeyList var keyBlockBuffer = new byte[keyBlockSize]; mdictStream.BaseStream.Read(keyBlockBuffer, 0, keyBlockBuffer.Length); string keyBlockText = BitConverter.ToString(keyBlockBuffer).Replace("-", ""); //KeyBlockInfo not exist IdxBlockInfoList = new List <MdictHelper.Idx>(); if (keyBlockInfoList.Count == 0) { var keyBlockCompressed = new string[1]; switch (keyBlockText.Substring(0, 8)) { case "00000000": keyBlockCompressed = new[] { keyBlockText }; break; case "01000000": break; case "02000000": var stringSeparators = new[] { "02000000" }; keyBlockCompressed = keyBlockText.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries); break; } int i = 0; foreach (var kb in keyBlockCompressed) { string keyBlock = MdictHelper.UnZipZlibHexString(kb.Substring(8)).Replace("-", ""); var _kbs = MdictHelper.SplitUncompressesBlock(keyBlock, Headers["Encoding"]); var idx = new MdictHelper.Idx() { Id = i++, FirstEntry = _kbs.Min(p => p.Value2), LastEntry = _kbs.Max(p => p.Value2) }; foreach (var _kb in _kbs) { idx.Entries.Add(new MdictHelper.Tuple <long, long, string>(_kb.Value1, -1, _kb.Value2)); } IdxBlockInfoList.Add(idx); } } else { int l = 0, i = 0; foreach (var kbi in keyBlockInfoList) { int st = l; int ed = l + ((int)kbi.Value1 * 2); switch (keyBlockText.Substring(0, 8)) { case "00000000": var _qf = MdictHelper.SplitUncompressesBlock(keyBlockText.Substring(st + (NumberWidth * 2), ed - (st + (NumberWidth * 2))), Headers["Encoding"]); var idx = new MdictHelper.Idx() { Id = i++, FirstEntry = _qf.Min(p => p.Value2), LastEntry = _qf.Max(p => p.Value2) }; foreach (var kj in _qf) { idx.Entries.Add(new MdictHelper.Tuple <long, long, string>(kj.Value1, -1, kj.Value2)); } IdxBlockInfoList.Add(idx); break; case "01000000": break; case "02000000": string keyBlock = MdictHelper.UnZipZlibHexString(keyBlockText.Substring(st + (NumberWidth * 2), ed - (st + (NumberWidth * 2)))).Replace("-", ""); var _qg = MdictHelper.SplitUncompressesBlock(keyBlock, Headers["Encoding"]); var idx_ = new MdictHelper.Idx() { Id = i++, FirstEntry = _qg.Min(p => p.Value2), LastEntry = _qg.Max(p => p.Value2) }; foreach (var kj in _qg) { idx_.Entries.Add(new MdictHelper.Tuple <long, long, string>(kj.Value1, -1, kj.Value2)); } IdxBlockInfoList.Add(idx_); break; } l += ((int)kbi.Value1 * 2); } } long lastPosition = -1; for (int i = IdxBlockInfoList.Count - 1; i >= 0; i--) { IdxBlockInfoList[i].Entries.Last().Value2 = lastPosition != -1 ? lastPosition - IdxBlockInfoList[i].Entries.Last().Value1 : -1; for (int j = IdxBlockInfoList[i].Entries.Count - 1 - 1; j >= 0; j--) { IdxBlockInfoList[i].Entries[j].Value2 = IdxBlockInfoList[i].Entries[j + 1].Value1 - IdxBlockInfoList[i].Entries[j].Value1; } lastPosition = IdxBlockInfoList[i].Entries.First().Value1; } RecordBlockInfoStartPosition = mdictStream.BaseStream.Position; mdictStream.Close(); return(true); } catch { return(false); } }