private Imgd FileLoader(string filePath, MaterialDef matDef, MapGenConfig config) { logger.Debug($"Load image from \"{filePath}\""); if (FileExtUtil.IsExtension(filePath, ".imd")) { return(ImageResizer.NormalizeImageSize(File.OpenRead(filePath).Using(s => Imgd.Read(s)))); } if (FileExtUtil.IsExtension(filePath, ".png")) { var imdFile = Path.ChangeExtension(filePath, ".imd"); if (config.skipConversionIfExists && File.Exists(imdFile)) { logger.Debug($"Skipping png to imd conversion, due to imd file existence and skipConversionIfExists option."); } else { try { logger.Debug($"Using ImgTool for png to imd conversion."); var imgtoolOptions = matDef.imgtoolOptions ?? config.imgtoolOptions ?? "-b 8"; var result = new RunCmd( "OpenKh.Command.ImgTool.exe", $"imd \"{filePath}\" -o \"{imdFile}\" {imgtoolOptions}" ); if (result.ExitCode != 0) { throw new Exception($"ImgTool failed ({result.ExitCode})"); } } catch (Win32Exception ex) { throw new Exception("ImgTool failed.", ex); } } return(FileLoader(imdFile, matDef, config)); } throw new NotSupportedException(Path.GetExtension(filePath)); }
public void Run(string inputFile, string outputMap) { var yamlReader = new YamlDotNet.Serialization.DeserializerBuilder() .IgnoreUnmatchedProperties() .Build(); MapBuilder builder; MapGenConfig config; string baseDir; string outMapFile; if (FileExtUtil.IsExtension(inputFile, ".yml")) { var ymlFile = Path.GetFullPath(inputFile); baseDir = Path.GetDirectoryName(ymlFile); logger.Debug($"ymlFile is \"{ymlFile}\""); logger.Debug($"baseDir is \"{baseDir}\""); config = yamlReader.Deserialize <MapGenConfig>(File.ReadAllText(ymlFile)); var modelFile = Path.Combine(baseDir, config.inputFile); outMapFile = Path.GetFullPath(outputMap ?? Path.Combine(baseDir, config.outputFile)); builder = new MapBuilder(modelFile, config, CreateImageLoader(baseDir, config, FileLoader)); } else { var modelFile = Path.GetFullPath(inputFile); baseDir = Path.GetDirectoryName(modelFile); var ymlFile = Path.Combine(baseDir, "mapdef.yml"); outMapFile = Path.GetFullPath(outputMap ?? Path.Combine(baseDir, Path.ChangeExtension(inputFile, ".map"))); logger.Debug($"ymlFile is \"{ymlFile}\""); logger.Debug($"baseDir is \"{baseDir}\""); config = File.Exists(ymlFile) ? yamlReader.Deserialize <MapGenConfig>(File.ReadAllText(ymlFile)) : new MapGenConfig(); builder = new MapBuilder(modelFile, config, CreateImageLoader(baseDir, config, FileLoader)); } logger.Debug("Building map file structure."); var buff = new MemoryStream(); void trySaveTo(string toFile, MemoryStream stream) { if (!string.IsNullOrWhiteSpace(toFile)) { toFile = Path.Combine(baseDir, toFile); logger.Debug($"Writing raw data to \"{toFile}\"."); Directory.CreateDirectory(Path.GetDirectoryName(toFile)); File.WriteAllBytes(toFile, stream.ToArray()); } } Bar.Write( buff, builder.GetBarEntries(trySaveTo) .Concat(LoadAdditionalBarEntries(config, CreateRawFileLoader(baseDir))) .ToArray() ); logger.Debug($"Writing to \"{outMapFile}\"."); File.WriteAllBytes(outMapFile, buff.ToArray()); logger.Debug("Done"); }