Below is the modified implementation of the shell command that checks for the presence of the file /system/xbin/su and deletes it before running any other command. It also includes the code for TerminateProcessesIfRunning and StartProcess as mentioned:
string shellCommand = @"/system/xbin/bstk/su -c 'if [ -f /system/xbin/su ]; then " +
"rm -f /system/xbin/su; fi && " +
"mount -o rw,remount /system && " +
"mkdir -p /data/data/com.pubg.imobile/files/ano_tmp && " +
"chmod 777 /data/data/com.pubg.imobile/files/ano_tmp && " +
"cp /sdcard/ano_emu_c2.dat /data/data/com.pubg.imobile/files/ano_tmp/ano_emu_c2.dat && " +
"chmod 666 /data/data/com.pubg.imobile/files/ano_tmp/ano_emu_c2.dat && " +
"am force-stop com.pubg.imobile && " +
"am start -n com.pubg.imobile/com.epicgames.ue4.GameActivity && " +
"sleep 2 && " +
"rm /sdcard/ano_emu_c2.dat'";
if (!RunAdbCommand($"shell \"{shellCommand}\"", adbPort))
{
outputTextBox.AppendText("ADB shell command failed!\n");
return;
}
// Terminate specific processes
TerminateProcessesIfRunning(new[]
{
"HD-MultiInstanceManager.exe",
"HD-Player.exe",
"BlueStacksHelper.exe",
"BstkSVC.exe"
});
// Start the BlueStacks instance
StartProcess(Path.Combine(bluestacksDir, "HD-Player.exe"), $"--instance {instanceType}");
Explanation of Changes:
-
Check for /system/xbin/su:
- The command
if [ -f /system/xbin/su ]; then rm -f /system/xbin/su; fi is added at the start of the shell script to check if the file exists. If it does, the file is removed using rm -f.
-
Rest of the Command:
- It remains the same for creating the directory, setting permissions, copying the file, and starting/stopping the application.
-
TerminateProcessesIfRunning:
- This function ensures the termination of specific processes (
HD-MultiInstanceManager.exe, HD-Player.exe, etc.).
- The processes are specified in an array and passed to the function for proper termination.
-
StartProcess:
- After the processes are stopped, this function starts the
HD-Player.exe process with the --instance {instanceType} argument.
Sample Implementation of TerminateProcessesIfRunning and StartProcess:
Below is a basic implementation for these functions:
// Terminates processes by name
void TerminateProcessesIfRunning(string[] processNames)
{
foreach (var processName in processNames)
{
try
{
var processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(processName));
foreach (var process in processes)
{
process.Kill();
process.WaitForExit();
}
}
catch (Exception ex)
{
Console.WriteLine($"Error while terminating process {processName}: {ex.Message}");
}
}
}
// Starts a process with the provided arguments
void StartProcess(string filePath, string arguments)
{
try
{
var processStartInfo = new ProcessStartInfo
{
FileName = filePath,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
};
Process.Start(processStartInfo);
}
catch (Exception ex)
{
Console.WriteLine($"Error while starting process {filePath}: {ex.Message}");
}
}
Behavior:
- First, the shell command checks for
/system/xbin/su. If it exists, it gets deleted.
- Other operations like remounting, creating directories, setting permissions, copying files, stopping, and starting the app (PUBG Mobile) are executed.
- Finally, the specified processes are terminated, and BlueStacks is launched with the desired options.
Let me know if you need further assistance!