Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Vehicle & Remote Method Enhancements #102

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 deletions OmsiExtensionsCLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Reflection;
Expand Down Expand Up @@ -54,34 +55,46 @@ static void Main(string[] args)
Console.WriteLine($"Read data: map:{map?.Name} path:{map?.Filename} friendly:{map?.FriendlyName}".PadRight(Console.WindowWidth - 1));
Console.WriteLine($"Time: {omsi.Globals.Time.Day}/{omsi.Globals.Time.Month}/{omsi.Globals.Time.Year} - {omsi.Globals.Time.Hour}:{omsi.Globals.Time.Minute}:{omsi.Globals.Time.Second:F2} ");
Console.WriteLine($"Camera pos: {cam.Pos} ".PadRight(Console.WindowWidth - 1));
Console.WriteLine($"{omsi.Globals.Drivers}".PadRight(Console.WindowWidth - 1));

/*if(!dXTests.IsReady)
dXTests.CreateTexture();
if(dXTests.IsReady)
dXTests.UpdateTexture();*/

Console.WriteLine($"[MOUSE] pos: {progMan.MausPos}".PadRight(Console.WindowWidth - 1));
Console.WriteLine($"[MOUSE] ray_pos: {progMan.MausLine3DPos} ray_dir: {progMan.MausLine3DDir}".PadRight(Console.WindowWidth - 1));
Console.WriteLine($"[MOUSE] mouse_mesh_event: {progMan.Maus_MeshEvent}".PadRight(Console.WindowWidth - 1));
CheckClickPos(progMan, meshes, meshInsts);

Console.WriteLine($"Loaded textures: {textures.Count}");
Console.WriteLine($"Path id: {playerVehicle.PathInfo.path.path}");

/*Console.WriteLine("".PadRight(Console.WindowWidth-1));
try
//Console.WriteLine($"{omsi.Globals.Drivers}".PadRight(Console.WindowWidth - 1));

///*if(!dXTests.IsReady)
// dXTests.CreateTexture();
//if(dXTests.IsReady)
// dXTests.UpdateTexture();*/

//Console.WriteLine($"[MOUSE] pos: {progMan.MausPos}".PadRight(Console.WindowWidth - 1));
//Console.WriteLine($"[MOUSE] ray_pos: {progMan.MausLine3DPos} ray_dir: {progMan.MausLine3DDir}".PadRight(Console.WindowWidth - 1));
//Console.WriteLine($"[MOUSE] mouse_mesh_event: {progMan.Maus_MeshEvent}".PadRight(Console.WindowWidth - 1));
//CheckClickPos(progMan, meshes, meshInsts);

//Console.WriteLine($"Loaded textures: {textures.Count}");
//Console.WriteLine($"Path id: {playerVehicle.PathInfo.path.path}");

///*Console.WriteLine("".PadRight(Console.WindowWidth-1));
//try
//{
// if (omsi.Globals.PlayerVehicle != null)
// {
// Console.WriteLine($"INEO_PS_Matricule: {omsi.Globals.PlayerVehicle.GetStringVariable("INEO_Login")}".PadRight(Console.WindowWidth - 1));


// omsi.Globals.PlayerVehicle.SetStringVariable("INEO_Login", toggle ? "thomas" : "01234");
// toggle = !toggle;
// }
//}
//catch (Exception e) { Console.WriteLine(e.Message); }*/
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably shouldn't have been committed...



var OMSIRM = omsi.RemoteMethods;
OMSIRM.PlaceRandomBus();
Console.WriteLine("Placed");


OMSIRM.MakeVehicle(@"Vehicles\GPM_MAN_LionsCity_M\MAN_A47.bus").ContinueWith((id) =>
{
if (omsi.Globals.PlayerVehicle != null)
{
Console.WriteLine($"INEO_PS_Matricule: {omsi.Globals.PlayerVehicle.GetStringVariable("INEO_Login")}".PadRight(Console.WindowWidth - 1));


omsi.Globals.PlayerVehicle.SetStringVariable("INEO_Login", toggle ? "thomas" : "01234");
toggle = !toggle;
}
}
catch (Exception e) { Console.WriteLine(e.Message); }*/
Console.WriteLine($"Spawned Vehicle ID: {id}");
Debugger.Break();
});

Thread.Sleep(20);
}
Expand Down
104 changes: 91 additions & 13 deletions OmsiHook/OmsiRemoteMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,97 @@ private void ResultReaderTask()
}
}

[Obsolete]
public int MakeVehicle()
private async Task<int> TempRVListCreate(uint classAddr, int capacity)
{
int vehList = TTempRVListCreate(0x0074802C, 1);
string path = @"Vehicles\GPM_MAN_LionsCity_M\MAN_A47.bus";
int mem = memory.AllocateString(path, false).Result;

return TProgManMakeVehicle(memory.ReadMemory<int>(0x00862f28), vehList,
memory.ReadMemory<int>(0x008615A8), false, false,
0, false, false, false, false,
-1, true, 0, (byte)3, false,
0, 0, 0, 0, 0, false,
false, true, true, mem);
if (localPlugin)
{
return TTempRVListCreate(unchecked((int)classAddr), capacity);
}
else
{
if (!IsInitialised)
throw new NotInitialisedException("OmsiHook RPC plugin is not connected! Did you make sure to call OmsiRemoteMethods.InitRemoteMethods() before this call?");

int argPos = 0;
var method = OmsiHookRPCMethods.RemoteMethod.TTempRVListCreate;
byte[] writeBuffer = asyncWriteBuff.Value;
//Span<byte> readBuffer = stackalloc byte[4];
(int resultPromise, TaskCompletionSource<int> promise) = CreateResultPromise();
BitConverter.TryWriteBytes(writeBuffer[(argPos)..], (int)method);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], resultPromise);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], classAddr);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], capacity);
lock (pipeTX)
pipeTX.Write(writeBuffer);
return await promise.Task;
}
}

public async Task<int> MakeVehicle(string path, int licensePlateIndex = -1, bool randomLicensePlate = true,
int type = 0, int groupHof = 0, int tour = 0, int line = 0, bool scheduled = false, bool aiRoadVehicle = false,
int farbschema = 0, bool randomFarbschema = true)
{
var vehList = TempRVListCreate(0x0074802C, 1);
//string path = @"Vehicles\GPM_MAN_LionsCity_M\MAN_A47.bus";
var pathAddr = memory.AllocateString(path, false);
await Task.WhenAll(vehList, pathAddr);
int progManAddr = memory.ReadMemory<int>(0x00862f28);
int roadVehicleTypes = memory.ReadMemory<int>(0x008615A8);

if (localPlugin)
{
return TProgManMakeVehicle(progManAddr, vehList.Result,
roadVehicleTypes, false, false,
0, false, false, false, false,
licensePlateIndex, true, 0, (byte)3, false,
groupHof, type, tour, line, farbschema, scheduled,
aiRoadVehicle, randomLicensePlate, randomFarbschema, pathAddr.Result);
}
else
{
if (!IsInitialised)
throw new NotInitialisedException("OmsiHook RPC plugin is not connected! Did you make sure to call OmsiRemoteMethods.InitRemoteMethods() before this call?");

int argPos = 0;
var method = OmsiHookRPCMethods.RemoteMethod.TProgManMakeVehicle;
byte[] writeBuffer = asyncWriteBuff.Value;
(int resultPromise, TaskCompletionSource<int> promise) = CreateResultPromise();
BitConverter.TryWriteBytes(writeBuffer[(argPos)..], (int)method);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], resultPromise);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], progManAddr);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], vehList.Result);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], roadVehicleTypes);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], false);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], false);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], 0);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], false);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], false);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], false);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], false);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], licensePlateIndex);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], true);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], 0);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], scheduled);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], 0);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], 3);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], false);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], groupHof);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], type);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], tour);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], line);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], farbschema);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], scheduled);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], aiRoadVehicle);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], randomLicensePlate);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], randomFarbschema);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], pathAddr.Result);
lock (pipeTX)
pipeTX.Write(writeBuffer);
return promise.Task.Result;
//lock (pipeRX)
// pipeRX.Read(readBuffer);
//return BitConverter.ToInt32(readBuffer);
}
}

public async Task CloseRPCSession(bool killAllConnections)
Expand Down Expand Up @@ -176,6 +254,7 @@ public int PlaceRandomBus(int aiType = 0, int group = 1, int type = -1, bool sch
(int resultPromise, TaskCompletionSource<int> promise) = CreateResultPromise();
BitConverter.TryWriteBytes(writeBuffer[(argPos)..], (int)method);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], resultPromise);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], memory.ReadMemory<int>(0x00862f28));
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], aiType);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], group);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], 0);
Expand All @@ -184,7 +263,6 @@ public int PlaceRandomBus(int aiType = 0, int group = 1, int type = -1, bool sch
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], type);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 1)..], scheduled);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], 0);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], aiType);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], tour);
BitConverter.TryWriteBytes(writeBuffer[(argPos += 4)..], line);
lock (pipeTX)
Expand Down