Camtech075

TC Helper source

Oct 2nd, 2011
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import javax.swing.*;
  5.  
  6. public class EATileChangeHelper {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. new RunFrame("Made by Cam");
  11.  
  12. }
  13.  
  14. }
  15.  
  16. class RunFrame extends JFrame implements ActionListener {
  17.  
  18. JTextField whichFile,howWideMap,howTallMap,topRightX,topRightY,columns,rows;
  19.  
  20. public RunFrame(String s) {
  21.  
  22. super(s);
  23.  
  24. Container contentPane = getContentPane();
  25. setLayout(new GridLayout(8,2));
  26.  
  27. JLabel findMARFile = new JLabel("MAR file (see readme): ");
  28. JLabel setMapWidth = new JLabel("How wide is the map? ");
  29. JLabel setMapHeight = new JLabel("How tall is the map? ");
  30. JLabel setX1 = new JLabel("Top right X coordinate? ");
  31. JLabel setY1 = new JLabel("Top right Y coordinate? ");
  32. JLabel setColumns = new JLabel("How many columns (width)?");
  33. JLabel setRows = new JLabel("How many rows (height)? ");
  34. JLabel placeholder = new JLabel(" ");
  35.  
  36. whichFile = new JTextField(15);
  37. howWideMap = new JTextField(2);
  38. howTallMap = new JTextField(2);
  39. topRightX = new JTextField(2);
  40. topRightY = new JTextField(2);
  41. columns = new JTextField(2);
  42. rows = new JTextField(2);
  43.  
  44. JButton go = new JButton("Start!");
  45. go.addActionListener(this);
  46.  
  47. contentPane.add(findMARFile);
  48. contentPane.add(whichFile);
  49. contentPane.add(setMapWidth);
  50. contentPane.add(howWideMap);
  51. contentPane.add(setMapHeight);
  52. contentPane.add(howTallMap);
  53. contentPane.add(setX1);
  54. contentPane.add(topRightX);
  55. contentPane.add(setY1);
  56. contentPane.add(topRightY);
  57. contentPane.add(setColumns);
  58. contentPane.add(columns);
  59. contentPane.add(setRows);
  60. contentPane.add(rows);
  61. contentPane.add(placeholder);
  62. contentPane.add(go);
  63.  
  64. pack();
  65. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66. setVisible(true);
  67.  
  68. }
  69.  
  70. @Override
  71. public void actionPerformed(ActionEvent e) {
  72.  
  73. JDialog dialogs = new JDialog();
  74. String resultMessage = new String("");
  75.  
  76. try
  77. {
  78.  
  79. String fileName = whichFile.getText();
  80.  
  81. File marFile = new File(fileName);
  82. int mapWidth = Integer.parseInt(howWideMap.getText());
  83. int mapHeight = Integer.parseInt(howTallMap.getText());
  84. int X1 = Integer.parseInt(topRightX.getText());
  85. int Y1 = Integer.parseInt(topRightY.getText());
  86. int changeWidth = Integer.parseInt(columns.getText());
  87. int changeHeight = Integer.parseInt(rows.getText());
  88.  
  89. int begin = Y1*mapWidth + X1;
  90.  
  91. boolean coordinatesTooLarge = begin > mapWidth*mapHeight;
  92. boolean changeTooLarge = changeWidth > mapWidth || changeHeight > mapHeight;
  93. boolean impossibleXY = X1 >= mapWidth || Y1 >= mapHeight;
  94.  
  95. if (coordinatesTooLarge || changeTooLarge || impossibleXY)
  96. {
  97.  
  98. throw new TileChangeException("Something is wrong with your coordinates. Please check them.");
  99.  
  100. }
  101.  
  102. MARHelper marh = new MARHelper(marFile);
  103. marh.ripTCData(begin,mapWidth,changeWidth,changeHeight);
  104. marh.convertToEventFile();
  105.  
  106. dialogs = new JDialog((Frame) null,"Done!",true);
  107. resultMessage = "Finished.";
  108.  
  109. }
  110. catch (IOException ioe)
  111. {
  112.  
  113. //System.out.println("Error: File not found");
  114. dialogs = new JDialog((Frame) null,"Error",true);
  115. resultMessage = "Error: File not found";
  116.  
  117. }
  118. catch (NumberFormatException nfe)
  119. {
  120.  
  121. //System.out.println("That's not a number.");
  122. dialogs = new JDialog((Frame) null,"Error",true);
  123. resultMessage = "Error: That's not a number.";
  124.  
  125. }
  126. catch (NullPointerException npe)
  127. {
  128.  
  129. //System.out.println("Please don't leave fields blank.");
  130. dialogs = new JDialog((Frame) null,"Error",true);
  131. resultMessage = "Error: Blank input fields";
  132.  
  133. }
  134. catch (TileChangeException tce)
  135. {
  136.  
  137. dialogs = new JDialog((Frame) null,"Error",true);
  138. resultMessage = tce.getMessage();
  139.  
  140. }
  141. catch (Exception x)
  142. {
  143.  
  144. //System.out.println("Unknown exception encountered.");
  145. dialogs = new JDialog((Frame) null,"Error",true);
  146. resultMessage = "An unknown error occurred.";
  147.  
  148. }
  149. finally
  150. {
  151.  
  152. JLabel dialogLabel = new JLabel(resultMessage);
  153. dialogs.add(dialogLabel);
  154. dialogs.pack();
  155. dialogs.setVisible(true);
  156.  
  157. }
  158.  
  159. }
  160.  
  161. }
  162.  
  163. class MARHelper {
  164.  
  165. //Contains the actual meat and bones - IE, it does the actual work.
  166.  
  167. File file;
  168. File dataFile;
  169.  
  170. public MARHelper(File file) throws IOException {
  171.  
  172. this.file = file;
  173.  
  174. if (!file.toString().endsWith(".MAR"))
  175. {
  176.  
  177. System.out.println("Not a .MAR file, loser.");
  178.  
  179. }
  180. else
  181. {
  182.  
  183. String fileName = file.toString().replace(".MAR","");
  184.  
  185. fileName += "MapData.bin";
  186.  
  187. DataInputStream dis = new DataInputStream(new FileInputStream(file));
  188. DataOutputStream dos = new DataOutputStream(new FileOutputStream(fileName));
  189.  
  190. short mTile = 0x0000;
  191. int rTile;
  192.  
  193. while (dis.available() > 0)
  194. {
  195.  
  196. mTile = dis.readShort();
  197.  
  198. rTile = ((mTile & 0xFF)*0x100) + ((mTile >>> 8) & 0xFF);
  199. rTile = rTile / 8;
  200.  
  201. rTile = ((rTile & 0xFF)*0x100) + ((rTile >>> 8) & 0xFF);
  202.  
  203. dos.writeShort(rTile);
  204.  
  205. }
  206.  
  207. }
  208.  
  209. }
  210.  
  211. public void ripTCData(int begin, int width, int x, int y) throws IOException {
  212.  
  213. this.dataFile = new File(file.toString().replace(".MAR","").concat("MapData.bin"));
  214.  
  215. DataOutputStream tcRipper = new DataOutputStream(new FileOutputStream("ChangeData.bin"));
  216. DataInputStream dis = new DataInputStream(new FileInputStream(dataFile));
  217.  
  218. int tilesRead = 0;
  219. int totalTiles = x*y;
  220.  
  221. while(tilesRead < totalTiles)
  222. {
  223.  
  224. int columnsRead = 0;
  225.  
  226. while(columnsRead < x)
  227. {
  228.  
  229. int v = dis.readShort();
  230.  
  231. tcRipper.writeShort(v);
  232. ++tilesRead;
  233. ++columnsRead;
  234.  
  235. }
  236.  
  237. dis.skipBytes(width - x);
  238.  
  239. }
  240.  
  241. }
  242.  
  243. public void convertToEventFile() throws IOException {
  244.  
  245. PrintStream ps = new PrintStream("TileChanges.txt");
  246. FileInputStream fis = new FileInputStream(new File("ChangeData.bin"));
  247.  
  248. while (fis.available() != 0)
  249. {
  250.  
  251. int single = fis.read();
  252.  
  253. String halfTile = Integer.toHexString(single).toUpperCase();
  254.  
  255. if (single < 0x10)
  256. {
  257.  
  258. halfTile = "0".concat(halfTile);
  259.  
  260. }
  261.  
  262. ps.print("0x" + halfTile + " ");
  263.  
  264. }
  265.  
  266. }
  267.  
  268. }
  269.  
  270. class TileChangeException extends Exception {
  271.  
  272. public TileChangeException(String s) {
  273.  
  274. super(s);
  275.  
  276. }
  277.  
  278. }
  279.  
Advertisement
Add Comment
Please, Sign In to add comment