Browse Source

Windows data bundle now compiling

zig-compilation
Macoy Madson 1 year ago
parent
commit
b74351b679
  1. 40
      src/DataBundle.cake
  2. 5
      src/WindowsHeader.cake

40
src/DataBundle.cake

@ -74,7 +74,7 @@
(when (isalnum (deref current-char))
(set (deref buffer-write) (deref current-char))
(incr buffer-write)
(when (>= buffer-write (- out-buffer-size 1))
(when (>= (- buffer-write out-buffer) (- out-buffer-size 1))
(break))))
(set (deref buffer-write) 0))
@ -116,7 +116,7 @@
;; I'm not actually sure how long these can be!
(var safe-resource-name ([] 1024 char) (array 0))
(data-bundle-create-safe-resource-name
filename safe-resource-name (array-size safe-resource-name))
(call-on c_str filename) safe-resource-name (array-size safe-resource-name))
;; Note that while we could bundle all our resources into a single RC file, we'd rather build
;; them separately so they don't all have to be recompiled if one changes
(var resource-definition-file ([] 1024 char) (array 0))
@ -160,48 +160,55 @@
(comptime-cond
('Windows
(defun-comptime bundle-generate-runtime-load ()
(defun-comptime bundle-generate-runtime-load (environment (& EvaluatorEnvironment)
was-code-modified (& bool) &return bool)
(scope
(get-or-create-comptime-var bundle-files-processed bool false)
(when (deref bundle-files-processed)
(return true))
(set (deref bundle-files-processed) true))
(get-or-create-comptime-var files-to-bundle (<> (in std vector) (in std string)))
(get-or-create-comptime-var data-bundle-invocations (<> (in std vector) (* (const Token))))
(var load-resources-body (* (<> std::vector Token)) (new (<> std::vector Token)))
(call-on push_back (field environment comptimeTokens) load-resources-body)
;; Generate the runtime code for finding the resource
(var start-var-name-token (* (const Token)) (at i data-bundle-invocations))
(each-in-range (call-on-ptr size data-bundle-invocations) i
;; Generate the runtime code for finding the resource
(var start-var-name-token (* (const Token)) (at i (deref data-bundle-invocations)))
(var end-var-name-token (* (const Token)) (+ start-var-name-token 1))
(var base-type-start-token (* (const Token)) (+ start-var-name-token 2))
(var type-token (* (const Token)) (at i data-bundle-invocations))
(var type-token (* (const Token)) (+ start-var-name-token 3))
(var resource-name-token Token (deref start-var-name-token))
(set (field resource-name-token type) TokenType_String)
(var safe-resource-name ([] 1024 char) (array 0))
(scope
(var filename (& (const (in std string))) (at i (deref files-to-bundle)))
(data-bundle-create-safe-resource-name
(call-on c_str filename) safe-resource-name (array-size safe-resource-name)))
(set (field resource-name-token contents) safe-resource-name)
(tokenize-push (deref load-resources-body)
(scope
(var resource data-bundle-resource
(data-bundle-get-resource (token-splice resource-name-token)))
(data-bundle-get-resource (token-splice-addr resource-name-token)))
(unless (field resource start-resource)
(return false))
(set (token-splice start-var-name-token)
(type-cast (field resource start-resource) (* (token-splice base-type-start-token))))
(set (token-splice end-var-name-token)
(type-cast (field resource end-resource) (* (token-splice base-type-start-token))))))
(type-cast (field resource end-resource) (* (token-splice base-type-start-token)))))))
(var load-all-resources-function (* (<> std::vector Token)) (new (<> std::vector Token)))
(call-on push_back (field environment comptimeTokens) load-all-resources-function)
(tokenize-push (deref load-all-resources-function)
(defun data-bundle-load-all-resources (&return bool)
;; TODO: This will break if we're loading from a DLL!
(var h-module HMODULE null)
(token-splice-array (deref load-resources-body))
(return true)))
(unless (ReplaceAndEvaluateDefinition environment
"data-bundle-load-all-resources"
(deref load-all-resources-function))
(return false)))
(return false))
(return true))
(add-compile-time-hook post-references-resolved bundle-generate-runtime-load)))
(defstruct-local data-bundle-resource
@ -210,11 +217,16 @@
(comptime-cond
('Windows
(import "WindowsHeader.cake")
(c-import "<stdio.h>")
(defun-local data-bundle-get-resource (resource-name (* (const char))
&return data-bundle-resource)
;; TODO: This will break if we're loading from a DLL!
(var h-module HMODULE null)
(var final-resource data-bundle-resource (array 0))
(var resource-info HRSRC
(FindResourceA h-module "CUSTOMDATA"))
(FindResourceA h-module "CUSTOMDATA" resource-name))
(unless resource-info
(fprintf stderr "Could not find resource %s\n" resource-name)
(return final-resource))
@ -224,14 +236,14 @@
(fprintf stderr "Could not load resource %s\n" resource-name)
(return final-resource))
(var resource-address HVOID (LockResource h-module resource))
(var resource-address LPVOID (LockResource resource))
(unless resource-address
(fprintf stderr "Could not lock resource %s\n" resource-name)
(return final-resource))
(var resource-size DWORD (SizeofResource h-module resource-info))
(set (field final-resource start-resource) (type-cast resource-address (* void)))
(set (field final-resource end-resource) (+ (type-cast resource-address (* void))
(set (field final-resource end-resource) (+ (type-cast resource-address (* char))
resource-size))
(return final-resource))))

5
src/WindowsHeader.cake

@ -0,0 +1,5 @@
(export
(c-preprocessor-define WIN32_LEAN_AND_MEAN)
(c-import "windows.h"
;; For Zig cross-compilation
"minwindef.h"))
Loading…
Cancel
Save