From 8f5559f5310af38a7d1658bafb847f22bb1d1746 Mon Sep 17 00:00:00 2001
From: ConfiG <cgytrus@cgyt.ru>
Date: Tue, 1 Aug 2023 21:57:01 +0300
Subject: [PATCH] refactor actions  - add ability to automatically create a
 draft release  - change windows to windows-latest  - unify "Unzip CLI" and
 "Add CLI to Path"  - remove out folder  - prevent unnecessary files from
 generating on windows  - leave resources in platform-specific artifacts  -
 unify "Package MacOS" and "Package Windows"  - use steps outputs instead of
 env variables for storing version/hash

---
 .github/workflows/build.yml            | 117 ++++++++++++-------------
 installer/windows/dl-vcr.ps1           |   4 +
 installer/windows/installer.nsi        |   7 +-
 loader/CMakeLists.txt                  |  17 ++--
 loader/launcher/windows/CMakeLists.txt |  14 +--
 5 files changed, 84 insertions(+), 75 deletions(-)
 create mode 100644 installer/windows/dl-vcr.ps1

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 712e3e87..b42a2b56 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -2,6 +2,12 @@ name: Build Binaries
 
 on:
   workflow_dispatch:
+    inputs:
+      draft:
+        description: Create a draft release
+        required: false
+        type: boolean
+        default: false
   push:
     branches:
     - '**'              # every branch
@@ -15,11 +21,11 @@ jobs:
         config:
         - name: "Windows"
           os_identifier: "win"
-          os: windows-2019
+          os: windows-latest
           prefixes: ''
           extra_flags: '-T host=x86 -A win32 -DGEODE_DEBUG=On'
-          out_paths: './bin/nightly/Geode.pdb ./bin/nightly/Geode.dll ./bin/nightly/GeodeUpdater.exe ./bin/nightly/Geode.lib ./bin/nightly/XInput9_1_0.dll'
           cli_cmd: ''
+          package_cmd: 'makensis -WX -V3 ./installer/windows/installer.nsi'
           installer_path: './installer/windows/geode-installer-win.exe'
 
         - name: "macOS"
@@ -27,9 +33,9 @@ jobs:
           os: macos-latest
           prefixes: 'PATH="/usr/local/opt/ccache/libexec:$PATH"'
           extra_flags: "-DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Debug -DGEODE_DEBUG=On -DCMAKE_OSX_DEPLOYMENT_TARGET=10.13"
-          out_paths: './bin/nightly/Geode.dylib ./bin/nightly/GeodeBootstrapper.dylib ./loader/include/link/libfmod.dylib'
           cli_cmd: 'chmod +x $GITHUB_WORKSPACE/cli/geode'
-          installer_path: './out/geode-installer-mac.pkg'
+          package_cmd: './installer/mac/package.sh ./bin/nightly ./installer/mac/geode-installer-mac.pkg'
+          installer_path: './installer/mac/geode-installer-mac.pkg'
 
     name: ${{ matrix.config.name }}
     runs-on: ${{ matrix.config.os }}
@@ -57,23 +63,20 @@ jobs:
         fileName: "*-${{ matrix.config.os_identifier }}.zip"
         tarBall: false
         zipBall: false
-        out-file-path: "cli" 
+        out-file-path: "cli"
 
-    - name: Unzip CLI
+    - name: Setup CLI
       run: |
         7z x "${{ github.workspace }}/cli/*-${{ matrix.config.os_identifier }}.zip" -o"${{ github.workspace }}/cli"
-
-    - name: Add CLI to Path
-      run: |
         ${{ matrix.config.cli_cmd }}
         echo "${{ github.workspace }}/cli" >> $GITHUB_PATH
 
     - name: Configure CMake
       run: >
-        ${{ matrix.config.prefixes }} cmake -B 
-        ${{ github.workspace }}/build 
-        ${{ matrix.config.extra_flags }} 
-        -DCLI_PATH="${{ github.workspace }}/cli" 
+        ${{ matrix.config.prefixes }} cmake -B
+        ${{ github.workspace }}/build
+        ${{ matrix.config.extra_flags }}
+        -DCLI_PATH="${{ github.workspace }}/cli"
         -D CMAKE_C_COMPILER_LAUNCHER=ccache
         -D CMAKE_CXX_COMPILER_LAUNCHER=ccache
 
