Skip to content

Commit

Permalink
new expansions format support
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarov-andrey committed Sep 13, 2012
1 parent 9b311ea commit 3874f62
Show file tree
Hide file tree
Showing 11 changed files with 647 additions and 112 deletions.
52 changes: 22 additions & 30 deletions src/Lightning.ml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ external rm : string -> string -> (unit -> unit) -> unit = "ml_rm";
external downloadExpansions : unit -> unit = "ml_downloadExpansions"; external downloadExpansions : unit -> unit = "ml_downloadExpansions";
external getExpansionPath : bool -> string = "ml_getExpansionPath"; external getExpansionPath : bool -> string = "ml_getExpansionPath";
external getExpansionVer : bool -> int = "ml_getExpansionVer"; external getExpansionVer : bool -> int = "ml_getExpansionVer";
external _extractExpansions : (bool -> unit) -> unit = "ml_extractExpansions";
external expansionExtracted : unit -> bool = "ml_expansionExtracted";


value unzipCbs = Hashtbl.create 0; value unzipCbs = Hashtbl.create 0;


Expand All @@ -146,13 +148,13 @@ value apkPath = _apkPath ();
value apkVer = _getVersion (); value apkVer = _getVersion ();
value externalStoragePath = _externalStoragePath (); value externalStoragePath = _externalStoragePath ();
value assetsVerFilename = externalStoragePath ^ "assets/a" ^ apkVer; value assetsVerFilename = externalStoragePath ^ "assets/a" ^ apkVer;
value expansionVerFilename = externalStoragePath ^ "assets/e" ^ (string_of_int (getExpansionVer True)); (* value expansionVerFilename = externalStoragePath ^ "assets/e" ^ (string_of_int (getExpansionVer True)); *)


value assetsExtracted () = value assetsExtracted () =
Sys.file_exists assetsVerFilename; Sys.file_exists assetsVerFilename;


value expansionExtracted () = (* value expansionExtracted () =
Sys.file_exists expansionVerFilename; Sys.file_exists expansionVerFilename; *)


value extractAssets cb = value extractAssets cb =
let assetsPath = externalStoragePath ^ "assets" in let assetsPath = externalStoragePath ^ "assets" in
Expand All @@ -172,16 +174,15 @@ value extractAssets cb =


value extractExpansions cb = value extractExpansions cb =
( (
Callback.register "expnsDownloadComplete" ( Callback.register "expnsDownloadComplete" (fun () -> _extractExpansions cb);
fun () -> unzip (getExpansionPath True) (externalStoragePath ^ "assets/") ( downloadExpansions ();
(* fun () -> unzip (getExpansionPath True) (externalStoragePath ^ "assets/") (
fun success -> fun success ->
( (
if success then close_out (open_out expansionVerFilename) else (); if success then close_out (open_out expansionVerFilename) else ();
cb success; cb success;
) )
) ) *)
);
downloadExpansions ();
); );


value extractAssetsIfRequired cb = value extractAssetsIfRequired cb =
Expand All @@ -193,28 +194,19 @@ value extractAssetsIfRequired cb =
else else
rm (ExtString.String.slice ~last:~-1 externalStoragePath) "assets" (fun () -> extractAssets cb); rm (ExtString.String.slice ~last:~-1 externalStoragePath) "assets" (fun () -> extractAssets cb);


value extractAssetsAndExpansionsIfRequired cb = value extractAssetsAndExpansionsIfRequired cb =
if (assetsExtracted ()) && (expansionExtracted ()) then let extractAssetsRes = ref None
( and extractExpansionRes = ref None in
setAssetsDir (externalStoragePath ^ "assets/"); let callCb () =
cb True; match (!extractAssetsRes, !extractExpansionRes) with
) [ (Some ear, Some eer) -> cb (ear && eer)
else | _ -> ()
let extractAssetsRes = ref None ]
and extractExpansionRes = ref None in in
let callCb () = (
match (!extractAssetsRes, !extractExpansionRes) with extractAssetsIfRequired (fun success -> ( extractAssetsRes.val := Some success; callCb (); ));
[ (Some ear, Some eer) -> cb (ear && eer) extractExpansions (fun success -> ( extractExpansionRes.val := Some success; callCb (); ));
| _ -> () );
]
in
let rmCb () =
(
extractAssets (fun success -> ( extractAssetsRes.val := Some success; callCb (); ));
extractExpansions (fun success -> ( extractExpansionRes.val := Some success; callCb (); ));
)
in
rm (ExtString.String.slice ~last:~-1 externalStoragePath) "assets" rmCb;


