Skip to content

Commit

Permalink
feat(Processory.Insight): add memory reading and logging for screen c…
Browse files Browse the repository at this point in the history
…lient

feat(solution): integrate .editorconfig into solution items
feat(MemoryReader): introduce new MemoryReader class for process memory access
refactor(ProcessoryClient): enhance class with MemoryReader and modify property access
feat(Processory.csproj): enable unsafe blocks and add multiple analyzer packages
refactor(ProcessService): modify access modifiers and property usage for consistency
  • Loading branch information
myinusa committed Jun 23, 2024
1 parent fa442a3 commit d6a7b86
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 18 deletions.
13 changes: 13 additions & 0 deletions Processory.Insight/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ public static void Main() {
var GivenName = "fm";
var processory = new ProcessoryClient(GivenName);
processory.ProcessService.LogProcessAndModuleInfo();

var baseAddress = processory.ProcessService.ProcessHandle?.MainModule?.BaseAddress;

if (baseAddress == null) {
Console.WriteLine("Could not find process");
return;
}

var screenClient = (ulong)(baseAddress + 0x4C7FF08);
var screenClientValue = processory.MemoryReader.Read<uint>(screenClient);

Console.WriteLine("Screen client: {0:X}", screenClient);
Console.WriteLine("Screen client value: {0}", screenClientValue);
}

}
Expand Down
13 changes: 6 additions & 7 deletions Processory.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ VisualStudioVersion = 17.10.35013.160
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Processory", "Processory\Processory.csproj", "{30D52856-AB7C-4A8A-911B-018DA366A0FE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject1", "..\TestProject1\TestProject1.csproj", "{EAD83DD0-1158-43BA-AB68-62C4F9427622}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Processory.Tests", "Processory.Tests\Processory.Tests.csproj", "{5B52805D-FF45-4518-B2F0-361289363180}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Processory.Tests", "Processory.Tests\Processory.Tests.csproj", "{5B52805D-FF45-4518-B2F0-361289363180}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Processory.Insight", "Processory.Insight\Processory.Insight.csproj", "{678680F6-1E6D-4F90-860B-605911A94D32}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D4161AE0-3879-42D2-9B18-C46218071DD6}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,10 +24,6 @@ Global
{30D52856-AB7C-4A8A-911B-018DA366A0FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30D52856-AB7C-4A8A-911B-018DA366A0FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30D52856-AB7C-4A8A-911B-018DA366A0FE}.Release|Any CPU.Build.0 = Release|Any CPU
{EAD83DD0-1158-43BA-AB68-62C4F9427622}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EAD83DD0-1158-43BA-AB68-62C4F9427622}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EAD83DD0-1158-43BA-AB68-62C4F9427622}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EAD83DD0-1158-43BA-AB68-62C4F9427622}.Release|Any CPU.Build.0 = Release|Any CPU
{5B52805D-FF45-4518-B2F0-361289363180}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B52805D-FF45-4518-B2F0-361289363180}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B52805D-FF45-4518-B2F0-361289363180}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
44 changes: 44 additions & 0 deletions Processory/MemoryReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Processory.Native;

namespace Processory.Memory;

public class MemoryReader {
protected readonly ProcessoryClient ProcessoryClient;

public MemoryReader(ProcessoryClient processoryClient) {
this.ProcessoryClient = processoryClient;
}

public byte[] Read(UIntPtr address, int size) {
var buffer = new byte[size];
var bytesRead = MethodsNative.ReadProcessMemory(ProcessoryClient.ProcessHandle, address, buffer, (UIntPtr)size, out UIntPtr _);

if (!bytesRead) {
return Array.Empty<byte>();
}

return buffer;
}

public T Read<T>(ulong offset)
where T : unmanaged {
T value = default;
ReadRef(offset, ref value);
return value;
}

private unsafe bool ReadProcessMemory(UIntPtr location, void* buffer, nuint numBytes) {
return MethodsNative.ReadProcessMemory(ProcessoryClient.ProcessHandle, location, (UIntPtr)buffer, numBytes, out _);
}

public unsafe void ReadRef<T>(ulong offset, ref T value)
where T : unmanaged {
void* buffer = Unsafe.AsPointer(ref value);
if (!ReadProcessMemory((UIntPtr)offset, buffer, (nuint)sizeof(T))) {
// ThrowHelpers.ThrowReadExternalMemoryExceptionWindows(offset, sizeof(T));
}
}

}
17 changes: 11 additions & 6 deletions Processory/Processory.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using Processory.Memory;
using Processory.Services;

namespace Processory;

public class ProcessoryClient { // Renamed from Processory to ProcessoryClient
//private readonly nint _processHandle;
public readonly string _processName;
public ProcessService ProcessService;
public class ProcessoryClient {
public MemoryReader MemoryReader { get; set; }
public ProcessService ProcessService { get; set; }
public string ProcessName { get; }
public nint ProcessHandle { get; }

public ProcessoryClient(string processName) {
_processName = processName;
ProcessName = processName;
ProcessService = new ProcessService(this);
// _processHandle = GetProcessHandle();
MemoryReader = new MemoryReader(this);
ProcessHandle = ProcessService.GetProcessHandle();
}


}
22 changes: 19 additions & 3 deletions Processory/Processory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<!-- <ItemGroup>
<Folder Include="Processory\" />
</ItemGroup> -->
<ItemGroup>
<PackageReference Include="Roslynator.Analyzers" Version="4.12.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Roslynator.Formatting.Analyzers" Version="4.12.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.27.0.93347">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions Processory/Services/ProcessService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public class ProcessService {

public ProcessService(ProcessoryClient processoryClient) {
ProcessoryClient = processoryClient;
processName = processoryClient._processName;
processName = processoryClient.ProcessName;
GetProcessHandle();
}

private nint GetProcessHandle() {
public nint GetProcessHandle() {
OpenProcessHandler();
if (ProcessHandle != null) {
return NativeMethods.OpenProcess(Flags.ProcessAccess.All, false, ProcessHandle.Id);
Expand Down

0 comments on commit d6a7b86

Please sign in to comment.