Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import javax.swing.*;
- public class EATileChangeHelper {
- public static void main(String[] args) {
- new RunFrame("Made by Cam");
- }
- }
- class RunFrame extends JFrame implements ActionListener {
- JTextField whichFile,howWideMap,howTallMap,topRightX,topRightY,columns,rows;
- public RunFrame(String s) {
- super(s);
- Container contentPane = getContentPane();
- setLayout(new GridLayout(8,2));
- JLabel findMARFile = new JLabel("MAR file (see readme): ");
- JLabel setMapWidth = new JLabel("How wide is the map? ");
- JLabel setMapHeight = new JLabel("How tall is the map? ");
- JLabel setX1 = new JLabel("Top right X coordinate? ");
- JLabel setY1 = new JLabel("Top right Y coordinate? ");
- JLabel setColumns = new JLabel("How many columns (width)?");
- JLabel setRows = new JLabel("How many rows (height)? ");
- JLabel placeholder = new JLabel(" ");
- whichFile = new JTextField(15);
- howWideMap = new JTextField(2);
- howTallMap = new JTextField(2);
- topRightX = new JTextField(2);
- topRightY = new JTextField(2);
- columns = new JTextField(2);
- rows = new JTextField(2);
- JButton go = new JButton("Start!");
- go.addActionListener(this);
- contentPane.add(findMARFile);
- contentPane.add(whichFile);
- contentPane.add(setMapWidth);
- contentPane.add(howWideMap);
- contentPane.add(setMapHeight);
- contentPane.add(howTallMap);
- contentPane.add(setX1);
- contentPane.add(topRightX);
- contentPane.add(setY1);
- contentPane.add(topRightY);
- contentPane.add(setColumns);
- contentPane.add(columns);
- contentPane.add(setRows);
- contentPane.add(rows);
- contentPane.add(placeholder);
- contentPane.add(go);
- pack();
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- JDialog dialogs = new JDialog();
- String resultMessage = new String("");
- try
- {
- String fileName = whichFile.getText();
- File marFile = new File(fileName);
- int mapWidth = Integer.parseInt(howWideMap.getText());
- int mapHeight = Integer.parseInt(howTallMap.getText());
- int X1 = Integer.parseInt(topRightX.getText());
- int Y1 = Integer.parseInt(topRightY.getText());
- int changeWidth = Integer.parseInt(columns.getText());
- int changeHeight = Integer.parseInt(rows.getText());
- int begin = Y1*mapWidth + X1;
- boolean coordinatesTooLarge = begin > mapWidth*mapHeight;
- boolean changeTooLarge = changeWidth > mapWidth || changeHeight > mapHeight;
- boolean impossibleXY = X1 >= mapWidth || Y1 >= mapHeight;
- if (coordinatesTooLarge || changeTooLarge || impossibleXY)
- {
- throw new TileChangeException("Something is wrong with your coordinates. Please check them.");
- }
- MARHelper marh = new MARHelper(marFile);
- marh.ripTCData(begin,mapWidth,changeWidth,changeHeight);
- marh.convertToEventFile();
- dialogs = new JDialog((Frame) null,"Done!",true);
- resultMessage = "Finished.";
- }
- catch (IOException ioe)
- {
- //System.out.println("Error: File not found");
- dialogs = new JDialog((Frame) null,"Error",true);
- resultMessage = "Error: File not found";
- }
- catch (NumberFormatException nfe)
- {
- //System.out.println("That's not a number.");
- dialogs = new JDialog((Frame) null,"Error",true);
- resultMessage = "Error: That's not a number.";
- }
- catch (NullPointerException npe)
- {
- //System.out.println("Please don't leave fields blank.");
- dialogs = new JDialog((Frame) null,"Error",true);
- resultMessage = "Error: Blank input fields";
- }
- catch (TileChangeException tce)
- {
- dialogs = new JDialog((Frame) null,"Error",true);
- resultMessage = tce.getMessage();
- }
- catch (Exception x)
- {
- //System.out.println("Unknown exception encountered.");
- dialogs = new JDialog((Frame) null,"Error",true);
- resultMessage = "An unknown error occurred.";
- }
- finally
- {
- JLabel dialogLabel = new JLabel(resultMessage);
- dialogs.add(dialogLabel);
- dialogs.pack();
- dialogs.setVisible(true);
- }
- }
- }
- class MARHelper {
- //Contains the actual meat and bones - IE, it does the actual work.
- File file;
- File dataFile;
- public MARHelper(File file) throws IOException {
- this.file = file;
- if (!file.toString().endsWith(".MAR"))
- {
- System.out.println("Not a .MAR file, loser.");
- }
- else
- {
- String fileName = file.toString().replace(".MAR","");
- fileName += "MapData.bin";
- DataInputStream dis = new DataInputStream(new FileInputStream(file));
- DataOutputStream dos = new DataOutputStream(new FileOutputStream(fileName));
- short mTile = 0x0000;
- int rTile;
- while (dis.available() > 0)
- {
- mTile = dis.readShort();
- rTile = ((mTile & 0xFF)*0x100) + ((mTile >>> 8) & 0xFF);
- rTile = rTile / 8;
- rTile = ((rTile & 0xFF)*0x100) + ((rTile >>> 8) & 0xFF);
- dos.writeShort(rTile);
- }
- }
- }
- public void ripTCData(int begin, int width, int x, int y) throws IOException {
- this.dataFile = new File(file.toString().replace(".MAR","").concat("MapData.bin"));
- DataOutputStream tcRipper = new DataOutputStream(new FileOutputStream("ChangeData.bin"));
- DataInputStream dis = new DataInputStream(new FileInputStream(dataFile));
- int tilesRead = 0;
- int totalTiles = x*y;
- while(tilesRead < totalTiles)
- {
- int columnsRead = 0;
- while(columnsRead < x)
- {
- int v = dis.readShort();
- tcRipper.writeShort(v);
- ++tilesRead;
- ++columnsRead;
- }
- dis.skipBytes(width - x);
- }
- }
- public void convertToEventFile() throws IOException {
- PrintStream ps = new PrintStream("TileChanges.txt");
- FileInputStream fis = new FileInputStream(new File("ChangeData.bin"));
- while (fis.available() != 0)
- {
- int single = fis.read();
- String halfTile = Integer.toHexString(single).toUpperCase();
- if (single < 0x10)
- {
- halfTile = "0".concat(halfTile);
- }
- ps.print("0x" + halfTile + " ");
- }
- }
- }
- class TileChangeException extends Exception {
- public TileChangeException(String s) {
- super(s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment