private void Load(string path) { if (!Exists(path)) { throw new FileNotFoundException($"File:{path} can not be found."); } //initialization FolderStack = new Stack <JMDPackedFolderInfo>(); PathStack = new Stack <string>(); ModifiedFileInfos = new List <ModifiedFileInfo>(); // Path = path; FileInfo fi = new FileInfo(path); FileStream fs = new FileStream(path, FileMode.Open); BinaryReader br = new BinaryReader(fs); string FileName = fi.Extension != "" ? fi.Name.Replace(fi.Extension, "") : fi.Name; HeaderKey = KeyGenerator.GetHeaderKey(FileName); byte[] block1 = br.ReadBytes(0x80); if (!CheckFile(block1)) { throw new Exception($"File:{path} is not a correct JMDFile,check if this file is changed."); } byte[] block2 = br.ReadBytes(0x80); Header = new JMDHeader(block2, HeaderKey); if (!Header.IsCorrectJMDFile) { HeaderKey = (BitConverter.ToUInt32(block2, 4) ^ 0x100) + 0x7b8c043f ^ 0x8473fbc1; Header = new JMDHeader(block2, HeaderKey); FixedMode = true; } uint Block3Count = Header.StreamInfoCount; StreamInfos = new List <JMDStreamInfo>(); for (int i = 0; i < Block3Count; i++) { byte[] bk3 = br.ReadBytes(0x20); StreamInfos.Add(new JMDStreamInfo(bk3, Header.StreamInfosKey)); } JMDStreamInfo bk4Info = GetStreamInfo(0xFFFFFFFF); fs.Seek(bk4Info.Offset, SeekOrigin.Begin); byte[] bk4 = new byte[bk4Info.Size]; fs.Read(bk4, 0, bk4.Length); NowFolderContent = JMDPackedFilesInfoDecoder.GetJMDPackedFileInfos(bk4, HeaderKey, 0xFFFFFFFF); this.FolderStack.Push(new JMDPackedFolderInfo() { FolderName = "", Index = 0xFFFFFFFF, ParentIndex = 0 }); PathStack.Push(""); fs.Close(); }
public bool BackToParentFolder() { if (NowFolder.Index == 0xFFFFFFFF) { return(false); } JMDPackedFolderInfo nowFolder = this.FolderStack.Pop(); this.NowFolderContent = JMDPackedFilesInfoDecoder.GetJMDPackedFileInfos(GetStreamData(nowFolder.ParentIndex), this.HeaderKey, nowFolder.ParentIndex); this.PathStack.Pop(); return(true); }
public bool EnterToFolder(string FolderName) { JMDPackedFolderInfo FolderInfo = (JMDPackedFolderInfo)Array.Find(NowFolderContent, x => x.Type == ObjectType.Folder && ((JMDPackedFolderInfo)x).FolderName == FolderName); if (FolderName is null) { return(false); } this.ParentFolder = this.NowFolder; this.NowFolderContent = JMDPackedFilesInfoDecoder.GetJMDPackedFileInfos(GetStreamData(FolderInfo.Index), this.HeaderKey, this.NowFolder.Index); this.FolderStack.Push(FolderInfo); this.PathStack.Push(FolderInfo.FolderName); return(true); }
//AutoClose!!! public void ApplyModification(string File) { string tempS = ""; bool tempMode = false; if (Path == File) { tempS = File; string newPath = $"{Path}.temp{DateTime.Now.ToString("yyyyMMddhhmmssffff")}"; File = newPath; tempMode = true; } FileStream fs = new FileStream(File, FileMode.Create); FileStream jmdFileOrg = new FileStream(Path, FileMode.Open); byte[] headerString = Encoding.GetEncoding("UTF-16").GetBytes(HeaderString); byte[] DescriptString = Encoding.GetEncoding("UTF-16").GetBytes(JMDFile.DescriptString); //offset:0x00 size:0x80 fs.Seek(0, SeekOrigin.Begin); fs.Write(headerString, 0, headerString.Length); fs.Seek(0x40, SeekOrigin.Begin); fs.Write(DescriptString, 0, DescriptString.Length); fs.Seek(0x80, SeekOrigin.Begin); //FileDataWriter byte[] data2; int offset = GetNextOffset(0, 0x100 + (0x20 * StreamInfos.Count)); int firstOffset = offset; using (MemoryStream ms = new MemoryStream()) { for (int i = 0; i < StreamInfos.Count; i++) { ms.Seek(offset - firstOffset, SeekOrigin.Begin); JMDStreamInfo streamInfo = StreamInfos[i]; int tempOffset = offset; switch (streamInfo.StreamMode) { case StreamMode.JMDFile: if (IsChangedFolder(streamInfo.Index)) { IPackedObject[] objects = JMDPackedFilesInfoDecoder.GetJMDPackedFileInfos(GetStreamData(streamInfo.Index, jmdFileOrg), this.HeaderKey, this.NowFolder.Index); List <ModifiedFileInfo> needModify = ModifiedFileInfos.FindAll(x => x.FolderIndex == streamInfo.Index); for (int a = 0; a < objects.Length; a++) { int indexAAA = -1; //20210425현재 캐스팅오류 임시해결 if (indexAAA != -1) { JMDPackedFileInfo objT = objects[a] as JMDPackedFileInfo; objT.FileSize = (int)needModify[indexAAA].NewFileSize; } } uint hash = 0; byte[] newD = JMDPackedFilesInfoDecoder.ToByteArray(objects, HeaderKey, out hash); ms.Write(newD, 0, newD.Length); StreamInfos[i].Size = (uint)newD.Length; StreamInfos[i].Size2 = (uint)newD.Length; StreamInfos[i].Hash = hash; offset = GetNextOffset(offset, (int)newD.Length); } else { jmdFileOrg.Seek(streamInfo.Offset, SeekOrigin.Begin); byte[] dataT = new byte[streamInfo.Size]; jmdFileOrg.Read(dataT, 0, dataT.Length); ms.Write(dataT, 0, dataT.Length); offset = GetNextOffset(offset, (int)dataT.Length); } break; case StreamMode.Local: LocalStreamAddition lsa = streamInfo.StreamAddition as LocalStreamAddition; FileStream localFile = new FileStream(lsa.FileName, FileMode.Open); localFile.Seek(lsa.Offset, SeekOrigin.Begin); byte[] dataT2 = new byte[lsa.Length]; localFile.Read(dataT2, 0, dataT2.Length); localFile.Close(); if (streamInfo.NeedCrypt) { dataT2 = JMDCrypt.Decrypt(dataT2, lsa.Key); } ms.Write(dataT2, 0, dataT2.Length); offset = GetNextOffset(offset, (int)dataT2.Length); break; } StreamInfos[i].Offset = (uint)tempOffset; } data2 = ms.ToArray(); } //offset:0x80 size:0x80 byte[] b2 = Header.ToByteArray(HeaderKey); fs.Write(b2, 0, b2.Length); //offset:0x100 size:? foreach (JMDStreamInfo jsi in StreamInfos) { byte[] atrq = jsi.ToByteArray(Header.StreamInfosKey); fs.Write(atrq, 0, atrq.Length); } // fs.Seek(firstOffset, SeekOrigin.Begin); fs.Write(data2, 0, data2.Length); byte[] zzzzz = new byte[offset - fs.Position]; fs.Write(zzzzz, 0, zzzzz.Length); fs.Close(); jmdFileOrg.Close(); //Reload if (tempMode) { Delete(Path); FileInfo fi = new FileInfo(File); fi.MoveTo(tempS); File = tempS; } Load(File); }
public IPackedObject[] GetInFolderObject(JMDPackedFolderInfo info) { return(JMDPackedFilesInfoDecoder.GetJMDPackedFileInfos(GetStreamData(info.Index), HeaderKey, info.Index)); }