private boolean upgradePlugin(int plugin) { try { String srcDir = Plugins.getUpgradePath(Plugins.Plugin.convert(plugin)); if (srcDir.length() == 0) return true; // no upgrade to do String dstDir = Preferences.getAngbandFilesDirectory(plugin) + "/save"; // Log.d("Angband","upgrade "+srcDir+" to "+dstDir); File fdstDir = new File(dstDir); File[] saveFiles = fdstDir.listFiles(); if (saveFiles != null && saveFiles.length > 0) return true; // save files found in dst (already upgraded) // Log.d("Angband","found no save files in dst"); File fsrcDir = new File(srcDir); saveFiles = fsrcDir.listFiles(); if (saveFiles == null || saveFiles.length == 0) return true; // no save files found in src // Log.d("Angband","found "+saveFiles.length+" save files in src"); // upgrade! for (int i = 0; i < saveFiles.length; i++) { String src = saveFiles[i].getAbsolutePath(); String dst = dstDir + "/" + saveFiles[i].getName(); // Log.d("Angband","upgrade "+src+" to "+dst); InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } return true; } catch (Exception e) { message = "Error: failed to copy save file(s) from prior version. " + e.getMessage(); // Log.v("Angband", "error upgrading save files: " + e); return false; } }
private boolean doesCrcMatch(int plugin) { // Log.d("Angband","doesCrcMatch "+plugin); boolean result = false; try { File f = new File(Preferences.getAngbandFilesDirectory(plugin)); f.mkdirs(); String filename = f.getAbsolutePath() + "/crc" + Plugins.getFilesDir(Plugins.Plugin.convert(plugin)); File myfile = new File(filename); FileInputStream fis = new FileInputStream(myfile); String currentCrc = new Scanner(fis).useDelimiter("\\A").next().trim(); // Log.d("Angband","doesCrcMatch.currentcrc="+currentCrc); // Log.d("Angband","doesCrcMatch.plugincrc="+Plugins.getPluginCrc(plugin)); result = (Plugins.getPluginCrc(plugin).compareTo(currentCrc) == 0); } catch (Exception e) { Log.v("Angband", "doesCrcMatch.error reading crc: " + e); } return result; }
private boolean extractPluginResources(int plugin) { // Log.d("Angband","extractPluginResources "+plugin); boolean result = true; try { File f = new File(Preferences.getAngbandFilesDirectory(plugin)); f.mkdirs(); String abs_path = f.getAbsolutePath(); // Log.v("Angband", "installing to " + abs_path); // copy game files ZipInputStream zis = Plugins.getPluginZip(plugin); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { String ze_name = ze.getName(); // Log.v("Angband", "extracting " + ze_name); String filename = abs_path + "/" + ze_name; File myfile = new File(filename); if (ze.isDirectory()) { myfile.mkdirs(); continue; } byte contents[] = new byte[(int) ze.getSize()]; FileOutputStream fos = new FileOutputStream(myfile); int remaining = (int) ze.getSize(); int totalRead = 0; while (remaining > 0) { int readlen = zis.read(contents, 0, remaining); fos.write(contents, 0, readlen); totalRead += readlen; remaining -= readlen; } fos.close(); // perform a basic length validation myfile = new File(filename); if (myfile.length() != totalRead) { // Log.v("Angband", "Installer.length mismatch: " + filename); message = "Error: failed to verify installed file on sdcard: " + filename; throw new IllegalStateException(); } zis.closeEntry(); } zis.close(); // update crc file File myfile_crc = new File(abs_path + "/crc" + Plugins.getFilesDir(Plugins.Plugin.convert(plugin))); if (myfile_crc.exists()) myfile_crc.delete(); Writer crc_out = new OutputStreamWriter(new FileOutputStream(myfile_crc)); String crc_val = Plugins.getPluginCrc(plugin); crc_out.write(crc_val); crc_out.close(); } catch (Exception e) { result = false; if (message.length() == 0) message = "Error: failed to install files to sdcard. " + e.getMessage(); // Log.v("Angband", "error extracting files: " + e); } return result; }