mirror of
https://gitea.tendokyu.moe/TeamTofuShop/segatools.git
synced 2026-09-27 12:01:59 +03:00
This adds full support for the Taisen series of games, namely Sangokushi Taisen and Eiketsu Taisen. Games added: * Sangokushi Taisen (SDDD) * Eiketsu Taisen (SDGY) Devices added: * CHC-320 printer (SGT) * "Printer camera" (SGT, unsure what this actually really is) * CX-7000 printer (EKT) * Y3CR BD SIE F720MM (SGT, EKT) Notable changes in the codebase: * Renamed everything printer specific to seperate between CHC and CX. * Many new function and registry hooks were added across the board. * An error is now logged when segatools.ini (or the path in `SEGATOOLS_CONFIG_PATH`) cannot be found. * Netenv now redirects UDP broadcasts targeted at the subnet that is specified in the keychip configuration. The terminal announces it's presence by broadcasting UDP to 192.168.189.255, this will be redirected to 255.255.255.255. * Vfs now seperates between absolute and relative paths in `vfs_fixup_path` via an environment variable called `SEGATOOLS_VFS_RELATIVE_PATH`. This is needed because amcapture accesses files as workingdirectory-relative. * The Y3 board emulation has support for external Y3 I/O dlls. The default implementation (y3ws) that comes with this is a websocket implementation. The docs are available under `doc\y3ws.txt` and a sample card player .html file is under `dist\ekt\card_player.html`. I already know one person that is hosting a massively improved version of it. * For websockets, my own websocket implementation is used as a subproject (MIT license): https://github.com/akechi-haruka/cwinwebsocket * For JSON, cJSON was embedded (MIT license): https://github.com/DaveGamble/cJSON * y3ws reads all printed cards from `DEVICE\print` by default including card back sides. It's up to the client to merge or skip them. Remarks: * SGT takes ~8 minutes to load. This seems to be intentional. * SGT uses some weird TCP network implementation like IDZ. I have not bothered reversing that yet and I have confirmed everything working from the test menu. * EKT will throw a network error if no terminal is found. You must run the terminal on another computer to be able to launch the satellite. * EKT has a very bizzare speed glitch that will speed up the ingame unit movement by several 1000% and also slows down cutscene animations by 90%. When this effect is active, you also take a ton more damage than usual. I do not know what causes this and it seems PC specific. * EKT is very stutter sensitive and will throw error 6401 (I/O timeout) at random when trying to alt+tab or have other things running. * EKT features a livestream system called Enbu (or "Dojo Upload"). While you are in a match, regardless of vs. AI or another player, the game will record your screen and live-stream it to the Enbu server as defined by the game server's startup response. The application responsible for that, "AM Capture" will not limit it's recording to the game window, but also anything that overlays the game window (notifications, popups, alt+tabbed windows, web browsers, etc). Since this is live-streamed, killing the process will have no effect afterwards, as the frames showing unwanted things will already have been transmitted. To make people aware of this, a one-time dialog message will pop up when starting EKT. The flag for that is stored in the DEVICE folder. Closes #25. Co-authored-by: Dniel97 <Dniel97@noreply.gitea.tendokyu.moe> Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/85 Co-authored-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com> Co-committed-by: kyoubate-haruka <46010460+kyoubate-haruka@users.noreply.github.com>
354 lines
12 KiB
C
354 lines
12 KiB
C
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <windows.h>
|
|
#include <winsock2.h>
|
|
#include <ws2tcpip.h>
|
|
|
|
#include "3rdparty/cjson/cJSON.h"
|
|
|
|
#include "config.h"
|
|
#include "y3ws.h"
|
|
|
|
#include <wchar.h>
|
|
|
|
#include "hooklib/y3.h"
|
|
|
|
#include "winwebsocket.h"
|
|
#include "lib/base64.h"
|
|
|
|
#include "util/dprintf.h"
|
|
#include "util/env.h"
|
|
|
|
static void onopen(struct wws_connection*);
|
|
static void onclose(struct wws_connection*);
|
|
static void onmessage(struct wws_connection*, const char*, size_t);
|
|
static void onlog(const char*, ...);
|
|
|
|
#define PROTOCOL_VERSION 1
|
|
#define GAME_MAX_CARDS 32
|
|
#define MAX_CARDS 500
|
|
#define CARD_ID_LEN 5
|
|
#define OUTPUT_BUFFER_SIZE (1024 * 1024 * 5)
|
|
|
|
static struct y3ws_config cfg;
|
|
|
|
static bool is_initialized = false;
|
|
static struct CardInfo card_info[GAME_MAX_CARDS];
|
|
static int card_info_size = 0;
|
|
static CRITICAL_SECTION card_info_lock;
|
|
|
|
#pragma region y3-dll functions
|
|
uint16_t y3_io_get_api_version() {
|
|
return 0x0100;
|
|
}
|
|
|
|
HRESULT y3_io_init() {
|
|
if (is_initialized) {
|
|
return S_FALSE;
|
|
}
|
|
|
|
y3ws_config_load(&cfg, get_config_path());
|
|
|
|
memset(card_info, 0, sizeof(card_info));
|
|
InitializeCriticalSection(&card_info_lock);
|
|
|
|
wws_set_callbacks(onopen, onclose, onmessage, onlog);
|
|
wws_set_verbose(cfg.debug);
|
|
|
|
HRESULT hr = wws_start(cfg.port);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
dprintf("Y3WS: Started server on port %d\n", cfg.port);
|
|
if (cfg.debug) {
|
|
dprintf("Y3WS: WS debug logging is on\n");
|
|
}
|
|
is_initialized = true;
|
|
} else {
|
|
dprintf("Y3WS: Error starting server: %lx\n", hr);
|
|
}
|
|
|
|
return hr;
|
|
}
|
|
|
|
HRESULT y3_io_close() {
|
|
|
|
HRESULT hr = wws_stop();
|
|
|
|
DeleteCriticalSection(&card_info_lock);
|
|
|
|
is_initialized = false;
|
|
|
|
return hr;
|
|
}
|
|
|
|
HRESULT y3_io_get_cards(struct CardInfo* pCardInfo, int* numCards) {
|
|
EnterCriticalSection(&card_info_lock);
|
|
for (int i = 0; i < card_info_size; i++) {
|
|
memcpy(&pCardInfo[i], &card_info[i], sizeof(struct CardInfo));
|
|
}
|
|
*numCards = card_info_size;
|
|
LeaveCriticalSection(&card_info_lock);
|
|
return S_OK;
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region y3ws functions
|
|
static void y3ws_make_error_packet(const char* message, char* output_data, size_t* output_size) {
|
|
dprintf("Y3WS: Error: %s\n", message);
|
|
*output_size = sprintf_s((char*)output_data, *output_size, "{\"version\":%d, \"success\":false,\"error\":\"%s\"}", PROTOCOL_VERSION, message);
|
|
}
|
|
|
|
static void y3ws_make_success_packet(char* output_data, size_t* output_size) {
|
|
*output_size = sprintf_s((char*)output_data, *output_size, "{\"version\":%d, \"success\":true}", PROTOCOL_VERSION);
|
|
}
|
|
|
|
static void y3ws_read_cards(char* output_data, size_t* output_size) {
|
|
WIN32_FIND_DATAW ffd;
|
|
|
|
wchar_t path[MAX_PATH];
|
|
swprintf(path, MAX_PATH, L"%ls\\*", cfg.card_path);
|
|
|
|
HANDLE hFind = FindFirstFileW(path, &ffd);
|
|
if (INVALID_HANDLE_VALUE == hFind) {
|
|
dprintf("Y3WS: Failed to access directory: %ls (%ld)\n", path, GetLastError());
|
|
y3ws_make_error_packet("Could not read from card directory", output_data, output_size);
|
|
return;
|
|
}
|
|
|
|
cJSON* response = cJSON_CreateObject();
|
|
cJSON* pversion = cJSON_CreateNumber(PROTOCOL_VERSION);
|
|
cJSON_AddItemToObject(response, "version", pversion);
|
|
cJSON* success = cJSON_CreateBool(true);
|
|
cJSON_AddItemToObject(response, "success", success);
|
|
cJSON* cards = cJSON_CreateArray();
|
|
cJSON_AddItemToObject(response, "cards", cards);
|
|
|
|
int count = 0;
|
|
do {
|
|
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
continue;
|
|
}
|
|
|
|
swprintf(path, MAX_PATH, L"%ls\\%ls", cfg.card_path, ffd.cFileName);
|
|
|
|
HANDLE hImage = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
if (hImage == INVALID_HANDLE_VALUE) {
|
|
dprintf("Y3WS: Error opening %ls: %ld\n", path, GetLastError());
|
|
continue;
|
|
}
|
|
DWORD ret = SetFilePointer(hImage, -CARD_ID_LEN, NULL, FILE_END);
|
|
if (ret == INVALID_SET_FILE_POINTER) {
|
|
dprintf("Y3WS: Error seeking in %ls: %ld\n", path, GetLastError());
|
|
continue;
|
|
}
|
|
uint8_t buf[CARD_ID_LEN];
|
|
DWORD bytesRead = 0;
|
|
if (!ReadFile(hImage, &buf, CARD_ID_LEN, &bytesRead, NULL) || bytesRead != CARD_ID_LEN) {
|
|
dprintf("Y3WS: Error reading card ID from %ls: %ld\n", path, GetLastError());
|
|
continue;
|
|
}
|
|
|
|
cJSON* card = cJSON_CreateObject();
|
|
cJSON_AddItemToArray(cards, card);
|
|
|
|
// cJSON isn't wide...
|
|
char fpatha[MAX_PATH];
|
|
size_t fpatha_len = 0;
|
|
wcstombs_s(&fpatha_len, fpatha, MAX_PATH, ffd.cFileName, MAX_PATH);
|
|
fpatha[fpatha_len] = 0;
|
|
|
|
// ReSharper disable once CppRedundantCastExpression
|
|
cJSON* path_str = cJSON_CreateString(fpatha);
|
|
cJSON_AddItemToObject(card, "path", path_str);
|
|
cJSON* card_id = cJSON_CreateNumber((double)((uint64_t)buf[0] >> 32 | (uint64_t)buf[1] >> 24 | (uint64_t)buf[2] >> 16 | (uint64_t)buf[3] >> 8 | buf[4]));
|
|
cJSON_AddItemToObject(card, "card_id", card_id);
|
|
|
|
} while (FindNextFileW(hFind, &ffd) != 0 && count++ < MAX_CARDS);
|
|
|
|
cJSON_PrintPreallocated(response, output_data, (int)*output_size, false);
|
|
*output_size = strlen(output_data);
|
|
|
|
dprintf("Y3WS: Sent %d card(s) to the client\n", count);
|
|
|
|
cJSON_Delete(response);
|
|
FindClose(hFind);
|
|
}
|
|
|
|
static void y3ws_read_card_image(char* file_name, char* output_data, size_t* output_size) {
|
|
wchar_t path[MAX_PATH];
|
|
swprintf(path, MAX_PATH, L"%ls\\%hs", cfg.card_path, file_name);
|
|
|
|
HANDLE hImage = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
if (hImage == INVALID_HANDLE_VALUE) {
|
|
dprintf("Y3WS: Failed to access file: %ls (%ld)\n", path, GetLastError());
|
|
y3ws_make_error_packet("Could not read file", output_data, output_size);
|
|
return;
|
|
}
|
|
|
|
DWORD size = GetFileSize(hImage, NULL);
|
|
if (size == INVALID_FILE_SIZE) {
|
|
dprintf("Y3WS: Failed to access file size: %ls (%ld)\n", path, GetLastError());
|
|
y3ws_make_error_packet("Could not read file", output_data, output_size);
|
|
return;
|
|
}
|
|
|
|
uint8_t* buf = malloc(size);
|
|
size_t b64size = ((size * 4) / 3) + (size / 96) + 6;
|
|
char* b64buf = malloc(b64size);
|
|
if (buf == NULL) {
|
|
dprintf("Y3WS: Allocation error for file: %ls (%ld)\n", path, GetLastError());
|
|
y3ws_make_error_packet("Could not read file", output_data, output_size);
|
|
goto end;
|
|
}
|
|
|
|
DWORD bytesRead = 0;
|
|
if (!ReadFile(hImage, buf, size, &bytesRead, NULL) || bytesRead != size) {
|
|
dprintf("Y3WS: File read failed: %ls (%ld)\n", path, GetLastError());
|
|
y3ws_make_error_packet("Could not read file", output_data, output_size);
|
|
goto end;
|
|
}
|
|
|
|
b64size = base64_encode(buf, size, b64buf);
|
|
|
|
if (b64size + 100 > *output_size) {
|
|
dprintf("Y3WS: Encoded file size exceeds buffer: %ls (%llu / %llu)\n", path, (uint64_t) b64size, (uint64_t) *output_size);
|
|
y3ws_make_error_packet("File too large", output_data, output_size);
|
|
}
|
|
|
|
*output_size = sprintf_s((char*)output_data, *output_size, "{\"version\":%d, \"success\":true,\"data\":\"%s\"}", PROTOCOL_VERSION, (char*)b64buf);
|
|
end:
|
|
free(buf);
|
|
free(b64buf);
|
|
}
|
|
|
|
static void y3ws_set_cards_from_json(const cJSON* cards, char* output_data, size_t* output_size) {
|
|
|
|
const cJSON* card = NULL;
|
|
card_info_size = 0;
|
|
|
|
EnterCriticalSection(&card_info_lock);
|
|
memset(card_info, 0, sizeof(card_info));
|
|
cJSON_ArrayForEach(card, cards) {
|
|
cJSON* x = cJSON_GetObjectItemCaseSensitive(card, "x");
|
|
cJSON* y = cJSON_GetObjectItemCaseSensitive(card, "y");
|
|
cJSON* r = cJSON_GetObjectItemCaseSensitive(card, "rotation");
|
|
cJSON* id = cJSON_GetObjectItemCaseSensitive(card, "card_id");
|
|
|
|
if (cJSON_IsNumber(x) && cJSON_IsNumber(y) && cJSON_IsNumber(r) && cJSON_IsNumber(id)) {
|
|
card_info[card_info_size].eCardStatus = VALID;
|
|
card_info[card_info_size].fX = (float)x->valuedouble;
|
|
card_info[card_info_size].fY = (float)y->valuedouble;
|
|
card_info[card_info_size].fAngle = (float)r->valuedouble;
|
|
card_info[card_info_size].uID = id->valueint;
|
|
} else {
|
|
dprintf("Y3WS: Invalid object in card array at index %d\n", card_info_size);
|
|
}
|
|
|
|
if (++card_info_size >= GAME_MAX_CARDS) {
|
|
dprintf("Y3WS: too many cards specified, truncating!\n");
|
|
break;
|
|
}
|
|
|
|
}
|
|
LeaveCriticalSection(&card_info_lock);
|
|
y3ws_make_success_packet(output_data, output_size);
|
|
}
|
|
|
|
static void y3ws_handle(const char* input_data, uint32_t input_length, char* output_data, size_t* output_size) {
|
|
cJSON* json = cJSON_ParseWithLength(input_data, input_length);
|
|
if (json == NULL) {
|
|
const char *error_ptr = cJSON_GetErrorPtr();
|
|
|
|
dprintf("Y3WS: Invalid JSON received!\n");
|
|
dprintf("Y3WS: Message was: %s\n", input_data);
|
|
dprintf("Y3WS: Error at: %s\n", error_ptr);
|
|
y3ws_make_error_packet("Invalid JSON", output_data, output_size);
|
|
goto end;
|
|
}
|
|
|
|
const cJSON* ver = cJSON_GetObjectItemCaseSensitive(json, "version");
|
|
const cJSON* cmd = cJSON_GetObjectItemCaseSensitive(json, "command");
|
|
|
|
if (!cJSON_IsNumber(ver) || ver->valueint <= 0) {
|
|
y3ws_make_error_packet("Missing version attribute", output_data, output_size);
|
|
goto end;
|
|
}
|
|
if (!cJSON_IsString(cmd)) {
|
|
y3ws_make_error_packet("Missing command attribute", output_data, output_size);
|
|
goto end;
|
|
}
|
|
if (ver->valueint != PROTOCOL_VERSION) {
|
|
y3ws_make_error_packet("Incompatible version", output_data, output_size);
|
|
goto end;
|
|
}
|
|
|
|
if (strcmp(cmd->valuestring, "ping") == 0) {
|
|
y3ws_make_success_packet(output_data, output_size);
|
|
} else if (strcmp(cmd->valuestring, "get_game_id") == 0) {
|
|
*output_size = sprintf_s((char*)output_data, *output_size, "{\"version\":%d, \"success\":true,\"game_id\":\"%s\"}", PROTOCOL_VERSION, cfg.game_id);
|
|
} else if (strcmp(cmd->valuestring, "get_cards") == 0) {
|
|
y3ws_read_cards(output_data, output_size);
|
|
} else if (strcmp(cmd->valuestring, "get_card_image") == 0) {
|
|
const cJSON* path = cJSON_GetObjectItemCaseSensitive(json, "path");
|
|
if (cJSON_IsString(path)) {
|
|
y3ws_read_card_image(path->valuestring, output_data, output_size);
|
|
} else {
|
|
y3ws_make_error_packet("Missing attribute", output_data, output_size);
|
|
}
|
|
} else if (strcmp(cmd->valuestring, "set_field") == 0) {
|
|
const cJSON* cards = cJSON_GetObjectItemCaseSensitive(json, "cards");
|
|
if (cJSON_IsArray(cards)) {
|
|
y3ws_set_cards_from_json(cards, output_data, output_size);
|
|
} else {
|
|
y3ws_make_error_packet("Missing attribute", output_data, output_size);
|
|
}
|
|
} else {
|
|
y3ws_make_error_packet("Unknown command", output_data, output_size);
|
|
}
|
|
|
|
end:
|
|
cJSON_Delete(json);
|
|
}
|
|
#pragma endregion
|
|
|
|
#pragma region websocket callbacks
|
|
|
|
static void onopen(struct wws_connection* client) {
|
|
dprintf("Y3WS: Connection opened, addr: %s\n", client->ip_str);
|
|
}
|
|
|
|
static void onclose(struct wws_connection* client) {
|
|
dprintf("Y3WS: Connection closed, addr: %s\n", client->ip_str);
|
|
}
|
|
|
|
static void onmessage(struct wws_connection* client, const char* msg, size_t size) {
|
|
if (cfg.debug) {
|
|
dprintf("Y3WS: Message: %.*s\n", (int)size, msg);
|
|
}
|
|
char* out_buf = malloc(OUTPUT_BUFFER_SIZE);
|
|
if (out_buf == NULL) {
|
|
dprintf("Y3WS: out of memory for allocating response buffer\n");
|
|
client->is_connected = false;
|
|
return;
|
|
}
|
|
size_t out_size = OUTPUT_BUFFER_SIZE;
|
|
|
|
y3ws_handle(msg, size, out_buf, &out_size);
|
|
HRESULT hr = wws_send(client, out_buf, out_size);
|
|
if (!SUCCEEDED(hr)) {
|
|
dprintf("Y3WS: Error sending message: %lx\n", hr);
|
|
}
|
|
|
|
free(out_buf);
|
|
}
|
|
|
|
static void onlog(const char* format, ...) {
|
|
va_list args;
|
|
va_start (args, format);
|
|
dprintf("Websocket: ");
|
|
dprintfv(format, args);
|
|
va_end (args);
|
|
}
|
|
|
|
#pragma endregion |