From 7f2484d82112851dbeba83afd18790a58b03599d Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 29 Oct 2013 23:36:53 +0300 Subject: Initial porting to NACL by Vasily_Makarov. --- src/CMakeLists.txt | 2 + src/Makefile.am | 2 + src/client.cpp | 22 ++++++++ src/gui/palette.h | 5 ++ src/main.cpp | 17 ++++++- src/net/nacltcpnet.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++ src/net/nacltcpnet.h | 71 ++++++++++++++++++++++++++ src/net/sdltcpnet.cpp | 4 ++ src/net/sdltcpnet.h | 3 ++ src/resources/imagewriter.cpp | 1 + src/utils/files.cpp | 6 ++- src/utils/files.h | 7 +-- src/utils/gettext.h | 4 +- src/utils/mkdir.cpp | 3 ++ src/utils/paths.cpp | 9 +++- 15 files changed, 263 insertions(+), 9 deletions(-) create mode 100644 src/net/nacltcpnet.cpp create mode 100644 src/net/nacltcpnet.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 254704a88..6bada3941 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -418,6 +418,8 @@ SET(SRCS net/messageout.cpp net/messageout.h net/npchandler.h + net/nacltcpnet.cpp + net/nacltcpnet.h net/net.cpp net/net.h net/partyhandler.h diff --git a/src/Makefile.am b/src/Makefile.am index 9f07e72f7..ebce034ea 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -428,6 +428,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ net/messagein.h \ net/messageout.cpp \ net/messageout.h \ + net/nacltcpnet.cpp \ + net/nacltcpnet.h \ net/net.cpp \ net/net.h \ net/npchandler.h \ diff --git a/src/client.cpp b/src/client.cpp index 8f1110162..24ed15987 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -140,12 +140,17 @@ #include #include +#include #include #include "mumblemanager.h" #include "debug.h" +#if defined __native_client__ +#define _nacl_dir std::string("/persistent/manaplus") +#endif + /** * Tells the max tick value, * setting it back to zero (and start again). @@ -400,6 +405,12 @@ void Client::gameInit() #endif #endif +#if defined __native_client__ + + const std::string dirName = _nacl_dir.append("/data"); + Files::extractZip("/http/data.zip", "data", dirName); +#endif + #ifdef ENABLE_NLS std::string lang = config.getStringValue("lang"); #ifdef WIN32 @@ -551,6 +562,11 @@ void Client::gameInit() } #endif #endif + +#if defined __native_client__ + resman->addToSearchPath(_nacl_dir.append("/data"), false); +#endif + // Add branding/data to PhysFS search path if (!mOptions.brandingPath.empty()) { @@ -756,11 +772,13 @@ Client::~Client() void Client::bindTextDomain(const char *const name, const char *const path) { +#ifdef ENABLE_NLS const char *const dir = bindtextdomain(name, path); if (dir) logger->log("bindtextdomain: %s", dir); else logger->log("bindtextdomain failed"); +#endif } void Client::setEnv(const char *const name, const char *const value) @@ -1944,6 +1962,8 @@ void Client::initLocalDataDir() #elif defined __ANDROID__ mLocalDataDir = getSdStoragePath() + branding.getValue( "appShort", "ManaPlus") + "/local"; +#elif defined __native_client__ + mLocalDataDir = _nacl_dir.append("/local"); #else mLocalDataDir = std::string(PhysFs::getUserDir()) + ".local/share/mana"; @@ -2001,6 +2021,8 @@ void Client::initConfigDir() #elif defined __ANDROID__ mConfigDir = getSdStoragePath() + branding.getValue( "appShort", "ManaPlus").append("/config"); +#elif defined __native_client__ + mConfigDir = _nacl_dir.append("/config"); #else mConfigDir = std::string(PhysFs::getUserDir()).append( "/.config/mana/").append(branding.getValue("appShort", "mana")); diff --git a/src/gui/palette.h b/src/gui/palette.h index f9980291f..fdde01737 100644 --- a/src/gui/palette.h +++ b/src/gui/palette.h @@ -28,7 +28,12 @@ #include +#if defined __native_client__ +#include +#else #include +#endif + #include #include #include diff --git a/src/main.cpp b/src/main.cpp index 193610156..f9ee5d5d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,6 +48,10 @@ #include #endif +#ifdef __native_client__ +#include +#endif + #include #include #include @@ -274,6 +278,15 @@ int main(int argc, char *argv[]) LoadLibrary("exchndl.dll"); #endif +#if defined __native_client__ + // for now for debug hardcoded path + umount("/"); + mount("", "/", "memfs", 0, ""); + mount("", "/persistent", "html5fs", 0, + "type=PERSISTENT,expected_size=104857600"); + mount("http://localhost:5103/my/manaplus", "/http", "httpfs", 0, ""); +#endif + selfName = argv[0]; // Parse command line options @@ -292,7 +305,9 @@ int main(int argc, char *argv[]) } // Initialize PhysicsFS -#ifdef ANDROID +#if defined(__native_client__) + if (!PHYSFS_init("/fakebinary")) +#elif defined(ANDROID) mkdir_r(getSdStoragePath().c_str()); if (!PHYSFS_init((getRealPath(".").append("/fakebinary")).c_str())) diff --git a/src/net/nacltcpnet.cpp b/src/net/nacltcpnet.cpp new file mode 100644 index 000000000..ccdb5a3f6 --- /dev/null +++ b/src/net/nacltcpnet.cpp @@ -0,0 +1,116 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#if defined __native_client__ + +#include "net/sdltcpnet.h" + +#if defined __linux__ || defined __linux +#include "logger.h" + +#include + +#if defined(M_TCPOK) && !defined(ANDROID) +#include +#include +#include +#else +#include +#include +#include +#include +// Use linear timeouts for thin streams +#define TCP_THIN_LINEAR_TIMEOUTS 16 +// Fast retrans. after 1 dupack +#define TCP_THIN_DUPACK 17 +#endif + +#endif + +#include "debug.h" + +struct _SDLNet_SocketSet +{ +}; + +struct _TCPsocket +{ + int ready; + int channel; + IPaddress remoteAddress; + IPaddress localAddress; + int sflag; +}; + +void TcpNet::init() +{ +} + +void TcpNet::quit() +{ +} + +void TcpNet::closeSocket(const TcpNet::Socket socket) +{ +} + +int TcpNet::send(const TcpNet::Socket sock, const void *const data, + const int len) +{ +} + +const char *TcpNet::getError() +{ +} + +int TcpNet::resolveHost(IPaddress *const address, const char *const host, + const Uint16 port) +{ +} + +TcpNet::Socket TcpNet::open(IPaddress *const ip) +{ +} + +TcpNet::SocketSet TcpNet::allocSocketSet(const int maxsockets) +{ +} + +int TcpNet::addSocket(const TcpNet::SocketSet set, const TcpNet::Socket sock) +{ +} + +int TcpNet::checkSockets(const TcpNet::SocketSet set, const Uint32 timeout) +{ +} + +int TcpNet::recv(const TcpNet::Socket sock, void *const data, const int maxlen) +{ +} + +int TcpNet::delSocket(const TcpNet::SocketSet set, const TcpNet::Socket sock) +{ +} + +void TcpNet::freeSocketSet(const TcpNet::SocketSet set) +{ +} + +#endif // __native_client__ diff --git a/src/net/nacltcpnet.h b/src/net/nacltcpnet.h new file mode 100644 index 000000000..770384e7e --- /dev/null +++ b/src/net/nacltcpnet.h @@ -0,0 +1,71 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef NET_NACLTCPNET_H +#define NET_NACLTCPNET_H + +#if defined __native_client__ + +#include +#include "localconsts.h" + +typedef struct { + Uint32 host; /* 32-bit IPv4 host address */ + Uint16 port; /* 16-bit protocol port */ +} IPaddress; + +typedef struct _SDLNet_SocketSet *SDLNet_SocketSet; +typedef struct _TCPsocket *TCPsocket; + +namespace TcpNet +{ + typedef ::SDLNet_SocketSet SocketSet; + typedef ::TCPsocket Socket; + + void init(); + + void quit(); + + void closeSocket(const TcpNet::Socket socket); + + int send(const TcpNet::Socket sock, const void *const data, const int len); + + const char *getError(); + + int resolveHost(IPaddress *const address, const char *const host, + const Uint16 port); + + TcpNet::Socket open(IPaddress *const ip); + + SocketSet allocSocketSet(const int maxsockets); + + int addSocket(const TcpNet::SocketSet set, const TcpNet::Socket sock); + + int checkSockets(const TcpNet::SocketSet set, const Uint32 timeout); + + int recv(const TcpNet::Socket sock, void *const data, const int maxlen); + + int delSocket(const TcpNet::SocketSet set, const TcpNet::Socket sock); + + void freeSocketSet(const TcpNet::SocketSet set); +} // namespace TcpNet + +#endif // __native_client__ +#endif // NET_NACLTCPNET_H diff --git a/src/net/sdltcpnet.cpp b/src/net/sdltcpnet.cpp index 4ea659ecf..4d708ea68 100644 --- a/src/net/sdltcpnet.cpp +++ b/src/net/sdltcpnet.cpp @@ -18,6 +18,8 @@ * along with this program. If not, see . */ +#if !defined __native_client__ + #include "net/sdltcpnet.h" #if defined __linux__ || defined __linux @@ -150,3 +152,5 @@ void TcpNet::freeSocketSet(const TcpNet::SocketSet set) { SDLNet_FreeSocketSet(set); } + +#endif // __native_client__ diff --git a/src/net/sdltcpnet.h b/src/net/sdltcpnet.h index a61c218b8..04c80e71a 100644 --- a/src/net/sdltcpnet.h +++ b/src/net/sdltcpnet.h @@ -21,6 +21,8 @@ #ifndef NET_SDLTCPNET_H #define NET_SDLTCPNET_H +#if !defined __native_client__ + #include #include "localconsts.h" @@ -58,4 +60,5 @@ namespace TcpNet void freeSocketSet(const TcpNet::SocketSet set); } // namespace TcpNet +#endif // __native_client__ #endif // NET_SDLTCPNET_H diff --git a/src/resources/imagewriter.cpp b/src/resources/imagewriter.cpp index 1db9f139d..3e1eeead5 100644 --- a/src/resources/imagewriter.cpp +++ b/src/resources/imagewriter.cpp @@ -24,6 +24,7 @@ #include "logger.h" +#include #include #include #include diff --git a/src/utils/files.cpp b/src/utils/files.cpp index f4c93e6e6..c06b3d892 100644 --- a/src/utils/files.cpp +++ b/src/utils/files.cpp @@ -18,7 +18,7 @@ * along with this program. If not, see . */ -#ifdef ANDROID +#if defined(ANDROID) || defined(__native_client__) #include "utils/files.h" #include "logger.h" @@ -30,6 +30,7 @@ #include "localconsts.h" +#ifdef ANDROID void Files::extractLocale() { // in future need also remove all locales in local dir @@ -60,6 +61,7 @@ void Files::extractLocale() resman->removeFromSearchPath(fileName2); remove(fileName2.c_str()); } +#endif // ANDROID void Files::copyPhysFsFile(const std::string &inFile, const std::string &outFile) @@ -98,4 +100,4 @@ void Files::extractZip(const std::string &zipName, const std::string &inDir, remove(zipName.c_str()); } -#endif // ANDROID +#endif // ANDROID __native_client__ diff --git a/src/utils/files.h b/src/utils/files.h index bb9a6a11f..519b7f520 100644 --- a/src/utils/files.h +++ b/src/utils/files.h @@ -21,13 +21,14 @@ #ifndef UTILS_FILES_H #define UTILS_FILES_H -#ifdef ANDROID +#if defined(ANDROID) || defined(__native_client__) #include namespace Files { +#ifdef ANDROID void extractLocale(); - +#endif void copyPhysFsFile(const std::string &inFile, const std::string &outFile); void copyPhysFsDir(const std::string &inDir, const std::string &outDir); @@ -36,5 +37,5 @@ namespace Files const std::string &outDir); } // namespace Files -#endif // ANDROID +#endif // ANDROID __native_client__ #endif // UTILS_FILES_H diff --git a/src/utils/gettext.h b/src/utils/gettext.h index ed686cffd..a00e44898 100644 --- a/src/utils/gettext.h +++ b/src/utils/gettext.h @@ -27,10 +27,10 @@ #include "config.h" #endif -#include - #ifdef ENABLE_NLS +#include + #define _(s) (const_cast (gettext(s))) #define N_(s) (const_cast (s)) diff --git a/src/utils/mkdir.cpp b/src/utils/mkdir.cpp index fc6909710..959b15b04 100644 --- a/src/utils/mkdir.cpp +++ b/src/utils/mkdir.cpp @@ -36,6 +36,8 @@ #include #endif +#include + #include "debug.h" #if defined WIN32 @@ -112,6 +114,7 @@ int mkdir_r(const char *const pathname) return 0; } #else + /// Create a directory, making leading components first if necessary int mkdir_r(const char *const pathname) { diff --git a/src/utils/paths.cpp b/src/utils/paths.cpp index e3992cd72..dbe0cccce 100644 --- a/src/utils/paths.cpp +++ b/src/utils/paths.cpp @@ -30,11 +30,18 @@ #include "resources/resourcemanager.h" +#ifdef __native_client__ +#include +#define realpath(N, R) strcpy(R, N) +#endif + #ifdef WIN32 #include "utils/specialfolder.h" #define realpath(N, R) _fullpath((R), (N), _MAX_PATH) #elif defined __OpenBSD__ #include +#elif defined __native_client__ +#include #endif #ifdef ANDROID @@ -47,7 +54,7 @@ std::string getRealPath(const std::string &str) { -#if defined(__OpenBSD__) || defined(__ANDROID__) +#if defined(__OpenBSD__) || defined(__ANDROID__) || defined(__native_client__) char *realPath = reinterpret_cast(calloc(PATH_MAX, sizeof(char))); realpath(str.c_str(), realPath); #else -- cgit v1.2.3-70-g09d2