Guest User

PJM>BK2 Input Log converter v0.01

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