Embedding text assets during build into code

I am having some weird issues on a few Android systems when reading json data. Somehow some of the json files are not being read properly, or from time to time are corrupted - although they are properly in the APK file. ( Doesn’t happen for most uses, but for some )

This leads to some levels being broken, or language files being not loaded correctly.

Is there a way to automatically embed text assets into code directly during the release build process? So instead of me loading the e.g. levels.json file during execution, it would be already embedded in the cpp code

Something like that already in place somewhere?

You should be able to do it with macro. Have a macro function which loads JSON at compile-time and then returns string, so it’s inlined. Like this:

package build;

#if macro
import sys.io.Process;
#end

class BuildInfo 
{
	macro public static function buildDate()
	{
		var ret = Date.now().toString();
		return macro $v{ret};
	}

	macro public static function gitVersion()
	{
		var branch = new sys.io.Process("git", ["rev-parse", "--abbrev-ref", "HEAD"]).stdout.readLine();
		var version = new sys.io.Process("git", ["describe", "--always"]).stdout.readLine();
		var ret = version + "-" + branch;
		return macro $v{ret};
	}
}

Try <assets path="to/json/files" embed="true" />

This sounds like a strange issue, though. Any pattern in where or whom it occurs on?

Maybe UTF8 or not fully downloaded APK? Memory issues? I’m really not sure, it didn’t happen for many. But it was really weird indeed. Which asset files would you usually recommend to be embedded? Any reason not to embed all?

The Haxe compiler hits a memory limit if you try and embed all your assets, also, the more you embed, the more it (may) slow down the startup time for your executable. It also increases the memory used when you run your executable, I believe, so there’s a balance to it :slight_smile:

True, just tried with embedded TTFs and that was not nice :stuck_out_tongue: Ok, will limit to .jsons only for now :slight_smile: