| 40 | } |
| 41 | |
| 42 | void Plugin::Load() |
| 43 | { |
| 44 | if (_context) |
| 45 | { |
| 46 | JS_FreeContext(_context); |
| 47 | } |
| 48 | |
| 49 | if (!_path.empty()) |
| 50 | { |
| 51 | LoadCodeFromFile(); |
| 52 | } |
| 53 | |
| 54 | auto& scriptEngine = OpenRCT2::GetContext()->GetScriptEngine(); |
| 55 | _context = scriptEngine.CreateContext(); |
| 56 | JS_SetContextOpaque(_context, this); |
| 57 | |
| 58 | JSValue registerFunc = JS_NewCFunction(_context, registerPlugin, "registerPlugin", 1); |
| 59 | JSValue glb = JS_GetGlobalObject(_context); |
| 60 | JS_SetPropertyStr(_context, glb, "registerPlugin", registerFunc); |
| 61 | JS_FreeValue(_context, glb); |
| 62 | |
| 63 | JSValue res = JS_Eval(_context, _code.c_str(), _code.length(), _path.c_str(), JS_EVAL_TYPE_GLOBAL); |
| 64 | if (JS_IsException(res)) |
| 65 | { |
| 66 | JSValue exceptionVal = JS_GetException(_context); |
| 67 | std::string details = Stringify(_context, exceptionVal); |
| 68 | JS_FreeValue(_context, exceptionVal); |
| 69 | JS_FreeValue(_context, res); |
| 70 | JS_FreeContext(_context); |
| 71 | throw std::runtime_error("Failed to load plug-in script: " + _path + "\n" + details); |
| 72 | } |
| 73 | JS_FreeValue(_context, res); |
| 74 | |
| 75 | _hasLoaded = true; |
| 76 | } |
| 77 | |
| 78 | void Plugin::Start() |
| 79 | { |
no test coverage detected