Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * PJM>BK2 Input converter v0.2
- * Author: CoolKirby
- * Language: Java (with English comments)
- * First created: 9/12/15
- * Last revised: 9/15/15
- * Works with 1 and 2-player PJMs!
- * Does not support resets or changing discs
- * Only supports PSX Controller input for now, as this is the most common PJM setting
- * Supports both DualShock and PSX Controller output, DualShock is default, but Std Controller is more compact
- */
- import java.io.IOException; //Necessary to prevent exceptions
- import java.io.FileReader;
- import java.io.BufferedReader;
- import java.io.FileWriter;
- import java.io.PrintWriter;
- /*
- Input formats:
- PJM:|Square|Cross|Circle|Triangle|R1|L1|R2|L2|Left|Down|Right|Up|Start|Select|
- BK2:|Up|Down|Left|Right|Select|Start|Square|Triangle|Circle|Cross|L1|R1|L2|R2| L3|R3|MODE| <--DualShock only
- PJM #XO^1234LDRUSs
- BK2 UDLRsSQTOXlrLR...
- num 0123456789ABCDEFa
- */
- public class PJMConverter{
- public static void main(String[] args) throws IOException{
- try{
- countLinesDS("convert.pjm"); //File to read from (input - PJM)
- }catch(IOException e){
- try{
- countLinesStd("convert_pad.pjm"); //Standard PSX Controller file to read from (input - PJM)
- }catch(IOException ee){
- System.out.println(e.getMessage());
- }
- }
- }
- public static void countLinesDS(String path) throws IOException{
- Boolean DualShockOut=true; //In future, maybe check PJM for (rare) existing DualShock input
- countLines(path,DualShockOut); //If integrated into BizHawk, it could set this based on existing PSX settings
- }
- public static void countLinesStd(String path) throws IOException{
- Boolean DualShockOut=false;
- countLines(path,DualShockOut);
- }
- public static void countLines(String path, Boolean DualShockOut) throws IOException{
- FileReader a = new FileReader(path);
- BufferedReader num = new BufferedReader(a); //Open countLines text buffer
- String aLine;
- int numLines=0;
- while ((aLine=num.readLine()) != null){ //Count number of lines in the PJM
- numLines++;
- }
- num.close(); //Close countLines text buffer
- if(numLines<2){ //Assume error if input file only has one line
- System.out.println("Input PJM is too short! Conversion failed. Please check your file.");
- System.exit(5000);
- }
- OpenFile(path,numLines,DualShockOut);
- }
- public static void OpenFile(String path, int numLines, Boolean DualShockOut) throws IOException{
- String output="Input Log.txt"; //File to write to (output - BK2 Input Log.txt)
- FileReader fr = new FileReader(path); // Loads initial PJM
- BufferedReader reader = new BufferedReader(fr); // Reads whole lines in to speed up script
- String[] PJMData = new String[numLines]; // Create the array of PJM input with the calculated # of lines
- int inputStart;
- PJMData[0]=reader.readLine(); //Read the first line of the PJM separately
- if(PJMData[0].indexOf('#')==-1) //Find where the input starts
- inputStart=PJMData[0].indexOf('.');
- else
- inputStart=PJMData[0].indexOf('#'); //Failsafe in case Square was pressed on first frame
- PJMData[0]=PJMData[0].substring(inputStart,PJMData[0].length()); //Strip off the PJM header
- for(int i=1;i<numLines;i++){ //Read the rest of the PJM starting from frame 2
- PJMData[i]=reader.readLine();
- if(numLines-i==0){
- PJMData[i]=reader.readLine(); //"Catch" the elusive last input and add it to the table
- }
- }
- reader.close();
- parseFile(PJMData,numLines,path,output,DualShockOut);
- }
- public static void parseFile(String[] PJMData, int numLines, String path, String output, Boolean DualShockOut) throws IOException {
- int numBK2Lines=numLines+1;
- Boolean P2=false; //initialize as false
- if(PJMData[1].charAt(15)=='.') //discreetly checks for Player 2 input without confusing the P1 scanner
- P2=true;
- String[] BK2Data = new String[numBK2Lines]; //1 line larger to fit BK2 LogKey comment
- String[] BK2DataP2 = new String[numBK2Lines]; //second array for holding Player 2 input
- char[] Button = new char[14]; //array for pressed buttons on an input line
- //Add in BizHawk LogKey line for author's convenience
- if(P2){ //If Player 2 input exists, recognize it
- if(DualShockOut) //2 players + DualShock for both
- 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|";
- else //2 players + Standard Controller for both
- 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|";
- }else{
- if(DualShockOut) //1 player + DualShock
- 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|";
- else //1 player + Standard Controller
- 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|";
- }
- for(int i=1,p=0;i<numBK2Lines;i++,p++){ // The big parser! Player 1 Input
- BK2Data[i]="";
- for(int b=0;b<14;b++) // The default blank Input Log line
- Button[b]='.';
- if(PJMData[p].charAt(0)=='#') //P1 Square
- Button[6]='Q';
- if(PJMData[p].charAt(1)=='X') //P1 Cross
- Button[9]='X';
- if(PJMData[p].charAt(2)=='O') //P1 Circle
- Button[8]='O';
- if(PJMData[p].charAt(3)=='^') //P1 Triangle
- Button[7]='T';
- if(PJMData[p].charAt(4)=='1') //P1 R1
- Button[11]='r';
- if(PJMData[p].charAt(5)=='2') //P1 L1
- Button[10]='l';
- if(PJMData[p].charAt(6)=='3') //P1 R2
- Button[13]='R';
- if(PJMData[p].charAt(7)=='4') //P1 L2
- Button[12]='L';
- if(PJMData[p].charAt(8)=='L') //P1 D-Pad Left
- Button[2]='L';
- if(PJMData[p].charAt(9)=='D') //P1 D-Pad Down
- Button[1]='D';
- if(PJMData[p].charAt(10)=='R') //P1 D-Pad Right
- Button[3]='R';
- if(PJMData[p].charAt(11)=='U') //P1 D-Pad Up
- Button[0]='U';
- if(PJMData[p].charAt(12)=='S') //P1 Start
- Button[5]='S';
- if(PJMData[p].charAt(13)=='s') //P1 Select
- Button[4]='s';
- for(int s=0;s<14;s++) // Set buttons to input line positions
- BK2Data[i]=BK2Data[i]+Button[s];
- if(P2){ //If Player 2 input exists, parse it!
- BK2DataP2[i]="";
- for(int b=0;b<14;b++) // The default blank Input Log line
- Button[b]='.';
- if(PJMData[p].charAt(15)=='#') //P2 Square
- Button[6]='Q';
- if(PJMData[p].charAt(16)=='X') //P2 Cross
- Button[9]='X';
- if(PJMData[p].charAt(17)=='O') //P2 Circle
- Button[8]='O';
- if(PJMData[p].charAt(18)=='^') //P2 Triangle
- Button[7]='T';
- if(PJMData[p].charAt(19)=='1') //P2 R1
- Button[11]='r';
- if(PJMData[p].charAt(20)=='2') //P2 L1
- Button[10]='l';
- if(PJMData[p].charAt(21)=='3') //P2 R2
- Button[13]='R';
- if(PJMData[p].charAt(22)=='4') //P2 L2
- Button[12]='L';
- if(PJMData[p].charAt(23)=='L') //P2 D-Pad Left
- Button[2]='L';
- if(PJMData[p].charAt(24)=='D') //P2 D-Pad Down
- Button[1]='D';
- if(PJMData[p].charAt(25)=='R') //P2 D-Pad Right
- Button[3]='R';
- if(PJMData[p].charAt(26)=='U') //P2 D-Pad Up
- Button[0]='U';
- if(PJMData[p].charAt(27)=='S') //P2 Start
- Button[5]='S';
- if(PJMData[p].charAt(28)=='s') //P2 Select
- Button[4]='s';
- for(int s=0;s<14;s++) // Set buttons to input line positions
- BK2DataP2[i]=BK2DataP2[i]+Button[s];
- }
- }
- /*
- PJM:|Sq|Cros|Circ|Trian|....R1|...L1|....R2|......L2|..Left|.Down|Rt|Up|St|Se|
- 6 9 8 7 11 10 13 12 2 1 3 0 5 4
- # X O ^ 1 2 3 4 L D R U S s
- BK2:|Up|Down|Left|Right|Select|Start|Square|Triangle|Circle|Cross|L1|R1|L2|R2|L3|R3|MODE| <--DualShock only
- 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
- U D L R s S Q T O X l r L R . . .
- PJM ..............||0| BK2 | -1,...| 128, 128, 128, 128,.................|
- num 0123456789ABCDEFa output | -1,...| 128, 128, 128, 128, + .............. + ...|
- */
- System.out.println(numBK2Lines+" lines in finished BK2");
- //Write File
- FileWriter write = new FileWriter(output);
- PrintWriter print_line = new PrintWriter(write); //use PrintWriter to print "%n" character on both Windows and Unix
- //Output all BK2 Input, including beginning and ending lines
- print_line.printf("%s"+"%n",BK2Data[0]); //First line is LogKey, no appended data needed
- for(int l=1;l<=numLines;l++){
- if(P2){
- if(DualShockOut) //2 players + DualShock
- print_line.printf("| -1,...| 128, 128, 128, 128,"+"%s"+"...| 128, 128, 128, 128,"+"%s"+"...|"+"%n",BK2Data[l],BK2DataP2[l]);
- else //2 players + Standard Controller
- print_line.printf("| -1,...|"+"%s"+"|"+"%s"+"|"+"%n",BK2Data[l],BK2DataP2[l]);
- }else{
- if(DualShockOut) //1 player + DualShock
- print_line.printf("| -1,...| 128, 128, 128, 128,"+"%s"+"...|"+"%n",BK2Data[l]);
- else //1 player + Standard Controller
- print_line.printf("| -1,...|"+"%s"+"|"+"%n",BK2Data[l]);
- }
- }
- print_line.close(); // Close file printing stream
- System.out.println("Done! Check out Input Log.txt");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment