Merge pull request from Wargus/tim/lua-args

Allow passing a string as argument to the Lua startup file
This commit is contained in:
Andrettin 2015-12-04 17:43:54 +01:00
commit bf514a2027
6 changed files with 35 additions and 11 deletions

View file

@ -46,6 +46,7 @@ public:
std::string applicationName;
std::string luaStartFilename;
std::string luaEditorStartFilename;
std::string luaScriptArguments;
std::string LocalPlayerName; /// Name of local player
private:
std::string userDirectory; /// Directory containing user settings and data

View file

@ -68,7 +68,7 @@ enum {
extern lua_State *Lua;
extern int LuaLoadFile(const std::string &file);
extern int LuaLoadFile(const std::string &file, const std::string &strArg = "");
extern int LuaCall(int narg, int clear, bool exitOnError = true);
#define LuaError(l, args) \
@ -310,7 +310,7 @@ extern bool LuaToBoolean(lua_State *l, int index, int subIndex);
extern void LuaGarbageCollect(); /// Perform garbage collection
extern void InitLua(); /// Initialise Lua
extern void LoadCcl(const std::string &filename); /// Load ccl config file
extern void LoadCcl(const std::string &filename, const std::string &luaArgStr = ""); /// Load ccl config file
extern void SavePreferences(); /// Save user preferences
extern int CclCommand(const std::string &command, bool exitOnError = true);

View file

@ -1507,7 +1507,7 @@ void NetworkServerStartGame()
}
}
#if 0
#ifdef DEBUG
printf("INITIAL ServerSetupState:\n");
for (int i = 0; i < PlayerMax - 1; ++i) {
printf("%02d: CO: %d Race: %d Host: ", i, ServerSetupState.CompOpt[i], ServerSetupState.Race[i]);

View file

@ -615,6 +615,16 @@ static void LibraryFileName(const char *file, char(&buffer)[PATH_MAX])
return;
}
// Support for scripts in default scripts dir.
sprintf(buffer, "scripts/%s", file);
if (FindFileWithExtension(buffer)) {
return;
}
sprintf(buffer, "%s/scripts/%s", StratagusLibPath.c_str(), file);
if (FindFileWithExtension(buffer)) {
return;
}
DebugPrint("File `%s' not found\n" _C_ file);
strcpy_s(buffer, PATH_MAX, file);
}

View file

@ -205,10 +205,11 @@ static bool GetFileContent(const std::string &file, std::string &content)
** Load a file and execute it
**
** @param file File to load and execute
** @param nargs Number of arguments that caller has put on the stack
**
** @return 0 for success, else exit.
*/
int LuaLoadFile(const std::string &file)
int LuaLoadFile(const std::string &file, const std::string &strArg)
{
DebugPrint("Loading '%s'\n" _C_ file.c_str());
@ -219,7 +220,12 @@ int LuaLoadFile(const std::string &file)
const int status = luaL_loadbuffer(Lua, content.c_str(), content.size(), file.c_str());
if (!status) {
LuaCall(0, 1);
if (!strArg.empty()) {
lua_pushstring(Lua, strArg.c_str());
LuaCall(1, 1);
} else {
LuaCall(0, 1);
}
} else {
report(status, true);
}
@ -2385,7 +2391,7 @@ void SavePreferences()
/**
** Load stratagus config file.
*/
void LoadCcl(const std::string &filename)
void LoadCcl(const std::string &filename, const std::string &luaArgStr)
{
// Load and evaluate configuration file
CclInConfigFile = 1;
@ -2396,12 +2402,11 @@ void LoadCcl(const std::string &filename)
}
ShowLoadProgress(_("Script %s\n"), name.c_str());
LuaLoadFile(name);
LuaLoadFile(name, luaArgStr);
CclInConfigFile = 0;
LuaGarbageCollect();
}
void ScriptRegister()
{
AliasRegister();

View file

@ -457,6 +457,7 @@ static void Usage()
"\t-e\t\tStart editor (instead of game)\n"
"\t-E file.lua\tEditor configuration start file (default editor.lua)\n"
"\t-F\t\tFull screen video mode\n"
"\t-G \"[options]\"\tGame options (passed to game scripts)\n"
"\t-h\t\tHelp shows this page\n"
"\t-i\t\tEnables unit info dumping into log (for debugging)\n"
"\t-I addr\t\tNetwork address to use\n"
@ -530,12 +531,16 @@ static void RedirectOutput()
void ParseCommandLine(int argc, char **argv, Parameters &parameters)
{
for (;;) {
switch (getopt(argc, argv, "ac:d:D:eE:FhiI:lN:oOP:ps:S:u:v:Wx:Z?")) {
switch (getopt(argc, argv, "ac:d:D:eE:FG:hiI:lN:oOP:ps:S:u:v:Wx:Z?-")) {
case 'a':
EnableAssert = true;
continue;
case 'c':
parameters.luaStartFilename = optarg;
if (strlen(optarg) > 4 &&
!(strstr(optarg, ".lua") == optarg + strlen(optarg) - 4)) {
parameters.luaStartFilename += ".lua";
}
continue;
case 'd': {
StratagusLibPath = optarg;
@ -558,6 +563,9 @@ void ParseCommandLine(int argc, char **argv, Parameters &parameters)
VideoForceFullScreen = 1;
Video.FullScreen = 1;
continue;
case 'G':
parameters.luaScriptArguments = optarg;
continue;
case 'i':
EnableUnitDebug = true;
continue;
@ -658,7 +666,7 @@ void ParseCommandLine(int argc, char **argv, Parameters &parameters)
}
if (argc - optind > 1) {
fprintf(stderr, "too many files\n");
fprintf(stderr, "too many map files. if you meant to pass game arguments, these go after '--'\n");
Usage();
ExitFatal(-1);
}
@ -751,7 +759,7 @@ int stratagusMain(int argc, char **argv)
// Initialise AI module
InitAiModule();
LoadCcl(parameters.luaStartFilename);
LoadCcl(parameters.luaStartFilename, parameters.luaScriptArguments);
PrintHeader();
PrintLicense();