From 12daf58ec2af5f3b02f9fb31e7a62287426844d8 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 3 Oct 2026 08:27:35 +0300
Subject: [PATCH 86/86] Meson: Add preload for emscripten client

Add framework to implement emscripten preload in,
and helpdata.txt and client theme preloading as an example.

See RM #2280

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 meson.build                      | 16 ++++++++++++++--
 platforms/Makefile.am            |  1 +
 platforms/emscripten/datacopy.sh | 23 +++++++++++++++++++++++
 3 files changed, 38 insertions(+), 2 deletions(-)
 create mode 100755 platforms/emscripten/datacopy.sh

diff --git a/meson.build b/meson.build
index 73f9075d13..314c620b94 100644
--- a/meson.build
+++ b/meson.build
@@ -709,6 +709,7 @@ if host_system == 'emscripten'
   zlib_dep = []
   syslua = 'false'
   ems_suffix = 'html'
+  ems_largs = [ '--preload-file', 'data' ]
 
   priv_conf_data.set('ALWAYS_ROOT', 1)
 
@@ -739,6 +740,7 @@ else
   endif
 
   ems_suffix = []
+  ems_largs = []
 endif
 
 # Set unconditionally, as it was checked as hard requirement
@@ -1325,6 +1327,14 @@ else
   tinycthr_files = files()
 endif
 
+if host_system == 'emscripten'
+  ems_data = custom_target('datacopy', output: 'data',
+                   command: [sh_exe, files('platforms/emscripten/datacopy.sh')],
+                   depend_files: files('platforms/emscripten/datacopy.sh'))
+else
+  ems_data = []
+endif
+
 if syslua != 'false' and lua_dep_tmp.found()
   lua_dep = lua_dep_tmp
   fc_lua = []
@@ -4042,12 +4052,13 @@ executable('freeciv-sdl2',
   sdl2_gfx_src,
   clienticon,
   c_args: sdl2_gfx_cargs,
-  sources: [verhdr],
+  sources: [verhdr, ems_data],
   include_directories: [client_inc, sdl2_gfx_inc ],
   dependencies: [sdl2main_dep, audio_dep,
                  sdl2_image_dep, sdl2_gfx_dep, sdl2_ttf_dep,
                  net_dep, gettext_dep, mw_extra_dep],
   link_with: client_common,
+  link_args: ems_largs,
   name_suffix: ems_suffix,
   install: true,
   win_subsystem: 'windows'
@@ -4174,12 +4185,13 @@ executable('freeciv-sdl3',
   'client/gui-sdl3/wldlg.c',
   'client/gui-sdl3/SDL3_gfx/SDL3_rotozoom.c',
   clienticon,
-  sources: [verhdr],
+  sources: [verhdr, ems_data],
   include_directories: [client_inc, sdl3_gfx_inc ],
   dependencies: [sdl3main_dep, audio_dep,
                  sdl3_image_dep, sdl3_gfx_dep, sdl3_ttf_dep,
                  net_dep, gettext_dep, mw_extra_dep],
   link_with: client_common,
+  link_args: ems_largs,
   name_suffix: ems_suffix,
   install: true,
   win_subsystem: 'windows'
diff --git a/platforms/Makefile.am b/platforms/Makefile.am
index 3446632c50..afc4b7711d 100644
--- a/platforms/Makefile.am
+++ b/platforms/Makefile.am
@@ -6,6 +6,7 @@ EXTRA_DIST =						\
 		appimage/build_appimages.sh		\
 		emscripten/emsbuild.sh			\
 		emscripten/emssetup.sh			\
+		emscripten/datacopy.sh			\
 		emscripten/setups/cross-ems.tmpl	\
 		flatpak/build_flatpaks.sh		\
 		flatpak/org.freeciv.gtk322.yml		\
diff --git a/platforms/emscripten/datacopy.sh b/platforms/emscripten/datacopy.sh
new file mode 100755
index 0000000000..6764e67bfc
--- /dev/null
+++ b/platforms/emscripten/datacopy.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+# datacopy.sh: Copy data to emscripten build directory
+#
+# (c) 2026 Freeciv team
+#
+# This script is licensed under Gnu General Public License version 2 or later.
+# See COPYING available from the same location you got this script.
+
+DATA_SRC="$(cd "$(dirname "$0")/../../" || exit 1 ; pwd)"
+DATA_TGT="$(pwd)"
+
+if ! mkdir -p "${DATA_TGT}/data/themes" ; then
+  echo "Failed to create data directories!" >&2
+  exit 1
+fi
+
+if ! cp "${DATA_SRC}/data/helpdata.txt" "${DATA_TGT}/data/" ||
+   ! cp -R "${DATA_SRC}/data/themes/gui-sdl2" "${DATA_TGT}/data/themes/"
+then
+  echo "Copying data failed!" >&2
+  exit 1
+fi
-- 
2.53.0

