├── .gitattributes ├── .github └── workflows │ └── publish.yml ├── .gitignore ├── README.md ├── unlockfps ├── AssemblyInfo.cs ├── Config.cs ├── FodyWeavers.xml ├── FpsPatterns.cs ├── GameConstants.cs ├── Logging │ ├── ConsoleLogger.cs │ ├── ILogger.cs │ ├── ILoggerFactory.cs │ ├── LogLevel.cs │ └── LogManager.cs ├── NativeMethods.txt ├── Program.cs ├── Properties │ └── launchSettings.json ├── Services │ ├── ConfigService.cs │ ├── GameInstanceService.cs │ └── ProcessService.cs ├── Utils │ ├── Native.cs │ ├── NativeMethods.cs │ ├── ProcessUtils.cs │ ├── TaskUtils.cs │ ├── Win32Window.cs │ └── WineHelper.cs ├── app.manifest └── unlockfps.csproj ├── unlockfps_gui ├── App.axaml ├── App.axaml.cs ├── AppBuilderExtensions.cs ├── AssemblyInfo.cs ├── Assets │ └── icon.ico ├── Converters │ ├── BooleanConverters.cs │ ├── DelegateConverter.cs │ ├── Enum2ListConverter.cs │ ├── FullPath2NameConverter.cs │ └── HasItemsConverter.cs ├── FodyWeavers.xml ├── OSVersionExt │ ├── Environment │ │ ├── EnvironmentProvider.cs │ │ └── IEnvironment.cs │ ├── MajorVersion10 │ │ ├── MajorVersion10Properties.cs │ │ └── RegistryProviderDefault.cs │ ├── OSVersion.cs │ ├── Registry │ │ └── IRegistry.cs │ ├── VersionInfo.cs │ └── Win32API │ │ ├── IWin32API.cs │ │ ├── SystemMetrics.cs │ │ ├── Win32ApiEnums.cs │ │ └── Win32ApiProvider.cs ├── Program.cs ├── Styles │ └── TabStyles.axaml ├── Utils │ ├── AssemblyAttributeUtil.cs │ ├── ConsoleManager.cs │ ├── Native.cs │ ├── ProcessUtils.cs │ ├── ReflectionUtil.cs │ └── WineHelper.cs ├── ViewModels │ └── ViewModelBase.cs ├── Views │ ├── AboutWindow.axaml │ ├── AboutWindow.axaml.cs │ ├── AlertWindow.axaml │ ├── AlertWindow.axaml.cs │ ├── InitializationWindow.axaml │ ├── InitializationWindow.axaml.cs │ ├── MainWindow.axaml │ ├── MainWindow.axaml.cs │ ├── SettingsWindow.axaml │ └── SettingsWindow.axaml.cs ├── WindowChromeExtensions.cs ├── app.manifest └── unlockfps_gui.csproj ├── unlockfps_nc.sln └── unlockfps_nc ├── AboutForm.Designer.cs ├── AboutForm.cs ├── AboutForm.resx ├── MainForm.Designer.cs ├── MainForm.cs ├── MainForm.resx ├── Model └── Config.cs ├── Program.cs ├── Resources ├── app.manifest └── icon.ico ├── Service ├── ConfigService.cs └── ProcessService.cs ├── SettingsForm.Designer.cs ├── SettingsForm.cs ├── SettingsForm.resx ├── SetupForm.Designer.cs ├── SetupForm.cs ├── SetupForm.resx ├── Utility ├── Native.cs └── ProcessUtils.cs └── unlockfps_nc.csproj /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: publish 2 | env: 3 | ProjectName: genshin-fps-unlock 4 | on: 5 | create: 6 | tags: 7 | - "v*.*.*" 8 | 9 | jobs: 10 | build: 11 | name: Build and Release 12 | if: ${{ StartsWith(github.ref, 'refs/tags/') }} 13 | runs-on: windows-latest 14 | 15 | steps: 16 | - name: Checkout code 17 | uses: actions/checkout@v2 18 | with: 19 | fetch-depth: '0' 20 | submodules: 'recursive' 21 | 22 | - name: Setup .NET 23 | uses: actions/setup-dotnet@v1 24 | with: 25 | dotnet-version: 8.0.x 26 | 27 | - name: Publish 28 | if: ${{ success() }} 29 | shell: pwsh 30 | run: | 31 | echo ${{ github.ref }} 32 | dotnet publish unlockfps_gui --runtime win-x64 --configuration Release --output ci-publish-win64 33 | rm ./ci-publish-win64/*.pdb 34 | rm ./ci-publish-win64/unlockfps_cli.* 35 | 36 | - name: Get tag 37 | uses: dawidd6/action-get-tag@v1 38 | if: ${{ success() && startsWith(github.ref, 'refs/tags/') }} 39 | id: tag 40 | 41 | - name: Pack via 7z 42 | if: ${{ success() && startsWith(github.ref, 'refs/tags/') }} 43 | run: | 44 | mkdir -p ./ci-pack/ 45 | 7z a -mx9 -mfb=273 -ms -md=31 -myx=9 -mtm=- -mmt -mmtf -md=1536m -mmf=bt3 -mmc=10000 -mpb=0 -mlc=0 "./ci-pack/${{ env.ProjectName }}-${{ steps.tag.outputs.tag }}-win64.7z" "./ci-publish-win64/*" -x!"${{ env.ProjectName }}" -r 46 | 47 | - name: Create a new GitHub release if a new tag is pushed 48 | uses: softprops/action-gh-release@v1 49 | if: ${{ success() && startsWith(github.ref, 'refs/tags/') }} 50 | env: 51 | GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} 52 | with: 53 | name: ${{ steps.tag.outputs.tag }} 54 | prerelease: true 55 | draft: false 56 | files: | 57 | ./ci-pack/${{ env.ProjectName }}-${{ steps.tag.outputs.tag }}-win64.7z -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Genshin Impact FPS Unlocker 2 | 3 | A forked version which rewrites GUI and supports linux with WINE. 4 | 5 | ![image](https://github.com/Milkitic/genshin-fps-unlock-universal/assets/24785749/e92fe460-c045-46ef-bbf1-7f350e7eb179) 6 | 7 | > Running in Windows 11 & Linux KDE 8 | 9 | - This tool helps you to unlock the 60 fps limit in the game 10 | - This is an external program which uses **WriteProcessMemory** to write the desired fps to the game 11 | - Handle protection bypass is already included 12 | - Does not require a driver for R/W access 13 | - Supports OS and CN version 14 | - Should work for future updates 15 | - If the source needs to be updated, I'll try to do it as soon as possible 16 | - You can download the compiled binary over at '[Release](https://github.com/34736384/genshin-fps-unlock/releases)' if you don't want to compile it yourself 17 | 18 | ## Compiling 19 | 1. Install Visual Studio 2022 with Desktop C++ workload in Visual Studio Installer. 20 | 2. Install .NET 8 SDK. 21 | 3. Use `dotnet build ./unlockfps_gui` for regular compiling. Use `dotnet publish ./unlockfps_gui -c Release -r win-x64` for AOT publish. 22 | 23 | ## Usage 24 | 25 | ### Running on Windows 26 | 27 | - Run the exe and click 'Launch' 28 | - If it is your first time running, unlocker will attempt to find your game through the registry. If it fails, then it will ask you to either browse or run the game. 29 | - Place the compiled exe anywhere you want (except for the game folder) 30 | - Make sure your game is closed—the unlocker will automatically start the game for you 31 | - Run the exe as administrator, and leave the exe running 32 | >It requires adminstrator because the game needs to be started by the unlocker and the game requires such permission 33 | - To load other third-party plugins, go to `Options->Settings->DLLs` and click add 34 | 35 | ### Running with wine 36 | 37 | #### Prerequisite 38 | 39 | ``` 40 | WINEPREFIX=... winetricks dotnet45 41 | WINEPREFIX=... winecfg -v win7 42 | ``` 43 | #### Running 44 | 45 | ``` 46 | WINEPREFIX=... wine unlockfps.exe 47 | ``` 48 | ![Screenshot_20240206_144503](https://github.com/Milkitic/genshin-fps-unlock-aot/assets/24785749/c1e0377a-89eb-49f6-958a-37b9229c5875) 49 | 50 | ## Version 3.0.0 Changes 51 | - Rewritten the project in .NET 8 52 | - Added a launch option to use mobile UI (for streaming from mobile devices or touchscreen laptops) 53 | ## Notes 54 | - HoYoverse (miHoYo) is well aware of this tool, and you will not get banned for using **ONLY** fps unlock. 55 | - If you are using other third-party plugins, you are doing it at your own risk. 56 | - Any artifacts from unlocking fps (e.g. stuttering) is **NOT** a bug of the unlocker 57 | 58 | -------------------------------------------------------------------------------- /unlockfps/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | [assembly: PropertyChanged.FilterType(@".*ViewModel")] 2 | [assembly: PropertyChanged.FilterType(@".*\.Config")] 3 | [assembly: PropertyChanged.FilterType(@".*\.LaunchOptions")] -------------------------------------------------------------------------------- /unlockfps/Config.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.ObjectModel; 2 | using System.ComponentModel; 3 | 4 | namespace UnlockFps; 5 | 6 | public partial class LaunchOptions : INotifyPropertyChanged 7 | { 8 | public string? GamePath { get; set; } 9 | 10 | public bool IsWindowBorderless { get; set; } 11 | public bool Fullscreen { get; set; } = true; 12 | public bool IsExclusiveFullscreen { get; set; } 13 | public bool UseCustomResolution { get; set; } 14 | public int CustomResolutionX { get; set; } = 1920; 15 | public int CustomResolutionY { get; set; } = 1080; 16 | public bool UseMobileUI { get; set; } 17 | 18 | public int MonitorId { get; set; } = 1; 19 | 20 | public bool SuspendLoad { get; set; } 21 | public ObservableCollection DllList { get; set; } = new(); 22 | } 23 | 24 | public partial class Config : INotifyPropertyChanged 25 | { 26 | public LaunchOptions LaunchOptions { get; set; } = new(); 27 | 28 | public bool AutoLaunch { get; set; } 29 | public bool AutoClose { get; set; } 30 | public bool UsePowerSave { get; set; } 31 | public int FpsTarget { get; set; } = 120; 32 | public int FpsPowerSave { get; set; } = 10; 33 | public int ProcessPriority { get; set; } = 3; 34 | public bool ShowDebugConsole { get; set; } 35 | public bool WindowQueryUseEvent { get; set; } = true; 36 | } -------------------------------------------------------------------------------- /unlockfps/FodyWeavers.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | -------------------------------------------------------------------------------- /unlockfps/FpsPatterns.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Runtime.InteropServices; 3 | using UnlockFps.Logging; 4 | using UnlockFps.Utils; 5 | 6 | namespace UnlockFps; 7 | 8 | internal static class FpsPatterns 9 | { 10 | private static readonly ILogger Logger = LogManager.GetLogger(nameof(FpsPatterns)); 11 | 12 | public static unsafe nint ProvideAddress(ProcessModule mdUnityPlayer, ProcessModule mdUserAssembly, Process process) 13 | { 14 | var unityPlayerPath = mdUnityPlayer.FileName; 15 | var userAssemblyPath = mdUserAssembly.FileName; 16 | 17 | using ModuleGuard shUnityPlayer = Utils.NativeMethods.LoadLibraryEx(unityPlayerPath, nint.Zero, 0x20); 18 | using ModuleGuard shUserAssembly = Utils.NativeMethods.LoadLibraryEx(userAssemblyPath, nint.Zero, 0x20); 19 | 20 | var pUnityPlayer = shUnityPlayer.BaseAddress; 21 | var pUserAssembly = shUserAssembly.BaseAddress; 22 | 23 | var dosHeader = Marshal.PtrToStructure(pUnityPlayer); 24 | var ntHeader = 25 | Marshal.PtrToStructure((nint)(pUnityPlayer.ToInt64() + dosHeader.e_lfanew)); 26 | 27 | if (ntHeader.FileHeader.TimeDateStamp < 0x656FFAF7U) // < 3.7 28 | { 29 | Logger.LogDebug($"TimeDateStamp: {ntHeader.FileHeader.TimeDateStamp}, <3.7"); 30 | var addressPtr = ProcessUtils.PatternScan(pUnityPlayer, "7F 0F 8B 05 ?? ?? ?? ??"); 31 | byte* address = (byte*)addressPtr; 32 | if (address == null) throw new Exception("Unrecognized FPS pattern."); 33 | 34 | Logger.LogDebug($"Scanned pattern successfully: 0x{addressPtr:X16}"); 35 | byte* rip = address + 2; 36 | int rel = *(int*)(rip + 2); 37 | var localVa = rip + rel + 6; 38 | var rva = localVa - pUnityPlayer.ToInt64(); 39 | return (nint)(pUnityPlayer.ToInt64() + rva); 40 | } 41 | else 42 | { 43 | byte* rip = null; 44 | if (ntHeader.FileHeader.TimeDateStamp < 0x656FFAF7U) // < 4.3 45 | { 46 | Logger.LogDebug($"TimeDateStamp: {ntHeader.FileHeader.TimeDateStamp}, <4.3"); 47 | var addressPtr = 48 | ProcessUtils.PatternScan(pUserAssembly, "E8 ?? ?? ?? ?? 85 C0 7E 07 E8 ?? ?? ?? ?? EB 05"); 49 | byte* address = (byte*)addressPtr; 50 | if (address == null) throw new Exception("Unrecognized FPS pattern."); 51 | 52 | Logger.LogDebug($"Scanned pattern successfully: 0x{addressPtr:X16}"); 53 | rip = address; 54 | rip += *(int*)(rip + 1) + 5; 55 | rip += *(int*)(rip + 3) + 7; 56 | } 57 | else 58 | { 59 | Logger.LogDebug($"TimeDateStamp: {ntHeader.FileHeader.TimeDateStamp}"); 60 | var addressPtr = ProcessUtils.PatternScan(pUserAssembly, "B9 3C 00 00 00 FF 15"); 61 | byte* address = (byte*)addressPtr; 62 | if (address == null) throw new Exception("Unrecognized FPS pattern."); 63 | 64 | Logger.LogDebug($"Scanned pattern successfully: 0x{addressPtr:X16}"); 65 | rip = address; 66 | rip += 5; 67 | rip += *(int*)(rip + 2) + 6; 68 | } 69 | 70 | byte* remoteVa = rip - pUserAssembly.ToInt64() + mdUserAssembly.BaseAddress.ToInt64(); 71 | byte* dataPtr = null; 72 | 73 | Span readResult = stackalloc byte[8]; 74 | while (dataPtr == null) 75 | { 76 | Utils.NativeMethods.ReadProcessMemory(process.Handle, (nint)remoteVa, readResult, readResult.Length, out _); 77 | ulong value = BitConverter.ToUInt64(readResult); 78 | dataPtr = (byte*)value; 79 | } 80 | 81 | byte* localVa = dataPtr - mdUnityPlayer.BaseAddress.ToInt64() + pUnityPlayer.ToInt64(); 82 | while (localVa[0] == 0xE8 || localVa[0] == 0xE9) 83 | localVa += *(int*)(localVa + 1) + 5; 84 | 85 | localVa += *(int*)(localVa + 2) + 6; 86 | var rva = localVa - pUnityPlayer.ToInt64(); 87 | return (nint)(mdUnityPlayer.BaseAddress.ToInt64() + rva); 88 | } 89 | 90 | } 91 | } -------------------------------------------------------------------------------- /unlockfps/GameConstants.cs: -------------------------------------------------------------------------------- 1 | namespace UnlockFps; 2 | 3 | public static class GameConstants 4 | { 5 | public static string[] GameNames { get; } = ["YuanShen", "GenshinImpact"]; 6 | } -------------------------------------------------------------------------------- /unlockfps/Logging/ConsoleLogger.cs: -------------------------------------------------------------------------------- 1 | using Milki.Extensions.Threading; 2 | 3 | namespace UnlockFps.Logging; 4 | 5 | public class ConsoleLogger(string name) : ILogger 6 | { 7 | private static readonly SynchronizationContext LoggerSynchronizationContext = 8 | new SingleSynchronizationContext("Default ConsoleLogger"); 9 | 10 | public void Log(LogLevel logLevel, string message) 11 | { 12 | LoggerSynchronizationContext.Post(_ => 13 | { 14 | if (logLevel == LogLevel.Trace) 15 | { 16 | Console.ForegroundColor = ConsoleColor.DarkGray; 17 | } 18 | else if (logLevel == LogLevel.Debug) 19 | { 20 | Console.ForegroundColor = ConsoleColor.DarkGray; 21 | } 22 | else if (logLevel == LogLevel.Information) 23 | { 24 | Console.ForegroundColor = ConsoleColor.White; 25 | } 26 | else if (logLevel == LogLevel.Warning) 27 | { 28 | Console.ForegroundColor = ConsoleColor.DarkYellow; 29 | } 30 | else if (logLevel == LogLevel.Error) 31 | { 32 | Console.ForegroundColor = ConsoleColor.Red; 33 | } 34 | else if (logLevel == LogLevel.Critical) 35 | { 36 | Console.ForegroundColor = ConsoleColor.White; 37 | Console.BackgroundColor = ConsoleColor.Red; 38 | } 39 | 40 | Console.Write($"[{DateTime.Now:HH:mm:ss.fff}] "); 41 | if (logLevel == LogLevel.Trace) 42 | { 43 | Console.Write("TRACE "); 44 | } 45 | else if (logLevel == LogLevel.Debug) 46 | { 47 | Console.Write("DEBUG "); 48 | } 49 | else if (logLevel == LogLevel.Information) 50 | { 51 | Console.Write("INFO "); 52 | } 53 | else if (logLevel == LogLevel.Warning) 54 | { 55 | Console.Write("WARN "); 56 | } 57 | else if (logLevel == LogLevel.Error) 58 | { 59 | Console.Write("ERROR "); 60 | } 61 | else if (logLevel == LogLevel.Critical) 62 | { 63 | Console.Write("CRITICAL "); 64 | } 65 | 66 | Console.Write($"{name}: "); 67 | Console.WriteLine(message); 68 | Console.ResetColor(); 69 | }, null); 70 | } 71 | 72 | public void LogInformation(string message) 73 | { 74 | Log(LogLevel.Information, message); 75 | } 76 | 77 | public void LogDebug(string message) 78 | { 79 | Log(LogLevel.Debug, message); 80 | } 81 | 82 | public void LogError(string message) 83 | { 84 | Log(LogLevel.Error, message); 85 | } 86 | 87 | public void LogWarning(string message) 88 | { 89 | Log(LogLevel.Warning, message); 90 | } 91 | 92 | public void LogInformation(Exception exception, string message) 93 | { 94 | Log(LogLevel.Information, message + "\r\n" + exception); 95 | } 96 | 97 | public void LogDebug(Exception exception, string message) 98 | { 99 | Log(LogLevel.Debug, message + "\r\n" + exception); 100 | } 101 | 102 | public void LogError(Exception exception, string message) 103 | { 104 | Log(LogLevel.Error, message + "\r\n" + exception); 105 | } 106 | 107 | public void LogWarning(Exception exception, string message) 108 | { 109 | Log(LogLevel.Warning, message + "\r\n" + exception); 110 | } 111 | } -------------------------------------------------------------------------------- /unlockfps/Logging/ILogger.cs: -------------------------------------------------------------------------------- 1 | namespace UnlockFps.Logging; 2 | 3 | public interface ILogger 4 | { 5 | void Log(LogLevel logLevel, string message); 6 | void LogInformation(string message); 7 | void LogDebug(string message); 8 | void LogError(string message); 9 | void LogWarning(string message); 10 | void LogInformation(Exception exception, string message); 11 | void LogDebug(Exception exception, string message); 12 | void LogError(Exception exception, string message); 13 | void LogWarning(Exception exception, string message); 14 | } 15 | 16 | public interface ILogger : ILogger 17 | { 18 | } -------------------------------------------------------------------------------- /unlockfps/Logging/ILoggerFactory.cs: -------------------------------------------------------------------------------- 1 | namespace UnlockFps.Logging; 2 | 3 | public interface ILoggerFactory 4 | { 5 | ILogger CreateLogger(string name); 6 | ILogger CreateLogger(); 7 | } -------------------------------------------------------------------------------- /unlockfps/Logging/LogLevel.cs: -------------------------------------------------------------------------------- 1 | namespace UnlockFps.Logging; 2 | 3 | public enum LogLevel 4 | { 5 | Trace = 0, 6 | Debug = 1, 7 | Information = 2, 8 | Warning = 3, 9 | Error = 4, 10 | Critical = 5, 11 | None = 6, 12 | } -------------------------------------------------------------------------------- /unlockfps/Logging/LogManager.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace UnlockFps.Logging; 4 | 5 | public static class LogManager 6 | { 7 | private static ILoggerFactory? _loggerFactory; 8 | public static void SetLoggerFactory(ILoggerFactory loggerFactory) => _loggerFactory = loggerFactory; 9 | public static ILogger GetLogger(string name) => _loggerFactory?.CreateLogger(name) ?? new ConsoleLogger(name); 10 | //public static void Info(string message) 11 | //{ 12 | // var name = GetClassName() ?? nameof(Logger); 13 | // if (_loggerFactory is null) Console.WriteLine(message); 14 | // else _loggerFactory.CreateLogger(name).LogInformation(message); 15 | //} 16 | 17 | //public static void Debug(string message) 18 | //{ 19 | // var name = GetClassName() ?? nameof(Logger); 20 | // if (_loggerFactory is null) Console.WriteLine(message); 21 | // else _loggerFactory.CreateLogger(name).LogDebug(message); 22 | //} 23 | 24 | //public static void Error(string message) 25 | //{ 26 | // var name = GetClassName() ?? nameof(Logger); 27 | // if (_loggerFactory is null) Console.WriteLine(message); 28 | // else _loggerFactory.CreateLogger(name).LogError(message); 29 | //} 30 | 31 | //public static void Warn(string message) 32 | //{ 33 | // var name = GetClassName() ?? nameof(Logger); 34 | // if (_loggerFactory is null) Console.WriteLine(message); 35 | // else _loggerFactory.CreateLogger(name).LogWarning(message); 36 | //} 37 | 38 | //private static string? GetClassName() 39 | //{ 40 | // var methodInfo = new StackTrace().GetFrame(2)?.GetMethod(); 41 | // return methodInfo?.ReflectedType?.Name; 42 | //} 43 | } -------------------------------------------------------------------------------- /unlockfps/NativeMethods.txt: -------------------------------------------------------------------------------- 1 | CreateProcess 2 | DispatchMessage 3 | GetClassName 4 | GetForegroundWindow 5 | GetMessage 6 | GetProcessImageFileName 7 | GetWindowText 8 | GetWindowThreadProcessId 9 | ResumeThread 10 | SetWinEventHook 11 | TranslateMessage 12 | 13 | EVENT_SYSTEM_FOREGROUND 14 | WINEVENT_OUTOFCONTEXT 15 | WINEVENT_SKIPOWNPROCESS 16 | 17 | OpenProcess 18 | QueryFullProcessImageName 19 | 20 | EnumWindows -------------------------------------------------------------------------------- /unlockfps/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics.CodeAnalysis; 2 | using CommandLine; 3 | using UnlockFps.Logging; 4 | using UnlockFps.Services; 5 | 6 | namespace UnlockFps; 7 | 8 | internal class Program 9 | { 10 | public class Options 11 | { 12 | [Option('m', "monitor-only", Default = false, Required = false)] 13 | public bool MonitorOnly { get; set; } 14 | } 15 | 16 | private static readonly ILogger Logger = LogManager.GetLogger(nameof(Program)); 17 | 18 | [STAThread] 19 | [DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties, typeof(Options))] 20 | static async Task Main(string[] args) 21 | { 22 | await Parser.Default.ParseArguments(args) 23 | .WithParsedAsync(async o => 24 | { 25 | if (o.MonitorOnly) 26 | { 27 | CreateMonitorOnly(); 28 | } 29 | else 30 | { 31 | await CreateProcessWithMonitor(); 32 | } 33 | }); 34 | } 35 | 36 | private static void CreateMonitorOnly() 37 | { 38 | var configService = new ConfigService(); 39 | configService.Save(); 40 | 41 | using var cts = new CancellationTokenSource(); 42 | var gameInstanceService = new GameInstanceService(configService); 43 | Console.CancelKeyPress += (_, e) => 44 | { 45 | Exit(gameInstanceService); 46 | }; 47 | Logger.LogInformation("Monitor mode. Press 'Ctrl+C' to exit."); 48 | gameInstanceService.Start(); 49 | while (Console.ReadLine() != "exit") 50 | { 51 | 52 | } 53 | 54 | Exit(gameInstanceService); 55 | } 56 | 57 | private static async ValueTask CreateProcessWithMonitor() 58 | { 59 | var configService = new ConfigService(); 60 | configService.Save(); 61 | 62 | using var cts = new CancellationTokenSource(); 63 | var gameInstanceService = new GameInstanceService(configService); 64 | gameInstanceService.Start(); 65 | 66 | var processService = new ProcessService(configService); 67 | processService.Start(); 68 | 69 | gameInstanceService.ProcessExit += (p) => 70 | { 71 | Exit(gameInstanceService); 72 | }; 73 | Console.CancelKeyPress += (_, e) => 74 | { 75 | processService.KillLastProcess(); 76 | Exit(gameInstanceService); 77 | }; 78 | 79 | 80 | while (Console.ReadLine() != "exit") 81 | { 82 | 83 | } 84 | 85 | processService.KillLastProcess(); 86 | Exit(gameInstanceService); 87 | } 88 | 89 | private static void Exit(GameInstanceService gameInstanceService) 90 | { 91 | gameInstanceService.Stop(); 92 | Environment.Exit(0); 93 | } 94 | } -------------------------------------------------------------------------------- /unlockfps/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "unlockfps": { 4 | "commandName": "Project", 5 | "commandLineArgs": "-m" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /unlockfps/Services/ConfigService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.ObjectModel; 2 | using System.Text.Json; 3 | using System.Text.Json.Serialization; 4 | 5 | namespace UnlockFps.Services; 6 | 7 | public class ConfigService 8 | { 9 | private const string ConfigName = "fps_config.json"; 10 | 11 | public Config Config { get; private set; } = new(); 12 | 13 | public ConfigService() 14 | { 15 | Load(); 16 | StandardizeValues(); 17 | } 18 | 19 | private void Load() 20 | { 21 | if (!File.Exists(ConfigName)) 22 | return; 23 | 24 | var json = File.ReadAllText(ConfigName); 25 | Config = JsonSerializer.Deserialize(json, ConfigJsonContext.Default.Config)!; 26 | } 27 | 28 | private void StandardizeValues() 29 | { 30 | if (Config.LaunchOptions == null!) 31 | { 32 | Config.LaunchOptions = new LaunchOptions(); 33 | } 34 | 35 | if (!string.IsNullOrWhiteSpace(Config.LaunchOptions.GamePath)) 36 | { 37 | Config.LaunchOptions.GamePath = File.Exists(Config.LaunchOptions.GamePath) 38 | ? Path.GetFullPath(Config.LaunchOptions.GamePath) 39 | : null; 40 | } 41 | 42 | Config.FpsTarget = Math.Clamp(Config.FpsTarget, 1, 420); 43 | Config.ProcessPriority = Math.Clamp(Config.ProcessPriority, 0, 5); 44 | Config.LaunchOptions.CustomResolutionX = Math.Clamp(Config.LaunchOptions.CustomResolutionX, 200, 7680); 45 | Config.LaunchOptions.CustomResolutionY = Math.Clamp(Config.LaunchOptions.CustomResolutionY, 200, 4320); 46 | Config.LaunchOptions.MonitorId = Math.Clamp(Config.LaunchOptions.MonitorId, 1, 100); 47 | Config.FpsPowerSave = Math.Clamp(Config.FpsPowerSave, 1, 30); 48 | 49 | if (Config.LaunchOptions.DllList == null!) 50 | { 51 | Config.LaunchOptions.DllList = new ObservableCollection(); 52 | } 53 | else 54 | { 55 | Config.LaunchOptions.DllList = new ObservableCollection( 56 | Config.LaunchOptions.DllList 57 | .Where(k => !string.IsNullOrWhiteSpace(k) && File.Exists(k)) 58 | .Select(Path.GetFullPath) 59 | ); 60 | } 61 | } 62 | 63 | public void Save() 64 | { 65 | var json = JsonSerializer.Serialize(Config, ConfigJsonContext.Default.Config); 66 | File.WriteAllText(ConfigName, json); 67 | } 68 | } 69 | 70 | [JsonSourceGenerationOptions(WriteIndented = true, ReadCommentHandling = JsonCommentHandling.Skip)] 71 | [JsonSerializable(typeof(Config))] 72 | internal partial class ConfigJsonContext : JsonSerializerContext; -------------------------------------------------------------------------------- /unlockfps/Services/ProcessService.cs: -------------------------------------------------------------------------------- 1 | using System.Buffers; 2 | using System.ComponentModel; 3 | using System.Diagnostics; 4 | using System.Runtime.InteropServices; 5 | using System.Runtime.Versioning; 6 | using System.Text; 7 | using UnlockFps.Logging; 8 | using UnlockFps.Utils; 9 | using Windows.Win32.System.Threading; 10 | 11 | using static Windows.Win32.PInvoke; 12 | using PROCESS_INFORMATION = Windows.Win32.System.Threading.PROCESS_INFORMATION; 13 | 14 | namespace UnlockFps.Services; 15 | 16 | [SupportedOSPlatform("windows5.1.2600")] 17 | public class ProcessService 18 | { 19 | private static readonly ILogger Logger = LogManager.GetLogger(nameof(ProcessService)); 20 | 21 | private readonly Config _config; 22 | private int _lastProcessId; 23 | 24 | public ProcessService(ConfigService configService) 25 | { 26 | _config = configService.Config; 27 | } 28 | 29 | public void Start() 30 | { 31 | if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 32 | { 33 | throw new PlatformNotSupportedException("Only windows or wine is supported."); 34 | } 35 | 36 | var runningProcess = Process.GetProcesses() 37 | .FirstOrDefault(x => Array.IndexOf(GameConstants.GameNames, x.ProcessName) != -1); 38 | 39 | if (runningProcess is not null) 40 | { 41 | throw new Exception("An instance of the game is already running: " + runningProcess.Id); 42 | } 43 | 44 | var launchOptions = _config.LaunchOptions; 45 | using var disposable = CreateProcessRaw(launchOptions, out var lpProcessInformation); 46 | 47 | if (!ProcessUtils.InjectDlls(lpProcessInformation.hProcess, launchOptions.DllList)) 48 | { 49 | throw new Win32Exception(Marshal.GetLastWin32Error(), 50 | $"Dll Injection failed. ({Marshal.GetLastPInvokeErrorMessage()})"); 51 | } 52 | 53 | if (launchOptions.SuspendLoad) 54 | { 55 | var retCode = ResumeThread(lpProcessInformation.hThread); 56 | if (retCode == 0xFFFFFFFF) 57 | { 58 | throw new Win32Exception(Marshal.GetLastWin32Error(), 59 | $"ResumeThread failed. ({Marshal.GetLastPInvokeErrorMessage()})"); 60 | } 61 | } 62 | 63 | _lastProcessId = (int)lpProcessInformation.dwProcessId; 64 | } 65 | 66 | private static unsafe IDisposable CreateProcessRaw(LaunchOptions launchOptions, out PROCESS_INFORMATION lpProcessInformation) 67 | { 68 | var lpCurrentDirectory = Path.GetDirectoryName(launchOptions.GamePath); 69 | var commandLine = BuildCommandLine(launchOptions); 70 | var lpStartupInfo = new STARTUPINFOW(); 71 | var dwCreationFlags = launchOptions.SuspendLoad ? PROCESS_CREATION_FLAGS.CREATE_SUSPENDED : default; 72 | 73 | var array = ArrayPool.Shared.Rent(commandLine.Length + 1); 74 | try 75 | { 76 | var lpCommandLine = new Span(array, 0, commandLine.Length + 1); 77 | commandLine.CopyTo(lpCommandLine); 78 | lpCommandLine[^1] = '\0'; 79 | 80 | if (!CreateProcess(launchOptions.GamePath, ref lpCommandLine, 81 | default, default, false, 82 | dwCreationFlags, default, lpCurrentDirectory, 83 | in lpStartupInfo, out lpProcessInformation)) 84 | { 85 | throw new Win32Exception(Marshal.GetLastWin32Error(), 86 | $"CreateProcess failed. ({Marshal.GetLastPInvokeErrorMessage()})"); 87 | } 88 | } 89 | finally 90 | { 91 | ArrayPool.Shared.Return(array); 92 | } 93 | 94 | return new ThreadGuard(lpProcessInformation.hThread); 95 | } 96 | 97 | private static string BuildCommandLine(LaunchOptions launchOptions) 98 | { 99 | var commandLine = new StringBuilder($"{launchOptions.GamePath} "); 100 | if (launchOptions.IsWindowBorderless) 101 | { 102 | commandLine.Append("-popupwindow "); 103 | } 104 | 105 | if (launchOptions.UseCustomResolution) 106 | { 107 | commandLine.Append( 108 | $"-screen-width {launchOptions.CustomResolutionX} -screen-height {launchOptions.CustomResolutionY} "); 109 | } 110 | 111 | commandLine.Append($"-screen-fullscreen {(launchOptions.Fullscreen ? 1 : 0)} "); 112 | if (launchOptions.Fullscreen) 113 | { 114 | commandLine.Append($"-window-mode {(launchOptions.IsExclusiveFullscreen ? "exclusive" : "borderless")} "); 115 | } 116 | 117 | if (launchOptions.UseMobileUI) 118 | { 119 | commandLine.Append("use_mobile_platform -is_cloud 1 -platform_type CLOUD_THIRD_PARTY_MOBILE "); 120 | } 121 | 122 | commandLine.Append($"-monitor {launchOptions.MonitorId} "); 123 | return commandLine.ToString(); 124 | } 125 | 126 | public void KillLastProcess() 127 | { 128 | try 129 | { 130 | var process = Process.GetProcessById(_lastProcessId); 131 | if (Array.IndexOf(GameConstants.GameNames, process.ProcessName) != -1) 132 | { 133 | process.Kill(); 134 | } 135 | } 136 | catch (Exception ex) 137 | { 138 | Logger.LogWarning(ex, "Kill process failed"); 139 | } 140 | } 141 | } -------------------------------------------------------------------------------- /unlockfps/Utils/NativeMethods.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.InteropServices; 2 | 3 | namespace UnlockFps.Utils; 4 | 5 | internal static partial class NativeMethods 6 | { 7 | public delegate void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); 8 | 9 | [LibraryImport("user32.dll")] 10 | public static partial nint GetForegroundWindow(); 11 | 12 | [LibraryImport("user32.dll")] 13 | public static partial uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 14 | 15 | [LibraryImport("kernel32.dll", SetLastError = true)] 16 | [return: MarshalAs(UnmanagedType.Bool)] 17 | public static partial bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, Span lpBuffer, int nSize, out int lpNumberOfBytesWritten); 18 | 19 | [LibraryImport("kernel32.dll", SetLastError = true)] 20 | [return: MarshalAs(UnmanagedType.Bool)] 21 | public static partial bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, Span lpBuffer, int nSize, out int lpNumberOfBytesRead); 22 | 23 | [LibraryImport("kernel32.dll", EntryPoint = "LoadLibraryExW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] 24 | public static partial nint LoadLibraryEx(string lpLibFileName, IntPtr hFile, uint dwFlags); 25 | 26 | [LibraryImport("user32.dll", SetLastError = true)] 27 | public static partial IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, 28 | WinEventProc lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags); 29 | 30 | } -------------------------------------------------------------------------------- /unlockfps/Utils/ProcessUtils.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.InteropServices; 2 | using System.Text; 3 | 4 | namespace UnlockFps.Utils; 5 | 6 | internal class ProcessUtils 7 | { 8 | public static string GetProcessPathFromPid(uint pid, out nint processHandle) 9 | { 10 | var hProcess = Native.OpenProcess( 11 | ProcessAccess.QUERY_LIMITED_INFORMATION | 12 | ProcessAccess.TERMINATE | 13 | StandardAccess.SYNCHRONIZE, false, pid); 14 | 15 | processHandle = hProcess; 16 | 17 | if (hProcess == nint.Zero) 18 | return string.Empty; 19 | 20 | StringBuilder sb = new StringBuilder(1024); 21 | uint bufferSize = (uint)sb.Capacity; 22 | if (!Native.QueryFullProcessImageName(hProcess, 0, sb, ref bufferSize)) 23 | return string.Empty; 24 | 25 | return sb.ToString(); 26 | } 27 | 28 | public static bool InjectDlls(nint processHandle, IReadOnlyList dllPaths) 29 | { 30 | if (dllPaths.Count == 0) 31 | return true; 32 | 33 | Native.RtlAdjustPrivilege(20, true, false, out var _); 34 | 35 | var kernel32 = Native.LoadLibrary("kernel32.dll"); 36 | var loadLibrary = Native.GetProcAddress(kernel32, "LoadLibraryW"); 37 | 38 | var remoteVa = Native.VirtualAllocEx(processHandle, nint.Zero, 0x1000, 39 | AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.READWRITE); 40 | if (remoteVa == nint.Zero) 41 | return false; 42 | 43 | foreach (var dllPath in dllPaths) 44 | { 45 | var nativeString = Marshal.StringToHGlobalUni(dllPath); 46 | var bytes = Encoding.Unicode.GetBytes(dllPath); 47 | Marshal.FreeHGlobal(nativeString); 48 | 49 | if (!Native.WriteProcessMemory(processHandle, remoteVa, bytes, bytes.Length, out var bytesWritten)) 50 | return false; 51 | 52 | var thread = Native.CreateRemoteThread(processHandle, nint.Zero, 0, loadLibrary, remoteVa, 0, out var threadId); 53 | if (thread == nint.Zero) 54 | return false; 55 | 56 | Native.WaitForSingleObject(thread, uint.MaxValue); 57 | Native.CloseHandle(thread); 58 | Native.WriteProcessMemory(processHandle, remoteVa, new byte[bytes.Length], bytes.Length, out _); 59 | } 60 | 61 | Native.VirtualFreeEx(processHandle, remoteVa, 0, FreeType.RELEASE); 62 | 63 | return true; 64 | } 65 | 66 | public static unsafe nint PatternScan(nint module, string signature) 67 | { 68 | var dosHeader = Marshal.PtrToStructure(module); 69 | var ntHeader = Marshal.PtrToStructure((nint)(module.ToInt64() + dosHeader.e_lfanew)); 70 | 71 | var sizeOfImage = ntHeader.OptionalHeader.SizeOfImage; 72 | 73 | using var scanner = new Reloaded.Memory.Sigscan.Scanner((byte*)module.ToPointer(), (int)sizeOfImage); 74 | 75 | var result = scanner.FindPattern(signature); 76 | if (result.Found) 77 | { 78 | return (nint)(module.ToInt64() + result.Offset); 79 | } 80 | 81 | return nint.Zero; 82 | } 83 | 84 | public static nint GetModuleBase(nint hProcess, string moduleName) 85 | { 86 | var modules = new nint[1024]; 87 | 88 | if (!Native.EnumProcessModules(hProcess, modules, (uint)(modules.Length * nint.Size), out var bytesNeeded)) 89 | { 90 | if (Marshal.GetLastWin32Error() != 299) 91 | return nint.Zero; 92 | } 93 | 94 | foreach (var module in modules.Where(x => x != nint.Zero)) 95 | { 96 | StringBuilder sb = new StringBuilder(1024); 97 | if (Native.GetModuleBaseName(hProcess, module, sb, (uint)sb.Capacity) == 0) 98 | continue; 99 | 100 | if (sb.ToString() != moduleName) 101 | continue; 102 | 103 | if (!Native.GetModuleInformation(hProcess, module, out var moduleInfo, (uint)Marshal.SizeOf())) 104 | continue; 105 | 106 | return moduleInfo.lpBaseOfDll; 107 | } 108 | 109 | return nint.Zero; 110 | } 111 | } -------------------------------------------------------------------------------- /unlockfps/Utils/TaskUtils.cs: -------------------------------------------------------------------------------- 1 | namespace UnlockFps.Utils; 2 | 3 | public static class TaskUtils 4 | { 5 | public static bool TaskSleep(double milliseconds, CancellationTokenSource cts) 6 | { 7 | return TaskSleep(TimeSpan.FromMilliseconds(milliseconds), cts); 8 | } 9 | 10 | public static bool TaskSleep(double milliseconds, CancellationToken token) 11 | { 12 | return TaskSleep(TimeSpan.FromMilliseconds(milliseconds), token); 13 | } 14 | 15 | public static bool TaskSleep(TimeSpan delay, CancellationTokenSource cts) 16 | { 17 | return TaskSleep(delay, cts.Token); 18 | } 19 | 20 | public static bool TaskSleep(TimeSpan delay, in CancellationToken token) 21 | { 22 | try 23 | { 24 | Task.Delay(delay).Wait(token); 25 | } 26 | catch (TaskCanceledException) 27 | { 28 | return false; 29 | } 30 | catch (OperationCanceledException) 31 | { 32 | return false; 33 | } 34 | 35 | return true; 36 | } 37 | } -------------------------------------------------------------------------------- /unlockfps/Utils/Win32Window.cs: -------------------------------------------------------------------------------- 1 | using System.Buffers; 2 | using System.Runtime.Versioning; 3 | using Windows.Win32; 4 | using Windows.Win32.Foundation; 5 | using Windows.Win32.System.Threading; 6 | 7 | namespace UnlockFps.Utils; 8 | 9 | [SupportedOSPlatform("windows5.0")] 10 | public class Win32Window 11 | { 12 | private readonly HWND _hWnd; 13 | private string? _className; 14 | private string? _title; 15 | private string? _processName; 16 | private uint _pid; 17 | 18 | public Win32Window(nint handle) 19 | { 20 | _hWnd = (HWND)handle; 21 | } 22 | 23 | public nint Handle => _hWnd; 24 | 25 | public string ClassName => _className ??= CallWin32ToGetPWSTR(512, (p, l) => PInvoke.GetClassName(_hWnd, p, l)); 26 | 27 | public string Title => _title ??= CallWin32ToGetPWSTR(512, (p, l) => PInvoke.GetWindowText(_hWnd, p, l)); 28 | 29 | public uint ProcessId => _pid is 0 ? (_pid = GetProcessIdCore()) : _pid; 30 | 31 | //public string ProcessName => _processName ??= Process.GetProcessById((int)ProcessId).ProcessName; 32 | public unsafe string ProcessName 33 | { 34 | get 35 | { 36 | if (_processName == null) 37 | { 38 | var hProcess = 39 | PInvoke.OpenProcess( 40 | PROCESS_ACCESS_RIGHTS.PROCESS_QUERY_LIMITED_INFORMATION | 41 | PROCESS_ACCESS_RIGHTS.PROCESS_TERMINATE | PROCESS_ACCESS_RIGHTS.PROCESS_SYNCHRONIZE, false, 42 | ProcessId); 43 | try 44 | { 45 | uint bufferSize = 512; 46 | Span span = stackalloc char[(int)bufferSize]; 47 | 48 | fixed (char* o = span) 49 | { 50 | if (!PInvoke.QueryFullProcessImageName(hProcess, 0, new PWSTR(o), &bufferSize)) 51 | { 52 | return ""; 53 | } 54 | } 55 | 56 | var path = new string(span.Slice(0, (int)bufferSize)); 57 | var processName = Path.GetFileNameWithoutExtension(path); 58 | _processName = processName; 59 | } 60 | finally 61 | { 62 | PInvoke.CloseHandle(hProcess); 63 | } 64 | } 65 | 66 | return _processName; 67 | } 68 | } 69 | 70 | private unsafe uint GetProcessIdCore() 71 | { 72 | uint pid = 0; 73 | PInvoke.GetWindowThreadProcessId(_hWnd, &pid); 74 | return pid; 75 | } 76 | 77 | private unsafe string CallWin32ToGetPWSTR(int bufferLength, Func getter) 78 | { 79 | var buffer = ArrayPool.Shared.Rent(bufferLength); 80 | try 81 | { 82 | fixed (char* ptr = buffer) 83 | { 84 | getter(ptr, bufferLength); 85 | return new string(ptr); 86 | } 87 | } 88 | finally 89 | { 90 | ArrayPool.Shared.Return(buffer); 91 | } 92 | } 93 | } -------------------------------------------------------------------------------- /unlockfps/Utils/WineHelper.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics.CodeAnalysis; 2 | using System.Runtime.InteropServices; 3 | using UnlockFps.Logging; 4 | 5 | namespace UnlockFps.Utils; 6 | 7 | internal static partial class WineHelper 8 | { 9 | private static readonly ILogger Logger = LogManager.GetLogger(nameof(WineHelper)); 10 | 11 | public static bool DetectWine( 12 | [NotNullWhen(true)] out string? version, 13 | [NotNullWhen(true)] out string? buildId) 14 | { 15 | try 16 | { 17 | version = GetVersion(); 18 | Logger.LogInformation($"Wine version: {version}"); 19 | buildId = GetBuildId(); 20 | Logger.LogInformation($"Wine build id: {buildId}"); 21 | return true; 22 | } 23 | catch (EntryPointNotFoundException) 24 | { 25 | version = null; 26 | buildId = null; 27 | return false; 28 | } 29 | catch (DllNotFoundException) 30 | { 31 | version = null; 32 | buildId = null; 33 | return false; 34 | } 35 | } 36 | 37 | [LibraryImport("ntdll", EntryPoint = "wine_get_version", StringMarshalling = StringMarshalling.Utf8)] 38 | private static partial string GetVersion(); 39 | 40 | [LibraryImport("ntdll", EntryPoint = "wine_get_build_id", StringMarshalling = StringMarshalling.Utf8)] 41 | private static partial string GetBuildId(); 42 | } -------------------------------------------------------------------------------- /unlockfps/app.manifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 54 | 62 | 63 | 64 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /unlockfps/unlockfps.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | true 9 | app.manifest 10 | UnlockFps 11 | unlockfps_cli 12 | 13 | 14 | 15 | true 16 | 17 | true 18 | true 19 | true 20 | 21 | 22 | 23 | 24 | 25 | all 26 | 27 | 28 | 29 | all 30 | compile; runtime; build; native; contentfiles; analyzers; buildtransitive 31 | 32 | 33 | all 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /unlockfps_gui/App.axaml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 | 11 | 16 | 17 | 18 | 19 | 22 | 25 | 26 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /unlockfps_gui/App.axaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using Avalonia; 4 | using Avalonia.Controls; 5 | using Avalonia.Controls.ApplicationLifetimes; 6 | using Avalonia.Markup.Xaml; 7 | using Microsoft.Extensions.DependencyInjection; 8 | using UnlockFps.Gui.Utils; 9 | using UnlockFps.Gui.Views; 10 | using UnlockFps.Services; 11 | 12 | namespace UnlockFps.Gui; 13 | 14 | public partial class App : Application 15 | { 16 | public static ServiceProvider DefaultServices { get; private set; } = null!; 17 | 18 | public static Window? CurrentMainWindow => 19 | (App.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow; 20 | 21 | public override void Initialize() 22 | { 23 | AvaloniaXamlLoader.Load(this); 24 | } 25 | 26 | public override void RegisterServices() 27 | { 28 | base.RegisterServices(); 29 | 30 | var services = new ServiceCollection(); 31 | services.AddTransient(); 32 | services.AddTransient(); 33 | services.AddTransient(); 34 | services.AddTransient(); 35 | services.AddTransient(); 36 | services.AddSingleton(); 37 | services.AddSingleton(); 38 | services.AddSingleton(); 39 | DefaultServices = services.BuildServiceProvider(); 40 | } 41 | 42 | public override void OnFrameworkInitializationCompleted() 43 | { 44 | if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) 45 | { 46 | TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException; 47 | var configService = DefaultServices.GetRequiredService(); 48 | configService.Config.PropertyChanged += Config_PropertyChanged; 49 | ToggleConsole(configService.Config.ShowDebugConsole); 50 | if (!Program.DuplicatedInstance) 51 | { 52 | desktop.MainWindow = DefaultServices.GetRequiredService(); 53 | } 54 | else 55 | { 56 | var alertWindow = DefaultServices.GetRequiredService(); 57 | alertWindow.Text = "Another unlocker is already running."; 58 | desktop.MainWindow = alertWindow; 59 | } 60 | } 61 | 62 | base.OnFrameworkInitializationCompleted(); 63 | } 64 | 65 | private static void Config_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) 66 | { 67 | if (sender is Config config && e.PropertyName == nameof(Config.ShowDebugConsole)) 68 | { 69 | ToggleConsole(config.ShowDebugConsole); 70 | } 71 | } 72 | 73 | private static void TaskSchedulerOnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e) 74 | { 75 | if (e.Exception.InnerExceptions.Count == 1) 76 | { 77 | Console.WriteLine("Unobserved task exception: " + e.Exception.InnerException); 78 | } 79 | else 80 | { 81 | Console.WriteLine("Unobserved task exception: " + e.Exception); 82 | } 83 | } 84 | 85 | private static void ToggleConsole(bool show) 86 | { 87 | try 88 | { 89 | if (show) 90 | { 91 | ConsoleManager.Show(); 92 | } 93 | else 94 | { 95 | ConsoleManager.Hide(); 96 | } 97 | } 98 | catch 99 | { 100 | // ignored 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /unlockfps_gui/AppBuilderExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Avalonia; 3 | using Avalonia.Media; 4 | using SkiaSharp; 5 | 6 | namespace UnlockFps.Gui; 7 | 8 | public static class AppBuilderExtensions 9 | { 10 | public static AppBuilder WithNativeFonts(this AppBuilder appBuilder, string? specificFontFamily = null) 11 | { 12 | string? familyName = null; 13 | 14 | if (specificFontFamily != null) 15 | { 16 | var family = SKFontManager.Default.MatchFamily(specificFontFamily); 17 | familyName = family?.FamilyName; 18 | } 19 | 20 | FontManagerOptions options = new(); 21 | 22 | if (familyName == null) 23 | { 24 | familyName = SKFontManager.Default.MatchCharacter('a')?.FamilyName; 25 | if (familyName == null) 26 | { 27 | Console.Error.WriteLine("Cannot find default font."); 28 | } 29 | } 30 | 31 | if (familyName != null) 32 | { 33 | options.DefaultFamilyName = familyName; 34 | 35 | var fontFallbacks = new FontFallback[] 36 | { 37 | new() { FontFamily = FontFamily.Parse(familyName) }, 38 | }; 39 | options.FontFallbacks = fontFallbacks; 40 | } 41 | 42 | return appBuilder.With(options); 43 | } 44 | } -------------------------------------------------------------------------------- /unlockfps_gui/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | [assembly: PropertyChanged.FilterType(@".*ViewModel")] 2 | [assembly: PropertyChanged.FilterType(@".*\.Config")] -------------------------------------------------------------------------------- /unlockfps_gui/Assets/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Milkitic/genshin-fps-unlock-universal/78ff40bb376b53a582ac604379a286e16200e866/unlockfps_gui/Assets/icon.ico -------------------------------------------------------------------------------- /unlockfps_gui/Converters/BooleanConverters.cs: -------------------------------------------------------------------------------- 1 | using Avalonia.Data.Converters; 2 | 3 | namespace UnlockFps.Gui.Converters; 4 | 5 | public static class BooleanConverters 6 | { 7 | public static readonly IValueConverter Not = 8 | new DelegateConverter((x, _, _, _) => !(bool)x, (x, _, _, _) => !(bool)x); 9 | } -------------------------------------------------------------------------------- /unlockfps_gui/Converters/DelegateConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Globalization; 3 | using Avalonia.Data.Converters; 4 | 5 | namespace UnlockFps.Gui.Converters; 6 | 7 | public class DelegateConverter : IValueConverter 8 | { 9 | public delegate object? ConvertDelegate(object? value, Type targetType, object? parameter, CultureInfo culture); 10 | 11 | public delegate object? ConvertBackDelegate(object? value, Type targetType, object? parameter, 12 | CultureInfo culture); 13 | 14 | private readonly ConvertDelegate _convert; 15 | private readonly ConvertBackDelegate? _convertBack; 16 | 17 | public DelegateConverter(ConvertDelegate convert, ConvertBackDelegate? convertBack = null) 18 | { 19 | _convert = convert ?? throw new ArgumentNullException(nameof(convert)); 20 | _convertBack = convertBack; 21 | } 22 | 23 | public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) 24 | { 25 | return _convert(value, targetType, parameter, culture); 26 | } 27 | 28 | public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) 29 | { 30 | if (_convertBack == null) 31 | throw new NotImplementedException($"ConvertBack() of {GetType().Name} is not implemented."); 32 | return _convertBack(value, targetType, parameter, culture); 33 | } 34 | } -------------------------------------------------------------------------------- /unlockfps_gui/Converters/Enum2ListConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Globalization; 3 | using System.Linq; 4 | using Avalonia.Data.Converters; 5 | 6 | namespace UnlockFps.Gui.Converters; 7 | 8 | internal sealed class Enum2ListConverter : IValueConverter 9 | { 10 | public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) 11 | { 12 | if (parameter is Type t && t.IsSubclassOf(typeof(Enum))) 13 | return GetTypeList(t); 14 | if (value is Enum) 15 | return GetTypeList(value.GetType()); 16 | return value; 17 | } 18 | 19 | public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) 20 | { 21 | throw new NotImplementedException(); 22 | } 23 | 24 | private static object GetTypeList(Type t) 25 | { 26 | var list = Enum.GetValues(t).Cast().ToList(); 27 | return list; 28 | } 29 | } -------------------------------------------------------------------------------- /unlockfps_gui/Converters/FullPath2NameConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Globalization; 3 | using Avalonia.Data.Converters; 4 | 5 | namespace UnlockFps.Gui.Converters; 6 | 7 | internal sealed class FullPath2NameConverter : IValueConverter 8 | { 9 | public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) 10 | { 11 | if (value is string s) 12 | { 13 | return System.IO.Path.GetFileName(s); 14 | } 15 | 16 | return value?.ToString(); 17 | } 18 | 19 | public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) 20 | { 21 | throw new NotImplementedException(); 22 | } 23 | } -------------------------------------------------------------------------------- /unlockfps_gui/Converters/HasItemsConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Globalization; 4 | using System.Linq; 5 | using Avalonia; 6 | using Avalonia.Data.Converters; 7 | 8 | namespace UnlockFps.Gui.Converters; 9 | 10 | internal sealed class HasItemsConverter : IValueConverter 11 | { 12 | public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) 13 | { 14 | if (value is IEnumerable enumerable) 15 | { 16 | var enumerator = enumerable.GetEnumerator(); 17 | var moveNext = enumerator.MoveNext(); 18 | if (enumerator is IDisposable disposable) 19 | { 20 | disposable.Dispose(); 21 | } 22 | 23 | return moveNext; 24 | } 25 | 26 | if (value is int i) 27 | { 28 | return i > 0; 29 | } 30 | 31 | return AvaloniaProperty.UnsetValue; 32 | } 33 | 34 | public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) 35 | { 36 | throw new NotImplementedException(); 37 | } 38 | 39 | private static object GetTypeList(Type t) 40 | { 41 | var list = Enum.GetValues(t).Cast().ToList(); 42 | return list; 43 | } 44 | } -------------------------------------------------------------------------------- /unlockfps_gui/FodyWeavers.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | -------------------------------------------------------------------------------- /unlockfps_gui/OSVersionExt/Environment/EnvironmentProvider.cs: -------------------------------------------------------------------------------- 1 | using OSVersionExt.Environment; 2 | 3 | 4 | namespace OSVersionExt 5 | { 6 | public class EnvironmentProvider : IEnvironment 7 | { 8 | public EnvironmentProvider() 9 | { 10 | // NOP 11 | } 12 | 13 | /// 14 | /// Determines whether the current operating system is a 64-bit operating system. 15 | /// 16 | /// true if the operating system is 64-bit; otherwise, false. 17 | public bool Is64BitOperatingSystem() 18 | { 19 | return System.Environment.Is64BitOperatingSystem; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /unlockfps_gui/OSVersionExt/Environment/IEnvironment.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OSVersionExt.Environment 7 | { 8 | public interface IEnvironment 9 | { 10 | /// 11 | /// Determines whether the current operating system is a 64-bit operating system. 12 | /// 13 | /// 14 | bool Is64BitOperatingSystem(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /unlockfps_gui/OSVersionExt/MajorVersion10/MajorVersion10Properties.cs: -------------------------------------------------------------------------------- 1 | using OSVersionExt.Registry; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace OSVersionExt.MajorVersion10 8 | { 9 | public readonly struct RegistryEntry 10 | { 11 | /// 12 | /// The full registry path of the key, beginning with a valid registry root, such as "HKEY_CURRENT_USER". 13 | /// 14 | public string FullPathToKey { get; } 15 | 16 | /// 17 | /// The name of the name/value pair. 18 | /// 19 | public string ValueName { get; } 20 | 21 | /// 22 | /// The value to return if ValueName does not exist. 23 | /// 24 | public string DefaultValueNotFound {get;} 25 | 26 | public RegistryEntry(string fullPathToKey, string valueName, string defaultValue) 27 | { 28 | FullPathToKey = fullPathToKey; 29 | ValueName = valueName; 30 | DefaultValueNotFound = defaultValue; 31 | } 32 | } 33 | 34 | /// 35 | /// Get the release id and UBR (Update Build Revision) on Windows system having major version 10. 36 | /// 37 | public class MajorVersion10Properties 38 | { 39 | private const string FullPathToCurrentVersion = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion"; 40 | private readonly RegistryEntry _releaseIdRegistry = new RegistryEntry(FullPathToCurrentVersion, "ReleaseId", null); 41 | private readonly RegistryEntry _ubrRegistry = new RegistryEntry(FullPathToCurrentVersion, "UBR", null); 42 | private readonly RegistryEntry _displayVersionRegistry = new RegistryEntry(FullPathToCurrentVersion, "DisplayVersion", null); 43 | 44 | private IRegistry _registryProvider; 45 | 46 | private string _releaseId = null; 47 | private string _UBR = null; 48 | private string _displayVersion = null; 49 | 50 | /// 51 | /// Returns the Windows numeric release ID (e.g. 1909, 2004, 2009). For versions like 20H2 use DisplayVersion. 52 | /// 53 | /// returns the release id or null, if detection has failed. 54 | [Obsolete("Works until Windows 10 version 2009/20H2 (build 19042) only. Use DisplayVersion instead.")] 55 | public string ReleaseId { get => _releaseId; } 56 | 57 | /// 58 | /// Gets the Update Build Revision of a Windows 10 system 59 | /// 60 | /// returns null, if detection has failed. 61 | public string UBR { get => _UBR; } 62 | 63 | /// 64 | /// Gets the Display Version such as 1909, 2004, 20H2. 65 | /// 66 | public string DisplayVersion { get => _displayVersion; } 67 | 68 | /// 69 | /// Create instance with custom registry provider. 70 | /// 71 | /// 72 | /// 73 | public MajorVersion10Properties(IRegistry registryProvider) 74 | { 75 | _ = registryProvider ?? throw new ArgumentNullException(); 76 | 77 | _registryProvider = registryProvider; 78 | GetAllProperties(); 79 | } 80 | 81 | public MajorVersion10Properties() 82 | { 83 | _registryProvider = new RegistryProviderDefault(); 84 | GetAllProperties(); 85 | } 86 | 87 | private void GetAllProperties() 88 | { 89 | _releaseId = GetReleaseId(); 90 | _UBR = GetUBR(); 91 | _displayVersion = GetDisplayVersion(); 92 | } 93 | 94 | /// 95 | /// The version number representing feature updates, is referred as the release id, such as 1903, 1909. 96 | /// Works until Windows 10 version 2009/20H2 (build 19042) only. 97 | /// 98 | /// Returns the release id or null, if value is not available. 99 | /// Feature updates for Windows 10 are released twice a year, around March and September, via the Semi-Annual Channel. 100 | private string GetReleaseId() 101 | { 102 | return _registryProvider.GetValue(_releaseIdRegistry.FullPathToKey, _releaseIdRegistry.ValueName, _releaseIdRegistry.DefaultValueNotFound)?.ToString(); 103 | } 104 | 105 | /// 106 | /// Gets the UBR (Update Build Revision). 107 | /// 108 | /// 109 | /// E.g, it returns 778 for Microsoft Windows [Version 10.0.18363.778] 110 | private string GetUBR() 111 | { 112 | return _registryProvider.GetValue(_ubrRegistry.FullPathToKey, _ubrRegistry.ValueName, _ubrRegistry.DefaultValueNotFound)?.ToString(); 113 | } 114 | 115 | 116 | /// 117 | /// Returns the DisplayVersion such as 20H2 (for ReleaseId 2009). If value is not found, it will return the ReleaseId. 118 | /// 119 | /// 120 | private string GetDisplayVersion() 121 | { 122 | string displayVersion = _registryProvider.GetValue(_displayVersionRegistry.FullPathToKey, _displayVersionRegistry.ValueName, _displayVersionRegistry.DefaultValueNotFound)?.ToString(); 123 | 124 | return displayVersion ?? GetReleaseId(); 125 | } 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /unlockfps_gui/OSVersionExt/MajorVersion10/RegistryProviderDefault.cs: -------------------------------------------------------------------------------- 1 | using OSVersionExt.Registry; 2 | 3 | namespace OSVersionExt.MajorVersion10 4 | { 5 | public class RegistryProviderDefault : IRegistry 6 | { 7 | public RegistryProviderDefault() 8 | { 9 | // NOP 10 | } 11 | public object GetValue(string keyName, string valueName, object defaultValue) 12 | { 13 | return Microsoft.Win32.Registry.GetValue(keyName, valueName, defaultValue); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /unlockfps_gui/OSVersionExt/Registry/IRegistry.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OSVersionExt.Registry 7 | { 8 | public interface IRegistry 9 | { 10 | /// 11 | /// Retrieves the value associated with the specified name, in the specified registry key. 12 | /// If the name is not found in the specified key, returns a default value that you provide, or null if the specified key does not exist. 13 | /// 14 | /// The full registry path of the key, beginning with a valid registry root, such as "HKEY_CURRENT_USER". 15 | /// The name of the name/value pair. 16 | /// The value to return if valueName does not exist. 17 | /// 18 | object GetValue(string keyName, string valueName, object defaultValue); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /unlockfps_gui/OSVersionExt/VersionInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | 7 | namespace OSVersionExt 8 | { 9 | public class VersionInfo 10 | { 11 | public Version Version { get; private set; } 12 | 13 | public VersionInfo(int major, int minor, int build) 14 | { 15 | this.Version = new Version(major, minor, build); 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /unlockfps_gui/OSVersionExt/Win32API/IWin32API.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace OSVersionExt.Win32API 7 | { 8 | public interface IWin32API 9 | { 10 | NTSTATUS RtlGetVersion(ref OSVERSIONINFOEX versionInfo); 11 | int GetSystemMetrics(SystemMetric smIndex); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /unlockfps_gui/OSVersionExt/Win32API/Win32ApiEnums.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.InteropServices; 5 | using System.Text; 6 | 7 | namespace OSVersionExt.Win32API 8 | { 9 | /// 10 | /// Holds information, whether the Windows is a server, workstation or domain controller. 11 | /// 12 | public enum ProductType : byte 13 | { 14 | /// 15 | /// The operating system is Windows 10, Windows 8, Windows 7,... 16 | /// 17 | /// VER_NT_WORKSTATION 18 | Workstation = 0x0000001, 19 | /// 20 | /// The system is a domain controller and the operating system is Windows Server. 21 | /// 22 | /// VER_NT_DOMAIN_CONTROLLER 23 | DomainController = 0x0000002, 24 | /// 25 | /// The operating system is Windows Server. Note that a server that is also a domain controller 26 | /// is reported as VER_NT_DOMAIN_CONTROLLER, not VER_NT_SERVER. 27 | /// 28 | /// VER_NT_SERVER 29 | Server = 0x0000003 30 | } 31 | 32 | 33 | /// 34 | /// Holds specific information for certain Windows variants (e.g. Small Business, Datacenter,...) 35 | /// 36 | [Flags] 37 | public enum SuiteMask : ushort 38 | { 39 | /// 40 | /// Microsoft BackOffice components are installed. 41 | /// 42 | VER_SUITE_BACKOFFICE = 0x00000004, 43 | /// 44 | /// Windows Server 2003, Web Edition is installed 45 | /// 46 | VER_SUITE_BLADE = 0x00000400, 47 | /// 48 | /// Windows Server 2003, Compute Cluster Edition is installed. 49 | /// 50 | VER_SUITE_COMPUTE_SERVER = 0x00004000, 51 | /// 52 | /// Windows Server 2008 Datacenter, Windows Server 2003, Datacenter Edition, or Windows 2000 Datacenter Server is installed. 53 | /// 54 | VER_SUITE_DATACENTER = 0x00000080, 55 | /// 56 | /// Windows Server 2008 Enterprise, Windows Server 2003, Enterprise Edition, or Windows 2000 Advanced Server is installed. 57 | /// Refer to the Remarks section for more information about this bit flag. 58 | /// 59 | VER_SUITE_ENTERPRISE = 0x00000002, 60 | /// 61 | /// Windows XP Embedded is installed. 62 | /// 63 | VER_SUITE_EMBEDDEDNT = 0x00000040, 64 | /// 65 | /// Windows Vista Home Premium, Windows Vista Home Basic, or Windows XP Home Edition is installed. 66 | /// 67 | VER_SUITE_PERSONAL = 0x00000200, 68 | /// 69 | /// Remote Desktop is supported, but only one interactive session is supported. This value is set unless the system is running in application server mode. 70 | /// 71 | VER_SUITE_SINGLEUSERTS = 0x00000100, 72 | /// 73 | /// Microsoft Small Business Server was once installed on the system, but may have been upgraded to another version of Windows. 74 | /// Refer to the Remarks section for more information about this bit flag. 75 | /// 76 | VER_SUITE_SMALLBUSINESS = 0x00000001, 77 | /// 78 | /// Microsoft Small Business Server is installed with the restrictive client license in force. Refer to the Remarks section for more information about this bit flag. 79 | /// 80 | VER_SUITE_SMALLBUSINESS_RESTRICTED = 0x00000020, 81 | /// 82 | /// Windows Storage Server 2003 R2 or Windows Storage Server 2003is installed. 83 | /// 84 | VER_SUITE_STORAGE_SERVER = 0x00002000, 85 | /// 86 | /// Terminal Services is installed. This value is always set. 87 | /// If VER_SUITE_TERMINAL is set but VER_SUITE_SINGLEUSERTS is not set, the system is running in application server mode. 88 | /// 89 | VER_SUITE_TERMINAL = 0x00000010, 90 | /// 91 | /// Windows Home Server is installed. 92 | /// 93 | VER_SUITE_WH_SERVER = 0x00008000 94 | 95 | //VER_SUITE_MULTIUSERTS = 0x00020000 96 | } 97 | 98 | public enum NTSTATUS : uint 99 | { 100 | /// 101 | /// The operation completed successfully. 102 | /// 103 | STATUS_SUCCESS = 0x00000000 104 | } 105 | 106 | /// 107 | /// Contains operating system version information. The information includes major and 108 | /// minor version numbers, a build number, a platform identifier, and information about 109 | /// product suites and the latest Service Pack installed on the system. 110 | /// 111 | /// 112 | /// 113 | /// var osVersionInfo = new OSVERSIONINFOEX { OSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)) }; 114 | /// 115 | /// 116 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 117 | public struct OSVERSIONINFOEX 118 | { 119 | // The OSVersionInfoSize field must be set to Marshal.SizeOf(typeof(OSVERSIONINFOEX)) 120 | public int OSVersionInfoSize; 121 | public int MajorVersion; 122 | public int MinorVersion; 123 | public int BuildNumber; 124 | public int PlatformId; 125 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] 126 | public string CSDVersion; 127 | public ushort ServicePackMajor; 128 | public ushort ServicePackMinor; 129 | public SuiteMask SuiteMask; 130 | public ProductType ProductType; 131 | public byte Reserved; 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /unlockfps_gui/OSVersionExt/Win32API/Win32ApiProvider.cs: -------------------------------------------------------------------------------- 1 | using OSVersionExt.Win32API; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Runtime.InteropServices; 6 | using System.Security; 7 | using System.Text; 8 | 9 | namespace OSVersionExt 10 | { 11 | /// 12 | /// Win32 API Provider 13 | /// 14 | /// CLR wrapper https://github.com/microsoft/referencesource/blob/master/mscorlib/microsoft/win32/win32native.cs 15 | public class Win32ApiProvider : IWin32API 16 | { 17 | private const String NTDLL = "ntdll.dll"; 18 | private const String USER32 = "user32.dll"; 19 | 20 | [SecurityCritical] 21 | [DllImport(NTDLL, EntryPoint = "RtlGetVersion", SetLastError = true, CharSet = CharSet.Unicode)] 22 | internal static extern NTSTATUS ntdll_RtlGetVersion(ref OSVERSIONINFOEX versionInfo); 23 | 24 | [DllImport(USER32, EntryPoint = "GetSystemMetrics")] 25 | internal static extern int ntdll_GetSystemMetrics(SystemMetric smIndex); 26 | 27 | 28 | public NTSTATUS RtlGetVersion(ref OSVERSIONINFOEX versionInfo) 29 | { 30 | return ntdll_RtlGetVersion(ref versionInfo); 31 | } 32 | 33 | public int GetSystemMetrics(SystemMetric smIndex) 34 | { 35 | return ntdll_GetSystemMetrics(smIndex); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /unlockfps_gui/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using Avalonia; 4 | using Avalonia.ReactiveUI; 5 | using UnlockFps.Gui.Utils; 6 | 7 | namespace UnlockFps.Gui; 8 | 9 | internal sealed class Program 10 | { 11 | // Initialization code. Don't use any Avalonia, third-party APIs or any 12 | // SynchronizationContext-reliant code before AppMain is called: things aren't initialized 13 | // yet and stuff might break. 14 | [STAThread] 15 | public static void Main(string[] args) 16 | { 17 | using (new Mutex(true, @"GenshinFPSUnlocker", out var createdNew)) 18 | { 19 | DuplicatedInstance = !createdNew; 20 | BuildAvaloniaApp() 21 | .StartWithClassicDesktopLifetime(args); 22 | } 23 | } 24 | 25 | public static bool DuplicatedInstance { get; private set; } 26 | 27 | // Avalonia configuration, don't remove; also used by visual designer. 28 | public static AppBuilder BuildAvaloniaApp() 29 | { 30 | var appBuilder = AppBuilder.Configure() 31 | .UsePlatformDetect() 32 | .WithNativeFonts() 33 | .LogToTrace() 34 | .UseReactiveUI(); 35 | if (WineHelper.DetectWine(out _, out _)) 36 | { 37 | return appBuilder 38 | .With(new Win32PlatformOptions 39 | { 40 | CompositionMode = [Win32CompositionMode.RedirectionSurface], 41 | RenderingMode = [Win32RenderingMode.Software], 42 | OverlayPopups = true 43 | }); 44 | } 45 | else 46 | { 47 | return appBuilder; 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /unlockfps_gui/Styles/TabStyles.axaml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 64 | 65 | 66 | 67 | 68 | 71 | 72 | 73 | 77 | 80 | 81 | 82 | 14 83 | Normal 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /unlockfps_gui/Utils/AssemblyAttributeUtil.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Reflection; 4 | using System.Runtime.Loader; 5 | 6 | namespace UnlockFps.Gui.Utils; 7 | 8 | internal static class AssemblyAttributeUtil 9 | { 10 | public static T? GetAssemblyAttribute(out T? coreAttribute) where T : Attribute 11 | { 12 | var versionAsm = Assembly.GetCallingAssembly(); 13 | var entryAsm = Assembly.GetEntryAssembly(); 14 | var desiredEntryAsmName = versionAsm.GetName().Name?.Split('.')[0]; 15 | var desiredEntryAsm = 16 | AssemblyLoadContext.Default.Assemblies.FirstOrDefault(k => k.GetName().Name == desiredEntryAsmName); 17 | if (desiredEntryAsm != null) 18 | { 19 | versionAsm = desiredEntryAsm; 20 | } 21 | 22 | if (entryAsm == versionAsm) 23 | { 24 | coreAttribute = null; 25 | return versionAsm.GetCustomAttribute(); 26 | } 27 | else 28 | { 29 | coreAttribute = versionAsm.GetCustomAttribute(); 30 | return entryAsm?.GetCustomAttribute(); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /unlockfps_gui/Utils/ConsoleManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.IO; 4 | using System.Reflection.Metadata; 5 | using System.Runtime.InteropServices; 6 | using System.Security; 7 | 8 | namespace UnlockFps.Gui.Utils; 9 | 10 | [SuppressUnmanagedCodeSecurity] 11 | internal static class ConsoleManager 12 | { 13 | [Flags] 14 | public enum CharacterAttributes 15 | { 16 | FOREGROUND_BLUE = 0x0001, 17 | FOREGROUND_GREEN = 0x0002, 18 | FOREGROUND_RED = 0x0004, 19 | FOREGROUND_INTENSITY = 0x0008, 20 | BACKGROUND_BLUE = 0x0010, 21 | BACKGROUND_GREEN = 0x0020, 22 | BACKGROUND_RED = 0x0040, 23 | BACKGROUND_INTENSITY = 0x0080, 24 | COMMON_LVB_LEADING_BYTE = 0x0100, 25 | COMMON_LVB_TRAILING_BYTE = 0x0200, 26 | COMMON_LVB_GRID_HORIZONTAL = 0x0400, 27 | COMMON_LVB_GRID_LVERTICAL = 0x0800, 28 | COMMON_LVB_GRID_RVERTICAL = 0x1000, 29 | COMMON_LVB_REVERSE_VIDEO = 0x4000, 30 | COMMON_LVB_UNDERSCORE = 0x8000 31 | } 32 | 33 | private static ConsoleEventDelegate? _handler; 34 | private delegate bool ConsoleEventDelegate(int eventType); 35 | public static bool HasConsole => GetConsoleWindow() != IntPtr.Zero; 36 | 37 | private const string Kernel32_DllName = "kernel32"; 38 | 39 | [DllImport(Kernel32_DllName)] 40 | private static extern bool AllocConsole(); 41 | 42 | [DllImport(Kernel32_DllName)] 43 | private static extern bool FreeConsole(); 44 | 45 | [DllImport(Kernel32_DllName)] 46 | private static extern IntPtr GetConsoleWindow(); 47 | 48 | [DllImport(Kernel32_DllName)] 49 | private static extern int GetConsoleOutputCP(); 50 | [DllImport(Kernel32_DllName)] 51 | private static extern int SetConsoleTextAttribute(IntPtr hConsoleOutput, 52 | CharacterAttributes wAttributes); 53 | [DllImport(Kernel32_DllName, SetLastError = true)] 54 | private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add); 55 | 56 | private const int MF_BYCOMMAND = 0x00000000; 57 | public const int SC_CLOSE = 0xF060; 58 | 59 | [DllImport("user32.dll")] 60 | public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags); 61 | 62 | [DllImport("user32.dll")] 63 | private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); 64 | 65 | /// 66 | /// Creates a new console instance if the process is not attached to a console already. 67 | /// 68 | public static void Show() 69 | { 70 | if (HasConsole) return; 71 | AllocConsole(); 72 | //var intPtr = GetConsoleWindow(); 73 | //SetConsoleTextAttribute(intPtr, 74 | // CharacterAttributes.BACKGROUND_INTENSITY | CharacterAttributes.FOREGROUND_INTENSITY); 75 | InvalidateOutAndError(); 76 | 77 | Console.Title = "Genshin FPS Unlocker Debugging Console"; 78 | Console.ForegroundColor = ConsoleColor.Yellow; 79 | Console.WriteLine("Note: Closing this window will lead to program exiting."); 80 | Console.ResetColor(); 81 | var hMenu = GetSystemMenu(GetConsoleWindow(), false); 82 | DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND); 83 | } 84 | 85 | public static void BindExitAction(Action? exitAction) 86 | { 87 | if (exitAction == null || _handler != null) return; 88 | _handler = eventType => 89 | { 90 | if (eventType != 2) return false; 91 | exitAction(); 92 | return true; 93 | }; 94 | 95 | SetConsoleCtrlHandler(_handler, true); 96 | } 97 | 98 | /// 99 | /// If the process has a console attached to it, it will be detached and no longer visible. Writing to the System.Console is still possible, but no output will be shown. 100 | /// 101 | public static void Hide() 102 | { 103 | if (!HasConsole) return; 104 | SetOutAndErrorNull(); 105 | FreeConsole(); 106 | } 107 | 108 | //public static void Toggle() 109 | //{ 110 | // if (HasConsole) Hide(); 111 | // else Show(); 112 | //} 113 | 114 | private static void InvalidateOutAndError() 115 | { 116 | Type type = typeof(System.Console); 117 | System.Reflection.FieldInfo? _out = type.GetField( 118 | #if !NETSTANDARD2_0 119 | "s_out", 120 | #else 121 | "_out", 122 | #endif 123 | 124 | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); 125 | 126 | System.Reflection.FieldInfo? _error = type.GetField( 127 | #if !NETSTANDARD2_0 128 | "s_error", 129 | #else 130 | "_error", 131 | #endif 132 | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); 133 | 134 | //System.Reflection.MethodInfo? _InitializeStdOutError = type.GetMethod("InitializeStdOutError", 135 | // System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); 136 | 137 | Debug.Assert(_out != null); 138 | Debug.Assert(_error != null); 139 | 140 | //Debug.Assert(_InitializeStdOutError != null); 141 | 142 | _out.SetValue(null, null); 143 | _error.SetValue(null, null); 144 | 145 | var o = Console.Out; 146 | o = Console.Error; 147 | //_InitializeStdOutError.Invoke(null, new object[] { true }); 148 | } 149 | 150 | private static void SetOutAndErrorNull() 151 | { 152 | Console.SetOut(TextWriter.Null); 153 | Console.SetError(TextWriter.Null); 154 | } 155 | } -------------------------------------------------------------------------------- /unlockfps_gui/Utils/ProcessUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.InteropServices; 5 | using System.Text; 6 | 7 | namespace UnlockFps.Gui.Utils 8 | { 9 | internal class ProcessUtils 10 | { 11 | public static string GetProcessPathFromPid(uint pid, out IntPtr processHandle) 12 | { 13 | var hProcess = Native.OpenProcess( 14 | ProcessAccess.QUERY_LIMITED_INFORMATION | 15 | ProcessAccess.TERMINATE | 16 | StandardAccess.SYNCHRONIZE, false, pid); 17 | 18 | processHandle = hProcess; 19 | 20 | if (hProcess == IntPtr.Zero) 21 | return string.Empty; 22 | 23 | StringBuilder sb = new StringBuilder(1024); 24 | uint bufferSize = (uint)sb.Capacity; 25 | if (!Native.QueryFullProcessImageName(hProcess, 0, sb, ref bufferSize)) 26 | return string.Empty; 27 | 28 | return sb.ToString(); 29 | } 30 | 31 | public static bool InjectDlls(IntPtr processHandle, IReadOnlyList dllPaths) 32 | { 33 | if (dllPaths.Count == 0) 34 | return true; 35 | 36 | Native.RtlAdjustPrivilege(20, true, false, out var _); 37 | 38 | var kernel32 = Native.LoadLibrary("kernel32.dll"); 39 | var loadLibrary = Native.GetProcAddress(kernel32, "LoadLibraryW"); 40 | 41 | var remoteVa = Native.VirtualAllocEx(processHandle, IntPtr.Zero, 0x1000, 42 | AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.READWRITE); 43 | if (remoteVa == IntPtr.Zero) 44 | return false; 45 | 46 | foreach (var dllPath in dllPaths) 47 | { 48 | var nativeString = Marshal.StringToHGlobalUni(dllPath); 49 | var bytes = Encoding.Unicode.GetBytes(dllPath); 50 | Marshal.FreeHGlobal(nativeString); 51 | 52 | if (!Native.WriteProcessMemory(processHandle, remoteVa, bytes, bytes.Length, out var bytesWritten)) 53 | return false; 54 | 55 | var thread = Native.CreateRemoteThread(processHandle, IntPtr.Zero, 0, loadLibrary, remoteVa, 0, out var threadId); 56 | if (thread == IntPtr.Zero) 57 | return false; 58 | 59 | Native.WaitForSingleObject(thread, uint.MaxValue); 60 | Native.CloseHandle(thread); 61 | Native.WriteProcessMemory(processHandle, remoteVa, new byte[bytes.Length], bytes.Length, out _); 62 | } 63 | 64 | Native.VirtualFreeEx(processHandle, remoteVa, 0, FreeType.RELEASE); 65 | 66 | return true; 67 | } 68 | 69 | public static unsafe IntPtr PatternScan(IntPtr module, string signature) 70 | { 71 | var tokens = signature.Split(' '); 72 | var patternBytes = tokens 73 | .ToList() 74 | .Select(x => x == "?" ? (byte)0xFF : Convert.ToByte(x, 16)) 75 | .ToArray(); 76 | 77 | var dosHeader = Marshal.PtrToStructure(module); 78 | var ntHeader = Marshal.PtrToStructure((nint)(module.ToInt64() + dosHeader.e_lfanew)); 79 | 80 | var sizeOfImage = ntHeader.OptionalHeader.SizeOfImage; 81 | var scanBytes = (byte*)module; 82 | 83 | var s = patternBytes.Length; 84 | var d = patternBytes; 85 | 86 | for (var i = 0U; i < sizeOfImage - s; i++) 87 | { 88 | var found = true; 89 | for (var j = 0; j < s; j++) 90 | { 91 | if (d[j] != scanBytes[i + j] && d[j] != 0xFF) 92 | { 93 | found = false; 94 | break; 95 | } 96 | } 97 | 98 | if (found) 99 | return (nint)(module.ToInt64() + i); 100 | } 101 | 102 | return IntPtr.Zero; 103 | } 104 | 105 | public static IntPtr GetModuleBase(IntPtr hProcess, string moduleName) 106 | { 107 | var modules = new IntPtr[1024]; 108 | 109 | if (!Native.EnumProcessModules(hProcess, modules, (uint)(modules.Length * IntPtr.Size), out var bytesNeeded)) 110 | { 111 | if (Marshal.GetLastWin32Error() != 299) 112 | return IntPtr.Zero; 113 | } 114 | 115 | foreach (var module in modules.Where(x => x != IntPtr.Zero)) 116 | { 117 | StringBuilder sb = new StringBuilder(1024); 118 | if (Native.GetModuleBaseName(hProcess, module, sb, (uint)sb.Capacity) == 0) 119 | continue; 120 | 121 | if (sb.ToString() != moduleName) 122 | continue; 123 | 124 | if (!Native.GetModuleInformation(hProcess, module, out var moduleInfo, (uint)Marshal.SizeOf())) 125 | continue; 126 | 127 | return moduleInfo.lpBaseOfDll; 128 | } 129 | 130 | return IntPtr.Zero; 131 | } 132 | 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /unlockfps_gui/Utils/ReflectionUtil.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using Semver; 3 | 4 | namespace UnlockFps.Gui.Utils; 5 | 6 | internal static class ReflectionUtil 7 | { 8 | private static string? _version; 9 | private static string? _company; 10 | 11 | public static string GetInformationalVersion() 12 | { 13 | if (_version != null) return _version; 14 | 15 | //var runner = AssemblyAttributeUtil.GetAssemblyAttribute(out var core); 16 | var runner = typeof(ReflectionUtil).Assembly.GetCustomAttribute(); 17 | var core = default(AssemblyInformationalVersionAttribute); 18 | var runnerVersion = runner!.InformationalVersion; 19 | FixCommit(ref runnerVersion); 20 | 21 | if (core == null) 22 | { 23 | return _version ??= runnerVersion; 24 | } 25 | 26 | var coreVersion = core.InformationalVersion; 27 | FixCommit(ref coreVersion); 28 | if (coreVersion == runnerVersion) 29 | { 30 | return _version ??= runnerVersion; 31 | } 32 | 33 | return _version ??= $"{runnerVersion} (core: {coreVersion})"; 34 | } 35 | 36 | public static string GetCompany() 37 | { 38 | if (_company != null) return _company; 39 | 40 | var runner = AssemblyAttributeUtil.GetAssemblyAttribute(out var core); 41 | var runnerCompany = runner!.Company; 42 | if (core == null) 43 | { 44 | return _company ??= runnerCompany; 45 | } 46 | 47 | var coreCompany = core.Company; 48 | if (coreCompany == runnerCompany) 49 | { 50 | return _company ??= runnerCompany; 51 | } 52 | 53 | return _company ??= $"{runnerCompany} (core: {coreCompany})"; 54 | } 55 | 56 | private static void FixCommit(ref string version) 57 | { 58 | if (!SemVersion.TryParse(version, SemVersionStyles.Strict, out var semVer)) return; 59 | 60 | if (!semVer.IsPrerelease) 61 | { 62 | var lastIndexOf = version.LastIndexOf('+'); 63 | 64 | var lastIndexOfDot = version.LastIndexOf('.'); 65 | var subStr = version.Substring(lastIndexOfDot + 1); 66 | if (subStr.Length == 40) 67 | { 68 | version = version.Substring(0, lastIndexOfDot); 69 | } 70 | else 71 | { 72 | version = version.Substring(0, lastIndexOf); 73 | } 74 | } 75 | else if (semVer.Metadata.Length > 7) 76 | { 77 | var lastIndexOf = version.LastIndexOf('+'); 78 | version = version.Substring(0, lastIndexOf + 8); 79 | } 80 | } 81 | } -------------------------------------------------------------------------------- /unlockfps_gui/Utils/WineHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.CodeAnalysis; 3 | using System.Runtime.InteropServices; 4 | 5 | namespace UnlockFps.Gui.Utils; 6 | 7 | internal static partial class WineHelper 8 | { 9 | public static bool DetectWine( 10 | [NotNullWhen(true)] out string? version, 11 | [NotNullWhen(true)] out string? buildId) 12 | { 13 | try 14 | { 15 | version = GetVersion(); 16 | Console.WriteLine("Wine version: " + version); 17 | buildId = GetBuildId(); 18 | Console.WriteLine("Wine build id: " + buildId); 19 | return true; 20 | } 21 | catch (EntryPointNotFoundException) 22 | { 23 | version = null; 24 | buildId = null; 25 | return false; 26 | } 27 | catch (DllNotFoundException) 28 | { 29 | version = null; 30 | buildId = null; 31 | return false; 32 | } 33 | } 34 | 35 | [LibraryImport("ntdll", EntryPoint = "wine_get_version", StringMarshalling = StringMarshalling.Utf8)] 36 | private static partial string GetVersion(); 37 | 38 | [LibraryImport("ntdll", EntryPoint = "wine_get_build_id", StringMarshalling = StringMarshalling.Utf8)] 39 | private static partial string GetBuildId(); 40 | } -------------------------------------------------------------------------------- /unlockfps_gui/ViewModels/ViewModelBase.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Runtime.CompilerServices; 3 | using ReactiveUI; 4 | 5 | namespace UnlockFps.Gui.ViewModels; 6 | 7 | public class ViewModelBase : ReactiveObject 8 | { 9 | protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) 10 | { 11 | this.RaisePropertyChanged(propertyName); 12 | } 13 | 14 | protected bool SetField(ref T field, T value, [CallerMemberName] string? propertyName = null) 15 | { 16 | if (EqualityComparer.Default.Equals(field, value)) return false; 17 | field = value; 18 | OnPropertyChanged(propertyName); 19 | return true; 20 | } 21 | } -------------------------------------------------------------------------------- /unlockfps_gui/Views/AboutWindow.axaml: -------------------------------------------------------------------------------- 1 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 25 | 26 | 27 | 28 | 29 | 33 | 34 | 35 | 36 | 37 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /unlockfps_gui/Views/AboutWindow.axaml.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using Avalonia.Controls; 3 | using Avalonia.Input; 4 | using UnlockFps.Gui.Utils; 5 | 6 | namespace UnlockFps.Gui.Views; 7 | 8 | public partial class AboutWindow : Window 9 | { 10 | public AboutWindow() 11 | { 12 | this.SetSystemChrome(); 13 | InitializeComponent(); 14 | Run_Version.Text = "v" + ReflectionUtil.GetInformationalVersion(); 15 | } 16 | 17 | private void HyperLink_OnTapped(object? sender, TappedEventArgs e) 18 | { 19 | if (sender is TextBlock { Text: { } text }) 20 | { 21 | Process.Start(new ProcessStartInfo(text) { UseShellExecute = true }); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /unlockfps_gui/Views/AlertWindow.axaml: -------------------------------------------------------------------------------- 1 | 15 | 19 | 20 | 21 | 26 | 32 | 33 | 38 | 39 | 40 | 44 | 45 |