/**
* eAdventure (formerly <e-Adventure> and <e-Game>) is a research project of the
* <e-UCM> research group.
*
* Copyright 2005-2010 <e-UCM> research group.
*
* You can access a list of all the contributors to eAdventure at:
* http://e-adventure.e-ucm.es/contributors
*
* <e-UCM> is a research group of the Department of Software Engineering
* and Artificial Intelligence at the Complutense University of Madrid
* (School of Computer Science).
*
* C Profesor Jose Garcia Santesmases sn,
* 28040 Madrid (Madrid), Spain.
*
* For more info please visit: <http://e-adventure.e-ucm.es> or
* <http://www.e-ucm.es>
*
* ****************************************************************************
*
* This file is part of eAdventure, version 2.0
*
* eAdventure is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* eAdventure is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with eAdventure. If not, see <http://www.gnu.org/licenses/>.
*/
package es.eucm.ead.engine.desktop.platform.assets;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import es.eucm.ead.engine.assets.AssetHandlerImpl;
import es.eucm.ead.tools.GenericInjector;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
@Singleton
public class DesktopAssetHandler extends AssetHandlerImpl {
private ZipFile zipFile;
private boolean zipped;
private Map<String, String> tempFiles;
@Inject
public DesktopAssetHandler(GenericInjector injector) {
super(injector);
zipped = false;
tempFiles = new HashMap<String, String>();
}
@Override
public void setResourcesLocation(String uri) {
super.setResourcesLocation(uri);
if (uri != null && uri != null) {
try {
zipFile = new ZipFile(new File(uri));
zipped = true;
} catch (ZipException e) {
zipped = false;
} catch (IOException e) {
zipped = false;
}
} else {
zipped = false;
}
}
public String getTempFilePath(String path) {
if (!tempFiles.containsKey(path)) {
FileHandle file = getFileHandle(path);
if (file.exists()) {
try {
File f = File.createTempFile("video", ".avi");
f.deleteOnExit();
FileHandle tempFile = new FileHandle(f);
file.copyTo(tempFile);
tempFiles.put(path, f.getAbsolutePath());
} catch (IOException e) {
}
}
}
return tempFiles.get(path);
}
@Override
public FileHandle getProjectFileHandle(String uri) {
FileHandle fh = super.getProjectFileHandle(uri);
if (!fh.exists() && zipped) {
ZipEntry zipEntry = zipFile.getEntry(uri);
fh = new ZipFileHandle(zipFile, zipEntry);
}
return fh;
}
public class ZipFileHandle extends FileHandle {
private ZipFile zipFile;
private ZipEntry zipEntry;
public ZipFileHandle(ZipFile zipFile, ZipEntry zipEntry) {
this.zipFile = zipFile;
this.zipEntry = zipEntry;
}
@Override
public InputStream read() {
try {
return zipFile.getInputStream(zipEntry);
} catch (IOException e) {
}
return null;
}
@Override
public boolean exists() {
return zipEntry != null;
}
@Override
public long length() {
try {
return read().available();
} catch (IOException e) {
return 0;
}
}
@Override
public String toString() {
return zipFile.getName() + zipEntry.getName();
}
}
@Override
public void getTextfileAsync(String path, TextHandler textHandler) {
textHandler.handle(getTextFile(path));
}
@Override
public String getTextFile(String path) {
if (Gdx.files != null) {
return super.getTextFile(path);
} else {
String result = null;
String absolutePath = (resourcesUri == null ? "" : resourcesUri)
+ path.substring(1);
InputStream is = ClassLoader
.getSystemResourceAsStream(absolutePath);
if (is == null) {
File f = new File(absolutePath);
if (f.exists()) {
try {
is = new FileInputStream(f);
} catch (FileNotFoundException e) {
}
}
}
if (is != null) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
try {
String line;
result = "";
while ((line = reader.readLine()) != null) {
result += line + "\n";
}
} catch (IOException e) {
logger.error("Error reading file", e);
result = null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
logger.error("Error closing reader", e);
}
}
}
} else {
logger.warn("Unable to find resource {}", path);
}
return result;
}
}
}