Skip to content

Commit

Permalink
Merge pull request #1 from spitfirex86/master
Browse files Browse the repository at this point in the history
2.2.0
  • Loading branch information
rtsonneveld authored Aug 5, 2019
2 parents 2fc389d + f599e09 commit cf8f4ef
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 86 deletions.
7 changes: 5 additions & 2 deletions Rayman2LevelSwitcher.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.23107.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.29123.88
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rayman2LevelSwitcher", "Rayman2LevelSwitcher\Rayman2LevelSwitcher.csproj", "{BCBA08D4-60A2-457E-AD48-EBC82C0973D3}"
EndProject
Expand All @@ -19,4 +19,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {20EEF419-052A-42FD-BA98-9CBA248DEE11}
EndGlobalSection
EndGlobal
112 changes: 77 additions & 35 deletions Rayman2LevelSwitcher/Game/Rayman2Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,84 @@ public class Rayman2Manager
{
#region Constant Values

private const int Off_engineStructure = 0x500380;
private const int Off_engineMode = Off_engineStructure + 0x0;
private const int Off_levelName = Off_engineStructure + 0x1F;
private const int Off_healthpointer_1 = 0x500584;
private const int Off_voidpointer = 0x500FAA;
private const int OffEngineStructure = 0x500380;
private const int OffEngineMode = OffEngineStructure + 0x0;
private const int OffLevelName = OffEngineStructure + 0x1F;
private const int OffHealthPointer = 0x500584;
private const int OffVoidPointer = 0x500FAA;

#endregion

public (float, float, float) PlayerCoordinates
{
get => ReadCoordinates(0x500560, 0x224, 0x310, 0x34, 0x0, 0x1ac);
set => WriteCoordinates(value.Item1, value.Item2, value.Item3, 0x500560, 0x224, 0x310, 0x34, 0x0, 0x1ac);
}

public (float, float, float) GlmCoordinates
{
get => ReadCoordinates(0x500298, 0x234, 0x10, 0xC, 0xB0);
set => WriteCoordinates(value.Item1, value.Item2, value.Item3, 0x500298, 0x234, 0x10, 0xC, 0xB0);
}

/// <summary>
/// Loads the specified position
/// Write coordinates to the game memory
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
public void LoadPosition(float x, float y, float z)
/// <param name="baseAddress"></param>
/// <param name="offsets"></param>
private void WriteCoordinates(float x, float y, float z, int baseAddress, params int[] offsets)
{
int processHandle = GetRayman2ProcessHandle();

if (processHandle < 0)
return;

int bytesReadOrWritten = 0;
int off_xcoord = Memory.GetPointerPath(processHandle, 0x500560, 0x224, 0x310, 0x34, 0x0) + 0x1ac;
int off_ycoord = off_xcoord + 4;
int off_zcoord = off_xcoord + 8;
int offXcoord = Memory.GetPointerPath(processHandle, baseAddress, offsets);
int offYcoord = offXcoord + 4;
int offZcoord = offXcoord + 8;

byte[] xCoordBuffer = BitConverter.GetBytes(x);
byte[] yCoordBuffer = BitConverter.GetBytes(y);
byte[] zCoordBuffer = BitConverter.GetBytes(z);

Memory.WriteProcessMemory(processHandle, off_xcoord, xCoordBuffer, 4, ref bytesReadOrWritten);
Memory.WriteProcessMemory(processHandle, off_ycoord, yCoordBuffer, 4, ref bytesReadOrWritten);
Memory.WriteProcessMemory(processHandle, off_zcoord, zCoordBuffer, 4, ref bytesReadOrWritten);
Memory.WriteProcessMemory(processHandle, offXcoord, xCoordBuffer, 4, ref bytesReadOrWritten);
Memory.WriteProcessMemory(processHandle, offYcoord, yCoordBuffer, 4, ref bytesReadOrWritten);
Memory.WriteProcessMemory(processHandle, offZcoord, zCoordBuffer, 4, ref bytesReadOrWritten);
}

/// <summary>
/// Reads coordinates from game memory
/// </summary>
/// <param name="baseAddress"></param>
/// <param name="offsets"></param>
/// <returns></returns>
private (float, float, float) ReadCoordinates(int baseAddress, params int[] offsets)
{
int processHandle = GetRayman2ProcessHandle();
if (processHandle < 0)
return (0, 0, 0);

int bytesReadOrWritten = 0;
int offXcoord = Memory.GetPointerPath(processHandle, baseAddress, offsets);
int offYcoord = offXcoord + 4;
int offZcoord = offXcoord + 8;

byte[] xCoordBuffer = new byte[4];
byte[] yCoordBuffer = new byte[4];
byte[] zCoordBuffer = new byte[4];

Memory.ReadProcessMemory(processHandle, offXcoord, xCoordBuffer, 4, ref bytesReadOrWritten);
Memory.ReadProcessMemory(processHandle, offYcoord, yCoordBuffer, 4, ref bytesReadOrWritten);
Memory.ReadProcessMemory(processHandle, offZcoord, zCoordBuffer, 4, ref bytesReadOrWritten);

return (
BitConverter.ToSingle(xCoordBuffer, 0),
BitConverter.ToSingle(yCoordBuffer, 0),
BitConverter.ToSingle(zCoordBuffer, 0)
);
}

/// <summary>
Expand Down Expand Up @@ -99,7 +144,7 @@ public string GetCurrentLevelName(int processHandle)

byte[] buffer = new byte[16];

Memory.ReadProcessMemory(processHandle, Off_levelName, buffer, buffer.Length, ref bytesReadOrWritten);
Memory.ReadProcessMemory(processHandle, OffLevelName, buffer, buffer.Length, ref bytesReadOrWritten);

string levelName = Encoding.ASCII.GetString(buffer);

Expand All @@ -116,14 +161,14 @@ public bool IsRayman2Focused()
{
const int nChars = 256;

StringBuilder Buff = new StringBuilder(nChars);
StringBuilder buff = new StringBuilder(nChars);

IntPtr handle = Memory.GetForegroundWindow();

if (Memory.GetWindowText(handle, Buff, nChars) <= 0)
if (Memory.GetWindowText(handle, buff, nChars) <= 0)
return false;

return Buff.ToString() == "Rayman II";
return buff.ToString() == "Rayman II";
}

/// <summary>
Expand All @@ -141,7 +186,7 @@ public bool IsRayman2Paused()

byte[] pausePointerBuffer = new byte[4];

Memory.ReadProcessMemory(processHandle, Off_voidpointer, pausePointerBuffer, pausePointerBuffer.Length, ref bytesReadOrWritten);
Memory.ReadProcessMemory(processHandle, OffVoidPointer, pausePointerBuffer, pausePointerBuffer.Length, ref bytesReadOrWritten);

return BitConverter.ToInt32(pausePointerBuffer, 0) == 1;
}
Expand All @@ -161,19 +206,19 @@ public void ActivateNoHealth()
byte[] buffer = { 0 };
byte[] healthPointerBuffer = new byte[4];

Memory.ReadProcessMemory(processHandle, Off_healthpointer_1, healthPointerBuffer, healthPointerBuffer.Length, ref bytesReadOrWritten);
Memory.ReadProcessMemory(processHandle, OffHealthPointer, healthPointerBuffer, healthPointerBuffer.Length, ref bytesReadOrWritten);

int off_healthPointer = BitConverter.ToInt32(healthPointerBuffer, 0) + 0x245;
int offHealthPointer = BitConverter.ToInt32(healthPointerBuffer, 0) + 0x245;

Memory.WriteProcessMemory(processHandle, off_healthPointer, buffer, buffer.Length, ref bytesReadOrWritten);
Memory.WriteProcessMemory(processHandle, offHealthPointer, buffer, buffer.Length, ref bytesReadOrWritten);
}

/// <summary>
/// Activate void in Rayman 2
/// </summary>
public void ActivateVoid()
{
Process process = new Rayman2Manager().GetRayman2Process();
Process process = GetRayman2Process();

if (process == null)
{
Expand All @@ -187,27 +232,25 @@ public void ActivateVoid()

byte[] buffer = { 0 };

Memory.WriteProcessMemory((int)processHandle, Rayman2Manager.Off_voidpointer, buffer, buffer.Length, ref bytesReadOrWritten);
Memory.WriteProcessMemory((int)processHandle, OffVoidPointer, buffer, buffer.Length, ref bytesReadOrWritten);
}

/// <summary>
/// Reload the current level in Rayman 2
/// </summary>
public void ReloadLevel()
{
var manager = new Rayman2Manager();

int processHandle = manager.GetRayman2ProcessHandle();
int processHandle = GetRayman2ProcessHandle();

int bytesReadOrWritten = 0;

byte[] currentBufferLevelName = new byte[1];
Memory.ReadProcessMemory(processHandle, Off_levelName, currentBufferLevelName, currentBufferLevelName.Length, ref bytesReadOrWritten);
Memory.ReadProcessMemory(processHandle, OffLevelName, currentBufferLevelName, currentBufferLevelName.Length, ref bytesReadOrWritten);

if (currentBufferLevelName[0] == 0)
{
byte[] bufferLevelNameMenu = Encoding.ASCII.GetBytes("menu" + Char.MinValue); // null-terminated
Memory.WriteProcessMemory(processHandle, Off_levelName, bufferLevelNameMenu, bufferLevelNameMenu.Length, ref bytesReadOrWritten);
Memory.WriteProcessMemory(processHandle, OffLevelName, bufferLevelNameMenu, bufferLevelNameMenu.Length, ref bytesReadOrWritten);
return;
}

Expand All @@ -216,7 +259,7 @@ public void ReloadLevel()
6
};

Memory.WriteProcessMemory(processHandle, Off_engineMode, buffer, buffer.Length, ref bytesReadOrWritten);
Memory.WriteProcessMemory(processHandle, OffEngineMode, buffer, buffer.Length, ref bytesReadOrWritten);
}

/// <summary>
Expand All @@ -225,21 +268,20 @@ public void ReloadLevel()
/// <param name="levelName"></param>
public void ChangeLevel(string levelName)
{
var manager = new Rayman2Manager();

int processHandle = manager.GetRayman2ProcessHandle();
int processHandle = GetRayman2ProcessHandle();

int bytesReadOrWritten = 0;

var buffer = Encoding.ASCII.GetBytes(levelName + Char.MinValue);
Memory.WriteProcessMemory(processHandle, Off_levelName, buffer, buffer.Length, ref bytesReadOrWritten);
Memory.WriteProcessMemory(processHandle, OffLevelName, buffer, buffer.Length, ref bytesReadOrWritten);

buffer = new byte[]
{
6
};

Memory.WriteProcessMemory(processHandle, Off_engineMode, buffer, buffer.Length, ref bytesReadOrWritten);
Memory.WriteProcessMemory(processHandle, OffEngineMode, buffer, buffer.Length, ref bytesReadOrWritten);
}

}
}
12 changes: 10 additions & 2 deletions Rayman2LevelSwitcher/Helpers/Memory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

Expand Down Expand Up @@ -36,8 +37,15 @@ public static int GetPointerPath(int processHandle, int baseAddress, params int[

foreach (int offset in offsets)
{
ReadProcessMemory(processHandle, currentAddress + offset, buffer, buffer.Length, ref bytesReadOrWritten);
currentAddress = BitConverter.ToInt32(buffer, 0);
if (offset == offsets.Last())
{
currentAddress += offset;
}
else
{
ReadProcessMemory(processHandle, currentAddress + offset, buffer, buffer.Length, ref bytesReadOrWritten);
currentAddress = BitConverter.ToInt32(buffer, 0);
}
}

return currentAddress;
Expand Down
4 changes: 2 additions & 2 deletions Rayman2LevelSwitcher/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.1.0")]
[assembly: AssemblyFileVersion("2.1.1.0")]
[assembly: AssemblyVersion("2.2.0.0")]
[assembly: AssemblyFileVersion("2.2.0.0")]
38 changes: 38 additions & 0 deletions Rayman2LevelSwitcher/Rayman2LevelSwitcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -70,14 +85,22 @@
<Compile Include="ViewModels\AppViewModel.cs" />
<Compile Include="ViewModels\Bookmarks\BookmarkItemViewModel.cs" />
<Compile Include="DataModels\Enums\Rayman2LevelType.cs" />
<Compile Include="ViewModels\GlmWindowViewModel.cs" />
<Compile Include="ViewModels\Levels\Rayman2LevelViewModel.cs" />
<Compile Include="ViewModels\Levels\Rayman2LevelContainerViewModel.cs" />
<Compile Include="ViewModels\Bookmarks\BookmarksViewModel.cs" />
<Compile Include="ViewModels\GameManagerViewModel.cs" />
<Compile Include="Game\Rayman2Manager.cs" />
<Compile Include="Windows\GlmWindow.xaml.cs">
<DependentUpon>GlmWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Windows\RenameDialog.xaml.cs">
<DependentUpon>RenameDialog.xaml</DependentUpon>
</Compile>
<Page Include="Windows\GlmWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Windows\MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down Expand Up @@ -121,10 +144,25 @@
<PackageReference Include="PropertyChanged.Fody">
<Version>3.0.1</Version>
</PackageReference>
<PackageReference Include="System.ValueTuple">
<Version>4.5.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Resource Include="FodyWeavers.xml" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.5.2">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.5.2 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
1 change: 0 additions & 1 deletion Rayman2LevelSwitcher/ViewModels/AppViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Data;

namespace Rayman2LevelSwitcher
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public BookmarkItemViewModel(string level, string name, float x, float y, float
/// </summary>
public void LoadBookmark()
{
new Rayman2Manager().LoadPosition(X, Y, Z);
new Rayman2Manager().PlayerCoordinates = (X, Y, Z);
}

#endregion
Expand Down
Loading

0 comments on commit cf8f4ef

Please sign in to comment.