OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4ScriptStandalone.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2011-2016, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #include "../../include/c4script/c4script.h"
17 
18 #include "C4Include.h"
19 
20 #include "lib/C4Random.h"
21 #include "c4group/C4Group.h"
22 #include "script/C4Aul.h"
23 #include "script/C4ScriptHost.h"
24 #include "object/C4DefList.h"
25 
26 /* StandaloneStubs.cpp is shared with mape, which has a real implementation of these */
27 C4Def* C4DefList::GetByName(const StdStrBuf &) {return nullptr;}
28 C4Def * C4DefList::GetDef(int) {return 0;}
29 int C4DefList::GetDefCount() {return 0;}
33 
34 static void InitializeC4Script()
35 {
37 
38  // Seed PRNG
39  FixedRandom(time(nullptr));
40 }
41 
42 static void ClearC4Script()
43 {
44  GameScript.Clear();
46 }
47 
48 static C4Value RunLoadedC4Script()
49 {
50  // Link script engine (resolve includes/appends, generate code)
51  ScriptEngine.Link(nullptr);
52 
53  C4Value result = GameScript.Call("Main");
54  return result;
55 }
56 
57 static int RunFile(const char * filename, bool checkOnly)
58 {
59  C4Group File;
60  if (!File.Open(GetWorkingDirectory()))
61  {
62  fprintf(stderr, "Open failed: %s\n", File.GetError());
63  return 1;
64  }
65 
66  // get scripts
67  StdStrBuf fn;
68  File.ResetSearch();
69  if (!File.FindNextEntry(filename, &fn))
70  {
71  fprintf(stderr, "FindNextEntry failed: %s\n", File.GetError());
72  return 1;
73  }
74 
75  InitializeC4Script();
76  GameScript.Load(File, fn.getData(), nullptr, nullptr);
77  if (!checkOnly)
78  RunLoadedC4Script();
79  ClearC4Script();
80  return ScriptEngine.errCnt;
81 }
82 
83 static int RunString(const char *script, bool checkOnly)
84 {
85  InitializeC4Script();
86  GameScript.LoadData("<memory>", script, nullptr);
87  if (!checkOnly)
88  RunLoadedC4Script();
89  ClearC4Script();
90  return ScriptEngine.errCnt;
91 }
92 
93 
94 int c4s_runfile(const char *filename) { return RunFile(filename, false); }
95 int c4s_runstring(const char *script) { return RunString(script, false); }
96 
97 int c4s_checkfile(const char *filename) { return RunFile(filename, true); }
98 int c4s_checkstring(const char *script) { return RunString(script, true); }
const char * getData() const
Definition: StdBuf.h:450
int c4s_checkfile(const char *filename)
C4Def * GetDef(int32_t Index)
C4GameScriptHost GameScript
C4AulScriptEngine ScriptEngine
Definition: C4Globals.cpp:43
int c4s_runfile(const char *filename)
C4Value Call(const char *szFunction, C4AulParSet *pPars=0, bool fPassError=false)
int32_t GetDefCount()
virtual bool Load(C4Group &, const char *, const char *, C4LangStringTable *)
void Link(C4DefList *rDefs)
Definition: C4AulLink.cpp:165
const char * GetWorkingDirectory()
Definition: StdFile.cpp:613
void CallEveryDefinition()
Definition: stub-handle.cpp:77
bool Open(const char *szGroupName, bool fCreate=false)
Definition: C4Group.cpp:514
int c4s_checkstring(const char *script)
Definition: C4Def.h:100
int c4s_runstring(const char *script)
C4Def * GetByName(const StdStrBuf &)
const char * GetError()
Definition: C4Group.cpp:504
void ResetIncludeDependencies()
Definition: stub-handle.cpp:78
void SortByPriority()
Definition: stub-handle.cpp:76
bool FindNextEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr, bool fStartAtFilename=false)
Definition: C4Group.cpp:1780
void InitCoreFunctionMap(C4AulScriptEngine *pEngine)
Definition: C4Script.cpp:1141
virtual bool LoadData(const char *, const char *, C4LangStringTable *)
void FixedRandom(uint64_t seed)
Definition: C4Random.cpp:37
void ResetSearch(bool reload_contents=false)
Definition: C4Group.cpp:1013