C4Script: Allow executing strings from cmdline instead of only files

shapetextures
Nicolas Hake 2015-09-04 13:40:27 +02:00
parent 2b876a6cb6
commit 4ae2d9f70b
3 changed files with 57 additions and 15 deletions

View File

@ -1,7 +1,7 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2012-2013, The OpenClonk Team and contributors
* Copyright (c) 2012-2015, The OpenClonk Team and contributors
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
@ -20,7 +20,8 @@
extern "C" {
#endif
int c4s_runscript(const char * filename);
int c4s_runfile(const char *filename);
int c4s_runstring(const char *script);
#ifdef __cplusplus
}

View File

@ -1,7 +1,7 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2012-2013, The OpenClonk Team and contributors
* Copyright (c) 2012-2015, The OpenClonk Team and contributors
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
@ -17,7 +17,30 @@
// c4script.h is useable without that.
#include "../../include/c4script/c4script.h"
#include <stdio.h>
#include <string.h>
int usage(const char *argv0)
{
fprintf(stderr, "Usage:\n%s -e <script>\n%s <file>\n", argv0, argv0);
return 1;
}
int main(int argc, const char * argv[])
{
return c4s_runscript(argv[1]);
if (argc < 2)
return usage(argv[0]);
if (strcmp(argv[1], "-e") == 0)
{
if (argc != 3)
return usage(argv[0]);
return c4s_runstring(argv[2]);
}
else
{
if (argc != 2)
return usage(argv[0]);
return c4s_runfile(argv[1]);
}
}

View File

@ -1,7 +1,7 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2011-2013, The OpenClonk Team and contributors
* Copyright (c) 2011-2015, The OpenClonk Team and contributors
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
@ -24,13 +24,29 @@
#include "object/C4DefList.h"
#include "script/C4ScriptHost.h"
int c4s_runscript(const char * filename)
void InitializeC4Script()
{
InitCoreFunctionMap(&ScriptEngine);
// Seed PRNG
FixedRandom(time(NULL));
}
C4Value RunLoadedC4Script()
{
// Link script engine (resolve includes/appends, generate code)
ScriptEngine.Link(&::Definitions);
// Set name list for globals
ScriptEngine.GlobalNamed.SetNameList(&ScriptEngine.GlobalNamedNames);
C4Value result = GameScript.Call("Main");
GameScript.Clear();
ScriptEngine.Clear();
return result;
}
int c4s_runfile(const char * filename)
{
C4Group File;
if (!File.Open(GetWorkingDirectory()))
{
@ -46,15 +62,17 @@ int c4s_runscript(const char * filename)
fprintf(stderr, "FindNextEntry failed: %s\n", File.GetError());
return 1;
}
InitializeC4Script();
GameScript.Load(File, fn.getData(), NULL, NULL);
// Link script engine (resolve includes/appends, generate code)
ScriptEngine.Link(&::Definitions);
// Set name list for globals
ScriptEngine.GlobalNamed.SetNameList(&ScriptEngine.GlobalNamedNames);
GameScript.Call("Main");
GameScript.Clear();
ScriptEngine.Clear();
RunLoadedC4Script();
return 0;
}
int c4s_runstring(const char *script)
{
InitializeC4Script();
GameScript.LoadData("<memory>", script, NULL);
RunLoadedC4Script();
return 0;
}