forked from Mirrors/openclonk
C4Script: Allow executing strings from cmdline instead of only files
parent
2b876a6cb6
commit
4ae2d9f70b
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* OpenClonk, http://www.openclonk.org
|
* 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
|
* Distributed under the terms of the ISC license; see accompanying file
|
||||||
* "COPYING" for details.
|
* "COPYING" for details.
|
||||||
|
@ -20,7 +20,8 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int c4s_runscript(const char * filename);
|
int c4s_runfile(const char *filename);
|
||||||
|
int c4s_runstring(const char *script);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* OpenClonk, http://www.openclonk.org
|
* 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
|
* Distributed under the terms of the ISC license; see accompanying file
|
||||||
* "COPYING" for details.
|
* "COPYING" for details.
|
||||||
|
@ -17,7 +17,30 @@
|
||||||
// c4script.h is useable without that.
|
// c4script.h is useable without that.
|
||||||
#include "../../include/c4script/c4script.h"
|
#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[])
|
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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* OpenClonk, http://www.openclonk.org
|
* 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
|
* Distributed under the terms of the ISC license; see accompanying file
|
||||||
* "COPYING" for details.
|
* "COPYING" for details.
|
||||||
|
@ -24,13 +24,29 @@
|
||||||
#include "object/C4DefList.h"
|
#include "object/C4DefList.h"
|
||||||
#include "script/C4ScriptHost.h"
|
#include "script/C4ScriptHost.h"
|
||||||
|
|
||||||
int c4s_runscript(const char * filename)
|
void InitializeC4Script()
|
||||||
{
|
{
|
||||||
InitCoreFunctionMap(&ScriptEngine);
|
InitCoreFunctionMap(&ScriptEngine);
|
||||||
|
|
||||||
// Seed PRNG
|
// Seed PRNG
|
||||||
FixedRandom(time(NULL));
|
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;
|
C4Group File;
|
||||||
if (!File.Open(GetWorkingDirectory()))
|
if (!File.Open(GetWorkingDirectory()))
|
||||||
{
|
{
|
||||||
|
@ -46,15 +62,17 @@ int c4s_runscript(const char * filename)
|
||||||
fprintf(stderr, "FindNextEntry failed: %s\n", File.GetError());
|
fprintf(stderr, "FindNextEntry failed: %s\n", File.GetError());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InitializeC4Script();
|
||||||
GameScript.Load(File, fn.getData(), NULL, NULL);
|
GameScript.Load(File, fn.getData(), NULL, NULL);
|
||||||
|
RunLoadedC4Script();
|
||||||
// Link script engine (resolve includes/appends, generate code)
|
return 0;
|
||||||
ScriptEngine.Link(&::Definitions);
|
}
|
||||||
|
|
||||||
// Set name list for globals
|
int c4s_runstring(const char *script)
|
||||||
ScriptEngine.GlobalNamed.SetNameList(&ScriptEngine.GlobalNamedNames);
|
{
|
||||||
GameScript.Call("Main");
|
InitializeC4Script();
|
||||||
GameScript.Clear();
|
GameScript.LoadData("<memory>", script, NULL);
|
||||||
ScriptEngine.Clear();
|
RunLoadedC4Script();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue