diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..426d76d --- /dev/null +++ b/.gitignore @@ -0,0 +1,398 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files +*.ncb +*.aps + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +*.sln.iml diff --git a/BG3Cam.sln b/BG3Cam.sln new file mode 100644 index 0000000..aa13ebf --- /dev/null +++ b/BG3Cam.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.33808.371 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BG3Cam", "BG3Cam\BG3Cam.csproj", "{C7A82634-9636-4C7C-BBC0-A96AAAD3F9EA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C7A82634-9636-4C7C-BBC0-A96AAAD3F9EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C7A82634-9636-4C7C-BBC0-A96AAAD3F9EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C7A82634-9636-4C7C-BBC0-A96AAAD3F9EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C7A82634-9636-4C7C-BBC0-A96AAAD3F9EA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {79251129-647B-487E-A7EC-6C85CD97FA38} + EndGlobalSection +EndGlobal diff --git a/BG3Cam/BG3Cam.csproj b/BG3Cam/BG3Cam.csproj new file mode 100644 index 0000000..22c7ceb --- /dev/null +++ b/BG3Cam/BG3Cam.csproj @@ -0,0 +1,13 @@ + + + + WinExe + net7.0-windows + enable + true + enable + True + x64 + + + \ No newline at end of file diff --git a/BG3Cam/Hotkeys.cs b/BG3Cam/Hotkeys.cs new file mode 100644 index 0000000..e5e1626 --- /dev/null +++ b/BG3Cam/Hotkeys.cs @@ -0,0 +1,35 @@ +using System.Runtime.InteropServices; + +namespace BG3Cam +{ + public class Hotkeys + { + [DllImport("user32")] static extern short GetKeyState(int keyCode); + [DllImport("user32")] static extern short GetAsyncKeyState(int keyCode); + static Dictionary ToggleVals = new Dictionary(); + static Dictionary SingleVals = new Dictionary(); + public static Boolean ToggledKey(Keys keyCode) + { + if (!ToggleVals.ContainsKey(keyCode)) ToggleVals.Add(keyCode, false); + if (SinglePress(keyCode)) + ToggleVals[keyCode] = !ToggleVals[keyCode]; + return ToggleVals[keyCode]; + } + public static Boolean SinglePress(Keys keyCode) + { + if (!SingleVals.ContainsKey(keyCode)) SingleVals.Add(keyCode, false); + if (IsPressed(keyCode)) + { + if (SingleVals[keyCode]) return false; + SingleVals[keyCode] = true; + return true; + } + else SingleVals[keyCode] = false; + return false; + } + public static Boolean IsPressed(Keys keyCode) + { + return (GetAsyncKeyState((int)keyCode) & 0x8000) != 0; + } + } +} diff --git a/BG3Cam/MainWindow.Designer.cs b/BG3Cam/MainWindow.Designer.cs new file mode 100644 index 0000000..7e464b1 --- /dev/null +++ b/BG3Cam/MainWindow.Designer.cs @@ -0,0 +1,120 @@ +namespace BG3Cam +{ + partial class MainWindow + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + maxZoomVal = new NumericUpDown(); + minZoomVal = new NumericUpDown(); + labelMax = new Label(); + labelMin = new Label(); + mouseTiltTask = new System.ComponentModel.BackgroundWorker(); + note = new Label(); + ((System.ComponentModel.ISupportInitialize)maxZoomVal).BeginInit(); + ((System.ComponentModel.ISupportInitialize)minZoomVal).BeginInit(); + SuspendLayout(); + // + // maxZoomVal + // + maxZoomVal.DecimalPlaces = 1; + maxZoomVal.Location = new Point(170, 12); + maxZoomVal.Maximum = new decimal(new int[] { 10000, 0, 0, 0 }); + maxZoomVal.Minimum = new decimal(new int[] { 1, 0, 0, 65536 }); + maxZoomVal.Name = "maxZoomVal"; + maxZoomVal.Size = new Size(120, 23); + maxZoomVal.TabIndex = 1; + maxZoomVal.Value = new decimal(new int[] { 12, 0, 0, 0 }); + maxZoomVal.ValueChanged += maxZoomVal_ValueChanged; + // + // minZoomVal + // + minZoomVal.DecimalPlaces = 1; + minZoomVal.Location = new Point(170, 41); + minZoomVal.Minimum = new decimal(new int[] { 1, 0, 0, 65536 }); + minZoomVal.Name = "minZoomVal"; + minZoomVal.Size = new Size(120, 23); + minZoomVal.TabIndex = 2; + minZoomVal.Value = new decimal(new int[] { 35, 0, 0, 65536 }); + minZoomVal.ValueChanged += minZoomVal_ValueChanged; + // + // labelMax + // + labelMax.AutoSize = true; + labelMax.Location = new Point(102, 14); + labelMax.Name = "labelMax"; + labelMax.Size = new Size(65, 15); + labelMax.TabIndex = 3; + labelMax.Text = "Max Zoom"; + // + // labelMin + // + labelMin.AutoSize = true; + labelMin.Location = new Point(104, 43); + labelMin.Name = "labelMin"; + labelMin.Size = new Size(63, 15); + labelMin.TabIndex = 4; + labelMin.Text = "Min Zoom"; + // + // note + // + note.AutoSize = true; + note.Location = new Point(25, 69); + note.Name = "note"; + note.Size = new Size(259, 30); + note.TabIndex = 5; + note.Text = "Note - when moving the camera up and down,\r\nthe camera pans slowly due to the game engine"; + // + // MainWindow + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(308, 111); + Controls.Add(note); + Controls.Add(labelMin); + Controls.Add(labelMax); + Controls.Add(minZoomVal); + Controls.Add(maxZoomVal); + Name = "MainWindow"; + Text = "Baldur's Gate 3 Camera Mod"; + FormClosing += MainWindow_FormClosing; + Load += MainWindow_Load; + ((System.ComponentModel.ISupportInitialize)maxZoomVal).EndInit(); + ((System.ComponentModel.ISupportInitialize)minZoomVal).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private NumericUpDown maxZoomVal; + private NumericUpDown minZoomVal; + private Label labelMax; + private Label labelMin; + private System.ComponentModel.BackgroundWorker mouseTiltTask; + private Label note; + } +} \ No newline at end of file diff --git a/BG3Cam/MainWindow.cs b/BG3Cam/MainWindow.cs new file mode 100644 index 0000000..42bac7b --- /dev/null +++ b/BG3Cam/MainWindow.cs @@ -0,0 +1,106 @@ +using System.Diagnostics; + +namespace BG3Cam +{ + public partial class MainWindow : Form + { + Memory mem; + public MainWindow() + { + InitializeComponent(); + } + nint obj; + int camOffset; + int camMaxOffset; + int camMaxAbsOffset; + int camTiltOffset; + float defaultZoomMax = 12f; + float defaultZoomMin = 3.5f; + float defaultMaxAbs = 1f; + float defaultPitchMaxZoom = 1f; + float defaultPitchMinZoom = 1f; + int prevMouseY; + float curTilt; + void MainWindow_Load(object sender, EventArgs e) + { + var bg3Proc = Process.GetProcesses().FirstOrDefault(p => p.ProcessName == "bg3" || p.ProcessName == "bg3_dx11"); + if (bg3Proc == null) + { + MessageBox.Show("Baldur's Gate 3 not found", "BG3 not found"); + Environment.Exit(0); + } + mem = new Memory(bg3Proc); + var camFuncAddr = mem.FindPattern("4C 8B 05 ? ? ? ? 41 80 B8"); + var objBase = mem.ReadProcessMemory(camFuncAddr + 3); + obj = mem.ReadProcessMemory(camFuncAddr + 3 + 4 + objBase); + + var camFuncBytes = mem.ReadProcessMemory(camFuncAddr, 0x100); + + var camOffsetStart = mem.FindPattern("F6 C1 01 75 07 49 8D 80", bytes: camFuncBytes); + camOffset = BitConverter.ToInt32(camFuncBytes, (int)camOffsetStart + 8); + + var maxDistOffAddr = mem.FindPattern("C3 F3 0F 10 48", bytes: camFuncBytes); + camMaxOffset = camFuncBytes[maxDistOffAddr + 5]; + + var camMaxAbs = mem.FindPattern("F3 0F 10 80 ? ? ? ? C3", bytes: camFuncBytes); + camMaxAbsOffset = camFuncBytes[camMaxAbs + 4]; + + var camTilt = mem.FindPattern("C3 F3 0F 10 80 ? ? ? ? F3 0F 10 88 ? ? ? ? 0F 14 C8 66 48 0F 7E C8 C3"); + camTiltOffset = mem.ReadProcessMemory(camTilt + 5); + + var curMaxZoom = mem.ReadProcessMemory(obj + camOffset + camMaxOffset); // default val is 12, addr offset is 0x7b4 in release patch + if (curMaxZoom != defaultZoomMax) + { + MessageBox.Show("Max Zoom not found", "Bad Game State"); + Environment.Exit(0); + } + maxZoomVal.Value = (decimal)25f; + minZoomVal.Value = (decimal)0.5f; + defaultZoomMin = mem.ReadProcessMemory(obj + camOffset + camMaxOffset + 4); + defaultMaxAbs = mem.ReadProcessMemory(obj + camOffset + camMaxAbsOffset); + curTilt = defaultPitchMaxZoom = mem.ReadProcessMemory(obj + camOffset + camTiltOffset); + defaultPitchMinZoom = mem.ReadProcessMemory(obj + camOffset + camTiltOffset + 4); + mem.WriteProcessMemory(obj + camOffset + camMaxAbsOffset, 10000.0f); + mem.WriteProcessMemory(obj + camOffset + camTiltOffset, curTilt); + mem.WriteProcessMemory(obj + camOffset + camTiltOffset + 4, curTilt); + ChangePitchOnMouseMove(); + } + bool running = true; + void MainWindow_FormClosing(object sender, FormClosingEventArgs e) + { + running = false; + mem.WriteProcessMemory(obj + camOffset + camMaxOffset, defaultZoomMax); + mem.WriteProcessMemory(obj + camOffset + camMaxOffset + 4, defaultZoomMin); + mem.WriteProcessMemory(obj + camOffset + camMaxAbsOffset, defaultMaxAbs); + mem.WriteProcessMemory(obj + camOffset + camTiltOffset, defaultPitchMaxZoom); + mem.WriteProcessMemory(obj + camOffset + camTiltOffset + 4, defaultPitchMinZoom); + } + void maxZoomVal_ValueChanged(object sender, EventArgs e) + { + mem.WriteProcessMemory(obj + camOffset + camMaxOffset, (float)maxZoomVal.Value); + } + void minZoomVal_ValueChanged(object sender, EventArgs e) + { + mem.WriteProcessMemory(obj + camOffset + camMaxOffset + 4, (float)minZoomVal.Value); + } + async Task ChangePitchOnMouseMove() + { + while (running) + { + if (Hotkeys.SinglePress(Keys.MButton)) prevMouseY = Cursor.Position.Y; + if (Hotkeys.IsPressed(Keys.MButton)) + { + var diff = Cursor.Position.Y - prevMouseY; + if (diff != 0) + { + curTilt += diff * 0.05f; + mem.WriteProcessMemory(obj + camOffset + camTiltOffset, curTilt); + mem.WriteProcessMemory(obj + camOffset + camTiltOffset + 4, curTilt); + prevMouseY = Cursor.Position.Y; + } + } + await Task.Delay(16); + } + } + } +} \ No newline at end of file diff --git a/BG3Cam/MainWindow.resx b/BG3Cam/MainWindow.resx new file mode 100644 index 0000000..244271d --- /dev/null +++ b/BG3Cam/MainWindow.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/BG3Cam/Program.cs b/BG3Cam/Program.cs new file mode 100644 index 0000000..e5c1387 --- /dev/null +++ b/BG3Cam/Program.cs @@ -0,0 +1,11 @@ +namespace BG3Cam +{ + static class Program + { + [STAThread] static void Main() + { + ApplicationConfiguration.Initialize(); + Application.Run(new MainWindow()); + } + } +} \ No newline at end of file diff --git a/BG3Cam/SigScan.cs b/BG3Cam/SigScan.cs new file mode 100644 index 0000000..3c13e55 --- /dev/null +++ b/BG3Cam/SigScan.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace BG3Cam +{ + public unsafe class Memory + { + public static readonly nint kernel = NativeLibrary.Load("kernel32.dll"); + public static readonly delegate* unmanaged[Stdcall] OpenProcess = (delegate* unmanaged[Stdcall])NativeLibrary.GetExport(kernel, nameof(OpenProcess)); + public static readonly delegate* unmanaged[Stdcall] ReadProcessMemory2 = (delegate* unmanaged[Stdcall])NativeLibrary.GetExport(kernel, nameof(ReadProcessMemory)); + [DllImport("kernel32")] static extern Boolean WriteProcessMemory(nint hProcess, nint lpBaseAddress, Byte[] buffer, Int32 nSize, out Int32 lpNumberOfBytesWritten); + [DllImport("kernel32")] private static extern bool ReadProcessMemory(nint hProcess, nint lpBaseAddress, [Out()] byte[] lpBuffer, int dwSize, out int lpNumberOfBytesRead); + byte[] cache; + Process proc; + public Memory(Process proc) + { + this.proc = proc; + var size = proc.MainModule.ModuleMemorySize; + cache = new byte[size]; + ReadProcessMemory(proc.Handle, proc.MainModule.BaseAddress, cache, size, out _); + } + public T ReadProcessMemory(nint addr) + { + var buffer = new byte[Marshal.SizeOf()]; + ReadProcessMemory2(proc.Handle, addr, buffer, Marshal.SizeOf(), out _); + var structPtr = GCHandle.Alloc(buffer, GCHandleType.Pinned); + var obj = Marshal.PtrToStructure(structPtr.AddrOfPinnedObject()); + structPtr.Free(); + return obj; + } + public byte[] ReadProcessMemory(nint addr, int nBytes) + { + var buffer = new byte[nBytes]; + ReadProcessMemory2(proc.Handle, addr, buffer, nBytes, out _); + return buffer; + } + public void WriteProcessMemory(nint addr, T value) + { + var objSize = Marshal.SizeOf(value); + var objBytes = new Byte[objSize]; + var objPtr = Marshal.AllocHGlobal(objSize); + Marshal.StructureToPtr(value, objPtr, true); + Marshal.Copy(objPtr, objBytes, 0, objSize); + Marshal.FreeHGlobal(objPtr); + WriteProcessMemory(proc.Handle, addr, objBytes, objBytes.Length, out _); + } + static bool MaskCheck(int nOffset, byte[] btPattern, string strMask, byte[] bytes) + { + //return btPattern.Select((b,i)=>(b,i)).All(b => strMask[b.b] == '?' || b.b == cache[nOffset + b.i]); + for (var x = 0; x < btPattern.Length; x++) + { + if (strMask[x] == '?') continue; + if ((strMask[x] == 'x') && (btPattern[x] != bytes[nOffset + x])) return false; + } + return true; + } + public static nint FindPattern(byte[] btPattern, string strMask, nint addr, int size, byte[] bytes) + { + for (var x = 0; x < size - strMask.Length; x++) + if (MaskCheck(x, btPattern, strMask, bytes)) return addr + x; + return 0; + } + public static List FindPatterns(byte[] btPattern, string strMask, nint addr, int size, byte[] bytes) + { + var ptrs = new List(); + for (var x = 0; x < size - strMask.Length; x++) + if (MaskCheck(x, btPattern, strMask, bytes)) ptrs.Add(addr + x); + return ptrs; + } + public nint FindPattern(string pattern, nint addr = 0, int size = 0, byte[] bytes = null) + { + if (bytes == null) + { + bytes = cache; + if (addr == 0) addr = proc.MainModule.BaseAddress; + } + if (size == 0) size = bytes.Length; + + var arrayOfBytes = pattern.Split(' ').Select(b => b.Contains("?") ? (byte)0 : (byte)Convert.ToInt32(b, 16)).ToArray(); + var strMask = String.Join("", pattern.Split(' ').Select(b => b.Contains("?") ? '?' : 'x')); + return FindPattern(arrayOfBytes, strMask, addr, size, bytes); + } + public List FindPatterns(string pattern, nint addr = 0, int size = 0, byte[] bytes = null) + { + if (bytes == null) + { + bytes = cache; + if (addr == 0) addr = proc.MainModule.BaseAddress; + } + if (size == 0) size = bytes.Length; + var arrayOfBytes = pattern.Split(' ').Select(b => b.Contains("?") ? (byte)0 : (byte)Convert.ToInt32(b, 16)).ToArray(); + var strMask = String.Join("", pattern.Split(' ').Select(b => b.Contains("?") ? '?' : 'x')); + return FindPatterns(arrayOfBytes, strMask, addr, size, bytes); + } + } +} \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..607e979 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 shalzuth + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6161f7e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# BG3Cam + baldur's gate 3 camera mod to zoom and tilt the camera