⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resreader.java

📁 一个j2me的滑雪游戏
💻 JAVA
字号:

package com.massp.util;

import java.io.*;


/**
 * Reads the resource file which was delivered with the original
 * <I>Bum Bum Tennis</I> source code and splits it into its main parts.
 *
 * @author <a href="mailto:benjamin.broll@massp.com">Benjamin Broll</a>
 * @version $Revision: 1.2 $
 * @changed $Author: xavier $ $Date: 2002/11/27 18:08:08 $
 */
public class ResReader {

  public void writeBytesFrom(int pos) {
    String file = "C:\\USR\\WORK\\SkiJumping\\resources\\_original\\JMP.spf";
    String output = "C:\\USR\\WORK\\SkiJumping\\resources\\misc\\rest.bin";

    try {
      byte[] buf = new byte[1024];

      DataOutputStream out = new DataOutputStream(new FileOutputStream(output));
      DataInputStream in = new DataInputStream(new FileInputStream(file));

      in.skip(pos);
      int count = -1;
      while ((count = in.read(buf)) != -1) {
        out.write(buf, 0, count);
      }

      in.close();
      out.close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
  
  public void writeImage(String header, String data, String file)
          throws IOException {
    DataInputStream hin = new DataInputStream(new FileInputStream(header));
    DataInputStream din = new DataInputStream(new FileInputStream(data));
    DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
    
    byte[] buf = new byte[1024];
    int len = -1;
    while ((len = hin.read(buf)) != -1) {
      out.write(buf, 0, len);
    }
    
    len = -1;
    while ((len = din.read(buf)) != -1) {
      out.write(buf, 0, len);
    }
    
    hin.close();
    din.close();
    out.close();    
  }
    
  
  private void skipUnnecessary(DataInputStream in) throws IOException {
    in.readShort();
    in.readInt();
    in.readShort();
    in.readShort();
  }
  
  public void readImages(DataInputStream in, String dir)
          throws IOException {
    boolean write = false;
    if (dir != null && dir.length() > 0) {
      write = true;
    }
    skipUnnecessary(in);
            
    byte hc = in.readByte();
    byte nImages = in.readByte();

    System.out.println("The amount of different image headers is - hc: " + hc);      
    System.out.println("The amount of different images is - nImages: " + nImages);

    byte[][] header = new byte[hc][];
    int[] icList = new int[hc];
    byte[][] img = new byte[nImages][];
    int n = 0;

    DataOutputStream out = null;
    for (int cnt = 0; cnt < hc; cnt++) {
      header[cnt] = new byte[in.readShort()];
      in.read(header[cnt]);
      System.out.println("Read header with number - cnt: " + cnt);
      if (write) {
        System.out.print("Writing header " + cnt + " ... ");
        out = new DataOutputStream(new FileOutputStream(dir + "header" + cnt + ".bin"));
        out.write(header[cnt]);
        out.close();
        System.out.println("done!");
      }
      icList[cnt] = in.readByte();
      System.out.println("The amount of different images with this header is "
                         + "- icList[cnt]: " + icList[cnt]);

      for (int i = 0; i < icList[cnt]; i++, n++) {
        img[n] = new byte[in.readShort()];
        in.read(img[n]);
        if (write) {
          System.out.print("Writing data of image " + n + " ... ");
          out = new DataOutputStream(new FileOutputStream(dir + "data" + n + ".bin"));
          out.write(img[n]);
          out.close();
          System.out.println("done!");
          
          System.out.print("Writing image " + n + " ... ");
          out = new DataOutputStream(new FileOutputStream(dir + "img" + n + ".gif"));
          out.write(header[cnt]);
          out.write(img[n]);
          out.close();
          System.out.println("done!");
        }
      }
    }
    in.readShort();
  }
  
  public void readSysStrings(DataInputStream in, String dir)
          throws IOException {
    boolean write = false;
    if (dir != null && dir.length() > 0) {
      write = true;
    }
    readImages(in, null);
            
    int j = in.readShort();
    System.out.println("The amount of \"Sys\"-Strings - j: " + j);

    
    DataOutputStream out = null;
    if (write) {
      out = new DataOutputStream(new FileOutputStream(dir + "sysstrings_jp.bin"));
    }
    byte[] buf = null;
    for (int i = 0; i < j; i++) {
      buf = new byte[in.readByte()];
      in.read(buf);
      if (write) {
        System.out.print("Writing \"Sys\"-String " + i + " ... ");
        out.write(("" + i + ":").getBytes());
        out.write(buf);
        out.write("\r\n".getBytes());
        System.out.println("done!");
      }
    }
    if (write) {
      out.close();
    }
  }
  
  public void readAniPat(DataInputStream in, String dir) throws IOException {
    boolean write = false;
    if (dir != null && dir.length() > 0) {
      write = true;
    }
    readSysStrings(in, null);
    
    DataOutputStream out = null;
    if (write) {
      out = new DataOutputStream(new FileOutputStream(dir + "anipat.bin"));
    }
    byte b = -1;
    for (int j = 0; j < 12; j++) {
      for (int i = 0; i < 4; i++) {
        b = in.readByte();
        System.out.println("AniPat[" + j + "][" + i + "]: " + b);
        if (write) {
          out.writeByte(b);
        }
      }
    }
    if (write) {
      out.close();
    }
  }
  
  public void readAniWait(DataInputStream in, String dir) throws IOException {
    boolean write = false;
    if (dir != null && dir.length() > 0) {
      write = true;
    }
    readAniPat(in, null);
    
    DataOutputStream out = null;
    if (write) {
      out = new DataOutputStream(new FileOutputStream(dir + "aniwait.bin"));
    }
    byte b = -1;
    for (int i = 0; i < 11; i++) {
      b = in.readByte();
      System.out.println("AniWait[" + i + "]: " + b);
      if (write) {
        out.writeByte(b);
      }
    }
    if (write) {
      out.close();
    }
  }
  
  public void readAniMax(DataInputStream in, String dir) throws IOException {
    boolean write = false;
    if (dir != null && dir.length() > 0) {
      write = true;
    }
    readAniWait(in, null);
    
    DataOutputStream out = null;
    if (write) {
      out = new DataOutputStream(new FileOutputStream(dir + "animax.bin"));
    }
    byte b = -1;
    for (int i = 0; i < 12; i++) {
      b = in.readByte();
      System.out.println("AniMax[" + i + "]: " + b);
      if (write) {
        out.writeByte(b);
      }
    }
    if (write) {
      out.close();
    }
  }
  
  public void readCom(DataInputStream in, String dir) throws IOException {
    boolean write = false;
    if (dir != null && dir.length() > 0) {
      write = true;
    }
    readAniMax(in, null);
    
    DataOutputStream out = null;
    if (write) {
      out = new DataOutputStream(new FileOutputStream(dir + "com.bin"));
    }
    int b = -1;
    for (int i = 0; i < 9; i++) {
      b = in.readInt();
      System.out.println("Com[" + i + "]: " + b);
      if (write) {
        out.writeInt(b);
      }
    }
    if (write) {
      out.close();
    }
  }
  
  public void readRankS(DataInputStream in, String dir) throws IOException {
    boolean write = false;
    if (dir != null && dir.length() > 0) {
      write = true;
    }
    readCom(in, null);
    
    DataOutputStream out = null;
    if (write) {
      out = new DataOutputStream(new FileOutputStream(dir + "ranks.bin"));
    }
    short b = -1;
    for (int i = 0; i < 7; i++) {
      b = in.readShort();
      System.out.println("RankS[" + i + "]: " + b);
      if (write) {
        out.writeShort(b);
      }
    }
    if (write) {
      out.close();
    }
  }
  
  public void readRankIndex(DataInputStream in, String dir) throws IOException {
    boolean write = false;
    if (dir != null && dir.length() > 0) {
      write = true;
    }
    readRankS(in, null);
    
    DataOutputStream out = null;
    if (write) {
      out = new DataOutputStream(new FileOutputStream(dir + "rankindex.bin"));
    }
    byte b = -1;
    for (int i = 0; i < 28; i++) {
      b = in.readByte();
      System.out.println("RankIndex[" + i + "]: " + b);
      if (write) {
        out.writeByte(b);
      }
    }
    if (write) {
      out.close();
    }
  }
  
  public void readPallete(DataInputStream in, String dir) throws IOException {
    boolean write = false;
    if (dir != null && dir.length() > 0) {
      write = true;
    }
    readRankIndex(in, null);
    
    DataOutputStream out = null;
    byte[][] b = new byte[8][55];
    for (int i = 0; i < 8; i++) {
      in.read(b[i]);
      if (write) {
        System.out.print("Writing pallete " + i + " ... ");
        out = new DataOutputStream(new FileOutputStream(dir + "pallete" + i + ".bin"));
        out.write(b[i]);
        out.close();
        System.out.println("done!");
      }
    }
  }
  
  public void readFace(DataInputStream in, String dir) throws IOException {
    boolean write = false;
    if (dir != null && dir.length() > 0) {
      write = true;
    }
    readPallete(in, null);
    
    DataOutputStream out = null;
    if (write) {
      out = new DataOutputStream(new FileOutputStream(dir + "face.bin"));
    }
    byte b = -1;
    for (int i = 0; i < 8; i++) {
      b = in.readByte();
      System.out.println("Face[" + i + "]: " + b);
      if (write) {
        out.writeByte(b);
      }
    }
    if (write) {
      out.close();
    }
    
    byte[] buf = new byte[147];
    in.read(buf);
  }
  
  public void readScores(DataInputStream in, String dir) throws IOException {
    readFace(in, null);
    
    System.out.println("Score[0]: " + in.readLong());
    System.out.println("Score[2]: " + in.readLong());
    System.out.println("Score[3]: " + in.readByte());
    System.out.println("Score[5]: " + in.readByte());
    System.out.println("nLastStage: " + in.readInt());
  }
  
  
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {    
    String file = "C:\\USR\\WORK\\SkiJumping\\resources\\_original\\JMP.spf";    
    String dir = "C:\\USR\\WORK\\SkiJumping\\resources\\misc\\";
    
    try {
    /*
      DataInputStream in = new DataInputStream(new FileInputStream(file));
      
      ResReader r = new ResReader();
      //r.readPallete(in, dir);
      r.readImages(in, dir);
      //r.readFace(in, dir);
      
      //for (int i = 0; i < 8; i++) {
      //  r.writeImage("E:\\Tennis\\resources\\images\\4bit\\header\\pallete" + i + ".bin",
      //               "E:\\Tennis\\resources\\images\\4bit\\data\\data47.bin",
      //               "E:\\Tennis\\resources\\images\\4bit\\gif\\pallete" + i + "_data47.gif");
      //}

      */

      String[] 	Sys;
      byte buf[];  
      byte 		img[][];
      int[] 	icList;
      byte 	header[][];
      int i, j, n, hc;
      
      Sys = new String[5 + 106];
      
      
      DataInputStream in = new DataInputStream(new FileInputStream(file));      
      DataOutputStream out = new DataOutputStream(new FileOutputStream("C:\\USR\\WORK\\SkiJumping\\resources\\JMP_out.spf"));

			in.readInt();
			in.readInt();

			hc = in.readShort();

      // Load in-game images

			hc = in.readByte();
      System.out.println("hc: " +hc);
      //////out.write(hc);
			header = new byte[hc][];
			icList = new int[hc];

			i = in.readByte();      
      System.out.println("i: " +i);
      //////out.write(i);

			img = new byte[i][];
			n = 0;

			for (j = 0; j < hc; j++)
			{
				System.gc();
				i = in.readShort();
        //////out.writeShort(i);
				header[j] = new byte[i];
				in.read(header[j]);
        //////out.write(header[j], 0, header[j].length);
				icList[j] = in.readByte();
        //////out.write(icList[j]);

				for (i=0; i<icList[j]; i++)	{
					System.gc();
          int x = in.readShort();
					img[n] = new byte[x];
          //img[n] = new byte[in.readShort()];
          //////out.writeShort(x);
					in.read(img[n++]);
          //////out.write(img[n-1], 0, img[n-1].length);
				}
			}

			in.readShort();

			// Load in-game strings

      System.out.println("reading strings...");

			j = in.readShort();
      //out.writeShort(j);

      int x = 0;

			for (i = 0; i < j; i++) {
				System.gc();
        x = in.readByte();
        //out.write(x);
				buf = new byte[x];
				in.read(buf);
        //out.write(buf, 0, buf.length);
				System.gc();
				Sys[5 + i] = new String(buf);
				//System.out.println("Sys[" + (5+i) + "] = " + Sys[5+i]);
        //Sys[5 + i] = new String("{STRING_" + (5+i) + "}");
			}

      // Load game variables initial values (146 bytes)
      
      int bytes = 146;
      System.out.println("Reading " + bytes + " bytes...");
      for( i = 0; i < bytes; i++ ) {
        out.write(in.readByte());
        //System.out.println("Byte [" + i + "] = " + b);
      }

      out.close();
            
      in.close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -