<commit_before>package VASL.build.module.map;

import static VASSAL.tools.image.tilecache.ZipFileImageTilerState.STARTING_IMAGE;
import static VASSAL.tools.image.tilecache.ZipFileImageTilerState.TILE_WRITTEN;
import static VASSAL.tools.image.tilecache.ZipFileImageTilerState.TILING_FINISHED;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;

import VASSAL.Info;
import VASSAL.launch.TilingHandler;
import VASSAL.build.GameModule;
import VASSAL.tools.DataArchive;
import VASSAL.tools.image.ImageUtils;
import VASSAL.tools.image.tilecache.TileUtils;
import VASSAL.tools.io.FileArchive;
import VASSAL.tools.io.FileStore;
import VASSAL.tools.io.IOUtils;
import VASSAL.tools.io.InputOutputStreamPump;
import VASSAL.tools.io.InputStreamPump;
import VASSAL.tools.io.ProcessLauncher;
import VASSAL.tools.io.ProcessWrapper;
import VASSAL.tools.lang.Pair;
import VASSAL.tools.swing.EDT;
import VASSAL.tools.swing.ProgressDialog;
import VASSAL.tools.swing.Progressor;

public class ASLTilingHandler extends VASSAL.launch.TilingHandler {
  public ASLTilingHandler(
    String aname,
    File cdir,
    Dimension tdim,
    int mhlim,
    int pid)
  {
    super(aname, cdir, tdim, mhlim, pid);
  }

/*
  @Override
  protected boolean isFresh(FileArchive archive,
                            FileStore tcache, String iname)
                                                           throws IOException {
    final String apath = iname;

    // look at the first 1:1 tile
    final String tpath = TileUtils.tileName(iname, 0, 0, 1);

    // check whether the image is older than the tile
    final long imtime = archive.getMTime(apath);

System.out.println(apath + ", " + imtime + ", " + tcache.getMTime(tpath));

    return imtime > 0 && // time in archive might be goofy
           imtime <= tcache.getMTime(tpath);
  }
*/

  @Override
  protected Dimension getImageSize(DataArchive archive, String iname)
                                                           throws IOException {
    InputStream in = null;
    try {
      in = archive.getInputStream(iname);
      final Dimension id = ImageUtils.getImageSize(iname, in);
      in.close();
      return id;
    }
    finally {
      IOUtils.closeQuietly(in);
    }
  }

  @Override
  protected Pair<Integer,Integer> findImages(
    DataArchive archive,
    FileStore tcache,
    List<String> multi,
    List<Pair<String,IOException>> failed) throws IOException
  {
    final FileArchive fa = archive.getArchive();
    final String iname = fa.getFile().getName() + ".gif";

    int maxpix = 0; // number of pixels in the largest image
    int tcount = 0; // tile count

    // look at the first 1:1 tile
    final String tpath = TileUtils.tileName(iname, 0, 0, 1);

    // check whether the image is older than the tile
    final long imtime = fa.getMTime(iname);

    // skip images with fresh tiles
    if (imtime <= 0 || // time in archive might be goofy
        imtime > tcache.getMTime(tpath)) {
      final Dimension idim;
      try {
        idim = getImageSize(archive, iname);
      }
      catch (IOException e) {
        // skip images we can't read
        failed.add(Pair.of(iname, e));
        return new Pair<Integer,Integer>(0, 0);
      }

      // count the tiles at all sizes if we have more than one tile at 1:1
      final int t = TileUtils.tileCountAtScale(idim, tdim, 1) > 1 ?
                    TileUtils.tileCount(idim, tdim) : 0;

      if (t > 0) {
        tcount = t;
        multi.add(iname);
        maxpix = idim.width * idim.height;
      }
    }

    return new Pair<Integer,Integer>(tcount, maxpix);
  }

