Skip to content

Commit

Permalink
Merge pull request #82 from emulamer/bs_130
Browse files Browse the repository at this point in the history
Bs 130
  • Loading branch information
emulamer authored Sep 7, 2019
2 parents 6485efd + 2c1372b commit a80e21f
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 38 deletions.
139 changes: 121 additions & 18 deletions BeatOn/BeatSaberModder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class BeatSaberModder
public const string LIBMODLOADER_TARGET_FILE = "lib/armeabi-v7a/libmodloader.so";
public const string LIBMODLOADER64_TARGET_FILE = "lib/arm64-v8a/libmodloader.so";
public const string MOD_TAG_FILE = "beaton.modded";
public const string MODLOADERV2_TAG_FILE = "beaton.modloader2.modded";
public const string BS_PLAYER_DATA_FILE = "/sdcard/Android/data/com.beatgames.beatsaber/files/PlayerData.dat";

public event EventHandler<string> StatusUpdated;
Expand Down Expand Up @@ -71,7 +72,7 @@ public bool IsTempApkModded
Log.LogErr("IsTempApkModded was called, but the TempApk does not exist!");
throw new ModException("IsTempApkModded was called, but the TempApk does not exist!");
}
return CheckApkHasModTagFile(TempApk);
return CheckApkHasAllTagFiles(TempApk);
}
}

Expand All @@ -94,6 +95,31 @@ public void CheckCleanupTempApk()
}
}

private bool IsInstalledBeatSaberOriginal
{
get
{
#if EMULATOR
return true;
#endif
string bsApk = FindBeatSaberApk();
if (bsApk == null)
{
Log.LogErr($"Tried to call {nameof(IsInstalledBeatSaberModded)} when beat saber isn't installed.");
throw new ModException("Beat saber is not installed, cannot check if it is modded.");
}
try
{
return !(CheckApkHasModloaderTagFile(bsApk) || CheckApkHasModTagFile(bsApk));
}
catch (Exception ex)
{
Log.LogErr($"Exception in {nameof(IsInstalledBeatSaberModded)} when trying to check if it is modded.", ex);
throw new ModException("Error checking if installed beat saber is modded.", ex);
}
}
}

