From f16608815f880a4c19de0490befa28a5870779a4 Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Fri, 22 Sep 2023 21:33:54 -0500 Subject: [PATCH] Work on the Windows build. Edge binary can now be embedded into the application. --- .gitignore | 1 + build-godot.sh | 65 +++++++++++++++++ build-n2n.sh | 67 +++++++++++------ cmake/x86_64-linux-gnu.cmake | 1 + cmake/x86_64-w64-mingw32.cmake | 9 +++ custom.py | 2 +- hamncheese/Scripts/edge.gd | 12 ++++ modules/.gitignore | 3 + modules/embedded/SCsub | 12 ++++ modules/embedded/config.py | 6 ++ modules/embedded/embedded.cpp | 51 +++++++++++++ modules/embedded/embedded.h | 22 ++++++ modules/embedded/register_types.cpp | 19 +++++ modules/embedded/register_types.h | 4 ++ modules/embedded/stddclmr.h | 95 +++++++++++++++++++++++++ n2n/thirdparty/libnatpmp/CMakeLists.txt | 2 +- prereqs.sh | 2 + 17 files changed, 350 insertions(+), 23 deletions(-) create mode 100644 cmake/x86_64-linux-gnu.cmake create mode 100644 cmake/x86_64-w64-mingw32.cmake create mode 100644 modules/.gitignore create mode 100644 modules/embedded/SCsub create mode 100644 modules/embedded/config.py create mode 100644 modules/embedded/embedded.cpp create mode 100644 modules/embedded/embedded.h create mode 100644 modules/embedded/register_types.cpp create mode 100644 modules/embedded/register_types.h create mode 100644 modules/embedded/stddclmr.h diff --git a/.gitignore b/.gitignore index df2b0ab..2f9af32 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *~ output.log bin/ +modules/embedded/embeds/ hamncheese/export_presets.cfg temp/ scp.sh diff --git a/build-godot.sh b/build-godot.sh index aa2d86b..7a4d3de 100755 --- a/build-godot.sh +++ b/build-godot.sh @@ -1,9 +1,74 @@ #!/bin/bash +function createEmbeddedBinary() { + local BINFILE=$1 + local SOURCEFILE=$2 + local BLOCKER=$3 + + outputLicense > ${SOURCEFILE} + outputHeader ${BLOCKER} >> ${SOURCEFILE} + printf "\n#ifdef EMBED_HERE\n\n" >> ${SOURCEFILE} + xxd -i ${BINFILE} >> ${SOURCEFILE} + printf "\n#else // EMBED_HERE\n\n" >> ${SOURCEFILE} + printf "extern unsigned char ${BINFILE/\./_}[];\n" >> ${SOURCEFILE} + printf "extern unsigned int ${BINFILE/\./_}_len;\n" >> ${SOURCEFILE} + printf "\n#endif // EMBED_HERE\n\n" >> ${SOURCEFILE} + outputFooter ${BLOCKER} >> ${SOURCEFILE} +} + + +function outputFooter() { + local BLOCKER=$1 + + printf "\n#pragma GCC diagnostic pop\n\n\n#endif // ${BLOCKER}\n" +} + + +function outputHeader() { + local BLOCKER=$1 + + printf "\n\n#ifndef ${BLOCKER}\n#define ${BLOCKER}\n\n\n" + printf "// ===== THIS FILE IS AUTOMATICALLY GENERATED - DO NOT EDIT =====\n\n\n" + printf "#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wunused-variable\"\n" +} + + +function outputLicense() { + cat <<-LICENSE + /* + * + * Ham'n'Cheese + * Copyright (C) 2006-2020 Scott Duensing + * + * 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 (at your option) 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + LICENSE +} + + # Eventually provide a build_profile file to reduce file size. TEMPLATE="disable_3d=yes svg=no" LTO="lto=none" # Use "lto=full" for releases. +# Embed edge binaries. +pushd bin +mkdir -p ../modules/embedded/embeds +createEmbeddedBinary x86_64-linux-gnu-edge ../modules/embedded/embeds/linux_x86_64.h EDGE_H +popd + pushd godot # Use Our Custom Settings. diff --git a/build-n2n.sh b/build-n2n.sh index 56d9077..bc9b196 100755 --- a/build-n2n.sh +++ b/build-n2n.sh @@ -1,28 +1,53 @@ #!/bin/bash -# Build n2n for current platform. +# Build n2n. + + +BUILDROOT=${PWD} mkdir -p bin -mkdir -p n2n/thirdparty/libnatpmp/build -pushd n2n/thirdparty/libnatpmp/build -cmake .. -make -popd -mkdir -p n2n/thirdparty/miniupnp/miniupnpc/build -pushd n2n/thirdparty/miniupnp/miniupnpc/build -cmake .. -make -popd +function buildn2n() { + TRIPLE=$1 + TOOLCHAIN=${BUILDROOT}/cmake/${TRIPLE}.cmake + + pushd n2n/thirdparty/libnatpmp + sed -i 's/Iphlpapi/iphlpapi/g' CMakeLists.txt + [[ -d build ]] && rm -rf build + mkdir -p build + cd build + cmake -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN} .. + make + popd -pushd n2n -./autogen.sh -CFLAGS="-I thirdparty/libnatpmp -I thirdparty/miniupnp/miniupnpc/include" \ - LDFLAGS="-L thirdparty/libnatpmp/build -L thirdparty/miniupnp/miniupnpc/build" \ - ./configure --enable-pthread --enable-natpmp --enable-miniupnp -make edge -make supernode -mv edge ../bin/. -mv supernode ../bin/. -popd + pushd n2n/thirdparty/miniupnp/miniupnpc + [[ -d build ]] && rm -rf build + mkdir -p build + cmake -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN} .. + make + popd + + pushd n2n + ./autogen.sh + CFLAGS="-I ${BUILDROOT}/n2n/thirdparty/libnatpmp -I ${BUILDROOT}/n2n/thirdparty/miniupnp/miniupnpc/include" \ + LDFLAGS="-L ${BUILDROOT}/n2n/thirdparty/libnatpmp/build -L ${BUILDROOT}/n2n/thirdparty/miniupnp/miniupnpc/build" \ + ./configure \ + --target=${TRIPLE} \ + --host=${TRIPLE} \ + --build=x86_64-linux \ + --enable-pthread \ + --enable-natpmp \ + --enable-miniupnp + make edge + make supernode + upx -9 edge + upx -9 supernode + mv edge ../bin/${TRIPLE}-edge + mv supernode ../bin/${TRIPLE}-supernode + popd +} + + +buildn2n "x86_64-linux-gnu" +buildn2n "x86_64-w64-mingw32" diff --git a/cmake/x86_64-linux-gnu.cmake b/cmake/x86_64-linux-gnu.cmake new file mode 100644 index 0000000..63ea6ae --- /dev/null +++ b/cmake/x86_64-linux-gnu.cmake @@ -0,0 +1 @@ +# Nothing to do on the native platform. diff --git a/cmake/x86_64-w64-mingw32.cmake b/cmake/x86_64-w64-mingw32.cmake new file mode 100644 index 0000000..b0f70a6 --- /dev/null +++ b/cmake/x86_64-w64-mingw32.cmake @@ -0,0 +1,9 @@ +set(CMAKE_SYSTEM_NAME Windows) +set(CMAKE_SYSTEM_PROCESSOR x86_64) + +set(triple x86_64-w64-mingw32) + +set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc-10-posix) +set(CMAKE_C_COMPILER_TARGET ${triple}) +set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-c++-posix) +set(CMAKE_CXX_COMPILER_TARGET ${triple}) diff --git a/custom.py b/custom.py index 62ff08a..578e4c9 100644 --- a/custom.py +++ b/custom.py @@ -40,4 +40,4 @@ module_visual_script_enabled = "no" module_vorbis_enabled = "no" module_websocket_enabled = "no" module_webxr_enabled = "no" -#custom_modules = "../modules" +custom_modules = "../modules" diff --git a/hamncheese/Scripts/edge.gd b/hamncheese/Scripts/edge.gd index 81d1f8e..d4b9b81 100644 --- a/hamncheese/Scripts/edge.gd +++ b/hamncheese/Scripts/edge.gd @@ -51,6 +51,18 @@ func _process(_delta): _data_buffer[received["_tag"]].append(received) +func _ready(): + # Extract the edge binary. + var binary = OS.get_user_data_dir() + "/edge" + if OS.get_name() == "Windows": + binary = binary + ".exe" + if !FileAccess.file_exists(binary): + var extract = Embedded.new() + extract.save_edge(binary) + if OS.get_name() != "Windows": + OS.execute("chmod", ["+x", binary]) + + func _send(what: String, mode: String = MODE_READ): if _udp.is_socket_connected(): var packet = mode + " " + str(_sequence) + ":1:" + _password + " " + what diff --git a/modules/.gitignore b/modules/.gitignore new file mode 100644 index 0000000..68bdb97 --- /dev/null +++ b/modules/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +*.o +*~ diff --git a/modules/embedded/SCsub b/modules/embedded/SCsub new file mode 100644 index 0000000..355b735 --- /dev/null +++ b/modules/embedded/SCsub @@ -0,0 +1,12 @@ +Import('env') + +env_embedded = env.Clone() + +# Bindings. +env_embedded.add_source_files(env.modules_sources, "*.cpp") + +# Append CCFLAGS flags for both C and C++ code. +# If you need to, you can: +# - Append CFLAGS for C code only. +# - Append CXXFLAGS for C++ code only. +env_embedded.Append(CCFLAGS=['-g', '-O2']) diff --git a/modules/embedded/config.py b/modules/embedded/config.py new file mode 100644 index 0000000..d22f945 --- /dev/null +++ b/modules/embedded/config.py @@ -0,0 +1,6 @@ +def can_build(env, platform): + return True + + +def configure(env): + pass diff --git a/modules/embedded/embedded.cpp b/modules/embedded/embedded.cpp new file mode 100644 index 0000000..4b31355 --- /dev/null +++ b/modules/embedded/embedded.cpp @@ -0,0 +1,51 @@ +#include "embedded.h" + +#define EMBED_HERE + +extern "C" { + #include + #include "stddclmr.h" + +#ifdef __linux__ +#ifdef __arm__ + #include "embeds/linux_arm64.h" +#else + #include "embeds/linux_x86_64.h" + unsigned char *data = x86_64_linux_gnu_edge; + size_t length = (size_t)x86_64_linux_gnu_edge_len; +#endif +#elif _WIN32 + #include "embeds/windows_x86_64.h" + unsigned char *data = x86_64_w64_mingw32_edge; + size_t length = (size_t)x86_64_w64_mingw32_edge_len; +#elif __APPLE__ +#ifdef __arm__ + #include "embeds/macos_arm64.h" +#else + #include "embeds/macos_x86_64.h" +#endif +#endif +} + + + +Embedded::Embedded() { +} + + +void Embedded::_bind_methods() { + ClassDB::bind_method(D_METHOD("save_edge", "filename"), &Embedded::save_edge); +} + + +bool Embedded::save_edge(String filename) { + FILE *out; + + out = fopen(filename.ascii().get_data(), "wb"); + if (out) { + fwrite(data, length, 1, out); + fclose(out); + return true; + } + return false; +} diff --git a/modules/embedded/embedded.h b/modules/embedded/embedded.h new file mode 100644 index 0000000..6655233 --- /dev/null +++ b/modules/embedded/embedded.h @@ -0,0 +1,22 @@ +#ifndef GODOT_EMBEDDED_H +#define GODOT_EMBEDDED_H + + +#include "core/object/ref_counted.h" +#include "core/variant/variant.h" + + +class Embedded : public RefCounted { + GDCLASS(Embedded, RefCounted); + + protected: + static void _bind_methods(); + + public: + Embedded(); + + bool save_edge(String filename); +}; + + +#endif // GODOT_EMBEDDED_H diff --git a/modules/embedded/register_types.cpp b/modules/embedded/register_types.cpp new file mode 100644 index 0000000..c920c5c --- /dev/null +++ b/modules/embedded/register_types.cpp @@ -0,0 +1,19 @@ +#include "register_types.h" +#include "core/object/class_db.h" +#include "embedded.h" + + +void initialize_embedded_module(ModuleInitializationLevel p_level) { + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } + ClassDB::register_class(); +} + + +void uninitialize_embedded_module(ModuleInitializationLevel p_level) { + if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { + return; + } + // Nothing to do here. +} diff --git a/modules/embedded/register_types.h b/modules/embedded/register_types.h new file mode 100644 index 0000000..f043d5d --- /dev/null +++ b/modules/embedded/register_types.h @@ -0,0 +1,4 @@ +#include "modules/register_module_types.h" + +void initialize_embedded_module(ModuleInitializationLevel p_level); +void uninitialize_embedded_module(ModuleInitializationLevel p_level); diff --git a/modules/embedded/stddclmr.h b/modules/embedded/stddclmr.h new file mode 100644 index 0000000..8e6b3d9 --- /dev/null +++ b/modules/embedded/stddclmr.h @@ -0,0 +1,95 @@ +#ifndef STDDCLMR_H +#define STDDCLMR_H + +/* +Action figures sold separately. Add toner. All models over 18 years of age. +All rights reserved. Allow four to six weeks for delivery. An equal +opportunity employer. Any resemblance to actual persons, living or dead, is +unintentional and purely coincidental. Apply only to affected area. Approved +for veterans. As seen on TV. At participating locations only. Avoid contact +with mucous membranes. Avoid contact with skin. Avoid extreme temperatures +and store in a cool dry place. Batteries not included. Be sure each item is +properly endorsed. Beware of dog. Booths for two or more. Breaking seal +constitutes acceptance of agreement. Call toll free number before digging. +Caveat emptor. Check here if tax deductible. Close cover before striking +Colors may fade. Contains a substantial amount of non-tobacco ingredients. +Contents may settle during shipment. Contestants have been briefed on some +questions before the show. Copyright 1995 Joker's Wild. Disclaimer does +not cover hurricane, lightning, tornado, tsunami, volcanic eruption, +earthquake, flood, and other Acts of God, misuse, neglect, unauthorized +repair, damage from improper installation, broken antenna or marred cabinet, +incorrect line voltage, missing or altered serial numbers, sonic boom +vibrations, electromagnetic radiation from nuclear blasts, customer +adjustments that are not covered in the joke list, and incidents owing to +airplane crash, ship sinking, motor vehicle accidents, leaky roof, broken +glass, falling rocks, mud slides, forest fire, flying projectiles, or +dropping the item. Do not bend, fold, mutilate, or spindle. Do not place +near flammable or magnetic source. Do not puncture, incinerate, or store +above 120 degrees Fahrenheit. Do not stamp. Use other side for additional +listings. Do not use while operating a motor vehicle or heavy equipment. Do +not write below this line. Documents are provided "as is" without any +warranties expressed or implied. Don't quote me on anything. Don't quote me +on that. Driver does not carry cash. Drop in any mailbox. Edited for +television. Employees and their families are not eligible. Falling rock. +First pull up, then pull down. Flames redirected to /dev/null. For a +limited time only. For external use only. For off-road use only. For office +use only. For recreational use only. Do not disturb. Freshest if eaten +before date on carton. Hand wash only, tumble dry on low heat. If a rash, +redness, irritation, or swelling develops, discontinue use. If condition +persists, consult your physician. If defects are discovered, do not attempt +to fix them yourself, but return to an authorized service center. If +ingested, do not induce vomiting, if symptoms persist, consult a doctor. +Keep away from open flames and avoid inhaling fumes. Keep away from +sunlight, pets, and small children. Keep cool; process promptly. Limit +one-per-family please. Limited time offer, call now to ensure prompt +delivery. List at least two alternate dates. List each check separately by +bank number. List was current at time of printing. Lost ticket pays maximum +rate. May be too intense for some viewers. Must be 18 to enter. No Canadian +coins. No alcohol, dogs or horses. No anchovies unless otherwise specified. +No animals were harmed in the production of these documents. No money down. +No other warranty expressed or implied. No passes accepted for this +engagement. No postage necessary if mailed in the United States. No +preservatives added. No purchase necessary. No salt, MSG, artificial color +or flavor added. No shoes, no shirt, no service, no kidding. No solicitors. +No substitutions allowed. No transfers issued until the bus comes to a +complete stop. No user-serviceable parts inside. Not affiliated with the +American Red Cross. Not liable for damages due to use or misuse. Not +recommended for children. Not responsible for direct, indirect, incidental +or consequential damages resulting from any defect, error or failure to +perform. Not the Beatles. Objects in mirror may be closer than they appear. +One size fits all. Many suitcases look alike. Other copyright laws for +specific entries apply wherever noted. Other restrictions may apply. Package +sold by weight, not volume. Parental advisory - explicit lyrics. Penalty for +private use. Place stamp here. Please remain seated until the ride has come +to a complete stop. Possible penalties for early withdrawal. Post office will +not deliver without postage. Postage will be paid by addressee. Prerecorded +for this time zone. Price does not include taxes. Processed at location +stamped in code at top of carton. Quantities are limited while supplies last. +Read at your own risk. Record additional transactions on back of previous +stub. Replace with same type. Reproduction strictly prohibited. Restaurant +package, not for resale. Return to sender, no forwarding order on file, +unable to forward. Safety goggles may be required during use. Sanitized for +your protection. Sealed for your protection, do not use if the safety seal is +broken. See label for sequence. Shading within a garment may occur. Sign here +without admitting guilt. Simulated picture. Slightly enlarged to show detail. +Slightly higher west of the Rockies. Slippery when wet. Smoking these may be +hazardous to your health. Some assembly required. Some equipment shown is +optional. Some of the trademarks mentioned in this product appear for +identification purposes only. Subject to FCC approval. Subject to change +without notice. Substantial penalty for early withdrawal. Text may contain +material some readers may find objectionable, parental guidance is advised. +Text used in these documents is made from 100% recycled electrons and magnetic +particles. These documents do not reflect the thoughts or opinions of either +myself, my company, my friends, or my rabbit. This is not an offer to sell +securities. This offer is void where prohibited, taxed, or otherwise +restricted. This product is meant for educational purposes only. Times +approximate. Unix is a registered trademark of AT&T. Use only as directed. Use +only in a well-ventilated are. User assumes full liabilities. Void where +prohibited. We have sent the forms which seem right for you. You must be +present to win. You need not be present to win. Your canceled check is your +receipt. Your mileage may vary. I didn't do it. You can't prove anything. + +This supersedes all previous notices. +*/ + +#endif // STDDCLMR_H diff --git a/n2n/thirdparty/libnatpmp/CMakeLists.txt b/n2n/thirdparty/libnatpmp/CMakeLists.txt index eac3985..3477ed5 100644 --- a/n2n/thirdparty/libnatpmp/CMakeLists.txt +++ b/n2n/thirdparty/libnatpmp/CMakeLists.txt @@ -21,7 +21,7 @@ target_include_directories(natpmp PUBLIC ${CMAKE_CURRENT_LIST_DIR}) target_compile_definitions(natpmp PRIVATE -DENABLE_STRNATPMPERR) if (WIN32) - target_link_libraries(natpmp PUBLIC ws2_32 Iphlpapi) + target_link_libraries(natpmp PUBLIC ws2_32 iphlpapi) target_compile_definitions(natpmp PUBLIC -DNATPMP_STATICLIB) endif (WIN32) diff --git a/prereqs.sh b/prereqs.sh index ea4c25d..e311246 100755 --- a/prereqs.sh +++ b/prereqs.sh @@ -10,6 +10,8 @@ apt-get -y install \ build-essential \ scons \ pkg-config \ + xxd \ + upx-ucl \ libx11-dev \ libxcursor-dev \ libxinerama-dev \