private uint Send(byte[] data) { uint bytesWritten = 0; try { mailslot.Handle = WinFunctions.CreateFile(target, WinFunctions.FileDesiredAccess.GenericWrite, 0, IntPtr.Zero, WinFunctions.FileCreationDisposition.OpenExisting, 0, IntPtr.Zero); WinFunctions.WriteFile(mailslot.Handle, data, (uint)data.Length, out bytesWritten, IntPtr.Zero); } catch (Exception ex) { WinFunctions.ThrowException("Send failed.", ex); } finally { if (mailslot.IsValid()) { mailslot.Dispose(); } } return(bytesWritten); }
public static void OnLoad() { if (!(GameFunctions.Init() && GameMemory.Init() && GameOffsets.Init())) { Game.DisplayNotification($"~r~[ERROR] Spotlight: ~s~Failed to initialize, unloading..."); Game.LogTrivial($"[ERROR] Failed to initialize, unloading..."); Game.UnloadActivePlugin(); } WinFunctions.CopyTlsValues(WinFunctions.GetProcessMainThreadId(), WinFunctions.GetCurrentThreadId(), GameOffsets.TlsAllocator0, GameOffsets.TlsAllocator1, GameOffsets.TlsAllocator2); bool on = false; Ped p = Game.LocalPlayer.Character; ulong shadowId = Render.GenerateShadowId(); while (true) { GameFiber.Yield(); if (Game.IsKeyDown(System.Windows.Forms.Keys.NumPad0)) { on = !on; } if (p && on) { Game.DisplayHelp("Light: ~g~On"); Render.SpotLight(p.GetOffsetPositionFront(1), p.Direction, eLightFlags.CanRenderUnderground | eLightFlags.EnableVolume | eLightFlags.DisableSpecular, 30, 45, 0.06f, 0.175f, 45, 5, 8.25f, System.Drawing.Color.Red, shadowId); } } }
public uint Read(out byte[] data) { if (!mailslot.IsValid()) { throw new Exception("Mailslot handle is invalid."); } uint bytesRead = 0; try { var info = mailslot.GetMailslotInfo(); if (info.MessageCount == 0) { data = new byte[] { }; return(0); } data = new byte[info.NextMessageSize]; WinFunctions.ReadFile(mailslot.Handle, data, (uint)data.Length, out bytesRead, IntPtr.Zero); } catch (Exception ex) { if (!IsRunning) { data = new byte[] {}; return(bytesRead); } data = new byte[] { }; WinFunctions.ThrowException("Read failed.", ex); } return(bytesRead); }
/// <summary> /// Sets a hook. /// </summary> public MouseHook() { proc = new HookHandlerDelegate(HookCallback); using (Process currentP = Process.GetCurrentProcess()) using (ProcessModule currentModule = currentP.MainModule) { hookID = WinFunctions.SetWindowsHookEx(14, proc, WinFunctions.GetModuleHandle(currentModule.ModuleName), 0); } }
public void Dispose() { thisThreadRefCount--; if (thisThreadRefCount == 0) { WinFunctions.SetTlsValues(thisThreadTls, thisThreadSavedValues, Offsets); } }
private void LoadDll(string dllPath) { Guard.AgainstNullOrEmptyOrWhiteSpaceString(dllPath, nameof(dllPath)); dllHandle = WinFunctions.LoadLibrary(dllPath); if (dllHandle == IntPtr.Zero) { throw new MpvAPIException("Failed to load Mpv DLL. .NET apps by default are 32-bit so make sure you're loading the 32-bit DLL."); } }
public static UsingTls Scope() { if (thisThreadRefCount == 0) { EnsureTlsPointers(); WinFunctions.GetTlsValues(thisThreadTls, thisThreadSavedValues, Offsets); WinFunctions.CopyTlsValues(mainThreadTls, thisThreadTls, Offsets); } thisThreadRefCount++; return(default);
protected virtual void Dispose(bool disposing) { if (disposing) { if (!disposed) { WinFunctions.FreeLibrary(dllHandle); } disposed = true; } }
private static void UpdateSpotlights() { WinFunctions.CopyTlsValues(WinFunctions.GetProcessMainThreadId(), WinFunctions.GetCurrentThreadId(), GameMemory.TlsAllocatorOffset0, GameMemory.TlsAllocatorOffset1, GameMemory.TlsAllocatorOffset2); while (true) { GameFiber.Yield(); for (int i = 0; i < spotlights.Count; i++) { spotlights[i]?.Update(); } } }
private void CloseMailslot() { if (!IsValid()) { return; } if (!WinFunctions.CloseHandle(mailslotHandle)) { WinFunctions.ThrowException("Не удалось вызвать API функцию CloseHandle"); } mailslotHandle = InvalidHandle; }
private static void EnsureTlsPointers() { if (mainThreadTls == IntPtr.Zero) { mainThreadTls = WinFunctions.GetTlsPointer(WinFunctions.GetProcessMainThreadId()); } if (thisThreadTls == IntPtr.Zero) { thisThreadTls = WinFunctions.GetTlsPointer(WinFunctions.GetCurrentThreadId()); thisThreadSavedValues = new long[Offsets.Length]; } }
private IntPtr HookCallback(int nCode, IntPtr wParam, ref MSLLHOOKSTRUCT lParam) { if (nCode >= 0) { if (wParam == (IntPtr)WM_LBUTTONDOWN) { OnMouseDownEvent(new MouseHookEventArgs(lParam.pt)); } if (wParam == (IntPtr)WM_LBUTTONUP) { OnMouseUpEvent(new MouseHookEventArgs(lParam.pt)); } } //Pass information to next application return(WinFunctions.CallNextHookEx(hookID, nCode, wParam, ref lParam)); }
public static WinFunctions.MailslotInfo GetMailslotInfo(IntPtr mailslotHandle) { var info = new WinFunctions.MailslotInfo(); var result = WinFunctions.GetMailslotInfo( mailslotHandle, ref info.MaxMessageSize, ref info.NextMessageSize, ref info.MessageCount, ref info.ReadTimeout ); if (!result) { WinFunctions.ThrowException("Не удалось вызвать API функцию GetMailslotInfo"); } return(info); }
public static UsingTls Scope() { if (thisThreadRefCount == 0) { if (mainThreadTls == IntPtr.Zero) { mainThreadTls = WinFunctions.GetTlsPointer(WinFunctions.GetProcessMainThreadId()); } if (thisThreadTls == IntPtr.Zero) { thisThreadTls = WinFunctions.GetTlsPointer(WinFunctions.GetCurrentThreadId()); } WinFunctions.GetTlsValues(thisThreadTls, thisThreadSavedValues, Offsets); WinFunctions.CopyTlsValues(mainThreadTls, thisThreadTls, Offsets); } thisThreadRefCount++; return(default);
protected virtual void CancelEditorCommand() { CppFunFactory.GetSingle().CancelEditorCommand(WinFunctions.GetParent(this.Handle)); }
public Server(string name) { mailslot = new Mailslot(name); mailslot.Handle = WinFunctions.CreateMailslot(mailslot.Name, 0, 0, IntPtr.Zero); }
private static void Main() { while (Game.IsLoading) { GameFiber.Sleep(500); } if (!Directory.Exists(@"Plugins\Spotlight Resources\")) { Directory.CreateDirectory(@"Plugins\Spotlight Resources\"); } Settings = new Settings(@"Plugins\Spotlight Resources\General.ini", @"Plugins\Spotlight Resources\Offsets.ini", @"Plugins\Spotlight Resources\VisualSettings.xml", true); LoadSpotlightControllers(); bool gameFnInit = GameFunctions.Init(); bool gameMemInit = GameMemory.Init(); if (gameFnInit) { Game.LogTrivialDebug($"Successful {nameof(GameFunctions)} init"); } if (gameMemInit) { Game.LogTrivialDebug($"Successful {nameof(GameMemory)} init"); } if (!gameFnInit || !gameMemInit) { string str = ""; if (!gameFnInit) { str += nameof(GameFunctions); if (!gameMemInit) { str += " and "; str += nameof(GameMemory); } } else if (!gameMemInit) { str += nameof(GameMemory); } Game.DisplayNotification($"~r~[ERROR] Spotlight: ~s~Failed to initialize {str}, unloading..."); Game.LogTrivial($"[ERROR] Failed to initialize {str}, unloading..."); Game.UnloadActivePlugin(); } BaseSpotlight.CoronaPositionPtr = (NativeVector3 *)Game.AllocateMemory(sizeof(NativeVector3) * 2); BaseSpotlight.CoronaDirectionPtr = BaseSpotlight.CoronaPositionPtr++; // when the queue array that the GetFreeLightDrawDataSlotFromQueue function accesses is full, // it uses the TLS to get an allocator to allocate memory for a bigger array, // therefore we copy the allocator pointers from the main thread TLS to our current thread TLS. WinFunctions.CopyTlsValues(WinFunctions.GetProcessMainThreadId(), WinFunctions.GetCurrentThreadId(), GameMemory.TlsAllocatorOffset0, GameMemory.TlsAllocatorOffset1, GameMemory.TlsAllocatorOffset2); while (true) { GameFiber.Yield(); Update(); } }
private static void Main() { PluginState.Init(); PluginState.IsLoaded = true; while (Game.IsLoading) { GameFiber.Sleep(500); } if (!Directory.Exists(@"Plugins\Spotlight Resources\")) { Directory.CreateDirectory(@"Plugins\Spotlight Resources\"); } // let's keep using the Offsets.ini file for now //string vehSettingsFile = @"Plugins\Spotlight Resources\VehiclesSettings.xml"; //if (!File.Exists(vehSettingsFile) && File.Exists(@"Plugins\Spotlight Resources\Offsets.ini")) //{ // // legacy // vehSettingsFile = @"Plugins\Spotlight Resources\Offsets.ini"; //} Settings = new Settings(@"Plugins\Spotlight Resources\General.ini", @"Plugins\Spotlight Resources\Offsets.ini", @"Plugins\Spotlight Resources\VisualSettings.xml", true); LoadSpotlightControllers(); if (!(GameFunctions.Init() && GameMemory.Init() && GameOffsets.Init())) { Game.DisplayNotification($"~r~[ERROR] Spotlight: ~s~Failed to initialize, unloading..."); Game.LogTrivial($"[ERROR] Failed to initialize, unloading..."); Game.UnloadActivePlugin(); } if (Settings.EnableLightEmissives) { VehiclesUpdateHook.Hook(); } // when the queue array that the GetFreeLightDrawDataSlotFromQueue function accesses is full, // it uses the TLS to get an allocator to allocate memory for a bigger array, // therefore we copy the allocator pointers from the main thread TLS to our current thread TLS. WinFunctions.CopyTlsValues(WinFunctions.GetProcessMainThreadId(), WinFunctions.GetCurrentThreadId(), GameOffsets.TlsAllocator); if (Settings.EnableLightEmissives) { // TODO: find something better than this vehicles update hook to override the extralight emissives values // This function may execute multiple times per tick, which is not optimal VehiclesUpdateHook.VehiclesUpdate += OnVehiclesUpdate; } Game.LogTrivial("Initialized"); #if DEBUG bool f = false; #endif while (true) { GameFiber.Yield(); #if DEBUG if (Game.LocalPlayer.Character.CurrentVehicle) { if (Game.IsKeyDown(System.Windows.Forms.Keys.Y)) { Game.LocalPlayer.Character.CurrentVehicle.IsPositionFrozen = f = !f; } else if (Game.IsKeyDown(System.Windows.Forms.Keys.D7)) { Game.LocalPlayer.Character.CurrentVehicle.Rotation = new Rotator(45.0f, 0.0f, 0.0f); } else if (Game.IsKeyDown(System.Windows.Forms.Keys.D8)) { Game.LocalPlayer.Character.CurrentVehicle.Rotation = new Rotator(0.0f, 45.0f, 0.0f); } else if (Game.IsKeyDown(System.Windows.Forms.Keys.D9)) { Game.LocalPlayer.Character.CurrentVehicle.Rotation = new Rotator(0.0f, 0.0f, 45.0f); } else if (Game.IsKeyDown(System.Windows.Forms.Keys.D0)) { Game.LocalPlayer.Character.CurrentVehicle.Rotation = Rotator.Zero; } } #endif Update(); } }
private void OnAddButtonTabPageHandleCreated(object sender, EventArgs e) { WinFunctions.SendMessage(tabControl.Handle, WinFunctions.TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)20); }
protected virtual void CompleteEditorCommand(bool restoreDialogs = true) { CppFunFactory.GetSingle().CompleteEditorCommand(WinFunctions.GetParent(this.Handle), restoreDialogs); }
public void Dispose() { WinFunctions.UnhookWindowsHookEx(hookID); }