leaty / tuxtrain

An easy-to-use generic trainer for Linux, written in Rust.
MIT License
50 stars 3 forks source link

Sekiro trainer FPS and aspect ratio features #4

Closed SvdB-nonp closed 1 year ago

SvdB-nonp commented 1 year ago

I kept the issue name a bit generic, as I found something in the existing trainer, as well as my new one. This is just some first playing around with the sekiro trainer and my own additions.

Since both features did not match patterns, I was wondering whether this is due to my setup or whether they were untested anyway.

Just an FYI: I found this sekirofpsunlock tool, which looks like an adapted copy of FPSUnlockAnd More for linux. Could be handy for some sekiro-specific stuff.
Edit: This mod works perfectly for me, setting ultrawide and FPS, so the memory pattern replacement of this one is correct.

Current output

With the current sekiro.toml, see attached, I get the following output. The code mentioned in the other sections are examples from this file. I was too lazy today to already make a branch or fork, can create this later.

[user@fedora ~]$ sudo -E env "PATH=$PATH" tuxtrain -t ~/dev/tuxtrain/trainers/sekiro.toml
==> Sekiro Trainer 0.0.2
    Set FPS Framelock (to 165)
      - Error: Unable to find pattern.
    Fix game speed for framelock (to 84)
      - Error: Unable to find pattern.
    Disable 16:9 aspect ratio lock.
      - Error: EFAULT: Bad address

Changes done

Feature "Set FPS Framelock (to 165)"

At first this raised a bad address error. I noticed in SekiroFpsUnlockAndMore line 26 that they mention the 88 (index 5) could be an 89 (due to rounding of floating point?). This does fix the bad address error, but since it is not matching anything, I am not sure this is correct.

[[feature]]
name = "Set FPS Framelock (to 165)"
pattern = "C7 43 __ __ 88 89 3C 4C 89 AB"
replace = "__ __ __ 0C 98 C6 3B __ __ __"
enable = true

Feature "Fix game speed for framelock (to 84)"

No change made. I just noticed that this is again not finding any matches. I checked the other mods, and they only verified that this pattern is correct (as far as I could tell).

Feature "Disable 16:9 aspect ratio lock."

So, I drafted this based on the source code of SekiroFpsUnlockAndMore. I think this should be simple enough, first three bytes change, but I get the Bad address error which I do not know what to do with. I have no clue how a correct address looks like, so I cannot debug this reliably.

(I did already notice I could have the replace pattern have underscore-only after the first three bytes, but it is for now not yet relevant I think anyway.)

# Pattern taken from Sekiro FPS Unlock And More project:
# https://github.com/uberhalit/SekiroFpsUnlockAndMore/blob/d6312c6b0af0bcdf987568e1490b7d842548ae99/SekiroFpsUnlockAndMore/GameData.cs#L140
[[feature]]
name = "Disable 16:9 aspect ratio lock."
pattern = "85 C9 74 __ 47 8B __ __ __ __ __ __ 45 __ __ 74"
replace = "90 90 EB __ 47 8B __ __ __ __ __ __ 45 __ __ 74"
enable = true

Feature "Add ultrawide resolution entry 3440x1440 in place of 720p."

I am not sure whether this is needed, as my native resolution (3440x1440) is present in the list when running in fullscreen. However, as I already made this, I thought it couldn't hurt including this. I remember this needed to be added specifically when playing Sekiro on windows with FPS unlocked and widescreen patched, but that can of course have changed in the mean time.

# Pattern taken from Sekiro FPS Unlock And More project:
# https://github.com/uberhalit/SekiroFpsUnlockAndMore/blob/d6312c6b0af0bcdf987568e1490b7d842548ae99/SekiroFpsUnlockAndMore/GameData.cs#L126
# New values identified from https://www.wsgf.org/article/common-hex-values, Formatted Value column.
# 1280x720 = '00 05 __ __ D0 02 __ __ '
# 3440x1440 = '70 0D __ __ A0 05 __ __'

[[feature]]
name = "Add ultrawide resolution entry 3440x1440 in place of 720p."
pattern = "00 05 __ __ D0 02 __ __ A0 05 __ __ 2A 03 __ __"
replace = "70 0D 00 00 A0 05 00 00 A0 05 __ __ 2A 03 __ __"
enable = false

sekiro.zip

leaty commented 1 year ago

Thanks for the writeup.

Since both features did not match patterns, I was wondering whether this is due to my setup or whether they were untested anyway.

Partially untested, I had not tested the Sekiro trainer personally except looking at the original source patterns (I assumed the contributor got it working). But after testing, the issue becomes apparent. After fiddling slightly with the patterns it successfully finds them, unfortunately I reach the same EFAULT: Bad address error. This was new to me, but after checking /proc/pid/maps I can see that the range in which these patterns exist do not have write access (unlike Elden Ring/Dark Souls III). See below.

140000000-140001000 r--p 00000000 103:04 1434518827   /path/to/sekiro.exe
140001000-142929000 r-xp 00000000 00:00 0 
142929000-143af9000 r--p 00000000 00:00 0
140000000-140001000 rwxp 00000000 103:02 2213939433   /path/to/DarkSoulsIII.exe
140001000-144955000 rwxp 00000000 00:00 0 
144955000-1463df000 rwxp 00000000 00:00 0

This causes process_vm_writev to fail even with sudo giving the EFAULT: Bad address error, which I am not entirely sure as to why- perhaps it's supposed to work that way? However, modifying the code to use ptrace and manually opening /proc/pid/mem as writable works. I'd like to find the actual reason behind this before pushing though, I feel like process_vm_writev should always work.

In any case I might push a PR soon with the changes if you'd like to test it that way.

leaty commented 1 year ago

Update: As far as I can deduce, process_vm_writev respects page protection settings and it doesn't seem like there is any way to disable it (or I'm blind), so it's looking like I'll have to use a slower option.

EDIT: Actually I think I'll add ptrace as an option for each feature, where you'd enable it if the pattern exists within a protected map. This seems like the best option.

leaty commented 1 year ago

After making a couple of patches it should work now, I'd be thankful if you could confirm. Also, I chose to use ptrace as an automatic fallback instead.

Note that I moved "Fix game speed for framelock" to the bottom of the trainer because it seems to be broken (wrong), see: https://github.com/leaty/tuxtrain/blob/cfbf5ca1919c43842fd4ab9dd509306a6e539988/trainers/sekiro.toml#L45-L49

SvdB-nonp commented 1 year ago

I made it work with your new changes, was pretty easy now as I only had to change the region a bit. There is a pull request now for the change, however I am not sure how to link it to this issue.

In a short test it enabled 165hz and widescreen succesfully. Needed to change the region for FPS slightly to make that work.

leaty commented 1 year ago

Do you want to try getting Add ultrawide resolution entry 3440x1440 in place of 720p. to work as well? I can probably try by using gamescope this weekend otherwise, although I am not sure how accurate a test without an ultrawide monitor really is.

SvdB-nonp commented 1 year ago

For now there is no benefit to it, as in fullscreen it is there (in fullscreen mode) with no fix at all. I think the other mod used it to add it to window mode, as they used that mode for the FPS fix (in the past).

leaty commented 1 year ago

Sounds good, I'll give it a shot just in case it can solve it for windowed mode. But thanks for the contrib! It really helped in finding some flaws in the code (and me learning I should probably test trainers from a PR, although to be fair to the contributor it probably did work back then on an older version).