@@ -188,7 +188,7 @@ public static void unpackAsset(
188188 os .write (dataVersion .getBytes ());
189189 os .close ();
190190 } catch (Exception e ) {
191- Log .w ("python" , e );
191+ Log .w (TAG , e );
192192 }
193193 }
194194 }
@@ -201,23 +201,57 @@ public static void unpackPyBundle(
201201
202202 Log .v (TAG , "Unpacking " + resource + " " + target .getName ());
203203
204- // FIXME: Implement a versioning logic to speed-up the startup process (maybe hash-based?).
204+ // The version of data in memory and on disk.
205+ String dataVersion = getResourceString (ctx , "private_version" );
206+ String diskVersion = null ;
205207
206- // If the disk data is out of date, extract it and write the version file.
207- Log .v (TAG , "Extracting " + resource + " assets." );
208+ Log .v (TAG , "Data version is " + dataVersion );
209+
210+ // If no version, no unpacking is necessary.
211+ if (dataVersion == null ) {
212+ return ;
213+ }
214+
215+ // Check the current disk version, if any.
216+ String filesDir = target .getAbsolutePath ();
217+ String diskVersionFn = filesDir + "/" + "libpybundle" + ".version" ;
208218
209- if (cleanup_on_version_update ) {
210- recursiveDelete (target );
219+ try {
220+ byte buf [] = new byte [64 ];
221+ InputStream is = new FileInputStream (diskVersionFn );
222+ int len = is .read (buf );
223+ diskVersion = new String (buf , 0 , len );
224+ is .close ();
225+ } catch (Exception e ) {
226+ diskVersion = "" ;
211227 }
212- target .mkdirs ();
213-
214- AssetExtract ae = new AssetExtract (ctx );
215- if (!ae .extractTar (resource + ".so" , target .getAbsolutePath (), "pybundle" )) {
216- String msg = "Could not extract " + resource + " data." ;
217- if (ctx instanceof Activity ) {
218- toastError ((Activity )ctx , msg );
219- } else {
220- Log .v (TAG , msg );
228+
229+ if (! dataVersion .equals (diskVersion )) {
230+ // If the disk data is out of date, extract it and write the version file.
231+ Log .v (TAG , "Extracting " + resource + " assets." );
232+
233+ if (cleanup_on_version_update ) {
234+ recursiveDelete (target );
235+ }
236+ target .mkdirs ();
237+
238+ AssetExtract ae = new AssetExtract (ctx );
239+ if (!ae .extractTar (resource + ".so" , target .getAbsolutePath (), "pybundle" )) {
240+ String msg = "Could not extract " + resource + " data." ;
241+ if (ctx instanceof Activity ) {
242+ toastError ((Activity )ctx , msg );
243+ } else {
244+ Log .v (TAG , msg );
245+ }
246+ }
247+
248+ try {
249+ // Write version file.
250+ FileOutputStream os = new FileOutputStream (diskVersionFn );
251+ os .write (dataVersion .getBytes ());
252+ os .close ();
253+ } catch (Exception e ) {
254+ Log .w (TAG , e );
221255 }
222256 }
223257 }
0 commit comments