Created
March 25, 2022 00:30
-
-
Save z16/d70471218c5df274f3415f91f59937e3 to your computer and use it in GitHub Desktop.
Example Windower IPC implementation in C#. To use, load the `Eval` addon and simply register the IPC event with `//eval windower.register_event('ipc message', print)`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO.MemoryMappedFiles; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using static System.Console; | |
namespace WindowerIpc; | |
internal class Program { | |
private const String Character = "Arcon"; | |
private const String Addon = "eval"; | |
private const String Text = "Test"; | |
private static void Main() { | |
if (Addon.Length >= 0x20) { | |
WriteLine("Addon name too long."); | |
return; | |
} | |
if (Text.Length >= 0x1000 - 0x20 - 0x20) { | |
WriteLine("Text too long."); | |
return; | |
} | |
var pol = Process.GetProcessesByName("pol").FirstOrDefault(process => process.MainWindowTitle == Character); | |
if (pol == null) { | |
WriteLine("Target process not found."); | |
return; | |
} | |
using var mutex = new Mutex(false, $@"Local\Windower Mutex IPC"); | |
using var ev = EventWaitHandle.OpenExisting($@"Local\Windower Event {pol.Id}"); | |
using var processesMmf = MemoryMappedFile.OpenExisting(@"Local\Windower MMF Instances", MemoryMappedFileRights.ReadWrite); | |
using var messagesMmf = MemoryMappedFile.OpenExisting($@"Local\Windower MMF {pol.Id}", MemoryMappedFileRights.Write); | |
mutex.WaitOne(); | |
var processes = processesMmf.CreateViewAccessor(0, 12 * 100, MemoryMappedFileAccess.ReadWrite); | |
var (processOffset, processId, processIndex) = Enumerable.Range(0, 100) | |
.Select(index => index * 12) | |
.Select(offset => (offset, ProcessId: processes.ReadUInt32(offset), processes.ReadUInt32(offset + 0x08))) | |
.First(process => process.ProcessId == pol.Id); | |
var messages = messagesMmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Write); | |
var messageOffset = 0x1000 * (processIndex % 0x10); | |
var utf8 = Encoding.UTF8; | |
var addon = utf8.GetBytes(Addon); | |
var text = utf8.GetBytes(Text); | |
messages.WriteArray(messageOffset + 0x00, addon, 0, addon.Length); | |
messages.WriteArray(messageOffset + 0x20, addon, 0, addon.Length); | |
messages.WriteArray(messageOffset + 0x40, text, 0, text.Length); | |
processes.Write(processOffset + 0x08, processIndex + 1); | |
ev.Set(); | |
mutex.ReleaseMutex(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment