Skip to content

Commit 24f20a0

Browse files
committed
v1.0.4.4 "Added in game console, fixed Vector3 offsets and other issues"
v1.0.4.4 Added in game console, Fixed Vector3 offsets Fixed TextElement Fixed Drawing Text Updated Entity.Position
1 parent eaaaa69 commit 24f20a0

File tree

16 files changed

+112
-71
lines changed

16 files changed

+112
-71
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

2+
*.editorconfig
23
*.nuspec

examples/ExampleScript.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,20 @@ public Main()
2121

2222
private void OnTick(object sender, EventArgs e)
2323
{
24-
Player player = Game.Player;
25-
Ped playerPed = player.Character;
24+
Ped playerPed = Game.Player.Character;
2625

2726
if (ragdoll)
2827
{
2928
Function.Call(Hash.SET_PED_TO_RAGDOLL, playerPed, 5000, 5000, 0, false, false, false);
3029
}
31-
3230
}
3331

3432
private void OnKeyDown(object sender, KeyEventArgs e)
3533
{
3634
if (e.KeyCode == Keys.C)
3735
{
3836
ragdoll = !ragdoll;
37+
RDR2.UI.Screen.ShowSubtitle("Ragdoll");
3938
}
4039
}
4140

source/core/Console.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public class Console : MarshalByRefObject
4040
static readonly Color InputColor = Color.White;
4141
static readonly Color InputColorBusy = Color.DarkGray;
4242
static readonly Color OutputColor = Color.White;
43-
static readonly Color PrefixColor = Color.FromArgb(255, 52, 152, 219);
44-
static readonly Color BackgroundColor = Color.FromArgb(200, Color.Black);
45-
static readonly Color AltBackgroundColor = Color.FromArgb(200, 52, 73, 94);
43+
static readonly Color PrefixColor = Color.FromArgb(255, 189, 216, 216);
44+
static readonly Color BackgroundColor = Color.FromArgb(120, Color.Black);
45+
static readonly Color AltBackgroundColor = Color.FromArgb(120, 180, 15, 15);
4646

4747
[DllImport("user32.dll")]
4848
static extern int ToUnicode(
@@ -554,7 +554,7 @@ void CompileExpression()
554554
compilerOptions.ReferencedAssemblies.Add("System.Drawing.dll");
555555
compilerOptions.ReferencedAssemblies.Add("System.Windows.Forms.dll");
556556
// Reference the newest scripting API
557-
compilerOptions.ReferencedAssemblies.Add("ScriptHookRDRDotNet.dll");
557+
compilerOptions.ReferencedAssemblies.Add("ScriptHookRDRNetAPI.dll");
558558
compilerOptions.ReferencedAssemblies.Add(typeof(ScriptDomain).Assembly.Location);
559559

560560
foreach (var script in ScriptDomain.CurrentDomain.RunningScripts.Where(x => x.IsRunning))
@@ -613,11 +613,12 @@ static unsafe void DrawRect(float x, float y, int width, int height, Color color
613613
}
614614
static unsafe void DrawText(float x, float y, string text, Color color)
615615
{
616+
float fX = x / (float)1280;
617+
float fY = y / (float)720;
616618
NativeFunc.Invoke(0x4170B650590B3B00 /*SET_TEXT_SCALE*/, 0.35f, 0.35f);
617619
NativeFunc.Invoke(0x50A41AD966910F03 /*SET_TEXT_COLOR*/, color.R, color.G, color.B, color.A);
618620
var res = NativeFunc.Invoke(0xFA925AC00EB830B9, 10, "LITERAL_STRING", text);
619-
object varString = (string)NativeMemory.PtrToStringUTF8(new IntPtr((char*)*res));
620-
NativeFunc.Invoke(0xD79334A4BB99BAD1, varString, x, y);
621+
NativeFunc.Invoke(0xD79334A4BB99BAD1, *res, fX, fY);
621622

622623
}
623624

source/core/DllMain.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ public ref class ScriptHookRDRDotNet
112112
static RDR2DN::Console^ console = nullptr;
113113
static RDR2DN::ScriptDomain^ domain = RDR2DN::ScriptDomain::CurrentDomain;
114114
static WinForms::Keys reloadKey = WinForms::Keys::None;
115-
static WinForms::Keys consoleKey = WinForms::Keys::F4;
115+
static WinForms::Keys consoleKey = WinForms::Keys::F8;
116116

117117
static void SetConsole()
118118
{
119119
console = (RDR2DN::Console^)AppDomain::CurrentDomain->GetData("Console");
120120
}
121121

122122
};
123-
123+
bool devConfig;
124124
static void ScriptHookRDRDotNet_ManagedInit()
125125
{
126126
RDR2DN::Console^% console = ScriptHookRDRDotNet::console;
@@ -157,6 +157,8 @@ static void ScriptHookRDRDotNet_ManagedInit()
157157
else if (data[0] == "ScriptsLocation")
158158
scriptPath = data[1];
159159
}
160+
devConfig = IO::File::Exists(IO::Path::ChangeExtension(Assembly::GetExecutingAssembly()->Location, ".dev"));
161+
160162
RDR2DN::Log::Message(RDR2DN::Log::Level::Info, "Config loaded from ", IO::Path::ChangeExtension(Assembly::GetExecutingAssembly()->Location, ".ini"));
161163