(* external test_c_fun: (unit -> unit) -> unit = "ml_test_c_fun"; *) (* external test_c_fun: (unit -> unit) -> unit = "ml_test_c_fun"; *)


Expand Down
3 changes: 1 addition & 2 deletions src/Lightning.mli
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ value setSupportEmail: string -> unit;
value getMACID: unit -> string; value getMACID: unit -> string;


value extractAssetsIfRequired: (bool -> unit) -> unit; value extractAssetsIfRequired: (bool -> unit) -> unit;
value extractAssetsAndExpansionsIfRequired: (bool -> unit) -> unit; value extractAssetsAndExpansionsIfRequired: (bool -> unit) -> unit;
(* value test_c_fun: (unit -> unit) -> unit; *)
2 changes: 2 additions & 0 deletions src/Texture.ml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ value zero : c =


type imageInfo; type imageInfo;
external loadImage: ?textureID:textureID -> ~path:string -> ~suffix:option string -> filter -> bool -> textureInfo = "ml_loadImage"; external loadImage: ?textureID:textureID -> ~path:string -> ~suffix:option string -> filter -> bool -> textureInfo = "ml_loadImage";


(* external loadImage: ?textureID:textureID -> ~path:string -> ~suffix:option string -> filter -> unit = "ml_loadImage"; (* external loadImage: ?textureID:textureID -> ~path:string -> ~suffix:option string -> filter -> unit = "ml_loadImage";
value zero_textureInfo = value zero_textureInfo =
{ {
Expand Down
27 changes: 19 additions & 8 deletions src/android/assets_extractor.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -527,25 +527,36 @@ void ml_downloadExpansions() {
static jmethodID gGetExpansionPathMid; static jmethodID gGetExpansionPathMid;
static jmethodID gGetExpansionVerMid; static jmethodID gGetExpansionVerMid;


value ml_getExpansionPath(value isMain) { char* get_expansion_path(int main) {
CAMLparam1(isMain);
CAMLlocal1(vpath);

JNIEnv *env; JNIEnv *env;
(*gJavaVM)->GetEnv(gJavaVM, (void**) &env, JNI_VERSION_1_4); (*gJavaVM)->AttachCurrentThread(gJavaVM, &env, NULL);
//(*gJavaVM)->GetEnv(gJavaVM, (void**) &env, JNI_VERSION_1_4);


if (!gGetExpansionPathMid) { if (!gGetExpansionPathMid) {
gGetExpansionPathMid = (*env)->GetMethodID(env, jViewCls, "getExpansionPath", "(Z)Ljava/lang/String;"); gGetExpansionPathMid = (*env)->GetMethodID(env, jViewCls, "getExpansionPath", "(Z)Ljava/lang/String;");
} }


jstring jpath = (*env)->CallObjectMethod(env, jView, gGetExpansionPathMid, Bool_val(isMain)); jstring jpath = (*env)->CallObjectMethod(env, jView, gGetExpansionPathMid, main);
if (!jpath) caml_failwith("Expansion path not specified"); if (!jpath) caml_failwith("Expansion path not specified");
const char* cpath = (*env)->GetStringUTFChars(env, jpath, JNI_FALSE); const char* cpath = (*env)->GetStringUTFChars(env, jpath, JNI_FALSE);
vpath = caml_copy_string(cpath);
char* retval = (char*)malloc(strlen(cpath) + 1);
strcpy(retval, cpath);


(*env)->ReleaseStringUTFChars(env, jpath, cpath); (*env)->ReleaseStringUTFChars(env, jpath, cpath);
(*env)->DeleteLocalRef(env, jpath); (*env)->DeleteLocalRef(env, jpath);


return retval;
}

value ml_getExpansionPath(value isMain) {
CAMLparam1(isMain);
CAMLlocal1(vpath);

char* cpath = get_expansion_path(Bool_val(isMain));
vpath = caml_copy_string(cpath);
free(cpath);

CAMLreturn(vpath); CAMLreturn(vpath);
} }


Expand Down
3 changes: 2 additions & 1 deletion src/android/assets_extractor.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
void ml_miniunz(value vzipPath, value vdstPath, value testPathFunc); void ml_miniunz(value vzipPath, value vdstPath, value testPathFunc);
JNIEXPORT void JNICALL Java_ru_redspell_lightning_LightView_00024UnzipCallbackRunnable_run(JNIEnv *env, jobject this); JNIEXPORT void JNICALL Java_ru_redspell_lightning_LightView_00024UnzipCallbackRunnable_run(JNIEnv *env, jobject this);
value ml_apkPath(); value ml_apkPath();
value ml_externalStoragePath(); value ml_externalStoragePath();
char* get_expansion_path(int main);
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ public static String getExpansionAPKFileName(Context c, boolean mainFile, int ve
static public String generateSaveFileName(Context c, String fileName) { static public String generateSaveFileName(Context c, String fileName) {
String path = getSaveFilePath(c) String path = getSaveFilePath(c)
+ File.separator + fileName; + File.separator + fileName;

Log.d("LIGHTNING", "generateSaveFileName " + path);

return path; return path;
} }


Expand Down
25 changes: 23 additions & 2 deletions src/android/java/src/ru/redspell/lightning/LightMediaPlayer.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.io.IOException; import java.io.IOException;
import java.io.FileInputStream;


public class LightMediaPlayer extends MediaPlayer { public class LightMediaPlayer extends MediaPlayer {
private static ArrayList<LightMediaPlayer> instances; private static ArrayList<LightMediaPlayer> instances;
Expand Down Expand Up @@ -94,11 +95,27 @@ public static void pauseAll() {
} }
} }


private static class OffsetSizePair {
public int offset;
public int size;

public OffsetSizePair(int offset, int size) {
this.offset = offset;
this.size = size;
}
}

private static native OffsetSizePair getOffsetSizePair(String path);

public static MediaPlayer createMediaPlayer(String assetsDir, String path) throws IOException { public static MediaPlayer createMediaPlayer(String assetsDir, String path) throws IOException {
MediaPlayer mp = new LightMediaPlayer(); MediaPlayer mp = new LightMediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC); mp.setAudioStreamType(AudioManager.STREAM_MUSIC);


if (assetsDir != null) { OffsetSizePair pair = getOffsetSizePair(path);

if (pair != null) {
mp.setDataSource((new FileInputStream(LightView.instance.getExpansionPath(true))).getFD(), pair.offset, pair.size);
} else if (assetsDir != null) {
mp.setDataSource(assetsDir + (assetsDir.charAt(assetsDir.length() - 1) == '/' ? "" : "/") + path); mp.setDataSource(assetsDir + (assetsDir.charAt(assetsDir.length() - 1) == '/' ? "" : "/") + path);
} else { } else {
AssetFileDescriptor afd = LightView.instance.getContext().getAssets().openFd(path); AssetFileDescriptor afd = LightView.instance.getContext().getAssets().openFd(path);
Expand All @@ -109,7 +126,11 @@ public static MediaPlayer createMediaPlayer(String assetsDir, String path) throw
} }


public static int getSoundId(String path, SoundPool sndPool) throws IOException { public static int getSoundId(String path, SoundPool sndPool) throws IOException {
if (path.charAt(0) == '/') { OffsetSizePair pair = getOffsetSizePair(path);

if (pair != null) {
sndPool.load((new FileInputStream(LightView.instance.getExpansionPath(true))).getFD(), pair.offset, pair.size, 1);
} else if (path.charAt(0) == '/') {
return sndPool.load(path, 1); return sndPool.load(path, 1);
} }


Expand Down
14 changes: 14 additions & 0 deletions src/android/java/src/ru/redspell/lightning/LightView.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -646,4 +646,18 @@ public void curlExternalLoaderError(int req, int errCode, int errMes) {
Log.d("LIGHTNING", "curlExternalLoaderError " + errCode + " " + errMes); Log.d("LIGHTNING", "curlExternalLoaderError " + errCode + " " + errMes);
queueEvent(new CurlExternErrorCallbackRunnable(req, errCode, errMes)); queueEvent(new CurlExternErrorCallbackRunnable(req, errCode, errMes));
} }

private static class ExpansionsCallbackRunnable implements Runnable {
private int cb;

public ExpansionsCallbackRunnable(int cb) {
this.cb = cb;
}

public native void run();
}

public void callExpansionsComplete(int cb) {
queueEvent(new ExpansionsCallbackRunnable(cb));
}
} }
Loading

0 comments on commit 3874f62

Please sign in to comment.