Skip to content

Commit dbec95c

Browse files
committed
Start on updating diff downgrading
1 parent 13d3220 commit dbec95c

9 files changed

Lines changed: 111 additions & 69 deletions

File tree

QuestAppVersionSwitcher/Assets/html/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<div style="width: 100%;">Welcome to Quest App Version Switcher</div>
1313
<div style="font-size: 80%; width: 100%;">
1414
Managing <div class="inline packageName">some game</div>
15+
<br>
16+
<div id="currentVersion"></div>
1517
</div>
1618
</div>
1719
<div class="menuItem selected" section="backup">Backup</div>

QuestAppVersionSwitcher/Assets/html/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ document.getElementById("logintoken").onclick = () => {
469469

470470
function UpdateVersion(version) {
471471
currentGameVersion = version
472+
document.getElementById("currentVersion").innerText = currentGameVersion
472473
}
473474

474475
var isGamePatched = false
Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System.Collections.Generic;
2+
using System.IO;
23
using System.Text.Json;
34
using ComputerUtils.Android.Logging;
45
using QuestAppVersionSwitcher.Core;
@@ -8,60 +9,66 @@ namespace QuestAppVersionSwitcher.DiffDowngrading
89
{
910
public class DiffCreator
1011
{
11-
public static void CreateDiff(string appId, string sourceBackup, string targetBackup, string outputDir)
12+
public static DiffDowngradeEntry CreateDiff(string appId, string sourceBackup, string targetBackup, string outputDir)
1213
{
13-
DiffDowngradeEntry e = new DiffDowngradeEntry();
14-
e.appid = appId;
15-
// add apk entry
16-
17-
string sourcePath = CoreService.coreVars.QAVSTmpDowngradeDir + "source";
18-
string targetPath = CoreService.coreVars.QAVSTmpDowngradeDir + "target";
19-
FileStream sourceStream = File.Create(sourcePath);
20-
FileStream targetStream = File.Create(targetPath);
21-
long sourceIndexCounter = 0;
22-
long targetIndexCounter = 0;
23-
e.parts.Add(GetPart(sourceBackup + "app.apk", targetBackup + "app.apk", ref sourceStream, ref targetStream, ref sourceIndexCounter, ref targetIndexCounter, true));
24-
25-
// Add obbs
26-
// ToDo
14+
DiffDowngradeEntry baseEntry = new DiffDowngradeEntry();
15+
baseEntry.appid = appId;
16+
if (!sourceBackup.EndsWith(Path.DirectorySeparatorChar)) sourceBackup += Path.DirectorySeparatorChar;
17+
if (!targetBackup.EndsWith(Path.DirectorySeparatorChar)) targetBackup += Path.DirectorySeparatorChar;
18+
if (!outputDir.EndsWith(Path.DirectorySeparatorChar)) outputDir += Path.DirectorySeparatorChar;
2719

2820
Logger.Log("Creating diff");
29-
sourceStream.Flush();
30-
targetStream.Flush();
31-
sourceStream.Dispose();
32-
targetStream.Dispose();
33-
e.SSHA256 = Utils.GetSHA256OfFile(sourcePath);
34-
e.TSHA256 = Utils.GetSHA256OfFile(targetPath);
35-
e.SV = BackupManager.GetBackupInfo(sourceBackup).gameVersion;
36-
e.TV = BackupManager.GetBackupInfo(targetBackup).gameVersion;
37-
e.isXDelta3 = true;
38-
e.SourceByteSize = sourceIndexCounter;
39-
e.TargetByteSize = targetIndexCounter;
40-
Logger.Log("Index entry: " + JsonSerializer.Serialize(e));
21+
baseEntry.SV = BackupManager.GetBackupInfo(sourceBackup).gameVersion;
22+
baseEntry.TV = BackupManager.GetBackupInfo(targetBackup).gameVersion;
23+
baseEntry.isXDelta3 = true;
4124

42-
//Xdelta3Lib.Encode()
25+
// Create entries
26+
baseEntry.Set(CreateDiffOfFile(baseEntry, sourceBackup + "app.apk", targetBackup + "app.apk", outputDir));
27+
// add obbs and other files
28+
string[] sourceDirFiles = Directory.GetFiles(sourceBackup + "/obb/" + appId + "/");
29+
string[] targetDirFiles = Directory.GetFiles(targetBackup + "/obb/" + appId + "/");
30+
for(int i = 0; i < targetDirFiles.Length; i++)
31+
{
32+
// generate one diff for every target backup obbs
33+
baseEntry.otherFiles.Add(CreateDiffOfFile(baseEntry, sourceDirFiles[i % sourceDirFiles.Length], targetDirFiles[i], outputDir));
34+
}
35+
36+
// The diff file is now created
37+
return baseEntry;
4338
}
44-
45-
public static DiffDowngradeFilePart GetPart(string sourceFilePath, string targetFilePath, ref FileStream sourceStream, ref FileStream targetStream, ref long sourceIndexCounter, ref long targetIndexCounter, bool isApk)
39+
40+
public static FileDiffDowngradeEntry CreateDiffOfFile(DiffDowngradeEntry baseEntry, string sourcePath, string targetPath,
41+
string outputPath)
4642
{
47-
FileStream sourceFileStream = File.OpenRead(sourceFilePath);
48-
FileStream targetFileStream = File.OpenRead(targetFilePath);
49-
DiffDowngradeFilePart part = new DiffDowngradeFilePart();
50-
part.filename = Path.GetFileName(sourceFilePath);
51-
part.sourceByteStartIndex = sourceIndexCounter;
52-
part.sourceByteLength = sourceFileStream.Length;
53-
part.targetByteStartIndex = targetIndexCounter;
54-
part.targetByteLength = targetFileStream.Length;
55-
part.isApk = isApk;
56-
sourceIndexCounter += sourceFileStream.Length;
57-
targetIndexCounter += targetFileStream.Length;
58-
Logger.Log("Adding part " + part.filename + " to diff: " + JsonSerializer.Serialize(part));
43+
FileDiffDowngradeEntry e = new FileDiffDowngradeEntry();
44+
e.sourceFilename = Path.GetFileName(sourcePath);
45+
e.outputFilename = Path.GetFileName(targetPath);
46+
e.diffFilename = baseEntry.GetDowngradeBaseName() + e.sourceFilename + ".diff";
47+
e.type = e.sourceFilename.ToLower().EndsWith(".apk") ? FileDiffDowngradeEntryType.Apk : FileDiffDowngradeEntryType.Obb;
48+
e.isXDelta3 = true;
49+
e.SourceByteSize = new FileInfo(sourcePath).Length;
50+
e.TargetByteSize = new FileInfo(targetPath).Length;
51+
e.SSHA256 = Utils.GetSHA256OfFile(sourcePath);
52+
e.TSHA256 = Utils.GetSHA256OfFile(targetPath);
53+
e.download = "";
54+
e.isDirectDownload = true;
55+
string diffPath = outputPath + e.diffFilename;
56+
using (Stream sourceStream = File.OpenRead(sourcePath))
57+
{
58+
using(Stream targetStream = File.OpenRead(targetPath))
59+
{
60+
using(Stream diffStream = File.OpenWrite(diffPath))
61+
{
62+
VCDiff.Encoders.VcEncoder encoder = new VCDiff.Encoders.VcEncoder(sourceStream, targetStream, diffStream);
63+
Logger.Log("Encoding diff file for " + e.sourceFilename + " -> " + e.outputFilename + " to " + e.diffFilename);
64+
encoder.Encode();
65+
encoder.Dispose();
66+
}
67+
}
68+
}
5969

60-
sourceFileStream.CopyTo(sourceStream);
61-
targetFileStream.CopyTo(targetStream);
62-
sourceFileStream.Dispose();
63-
targetFileStream.Dispose();
64-
return part;
70+
e.DSHA256 = Utils.GetSHA256OfFile(diffPath);
71+
return e;
6572
}
6673
}
6774
}

QuestAppVersionSwitcher/DiffDowngrading/DiffDowngradeEntry.cs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,55 @@
22

33
namespace QuestAppVersionSwitcher.DiffDowngrading
44
{
5-
public class DiffDowngradeEntry
5+
public class DiffDowngradeEntry : FileDiffDowngradeEntry
66
{
77
public string SV { get; set; }
88
public string TV { get; set; }
9+
public string appid { get; set; }
10+
11+
public string GetDowngradeBaseName()
12+
{
13+
return appid + "." + SV + "TO" + TV + ".";
14+
}
15+
public List<FileDiffDowngradeEntry> otherFiles { get; set; }
16+
}
17+
18+
public class FileDiffDowngradeEntry
19+
{
20+
public string sourceFilename { get; set; } = "";
21+
public string diffFilename { get; set; } = "";
22+
public string outputFilename { get; set; } = "";
23+
public FileDiffDowngradeEntryType type = FileDiffDowngradeEntryType.Apk;
24+
public bool isXDelta3 { get; set; }
25+
public long TargetByteSize { get; set; }
26+
public long SourceByteSize { get; set; }
927
public string SSHA256 { get; set; }
1028
public string DSHA256 { get; set; }
1129
public string TSHA256 { get; set; }
1230
public string download { get; set; }
1331
public bool isDirectDownload { get; set; }
14-
public string appid { get; set; }
15-
public bool isXDelta3 { get; set; }
16-
public long TargetByteSize { get; set; }
17-
public long SourceByteSize { get; set; }
18-
public List<DiffDowngradeFilePart> parts { get; set; }
19-
}
2032

21-
public class DiffDowngradeFilePart
33+
public void Set(FileDiffDowngradeEntry e)
34+
{
35+
this.sourceFilename = e.sourceFilename;
36+
this.outputFilename = e.outputFilename;
37+
this.type = e.type;
38+
this.isXDelta3 = e.isXDelta3;
39+
this.TargetByteSize = e.TargetByteSize;
40+
this.SourceByteSize = e.SourceByteSize;
41+
this.SSHA256 = e.SSHA256;
42+
this.DSHA256 = e.DSHA256;
43+
this.TSHA256 = e.TSHA256;
44+
this.download = e.download;
45+
this.isDirectDownload = e.isDirectDownload;
46+
}
47+
}
48+
49+
public enum FileDiffDowngradeEntryType
2250
{
23-
public string filename { get; set; } = "";
24-
public bool isApk { get; set; } = false;
25-
public long sourceByteStartIndex { get; set; } = 0;
26-
public long sourceByteLength { get; set; } = 0;
27-
public long targetByteStartIndex { get; set; } = 0;
28-
public long targetByteLength { get; set; } = 0;
51+
Unknown = -1,
52+
Apk = 0,
53+
Obb = 1
2954
}
3055

3156
public class DiffDowngradeEntryContainer

QuestAppVersionSwitcher/DiffDowngrading/DiffDowngrader.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using ComputerUtils.Android.AndroidTools;
88
using ComputerUtils.Android.Encryption;
99
using ComputerUtils.Android.FileManaging;
10+
using ComputerUtils.Android.Logging;
1011
using ComputerUtils.Android.VarUtils;
1112
using JetBrains.Annotations;
1213
using OculusGraphQLApiLib;
@@ -144,8 +145,11 @@ public void Done()
144145
using(FileStream patch = File.OpenRead(diffFileDownloadPath)) {
145146
using (FileStream output = File.Create(backupDir + "app.apk"))
146147
{
147-
PleOps.XdeltaSharp.Decoder.Decoder decoder = new PleOps.XdeltaSharp.Decoder.Decoder(input, patch, output);
148-
decoder.Run();
148+
VCDiff.Decoders.VcDecoder decoder = new VCDiff.Decoders.VcDecoder(input, patch, output);
149+
long bytesWritten;
150+
Logger.Log("Decoding diff file for " + gameName + " " + version + " to " + backupDir + "app.apk");
151+
decoder.Decode(out bytesWritten);
152+
Logger.Log("Wrote " + bytesWritten + " bytes to " + backupDir + "app.apk");
149153
decoder.Dispose();
150154
}
151155
}

QuestAppVersionSwitcher/PatchingManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class PatchingManager
3535
public const string ManifestPath = "AndroidManifest.xml";
3636
public static readonly Uri AndroidNamespaceUri = new Uri("http://schemas.android.com/apk/res/android");
3737

38-
public static readonly string mainScotlandLoaderVersion = "v0.1.0-alpha";
38+
public static readonly string mainScotlandLoaderVersion = "v0.2.0-alpha";
3939
public static readonly string scotland2Version = "v0.1.4";
4040
public static readonly string questLoaderVersion = "v1.3.0";
4141

@@ -67,7 +67,7 @@ public static void DownloadDependencies()
6767
{
6868
string currentMainLoaderScotlandVersion = File.Exists(mainLoaderScotlandVersionLocation) ? File.ReadAllText(mainLoaderScotlandVersionLocation) : "";
6969
string currentSL2Version = File.Exists(scotland2VersionLocation) ? File.ReadAllText(scotland2VersionLocation) : "";
70-
DownloadFileIfMissing(currentMainLoaderScotlandVersion, mainScotlandLoaderVersion, libMainScotlandPath, "https://github.com/sc2ad/LibMainLoader/releases/download/" + mainScotlandLoaderVersion + "/libmain.so");
70+
DownloadFileIfMissing(currentMainLoaderScotlandVersion, mainScotlandLoaderVersion, libMainScotlandPath, "https://cdn.discordapp.com/attachments/769457887415894036/1223477597556441269/libmain.so?ex=6619ff4e&is=66078a4e&hm=384c446d81a5db5f513f23420961cb45bb756819e2ad34ac809f5472a0e45dc6&");
7171
DownloadFileIfMissing(currentSL2Version, scotland2Version, libScotland2Path, "https://github.com/sc2ad/scotland2/releases/download/" + scotland2Version + "/libsl2.so");
7272
File.WriteAllText(mainLoaderScotlandVersionLocation, mainScotlandLoaderVersion);
7373
File.WriteAllText(scotland2VersionLocation, scotland2Version);

QuestAppVersionSwitcher/Properties/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.15.19" package="com.ComputerElite.questappversionswitcher" android:installLocation="preferExternal" android:versionCode="140">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.15.20" package="com.ComputerElite.questappversionswitcher" android:installLocation="preferExternal" android:versionCode="141">
33
<uses-sdk android:minSdkVersion="28" android:targetSdkVersion="32" />
44
<uses-permission android:name="oculus.permission.handtracking" />
55
<uses-permission android:name="com.oculus.permission.HAND_TRACKING" />

QuestAppVersionSwitcher/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
// Minor Version
2323
// Build Number
2424
// Revision
25-
[assembly: AssemblyVersion("1.15.19.0")]
26-
[assembly: AssemblyFileVersion("1.15.19.0")]
25+
[assembly: AssemblyVersion("1.15.20.0")]
26+
[assembly: AssemblyFileVersion("1.15.20.0")]

QuestAppVersionSwitcher/QuestAppVersionSwitcher.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@
244244
<PackageReference Include="System.Text.Json">
245245
<Version>8.0.0-preview.3.23174.8</Version>
246246
</PackageReference>
247+
<PackageReference Include="VCDiff">
248+
<Version>4.0.1</Version>
249+
</PackageReference>
247250
<PackageReference Include="Xamarin.Android.Arch.Work.Runtime">
248251
<Version>1.0.0.3</Version>
249252
</PackageReference>

0 commit comments

Comments
 (0)