Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * PJM>BK2 Input converter v0.3.1
- * Author: CoolKirby
- * Language: Java (Comments in English)
- * First created: 9/12/15
- * Last revised: 9/17/15
- * Works with 1 and 2-player, DualShock and PSX Controller PJMs!
- * Supports both DualShock and PSX Controller output: use 'convert.pjm' for DS, 'convert_pad.pjm' for PSX Controller
- *
- * Recent Changes: Support for native DualShock PJMs, Disc No./Disc Open/Disc Close/Reset, catch and correct most malformed PJMs
- */
- 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 #XO^1234LDRUS..s
- BK2 UDLRsSQTOXlrLR...
- num 0123456789ABCDEFa
- PJM:|Sq|Cros|Circ|Trian|....R1|...L1|....R2|......L2|..Left|.Down|Rt|Up|St|*L3|*R3|Sel|
- 6 9 8 7 11 10 13 12 2 1 3 0 5 14 15 4
- # X O ^ 1 2 3 4 L D R U S L R 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 < > M
- PJM ..............||0| BK2 | -1,...| 128, 128, 128, 128,.................|
- */
- 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{ //Nested try-catch for alternate read option
- 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;
- 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 has 0 or 1 lines
- 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)
- 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; //will contain index of first . or # char
- 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 line 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,DSInput=false; //Player 2 and DualShock off unless turned on below
- int numDots, P2do=0; //number of input spaces (dots) for PSX Controller; offset of P2 DualShock input dots
- int numTrayOps=0, discNum=1, specNumPos=16; //number of tray operations, % 2 R0 is closed tray, R1 is open; disc #; |1| position
- char SpecOpen,SpecClose,SpecReset; //default values for "special" characters preceding input
- if(PJMData[1].charAt(14)=='.'){ //discreetly checks for DS and Player 2 input without confusing the P1 scanner
- DSInput=true; //1 player, DualShock detected
- specNumPos=34;
- if(PJMData[1].charAt(33)=='.'||PJMData[1].charAt(33)=='#'){
- P2=true; //2 players, DualShock detected
- P2do=18;
- DSInput=true;
- specNumPos=66;
- }
- }else{
- if(PJMData[1].charAt(15)=='.'||PJMData[1].charAt(15)=='#'){
- P2=true; //2 players, Standard Controller
- specNumPos=30;
- }
- }
- if(DualShockOut) //If user wants DualShock input from a regular PJM, blank the last three dots
- numDots=17;
- else //The opposite: if user wants PSX Controller input from a DS PJM, remove the DS-specific input
- numDots=14;
- 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[numDots]; //array for pressed buttons on an input line
- String[] SpecButton = new String[numBK2Lines]; //array for Disc #, Reset, Tray Open & Close
- String[] SpecJoystick = new String[numBK2Lines]; //array for DualShock joystick values (0-255)
- String[] SpecJoystickP2 = new String[numBK2Lines]; //second array for holding Player 2 input
- //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<numDots;b++) // The default blank Input Log line
- Button[b]='.';
- SpecOpen='.'; SpecClose='.'; SpecReset='.'; //Re-initialize Special characters
- if(PJMData[p].charAt(specNumPos)=='1') //Reset Button
- SpecReset='r';
- if(PJMData[p].charAt(specNumPos)=='2'){ //Determine if PJM opened or closed the disc drive
- if(numTrayOps%2==0)
- SpecOpen='!'; //first, third, etc. calls are treated as opening drive
- else{
- SpecClose='!'; //second, fourth, etc. calls are treated as closing drive
- discNum++; //insert the next disc
- }
- numTrayOps++;
- }
- SpecButton[i]="| "+discNum+','+SpecOpen+SpecClose+SpecReset+'|'; //assemble beginning spec characters
- if(numDots==17){ //DualShock Joystick values
- if(DSInput==false||PJMData[p].charAt(19)<'0'||PJMData[p].charAt(19)>'9')
- SpecJoystick[i]="128, 128, 128, 128,"; //if no Stick value listed, assume malformed PJM and prefill values with defaults
- else //P1 LStick and RStick positions
- SpecJoystick[i]=PJMData[p].substring(17,20)+", "+PJMData[p].substring(21,24)+", "+PJMData[p].substring(25,28)+", "+PJMData[p].substring(29,32)+",";
- if(P2){
- if(DSInput==false||PJMData[p].charAt(52)<'0'||PJMData[p].charAt(52)>'9') //try 19 next*
- SpecJoystickP2[i]="128, 128, 128, 128,"; //if no Stick value listed, assume malformed PJM and prefill values with defaults
- else //P2 LStick and RStick positions
- SpecJoystickP2[i]=PJMData[p].substring(50,53)+", "+PJMData[p].substring(54,57)+", "+PJMData[p].substring(58,61)+", "+PJMData[p].substring(62,65)+",";
- }
- }
- 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(DSInput){ //DualShock specific
- if(DualShockOut){ //Only output DualShock buttons if DSOut=true
- if(PJMData[p].charAt(13)=='L') //P1 L3
- Button[14]='<';
- if(PJMData[p].charAt(14)=='R') //P1 R3
- Button[15]='>';
- }
- if(PJMData[p].charAt(15)=='s') //P1 Select from DualShock PJM
- Button[4]='s';
- }else{
- if(PJMData[p].charAt(13)=='s') //P1 Select from PSX Controller PJM
- Button[4]='s';
- }
- for(int s=0;s<numDots;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<numDots;b++) // The default blank Input Log line
- Button[b]='.';
- if(PJMData[p].charAt(P2do+15)=='#') //P2 Square
- Button[6]='Q';
- if(PJMData[p].charAt(P2do+16)=='X') //P2 Cross
- Button[9]='X';
- if(PJMData[p].charAt(P2do+17)=='O') //P2 Circle
- Button[8]='O';
- if(PJMData[p].charAt(P2do+18)=='^') //P2 Triangle
- Button[7]='T';
- if(PJMData[p].charAt(P2do+19)=='1') //P2 R1
- Button[11]='r';
- if(PJMData[p].charAt(P2do+20)=='2') //P2 L1
- Button[10]='l';
- if(PJMData[p].charAt(P2do+21)=='3') //P2 R2
- Button[13]='R';
- if(PJMData[p].charAt(P2do+22)=='4') //P2 L2
- Button[12]='L';
- if(PJMData[p].charAt(P2do+23)=='L') //P2 D-Pad Left
- Button[2]='L';
- if(PJMData[p].charAt(P2do+24)=='D') //P2 D-Pad Down
- Button[1]='D';
- if(PJMData[p].charAt(P2do+25)=='R') //P2 D-Pad Right
- Button[3]='R';
- if(PJMData[p].charAt(P2do+26)=='U') //P2 D-Pad Up
- Button[0]='U';
- if(PJMData[p].charAt(P2do+27)=='S') //P2 Start
- Button[5]='S';
- if(DSInput){ //DualShock specific
- if(DualShockOut){ //Only output DualShock buttons if DSOut=true
- if(PJMData[p].charAt(P2do+28)=='L') //P2 L3
- Button[14]='<';
- if(PJMData[p].charAt(P2do+29)=='R') //P2 R3
- Button[15]='>';
- }
- if(PJMData[p].charAt(P2do+30)=='s') //P2 Select from DualShock PJM
- Button[4]='s';
- }else{
- if(PJMData[p].charAt(28)=='s') //P2 Select from PSX Controller PJM
- Button[4]='s';
- }
- for(int s=0;s<numDots;s++) // Set buttons to input line positions
- BK2DataP2[i]=BK2DataP2[i]+Button[s];
- }
- }
- 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("%s"+" "+"%s"+"%s"+"| "+"%s"+"%s"+"|"+"%n",SpecButton[l],SpecJoystick[l],BK2Data[l],SpecJoystickP2[l],BK2DataP2[l]);
- else //2 players + Standard Controller
- print_line.printf("%s"+"%s"+"|"+"%s"+"|"+"%n",SpecButton[l],BK2Data[l],BK2DataP2[l]);
- }else{
- if(DualShockOut) //1 player + DualShock
- print_line.printf("%s"+" "+"%s"+"%s"+"|"+"%n",SpecButton[l],SpecJoystick[l],BK2Data[l]);
- else //1 player + Standard Controller
- print_line.printf("%s"+"%s"+"|"+"%n",SpecButton[l],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