private void LoadWebFile(FileReader reader) { Logger.Info("Loading " + reader.FullPath); try { var webFile = new WebFile(reader); foreach (var file in webFile.fileList) { var dummyPath = Path.Combine(Path.GetDirectoryName(reader.FullPath), file.fileName); var subReader = new FileReader(dummyPath, file.stream); switch (subReader.FileType) { case FileType.AssetsFile: LoadAssetsFromMemory(subReader, reader.FullPath); break; case FileType.BundleFile: LoadBundleFile(subReader, reader.FullPath); break; case FileType.WebFile: LoadWebFile(subReader); break; case FileType.ResourceFile: resourceFileReaders[file.fileName] = subReader; //TODO break; } } } catch (Exception e) { Logger.Error($"Error while reading web file {reader.FullPath}", e); } finally { reader.Dispose(); } }
private void LoadZipFile(FileReader reader) { Logger.Info("Loading " + reader.FileName); try { using (ZipArchive archive = new ZipArchive(reader.BaseStream, ZipArchiveMode.Read)) { List <string> splitFiles = new List <string>(); // register all files before parsing the assets so that the external references can be found // and find split files foreach (ZipArchiveEntry entry in archive.Entries) { if (entry.Name.Contains(".split")) { string baseName = Path.GetFileNameWithoutExtension(entry.Name); string basePath = Path.Combine(Path.GetDirectoryName(entry.FullName), baseName); if (!splitFiles.Contains(basePath)) { splitFiles.Add(basePath); importFilesHash.Add(baseName); } } else { importFilesHash.Add(entry.Name); } } // merge split files and load the result foreach (string basePath in splitFiles) { try { Stream splitStream = new MemoryStream(); int i = 0; while (true) { string path = $"{basePath}.split{i++}"; ZipArchiveEntry entry = archive.GetEntry(path); if (entry == null) { break; } using (Stream entryStream = entry.Open()) { entryStream.CopyTo(splitStream); } } splitStream.Seek(0, SeekOrigin.Begin); FileReader entryReader = new FileReader(basePath, splitStream); LoadFile(entryReader); } catch (Exception e) { Logger.Error($"Error while reading zip split file {basePath}", e); } } // load all entries foreach (ZipArchiveEntry entry in archive.Entries) { try { string dummyPath = Path.Combine(Path.GetDirectoryName(reader.FullPath), reader.FileName, entry.FullName); // create a new stream // - to store the deflated stream in // - to keep the data for later extraction Stream streamReader = new MemoryStream(); using (Stream entryStream = entry.Open()) { entryStream.CopyTo(streamReader); } streamReader.Position = 0; FileReader entryReader = new FileReader(dummyPath, streamReader); LoadFile(entryReader); if (entryReader.FileType == FileType.ResourceFile) { entryReader.Position = 0; if (!resourceFileReaders.ContainsKey(entry.Name)) { resourceFileReaders.Add(entry.Name, entryReader); } } } catch (Exception e) { Logger.Error($"Error while reading zip entry {entry.FullName}", e); } } } } catch (Exception e) { Logger.Error($"Error while reading zip file {reader.FileName}", e); } finally { reader.Dispose(); } }
private void LoadBlkFile(FileReader reader, Guid?targetId = null) { if (targetId == null) { Logger.Info("Loading " + reader.FileName); } else { Logger.Info("Loading " + reader.FileName + " with target ID " + targetId.Value.ToString()); } try { var blkFile = new BlkFile(reader); bool targetFound = false; for (int i = 0; i < blkFile.Files.Count; i++) { //Console.WriteLine(blkFile.Files[i].ID); if (targetId.HasValue && targetId.Value != blkFile.Files[i].ID) { continue; } targetFound = true; // TODO: proper dummyPath var dummyPath = Path.Combine(Path.GetDirectoryName(reader.FullPath), string.Format("{0}_{1}", reader.FileName, blkFile.Files[i].ID.ToString())); var subReader = new FileReader(dummyPath, new MemoryStream(blkFile.Files[i].Data)); var asset = LoadAssetsFromMemory(subReader, dummyPath); if (asset == null) { //Logger.Error("what"); continue; } foreach (var sharedFile in asset.m_Externals) { var sharedFileName = sharedFile.fileName; var sharedFileNameWithID = string.Format("{0}_{1}", sharedFileName, sharedFile.cabId.ToString()); if (!sharedFileName.EndsWith(".blk")) { // this will directly load .blk files, so anything that isn't one is not supported Logger.Warning(String.Format("attempted to load non-blk shared file ({0})", sharedFileName)); continue; } if (!importFilesHash.Contains(sharedFileNameWithID)) { var sharedFilePath = Path.Combine(Path.GetDirectoryName(reader.FullPath), sharedFileName); if (!File.Exists(sharedFilePath)) { var findFiles = Directory.GetFiles(Path.GetDirectoryName(reader.FullPath), sharedFileName, SearchOption.AllDirectories); if (findFiles.Length > 0) { sharedFilePath = findFiles[0]; } } if (File.Exists(sharedFilePath)) { // TODO: proper integration with the loading bar LoadBlkFile(new FileReader(sharedFilePath), sharedFile.cabId); //importFiles.Add(sharedFilePath); importFilesHash.Add(sharedFileNameWithID); } } } } if (blkFile.Files.Count > 0 && !targetFound) { Logger.Warning("failed to find target mhy0"); } } catch (Exception e) { Logger.Error($"Error while reading blk file {reader.FileName}", e); } finally { reader.Dispose(); } }