mirror of https://github.com/OpenRCT2/OpenRCT2.git
Add initial network API
This commit is contained in:
parent
5bd58118b5
commit
5d78a1f2bf
|
@ -264,10 +264,23 @@ export interface Ui {
|
|||
closeAllWindows(): void;
|
||||
}
|
||||
|
||||
export interface Player {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface Network {
|
||||
players: number;
|
||||
|
||||
getPlayer(index: number): Player;
|
||||
sendMessage(message: string);
|
||||
sendMessage(players: number[], message: string);
|
||||
}
|
||||
|
||||
declare global {
|
||||
var console: Console;
|
||||
var context: Context;
|
||||
var map: Map;
|
||||
var network: Network;
|
||||
var park: Park;
|
||||
var ui: Ui;
|
||||
}
|
||||
|
|
|
@ -9,21 +9,13 @@
|
|||
|
||||
#include "Plugin.h"
|
||||
|
||||
#include "..\OpenRCT2.h"
|
||||
#include "Duktape.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#else
|
||||
# include <fcntl.h>
|
||||
# include <sys/inotify.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
using namespace OpenRCT2::Scripting;
|
||||
|
||||
Plugin::Plugin(duk_context* context, const std::string& path)
|
||||
|
@ -34,7 +26,11 @@ Plugin::Plugin(duk_context* context, const std::string& path)
|
|||
|
||||
void Plugin::Load()
|
||||
{
|
||||
std::string projectedVariables = "console,context,map,park,ui";
|
||||
std::string projectedVariables = "console,context,map,network,park";
|
||||
if (!gOpenRCT2Headless)
|
||||
{
|
||||
projectedVariables += ",ui";
|
||||
}
|
||||
std::string code;
|
||||
{
|
||||
std::ifstream fs(_path);
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
/*****************************************************************************
|
||||
* Copyright (c) 2014-2018 OpenRCT2 developers
|
||||
*
|
||||
* For a complete list of all authors, please refer to contributors.md
|
||||
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
||||
*
|
||||
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../network/network.h"
|
||||
#include "Duktape.hpp"
|
||||
|
||||
namespace OpenRCT2::Scripting
|
||||
{
|
||||
class ScPlayer
|
||||
{
|
||||
private:
|
||||
int32_t _id;
|
||||
|
||||
public:
|
||||
ScPlayer(int32_t id)
|
||||
: _id(id)
|
||||
{
|
||||
}
|
||||
|
||||
int32_t id_get()
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
|
||||
std::string name_get()
|
||||
{
|
||||
auto index = network_get_player_index(_id);
|
||||
if (index == -1)
|
||||
return {};
|
||||
return network_get_player_name(index);
|
||||
}
|
||||
|
||||
static void Register(duk_context* ctx)
|
||||
{
|
||||
dukglue_register_property(ctx, &ScPlayer::id_get, nullptr, "id");
|
||||
dukglue_register_property(ctx, &ScPlayer::name_get, nullptr, "name");
|
||||
}
|
||||
};
|
||||
|
||||
class ScNetwork
|
||||
{
|
||||
private:
|
||||
duk_context* _context;
|
||||
|
||||
public:
|
||||
ScNetwork(duk_context* ctx)
|
||||
: _context(ctx)
|
||||
{
|
||||
}
|
||||
|
||||
int32_t players_get()
|
||||
{
|
||||
return network_get_num_players();
|
||||
}
|
||||
|
||||
std::shared_ptr<ScPlayer> getPlayer(int32_t index)
|
||||
{
|
||||
auto numPlayers = network_get_num_players();
|
||||
if (index < numPlayers)
|
||||
{
|
||||
auto playerId = network_get_player_id(index);
|
||||
return std::make_shared<ScPlayer>(playerId);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void Register(duk_context* ctx)
|
||||
{
|
||||
dukglue_register_property(ctx, &ScNetwork::players_get, nullptr, "players");
|
||||
dukglue_register_method(ctx, &ScNetwork::getPlayer, "getPlayer");
|
||||
}
|
||||
};
|
||||
} // namespace OpenRCT2::Scripting
|
|
@ -19,6 +19,7 @@
|
|||
#include "ScContext.hpp"
|
||||
#include "ScDisposable.hpp"
|
||||
#include "ScMap.hpp"
|
||||
#include "ScNetwork.hpp"
|
||||
#include "ScPark.hpp"
|
||||
#include "ScThing.hpp"
|
||||
#include "ScTile.hpp"
|
||||
|
@ -59,7 +60,9 @@ void ScriptEngine::Initialise()
|
|||
ScContext::Register(ctx);
|
||||
ScDisposable::Register(ctx);
|
||||
ScMap::Register(ctx);
|
||||
ScNetwork::Register(ctx);
|
||||
ScPark::Register(ctx);
|
||||
ScPlayer::Register(ctx);
|
||||
ScTile::Register(ctx);
|
||||
ScTileElement::Register(ctx);
|
||||
ScThing::Register(ctx);
|
||||
|
@ -67,6 +70,7 @@ void ScriptEngine::Initialise()
|
|||
dukglue_register_global(ctx, std::make_shared<ScConsole>(_console), "console");
|
||||
dukglue_register_global(ctx, std::make_shared<ScContext>(_execInfo, _hookEngine), "context");
|
||||
dukglue_register_global(ctx, std::make_shared<ScMap>(ctx), "map");
|
||||
dukglue_register_global(ctx, std::make_shared<ScNetwork>(ctx), "network");
|
||||
dukglue_register_global(ctx, std::make_shared<ScPark>(), "park");
|
||||
|
||||
LoadPlugins();
|
||||
|
@ -174,7 +178,7 @@ void ScriptEngine::Update()
|
|||
std::string result = std::string(duk_safe_to_string(_context, -1));
|
||||
_console.WriteLineError(result);
|
||||
}
|
||||
else
|
||||
else if (duk_get_type(_context, -1) != DUK_TYPE_UNDEFINED)
|
||||
{
|
||||
std::string result = Stringify(_context, -1);
|
||||
_console.WriteLine(result);
|
||||
|
|
Loading…
Reference in New Issue