summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoryexo <yexo@openttd.org>2011-12-21 14:55:28 +0000
committeryexo <yexo@openttd.org>2011-12-21 14:55:28 +0000
commit5988659eea20d3e84fdeadf448133e9b94e9630f (patch)
tree230c16eadf35e9868a3acf6d91b1b73ae412897d /src
parenta55478aaa6453d62f9708ee03393711b2d3120ee (diff)
downloadopenttd-5988659eea20d3e84fdeadf448133e9b94e9630f.tar.xz
(svn r23651) -Feature: [NoGo] GSText now accepts string arguments as parameters to the constructor
Diffstat (limited to 'src')
-rw-r--r--src/script/api/ai/ai_text.hpp.sq2
-rw-r--r--src/script/api/game/game_text.hpp.sq2
-rw-r--r--src/script/api/script_text.cpp30
-rw-r--r--src/script/api/script_text.hpp17
-rw-r--r--src/script/api/squirrel_export.awk6
-rw-r--r--src/script/squirrel_class.hpp6
-rw-r--r--src/script/squirrel_helper.hpp22
7 files changed, 76 insertions, 9 deletions
diff --git a/src/script/api/ai/ai_text.hpp.sq b/src/script/api/ai/ai_text.hpp.sq
index e1d8e96c6..39f3ac792 100644
--- a/src/script/api/ai/ai_text.hpp.sq
+++ b/src/script/api/ai/ai_text.hpp.sq
@@ -19,7 +19,7 @@ void SQAIText_Register(Squirrel *engine)
{
DefSQClass<ScriptText, ST_AI> SQAIText("AIText");
SQAIText.PreRegister(engine);
- SQAIText.AddConstructor<void (ScriptText::*)(StringID string), 2>(engine, "xi");
+ SQAIText.AddSQAdvancedConstructor(engine);
SQAIText.DefSQConst(engine, ScriptText::SCRIPT_TEXT_MAX_PARAMETERS, "SCRIPT_TEXT_MAX_PARAMETERS");
diff --git a/src/script/api/game/game_text.hpp.sq b/src/script/api/game/game_text.hpp.sq
index d43506580..8a35279d0 100644
--- a/src/script/api/game/game_text.hpp.sq
+++ b/src/script/api/game/game_text.hpp.sq
@@ -19,7 +19,7 @@ void SQGSText_Register(Squirrel *engine)
{
DefSQClass<ScriptText, ST_GS> SQGSText("GSText");
SQGSText.PreRegister(engine);
- SQGSText.AddConstructor<void (ScriptText::*)(StringID string), 2>(engine, "xi");
+ SQGSText.AddSQAdvancedConstructor(engine);
SQGSText.DefSQConst(engine, ScriptText::SCRIPT_TEXT_MAX_PARAMETERS, "SCRIPT_TEXT_MAX_PARAMETERS");
diff --git a/src/script/api/script_text.cpp b/src/script/api/script_text.cpp
index 10120bcde..e8803884b 100644
--- a/src/script/api/script_text.cpp
+++ b/src/script/api/script_text.cpp
@@ -15,10 +15,34 @@
#include "../squirrel.hpp"
#include "../../table/control_codes.h"
-ScriptText::ScriptText(StringID string) :
- ZeroedMemoryAllocator(),
- string(string)
+ScriptText::ScriptText(HSQUIRRELVM vm) :
+ ZeroedMemoryAllocator()
{
+ int nparam = sq_gettop(vm) - 1;
+ if (nparam < 1) {
+ throw sq_throwerror(vm, _SC("You need to pass at least a StringID to the constructor"));
+ }
+
+ /* First resolve the StringID. */
+ SQInteger sqstring;
+ if (SQ_FAILED(sq_getinteger(vm, 2, &sqstring))) {
+ throw sq_throwerror(vm, _SC("First argument must be a valid StringID"));
+ }
+ this->string = sqstring;
+
+ /* The rest of the parameters must be arguments. */
+ for (int i = 0; i < nparam - 1; i++) {
+ /* Push the parameter to the top of the stack. */
+ sq_push(vm, i + 3);
+
+ if (SQ_FAILED(this->_SetParam(i, vm))) {
+ this->~ScriptText();
+ throw sq_throwerror(vm, _SC("Invalid parameter"));
+ }
+
+ /* Pop the parameter again. */
+ sq_pop(vm, 1);
+ }
}
ScriptText::~ScriptText()
diff --git a/src/script/api/script_text.hpp b/src/script/api/script_text.hpp
index 27bcca485..112947b29 100644
--- a/src/script/api/script_text.hpp
+++ b/src/script/api/script_text.hpp
@@ -59,8 +59,11 @@ private:
*
* If you use parameters in your strings, you will have to define those
* parameters, for example like this:
- * local text = ScriptText(ScriptText.STR_NEWS); text.AddParam(1);
- * This will set the {COMPANY} to the name of Company 1.
+ * \code local text = ScriptText(ScriptText.STR_NEWS);
+ * text.AddParam(1); \endcode
+ * This will set the {COMPANY} to the name of Company 1. Alternatively you
+ * can directly give those arguments to the ScriptText constructor, like this:
+ * \code local text = ScriptText(ScriptText.STR_NEWS, 1); \endcode
*
* @api ai game
*/
@@ -68,12 +71,20 @@ class ScriptText : public Text , public ZeroedMemoryAllocator {
public:
static const int SCRIPT_TEXT_MAX_PARAMETERS = 20; ///< The maximum amount of parameters you can give to one object.
+#ifndef DOXYGEN_API
+ /**
+ * The constructor wrapper from Squirrel.
+ */
+ ScriptText(HSQUIRRELVM vm);
+#else
/**
* Generate a text from string. You can set parameters to the instance which
* can be required for the string.
* @param string The string of the text.
+ * @param ... Optional arguments for this string.
*/
- ScriptText(StringID string);
+ ScriptText(StringID string, ...);
+#endif
~ScriptText();
#ifndef DOXYGEN_API
diff --git a/src/script/api/squirrel_export.awk b/src/script/api/squirrel_export.awk
index cb9774db7..7fcb5336a 100644
--- a/src/script/api/squirrel_export.awk
+++ b/src/script/api/squirrel_export.awk
@@ -341,7 +341,11 @@ BEGIN {
print " SQ" api_cls ".PreRegister(engine, \"" api_super_cls "\");"
}
if (virtual_class == "false" && super_cls != "ScriptEvent") {
- print " SQ" api_cls ".AddConstructor<void (" cls "::*)(" cls_param[0] "), " cls_param[1]">(engine, \"" cls_param[2] "\");"
+ if (cls_param[2] == "v") {
+ print " SQ" api_cls ".AddSQAdvancedConstructor(engine);"
+ } else {
+ print " SQ" api_cls ".AddConstructor<void (" cls "::*)(" cls_param[0] "), " cls_param[1]">(engine, \"" cls_param[2] "\");"
+ }
}
print ""
diff --git a/src/script/squirrel_class.hpp b/src/script/squirrel_class.hpp
index 55efcdf13..ce63e3b03 100644
--- a/src/script/squirrel_class.hpp
+++ b/src/script/squirrel_class.hpp
@@ -117,6 +117,12 @@ public:
engine->AddMethod("constructor", DefSQConstructorCallback<CL, Func, Tnparam>, Tnparam, params);
}
+ void AddSQAdvancedConstructor(Squirrel *engine)
+ {
+ using namespace SQConvert;
+ engine->AddMethod("constructor", DefSQAdvancedConstructorCallback<CL>, 0, NULL);
+ }
+
void PostRegister(Squirrel *engine)
{
engine->AddClassEnd();
diff --git a/src/script/squirrel_helper.hpp b/src/script/squirrel_helper.hpp
index a72199dce..6314d3bc2 100644
--- a/src/script/squirrel_helper.hpp
+++ b/src/script/squirrel_helper.hpp
@@ -885,6 +885,28 @@ namespace SQConvert {
}
}
+ /**
+ * A general template to handle creating of an instance with a complex
+ * constructor.
+ */
+ template <typename Tcls>
+ inline SQInteger DefSQAdvancedConstructorCallback(HSQUIRRELVM vm)
+ {
+ try {
+ /* Find the amount of params we got */
+ int nparam = sq_gettop(vm);
+
+ /* Create the real instance */
+ Tcls *instance = new Tcls(vm);
+ sq_setinstanceup(vm, -nparam, instance);
+ sq_setreleasehook(vm, -nparam, DefSQDestructorCallback<Tcls>);
+ instance->AddRef();
+ return 0;
+ } catch (SQInteger e) {
+ return e;
+ }
+ }
+
} // namespace SQConvert
#endif /* SQUIRREL_HELPER_HPP */