Program.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.IO;
  3. using System.IO.Pipes;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.Text.Json;
  6. using System.Xml.Serialization;
  7. using Tile_Engine;
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. MapSquare[,] mapCells = new MapSquare[TileMap.MapWidth, TileMap.MapHeight];
  13. string mapDir = Path.Combine("..", "..","..", "..", "..", "Core", "Content", "Maps");
  14. string[] mapFiles = Directory.GetFiles(mapDir, "*.MAP", SearchOption.TopDirectoryOnly);
  15. Console.WriteLine($"Found {mapFiles.Length} .MAP files.");
  16. foreach (var mapFile in mapFiles)
  17. {
  18. try
  19. {
  20. using var fileStream = File.OpenRead(mapFile);
  21. BinaryFormatter formatter = new BinaryFormatter();
  22. mapCells = (MapSquare[,])formatter.Deserialize(fileStream);
  23. fileStream.Close();
  24. string jsonPath = Path.ChangeExtension(mapFile, ".json");
  25. // Convert MapSquare[,] to MapSquare[][]
  26. int width = mapCells.GetLength(0);
  27. int height = mapCells.GetLength(1);
  28. var jagged = new MapSquare[width][];
  29. for (int x = 0; x < width; x++)
  30. {
  31. jagged[x] = new MapSquare[height];
  32. for (int y = 0; y < height; y++)
  33. jagged[x][y] = mapCells[x, y];
  34. }
  35. string json = JsonSerializer.Serialize(jagged, new JsonSerializerOptions { WriteIndented = true });
  36. File.WriteAllText(jsonPath, json);
  37. Console.WriteLine($"Converted: {Path.GetFileName(mapFile)} -> {Path.GetFileName(jsonPath)}");
  38. }
  39. catch (Exception ex)
  40. {
  41. Console.WriteLine($"Failed to convert {mapFile}: {ex.Message}");
  42. }
  43. }
  44. Console.WriteLine("Done.");
  45. }
  46. }