예제 #1
0
파일: Client.cs 프로젝트: Sequenta/MailChat
        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);
        }
예제 #2
0
        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);
                }
            }
        }
예제 #3
0
파일: Server.cs 프로젝트: Sequenta/MailChat
        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);
        }
예제 #4
0
 /// <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);
         }
 }
예제 #5
0
 public void Dispose()
 {
     thisThreadRefCount--;
     if (thisThreadRefCount == 0)
     {
         WinFunctions.SetTlsValues(thisThreadTls, thisThreadSavedValues, Offsets);
     }
 }
예제 #6
0
        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.");
            }
        }
예제 #7
0
        public static UsingTls Scope()
        {
            if (thisThreadRefCount == 0)
            {
                EnsureTlsPointers();

                WinFunctions.GetTlsValues(thisThreadTls, thisThreadSavedValues, Offsets);
                WinFunctions.CopyTlsValues(mainThreadTls, thisThreadTls, Offsets);
            }
            thisThreadRefCount++;
            return(default);
예제 #8
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!disposed)
                {
                    WinFunctions.FreeLibrary(dllHandle);
                }

                disposed = true;
            }
        }
예제 #9
0
 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();
         }
     }
 }
예제 #10
0
 private void CloseMailslot()
 {
     if (!IsValid())
     {
         return;
     }
     if (!WinFunctions.CloseHandle(mailslotHandle))
     {
         WinFunctions.ThrowException("Не удалось вызвать API функцию CloseHandle");
     }
     mailslotHandle = InvalidHandle;
 }
예제 #11
0
        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];
            }
        }
예제 #12
0
 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));
 }
예제 #13
0
        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);
        }
예제 #14
0
        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);
예제 #15
0
 protected virtual void CancelEditorCommand()
 {
     CppFunFactory.GetSingle().CancelEditorCommand(WinFunctions.GetParent(this.Handle));
 }
예제 #16
0
파일: Server.cs 프로젝트: Sequenta/MailChat
 public Server(string name)
 {
     mailslot        = new Mailslot(name);
     mailslot.Handle = WinFunctions.CreateMailslot(mailslot.Name, 0, 0, IntPtr.Zero);
 }
예제 #17
0
        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();
            }
        }
예제 #18
0
        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();
            }
        }
예제 #19
0
 private void OnAddButtonTabPageHandleCreated(object sender, EventArgs e)
 {
     WinFunctions.SendMessage(tabControl.Handle, WinFunctions.TCM_SETMINTABWIDTH, IntPtr.Zero, (IntPtr)20);
 }
예제 #20
0
 protected virtual void CompleteEditorCommand(bool restoreDialogs = true)
 {
     CppFunFactory.GetSingle().CompleteEditorCommand(WinFunctions.GetParent(this.Handle), restoreDialogs);
 }
예제 #21
0
 public void Dispose()
 {
     WinFunctions.UnhookWindowsHookEx(hookID);
 }