Skip to content

Commit 2ec75ba

Browse files
ComputerEliteComputerElite
authored andcommitted
Imagine using the wrong method to write to a stream
1 parent a168f5c commit 2ec75ba

7 files changed

Lines changed: 39 additions & 16 deletions

File tree

QuestAppVersionSwitcher/Assets/html/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ <h2>You can download mods and cosmetics from the following sites. Only QMods are
204204
<div class="button" id="requestAppObbPermission">Request app obb permission</div>
205205
<div class="buttonLabel">Requests permissions to access the currently selected apps obb directory</div>
206206
</div>
207+
<div class="buttonContainer">
208+
<div class="button" id="requestManageStorageAppPermission">Allow manage storage permission for selected app</div>
209+
<div class="buttonLabel">Opens settings to allow manage storage for the selected app</div>
210+
</div>
207211
<div class="buttonContainer">
208212
<div class="button" id="checkUpdate">Check for QuestAppVersionSwitcher updates</div>
209213
<div class="buttonLabel">Checks if updates for QuestAppVersionSwitcher exist</div>

QuestAppVersionSwitcher/Assets/html/script.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,11 @@ document.getElementById("grantAccess").onclick = () => {
712712
})
713713
}
714714

715+
document.getElementById("requestManageStorageAppPermission").onclick = () => {
716+
fetch("grantmanagestorageappaccess?package=" + config.currentApp).then(res => {
717+
})
718+
}
719+
715720
document.getElementById("requestAppPermission").onclick = () => {
716721
fetch("grantaccess?package=" + config.currentApp).then(res => {
717722
res.text().then(text => {

QuestAppVersionSwitcher/FolderPermission.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using Java.Util.Logging;
1818
using QuestAppVersionSwitcher.Core;
1919
using File = System.IO.File;
20-
using Logger = ComputerUtils.Android.Logging.Logger;
2120

2221
namespace QuestAppVersionSwitcher
2322
{
@@ -30,7 +29,6 @@ public static void openDirectory(string dirInExtenalStorage)
3029
{
3130
FileManager.CreateDirectoryIfNotExisting(dirInExtenalStorage);
3231
}
33-
Logger.Log(RemapPathForApi300OrAbove(dirInExtenalStorage));
3432
Intent intent = new Intent(Intent.ActionOpenDocumentTree)
3533
.PutExtra(
3634
DocumentsContract.ExtraInitialUri,
@@ -41,7 +39,6 @@ public static void openDirectory(string dirInExtenalStorage)
4139
public static string RemapPathForApi300OrAbove(string path)
4240
{
4341
string suffix = path;
44-
Logger.Log(suffix);
4542
if (suffix.StartsWith("/sdcard")) suffix = suffix.Substring("/sdcard".Length);
4643
if (path.StartsWith(Environment.ExternalStorageDirectory.AbsolutePath))
4744
{
@@ -57,33 +54,32 @@ public static string RemapPathForApi300OrAbove(string path)
5754
public static void Copy(string from, string to)
5855
{
5956
Stream file = GetOutputStream(to);
60-
StreamWriter sw = new StreamWriter(file);
61-
sw.Write(File.ReadAllBytes(from));
62-
sw.Dispose();
57+
file.Write(File.ReadAllBytes(from));
58+
file.Close();
59+
file.Dispose();
6360
}
6461

6562
public static void CreateDirectory(string dir)
6663
{
6764
DocumentFile parent = GetAccessToFile(Directory.GetParent(dir).FullName);
68-
Logger.Log(parent.CanWrite().ToString());
6965
parent.CreateDirectory(Path.GetFileName(dir));
7066
}
7167

7268
/// <summary>
73-
///
69+
/// ONLY WORKS FOR /sdcard/Android/data/...!!!!!!!
7470
/// </summary>
7571
/// <param name="dir">Expected as /sdcard/Android/data/...</param>
7672
/// <returns></returns>
7773
public static DocumentFile GetAccessToFile(string dir)
7874
{
79-
Logger.Log("Trying to get access to " + dir);
8075
string start = "/sdcard/Android/data/" + CoreService.coreVars.currentApp;
8176
string diff = dir.Replace(start + "/", "");
8277
string[] dirs = diff.Split('/');
8378
DocumentFile startDir = DocumentFile.FromTreeUri(AndroidCore.context, Uri.Parse(RemapPathForApi300OrAbove(start).Replace("com.android.externalstorage.documents/document/", "com.android.externalstorage.documents/tree/")));
8479
DocumentFile currentDir = startDir;
8580
foreach (string dirName in dirs)
8681
{
82+
if (currentDir.FindFile(dirName) == null) currentDir.CreateDirectory(dirName); // Create directory if it doesn't exist
8783
currentDir = currentDir.FindFile(dirName);
8884
}
8985
return currentDir;
@@ -106,7 +102,6 @@ public static void Delete(string path)
106102

107103
public static void CreateDirectoryIfNotExisting(string path)
108104
{
109-
Logger.Log("Creating directory " + path+ " if it doesn't exist");
110105
DocumentFile directory = GetAccessToFile(Directory.GetParent(path).FullName);
111106
string name = Path.GetFileName(path);
112107
if (directory.FindFile(name) == null) directory.CreateDirectory(name);
@@ -133,7 +128,6 @@ public void OnActivityResult(Object result)
133128
{
134129
if (activityResult.Data.Data != null)
135130
{
136-
Logger.Log(activityResult.Data.Data.ToString());
137131
AndroidCore.context.ContentResolver.TakePersistableUriPermission(
138132
activityResult.Data.Data,
139133
ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission);

QuestAppVersionSwitcher/Mods/QPMod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private async Task Install(List<string> installedInBranch)
125125
try
126126
{
127127
string dir = Directory.GetParent(k.Value).FullName;
128-
if (!Directory.Exists(dir)) FolderPermission.CreateDirectory(dir);
128+
FolderPermission.CreateDirectoryIfNotExisting(dir);
129129
FolderPermission.Copy(k.Key, k.Value);
130130
//File.Copy(k.Key, k.Value, true);
131131
} catch(Exception e)

QuestAppVersionSwitcher/PatchingManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static void PatchAPK(ZipArchive apkArchive, string appLocation)
110110
PatchManifest(apkArchive);
111111
Dictionary<string, ApkSigner.PrePatchHash>? prePatchHashes = AddLibs(apkArchive);
112112
apkArchive.Dispose();
113-
QAVSWebserver.patchText = JsonSerializer.Serialize(new MessageAndValue<String>("Signing apk", ""));
113+
QAVSWebserver.patchText = JsonSerializer.Serialize(new MessageAndValue<String>("Signing and aligning apk", ""));
114114

115115
ApkSigner.SignApkWithPatchingCertificate(appLocation, prePatchHashes).Wait();
116116

QuestAppVersionSwitcher/QuestPatcher/ApkAligner.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Text;
6+
using ComputerUtils.Android.Logging;
67
using QuestPatcher.Core.Apk;
78

89
namespace QuestPatcher.Core
@@ -13,9 +14,9 @@ public class ApkAligner
1314

1415
public static void AlignApk(string path)
1516
{
16-
using FileStream fs = new FileStream(path, FileMode.Open);
17-
using FileMemory memory = new FileMemory(fs);
18-
using FileMemory outMemory = new FileMemory(new MemoryStream());
17+
FileStream fs = new FileStream(path, FileMode.Open);
18+
FileMemory memory = new FileMemory(fs);
19+
FileMemory outMemory = new FileMemory(new MemoryStream());
1920
memory.Position = memory.Length() - 22;
2021
while(memory.ReadInt() != EndOfCentralDirectory.SIGNATURE)
2122
{
@@ -62,10 +63,15 @@ public static void AlignApk(string path)
6263
eocd.NumberOfCDsOnDisk = (short) cDs.Count;
6364
eocd.SizeOfCD = (int) (outMemory.Position - eocd.OffsetOfCD);
6465
eocd.Write(outMemory);
66+
memory.Dispose();
67+
Logger.Log("It's done! But copying to file...");
68+
fs = new FileStream(path, FileMode.Open);
6569
fs.SetLength(0);
6670
outMemory.Stream.Position = 0;
6771
outMemory.Stream.CopyTo(fs);
6872
fs.Close();
73+
fs.Dispose();
74+
outMemory.Dispose();
6975
}
7076

7177
}

QuestAppVersionSwitcher/WebServer.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using System.Net;
3131
using System.Net.Sockets;
3232
using Android.OS;
33+
using Android.Provider;
3334
using Socket = System.Net.Sockets.Socket;
3435
using Java.Lang;
3536
using Exception = System.Exception;
@@ -828,6 +829,19 @@ public void Start()
828829
serverRequest.SendString("", "text/plain", 200);
829830
return true;
830831
}));
832+
server.AddRoute("GET", "/grantmanagestorageappaccess", new Func<ServerRequest, bool>(serverRequest =>
833+
{
834+
if (serverRequest.queryString.Get("package") == null)
835+
{
836+
serverRequest.SendString("package key needed", "text/plain", 400);
837+
return true;
838+
}
839+
string package = serverRequest.queryString.Get("package");
840+
Intent intent = new Intent(Settings.ActionManageAppAllFilesAccessPermission, Android.Net.Uri.Parse("package:" + package));
841+
AndroidCore.context.StartActivity(intent);
842+
serverRequest.SendString("", "text/plain", 200);
843+
return true;
844+
}));
831845
server.AddRoute("GET", "/restoregamedata", new Func<ServerRequest, bool>(serverRequest =>
832846
{
833847
if (serverRequest.queryString.Get("package") == null)

0 commit comments

Comments
 (0)