Skip to content

Commit

Permalink
Automatically attach to Dark Souls process
Browse files Browse the repository at this point in the history
  • Loading branch information
Chronial committed May 4, 2016
1 parent 99b543e commit be2347e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 88 deletions.
23 changes: 11 additions & 12 deletions DaS-PC-MPChan/DSCM.Designer.vb

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

36 changes: 22 additions & 14 deletions DaS-PC-MPChan/DSCM.vb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Public Class DSCM
Private WithEvents refTimer As New System.Windows.Forms.Timer()
Private WithEvents onlineTimer As New System.Windows.Forms.Timer()
Private WithEvents ircConnectTimer As New System.Windows.Forms.Timer()
Private WithEvents dsProcessTimer As New System.Windows.Forms.Timer()
Private WithEvents hotkeyTimer As New System.Windows.Forms.Timer()

'For hotkey support
Expand Down Expand Up @@ -50,10 +51,10 @@ Public Class DSCM

ircConnectTimer.Interval = 20000

Try
dsProcess = New DarkSoulsProcess()
Catch ex As DSProcessNotFound
End Try
dsProcessTimer.Interval = 1000
dsProcessTimer.Start()

attachDSProcess()

'Set initial form size to non-expanded
Me.Width = 450
Expand Down Expand Up @@ -354,7 +355,7 @@ Public Class DSCM
dgvFavoriteNodes.Height = Me.Height - 225
dgvRecentNodes.Height = Me.Height - 225

btnReconnect.Location = New Point(1, Me.Height - 65)
dsProcessStatus.Location = New Point(10, Me.Height - 63)
lblVer.Location = New Point(Me.Width - 100, Me.Height - 55)

lblNodes.Location = New Point(Me.Width - 167, 6)
Expand All @@ -371,16 +372,23 @@ Public Class DSCM
btnRemFavorite.Location = New Point(400, Me.Height - 65)
End Sub

Private Sub btnReconnect_Click(sender As Object, e As EventArgs) Handles btnReconnect.Click
If Not IsNothing(dsProcess) Then
dsProcess.Dispose()
dsProcess = Nothing
Private Sub attachDSProcess() Handles dsProcessTimer.Tick
If dsProcess isNot Nothing Then
If Not dsProcess.IsAttached
dsProcess.Dispose()
dsProcess = Nothing
End If
End If
If dsProcess is Nothing Then
Try
dsProcess = New DarkSoulsProcess()
dsProcessStatus.Text = " Attached to Dark Souls process"
dsProcessStatus.BackColor = System.Drawing.Color.FromArgb(200, 255, 200)
Catch ex As DSProcessAttachException
dsProcessStatus.Text = " " & ex.Message
dsProcessStatus.BackColor = System.Drawing.Color.FromArgb(255, 200, 200)
End Try
End If

Try
dsProcess = New DarkSoulsProcess()
Catch ex As DSProcessNotFound
End Try
End Sub

Private Sub chkDebugDrawing_CheckedChanged(sender As Object, e As EventArgs) Handles chkDebugDrawing.CheckedChanged
Expand Down
123 changes: 61 additions & 62 deletions DaS-PC-MPChan/DarkSoulsProcess.vb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Public Class DSProcessNotFound
Inherits System.ApplicationException
Public Class DSProcessAttachException
Inherits System.ApplicationException

Sub New(message As String)
MyBase.New(message)
End Sub
End Class

Public Class DarkSoulsProcess
Expand Down Expand Up @@ -54,15 +58,15 @@ Public Class DarkSoulsProcess
Protected disposed As Boolean = False

Sub New()
If Not TryAttachToProcess("darksouls")
Throw New DSProcessNotFound
End If
attachToProcess()
Dim beta = (ReadUInt32(dsBase + &H80) = &HE91B11E2&)
If beta Then
DetachFromProcess()
MsgBox("Beta version detected. Disconnecting from process.")
Throw New DSProcessNotFound
detachFromProcess()
Throw New DSProcessAttachException("Dark Souls beta is not supported")
End If
'TODO (chronial): Is this safe here, or should we wait a second because Dark Souls might be booting up?
findDllAddresses()
disableLowFPSDisonnect()
SetupNodeDumpHook()
End Sub

Expand All @@ -73,7 +77,7 @@ Public Class DarkSoulsProcess
End If

TearDownNodeDumpHook()
DetachFromProcess()
detachFromProcess()
disposed = True
End If
End Sub
Expand All @@ -82,72 +86,63 @@ Public Class DarkSoulsProcess
Dispose()
End Sub

Private Function TryAttachToProcess(ByVal windowCaption As String) As Boolean
Private Sub attachToProcess()
Dim windowCaption As String = "darksouls"
Dim _allProcesses() As Process = Process.GetProcesses
For Each pp As Process In _allProcesses
If pp.ProcessName.ToLower.Equals(windowCaption.ToLower) Then
Return TryAttachToProcess(pp)
If pp.ProcessName.ToLower.Equals(windowCaption.ToLower) AndAlso Not pp.HasExited Then
attachToProcess(pp)
Return
End If
Next
MessageBox.Show("Unable to find process '" & windowCaption & "'." & vbCrLf & " Is it running?")
Return False
End Function
Throw New DSProcessAttachException("Dark Souls not running")
End Sub

Private Function TryAttachToProcess(ByVal proc As Process) As Boolean
Private Sub attachToProcess(ByVal proc As Process)
If _targetProcessHandle = IntPtr.Zero Then 'not already attached
_targetProcess = proc
_targetProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, _targetProcess.Id)
If _targetProcessHandle = 0 Then
TryAttachToProcess = False
steamApiBase = 0
MessageBox.Show("OpenProcess() FAIL! Are you Administrator??")
Else
TryAttachToProcess = True

'Map various base addresses
For Each dll In _targetProcess.Modules

Select Case dll.modulename.tolower

Case "darksouls.exe"
'Note to self, extra variables due to issues with conversion. Fix "some day".
dsBasePtr = dll.BaseAddress
dsBase = dsBasePtr

Case "d3d9.dll"
If watchdog = False Then
dsPWPtr = dll.baseaddress
dsPWBase = dsPWPtr

If ReadUInt8(dsPWBase + &H6E41) = &HE8& Then
watchdog = True
'this is ineffective at disabling PVP Watchdog's node write
'WriteBytes(dsPWBase + &H6E41, {&H90, &H90, &H90, &H90, &H90})
End If
End If


'Find steam_api.dll for ability to directly add SteamIDs as nodes
Case "steam_api.dll"
steamApiBasePtr = dll.baseaddress
steamApiBase = steamApiBasePtr
steamApiDllModule = dll

End Select
Next

'Disable FPS Disconnection
If ReadUInt8(dsBase + &H978425) = &HE8& Then
WriteBytes(dsBase + &H978425, {&H90, &H90, &H90, &H90, &H90})
End If
Throw New DSProcessAttachException("OpenProcess() failed. Check Permissions")
End If
Else
MessageBox.Show("Already attached! (Please Detach first?)")
TryAttachToProcess = False
End If
End Function
End Sub
Private Sub findDllAddresses()
For Each dll In _targetProcess.Modules
Select Case dll.modulename.tolower
Case "darksouls.exe"
'Note to self, extra variables due to issues with conversion. Fix "some day".
dsBasePtr = dll.BaseAddress
dsBase = dsBasePtr

Case "d3d9.dll"
If watchdog = False Then
dsPWPtr = dll.baseaddress
dsPWBase = dsPWPtr

If ReadUInt8(dsPWBase + &H6E41) = &HE8& Then
watchdog = True
'this is ineffective at disabling PVP Watchdog's node write
'WriteBytes(dsPWBase + &H6E41, {&H90, &H90, &H90, &H90, &H90})
End If
End If

Case "steam_api.dll" 'Find steam_api.dll for ability to directly add SteamIDs as nodes
steamApiBasePtr = dll.baseaddress
steamApiBase = steamApiBasePtr
steamApiDllModule = dll

Private Sub DetachFromProcess()
End Select
Next
End Sub
Private Sub disableLowFPSDisonnect()
If ReadUInt8(dsBase + &H978425) = &HE8& Then
WriteBytes(dsBase + &H978425, {&H90, &H90, &H90, &H90, &H90})
End If
End Sub
Private Sub detachFromProcess()
If Not (_targetProcessHandle = IntPtr.Zero) Then
_targetProcess = Nothing
Try
Expand All @@ -157,7 +152,11 @@ Public Class DarkSoulsProcess
End Try
End If
End Sub

Public ReadOnly Property IsAttached As Boolean
Get
Return Not _targetProcess.HasExited
End Get
End Property
Public Property DrawNodes As Boolean
Get
Return (ReadBytes(dsBase + &HBA256C, 1)(0) = 1)
Expand Down

0 comments on commit be2347e

Please sign in to comment.