OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Script.cpp File Reference
#include "C4Include.h"
#include "script/C4AulDefFunc.h"
#include "script/C4AulExec.h"
#include "lib/C4Random.h"
#include "C4Version.h"
#include <regex>
Include dependency graph for C4Script.cpp:

Go to the source code of this file.

Macros

#define MAKE_AND_RETURN_ARRAY(values)
 
#define F(f)   ::AddFunc(p, #f, Fn##f)
 

Functions

StdStrBuf FnStringFormat (C4PropList *_this, C4String *szFormatPar, C4Value *Pars, int ParCount)
 
void InitCoreFunctionMap (C4AulScriptEngine *pEngine)
 

Variables

C4ScriptConstDef C4ScriptConstMap []
 
C4ScriptFnDef C4ScriptFnMap []
 

Macro Definition Documentation

#define F (   f)    ::AddFunc(p, #f, Fn##f)

Referenced by InitCoreFunctionMap().

#define MAKE_AND_RETURN_ARRAY (   values)
Value:
do { \
C4ValueArray *matrix = new C4ValueArray(sizeof(values) / sizeof(*values)); \
for (size_t i = 0; i < sizeof(values) / sizeof(*values); ++i) \
(*matrix)[i] = C4VInt(values[i]); \
return matrix; \
} while (0)
C4Value C4VInt(int32_t i)
Definition: C4Value.h:242

Definition at line 130 of file C4Script.cpp.

Function Documentation

StdStrBuf FnStringFormat ( C4PropList _this,
C4String szFormatPar,
C4Value Pars,
int  ParCount 
)

Definition at line 31 of file C4Script.cpp.

References StdStrBuf::Append(), StdStrBuf::AppendChar(), StdStrBuf::AppendCharacter(), StdStrBuf::AppendFormat(), C4String::GetCStr(), C4Value::getStr(), Inside(), SCopy(), and SLen().

32 {
33  int cPar=0;
34 
35  StdStrBuf StringBuf("", false);
36  const char * cpFormat = FnStringPar(szFormatPar);
37  const char * cpType;
38  char szField[20];
39  while (*cpFormat)
40  {
41  // Copy normal stuff
42  while (*cpFormat && (*cpFormat!='%'))
43  StringBuf.AppendChar(*cpFormat++);
44  // Field
45  if (*cpFormat=='%')
46  {
47  // Scan field type
48  for (cpType=cpFormat+1; *cpType && (*cpType == '+' || *cpType == '-' || *cpType == '.' || *cpType == '#' || *cpType == ' ' || Inside(*cpType,'0','9')); cpType++) {}
49  // Copy field
50  SCopy(cpFormat,szField,std::min<unsigned int>(sizeof szField - 1, cpType - cpFormat + 1));
51  // Insert field by type
52  switch (*cpType)
53  {
54  // number
55  case 'd': case 'x': case 'X':
56  {
57  if (cPar >= ParCount) throw C4AulExecError("format placeholder without parameter");
58  StringBuf.AppendFormat(szField, Pars[cPar++].getInt());
59  cpFormat+=SLen(szField);
60  break;
61  }
62  // character
63  case 'c':
64  {
65  if (cPar >= ParCount) throw C4AulExecError("format placeholder without parameter");
66  StringBuf.AppendCharacter(Pars[cPar++].getInt());
67  cpFormat+=SLen(szField);
68  break;
69  }
70  // C4ID
71  case 'i':
72  // C4Value
73  case 'v':
74  {
75  if (cPar >= ParCount) throw C4AulExecError("format placeholder without parameter");
76  StringBuf.Append(static_cast<const StdStrBuf&>(Pars[cPar++].GetDataString(10)));
77  cpFormat+=SLen(szField);
78  break;
79  }
80  // String
81  case 's':
82  {
83  // get string
84  if (cPar >= ParCount) throw C4AulExecError("format placeholder without parameter");
85  const char *szStr = "(null)";
86  if (Pars[cPar].GetData())
87  {
88  C4String * pStr = Pars[cPar].getStr();
89  if (!pStr) throw C4AulExecError("string format placeholder without string");
90  szStr = pStr->GetCStr();
91  }
92  ++cPar;
93  StringBuf.AppendFormat(szField, szStr);
94  cpFormat+=SLen(szField);
95  break;
96  }
97  case '%':
98  StringBuf.AppendChar('%');
99  cpFormat+=SLen(szField);
100  break;
101  // Undefined / Empty
102  default:
103  StringBuf.AppendChar('%');
104  cpFormat++;
105  break;
106  }
107  }
108  }
109  return StringBuf;
110 }
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:129
C4String * getStr() const
Definition: C4Value.h:117
const char * GetCStr() const
Definition: C4StringTable.h:49
size_t SLen(const char *sptr)
Definition: Standard.h:78
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45

Here is the call graph for this function:

void InitCoreFunctionMap ( C4AulScriptEngine pEngine)

Definition at line 1141 of file C4Script.cpp.

References Abs(), AddFunc(), Angle(), C4V_Int, C4VInt(), Cos(), Distance(), F, C4AulExec::FnLogCallStack(), C4AulExec::FnTranslate(), C4AulScriptEngine::GetPropList(), C4ScriptConstDef::Identifier, Inside(), Random(), C4AulScriptEngine::RegisterGlobalConstant(), Sin(), and WildcardMatch().