public bool IsInstalledBeatSaberModded
{
get
Expand All @@ -109,7 +135,7 @@ public bool IsInstalledBeatSaberModded
}
try
{
return CheckApkHasModTagFile(bsApk);
return CheckApkHasAllTagFiles(bsApk);
}
catch (Exception ex)
{
Expand All @@ -136,7 +162,7 @@ public bool CheckIsTempApkReadyForInstall()
{
if (TempApk == null)
return false;
return CheckApkHasModTagFile(TempApk);
return CheckApkHasAllTagFiles(TempApk);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -334,7 +360,7 @@ public void BackupOriginalApk()
throw new ModException("Beat Saber does not seem to be installed, could not find its APK.");
}
UpdateStatus("Verifying the installed APK isn't modded...");
if (IsInstalledBeatSaberModded)
if (!IsInstalledBeatSaberOriginal)
{
UpdateStatus("Installed beatsaber IS modded!");
if (File.Exists(Constants.BEATSABER_APK_BACKUP_FILE))
Expand Down Expand Up @@ -382,17 +408,22 @@ public void ApplyModToTempApk()
bool modFailed = false;
//keep track of any temp files that may have been used so we can clean them up
List<string> tempFiles = new List<string>();
bool isPartialUpgrade = false;
try
{
//// delete the assets relocation if it already exists in case the mod has been installed before
if (Directory.Exists(Constants.ASSETS_RELOC_PATH))
Directory.Delete(Constants.ASSETS_RELOC_PATH, true);
isPartialUpgrade = CheckApkHasModTagFile(TempApk) && !CheckApkHasModloaderTagFile(TempApk) && Directory.Exists(Constants.ASSETS_RELOC_PATH);

if (!isPartialUpgrade)
{
//// delete the assets relocation if it already exists in case the mod has been installed before
if (Directory.Exists(Constants.ASSETS_RELOC_PATH))
Directory.Delete(Constants.ASSETS_RELOC_PATH, true);

//// copy asset files from APK to /sdcard/wherever
ExtractAssetsFromApkToExternalStorage(TempApk, new List<string>() {
//// copy asset files from APK to /sdcard/wherever
ExtractAssetsFromApkToExternalStorage(TempApk, new List<string>() {
"Managed",
"boot.config" });

}
bool is64bit = IsApk64Bit(TempApk);

//// copy libassetredirect.so to the mods folder
Expand All @@ -401,18 +432,25 @@ public void ApplyModToTempApk()
//from this point on, the APK has been modified and isn't definitively recoverable if something goes wrong
tempApkModified = true;

//// modify classes.dex and inject the loadlibrary call for libmodloader.so
InjectModLoaderToApk(TempApk, tempFiles);
if (!isPartialUpgrade)
{
//// modify classes.dex and inject the loadlibrary call for libmodloader.so
InjectModLoaderToApk(TempApk, tempFiles);
}

//// add libmodloader.so to the apk
AddModLoaderToApk(TempApk);

//// fix the manifest
AddManifestModToApk(TempApk);

//// add a 1 byte file to the APK so we know it's been modded to make verifying it later easier
AddTagFileToApk(TempApk);
if (!isPartialUpgrade)
{
//// fix the manifest
AddManifestModToApk(TempApk);
//// add a 1 byte file to the APK so we know it's been modded to make verifying it later easier
AddTagFileToApk(TempApk);
}

AddModLoaderTagFileToApk(TempApk);

//// re-sign the APK
UpdateStatus("Re-signing the modded APK (this takes a minute)...");
SignApk(TempApk);
Expand Down Expand Up @@ -595,6 +633,28 @@ public void CleanupTempApk()

}

private bool CheckApkHasAllTagFiles(string apkFilename)
{
bool hasModTag = false;
bool hasModloaderTag = false;
using (var apk = new ZipFileProvider(apkFilename, FileCacheMode.None, true, QuestomAssets.Utils.FileUtils.GetTempDirectory()))
{
if (apk.FileExists(MOD_TAG_FILE))
{
hasModTag = true;
}
if (apk.FileExists(MODLOADERV2_TAG_FILE))
{
hasModloaderTag = true;
}
if (hasModTag && hasModloaderTag)
{
return true;
}
}
return (hasModTag && hasModloaderTag);
}

private bool CheckApkHasModTagFile(string apkFilename)
{
using (var apk = new ZipFileProvider(apkFilename, FileCacheMode.None, true, QuestomAssets.Utils.FileUtils.GetTempDirectory()))
Expand All @@ -605,6 +665,17 @@ private bool CheckApkHasModTagFile(string apkFilename)
return false;
}

private bool CheckApkHasModloaderTagFile(string apkFilename)
{
using (var apk = new ZipFileProvider(apkFilename, FileCacheMode.None, true, QuestomAssets.Utils.FileUtils.GetTempDirectory()))
{
if (apk.FileExists(MODLOADERV2_TAG_FILE))
return true;
}
return false;

}

private void UpdateStatus(string message)
{
StatusUpdated?.Invoke(this, message);
Expand Down Expand Up @@ -1002,13 +1073,31 @@ private void AddModLoaderToApk(string apkFilename)
{
if (apk.DirectoryExists(LIBMODLOADER_TARGET_FILE.GetDirectoryFwdSlash()))
{
if (apk.FileExists(LIBMODLOADER_TARGET_FILE))
{
apk.Delete(LIBMODLOADER_TARGET_FILE);
apk.Save();
}
apk.QueueWriteStream(LIBMODLOADER_TARGET_FILE, resStream, true, true);
}
if (apk.DirectoryExists(LIBMODLOADER64_TARGET_FILE.GetDirectoryFwdSlash()))
{
if (apk.FileExists(LIBMODLOADER64_TARGET_FILE))
{
apk.Delete(LIBMODLOADER64_TARGET_FILE);
apk.Save();
}
apk.QueueWriteStream(LIBMODLOADER64_TARGET_FILE, resStream64, true, true);
}
apk.Save();
try
{
apk.Save();
} catch (IOException)
{
GC.Collect();
System.Threading.Thread.Sleep(1000);
apk.Save();
}
}
}
}
Expand Down Expand Up @@ -1083,5 +1172,19 @@ private void AddTagFileToApk(string apkFilename)
}
}

private void AddModLoaderTagFileToApk(string apkFilename)
{
using (var apk = new ZipFileProvider(apkFilename, FileCacheMode.None, false, QuestomAssets.Utils.FileUtils.GetTempDirectory()))
{
if (apk.FileExists(MODLOADERV2_TAG_FILE))
{
Log.LogMsg("APK file already had the modloader v2 tag file.");
return;
}
apk.Write(MODLOADERV2_TAG_FILE, new byte[1], true, false);
apk.Save();
}
}

}
}
28 changes: 25 additions & 3 deletions BeatOn/Core/BeatOnCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,22 @@ private bool CheckReimportSongs()
return false;
}

private void KillBeatSaber()
private void KillBackgroundProcess(string packageName)
{
try
{
ActivityManager am = (ActivityManager)_context.GetSystemService(Context.ActivityService);
am.KillBackgroundProcesses("com.beatgames.beatsaber");
am.KillBackgroundProcesses(packageName);
}
catch (Exception ex)
{
Log.LogErr("Exception trying to kill background process for beatsaber.", ex);
}
}
private void KillBeatSaber()
{
KillBackgroundProcess("com.beatgames.beatsaber");
}

public void Start()
{
Expand Down Expand Up @@ -486,7 +490,25 @@ private void OpManager_OpStatusChanged(object sender, QuestomAssets.AssetOps.Ass

private void SendPackageStop(string packageName)
{
//doesn't work
try
{
Intent intent = new Intent("com.oculus.vrshell.intent.action.LAUNCH");
intent.SetPackage("com.oculus.vrshell");
intent.PutExtra("intent_data", Android.Net.Uri.Parse("systemux://home"));
intent.PutExtra("blackscreen", false);
//var intent = new Intent("com.oculus.system_activity");
//intent.SetPackage(packageName);

//intent.PutExtra("intent_pkg", "com.oculus.vrshell");
//intent.PutExtra("intent_cmd", "{\"Command\":\"exitToHome\", \"PlatformUIVersion\":3, \"ToPackage\":\"" + packageName + "\"}");
//_context.SendBroadcast(intent);
//intent.PutExtra("intent_cmd", "{\"Command\":\"returnToLauncher\", \"PlatformUIVersion\":3, \"ToPackage\":\"" + packageName + "\"}");
_context.SendBroadcast(intent);
Task.Delay(3000).ContinueWith(t => { KillBackgroundProcess(packageName); });
} catch (Exception e)
{
Log.LogErr("Exception trying to send package exittohome messages", e);
}
}

private void _SongDownloadManager_StatusChanged(object sender, DownloadStatusChangeArgs e)
Expand Down
19 changes: 18 additions & 1 deletion BeatOn/Core/RequestHandlers/PostFileUpload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ public void HandleRequest(HttpListenerContext context)
resp.BadRequest("Didn't get any useable files.");
return;
}

bool forceOverwrite = false;
if (!string.IsNullOrWhiteSpace(req.Url.Query))
{
foreach (string kvp in req.Url.Query.TrimStart('?').Split("&"))
{
var split = kvp.Split('=');
if (split.Count() < 1)
continue;
if (split[0].ToLower() == "overwrite")
{
forceOverwrite = true;
break;
}
}
}

foreach (var file in files.Keys.ToList())
{
var s = files[file];
Expand All @@ -100,7 +117,7 @@ public void HandleRequest(HttpListenerContext context)
{
provider.Dispose();
ms.Dispose();
});
}, overwriteIfExists: forceOverwrite);
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion BeatOn/Core/RequestHandlers/PostPackageAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void HandleRequest(HttpListenerContext context)
if (split[0].ToLower() == "package")
{
package= Java.Net.URLDecoder.Decode(split[1]);
break;

} else if (split[0].ToLower() == "action")
{
action = Java.Net.URLDecoder.Decode(split[1]);
Expand Down
Loading

0 comments on commit a80e21f

Please sign in to comment.