162164
}
@@ -176,8 +178,8 @@ static void ScriptHookRDRDotNet_ManagedInit()
176178
}
177179

178180

179-
// Console Stuff -- commented out until internal drawtext is solved
180-
/*try
181+
// Console Stuff
182+
try
181183
{
182184
// Instantiate console inside script domain, so that it can access the scripting API
183185
console = (RDR2DN::Console^)domain->AppDomain->CreateInstanceFromAndUnwrap(
@@ -197,7 +199,7 @@ static void ScriptHookRDRDotNet_ManagedInit()
197199
catch (Exception ^ ex)
198200
{
199201
RDR2DN::Log::Message(RDR2DN::Log::Level::Error, "Failed to create console: ", ex->ToString());
200-
}*/
202+
}
201203

202204
// Start scripts in the newly created domain
203205
domain->Start();
@@ -265,9 +267,12 @@ static void ScriptHookRDRDotNet_ManagedKeyboardMessage(unsigned long keycode, bo
265267
#include <Windows.h>
266268
#include <WinBase.h>
267269

270+
271+
268272
PVOID sGameFiber = nullptr;
269273
PVOID sScriptFiber = nullptr;
270274

275+
271276
static void ScriptMain()
272277
{
273278
sGameReloaded = true;
@@ -278,6 +283,7 @@ static void ScriptMain()
278283
// Check if our CLR fiber already exists. It should be created only once for the entire lifetime of the game process.
279284
if (sScriptFiber == nullptr)
280285
{
286+
281287
const LPFIBER_START_ROUTINE FiberMain = [](LPVOID lpFiberParameter) {
282288
// Main script execution loop
283289
while (true)
@@ -296,6 +302,8 @@ static void ScriptMain()
296302
// Code continues from here the next time the loop below switches back to our CLR fiber.
297303
SwitchToFiber(sGameFiber);
298304
}
305+
306+
299307
}
300308
};
301309

source/core/NativeFunc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static ulong[] ConvertPrimitiveArguments(object[] args)
196196
}
197197
if (args[i] is string valueString)
198198
{
199-
result[i] = (ulong)ScriptDomain.CurrentDomain.PinString(valueString);
199+
result[i] = (ulong)ScriptDomain.CurrentDomain.PinString(valueString).ToInt64();
200200
continue;
201201
}
202202

source/core/NativeMemory.cs

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ public static unsafe class NativeMemory
116116
static NativeMemory()
117117
{
118118
byte* address;
119-
}
119+
120+
/*address = FindPattern("\x40\x53\x48\x83\xEC\x20\x33\xDB\x38\x1D\x00\x00\x00\x00\x74\x1C", "xxxxxxxxxx????xx");
121+
GetPlayerAddressFunc = GetDelegateForFunctionPointer<GetHandleAddressFuncDelegate>(
122+
new IntPtr(*(int*)(address + 3) + address + 7));*/
123+
}
120124

121125
/// <summary>
122126
/// Reads a single 8-bit value from the specified <paramref name="address"/>.
@@ -315,43 +319,36 @@ public static string PtrToStringUTF8(IntPtr ptr)
315319

