Load map descriptions from their XML file.

Add basic hold-fire stance, and use it for some test maps.
Add JSON data container to map XML files, to simplify the interaction
between scripts and maps.
Fix fixed-point printing so it roundtrips safely through map files.
Fix camera startup positions in old-format maps.

This was SVN commit r7844.
This commit is contained in:
Ykkrosh
2010-08-04 21:15:41 +00:00
parent a5171d9145
commit 65bcedb9fc
21 changed files with 415 additions and 28 deletions
@@ -22,6 +22,7 @@
#include "AutoRooters.h"
#include "lib/debug.h"
#include "lib/utf8.h"
#include "ps/CLogger.h"
#include "ps/Profile.h"
#include "ps/utf16string.h"
@@ -628,6 +629,58 @@ std::wstring ScriptInterface::ToString(jsval obj)
return source;
}
CScriptValRooted ScriptInterface::ParseJSON(const utf16string& string)
{
jsval vp;
JSONParser* parser = JS_BeginJSONParse(m->m_cx, &vp);
if (!parser)
{
LOGERROR(L"ParseJSON failed to begin");
return CScriptValRooted();
}
if (!JS_ConsumeJSONText(m->m_cx, parser, string.c_str(), string.size()))
{
LOGERROR(L"ParseJSON failed to consume");
return CScriptValRooted();
}
if (!JS_FinishJSONParse(m->m_cx, parser, JSVAL_NULL))
{
LOGERROR(L"ParseJSON failed to finish");
return CScriptValRooted();
}
return CScriptValRooted(m->m_cx, vp);
}
struct Stringifier
{
static JSBool callback(const jschar *buf, uint32 len, void *data)
{
utf16string str(buf, buf+len);
std::wstring strw(str.begin(), str.end());
LibError err; // ignore Unicode errors
static_cast<Stringifier*>(data)->stream << utf8_from_wstring(strw, &err);
return JS_TRUE;
}
std::stringstream stream;
};
std::string ScriptInterface::StringifyJSON(jsval obj)
{
Stringifier str;
if (!JS_Stringify(m->m_cx, &obj, NULL, INT_TO_JSVAL(2), &Stringifier::callback, &str))
{
LOGERROR(L"StringifyJSON failed");
return "";
}
return str.stream.str();
}
void ScriptInterface::ReportError(const char* msg)
{
// JS_ReportError by itself doesn't seem to set a JS-style exception, and so
+13 -1
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2009 Wildfire Games.
/* Copyright (C) 2010 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -29,6 +29,8 @@
#include "ScriptTypes.h"
#include "ScriptVal.h"
#include "ps/utf16string.h"
class AutoGCRooter;
namespace boost { class rand48; }
@@ -154,6 +156,16 @@ public:
std::wstring ToString(jsval obj);
/**
* Parse a JSON string. Returns the undefined value on error.
*/
CScriptValRooted ParseJSON(const utf16string& string);
/**
* Stringify to a JSON string, UTF-8 encoded. Returns an empty string on error.
*/
std::string StringifyJSON(jsval obj);
/**
* Report the given error message through the JS error reporting mechanism,
* and throw a JS exception. (Callers can check IsPendingException, and must
@@ -19,6 +19,7 @@
#include "scriptinterface/ScriptInterface.h"
#include "lib/utf8.h"
#include "ps/CLogger.h"
#include <boost/random/linear_congruential.hpp>
@@ -125,4 +126,20 @@ public:
TS_ASSERT(script.Eval("Math.random()", d2));
TS_ASSERT_EQUALS(d1, d2);
}
void test_json()
{
ScriptInterface script("Test");
std::string input = "({'x':1,'z':[2,'3\\u263A\\ud800'],\"y\":true})";
CScriptValRooted val;
TS_ASSERT(script.Eval(input.c_str(), val));
std::string stringified = script.StringifyJSON(val.get());
TS_ASSERT_STR_EQUALS(stringified, "{\n \"x\":1,\n \"z\":[2,\n \"3\xE2\x98\xBA\xEF\xBF\xBD\"\n ],\n \"y\":true\n}");
std::wstring stringifiedw = wstring_from_utf8(stringified.c_str());
val = script.ParseJSON(utf16string(stringifiedw.begin(), stringifiedw.end()));
TS_ASSERT_WSTR_EQUALS(script.ToString(val.get()), L"({x:1, z:[2, \"3\\u263A\\uFFFD\"], y:true})");
}
};