Skip to content

Commit

Permalink
v1.0.4.4 "Added in game console, fixed Vector3 offsets and other issues"
Browse files Browse the repository at this point in the history
v1.0.4.4
Added in game console,
Fixed Vector3 offsets
Fixed TextElement
Fixed Drawing Text
Updated Entity.Position
  • Loading branch information
Saltyq committed Nov 24, 2019
1 parent eaaaa69 commit 24f20a0
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

*.editorconfig
*.nuspec
5 changes: 2 additions & 3 deletions examples/ExampleScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@ public Main()

private void OnTick(object sender, EventArgs e)
{
Player player = Game.Player;
Ped playerPed = player.Character;
Ped playerPed = Game.Player.Character;

if (ragdoll)
{
Function.Call(Hash.SET_PED_TO_RAGDOLL, playerPed, 5000, 5000, 0, false, false, false);
}

}

private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C)
{
ragdoll = !ragdoll;
RDR2.UI.Screen.ShowSubtitle("Ragdoll");
}
}

Expand Down
13 changes: 7 additions & 6 deletions source/core/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public class Console : MarshalByRefObject
static readonly Color InputColor = Color.White;
static readonly Color InputColorBusy = Color.DarkGray;
static readonly Color OutputColor = Color.White;
static readonly Color PrefixColor = Color.FromArgb(255, 52, 152, 219);
static readonly Color BackgroundColor = Color.FromArgb(200, Color.Black);
static readonly Color AltBackgroundColor = Color.FromArgb(200, 52, 73, 94);
static readonly Color PrefixColor = Color.FromArgb(255, 189, 216, 216);
static readonly Color BackgroundColor = Color.FromArgb(120, Color.Black);
static readonly Color AltBackgroundColor = Color.FromArgb(120, 180, 15, 15);

[DllImport("user32.dll")]
static extern int ToUnicode(
Expand Down Expand Up @@ -554,7 +554,7 @@ void CompileExpression()
compilerOptions.ReferencedAssemblies.Add("System.Drawing.dll");
compilerOptions.ReferencedAssemblies.Add("System.Windows.Forms.dll");
// Reference the newest scripting API
compilerOptions.ReferencedAssemblies.Add("ScriptHookRDRDotNet.dll");
compilerOptions.ReferencedAssemblies.Add("ScriptHookRDRNetAPI.dll");
compilerOptions.ReferencedAssemblies.Add(typeof(ScriptDomain).Assembly.Location);

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

}

Expand Down
18 changes: 13 additions & 5 deletions source/core/DllMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ public ref class ScriptHookRDRDotNet
static RDR2DN::Console^ console = nullptr;
static RDR2DN::ScriptDomain^ domain = RDR2DN::ScriptDomain::CurrentDomain;
static WinForms::Keys reloadKey = WinForms::Keys::None;
static WinForms::Keys consoleKey = WinForms::Keys::F4;
static WinForms::Keys consoleKey = WinForms::Keys::F8;

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

};

bool devConfig;
static void ScriptHookRDRDotNet_ManagedInit()
{
RDR2DN::Console^% console = ScriptHookRDRDotNet::console;
Expand Down Expand Up @@ -157,6 +157,8 @@ static void ScriptHookRDRDotNet_ManagedInit()
else if (data[0] == "ScriptsLocation")
scriptPath = data[1];
}
devConfig = IO::File::Exists(IO::Path::ChangeExtension(Assembly::GetExecutingAssembly()->Location, ".dev"));

RDR2DN::Log::Message(RDR2DN::Log::Level::Info, "Config loaded from ", IO::Path::ChangeExtension(Assembly::GetExecutingAssembly()->Location, ".ini"));

}
Expand All @@ -176,8 +178,8 @@ static void ScriptHookRDRDotNet_ManagedInit()
}


// Console Stuff -- commented out until internal drawtext is solved
/*try
// Console Stuff
try
{
// Instantiate console inside script domain, so that it can access the scripting API
console = (RDR2DN::Console^)domain->AppDomain->CreateInstanceFromAndUnwrap(
Expand All @@ -197,7 +199,7 @@ static void ScriptHookRDRDotNet_ManagedInit()
catch (Exception ^ ex)
{
RDR2DN::Log::Message(RDR2DN::Log::Level::Error, "Failed to create console: ", ex->ToString());
}*/
}

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



PVOID sGameFiber = nullptr;
PVOID sScriptFiber = nullptr;