316320
return PtrToStringUTF8(ptr, len);
317321
}
318-
public static string PtrToStringUTF8(IntPtr ptr, int len)
322+
public static string PtrToStringUTF8(IntPtr nativeUtf8, int len)
319323
{
320-
if (len < 0)
321-
throw new ArgumentException(null, nameof(len));
322-
323-
if (ptr == IntPtr.Zero)
324-
return null;
325-
if (len == 0)
326-
return string.Empty;
327-
328-
return Encoding.UTF8.GetString((byte*)ptr.ToPointer(), len);
329-
}
324+
while (Marshal.ReadByte(nativeUtf8, len) != 0) ++len;
325+
byte[] buffer = new byte[len];
326+
Marshal.Copy(nativeUtf8, buffer, 0, buffer.Length);
327+
return Encoding.UTF8.GetString(buffer);
328+
}
330329

331330

332331
public static IntPtr StringToCoTaskMemUTF8(string managedString)
333332
{
334333
int len = Encoding.UTF8.GetByteCount(managedString);
335-
336334
byte[] buffer = new byte[len + 1];
337-
338335
Encoding.UTF8.GetBytes(managedString, 0, managedString.Length, buffer, 0);
339-
340336
IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length);
341-
342337
Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length);
343-
344338
return nativeUtf8;
345339
}
346-
347340

348-
#region -- Cameras --
341+
//delegate ulong GetHandleAddressFuncDelegate(int handle);
342+
//static GetHandleAddressFuncDelegate GetPlayerAddressFunc;
349343

350-
#endregion
344+
/*public static IntPtr GetPlayerAddress(int handle)
345+
{
346+
return new IntPtr((long)GetPlayerAddressFunc(handle));
347+
}*/
351348

352-
#region -- Game Data --
349+
#region -- Game Data --
353350

354-
delegate uint GetHashKeyDelegate(IntPtr stringPtr, uint initialHash);
351+
delegate uint GetHashKeyDelegate(IntPtr stringPtr, uint initialHash);
355352
static GetHashKeyDelegate GetHashKeyFunc;
356353

357354
public static uint GetHashKey(string key)
@@ -368,21 +365,6 @@ public static uint GetHashKey(string key)
368365

369366
#endregion
370367

371-
#region -- Vehicle Offsets --
372-
373-
public static int GearOffset { get; }
374-
public static int HighGearOffset { get; }
375-
376-
public static int CurrentRPMOffset { get; }
377-
public static int AccelerationOffset { get; }
378-
379-
public static int FuelLevelOffset { get; }
380-
public static int WheelSpeedOffset { get; }
381-
382-
public static int SteeringAngleOffset { get; }
383-
public static int SteeringScaleOffset { get; }
384-
385-
#endregion
386368

387369

388370

source/core/Script.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class Script
1717
internal SemaphoreSlim continueEvent = new SemaphoreSlim(0);
1818
internal ConcurrentQueue<Tuple<bool, KeyEventArgs>> keyboardEvents = new ConcurrentQueue<Tuple<bool, KeyEventArgs>>();
1919

20+
private bool firstTime = true;
21+
2022
/// <summary>
2123
/// Gets or sets the interval in ms between each <see cref="Tick"/>.
2224
/// </summary>
@@ -122,8 +124,8 @@ void MainLoop()
122124
Abort(); return;
123125
}
124126

125-
// Yield execution to next tick
126-
Wait(Interval);
127+
// Yield execution to next tick
128+
Wait(Interval);
127129
}
128130
}
129131

@@ -135,8 +137,20 @@ public void Start()
135137
thread = new Thread(new ThreadStart(MainLoop));
136138
thread.Start();
137139

138-
Log.Message(Log.Level.Info, "Started script ", Name, ".");
139-
}
140+
unsafe
141+
{
142+
if (!firstTime) { }
143+
else
144+
{
145+
NativeFunc.Invoke(0x4170B650590B3B00, 0.1f, 0.1f);
146+
var res = NativeFunc.Invoke(0xFA925AC00EB830B9, 10, "LITERAL_STRING", "ScriptHookRDR2 .NET Loaded...");
147+
NativeFunc.Invoke(0xD79334A4BB99BAD1, *res, 0.0f, 0.0f);
148+
firstTime = false;
149+
}
150+
}
151+
152+
Log.Message(Log.Level.Info, "Started script ", Name, ".");
153+
}
140154
/// <summary>
141155
/// Aborts execution of this script.
142156
/// </summary>

source/core/ScriptDomain.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,9 @@ public IntPtr PinString(string str)
648648
}
649649
else
650650
{
651-
if (pinnedStrings.Contains(handle))
652-
{
653-
pinnedStrings.Add(handle);
654-
}
651+
652+
pinnedStrings.Add(handle);
653+
655654

656655
return handle;
657656
}

