Skip to content

A Cyberpunk 2077 plugin to connect/send/read messages with a remote server over TCP.

License

Notifications You must be signed in to change notification settings

rayshader/cp2077-red-socket

Repository files navigation

RedSocket

Cyberpunk 2077 GitHub License Donate

This plugin allows to connect to a remote TCP server. It is mainly intended for research purpose. It allows to read and write commands. As an example, it can be used with a debugger (with its own server script plugin) to send commands (e.g. to add breakpoints, watch values, ...). It includes an easy-to-use API for CET.

Important

This implementation is not secure and don't provide any kind of security when sending and reading messages. Only use this tool at your own risks, with your own TCP server on a local network.

Getting started

Compatibility

Installation

  1. Install requirements:
  1. Extract the latest archive into the Cyberpunk 2077 directory.

Demo

Screenshot of CET example

Usage

This plugin can be used with redscript and CET. It allows any length of commands to be sent and received.

Note

Don't include \r\n in a command, it is used internally. If you do, command will be ignored.

Redscript

You need to create a RedSocket.Socket:

import RedSocket.*

let socket: ref<Socket> = Socket.Create();

Note

Creating a socket with new Socket() will not work. Only the method above can be used in order to poll events of the socket in background.

Register listener with callbacks for commands/connection/disconnection.

let object: ref<IScriptable>;

socket.RegisterListener(object, n"OnCommand", n"OnConnection", n"OnDisconnection");

Note

You must register a listener before calling Connect. Any attempts of connection without a listener will be ignored.

Last argument is optional, declare it if you want to be notified when connection is closed by the remote server.

Connect to a server:

socket.Connect("127.0.0.1", 2077);

// in class of `object` above
public cb func OnConnection(status: Int32) {
    if status != 0 {
        FTLog(s"Failed to connect on 127.0.0.1:2077");
        // See in red4ext/logs/ for more details.
        return;
    }
    FTLog(s"Ready to read/write commands.");
}

Send a message:

socket.SendCommand("chat Hello world!");

Your callback for incoming commands will be executed when commands are fully received.

// in class of `object` above
public cb func OnCommand(command: String) {
    FTLog(s"Received command: \(command)");
}

Disconnect from the server:

socket.Disconnect();

Your callback for disconnection will be executed whenever the remote server closes the connection, or when you close the socket on your end.

// in class of `object` above
public cb func OnDisconnection() {
    FTLog(s"Connection is closed.");
}

When you don't need the socket anymore:

Socket.Destroy(socket);

Note

Usually, a Socket.Create call should be followed by a Socket.Destroy call. In any-case, the plugin will try to disconnect and dispose of remaining sockets when the game shuts down.

Cyber Engine Tweaks

Import API of this plugin:

local RedSocket = GetMod("RedSocket")

if RedSocket == nil then
    print("RedSocket is not installed.")
    return
end

Create a socket:

local socket = RedSocket.createSocket()

Register listener with callbacks:

---@param command string
local function OnCommand(command)
    print("Command: " .. command)
end

---@param status integer
local function OnConnection(status)
    if status ~= 0 then
        print("Failed to connect to server.")
        -- See in red4ext/logs/ for more details.
        return
    end
    print("Ready to read/write commands.")
end

local function OnDisconnection()
    print("Connection is closed.")
end

socket:RegisterListener(OnCommand, OnConnection, OnDisconnection)

Connect to a server:

socket:Connect("127.0.0.1", 2077)

Send a command:

socket:SendCommand("chat Hello world!")

Disconnect from server:

socket:Disconnect()

Tip

You should close sockets based on your usage. In any case, CET plugin keeps track of sockets you created. On shutdown event, it will try to disconnect them.

Development

Contributions are welcome, feel free to fill an issue or a PR.

Usage

  1. Install requirements:
  • XMake
  • Visual Studio 2022+
  • red-cli v0.4.0+
  1. Configure project with:
xmake -y
  1. Generate Visual Studio solution:
xmake project -k vsxmake
  1. Open .sln and build target RedSocket in Debug mode.

It will install scripts and plugin in game's directory, thanks to red-cli.

Release

  1. Open .sln and build target RedSocket in Release mode.

It will prepare outputs and create an archive ready to release.