1 cmake_minimum_required(VERSION 3.5)
4 # Create a single GRF file based on sprites/<grfname>.nfo and sprites/*.png
8 if(NOT NFORENUM_EXECUTABLE)
9 message(FATAL_ERROR "Script needs NFORENUM_EXECUTABLE defined")
11 if(NOT GRFCODEC_EXECUTABLE)
12 message(FATAL_ERROR "Script needs GRFCODEC_EXECUTABLE defined")
14 if(NOT GRFID_EXECUTABLE)
15 message(FATAL_ERROR "Script needs GRFID_EXECUTABLE defined")
17 if(NOT GRF_SOURCE_FOLDER)
18 message(FATAL_ERROR "Script needs GRF_SOURCE_FOLDER defined")
20 if(NOT GRF_BINARY_FILE)
21 message(FATAL_ERROR "Script needs GRF_BINARY_FILE defined")
24 # Remove the existing output so failures never go unnoticed
25 file(REMOVE ${GRF_BINARY_FILE} ${GRF_BINARY_FILE}.hash)
27 get_filename_component(GRF_SOURCE_FOLDER_NAME "${GRF_SOURCE_FOLDER}" NAME)
29 file(WRITE sprites/${GRF_SOURCE_FOLDER_NAME}.nfo "")
30 file(READ ${GRF_SOURCE_FOLDER}/${GRF_SOURCE_FOLDER_NAME}.nfo NFO_LINES)
31 # Replace ; with \;, and make a list out of this based on \n
32 string(REPLACE ";" "\\;" NFO_LINES "${NFO_LINES}")
33 string(REPLACE "\n" ";" NFO_LINES "${NFO_LINES}")
35 foreach(NFO_LINE IN LISTS NFO_LINES)
36 # Recover the ; that was really in the text (and not a newline)
37 string(REPLACE "\\;" ";" NFO_LINE "${NFO_LINE}")
39 if(NFO_LINE MATCHES "^#include")
40 string(REGEX REPLACE "^#include \"(.*)\"$" "\\1" INCLUDE_FILE ${NFO_LINE})
41 file(READ ${GRF_SOURCE_FOLDER}/${INCLUDE_FILE} INCLUDE_LINES)
42 file(APPEND sprites/${GRF_SOURCE_FOLDER_NAME}.nfo "${INCLUDE_LINES}")
44 file(APPEND sprites/${GRF_SOURCE_FOLDER_NAME}.nfo "${NFO_LINE}\n")
48 execute_process(COMMAND ${NFORENUM_EXECUTABLE} -s sprites/${GRF_SOURCE_FOLDER_NAME}.nfo RESULT_VARIABLE RESULT)
50 if(NOT RESULT MATCHES "^[0-9]*$")
51 message(FATAL_ERROR "Failed to run NFORenum (${RESULT}), please check NFORENUM_EXECUTABLE variable")
53 message(FATAL_ERROR "NFORenum failed")
56 execute_process(COMMAND ${GRFCODEC_EXECUTABLE} -n -s -e -g2 -p1 ${GRF_SOURCE_FOLDER_NAME}.grf RESULT_VARIABLE RESULT)
58 if(NOT RESULT MATCHES "^[0-9]*$")
59 message(FATAL_ERROR "Failed to run GRFCodec (${RESULT}), please check GRFCODEC_EXECUTABLE variable")
61 message(FATAL_ERROR "GRFCodec failed")
64 execute_process(COMMAND ${GRFID_EXECUTABLE} -m ${GRF_SOURCE_FOLDER_NAME}.grf OUTPUT_VARIABLE GRFID_HASH RESULT_VARIABLE RESULT)
66 if(NOT RESULT MATCHES "^[0-9]*$")
67 message(FATAL_ERROR "Failed to run GRFID (${RESULT}), please check GRFID_EXECUTABLE variable")
69 message(FATAL_ERROR "GRFID failed")
72 file(WRITE ${GRF_BINARY_FILE}.hash ${GRFID_HASH})
74 # Copy build files back to the source directory.
75 execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${GRF_SOURCE_FOLDER_NAME}.grf ${GRF_BINARY_FILE})