Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions app/src/main/feature/stores/steam/utils/SteamUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> =
arrayOf(
"GameOverlayRenderer.dll",
Expand Down Expand Up @@ -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)
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/runtime/display/XServerDisplayActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading