static void Main(string[] args) { Console.WriteLine(MapToolsVersion.Version.GetVersionString(ToolDescription)); Console.WriteLine( ); #if !DEBUG try { #endif MapInjectParsedArguments pargs = new MapInjectParsedArguments( args ); if ( pargs.Help ) { ShowHelp(); return; } if ( pargs.Source == "" ) { Console.WriteLine( "No source file specified!" ); return; } string source = pargs.Source; if ( Path.GetExtension( source ) == "" ) source = Path.ChangeExtension( source, "eu2map" ); source = Path.GetFullPath( source ); // Check if source exists if ( !System.IO.File.Exists( source ) ) { Console.WriteLine( "The specified source file \"{0}\" could not be found.", Path.GetFileName( source ) ); return; } EU2.Install install; if ( pargs.DirectoryOverride.Length > 0 ) { string dir = Path.GetFullPath( pargs.DirectoryOverride ); if ( !Directory.Exists( dir ) ) { Console.WriteLine( "The specified directory override \"{0}\" does not exist.", dir ); return; } install = EU2.Install.FromPath( dir ); } else install = EU2.Install.FromRegistry(); if ( install == null ) { Console.WriteLine( "Error: Can't get EU2 base directory (if using /D, check your path)." ); return; } Console.WriteLine( "Opening source file \"{0}\"...", Path.GetFileName( source ) ); EU2.Edit.File file = new EU2.Edit.File(); file.ReadFrom( source ); Console.WriteLine( "Regenerating boundboxes..." ); file.BoundBoxes = new BoundBoxes( file.IDMap.CalculateBoundBoxes() ); if ( pargs.RegenerateLightmaps > 0 ) { if ( file.Lightmap2 != null && file.Lightmap3 != null && pargs.RegenerateLightmaps < 2 ) { Console.WriteLine( "Lightmaps exists, not regenerating done." ); } else { if ( pargs.RegenerateLightmaps >= 2 || file.Lightmap2 == null ) { Console.WriteLine( "Regenerating Lightmap2 and Lightmap3 from Lightmap1..." ); Lightmap map2, map3; file.Lightmap1.Shrink( out map2, out map3 ); file.Lightmap2 = map2; file.Lightmap3 = map3; } else { Console.WriteLine( "Regenerating Lightmap3 from Lightmap2..." ); file.Lightmap3 = file.Lightmap2.Shrink(); } } } Console.WriteLine( "Regenerating IDGrid..." ); try { file.IDGrid = IDGrid.Build( file.IDMap ); } catch ( EU2.RegionSizeOverflowException e ) { Console.WriteLine( "The map you are trying to inject is too complicated (More than 256 provinces in the area {0},{1}-{2},{3}). Please reduce complexity before proceeding.", e.Region.Left, e.Region.Top, e.Region.Right, e.Region.Bottom ); return; } Console.WriteLine( "Regenerating Incognita Grid..." ); file.IncognitaGrid = IncognitaGrid.Build( file.IDMap, file.Provinces, file.IDGrid ); file.FileProcessing += new EU2.Edit.FileProcessingHandler( EU2File_WriteFileItemInject ); file.Inject( install, new InjectParams( pargs.NoTOT ) ); if ( pargs.SaveSource ) { file.FileProcessing -= new EU2.Edit.FileProcessingHandler( EU2File_WriteFileItemInject ); file.FileProcessing += new EU2.Edit.FileProcessingHandler( EU2File_WriteFileItemSave ); file.WriteTo( source ); } Console.Write( "Injection successful." ); #if !DEBUG } catch( Exception e ) { MapToolsLib.Utils.LogError( e ); } #endif Console.WriteLine( ); }
public static void ImportProvince( string source, string target, ExportMode mode ) { Console.WriteLine( "Opening source file \"{0}\"...", Path.GetFileName( source ) ); ProvinceList provinces = new ProvinceList(); FileStream stream = null; try { stream = new FileStream( source, FileMode.Open, FileAccess.Read, FileShare.Read ); if ( mode == ExportMode.XML ) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load( stream ); provinces.ReadFrom( doc ); } else { provinces.ReadFrom( new EU2.IO.CSVReader( stream ) ); } } finally { if ( stream != null ) stream.Close(); } Console.WriteLine( "Opening target file \"{0}\"...", Path.GetFileName( target ) ); EU2.Edit.File file = new EU2.Edit.File(); try { file.ReadFrom( target ); } catch ( EU2.Edit.InvalidFileFormatException ) { Console.WriteLine( "The specified target file is not an EU2Map file, or is corrupt." ); return; } file.Provinces = provinces; file.WriteTo( target ); Console.WriteLine( "Import successful." ); }
public static void Run( string source, string target, int regen, Point relocate, bool nowrite, PngLevel pngLevel, bool tolerant, bool checkIdgrid ) { if (pngLevel != PngLevel.All && pngLevel != PngLevel.None) { Console.WriteLine("Can't use PNG specifier with import. Use /p instead of /p:<spec>."); return; } if ( nowrite ) Console.WriteLine( "*** Simulation ***\n" ); // Open the PSD, and check if it's valid. PSD.File psd = QueryImage.Run( source ); if ( !CheckPSD( psd ) ) return; Console.WriteLine( ); // Get the mapinfo from the PSD MapInfo info = MapInfo.FromBitmap( psd.Layers[Boot.MapInfoLayerName].Image ); if ( !relocate.IsEmpty ) { relocate = new Point( Lightmap.FitToGrid( relocate.X ), Lightmap.FitToGrid( relocate.Y ) ); Console.WriteLine( "Image will be relocated from {0},{1} to {2},{3}...", info.X, info.Y, relocate.X, relocate.Y ); info.Location = relocate; } // Extract the 3 buffers from the PSD int[] colorbuffer, idbuffer, borderbuffer; ImportImage.GetBuffers( psd, out colorbuffer, out idbuffer, out borderbuffer ); // Remove ref to PSD so it can get garbagecollected psd = null; // Get the convertor IIDConvertor convertor = GetConvertor( info ); if ( convertor == null ) return; IIDConvertor tconvertor = null; if ( convertor is Inferis2IDConvertor && tolerant ) tconvertor = new InferisProxIDConvertor(); if ( tconvertor != null ) Console.WriteLine( "Using tolerant ID conversion." ); // Now, open the map file Console.WriteLine( "Opening target file \"{0}\"...", Path.GetFileName( target ) ); EU2.Edit.File file = new EU2.Edit.File(); file.ReadFrom( target ); int[] cidbuffer = null; if ( info.LightmapZoom == 0 ) { Console.Write( "Checking ID map... " ); // Use another buffer, since we need to remember the original for the image import later on. cidbuffer = new int[idbuffer.Length]; int invalid = 0; for ( int i=0; i<idbuffer.Length; ++i ) { cidbuffer[i] = convertor.ConvertRGB( idbuffer[i] ); if ( cidbuffer[i] < 0 || cidbuffer[i] > Province.Count ) { if ( tconvertor != null ) cidbuffer[i] = tconvertor.ConvertRGB( idbuffer[i] ); if ( cidbuffer[i] < 0 || cidbuffer[i] > Province.Count ) { Console.WriteLine( "Error: Found invalid ID {2} at ({0},{1}). RGB value = {3:X6}", i % info.Width, (int)(i/info.Width), cidbuffer[i], (idbuffer[i] & 0xFFFFFF) ); if ( invalid++ > 10 ) { Console.WriteLine( "Error: Too many invalid IDs found... Stopping." ); break; } } } if ( cidbuffer[i] == Province.TerraIncognitaID ) cidbuffer[i] = Province.MaxValue; } if ( invalid > 0 ) { Console.WriteLine( "Error: Invalid IDs encountered. Cannot import. Please fix." ); return; } } // If needed, write out test PNGs of the extracted layers if (pngLevel != PngLevel.None) WriteTestPNGs(colorbuffer, idbuffer, borderbuffer, pngLevel, info); // and get the correct Lightmap Lightmap map = null; switch ( info.LightmapZoom ) { case 0: map = file.Lightmap1; break; case 1: map = file.Lightmap2; break; case 2: map = file.Lightmap3; break; } if ( map == null ) { Console.WriteLine( "Error: The source image was exported from lightmap{0}, but it no longer exists in the target file." ); return; } if ( map.Zoom == 0 ) { // Then check if the bitmap differs... ushort[] checkbuffer = file.IDMap.ExportBitmapBuffer( info.Bounds ); bool mismatch = false; System.Diagnostics.Debug.Assert( checkbuffer.Length == cidbuffer.Length, "ID buffers have non-matching size" ); for ( int i=0; !mismatch && i<cidbuffer.Length; ++i ) { if ( cidbuffer[i] != checkbuffer[i] ) mismatch = true; } if ( mismatch ) { if ( map.Zoom > 0 ) { Console.WriteLine( "Changes to ID map only supported for lightmap 1." ); return; } Console.WriteLine( "Mismatch found, updating the ID map." ); file.IDMap.ImportBitmapBuffer( info.Bounds, cidbuffer ); } else Console.WriteLine( " No update needed to the ID map." ); ushort[] affectedIDs = null; bool doBoundboxes = false; if ( regen > 0 ) { // regenerate certain files first... if ( regen > 2 ) { Console.WriteLine( "Regenerating adjacenties (complete)..." ); file.AdjacencyTable = file.IDMap.BuildAdjacencyTable( file.Provinces ); // This marks all ids as affected. affectedIDs = new ushort[Province.Count]; for ( ushort i=0; i<Province.Count; ++i ) affectedIDs[i] = i; // Force regen of boundboxes doBoundboxes = true; } else if ( mismatch || regen > 1 ) { Console.Write( "Regenerating adjacenties (quick) " ); if ( regen > 1 ) Console.Write( "(forced)" ); Console.WriteLine( "... " ); // Rebuild part and note the affected Ids. affectedIDs = file.IDMap.BuildAdjacencyTable( file.AdjacencyTable, info.Bounds, file.Provinces ); // Force regen of boundboxes doBoundboxes = true; } } // reattach map.Attach( file.Provinces, file.AdjacencyTable, file.IDMap ); if ( doBoundboxes ) { Console.WriteLine( "Regenerating boundboxes..." ); file.BoundBoxes = new BoundBoxes( file.IDMap.CalculateBoundBoxes() ); } if ( affectedIDs != null && affectedIDs.Length > 0 ) { Rectangle[] boxes = file.BoundBoxes.GetBoxes( affectedIDs ); int[] indexes = map.GetBlockIndexes( boxes ); int[] indexes2 = map.GetBlockIndexes( info.Bounds ); int total = indexes.Length; for ( int i=0; i<indexes.Length; ++i ) { for ( int e=0; e<indexes2.Length; ++e ) { if ( indexes[i] == indexes2[e] ) { indexes[i] = -1; total--; break; } } } Console.WriteLine( "Decompressing blocks of affected provinces outside of modified area." ); Console.Write( "{0} provinces affected, {1} blocks to recompress... ", affectedIDs.Length, total ); if ( total > 0 ) { indexes2 = new int[total]; total = 0; for ( int i=0; i<indexes.Length; ++i ) { if ( indexes[i] > 0 ) indexes2[total++] = indexes[i]; } map.Recompress( indexes2 ); } if ( nowrite && checkIdgrid ) { Console.Write( "Checking for IDGrid complexity..." ); Rectangle errorArea; if ( IDGrid.CheckAreaForOverflow( file.IDMap, info.Bounds, out errorArea ) ) { Console.WriteLine(" ok!" ); } else { Console.WriteLine( " too complex (error in region {0},{1}-{2},{3}).", errorArea.Left, errorArea.Top, errorArea.Right, errorArea.Bottom); } } Console.WriteLine( "Done." ); } } else { Console.Write( "Scaling down ID map (file changes will not be applied at this zoom level)..." ); ushort[] tmpbuffer = MapToolsLib.Utils.ScaleIDBuffer( file.IDMap.ExportBitmapGrid( map.CoordMap.ZoomedToActual( info.Bounds ) ), map.Zoom ); idbuffer = new int[tmpbuffer.Length]; for ( int i=0; i<idbuffer.Length; ++i ) { if ( tmpbuffer[i] >= Province.Count ) tmpbuffer[i] = Province.TerraIncognitaID; idbuffer[i] = (tconvertor == null ? convertor : tconvertor).ConvertID( tmpbuffer[i] ); } Console.WriteLine( " Done." ); } Console.WriteLine( "Importing image..." ); RawImage rawimg = new IOShader( tconvertor == null ? convertor : tconvertor ).Unshade32( map.Zoom, info.Bounds, colorbuffer, idbuffer, borderbuffer ); map.EncodeImage( rawimg ); if ( !nowrite ) { file.WriteTo( target ); Console.Write( "Import successful." ); } else { try { file.WriteTo( System.IO.FileStream.Null ); } catch ( Exception e ) { Console.Write( "There's an error when saving the target file." ); throw e; } Console.Write( "The import process was tested successfully. You can safely do a real import with the same source and target." ); } }
static void Main(string[] args) { Console.WriteLine(MapToolsVersion.Version.GetVersionString(ToolDescription)); Console.WriteLine( ); #if !DEBUG try { #endif MapExtractParsedArguments pargs = new MapExtractParsedArguments( args ); if ( pargs.Help ) { ShowHelp(); return; } if ( pargs.Target == "" ) { Console.WriteLine( "No target file specified!" ); return; } string target = pargs.Target; if ( Path.GetExtension( target ) == "" ) target = Path.ChangeExtension( target, "eu2map" ); target = Path.GetFullPath( target ); // Check if target exists if ( System.IO.File.Exists( target ) && !pargs.Overwrite ) { Console.WriteLine( "The specified target file \"{0}\" exists. Specify the /O option to overwrite it.", Path.GetFileName( target ) ); return; } EU2.Install install; if ( pargs.DirectoryOverride.Length > 0 ) { string dir = Path.GetFullPath( pargs.DirectoryOverride ); if ( !Directory.Exists( dir ) ) { Console.WriteLine( "The specified directory override \"{0}\" does not exist.", dir ); return; } install = EU2.Install.FromPath( dir ); } else install = EU2.Install.FromRegistry(); if ( pargs.Create ) { Console.WriteLine( "Creating empty map file \"{0}\"...", Path.GetFileName( target ) ); EU2.Edit.File file = new EU2.Edit.File(); file.Lightmap1 = Lightmap.CreateEmpty( 0 ); file.Provinces = new ProvinceList(); file.AdjacencyTable = new AdjacencyTable(); file.IDMap = new IDMap(); FileStream stream = null; if ( install != null && System.IO.File.Exists( install.GetMapFile( "colorscales.csv" ) ) ) { stream = new FileStream( install.GetMapFile( "colorscales.csv" ), FileMode.Open, FileAccess.Read, FileShare.Read ); try { file.ColorScales = new ColorScales( new EU2.IO.CSVReader( stream ) ); } finally { if ( stream != null ) stream.Close(); } } file.FileProcessing += new FileProcessingHandler( EU2File_WriteFileItem ); file.WriteTo( target ); Console.WriteLine( "Extract successful." ); } else { if ( install == null ) { Console.WriteLine( "Error: Can't get EU2 base directory (if using /D, check your path)." ); return; } Console.WriteLine( "Extracting file from EU2 files to \"{0}\"...\nRemember: This can take a while.", Path.GetFileName( target ) ); EU2.Edit.FileExtractOptions options = new EU2.Edit.FileExtractOptions( pargs.IncludeLightmaps, pargs.ForceGenerate, true ); EU2.Edit.File.Extract( install, target, options, new EU2.Edit.FileProcessingHandler( EU2File_WriteFileItem ) ); Console.WriteLine( "Extract successful." ); } #if !DEBUG } catch( Exception e ) { MapToolsLib.Utils.LogError( e ); } #endif Console.WriteLine( ); }