OpenClonk
C4Script.cpp File Reference
#include "C4Include.h"
#include "C4Version.h"
#include "lib/C4Random.h"
#include "script/C4AulExec.h"
#include "script/C4AulDefFunc.h"
#include "script/C4ScriptLibraries.h"
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

◆ F

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

Referenced by InitCoreFunctionMap().

◆ MAKE_AND_RETURN_ARRAY

#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 127 of file C4Script.cpp.

Function Documentation

◆ FnStringFormat()

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

Definition at line 30 of file C4Script.cpp.

Referenced by Object().

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

◆ InitCoreFunctionMap()

void InitCoreFunctionMap ( C4AulScriptEngine pEngine)

Definition at line 1116 of file C4Script.cpp.

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

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

1117 {
1119  // add all def constants (all Int)
1120  for (C4ScriptConstDef *pCDef = &C4ScriptConstMap[0]; pCDef->Identifier; pCDef++)
1121  {
1122  assert(pCDef->ValType == C4V_Int); // only int supported currently
1123  pEngine->RegisterGlobalConstant(pCDef->Identifier, C4VInt(pCDef->Data));
1124  }
1125 
1126  C4PropListStatic * p = pEngine->GetPropList();
1127  // add all def script funcs
1128  for (C4ScriptFnDef *pDef = &C4ScriptFnMap[0]; pDef->Identifier; pDef++)
1129  new C4AulDefFunc(p, pDef);
1130 #define F(f) ::AddFunc(p, #f, Fn##f)
1131  F(ParseInt);
1132  F(Abs);
1133  F(Min);
1134  F(Max);
1135  F(Sin);
1136  F(Cos);
1137  F(Sqrt);
1138  F(ArcSin);
1139  F(ArcCos);
1140  F(BoundBy);
1141  F(Inside);
1142  F(Random);
1143 
1144  F(CreateArray);
1145  F(CreatePropList);
1146  F(GetProperties);
1147  F(GetProperty);
1148  F(SetProperty);
1149  F(GetPrototype);
1150  F(SetPrototype);
1151  F(ResetProperty);
1152  F(GetName);
1153  F(AddEffect);
1154  F(CreateEffect);
1155  F(CheckEffect);
1156  F(RemoveEffect);
1157  F(GetEffect);
1158  F(GetEffectCount);
1159  F(RegexReplace);
1160  F(RegexSearch);
1161  F(RegexMatch);
1162  F(RegexSplit);
1163  F(Distance);
1164  F(Angle);
1165  F(GetChar);
1166  F(GetType);
1167  F(ModulateColor);
1168  F(WildcardMatch);
1169  F(GetLength);
1170  F(SetLength);
1171  F(GetIndexOf);
1172  F(DeepEqual);
1173  F(StartCallTrace);
1174  F(StartScriptProfiler);
1175  F(StopScriptProfiler);
1176  F(SortArray);
1177  F(SortArrayByProperty);
1178  F(SortArrayByArrayElement);
1179  F(Trans_Identity);
1180  F(Trans_Translate);
1181  F(Trans_Scale);
1182  F(Trans_Rotate);
1183  F(LocateFunc);
1184  F(FileWrite);
1185  F(eval);
1186  F(StringToIdentifier);
1187  F(GetConstantNameByValue);
1188  F(ReplaceString);
1189 
1190  ::AddFunc(p, "Translate", C4AulExec::FnTranslate);
1191  ::AddFunc(p, "LogCallStack", C4AulExec::FnLogCallStack);
1192 #undef F
1193 }
void RegisterGlobalConstant(const char *szName, const C4Value &rValue)
Definition: C4Aul.cpp:123
static bool FnLogCallStack(C4PropList *_this)
Definition: C4AulExec.cpp:130
static void InstantiateAllLibraries(C4AulScriptEngine *engine)
C4ScriptFnDef C4ScriptFnMap[]
Definition: C4Script.cpp:1103
uint32_t Random()
Definition: C4Random.cpp:43
C4PropListStatic * GetPropList()
Definition: C4Aul.h:151
C4Value C4VInt(int32_t i)
Definition: C4Value.h:242
int32_t Angle(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2, int32_t iPrec)
Definition: Standard.cpp:37
int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2)
Definition: Standard.cpp:25
C4ScriptConstDef C4ScriptConstMap[]
Definition: C4Script.cpp:1054
const char * Identifier
Definition: C4AulDefFunc.h:276
#define F(f)
static C4String * FnTranslate(C4PropList *_this, C4String *text)
Definition: C4AulExec.cpp:100
const char * Identifier
Definition: C4AulDefFunc.h:268
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:374
T Abs(T val)
Definition: Standard.h:42
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:260
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43
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

◆ C4ScriptConstMap

C4ScriptConstDef C4ScriptConstMap[]

Definition at line 1054 of file C4Script.cpp.

◆ C4ScriptFnMap

C4ScriptFnDef C4ScriptFnMap[]
Initial value:
=
{
{ "Call", true, C4V_Any, { C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnCall },
{ "EffectCall", true, C4V_Any, { C4V_Object ,C4V_PropList,C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnEffectCall },
{ "Log", true, C4V_Bool, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnLog },
{ "DebugLog", true, C4V_Bool, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnDebugLog },
{ "FatalError", true, C4V_Nil, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnFatalError },
{ "Format", true, 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", true, 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, false, C4V_Nil, { C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil ,C4V_Nil}, nullptr }
}

Definition at line 1103 of file C4Script.cpp.