/// <summary> /// Imports a raw File to a RedEngine file (e.g. .dds to .xbm, .fbx to .mesh) /// </summary> /// <param name="rawRelative"></param> /// <param name="args"></param> /// <param name="outDir">can be a depotpath, or if null the parent directory of the rawfile</param> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> public bool Import(RedRelativePath rawRelative, GlobalImportArgs args, DirectoryInfo outDir = null) { #region checks if (rawRelative is null or { Exists : false })
private void ImportTaskInner(string path, string outDir, bool keep) { #region checks if (string.IsNullOrEmpty(path)) { _loggerService.Warning("Please fill in an input path."); return; } if (!string.IsNullOrEmpty(outDir) && !Directory.Exists(outDir)) { _loggerService.Warning("Please fill in a valid outdirectory path."); return; } var rawFile = new FileInfo(path); var inputDirInfo = new DirectoryInfo(path); if (!rawFile.Exists && !inputDirInfo.Exists) { _loggerService.Warning("Input path does not exist."); return; } var isDirectory = !rawFile.Exists; var basedir = rawFile.Exists ? new FileInfo(path).Directory : inputDirInfo; #endregion checks // create import settings var settings = new GlobalImportArgs().Register( _commonImportArgs.Value, _xbmImportArgs.Value, _gltfImportArgs.Value ); settings.Get <CommonImportArgs>().Keep = keep; settings.Get <XbmImportArgs>().Keep = keep; settings.Get <GltfImportArgs>().Keep = keep; var outDirectory = string.IsNullOrEmpty(outDir) ? null : new DirectoryInfo(outDir); // a directory was selected to import if (isDirectory) { _modTools.ImportFolder(basedir, settings, outDirectory); } // just a single file was selected else { var rawRelative = new RedRelativePath(rawFile.Directory, rawFile.GetRelativePath(rawFile.Directory)); // check if the file can be directly imported // if not, rebuild the single file if (!Enum.TryParse(rawRelative.Extension, true, out ERawFileFormat extAsEnum)) { // buffers can not be rebuilt on their own if (!settings.Get <CommonImportArgs>().Keep) { return; } // outdir needs to be the parent dir of the redfile to rebuild !! (user needs to take care of that) if (_modTools.RebuildBuffer(rawRelative, outDirectory)) { _loggerService.Success($"Successfully imported {path} to {outDirectory.FullName}"); } else { _loggerService.Error($"Failed to import {path}"); } } // the raw file can be imported directly else { if (_modTools.Import(rawRelative, settings, outDirectory)) { _loggerService.Success($"Successfully imported {path}"); } else { _loggerService.Error($"Failed to import {path}"); } } } }
public void Test_ImportExport(ECookedFileFormat extension) { var ext = $".{extension.ToString()}"; var infiles = s_groupedFiles[ext].ToList(); var modtools = ServiceLocator.Default.ResolveType <ModTools>(); var resultDir = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, s_testResultsDirectory, "temp")); if (!resultDir.Exists) { Directory.CreateDirectory(resultDir.FullName); } var logDir = new DirectoryInfo(Path.Combine(Environment.CurrentDirectory, s_testResultsDirectory, "logs")); if (!logDir.Exists) { Directory.CreateDirectory(logDir.FullName); } var isKeep = bool.Parse(s_config.GetSection(s_KEEP).Value); var isettings = new GlobalImportArgs().Register( new XbmImportArgs() { Keep = isKeep }, new MeshImportArgs() { Keep = isKeep }, new CommonImportArgs() { Keep = isKeep } ); var esettings = new GlobalExportArgs(); var uncookfails = new List <FileEntry>(); var importfails = new List <FileEntry>(); var equalfails = new List <FileEntry>(); // random tests var random = new Random(); var limit = Math.Min(int.Parse(s_config.GetSection(s_LIMIT).Value), infiles.Count); var filesToTest = infiles.OrderBy(a => random.Next()).Take(limit).ToList(); for (var i = 0; i < filesToTest.Count; i++) { var fileEntry = filesToTest[i]; // skip files without buffers var hasBuffers = (fileEntry.SegmentsEnd - fileEntry.SegmentsStart) > 1; if (!hasBuffers) { continue; } var ar = fileEntry.Archive as Archive; using var cr2wstream = new MemoryStream(); ar.CopyFileToStream(cr2wstream, fileEntry.NameHash64, false); var originalBytes = cr2wstream.ToByteArray(); // uncook var resUncook = modtools.UncookSingle(fileEntry.Archive as Archive, fileEntry.Key, resultDir, esettings, resultDir); if (!resUncook) { uncookfails.Add(fileEntry); Cleanup(); continue; } // rebuild var allfiles = resultDir.GetFiles("*", SearchOption.AllDirectories); var rawfile = allfiles.FirstOrDefault(_ => _.Extension != ext); if (rawfile == null) { Assert.Fail($"No raw file found in {resultDir}"); } var redrelative = new RedRelativePath(rawfile.Directory, rawfile.Name); var resImport = modtools.Import(redrelative, isettings); if (!resImport) { importfails.Add(fileEntry); Cleanup(); continue; } // compare var redfile = allfiles.FirstOrDefault(_ => _.Extension == ext); if (redfile == null) { Assert.Fail($"No red file found in {resultDir}"); } var newbytes = File.ReadAllBytes(redfile.FullName); if (!originalBytes.SequenceEqual(newbytes)) { equalfails.Add(fileEntry); var filename = Path.GetFileName(fileEntry.FileName); var dbgOriginalFile = Path.Combine(logDir.FullName, filename); var dbgNewFile = Path.Combine(logDir.FullName, $"{filename}.new"); File.WriteAllBytes(dbgOriginalFile, originalBytes); File.WriteAllBytes(dbgNewFile, newbytes); } else { } // clean temp dir Cleanup(); } if (uncookfails.Count > 0) { foreach (var fileEntry in uncookfails) { Console.WriteLine($"Failed to uncook - {fileEntry.FileName}"); } } if (importfails.Count > 0) { foreach (var fileEntry in importfails) { Console.WriteLine($"Failed to import - {fileEntry.FileName}"); } } if (equalfails.Count > 0) { foreach (var fileEntry in equalfails) { Console.WriteLine($"Not binary equal - {fileEntry.FileName}"); } } Assert.AreEqual(0, uncookfails.Count + importfails.Count + equalfails.Count); // cleanup var allf = resultDir.GetFiles("*", SearchOption.AllDirectories); foreach (var fileInfo in allf) { fileInfo.Delete(); } var alld = resultDir.GetDirectories(); foreach (var directoryInfo in alld) { directoryInfo.Delete(true); } void Cleanup() { var allfiles = resultDir.GetFiles("*", SearchOption.AllDirectories); try { foreach (var fileInfo in allfiles) { fileInfo.Delete(); } } catch (Exception e) { Console.WriteLine(e); throw; } } }