Prepare live detector (#8)

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2 2 years ago
parent cc0f69d8b1
commit d7115ef89f
  1. BIN
      bin/ArcadeReader$1.class
  2. BIN
      bin/ArcadeReader.class
  3. BIN
      bin/readers/DDRReader.class
  4. BIN
      bin/readers/ITGReader.class
  5. BIN
      bin/readers/LoveLiveReader.class
  6. BIN
      bin/readers/SoundVoltexReader.class
  7. BIN
      bin/readers/TestReader.class
  8. BIN
      bin/sigPlace.class
  9. BIN
      bin/sigServer.class
  10. 7
      exceptions/FailedResponseException.java
  11. BIN
      readers/Box.class
  12. 7
      readers/Box.java
  13. BIN
      readers/ColorRange.class
  14. BIN
      readers/PopnReader.class
  15. BIN
      readers/Reader.class
  16. 12
      readers/Reader.java
  17. BIN
      readers/fonts/Font.class
  18. BIN
      readers/fonts/Glyph.class
  19. 99
      requests/GETRequest.java
  20. 149
      requests/MultipartUtility.java
  21. 130
      requests/POSTRequest.java
  22. 38
      sigPlace.java

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -0,0 +1,7 @@
package exceptions;
public class FailedResponseException extends Exception{
public FailedResponseException(String msg) {
super(msg);
}
}

Binary file not shown.

@ -6,6 +6,7 @@ public class Box{
public int w;
public int h;
boolean ja_required;
boolean isNumber = false;
final static int BOX_THRESHOLD=8; //How many pixels outside the specified region the score can be.
public Box(int x,int y,int w, int h) {
this.x=x;
@ -16,4 +17,10 @@ public class Box{
boolean insideBox(int x,int y) {
return this.x-BOX_THRESHOLD<=x&&this.x+this.w+BOX_THRESHOLD>=x&&this.y-BOX_THRESHOLD<=y&&this.y+this.h+BOX_THRESHOLD>=y;
}
public void setNumber(boolean isNumber) {
this.isNumber = isNumber;
}
public boolean isNumber() {
return isNumber;
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -29,6 +29,7 @@ public abstract class Reader{
protected void addRegion(Box box,boolean isNumb) {
readRegions.add(box);
if (isNumb) {
box.setNumber(true);
sig_data_size++;
}
}
@ -436,4 +437,15 @@ public abstract class Reader{
return false;
return true;
}
public boolean isReasonableData() {
for (int i=0;i<readRegions.size();i++) {
if (readRegions.get(i).isNumber()) {
if (sig_data[i].length()==0) {
return false;
}
}
}
return true;
}
}

Binary file not shown.

Binary file not shown.

@ -0,0 +1,99 @@
package requests;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpClient.Builder;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.file.Path;
import java.time.Duration;
import exceptions.FailedResponseException;
public class GETRequest{
String url;
String[] headers;
long timeout;
Path file;
String user="";
String pass="";
private HttpRequest req;
private HttpClient client;
/**
* @param file The file path info, use this for file downloads or set to null for standard text.
* @param timeout in milliseconds
* */
public GETRequest(String url, String username, String password, long timeout, Path file, String...headers){
this.url = url;
this.user=username;
this.pass=password;
this.headers = headers;
this.timeout = timeout;
this.file=file;
}
/**
* @param file The file path info, use this for file downloads or set to null for standard text.
* @param timeout in milliseconds
* */
public GETRequest(String url, long timeout, Path file, String...headers){
this(url,"","",timeout,file,headers);
}
/**
* @param timeout in milliseconds
* */
public GETRequest(String url, long timeout, String...headers){
this(url,timeout,null,headers);
}
public GETRequest(String url){
this(url,30000,null);
}
public HttpResponse<?> run() throws FailedResponseException {
build();
try {
if (file==null) {
return client.send(req,BodyHandlers.ofString());
} else {
return client.send(req,BodyHandlers.ofFile(file));
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
throw new FailedResponseException("No proper response returned. THIS SHOULD NOT BE HAPPENING!");
}
protected java.net.http.HttpRequest.Builder finalizeRequestPreBuild(java.net.http.HttpRequest.Builder requestBuild) throws FailedResponseException {
return requestBuild.GET();
}
protected Builder finalizeClientPreBuild(Builder clientBuild) {
return clientBuild;
}
protected void build(){
boolean AUTH_REQUIRED=user.length()>0&&pass.length()>0;
try {
java.net.http.HttpRequest.Builder requestBuild=HttpRequest.newBuilder(new URI(url))
.version(HttpClient.Version.HTTP_2)
.timeout(Duration.ofMillis(timeout));
if (headers!=null&&headers.length>0) {
requestBuild.headers(headers);
}
req = finalizeRequestPreBuild(requestBuild).build();
Builder clientBuild=HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS);
if (AUTH_REQUIRED) {
clientBuild.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pass.toCharArray());
}
});
}
client = finalizeClientPreBuild(clientBuild).build();
} catch (URISyntaxException | FailedResponseException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,149 @@
package requests;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
/**
* This utility class provides an abstraction layer for sending multipart HTTP
* POST requests to a web server.
* @author www.codejava.net
*
*/
public class MultipartUtility {
private final String boundary;
private static final String LINE_FEED = "\r\n";
private HttpURLConnection httpConn;
private String charset;
private OutputStream outputStream;
private PrintWriter writer;
/**
* This constructor initializes a new HTTP POST request with content type
* is set to multipart/form-data
* @param requestURL
* @param charset
* @throws IOException
*/
public MultipartUtility(String requestURL, String charset)
throws IOException {
this.charset = charset;
// creates a unique boundary based on time stamp
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("User-Agent", "CodeJava Agent");
httpConn.setRequestProperty("Test", "Bonjour");
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
true);
}
/**
* Adds a form field to the request
* @param name field name
* @param value field value
*/
public void addFormField(String name, String value) {
writer.append("--" + boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + name + "\"")
.append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=" + charset).append(
LINE_FEED);
writer.append(LINE_FEED);
writer.append(value).append(LINE_FEED);
writer.flush();
}
/**
* Adds a upload file section to the request
* @param fieldName name attribute in <input type="file" name="..." />
* @param uploadFile a File to be uploaded
* @throws IOException
*/
public void addFilePart(String fieldName, File uploadFile)
throws IOException {
String fileName = uploadFile.getName();
writer.append("--" + boundary).append(LINE_FEED);
writer.append(
"Content-Disposition: form-data; name=\"" + fieldName
+ "\"; filename=\"" + fileName + "\"")
.append(LINE_FEED);
writer.append(
"Content-Type: "
+ URLConnection.guessContentTypeFromName(fileName))
.append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.flush();
FileInputStream inputStream = new FileInputStream(uploadFile);
char[] buffer = new char[1024];
String s = new String(buffer);
byte[] data = s.getBytes("ISO-8859-1");
int bytesRead = -1;
while ((bytesRead = inputStream.read(data)) > 0) {
outputStream.write(data, 0, bytesRead);
}
inputStream.close();
writer.flush();
}
/**
* Adds a header field to the request.
* @param name - name of the header field
* @param value - value of the header field
*/
public void addHeaderField(String name, String value) {
writer.append(name + ": " + value).append(LINE_FEED);
writer.flush();
}
/**
* Completes the request and receives response from the server.
* @return a list of Strings as response in case the server returned
* status OK, otherwise an exception is thrown.
* @throws IOException
*/
public List<String> finish() throws IOException {
List<String> response = new ArrayList<String>();
writer.flush();
writer.append("--" + boundary + "--").append(LINE_FEED);
writer.close();
// checks server's status code first
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
httpConn.disconnect();
} else {
System.out.println("Server returned non-OK status: " + status);
}
return response;
}
}

@ -0,0 +1,130 @@
package requests;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpClient.Builder;
import java.net.http.HttpClient.Version;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import javax.net.ssl.SSLSession;
import exceptions.FailedResponseException;
public class POSTRequest extends GETRequest{
String body = "";
Path uploadFile = null;
public POSTRequest(String url, String body, String username, String password, long timeout, Path outputFile, String... headers) {
super(url, username, password, timeout, outputFile, headers);
this.body=body;
}
public POSTRequest(String url, String body, long timeout, Path outputFile, String... headers) {
super(url, timeout, outputFile, headers);
this.body=body;
}
public POSTRequest(String url, String body, long timeout, String... headers) {
super(url, timeout, headers);
this.body=body;
}
public POSTRequest(String url, String body) {
super(url);
this.body=body;
}
public POSTRequest(String url, Path uploadFile) {
super(url);
this.uploadFile=uploadFile;
}
@Override
public HttpResponse<?> run() throws FailedResponseException {
if (uploadFile!=null) {
String charset = "ISO-8859-1";
File file = uploadFile.toFile();
String requestURL = url;
try {
MultipartUtility multipart = new MultipartUtility(requestURL, charset);
multipart.addHeaderField("User-Agent", "SIG HTTPCLIENT");
multipart.addFilePart("fileUpload", file);
List<String> response = multipart.finish();
return new HttpResponse<String>(){
@Override
public int statusCode() {
return 0;
}
@Override
public HttpRequest request() {
return null;
}
@Override
public Optional<HttpResponse<String>> previousResponse() {
return null;
}
@Override
public HttpHeaders headers() {
return null;
}
@Override
public String body() {
StringBuilder sb = new StringBuilder();
for (String s : response) {
sb.append(s);
}
return sb.toString();
}
@Override
public Optional<SSLSession> sslSession() {
return null;
}
@Override
public URI uri() {
return null;
}
@Override
public Version version() {
return null;
}
};
} catch (IOException ex) {
System.err.println(ex);
throw new FailedResponseException("Could not send response for POST File upload. THIS SHOULD NOT BE HAPPENING!");
}
} else {
return super.run();
}
}
@Override
protected java.net.http.HttpRequest.Builder finalizeRequestPreBuild(
java.net.http.HttpRequest.Builder requestBuild) throws FailedResponseException{
requestBuild.headers("Content-Type","application/json");
try {
return file!=null?requestBuild.POST(HttpRequest.BodyPublishers.ofFile(file)):
body.length()>0?requestBuild.POST(HttpRequest.BodyPublishers.ofString(body)):
requestBuild.POST(HttpRequest.BodyPublishers.noBody());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
throw new FailedResponseException("Could not complete request build. THIS SHOULD NOT BE HAPPENING!");
}
@Override
protected Builder finalizeClientPreBuild(Builder clientBuild) {
return clientBuild;
}
}

@ -17,12 +17,17 @@ import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Robot;
import java.awt.GraphicsEnvironment;
import readers.Box;
import readers.fonts.Font;
import requests.POSTRequest;
import readers.ColorRange;
import readers.PopnReader;
import readers.Reader;
public class sigPlace {
@ -86,8 +91,39 @@ public class sigPlace {
final static int TRANSPARENT = new Color(0,0,0,0).getRGB();
public static void main(String[] args) {
final Color SDVX_UI_COL = new Color(48,48,48);
if (args.length>0&&args[0].equals("arcade")) {
System.out.println("Arcade");
try {
Robot r = new Robot();
while (true) {
BufferedImage screenshot = r.createScreenCapture(GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds());
if (new Color(screenshot.getRGB(1550,604)).equals(SDVX_UI_COL)) {
//This is a SDVX image, so we will flip it.
BufferedImage newCanvas = new BufferedImage(screenshot.getHeight(),screenshot.getWidth(),BufferedImage.TYPE_INT_ARGB);
for (int x=0;x<screenshot.getWidth();x++) {
for (int y=0;y<screenshot.getHeight();y++) {
newCanvas.setRGB(screenshot.getHeight()-y-1, x, screenshot.getRGB(x,y));
}
}
screenshot.flush();
screenshot=newCanvas;
}
final Path temp = Paths.get("temp.png");
Reader result = ArcadeReader.interpret(screenshot);
System.out.println("Detected as "+result.getClass().getSimpleName());
ImageIO.write(screenshot,"png",temp.toFile());
result.interpretBoxes(temp, true);
if (result.isReasonableData()) {
System.out.println("Data is reasonable, prepare for submitting...");
}
/*if (result.isReasonableData()) {
POSTRequest postRes = new POSTRequest("http://"+args[0]+"/uploadform.html",Path.of("..",filename));
}*/
Thread.sleep(50);
}
} catch (AWTException | InterruptedException | IOException e) {
e.printStackTrace();
}
} else {
populateDatabase();
/*String fontName = "sdvx_EXScore";

Loading…
Cancel
Save