1

How can I build my C++ project to include all the Lua files in just one executable? I don't want people being able to edit the Lua files and alter the game.

Is it possible, and is it ethical?

Ethan Webster
  • 719
  • 2
  • 6
  • 11
  • 1
    See also http://stackoverflow.com/questions/194520/creating-standalone-lua-executables. – lhf Apr 06 '14 at 11:18
  • 1
    you can also do it textually like in [premake](https://github.com/annulen/premake/blob/master/scripts/embed.lua) and statically obfuscate (i.e. encrypt or compress) the strings if you want messing with the scripts to be harder – Dmitry Ledentsov Apr 07 '14 at 15:27

2 Answers2

2

Use "embedded bytecode" feature of LuaJIT:

To statically embed the bytecode of a module in your application, generate an object file and just link it with your application.

require() tries to load embedded bytecode data from exported symbols (in *.exe or lua51.dll on Windows) and from shared libraries in package.cpath.

Example:

luajit -b test.lua test.obj                 # Generate object file
# Link test.obj with your application and load it with require("test")
Community
  • 1
  • 1
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
1

Well, you want to put all Lua sources into your executable, so you need this:

  • A means of mapping file-names to the Lua files:

    char lua_files[]={
        "filename1\0filecontents1",
        "filename2\0filecontents2",
        0,
    };
    

    This can be created automatically, by a script reading your directory of Lua files when building.
    Optionally, it might even pre-compile the files, so you can remove the parser from your embeddded Lua library.

  • A custom loader:
    Write a short Lua C function, which searches the array and returns load and a string encompassing the found filecontents as a Lua string.
    Register it by placing it into package.searchers.

If you later want to allow loading custom scripts somehow, retain the old searchers and make sure you somehow require files not in your sources.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118