  @Override
  protected void runSlicer(List<String> multi, final int tcount, int maxheap)
                                   throws CancellationException, IOException {

    final InetAddress lo = InetAddress.getByName(null);
    final ServerSocket ssock = new ServerSocket(0, 0, lo);

    final int port = ssock.getLocalPort();

    final List<String> args = new ArrayList<String>();
    args.addAll(Arrays.asList(new String[] {
      Info.javaBinPath,
      "-classpath",
      System.getProperty("java.class.path"),
      "-Xmx" + maxheap + "M",
      "-DVASSAL.id=" + pid,
      "-Duser.home=" + System.getProperty("user.home"),
      "-DVASSAL.port=" + port,
      "VASSAL.tools.image.tilecache.ZipFileImageTiler",
      aname,
      cdir.getAbsolutePath(),
      String.valueOf(tdim.width),
      String.valueOf(tdim.height)
    }));

    args.addAll(multi);

    // set up the process
    final InputStreamPump outP = new InputOutputStreamPump(null, System.out);
    final InputStreamPump errP = new InputOutputStreamPump(null, System.err);

    final ProcessWrapper proc = new ProcessLauncher().launch(
      null,
      outP,
      errP,
      args.toArray(new String[args.size()])
    );

    Socket csock = null;
    DataInputStream in = null;
    try {
      csock = ssock.accept();
      csock.shutdownOutput();

      in = new DataInputStream(csock.getInputStream());

      boolean done = false;
      byte type;
      while (!done) {
        type = in.readByte();

        switch (type) {
        case STARTING_IMAGE:
          in.readUTF();
          break;

        case TILE_WRITTEN:
          break;

        case TILING_FINISHED:
          done = true;<commit_msg>Adjusted ASLTilingHandler to match changes to VASSAL's TilingHander in /trunk@8351. ASLTilingHandler now expects filenames on stdin.<commit_after>package VASL.build.module.map;

import static VASSAL.tools.image.tilecache.ZipFileImageTilerState.STARTING_IMAGE;
import static VASSAL.tools.image.tilecache.ZipFileImageTilerState.TILE_WRITTEN;
import static VASSAL.tools.image.tilecache.ZipFileImageTilerState.TILING_FINISHED;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;

import VASSAL.Info;
import VASSAL.launch.TilingHandler;
import VASSAL.build.GameModule;
import VASSAL.tools.DataArchive;
import VASSAL.tools.image.ImageUtils;
import VASSAL.tools.image.tilecache.TileUtils;
import VASSAL.tools.io.FileArchive;
import VASSAL.tools.io.FileStore;
import VASSAL.tools.io.IOUtils;
import VASSAL.tools.io.InputOutputStreamPump;
import VASSAL.tools.io.InputStreamPump;
import VASSAL.tools.io.ProcessLauncher;
import VASSAL.tools.io.ProcessWrapper;
import VASSAL.tools.lang.Pair;
import VASSAL.tools.swing.EDT;
import VASSAL.tools.swing.ProgressDialog;
import VASSAL.tools.swing.Progressor;

public class ASLTilingHandler extends VASSAL.launch.TilingHandler {
  public ASLTilingHandler(
    String aname,
    File cdir,
    Dimension tdim,
    int mhlim,
    int pid)
  {
    super(aname, cdir, tdim, mhlim, pid);
  }

/*
  @Override
  protected boolean isFresh(FileArchive archive,
                            FileStore tcache, String iname)
                                                           throws IOException {
    final String apath = iname;

    // look at the first 1:1 tile
    final String tpath = TileUtils.tileName(iname, 0, 0, 1);

    // check whether the image is older than the tile
    final long imtime = archive.getMTime(apath);

System.out.println(apath + ", " + imtime + ", " + tcache.getMTime(tpath));

    return imtime > 0 && // time in archive might be goofy
           imtime <= tcache.getMTime(tpath);
  }
*/

  @Override
  protected Dimension getImageSize(DataArchive archive, String iname)
                                                           throws IOException {
    InputStream in = null;
    try {
      in = archive.getInputStream(iname);
      final Dimension id = ImageUtils.getImageSize(iname, in);
      in.close();
      return id;
    }
    finally {
      IOUtils.closeQuietly(in);
    }
  }

  @Override
  protected Pair<Integer,Integer> findImages(
    DataArchive archive,
    FileStore tcache,
    List<String> multi,
    List<Pair<String,IOException>> failed) throws IOException
  {
    final FileArchive fa = archive.getArchive();
    final String iname = fa.getFile().getName() + ".gif";

    int maxpix = 0; // number of pixels in the largest image
    int tcount = 0; // tile count

    // look at the first 1:1 tile
    final String tpath = TileUtils.tileName(iname, 0, 0, 1);

    // check whether the image is older than the tile
    final long imtime = fa.getMTime(iname);

    // skip images with fresh tiles
    if (imtime <= 0 || // time in archive might be goofy
        imtime > tcache.getMTime(tpath)) {
      final Dimension idim;
      try {
        idim = getImageSize(archive, iname);
      }
      catch (IOException e) {
        // skip images we can't read
        failed.add(Pair.of(iname, e));
        return new Pair<Integer,Integer>(0, 0);
      }

      // count the tiles at all sizes if we have more than one tile at 1:1
      final int t = TileUtils.tileCountAtScale(idim, tdim, 1) > 1 ?
                    TileUtils.tileCount(idim, tdim) : 0;

      if (t > 0) {
        tcount = t;
        multi.add(iname);
        maxpix = idim.width * idim.height;
      }
    }

    return new Pair<Integer,Integer>(tcount, maxpix);
  }

  @Override
  protected void runSlicer(List<String> multi, final int tcount, int maxheap)
                                   throws CancellationException, IOException {

    final InetAddress lo = InetAddress.getByName(null);
    final ServerSocket ssock = new ServerSocket(0, 0, lo);

    final int port = ssock.getLocalPort();

    final List<String> args = new ArrayList<String>();
    args.addAll(Arrays.asList(new String[] {
      Info.javaBinPath,
      "-classpath",
      System.getProperty("java.class.path"),
      "-Xmx" + maxheap + "M",
      "-DVASSAL.id=" + pid,
      "-Duser.home=" + System.getProperty("user.home"),
      "-DVASSAL.port=" + port,
      "VASSAL.tools.image.tilecache.ZipFileImageTiler",
      aname,
      cdir.getAbsolutePath(),
      String.valueOf(tdim.width),
      String.valueOf(tdim.height)
    }));

    args.addAll(multi);

    // set up the process
    final InputStreamPump outP = new InputOutputStreamPump(null, System.out);
    final InputStreamPump errP = new InputOutputStreamPump(null, System.err);

    final ProcessWrapper proc = new ProcessLauncher().launch(
      null,
      outP,
      errP,
      args.toArray(new String[args.size()])
    );

    // write the image paths to child's stdin, one per line
    PrintWriter stdin = null;
    try {
      stdin = new PrintWriter(proc.stdin);
      for (String m : multi) {
        stdin.println(m);
      }
    }
    finally {
      IOUtils.closeQuietly(stdin);
    }

    Socket csock = null;
    DataInputStream in = null;
    try {
      csock = ssock.accept();
      csock.shutdownOutput();

      in = new DataInputStream(csock.getInputStream());

      boolean done = false;
      byte type;
      while (!done) {
        type = in.readByte();

        switch (type) {
        case STARTING_IMAGE:
          in.readUTF();
          break;

        case TILE_WRITTEN:
          break;

        case TILING_FINISHED:
          done = true;
{'fraction_non_alphanumeric': 0.10350386018494952, 'fraction_numerical': 0.004411639942309324, 'mean_word_length': 2.684901531728665, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4732302', 'n_tokens_mistral': 3772, 'n_tokens_neox': 3642, 'n_words': 900}