1- using System . IO ;
1+ using System . Collections . Generic ;
2+ using System . IO ;
23using System . Text . Json ;
34using ComputerUtils . Android . Logging ;
45using 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}
0 commit comments