Skip to content

Commit

Permalink
Load sound from bytes if it is a resource. More options for controlli…
Browse files Browse the repository at this point in the history
…ng and reading nme files.
  • Loading branch information
hughsando committed May 24, 2015
1 parent 7b7e115 commit 38c474d
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 32 deletions.
39 changes: 30 additions & 9 deletions nme/Assets.hx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Assets
for(key in info.keys())
trace(" " + key + " -> " + info.get(key).path + " " + info.get(key).isResource );
trace("---");
trace("All resources: " + haxe.Resource.listNames());
}
if (bytes==null)
return null;
Expand Down Expand Up @@ -223,6 +224,7 @@ class Assets
return data;
}


public static function hasBytes(id:String):Bool
{
var i = getInfo(id);
Expand Down Expand Up @@ -379,15 +381,30 @@ class Assets
return val;
}

var sound =
#if flash
cast(Type.createInstance(Type.resolveClass(i.className), []), Sound)
#elseif js
new Sound(new URLRequest(i.path))
#else
new Sound(new URLRequest(i.path), null, i.type == MUSIC || forceMusic)
#end
;
var sound:Sound = null;
#if flash
sound = cast(Type.createInstance(Type.resolveClass(i.className), []), Sound);
#elseif js
sound = new Sound(new URLRequest(i.path));
#else
if (i.isResource)
{
sound = new Sound();
var bytes = nme.Assets.getBytes(id);
sound.loadCompressedDataFromByteArray(bytes, bytes.length,i.type == MUSIC || forceMusic);
}
else if (byteFactory.exists(i.path))
{
var bytes = byteFactory.get(i.path)();
sound = new Sound();
sound.loadCompressedDataFromByteArray(bytes, bytes.length,i.type == MUSIC || forceMusic);
}
else
{
sound = new Sound(new URLRequest(i.path), null, i.type == MUSIC || forceMusic);
}

#end

trySetCache(i,useCache,sound);

Expand Down Expand Up @@ -456,10 +473,14 @@ class Assets
var reso = haxe.Resource.getBytes(s);
if (reso!=null)
ByteArray.fromBytes(reso);
trace("Seek " + s);
// Reverse lookup-by path...
for(asset in info)
{
trace(" " + asset.path);
if (asset.path == s)
return getBytesInfo(asset);
}
return getBytes(s);
});
return null; } ) ();
Expand Down
21 changes: 19 additions & 2 deletions nme/JNI.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package nme;
#if (android)

import cpp.zip.Uncompress;
import haxe.io.Bytes;
Expand Down Expand Up @@ -48,6 +47,7 @@ class JNI
*/
public static function createMemberMethod(className:String, memberName:String, signature:String, useArray:Bool = false, quietFail:Bool = false):Dynamic
{
#if android
init();
className = className.split(".").join("/");

Expand All @@ -60,6 +60,9 @@ class JNI
}
var method = new JNIMethod(handle);
return method.getMemberMethod(useArray);
#else
return null;
#end
}

/**
Expand All @@ -72,6 +75,7 @@ class JNI
*/
public static function createStaticMethod(className:String, memberName:String, signature:String, useArray:Bool = false, quietFail:Bool=false):Dynamic
{
#if android
init();
className = className.split(".").join("/");

Expand All @@ -84,6 +88,9 @@ class JNI
}
var method = new JNIMethod(handle);
return method.getStaticMethod(useArray);
#else
return null;
#end
}


Expand Down Expand Up @@ -121,9 +128,15 @@ class JNI


// Native Methods
#if android
private static var nme_jni_create_method = Loader.load("nme_jni_create_method", 5);
private static var nme_jni_call_member = Loader.load("nme_jni_call_member", 3);
private static var nme_jni_call_static = Loader.load("nme_jni_call_static", 2);
#else
private static var nme_jni_create_method:Dynamic;
private static var nme_jni_call_member:Dynamic;
private static var nme_jni_call_static:Dynamic;
#end
}

class JNIMethod
Expand Down Expand Up @@ -163,8 +176,12 @@ class JNIMethod
}

// Native Methods
#if android
private static var nme_jni_call_member = Loader.load("nme_jni_call_member", 3);
private static var nme_jni_call_static = Loader.load("nme_jni_call_static", 2);
#else
private static var nme_jni_call_member:Dynamic;
private static var nme_jni_call_static:Dynamic;
#end
}

#end
53 changes: 45 additions & 8 deletions nme/script/Nme.hx
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,17 @@ class Nme
#end
}

public static function runBytes(inBytes:Bytes,?verify:Dynamic->Bytes->Void)
{
runInput( new haxe.io.BytesInput(inBytes), verify );
}

public static function runFile(inFilename:String,?verify:Dynamic->Bytes->Void)
{
#if (cpp && !cppia)
if (inFilename.endsWith(".nme"))
{
nme.Assets.scriptBase = "";
var bytes = sys.io.File.getBytes(inFilename);
runInput( new haxe.io.BytesInput(bytes), verify );
}
Expand All @@ -127,17 +133,48 @@ class Nme
#end
}

public static function runResource(?inResource:String)

public static function getBytesHeader(inBytes:Bytes) : Dynamic
{
var input = new haxe.io.BytesInput(inBytes);
return getHeader(input);
}


public static function getResourceHeader(inResource:String) : Dynamic
{
var bytes = haxe.Resource.getBytes(inResource);
if (bytes==null)
return null;

var input = new haxe.io.BytesInput(bytes);
return getHeader(input);
}

public static function runResource(?inResource:String,?verify:Dynamic->Bytes->Void)
{
#if (cpp && !cppia)
if (inResource==null)
inResource = "ScriptMain.cppia";
var script = nme.Assets.getString(inResource);
if (script==null)
throw "Could not find resource script " + inResource;
#if (hxcpp_api_level>=320)
cpp.cppia.Host.run(script);
#end
inResource = "ScriptMain.cppia";

if (inResource.endsWith(".nme"))
{
var bytes = haxe.Resource.getBytes(inResource);
if (bytes==null)
return;
var input = new haxe.io.BytesInput(bytes);
nme.Assets.scriptBase = "";
runInput(input,verify);
}
else
{
var script = nme.Assets.getString(inResource);
if (script==null)
throw "Could not find resource script " + inResource;
#if (hxcpp_api_level>=320)
cpp.cppia.Host.run(script);
#end
}
#else
throw "Script not available on this platform";
#end
Expand Down
14 changes: 10 additions & 4 deletions nme/script/Server.hx
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,25 @@ class Server
if (directory==null)
{
var docsDir = nme.filesystem.File.documentsDirectory.nativePath;
directory = sys.FileSystem.fullPath( docsDir ) + "/" + inEngineName;
//directory = sys.FileSystem.fullPath( docsDir ) + "/" + inEngineName;
directory = docsDir + "/" + inEngineName;
trace("docs -> " + docsDir);
trace("Directory -> " + directory);
try
sys.FileSystem.createDirectory(directory)
{
sys.FileSystem.createDirectory(directory);
}
catch(e:Dynamic) {
directory = null;
trace("Error creating " + directory + ":" + e);
}
}
}
}

function readDir(outFiles:Array<String>, dir:String, file:String, recurse:Bool)
{
var path = file=="" ? dir : dir+"/"+file;
var path = file=="" ? dir :
(file.substr(0,1)=="/" || file.substr(0,1)=="\\" || file.substr(1,1)==":") ? file : dir+"/"+file;
if (!FileSystem.exists(path))
{
outFiles.push(" not a file - " + file);
Expand Down
4 changes: 2 additions & 2 deletions project/src/common/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ namespace nme

bool loadOggSampleFromFile(const char *inFileURL, QuickVec<unsigned char> &outBuffer, int *channels, int *bitsPerSample, int* outSampleRate)
{
FILE *f;
FILE *f = 0;

//Read the file data
#ifdef ANDROID
Expand All @@ -285,7 +285,7 @@ namespace nme

if (!f)
{
LOG_SOUND("FAILED to read sound file, file pointer as null?\n");
LOG_SOUND("FAILED to read \"%s\" file, file pointer as null?\n",inFileURL);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion project/src/iPhone/Sound.mm
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void stop()

this->data = nil;

std::string path = GetResourcePath() + gAssetBase + inFilename;
std::string path = inFilename[0]=='/' ? inFilename : GetResourcePath() + gAssetBase + inFilename;
NSString *ns_name = [[NSString alloc] initWithUTF8String:path.c_str()];
NSURL *theFileNameAndPathAsUrl = [NSURL fileURLWithPath:ns_name];
#ifndef OBJC_ARC
Expand Down
2 changes: 1 addition & 1 deletion project/src/openal/OpenALSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ namespace nme
GetBundleFilename(inFilename.c_str(),fileURL,1024);
#else
#ifdef IPHONE
std::string asset = GetResourcePath() + gAssetBase + inFilename;
std::string asset = inFilename[0]=='/' ? inFilename : GetResourcePath() + gAssetBase + inFilename;
const char *fileURL = asset.c_str();
#else
const char *fileURL = inFilename.c_str();
Expand Down
12 changes: 11 additions & 1 deletion tools/nme/src/helpers/FileHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import sys.io.File;
import sys.io.FileOutput;
import sys.FileSystem;
import neko.Lib;
import platforms.Platform;

class FileHelper
{
Expand Down Expand Up @@ -127,7 +128,16 @@ class FileHelper
PathHelper.mkdir(Path.directory(destination));

LogHelper.info("", " - Copying file: " + source + " -> " + destination);
File.copy(source, destination);

// Use system copy to preserve file permissions
if (PlatformHelper.hostPlatform == Platform.WINDOWS)
{
Sys.command("copy", [source, destination]);
}
else
{
Sys.command("cp", [source, destination]);
}
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions tools/nme/src/platforms/Platform.hx
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class Platform
else if (protocol=="nme")
{
if (project.command!="installer")
Log.error("Nme deployment can only be uised with the installer command");
Log.error("Nme deployment can only be used with the installer command");
var filename = getOutputDir() + "/" + project.app.file + ".nme";
if (!FileSystem.exists(filename))
Log.error('Could not find $filename to deploy');
Expand All @@ -419,9 +419,9 @@ class Platform
if (protocol=="bindir")
switch(PlatformHelper.hostPlatform)
{
case WINDOWS: arch="/Windows";
case MAC: arch="/Mac";
case LINUX: arch="/Linux";
case WINDOWS: arch="/Windows/" + project.app.file;
case MAC: arch="/Mac/" + project.app.file + ".app";
case LINUX: arch="/Linux/" + project.app.file;
default:Log.error("Unkown host platform for bindir, " + PlatformHelper.hostPlatform);
}

Expand Down

0 comments on commit 38c474d

Please sign in to comment.