static void ScriptMain()
{
sGameReloaded = true;
Expand All @@ -278,6 +283,7 @@ static void ScriptMain()
// Check if our CLR fiber already exists. It should be created only once for the entire lifetime of the game process.
if (sScriptFiber == nullptr)
{

const LPFIBER_START_ROUTINE FiberMain = [](LPVOID lpFiberParameter) {
// Main script execution loop
while (true)
Expand All @@ -296,6 +302,8 @@ static void ScriptMain()
// Code continues from here the next time the loop below switches back to our CLR fiber.
SwitchToFiber(sGameFiber);
}


}
};

Expand Down
2 changes: 1 addition & 1 deletion source/core/NativeFunc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static ulong[] ConvertPrimitiveArguments(object[] args)
}
if (args[i] is string valueString)
{
result[i] = (ulong)ScriptDomain.CurrentDomain.PinString(valueString);
result[i] = (ulong)ScriptDomain.CurrentDomain.PinString(valueString).ToInt64();
continue;
}

Expand Down
56 changes: 19 additions & 37 deletions source/core/NativeMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ public static unsafe class NativeMemory
static NativeMemory()
{
byte* address;
}

/*address = FindPattern("\x40\x53\x48\x83\xEC\x20\x33\xDB\x38\x1D\x00\x00\x00\x00\x74\x1C", "xxxxxxxxxx????xx");
GetPlayerAddressFunc = GetDelegateForFunctionPointer<GetHandleAddressFuncDelegate>(
new IntPtr(*(int*)(address + 3) + address + 7));*/
}

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

return PtrToStringUTF8(ptr, len);
}
public static string PtrToStringUTF8(IntPtr ptr, int len)
public static string PtrToStringUTF8(IntPtr nativeUtf8, int len)
{
if (len < 0)
throw new ArgumentException(null, nameof(len));

if (ptr == IntPtr.Zero)
return null;
if (len == 0)
return string.Empty;

return Encoding.UTF8.GetString((byte*)ptr.ToPointer(), len);
}
while (Marshal.ReadByte(nativeUtf8, len) != 0) ++len;
byte[] buffer = new byte[len];
Marshal.Copy(nativeUtf8, buffer, 0, buffer.Length);
return Encoding.UTF8.GetString(buffer);
}


public static IntPtr StringToCoTaskMemUTF8(string managedString)
{
int len = Encoding.UTF8.GetByteCount(managedString);

byte[] buffer = new byte[len + 1];

Encoding.UTF8.GetBytes(managedString, 0, managedString.Length, buffer, 0);

IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length);

Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length);

return nativeUtf8;
}


#region -- Cameras --
//delegate ulong GetHandleAddressFuncDelegate(int handle);
//static GetHandleAddressFuncDelegate GetPlayerAddressFunc;

#endregion
/*public static IntPtr GetPlayerAddress(int handle)
{
return new IntPtr((long)GetPlayerAddressFunc(handle));
}*/

#region -- Game Data --
#region -- Game Data --

delegate uint GetHashKeyDelegate(IntPtr stringPtr, uint initialHash);
delegate uint GetHashKeyDelegate(IntPtr stringPtr, uint initialHash);
static GetHashKeyDelegate GetHashKeyFunc;

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

#endregion

#region -- Vehicle Offsets --

public static int GearOffset { get; }
public static int HighGearOffset { get; }

public static int CurrentRPMOffset { get; }
public static int AccelerationOffset { get; }

public static int FuelLevelOffset { get; }
public static int WheelSpeedOffset { get; }

public static int SteeringAngleOffset { get; }
public static int SteeringScaleOffset { get; }

#endregion



Expand Down
22 changes: 18 additions & 4 deletions source/core/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class Script
internal SemaphoreSlim continueEvent = new SemaphoreSlim(0);
internal ConcurrentQueue<Tuple<bool, KeyEventArgs>> keyboardEvents = new ConcurrentQueue<Tuple<bool, KeyEventArgs>>();

private bool firstTime = true;

/// <summary>
/// Gets or sets the interval in ms between each <see cref="Tick"/>.
/// </summary>
Expand Down Expand Up @@ -122,8 +124,8 @@ void MainLoop()
Abort(); return;
}

// Yield execution to next tick
Wait(Interval);
// Yield execution to next tick
Wait(Interval);
}
}

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

