private void ExtractAssetsFromApkToExternalStorage(string apkFilename, List <string> excludePaths = null, bool fromBakFiles = false) { UpdateStatus("Extracting assets files from the APK to external storage..."); using (var apk = new ZipFileProvider(apkFilename, FileCacheMode.None, false, QuestomAssets.Utils.FileUtils.GetTempDirectory())) { foreach (var assetFilename in apk.FindFiles(APK_ASSETS_PATH + "*")) { string relativeFilename = assetFilename.Substring(APK_ASSETS_PATH.Length); if (excludePaths != null) { if (excludePaths.Any(x => relativeFilename.StartsWith(x))) { Log.LogMsg($"The asset file {assetFilename} ({relativeFilename}) is not included in assets that should be extracted, skipping."); continue; } } Log.LogMsg($"Extracting {assetFilename}..."); string targetFile = Path.Combine(Constants.ASSETS_RELOC_PATH, relativeFilename); string dirName = Path.GetDirectoryName(targetFile); try { if (!Directory.Exists(dirName)) { Log.LogMsg($"Assets target directory doesn't exist, creating {dirName}"); Directory.CreateDirectory(dirName); } } catch (Exception ex) { Log.LogErr($"Unable to create directory {dirName}!", ex); UpdateStatus("Failed to create assets directory in external storage!"); throw new ModException($"Unable to create directory {dirName}!", ex); } try { using (var readStream = apk.GetReadStream(assetFilename, true)) { if (targetFile.EndsWith(".bobak")) { targetFile = targetFile.Substring(0, targetFile.Length - 6); } using (var fs = File.Open(targetFile, FileMode.Create, FileAccess.Write)) { readStream.CopyTo(fs); } } if (!assetFilename.EndsWith(".bobak")) { apk.Rename(assetFilename, assetFilename + ".bobak"); } } catch (Exception ex) { Log.LogErr($"Failed to extract {assetFilename} to {targetFile}", ex); UpdateStatus("Failed extracting an asset from the APK to external storage!"); throw new ModException($"Failed to extract {assetFilename} to {targetFile}", ex); } } apk.Save(); } }
/// <summary> /// Checks if the backup exists, or creates a very unideal backup of the modded APK for the sake of uninstalling mods /// </summary> public void CheckCreateModdedBackup() { try { if (File.Exists(Constants.BEATSABER_APK_BACKUP_FILE)) { return; } Log.LogErr("WARNING: backup of original Beat Saber APK does not exist. Will attempt to fall back to a post-mod backup."); if (File.Exists(Constants.BEATSABER_APK_MODDED_BACKUP_FILE)) { return; } Log.LogErr("Post-modded backup of Beat Saber APK does not exist, will attempt to create one."); if (!IsBeatSaberInstalled) { Log.LogErr("Beat saber isn't even installed. Can't make any sort of backup."); return; } var apkPath = FindBeatSaberApk(); if (apkPath == null) { Log.LogErr("Unable to find the installed Beat Saber APK path. Cannot make a post-mod backup."); return; } Log.LogErr("Copying APK for unideal backup..."); try { File.Copy(apkPath, Constants.BEATSABER_APK_MODDED_BACKUP_FILE, true); } catch (Exception ex) { Log.LogErr("Exception trying to copy APK for half assed backup.", ex); return; } try { Log.LogErr("Restoring names of modded APK asset files so they can serve as a backup..."); using (var apk = new ZipFileProvider(Constants.BEATSABER_APK_MODDED_BACKUP_FILE, FileCacheMode.None, false, QuestomAssets.Utils.FileUtils.GetTempDirectory())) { foreach (var assetFilename in apk.FindFiles(APK_ASSETS_PATH + "*")) { if (assetFilename.EndsWith(".bobak")) { apk.Rename(assetFilename, assetFilename.Substring(0, assetFilename.Length - 6)); } } apk.Save(); } } catch (Exception ex) { Log.LogErr("Exception trying to make a half assed, post-mod backup while renaming files within the APK", ex); } } catch (Exception ex) { Log.LogErr($"Exception trying to check/create backups", ex); } }