/// <summary> Attempts to save the map, under the specified filename using the specified format. </summary> /// <param name="mapToSave"> Map file to be saved.</param> /// <param name="fileName">The name of the file to save to. </param> /// <param name="format"> The format to use when saving the map. </param> /// <returns> Whether or not the map save completed successfully. </returns> /// <exception cref="ArgumentNullException"> If mapToSave or fileName are null. </exception> /// <exception cref="ArgumentException"> If format is set to MapFormat.Unknown. </exception> /// <exception cref="MapFormatException"> If no converter could be found for the given format. </exception> /// <exception cref="NotImplementedException"> If saving to this format is not implemented or supported. </exception> public static bool TrySave([NotNull] Map mapToSave, [NotNull] string fileName, MapFormat format) { if (mapToSave == null) { throw new ArgumentNullException("mapToSave"); } if (fileName == null) { throw new ArgumentNullException("fileName"); } if (format == MapFormat.Unknown) { throw new ArgumentException("Format may not be \"Unknown\"", "format"); } if (Exporters.ContainsKey(format)) { IMapExporter converter = Exporters[format]; if (converter.SupportsExport) { try { return(converter.Save(mapToSave, fileName)); } catch (Exception ex) { Logger.LogAndReportCrash("Map failed to save", "MapConversion", ex, false); return(false); } } else { throw new NotSupportedException(format + " map converter does not support saving."); } } throw new NoMapConverterFoundException("No converter could be found for the given format."); }
/// <summary> Save the map, under the specified file name using the specified format. </summary> /// <param name="mapToSave"> Map file to be saved. </param> /// <param name="fileName">The name of the file to save to. </param> /// <param name="mapFormat"> The format to use when saving the map. </param> /// <exception cref="ArgumentNullException"> mapToSave or fileName are null. </exception> /// <exception cref="ArgumentException"> mapFormat is set to MapFormat.Unknown. </exception> /// <exception cref="NoMapConverterFoundException"> No exporter could be found for the given format. </exception> /// <exception cref="Exception"> Other kinds of exceptions may be thrown by the map exporter. </exception> public static void Save([NotNull] Map mapToSave, [NotNull] string fileName, MapFormat mapFormat) { if (mapToSave == null) { throw new ArgumentNullException("mapToSave"); } if (fileName == null) { throw new ArgumentNullException("fileName"); } if (mapFormat == MapFormat.Unknown) { throw new ArgumentException("Format may not be \"Unknown\"", "mapFormat"); } if (Exporters.ContainsKey(mapFormat)) { IMapExporter converter = Exporters[mapFormat]; if (converter.SupportsExport) { converter.Save(mapToSave, fileName); } } throw new NoMapConverterFoundException("No exporter could be found for the given format."); }
static bool ConvertOneMap([NotNull] FileSystemInfo fileSystemInfo) { if (fileSystemInfo == null) { throw new ArgumentNullException("fileSystemInfo"); } try { Map map; if (importer != null) { if (!importer.ClaimsName(fileSystemInfo.FullName)) { return(false); } Console.Write("Loading {0}... ", fileSystemInfo.Name); map = importer.Load(fileSystemInfo.FullName); } else { Console.Write("Checking {0}... ", fileSystemInfo.Name); map = MapUtility.Load(fileSystemInfo.FullName); } string targetFileName; if ((fileSystemInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory) { targetFileName = fileSystemInfo.Name + '.' + exporter.FileExtension; } else { targetFileName = Path.GetFileNameWithoutExtension(fileSystemInfo.Name) + '.' + exporter.FileExtension; } string targetPath = Path.Combine(outputDirName, targetFileName); if (!overwrite && File.Exists(targetPath)) { Console.WriteLine(); if (!ShowYesNo("File \"{0}\" already exists. Overwrite?", targetFileName)) { return(false); } } Console.Write("Saving {0}... ", Path.GetFileName(targetFileName)); exporter.Save(map, targetPath); Console.WriteLine("ok"); return(true); } catch (NoMapConverterFoundException) { Console.WriteLine("skip"); return(false); } catch (Exception ex) { Console.WriteLine("ERROR"); Console.Error.WriteLine("{0}: {1}", ex.GetType().Name, ex.Message); return(false); } }
public static int Launch(string[] args) { ProgramArguments arguments = ProgramArguments.Parse(args); if (arguments == null) { return((int)FailCode.FailedParsingArguments); } else { // make sure we have our list of exporters Exporters.Load(); // try to find matching exporters IImageExporter imageExporter = null; IMapExporter mapExporter = null; string imageExtension = Path.GetExtension(arguments.image).Substring(1).ToLower(); foreach (var exporter in Exporters.ImageExporters) { if (exporter.ImageExtension.ToLower() == imageExtension) { imageExporter = exporter; break; } } if (imageExporter == null) { Console.WriteLine("Failed to find exporters for specified image type."); return((int)FailCode.ImageExporter); } if (!string.IsNullOrEmpty(arguments.map)) { string mapExtension = Path.GetExtension(arguments.map).Substring(1).ToLower(); foreach (var exporter in Exporters.MapExporters) { if (exporter.MapExtension.ToLower() == mapExtension) { mapExporter = exporter; break; } } if (mapExporter == null) { Console.WriteLine("Failed to find exporters for specified map type."); return((int)FailCode.MapExporter); } } // compile a list of images List <string> images = new List <string>(); FindImages(arguments, images); // make sure we found some images if (images.Count == 0) { Console.WriteLine("No images to pack."); return((int)FailCode.NoImages); } // make sure no images have the same name if we're building a map if (mapExporter != null) { for (int i = 0; i < images.Count; i++) { string str1 = Path.GetFileNameWithoutExtension(images[i]); for (int j = i + 1; j < images.Count; j++) { string str2 = Path.GetFileNameWithoutExtension(images[j]); if (str1 == str2) { Console.WriteLine("Two images have the same name: {0} = {1}", images[i], images[j]); return((int)FailCode.ImageNameCollision); } } } } // generate our output ImagePacker imagePacker = new ImagePacker(); Bitmap outputImage; Dictionary <string, Rectangle> outputMap; // pack the image, generating a map only if desired int result = imagePacker.PackImage(images, arguments.pow2, arguments.sqr, arguments.mw, arguments.mh, arguments.pad, mapExporter != null, out outputImage, out outputMap); if (result != 0) { Console.WriteLine("There was an error making the image sheet."); return(result); } // try to save using our exporters try { if (File.Exists(arguments.image)) { File.Delete(arguments.image); } imageExporter.Save(arguments.image, outputImage); } catch (Exception e) { Console.WriteLine("Error saving file: " + e.Message); return((int)FailCode.FailedToSaveImage); } if (mapExporter != null) { try { if (File.Exists(arguments.map)) { File.Delete(arguments.map); } mapExporter.Save(arguments.map, outputMap); } catch (Exception e) { Console.WriteLine("Error saving file: " + e.Message); return((int)FailCode.FailedToSaveMap); } } } return(0); }
void PackOnce() { if (mPackTasks.Count == 0) { return; } // generate our output ImagePacker imagePacker = new ImagePacker(); Bitmap outputImage; Dictionary <string, System.Drawing.Rectangle> outputMap; PackTask pt = mPackTasks.Dequeue(); Console.WriteLine("Packing {0} ({1} left to pack)", pt.outputFile, mPackTasks.Count); // pack the image, generating a map only if desired int result = imagePacker.PackImage(pt.inputFiles, REQUIRE_POW2, REQUIRE_SQUARE, 4096, 4096, PADDING, true, out outputImage, out outputMap); if (result != 0) { Console.WriteLine("There was an error making the image sheet."); return; } // try to save using our exporters try { if (File.Exists(pt.outputFile)) { File.Delete(pt.outputFile); } mImageExporter.Save(pt.outputFile, outputImage); Console.WriteLine("Saved atlas {0}.", pt.outputFile); } catch (Exception e) { Console.WriteLine("Error saving file: " + e.Message); return; } if (mMapExporter != null) { try { if (File.Exists(pt.outputMap)) { File.Delete(pt.outputMap); } mMapExporter.Save(pt.outputMap, outputMap); Console.WriteLine("Saved atlas map {0}.", pt.outputMap); } catch (Exception e) { Console.WriteLine("Error saving file: " + e.Message); return; } } foreach (string filename in pt.inputFiles) { if (File.Exists(filename)) { try { File.Delete(filename); } catch (IOException) { // Welp } } } }
static bool ConvertOneMap([NotNull] FileSystemInfo fileSystemInfo, [NotNull] string relativeName) { if (fileSystemInfo == null) { throw new ArgumentNullException("fileSystemInfo"); } if (relativeName == null) { throw new ArgumentNullException("relativeName"); } try { // if output directory was not given, save to same directory as the map file if (!outputDirGiven) { outputDirName = Paths.GetDirectoryNameOrRoot(fileSystemInfo.FullName); } // load the map file Map map; if (importer != null) { if (!importer.ClaimsName(fileSystemInfo.FullName)) { return(false); } Console.Write("Loading {0}... ", relativeName); map = importer.Load(fileSystemInfo.FullName); } else { Console.Write("Checking {0}... ", relativeName); map = MapUtility.Load(fileSystemInfo.FullName, tryHard); } // select target map file name string targetFileName; if ((fileSystemInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory) { targetFileName = fileSystemInfo.Name + '.' + exporter.FileExtension; } else { targetFileName = Path.GetFileNameWithoutExtension(fileSystemInfo.Name) + '.' + exporter.FileExtension; } // get full target map file name, check if it already exists string targetPath = Path.Combine(outputDirName, targetFileName); if (!overwrite && File.Exists(targetPath)) { Console.WriteLine(); if (!ShowYesNo("File \"{0}\" already exists. Overwrite?", targetFileName)) { return(false); } } // save Console.Write("Saving {0}... ", Path.GetFileName(targetFileName)); exporter.Save(map, targetPath); Console.WriteLine("ok"); return(true); } catch (NoMapConverterFoundException) { Console.WriteLine("skip"); return(false); } catch (Exception ex) { Console.WriteLine("ERROR"); Console.Error.WriteLine("{0}: {1}", ex.GetType().Name, ex.Message); return(false); } }
static bool ConvertOneMap([NotNull] FileSystemInfo sourceFile, [NotNull] string relativeName) { if (sourceFile == null) { throw new ArgumentNullException("sourceFile"); } if (relativeName == null) { throw new ArgumentNullException("relativeName"); } try { // if output directory was not given, save to same directory as the map file if (!outputDirGiven) { outputDirName = Paths.GetDirectoryNameOrRoot(sourceFile.FullName); } // load the map file Map map; if (importer != null) { if (!importer.ClaimsName(sourceFile.FullName)) { return(false); } Console.Write("Loading {0}... ", relativeName); map = importer.Load(sourceFile.FullName); } else { Console.Write("Checking {0}... ", relativeName); map = MapUtility.Load(sourceFile.FullName, tryHard); } // select target map file name string targetName; bool sourceIsDir = (sourceFile.Attributes & FileAttributes.Directory) == FileAttributes.Directory; bool targetIsDir = (exporter.StorageType == MapStorageType.Directory); if (targetIsDir) { if (sourceIsDir) { targetName = sourceFile.Name; } else { targetName = Path.GetFileNameWithoutExtension(sourceFile.Name); } } else { if (sourceIsDir) { targetName = sourceFile.Name + '.' + exporter.FileExtension; } else { targetName = Path.GetFileNameWithoutExtension(sourceFile.Name) + '.' + exporter.FileExtension; } } // get full target map file name, check if it already exists string targetPath = Path.Combine(outputDirName, targetName); Console.WriteLine("targetPath=" + targetPath + " | fileExists=" + File.Exists(targetPath) + " | dirExists=" + Directory.Exists(targetPath)); if (!overwrite && (!targetIsDir && File.Exists(targetPath) || targetIsDir && Directory.Exists(targetPath))) { string targetType = (targetIsDir ? "Directory" : "File"); Console.WriteLine(); if (!ConsoleUtil.ShowYesNo("{0} \"{1}\" already exists. Overwrite?", targetType, targetName)) { return(false); } } // save Console.Write("Saving {0}... ", Path.GetFileName(targetName)); exporter.Save(map, targetPath); Console.WriteLine("ok"); return(true); } catch (NoMapConverterFoundException) { Console.WriteLine("skip"); return(false); } catch (Exception ex) { Console.WriteLine("ERROR"); Console.Error.WriteLine("{0}: {1}", ex.GetType().Name, ex.Message); Console.Error.WriteLine(ex.StackTrace); return(false); } }