@@ -82,46 +85,19 @@ jobs:
         cd build
         cmake --build . --config RelWithDebInfo --parallel
 
-    - name: Move to output folder
-      shell: bash
-      working-directory: ${{ github.workspace }}
-      run: |
-        mkdir ./out
-        mv ${{ matrix.config.out_paths }} ./out
-
     - name: Delete resource cache file
-      run: |
-        rm ./bin/nightly/resources/.geode_cache
-      
-    - name: Upload resources
-      uses: actions/upload-artifact@v2
-      with:
-        name: resources
-        path: ./bin/nightly/resources
-      if: matrix.config.os_identifier == 'win'
+      run: rm ./bin/nightly/resources/.geode_cache
 
     - name: Upload Artifacts
       uses: actions/upload-artifact@v2
       with:
         name: geode-${{ matrix.config.os_identifier }}
-        path: ./out
+        path: ./bin/nightly
 
-    # Make these a script
-    - name: Package MacOS
-      run: |
-        cp -R ./bin/nightly/resources ./out/resources
-        ./installer/mac/package.sh ./out ./out/geode-installer-mac.pkg
-      if: matrix.config.os_identifier == 'mac'
+    - name: Package
+      run: ${{ matrix.config.package_cmd }}
 
-    - name: Package Windows
-      run: |
-        cp -R ./bin/nightly/resources ./out/resources
-        iwr -useb https://aka.ms/vs/17/release/vc_redist.x86.exe -outfile './installer/windows/VC_redist.x86.exe'
-        makensis -WX -V3 ./installer/windows/installer.nsi
-        mv ./installer/windows/GeodeInstaller.exe ./installer/windows/geode-installer-win.exe
-      if: matrix.config.os_identifier == 'win'
-
-    - name: Publish Installers
+    - name: Publish Installer
       uses: actions/upload-artifact@v2
       with:
         name: geode-installer-${{ matrix.config.os_identifier }}
@@ -133,35 +109,37 @@ jobs:
     if: github.ref == 'refs/heads/main'
     steps:
       - uses: actions/checkout@v2
-      
-      - name: Declare commit hash
+
+      - name: Declare version and commit hash
+        id: ref
         shell: bash
         run: |
-          echo "SHA_SHORT=$(git rev-parse --short "$GITHUB_SHA")" >> "$GITHUB_ENV"
+          echo "version=$(cat ${{ github.workspace }}/VERSION | xargs)" >> $GITHUB_OUTPUT
+          echo "hash=$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_OUTPUT
 
       - name: Download artifacts
         uses: actions/download-artifact@v3
         with:
           path: ${{ github.workspace }}
-          
+
       - name: Move installers
         run: |
-          mv geode-installer-mac/geode-installer-mac.pkg geode-installer-${{ env.SHA_SHORT }}-mac.pkg
-          mv geode-installer-win/geode-installer-win.exe geode-installer-${{ env.SHA_SHORT }}-win.exe
+          mv geode-installer-mac/geode-installer-mac.pkg geode-installer-${{ steps.ref.outputs.hash }}-mac.pkg
+          mv geode-installer-win/geode-installer-win.exe geode-installer-${{ steps.ref.outputs.hash }}-win.exe
 
       - name: Zip MacOS artifacts
         uses: vimtor/action-zip@v1
         with:
           files: geode-mac/Geode.dylib geode-mac/GeodeBootstrapper.dylib
-          dest: geode-${{ env.SHA_SHORT }}-mac.zip
+          dest: geode-${{ steps.ref.outputs.hash }}-mac.zip
 
       - name: Zip Windows artifacts
         uses: vimtor/action-zip@v1
         with:
           files: geode-win/XInput9_1_0.dll geode-win/Geode.dll geode-win/GeodeUpdater.exe geode-win/Geode.lib
-          dest: geode-${{ env.SHA_SHORT }}-win.zip
-          
-      - name: Update Nightly Release
+          dest: geode-${{ steps.ref.outputs.hash }}-win.zip
+
+      - name: Update Nightly release
         uses: andelf/nightly-release@main
         env:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -173,7 +151,28 @@ jobs:
           body: |
             Dev release of Geode. Will not install the resources automatically, so you should use the installers to install them.
           files: |
-            ./geode-${{ env.SHA_SHORT }}-mac.zip
-            ./geode-${{ env.SHA_SHORT }}-win.zip
-            ./geode-installer-${{ env.SHA_SHORT }}-mac.pkg
-            ./geode-installer-${{ env.SHA_SHORT }}-win.exe
+            ./geode-${{ steps.ref.outputs.hash }}-mac.zip
+            ./geode-${{ steps.ref.outputs.hash }}-win.zip
+            ./geode-installer-${{ steps.ref.outputs.hash }}-mac.pkg
+            ./geode-installer-${{ steps.ref.outputs.hash }}-win.exe
+
+      - name: Create draft release
+        uses: softprops/action-gh-release@v1
+        env:
+          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+        if: ${{ inputs.draft }}
+        with:
+          tag_name: v${{ steps.ref.outputs.version }}
+          name: Geode v${{ steps.ref.outputs.version }}
+          body: |
+            TODO before publishing:
+             - mark if pre-release
+             - add changelog
+             - remove this
+          draft: true
+          files: |
+            ./geode-${{ steps.ref.outputs.version }}-mac.zip
+            ./geode-${{ steps.ref.outputs.version }}-win.zip
+            ./geode-installer-${{ steps.ref.outputs.version }}-mac.pkg
+            ./geode-installer-${{ steps.ref.outputs.version }}-win.exe
+
diff --git a/installer/windows/dl-vcr.ps1 b/installer/windows/dl-vcr.ps1
new file mode 100644
index 00000000..4696f53a
--- /dev/null
+++ b/installer/windows/dl-vcr.ps1
@@ -0,0 +1,4 @@
+if(Test-path VC_redist.x86.exe -PathType leaf) {
+    Return
+}
+Invoke-WebRequest -useb https://aka.ms/vs/17/release/vc_redist.x86.exe -outfile VC_redist.x86.exe
\ No newline at end of file
diff --git a/installer/windows/installer.nsi b/installer/windows/installer.nsi
index 550bb0ff..c935ec70 100644
--- a/installer/windows/installer.nsi
+++ b/installer/windows/installer.nsi
@@ -5,7 +5,7 @@
 
 ; settings
     Name "Geode"
-    OutFile "GeodeInstaller.exe"
+    OutFile "geode-installer-win.exe"
     Unicode true
     InstallDir "$PROGRAMFILES32\Steam\steamapps\common\Geometry Dash\" ; set default path to the most common one
     XPStyle on
@@ -191,7 +191,7 @@
 
 ; actual code
 
-!define BINDIR ..\..\out
+!define BINDIR ..\..\bin\nightly
 
 ; installer
 
@@ -316,9 +316,10 @@ Function .onVerifyInstDir
         Return
 FunctionEnd
 
+; download vc redist in compile-time
+!execute "pwsh -nol -noni -nop dl-vcr.ps1"
 Section "Visual Studio Runtime"
     SetOutPath $INSTDIR
-    ; needs to be downloaded from https://aka.ms/vs/17/release/vc_redist.x86.exe before compiling installer
     File VC_redist.x86.exe
     ExecWait "$INSTDIR\VC_redist.x86.exe /install /quiet /norestart"
     Delete "$INSTDIR\VC_redist.x86.exe"
diff --git a/loader/CMakeLists.txt b/loader/CMakeLists.txt
index 0e1e0710..a098f2b2 100644
--- a/loader/CMakeLists.txt
+++ b/loader/CMakeLists.txt
@@ -110,8 +110,8 @@ target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
 make_directory("${GEODE_BIN_PATH}/nightly")
 
 # Prevent SDK from activating a loader build
-set_target_properties(${PROJECT_NAME} PROPERTIES 
-	PREFIX "" 
+set_target_properties(${PROJECT_NAME} PROPERTIES
+	PREFIX ""
 	OUTPUT_NAME "Geode"
 	# i'm going to say a slur that not a single soul has uttered in the last 200 years
 	LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${GEODE_BIN_PATH}/nightly"
@@ -194,7 +194,7 @@ if (NOT GEODE_DISABLE_PRECOMPILED_HEADERS)
 endif()
 
 # Create launcher
-if (APPLE) 
+if (APPLE)
 	set_target_properties(geode-loader PROPERTIES 
 		SYSTEM_NAME MacOS
 		OSX_DEPLOYMENT_TARGET 10.9
@@ -205,18 +205,21 @@ if (APPLE)
 
 	if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm" OR GEODE_TARGET_PLATFORM STREQUAL "iOS")
 		add_custom_command(TARGET geode-loader
-		    POST_BUILD COMMAND 
-		    ${CMAKE_INSTALL_NAME_TOOL} -id \"/Library/MobileSubstrate/DynamicLibraries/Geode.dylib\"
-		    $<TARGET_FILE:geode-loader>)
+			POST_BUILD COMMAND
+			${CMAKE_INSTALL_NAME_TOOL} -id \"/Library/MobileSubstrate/DynamicLibraries/Geode.dylib\"
+			$<TARGET_FILE:geode-loader>)
 	endif()
 elseif (WIN32)
 	add_subdirectory(launcher/windows)
 
 	target_link_libraries(${PROJECT_NAME} dbghelp)
 
-	# disable warnings about CCNode::setID
 	if (MSVC)
+		# disable warnings about CCNode::setID
 		target_link_options(${PROJECT_NAME} PUBLIC /ignore:4217)
+
+		# disable outputting .exp file
+		target_link_options(${PROJECT_NAME} PRIVATE /NOEXP)
 	endif()
 endif()
 
diff --git a/loader/launcher/windows/CMakeLists.txt b/loader/launcher/windows/CMakeLists.txt
index b01a6a34..5286a48e 100644
--- a/loader/launcher/windows/CMakeLists.txt
+++ b/loader/launcher/windows/CMakeLists.txt
@@ -3,10 +3,8 @@ cmake_minimum_required(VERSION 3.3.0)
 add_library(ProxyLoader SHARED proxyLoader.c)
 target_compile_features(ProxyLoader PUBLIC cxx_std_17)
 set_target_properties(ProxyLoader PROPERTIES
-	PREFIX "" 
+	PREFIX ""
 	OUTPUT_NAME "XInput9_1_0"
-	RUNTIME_OUTPUT_DIRECTORY "${GEODE_BIN_PATH}/nightly"
-	LIBRARY_OUTPUT_DIRECTORY "${GEODE_BIN_PATH}/nightly"
 	LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${GEODE_BIN_PATH}/nightly"
 	RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${GEODE_BIN_PATH}/nightly"
 	ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO "${GEODE_BIN_PATH}/nightly"
@@ -27,10 +25,8 @@ target_link_libraries(ProxyLoader PRIVATE fake-geode-loader)
 add_executable(Updater Updater.cpp)
 target_compile_features(Updater PUBLIC cxx_std_17)
 set_target_properties(Updater PROPERTIES
-	PREFIX "" 
+	PREFIX ""
 	OUTPUT_NAME "GeodeUpdater"
-	RUNTIME_OUTPUT_DIRECTORY "${GEODE_BIN_PATH}/nightly"
-	LIBRARY_OUTPUT_DIRECTORY "${GEODE_BIN_PATH}/nightly"
 	RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${GEODE_BIN_PATH}/nightly"
 	ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO "${GEODE_BIN_PATH}/nightly"
 	LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${GEODE_BIN_PATH}/nightly"
@@ -45,3 +41,9 @@ set_target_properties(Updater PROPERTIES
 	ARCHIVE_OUTPUT_DIRECTORY "${GEODE_BIN_PATH}/nightly"
 )
 target_link_libraries(Updater PUBLIC ghc_filesystem)
+
+if (MSVC)
+	target_link_options(fake-geode-loader PRIVATE /NOIMPLIB /NOEXP /DEBUG:NONE)
+	target_link_options(ProxyLoader PRIVATE /NOIMPLIB /NOEXP /DEBUG:NONE)
+	target_link_options(Updater PRIVATE /DEBUG:NONE)
+endif()