DroidFish: Don't read the whole engine file just to determine if it is a network engine or not.

This commit is contained in:
Peter Osterlund 2014-03-28 23:00:41 +00:00
parent 9cef130ce1
commit 55de4ccdc4
4 changed files with 38 additions and 45 deletions

View File

@ -21,11 +21,9 @@ package org.petero.droidfish;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -2694,16 +2692,7 @@ public class DroidFish extends Activity implements GUIInterface {
public boolean accept(String filename) {
if (internalEngine(filename))
return false;
try {
InputStream inStream = new FileInputStream(filename);
InputStreamReader inFile = new InputStreamReader(inStream);
char[] buf = new char[4];
boolean ret = (inFile.read(buf) == 4) && "NETE".equals(new String(buf));
inFile.close();
return ret;
} catch (IOException e) {
return false;
}
return EngineUtil.isNetEngine(filename);
}
});
final int numFiles = fileNames.length;
@ -2835,8 +2824,8 @@ public class DroidFish extends Activity implements GUIInterface {
String hostName = "";
String port = "0";
try {
String[] lines = Util.readFile(networkEngineToConfig);
if ((lines.length >= 1) && lines[0].equals("NETE")) {
if (EngineUtil.isNetEngine(networkEngineToConfig)) {
String[] lines = Util.readFile(networkEngineToConfig);
if (lines.length > 1)
hostName = lines[1];
if (lines.length > 2)

View File

@ -18,6 +18,11 @@
package org.petero.droidfish.engine;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.os.Build;
public class EngineUtil {
@ -43,6 +48,21 @@ public class EngineUtil {
return "stockfish-" + abi;
}
/** Return true if file "engine" is a network engine. */
public static boolean isNetEngine(String engine) {
boolean netEngine = false;
try {
InputStream inStream = new FileInputStream(engine);
InputStreamReader inFile = new InputStreamReader(inStream);
char[] buf = new char[4];
if ((inFile.read(buf) == 4) && "NETE".equals(new String(buf)))
netEngine = true;
inFile.close();
} catch (IOException e) {
}
return netEngine;
}
/** Executes chmod 744 exePath. */
final static native boolean chmod(String exePath);

View File

@ -67,19 +67,19 @@ public class NetworkEngine extends UCIEngineBase {
if (socket == null) {
String host = null;
String port = null;
boolean fail = false;
try {
String[] lines = Util.readFile(fileName);
if ((lines.length < 3) || !lines[0].equals("NETE")) {
fail = true;
} else {
host = lines[1];
port = lines[2];
boolean ok = false;
if (EngineUtil.isNetEngine(fileName)) {
try {
String[] lines = Util.readFile(fileName);
if (lines.length >= 3) {
host = lines[1];
port = lines[2];
ok = true;
}
} catch (IOException e1) {
}
} catch (IOException e1) {
fail = true;
}
if (fail) {
if (!ok) {
isError = true;
report.reportError(context.getString(R.string.network_engine_config_error));
} else {

View File

@ -18,10 +18,6 @@
package org.petero.droidfish.engine;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
@ -46,22 +42,10 @@ public abstract class UCIEngineBase implements UCIEngine {
return new CuckooChessEngine(report);
else if ("stockfish".equals(engine))
return new InternalStockFish(context, report);
else {
boolean netEngine = false;
try {
InputStream inStream = new FileInputStream(engine);
InputStreamReader inFile = new InputStreamReader(inStream);
char[] buf = new char[4];
if ((inFile.read(buf) == 4) && "NETE".equals(new String(buf)))
netEngine = true;
inFile.close();
} catch (IOException e) {
}
if (netEngine)
return new NetworkEngine(context, engine, engineOptions, report);
else
return new ExternalEngine(context, engine, report);
}
else if (EngineUtil.isNetEngine(engine))
return new NetworkEngine(context, engine, engineOptions, report);
else
return new ExternalEngine(context, engine, report);
}
protected UCIEngineBase() {