Skip to content

Commit e4580ed

Browse files
Merge pull request #160 from DanTheMan827/add-obb
Backup/restore obb
2 parents 75ecfcc + bcaa639 commit e4580ed

File tree

3 files changed

+86
-9
lines changed

3 files changed

+86
-9
lines changed

QuestPatcher.Core/AndroidDebugBridge.cs

+7
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,13 @@ public async Task InstallApp(string apkPath)
542542
await RunShellCommand($"rm {pushPath}");
543543
}
544544

545+
public async Task<bool> Exists(string path)
546+
{
547+
var output = await RunShellCommand($"stat {path.WithForwardSlashes().EscapeBash()}");
548+
549+
return output.ExitCode == 0;
550+
}
551+
545552
public async Task CreateDirectory(string path)
546553
{
547554
await RunShellCommand($"mkdir -p {path.WithForwardSlashes().EscapeBash()}");

QuestPatcher.Core/InstallManager.cs

+47-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public class InstallManager : INotifyPropertyChanged
3636
};
3737

3838
private const string DataDirectoryTemplate = "/sdcard/Android/data/{0}/files";
39+
private const string ObbDirectoryTemplate = "/sdcard/Android/obb/{0}";
3940
private const string DataBackupTemplate = "/sdcard/QuestPatcher/{0}/backup";
41+
private const string ObbBackupTemplate = "/sdcard/QuestPatcher/{0}/obb";
4042

4143
/// <summary>
4244
/// The APK currently installed on the quest
@@ -51,6 +53,10 @@ public class InstallManager : INotifyPropertyChanged
5153
/// </summary>
5254
private string DataPath => string.Format(DataDirectoryTemplate, _config.AppId);
5355

56+
/// <summary>
57+
/// The /sdcard/Android/obb/... directory for the installed app
58+
/// </summary>
59+
private string ObbPath => string.Format(ObbDirectoryTemplate, _config.AppId);
5460

5561
private readonly string _currentlyInstalledPath;
5662
private readonly AndroidDebugBridge _debugBridge;
@@ -195,13 +201,16 @@ public async Task NewApkInstalled(string newApk)
195201
/// Creates a backup of the /sdcard/Android/data/... directory for the current app.
196202
/// </summary>
197203
/// <returns>The path to the backup on the quest</returns>
198-
public async Task<string> CreateDataBackup()
204+
public async Task<string?> CreateDataBackup()
199205
{
200206
string backupPath = string.Format(DataBackupTemplate, _config.AppId);
201207

202-
// Avoid failing if no files are present in the data directory
203-
// TODO: Perhaps check if it exists first and then skip backup if missing? This is more complex.
204-
await _debugBridge.CreateDirectory(DataPath);
208+
// Check if the data directory exists and skip if it doesn't.
209+
if (!await _debugBridge.Exists(DataPath))
210+
{
211+
return null;
212+
}
213+
205214
// Remove the backup path if it already exists and then recreate it
206215
await _debugBridge.RemoveDirectory(backupPath);
207216
await _debugBridge.CreateDirectory(backupPath);
@@ -211,6 +220,28 @@ public async Task<string> CreateDataBackup()
211220
return backupPath;
212221
}
213222

223+
/// <summary>
224+
/// Creates a backup of the /sdcard/Android/obb/... directory for the current app.
225+
/// </summary>
226+
/// <returns>The path to the backup on the quest</returns>
227+
public async Task<string?> CreateObbBackup()
228+
{
229+
string backupPath = string.Format(ObbBackupTemplate, _config.AppId);
230+
231+
// Check if the data directory exists and skip if it doesn't.
232+
if (!await _debugBridge.Exists(ObbPath))
233+
{
234+
return null;
235+
}
236+
237+
// Remove the backup path if it already exists
238+
await _debugBridge.RemoveDirectory(backupPath);
239+
// Copy all the files to the data backup
240+
await _debugBridge.Move(ObbPath, backupPath);
241+
242+
return backupPath;
243+
}
244+
214245
/// <summary>
215246
/// Restores a data backup created with CreateDataBackup.
216247
/// Will delete any mod/lib files in the backup to avoid old mods causing crashes.
@@ -228,6 +259,18 @@ public async Task RestoreDataBackup(string backupPath)
228259
await _debugBridge.RemoveDirectory(Path.Combine(DataPath, "mods"));
229260
}
230261

262+
/// <summary>
263+
/// Restores a obb backup created with CreateObbBackup.
264+
/// </summary>
265+
/// <param name="backupPath">The path to the backup on the quest</param>
266+
public async Task RestoreObbBackup(string backupPath)
267+
{
268+
// Remove the backup obb path if it already exists
269+
await _debugBridge.RemoveDirectory(ObbPath);
270+
// Move the obb backup to the original path.
271+
await _debugBridge.Move(backupPath, ObbPath);
272+
}
273+
231274
/// <summary>
232275
/// Uninstalls the current app
233276
/// </summary>

QuestPatcher.Core/Patching/PatchingManager.cs

+32-5
Original file line numberDiff line numberDiff line change
@@ -664,17 +664,32 @@ public async Task PatchApp()
664664
await apk.DisposeAsync();
665665
}
666666

667+
// Close any running instance of the app.
668+
await _debugBridge.ForceStop(_config.AppId);
669+
667670
Log.Information("Uninstalling the default APK . . .");
668671
Log.Information("Backing up data directory");
669-
string? backupPath;
672+
string? dataBackupPath;
670673
try
671674
{
672-
backupPath = await _installManager.CreateDataBackup();
675+
dataBackupPath = await _installManager.CreateDataBackup();
673676
}
674677
catch (Exception ex)
675678
{
676679
Log.Error(ex, "Failed to create data backup");
677-
backupPath = null; // Indicate that the backup failed
680+
dataBackupPath = null; // Indicate that the backup failed
681+
}
682+
683+
Log.Information("Backing up obb directory");
684+
string? obbBackupPath;
685+
try
686+
{
687+
obbBackupPath = await _installManager.CreateObbBackup();
688+
}
689+
catch (Exception ex)
690+
{
691+
Log.Error(ex, "Failed to create obb backup");
692+
obbBackupPath = null; // Indicate that the backup failed
678693
}
679694

680695
PatchingStage = PatchingStage.UninstallingOriginal;
@@ -692,19 +707,31 @@ public async Task PatchApp()
692707
PatchingStage = PatchingStage.InstallingModded;
693708
await _debugBridge.InstallApp(_patchedApkPath);
694709

695-
if (backupPath != null)
710+
if (dataBackupPath != null)
696711
{
697712
Log.Information("Restoring data backup");
698713
try
699714
{
700-
await _installManager.RestoreDataBackup(backupPath);
715+
await _installManager.RestoreDataBackup(dataBackupPath);
701716
}
702717
catch (Exception ex)
703718
{
704719
Log.Error(ex, "Failed to restore data backup");
705720
}
706721
}
707722

723+
if (obbBackupPath != null)
724+
{
725+
Log.Information("Restoring obb backup");
726+
try
727+
{
728+
await _installManager.RestoreObbBackup(obbBackupPath);
729+
}
730+
catch (Exception ex)
731+
{
732+
Log.Error(ex, "Failed to restore obb backup");
733+
}
734+
}
708735

709736
// Recreate the mod directories as they will not be present after the uninstall/backup restore
710737
await _modManager.CreateModDirectories();

0 commit comments

Comments
 (0)