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.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -2694,16 +2692,7 @@ public class DroidFish extends Activity implements GUIInterface {
public boolean accept(String filename) { public boolean accept(String filename) {
if (internalEngine(filename)) if (internalEngine(filename))
return false; return false;
try { return EngineUtil.isNetEngine(filename);
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;
}
} }
}); });
final int numFiles = fileNames.length; final int numFiles = fileNames.length;
@ -2835,8 +2824,8 @@ public class DroidFish extends Activity implements GUIInterface {
String hostName = ""; String hostName = "";
String port = "0"; String port = "0";
try { try {
if (EngineUtil.isNetEngine(networkEngineToConfig)) {
String[] lines = Util.readFile(networkEngineToConfig); String[] lines = Util.readFile(networkEngineToConfig);
if ((lines.length >= 1) && lines[0].equals("NETE")) {
if (lines.length > 1) if (lines.length > 1)
hostName = lines[1]; hostName = lines[1];
if (lines.length > 2) if (lines.length > 2)

View File

@ -18,6 +18,11 @@
package org.petero.droidfish.engine; 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; import android.os.Build;
public class EngineUtil { public class EngineUtil {
@ -43,6 +48,21 @@ public class EngineUtil {
return "stockfish-" + abi; 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. */ /** Executes chmod 744 exePath. */
final static native boolean chmod(String exePath); final static native boolean chmod(String exePath);

View File

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

View File

@ -18,10 +18,6 @@
package org.petero.droidfish.engine; 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.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
@ -46,23 +42,11 @@ public abstract class UCIEngineBase implements UCIEngine {
return new CuckooChessEngine(report); return new CuckooChessEngine(report);
else if ("stockfish".equals(engine)) else if ("stockfish".equals(engine))
return new InternalStockFish(context, report); return new InternalStockFish(context, report);
else { else if (EngineUtil.isNetEngine(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) {
}
if (netEngine)
return new NetworkEngine(context, engine, engineOptions, report); return new NetworkEngine(context, engine, engineOptions, report);
else else
return new ExternalEngine(context, engine, report); return new ExternalEngine(context, engine, report);
} }
}
protected UCIEngineBase() { protected UCIEngineBase() {
processAlive = false; processAlive = false;