Referenced by c4_mapgen_handle_init_script_engine(), and C4Game::InitScriptEngine().

1142 {
1143  // add all def constants (all Int)
1144  for (C4ScriptConstDef *pCDef = &C4ScriptConstMap[0]; pCDef->Identifier; pCDef++)
1145  {
1146  assert(pCDef->ValType == C4V_Int); // only int supported currently
1147  pEngine->RegisterGlobalConstant(pCDef->Identifier, C4VInt(pCDef->Data));
1148  }
1149 
1150  C4PropListStatic * p = pEngine->GetPropList();
1151  // add all def script funcs
1152  for (C4ScriptFnDef *pDef = &C4ScriptFnMap[0]; pDef->Identifier; pDef++)
1153  new C4AulDefFunc(p, pDef);
1154 #define F(f) ::AddFunc(p, #f, Fn##f)
1155  F(ParseInt);
1156  F(Abs);
1157  F(Min);
1158  F(Max);
1159  F(Sin);
1160  F(Cos);
1161  F(Sqrt);
1162  F(ArcSin);
1163  F(ArcCos);
1164  F(BoundBy);
1165  F(Inside);
1166  F(Random);
1167 
1168  F(CreateArray);
1169  F(CreatePropList);
1170  F(GetProperties);
1171  F(GetProperty);
1172  F(SetProperty);
1173  F(GetPrototype);
1174  F(SetPrototype);
1175  F(ResetProperty);
1176  F(GetName);
1177  F(AddEffect);
1178  F(CreateEffect);
1179  F(CheckEffect);
1180  F(RemoveEffect);
1181  F(GetEffect);
1182  F(GetEffectCount);
1183  F(RegexReplace);
1184  F(RegexSearch);
1185  F(RegexMatch);
1186  F(RegexSplit);
1187  F(Distance);
1188  F(Angle);
1189  F(GetChar);
1190  F(GetType);
1191  F(ModulateColor);
1192  F(WildcardMatch);
1193  F(GetLength);
1194  F(SetLength);
1195  F(GetIndexOf);
1196  F(DeepEqual);
1197  F(FatalError);
1198  F(StartCallTrace);
1199  F(StartScriptProfiler);
1200  F(StopScriptProfiler);
1201  F(SortArray);
1202  F(SortArrayByProperty);
1203  F(SortArrayByArrayElement);
1204  F(Trans_Identity);
1205  F(Trans_Translate);
1206  F(Trans_Scale);
1207  F(Trans_Rotate);
1208  F(LocateFunc);
1209  F(FileWrite);
1210  F(eval);
1211  F(StringToIdentifier);
1212  F(GetConstantNameByValue);
1213  F(ReplaceString);
1214 
1215  ::AddFunc(p, "Translate", C4AulExec::FnTranslate);
1216  ::AddFunc(p, "LogCallStack", C4AulExec::FnLogCallStack);
1217 #undef F
1218 }
void RegisterGlobalConstant(const char *szName, const C4Value &rValue)
static bool FnLogCallStack(C4PropList *_this)
Definition: C4AulExec.cpp:134
C4ScriptFnDef C4ScriptFnMap[]
Definition: C4Script.cpp:1129
uint32_t Random()
Definition: C4Random.cpp:43
C4PropListStatic * GetPropList()
Definition: C4Aul.h:153
C4Value C4VInt(int32_t i)
Definition: C4Value.h:242
int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2)
Definition: Standard.cpp:24
C4ScriptConstDef C4ScriptConstMap[]
Definition: C4Script.cpp:1080
const char * Identifier
Definition: C4AulDefFunc.h:277
#define F(f)
static C4String * FnTranslate(C4PropList *_this, C4String *text)
Definition: C4AulExec.cpp:104
int Angle(int iX1, int iY1, int iX2, int iY2)
Definition: Standard.cpp:35
const char * Identifier
Definition: C4AulDefFunc.h:269
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:384
T Abs(T val)
Definition: Standard.h:44
C4Real Sin(const C4Real &fAngle)
Definition: C4Real.h:265
void AddFunc(C4PropListStatic *Parent, const char *Name, RType(*pFunc)(ThisType *, ParTypes...), bool Public=true)
Definition: C4AulDefFunc.h:261
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45
C4Real Cos(const C4Real &fAngle)
Definition: C4Real.h:266

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

C4ScriptConstDef C4ScriptConstMap[]

Definition at line 1080 of file C4Script.cpp.

C4ScriptFnDef C4ScriptFnMap[]
Initial value:
=
{
{ "Call", 1, C4V_Any, { C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnCall },
{ "EffectCall", 1, C4V_Any, { C4V_Object ,C4V_PropList,C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnEffectCall },
{ "Log", 1, C4V_Bool, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnLog },
{ "DebugLog", 1, C4V_Bool, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnDebugLog },
{ "Format", 1, C4V_String, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnFormat },
{ "Trans_Mul", 1, C4V_Array, { C4V_Array ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnTrans_Mul},
{ nullptr, 0, C4V_Nil, { C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil}, 0 }
}

Definition at line 1129 of file C4Script.cpp.