Guest User

PJM>BK2 Input Log converter v0.3

a guest
Sep 16th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.23 KB | None | 0 0
  1. /**
  2.  * PJM>BK2 Input converter v0.3
  3.  * Author: CoolKirby
  4.  * Language: Java (Comments in English)
  5.  * First created: 9/12/15
  6.  * Last revised: 9/16/15
  7.  * Works with 1 and 2-player PJMs!
  8.  * Works with both DualShock and PSX Controller PJMs! (though most are PSX Controller only)
  9.  * Supports both DualShock and PSX Controller output: use 'convert.pjm' for DS, 'convert_pad.pjm' for PSX Controller
  10.  *
  11.  * Recent Changes: disc 1 instead of -1, Support for native DualShock PJMs, Disc No./Reset/Disc Open/Disc Close, catch and correct most malformed PJMs
  12. */
  13.  
  14. import java.io.IOException; //Necessary to prevent exceptions
  15. import java.io.FileReader;
  16. import java.io.BufferedReader;
  17. import java.io.FileWriter;
  18. import java.io.PrintWriter;
  19.  
  20. /*
  21. Input formats:
  22. PJM #XO^1234LDRUS..s
  23. BK2 UDLRsSQTOXlrLR...
  24. num 0123456789ABCDEFa
  25.  
  26.      PJM:|Sq|Cros|Circ|Trian|....R1|...L1|....R2|......L2|..Left|.Down|Rt|Up|St|*L3|*R3|Sel|
  27.            6    9    8     7     11    10     13       12      2     1  3  0  5  14  15   4
  28.            #    X    O     ^      1     2      3        4      L     D  R  U  S   L   R   s
  29.      BK2:|Up|Down|Left|Right|Select|Start|Square|Triangle|Circle|Cross|L1|R1|L2| R2|*L3|*R3|*MODE| *DualShock only
  30.            0    1    2     3      4     5      6        7      8     9 10 11 12  13  14  15    16
  31.            U    D    L     R      s     S      Q        T      O     X  l  r  L   R   <   >     M
  32.      
  33.       PJM ..............||0|    BK2  |   -1,...|  128,  128,  128,  128,.................|
  34. */
  35.  
  36. public class PJMConverter{
  37.   public static void main(String[] args) throws IOException{
  38.       try{
  39.         countLinesDS("convert.pjm"); //File to read from (input - PJM)
  40.       }catch(IOException e){
  41.         try{ //Nested try-catch for alternate read option
  42.           countLinesStd("convert_pad.pjm"); //Standard PSX Controller file to read from (input - PJM)
  43.         }catch(IOException ee){
  44.           System.out.println(e.getMessage());
  45.         }
  46.       }
  47.   }
  48.  
  49.   public static void countLinesDS(String path) throws IOException{
  50.     Boolean DualShockOut=true;
  51.     countLines(path,DualShockOut); //If integrated into BizHawk, it could set this based on existing PSX settings
  52.   }
  53.  
  54.   public static void countLinesStd(String path) throws IOException{
  55.     Boolean DualShockOut=false;
  56.     countLines(path,DualShockOut);
  57.   }
  58.  
  59.   public static void countLines(String path, Boolean DualShockOut) throws IOException{
  60.       FileReader a = new FileReader(path);
  61.       BufferedReader num = new BufferedReader(a); //Open countLines text buffer
  62.        
  63.       String aLine;
  64.       int numLines=0;
  65.      
  66.       while ((aLine=num.readLine()) != null){ //Count number of lines in the PJM
  67.         numLines++;
  68.       }
  69.       num.close(); //Close countLines text buffer
  70.      
  71.       if(numLines<2){ //Assume error if input file has 0 or 1 lines
  72.         System.out.println("Input PJM is too short! Conversion failed. Please check your file.");
  73.         System.exit(5000);
  74.       }
  75.      
  76.       OpenFile(path,numLines,DualShockOut);
  77.   }
  78.      
  79.   public static void OpenFile(String path, int numLines, Boolean DualShockOut) throws IOException{
  80.       String output="Input Log.txt"; //File to write to (output - BK2)
  81.      
  82.       FileReader fr = new FileReader(path); // Loads initial PJM
  83.       BufferedReader reader = new BufferedReader(fr); // Reads whole lines in to speed up script
  84.      
  85.       String[] PJMData = new String[numLines]; // Create the array of PJM input with the calculated # of lines
  86.       int inputStart; //will contain index of first . or # char
  87.      
  88.       PJMData[0]=reader.readLine(); //Read the first line of the PJM separately
  89.       if(PJMData[0].indexOf('#')==-1) //Find where the input starts
  90.         inputStart=PJMData[0].indexOf('.');
  91.       else
  92.         inputStart=PJMData[0].indexOf('#'); //Failsafe in case Square was pressed on first frame
  93.       PJMData[0]=PJMData[0].substring(inputStart,PJMData[0].length()); //Strip off the PJM header
  94.      
  95.       for(int i=1;i<numLines;i++){ //Read the rest of the PJM starting from frame 2
  96.         PJMData[i]=reader.readLine();
  97.         if(numLines-i==0){
  98.           PJMData[i]=reader.readLine(); //"Catch" the elusive last input line and add it to the table
  99.         }
  100.       }
  101.      
  102.       reader.close();
  103.      
  104.       parseFile(PJMData,numLines,path,output,DualShockOut);
  105.   }
  106.  
  107.   public static void parseFile(String[] PJMData, int numLines, String path, String output, Boolean DualShockOut) throws IOException {
  108.     int numBK2Lines=numLines+1;
  109.    
  110.     Boolean P2=false,DSInput=false; //Player 2 and DualShock off unless turned on below
  111.     int numDots, P2do=0; //number of input spaces (dots) for PSX Controller; offset of P2 DualShock input dots
  112.     int numTrayOps=0, discNum=1, specNumPos=16; //number of tray operations, % 2 R0 is closed tray, R1 is open; disc #; |1| position
  113.     char SpecReset,SpecOpen,SpecClose; //default values for "special" characters preceding input
  114.    
  115.     if(PJMData[1].charAt(14)=='.'){ //discreetly checks for DS and Player 2 input without confusing the P1 scanner
  116.       DSInput=true; //1 player, DualShock detected
  117.       specNumPos=34;
  118.       if(PJMData[1].charAt(33)=='.'||PJMData[1].charAt(33)=='#'){
  119.         P2=true; //2 players, DualShock detected
  120.         P2do=18;
  121.         DSInput=true;
  122.         specNumPos=66;
  123.       }
  124.     }else{
  125.       if(PJMData[1].charAt(15)=='.'||PJMData[1].charAt(15)=='#'){
  126.         P2=true; //2 players, Standard Controller
  127.         specNumPos=30;
  128.       }
  129.     }
  130.    
  131.     if(DualShockOut) //If user wants DualShock input from a regular PJM, blank the last three dots
  132.       numDots=17;
  133.     else //The opposite: if user wants PSX Controller input from a DS PJM, remove the DS-specific input
  134.       numDots=14;
  135.    
  136.     String[] BK2Data = new String[numBK2Lines]; //1 line larger to fit BK2 LogKey comment
  137.     String[] BK2DataP2 = new String[numBK2Lines]; //second array for holding Player 2 input
  138.    
  139.     char[] Button = new char[numDots]; //array for pressed buttons on an input line
  140.     String[] SpecButton = new String[numBK2Lines]; //array for Disc #, Reset, Tray Open & Close
  141.    
  142.     String[] SpecJoystick = new String[numBK2Lines]; //array for DualShock joystick values (0-255)
  143.     String[] SpecJoystickP2 = new String[numBK2Lines]; //second array for holding Player 2 input
  144.    
  145.     //Add in BizHawk LogKey line for author's convenience
  146.     if(P2){ //If Player 2 input exists, recognize it
  147.       if(DualShockOut) //2 players + DualShock for both
  148.         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|";
  149.       else //2 players + Standard Controller for both
  150.         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|";
  151.     }else{
  152.       if(DualShockOut) //1 player + DualShock
  153.         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|";
  154.       else //1 player + Standard Controller
  155.         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|";
  156.     }
  157.     for(int i=1,p=0;i<numBK2Lines;i++,p++){ // The big parser! Player 1 Input
  158.       BK2Data[i]="";
  159.       for(int b=0;b<numDots;b++) // The default blank Input Log line
  160.         Button[b]='.';
  161.       SpecReset='.'; SpecOpen='.'; SpecClose='.'; //Re-initialize Special characters
  162.      
  163.       if(PJMData[p].charAt(specNumPos)=='1') //Reset Button
  164.         SpecReset='!';
  165.       if(PJMData[p].charAt(specNumPos)=='2'){ //Determine if PJM opened or closed the disc drive
  166.         if(numTrayOps%2==0)
  167.           SpecOpen='!'; //first, third, etc. calls are treated as opening drive
  168.         else{
  169.           SpecClose='!'; //second, fourth, etc. calls are treated as closing drive
  170.           discNum++; //insert the next disc
  171.         }
  172.         numTrayOps++;
  173.       }
  174.       SpecButton[i]="|    "+discNum+','+SpecReset+SpecOpen+SpecClose+'|'; //assemble beginning spec characters
  175.      
  176.       if(numDots==17){ //DualShock Joystick values
  177.         if(DSInput==false||PJMData[p].charAt(19)<'0'||PJMData[p].charAt(19)>'9')
  178.           SpecJoystick[i]="128,  128,  128,  128,"; //if no Stick value listed, assume malformed PJM and prefill values with defaults
  179.         else //P1 LStick and RStick positions
  180.           SpecJoystick[i]=PJMData[p].substring(17,20)+",  "+PJMData[p].substring(21,24)+",  "+PJMData[p].substring(25,28)+",  "+PJMData[p].substring(29,32)+",";
  181.         if(P2){
  182.           if(DSInput==false||PJMData[p].charAt(52)<'0'||PJMData[p].charAt(52)>'9') //try 19 next*
  183.             SpecJoystickP2[i]="128,  128,  128,  128,"; //if no Stick value listed, assume malformed PJM and prefill values with defaults
  184.           else //P2 LStick and RStick positions
  185.             SpecJoystickP2[i]=PJMData[p].substring(50,53)+",  "+PJMData[p].substring(54,57)+",  "+PJMData[p].substring(58,61)+",  "+PJMData[p].substring(62,65)+",";
  186.         }
  187.       }
  188.      
  189.       if(PJMData[p].charAt(0)=='#') //P1 Square
  190.         Button[6]='Q';
  191.       if(PJMData[p].charAt(1)=='X') //P1 Cross
  192.         Button[9]='X';
  193.       if(PJMData[p].charAt(2)=='O') //P1 Circle
  194.         Button[8]='O';
  195.       if(PJMData[p].charAt(3)=='^') //P1 Triangle
  196.         Button[7]='T';
  197.       if(PJMData[p].charAt(4)=='1') //P1 R1
  198.         Button[11]='r';
  199.       if(PJMData[p].charAt(5)=='2') //P1 L1
  200.         Button[10]='l';
  201.       if(PJMData[p].charAt(6)=='3') //P1 R2
  202.         Button[13]='R';
  203.       if(PJMData[p].charAt(7)=='4') //P1 L2
  204.         Button[12]='L';
  205.       if(PJMData[p].charAt(8)=='L') //P1 D-Pad Left
  206.         Button[2]='L';
  207.       if(PJMData[p].charAt(9)=='D') //P1 D-Pad Down
  208.         Button[1]='D';
  209.       if(PJMData[p].charAt(10)=='R') //P1 D-Pad Right
  210.         Button[3]='R';
  211.       if(PJMData[p].charAt(11)=='U') //P1 D-Pad Up
  212.         Button[0]='U';
  213.       if(PJMData[p].charAt(12)=='S') //P1 Start
  214.         Button[5]='S';
  215.       if(DSInput){ //DualShock specific
  216.         if(DualShockOut){ //Only output DualShock buttons if DSOut=true
  217.           if(PJMData[p].charAt(13)=='L') //P1 L3
  218.             Button[14]='<';
  219.           if(PJMData[p].charAt(14)=='R') //P1 R3
  220.             Button[15]='>';
  221.         }
  222.         if(PJMData[p].charAt(15)=='s') //P1 Select from DualShock PJM
  223.           Button[4]='s';
  224.       }else{
  225.         if(PJMData[p].charAt(13)=='s') //P1 Select from PSX Controller PJM
  226.           Button[4]='s';
  227.       }
  228.       for(int s=0;s<numDots;s++) // Set buttons to input line positions
  229.         BK2Data[i]=BK2Data[i]+Button[s];
  230.      
  231.       if(P2){ //If Player 2 input exists, parse it!
  232.         BK2DataP2[i]="";
  233.         for(int b=0;b<numDots;b++) // The default blank Input Log line
  234.           Button[b]='.';
  235.        
  236.         if(PJMData[p].charAt(P2do+15)=='#') //P2 Square
  237.           Button[6]='Q';
  238.         if(PJMData[p].charAt(P2do+16)=='X') //P2 Cross
  239.           Button[9]='X';
  240.         if(PJMData[p].charAt(P2do+17)=='O') //P2 Circle
  241.           Button[8]='O';
  242.         if(PJMData[p].charAt(P2do+18)=='^') //P2 Triangle
  243.           Button[7]='T';
  244.         if(PJMData[p].charAt(P2do+19)=='1') //P2 R1
  245.           Button[11]='r';
  246.         if(PJMData[p].charAt(P2do+20)=='2') //P2 L1
  247.           Button[10]='l';
  248.         if(PJMData[p].charAt(P2do+21)=='3') //P2 R2
  249.           Button[13]='R';
  250.         if(PJMData[p].charAt(P2do+22)=='4') //P2 L2
  251.           Button[12]='L';
  252.         if(PJMData[p].charAt(P2do+23)=='L') //P2 D-Pad Left
  253.           Button[2]='L';
  254.         if(PJMData[p].charAt(P2do+24)=='D') //P2 D-Pad Down
  255.           Button[1]='D';
  256.         if(PJMData[p].charAt(P2do+25)=='R') //P2 D-Pad Right
  257.           Button[3]='R';
  258.         if(PJMData[p].charAt(P2do+26)=='U') //P2 D-Pad Up
  259.           Button[0]='U';
  260.         if(PJMData[p].charAt(P2do+27)=='S') //P2 Start
  261.           Button[5]='S';
  262.         if(DSInput){ //DualShock specific
  263.           if(DualShockOut){ //Only output DualShock buttons if DSOut=true
  264.             if(PJMData[p].charAt(P2do+28)=='L') //P2 L3
  265.               Button[14]='<';
  266.             if(PJMData[p].charAt(P2do+29)=='R') //P2 R3
  267.               Button[15]='>';
  268.           }
  269.           if(PJMData[p].charAt(P2do+30)=='s') //P2 Select from DualShock PJM
  270.             Button[4]='s';
  271.         }else{
  272.           if(PJMData[p].charAt(28)=='s') //P2 Select from PSX Controller PJM
  273.             Button[4]='s';
  274.         }
  275.         for(int s=0;s<numDots;s++) // Set buttons to input line positions
  276.           BK2DataP2[i]=BK2DataP2[i]+Button[s];
  277.       }
  278.     }
  279.    
  280.     System.out.println(numBK2Lines+" lines in finished BK2");
  281.    
  282.     //Write File
  283.     FileWriter write = new FileWriter(output);
  284.     PrintWriter print_line = new PrintWriter(write); //use PrintWriter to print "%n" character on both Windows and Unix
  285.    
  286.     //Output all BK2 Input, including beginning and ending lines
  287.     print_line.printf("%s"+"%n",BK2Data[0]); //First line is LogKey, no appended data needed
  288.    
  289.     for(int l=1;l<=numLines;l++){
  290.       if(P2){
  291.         if(DualShockOut) //2 players + DualShock
  292.           print_line.printf("%s"+"  "+"%s"+"%s"+"|  "+"%s"+"%s"+"|"+"%n",SpecButton[l],SpecJoystick[l],BK2Data[l],SpecJoystickP2[l],BK2DataP2[l]);
  293.         else //2 players + Standard Controller
  294.           print_line.printf("%s"+"%s"+"|"+"%s"+"|"+"%n",SpecButton[l],BK2Data[l],BK2DataP2[l]);
  295.       }else{
  296.         if(DualShockOut) //1 player + DualShock
  297.           print_line.printf("%s"+"  "+"%s"+"%s"+"|"+"%n",SpecButton[l],SpecJoystick[l],BK2Data[l]);
  298.         else //1 player + Standard Controller
  299.           print_line.printf("%s"+"%s"+"|"+"%n",SpecButton[l],BK2Data[l]);
  300.       }
  301.     }
  302.    
  303.     print_line.close(); // Close file printing stream
  304.     System.out.println("Done! Check out Input Log.txt");
  305.   }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment