Skip to content

Commit

Permalink
Merge pull request #7 from 644/cutscenemovement
Browse files Browse the repository at this point in the history
Cutscene movement and other fixes
  • Loading branch information
644 authored Nov 1, 2021
2 parents 223298e + af84d58 commit ceb8d88
Show file tree
Hide file tree
Showing 12 changed files with 589 additions and 291 deletions.
2 changes: 1 addition & 1 deletion CheckUpdates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private async void CheckUpdates_Load(object sender, EventArgs e)
{
await CheckGitHubNewerVersion();
}
catch (Exception err)
catch
{
label1.Text = "Could not check for updates";
}
Expand Down
1 change: 1 addition & 0 deletions Config/config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
},
"TrackRemapping": {},
"Shuffle": false,
"CutsceneMovement": false,
"ShuffleRotation": [
"Battle_00",
"Battle_0a",
Expand Down
1 change: 1 addition & 0 deletions Config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
},
"TrackRemapping": {},
"Shuffle": false,
"CutsceneMovement": false,
"ShuffleRotation": [
"Battle_00",
"Battle_0a",
Expand Down
1 change: 1 addition & 0 deletions DMC3MusicConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class DMC3MusicConfig
public Dictionary<string, List<string>> RoomTracks { get; set; }
public Dictionary<string, List<string>> AmbientTracks { get; set; }
public bool Shuffle { get; set; }
public bool CutsceneMovement { get; set; }
public List<string> ShuffleRotation { get; set; }
public int BattleTimer { get; set; }
public int AmbientTimer { get; set; }
Expand Down
180 changes: 180 additions & 0 deletions DMC3Process.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
using Microsoft.Win32.SafeHandles;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace dmc3music
{
class DMC3Process : IDisposable
{
[DllImport("kernel32.dll")]
private static extern UIntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);

[DllImport("kernel32", SetLastError = true)]
private static extern int ReadProcessMemory(UIntPtr hProcess, IntPtr lpBase, ref int lpBuffer, int nSize, int lpNumberOfBytesRead);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(UIntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten);

[DllImport("kernel32.dll")]
private static extern int CloseHandle(UIntPtr hProcess);

[Flags]
public enum ProcessAccessFlags : uint
{
All = 0x001F0FFF,
Terminate = 0x00000001,
CreateThread = 0x00000002,
VMOperation = 0x00000008,
VMRead = 0x00000010,
VMWrite = 0x00000020,
DupHandle = 0x00000040,
SetInformation = 0x00000200,
QueryInformation = 0x00000400,
Synchronize = 0x00100000
}

public Process Process { get; set; }
public UIntPtr Handle { get; set; }
public IntPtr BaseAddress { get; set; }
public Version VersionInfo { get; set; }
public bool ProcHasExited { get; set; } = false;

private void HasExited(object sender, EventArgs e)
{
ProcHasExited = true;
}

public bool OpenReadOnly()
{
Process[] localProcess = Process.GetProcessesByName("dmc3se");

if (localProcess.Length == 0)
{
return false;
}

Process = localProcess[0];
Handle = OpenProcess(ProcessAccessFlags.VMRead, false, Process.Id);

if (Handle == UIntPtr.Zero)
{
return false;
}

BaseAddress = Process.MainModule.BaseAddress;
try
{
VersionInfo = new Version(Process.MainModule.FileVersionInfo.ProductVersion.Replace(",", "."));
}
catch
{
return false;
}

Process.EnableRaisingEvents = true;
Process.Exited += new EventHandler(HasExited);

return true;
}

public bool OpenReadWrite()
{
Process[] localProcess = Process.GetProcessesByName("dmc3se");

if (localProcess.Length == 0)
{
return false;
}

Process = localProcess[0];
Handle = OpenProcess(ProcessAccessFlags.All, false, Process.Id);

if (Handle == UIntPtr.Zero)
{
return false;
}

BaseAddress = Process.MainModule.BaseAddress;
try
{
VersionInfo = new Version(Process.MainModule.FileVersionInfo.ProductVersion.Replace(",", "."));
}
catch
{
return false;
}

Process.EnableRaisingEvents = true;
Process.Exited += new EventHandler(HasExited);

return true;
}

public void WriteMem(byte[] bytes, int address)
{
IntPtr offset = IntPtr.Add(BaseAddress, address);
WriteProcessMemory(Handle, offset, bytes, (uint)bytes.LongLength, out _);
}

public void WriteInt(int intVal, int address)
{
byte[] bytes = new byte[4];

bytes[3] = (byte)(intVal >> 24);
bytes[2] = (byte)(intVal >> 16);
bytes[1] = (byte)(intVal >> 8);
bytes[0] = (byte)intVal;

WriteProcessMemory(Handle, new IntPtr(address), bytes, (uint)bytes.LongLength, out _);
}

public void WriteExactMem(byte[] bytes, int address)
{
WriteProcessMemory(Handle, new IntPtr(address), bytes, (uint)bytes.LongLength, out int bytesWritten);
Console.WriteLine(bytesWritten);
}

public int ReadExactMem(int address)
{
int res = -1;
ReadProcessMemory(Handle, new IntPtr(address), ref res, sizeof(int), 0);
return res;
}

public int ReadMem(int address)
{
IntPtr offset = IntPtr.Add(BaseAddress, address);
int res = -1;
ReadProcessMemory(Handle, offset, ref res, sizeof(int), 0);
return res;
}

public int GetIntPtr(IntPtr address)
{
int res = 0;
ReadProcessMemory(Handle, address, ref res, sizeof(int), 0);
return res;
}

private bool _disposedValue;

private readonly SafeHandle _safeHandle = new SafeFileHandle(IntPtr.Zero, true);

public void Dispose() => Dispose(true);

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
CloseHandle(Handle);
_safeHandle.Dispose();
}

_disposedValue = true;
}
}
}
}
35 changes: 13 additions & 22 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ceb8d88

Please sign in to comment.