private void createFromDir(string path) { fireDebug("Creating U8 File from: {0}", path); if (path[path.Length - 1] != Path.DirectorySeparatorChar) { path += Path.DirectorySeparatorChar; } fireDebug(" Collecting Content..."); string[] dirEntries = getDirContent(path, true); int offsetToName = 1; int offsetToData = 0; fireDebug(" Creating U8 Header..."); u8Header = new U8_Header(); rootNode = new U8_Node(); u8Nodes = new List <U8_Node>(); stringTable = new List <string>(); data = new List <byte[]>(); //Create Rootnode fireDebug(" Creating Rootnode..."); rootNode.Type = U8_NodeType.Directory; rootNode.OffsetToName = 0; rootNode.OffsetToData = 0; rootNode.SizeOfData = (uint)dirEntries.Length + 1; //Create Nodes for (int i = 0; i < dirEntries.Length; i++) { fireDebug(" Creating Node #{0} of {1}", i + 1, dirEntries.Length); fireProgress((i + 1) * 100 / dirEntries.Length); U8_Node tempNode = new U8_Node(); byte[] tempData = new byte[0]; string tempDirEntry = dirEntries[i].Remove(0, path.Length - 1); if (Directory.Exists(dirEntries[i])) //It's a dir { fireDebug(" -> Directory"); tempNode.Type = U8_NodeType.Directory; tempNode.OffsetToData = (uint)Shared.CountCharsInString(tempDirEntry, Path.DirectorySeparatorChar); //Recursion int size = u8Nodes.Count + 2; for (int j = 0; j < dirEntries.Length; j++) { if (dirEntries[j].Contains(dirEntries[i] + System.IO.Path.DirectorySeparatorChar.ToString())) { size++; } } tempNode.SizeOfData = (uint)size; } else //It's a file { fireDebug(" -> File"); fireDebug(" -> Reading File Data..."); tempData = File.ReadAllBytes(dirEntries[i]); tempNode.Type = U8_NodeType.File; tempNode.OffsetToData = (uint)offsetToData; tempNode.SizeOfData = (uint)tempData.Length; offsetToData += Shared.AddPadding(offsetToData + tempData.Length, dataPadding); } tempNode.OffsetToName = (ushort)offsetToName; offsetToName += (Path.GetFileName(dirEntries[i])).Length + 1; fireDebug(" -> Reading Name..."); string tempName = Path.GetFileName(dirEntries[i]); if (tempName.ToLower() == "icon.bin") { iconSize = getRealSize(tempData); } else if (tempName.ToLower() == "banner.bin") { bannerSize = getRealSize(tempData); } else if (tempName.ToLower() == "sound.bin") { soundSize = getRealSize(tempData); } u8Nodes.Add(tempNode); stringTable.Add(tempName); data.Add(tempData); } //Update U8 Header fireDebug(" Updating U8 Header..."); u8Header.HeaderSize = (uint)(((u8Nodes.Count + 1) * 12) + offsetToName); u8Header.OffsetToData = (uint)Shared.AddPadding((int)u8Header.OffsetToRootNode + (int)u8Header.HeaderSize, dataPadding); //Update dataOffsets fireDebug(" Calculating Data Offsets..."); for (int i = 0; i < u8Nodes.Count; i++) { fireDebug(" -> Node #{0} of {1}...", i + 1, u8Nodes.Count); int tempOffset = (int)u8Nodes[i].OffsetToData; u8Nodes[i].OffsetToData = (uint)(u8Header.OffsetToData + tempOffset); } fireDebug("Creating U8 File Finished..."); }
private void addEntry(string nodePath, byte[] fileData) { //Parse path if (nodePath.StartsWith("/")) { nodePath = nodePath.Remove(0, 1); } string[] path = nodePath.Split('/'); //Last entry is the filename int nodeIndex = -1; int maxIndex = (u8Nodes.Count > 0) ? u8Nodes.Count - 1 : 0; int currentIndex = 0; List <int> pathIndices = new List <int>(); for (int i = 0; i < path.Length - 1; i++) { for (int j = currentIndex; j <= maxIndex; j++) { if (stringTable[j].ToLower() == path[i].ToLower()) { if (i == path.Length - 2) { nodeIndex = j; } maxIndex = (int)u8Nodes[j].SizeOfData - 1; currentIndex = j + 1; pathIndices.Add(j); break; } if (j == maxIndex - 1) { throw new Exception("Path wasn't found!"); } } } //Get last entry in current dir int lastEntry; if (nodeIndex > -1) { lastEntry = (int)u8Nodes[nodeIndex].SizeOfData - 2; } else { lastEntry = (rootNode.SizeOfData > 1) ? (int)rootNode.SizeOfData - 2 : -1; } //Create and insert node + data U8_Node tempNode = new U8_Node(); tempNode.Type = (fileData.Length == 0) ? U8_NodeType.Directory : U8_NodeType.File; tempNode.SizeOfData = (uint)((fileData.Length == 0) ? lastEntry + 2 : fileData.Length); tempNode.OffsetToData = (uint)((fileData.Length == 0) ? Shared.CountCharsInString(nodePath, '/') : 0); stringTable.Insert(lastEntry + 1, path[path.Length - 1]); u8Nodes.Insert(lastEntry + 1, tempNode); data.Insert(lastEntry + 1, fileData); //Update rootnode and path sizes (+1) rootNode.SizeOfData += 1; foreach (int index in pathIndices) { if (u8Nodes[index].Type == U8_NodeType.Directory) { u8Nodes[index].SizeOfData += 1; } } for (int i = lastEntry + 1; i < u8Nodes.Count; i++) { if (u8Nodes[i].Type == U8_NodeType.Directory) { u8Nodes[i].SizeOfData += 1; } } }