Skip to content

Commit

Permalink
Add automatic conf.lua loading
Browse files Browse the repository at this point in the history
- Fixes #58
  • Loading branch information
RobLoach committed Jun 25, 2016
1 parent 88dca56 commit 7b9f0cb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
16 changes: 14 additions & 2 deletions lutro.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,21 +388,29 @@ int lutro_unzip(const char *path, const char *extraction_directory)
int lutro_load(const char *path)
{
char mainfile[PATH_MAX_LENGTH];
// conf.lua https://love2d.org/wiki/Config_Files
char conffile[PATH_MAX_LENGTH];
char gamedir[PATH_MAX_LENGTH];

strlcpy(mainfile, path, PATH_MAX_LENGTH);
strlcpy(conffile, path, PATH_MAX_LENGTH);
strlcpy(gamedir, path, PATH_MAX_LENGTH);

if (path_is_directory(mainfile))
if (path_is_directory(mainfile)) {
fill_pathname_join(mainfile, gamedir, "main.lua", sizeof(mainfile));
else
fill_pathname_join(conffile, gamedir, "conf.lua", sizeof(conffile));
}
else {
path_basedir(gamedir);
}

if (!strcmp(path_get_extension(mainfile), "lutro"))
{
fill_pathname(gamedir, mainfile, "/", sizeof(gamedir));
fill_pathname(gamedir, conffile, "/", sizeof(gamedir));
lutro_unzip(mainfile, gamedir);
fill_pathname_join(mainfile, gamedir, "main.lua", sizeof(mainfile));
fill_pathname_join(conffile, gamedir, "conf.lua", sizeof(conffile));
}

fill_pathname_slash(gamedir, sizeof(gamedir));
Expand All @@ -411,6 +419,10 @@ int lutro_load(const char *path)
snprintf(package_path, PATH_MAX_LENGTH, ";%s?.lua;%s?/init.lua", gamedir, gamedir);
lutro_set_package_path(L, package_path);

// Load the configuration file, ignoring any errors.
dofile(L, conffile);

// Now that configuration is in place, load main.lua.
if(dofile(L, mainfile))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
Expand Down
Binary file added test.lutro
Binary file not shown.
4 changes: 4 additions & 0 deletions test/conf.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function lutro.conf(t)
t.width = 640
t.height = 480
end
9 changes: 2 additions & 7 deletions test/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@ local intervalTime = 2.5
local joystickButton = 0
local keypressed = ""

function lutro.conf(t)
t.width = 640
t.height = 480
function lutro.load()
lutro.graphics.setBackgroundColor(0, 0, 0)

-- Initiate all available test states.
for i, state in ipairs(availableStates) do
local test = require(state)
test['name'] = "lutro." .. string.gsub(state, "/", ".")
table.insert(states, test)
end
end

function lutro.load()
lutro.graphics.setBackgroundColor(0, 0, 0)

-- Load all states.
for i, state in ipairs(states) do
Expand Down

0 comments on commit 7b9f0cb

Please sign in to comment.