2020-07-16 17:56:24 +08:00
|
|
|
/* Copyright (C) 2019 Mr Goldberg
|
|
|
|
This file is part of the Goldberg Emulator
|
|
|
|
|
|
|
|
The Goldberg Emulator is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 3 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The Goldberg Emulator is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the Goldberg Emulator; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#ifndef __INCLUDED_COMMON_INCLUDES__
|
|
|
|
#define __INCLUDED_COMMON_INCLUDES__
|
|
|
|
|
2023-12-20 23:10:25 +08:00
|
|
|
// OS detection
|
2020-07-16 17:56:24 +08:00
|
|
|
#if defined(WIN64) || defined(_WIN64) || defined(__MINGW64__)
|
|
|
|
#define __WINDOWS_64__
|
|
|
|
#elif defined(WIN32) || defined(_WIN32) || defined(__MINGW32__)
|
|
|
|
#define __WINDOWS_32__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__WINDOWS_32__) || defined(__WINDOWS_64__)
|
|
|
|
#define __WINDOWS__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__linux__) || defined(linux)
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
#define __LINUX_64__
|
|
|
|
#else
|
|
|
|
#define __LINUX_32__
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__LINUX_32__) || defined(__LINUX_64__)
|
|
|
|
#define __LINUX__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__WINDOWS__)
|
|
|
|
#define STEAM_WIN32
|
|
|
|
#ifndef NOMINMAX
|
|
|
|
#define NOMINMAX
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2024-05-31 18:19:30 +08:00
|
|
|
// we need this otherwise 'S_API_EXPORT' will be dllimport
|
2020-07-16 17:56:24 +08:00
|
|
|
#define STEAM_API_EXPORTS
|
|
|
|
|
2023-12-20 23:10:25 +08:00
|
|
|
// C/C++ includes
|
|
|
|
#include <cstdint>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
2024-04-17 04:58:05 +08:00
|
|
|
#include <string_view>
|
2023-12-20 23:10:25 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <cctype>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <queue>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2024-01-15 01:39:19 +08:00
|
|
|
#include <filesystem>
|
|
|
|
#include <optional>
|
2023-12-20 23:10:25 +08:00
|
|
|
|
2023-12-20 10:13:31 +08:00
|
|
|
// OS specific includes + definitions
|
2020-07-16 17:56:24 +08:00
|
|
|
#if defined(__WINDOWS__)
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <processthreadsapi.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <direct.h>
|
|
|
|
#include <iphlpapi.h> // Include winsock2 before this, or winsock2 iphlpapi will be unavailable
|
|
|
|
#include <shlobj.h>
|
|
|
|
|
2023-12-20 10:13:31 +08:00
|
|
|
// we need this for BCryptGenRandom() in base.cpp
|
2023-12-14 10:29:42 +08:00
|
|
|
#include <bcrypt.h>
|
2024-05-22 10:29:20 +08:00
|
|
|
// we need non-standard S_IFDIR for Windows
|
|
|
|
#include <sys/stat.h>
|
2020-07-16 17:56:24 +08:00
|
|
|
|
2023-12-20 10:13:31 +08:00
|
|
|
#define MSG_NOSIGNAL 0
|
2020-07-16 17:56:24 +08:00
|
|
|
|
|
|
|
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
|
|
|
|
#define PATH_SEPARATOR "\\"
|
|
|
|
|
|
|
|
#ifdef EMU_EXPERIMENTAL_BUILD
|
|
|
|
#include <winhttp.h>
|
2023-12-27 15:09:44 +08:00
|
|
|
#include "detours/detours.h"
|
2020-07-16 17:56:24 +08:00
|
|
|
#endif
|
|
|
|
|
2024-01-07 21:43:53 +08:00
|
|
|
#include "crash_printer/win.hpp"
|
|
|
|
|
2021-04-26 00:44:41 +08:00
|
|
|
// Convert a wide Unicode string to an UTF8 string
|
2023-12-20 23:10:25 +08:00
|
|
|
static inline std::string utf8_encode(const std::wstring &wstr)
|
2021-04-26 00:44:41 +08:00
|
|
|
{
|
|
|
|
if( wstr.empty() ) return std::string();
|
|
|
|
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
|
|
|
|
std::string strTo( size_needed, 0 );
|
|
|
|
WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
|
|
|
|
return strTo;
|
|
|
|
}
|
|
|
|
|
2024-04-11 07:16:53 +08:00
|
|
|
// Convert UTF8 string to a wide Unicode String
|
2023-12-20 23:10:25 +08:00
|
|
|
static inline std::wstring utf8_decode(const std::string &str)
|
2021-04-26 00:44:41 +08:00
|
|
|
{
|
|
|
|
if( str.empty() ) return std::wstring();
|
|
|
|
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
|
|
|
|
std::wstring wstrTo( size_needed, 0 );
|
|
|
|
MultiByteToWideChar (CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
|
|
|
|
return wstrTo;
|
|
|
|
}
|
|
|
|
|
2023-12-20 23:10:25 +08:00
|
|
|
static inline void reset_LastError()
|
2022-08-12 08:52:02 +08:00
|
|
|
{
|
|
|
|
SetLastError(0);
|
|
|
|
}
|
|
|
|
|
2020-07-16 17:56:24 +08:00
|
|
|
#elif defined(__LINUX__)
|
2023-12-20 10:13:31 +08:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif // _GNU_SOURCE
|
|
|
|
|
2020-07-16 17:56:24 +08:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/mount.h>
|
2024-03-01 18:50:58 +08:00
|
|
|
#include <sys/stat.h>
|
2020-07-16 17:56:24 +08:00
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2022-09-03 16:20:03 +08:00
|
|
|
#include <netinet/tcp.h>
|
2020-07-16 17:56:24 +08:00
|
|
|
#include <linux/netdevice.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <utime.h>
|
|
|
|
|
2024-01-07 21:43:53 +08:00
|
|
|
#include "crash_printer/linux.hpp"
|
|
|
|
|
2020-07-16 17:56:24 +08:00
|
|
|
#define PATH_MAX_STRING_SIZE 512
|
|
|
|
#define PATH_SEPARATOR "/"
|
2021-04-26 00:44:41 +08:00
|
|
|
#define utf8_decode(a) a
|
2024-04-20 23:24:26 +08:00
|
|
|
#define utf8_encode(a) a
|
2022-08-12 08:52:02 +08:00
|
|
|
#define reset_LastError()
|
2020-07-16 17:56:24 +08:00
|
|
|
#endif
|
|
|
|
|
2023-12-20 23:10:25 +08:00
|
|
|
// Other libs includes
|
2023-12-26 04:30:38 +08:00
|
|
|
#include "json/json.hpp"
|
|
|
|
#include "utfcpp/utf8.h"
|
|
|
|
#include "controller/gamepad.h"
|
2020-07-16 17:56:24 +08:00
|
|
|
|
2024-01-15 01:39:19 +08:00
|
|
|
// common includes
|
|
|
|
#include "common_helpers/common_helpers.hpp"
|
|
|
|
|
2023-12-20 23:10:25 +08:00
|
|
|
// Steamsdk includes
|
2023-12-27 15:09:44 +08:00
|
|
|
#include "steam/steam_api.h"
|
|
|
|
#include "steam/steam_gameserver.h"
|
|
|
|
#include "steam/steamdatagram_tickets.h"
|
2020-07-16 17:56:24 +08:00
|
|
|
|
2024-04-10 16:59:51 +08:00
|
|
|
#define AS_STR(x) #x
|
|
|
|
#define EXPAND_AS_STR(x) AS_STR(x)
|
|
|
|
|
2023-12-20 10:13:31 +08:00
|
|
|
// PRINT_DEBUG definition
|
|
|
|
// notice the extra call to WSASetLastError(0) in Windows def
|
|
|
|
#ifndef EMU_RELEASE_BUILD
|
2023-12-21 09:20:32 +08:00
|
|
|
// we need this for printf specifiers for intptr_t such as PRIdPTR
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2023-12-20 10:13:31 +08:00
|
|
|
//#define PRINT_DEBUG(...) fprintf(stdout, __VA_ARGS__)
|
|
|
|
extern const std::string dbg_log_file;
|
|
|
|
extern const std::chrono::time_point<std::chrono::high_resolution_clock> startup_counter;
|
|
|
|
#if defined(__WINDOWS__)
|
2023-12-21 11:33:12 +08:00
|
|
|
#define PRINT_DEBUG(a, ...) do { \
|
|
|
|
auto __prnt_dbg_ctr = std::chrono::high_resolution_clock::now(); \
|
|
|
|
auto __prnt_dbg_duration = __prnt_dbg_ctr - startup_counter; \
|
|
|
|
auto __prnt_dbg_micro = std::chrono::duration_cast<std::chrono::duration<unsigned long long, std::micro>>(__prnt_dbg_duration); \
|
|
|
|
auto __prnt_dbg_ms = std::chrono::duration_cast<std::chrono::duration<unsigned long long, std::milli>>(__prnt_dbg_duration); \
|
|
|
|
auto __prnt_dbg_f = fopen(dbg_log_file.c_str(), "a"); \
|
2024-02-24 02:41:30 +08:00
|
|
|
if (!__prnt_dbg_f) break; \
|
2024-04-06 13:11:25 +08:00
|
|
|
fprintf(__prnt_dbg_f, "[%llu ms, %llu us] [tid %lu] %s() " a "\n", \
|
2024-05-22 08:27:47 +08:00
|
|
|
__prnt_dbg_ms.count(), __prnt_dbg_micro.count(), GetCurrentThreadId(), __FUNCTION__, ##__VA_ARGS__); \
|
2023-12-21 11:33:12 +08:00
|
|
|
fclose(__prnt_dbg_f); \
|
|
|
|
WSASetLastError(0); \
|
2023-12-20 10:13:31 +08:00
|
|
|
} while (0)
|
|
|
|
#elif defined(__LINUX__)
|
|
|
|
#include <sys/syscall.h>
|
2024-04-06 13:11:25 +08:00
|
|
|
#define PRINT_DEBUG(a, ...) do { \
|
|
|
|
auto __prnt_dbg_ctr = std::chrono::high_resolution_clock::now(); \
|
|
|
|
auto __prnt_dbg_duration = __prnt_dbg_ctr - startup_counter; \
|
|
|
|
auto __prnt_dbg_micro = std::chrono::duration_cast<std::chrono::duration<unsigned long long, std::micro>>(__prnt_dbg_duration); \
|
|
|
|
auto __prnt_dbg_ms = std::chrono::duration_cast<std::chrono::duration<unsigned long long, std::milli>>(__prnt_dbg_duration); \
|
|
|
|
auto __prnt_dbg_f = fopen(dbg_log_file.c_str(), "a"); \
|
|
|
|
if (!__prnt_dbg_f) break; \
|
2024-04-15 02:56:18 +08:00
|
|
|
fprintf(__prnt_dbg_f, "[%llu ms, %llu us] [tid %ld] %s " a "\n", \
|
|
|
|
__prnt_dbg_ms.count(), __prnt_dbg_micro.count(), syscall(SYS_gettid), __PRETTY_FUNCTION__, ##__VA_ARGS__); \
|
2024-04-06 13:11:25 +08:00
|
|
|
fclose(__prnt_dbg_f); \
|
2023-12-20 10:13:31 +08:00
|
|
|
} while (0)
|
2024-04-06 13:11:25 +08:00
|
|
|
#else
|
|
|
|
#warning "Unrecognized OS, cannot set debug print"
|
|
|
|
#define PRINT_DEBUG(...)
|
2023-12-20 10:13:31 +08:00
|
|
|
#endif
|
|
|
|
#else // EMU_RELEASE_BUILD
|
|
|
|
#define PRINT_DEBUG(...)
|
|
|
|
#endif // EMU_RELEASE_BUILD
|
|
|
|
|
2024-04-06 13:11:25 +08:00
|
|
|
// function entry
|
|
|
|
#define PRINT_DEBUG_ENTRY() PRINT_DEBUG("")
|
|
|
|
#define PRINT_DEBUG_TODO() PRINT_DEBUG("// TODO")
|
2024-05-22 10:29:20 +08:00
|
|
|
#define PRINT_DEBUG_GNU_WIN() PRINT_DEBUG("GNU/Win")
|
2024-04-06 13:11:25 +08:00
|
|
|
|
2020-07-16 17:56:24 +08:00
|
|
|
// Emulator includes
|
2023-12-20 23:10:25 +08:00
|
|
|
// add them here after the inline functions definitions
|
2020-07-16 17:56:24 +08:00
|
|
|
#include "net.pb.h"
|
|
|
|
#include "settings.h"
|
|
|
|
#include "local_storage.h"
|
|
|
|
#include "network.h"
|
|
|
|
|
|
|
|
// Emulator defines
|
|
|
|
#define CLIENT_HSTEAMUSER 1
|
|
|
|
#define SERVER_HSTEAMUSER 1
|
|
|
|
|
2024-04-20 07:22:16 +08:00
|
|
|
#define DEFAULT_NAME "gse orca"
|
2020-07-16 17:56:24 +08:00
|
|
|
#define DEFAULT_LANGUAGE "english"
|
2024-04-20 07:22:16 +08:00
|
|
|
#define DEFAULT_IP_COUNTRY "US"
|
2020-07-16 17:56:24 +08:00
|
|
|
|
|
|
|
#define LOBBY_CONNECT_APPID ((uint32)-2)
|
|
|
|
|
2024-05-03 06:29:57 +08:00
|
|
|
#endif //__INCLUDED_COMMON_INCLUDES__
|