1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/* Copyright (C) 2021 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "scriptinterface/Object.h"
#include "scriptinterface/ScriptConversions.h"
#include "scriptinterface/ScriptInterface.h"
#include "graphics/Color.h"
#include "maths/Fixed.h"
#include "maths/FixedVector2D.h"
#include "maths/FixedVector3D.h"
#include "maths/Rect.h"
#include "ps/CLogger.h"
#include "simulation2/helpers/CinemaPath.h"
#include "simulation2/helpers/Grid.h"
#include "simulation2/system/IComponent.h"
#include "simulation2/system/ParamNode.h"
#define FAIL(msg) STMT(LOGERROR(msg); return false)
#define FAIL_VOID(msg) STMT(ScriptException::Raise(rq, msg); return)
template<> void Script::ToJSVal<IComponent*>(const ScriptRequest& rq, JS::MutableHandleValue ret, IComponent* const& val)
{
if (val == NULL)
{
ret.setNull();
return;
}
// If this is a scripted component, just return the JS object directly
JS::RootedValue instance(rq.cx, val->GetJSInstance());
if (!instance.isNull())
{
ret.set(instance);
return;
}
// Otherwise we need to construct a wrapper object
// (TODO: cache wrapper objects?)
JS::RootedObject obj(rq.cx);
if (!val->NewJSObject(rq.GetScriptInterface(), &obj))
{
// Report as an error, since scripts really shouldn't try to use unscriptable interfaces
LOGERROR("IComponent does not have a scriptable interface");
ret.setUndefined();
return;
}
JS_SetPrivate(obj, static_cast<void*>(val));
ret.setObject(*obj);
}
template<> void Script::ToJSVal<CParamNode>(const ScriptRequest& rq, JS::MutableHandleValue ret, CParamNode const& val)
{
val.ToJSVal(rq, true, ret);
// Prevent modifications to the object, so that it's safe to share between
// components and to reconstruct on deserialization
if (ret.isObject())
Script::FreezeObject(rq, ret, true);
}
template<> void Script::ToJSVal<const CParamNode*>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CParamNode* const& val)
{
if (val)
ToJSVal(rq, ret, *val);
else
ret.setUndefined();
}
template<> bool Script::FromJSVal<CColor>(const ScriptRequest& rq, JS::HandleValue v, CColor& out)
{
if (!v.isObject())
FAIL("CColor has to be an object");
JS::RootedObject obj(rq.cx, &v.toObject());
JS::RootedValue r(rq.cx);
JS::RootedValue g(rq.cx);
JS::RootedValue b(rq.cx);
JS::RootedValue a(rq.cx);
if (!JS_GetProperty(rq.cx, obj, "r", &r) || !FromJSVal(rq, r, out.r))
FAIL("Failed to get property CColor.r");
if (!JS_GetProperty(rq.cx, obj, "g", &g) || !FromJSVal(rq, g, out.g))
FAIL("Failed to get property CColor.g");
if (!JS_GetProperty(rq.cx, obj, "b", &b) || !FromJSVal(rq, b, out.b))
FAIL("Failed to get property CColor.b");
if (!JS_GetProperty(rq.cx, obj, "a", &a) || !FromJSVal(rq, a, out.a))
FAIL("Failed to get property CColor.a");
return true;
}
template<> void Script::ToJSVal<CColor>(const ScriptRequest& rq, JS::MutableHandleValue ret, CColor const& val)
{
Script::CreateObject(
rq,
ret,
"r", val.r,
"g", val.g,
"b", val.b,
"a", val.a);
}
template<> bool Script::FromJSVal<fixed>(const ScriptRequest& rq, JS::HandleValue v, fixed& out)
{
double ret;
if (!JS::ToNumber(rq.cx, v, &ret))
return false;
out = fixed::FromDouble(ret);
// double can precisely represent the full range of fixed, so this is a non-lossy conversion
return true;
}
template<> void Script::ToJSVal<fixed>(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue ret, const fixed& val)
{
ret.set(JS::NumberValue(val.ToDouble()));
}
template<> bool Script::FromJSVal<CFixedVector3D>(const ScriptRequest& rq, JS::HandleValue v, CFixedVector3D& out)
{
if (!v.isObject())
return false; // TODO: report type error
JS::RootedObject obj(rq.cx, &v.toObject());
JS::RootedValue p(rq.cx);
if (!JS_GetProperty(rq.cx, obj, "x", &p)) return false; // TODO: report type errors
if (!FromJSVal(rq, p, out.X)) return false;
if (!JS_GetProperty(rq.cx, obj, "y", &p)) return false;
if (!FromJSVal(rq, p, out.Y)) return false;
if (!JS_GetProperty(rq.cx, obj, "z", &p)) return false;
if (!FromJSVal(rq, p, out.Z)) return false;
return true;
}
template<> void Script::ToJSVal<CFixedVector3D>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CFixedVector3D& val)
{
JS::RootedObject global(rq.cx, rq.glob);
JS::RootedValue valueVector3D(rq.cx);
if (!ScriptInterface::GetGlobalProperty(rq, "Vector3D", &valueVector3D))
FAIL_VOID("Failed to get Vector3D constructor");
JS::RootedValueArray<3> args(rq.cx);
args[0].setNumber(val.X.ToDouble());
args[1].setNumber(val.Y.ToDouble());
args[2].setNumber(val.Z.ToDouble());
JS::RootedObject objVec(rq.cx);
if (!JS::Construct(rq.cx, valueVector3D, args, &objVec))
FAIL_VOID("Failed to construct Vector3D object");
ret.setObject(*objVec);
}
template<> bool Script::FromJSVal<CFixedVector2D>(const ScriptRequest& rq, JS::HandleValue v, CFixedVector2D& out)
{
if (!v.isObject())
return false; // TODO: report type error
JS::RootedObject obj(rq.cx, &v.toObject());
JS::RootedValue p(rq.cx);
if (!JS_GetProperty(rq.cx, obj, "x", &p)) return false; // TODO: report type errors
if (!FromJSVal(rq, p, out.X)) return false;
if (!JS_GetProperty(rq.cx, obj, "y", &p)) return false;
if (!FromJSVal(rq, p, out.Y)) return false;
return true;
}
template<> void Script::ToJSVal<CFixedVector2D>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CFixedVector2D& val)
{
JS::RootedObject global(rq.cx, rq.glob);
JS::RootedValue valueVector2D(rq.cx);
if (!ScriptInterface::GetGlobalProperty(rq, "Vector2D", &valueVector2D))
FAIL_VOID("Failed to get Vector2D constructor");
JS::RootedValueArray<2> args(rq.cx);
args[0].setNumber(val.X.ToDouble());
args[1].setNumber(val.Y.ToDouble());
JS::RootedObject objVec(rq.cx);
if (!JS::Construct(rq.cx, valueVector2D, args, &objVec))
FAIL_VOID("Failed to construct Vector2D object");
ret.setObject(*objVec);
}
template<> void Script::ToJSVal<Grid<u8> >(const ScriptRequest& rq, JS::MutableHandleValue ret, const Grid<u8>& val)
{
u32 length = (u32)(val.m_W * val.m_H);
u32 nbytes = (u32)(length * sizeof(u8));
JS::RootedObject objArr(rq.cx, JS_NewUint8Array(rq.cx, length));
// Copy the array data and then remove the no-GC check to allow further changes to the JS data
{
JS::AutoCheckCannotGC nogc;
bool sharedMemory;
memcpy((void*)JS_GetUint8ArrayData(objArr, &sharedMemory, nogc), val.m_Data, nbytes);
}
JS::RootedValue data(rq.cx, JS::ObjectValue(*objArr));
Script::CreateObject(
rq,
ret,
"width", val.m_W,
"height", val.m_H,
"data", data);
}
template<> void Script::ToJSVal<Grid<u16> >(const ScriptRequest& rq, JS::MutableHandleValue ret, const Grid<u16>& val)
{
u32 length = (u32)(val.m_W * val.m_H);
u32 nbytes = (u32)(length * sizeof(u16));
JS::RootedObject objArr(rq.cx, JS_NewUint16Array(rq.cx, length));
// Copy the array data and then remove the no-GC check to allow further changes to the JS data
{
JS::AutoCheckCannotGC nogc;
bool sharedMemory;
memcpy((void*)JS_GetUint16ArrayData(objArr, &sharedMemory, nogc), val.m_Data, nbytes);
}
JS::RootedValue data(rq.cx, JS::ObjectValue(*objArr));
Script::CreateObject(
rq,
ret,
"width", val.m_W,
"height", val.m_H,
"data", data);
}
template<> bool Script::FromJSVal<TNSpline>(const ScriptRequest& rq, JS::HandleValue v, TNSpline& out)
{
if (!v.isObject())
FAIL("Argument must be an object");
JS::RootedObject obj(rq.cx, &v.toObject());
bool isArray;
if (!JS::IsArrayObject(rq.cx, obj, &isArray) || !isArray)
FAIL("Argument must be an array");
u32 numberOfNodes = 0;
if (!JS::GetArrayLength(rq.cx, obj, &numberOfNodes))
FAIL("Failed to get array length");
for (u32 i = 0; i < numberOfNodes; ++i)
{
JS::RootedValue node(rq.cx);
if (!JS_GetElement(rq.cx, obj, i, &node))
FAIL("Failed to read array element");
fixed deltaTime;
if (!FromJSProperty(rq, node, "deltaTime", deltaTime))
FAIL("Failed to read Spline.deltaTime property");
CFixedVector3D position;
if (!FromJSProperty(rq, node, "position", position))
FAIL("Failed to read Spline.position property");
out.AddNode(position, CFixedVector3D(), deltaTime);
}
if (out.GetAllNodes().empty())
FAIL("Spline must contain at least one node");
return true;
}
template<> bool Script::FromJSVal<CCinemaPath>(const ScriptRequest& rq, JS::HandleValue v, CCinemaPath& out)
{
if (!v.isObject())
FAIL("Argument must be an object");
JS::RootedObject obj(rq.cx, &v.toObject());
CCinemaData pathData;
TNSpline positionSpline, targetSpline;
if (!FromJSProperty(rq, v, "name", pathData.m_Name))
FAIL("Failed to get CCinemaPath.name property");
if (!FromJSProperty(rq, v, "orientation", pathData.m_Orientation))
FAIL("Failed to get CCinemaPath.orientation property");
if (!FromJSProperty(rq, v, "positionNodes", positionSpline))
FAIL("Failed to get CCinemaPath.positionNodes property");
if (pathData.m_Orientation == L"target" && !FromJSProperty(rq, v, "targetNodes", targetSpline))
FAIL("Failed to get CCinemaPath.targetNodes property");
// Other properties are not necessary to be defined
if (!FromJSProperty(rq, v, "timescale", pathData.m_Timescale))
pathData.m_Timescale = fixed::FromInt(1);
if (!FromJSProperty(rq, v, "mode", pathData.m_Mode))
pathData.m_Mode = L"ease_inout";
if (!FromJSProperty(rq, v, "style", pathData.m_Style))
pathData.m_Style = L"default";
out = CCinemaPath(pathData, positionSpline, targetSpline);
return true;
}
// define vectors
JSVAL_VECTOR(CFixedVector2D)
#undef FAIL
#undef FAIL_VOID
|