Outpost Universe Forums

Off Topic => Computers & Programming General => Topic started by: Vagabond on February 09, 2018, 09:56:33 PM

Title: Embedded File Resources on Linux
Post by: Vagabond on February 09, 2018, 09:56:33 PM
I am curious, are file resources (such as embedding icons and string resouces in an exe or dll) an accepted practice on Linux as it is on Windows? I'm not a Linux user.

If so, is anyone aware of a nice cross platform way to access a resource from C or C++?

Thanks,
Brett
Title: Re: Embedded File Resources on Linux
Post by: Hooman on February 14, 2018, 09:10:49 PM
Hmm, good question.

There is a relevant looking article on StackOverflow about Embedding Binary Blobs Using GCC/MinGW (https://stackoverflow.com/questions/2627004/embedding-binary-blobs-using-gcc-mingw).

The solution looks compiler specific, though this likely will be. The Microsoft resource compiler is of course a compiler specific tool. I'm not aware of any standard C++ language features that let you directly include an external file and make it accessible in memory through a symbol name. I know the NASM assembler has such a feature built in. Futher, NASM is cross platform, so it's likely such features could be implemented on multiple platforms. It just seems like the tooling is fragmented here, so you'd need to do it a different way for each platform, or rely on external tools, such as NASM, or possibly have a converter the builds a C++ byte array out of the file, effectively converting the binary data to C++ source code.
Title: Re: Embedded File Resources on Linux
Post by: Vagabond on February 15, 2018, 10:17:24 PM
Thanks Hooman.

Sounds like embedding resources would be carried out by the specific compiler/IDE.

In the context of OP2Archive, we could look at defining a function that took a resource key and returned a stream of the resource. This function would need separate bodies, one for Linux and one for Windows. The returned stream would be platform agnostic allowing the rest of the code to handle it regardless of platform concerns.

This function could be an interface or abstract function that was implemented for each platform. Or the body of the function could be a preprocessor directive redirect to the platform specific code.

Then the Archive code would need modifiying to include a constructor that takes a Stream.

I think the different StreamReaders in OP2Utility are already platform agnostic.

The rest of the Archive code would still be platform specific, but at least we wouldn't be adding to the problem.

I took a look at the Windows side of the function. No problem embedding the VolTemplate.vol as a custom resource. I understand the code to get a handle to the resource. I don't understand handles enough to figure out how to get a Stream from the resource handle.

We would just need to make sure the resource is referenced to the same key for both the Linux and Windows build. (If this is possible?)

-Brett
Title: Re: Embedded File Resources on Linux
Post by: leeor_net on February 20, 2018, 09:37:40 PM
Can use the bin2c method:

https://github.com/gwilymk/bin2c

I have similar code that I use for this sort of thing.
Title: Re: Embedded File Resources on Linux
Post by: Vagabond on February 21, 2018, 11:11:53 PM
Thanks Leeor. It looks like they are embedding it using about 115 lines of C code. Very promising and they have a permissive license. I'd like to look at this closer in the future.

-Brett