1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Diagnostics ;
4+ using System . Linq ;
5+ using System . Runtime . InteropServices ;
6+ using System . Text ;
7+ #pragma warning disable 1591
8+
9+ // Note: Please be careful when modifying this because it could break existing components!
10+
11+ namespace VeninethTrainer
12+ {
13+ using OffsetT = Int32 ;
14+
15+ public class DeepPointer
16+ {
17+ private IntPtr _absoluteBase ;
18+ private bool _usingAbsoluteBase ;
19+
20+ private OffsetT _base ;
21+ private List < OffsetT > _offsets ;
22+ private string _module ;
23+
24+ public DeepPointer ( IntPtr absoluteBase , params OffsetT [ ] offsets )
25+ {
26+ _absoluteBase = absoluteBase ;
27+ _usingAbsoluteBase = true ;
28+
29+ InitializeOffsets ( offsets ) ;
30+ }
31+
32+ public DeepPointer ( string module , OffsetT base_ , params OffsetT [ ] offsets )
33+ : this ( base_ , offsets )
34+ {
35+ _module = module . ToLower ( ) ;
36+ }
37+
38+ public DeepPointer ( OffsetT base_ , params OffsetT [ ] offsets )
39+ {
40+ _base = base_ ;
41+ InitializeOffsets ( offsets ) ;
42+ }
43+
44+ public T Deref < T > ( Process process , T default_ = default ( T ) ) where T : struct // all value types including structs
45+ {
46+ T val ;
47+ if ( ! Deref ( process , out val ) )
48+ val = default_ ;
49+ return val ;
50+ }
51+
52+ public bool Deref < T > ( Process process , out T value ) where T : struct
53+ {
54+ IntPtr ptr ;
55+ if ( ! DerefOffsets ( process , out ptr )
56+ || ! process . ReadValue ( ptr , out value ) )
57+ {
58+ value = default ( T ) ;
59+ return false ;
60+ }
61+
62+ return true ;
63+ }
64+
65+ public byte [ ] DerefBytes ( Process process , int count )
66+ {
67+ byte [ ] bytes ;
68+ if ( ! DerefBytes ( process , count , out bytes ) )
69+ bytes = null ;
70+ return bytes ;
71+ }
72+
73+ public bool DerefBytes ( Process process , int count , out byte [ ] value )
74+ {
75+ IntPtr ptr ;
76+ if ( ! DerefOffsets ( process , out ptr )
77+ || ! process . ReadBytes ( ptr , count , out value ) )
78+ {
79+ value = null ;
80+ return false ;
81+ }
82+
83+ return true ;
84+ }
85+
86+ public string DerefString ( Process process , int numBytes , string default_ = null )
87+ {
88+ string str ;
89+ if ( ! DerefString ( process , ReadStringType . AutoDetect , numBytes , out str ) )
90+ str = default_ ;
91+ return str ;
92+ }
93+
94+ public string DerefString ( Process process , ReadStringType type , int numBytes , string default_ = null )
95+ {
96+ string str ;
97+ if ( ! DerefString ( process , type , numBytes , out str ) )
98+ str = default_ ;
99+ return str ;
100+ }
101+
102+ public bool DerefString ( Process process , int numBytes , out string str )
103+ {
104+ return DerefString ( process , ReadStringType . AutoDetect , numBytes , out str ) ;
105+ }
106+
107+ public bool DerefString ( Process process , ReadStringType type , int numBytes , out string str )
108+ {
109+ var sb = new StringBuilder ( numBytes ) ;
110+ if ( ! DerefString ( process , type , sb ) )
111+ {
112+ str = null ;
113+ return false ;
114+ }
115+ str = sb . ToString ( ) ;
116+ return true ;
117+ }
118+
119+ public bool DerefString ( Process process , StringBuilder sb )
120+ {
121+ return DerefString ( process , ReadStringType . AutoDetect , sb ) ;
122+ }
123+
124+ public bool DerefString ( Process process , ReadStringType type , StringBuilder sb )
125+ {
126+ IntPtr ptr ;
127+ if ( ! DerefOffsets ( process , out ptr )
128+ || ! process . ReadString ( ptr , type , sb ) )
129+ {
130+ return false ;
131+ }
132+ return true ;
133+ }
134+
135+ public bool DerefOffsets ( Process process , out IntPtr ptr )
136+ {
137+ bool is64Bit = process . Is64Bit ( ) ;
138+
139+ if ( ! string . IsNullOrEmpty ( _module ) )
140+ {
141+ ProcessModuleWow64Safe module = process . ModulesWow64Safe ( )
142+ . FirstOrDefault ( m => m . ModuleName . ToLower ( ) == _module ) ;
143+ if ( module == null )
144+ {
145+ ptr = IntPtr . Zero ;
146+ return false ;
147+ }
148+
149+ ptr = module . BaseAddress + _base ;
150+ }
151+ else if ( _usingAbsoluteBase )
152+ {
153+ ptr = _absoluteBase ;
154+ }
155+ else
156+ {
157+ ptr = process . MainModuleWow64Safe ( ) . BaseAddress + _base ;
158+ }
159+
160+ for ( int i = 0 ; i < _offsets . Count - 1 ; i ++ )
161+ {
162+ if ( ! process . ReadPointer ( ptr + _offsets [ i ] , is64Bit , out ptr )
163+ || ptr == IntPtr . Zero )
164+ {
165+ return false ;
166+ }
167+ }
168+
169+ ptr = ptr + _offsets [ _offsets . Count - 1 ] ;
170+ return true ;
171+ }
172+
173+ private void InitializeOffsets ( params OffsetT [ ] offsets )
174+ {
175+ _offsets = new List < OffsetT > ( ) ;
176+ _offsets . Add ( 0 ) ; // deref base first
177+ _offsets . AddRange ( offsets ) ;
178+ }
179+ }
180+
181+ [ StructLayout ( LayoutKind . Sequential ) ]
182+ public struct Vector3f
183+ {
184+ public float X { get ; set ; }
185+ public float Y { get ; set ; }
186+ public float Z { get ; set ; }
187+
188+ public int IX { get { return ( int ) X ; } }
189+ public int IY { get { return ( int ) Y ; } }
190+ public int IZ { get { return ( int ) Z ; } }
191+
192+ public Vector3f ( float x , float y , float z ) : this ( )
193+ {
194+ X = x ;
195+ Y = y ;
196+ Z = z ;
197+ }
198+
199+ public float Distance ( Vector3f other )
200+ {
201+ float result = ( X - other . X ) * ( X - other . X ) +
202+ ( Y - other . Y ) * ( Y - other . Y ) +
203+ ( Z - other . Z ) * ( Z - other . Z ) ;
204+ return ( float ) Math . Sqrt ( result ) ;
205+ }
206+
207+ public float DistanceXY ( Vector3f other )
208+ {
209+ float result = ( X - other . X ) * ( X - other . X ) +
210+ ( Y - other . Y ) * ( Y - other . Y ) ;
211+ return ( float ) Math . Sqrt ( result ) ;
212+ }
213+
214+ public bool BitEquals ( Vector3f other )
215+ {
216+ return X . BitEquals ( other . X )
217+ && Y . BitEquals ( other . Y )
218+ && Z . BitEquals ( other . Z ) ;
219+ }
220+
221+ public bool BitEqualsXY ( Vector3f other )
222+ {
223+ return X . BitEquals ( other . X )
224+ && Y . BitEquals ( other . Y ) ;
225+ }
226+
227+ public override string ToString ( )
228+ {
229+ return X + " " + Y + " " + Z ;
230+ }
231+ }
232+ }
0 commit comments