private async Task Action(string[] path, string outpath, bool deserialize, bool serialize, string pattern, string regex, ETextConvertFormat format, IHost host) { var serviceProvider = host.Services; var consoleFunctions = serviceProvider.GetRequiredService <ConsoleFunctions>(); await consoleFunctions.Cr2wTask(path, outpath, deserialize, serialize, pattern, regex, format); }
/// <summary> /// Converts a given redengine file to a text format in a given outputDirectory /// </summary> /// <param name="format"></param> /// <param name="infile"></param> /// <param name="outputDirInfo"></param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public bool ConvertToAndWrite(ETextConvertFormat format, string infile, DirectoryInfo outputDirInfo) { try { var text = ConvertToText(format, infile); var outpath = Path.Combine(outputDirInfo.FullName, $"{Path.GetFileName(infile)}.{format}"); File.WriteAllText(outpath, text); _loggerService.Success($"Exported {infile} to {outpath}"); return(true); } catch (InvalidParsingException ie) { _loggerService.Error(ie); } catch (SerializationException se) { _loggerService.Error(se); } catch (Exception e) { _loggerService.Error(e); } return(false); }
/// <summary> /// Converts a W2RC stream to text /// </summary> /// <param name="format"></param> /// <param name="instream"></param> /// <returns></returns> /// <exception cref="InvalidParsingException"></exception> /// <exception cref="SerializationException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> public string ConvertToText(ETextConvertFormat format, string infile) { using var instream = new FileStream(infile, FileMode.Open, FileAccess.Read); if (!_wolvenkitFileService.TryReadRed4File(instream, out var cr2w)) { throw new InvalidParsingException("ConvertToText"); } cr2w.MetaData.FileName = infile; var dto = new RedFileDto(cr2w); var json = RedJsonSerializer.Serialize(dto); if (string.IsNullOrEmpty(json)) { throw new SerializationException(); } switch (format) { case ETextConvertFormat.json: return(json); case ETextConvertFormat.xml: throw new NotSupportedException(nameof(format)); default: throw new ArgumentOutOfRangeException(nameof(format), format, null); } }
/// <summary> /// Converts a given redengine file to a text format in a given outputDirectory /// </summary> /// <param name="format"></param> /// <param name="infile"></param> /// <param name="outputDirInfo"></param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public bool ConvertToAndWrite(ETextConvertFormat format, string infile, DirectoryInfo outputDirInfo) { using var fs = new FileStream(infile, FileMode.Open, FileAccess.Read); try { var json = ConvertToText(format, fs); var outpath = Path.Combine(outputDirInfo.FullName, $"{Path.GetFileName(infile)}.{format.ToString()}"); switch (format) { case ETextConvertFormat.json: File.WriteAllText(outpath, json); break; case ETextConvertFormat.xml: var doc = JsonConvert.DeserializeXmlNode(json, RedFileDto.Magic); doc?.Save(outpath); break; default: throw new ArgumentOutOfRangeException(nameof(format), format, null); } return(true); } catch (InvalidParsingException ie) { _loggerService.Error(ie); } catch (SerializationException se) { _loggerService.Error(se); } catch (Exception e) { _loggerService.Error(e); } return(false); }
/// <summary> /// Converts a W2RC stream to text /// </summary> /// <param name="format"></param> /// <param name="instream"></param> /// <returns></returns> /// <exception cref="InvalidParsingException"></exception> /// <exception cref="SerializationException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> public string ConvertToText(ETextConvertFormat format, Stream instream) { var cr2w = _wolvenkitFileService.TryReadCr2WFile(instream); if (cr2w == null) { throw new InvalidParsingException(); } var json = ""; var dto = new RedFileDto(cr2w); json = JsonConvert.SerializeObject(dto, Formatting.Indented); if (string.IsNullOrEmpty(json)) { throw new SerializationException(); } switch (format) { case ETextConvertFormat.json: return(json); case ETextConvertFormat.xml: { var doc = JsonConvert.DeserializeXmlNode(json, RedFileDto.Magic); using var tw = new StringWriter(); doc?.Save(tw); return(tw.ToString()); } default: throw new ArgumentOutOfRangeException(nameof(format), format, null); } }
private void Cr2wTaskInner(string path, string outputDirectory, bool deserialize, bool serialize, string pattern = "", string regex = "", ETextConvertFormat format = ETextConvertFormat.json) { #region checks if (string.IsNullOrEmpty(path)) { _loggerService.Warning("Please fill in an input path."); return; } var inFileInfo = new FileInfo(path); var inDirInfo = new DirectoryInfo(path); var isDirectory = !inFileInfo.Exists && inDirInfo.Exists; var isFile = inFileInfo.Exists && !inDirInfo.Exists; if (!isDirectory && !isFile) { _loggerService.Error("Input file does not exist."); return; } #endregion checks Stopwatch watch = new(); watch.Restart(); // get all files var fileInfos = isDirectory ? inDirInfo.GetFiles("*", SearchOption.AllDirectories).ToList() : new List <FileInfo> { inFileInfo }; IEnumerable <FileInfo> finalmatches = fileInfos; if (deserialize) { finalmatches = fileInfos.Where(_ => _.Extension == $".{format}"); } if (serialize) { finalmatches = fileInfos.Where(_ => Enum.GetNames(typeof(ERedExtension)).Contains(_.TrimmedExtension())); } // check search pattern then regex if (!string.IsNullOrEmpty(pattern)) { finalmatches = fileInfos.MatchesWildcard(item => item.FullName, pattern); } if (!string.IsNullOrEmpty(regex)) { var searchTerm = new System.Text.RegularExpressions.Regex($@"{regex}"); var queryMatchingFiles = from file in finalmatches let matches = searchTerm.Matches(file.FullName) where matches.Count > 0 select file; finalmatches = queryMatchingFiles; } var finalMatchesList = finalmatches.ToList(); _loggerService.Info($"Found {finalMatchesList.Count} files to process."); int progress = 0; //foreach (var fileInfo in finalMatchesList) Parallel.ForEach(finalMatchesList, fileInfo => { var outputDirInfo = string.IsNullOrEmpty(outputDirectory) ? fileInfo.Directory : new DirectoryInfo(outputDirectory); if (outputDirInfo == null || !outputDirInfo.Exists) { _loggerService.Error("Invalid output directory."); return; } if (serialize) { var infile = fileInfo.FullName; if (_modTools.ConvertToAndWrite(format, infile, outputDirInfo)) { _loggerService.Success($"Saved {infile} to {format.ToString()}."); } } if (deserialize) { try { _modTools.ConvertFromAndWrite(fileInfo, outputDirInfo); _loggerService.Success($"Converted {fileInfo.FullName} to CR2W"); } catch (Exception e) { _loggerService.Error($"Could not convert {fileInfo.FullName} Error:"); _loggerService.Error(e.ToString()); //throw; } } Interlocked.Increment(ref progress); }); //} watch.Stop(); _loggerService.Info($"Elapsed time: {watch.ElapsedMilliseconds.ToString()}ms."); }
public void Cr2wTask(string[] path, string outpath, bool deserialize, bool serialize, string pattern, string regex, ETextConvertFormat format) { if (path == null || path.Length < 1) { _loggerService.Warning("Please fill in an input path."); return; } foreach (var s in path) { Cr2wTaskInner(s, outpath, deserialize, serialize, pattern, regex, format); } // Parallel.ForEach(path, file => // { // Cr2wTaskInner(file, outpath, deserialize, serialize, pattern, regex, format); // }); }