source/scripting_v3/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@
2828
// Build Number
2929
// Revision
3030
//
31-
[assembly: AssemblyVersion("1.0.0.4")]
32-
[assembly: AssemblyFileVersion("1.0.0.4")]
31+
[assembly: AssemblyVersion("1.0.4.4")]
32+
[assembly: AssemblyFileVersion("1.0.4.4")]

source/scripting_v3/RDR2.Math/Quaternion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public Vector3 Axis
9797
{
9898
get
9999
{
100-
Vector3 axis;
100+
Vector3 axis = new Vector3();
101101
float length = Length();
102102

103103
if (length != 0.0f)

source/scripting_v3/RDR2.Math/Vector3.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,33 @@ internal class Random
2929
}
3030

3131
[Serializable]
32-
[StructLayout(LayoutKind.Sequential, Pack = 4)]
32+
[StructLayout(LayoutKind.Explicit, Pack = 4)]
3333
public struct Vector3 : IEquatable<Vector3>
3434
{
3535
/// <summary>
3636
/// Gets or sets the X component of the vector.
3737
/// </summary>
3838
/// <value>The X component of the vector.</value>
39+
[FieldOffset(0)]
3940
public float X;
4041

4142
/// <summary>
4243
/// Gets or sets the Y component of the vector.
4344
/// </summary>
4445
/// <value>The Y component of the vector.</value>
46+
[FieldOffset(4)]
4547
public float Y;
4648

4749
/// <summary>
4850
/// Gets or sets the Z component of the vector.
4951
/// </summary>
5052
/// <value>The Z component of the vector.</value>
53+
[FieldOffset(8)]
5154
public float Z;
5255

56+
[FieldOffset(12)]
57+
float _padding;
58+
5359
/// <summary>
5460
/// Initializes a new instance of the <see cref="Vector3"/> class.
5561
/// </summary>
@@ -61,6 +67,7 @@ public Vector3(float x, float y, float z)
6167
X = x;
6268
Y = y;
6369
Z = z;
70+
_padding = 0;
6471
}
6572

6673
/// <summary>

source/scripting_v3/RDR2.Native/Native.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ internal NativeVector3(float x, float y, float z)
3737
Z = z;
3838
}
3939

40-
public static explicit operator Vector2(NativeVector3 val) => new Vector2(val.X, val.Y);
41-
public static explicit operator Vector3(NativeVector3 val) => new Vector3(val.X, val.Y, val.Z);
40+
public static implicit operator Vector3(NativeVector3 value) => new Vector3(value.X, value.Y, value.Z);
41+
public static implicit operator NativeVector3(Vector3 value) => new NativeVector3(value.X, value.Y, value.Z);
4242
}
4343

4444
internal unsafe static class NativeHelper<T>
@@ -458,8 +458,7 @@ internal static unsafe ulong ObjectToNative(object value)
458458
}
459459
if (value is string valueString)
460460
{
461-
RDR2DN.Log.Message(RDR2DN.Log.Level.Info, "st1. input is string...");
462-
return (ulong)RDR2DN.ScriptDomain.CurrentDomain.PinString(valueString);
461+
return (ulong)RDR2DN.ScriptDomain.CurrentDomain.PinString(valueString).ToInt64();
463462
}
464463

465464
// Scripting types
@@ -511,7 +510,7 @@ internal static unsafe object ObjectFromNative(Type type, ulong* value)
511510
{
512511
if (type == typeof(string))
513512
{
514-
return RDR2DN.NativeMemory.PtrToStringUTF8(new IntPtr((char*)*value));
513+
return RDR2DN.NativeMemory.PtrToStringUTF8(new IntPtr((byte*)*value));
515514
}
516515

517516
// Scripting types

source/scripting_v3/RDR2.UI/TextElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void InternalDraw(SizeF offset, float screenWidth, float screenHeight)
274274

275275
if (Shadow)
276276
{
277-
Function.Call(Hash.SET_TEXT_DROPSHADOW);
277+
Function.Call(Hash.SET_TEXT_DROPSHADOW,2,0,0,0,255);
278278
}
279279

280280
Function.Call(Hash.SET_TEXT_SCALE, Scale, Scale);

0 commit comments

Comments
 (0)