Log.Message(Log.Level.Info, "Started script ", Name, ".");
}
unsafe
{
if (!firstTime) { }
else
{
NativeFunc.Invoke(0x4170B650590B3B00, 0.1f, 0.1f);
var res = NativeFunc.Invoke(0xFA925AC00EB830B9, 10, "LITERAL_STRING", "ScriptHookRDR2 .NET Loaded...");
NativeFunc.Invoke(0xD79334A4BB99BAD1, *res, 0.0f, 0.0f);
firstTime = false;
}
}

Log.Message(Log.Level.Info, "Started script ", Name, ".");
}
/// <summary>
/// Aborts execution of this script.
/// </summary>
Expand Down
7 changes: 3 additions & 4 deletions source/core/ScriptDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,9 @@ public IntPtr PinString(string str)
}
else
{
if (pinnedStrings.Contains(handle))
{
pinnedStrings.Add(handle);
}

pinnedStrings.Add(handle);


return handle;
}
Expand Down
4 changes: 2 additions & 2 deletions source/scripting_v3/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.4")]
[assembly: AssemblyFileVersion("1.0.0.4")]
[assembly: AssemblyVersion("1.0.4.4")]
[assembly: AssemblyFileVersion("1.0.4.4")]
2 changes: 1 addition & 1 deletion source/scripting_v3/RDR2.Math/Quaternion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Vector3 Axis
{
get
{
Vector3 axis;
Vector3 axis = new Vector3();
float length = Length();

if (length != 0.0f)
Expand Down
9 changes: 8 additions & 1 deletion source/scripting_v3/RDR2.Math/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,33 @@ internal class Random
}

[Serializable]
[StructLayout(LayoutKind.Sequential, Pack = 4)]
[StructLayout(LayoutKind.Explicit, Pack = 4)]
public struct Vector3 : IEquatable<Vector3>
{
/// <summary>
/// Gets or sets the X component of the vector.
/// </summary>
/// <value>The X component of the vector.</value>
[FieldOffset(0)]
public float X;

/// <summary>
/// Gets or sets the Y component of the vector.
/// </summary>
/// <value>The Y component of the vector.</value>
[FieldOffset(4)]
public float Y;

/// <summary>
/// Gets or sets the Z component of the vector.
/// </summary>
/// <value>The Z component of the vector.</value>
[FieldOffset(8)]
public float Z;

[FieldOffset(12)]
float _padding;

/// <summary>
/// Initializes a new instance of the <see cref="Vector3"/> class.
/// </summary>
Expand All @@ -61,6 +67,7 @@ public Vector3(float x, float y, float z)
X = x;
Y = y;
Z = z;
_padding = 0;
}

/// <summary>
Expand Down
9 changes: 4 additions & 5 deletions source/scripting_v3/RDR2.Native/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ internal NativeVector3(float x, float y, float z)
Z = z;
}

public static explicit operator Vector2(NativeVector3 val) => new Vector2(val.X, val.Y);
public static explicit operator Vector3(NativeVector3 val) => new Vector3(val.X, val.Y, val.Z);
public static implicit operator Vector3(NativeVector3 value) => new Vector3(value.X, value.Y, value.Z);
public static implicit operator NativeVector3(Vector3 value) => new NativeVector3(value.X, value.Y, value.Z);
}

internal unsafe static class NativeHelper<T>
Expand Down Expand Up @@ -458,8 +458,7 @@ internal static unsafe ulong ObjectToNative(object value)
}
if (value is string valueString)
{
RDR2DN.Log.Message(RDR2DN.Log.Level.Info, "st1. input is string...");
return (ulong)RDR2DN.ScriptDomain.CurrentDomain.PinString(valueString);
return (ulong)RDR2DN.ScriptDomain.CurrentDomain.PinString(valueString).ToInt64();
}

// Scripting types
Expand Down Expand Up @@ -511,7 +510,7 @@ internal static unsafe object ObjectFromNative(Type type, ulong* value)
{
if (type == typeof(string))
{
return RDR2DN.NativeMemory.PtrToStringUTF8(new IntPtr((char*)*value));
return RDR2DN.NativeMemory.PtrToStringUTF8(new IntPtr((byte*)*value));
}

// Scripting types
Expand Down
2 changes: 1 addition & 1 deletion source/scripting_v3/RDR2.UI/TextElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ void InternalDraw(SizeF offset, float screenWidth, float screenHeight)

if (Shadow)
{
Function.Call(Hash.SET_TEXT_DROPSHADOW);
Function.Call(Hash.SET_TEXT_DROPSHADOW,2,0,0,0,255);
}

Function.Call(Hash.SET_TEXT_SCALE, Scale, Scale);
Expand Down
Loading

0 comments on commit 24f20a0

Please sign in to comment.