diff --git a/app/src/main/feature/stores/steam/utils/SteamUtils.kt b/app/src/main/feature/stores/steam/utils/SteamUtils.kt index 44de79a73..727dbd0a5 100644 --- a/app/src/main/feature/stores/steam/utils/SteamUtils.kt +++ b/app/src/main/feature/stores/steam/utils/SteamUtils.kt @@ -22,6 +22,7 @@ import com.winlator.cmod.runtime.wine.WineRegistryEditor import com.winlator.cmod.feature.stores.steam.enums.EOSType import com.winlator.cmod.feature.stores.steam.enums.EPersonaState import com.winlator.cmod.feature.stores.steam.service.SteamService.Companion.getOwnedAppDlc +import com.winlator.cmod.runtime.system.LogManager import kotlinx.coroutines.runBlocking import okhttp3.Call import okhttp3.Callback @@ -46,6 +47,8 @@ import java.nio.file.StandardOpenOption import java.util.concurrent.TimeUnit object SteamUtils { + private const val TAG = "SteamUtils" + private fun coreSteamClientFiles(): Array = arrayOf( "GameOverlayRenderer.dll", @@ -618,42 +621,40 @@ object SteamUtils { */ @JvmStatic fun restoreOriginalExecutable( - context: Context, steamAppId: Int, + gameInstallPath: String, + relativeExecutablePath: String, ) { - Timber.i("Starting restoreOriginalExecutable for appId: $steamAppId") - var restoredCount = 0 + if (gameInstallPath.isBlank() || relativeExecutablePath.isBlank()) { + LogManager.logW(TAG, "Missing game or executable path for appId=$steamAppId; skipping original executable restore") + return + } - val imageFs = ImageFs.find(context) - val dosDevicesRoot = File(imageFs.wineprefix, "dosdevices") - val dosDevicesPath = - dosDevicesRoot - .listFiles() - ?.firstOrNull { it.isDirectory && it.name.matches(Regex("[a-z]:")) && it.name != "c:" && it.name != "z:" } - ?: File(dosDevicesRoot, "f:") - - dosDevicesPath - .walkTopDown() - .maxDepth(10) - .filter { it.isFile && it.name.endsWith(".original.exe", ignoreCase = true) } - .forEach { file -> - try { - val origPath = file.toPath() - val originalPath = origPath.parent.resolve(origPath.fileName.toString().removeSuffix(".original.exe")) + try { + val gameDir = File(gameInstallPath).canonicalFile + val executable = + File(gameDir, relativeExecutablePath.replace("\\", "/")).canonicalFile - if (Files.exists(originalPath)) { - Files.delete(originalPath) - } + if (!executable.toPath().startsWith(gameDir.toPath())) { + LogManager.logW(TAG, "Executable path is outside the game directory for appId=$steamAppId; skipping restore") + return + } - Files.copy(origPath, originalPath, StandardCopyOption.REPLACE_EXISTING) - Timber.i("Restored ${originalPath.fileName} from backup") - restoredCount++ - } catch (e: Exception) { - Timber.w(e, "Failed to restore ${file.name} from backup") - } + val backup = File(executable.parentFile, executable.name + ".original.exe") + if (!backup.isFile) { + LogManager.logI(TAG) { "No original executable backup found for appId=$steamAppId" } + return } - Timber.i("Finished restoreOriginalExecutable for appId: $steamAppId. Restored $restoredCount executable(s)") + Files.copy( + backup.toPath(), + executable.toPath(), + StandardCopyOption.REPLACE_EXISTING, + ) + LogManager.logI(TAG) { "Restored ${executable.name} from backup for appId=$steamAppId" } + } catch (e: Exception) { + LogManager.logW(TAG, "Failed to restore original executable for appId=$steamAppId", e) + } } /** diff --git a/app/src/main/runtime/display/XServerDisplayActivity.java b/app/src/main/runtime/display/XServerDisplayActivity.java index 37dec2a38..0276ea7b2 100644 --- a/app/src/main/runtime/display/XServerDisplayActivity.java +++ b/app/src/main/runtime/display/XServerDisplayActivity.java @@ -7677,7 +7677,7 @@ private void setupSteamGameFiles() { MarkerUtils.INSTANCE.removeMarker(gameInstallPath, Marker.STEAM_DRM_PATCHED); MarkerUtils.INSTANCE.removeMarker(gameInstallPath, Marker.STEAM_DRM_UNPACK_CHECKED); - SteamUtils.restoreOriginalExecutable(this, appId); + SteamUtils.restoreOriginalExecutable(appId, gameInstallPath, container.getExecutablePath()); boolean bionicRuntimeOk = com.winlator.cmod.feature.stores.steam.wnsteam .WnSteamAssetsInstaller.INSTANCE.installBionicRuntime(this); @@ -7784,7 +7784,7 @@ private void setupSteamGameFiles() { if (unpackFiles) { SteamUtils.restoreUnpackedExecutable(this, appId); } else { - SteamUtils.restoreOriginalExecutable(this, appId); + SteamUtils.restoreOriginalExecutable(appId, gameInstallPath, container.getExecutablePath()); } SteamUtils.restoreSteamclientFiles(this, appId);