Guest User

PJM>BK2 Input Log converter v0.2

a guest
Sep 15th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.42 KB | None | 0 0
  1. /**
  2.  * PJM>BK2 Input converter v0.2
  3.  * Author: CoolKirby
  4.  * Language: Java (with English comments)
  5.  * First created: 9/12/15
  6.  * Last revised: 9/15/15
  7.  * Works with 1 and 2-player PJMs!
  8.  * Does not support resets or changing discs
  9.  * Only supports PSX Controller input for now, as this is the most common PJM setting
  10.  * Supports both DualShock and PSX Controller output, DualShock is default, but Std Controller is more compact
  11. */
  12.  
  13. import java.io.IOException; //Necessary to prevent exceptions
  14. import java.io.FileReader;
  15. import java.io.BufferedReader;
  16. import java.io.FileWriter;
  17. import java.io.PrintWriter;
  18.  
  19. /*
  20. Input formats:
  21. PJM:|Square|Cross|Circle|Triangle|R1|L1|R2|L2|Left|Down|Right|Up|Start|Select|
  22. BK2:|Up|Down|Left|Right|Select|Start|Square|Triangle|Circle|Cross|L1|R1|L2|R2| L3|R3|MODE| <--DualShock only
  23.  
  24. PJM #XO^1234LDRUSs
  25. BK2 UDLRsSQTOXlrLR...
  26. num 0123456789ABCDEFa
  27. */
  28.  
  29. public class PJMConverter{
  30.   public static void main(String[] args) throws IOException{
  31.       try{
  32.         countLinesDS("convert.pjm"); //File to read from (input - PJM)
  33.       }catch(IOException e){
  34.         try{
  35.           countLinesStd("convert_pad.pjm"); //Standard PSX Controller file to read from (input - PJM)
  36.         }catch(IOException ee){
  37.           System.out.println(e.getMessage());
  38.         }
  39.       }
  40.   }
  41.  
  42.   public static void countLinesDS(String path) throws IOException{
  43.     Boolean DualShockOut=true; //In future, maybe check PJM for (rare) existing DualShock input
  44.     countLines(path,DualShockOut); //If integrated into BizHawk, it could set this based on existing PSX settings
  45.   }
  46.  
  47.   public static void countLinesStd(String path) throws IOException{
  48.     Boolean DualShockOut=false;
  49.     countLines(path,DualShockOut);
  50.   }
  51.  
  52.   public static void countLines(String path, Boolean DualShockOut) throws IOException{
  53.       FileReader a = new FileReader(path);
  54.       BufferedReader num = new BufferedReader(a); //Open countLines text buffer
  55.        
  56.       String aLine;
  57.       int numLines=0;
  58.      
  59.       while ((aLine=num.readLine()) != null){ //Count number of lines in the PJM
  60.         numLines++;
  61.       }
  62.       num.close(); //Close countLines text buffer
  63.      
  64.       if(numLines<2){ //Assume error if input file only has one line
  65.         System.out.println("Input PJM is too short! Conversion failed. Please check your file.");
  66.         System.exit(5000);
  67.       }
  68.      
  69.       OpenFile(path,numLines,DualShockOut);
  70.   }
  71.      
  72.   public static void OpenFile(String path, int numLines, Boolean DualShockOut) throws IOException{
  73.       String output="Input Log.txt"; //File to write to (output - BK2 Input Log.txt)
  74.      
  75.       FileReader fr = new FileReader(path); // Loads initial PJM
  76.       BufferedReader reader = new BufferedReader(fr); // Reads whole lines in to speed up script
  77.      
  78.       String[] PJMData = new String[numLines]; // Create the array of PJM input with the calculated # of lines
  79.       int inputStart;
  80.      
  81.       PJMData[0]=reader.readLine(); //Read the first line of the PJM separately
  82.       if(PJMData[0].indexOf('#')==-1) //Find where the input starts
  83.         inputStart=PJMData[0].indexOf('.');
  84.       else
  85.         inputStart=PJMData[0].indexOf('#'); //Failsafe in case Square was pressed on first frame
  86.       PJMData[0]=PJMData[0].substring(inputStart,PJMData[0].length()); //Strip off the PJM header
  87.      
  88.       for(int i=1;i<numLines;i++){ //Read the rest of the PJM starting from frame 2
  89.         PJMData[i]=reader.readLine();
  90.         if(numLines-i==0){
  91.           PJMData[i]=reader.readLine(); //"Catch" the elusive last input and add it to the table
  92.         }
  93.       }
  94.      
  95.       reader.close();
  96.      
  97.       parseFile(PJMData,numLines,path,output,DualShockOut);
  98.   }
  99.  
  100.   public static void parseFile(String[] PJMData, int numLines, String path, String output, Boolean DualShockOut) throws IOException {
  101.     int numBK2Lines=numLines+1;
  102.    
  103.     Boolean P2=false; //initialize as false
  104.    
  105.     if(PJMData[1].charAt(15)=='.') //discreetly checks for Player 2 input without confusing the P1 scanner
  106.       P2=true;
  107.    
  108.     String[] BK2Data = new String[numBK2Lines]; //1 line larger to fit BK2 LogKey comment
  109.     String[] BK2DataP2 = new String[numBK2Lines]; //second array for holding Player 2 input
  110.    
  111.     char[] Button = new char[14]; //array for pressed buttons on an input line
  112.    
  113.     //Add in BizHawk LogKey line for author's convenience
  114.     if(P2){ //If Player 2 input exists, recognize it
  115.       if(DualShockOut) //2 players + DualShock for both
  116.         BK2Data[0]="LogKey:#Disc Select|Open|Close|Reset|#P1 LStick X|P1 LStick Y|P1 RStick X|P1 RStick Y|P1 Up|P1 Down|P1 Left|P1 Right|P1 Select|P1 Start|P1 Square|P1 Triangle|P1 Circle|P1 Cross|P1 L1|P1 R1|P1 L2|P1 R2|P1 L3|P1 R3|P1 MODE|#P2 LStick X|P2 LStick Y|P2 RStick X|P2 RStick Y|P2 Up|P2 Down|P2 Left|P2 Right|P2 Select|P2 Start|P2 Square|P2 Triangle|P2 Circle|P2 Cross|P2 L1|P2 R1|P2 L2|P2 R2|P2 L3|P2 R3|P2 MODE|";
  117.       else //2 players + Standard Controller for both
  118.         BK2Data[0]="LogKey:#Disc Select|Open|Close|Reset|#P1 Up|P1 Down|P1 Left|P1 Right|P1 Select|P1 Start|P1 Square|P1 Triangle|P1 Circle|P1 Cross|P1 L1|P1 R1|P1 L2|P1 R2|#P2 Up|P2 Down|P2 Left|P2 Right|P2 Select|P2 Start|P2 Square|P2 Triangle|P2 Circle|P2 Cross|P2 L1|P2 R1|P2 L2|P2 R2|";
  119.     }else{
  120.       if(DualShockOut) //1 player + DualShock
  121.         BK2Data[0]="LogKey:#Disc Select|Open|Close|Reset|#P1 LStick X|P1 LStick Y|P1 RStick X|P1 RStick Y|P1 Up|P1 Down|P1 Left|P1 Right|P1 Select|P1 Start|P1 Square|P1 Triangle|P1 Circle|P1 Cross|P1 L1|P1 R1|P1 L2|P1 R2|P1 L3|P1 R3|P1 MODE|";
  122.       else //1 player + Standard Controller
  123.         BK2Data[0]="LogKey:#Disc Select|Open|Close|Reset|#P1 Up|P1 Down|P1 Left|P1 Right|P1 Select|P1 Start|P1 Square|P1 Triangle|P1 Circle|P1 Cross|P1 L1|P1 R1|P1 L2|P1 R2|";
  124.     }
  125.     for(int i=1,p=0;i<numBK2Lines;i++,p++){ // The big parser! Player 1 Input
  126.       BK2Data[i]="";
  127.       for(int b=0;b<14;b++) // The default blank Input Log line
  128.         Button[b]='.';
  129.      
  130.       if(PJMData[p].charAt(0)=='#') //P1 Square
  131.         Button[6]='Q';
  132.       if(PJMData[p].charAt(1)=='X') //P1 Cross
  133.         Button[9]='X';
  134.       if(PJMData[p].charAt(2)=='O') //P1 Circle
  135.         Button[8]='O';
  136.       if(PJMData[p].charAt(3)=='^') //P1 Triangle
  137.         Button[7]='T';
  138.       if(PJMData[p].charAt(4)=='1') //P1 R1
  139.         Button[11]='r';
  140.       if(PJMData[p].charAt(5)=='2') //P1 L1
  141.         Button[10]='l';
  142.       if(PJMData[p].charAt(6)=='3') //P1 R2
  143.         Button[13]='R';
  144.       if(PJMData[p].charAt(7)=='4') //P1 L2
  145.         Button[12]='L';
  146.       if(PJMData[p].charAt(8)=='L') //P1 D-Pad Left
  147.         Button[2]='L';
  148.       if(PJMData[p].charAt(9)=='D') //P1 D-Pad Down
  149.         Button[1]='D';
  150.       if(PJMData[p].charAt(10)=='R') //P1 D-Pad Right
  151.         Button[3]='R';
  152.       if(PJMData[p].charAt(11)=='U') //P1 D-Pad Up
  153.         Button[0]='U';
  154.       if(PJMData[p].charAt(12)=='S') //P1 Start
  155.         Button[5]='S';
  156.       if(PJMData[p].charAt(13)=='s') //P1 Select
  157.         Button[4]='s';
  158.       for(int s=0;s<14;s++) // Set buttons to input line positions
  159.         BK2Data[i]=BK2Data[i]+Button[s];
  160.      
  161.       if(P2){ //If Player 2 input exists, parse it!
  162.         BK2DataP2[i]="";
  163.         for(int b=0;b<14;b++) // The default blank Input Log line
  164.           Button[b]='.';
  165.        
  166.         if(PJMData[p].charAt(15)=='#') //P2 Square
  167.           Button[6]='Q';
  168.         if(PJMData[p].charAt(16)=='X') //P2 Cross
  169.           Button[9]='X';
  170.         if(PJMData[p].charAt(17)=='O') //P2 Circle
  171.           Button[8]='O';
  172.         if(PJMData[p].charAt(18)=='^') //P2 Triangle
  173.           Button[7]='T';
  174.         if(PJMData[p].charAt(19)=='1') //P2 R1
  175.           Button[11]='r';
  176.         if(PJMData[p].charAt(20)=='2') //P2 L1
  177.           Button[10]='l';
  178.         if(PJMData[p].charAt(21)=='3') //P2 R2
  179.           Button[13]='R';
  180.         if(PJMData[p].charAt(22)=='4') //P2 L2
  181.           Button[12]='L';
  182.         if(PJMData[p].charAt(23)=='L') //P2 D-Pad Left
  183.           Button[2]='L';
  184.         if(PJMData[p].charAt(24)=='D') //P2 D-Pad Down
  185.           Button[1]='D';
  186.         if(PJMData[p].charAt(25)=='R') //P2 D-Pad Right
  187.           Button[3]='R';
  188.         if(PJMData[p].charAt(26)=='U') //P2 D-Pad Up
  189.           Button[0]='U';
  190.         if(PJMData[p].charAt(27)=='S') //P2 Start
  191.           Button[5]='S';
  192.         if(PJMData[p].charAt(28)=='s') //P2 Select
  193.           Button[4]='s';
  194.         for(int s=0;s<14;s++) // Set buttons to input line positions
  195.           BK2DataP2[i]=BK2DataP2[i]+Button[s];
  196.       }
  197.     }
  198.      /*
  199.      PJM:|Sq|Cros|Circ|Trian|....R1|...L1|....R2|......L2|..Left|.Down|Rt|Up|St|Se|
  200.            6    9    8     7     11    10     13       12      2     1  3  0  5  4
  201.            #    X    O     ^      1     2      3        4      L     D  R  U  S  s
  202.      BK2:|Up|Down|Left|Right|Select|Start|Square|Triangle|Circle|Cross|L1|R1|L2|R2|L3|R3|MODE| <--DualShock only
  203.            0    1    2     3      4     5      6        7      8     9 10 11 12 13 14 15   16
  204.            U    D    L     R      s     S      Q        T      O     X  l  r  L  R  .  .    .
  205.      
  206.       PJM ..............||0|    BK2  |   -1,...|  128,  128,  128,  128,.................|
  207.       num 0123456789ABCDEFa  output  |   -1,...|  128,  128,  128,  128, + .............. + ...|
  208.      */
  209.    
  210.     System.out.println(numBK2Lines+" lines in finished BK2");
  211.    
  212.     //Write File
  213.     FileWriter write = new FileWriter(output);
  214.     PrintWriter print_line = new PrintWriter(write); //use PrintWriter to print "%n" character on both Windows and Unix
  215.    
  216.     //Output all BK2 Input, including beginning and ending lines
  217.     print_line.printf("%s"+"%n",BK2Data[0]); //First line is LogKey, no appended data needed
  218.    
  219.     for(int l=1;l<=numLines;l++){
  220.       if(P2){
  221.         if(DualShockOut) //2 players + DualShock
  222.           print_line.printf("|   -1,...|  128,  128,  128,  128,"+"%s"+"...|  128,  128,  128,  128,"+"%s"+"...|"+"%n",BK2Data[l],BK2DataP2[l]);
  223.         else //2 players + Standard Controller
  224.           print_line.printf("|   -1,...|"+"%s"+"|"+"%s"+"|"+"%n",BK2Data[l],BK2DataP2[l]);
  225.       }else{
  226.         if(DualShockOut) //1 player + DualShock
  227.           print_line.printf("|   -1,...|  128,  128,  128,  128,"+"%s"+"...|"+"%n",BK2Data[l]);
  228.         else //1 player + Standard Controller
  229.           print_line.printf("|   -1,...|"+"%s"+"|"+"%n",BK2Data[l]);
  230.       }
  231.     }
  232.    
  233.     print_line.close(); // Close file printing stream
  234.     System.out.println("Done! Check out Input Log.txt");
  235.   }
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment