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

Add linux port #135

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ crashlytics-build.properties
fabric.properties

server/packages/**
*.exe
*.exe

# Linux stuff
linux/plugins/ets2-telemetry-lin.so
linux/plugins/read_util
linux/server/read_util
favourites.json*
__pycache__/
28 changes: 28 additions & 0 deletions linux/plugins/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
SDK_HEADERS=\
../sdk/include/*.h \
../sdk/include/common/*.h \
../sdk/include/amtrucks/*.h \
../sdk/include/eurotrucks2/*.h

SDK_INCLUDES=\
-I../sdk/include \
-I../sdk/include/common/ \
-I../sdk/include/amtrucks/ \
-I../sdk/include/eurotrucks2

CLEANABLE=*.so read_util dummy

all: ets2-telemetry-lin.so read_util dummy

ets2-telemetry-lin.so: plugin.cpp $(SDK_HEADERS) defs.h
g++ -O2 -o $@ -fPIC -Wall --shared -Wl,-soname,$@ $(SDK_INCLUDES) plugin.cpp -lrt

read_util: read_util.cpp $(SDK_HEADERS) defs.h
g++ -O2 -o $@ -Wall $(SDK_INCLUDES) read_util.cpp -lrt

dummy: dummy.cpp $(SDK_HEADERS) defs.h
g++ -O2 -o $@ -Wall $(SDK_INCLUDES) dummy.cpp -lrt

.PHONY: clean
clean:
@rm -f -- $(CLEANABLE)
31 changes: 31 additions & 0 deletions linux/plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# ETS2 Local Radio Linux port

## Installation instructions

To build and install the plugin and utility script, you will need GCC and Make (these come standard with a lot of distributions).

In this directory, run the command:

```bash
make all && ./install.sh
```

If you get errors, you probably need to install a dependency.

You will need to copy the plugin `ets2-telemetry-lin.so` manually to your plugins directory if ETS is not installed via Steam on your main partition, or if you are using ATS.

## Running

Go to the `server` directory and run:

```bash
python3 main.py
```

This will by default run at `localhost:3141`, but feel free to change the port by adding the `--port` flag.

Then just visit `localhost:3141` (or your specified port) to view the web interface!

## Development

The `dummy` allows you to send fake data to the shared memory, for testing purposes.
20 changes: 20 additions & 0 deletions linux/plugins/defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __DEFS__
#define __DEFS__

// SDK
#include "scssdk_telemetry.h"
#include "eurotrucks2/scssdk_eut2.h"
#include "eurotrucks2/scssdk_telemetry_eut2.h"
#include "amtrucks/scssdk_ats.h"
#include "amtrucks/scssdk_telemetry_ats.h"

const char shm_name[] = "/ets2radiolinux";
const int NUM_PAGES = 1;

typedef struct telemetry_state_t
{
scs_value_dplacement_t world_position;
scs_value_bool_t electricity;
} telemetry_state_t;

#endif /* __DEFS__ */
Binary file added linux/plugins/dummy
Binary file not shown.
111 changes: 111 additions & 0 deletions linux/plugins/dummy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* @brief This provides dummy data to the shared memory.
*/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>

#include "defs.h"

/**
* @brief Useful telemetry data.
*/
telemetry_state_t telemetry;

/**
* @brief Opaque pointer to use for mmap
*/
void* mapped_region = NULL;
size_t MAP_SIZE = -1;


int main()
{
memset(&telemetry, 0, sizeof(telemetry));
MAP_SIZE = getpagesize();

// Open shared memory
int handle = shm_open(shm_name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (handle < 0) {
fprintf(stderr, "Local radio: could not open shared memory");
return 1;
}

if (ftruncate(handle, MAP_SIZE) < 0) {
fprintf(stderr, "Local radio: could not truncate shared memory");
return 1;
}

mapped_region = mmap(
0,
MAP_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED,
handle,
0
);

if (mapped_region == MAP_FAILED) {
fprintf(stderr, "Local radio: could not mmap shared memory");
return 1;
}

telemetry_state_t telemetry;
memset(&telemetry, 0, sizeof(telemetry_state_t));
telemetry.electricity = { 1 };

char buf[128];
while (1) {
printf("Enter location (x,y,z): ");
fgets(buf, 128, stdin);

char numBuf[32];
int offset = 0;
double coords[3];
int onCoord = 0;
char current;
for (int i = 0; i < 128; ++i) {
current = buf[i];
if (current == '\x00') {
break;
}

if (current == ',' || current == '\n') {
numBuf[i - offset] = '\x00';
coords[onCoord] = strtod(numBuf, NULL);

onCoord++;
offset = i + 1;

if (onCoord > 2) {
break;
}

continue;
}

numBuf[i - offset] = current;
}

telemetry.world_position.position = {
coords[0],
coords[1],
coords[2],
};

memcpy(mapped_region, &telemetry, sizeof(telemetry_state_t));
}

return 0;
}

void __attribute__ ((destructor)) unload(void)
{
shm_unlink(shm_name);
}
16 changes: 16 additions & 0 deletions linux/plugins/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

cp read_util "../server"

if [ ! -d "$HOME/.local/share/Steam/steamapps/common/Euro Truck Simulator 2/bin/linux_x64/" ]; then
echo "WARN: Could not find ETS2 installation! Please manually move ets2-telemetry-lin.so to your plugins directory."
echo "Installed with partial success"
exit 1
else
mkdir -p "$HOME/.local/share/Steam/steamapps/common/Euro Truck Simulator 2/bin/linux_x64/plugins"
mkdir -p "$HOME/.local/share/Steam/steamapps/common/Euro Truck Simulator 2/bin/linux_x86/plugins"
cp ets2-telemetry-lin.so "$HOME/.local/share/Steam/steamapps/common/Euro Truck Simulator 2/bin/linux_x64/plugins"
cp ets2-telemetry-lin.so "$HOME/.local/share/Steam/steamapps/common/Euro Truck Simulator 2/bin/linux_x86/plugins"
fi

echo "Installed successfully"
Loading