From 76f485647983ebd7eb4c3abbca0869d13f76920b Mon Sep 17 00:00:00 2001 From: kwolekr Date: Thu, 28 Apr 2016 03:43:09 -0400 Subject: Move biome calculation to BiomeGen BiomeGen defines an interface that, given a set of BiomeParams, computes biomes for a given area using the algorithm implemented by that specific BiomeGen. This abstracts away the old system where each mapgen supplied the noises required for biome generation. --- src/script/lua_api/l_mapgen.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/script') diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index fb839176b..405b93b86 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -528,24 +528,26 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L) return 1; } case MGOBJ_BIOMEMAP: { - if (!mg->biomemap) + if (!mg->biomegen) return 0; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { - lua_pushinteger(L, mg->biomemap[i]); + lua_pushinteger(L, mg->biomegen->biomemap[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_HEATMAP: { - if (!mg->heatmap) + if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL) return 0; + BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen; + lua_newtable(L); for (size_t i = 0; i != maplen; i++) { - lua_pushnumber(L, mg->heatmap[i]); + lua_pushnumber(L, bg->heatmap[i]); lua_rawseti(L, -2, i + 1); } @@ -553,12 +555,14 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L) } case MGOBJ_HUMIDMAP: { - if (!mg->humidmap) + if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL) return 0; + BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen; + lua_newtable(L); for (size_t i = 0; i != maplen; i++) { - lua_pushnumber(L, mg->humidmap[i]); + lua_pushnumber(L, bg->humidmap[i]); lua_rawseti(L, -2, i + 1); } -- cgit v1.2.3 From 9b5c492be57945c2df63e84ce8dbf057f45b2754 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Mon, 2 May 2016 02:45:59 -0400 Subject: Fix MgStoneType and BiomeType enum names --- src/mapgen.cpp | 6 +++--- src/mapgen.h | 6 +++--- src/mapgen_flat.cpp | 6 +++--- src/mapgen_fractal.cpp | 6 +++--- src/mapgen_v5.cpp | 6 +++--- src/mapgen_v7.cpp | 6 +++--- src/mapgen_valleys.cpp | 6 +++--- src/mg_biome.h | 14 +++++++------- src/script/lua_api/l_mapgen.cpp | 12 ++++++------ 9 files changed, 34 insertions(+), 34 deletions(-) (limited to 'src/script') diff --git a/src/mapgen.cpp b/src/mapgen.cpp index f491ed9eb..01593718b 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -384,7 +384,7 @@ MgStoneType MapgenBasic::generateBiomes() { v3s16 em = vm->m_area.getExtent(); u32 index = 0; - MgStoneType stone_type = STONE; + MgStoneType stone_type = MGSTONE_STONE; for (s16 z = node_min.Z; z <= node_max.Z; z++) for (s16 x = node_min.X; x <= node_max.X; x++, index++) { @@ -429,9 +429,9 @@ MgStoneType MapgenBasic::generateBiomes() // This is more efficient than detecting per-node and will not // miss any desert stone or sandstone biomes. if (biome->c_stone == c_desert_stone) - stone_type = DESERT_STONE; + stone_type = MGSTONE_DESERT_STONE; else if (biome->c_stone == c_sandstone) - stone_type = SANDSTONE; + stone_type = MGSTONE_SANDSTONE; } if (c == c_stone) { diff --git a/src/mapgen.h b/src/mapgen.h index bee8e786a..3ec148296 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -76,9 +76,9 @@ enum GenNotifyType { // TODO(hmmmm/paramat): make stone type selection dynamic enum MgStoneType { - STONE, - DESERT_STONE, - SANDSTONE, + MGSTONE_STONE, + MGSTONE_DESERT_STONE, + MGSTONE_SANDSTONE, }; struct GenNotifyEvent { diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp index 97f1c4b2a..2cbf9ace0 100644 --- a/src/mapgen_flat.cpp +++ b/src/mapgen_flat.cpp @@ -257,7 +257,7 @@ void MapgenFlat::makeChunk(BlockMakeData *data) dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; - if (stone_type == STONE) { + if (stone_type == MGSTONE_STONE) { dp.c_cobble = c_cobble; dp.c_moss = c_mossycobble; dp.c_stair = c_stair_cobble; @@ -267,7 +267,7 @@ void MapgenFlat::makeChunk(BlockMakeData *data) dp.holesize = v3s16(1, 2, 1); dp.roomsize = v3s16(0, 0, 0); dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == DESERT_STONE) { + } else if (stone_type == MGSTONE_DESERT_STONE) { dp.c_cobble = c_desert_stone; dp.c_moss = c_desert_stone; dp.c_stair = c_desert_stone; @@ -277,7 +277,7 @@ void MapgenFlat::makeChunk(BlockMakeData *data) dp.holesize = v3s16(2, 3, 2); dp.roomsize = v3s16(2, 5, 2); dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == SANDSTONE) { + } else if (stone_type == MGSTONE_SANDSTONE) { dp.c_cobble = c_sandstonebrick; dp.c_moss = c_sandstonebrick; dp.c_stair = c_sandstonebrick; diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp index 81e162072..3217d52e2 100644 --- a/src/mapgen_fractal.cpp +++ b/src/mapgen_fractal.cpp @@ -273,7 +273,7 @@ void MapgenFractal::makeChunk(BlockMakeData *data) dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; - if (stone_type == STONE) { + if (stone_type == MGSTONE_STONE) { dp.c_cobble = c_cobble; dp.c_moss = c_mossycobble; dp.c_stair = c_stair_cobble; @@ -283,7 +283,7 @@ void MapgenFractal::makeChunk(BlockMakeData *data) dp.holesize = v3s16(1, 2, 1); dp.roomsize = v3s16(0, 0, 0); dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == DESERT_STONE) { + } else if (stone_type == MGSTONE_DESERT_STONE) { dp.c_cobble = c_desert_stone; dp.c_moss = c_desert_stone; dp.c_stair = c_desert_stone; @@ -293,7 +293,7 @@ void MapgenFractal::makeChunk(BlockMakeData *data) dp.holesize = v3s16(2, 3, 2); dp.roomsize = v3s16(2, 5, 2); dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == SANDSTONE) { + } else if (stone_type == MGSTONE_SANDSTONE) { dp.c_cobble = c_sandstonebrick; dp.c_moss = c_sandstonebrick; dp.c_stair = c_sandstonebrick; diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index 7698af825..6c1441bc2 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -255,7 +255,7 @@ void MapgenV5::makeChunk(BlockMakeData *data) dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; - if (stone_type == STONE) { + if (stone_type == MGSTONE_STONE) { dp.c_cobble = c_cobble; dp.c_moss = c_mossycobble; dp.c_stair = c_stair_cobble; @@ -265,7 +265,7 @@ void MapgenV5::makeChunk(BlockMakeData *data) dp.holesize = v3s16(1, 2, 1); dp.roomsize = v3s16(0, 0, 0); dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == DESERT_STONE) { + } else if (stone_type == MGSTONE_DESERT_STONE) { dp.c_cobble = c_desert_stone; dp.c_moss = c_desert_stone; dp.c_stair = c_desert_stone; @@ -275,7 +275,7 @@ void MapgenV5::makeChunk(BlockMakeData *data) dp.holesize = v3s16(2, 3, 2); dp.roomsize = v3s16(2, 5, 2); dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == SANDSTONE) { + } else if (stone_type == MGSTONE_SANDSTONE) { dp.c_cobble = c_sandstonebrick; dp.c_moss = c_sandstonebrick; dp.c_stair = c_sandstonebrick; diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index ee609bc95..e6801a24b 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -285,7 +285,7 @@ void MapgenV7::makeChunk(BlockMakeData *data) dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; - if (stone_type == STONE) { + if (stone_type == MGSTONE_STONE) { dp.c_cobble = c_cobble; dp.c_moss = c_mossycobble; dp.c_stair = c_stair_cobble; @@ -295,7 +295,7 @@ void MapgenV7::makeChunk(BlockMakeData *data) dp.holesize = v3s16(1, 2, 1); dp.roomsize = v3s16(0, 0, 0); dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == DESERT_STONE) { + } else if (stone_type == MGSTONE_DESERT_STONE) { dp.c_cobble = c_desert_stone; dp.c_moss = c_desert_stone; dp.c_stair = c_desert_stone; @@ -305,7 +305,7 @@ void MapgenV7::makeChunk(BlockMakeData *data) dp.holesize = v3s16(2, 3, 2); dp.roomsize = v3s16(2, 5, 2); dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == SANDSTONE) { + } else if (stone_type == MGSTONE_SANDSTONE) { dp.c_cobble = c_sandstonebrick; dp.c_moss = c_sandstonebrick; dp.c_stair = c_sandstonebrick; diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index fa790677a..d424ca068 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -312,7 +312,7 @@ void MapgenValleys::makeChunk(BlockMakeData *data) dp.np_density = nparams_dungeon_density; dp.np_wetness = nparams_dungeon_wetness; dp.c_water = c_water_source; - if (stone_type == STONE) { + if (stone_type == MGSTONE_STONE) { dp.c_cobble = c_cobble; dp.c_moss = c_mossycobble; dp.c_stair = c_stair_cobble; @@ -322,7 +322,7 @@ void MapgenValleys::makeChunk(BlockMakeData *data) dp.holesize = v3s16(1, 2, 1); dp.roomsize = v3s16(0, 0, 0); dp.notifytype = GENNOTIFY_DUNGEON; - } else if (stone_type == DESERT_STONE) { + } else if (stone_type == MGSTONE_DESERT_STONE) { dp.c_cobble = c_desert_stone; dp.c_moss = c_desert_stone; dp.c_stair = c_desert_stone; @@ -332,7 +332,7 @@ void MapgenValleys::makeChunk(BlockMakeData *data) dp.holesize = v3s16(2, 3, 2); dp.roomsize = v3s16(2, 5, 2); dp.notifytype = GENNOTIFY_TEMPLE; - } else if (stone_type == SANDSTONE) { + } else if (stone_type == MGSTONE_SANDSTONE) { dp.c_cobble = c_sandstonebrick; dp.c_moss = c_sandstonebrick; dp.c_stair = c_sandstonebrick; diff --git a/src/mg_biome.h b/src/mg_biome.h index e9378fd79..e78e90e5f 100644 --- a/src/mg_biome.h +++ b/src/mg_biome.h @@ -33,13 +33,13 @@ class BiomeManager; #define BIOME_NONE ((u8)0) -enum BiomeType -{ - BIOME_NORMAL, - BIOME_LIQUID, - BIOME_NETHER, - BIOME_AETHER, - BIOME_FLAT +// TODO(hmmmm): Decide whether this is obsolete or will be used in the future +enum BiomeType { + BIOMETYPE_NORMAL, + BIOMETYPE_LIQUID, + BIOMETYPE_NETHER, + BIOMETYPE_AETHER, + BIOMETYPE_FLAT, }; class Biome : public ObjDef, public NodeResolver { diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 405b93b86..6baf217af 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -39,11 +39,11 @@ with this program; if not, write to the Free Software Foundation, Inc., struct EnumString ModApiMapgen::es_BiomeTerrainType[] = { - {BIOME_NORMAL, "normal"}, - {BIOME_LIQUID, "liquid"}, - {BIOME_NETHER, "nether"}, - {BIOME_AETHER, "aether"}, - {BIOME_FLAT, "flat"}, + {BIOMETYPE_NORMAL, "normal"}, + {BIOMETYPE_LIQUID, "liquid"}, + {BIOMETYPE_NETHER, "nether"}, + {BIOMETYPE_AETHER, "aether"}, + {BIOMETYPE_FLAT, "flat"}, {0, NULL}, }; @@ -371,7 +371,7 @@ Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef) return NULL; BiomeType biometype = (BiomeType)getenumfield(L, index, "type", - ModApiMapgen::es_BiomeTerrainType, BIOME_NORMAL); + ModApiMapgen::es_BiomeTerrainType, BIOMETYPE_NORMAL); Biome *b = BiomeManager::create(biometype); b->name = getstringfield_default(L, index, "name", ""); -- cgit v1.2.3 From 62d15ac7c1fcd7214a9e45d46bbc560f998edb95 Mon Sep 17 00:00:00 2001 From: red-001 Date: Sat, 28 May 2016 04:37:28 +0100 Subject: Add base64 encoding and decoding to the lua api. (#3919) --- doc/lua_api.txt | 4 ++++ doc/menu_lua_api.txt | 4 ++++ src/script/lua_api/l_util.cpp | 35 +++++++++++++++++++++++++++++++++++ src/script/lua_api/l_util.h | 6 ++++++ 4 files changed, 49 insertions(+) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 593e0c438..01763fd73 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2438,6 +2438,10 @@ These functions return the leftover itemstack. * See documentation on `minetest.compress()` for supported compression methods. * currently supported. * `...` indicates method-specific arguments. Currently, no methods use this. +* `minetest.encode_base64(string)`: returns string encoded in base64 + * Encodes a string in base64. +* `minetest.decode_base64(string)`: returns string + * Decodes a string encoded in base64. * `minetest.is_protected(pos, name)`: returns boolean * Returns true, if player `name` shouldn't be abled to dig at `pos` or do other actions, defineable by mods, due to some mod-defined ownership-like concept. diff --git a/doc/menu_lua_api.txt b/doc/menu_lua_api.txt index ac8713a32..479880266 100644 --- a/doc/menu_lua_api.txt +++ b/doc/menu_lua_api.txt @@ -210,6 +210,10 @@ string:trim() ^ eg. string.trim("\n \t\tfoo bar\t ") == "foo bar" core.is_yes(arg) (possible in async calls) ^ returns whether arg can be interpreted as yes +minetest.encode_base64(string) (possible in async calls) +^ Encodes a string in base64. +minetest.decode_base64(string) (possible in async calls) +^ Decodes a string encoded in base64. Version compat: core.get_min_supp_proto() diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index c3e6c8964..e90b7fbcf 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "settings.h" #include "util/auth.h" +#include "util/base64.h" #include // log([level,] text) @@ -320,6 +321,34 @@ int ModApiUtil::l_decompress(lua_State *L) return 1; } +// encode_base64(string) +int ModApiUtil::l_encode_base64(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + size_t size; + const char *data = luaL_checklstring(L, 1, &size); + + std::string out = base64_encode((const unsigned char *)(data), size); + + lua_pushlstring(L, out.data(), out.size()); + return 1; +} + +// decode_base64(string) +int ModApiUtil::l_decode_base64(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + size_t size; + const char *data = luaL_checklstring(L, 1, &size); + + std::string out = base64_decode(std::string(data, size)); + + lua_pushlstring(L, out.data(), out.size()); + return 1; +} + // mkdir(path) int ModApiUtil::l_mkdir(lua_State *L) { @@ -433,6 +462,9 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(get_dir_list); API_FCT(request_insecure_environment); + + API_FCT(encode_base64); + API_FCT(decode_base64); } void ModApiUtil::InitializeAsync(AsyncEngine& engine) @@ -459,5 +491,8 @@ void ModApiUtil::InitializeAsync(AsyncEngine& engine) ASYNC_API_FCT(mkdir); ASYNC_API_FCT(get_dir_list); + + ASYNC_API_FCT(encode_base64); + ASYNC_API_FCT(decode_base64); } diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index 6fac7e7eb..779dbe281 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -95,6 +95,12 @@ private: // request_insecure_environment() static int l_request_insecure_environment(lua_State *L); + // encode_base64(string) + static int l_encode_base64(lua_State *L); + + // decode_base64(string) + static int l_decode_base64(lua_State *L); + public: static void Initialize(lua_State *L, int top); -- cgit v1.2.3 From d499ec483837fa7210176ef39beba2d5a3a5a61d Mon Sep 17 00:00:00 2001 From: Auke Kok Date: Fri, 27 May 2016 21:08:23 -0700 Subject: Particles: Add option to remove particles on collision Adds the particle option `collision_removal = bool` Some particles are hard to use right now since they either go through solid blocks (without collision detection), and with collision detection enabled they (e.g. raindrops) would just stop dead on the floor and sit there until they expire, or worse, scrape along a wall or ceiling. We can solve the problem by adding a boolean flag that tells the particle to be removed if it ever collides with something. This will make it easier to add rain that doesn't fall through your roof or stick on the top of it. Or clouds and smoke that don't go through trees. Particles that collide with this flag are marked expired unconditionally, causing them to be treated like normal expired particles and cleaned up normally. Documentation is adjusted accordingly. An added bonus of this patch is that particles can potentially collide many times with nodes, and this reduces the amount of collisions to 1 (max), which may end up reducing particle load on the client. --- doc/lua_api.txt | 6 ++++++ src/client.h | 2 ++ src/network/clientpackethandler.cpp | 7 +++++++ src/network/networkprotocol.h | 2 ++ src/particles.cpp | 39 +++++++++++++++++++++++-------------- src/particles.h | 5 +++++ src/script/lua_api/l_particles.cpp | 24 ++++++++++++++++------- src/server.cpp | 19 ++++++++++++------ src/server.h | 12 ++++++++---- 9 files changed, 84 insertions(+), 32 deletions(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 01763fd73..03f2dad32 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -3885,6 +3885,9 @@ Definition tables size = 1, collisiondetection = false, -- ^ collisiondetection: if true collides with physical objects + collision_removal = false, + -- ^ collision_removal: if true then particle is removed when it collides, + -- ^ requires collisiondetection = true to have any effect vertical = false, -- ^ vertical: if true faces player using y axis only texture = "image.png", @@ -3914,6 +3917,9 @@ Definition tables -- ^ minsize/maxsize, minexptime/maxexptime (expirationtime) collisiondetection = false, -- ^ collisiondetection: if true uses collision detection + collision_removal = false, + -- ^ collision_removal: if true then particle is removed when it collides, + -- ^ requires collisiondetection = true to have any effect vertical = false, -- ^ vertical: if true faces player using y axis only texture = "image.png", diff --git a/src/client.h b/src/client.h index cdadb9d3e..a7eb22ad9 100644 --- a/src/client.h +++ b/src/client.h @@ -182,6 +182,7 @@ struct ClientEvent f32 expirationtime; f32 size; bool collisiondetection; + bool collision_removal; bool vertical; std::string *texture; } spawn_particle; @@ -199,6 +200,7 @@ struct ClientEvent f32 minsize; f32 maxsize; bool collisiondetection; + bool collision_removal; bool vertical; std::string *texture; u32 id; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 0498f4048..48c573da5 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -898,8 +898,10 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt) bool collisiondetection = readU8(is); std::string texture = deSerializeLongString(is); bool vertical = false; + bool collision_removal = false; try { vertical = readU8(is); + collision_removal = readU8(is); } catch (...) {} ClientEvent event; @@ -910,6 +912,7 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt) event.spawn_particle.expirationtime = expirationtime; event.spawn_particle.size = size; event.spawn_particle.collisiondetection = collisiondetection; + event.spawn_particle.collision_removal = collision_removal; event.spawn_particle.vertical = vertical; event.spawn_particle.texture = new std::string(texture); @@ -942,8 +945,11 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) *pkt >> id; bool vertical = false; + bool collision_removal = false; try { *pkt >> vertical; + *pkt >> collision_removal; + } catch (...) {} ClientEvent event; @@ -961,6 +967,7 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) event.add_particlespawner.minsize = minsize; event.add_particlespawner.maxsize = maxsize; event.add_particlespawner.collisiondetection = collisiondetection; + event.add_particlespawner.collision_removal = collision_removal; event.add_particlespawner.vertical = vertical; event.add_particlespawner.texture = new std::string(texture); event.add_particlespawner.id = id; diff --git a/src/network/networkprotocol.h b/src/network/networkprotocol.h index 177b97680..3a9483efb 100644 --- a/src/network/networkprotocol.h +++ b/src/network/networkprotocol.h @@ -474,6 +474,7 @@ enum ToClientCommand u8 bool vertical u32 len u8[len] texture + u8 collision_removal */ TOCLIENT_ADD_PARTICLESPAWNER = 0x47, @@ -495,6 +496,7 @@ enum ToClientCommand u32 len u8[len] texture u32 id + u8 collision_removal */ TOCLIENT_DELETE_PARTICLESPAWNER_LEGACY = 0x48, diff --git a/src/particles.cpp b/src/particles.cpp index 525258a25..ccca691d1 100644 --- a/src/particles.cpp +++ b/src/particles.cpp @@ -54,6 +54,7 @@ Particle::Particle( float expirationtime, float size, bool collisiondetection, + bool collision_removal, bool vertical, video::ITexture *texture, v2f texpos, @@ -85,6 +86,7 @@ Particle::Particle( m_player = player; m_size = size; m_collisiondetection = collisiondetection; + m_collision_removal = collision_removal; m_vertical = vertical; // Irrlicht stuff @@ -126,20 +128,21 @@ void Particle::render() void Particle::step(float dtime) { m_time += dtime; - if (m_collisiondetection) - { + if (m_collisiondetection) { aabb3f box = m_collisionbox; - v3f p_pos = m_pos*BS; - v3f p_velocity = m_velocity*BS; - collisionMoveSimple(m_env, m_gamedef, - BS*0.5, box, - 0, dtime, - &p_pos, &p_velocity, m_acceleration * BS); - m_pos = p_pos/BS; - m_velocity = p_velocity/BS; - } - else - { + v3f p_pos = m_pos * BS; + v3f p_velocity = m_velocity * BS; + collisionMoveResult r = collisionMoveSimple(m_env, + m_gamedef, BS * 0.5, box, 0, dtime, &p_pos, + &p_velocity, m_acceleration * BS); + if (m_collision_removal && r.collides) { + // force expiration of the particle + m_expiration = -1.0; + } else { + m_pos = p_pos / BS; + m_velocity = p_velocity / BS; + } + } else { m_velocity += m_acceleration * dtime; m_pos += m_velocity * dtime; } @@ -210,8 +213,8 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, u16 amount, float time, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, - bool collisiondetection, bool vertical, video::ITexture *texture, u32 id, - ParticleManager *p_manager) : + bool collisiondetection, bool collision_removal, bool vertical, + video::ITexture *texture, u32 id, ParticleManager *p_manager) : m_particlemanager(p_manager) { m_gamedef = gamedef; @@ -230,6 +233,7 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, m_minsize = minsize; m_maxsize = maxsize; m_collisiondetection = collisiondetection; + m_collision_removal = collision_removal; m_vertical = vertical; m_texture = texture; m_time = 0; @@ -277,6 +281,7 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) exptime, size, m_collisiondetection, + m_collision_removal, m_vertical, m_texture, v2f(0.0, 0.0), @@ -317,6 +322,7 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) exptime, size, m_collisiondetection, + m_collision_removal, m_vertical, m_texture, v2f(0.0, 0.0), @@ -446,6 +452,7 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, event->add_particlespawner.minsize, event->add_particlespawner.maxsize, event->add_particlespawner.collisiondetection, + event->add_particlespawner.collision_removal, event->add_particlespawner.vertical, texture, event->add_particlespawner.id, @@ -480,6 +487,7 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, event->spawn_particle.expirationtime, event->spawn_particle.size, event->spawn_particle.collisiondetection, + event->spawn_particle.collision_removal, event->spawn_particle.vertical, texture, v2f(0.0, 0.0), @@ -555,6 +563,7 @@ void ParticleManager::addNodeParticle(IGameDef* gamedef, scene::ISceneManager* s visual_size, true, false, + false, texture, texpos, texsize); diff --git a/src/particles.h b/src/particles.h index dda84385c..bc3ca53b7 100644 --- a/src/particles.h +++ b/src/particles.h @@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc., struct ClientEvent; class ParticleManager; +class ClientEnvironment; class Particle : public scene::ISceneNode { @@ -45,6 +46,7 @@ class Particle : public scene::ISceneNode float expirationtime, float size, bool collisiondetection, + bool collision_removal, bool vertical, video::ITexture *texture, v2f texpos, @@ -97,6 +99,7 @@ private: float m_size; u8 m_light; bool m_collisiondetection; + bool m_collision_removal; bool m_vertical; v3s16 m_camera_offset; }; @@ -115,6 +118,7 @@ class ParticleSpawner float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, + bool collision_removal, bool vertical, video::ITexture *texture, u32 id, @@ -148,6 +152,7 @@ class ParticleSpawner video::ITexture *m_texture; std::vector m_spawntimes; bool m_collisiondetection; + bool m_collision_removal; bool m_vertical; }; diff --git a/src/script/lua_api/l_particles.cpp b/src/script/lua_api/l_particles.cpp index f6c1725de..263e35407 100644 --- a/src/script/lua_api/l_particles.cpp +++ b/src/script/lua_api/l_particles.cpp @@ -21,13 +21,16 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_internal.h" #include "common/c_converter.h" #include "server.h" +#include "particles.h" // add_particle({pos=, velocity=, acceleration=, expirationtime=, -// size=, collisiondetection=, vertical=, texture=, player=}) +// size=, collisiondetection=, collision_removal=, vertical=, +// texture=, player=}) // pos/velocity/acceleration = {x=num, y=num, z=num} // expirationtime = num (seconds) // size = num // collisiondetection = bool +// collision_removal = bool // vertical = bool // texture = e.g."default_wood.png" int ModApiParticles::l_add_particle(lua_State *L) @@ -41,8 +44,8 @@ int ModApiParticles::l_add_particle(lua_State *L) float expirationtime, size; expirationtime = size = 1; - bool collisiondetection, vertical; - collisiondetection = vertical = false; + bool collisiondetection, vertical, collision_removal; + collisiondetection = vertical = collision_removal = false; std::string texture = ""; std::string playername = ""; @@ -94,12 +97,14 @@ int ModApiParticles::l_add_particle(lua_State *L) size = getfloatfield_default(L, 1, "size", 1); collisiondetection = getboolfield_default(L, 1, "collisiondetection", collisiondetection); + collision_removal = getboolfield_default(L, 1, + "collision_removal", collision_removal); vertical = getboolfield_default(L, 1, "vertical", vertical); texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); } - getServer(L)->spawnParticle(playername, pos, vel, acc, - expirationtime, size, collisiondetection, vertical, texture); + getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, size, + collisiondetection, collision_removal, vertical, texture); return 1; } @@ -110,6 +115,7 @@ int ModApiParticles::l_add_particle(lua_State *L) // minexptime=, maxexptime=, // minsize=, maxsize=, // collisiondetection=, +// collision_removal=, // vertical=, // texture=, // player=}) @@ -117,6 +123,7 @@ int ModApiParticles::l_add_particle(lua_State *L) // minexptime/maxexptime = num (seconds) // minsize/maxsize = num // collisiondetection = bool +// collision_removal = bool // vertical = bool // texture = e.g."default_wood.png" int ModApiParticles::l_add_particlespawner(lua_State *L) @@ -129,8 +136,8 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0); float time, minexptime, maxexptime, minsize, maxsize; time= minexptime= maxexptime= minsize= maxsize= 1; - bool collisiondetection, vertical; - collisiondetection= vertical= false; + bool collisiondetection, vertical, collision_removal; + collisiondetection = vertical = collision_removal = false; std::string texture = ""; std::string playername = ""; @@ -189,6 +196,8 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) maxsize = getfloatfield_default(L, 1, "maxsize", maxsize); collisiondetection = getboolfield_default(L, 1, "collisiondetection", collisiondetection); + collision_removal = getboolfield_default(L, 1, + "collision_removal", collision_removal); vertical = getboolfield_default(L, 1, "vertical", vertical); texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); @@ -201,6 +210,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) minexptime, maxexptime, minsize, maxsize, collisiondetection, + collision_removal, vertical, texture, playername); lua_pushnumber(L, id); diff --git a/src/server.cpp b/src/server.cpp index a3b686c25..ada45dc68 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1673,7 +1673,8 @@ void Server::SendShowFormspecMessage(u16 peer_id, const std::string &formspec, // Spawns a particle on peer with peer_id void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, - bool vertical, std::string texture) + bool collision_removal, + bool vertical, const std::string &texture) { DSTACK(FUNCTION_NAME); @@ -1683,6 +1684,7 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat << size << collisiondetection; pkt.putLongString(texture); pkt << vertical; + pkt << collision_removal; if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); @@ -1695,7 +1697,8 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat // Adds a ParticleSpawner on peer with peer_id void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, - float minsize, float maxsize, bool collisiondetection, bool vertical, std::string texture, u32 id) + float minsize, float maxsize, bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture, u32 id) { DSTACK(FUNCTION_NAME); @@ -1708,6 +1711,7 @@ void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3 pkt.putLongString(texture); pkt << id << vertical; + pkt << collision_removal; if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); @@ -3160,7 +3164,8 @@ void Server::notifyPlayers(const std::wstring &msg) void Server::spawnParticle(const std::string &playername, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool - collisiondetection, bool vertical, const std::string &texture) + collisiondetection, bool collision_removal, + bool vertical, const std::string &texture) { // m_env will be NULL if the server is initializing if (!m_env) @@ -3175,13 +3180,15 @@ void Server::spawnParticle(const std::string &playername, v3f pos, } SendSpawnParticle(peer_id, pos, velocity, acceleration, - expirationtime, size, collisiondetection, vertical, texture); + expirationtime, size, collisiondetection, + collision_removal, vertical, texture); } u32 Server::addParticleSpawner(u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, - bool collisiondetection, bool vertical, const std::string &texture, + bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture, const std::string &playername) { // m_env will be NULL if the server is initializing @@ -3200,7 +3207,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, SendAddParticleSpawner(peer_id, amount, spawntime, minpos, maxpos, minvel, maxvel, minacc, maxacc, minexptime, maxexptime, minsize, maxsize, - collisiondetection, vertical, texture, id); + collisiondetection, collision_removal, vertical, texture, id); return id; } diff --git a/src/server.h b/src/server.h index daf51dee1..7ee15a463 100644 --- a/src/server.h +++ b/src/server.h @@ -275,7 +275,8 @@ public: void spawnParticle(const std::string &playername, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, - bool collisiondetection, bool vertical, const std::string &texture); + bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture); u32 addParticleSpawner(u16 amount, float spawntime, v3f minpos, v3f maxpos, @@ -283,7 +284,8 @@ public: v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, - bool collisiondetection, bool vertical, const std::string &texture, + bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture, const std::string &playername); void deleteParticleSpawner(const std::string &playername, u32 id); @@ -456,7 +458,8 @@ private: v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, - bool collisiondetection, bool vertical, std::string texture, u32 id); + bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture, u32 id); void SendDeleteParticleSpawner(u16 peer_id, u32 id); @@ -464,7 +467,8 @@ private: void SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, - bool collisiondetection, bool vertical, std::string texture); + bool collisiondetection, bool collision_removal, + bool vertical, const std::string &texture); u32 SendActiveObjectRemoveAdd(u16 peer_id, const std::string &datas); void SendActiveObjectMessages(u16 peer_id, const std::string &datas, bool reliable = true); -- cgit v1.2.3 From a9bc7dc405df04d5a3cb5fe5de43190200dc692d Mon Sep 17 00:00:00 2001 From: Zeno- Date: Mon, 30 May 2016 22:37:40 +1000 Subject: Remove unused code in s_security.cpp (#4172) Note that the macro CHECK_FILE_ERR implements the code removed --- src/script/cpp_api/s_security.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'src/script') diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index 730235c7b..e117a4811 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -249,8 +249,8 @@ bool ScriptApiSecurity::isSecure(lua_State *L) #define CHECK_FILE_ERR(ret, fp) \ if (ret) { \ - if (fp) std::fclose(fp); \ lua_pushfstring(L, "%s: %s", path, strerror(errno)); \ + if (fp) std::fclose(fp); \ return false; \ } @@ -291,20 +291,12 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path) // Read the file int ret = std::fseek(fp, 0, SEEK_END); CHECK_FILE_ERR(ret, fp); - if (ret) { - std::fclose(fp); - lua_pushfstring(L, "%s: %s", path, strerror(errno)); - return false; - } + size_t size = std::ftell(fp) - start; char *code = new char[size]; ret = std::fseek(fp, start, SEEK_SET); CHECK_FILE_ERR(ret, fp); - if (ret) { - std::fclose(fp); - lua_pushfstring(L, "%s: %s", path, strerror(errno)); - return false; - } + size_t num_read = std::fread(code, 1, size, fp); if (path) { std::fclose(fp); -- cgit v1.2.3 From 27db92925261ed6646d0a1c0512711ab3aeb5fb8 Mon Sep 17 00:00:00 2001 From: est31 Date: Mon, 30 May 2016 23:27:48 +1000 Subject: Add minetest.check_password_entry callback Gives a convenient way to check a player's password. This entirely bypasses the SRP protocol, so should be used with great care. This function is not intended to be used in-game, but solely by external protocols, where no authentication of the minetest engine is provided, and also only for protocols, in which the user already gives the server the plaintext password. Examples for good use are the classical http form, or irc, an example for a bad use is a password change dialog inside formspec. Users should be aware that they lose the advantages of the SRP protocol if they enter their passwords for servers outside the normal entry box, like in in-game formspec menus, or through irc /msg s, This patch also fixes an auth.h mistake which has mixed up the order of params inside the decode_srp_verifier_and_salt function. Zeno-: Added errorstream message for invalid format when I committed --- doc/lua_api.txt | 9 +++++++++ src/script/lua_api/l_util.cpp | 30 ++++++++++++++++++++++++++++++ src/script/lua_api/l_util.h | 3 +++ src/util/auth.h | 2 +- 4 files changed, 43 insertions(+), 1 deletion(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 03f2dad32..82a0acbee 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1951,12 +1951,21 @@ Call these functions only at load time! * `minetest.notify_authentication_modified(name)` * Should be called by the authentication handler if privileges changes. * To report everybody, set `name=nil`. +* `minetest.check_password_entry(name, entry, password)` + * Returns true if the "db entry" for a player with name matches given + * password, false otherwise. + * The "db entry" is the usually player-individual value that is derived + * from the player's chosen password and stored on the server in order to allow + * authentication whenever the player desires to log in. + * Only use this function for making it possible to log in via the password from + * via protocols like IRC, other uses for inside the game are frowned upon. * `minetest.get_password_hash(name, raw_password)` * Convert a name-password pair to a password hash that Minetest can use. * The returned value alone is not a good basis for password checks based * on comparing the password hash in the database with the password hash * from the function, with an externally provided password, as the hash * in the db might use the new SRP verifier format. + * For this purpose, use minetest.check_password_entry instead. * `minetest.string_to_privs(str)`: returns `{priv1=true,...}` * `minetest.privs_to_string(privs)`: returns `"priv1,priv2,..."` * Convert between two privilege representations diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index e90b7fbcf..d090fc91c 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -246,6 +246,35 @@ int ModApiUtil::l_get_hit_params(lua_State *L) return 1; } +// check_password_entry(name, entry, password) +int ModApiUtil::l_check_password_entry(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + std::string name = luaL_checkstring(L, 1); + std::string entry = luaL_checkstring(L, 2); + std::string password = luaL_checkstring(L, 3); + + if (base64_is_valid(entry)) { + std::string hash = translate_password(name, password); + lua_pushboolean(L, hash == entry); + return 1; + } + + std::string salt; + std::string verifier; + + if (!decode_srp_verifier_and_salt(entry, &verifier, &salt)) { + // invalid format + warningstream << "Invalid password format for " << name << std::endl; + lua_pushboolean(L, false); + return 1; + } + std::string gen_verifier = generate_srp_verifier(name, password, salt); + + lua_pushboolean(L, gen_verifier == verifier); + return 1; +} + // get_password_hash(name, raw_password) int ModApiUtil::l_get_password_hash(lua_State *L) { @@ -449,6 +478,7 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(get_dig_params); API_FCT(get_hit_params); + API_FCT(check_password_entry); API_FCT(get_password_hash); API_FCT(is_yes); diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index 779dbe281..3012d55aa 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -71,6 +71,9 @@ private: // get_hit_params(groups, tool_capabilities[, time_from_last_punch]) static int l_get_hit_params(lua_State *L); + // check_password_entry(name, entry, password) + static int l_check_password_entry(lua_State *L); + // get_password_hash(name, raw_password) static int l_get_password_hash(lua_State *L); diff --git a/src/util/auth.h b/src/util/auth.h index 1fd6ab453..7cdc7d74d 100644 --- a/src/util/auth.h +++ b/src/util/auth.h @@ -45,6 +45,6 @@ std::string encode_srp_verifier(const std::string &verifier, /// Reads the DB-formatted SRP verifier and gets the verifier /// and salt components. bool decode_srp_verifier_and_salt(const std::string &encoded, - std::string *salt, std::string *bytes_v); + std::string *verifier, std::string *salt); #endif -- cgit v1.2.3 From dfbdb5bcd7bc48efb21d585d5c22454a9d5f0f1e Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sat, 4 Jun 2016 01:35:37 -0400 Subject: Change internal type for seeds to s32 This fixes value truncation (and therefore incompatibility) on platforms with an LP32 data model, such as VAX or MS-DOS. --- src/cavegen.cpp | 4 ++-- src/cavegen.h | 6 +++--- src/dungeongen.h | 2 +- src/mapgen.cpp | 20 +++++++++++++++++--- src/mapgen.h | 6 +++--- src/mg_biome.h | 2 +- src/noise.cpp | 26 +++++++++++++------------- src/noise.h | 32 ++++++++++++++++---------------- src/script/lua_api/l_env.cpp | 2 +- src/script/lua_api/l_noise.cpp | 2 +- src/script/lua_api/l_noise.h | 4 ++-- src/treegen.cpp | 8 ++++---- src/treegen.h | 8 ++++---- 13 files changed, 68 insertions(+), 54 deletions(-) (limited to 'src/script') diff --git a/src/cavegen.cpp b/src/cavegen.cpp index 1c25ce711..dc7355fe0 100644 --- a/src/cavegen.cpp +++ b/src/cavegen.cpp @@ -35,7 +35,7 @@ static NoiseParams nparams_caveliquids(0, 1, v3f(150.0, 150.0, 150.0), 776, 3, 0 CavesNoiseIntersection::CavesNoiseIntersection( INodeDefManager *nodedef, BiomeManager *biomemgr, v3s16 chunksize, - NoiseParams *np_cave1, NoiseParams *np_cave2, int seed, float cave_width) + NoiseParams *np_cave1, NoiseParams *np_cave2, s32 seed, float cave_width) { assert(nodedef); assert(biomemgr); @@ -130,7 +130,7 @@ void CavesNoiseIntersection::generateCaves(MMVManip *vm, CavesRandomWalk::CavesRandomWalk( INodeDefManager *ndef, GenerateNotifier *gennotify, - int seed, + s32 seed, int water_level, content_t water_source, content_t lava_source) diff --git a/src/cavegen.h b/src/cavegen.h index da0894635..2bf503d47 100644 --- a/src/cavegen.h +++ b/src/cavegen.h @@ -41,7 +41,7 @@ class CavesNoiseIntersection { public: CavesNoiseIntersection(INodeDefManager *nodedef, BiomeManager *biomemgr, v3s16 chunksize, NoiseParams *np_cave1, NoiseParams *np_cave2, - int seed, float cave_width); + s32 seed, float cave_width); ~CavesNoiseIntersection(); void generateCaves(MMVManip *vm, v3s16 nmin, v3s16 nmax, u8 *biomemap); @@ -83,7 +83,7 @@ public: s16 *heightmap; // configurable parameters - int seed; + s32 seed; int water_level; int lava_depth; NoiseParams *np_caveliquids; @@ -122,7 +122,7 @@ public: // If gennotify is NULL, generation events are not logged. CavesRandomWalk(INodeDefManager *ndef, GenerateNotifier *gennotify = NULL, - int seed = 0, + s32 seed = 0, int water_level = 1, content_t water_source = CONTENT_IGNORE, content_t lava_source = CONTENT_IGNORE); diff --git a/src/dungeongen.h b/src/dungeongen.h index f0a5e5ba8..898627483 100644 --- a/src/dungeongen.h +++ b/src/dungeongen.h @@ -39,7 +39,7 @@ int dir_to_facedir(v3s16 d); struct DungeonParams { - int seed; + s32 seed; content_t c_water; content_t c_river_water; diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 041356e3d..32f7e29eb 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -89,11 +89,25 @@ Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) : { generating = false; id = mapgenid; - seed = (int)params->seed; water_level = params->water_level; flags = params->flags; csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE); + /* + We are losing half our entropy by doing this, but it is necessary to + preserve reverse compatibility. If the top half of our current 64 bit + seeds ever starts getting used, existing worlds will break due to a + different hash outcome and no way to differentiate between versions. + + A solution could be to add a new bit to designate that the top half of + the seed value should be used, essentially a 1-bit version code, but + this would require increasing the total size of a seed to 9 bytes (yuck) + + It's probably okay if this never gets fixed. 4.2 billion possibilities + ought to be enough for anyone. + */ + seed = (s32)params->seed; + vm = NULL; ndef = emerge->ndef; biomegen = NULL; @@ -107,7 +121,7 @@ Mapgen::~Mapgen() } -u32 Mapgen::getBlockSeed(v3s16 p, int seed) +u32 Mapgen::getBlockSeed(v3s16 p, s32 seed) { return (u32)seed + p.Z * 38134234 + @@ -116,7 +130,7 @@ u32 Mapgen::getBlockSeed(v3s16 p, int seed) } -u32 Mapgen::getBlockSeed2(v3s16 p, int seed) +u32 Mapgen::getBlockSeed2(v3s16 p, s32 seed) { u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed; n = (n >> 13) ^ n; diff --git a/src/mapgen.h b/src/mapgen.h index 10595fafc..0342fd46e 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -150,7 +150,7 @@ struct MapgenParams { */ class Mapgen { public: - int seed; + s32 seed; int water_level; u32 flags; bool generating; @@ -171,8 +171,8 @@ public: Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge); virtual ~Mapgen(); - static u32 getBlockSeed(v3s16 p, int seed); - static u32 getBlockSeed2(v3s16 p, int seed); + static u32 getBlockSeed(v3s16 p, s32 seed); + static u32 getBlockSeed2(v3s16 p, s32 seed); s16 findGroundLevelFull(v2s16 p2d); s16 findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax); s16 findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax); diff --git a/src/mg_biome.h b/src/mg_biome.h index e78e90e5f..389b7664f 100644 --- a/src/mg_biome.h +++ b/src/mg_biome.h @@ -80,7 +80,7 @@ struct BiomeParams { virtual void writeParams(Settings *settings) const = 0; virtual ~BiomeParams() {} - int seed; + s32 seed; }; class BiomeGen { diff --git a/src/noise.cpp b/src/noise.cpp index 2ddc3926f..c57c98ccb 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -156,7 +156,7 @@ s32 PcgRandom::randNormalDist(s32 min, s32 max, int num_trials) /////////////////////////////////////////////////////////////////////////////// -float noise2d(int x, int y, int seed) +float noise2d(int x, int y, s32 seed) { unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_SEED * seed) & 0x7fffffff; @@ -166,7 +166,7 @@ float noise2d(int x, int y, int seed) } -float noise3d(int x, int y, int z, int seed) +float noise3d(int x, int y, int z, s32 seed) { unsigned int n = (NOISE_MAGIC_X * x + NOISE_MAGIC_Y * y + NOISE_MAGIC_Z * z + NOISE_MAGIC_SEED * seed) & 0x7fffffff; @@ -235,7 +235,7 @@ float triLinearInterpolationNoEase( return linearInterpolation(u, v, z); } -float noise2d_gradient(float x, float y, int seed, bool eased) +float noise2d_gradient(float x, float y, s32 seed, bool eased) { // Calculate the integer coordinates int x0 = myfloor(x); @@ -256,7 +256,7 @@ float noise2d_gradient(float x, float y, int seed, bool eased) } -float noise3d_gradient(float x, float y, float z, int seed, bool eased) +float noise3d_gradient(float x, float y, float z, s32 seed, bool eased) { // Calculate the integer coordinates int x0 = myfloor(x); @@ -290,7 +290,7 @@ float noise3d_gradient(float x, float y, float z, int seed, bool eased) } -float noise2d_perlin(float x, float y, int seed, +float noise2d_perlin(float x, float y, s32 seed, int octaves, float persistence, bool eased) { float a = 0; @@ -306,7 +306,7 @@ float noise2d_perlin(float x, float y, int seed, } -float noise2d_perlin_abs(float x, float y, int seed, +float noise2d_perlin_abs(float x, float y, s32 seed, int octaves, float persistence, bool eased) { float a = 0; @@ -321,7 +321,7 @@ float noise2d_perlin_abs(float x, float y, int seed, } -float noise3d_perlin(float x, float y, float z, int seed, +float noise3d_perlin(float x, float y, float z, s32 seed, int octaves, float persistence, bool eased) { float a = 0; @@ -336,7 +336,7 @@ float noise3d_perlin(float x, float y, float z, int seed, } -float noise3d_perlin_abs(float x, float y, float z, int seed, +float noise3d_perlin_abs(float x, float y, float z, s32 seed, int octaves, float persistence, bool eased) { float a = 0; @@ -363,7 +363,7 @@ float contour(float v) ///////////////////////// [ New noise ] //////////////////////////// -float NoisePerlin2D(NoiseParams *np, float x, float y, int seed) +float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed) { float a = 0; float f = 1.0; @@ -389,7 +389,7 @@ float NoisePerlin2D(NoiseParams *np, float x, float y, int seed) } -float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed) +float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed) { float a = 0; float f = 1.0; @@ -416,7 +416,7 @@ float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed) } -Noise::Noise(NoiseParams *np_, int seed, u32 sx, u32 sy, u32 sz) +Noise::Noise(NoiseParams *np_, s32 seed, u32 sx, u32 sy, u32 sz) { memcpy(&np, np_, sizeof(np)); this->seed = seed; @@ -543,7 +543,7 @@ void Noise::resizeNoiseBuf(bool is3d) void Noise::gradientMap2D( float x, float y, float step_x, float step_y, - int seed) + s32 seed) { float v00, v01, v10, v11, u, v, orig_u; u32 index, i, j, noisex, noisey; @@ -607,7 +607,7 @@ void Noise::gradientMap2D( void Noise::gradientMap3D( float x, float y, float z, float step_x, float step_y, float step_z, - int seed) + s32 seed) { float v000, v010, v100, v110; float v001, v011, v101, v111; diff --git a/src/noise.h b/src/noise.h index 0e4252dd4..41b93ae01 100644 --- a/src/noise.h +++ b/src/noise.h @@ -148,7 +148,7 @@ struct NoiseParams { class Noise { public: NoiseParams np; - int seed; + s32 seed; u32 sx; u32 sy; u32 sz; @@ -157,7 +157,7 @@ public: float *persist_buf; float *result; - Noise(NoiseParams *np, int seed, u32 sx, u32 sy, u32 sz=1); + Noise(NoiseParams *np, s32 seed, u32 sx, u32 sy, u32 sz=1); ~Noise(); void setSize(u32 sx, u32 sy, u32 sz=1); @@ -167,11 +167,11 @@ public: void gradientMap2D( float x, float y, float step_x, float step_y, - int seed); + s32 seed); void gradientMap3D( float x, float y, float z, float step_x, float step_y, float step_z, - int seed); + s32 seed); float *perlinMap2D(float x, float y, float *persistence_map=NULL); float *perlinMap3D(float x, float y, float z, float *persistence_map=NULL); @@ -202,11 +202,11 @@ private: }; -float NoisePerlin2D(NoiseParams *np, float x, float y, int seed); -float NoisePerlin3D(NoiseParams *np, float x, float y, float z, int seed); +float NoisePerlin2D(NoiseParams *np, float x, float y, s32 seed); +float NoisePerlin3D(NoiseParams *np, float x, float y, float z, s32 seed); inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff, - float y, float yoff, int seed) + float y, float yoff, s32 seed) { return NoisePerlin2D(np, x + xoff * np->spread.X, @@ -215,7 +215,7 @@ inline float NoisePerlin2D_PO(NoiseParams *np, float x, float xoff, } inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff, - float y, float yoff, float z, float zoff, int seed) + float y, float yoff, float z, float zoff, s32 seed) { return NoisePerlin3D(np, x + xoff * np->spread.X, @@ -225,22 +225,22 @@ inline float NoisePerlin3D_PO(NoiseParams *np, float x, float xoff, } // Return value: -1 ... 1 -float noise2d(int x, int y, int seed); -float noise3d(int x, int y, int z, int seed); +float noise2d(int x, int y, s32 seed); +float noise3d(int x, int y, int z, s32 seed); -float noise2d_gradient(float x, float y, int seed, bool eased=true); -float noise3d_gradient(float x, float y, float z, int seed, bool eased=false); +float noise2d_gradient(float x, float y, s32 seed, bool eased=true); +float noise3d_gradient(float x, float y, float z, s32 seed, bool eased=false); -float noise2d_perlin(float x, float y, int seed, +float noise2d_perlin(float x, float y, s32 seed, int octaves, float persistence, bool eased=true); -float noise2d_perlin_abs(float x, float y, int seed, +float noise2d_perlin_abs(float x, float y, s32 seed, int octaves, float persistence, bool eased=true); -float noise3d_perlin(float x, float y, float z, int seed, +float noise3d_perlin(float x, float y, float z, s32 seed, int octaves, float persistence, bool eased=false); -float noise3d_perlin_abs(float x, float y, float z, int seed, +float noise3d_perlin_abs(float x, float y, float z, s32 seed, int octaves, float persistence, bool eased=false); inline float easeCurve(float t) diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 8284c3fcb..ebdea09e4 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -758,7 +758,7 @@ int ModApiEnvMod::l_get_perlin_map(lua_State *L) return 0; v3s16 size = read_v3s16(L, 2); - int seed = (int)(env->getServerMap().getSeed()); + s32 seed = (s32)(env->getServerMap().getSeed()); LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(&np, seed, size); *(void **)(lua_newuserdata(L, sizeof(void *))) = n; luaL_getmetatable(L, "PerlinNoiseMap"); diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp index 04dc6048f..e0039371f 100644 --- a/src/script/lua_api/l_noise.cpp +++ b/src/script/lua_api/l_noise.cpp @@ -146,7 +146,7 @@ const luaL_reg LuaPerlinNoise::methods[] = { LuaPerlinNoiseMap */ -LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, int seed, v3s16 size) +LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, s32 seed, v3s16 size) { m_is3d = size.Z > 1; np = *params; diff --git a/src/script/lua_api/l_noise.h b/src/script/lua_api/l_noise.h index 492eb7550..40bfd1315 100644 --- a/src/script/lua_api/l_noise.h +++ b/src/script/lua_api/l_noise.h @@ -79,7 +79,7 @@ class LuaPerlinNoiseMap : public ModApiBase { static int l_getMapSlice(lua_State *L); public: - LuaPerlinNoiseMap(NoiseParams *np, int seed, v3s16 size); + LuaPerlinNoiseMap(NoiseParams *np, s32 seed, v3s16 size); ~LuaPerlinNoiseMap(); @@ -111,7 +111,7 @@ private: static int l_next(lua_State *L); public: - LuaPseudoRandom(int seed) : + LuaPseudoRandom(s32 seed) : m_pseudo(seed) {} // LuaPseudoRandom(seed) diff --git a/src/treegen.cpp b/src/treegen.cpp index 208f34552..36d387c57 100644 --- a/src/treegen.cpp +++ b/src/treegen.cpp @@ -31,7 +31,7 @@ namespace treegen { void make_tree(MMVManip &vmanip, v3s16 p0, - bool is_apple_tree, INodeDefManager *ndef, int seed) + bool is_apple_tree, INodeDefManager *ndef, s32 seed) { /* NOTE: Tree-placing code is currently duplicated in the engine @@ -149,7 +149,7 @@ treegen::error make_ltree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, TreeDef tree_definition) { MapNode dirtnode(ndef->getId("mapgen_dirt")); - int seed; + s32 seed; if (tree_definition.explicit_seed) seed = tree_definition.seed + 14002; else @@ -649,7 +649,7 @@ v3f transposeMatrix(irr::core::matrix4 M, v3f v) } -void make_jungletree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, int seed) +void make_jungletree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, s32 seed) { /* NOTE: Tree-placing code is currently duplicated in the engine @@ -748,7 +748,7 @@ void make_jungletree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, int seed } -void make_pine_tree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, int seed) +void make_pine_tree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, s32 seed) { /* NOTE: Tree-placing code is currently duplicated in the engine diff --git a/src/treegen.h b/src/treegen.h index 4b0089d1e..4e6f95e67 100644 --- a/src/treegen.h +++ b/src/treegen.h @@ -54,19 +54,19 @@ namespace treegen { bool thin_branches; MapNode fruitnode; int fruit_chance; - int seed; + s32 seed; bool explicit_seed; }; // Add default tree void make_tree(MMVManip &vmanip, v3s16 p0, - bool is_apple_tree, INodeDefManager *ndef, int seed); + bool is_apple_tree, INodeDefManager *ndef, s32 seed); // Add jungle tree void make_jungletree(MMVManip &vmanip, v3s16 p0, - INodeDefManager *ndef, int seed); + INodeDefManager *ndef, s32 seed); // Add pine tree void make_pine_tree(MMVManip &vmanip, v3s16 p0, - INodeDefManager *ndef, int seed); + INodeDefManager *ndef, s32 seed); // Add L-Systems tree (used by engine) treegen::error make_ltree(MMVManip &vmanip, v3s16 p0, INodeDefManager *ndef, -- cgit v1.2.3 From d24f3841740b471eff384c8bd6e8bbfdfd03a3e2 Mon Sep 17 00:00:00 2001 From: paramat Date: Fri, 3 Jun 2016 12:58:50 +0100 Subject: Biome API: Add per-biome riverbed material and depth Mgvalleys: Remove riverbed sand placement from base terrain generation Riverbed material placement moved to MapgenBasic::generateBiomes() Document fields and add note that the biome API is still unstable --- doc/lua_api.txt | 6 ++++++ src/mapgen.cpp | 24 ++++++++++++++++++------ src/mapgen_valleys.cpp | 13 ++----------- src/mg_biome.cpp | 3 +++ src/mg_biome.h | 2 ++ src/script/lua_api/l_mapgen.cpp | 2 ++ 6 files changed, 33 insertions(+), 17 deletions(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index f348f5103..30f4d87df 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -3723,6 +3723,9 @@ Definition tables ### Biome definition (`register_biome`) +**Note** +The biome API is still in an experimental phase and subject to change. + { name = "tundra", node_dust = "default:snow", @@ -3742,6 +3745,9 @@ Definition tables -- ^ Node that replaces all seawater nodes not in the defined surface layer. node_river_water = "default:ice", -- ^ Node that replaces river water in mapgens that use default:river_water. + node_riverbed = "default:gravel", + depth_riverbed = 2, + -- ^ Node placed under river water and thickness of this layer. y_min = 1, y_max = 31000, -- ^ Lower and upper limits for biome. diff --git a/src/mapgen.cpp b/src/mapgen.cpp index b5c48a471..66892a574 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -536,13 +536,15 @@ MgStoneType MapgenBasic::generateBiomes() u16 depth_top = 0; u16 base_filler = 0; u16 depth_water_top = 0; + u16 depth_riverbed = 0; u32 vi = vm->m_area.index(x, node_max.Y, z); // Check node at base of mapchunk above, either a node of a previously // generated mapchunk or if not, a node of overgenerated base terrain. content_t c_above = vm->m_data[vi + em.X].getContent(); bool air_above = c_above == CONTENT_AIR; - bool water_above = (c_above == c_water_source || c_above == c_river_water_source); + bool river_water_above = c_above == c_river_water_source; + bool water_above = c_above == c_water_source || river_water_above; // If there is air or water above enable top/filler placement, otherwise force // nplaced to stone level by setting a number exceeding any possible filler depth. @@ -564,10 +566,11 @@ MgStoneType MapgenBasic::generateBiomes() biome = biomegen->getBiomeAtIndex(index, y); depth_top = biome->depth_top; - base_filler = MYMAX(depth_top - + biome->depth_filler - + noise_filler_depth->result[index], 0.f); + base_filler = MYMAX(depth_top + + biome->depth_filler + + noise_filler_depth->result[index], 0.f); depth_water_top = biome->depth_water_top; + depth_riverbed = biome->depth_riverbed; // Detect stone type for dungeons during every biome calculation. // This is more efficient than detecting per-node and will not @@ -590,7 +593,15 @@ MgStoneType MapgenBasic::generateBiomes() || c_below == c_river_water_source) nplaced = U16_MAX; - if (nplaced < depth_top) { + if (river_water_above) { + if (nplaced < depth_riverbed) { + vm->m_data[vi] = MapNode(biome->c_riverbed); + nplaced++; + } else { + nplaced = U16_MAX; // Disable top/filler placement + river_water_above = false; + } + } else if (nplaced < depth_top) { vm->m_data[vi] = MapNode(biome->c_top); nplaced++; } else if (nplaced < base_filler) { @@ -610,9 +621,10 @@ MgStoneType MapgenBasic::generateBiomes() water_above = true; } else if (c == c_river_water_source) { vm->m_data[vi] = MapNode(biome->c_river_water); - nplaced = depth_top; // Enable filler placement for next surface + nplaced = 0; // Enable riverbed placement for next surface air_above = false; water_above = true; + river_water_above = true; } else if (c == CONTENT_AIR) { nplaced = 0; // Enable top/filler placement for next surface air_above = true; diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index 2cd733d36..6581b792f 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -114,11 +114,6 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager * // Resolve content to be used c_lava_source = ndef->getId("mapgen_lava_source"); - c_sand = ndef->getId("mapgen_sand"); - - // Fall back to more basic content if not defined - if (c_sand == CONTENT_IGNORE) - c_sand = c_stone; } @@ -493,7 +488,6 @@ int MapgenValleys::generateTerrain() MapNode n_air(CONTENT_AIR); MapNode n_river_water(c_river_water_source); - MapNode n_sand(c_sand); MapNode n_stone(c_stone); MapNode n_water(c_water_source); @@ -537,10 +531,7 @@ int MapgenValleys::generateTerrain() float surface_delta = (float)y - surface_y; bool river = y + 1 < river_y; - if (fabs(surface_delta) <= 0.5f && y > water_level && river) { - // river bottom - vm->m_data[index_data] = n_sand; - } else if (slope * fill > surface_delta) { + if (slope * fill > surface_delta) { // ground vm->m_data[index_data] = n_stone; if (y > heightmap[index_2d]) @@ -553,7 +544,7 @@ int MapgenValleys::generateTerrain() } else if (river) { // river vm->m_data[index_data] = n_river_water; - } else { + } else { // air vm->m_data[index_data] = n_air; } } diff --git a/src/mg_biome.cpp b/src/mg_biome.cpp index df728af33..78034bf6c 100644 --- a/src/mg_biome.cpp +++ b/src/mg_biome.cpp @@ -46,6 +46,7 @@ BiomeManager::BiomeManager(IGameDef *gamedef) : b->depth_top = 0; b->depth_filler = -MAX_MAP_GENERATION_LIMIT; b->depth_water_top = 0; + b->depth_riverbed = 0; b->y_min = -MAX_MAP_GENERATION_LIMIT; b->y_max = MAX_MAP_GENERATION_LIMIT; b->heat_point = 0.0; @@ -57,6 +58,7 @@ BiomeManager::BiomeManager(IGameDef *gamedef) : b->m_nodenames.push_back("mapgen_water_source"); b->m_nodenames.push_back("mapgen_water_source"); b->m_nodenames.push_back("mapgen_river_water_source"); + b->m_nodenames.push_back("mapgen_stone"); b->m_nodenames.push_back("ignore"); m_ndef->pendNodeResolve(b); @@ -237,5 +239,6 @@ void Biome::resolveNodeNames() getIdFromNrBacklog(&c_water_top, "mapgen_water_source", CONTENT_AIR); getIdFromNrBacklog(&c_water, "mapgen_water_source", CONTENT_AIR); getIdFromNrBacklog(&c_river_water, "mapgen_river_water_source", CONTENT_AIR); + getIdFromNrBacklog(&c_riverbed, "mapgen_stone", CONTENT_AIR); getIdFromNrBacklog(&c_dust, "ignore", CONTENT_IGNORE); } diff --git a/src/mg_biome.h b/src/mg_biome.h index 568d0b1ac..eb0a18a2f 100644 --- a/src/mg_biome.h +++ b/src/mg_biome.h @@ -54,11 +54,13 @@ public: content_t c_water_top; content_t c_water; content_t c_river_water; + content_t c_riverbed; content_t c_dust; s16 depth_top; s16 depth_filler; s16 depth_water_top; + s16 depth_riverbed; s16 y_min; s16 y_max; diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 6baf217af..dc188f8a4 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -378,6 +378,7 @@ Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef) b->depth_top = getintfield_default(L, index, "depth_top", 0); b->depth_filler = getintfield_default(L, index, "depth_filler", -31000); b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0); + b->depth_riverbed = getintfield_default(L, index, "depth_riverbed", 0); b->y_min = getintfield_default(L, index, "y_min", -31000); b->y_max = getintfield_default(L, index, "y_max", 31000); b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f); @@ -391,6 +392,7 @@ Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef) nn.push_back(getstringfield_default(L, index, "node_water_top", "")); nn.push_back(getstringfield_default(L, index, "node_water", "")); nn.push_back(getstringfield_default(L, index, "node_river_water", "")); + nn.push_back(getstringfield_default(L, index, "node_riverbed", "")); nn.push_back(getstringfield_default(L, index, "node_dust", "")); ndef->pendNodeResolve(b); -- cgit v1.2.3 From dac40af6eeeb7205d507046fd4d9ae06ae182095 Mon Sep 17 00:00:00 2001 From: Diego Martinez Date: Mon, 4 Jan 2016 22:49:11 -0300 Subject: Server: Add reason for leave to `on_leaveplayer` callbacks --- doc/lua_api.txt | 3 ++- src/script/cpp_api/s_player.cpp | 6 ++++-- src/script/cpp_api/s_player.h | 2 +- src/server.cpp | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index ea47d0044..d89b0bf7b 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1912,8 +1912,9 @@ Call these functions only at load time! * If it returns a string, the player is disconnected with that string as reason * `minetest.register_on_joinplayer(func(ObjectRef))` * Called when a player joins the game -* `minetest.register_on_leaveplayer(func(ObjectRef))` +* `minetest.register_on_leaveplayer(func(ObjectRef, timed_out))` * Called when a player leaves the game + * `timed_out`: True for timeout, false for other reasons. * `minetest.register_on_cheat(func(ObjectRef, cheat))` * Called when a player cheats * `cheat`: `{type=}`, where `` is one of: diff --git a/src/script/cpp_api/s_player.cpp b/src/script/cpp_api/s_player.cpp index 807430678..a8c07476c 100644 --- a/src/script/cpp_api/s_player.cpp +++ b/src/script/cpp_api/s_player.cpp @@ -135,7 +135,8 @@ void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player) runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); } -void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player) +void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player, + bool timeout) { SCRIPTAPI_PRECHECKHEADER @@ -144,7 +145,8 @@ void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player) lua_getfield(L, -1, "registered_on_leaveplayers"); // Call callbacks objectrefGetOrCreate(L, player); - runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); + lua_pushboolean(L, timeout); + runCallbacks(2, RUN_CALLBACKS_MODE_FIRST); } void ScriptApiPlayer::on_cheat(ServerActiveObject *player, diff --git a/src/script/cpp_api/s_player.h b/src/script/cpp_api/s_player.h index 2e4dc2222..86ee1b024 100644 --- a/src/script/cpp_api/s_player.h +++ b/src/script/cpp_api/s_player.h @@ -38,7 +38,7 @@ public: bool on_prejoinplayer(const std::string &name, const std::string &ip, std::string *reason); void on_joinplayer(ServerActiveObject *player); - void on_leaveplayer(ServerActiveObject *player); + void on_leaveplayer(ServerActiveObject *player, bool timeout); void on_cheat(ServerActiveObject *player, const std::string &cheat_type); bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter, float time_from_last_punch, diff --git a/src/server.cpp b/src/server.cpp index ada45dc68..f7f698d50 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2683,7 +2683,7 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason) PlayerSAO *playersao = player->getPlayerSAO(); assert(playersao); - m_script->on_leaveplayer(playersao); + m_script->on_leaveplayer(playersao, reason == CDR_TIMEOUT); playersao->disconnected(); } -- cgit v1.2.3 From 559dd9946988cd35a7c26bcafe7d0f8c42dc547a Mon Sep 17 00:00:00 2001 From: Ekdohibs Date: Mon, 21 Mar 2016 12:58:52 +0100 Subject: Make node timers more efficient --- src/content_nodemeta.cpp | 2 +- src/environment.cpp | 32 ++++++++-------- src/map.cpp | 9 +++-- src/map.h | 2 +- src/mapblock.h | 4 +- src/nodetimer.cpp | 72 +++++++++++++++++------------------ src/nodetimer.h | 77 ++++++++++++++++++++++++++++++-------- src/script/lua_api/l_nodetimer.cpp | 4 +- 8 files changed, 125 insertions(+), 77 deletions(-) (limited to 'src/script') diff --git a/src/content_nodemeta.cpp b/src/content_nodemeta.cpp index 7f4264d8e..79a32b6bf 100644 --- a/src/content_nodemeta.cpp +++ b/src/content_nodemeta.cpp @@ -186,7 +186,7 @@ void content_nodemeta_deserialize_legacy(std::istream &is, meta->set(p, data); if(need_timer) - timers->set(p, NodeTimer(1., 0.)); + timers->set(NodeTimer(1., 0., p)); } } diff --git a/src/environment.cpp b/src/environment.cpp index 413bc7ff1..eea264699 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -1030,17 +1030,17 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime) m_lbm_mgr.applyLBMs(this, block, stamp); // Run node timers - std::map elapsed_timers = + std::vector elapsed_timers = block->m_node_timers.step((float)dtime_s); - if(!elapsed_timers.empty()){ + if (!elapsed_timers.empty()) { MapNode n; - for(std::map::iterator + for (std::vector::iterator i = elapsed_timers.begin(); i != elapsed_timers.end(); ++i){ - n = block->getNodeNoEx(i->first); - v3s16 p = i->first + block->getPosRelative(); - if(m_script->node_on_timer(p,n,i->second.elapsed)) - block->setNodeTimer(i->first,NodeTimer(i->second.timeout,0)); + n = block->getNodeNoEx(i->position); + v3s16 p = i->position + block->getPosRelative(); + if (m_script->node_on_timer(p, n, i->elapsed)) + block->setNodeTimer(NodeTimer(i->timeout, 0, i->position)); } } @@ -1434,17 +1434,19 @@ void ServerEnvironment::step(float dtime) MOD_REASON_BLOCK_EXPIRED); // Run node timers - std::map elapsed_timers = + std::vector elapsed_timers = block->m_node_timers.step((float)dtime); - if(!elapsed_timers.empty()){ + if (!elapsed_timers.empty()) { MapNode n; - for(std::map::iterator + for (std::vector::iterator i = elapsed_timers.begin(); - i != elapsed_timers.end(); ++i){ - n = block->getNodeNoEx(i->first); - p = i->first + block->getPosRelative(); - if(m_script->node_on_timer(p,n,i->second.elapsed)) - block->setNodeTimer(i->first,NodeTimer(i->second.timeout,0)); + i != elapsed_timers.end(); ++i) { + n = block->getNodeNoEx(i->position); + p = i->position + block->getPosRelative(); + if (m_script->node_on_timer(p, n, i->elapsed)) { + block->setNodeTimer(NodeTimer( + i->timeout, 0, i->position)); + } } } } diff --git a/src/map.cpp b/src/map.cpp index 03daf4fa8..a1f2086ce 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -2087,11 +2087,13 @@ NodeTimer Map::getNodeTimer(v3s16 p) return NodeTimer(); } NodeTimer t = block->m_node_timers.get(p_rel); - return t; + NodeTimer nt(t.timeout, t.elapsed, p); + return nt; } -void Map::setNodeTimer(v3s16 p, NodeTimer t) +void Map::setNodeTimer(const NodeTimer &t) { + v3s16 p = t.position; v3s16 blockpos = getNodeBlockPos(p); v3s16 p_rel = p - blockpos*MAP_BLOCKSIZE; MapBlock *block = getBlockNoCreateNoEx(blockpos); @@ -2105,7 +2107,8 @@ void Map::setNodeTimer(v3s16 p, NodeTimer t) <m_node_timers.set(p_rel, t); + NodeTimer nt(t.timeout, t.elapsed, p_rel); + block->m_node_timers.set(nt); } void Map::removeNodeTimer(v3s16 p) diff --git a/src/map.h b/src/map.h index 78614d228..23da56471 100644 --- a/src/map.h +++ b/src/map.h @@ -327,7 +327,7 @@ public: */ NodeTimer getNodeTimer(v3s16 p); - void setNodeTimer(v3s16 p, NodeTimer t); + void setNodeTimer(const NodeTimer &t); void removeNodeTimer(v3s16 p); /* diff --git a/src/mapblock.h b/src/mapblock.h index 73c17ee60..5adfcf3fb 100644 --- a/src/mapblock.h +++ b/src/mapblock.h @@ -488,9 +488,9 @@ public: m_node_timers.remove(p); } - inline void setNodeTimer(v3s16 p, NodeTimer t) + inline void setNodeTimer(const NodeTimer &t) { - m_node_timers.set(p,t); + m_node_timers.set(t); } inline void clearNodeTimers() diff --git a/src/nodetimer.cpp b/src/nodetimer.cpp index 350940546..003d08782 100644 --- a/src/nodetimer.cpp +++ b/src/nodetimer.cpp @@ -47,36 +47,38 @@ void NodeTimerList::serialize(std::ostream &os, u8 map_format_version) const { if (map_format_version == 24) { // Version 0 is a placeholder for "nothing to see here; go away." - if (m_data.empty()) { + if (m_timers.empty()) { writeU8(os, 0); // version return; } writeU8(os, 1); // version - writeU16(os, m_data.size()); + writeU16(os, m_timers.size()); } if (map_format_version >= 25) { writeU8(os, 2 + 4 + 4); // length of the data for a single timer - writeU16(os, m_data.size()); + writeU16(os, m_timers.size()); } - for (std::map::const_iterator - i = m_data.begin(); - i != m_data.end(); ++i) { - v3s16 p = i->first; + for (std::multimap::const_iterator + i = m_timers.begin(); + i != m_timers.end(); ++i) { NodeTimer t = i->second; + NodeTimer nt = NodeTimer(t.timeout, + t.timeout - (f32)(i->first - m_time), t.position); + v3s16 p = t.position; u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X; writeU16(os, p16); - t.serialize(os); + nt.serialize(os); } } void NodeTimerList::deSerialize(std::istream &is, u8 map_format_version) { - m_data.clear(); + clear(); - if(map_format_version == 24){ + if (map_format_version == 24) { u8 timer_version = readU8(is); if(timer_version == 0) return; @@ -84,7 +86,7 @@ void NodeTimerList::deSerialize(std::istream &is, u8 map_format_version) throw SerializationError("unsupported NodeTimerList version"); } - if(map_format_version >= 25){ + if (map_format_version >= 25) { u8 timer_data_len = readU8(is); if(timer_data_len != 2+4+4) throw SerializationError("unsupported NodeTimer data length"); @@ -92,8 +94,7 @@ void NodeTimerList::deSerialize(std::istream &is, u8 map_format_version) u16 count = readU16(is); - for(u16 i=0; i NodeTimerList::step(float dtime) +std::vector NodeTimerList::step(float dtime) { - std::map elapsed_timers; - // Increment timers - for(std::map::iterator - i = m_data.begin(); - i != m_data.end(); ++i){ - v3s16 p = i->first; + std::vector elapsed_timers; + m_time += dtime; + if (m_next_trigger_time == -1. || m_time < m_next_trigger_time) { + return elapsed_timers; + } + std::multimap::iterator i = m_timers.begin(); + // Process timers + for (; i != m_timers.end() && i->first <= m_time; ++i) { NodeTimer t = i->second; - t.elapsed += dtime; - if(t.elapsed >= t.timeout) - elapsed_timers.insert(std::make_pair(p, t)); - else - i->second = t; + t.elapsed = t.timeout + (f32)(m_time - i->first); + elapsed_timers.push_back(t); + m_iterators.erase(t.position); } // Delete elapsed timers - for(std::map::const_iterator - i = elapsed_timers.begin(); - i != elapsed_timers.end(); ++i){ - v3s16 p = i->first; - m_data.erase(p); - } + m_timers.erase(m_timers.begin(), i); + if (m_timers.empty()) + m_next_trigger_time = -1.; + else + m_next_trigger_time = m_timers.begin()->first; return elapsed_timers; } diff --git a/src/nodetimer.h b/src/nodetimer.h index 9fb56edec..0fd43b2a8 100644 --- a/src/nodetimer.h +++ b/src/nodetimer.h @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irr_v3d.h" #include #include +#include /* NodeTimer provides per-node timed callback functionality. @@ -36,8 +37,10 @@ class NodeTimer { public: NodeTimer(): timeout(0.), elapsed(0.) {} - NodeTimer(f32 timeout_, f32 elapsed_): - timeout(timeout_), elapsed(elapsed_) {} + NodeTimer(const v3s16 &position_): + timeout(0.), elapsed(0.), position(position_) {} + NodeTimer(f32 timeout_, f32 elapsed_, v3s16 position_): + timeout(timeout_), elapsed(elapsed_), position(position_) {} ~NodeTimer() {} void serialize(std::ostream &os) const; @@ -45,6 +48,7 @@ public: f32 timeout; f32 elapsed; + v3s16 position; }; /* @@ -54,37 +58,78 @@ public: class NodeTimerList { public: - NodeTimerList() {} + NodeTimerList(): m_next_trigger_time(-1.), m_time(0.) {} ~NodeTimerList() {} void serialize(std::ostream &os, u8 map_format_version) const; void deSerialize(std::istream &is, u8 map_format_version); // Get timer - NodeTimer get(v3s16 p){ - std::map::iterator n = m_data.find(p); - if(n == m_data.end()) + NodeTimer get(const v3s16 &p) { + std::map::iterator>::iterator n = + m_iterators.find(p); + if (n == m_iterators.end()) return NodeTimer(); - return n->second; + NodeTimer t = n->second->second; + t.elapsed = t.timeout - (n->second->first - m_time); + return t; } // Deletes timer - void remove(v3s16 p){ - m_data.erase(p); + void remove(v3s16 p) { + std::map::iterator>::iterator n = + m_iterators.find(p); + if(n != m_iterators.end()) { + double removed_time = n->second->first; + m_timers.erase(n->second); + m_iterators.erase(n); + // Yes, this is float equality, but it is not a problem + // since we only test equality of floats as an ordered type + // and thus we never lose precision + if (removed_time == m_next_trigger_time) { + if (m_timers.empty()) + m_next_trigger_time = -1.; + else + m_next_trigger_time = m_timers.begin()->first; + } + } + } + // Undefined behaviour if there already is a timer + void insert(NodeTimer timer) { + v3s16 p = timer.position; + double trigger_time = m_time + (double)(timer.timeout - timer.elapsed); + std::multimap::iterator it = + m_timers.insert(std::pair( + trigger_time, timer + )); + m_iterators.insert( + std::pair::iterator>(p, it)); + if (m_next_trigger_time == -1. || trigger_time < m_next_trigger_time) + m_next_trigger_time = trigger_time; } // Deletes old timer and sets a new one - void set(v3s16 p, NodeTimer t){ - m_data[p] = t; + inline void set(const NodeTimer &timer) { + remove(timer.position); + insert(timer); } // Deletes all timers - void clear(){ - m_data.clear(); + void clear() { + m_timers.clear(); + m_iterators.clear(); + m_next_trigger_time = -1.; + } + + inline double getNextTriggerTime() { + return m_next_trigger_time; } - // A step in time. Returns map of elapsed timers. - std::map step(float dtime); + // Move forward in time, returns elapsed timers + std::vector step(float dtime); private: - std::map m_data; + std::multimap m_timers; + std::map::iterator> m_iterators; + double m_next_trigger_time; + double m_time; }; #endif diff --git a/src/script/lua_api/l_nodetimer.cpp b/src/script/lua_api/l_nodetimer.cpp index 601113516..3242d6ea5 100644 --- a/src/script/lua_api/l_nodetimer.cpp +++ b/src/script/lua_api/l_nodetimer.cpp @@ -45,7 +45,7 @@ int NodeTimerRef::l_set(lua_State *L) if(env == NULL) return 0; f32 t = luaL_checknumber(L,2); f32 e = luaL_checknumber(L,3); - env->getMap().setNodeTimer(o->m_p,NodeTimer(t,e)); + env->getMap().setNodeTimer(NodeTimer(t, e, o->m_p)); return 0; } @@ -56,7 +56,7 @@ int NodeTimerRef::l_start(lua_State *L) ServerEnvironment *env = o->m_env; if(env == NULL) return 0; f32 t = luaL_checknumber(L,2); - env->getMap().setNodeTimer(o->m_p,NodeTimer(t,0)); + env->getMap().setNodeTimer(NodeTimer(t, 0, o->m_p)); return 0; } -- cgit v1.2.3 From fa0bbbf96df17f0d7911274ea85e5c049c20d07b Mon Sep 17 00:00:00 2001 From: raymoo Date: Fri, 17 Jun 2016 12:57:27 -0700 Subject: Player: New get_look, set_look API Deprecate get_look / set_look pitch / yaw --- doc/lua_api.txt | 18 +++++++--- src/player.h | 16 +++++++-- src/script/lua_api/l_object.cpp | 80 +++++++++++++++++++++++++++++++++++++++-- src/script/lua_api/l_object.h | 16 +++++++++ 4 files changed, 121 insertions(+), 9 deletions(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index ed8f8504e..6d359379e 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2679,10 +2679,20 @@ This is basically a reference to a C++ `ServerActiveObject` * `get_player_velocity()`: returns `nil` if is not a player, otherwise a table {x, y, z} representing the player's instantaneous velocity in nodes/s * `get_look_dir()`: get camera direction as a unit vector -* `get_look_pitch()`: pitch in radians -* `get_look_yaw()`: yaw in radians (wraps around pretty randomly as of now) -* `set_look_pitch(radians)`: sets look pitch -* `set_look_yaw(radians)`: sets look yaw +* `get_look_vertical()`: pitch in radians + * Angle ranges between -pi/2 and pi/2, which are straight up and down respectively. +* `get_look_horizontal()`: yaw in radians + * Angle is counter-clockwise from the +z direction. +* `set_look_vertical(radians)`: sets look pitch + * radians - Angle from looking forward, where positive is downwards. +* `set_look_horizontal(radians)`: sets look yaw + * radians - Angle from the +z direction, where positive is counter-clockwise. +* `get_look_pitch()`: pitch in radians - Deprecated as broken. Use get_look_vertical. + * Angle ranges between -pi/2 and pi/2, which are straight down and up respectively. +* `get_look_yaw()`: yaw in radians - Deprecated as broken. Use get_look_horizontal. + * Angle is counter-clockwise from the +x direction. +* `set_look_pitch(radians)`: sets look pitch - Deprecated. Use set_look_vertical. +* `set_look_yaw(radians)`: sets look yaw - Deprecated. Use set_look_horizontal. * `get_breath()`: returns players breath * `set_breath(value)`: sets players breath * values: diff --git a/src/player.h b/src/player.h index 6687ca86e..e6fcf388a 100644 --- a/src/player.h +++ b/src/player.h @@ -188,16 +188,28 @@ public: m_breath = breath; } - f32 getRadPitch() + // Deprecated + f32 getRadPitchDep() { return -1.0 * m_pitch * core::DEGTORAD; } - f32 getRadYaw() + // Deprecated + f32 getRadYawDep() { return (m_yaw + 90.) * core::DEGTORAD; } + f32 getRadPitch() + { + return m_pitch * core::DEGTORAD; + } + + f32 getRadYaw() + { + return m_yaw * core::DEGTORAD; + } + const char *getName() const { return m_name; diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 6d6614e7d..befae4253 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1016,27 +1016,61 @@ int ObjectRef::l_get_look_dir(lua_State *L) Player *player = getplayer(ref); if (player == NULL) return 0; // Do it - float pitch = player->getRadPitch(); - float yaw = player->getRadYaw(); + float pitch = player->getRadPitchDep(); + float yaw = player->getRadYawDep(); v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw)); push_v3f(L, v); return 1; } +// DEPRECATED // get_look_pitch(self) int ObjectRef::l_get_look_pitch(lua_State *L) { NO_MAP_LOCK_REQUIRED; + + log_deprecated(L, + "Deprecated call to get_look_pitch, use get_look_vertical instead"); + ObjectRef *ref = checkobject(L, 1); Player *player = getplayer(ref); if (player == NULL) return 0; // Do it - lua_pushnumber(L, player->getRadPitch()); + lua_pushnumber(L, player->getRadPitchDep()); return 1; } +// DEPRECATED // get_look_yaw(self) int ObjectRef::l_get_look_yaw(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + log_deprecated(L, + "Deprecated call to get_look_yaw, use get_look_horizontal instead"); + + ObjectRef *ref = checkobject(L, 1); + Player *player = getplayer(ref); + if (player == NULL) return 0; + // Do it + lua_pushnumber(L, player->getRadYawDep()); + return 1; +} + +// get_look_pitch2(self) +int ObjectRef::l_get_look_vertical(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + ObjectRef *ref = checkobject(L, 1); + Player *player = getplayer(ref); + if (player == NULL) return 0; + // Do it + lua_pushnumber(L, player->getRadPitch()); + return 1; +} + +// get_look_yaw2(self) +int ObjectRef::l_get_look_horizontal(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); @@ -1047,10 +1081,41 @@ int ObjectRef::l_get_look_yaw(lua_State *L) return 1; } +// set_look_vertical(self, radians) +int ObjectRef::l_set_look_vertical(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + ObjectRef *ref = checkobject(L, 1); + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; + float pitch = luaL_checknumber(L, 2) * core::RADTODEG; + // Do it + co->setPitch(pitch); + return 1; +} + +// set_look_horizontal(self, radians) +int ObjectRef::l_set_look_horizontal(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + ObjectRef *ref = checkobject(L, 1); + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; + float yaw = luaL_checknumber(L, 2) * core::RADTODEG; + // Do it + co->setYaw(yaw); + return 1; +} + +// DEPRECATED // set_look_pitch(self, radians) int ObjectRef::l_set_look_pitch(lua_State *L) { NO_MAP_LOCK_REQUIRED; + + log_deprecated(L, + "Deprecated call to set_look_pitch, use set_look_vertical instead."); + ObjectRef *ref = checkobject(L, 1); PlayerSAO* co = getplayersao(ref); if (co == NULL) return 0; @@ -1060,10 +1125,15 @@ int ObjectRef::l_set_look_pitch(lua_State *L) return 1; } +// DEPRECATED // set_look_yaw(self, radians) int ObjectRef::l_set_look_yaw(lua_State *L) { NO_MAP_LOCK_REQUIRED; + + log_deprecated(L, + "Deprecated call to set_look_yaw, use set_look_horizontal instead."); + ObjectRef *ref = checkobject(L, 1); PlayerSAO* co = getplayersao(ref); if (co == NULL) return 0; @@ -1754,6 +1824,10 @@ const luaL_reg ObjectRef::methods[] = { luamethod(ObjectRef, get_look_dir), luamethod(ObjectRef, get_look_pitch), luamethod(ObjectRef, get_look_yaw), + luamethod(ObjectRef, get_look_vertical), + luamethod(ObjectRef, get_look_horizontal), + luamethod(ObjectRef, set_look_horizontal), + luamethod(ObjectRef, set_look_vertical), luamethod(ObjectRef, set_look_yaw), luamethod(ObjectRef, set_look_pitch), luamethod(ObjectRef, get_breath), diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h index a4457cc05..a37d29535 100644 --- a/src/script/lua_api/l_object.h +++ b/src/script/lua_api/l_object.h @@ -189,15 +189,31 @@ private: // get_look_dir(self) static int l_get_look_dir(lua_State *L); + // DEPRECATED // get_look_pitch(self) static int l_get_look_pitch(lua_State *L); + // DEPRECATED // get_look_yaw(self) static int l_get_look_yaw(lua_State *L); + // get_look_pitch2(self) + static int l_get_look_vertical(lua_State *L); + + // get_look_yaw2(self) + static int l_get_look_horizontal(lua_State *L); + + // set_look_vertical(self, radians) + static int l_set_look_vertical(lua_State *L); + + // set_look_horizontal(self, radians) + static int l_set_look_horizontal(lua_State *L); + + // DEPRECATED // set_look_pitch(self, radians) static int l_set_look_pitch(lua_State *L); + // DEPRECATED // set_look_yaw(self, radians) static int l_set_look_yaw(lua_State *L); -- cgit v1.2.3 From 92705306bfb4994107a43514f29997cea15d48dc Mon Sep 17 00:00:00 2001 From: kwolekr Date: Tue, 14 Jun 2016 00:10:55 -0400 Subject: Mapgen: Refactor mapgen creation and management - Move mapgen creation logic out of EmergeManager and into Mapgen - Internally represent mapgen type as an enum value, instead of a string - Remove the need for a MapgenFactory per mapgen --- src/emerge.cpp | 68 +++-------------------- src/emerge.h | 3 - src/mapgen.cpp | 112 +++++++++++++++++++++++++++++++++++++- src/mapgen.h | 33 ++++++++--- src/mapgen_flat.h | 14 +---- src/mapgen_fractal.h | 14 +---- src/mapgen_singlenode.h | 17 ++---- src/mapgen_v5.h | 15 +---- src/mapgen_v6.h | 16 +----- src/mapgen_v7.h | 14 +---- src/mapgen_valleys.h | 14 +---- src/script/lua_api/l_mainmenu.cpp | 4 +- 12 files changed, 160 insertions(+), 164 deletions(-) (limited to 'src/script') diff --git a/src/emerge.cpp b/src/emerge.cpp index a2a10a177..48de6cb1a 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -34,13 +34,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "log.h" #include "map.h" #include "mapblock.h" -#include "mapgen_flat.h" -#include "mapgen_fractal.h" -#include "mapgen_v5.h" -#include "mapgen_v6.h" -#include "mapgen_v7.h" -#include "mapgen_valleys.h" -#include "mapgen_singlenode.h" #include "mg_biome.h" #include "mg_ore.h" #include "mg_decoration.h" @@ -53,13 +46,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "settings.h" #include "voxel.h" - -struct MapgenDesc { - const char *name; - MapgenFactory *factory; - bool is_user_visible; -}; - class EmergeThread : public Thread { public: bool enable_mapgen_debug_info; @@ -99,20 +85,6 @@ private: friend class EmergeManager; }; -//// -//// Built-in mapgens -//// - -MapgenDesc g_reg_mapgens[] = { - {"v5", new MapgenFactoryV5, true}, - {"v6", new MapgenFactoryV6, true}, - {"v7", new MapgenFactoryV7, true}, - {"flat", new MapgenFactoryFlat, true}, - {"fractal", new MapgenFactoryFractal, true}, - {"valleys", new MapgenFactoryValleys, true}, - {"singlenode", new MapgenFactorySinglenode, false}, -}; - //// //// EmergeManager //// @@ -195,24 +167,24 @@ void EmergeManager::initMapgens() if (m_mapgens.size()) return; - MapgenFactory *mgfactory = getMapgenFactory(params.mg_name); - if (!mgfactory) { + MapgenType mgtype = Mapgen::getMapgenType(params.mg_name); + if (mgtype == MAPGEN_INVALID) { + const char *default_mapgen_name = Mapgen::getMapgenName(MAPGEN_DEFAULT); errorstream << "EmergeManager: mapgen " << params.mg_name << - " not registered; falling back to " << DEFAULT_MAPGEN << std::endl; - - params.mg_name = DEFAULT_MAPGEN; + " not registered; falling back to " << + default_mapgen_name << std::endl; - mgfactory = getMapgenFactory(params.mg_name); - FATAL_ERROR_IF(mgfactory == NULL, "Couldn't use any mapgen!"); + params.mg_name = default_mapgen_name; + mgtype = MAPGEN_DEFAULT; } if (!params.sparams) { - params.sparams = mgfactory->createMapgenParams(); + params.sparams = Mapgen::createMapgenParams(mgtype); params.sparams->readParams(g_settings); } for (u32 i = 0; i != m_threads.size(); i++) { - Mapgen *mg = mgfactory->createMapgen(i, ¶ms, this); + Mapgen *mg = Mapgen::createMapgen(mgtype, i, ¶ms, this); m_mapgens.push_back(mg); } } @@ -369,28 +341,6 @@ bool EmergeManager::isBlockUnderground(v3s16 blockpos) return blockpos.Y * (MAP_BLOCKSIZE + 1) <= params.water_level; } - -void EmergeManager::getMapgenNames( - std::vector *mgnames, bool include_hidden) -{ - for (u32 i = 0; i != ARRLEN(g_reg_mapgens); i++) { - if (include_hidden || g_reg_mapgens[i].is_user_visible) - mgnames->push_back(g_reg_mapgens[i].name); - } -} - - -MapgenFactory *EmergeManager::getMapgenFactory(const std::string &mgname) -{ - for (u32 i = 0; i != ARRLEN(g_reg_mapgens); i++) { - if (mgname == g_reg_mapgens[i].name) - return g_reg_mapgens[i].factory; - } - - return NULL; -} - - bool EmergeManager::pushBlockEmergeData( v3s16 pos, u16 peer_requested, diff --git a/src/emerge.h b/src/emerge.h index 825ac1c0f..303a35529 100644 --- a/src/emerge.h +++ b/src/emerge.h @@ -140,9 +140,6 @@ public: int getGroundLevelAtPoint(v2s16 p); bool isBlockUnderground(v3s16 blockpos); - static MapgenFactory *getMapgenFactory(const std::string &mgname); - static void getMapgenNames( - std::vector *mgnames, bool include_hidden); static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize); private: diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 1e0e3aa44..e45233b33 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -39,6 +39,13 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" #include "filesys.h" #include "log.h" +#include "mapgen_flat.h" +#include "mapgen_fractal.h" +#include "mapgen_v5.h" +#include "mapgen_v6.h" +#include "mapgen_v7.h" +#include "mapgen_valleys.h" +#include "mapgen_singlenode.h" #include "cavegen.h" #include "dungeongen.h" @@ -63,6 +70,28 @@ FlagDesc flagdesc_gennotify[] = { {NULL, 0} }; +struct MapgenDesc { + const char *name; + bool is_user_visible; +}; + +//// +//// Built-in mapgens +//// + +static MapgenDesc g_reg_mapgens[] = { + {"v5", true}, + {"v6", true}, + {"v7", true}, + {"flat", true}, + {"fractal", true}, + {"valleys", true}, + {"singlenode", false}, +}; + +STATIC_ASSERT( + ARRLEN(g_reg_mapgens) == MAPGEN_INVALID, + registered_mapgens_is_wrong_size); //// //// Mapgen @@ -121,6 +150,83 @@ Mapgen::~Mapgen() } +MapgenType Mapgen::getMapgenType(const std::string &mgname) +{ + for (size_t i = 0; i != ARRLEN(g_reg_mapgens); i++) { + if (mgname == g_reg_mapgens[i].name) + return (MapgenType)i; + } + + return MAPGEN_INVALID; +} + + +const char *Mapgen::getMapgenName(MapgenType mgtype) +{ + size_t index = (size_t)mgtype; + if (index == MAPGEN_INVALID || index >= ARRLEN(g_reg_mapgens)) + return "invalid"; + + return g_reg_mapgens[index].name; +} + + +Mapgen *Mapgen::createMapgen(MapgenType mgtype, int mgid, + MapgenParams *params, EmergeManager *emerge) +{ + switch (mgtype) { + case MAPGEN_FLAT: + return new MapgenFlat(mgid, params, emerge); + case MAPGEN_FRACTAL: + return new MapgenFractal(mgid, params, emerge); + case MAPGEN_SINGLENODE: + return new MapgenSinglenode(mgid, params, emerge); + case MAPGEN_V5: + return new MapgenV5(mgid, params, emerge); + case MAPGEN_V6: + return new MapgenV6(mgid, params, emerge); + case MAPGEN_V7: + return new MapgenV7(mgid, params, emerge); + case MAPGEN_VALLEYS: + return new MapgenValleys(mgid, params, emerge); + default: + return NULL; + } +} + + +MapgenSpecificParams *Mapgen::createMapgenParams(MapgenType mgtype) +{ + switch (mgtype) { + case MAPGEN_FLAT: + return new MapgenFlatParams; + case MAPGEN_FRACTAL: + return new MapgenFractalParams; + case MAPGEN_SINGLENODE: + return new MapgenSinglenodeParams; + case MAPGEN_V5: + return new MapgenV5Params; + case MAPGEN_V6: + return new MapgenV6Params; + case MAPGEN_V7: + return new MapgenV7Params; + case MAPGEN_VALLEYS: + return new MapgenValleysParams; + default: + return NULL; + } +} + + +void Mapgen::getMapgenNames(std::vector *mgnames, bool include_hidden) +{ + for (u32 i = 0; i != ARRLEN(g_reg_mapgens); i++) { + if (include_hidden || g_reg_mapgens[i].is_user_visible) + mgnames->push_back(g_reg_mapgens[i].name); + } +} + + u32 Mapgen::getBlockSeed(v3s16 p, s32 seed) { return (u32)seed + @@ -891,9 +997,9 @@ void MapgenParams::load(const Settings &settings) } delete sparams; - MapgenFactory *mgfactory = EmergeManager::getMapgenFactory(mg_name); - if (mgfactory) { - sparams = mgfactory->createMapgenParams(); + MapgenType mgtype = Mapgen::getMapgenType(mg_name); + if (mgtype != MAPGEN_INVALID) { + sparams = Mapgen::createMapgenParams(mgtype); sparams->readParams(&settings); } } diff --git a/src/mapgen.h b/src/mapgen.h index 90ac84bd8..618a2f6b8 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -26,7 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/string.h" #include "util/container.h" -#define DEFAULT_MAPGEN "v6" +#define MAPGEN_DEFAULT MAPGEN_V6 +#define MAPGEN_DEFAULT_NAME "v6" /////////////////// Mapgen flags #define MG_TREES 0x01 @@ -107,6 +108,17 @@ private: std::list m_notify_events; }; +enum MapgenType { + MAPGEN_V5, + MAPGEN_V6, + MAPGEN_V7, + MAPGEN_FLAT, + MAPGEN_FRACTAL, + MAPGEN_VALLEYS, + MAPGEN_SINGLENODE, + MAPGEN_INVALID, +}; + struct MapgenSpecificParams { virtual void readParams(const Settings *settings) = 0; virtual void writeParams(Settings *settings) const = 0; @@ -124,7 +136,7 @@ struct MapgenParams { MapgenSpecificParams *sparams; MapgenParams() : - mg_name(DEFAULT_MAPGEN), + mg_name(MAPGEN_DEFAULT_NAME), chunksize(5), seed(0), water_level(1), @@ -173,6 +185,8 @@ public: Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge); virtual ~Mapgen(); + virtual MapgenType getType() const { return MAPGEN_INVALID; } + static u32 getBlockSeed(v3s16 p, s32 seed); static u32 getBlockSeed2(v3s16 p, s32 seed); s16 findGroundLevelFull(v2s16 p2d); @@ -198,6 +212,14 @@ public: // signify this and to cause Server::findSpawnPos() to try another (X, Z). virtual int getSpawnLevelAtPoint(v2s16 p) { return 0; } + // Mapgen management functions + static MapgenType getMapgenType(const std::string &mgname); + static const char *getMapgenName(MapgenType mgtype); + static Mapgen *createMapgen(MapgenType mgtype, int mgid, + MapgenParams *params, EmergeManager *emerge); + static MapgenSpecificParams *createMapgenParams(MapgenType mgtype); + static void getMapgenNames(std::vector *mgnames, bool include_hidden); + private: // isLiquidHorizontallyFlowable() is a helper function for updateLiquid() // that checks whether there are floodable nodes without liquid beneath @@ -267,11 +289,4 @@ protected: float cave_width; }; -struct MapgenFactory { - virtual Mapgen *createMapgen(int mgid, MapgenParams *params, - EmergeManager *emerge) = 0; - virtual MapgenSpecificParams *createMapgenParams() = 0; - virtual ~MapgenFactory() {} -}; - #endif diff --git a/src/mapgen_flat.h b/src/mapgen_flat.h index afe67b04e..39b6dc302 100644 --- a/src/mapgen_flat.h +++ b/src/mapgen_flat.h @@ -58,6 +58,8 @@ public: MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenFlat(); + virtual MapgenType getType() const { return MAPGEN_FLAT; } + virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); s16 generateTerrain(); @@ -72,16 +74,4 @@ private: Noise *noise_terrain; }; -struct MapgenFactoryFlat : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) - { - return new MapgenFlat(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() - { - return new MapgenFlatParams(); - }; -}; - #endif diff --git a/src/mapgen_fractal.h b/src/mapgen_fractal.h index 065142b84..cbd5567c4 100644 --- a/src/mapgen_fractal.h +++ b/src/mapgen_fractal.h @@ -62,6 +62,8 @@ public: MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenFractal(); + virtual MapgenType getType() const { return MAPGEN_FRACTAL; } + virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); bool getFractalAtPoint(s16 x, s16 y, s16 z); @@ -83,16 +85,4 @@ private: Noise *noise_seabed; }; -struct MapgenFactoryFractal : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) - { - return new MapgenFractal(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() - { - return new MapgenFractalParams(); - }; -}; - #endif diff --git a/src/mapgen_singlenode.h b/src/mapgen_singlenode.h index 2c6496c00..58672a0ed 100644 --- a/src/mapgen_singlenode.h +++ b/src/mapgen_singlenode.h @@ -23,10 +23,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapgen.h" struct MapgenSinglenodeParams : public MapgenSpecificParams { - MapgenSinglenodeParams() {} ~MapgenSinglenodeParams() {} - + void readParams(const Settings *settings) {} void writeParams(Settings *settings) const {} }; @@ -39,19 +38,11 @@ public: MapgenSinglenode(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenSinglenode(); - + + virtual MapgenType getType() const { return MAPGEN_SINGLENODE; } + void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); }; -struct MapgenFactorySinglenode : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) { - return new MapgenSinglenode(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() { - return new MapgenSinglenodeParams(); - }; -}; - #endif diff --git a/src/mapgen_v5.h b/src/mapgen_v5.h index 4e1772a64..5f6b10383 100644 --- a/src/mapgen_v5.h +++ b/src/mapgen_v5.h @@ -53,6 +53,8 @@ public: MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenV5(); + virtual MapgenType getType() const { return MAPGEN_V5; } + virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); int generateBaseTerrain(); @@ -63,17 +65,4 @@ private: Noise *noise_ground; }; - -struct MapgenFactoryV5 : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) - { - return new MapgenV5(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() - { - return new MapgenV5Params(); - }; -}; - #endif diff --git a/src/mapgen_v6.h b/src/mapgen_v6.h index a55fc6d53..20b0bf92e 100644 --- a/src/mapgen_v6.h +++ b/src/mapgen_v6.h @@ -127,6 +127,8 @@ public: MapgenV6(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenV6(); + virtual MapgenType getType() const { return MAPGEN_V6; } + void makeChunk(BlockMakeData *data); int getGroundLevelAtPoint(v2s16 p); int getSpawnLevelAtPoint(v2s16 p); @@ -162,18 +164,4 @@ public: virtual void generateCaves(int max_stone_y); }; - -struct MapgenFactoryV6 : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) - { - return new MapgenV6(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() - { - return new MapgenV6Params(); - }; -}; - - #endif diff --git a/src/mapgen_v7.h b/src/mapgen_v7.h index 9ed89a511..c75f18a93 100644 --- a/src/mapgen_v7.h +++ b/src/mapgen_v7.h @@ -59,6 +59,8 @@ public: MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenV7(); + virtual MapgenType getType() const { return MAPGEN_V7; } + virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); @@ -80,16 +82,4 @@ private: Noise *noise_ridge; }; -struct MapgenFactoryV7 : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) - { - return new MapgenV7(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() - { - return new MapgenV7Params(); - }; -}; - #endif diff --git a/src/mapgen_valleys.h b/src/mapgen_valleys.h index faaffa905..00f6aff07 100644 --- a/src/mapgen_valleys.h +++ b/src/mapgen_valleys.h @@ -91,6 +91,8 @@ public: MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenValleys(); + virtual MapgenType getType() const { return MAPGEN_VALLEYS; } + virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); @@ -137,16 +139,4 @@ private: virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth); }; -struct MapgenFactoryValleys : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) - { - return new MapgenValleys(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() - { - return new MapgenValleysParams(); - }; -}; - #endif diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 7b29db159..8b078eafd 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -31,7 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "convert_json.h" #include "serverlist.h" -#include "emerge.h" +#include "mapgen.h" #include "sound.h" #include "settings.h" #include "log.h" @@ -707,7 +707,7 @@ int ModApiMainMenu::l_set_topleft_text(lua_State *L) int ModApiMainMenu::l_get_mapgen_names(lua_State *L) { std::vector names; - EmergeManager::getMapgenNames(&names, lua_toboolean(L, 1)); + Mapgen::getMapgenNames(&names, lua_toboolean(L, 1)); lua_newtable(L); for (size_t i = 0; i != names.size(); i++) { -- cgit v1.2.3 From 3c63c3044d5e4ca36c2649c530f31622581d90fd Mon Sep 17 00:00:00 2001 From: kwolekr Date: Fri, 24 Jun 2016 18:15:56 -0400 Subject: Add MapSettingsManager and new mapgen setting script API functions This commit refactors the majority of the Mapgen settings system. - MapgenParams is now owned by MapSettingsManager, itself a part of ServerMap, instead of the EmergeManager. - New Script API functions added: core.get_mapgen_setting core.get_mapgen_setting_noiseparams, core.set_mapgen_setting, and core.set_mapgen_setting_noiseparams. - minetest.get/set_mapgen_params are deprecated by the above new functions. - It is now possible to view and modify any arbitrary mapgen setting from a mod, rather than the base MapgenParams structure. - MapgenSpecificParams has been removed. --- build/android/jni/Android.mk | 2 + doc/lua_api.txt | 19 +++ src/CMakeLists.txt | 1 + src/emerge.cpp | 40 ++--- src/emerge.h | 15 +- src/map.cpp | 100 +++-------- src/map.h | 10 +- src/map_settings_manager.cpp | 194 ++++++++++++++++++++++ src/map_settings_manager.h | 79 +++++++++ src/mapgen.cpp | 68 ++++---- src/mapgen.h | 20 +-- src/mapgen_flat.cpp | 28 ++-- src/mapgen_flat.h | 4 +- src/mapgen_fractal.cpp | 34 ++-- src/mapgen_fractal.h | 4 +- src/mapgen_singlenode.h | 2 +- src/mapgen_v5.cpp | 20 +-- src/mapgen_v5.h | 4 +- src/mapgen_v6.cpp | 33 ++-- src/mapgen_v6.h | 4 +- src/mapgen_v7.cpp | 30 ++-- src/mapgen_v7.h | 4 +- src/mapgen_valleys.cpp | 41 +++-- src/mapgen_valleys.h | 4 +- src/script/lua_api/l_mapgen.cpp | 154 +++++++++++++---- src/script/lua_api/l_mapgen.h | 12 ++ src/script/lua_api/l_vmanip.cpp | 2 +- src/server.cpp | 15 +- src/subgame.cpp | 4 +- src/unittest/CMakeLists.txt | 1 + src/unittest/test_map_settings_manager.cpp | 255 +++++++++++++++++++++++++++++ 31 files changed, 889 insertions(+), 314 deletions(-) create mode 100644 src/map_settings_manager.cpp create mode 100644 src/map_settings_manager.h create mode 100644 src/unittest/test_map_settings_manager.cpp (limited to 'src/script') diff --git a/build/android/jni/Android.mk b/build/android/jni/Android.mk index afd8c76b2..a42ab76b8 100644 --- a/build/android/jni/Android.mk +++ b/build/android/jni/Android.mk @@ -169,6 +169,7 @@ LOCAL_SRC_FILES := \ jni/src/log.cpp \ jni/src/main.cpp \ jni/src/map.cpp \ + jni/src/map_settings_manager.cpp \ jni/src/mapblock.cpp \ jni/src/mapblock_mesh.cpp \ jni/src/mapgen.cpp \ @@ -238,6 +239,7 @@ LOCAL_SRC_FILES := \ jni/src/unittest/test_connection.cpp \ jni/src/unittest/test_filepath.cpp \ jni/src/unittest/test_inventory.cpp \ + jni/src/unittest/test_map_settings_manager.cpp \ jni/src/unittest/test_mapnode.cpp \ jni/src/unittest/test_nodedef.cpp \ jni/src/unittest/test_noderesolver.cpp \ diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 6d359379e..297566068 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2089,7 +2089,9 @@ and `minetest.auth_reload` call the authetification handler. given biome_name string. * `minetest.get_mapgen_params()` Returns mapgen parameters, a table containing `mgname`, `seed`, `chunksize`, `water_level`, and `flags`. + * Deprecated: use minetest.get_mapgen_setting(name) instead * `minetest.set_mapgen_params(MapgenParams)` + * Deprecated: use minetest.set_mapgen_setting(name, value, override) instead * Set map generation parameters * Function cannot be called after the registration period; only initialization and `on_mapgen_init` @@ -2099,6 +2101,23 @@ and `minetest.auth_reload` call the authetification handler. * `flags` contains a comma-delimited string of flags to set, or if the prefix `"no"` is attached, clears instead. * `flags` is in the same format and has the same options as `mg_flags` in `minetest.conf` +* `minetest.get_mapgen_setting(name)` + * Gets the *active* mapgen setting (or nil if none exists) in string format with the following + order of precedence: + 1) Settings loaded from map_meta.txt or overrides set during mod execution + 2) Settings set by mods without a metafile override + 3) Settings explicitly set in the user config file, minetest.conf + 4) Settings set as the user config default +* `minetest.get_mapgen_setting_noiseparams(name)` + * Same as above, but returns the value as a NoiseParams table if the setting `name` exists + and is a valid NoiseParams +* `minetest.set_mapgen_setting(name, value, [override_meta=false])` + * Sets a mapgen param to `value`, and will take effect if the corresponding mapgen setting + is not already present in map_meta.txt. If the optional boolean override_meta is set to true, + this setting will become the active setting regardless of the map metafile contents. + * Note: to set the seed, use "seed", not "fixed_map_seed" +* `minetest.set_mapgen_setting_noiseparams(name, value, [override_meta=false])` + * Same as above, except value is a NoiseParams table * `minetest.set_noiseparams(name, noiseparams, set_default)` * Sets the noiseparams setting of `name` to the noiseparams table specified in `noiseparams`. * `set_default` is an optional boolean (default: `true`) that specifies whether the setting diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bffc1f7eb..bcd7a5984 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -414,6 +414,7 @@ set(common_SRCS light.cpp log.cpp map.cpp + map_settings_manager.cpp mapblock.cpp mapgen.cpp mapgen_flat.cpp diff --git a/src/emerge.cpp b/src/emerge.cpp index 48de6cb1a..daf42f5e2 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -156,37 +156,19 @@ EmergeManager::~EmergeManager() } -void EmergeManager::loadMapgenParams() -{ - params.load(*g_settings); -} - - -void EmergeManager::initMapgens() +bool EmergeManager::initMapgens(MapgenParams *params) { if (m_mapgens.size()) - return; - - MapgenType mgtype = Mapgen::getMapgenType(params.mg_name); - if (mgtype == MAPGEN_INVALID) { - const char *default_mapgen_name = Mapgen::getMapgenName(MAPGEN_DEFAULT); - errorstream << "EmergeManager: mapgen " << params.mg_name << - " not registered; falling back to " << - default_mapgen_name << std::endl; - - params.mg_name = default_mapgen_name; - mgtype = MAPGEN_DEFAULT; - } + return false; - if (!params.sparams) { - params.sparams = Mapgen::createMapgenParams(mgtype); - params.sparams->readParams(g_settings); - } + this->mgparams = params; for (u32 i = 0; i != m_threads.size(); i++) { - Mapgen *mg = Mapgen::createMapgen(mgtype, i, ¶ms, this); + Mapgen *mg = Mapgen::createMapgen(params->mgtype, i, params, this); m_mapgens.push_back(mg); } + + return true; } @@ -288,12 +270,14 @@ bool EmergeManager::enqueueBlockEmergeEx( // Mapgen-related helper functions // + +// TODO(hmmmm): Move this to ServerMap v3s16 EmergeManager::getContainingChunk(v3s16 blockpos) { - return getContainingChunk(blockpos, params.chunksize); + return getContainingChunk(blockpos, mgparams->chunksize); } - +// TODO(hmmmm): Move this to ServerMap v3s16 EmergeManager::getContainingChunk(v3s16 blockpos, s16 chunksize) { s16 coff = -chunksize / 2; @@ -327,7 +311,7 @@ int EmergeManager::getGroundLevelAtPoint(v2s16 p) return m_mapgens[0]->getGroundLevelAtPoint(p); } - +// TODO(hmmmm): Move this to ServerMap bool EmergeManager::isBlockUnderground(v3s16 blockpos) { #if 0 @@ -338,7 +322,7 @@ bool EmergeManager::isBlockUnderground(v3s16 blockpos) #endif // Use a simple heuristic; the above method is wildly inaccurate anyway. - return blockpos.Y * (MAP_BLOCKSIZE + 1) <= params.water_level; + return blockpos.Y * (MAP_BLOCKSIZE + 1) <= mgparams->water_level; } bool EmergeManager::pushBlockEmergeData( diff --git a/src/emerge.h b/src/emerge.h index 303a35529..cf0677145 100644 --- a/src/emerge.h +++ b/src/emerge.h @@ -97,8 +97,16 @@ public: u32 gen_notify_on; std::set gen_notify_on_deco_ids; - // Map generation parameters - MapgenParams params; + // Parameters passed to mapgens owned by ServerMap + // TODO(hmmmm): Remove this after mapgen helper methods using them + // are moved to ServerMap + MapgenParams *mgparams; + + // Hackish workaround: + // For now, EmergeManager must hold onto a ptr to the Map's setting manager + // since the Map can only be accessed through the Environment, and the + // Environment is not created until after script initialization. + MapSettingsManager *map_settings_mgr; // Managers of various map generation-related components BiomeManager *biomemgr; @@ -110,8 +118,7 @@ public: EmergeManager(IGameDef *gamedef); ~EmergeManager(); - void loadMapgenParams(); - void initMapgens(); + bool initMapgens(MapgenParams *mgparams); void startThreads(); void stopThreads(); diff --git a/src/map.cpp b/src/map.cpp index a1f2086ce..38f0fa37c 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -2130,11 +2130,15 @@ void Map::removeNodeTimer(v3s16 p) */ ServerMap::ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emerge): Map(dout_server, gamedef), + settings_mgr(g_settings, savedir + DIR_DELIM + "map_meta.txt"), m_emerge(emerge), m_map_metadata_changed(true) { verbosestream<map_settings_mgr = &settings_mgr; + /* Try to load map; if not found, create a new one. */ @@ -2170,26 +2174,15 @@ ServerMap::ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emer } else { - try{ - // Load map metadata (seed, chunksize) - loadMapMeta(); - } - catch(SettingNotFoundException &e){ - infostream<<"ServerMap: Some metadata not found." - <<" Using default settings."<params.seed; + return getMapgenParams()->seed; } s16 ServerMap::getWaterLevel() { - return m_emerge->params.water_level; + return getMapgenParams()->water_level; } bool ServerMap::initBlockMake(v3s16 blockpos, BlockMakeData *data) { - s16 csize = m_emerge->params.chunksize; + s16 csize = getMapgenParams()->chunksize; v3s16 bpmin = EmergeManager::getContainingChunk(blockpos, csize); v3s16 bpmax = bpmin + v3s16(1, 1, 1) * (csize - 1); @@ -2287,7 +2287,7 @@ bool ServerMap::initBlockMake(v3s16 blockpos, BlockMakeData *data) blockpos_over_limit(full_bpmax)) return false; - data->seed = m_emerge->params.seed; + data->seed = getSeed(); data->blockpos_min = bpmin; data->blockpos_max = bpmax; data->blockpos_requested = blockpos; @@ -2905,8 +2905,9 @@ void ServerMap::save(ModifiedState save_level) infostream<<"ServerMap: Saving whole map, this can take time." < &dst) } } -void ServerMap::saveMapMeta() -{ - DSTACK(FUNCTION_NAME); - - createDirs(m_savedir); - - std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt"; - std::ostringstream oss(std::ios_base::binary); - Settings conf; - - m_emerge->params.save(conf); - conf.writeLines(oss); - - oss << "[end_of_params]\n"; - - if(!fs::safeWriteToFile(fullpath, oss.str())) { - errorstream << "ServerMap::saveMapMeta(): " - << "could not write " << fullpath << std::endl; - throw FileNotGoodException("Cannot save chunk metadata"); - } - - m_map_metadata_changed = false; -} - -void ServerMap::loadMapMeta() -{ - DSTACK(FUNCTION_NAME); - - Settings conf; - std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt"; - - std::ifstream is(fullpath.c_str(), std::ios_base::binary); - if (!is.good()) { - errorstream << "ServerMap::loadMapMeta(): " - "could not open " << fullpath << std::endl; - throw FileNotGoodException("Cannot open map metadata"); - } - - if (!conf.parseConfigLines(is, "[end_of_params]")) { - throw SerializationError("ServerMap::loadMapMeta(): " - "[end_of_params] not found!"); - } - - m_emerge->params.load(conf); - - verbosestream << "ServerMap::loadMapMeta(): seed=" - << m_emerge->params.seed << std::endl; -} - void ServerMap::saveSectorMeta(ServerMapSector *sector) { DSTACK(FUNCTION_NAME); diff --git a/src/map.h b/src/map.h index 23da56471..13775fde1 100644 --- a/src/map.h +++ b/src/map.h @@ -33,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "modifiedstate.h" #include "util/container.h" #include "nodetimer.h" +#include "map_settings_manager.h" class Settings; class Database; @@ -46,8 +47,6 @@ class IRollbackManager; class EmergeManager; class ServerEnvironment; struct BlockMakeData; -struct MapgenParams; - /* MapEditEvent @@ -463,9 +462,8 @@ public: void save(ModifiedState save_level); void listAllLoadableBlocks(std::vector &dst); void listAllLoadedBlocks(std::vector &dst); - // Saves map seed and possibly other stuff - void saveMapMeta(); - void loadMapMeta(); + + MapgenParams *getMapgenParams(); /*void saveChunkMeta(); void loadChunkMeta();*/ @@ -506,6 +504,8 @@ public: u64 getSeed(); s16 getWaterLevel(); + MapSettingsManager settings_mgr; + private: // Emerge manager EmergeManager *m_emerge; diff --git a/src/map_settings_manager.cpp b/src/map_settings_manager.cpp new file mode 100644 index 000000000..53d17125c --- /dev/null +++ b/src/map_settings_manager.cpp @@ -0,0 +1,194 @@ +/* +Minetest +Copyright (C) 2010-2013 kwolekr, Ryan Kwolek + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program 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 Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "debug.h" +#include "filesys.h" +#include "log.h" +#include "mapgen.h" +#include "settings.h" + +#include "map_settings_manager.h" + +MapSettingsManager::MapSettingsManager( + Settings *user_settings, const std::string &map_meta_path) +{ + m_map_meta_path = map_meta_path; + m_user_settings = user_settings; + m_map_settings = new Settings; + mapgen_params = NULL; + + assert(m_user_settings != NULL); +} + + +MapSettingsManager::~MapSettingsManager() +{ + delete m_map_settings; + delete mapgen_params; +} + + +bool MapSettingsManager::getMapSetting( + const std::string &name, std::string *value_out) +{ + if (m_map_settings->getNoEx(name, *value_out)) + return true; + + // Compatibility kludge + if (m_user_settings == g_settings && name == "seed") + return m_user_settings->getNoEx("fixed_map_seed", *value_out); + + return m_user_settings->getNoEx(name, *value_out); +} + + +bool MapSettingsManager::getMapSettingNoiseParams( + const std::string &name, NoiseParams *value_out) +{ + return m_map_settings->getNoiseParams(name, *value_out) || + m_user_settings->getNoiseParams(name, *value_out); +} + + +bool MapSettingsManager::setMapSetting( + const std::string &name, const std::string &value, bool override_meta) +{ + if (mapgen_params) + return false; + + if (override_meta) + m_map_settings->set(name, value); + else + m_map_settings->setDefault(name, value); + + return true; +} + + +bool MapSettingsManager::setMapSettingNoiseParams( + const std::string &name, const NoiseParams *value, bool override_meta) +{ + if (mapgen_params) + return false; + + m_map_settings->setNoiseParams(name, *value, !override_meta); + return true; +} + + +bool MapSettingsManager::loadMapMeta() +{ + std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary); + + if (!is.good()) { + errorstream << "loadMapMeta: could not open " + << m_map_meta_path << std::endl; + return false; + } + + if (!m_map_settings->parseConfigLines(is, "[end_of_params]")) { + errorstream << "loadMapMeta: [end_of_params] not found!" << std::endl; + return false; + } + + return true; +} + + +bool MapSettingsManager::saveMapMeta() +{ + // If mapgen params haven't been created yet; abort + if (!mapgen_params) + return false; + + if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) { + errorstream << "saveMapMeta: could not create dirs to " + << m_map_meta_path; + return false; + } + + std::ostringstream oss(std::ios_base::binary); + Settings conf; + + mapgen_params->MapgenParams::writeParams(&conf); + mapgen_params->writeParams(&conf); + conf.writeLines(oss); + + // NOTE: If there are ever types of map settings other than + // those relating to map generation, save them here + + oss << "[end_of_params]\n"; + + if (!fs::safeWriteToFile(m_map_meta_path, oss.str())) { + errorstream << "saveMapMeta: could not write " + << m_map_meta_path << std::endl; + return false; + } + + return true; +} + + +MapgenParams *MapSettingsManager::makeMapgenParams() +{ + if (mapgen_params) + return mapgen_params; + + assert(m_user_settings != NULL); + assert(m_map_settings != NULL); + + // At this point, we have (in order of precedence): + // 1). m_mapgen_settings->m_settings containing map_meta.txt settings or + // explicit overrides from scripts + // 2). m_mapgen_settings->m_defaults containing script-set mgparams without + // overrides + // 3). g_settings->m_settings containing all user-specified config file + // settings + // 4). g_settings->m_defaults containing any low-priority settings from + // scripts, e.g. mods using Lua as an enhanced config file) + + // Now, get the mapgen type so we can create the appropriate MapgenParams + std::string mg_name; + MapgenType mgtype = getMapSetting("mg_name", &mg_name) ? + Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT; + if (mgtype == MAPGEN_INVALID) { + errorstream << "EmergeManager: mapgen '" << mg_name << + "' not valid; falling back to " << + Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl; + mgtype = MAPGEN_DEFAULT; + } + + // Create our MapgenParams + MapgenParams *params = Mapgen::createMapgenParams(mgtype); + if (params == NULL) + return NULL; + + params->mgtype = mgtype; + + // Load the rest of the mapgen params from our active settings + params->MapgenParams::readParams(m_user_settings); + params->MapgenParams::readParams(m_map_settings); + params->readParams(m_user_settings); + params->readParams(m_map_settings); + + // Hold onto our params + mapgen_params = params; + + return params; +} diff --git a/src/map_settings_manager.h b/src/map_settings_manager.h new file mode 100644 index 000000000..9f766f1f0 --- /dev/null +++ b/src/map_settings_manager.h @@ -0,0 +1,79 @@ +/* +Minetest +Copyright (C) 2010-2013 kwolekr, Ryan Kwolek + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program 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 Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef MAP_SETTINGS_MANAGER_HEADER +#define MAP_SETTINGS_MANAGER_HEADER + +#include + +class Settings; +struct NoiseParams; +struct MapgenParams; + +/* + MapSettingsManager is a centralized object for management (creating, + loading, storing, saving, etc.) of config settings related to the Map. + + It has two phases: the initial r/w "gather and modify settings" state, and + the final r/o "read and save settings" state. + + The typical use case is, in order, as follows: + - Create a MapSettingsManager object + - Try to load map metadata into it from the metadata file + - Manually view and modify the current configuration as desired through a + Settings-like interface + - When all modifications are finished, create a 'Parameters' object + containing the finalized, active parameters. This could be passed along + to whichever Map-related objects that may require it. + - Save these active settings to the metadata file when requested +*/ +class MapSettingsManager { +public: + // Finalized map generation parameters + MapgenParams *mapgen_params; + + MapSettingsManager(Settings *user_settings, + const std::string &map_meta_path); + ~MapSettingsManager(); + + bool getMapSetting(const std::string &name, std::string *value_out); + + bool getMapSettingNoiseParams( + const std::string &name, NoiseParams *value_out); + + // Note: Map config becomes read-only after makeMapgenParams() gets called + // (i.e. mapgen_params is non-NULL). Attempts to set map config after + // params have been finalized will result in failure. + bool setMapSetting(const std::string &name, + const std::string &value, bool override_meta = false); + + bool setMapSettingNoiseParams(const std::string &name, + const NoiseParams *value, bool override_meta = false); + + bool loadMapMeta(); + bool saveMapMeta(); + MapgenParams *makeMapgenParams(); + +private: + std::string m_map_meta_path; + Settings *m_map_settings; + Settings *m_user_settings; +}; + +#endif diff --git a/src/mapgen.cpp b/src/mapgen.cpp index e45233b33..b6fda91ac 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -176,26 +176,26 @@ Mapgen *Mapgen::createMapgen(MapgenType mgtype, int mgid, { switch (mgtype) { case MAPGEN_FLAT: - return new MapgenFlat(mgid, params, emerge); + return new MapgenFlat(mgid, (MapgenFlatParams *)params, emerge); case MAPGEN_FRACTAL: - return new MapgenFractal(mgid, params, emerge); + return new MapgenFractal(mgid, (MapgenFractalParams *)params, emerge); case MAPGEN_SINGLENODE: - return new MapgenSinglenode(mgid, params, emerge); + return new MapgenSinglenode(mgid, (MapgenSinglenodeParams *)params, emerge); case MAPGEN_V5: - return new MapgenV5(mgid, params, emerge); + return new MapgenV5(mgid, (MapgenV5Params *)params, emerge); case MAPGEN_V6: - return new MapgenV6(mgid, params, emerge); + return new MapgenV6(mgid, (MapgenV6Params *)params, emerge); case MAPGEN_V7: - return new MapgenV7(mgid, params, emerge); + return new MapgenV7(mgid, (MapgenV7Params *)params, emerge); case MAPGEN_VALLEYS: - return new MapgenValleys(mgid, params, emerge); + return new MapgenValleys(mgid, (MapgenValleysParams *)params, emerge); default: return NULL; } } -MapgenSpecificParams *Mapgen::createMapgenParams(MapgenType mgtype) +MapgenParams *Mapgen::createMapgenParams(MapgenType mgtype) { switch (mgtype) { case MAPGEN_FLAT: @@ -970,52 +970,46 @@ void GenerateNotifier::getEvents( MapgenParams::~MapgenParams() { delete bparams; - delete sparams; } -void MapgenParams::load(const Settings &settings) +void MapgenParams::readParams(const Settings *settings) { std::string seed_str; - const char *seed_name = (&settings == g_settings) ? "fixed_map_seed" : "seed"; + const char *seed_name = (settings == g_settings) ? "fixed_map_seed" : "seed"; - if (settings.getNoEx(seed_name, seed_str) && !seed_str.empty()) - seed = read_seed(seed_str.c_str()); - else - myrand_bytes(&seed, sizeof(seed)); + if (settings->getNoEx(seed_name, seed_str)) { + if (!seed_str.empty()) + seed = read_seed(seed_str.c_str()); + else + myrand_bytes(&seed, sizeof(seed)); + } + + std::string mg_name; + if (settings->getNoEx("mg_name", mg_name)) + this->mgtype = Mapgen::getMapgenType(mg_name); - settings.getNoEx("mg_name", mg_name); - settings.getS16NoEx("water_level", water_level); - settings.getS16NoEx("chunksize", chunksize); - settings.getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen); + settings->getS16NoEx("water_level", water_level); + settings->getS16NoEx("chunksize", chunksize); + settings->getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen); delete bparams; bparams = BiomeManager::createBiomeParams(BIOMEGEN_ORIGINAL); if (bparams) { - bparams->readParams(&settings); + bparams->readParams(settings); bparams->seed = seed; } - - delete sparams; - MapgenType mgtype = Mapgen::getMapgenType(mg_name); - if (mgtype != MAPGEN_INVALID) { - sparams = Mapgen::createMapgenParams(mgtype); - sparams->readParams(&settings); - } } -void MapgenParams::save(Settings &settings) const +void MapgenParams::writeParams(Settings *settings) const { - settings.set("mg_name", mg_name); - settings.setU64("seed", seed); - settings.setS16("water_level", water_level); - settings.setS16("chunksize", chunksize); - settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, U32_MAX); + settings->set("mg_name", Mapgen::getMapgenName(mgtype)); + settings->setU64("seed", seed); + settings->setS16("water_level", water_level); + settings->setS16("chunksize", chunksize); + settings->setFlagStr("mg_flags", flags, flagdesc_mapgen, U32_MAX); if (bparams) - bparams->writeParams(&settings); - - if (sparams) - sparams->writeParams(&settings); + bparams->writeParams(settings); } diff --git a/src/mapgen.h b/src/mapgen.h index 618a2f6b8..5fcf2a365 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -119,37 +119,29 @@ enum MapgenType { MAPGEN_INVALID, }; -struct MapgenSpecificParams { - virtual void readParams(const Settings *settings) = 0; - virtual void writeParams(Settings *settings) const = 0; - virtual ~MapgenSpecificParams() {} -}; - struct MapgenParams { - std::string mg_name; + MapgenType mgtype; s16 chunksize; u64 seed; s16 water_level; u32 flags; BiomeParams *bparams; - MapgenSpecificParams *sparams; MapgenParams() : - mg_name(MAPGEN_DEFAULT_NAME), + mgtype(MAPGEN_DEFAULT), chunksize(5), seed(0), water_level(1), flags(MG_CAVES | MG_LIGHT | MG_DECORATIONS), - bparams(NULL), - sparams(NULL) + bparams(NULL) { } virtual ~MapgenParams(); - void load(const Settings &settings); - void save(Settings &settings) const; + virtual void readParams(const Settings *settings); + virtual void writeParams(Settings *settings) const; }; @@ -217,7 +209,7 @@ public: static const char *getMapgenName(MapgenType mgtype); static Mapgen *createMapgen(MapgenType mgtype, int mgid, MapgenParams *params, EmergeManager *emerge); - static MapgenSpecificParams *createMapgenParams(MapgenType mgtype); + static MapgenParams *createMapgenParams(MapgenType mgtype); static void getMapgenNames(std::vector *mgnames, bool include_hidden); private: diff --git a/src/mapgen_flat.cpp b/src/mapgen_flat.cpp index 956af999a..2c1715e61 100644 --- a/src/mapgen_flat.cpp +++ b/src/mapgen_flat.cpp @@ -49,26 +49,24 @@ FlagDesc flagdesc_mapgen_flat[] = { /////////////////////////////////////////////////////////////////////////////////////// -MapgenFlat::MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge) +MapgenFlat::MapgenFlat(int mapgenid, MapgenFlatParams *params, EmergeManager *emerge) : MapgenBasic(mapgenid, params, emerge) { - MapgenFlatParams *sp = (MapgenFlatParams *)params->sparams; - - this->spflags = sp->spflags; - this->ground_level = sp->ground_level; - this->large_cave_depth = sp->large_cave_depth; - this->cave_width = sp->cave_width; - this->lake_threshold = sp->lake_threshold; - this->lake_steepness = sp->lake_steepness; - this->hill_threshold = sp->hill_threshold; - this->hill_steepness = sp->hill_steepness; + this->spflags = params->spflags; + this->ground_level = params->ground_level; + this->large_cave_depth = params->large_cave_depth; + this->cave_width = params->cave_width; + this->lake_threshold = params->lake_threshold; + this->lake_steepness = params->lake_steepness; + this->hill_threshold = params->hill_threshold; + this->hill_steepness = params->hill_steepness; //// 2D noise - noise_terrain = new Noise(&sp->np_terrain, seed, csize.X, csize.Z); - noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z); + noise_terrain = new Noise(¶ms->np_terrain, seed, csize.X, csize.Z); + noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z); - MapgenBasic::np_cave1 = sp->np_cave1; - MapgenBasic::np_cave2 = sp->np_cave2; + MapgenBasic::np_cave1 = params->np_cave1; + MapgenBasic::np_cave2 = params->np_cave2; } diff --git a/src/mapgen_flat.h b/src/mapgen_flat.h index 39b6dc302..8b3de2bcf 100644 --- a/src/mapgen_flat.h +++ b/src/mapgen_flat.h @@ -32,7 +32,7 @@ class BiomeManager; extern FlagDesc flagdesc_mapgen_flat[]; -struct MapgenFlatParams : public MapgenSpecificParams { +struct MapgenFlatParams : public MapgenParams { u32 spflags; s16 ground_level; s16 large_cave_depth; @@ -55,7 +55,7 @@ struct MapgenFlatParams : public MapgenSpecificParams { class MapgenFlat : public MapgenBasic { public: - MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge); + MapgenFlat(int mapgenid, MapgenFlatParams *params, EmergeManager *emerge); ~MapgenFlat(); virtual MapgenType getType() const { return MAPGEN_FLAT; } diff --git a/src/mapgen_fractal.cpp b/src/mapgen_fractal.cpp index 9e4c210dc..0951a0afa 100644 --- a/src/mapgen_fractal.cpp +++ b/src/mapgen_fractal.cpp @@ -47,29 +47,27 @@ FlagDesc flagdesc_mapgen_fractal[] = { /////////////////////////////////////////////////////////////////////////////////////// -MapgenFractal::MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge) +MapgenFractal::MapgenFractal(int mapgenid, MapgenFractalParams *params, EmergeManager *emerge) : MapgenBasic(mapgenid, params, emerge) { - MapgenFractalParams *sp = (MapgenFractalParams *)params->sparams; - - this->spflags = sp->spflags; - this->cave_width = sp->cave_width; - this->fractal = sp->fractal; - this->iterations = sp->iterations; - this->scale = sp->scale; - this->offset = sp->offset; - this->slice_w = sp->slice_w; - this->julia_x = sp->julia_x; - this->julia_y = sp->julia_y; - this->julia_z = sp->julia_z; - this->julia_w = sp->julia_w; + this->spflags = params->spflags; + this->cave_width = params->cave_width; + this->fractal = params->fractal; + this->iterations = params->iterations; + this->scale = params->scale; + this->offset = params->offset; + this->slice_w = params->slice_w; + this->julia_x = params->julia_x; + this->julia_y = params->julia_y; + this->julia_z = params->julia_z; + this->julia_w = params->julia_w; //// 2D terrain noise - noise_seabed = new Noise(&sp->np_seabed, seed, csize.X, csize.Z); - noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z); + noise_seabed = new Noise(¶ms->np_seabed, seed, csize.X, csize.Z); + noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z); - MapgenBasic::np_cave1 = sp->np_cave1; - MapgenBasic::np_cave2 = sp->np_cave2; + MapgenBasic::np_cave1 = params->np_cave1; + MapgenBasic::np_cave2 = params->np_cave2; this->formula = fractal / 2 + fractal % 2; this->julia = fractal % 2 == 0; diff --git a/src/mapgen_fractal.h b/src/mapgen_fractal.h index cbd5567c4..3331848bc 100644 --- a/src/mapgen_fractal.h +++ b/src/mapgen_fractal.h @@ -33,7 +33,7 @@ class BiomeManager; extern FlagDesc flagdesc_mapgen_fractal[]; -struct MapgenFractalParams : public MapgenSpecificParams { +struct MapgenFractalParams : public MapgenParams { u32 spflags; float cave_width; u16 fractal; @@ -59,7 +59,7 @@ struct MapgenFractalParams : public MapgenSpecificParams { class MapgenFractal : public MapgenBasic { public: - MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge); + MapgenFractal(int mapgenid, MapgenFractalParams *params, EmergeManager *emerge); ~MapgenFractal(); virtual MapgenType getType() const { return MAPGEN_FRACTAL; } diff --git a/src/mapgen_singlenode.h b/src/mapgen_singlenode.h index 58672a0ed..07520134d 100644 --- a/src/mapgen_singlenode.h +++ b/src/mapgen_singlenode.h @@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapgen.h" -struct MapgenSinglenodeParams : public MapgenSpecificParams { +struct MapgenSinglenodeParams : public MapgenParams { MapgenSinglenodeParams() {} ~MapgenSinglenodeParams() {} diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index 74d5e1ee3..9f189e253 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -45,25 +45,23 @@ FlagDesc flagdesc_mapgen_v5[] = { }; -MapgenV5::MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge) +MapgenV5::MapgenV5(int mapgenid, MapgenV5Params *params, EmergeManager *emerge) : MapgenBasic(mapgenid, params, emerge) { - MapgenV5Params *sp = (MapgenV5Params *)params->sparams; - - this->spflags = sp->spflags; - this->cave_width = sp->cave_width; + this->spflags = params->spflags; + this->cave_width = params->cave_width; // Terrain noise - noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z); - noise_factor = new Noise(&sp->np_factor, seed, csize.X, csize.Z); - noise_height = new Noise(&sp->np_height, seed, csize.X, csize.Z); + noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z); + noise_factor = new Noise(¶ms->np_factor, seed, csize.X, csize.Z); + noise_height = new Noise(¶ms->np_height, seed, csize.X, csize.Z); // 3D terrain noise // 1-up 1-down overgeneration - noise_ground = new Noise(&sp->np_ground, seed, csize.X, csize.Y + 2, csize.Z); + noise_ground = new Noise(¶ms->np_ground, seed, csize.X, csize.Y + 2, csize.Z); - MapgenBasic::np_cave1 = sp->np_cave1; - MapgenBasic::np_cave2 = sp->np_cave2; + MapgenBasic::np_cave1 = params->np_cave1; + MapgenBasic::np_cave2 = params->np_cave2; } diff --git a/src/mapgen_v5.h b/src/mapgen_v5.h index 5f6b10383..ddb090a9c 100644 --- a/src/mapgen_v5.h +++ b/src/mapgen_v5.h @@ -30,7 +30,7 @@ class BiomeManager; extern FlagDesc flagdesc_mapgen_v5[]; -struct MapgenV5Params : public MapgenSpecificParams { +struct MapgenV5Params : public MapgenParams { u32 spflags; float cave_width; NoiseParams np_filler_depth; @@ -50,7 +50,7 @@ struct MapgenV5Params : public MapgenSpecificParams { class MapgenV5 : public MapgenBasic { public: - MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge); + MapgenV5(int mapgenid, MapgenV5Params *params, EmergeManager *emerge); ~MapgenV5(); virtual MapgenType getType() const { return MAPGEN_V5; } diff --git a/src/mapgen_v6.cpp b/src/mapgen_v6.cpp index caa64827e..e4444963f 100644 --- a/src/mapgen_v6.cpp +++ b/src/mapgen_v6.cpp @@ -53,7 +53,7 @@ FlagDesc flagdesc_mapgen_v6[] = { ///////////////////////////////////////////////////////////////////////////// -MapgenV6::MapgenV6(int mapgenid, MapgenParams *params, EmergeManager *emerge) +MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params, EmergeManager *emerge) : Mapgen(mapgenid, params, emerge) { this->m_emerge = emerge; @@ -61,26 +61,25 @@ MapgenV6::MapgenV6(int mapgenid, MapgenParams *params, EmergeManager *emerge) this->heightmap = new s16[csize.X * csize.Z]; - MapgenV6Params *sp = (MapgenV6Params *)params->sparams; - this->spflags = sp->spflags; - this->freq_desert = sp->freq_desert; - this->freq_beach = sp->freq_beach; + this->spflags = params->spflags; + this->freq_desert = params->freq_desert; + this->freq_beach = params->freq_beach; - np_cave = &sp->np_cave; - np_humidity = &sp->np_humidity; - np_trees = &sp->np_trees; - np_apple_trees = &sp->np_apple_trees; + np_cave = ¶ms->np_cave; + np_humidity = ¶ms->np_humidity; + np_trees = ¶ms->np_trees; + np_apple_trees = ¶ms->np_apple_trees; //// Create noise objects - noise_terrain_base = new Noise(&sp->np_terrain_base, seed, csize.X, csize.Y); - noise_terrain_higher = new Noise(&sp->np_terrain_higher, seed, csize.X, csize.Y); - noise_steepness = new Noise(&sp->np_steepness, seed, csize.X, csize.Y); - noise_height_select = new Noise(&sp->np_height_select, seed, csize.X, csize.Y); - noise_mud = new Noise(&sp->np_mud, seed, csize.X, csize.Y); - noise_beach = new Noise(&sp->np_beach, seed, csize.X, csize.Y); - noise_biome = new Noise(&sp->np_biome, seed, + noise_terrain_base = new Noise(¶ms->np_terrain_base, seed, csize.X, csize.Y); + noise_terrain_higher = new Noise(¶ms->np_terrain_higher, seed, csize.X, csize.Y); + noise_steepness = new Noise(¶ms->np_steepness, seed, csize.X, csize.Y); + noise_height_select = new Noise(¶ms->np_height_select, seed, csize.X, csize.Y); + noise_mud = new Noise(¶ms->np_mud, seed, csize.X, csize.Y); + noise_beach = new Noise(¶ms->np_beach, seed, csize.X, csize.Y); + noise_biome = new Noise(¶ms->np_biome, seed, csize.X + 2 * MAP_BLOCKSIZE, csize.Y + 2 * MAP_BLOCKSIZE); - noise_humidity = new Noise(&sp->np_humidity, seed, + noise_humidity = new Noise(¶ms->np_humidity, seed, csize.X + 2 * MAP_BLOCKSIZE, csize.Y + 2 * MAP_BLOCKSIZE); //// Resolve nodes to be used diff --git a/src/mapgen_v6.h b/src/mapgen_v6.h index 20b0bf92e..f018ffaca 100644 --- a/src/mapgen_v6.h +++ b/src/mapgen_v6.h @@ -53,7 +53,7 @@ enum BiomeV6Type }; -struct MapgenV6Params : public MapgenSpecificParams { +struct MapgenV6Params : public MapgenParams { u32 spflags; float freq_desert; float freq_beach; @@ -124,7 +124,7 @@ public: content_t c_mossycobble; content_t c_stair_cobble; - MapgenV6(int mapgenid, MapgenParams *params, EmergeManager *emerge); + MapgenV6(int mapgenid, MapgenV6Params *params, EmergeManager *emerge); ~MapgenV6(); virtual MapgenType getType() const { return MAPGEN_V6; } diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index c24b3e8d1..d14fdb97a 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -50,30 +50,28 @@ FlagDesc flagdesc_mapgen_v7[] = { /////////////////////////////////////////////////////////////////////////////// -MapgenV7::MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge) +MapgenV7::MapgenV7(int mapgenid, MapgenV7Params *params, EmergeManager *emerge) : MapgenBasic(mapgenid, params, emerge) { - MapgenV7Params *sp = (MapgenV7Params *)params->sparams; - - this->spflags = sp->spflags; - this->cave_width = sp->cave_width; + this->spflags = params->spflags; + this->cave_width = params->cave_width; //// Terrain noise - noise_terrain_base = new Noise(&sp->np_terrain_base, seed, csize.X, csize.Z); - noise_terrain_alt = new Noise(&sp->np_terrain_alt, seed, csize.X, csize.Z); - noise_terrain_persist = new Noise(&sp->np_terrain_persist, seed, csize.X, csize.Z); - noise_height_select = new Noise(&sp->np_height_select, seed, csize.X, csize.Z); - noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z); - noise_mount_height = new Noise(&sp->np_mount_height, seed, csize.X, csize.Z); - noise_ridge_uwater = new Noise(&sp->np_ridge_uwater, seed, csize.X, csize.Z); + noise_terrain_base = new Noise(¶ms->np_terrain_base, seed, csize.X, csize.Z); + noise_terrain_alt = new Noise(¶ms->np_terrain_alt, seed, csize.X, csize.Z); + noise_terrain_persist = new Noise(¶ms->np_terrain_persist, seed, csize.X, csize.Z); + noise_height_select = new Noise(¶ms->np_height_select, seed, csize.X, csize.Z); + noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z); + noise_mount_height = new Noise(¶ms->np_mount_height, seed, csize.X, csize.Z); + noise_ridge_uwater = new Noise(¶ms->np_ridge_uwater, seed, csize.X, csize.Z); //// 3d terrain noise // 1-up 1-down overgeneration - noise_mountain = new Noise(&sp->np_mountain, seed, csize.X, csize.Y + 2, csize.Z); - noise_ridge = new Noise(&sp->np_ridge, seed, csize.X, csize.Y + 2, csize.Z); + noise_mountain = new Noise(¶ms->np_mountain, seed, csize.X, csize.Y + 2, csize.Z); + noise_ridge = new Noise(¶ms->np_ridge, seed, csize.X, csize.Y + 2, csize.Z); - MapgenBasic::np_cave1 = sp->np_cave1; - MapgenBasic::np_cave2 = sp->np_cave2; + MapgenBasic::np_cave1 = params->np_cave1; + MapgenBasic::np_cave2 = params->np_cave2; } diff --git a/src/mapgen_v7.h b/src/mapgen_v7.h index c75f18a93..3a6bc0801 100644 --- a/src/mapgen_v7.h +++ b/src/mapgen_v7.h @@ -32,7 +32,7 @@ class BiomeManager; extern FlagDesc flagdesc_mapgen_v7[]; -struct MapgenV7Params : public MapgenSpecificParams { +struct MapgenV7Params : public MapgenParams { u32 spflags; float cave_width; NoiseParams np_terrain_base; @@ -56,7 +56,7 @@ struct MapgenV7Params : public MapgenSpecificParams { class MapgenV7 : public MapgenBasic { public: - MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge); + MapgenV7(int mapgenid, MapgenV7Params *params, EmergeManager *emerge); ~MapgenV7(); virtual MapgenType getType() const { return MAPGEN_V7; } diff --git a/src/mapgen_valleys.cpp b/src/mapgen_valleys.cpp index 02a8fbfe0..a61f1b329 100644 --- a/src/mapgen_valleys.cpp +++ b/src/mapgen_valleys.cpp @@ -64,7 +64,7 @@ static FlagDesc flagdesc_mapgen_valleys[] = { /////////////////////////////////////////////////////////////////////////////// -MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *emerge) +MapgenValleys::MapgenValleys(int mapgenid, MapgenValleysParams *params, EmergeManager *emerge) : MapgenBasic(mapgenid, params, emerge) { // NOTE: MapgenValleys has a hard dependency on BiomeGenOriginal @@ -73,34 +73,33 @@ MapgenValleys::MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager * this->map_gen_limit = MYMIN(MAX_MAP_GENERATION_LIMIT, g_settings->getU16("map_generation_limit")); - MapgenValleysParams *sp = (MapgenValleysParams *)params->sparams; BiomeParamsOriginal *bp = (BiomeParamsOriginal *)params->bparams; - this->spflags = sp->spflags; - this->altitude_chill = sp->altitude_chill; - this->large_cave_depth = sp->large_cave_depth; - this->lava_features_lim = rangelim(sp->lava_features, 0, 10); - this->massive_cave_depth = sp->massive_cave_depth; - this->river_depth_bed = sp->river_depth + 1.f; - this->river_size_factor = sp->river_size / 100.f; - this->water_features_lim = rangelim(sp->water_features, 0, 10); - this->cave_width = sp->cave_width; + this->spflags = params->spflags; + this->altitude_chill = params->altitude_chill; + this->large_cave_depth = params->large_cave_depth; + this->lava_features_lim = rangelim(params->lava_features, 0, 10); + this->massive_cave_depth = params->massive_cave_depth; + this->river_depth_bed = params->river_depth + 1.f; + this->river_size_factor = params->river_size / 100.f; + this->water_features_lim = rangelim(params->water_features, 0, 10); + this->cave_width = params->cave_width; //// 2D Terrain noise - noise_filler_depth = new Noise(&sp->np_filler_depth, seed, csize.X, csize.Z); - noise_inter_valley_slope = new Noise(&sp->np_inter_valley_slope, seed, csize.X, csize.Z); - noise_rivers = new Noise(&sp->np_rivers, seed, csize.X, csize.Z); - noise_terrain_height = new Noise(&sp->np_terrain_height, seed, csize.X, csize.Z); - noise_valley_depth = new Noise(&sp->np_valley_depth, seed, csize.X, csize.Z); - noise_valley_profile = new Noise(&sp->np_valley_profile, seed, csize.X, csize.Z); + noise_filler_depth = new Noise(¶ms->np_filler_depth, seed, csize.X, csize.Z); + noise_inter_valley_slope = new Noise(¶ms->np_inter_valley_slope, seed, csize.X, csize.Z); + noise_rivers = new Noise(¶ms->np_rivers, seed, csize.X, csize.Z); + noise_terrain_height = new Noise(¶ms->np_terrain_height, seed, csize.X, csize.Z); + noise_valley_depth = new Noise(¶ms->np_valley_depth, seed, csize.X, csize.Z); + noise_valley_profile = new Noise(¶ms->np_valley_profile, seed, csize.X, csize.Z); //// 3D Terrain noise // 1-up 1-down overgeneration - noise_inter_valley_fill = new Noise(&sp->np_inter_valley_fill, seed, csize.X, csize.Y + 2, csize.Z); + noise_inter_valley_fill = new Noise(¶ms->np_inter_valley_fill, seed, csize.X, csize.Y + 2, csize.Z); // 1-down overgeneraion - noise_cave1 = new Noise(&sp->np_cave1, seed, csize.X, csize.Y + 1, csize.Z); - noise_cave2 = new Noise(&sp->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); - noise_massive_caves = new Noise(&sp->np_massive_caves, seed, csize.X, csize.Y + 1, csize.Z); + noise_cave1 = new Noise(¶ms->np_cave1, seed, csize.X, csize.Y + 1, csize.Z); + noise_cave2 = new Noise(¶ms->np_cave2, seed, csize.X, csize.Y + 1, csize.Z); + noise_massive_caves = new Noise(¶ms->np_massive_caves, seed, csize.X, csize.Y + 1, csize.Z); this->humid_rivers = (spflags & MGVALLEYS_HUMID_RIVERS); this->use_altitude_chill = (spflags & MGVALLEYS_ALT_CHILL); diff --git a/src/mapgen_valleys.h b/src/mapgen_valleys.h index 00f6aff07..6dd7ebc47 100644 --- a/src/mapgen_valleys.h +++ b/src/mapgen_valleys.h @@ -46,7 +46,7 @@ class BiomeGenOriginal; //extern Profiler *mapgen_profiler; -struct MapgenValleysParams : public MapgenSpecificParams { +struct MapgenValleysParams : public MapgenParams { u32 spflags; s16 large_cave_depth; s16 massive_cave_depth; @@ -88,7 +88,7 @@ struct TerrainNoise { class MapgenValleys : public MapgenBasic { public: - MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *emerge); + MapgenValleys(int mapgenid, MapgenValleysParams *params, EmergeManager *emerge); ~MapgenValleys(); virtual MapgenType getType() const { return MAPGEN_VALLEYS; } diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index dc188f8a4..9f14838ce 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -600,24 +600,37 @@ int ModApiMapgen::l_get_mapgen_params(lua_State *L) { NO_MAP_LOCK_REQUIRED; - MapgenParams *params = &getServer(L)->getEmergeManager()->params; + log_deprecated(L, "get_mapgen_params is deprecated; " + "use get_mapgen_setting instead"); + + std::string value; + + MapSettingsManager *settingsmgr = + getServer(L)->getEmergeManager()->map_settings_mgr; lua_newtable(L); - lua_pushstring(L, params->mg_name.c_str()); + settingsmgr->getMapSetting("mg_name", &value); + lua_pushstring(L, value.c_str()); lua_setfield(L, -2, "mgname"); - lua_pushinteger(L, params->seed); + settingsmgr->getMapSetting("seed", &value); + std::istringstream ss(value); + u64 seed; + ss >> seed; + lua_pushinteger(L, seed); lua_setfield(L, -2, "seed"); - lua_pushinteger(L, params->water_level); + settingsmgr->getMapSetting("water_level", &value); + lua_pushinteger(L, stoi(value, -32768, 32767)); lua_setfield(L, -2, "water_level"); - lua_pushinteger(L, params->chunksize); + settingsmgr->getMapSetting("chunksize", &value); + lua_pushinteger(L, stoi(value, -32768, 32767)); lua_setfield(L, -2, "chunksize"); - std::string flagstr = writeFlagString(params->flags, flagdesc_mapgen, U32_MAX); - lua_pushstring(L, flagstr.c_str()); + settingsmgr->getMapSetting("mg_flags", &value); + lua_pushstring(L, value.c_str()); lua_setfield(L, -2, "flags"); return 1; @@ -630,44 +643,120 @@ int ModApiMapgen::l_set_mapgen_params(lua_State *L) { NO_MAP_LOCK_REQUIRED; + log_deprecated(L, "set_mapgen_params is deprecated; " + "use set_mapgen_setting instead"); + if (!lua_istable(L, 1)) return 0; - EmergeManager *emerge = getServer(L)->getEmergeManager(); - if (emerge->isRunning()) - throw LuaError("Cannot set parameters while mapgen is running"); - - MapgenParams *params = &emerge->params; - u32 flags = 0, flagmask = 0; + MapSettingsManager *settingsmgr = + getServer(L)->getEmergeManager()->map_settings_mgr; lua_getfield(L, 1, "mgname"); - if (lua_isstring(L, -1)) { - params->mg_name = lua_tostring(L, -1); - delete params->sparams; - params->sparams = NULL; - } + if (lua_isstring(L, -1)) + settingsmgr->setMapSetting("mg_name", lua_tostring(L, -1), true); lua_getfield(L, 1, "seed"); if (lua_isnumber(L, -1)) - params->seed = lua_tointeger(L, -1); + settingsmgr->setMapSetting("seed", lua_tostring(L, -1), true); lua_getfield(L, 1, "water_level"); if (lua_isnumber(L, -1)) - params->water_level = lua_tointeger(L, -1); + settingsmgr->setMapSetting("water_level", lua_tostring(L, -1), true); lua_getfield(L, 1, "chunksize"); if (lua_isnumber(L, -1)) - params->chunksize = lua_tointeger(L, -1); + settingsmgr->setMapSetting("chunksize", lua_tostring(L, -1), true); warn_if_field_exists(L, 1, "flagmask", "Deprecated: flags field now includes unset flags."); - lua_getfield(L, 1, "flagmask"); + + lua_getfield(L, 1, "flags"); if (lua_isstring(L, -1)) - params->flags &= ~readFlagString(lua_tostring(L, -1), flagdesc_mapgen, NULL); + settingsmgr->setMapSetting("mg_flags", lua_tostring(L, -1), true); + + return 0; +} + +// get_mapgen_setting(name) +int ModApiMapgen::l_get_mapgen_setting(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; - if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) { - params->flags &= ~flagmask; - params->flags |= flags; + std::string value; + MapSettingsManager *settingsmgr = + getServer(L)->getEmergeManager()->map_settings_mgr; + + const char *name = luaL_checkstring(L, 1); + if (!settingsmgr->getMapSetting(name, &value)) + return 0; + + lua_pushstring(L, value.c_str()); + return 1; +} + +// get_mapgen_setting_noiseparams(name) +int ModApiMapgen::l_get_mapgen_setting_noiseparams(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + NoiseParams np; + MapSettingsManager *settingsmgr = + getServer(L)->getEmergeManager()->map_settings_mgr; + + const char *name = luaL_checkstring(L, 1); + if (!settingsmgr->getMapSettingNoiseParams(name, &np)) + return 0; + + push_noiseparams(L, &np); + return 1; +} + +// set_mapgen_setting(name, value, override_meta) +// set mapgen config values +int ModApiMapgen::l_set_mapgen_setting(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + MapSettingsManager *settingsmgr = + getServer(L)->getEmergeManager()->map_settings_mgr; + + const char *name = luaL_checkstring(L, 1); + const char *value = luaL_checkstring(L, 2); + bool override_meta = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : false; + + if (!settingsmgr->setMapSetting(name, value, override_meta)) { + errorstream << "set_mapgen_setting: cannot set '" + << name << "' after initialization" << std::endl; + } + + return 0; +} + + +// set_mapgen_setting_noiseparams(name, noiseparams, set_default) +// set mapgen config values for noise parameters +int ModApiMapgen::l_set_mapgen_setting_noiseparams(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + MapSettingsManager *settingsmgr = + getServer(L)->getEmergeManager()->map_settings_mgr; + + const char *name = luaL_checkstring(L, 1); + + NoiseParams np; + if (!read_noiseparams(L, 2, &np)) { + errorstream << "set_mapgen_setting_noiseparams: cannot set '" << name + << "'; invalid noiseparams table" << std::endl; + return 0; + } + + bool override_meta = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : false; + + if (!settingsmgr->setMapSettingNoiseParams(name, &np, override_meta)) { + errorstream << "set_mapgen_setting_noiseparams: cannot set '" + << name << "' after initialization" << std::endl; } return 0; @@ -683,8 +772,11 @@ int ModApiMapgen::l_set_noiseparams(lua_State *L) const char *name = luaL_checkstring(L, 1); NoiseParams np; - if (!read_noiseparams(L, 2, &np)) + if (!read_noiseparams(L, 2, &np)) { + errorstream << "set_noiseparams: cannot set '" << name + << "'; invalid noiseparams table" << std::endl; return 0; + } bool set_default = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : true; @@ -1143,7 +1235,7 @@ int ModApiMapgen::l_generate_ores(lua_State *L) EmergeManager *emerge = getServer(L)->getEmergeManager(); Mapgen mg; - mg.seed = emerge->params.seed; + mg.seed = emerge->mgparams->seed; mg.vm = LuaVoxelManip::checkobject(L, 1)->vm; mg.ndef = getServer(L)->getNodeDefManager(); @@ -1169,7 +1261,7 @@ int ModApiMapgen::l_generate_decorations(lua_State *L) EmergeManager *emerge = getServer(L)->getEmergeManager(); Mapgen mg; - mg.seed = emerge->params.seed; + mg.seed = emerge->mgparams->seed; mg.vm = LuaVoxelManip::checkobject(L, 1)->vm; mg.ndef = getServer(L)->getNodeDefManager(); @@ -1393,6 +1485,10 @@ void ModApiMapgen::Initialize(lua_State *L, int top) API_FCT(get_mapgen_params); API_FCT(set_mapgen_params); + API_FCT(get_mapgen_setting); + API_FCT(set_mapgen_setting); + API_FCT(get_mapgen_setting_noiseparams); + API_FCT(set_mapgen_setting_noiseparams); API_FCT(set_noiseparams); API_FCT(get_noiseparams); API_FCT(set_gen_notify); diff --git a/src/script/lua_api/l_mapgen.h b/src/script/lua_api/l_mapgen.h index 9751c0db6..bb94575c7 100644 --- a/src/script/lua_api/l_mapgen.h +++ b/src/script/lua_api/l_mapgen.h @@ -40,6 +40,18 @@ private: // set mapgen parameters static int l_set_mapgen_params(lua_State *L); + // get_mapgen_setting(name) + static int l_get_mapgen_setting(lua_State *L); + + // set_mapgen_setting(name, value, override_meta) + static int l_set_mapgen_setting(lua_State *L); + + // get_mapgen_setting_noiseparams(name) + static int l_get_mapgen_setting_noiseparams(lua_State *L); + + // set_mapgen_setting_noiseparams(name, value, override_meta) + static int l_set_mapgen_setting_noiseparams(lua_State *L); + // set_noiseparam_defaults(name, noiseparams, set_default) static int l_set_noiseparams(lua_State *L); diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index f13866408..0d8123acd 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -190,7 +190,7 @@ int LuaVoxelManip::l_calc_lighting(lua_State *L) Mapgen mg; mg.vm = vm; mg.ndef = ndef; - mg.water_level = emerge->params.water_level; + mg.water_level = emerge->mgparams->water_level; mg.calcLighting(pmin, pmax, fpmin, fpmax, propagate_shadow); diff --git a/src/server.cpp b/src/server.cpp index f7f698d50..97a53f189 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -266,9 +266,6 @@ Server::Server( //lock environment MutexAutoLock envlock(m_env_mutex); - // Load mapgen params from Settings - m_emerge->loadMapgenParams(); - // Create the Map (loads map_meta.txt, overriding configured mapgen params) ServerMap *servermap = new ServerMap(path_world, this, m_emerge); @@ -331,8 +328,11 @@ Server::Server( m_clients.setEnv(m_env); + if (!servermap->settings_mgr.makeMapgenParams()) + FATAL_ERROR("Couldn't create any mapgen type"); + // Initialize mapgens - m_emerge->initMapgens(); + m_emerge->initMapgens(servermap->getMapgenParams()); m_enable_rollback_recording = g_settings->getBool("enable_rollback_recording"); if (m_enable_rollback_recording) { @@ -402,11 +402,8 @@ Server::~Server() m_emerge->stopThreads(); // Delete things in the reverse order of creation - delete m_env; - - // N.B. the EmergeManager should be deleted after the Environment since Map - // depends on EmergeManager to write its current params to the map meta delete m_emerge; + delete m_env; delete m_rollback; delete m_banmanager; delete m_event; @@ -655,7 +652,7 @@ void Server::AsyncRunStep(bool initial_step) m_env->getGameTime(), m_lag, m_gamespec.id, - m_emerge->params.mg_name, + Mapgen::getMapgenName(m_emerge->mgparams->mgtype), m_mods); counter = 0.01; } diff --git a/src/subgame.cpp b/src/subgame.cpp index 7e9a0b368..55bbd3954 100644 --- a/src/subgame.cpp +++ b/src/subgame.cpp @@ -313,8 +313,8 @@ bool loadGameConfAndInitWorld(const std::string &path, const SubgameSpec &gamesp Settings conf; MapgenParams params; - params.load(*g_settings); - params.save(conf); + params.readParams(g_settings); + params.writeParams(&conf); conf.writeLines(oss); oss << "[end_of_params]\n"; diff --git a/src/unittest/CMakeLists.txt b/src/unittest/CMakeLists.txt index a07ed8ba5..34de99e12 100644 --- a/src/unittest/CMakeLists.txt +++ b/src/unittest/CMakeLists.txt @@ -6,6 +6,7 @@ set (UNITTEST_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/test_connection.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_filepath.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_inventory.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test_map_settings_manager.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_mapnode.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_nodedef.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_noderesolver.cpp diff --git a/src/unittest/test_map_settings_manager.cpp b/src/unittest/test_map_settings_manager.cpp new file mode 100644 index 000000000..b2ad53192 --- /dev/null +++ b/src/unittest/test_map_settings_manager.cpp @@ -0,0 +1,255 @@ + /* +Minetest +Copyright (C) 2010-2014 kwolekr, Ryan Kwolek + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program 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 Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "test.h" + +#include "noise.h" +#include "settings.h" +#include "mapgen_v5.h" +#include "util/sha1.h" +#include "map_settings_manager.h" + +class TestMapSettingsManager : public TestBase { +public: + TestMapSettingsManager() { TestManager::registerTestModule(this); } + const char *getName() { return "TestMapSettingsManager"; } + + void makeUserConfig(Settings *conf); + std::string makeMetaFile(bool make_corrupt); + + void runTests(IGameDef *gamedef); + + void testMapSettingsManager(); + void testMapMetaSaveLoad(); + void testMapMetaFailures(); +}; + +static TestMapSettingsManager g_test_instance; + +void TestMapSettingsManager::runTests(IGameDef *gamedef) +{ + TEST(testMapSettingsManager); + TEST(testMapMetaSaveLoad); + TEST(testMapMetaFailures); +} + +//////////////////////////////////////////////////////////////////////////////// + + +void check_noise_params(const NoiseParams *np1, const NoiseParams *np2) +{ + UASSERTEQ(float, np1->offset, np2->offset); + UASSERTEQ(float, np1->scale, np2->scale); + UASSERT(np1->spread == np2->spread); + UASSERTEQ(s32, np1->seed, np2->seed); + UASSERTEQ(u16, np1->octaves, np2->octaves); + UASSERTEQ(float, np1->persist, np2->persist); + UASSERTEQ(float, np1->lacunarity, np2->lacunarity); + UASSERTEQ(u32, np1->flags, np2->flags); +} + + +std::string read_file_to_string(const std::string &filepath) +{ + std::string buf; + FILE *f = fopen(filepath.c_str(), "rb"); + if (!f) + return ""; + + fseek(f, 0, SEEK_END); + + long filesize = ftell(f); + if (filesize == -1) + return ""; + rewind(f); + + buf.resize(filesize); + + fread(&buf[0], 1, filesize, f); + + fclose(f); + return buf; +} + + +void TestMapSettingsManager::makeUserConfig(Settings *conf) +{ + conf->set("mg_name", "v7"); + conf->set("seed", "5678"); + conf->set("water_level", "20"); + conf->set("mgv5_np_factor", "0, 12, (500, 250, 500), 920382, 5, 0.45, 3.0"); + conf->set("mgv5_np_height", "0, 15, (500, 250, 500), 841746, 5, 0.5, 3.0"); + conf->set("mgv5_np_filler_depth", "20, 1, (150, 150, 150), 261, 4, 0.7, 1.0"); + conf->set("mgv5_np_ground", "-43, 40, (80, 80, 80), 983240, 4, 0.55, 2.0"); +} + + +std::string TestMapSettingsManager::makeMetaFile(bool make_corrupt) +{ + std::string metafile = getTestTempFile(); + + const char *metafile_contents = + "mg_name = v5\n" + "seed = 1234\n" + "mg_flags = light\n" + "mgv5_np_filler_depth = 20, 1, (150, 150, 150), 261, 4, 0.7, 1.0\n" + "mgv5_np_height = 20, 10, (250, 250, 250), 84174, 4, 0.5, 1.0\n"; + + FILE *f = fopen(metafile.c_str(), "wb"); + UASSERT(f != NULL); + + fputs(metafile_contents, f); + if (!make_corrupt) + fputs("[end_of_params]\n", f); + + fclose(f); + + return metafile; +} + + +void TestMapSettingsManager::testMapSettingsManager() +{ + Settings user_settings; + makeUserConfig(&user_settings); + + std::string test_mapmeta_path = makeMetaFile(false); + + MapSettingsManager mgr(&user_settings, test_mapmeta_path); + std::string value; + + UASSERT(mgr.getMapSetting("mg_name", &value)); + UASSERT(value == "v7"); + + // Pretend we're initializing the ServerMap + UASSERT(mgr.loadMapMeta()); + + // Pretend some scripts are requesting mapgen params + UASSERT(mgr.getMapSetting("mg_name", &value)); + UASSERT(value == "v5"); + UASSERT(mgr.getMapSetting("seed", &value)); + UASSERT(value == "1234"); + UASSERT(mgr.getMapSetting("water_level", &value)); + UASSERT(value == "20"); + + // Pretend we have some mapgen settings configured from the scripting + UASSERT(mgr.setMapSetting("water_level", "15")); + UASSERT(mgr.setMapSetting("seed", "02468")); + UASSERT(mgr.setMapSetting("mg_flags", "nolight", true)); + + NoiseParams script_np_filler_depth(0, 100, v3f(200, 100, 200), 261, 4, 0.7, 2.0); + NoiseParams script_np_factor(0, 100, v3f(50, 50, 50), 920381, 3, 0.45, 2.0); + NoiseParams script_np_height(0, 100, v3f(450, 450, 450), 84174, 4, 0.5, 2.0); + NoiseParams meta_np_height(20, 10, v3f(250, 250, 250), 84174, 4, 0.5, 1.0); + NoiseParams user_np_ground(-43, 40, v3f(80, 80, 80), 983240, 4, 0.55, 2.0, NOISE_FLAG_EASED); + + mgr.setMapSettingNoiseParams("mgv5_np_filler_depth", &script_np_filler_depth, true); + mgr.setMapSettingNoiseParams("mgv5_np_height", &script_np_height); + mgr.setMapSettingNoiseParams("mgv5_np_factor", &script_np_factor); + + // Now make our Params and see if the values are correctly sourced + MapgenParams *params = mgr.makeMapgenParams(); + UASSERT(params->mgtype == MAPGEN_V5); + UASSERT(params->chunksize == 5); + UASSERT(params->water_level == 15); + UASSERT(params->seed == 1234); + UASSERT((params->flags & MG_LIGHT) == 0); + + MapgenV5Params *v5params = (MapgenV5Params *)params; + + check_noise_params(&v5params->np_filler_depth, &script_np_filler_depth); + check_noise_params(&v5params->np_factor, &script_np_factor); + check_noise_params(&v5params->np_height, &meta_np_height); + check_noise_params(&v5params->np_ground, &user_np_ground); + + UASSERT(mgr.setMapSetting("foobar", "25") == false); + + // Pretend the ServerMap is shutting down + UASSERT(mgr.saveMapMeta()); + + // Make sure our interface expectations are met + UASSERT(mgr.mapgen_params == params); + UASSERT(mgr.makeMapgenParams() == params); + + // Load the resulting map_meta.txt and make sure it contains what we expect + unsigned char expected_contents_hash[20] = { + 0xf6, 0x44, 0x90, 0xb7, 0xab, 0xd8, 0x91, 0xf4, 0x08, 0x96, + 0xfc, 0x7e, 0xed, 0x01, 0xc5, 0x9a, 0xfd, 0x2f, 0x2d, 0x79 + }; + + SHA1 ctx; + std::string metafile_contents = read_file_to_string(test_mapmeta_path); + ctx.addBytes(&metafile_contents[0], metafile_contents.size()); + unsigned char *sha1_result = ctx.getDigest(); + int resultdiff = memcmp(sha1_result, expected_contents_hash, 20); + free(sha1_result); + + UASSERT(!resultdiff); +} + + +void TestMapSettingsManager::testMapMetaSaveLoad() +{ + Settings conf; + std::string path = getTestTempDirectory() + + DIR_DELIM + "foobar" + DIR_DELIM + "map_meta.txt"; + + // Create a set of mapgen params and save them to map meta + conf.set("seed", "12345"); + conf.set("water_level", "5"); + MapSettingsManager mgr1(&conf, path); + MapgenParams *params1 = mgr1.makeMapgenParams(); + UASSERT(params1); + UASSERT(mgr1.saveMapMeta()); + + // Now try loading the map meta to mapgen params + conf.set("seed", "67890"); + conf.set("water_level", "32"); + MapSettingsManager mgr2(&conf, path); + UASSERT(mgr2.loadMapMeta()); + MapgenParams *params2 = mgr2.makeMapgenParams(); + UASSERT(params2); + + // Check that both results are correct + UASSERTEQ(u64, params1->seed, 12345); + UASSERTEQ(s16, params1->water_level, 5); + UASSERTEQ(u64, params2->seed, 12345); + UASSERTEQ(s16, params2->water_level, 5); +} + + +void TestMapSettingsManager::testMapMetaFailures() +{ + std::string test_mapmeta_path; + Settings conf; + + // Check to see if it'll fail on a non-existent map meta file + test_mapmeta_path = "woobawooba/fgdfg/map_meta.txt"; + UASSERT(!fs::PathExists(test_mapmeta_path)); + + MapSettingsManager mgr1(&conf, test_mapmeta_path); + UASSERT(!mgr1.loadMapMeta()); + + // Check to see if it'll fail on a corrupt map meta file + test_mapmeta_path = makeMetaFile(true); + UASSERT(fs::PathExists(test_mapmeta_path)); + + MapSettingsManager mgr2(&conf, test_mapmeta_path); + UASSERT(!mgr2.loadMapMeta()); +} -- cgit v1.2.3 From 281e9f39fdffd106e79122ae23e2b5443a5daea3 Mon Sep 17 00:00:00 2001 From: Foghrye4 Date: Mon, 25 Apr 2016 18:50:10 +0300 Subject: Adding minetest.clear_craft Modifications by est31: grammar fixes in doc + error messages and a little style fix, no functional change. --- doc/lua_api.txt | 8 ++++ src/craftdef.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++ src/craftdef.h | 4 ++ src/script/lua_api/l_craft.cpp | 76 ++++++++++++++++++++++++++++++++++- src/script/lua_api/l_craft.h | 1 + 5 files changed, 178 insertions(+), 1 deletion(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 297566068..a59a9c0f2 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1847,6 +1847,14 @@ Call these functions only at load time! * `minetest.register_craftitem(name, item definition)` * `minetest.register_alias(name, convert_to)` * `minetest.register_craft(recipe)` + * Check recipe table syntax for different types below. +* `minetest.clear_craft(recipe)` + * Will erase existing craft based either on output item or on input recipe. + * Specify either output or input only. If you specify both, input will be ignored. For input use the same recipe table + syntax as for `minetest.register_craft(recipe)`. For output specify only the item, without a quantity. + * If no erase candidate could be found, Lua exception will be thrown. + * Warning! The type field ("shaped","cooking" or any other) will be ignored if the recipe + contains output. Erasing is then done independently from the crafting method. * `minetest.register_ore(ore definition)` * `minetest.register_decoration(decoration definition)` * `minetest.override_item(name, redefinition)` diff --git a/src/craftdef.cpp b/src/craftdef.cpp index d3f1edaf9..45d3018a7 100644 --- a/src/craftdef.cpp +++ b/src/craftdef.cpp @@ -960,6 +960,96 @@ public: return recipes; } + + virtual bool clearCraftRecipesByOutput(const CraftOutput &output, IGameDef *gamedef) + { + std::map >::iterator vec_iter = + m_output_craft_definitions.find(output.item); + + if (vec_iter == m_output_craft_definitions.end()) + return false; + + std::vector &vec = vec_iter->second; + for (std::vector::iterator i = vec.begin(); + i != vec.end(); ++i) { + CraftDefinition *def = *i; + // Recipes are not yet hashed at this point + std::vector &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]; + std::vector new_vec_by_input; + /* We will preallocate necessary memory addresses, so we don't need to reallocate them later. + This would save us some performance. */ + new_vec_by_input.reserve(unhashed_inputs_vec.size()); + for (std::vector::iterator i2 = unhashed_inputs_vec.begin(); + i2 != unhashed_inputs_vec.end(); ++i2) { + if (def != *i2) { + new_vec_by_input.push_back(*i2); + } + } + m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].swap(new_vec_by_input); + } + m_output_craft_definitions.erase(output.item); + return true; + } + + virtual bool clearCraftRecipesByInput(CraftMethod craft_method, unsigned int craft_grid_width, + const std::vector &recipe, IGameDef *gamedef) + { + bool all_empty = true; + for (std::vector::size_type i = 0; + i < recipe.size(); i++) { + if (!recipe[i].empty()) { + all_empty = false; + break; + } + } + if (all_empty) + return false; + + CraftInput input(craft_method, craft_grid_width, craftGetItems(recipe, gamedef)); + // Recipes are not yet hashed at this point + std::vector &unhashed_inputs_vec = m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0]; + std::vector new_vec_by_input; + bool got_hit = false; + for (std::vector::size_type + i = unhashed_inputs_vec.size(); i > 0; i--) { + CraftDefinition *def = unhashed_inputs_vec[i - 1]; + /* If the input doesn't match the recipe definition, this recipe definition later + will be added back in source map. */ + if (!def->check(input, gamedef)) { + new_vec_by_input.push_back(def); + continue; + } + CraftOutput output = def->getOutput(input, gamedef); + got_hit = true; + std::map >::iterator + vec_iter = m_output_craft_definitions.find(output.item); + if (vec_iter == m_output_craft_definitions.end()) + continue; + std::vector &vec = vec_iter->second; + std::vector new_vec_by_output; + /* We will preallocate necessary memory addresses, so we don't need + to reallocate them later. This would save us some performance. */ + new_vec_by_output.reserve(vec.size()); + for (std::vector::iterator i = vec.begin(); + i != vec.end(); ++i) { + /* If pointers from map by input and output are not same, + we will add 'CraftDefinition*' to a new vector. */ + if (def != *i) { + /* Adding dereferenced iterator value (which are + 'CraftDefinition' reference) to a new vector. */ + new_vec_by_output.push_back(*i); + } + } + // Swaps assigned to current key value with new vector for output map. + m_output_craft_definitions[output.item].swap(new_vec_by_output); + } + if (got_hit) + // Swaps value with new vector for input map. + m_craft_defs[(int) CRAFT_HASH_TYPE_UNHASHED][0].swap(new_vec_by_input); + + return got_hit; + } + virtual std::string dump() const { std::ostringstream os(std::ios::binary); diff --git a/src/craftdef.h b/src/craftdef.h index cebb2d7ae..695ee0c2c 100644 --- a/src/craftdef.h +++ b/src/craftdef.h @@ -426,6 +426,10 @@ public: virtual std::vector getCraftRecipes(CraftOutput &output, IGameDef *gamedef, unsigned limit=0) const=0; + virtual bool clearCraftRecipesByOutput(const CraftOutput &output, IGameDef *gamedef) = 0; + virtual bool clearCraftRecipesByInput(CraftMethod craft_method, + unsigned int craft_grid_width, const std::vector &recipe, IGameDef *gamedef) = 0; + // Print crafting recipes for debugging virtual std::string dump() const=0; diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp index 391a0133d..d135c689f 100644 --- a/src/script/lua_api/l_craft.cpp +++ b/src/script/lua_api/l_craft.cpp @@ -34,7 +34,6 @@ struct EnumString ModApiCraft::es_CraftMethod[] = {0, NULL}, }; - // helper for register_craft bool ModApiCraft::readCraftRecipeShaped(lua_State *L, int index, int &width, std::vector &recipe) @@ -281,6 +280,80 @@ int ModApiCraft::l_register_craft(lua_State *L) return 0; /* number of results */ } +// clear_craft({[output=item], [recipe={{item00,item10},{item01,item11}}]) +int ModApiCraft::l_clear_craft(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + luaL_checktype(L, 1, LUA_TTABLE); + int table = 1; + + // Get the writable craft definition manager from the server + IWritableCraftDefManager *craftdef = + getServer(L)->getWritableCraftDefManager(); + + std::string output = getstringfield_default(L, table, "output", ""); + std::string type = getstringfield_default(L, table, "type", "shaped"); + CraftOutput c_output(output, 0); + if (output != "") { + if (craftdef->clearCraftRecipesByOutput(c_output, getServer(L))) + return 0; + else + throw LuaError("No craft recipe known for output" + " (output=\"" + output + "\")"); + } + std::vector recipe; + int width = 0; + CraftMethod method = CRAFT_METHOD_NORMAL; + /* + CraftDefinitionShaped + */ + if (type == "shaped") { + lua_getfield(L, table, "recipe"); + if (lua_isnil(L, -1)) + throw LuaError("Either output or recipe has to be defined"); + if (!readCraftRecipeShaped(L, -1, width, recipe)) + throw LuaError("Invalid crafting recipe"); + } + /* + CraftDefinitionShapeless + */ + else if (type == "shapeless") { + lua_getfield(L, table, "recipe"); + if (lua_isnil(L, -1)) + throw LuaError("Either output or recipe has to be defined"); + if (!readCraftRecipeShapeless(L, -1, recipe)) + throw LuaError("Invalid crafting recipe"); + } + /* + CraftDefinitionCooking + */ + else if (type == "cooking") { + method = CRAFT_METHOD_COOKING; + std::string rec = getstringfield_default(L, table, "recipe", ""); + if (rec == "") + throw LuaError("Crafting definition (cooking)" + " is missing a recipe"); + recipe.push_back(rec); + } + /* + CraftDefinitionFuel + */ + else if (type == "fuel") { + method = CRAFT_METHOD_FUEL; + std::string rec = getstringfield_default(L, table, "recipe", ""); + if (rec == "") + throw LuaError("Crafting definition (fuel)" + " is missing a recipe"); + recipe.push_back(rec); + } else { + throw LuaError("Unknown crafting definition type: \"" + type + "\""); + } + if (!craftdef->clearCraftRecipesByInput(method, width, recipe, getServer(L))) + throw LuaError("No crafting specified for input"); + lua_pop(L, 1); + return 0; +} + // get_craft_result(input) int ModApiCraft::l_get_craft_result(lua_State *L) { @@ -431,4 +504,5 @@ void ModApiCraft::Initialize(lua_State *L, int top) API_FCT(get_craft_recipe); API_FCT(get_craft_result); API_FCT(register_craft); + API_FCT(clear_craft); } diff --git a/src/script/lua_api/l_craft.h b/src/script/lua_api/l_craft.h index 548608776..eb2bce706 100644 --- a/src/script/lua_api/l_craft.h +++ b/src/script/lua_api/l_craft.h @@ -33,6 +33,7 @@ private: static int l_get_craft_recipe(lua_State *L); static int l_get_all_craft_recipes(lua_State *L); static int l_get_craft_result(lua_State *L); + static int l_clear_craft(lua_State *L); static bool readCraftReplacements(lua_State *L, int index, CraftReplacements &replacements); -- cgit v1.2.3 From fca8e53842718f7f23e11587ae6235c072892b97 Mon Sep 17 00:00:00 2001 From: Dorian Wouters Date: Thu, 4 Aug 2016 00:41:54 +0200 Subject: Fix l_request_insecure_environment not ignoring all whitespace (#4395) l_request_insecure_environment didn't ignore all whitespace in the secure.trusted_mods config option. Replaces std::remove with std::remove_if and the isspace function. --- src/script/lua_api/l_util.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/script') diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index d090fc91c..95a5fc4d1 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -446,8 +446,9 @@ int ModApiUtil::l_request_insecure_environment(lua_State *L) // Check secure.trusted_mods const char *mod_name = lua_tostring(L, -1); std::string trusted_mods = g_settings->get("secure.trusted_mods"); - trusted_mods.erase(std::remove(trusted_mods.begin(), - trusted_mods.end(), ' '), trusted_mods.end()); + trusted_mods.erase(std::remove_if(trusted_mods.begin(), + trusted_mods.end(), static_cast(&std::isspace)), + trusted_mods.end()); std::vector mod_list = str_split(trusted_mods, ','); if (std::find(mod_list.begin(), mod_list.end(), mod_name) == mod_list.end()) { -- cgit v1.2.3 From 058a869b70072aba8baea47e359c45e82daaf152 Mon Sep 17 00:00:00 2001 From: Ner'zhul Date: Wed, 10 Aug 2016 12:08:05 +0200 Subject: Permit usage of std::unordered_map & std::unorderered_set on c++11 compilers (#4430) This fallback to std::map & std::set for older compilers Use UNORDERED_SET as an example in decoration and ore biome sets Use UNORDERED_MAP as an example in nameidmapping --- src/mg_decoration.cpp | 2 +- src/mg_decoration.h | 4 ++-- src/mg_ore.cpp | 10 +++++----- src/mg_ore.h | 3 ++- src/nameidmapping.cpp | 2 +- src/nameidmapping.h | 12 ++++++------ src/script/lua_api/l_mapgen.cpp | 4 ++-- src/util/cpp11_container.h | 35 +++++++++++++++++++++++++++++++++++ 8 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 src/util/cpp11_container.h (limited to 'src/script') diff --git a/src/mg_decoration.cpp b/src/mg_decoration.cpp index 70dd9817a..127915b51 100644 --- a/src/mg_decoration.cpp +++ b/src/mg_decoration.cpp @@ -156,7 +156,7 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) } if (mg->biomemap) { - std::set::iterator iter; + UNORDERED_SET::iterator iter; if (!biomes.empty()) { iter = biomes.find(mg->biomemap[mapindex]); diff --git a/src/mg_decoration.h b/src/mg_decoration.h index ba3e9d3b2..da98fd482 100644 --- a/src/mg_decoration.h +++ b/src/mg_decoration.h @@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef MG_DECORATION_HEADER #define MG_DECORATION_HEADER -#include +#include "util/cpp11_container.h" #include "objdef.h" #include "noise.h" #include "nodedef.h" @@ -83,7 +83,7 @@ public: float fill_ratio; NoiseParams np; - std::set biomes; + UNORDERED_SET biomes; //std::list cutoffs; //Mutex cutoff_mutex; }; diff --git a/src/mg_ore.cpp b/src/mg_ore.cpp index 257901614..d840d745a 100644 --- a/src/mg_ore.cpp +++ b/src/mg_ore.cpp @@ -148,7 +148,7 @@ void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed, if (biomemap && !biomes.empty()) { u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X); - std::set::iterator it = biomes.find(biomemap[index]); + UNORDERED_SET::iterator it = biomes.find(biomemap[index]); if (it == biomes.end()) continue; } @@ -202,7 +202,7 @@ void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed, continue; if (biomemap && !biomes.empty()) { - std::set::iterator it = biomes.find(biomemap[index]); + UNORDERED_SET::iterator it = biomes.find(biomemap[index]); if (it == biomes.end()) continue; } @@ -270,7 +270,7 @@ void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed, continue; if (biomemap && !biomes.empty()) { - std::set::iterator it = biomes.find(biomemap[index]); + UNORDERED_SET::iterator it = biomes.find(biomemap[index]); if (it == biomes.end()) continue; } @@ -338,7 +338,7 @@ void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed, if (biomemap && !biomes.empty()) { u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X); - std::set::iterator it = biomes.find(biomemap[bmapidx]); + UNORDERED_SET::iterator it = biomes.find(biomemap[bmapidx]); if (it == biomes.end()) continue; } @@ -422,7 +422,7 @@ void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed, if (biomemap && !biomes.empty()) { u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X); - std::set::iterator it = biomes.find(biomemap[bmapidx]); + UNORDERED_SET::iterator it = biomes.find(biomemap[bmapidx]); if (it == biomes.end()) continue; } diff --git a/src/mg_ore.h b/src/mg_ore.h index 2e065cee3..e95fdd330 100644 --- a/src/mg_ore.h +++ b/src/mg_ore.h @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef MG_ORE_HEADER #define MG_ORE_HEADER +#include "util/cpp11_container.h" #include "objdef.h" #include "noise.h" #include "nodedef.h" @@ -64,7 +65,7 @@ public: float nthresh; // threshold for noise at which an ore is placed NoiseParams np; // noise for distribution of clusters (NULL for uniform scattering) Noise *noise; - std::set biomes; + UNORDERED_SET biomes; Ore(); virtual ~Ore(); diff --git a/src/nameidmapping.cpp b/src/nameidmapping.cpp index ed59ddd16..2af8befff 100644 --- a/src/nameidmapping.cpp +++ b/src/nameidmapping.cpp @@ -25,7 +25,7 @@ void NameIdMapping::serialize(std::ostream &os) const { writeU8(os, 0); // version writeU16(os, m_id_to_name.size()); - for(std::map::const_iterator + for(UNORDERED_MAP::const_iterator i = m_id_to_name.begin(); i != m_id_to_name.end(); ++i){ writeU16(os, i->first); diff --git a/src/nameidmapping.h b/src/nameidmapping.h index 417c441d2..23838c8ff 100644 --- a/src/nameidmapping.h +++ b/src/nameidmapping.h @@ -23,15 +23,15 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include -#include #include "irrlichttypes_bloated.h" +#include "util/cpp11_container.h" class NameIdMapping { public: void serialize(std::ostream &os) const; void deSerialize(std::istream &is); - + void clear(){ m_id_to_name.clear(); m_name_to_id.clear(); @@ -55,7 +55,7 @@ public: m_name_to_id.erase(name); } bool getName(u16 id, std::string &result) const{ - std::map::const_iterator i; + UNORDERED_MAP::const_iterator i; i = m_id_to_name.find(id); if(i == m_id_to_name.end()) return false; @@ -63,7 +63,7 @@ public: return true; } bool getId(const std::string &name, u16 &result) const{ - std::map::const_iterator i; + UNORDERED_MAP::const_iterator i; i = m_name_to_id.find(name); if(i == m_name_to_id.end()) return false; @@ -74,8 +74,8 @@ public: return m_id_to_name.size(); } private: - std::map m_id_to_name; - std::map m_name_to_id; + UNORDERED_MAP m_id_to_name; + UNORDERED_MAP m_name_to_id; }; #endif diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 9f14838ce..9f28231eb 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -100,7 +100,7 @@ Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr); Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef); size_t get_biome_list(lua_State *L, int index, - BiomeManager *biomemgr, std::set *biome_id_list); + BiomeManager *biomemgr, UNORDERED_SET *biome_id_list); Schematic *get_or_load_schematic(lua_State *L, int index, SchematicManager *schemmgr, StringMap *replace_names); @@ -401,7 +401,7 @@ Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef) size_t get_biome_list(lua_State *L, int index, - BiomeManager *biomemgr, std::set *biome_id_list) + BiomeManager *biomemgr, UNORDERED_SET *biome_id_list) { if (index < 0) index = lua_gettop(L) + 1 + index; diff --git a/src/util/cpp11_container.h b/src/util/cpp11_container.h new file mode 100644 index 000000000..f839be19a --- /dev/null +++ b/src/util/cpp11_container.h @@ -0,0 +1,35 @@ +/* +Minetest +Copyright (C) 2016 nerzhul, Loic Blot + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program 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 Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef MT_CPP11CONTAINER_HEADER +#define MT_CPP11CONTAINER_HEADER + +#if __cplusplus >= 201103L + #include + #include + #define UNORDERED_MAP std::unordered_map + #define UNORDERED_SET std::unordered_set +#else + #include + #include + #define UNORDERED_MAP std::map + #define UNORDERED_SET std::set +#endif + +#endif -- cgit v1.2.3 From 4503b5097f99d2806763650f33d8ef3b49f77ce4 Mon Sep 17 00:00:00 2001 From: Rogier-5 Date: Wed, 10 Aug 2016 12:10:00 +0200 Subject: Fixes for compiling with a newer (system) jsoncpp (#4429) * Move included json code to jsoncpp subdirectory This is needed to avoid having to specify the minetest src directory as a system include when fixing the json includes. * Fix json includes They used "", so that the compiler searches the project's directory first. The result was that when compiling with a system jsoncpp, the project's own version of json.h was still included, instead of the system version. The includes now use <>, so a system location, or one specified with '-Ilocation' is searched only. * Fix for jsoncpp deprecated function warning When compiling with a newer version of jsoncpp (and ENABLE_SYSTEM_JSONCPP=true), jsoncpp emits a warning about a deprecated function that minetest uses. --- cmake/Modules/FindJson.cmake | 4 +- src/convert_json.h | 2 +- src/json/CMakeLists.txt | 7 - src/json/UPDATING | 16 - src/json/json.h | 1914 ----------------- src/json/jsoncpp.cpp | 4367 --------------------------------------- src/jsoncpp/json/CMakeLists.txt | 7 + src/jsoncpp/json/UPDATING | 16 + src/jsoncpp/json/json.h | 1914 +++++++++++++++++ src/jsoncpp/json/jsoncpp.cpp | 4367 +++++++++++++++++++++++++++++++++++++++ src/mods.h | 2 +- src/script/common/c_content.cpp | 7 +- src/script/lua_api/l_util.cpp | 2 +- src/serverlist.cpp | 2 +- src/serverlist.h | 2 +- 15 files changed, 6317 insertions(+), 6312 deletions(-) delete mode 100644 src/json/CMakeLists.txt delete mode 100644 src/json/UPDATING delete mode 100644 src/json/json.h delete mode 100644 src/json/jsoncpp.cpp create mode 100644 src/jsoncpp/json/CMakeLists.txt create mode 100644 src/jsoncpp/json/UPDATING create mode 100644 src/jsoncpp/json/json.h create mode 100644 src/jsoncpp/json/jsoncpp.cpp (limited to 'src/script') diff --git a/cmake/Modules/FindJson.cmake b/cmake/Modules/FindJson.cmake index 1558b0fcf..e69d6c4c0 100644 --- a/cmake/Modules/FindJson.cmake +++ b/cmake/Modules/FindJson.cmake @@ -20,8 +20,8 @@ endif() if(NOT JSONCPP_FOUND) message(STATUS "Using bundled JSONCPP library.") - set(JSON_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/json) + set(JSON_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/jsoncpp) set(JSON_LIBRARY jsoncpp) - add_subdirectory(json) + add_subdirectory(jsoncpp/json) endif() diff --git a/src/convert_json.h b/src/convert_json.h index 6732fcfa3..55321af5f 100644 --- a/src/convert_json.h +++ b/src/convert_json.h @@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef __CONVERT_JSON_H__ #define __CONVERT_JSON_H__ -#include "json/json.h" +#include struct ModStoreMod; struct ModStoreModDetails; diff --git a/src/json/CMakeLists.txt b/src/json/CMakeLists.txt deleted file mode 100644 index 9056e4b6d..000000000 --- a/src/json/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -if(MSVC) - set(CMAKE_CXX_FLAGS_RELEASE "/MT /O2 /Ob2 /D NDEBUG") -endif() - -add_library(jsoncpp jsoncpp.cpp) -target_link_libraries(jsoncpp) - diff --git a/src/json/UPDATING b/src/json/UPDATING deleted file mode 100644 index d00076601..000000000 --- a/src/json/UPDATING +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh -cd .. -svn co https://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/jsoncpp jsoncpp -svn up jsoncpp -cd jsoncpp -python amalgamate.py -cp -R dist/json .. -cp dist/jsoncpp.cpp ../json - -# maybe you need to patch: -# src/json/jsoncpp.cpp: -# -#include -# +#include "json/json.h" - -#svn export --force https://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/jsoncpp/src/lib_json json -#svn export --force https://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/jsoncpp/include/json json diff --git a/src/json/json.h b/src/json/json.h deleted file mode 100644 index 396aafa82..000000000 --- a/src/json/json.h +++ /dev/null @@ -1,1914 +0,0 @@ -/// Json-cpp amalgated header (http://jsoncpp.sourceforge.net/). -/// It is intented to be used with #include - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: LICENSE -// ////////////////////////////////////////////////////////////////////// - -/* -The JsonCpp library's source code, including accompanying documentation, -tests and demonstration applications, are licensed under the following -conditions... - -The author (Baptiste Lepilleur) explicitly disclaims copyright in all -jurisdictions which recognize such a disclaimer. In such jurisdictions, -this software is released into the Public Domain. - -In jurisdictions which do not recognize Public Domain property (e.g. Germany as of -2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is -released under the terms of the MIT License (see below). - -In jurisdictions which recognize Public Domain property, the user of this -software may choose to accept it either as 1) Public Domain, 2) under the -conditions of the MIT License (see below), or 3) under the terms of dual -Public Domain/MIT License conditions described here, as they choose. - -The MIT License is about as close to Public Domain as a license can get, and is -described in clear, concise terms at: - - http://en.wikipedia.org/wiki/MIT_License - -The full text of the MIT License follows: - -======================================================================== -Copyright (c) 2007-2010 Baptiste Lepilleur - -Permission is hereby granted, free of charge, to any person -obtaining a copy of this software and associated documentation -files (the "Software"), to deal in the Software without -restriction, including without limitation the rights to use, copy, -modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS -BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -======================================================================== -(END LICENSE TEXT) - -The MIT license is compatible with both the GPL and commercial -software, affording one all of the rights of Public Domain with the -minor nuisance of being required to keep the above copyright notice -and license text in the source code. Note also that by accepting the -Public Domain "license" you can re-license your copy using whatever -license you like. - -*/ - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: LICENSE -// ////////////////////////////////////////////////////////////////////// - - - - - -#ifndef JSON_AMALGATED_H_INCLUDED -# define JSON_AMALGATED_H_INCLUDED -/// If defined, indicates that the source file is amalgated -/// to prevent private header inclusion. -#define JSON_IS_AMALGAMATION - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: include/json/config.h -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#ifndef JSON_CONFIG_H_INCLUDED -# define JSON_CONFIG_H_INCLUDED - -/// If defined, indicates that json library is embedded in CppTL library. -//# define JSON_IN_CPPTL 1 - -/// If defined, indicates that json may leverage CppTL library -//# define JSON_USE_CPPTL 1 -/// If defined, indicates that cpptl vector based map should be used instead of std::map -/// as Value container. -//# define JSON_USE_CPPTL_SMALLMAP 1 -/// If defined, indicates that Json specific container should be used -/// (hash table & simple deque container with customizable allocator). -/// THIS FEATURE IS STILL EXPERIMENTAL! There is know bugs: See #3177332 -//# define JSON_VALUE_USE_INTERNAL_MAP 1 -/// Force usage of standard new/malloc based allocator instead of memory pool based allocator. -/// The memory pools allocator used optimization (initializing Value and ValueInternalLink -/// as if it was a POD) that may cause some validation tool to report errors. -/// Only has effects if JSON_VALUE_USE_INTERNAL_MAP is defined. -//# define JSON_USE_SIMPLE_INTERNAL_ALLOCATOR 1 - -// If non-zero, the library uses exceptions to report bad input instead of C -// assertion macros. The default is to use exceptions. -# ifndef JSON_USE_EXCEPTION -# define JSON_USE_EXCEPTION 1 -# endif - -/// If defined, indicates that the source file is amalgated -/// to prevent private header inclusion. -/// Remarks: it is automatically defined in the generated amalgated header. -// #define JSON_IS_AMALGAMATION - - -# ifdef JSON_IN_CPPTL -# include -# ifndef JSON_USE_CPPTL -# define JSON_USE_CPPTL 1 -# endif -# endif - -# ifdef JSON_IN_CPPTL -# define JSON_API CPPTL_API -# elif defined(JSON_DLL_BUILD) -# define JSON_API __declspec(dllexport) -# elif defined(JSON_DLL) -# define JSON_API __declspec(dllimport) -# else -# define JSON_API -# endif - -// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for integer -// Storages, and 64 bits integer support is disabled. -// #define JSON_NO_INT64 1 - -#if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6 -// Microsoft Visual Studio 6 only support conversion from __int64 to double -// (no conversion from unsigned __int64). -#define JSON_USE_INT64_DOUBLE_CONVERSION 1 -#endif // if defined(_MSC_VER) && _MSC_VER < 1200 // MSVC 6 - -#if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008 -/// Indicates that the following function is deprecated. -# define JSONCPP_DEPRECATED(message) __declspec(deprecated(message)) -#endif - -#if !defined(JSONCPP_DEPRECATED) -# define JSONCPP_DEPRECATED(message) -#endif // if !defined(JSONCPP_DEPRECATED) - -namespace Json { - typedef int Int; - typedef unsigned int UInt; -# if defined(JSON_NO_INT64) - typedef int LargestInt; - typedef unsigned int LargestUInt; -# undef JSON_HAS_INT64 -# else // if defined(JSON_NO_INT64) - // For Microsoft Visual use specific types as long long is not supported -# if defined(_MSC_VER) // Microsoft Visual Studio - typedef __int64 Int64; - typedef unsigned __int64 UInt64; -# else // if defined(_MSC_VER) // Other platforms, use long long - typedef long long int Int64; - typedef unsigned long long int UInt64; -# endif // if defined(_MSC_VER) - typedef Int64 LargestInt; - typedef UInt64 LargestUInt; -# define JSON_HAS_INT64 -# endif // if defined(JSON_NO_INT64) -} // end namespace Json - - -#endif // JSON_CONFIG_H_INCLUDED - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: include/json/config.h -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: include/json/forwards.h -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#ifndef JSON_FORWARDS_H_INCLUDED -# define JSON_FORWARDS_H_INCLUDED - -#if !defined(JSON_IS_AMALGAMATION) -# include "config.h" -#endif // if !defined(JSON_IS_AMALGAMATION) - -namespace Json { - - // writer.h - class FastWriter; - class StyledWriter; - - // reader.h - class Reader; - - // features.h - class Features; - - // value.h - typedef unsigned int ArrayIndex; - class StaticString; - class Path; - class PathArgument; - class Value; - class ValueIteratorBase; - class ValueIterator; - class ValueConstIterator; -#ifdef JSON_VALUE_USE_INTERNAL_MAP - class ValueMapAllocator; - class ValueInternalLink; - class ValueInternalArray; - class ValueInternalMap; -#endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP - -} // namespace Json - - -#endif // JSON_FORWARDS_H_INCLUDED - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: include/json/forwards.h -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: include/json/features.h -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#ifndef CPPTL_JSON_FEATURES_H_INCLUDED -# define CPPTL_JSON_FEATURES_H_INCLUDED - -#if !defined(JSON_IS_AMALGAMATION) -# include "forwards.h" -#endif // if !defined(JSON_IS_AMALGAMATION) - -namespace Json { - - /** \brief Configuration passed to reader and writer. - * This configuration object can be used to force the Reader or Writer - * to behave in a standard conforming way. - */ - class JSON_API Features - { - public: - /** \brief A configuration that allows all features and assumes all strings are UTF-8. - * - C & C++ comments are allowed - * - Root object can be any JSON value - * - Assumes Value strings are encoded in UTF-8 - */ - static Features all(); - - /** \brief A configuration that is strictly compatible with the JSON specification. - * - Comments are forbidden. - * - Root object must be either an array or an object value. - * - Assumes Value strings are encoded in UTF-8 - */ - static Features strictMode(); - - /** \brief Initialize the configuration like JsonConfig::allFeatures; - */ - Features(); - - /// \c true if comments are allowed. Default: \c true. - bool allowComments_; - - /// \c true if root must be either an array or an object value. Default: \c false. - bool strictRoot_; - }; - -} // namespace Json - -#endif // CPPTL_JSON_FEATURES_H_INCLUDED - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: include/json/features.h -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: include/json/value.h -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#ifndef CPPTL_JSON_H_INCLUDED -# define CPPTL_JSON_H_INCLUDED - -#if !defined(JSON_IS_AMALGAMATION) -# include "forwards.h" -#endif // if !defined(JSON_IS_AMALGAMATION) -# include -# include - -# ifndef JSON_USE_CPPTL_SMALLMAP -# include -# else -# include -# endif -# ifdef JSON_USE_CPPTL -# include -# endif - -/** \brief JSON (JavaScript Object Notation). - */ -namespace Json { - - /** \brief Type of the value held by a Value object. - */ - enum ValueType - { - nullValue = 0, ///< 'null' value - intValue, ///< signed integer value - uintValue, ///< unsigned integer value - realValue, ///< double value - stringValue, ///< UTF-8 string value - booleanValue, ///< bool value - arrayValue, ///< array value (ordered list) - objectValue ///< object value (collection of name/value pairs). - }; - - enum CommentPlacement - { - commentBefore = 0, ///< a comment placed on the line before a value - commentAfterOnSameLine, ///< a comment just after a value on the same line - commentAfter, ///< a comment on the line after a value (only make sense for root value) - numberOfCommentPlacement - }; - -//# ifdef JSON_USE_CPPTL -// typedef CppTL::AnyEnumerator EnumMemberNames; -// typedef CppTL::AnyEnumerator EnumValues; -//# endif - - /** \brief Lightweight wrapper to tag static string. - * - * Value constructor and objectValue member assignement takes advantage of the - * StaticString and avoid the cost of string duplication when storing the - * string or the member name. - * - * Example of usage: - * \code - * Json::Value aValue( StaticString("some text") ); - * Json::Value object; - * static const StaticString code("code"); - * object[code] = 1234; - * \endcode - */ - class JSON_API StaticString - { - public: - explicit StaticString( const char *czstring ) - : str_( czstring ) - { - } - - operator const char *() const - { - return str_; - } - - const char *c_str() const - { - return str_; - } - - private: - const char *str_; - }; - - /** \brief Represents a JSON value. - * - * This class is a discriminated union wrapper that can represents a: - * - signed integer [range: Value::minInt - Value::maxInt] - * - unsigned integer (range: 0 - Value::maxUInt) - * - double - * - UTF-8 string - * - boolean - * - 'null' - * - an ordered list of Value - * - collection of name/value pairs (javascript object) - * - * The type of the held value is represented by a #ValueType and - * can be obtained using type(). - * - * values of an #objectValue or #arrayValue can be accessed using operator[]() methods. - * Non const methods will automatically create the a #nullValue element - * if it does not exist. - * The sequence of an #arrayValue will be automatically resize and initialized - * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. - * - * The get() methods can be used to obtanis default value in the case the required element - * does not exist. - * - * It is possible to iterate over the list of a #objectValue values using - * the getMemberNames() method. - */ - class JSON_API Value - { - friend class ValueIteratorBase; -# ifdef JSON_VALUE_USE_INTERNAL_MAP - friend class ValueInternalLink; - friend class ValueInternalMap; -# endif - public: - typedef std::vector Members; - typedef ValueIterator iterator; - typedef ValueConstIterator const_iterator; - typedef Json::UInt UInt; - typedef Json::Int Int; -# if defined(JSON_HAS_INT64) - typedef Json::UInt64 UInt64; - typedef Json::Int64 Int64; -#endif // defined(JSON_HAS_INT64) - typedef Json::LargestInt LargestInt; - typedef Json::LargestUInt LargestUInt; - typedef Json::ArrayIndex ArrayIndex; - - static const Value null; - /// Minimum signed integer value that can be stored in a Json::Value. - static const LargestInt minLargestInt; - /// Maximum signed integer value that can be stored in a Json::Value. - static const LargestInt maxLargestInt; - /// Maximum unsigned integer value that can be stored in a Json::Value. - static const LargestUInt maxLargestUInt; - - /// Minimum signed int value that can be stored in a Json::Value. - static const Int minInt; - /// Maximum signed int value that can be stored in a Json::Value. - static const Int maxInt; - /// Maximum unsigned int value that can be stored in a Json::Value. - static const UInt maxUInt; - -# if defined(JSON_HAS_INT64) - /// Minimum signed 64 bits int value that can be stored in a Json::Value. - static const Int64 minInt64; - /// Maximum signed 64 bits int value that can be stored in a Json::Value. - static const Int64 maxInt64; - /// Maximum unsigned 64 bits int value that can be stored in a Json::Value. - static const UInt64 maxUInt64; -#endif // defined(JSON_HAS_INT64) - - private: -#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION -# ifndef JSON_VALUE_USE_INTERNAL_MAP - class CZString - { - public: - enum DuplicationPolicy - { - noDuplication = 0, - duplicate, - duplicateOnCopy - }; - CZString( ArrayIndex index ); - CZString( const char *cstr, DuplicationPolicy allocate ); - CZString( const CZString &other ); - ~CZString(); - CZString &operator =( const CZString &other ); - bool operator<( const CZString &other ) const; - bool operator==( const CZString &other ) const; - ArrayIndex index() const; - const char *c_str() const; - bool isStaticString() const; - private: - void swap( CZString &other ); - const char *cstr_; - ArrayIndex index_; - }; - - public: -# ifndef JSON_USE_CPPTL_SMALLMAP - typedef std::map ObjectValues; -# else - typedef CppTL::SmallMap ObjectValues; -# endif // ifndef JSON_USE_CPPTL_SMALLMAP -# endif // ifndef JSON_VALUE_USE_INTERNAL_MAP -#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION - - public: - /** \brief Create a default Value of the given type. - - This is a very useful constructor. - To create an empty array, pass arrayValue. - To create an empty object, pass objectValue. - Another Value can then be set to this one by assignment. - This is useful since clear() and resize() will not alter types. - - Examples: - \code - Json::Value null_value; // null - Json::Value arr_value(Json::arrayValue); // [] - Json::Value obj_value(Json::objectValue); // {} - \endcode - */ - Value( ValueType type = nullValue ); - Value( Int value ); - Value( UInt value ); -#if defined(JSON_HAS_INT64) - Value( Int64 value ); - Value( UInt64 value ); -#endif // if defined(JSON_HAS_INT64) - Value( double value ); - Value( const char *value ); - Value( const char *beginValue, const char *endValue ); - /** \brief Constructs a value from a static string. - - * Like other value string constructor but do not duplicate the string for - * internal storage. The given string must remain alive after the call to this - * constructor. - * Example of usage: - * \code - * Json::Value aValue( StaticString("some text") ); - * \endcode - */ - Value( const StaticString &value ); - Value( const std::string &value ); -# ifdef JSON_USE_CPPTL - Value( const CppTL::ConstString &value ); -# endif - Value( bool value ); - Value( const Value &other ); - ~Value(); - - Value &operator=( const Value &other ); - /// Swap values. - /// \note Currently, comments are intentionally not swapped, for - /// both logic and efficiency. - void swap( Value &other ); - - ValueType type() const; - - bool operator <( const Value &other ) const; - bool operator <=( const Value &other ) const; - bool operator >=( const Value &other ) const; - bool operator >( const Value &other ) const; - - bool operator ==( const Value &other ) const; - bool operator !=( const Value &other ) const; - - int compare( const Value &other ) const; - - const char *asCString() const; - std::string asString() const; -# ifdef JSON_USE_CPPTL - CppTL::ConstString asConstString() const; -# endif - Int asInt() const; - UInt asUInt() const; -#if defined(JSON_HAS_INT64) - Int64 asInt64() const; - UInt64 asUInt64() const; -#endif // if defined(JSON_HAS_INT64) - LargestInt asLargestInt() const; - LargestUInt asLargestUInt() const; - float asFloat() const; - double asDouble() const; - bool asBool() const; - - bool isNull() const; - bool isBool() const; - bool isInt() const; - bool isInt64() const; - bool isUInt() const; - bool isUInt64() const; - bool isIntegral() const; - bool isDouble() const; - bool isNumeric() const; - bool isString() const; - bool isArray() const; - bool isObject() const; - - bool isConvertibleTo( ValueType other ) const; - - /// Number of values in array or object - ArrayIndex size() const; - - /// \brief Return true if empty array, empty object, or null; - /// otherwise, false. - bool empty() const; - - /// Return isNull() - bool operator!() const; - - /// Remove all object members and array elements. - /// \pre type() is arrayValue, objectValue, or nullValue - /// \post type() is unchanged - void clear(); - - /// Resize the array to size elements. - /// New elements are initialized to null. - /// May only be called on nullValue or arrayValue. - /// \pre type() is arrayValue or nullValue - /// \post type() is arrayValue - void resize( ArrayIndex size ); - - /// Access an array element (zero based index ). - /// If the array contains less than index element, then null value are inserted - /// in the array so that its size is index+1. - /// (You may need to say 'value[0u]' to get your compiler to distinguish - /// this from the operator[] which takes a string.) - Value &operator[]( ArrayIndex index ); - - /// Access an array element (zero based index ). - /// If the array contains less than index element, then null value are inserted - /// in the array so that its size is index+1. - /// (You may need to say 'value[0u]' to get your compiler to distinguish - /// this from the operator[] which takes a string.) - Value &operator[]( int index ); - - /// Access an array element (zero based index ) - /// (You may need to say 'value[0u]' to get your compiler to distinguish - /// this from the operator[] which takes a string.) - const Value &operator[]( ArrayIndex index ) const; - - /// Access an array element (zero based index ) - /// (You may need to say 'value[0u]' to get your compiler to distinguish - /// this from the operator[] which takes a string.) - const Value &operator[]( int index ) const; - - /// If the array contains at least index+1 elements, returns the element value, - /// otherwise returns defaultValue. - Value get( ArrayIndex index, - const Value &defaultValue ) const; - /// Return true if index < size(). - bool isValidIndex( ArrayIndex index ) const; - /// \brief Append value to array at the end. - /// - /// Equivalent to jsonvalue[jsonvalue.size()] = value; - Value &append( const Value &value ); - - /// Access an object value by name, create a null member if it does not exist. - Value &operator[]( const char *key ); - /// Access an object value by name, returns null if there is no member with that name. - const Value &operator[]( const char *key ) const; - /// Access an object value by name, create a null member if it does not exist. - Value &operator[]( const std::string &key ); - /// Access an object value by name, returns null if there is no member with that name. - const Value &operator[]( const std::string &key ) const; - /** \brief Access an object value by name, create a null member if it does not exist. - - * If the object as no entry for that name, then the member name used to store - * the new entry is not duplicated. - * Example of use: - * \code - * Json::Value object; - * static const StaticString code("code"); - * object[code] = 1234; - * \endcode - */ - Value &operator[]( const StaticString &key ); -# ifdef JSON_USE_CPPTL - /// Access an object value by name, create a null member if it does not exist. - Value &operator[]( const CppTL::ConstString &key ); - /// Access an object value by name, returns null if there is no member with that name. - const Value &operator[]( const CppTL::ConstString &key ) const; -# endif - /// Return the member named key if it exist, defaultValue otherwise. - Value get( const char *key, - const Value &defaultValue ) const; - /// Return the member named key if it exist, defaultValue otherwise. - Value get( const std::string &key, - const Value &defaultValue ) const; -# ifdef JSON_USE_CPPTL - /// Return the member named key if it exist, defaultValue otherwise. - Value get( const CppTL::ConstString &key, - const Value &defaultValue ) const; -# endif - /// \brief Remove and return the named member. - /// - /// Do nothing if it did not exist. - /// \return the removed Value, or null. - /// \pre type() is objectValue or nullValue - /// \post type() is unchanged - Value removeMember( const char* key ); - /// Same as removeMember(const char*) - Value removeMember( const std::string &key ); - - /// Return true if the object has a member named key. - bool isMember( const char *key ) const; - /// Return true if the object has a member named key. - bool isMember( const std::string &key ) const; -# ifdef JSON_USE_CPPTL - /// Return true if the object has a member named key. - bool isMember( const CppTL::ConstString &key ) const; -# endif - - /// \brief Return a list of the member names. - /// - /// If null, return an empty list. - /// \pre type() is objectValue or nullValue - /// \post if type() was nullValue, it remains nullValue - Members getMemberNames() const; - -//# ifdef JSON_USE_CPPTL -// EnumMemberNames enumMemberNames() const; -// EnumValues enumValues() const; -//# endif - - /// Comments must be //... or /* ... */ - void setComment( const char *comment, - CommentPlacement placement ); - /// Comments must be //... or /* ... */ - void setComment( const std::string &comment, - CommentPlacement placement ); - bool hasComment( CommentPlacement placement ) const; - /// Include delimiters and embedded newlines. - std::string getComment( CommentPlacement placement ) const; - - std::string toStyledString() const; - - const_iterator begin() const; - const_iterator end() const; - - iterator begin(); - iterator end(); - - private: - Value &resolveReference( const char *key, - bool isStatic ); - -# ifdef JSON_VALUE_USE_INTERNAL_MAP - inline bool isItemAvailable() const - { - return itemIsUsed_ == 0; - } - - inline void setItemUsed( bool isUsed = true ) - { - itemIsUsed_ = isUsed ? 1 : 0; - } - - inline bool isMemberNameStatic() const - { - return memberNameIsStatic_ == 0; - } - - inline void setMemberNameIsStatic( bool isStatic ) - { - memberNameIsStatic_ = isStatic ? 1 : 0; - } -# endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP - - private: - struct CommentInfo - { - CommentInfo(); - ~CommentInfo(); - - void setComment( const char *text ); - - char *comment_; - }; - - //struct MemberNamesTransform - //{ - // typedef const char *result_type; - // const char *operator()( const CZString &name ) const - // { - // return name.c_str(); - // } - //}; - - union ValueHolder - { - LargestInt int_; - LargestUInt uint_; - double real_; - bool bool_; - char *string_; -# ifdef JSON_VALUE_USE_INTERNAL_MAP - ValueInternalArray *array_; - ValueInternalMap *map_; -#else - ObjectValues *map_; -# endif - } value_; - ValueType type_ : 8; - int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. -# ifdef JSON_VALUE_USE_INTERNAL_MAP - unsigned int itemIsUsed_ : 1; // used by the ValueInternalMap container. - int memberNameIsStatic_ : 1; // used by the ValueInternalMap container. -# endif - CommentInfo *comments_; - }; - - - /** \brief Experimental and untested: represents an element of the "path" to access a node. - */ - class PathArgument - { - public: - friend class Path; - - PathArgument(); - PathArgument( ArrayIndex index ); - PathArgument( const char *key ); - PathArgument( const std::string &key ); - - private: - enum Kind - { - kindNone = 0, - kindIndex, - kindKey - }; - std::string key_; - ArrayIndex index_; - Kind kind_; - }; - - /** \brief Experimental and untested: represents a "path" to access a node. - * - * Syntax: - * - "." => root node - * - ".[n]" => elements at index 'n' of root node (an array value) - * - ".name" => member named 'name' of root node (an object value) - * - ".name1.name2.name3" - * - ".[0][1][2].name1[3]" - * - ".%" => member name is provided as parameter - * - ".[%]" => index is provied as parameter - */ - class Path - { - public: - Path( const std::string &path, - const PathArgument &a1 = PathArgument(), - const PathArgument &a2 = PathArgument(), - const PathArgument &a3 = PathArgument(), - const PathArgument &a4 = PathArgument(), - const PathArgument &a5 = PathArgument() ); - - const Value &resolve( const Value &root ) const; - Value resolve( const Value &root, - const Value &defaultValue ) const; - /// Creates the "path" to access the specified node and returns a reference on the node. - Value &make( Value &root ) const; - - private: - typedef std::vector InArgs; - typedef std::vector Args; - - void makePath( const std::string &path, - const InArgs &in ); - void addPathInArg( const std::string &path, - const InArgs &in, - InArgs::const_iterator &itInArg, - PathArgument::Kind kind ); - void invalidPath( const std::string &path, - int location ); - - Args args_; - }; - - - -#ifdef JSON_VALUE_USE_INTERNAL_MAP - /** \brief Allocator to customize Value internal map. - * Below is an example of a simple implementation (default implementation actually - * use memory pool for speed). - * \code - class DefaultValueMapAllocator : public ValueMapAllocator - { - public: // overridden from ValueMapAllocator - virtual ValueInternalMap *newMap() - { - return new ValueInternalMap(); - } - - virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) - { - return new ValueInternalMap( other ); - } - - virtual void destructMap( ValueInternalMap *map ) - { - delete map; - } - - virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) - { - return new ValueInternalLink[size]; - } - - virtual void releaseMapBuckets( ValueInternalLink *links ) - { - delete [] links; - } - - virtual ValueInternalLink *allocateMapLink() - { - return new ValueInternalLink(); - } - - virtual void releaseMapLink( ValueInternalLink *link ) - { - delete link; - } - }; - * \endcode - */ - class JSON_API ValueMapAllocator - { - public: - virtual ~ValueMapAllocator(); - virtual ValueInternalMap *newMap() = 0; - virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) = 0; - virtual void destructMap( ValueInternalMap *map ) = 0; - virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) = 0; - virtual void releaseMapBuckets( ValueInternalLink *links ) = 0; - virtual ValueInternalLink *allocateMapLink() = 0; - virtual void releaseMapLink( ValueInternalLink *link ) = 0; - }; - - /** \brief ValueInternalMap hash-map bucket chain link (for internal use only). - * \internal previous_ & next_ allows for bidirectional traversal. - */ - class JSON_API ValueInternalLink - { - public: - enum { itemPerLink = 6 }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture. - enum InternalFlags { - flagAvailable = 0, - flagUsed = 1 - }; - - ValueInternalLink(); - - ~ValueInternalLink(); - - Value items_[itemPerLink]; - char *keys_[itemPerLink]; - ValueInternalLink *previous_; - ValueInternalLink *next_; - }; - - - /** \brief A linked page based hash-table implementation used internally by Value. - * \internal ValueInternalMap is a tradional bucket based hash-table, with a linked - * list in each bucket to handle collision. There is an addional twist in that - * each node of the collision linked list is a page containing a fixed amount of - * value. This provides a better compromise between memory usage and speed. - * - * Each bucket is made up of a chained list of ValueInternalLink. The last - * link of a given bucket can be found in the 'previous_' field of the following bucket. - * The last link of the last bucket is stored in tailLink_ as it has no following bucket. - * Only the last link of a bucket may contains 'available' item. The last link always - * contains at least one element unless is it the bucket one very first link. - */ - class JSON_API ValueInternalMap - { - friend class ValueIteratorBase; - friend class Value; - public: - typedef unsigned int HashKey; - typedef unsigned int BucketIndex; - -# ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION - struct IteratorState - { - IteratorState() - : map_(0) - , link_(0) - , itemIndex_(0) - , bucketIndex_(0) - { - } - ValueInternalMap *map_; - ValueInternalLink *link_; - BucketIndex itemIndex_; - BucketIndex bucketIndex_; - }; -# endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION - - ValueInternalMap(); - ValueInternalMap( const ValueInternalMap &other ); - ValueInternalMap &operator =( const ValueInternalMap &other ); - ~ValueInternalMap(); - - void swap( ValueInternalMap &other ); - - BucketIndex size() const; - - void clear(); - - bool reserveDelta( BucketIndex growth ); - - bool reserve( BucketIndex newItemCount ); - - const Value *find( const char *key ) const; - - Value *find( const char *key ); - - Value &resolveReference( const char *key, - bool isStatic ); - - void remove( const char *key ); - - void doActualRemove( ValueInternalLink *link, - BucketIndex index, - BucketIndex bucketIndex ); - - ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex ); - - Value &setNewItem( const char *key, - bool isStatic, - ValueInternalLink *link, - BucketIndex index ); - - Value &unsafeAdd( const char *key, - bool isStatic, - HashKey hashedKey ); - - HashKey hash( const char *key ) const; - - int compare( const ValueInternalMap &other ) const; - - private: - void makeBeginIterator( IteratorState &it ) const; - void makeEndIterator( IteratorState &it ) const; - static bool equals( const IteratorState &x, const IteratorState &other ); - static void increment( IteratorState &iterator ); - static void incrementBucket( IteratorState &iterator ); - static void decrement( IteratorState &iterator ); - static const char *key( const IteratorState &iterator ); - static const char *key( const IteratorState &iterator, bool &isStatic ); - static Value &value( const IteratorState &iterator ); - static int distance( const IteratorState &x, const IteratorState &y ); - - private: - ValueInternalLink *buckets_; - ValueInternalLink *tailLink_; - BucketIndex bucketsSize_; - BucketIndex itemCount_; - }; - - /** \brief A simplified deque implementation used internally by Value. - * \internal - * It is based on a list of fixed "page", each page contains a fixed number of items. - * Instead of using a linked-list, a array of pointer is used for fast item look-up. - * Look-up for an element is as follow: - * - compute page index: pageIndex = itemIndex / itemsPerPage - * - look-up item in page: pages_[pageIndex][itemIndex % itemsPerPage] - * - * Insertion is amortized constant time (only the array containing the index of pointers - * need to be reallocated when items are appended). - */ - class JSON_API ValueInternalArray - { - friend class Value; - friend class ValueIteratorBase; - public: - enum { itemsPerPage = 8 }; // should be a power of 2 for fast divide and modulo. - typedef Value::ArrayIndex ArrayIndex; - typedef unsigned int PageIndex; - -# ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION - struct IteratorState // Must be a POD - { - IteratorState() - : array_(0) - , currentPageIndex_(0) - , currentItemIndex_(0) - { - } - ValueInternalArray *array_; - Value **currentPageIndex_; - unsigned int currentItemIndex_; - }; -# endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION - - ValueInternalArray(); - ValueInternalArray( const ValueInternalArray &other ); - ValueInternalArray &operator =( const ValueInternalArray &other ); - ~ValueInternalArray(); - void swap( ValueInternalArray &other ); - - void clear(); - void resize( ArrayIndex newSize ); - - Value &resolveReference( ArrayIndex index ); - - Value *find( ArrayIndex index ) const; - - ArrayIndex size() const; - - int compare( const ValueInternalArray &other ) const; - - private: - static bool equals( const IteratorState &x, const IteratorState &other ); - static void increment( IteratorState &iterator ); - static void decrement( IteratorState &iterator ); - static Value &dereference( const IteratorState &iterator ); - static Value &unsafeDereference( const IteratorState &iterator ); - static int distance( const IteratorState &x, const IteratorState &y ); - static ArrayIndex indexOf( const IteratorState &iterator ); - void makeBeginIterator( IteratorState &it ) const; - void makeEndIterator( IteratorState &it ) const; - void makeIterator( IteratorState &it, ArrayIndex index ) const; - - void makeIndexValid( ArrayIndex index ); - - Value **pages_; - ArrayIndex size_; - PageIndex pageCount_; - }; - - /** \brief Experimental: do not use. Allocator to customize Value internal array. - * Below is an example of a simple implementation (actual implementation use - * memory pool). - \code -class DefaultValueArrayAllocator : public ValueArrayAllocator -{ -public: // overridden from ValueArrayAllocator - virtual ~DefaultValueArrayAllocator() - { - } - - virtual ValueInternalArray *newArray() - { - return new ValueInternalArray(); - } - - virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) - { - return new ValueInternalArray( other ); - } - - virtual void destruct( ValueInternalArray *array ) - { - delete array; - } - - virtual void reallocateArrayPageIndex( Value **&indexes, - ValueInternalArray::PageIndex &indexCount, - ValueInternalArray::PageIndex minNewIndexCount ) - { - ValueInternalArray::PageIndex newIndexCount = (indexCount*3)/2 + 1; - if ( minNewIndexCount > newIndexCount ) - newIndexCount = minNewIndexCount; - void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount ); - if ( !newIndexes ) - throw std::bad_alloc(); - indexCount = newIndexCount; - indexes = static_cast( newIndexes ); - } - virtual void releaseArrayPageIndex( Value **indexes, - ValueInternalArray::PageIndex indexCount ) - { - if ( indexes ) - free( indexes ); - } - - virtual Value *allocateArrayPage() - { - return static_cast( malloc( sizeof(Value) * ValueInternalArray::itemsPerPage ) ); - } - - virtual void releaseArrayPage( Value *value ) - { - if ( value ) - free( value ); - } -}; - \endcode - */ - class JSON_API ValueArrayAllocator - { - public: - virtual ~ValueArrayAllocator(); - virtual ValueInternalArray *newArray() = 0; - virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) = 0; - virtual void destructArray( ValueInternalArray *array ) = 0; - /** \brief Reallocate array page index. - * Reallocates an array of pointer on each page. - * \param indexes [input] pointer on the current index. May be \c NULL. - * [output] pointer on the new index of at least - * \a minNewIndexCount pages. - * \param indexCount [input] current number of pages in the index. - * [output] number of page the reallocated index can handle. - * \b MUST be >= \a minNewIndexCount. - * \param minNewIndexCount Minimum number of page the new index must be able to - * handle. - */ - virtual void reallocateArrayPageIndex( Value **&indexes, - ValueInternalArray::PageIndex &indexCount, - ValueInternalArray::PageIndex minNewIndexCount ) = 0; - virtual void releaseArrayPageIndex( Value **indexes, - ValueInternalArray::PageIndex indexCount ) = 0; - virtual Value *allocateArrayPage() = 0; - virtual void releaseArrayPage( Value *value ) = 0; - }; -#endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP - - - /** \brief base class for Value iterators. - * - */ - class ValueIteratorBase - { - public: - typedef unsigned int size_t; - typedef int difference_type; - typedef ValueIteratorBase SelfType; - - ValueIteratorBase(); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - explicit ValueIteratorBase( const Value::ObjectValues::iterator ¤t ); -#else - ValueIteratorBase( const ValueInternalArray::IteratorState &state ); - ValueIteratorBase( const ValueInternalMap::IteratorState &state ); -#endif - - bool operator ==( const SelfType &other ) const - { - return isEqual( other ); - } - - bool operator !=( const SelfType &other ) const - { - return !isEqual( other ); - } - - difference_type operator -( const SelfType &other ) const - { - return computeDistance( other ); - } - - /// Return either the index or the member name of the referenced value as a Value. - Value key() const; - - /// Return the index of the referenced Value. -1 if it is not an arrayValue. - UInt index() const; - - /// Return the member name of the referenced Value. "" if it is not an objectValue. - const char *memberName() const; - - protected: - Value &deref() const; - - void increment(); - - void decrement(); - - difference_type computeDistance( const SelfType &other ) const; - - bool isEqual( const SelfType &other ) const; - - void copy( const SelfType &other ); - - private: -#ifndef JSON_VALUE_USE_INTERNAL_MAP - Value::ObjectValues::iterator current_; - // Indicates that iterator is for a null value. - bool isNull_; -#else - union - { - ValueInternalArray::IteratorState array_; - ValueInternalMap::IteratorState map_; - } iterator_; - bool isArray_; -#endif - }; - - /** \brief const iterator for object and array value. - * - */ - class ValueConstIterator : public ValueIteratorBase - { - friend class Value; - public: - typedef unsigned int size_t; - typedef int difference_type; - typedef const Value &reference; - typedef const Value *pointer; - typedef ValueConstIterator SelfType; - - ValueConstIterator(); - private: - /*! \internal Use by Value to create an iterator. - */ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - explicit ValueConstIterator( const Value::ObjectValues::iterator ¤t ); -#else - ValueConstIterator( const ValueInternalArray::IteratorState &state ); - ValueConstIterator( const ValueInternalMap::IteratorState &state ); -#endif - public: - SelfType &operator =( const ValueIteratorBase &other ); - - SelfType operator++( int ) - { - SelfType temp( *this ); - ++*this; - return temp; - } - - SelfType operator--( int ) - { - SelfType temp( *this ); - --*this; - return temp; - } - - SelfType &operator--() - { - decrement(); - return *this; - } - - SelfType &operator++() - { - increment(); - return *this; - } - - reference operator *() const - { - return deref(); - } - }; - - - /** \brief Iterator for object and array value. - */ - class ValueIterator : public ValueIteratorBase - { - friend class Value; - public: - typedef unsigned int size_t; - typedef int difference_type; - typedef Value &reference; - typedef Value *pointer; - typedef ValueIterator SelfType; - - ValueIterator(); - ValueIterator( const ValueConstIterator &other ); - ValueIterator( const ValueIterator &other ); - private: - /*! \internal Use by Value to create an iterator. - */ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - explicit ValueIterator( const Value::ObjectValues::iterator ¤t ); -#else - ValueIterator( const ValueInternalArray::IteratorState &state ); - ValueIterator( const ValueInternalMap::IteratorState &state ); -#endif - public: - - SelfType &operator =( const SelfType &other ); - - SelfType operator++( int ) - { - SelfType temp( *this ); - ++*this; - return temp; - } - - SelfType operator--( int ) - { - SelfType temp( *this ); - --*this; - return temp; - } - - SelfType &operator--() - { - decrement(); - return *this; - } - - SelfType &operator++() - { - increment(); - return *this; - } - - reference operator *() const - { - return deref(); - } - }; - - -} // namespace Json - - -#endif // CPPTL_JSON_H_INCLUDED - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: include/json/value.h -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: include/json/reader.h -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#ifndef CPPTL_JSON_READER_H_INCLUDED -# define CPPTL_JSON_READER_H_INCLUDED - -#if !defined(JSON_IS_AMALGAMATION) -# include "features.h" -# include "value.h" -#endif // if !defined(JSON_IS_AMALGAMATION) -# include -# include -# include - -namespace Json { - - /** \brief Unserialize a JSON document into a Value. - * - */ - class JSON_API Reader - { - public: - typedef char Char; - typedef const Char *Location; - - /** \brief Constructs a Reader allowing all features - * for parsing. - */ - Reader(); - - /** \brief Constructs a Reader allowing the specified feature set - * for parsing. - */ - Reader( const Features &features ); - - /** \brief Read a Value from a JSON document. - * \param document UTF-8 encoded string containing the document to read. - * \param root [out] Contains the root value of the document if it was - * successfully parsed. - * \param collectComments \c true to collect comment and allow writing them back during - * serialization, \c false to discard comments. - * This parameter is ignored if Features::allowComments_ - * is \c false. - * \return \c true if the document was successfully parsed, \c false if an error occurred. - */ - bool parse( const std::string &document, - Value &root, - bool collectComments = true ); - - /** \brief Read a Value from a JSON document. - * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the document to read. - * \param endDoc Pointer on the end of the UTF-8 encoded string of the document to read. - \ Must be >= beginDoc. - * \param root [out] Contains the root value of the document if it was - * successfully parsed. - * \param collectComments \c true to collect comment and allow writing them back during - * serialization, \c false to discard comments. - * This parameter is ignored if Features::allowComments_ - * is \c false. - * \return \c true if the document was successfully parsed, \c false if an error occurred. - */ - bool parse( const char *beginDoc, const char *endDoc, - Value &root, - bool collectComments = true ); - - /// \brief Parse from input stream. - /// \see Json::operator>>(std::istream&, Json::Value&). - bool parse( std::istream &is, - Value &root, - bool collectComments = true ); - - /** \brief Returns a user friendly string that list errors in the parsed document. - * \return Formatted error message with the list of errors with their location in - * the parsed document. An empty string is returned if no error occurred - * during parsing. - * \deprecated Use getFormattedErrorMessages() instead (typo fix). - */ - JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead") - std::string getFormatedErrorMessages() const; - - /** \brief Returns a user friendly string that list errors in the parsed document. - * \return Formatted error message with the list of errors with their location in - * the parsed document. An empty string is returned if no error occurred - * during parsing. - */ - std::string getFormattedErrorMessages() const; - - private: - enum TokenType - { - tokenEndOfStream = 0, - tokenObjectBegin, - tokenObjectEnd, - tokenArrayBegin, - tokenArrayEnd, - tokenString, - tokenNumber, - tokenTrue, - tokenFalse, - tokenNull, - tokenArraySeparator, - tokenMemberSeparator, - tokenComment, - tokenError - }; - - class Token - { - public: - TokenType type_; - Location start_; - Location end_; - }; - - class ErrorInfo - { - public: - Token token_; - std::string message_; - Location extra_; - }; - - typedef std::deque Errors; - - bool expectToken( TokenType type, Token &token, const char *message ); - bool readToken( Token &token ); - void skipSpaces(); - bool match( Location pattern, - int patternLength ); - bool readComment(); - bool readCStyleComment(); - bool readCppStyleComment(); - bool readString(); - void readNumber(); - bool readValue(); - bool readObject( Token &token ); - bool readArray( Token &token ); - bool decodeNumber( Token &token ); - bool decodeString( Token &token ); - bool decodeString( Token &token, std::string &decoded ); - bool decodeDouble( Token &token ); - bool decodeUnicodeCodePoint( Token &token, - Location ¤t, - Location end, - unsigned int &unicode ); - bool decodeUnicodeEscapeSequence( Token &token, - Location ¤t, - Location end, - unsigned int &unicode ); - bool addError( const std::string &message, - Token &token, - Location extra = 0 ); - bool recoverFromError( TokenType skipUntilToken ); - bool addErrorAndRecover( const std::string &message, - Token &token, - TokenType skipUntilToken ); - void skipUntilSpace(); - Value ¤tValue(); - Char getNextChar(); - void getLocationLineAndColumn( Location location, - int &line, - int &column ) const; - std::string getLocationLineAndColumn( Location location ) const; - void addComment( Location begin, - Location end, - CommentPlacement placement ); - void skipCommentTokens( Token &token ); - - typedef std::stack Nodes; - Nodes nodes_; - Errors errors_; - std::string document_; - Location begin_; - Location end_; - Location current_; - Location lastValueEnd_; - Value *lastValue_; - std::string commentsBefore_; - Features features_; - bool collectComments_; - }; - - /** \brief Read from 'sin' into 'root'. - - Always keep comments from the input JSON. - - This can be used to read a file into a particular sub-object. - For example: - \code - Json::Value root; - cin >> root["dir"]["file"]; - cout << root; - \endcode - Result: - \verbatim - { - "dir": { - "file": { - // The input stream JSON would be nested here. - } - } - } - \endverbatim - \throw std::exception on parse error. - \see Json::operator<<() - */ - std::istream& operator>>( std::istream&, Value& ); - -} // namespace Json - -#endif // CPPTL_JSON_READER_H_INCLUDED - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: include/json/reader.h -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: include/json/writer.h -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#ifndef JSON_WRITER_H_INCLUDED -# define JSON_WRITER_H_INCLUDED - -#if !defined(JSON_IS_AMALGAMATION) -# include "value.h" -#endif // if !defined(JSON_IS_AMALGAMATION) -# include -# include - -namespace Json { - - class Value; - - /** \brief Abstract class for writers. - */ - class JSON_API Writer - { - public: - virtual ~Writer(); - - virtual std::string write( const Value &root ) = 0; - }; - - /** \brief Outputs a Value in JSON format without formatting (not human friendly). - * - * The JSON document is written in a single line. It is not intended for 'human' consumption, - * but may be usefull to support feature such as RPC where bandwith is limited. - * \sa Reader, Value - */ - class JSON_API FastWriter : public Writer - { - public: - FastWriter(); - virtual ~FastWriter(){} - - void enableYAMLCompatibility(); - - /** \brief Drop the "null" string from the writer's output for nullValues. - * Strictly speaking, this is not valid JSON. But when the output is being - * fed to a browser's Javascript, it makes for smaller output and the - * browser can handle the output just fine. - */ - void dropNullPlaceholders(); - - public: // overridden from Writer - virtual std::string write( const Value &root ); - - private: - void writeValue( const Value &value ); - - std::string document_; - bool yamlCompatiblityEnabled_; - bool dropNullPlaceholders_; - }; - - /** \brief Writes a Value in JSON format in a human friendly way. - * - * The rules for line break and indent are as follow: - * - Object value: - * - if empty then print {} without indent and line break - * - if not empty the print '{', line break & indent, print one value per line - * and then unindent and line break and print '}'. - * - Array value: - * - if empty then print [] without indent and line break - * - if the array contains no object value, empty array or some other value types, - * and all the values fit on one lines, then print the array on a single line. - * - otherwise, it the values do not fit on one line, or the array contains - * object or non empty array, then print one value per line. - * - * If the Value have comments then they are outputed according to their #CommentPlacement. - * - * \sa Reader, Value, Value::setComment() - */ - class JSON_API StyledWriter: public Writer - { - public: - StyledWriter(); - virtual ~StyledWriter(){} - - public: // overridden from Writer - /** \brief Serialize a Value in JSON format. - * \param root Value to serialize. - * \return String containing the JSON document that represents the root value. - */ - virtual std::string write( const Value &root ); - - private: - void writeValue( const Value &value ); - void writeArrayValue( const Value &value ); - bool isMultineArray( const Value &value ); - void pushValue( const std::string &value ); - void writeIndent(); - void writeWithIndent( const std::string &value ); - void indent(); - void unindent(); - void writeCommentBeforeValue( const Value &root ); - void writeCommentAfterValueOnSameLine( const Value &root ); - bool hasCommentForValue( const Value &value ); - static std::string normalizeEOL( const std::string &text ); - - typedef std::vector ChildValues; - - ChildValues childValues_; - std::string document_; - std::string indentString_; - int rightMargin_; - int indentSize_; - bool addChildValues_; - }; - - /** \brief Writes a Value in JSON format in a human friendly way, - to a stream rather than to a string. - * - * The rules for line break and indent are as follow: - * - Object value: - * - if empty then print {} without indent and line break - * - if not empty the print '{', line break & indent, print one value per line - * and then unindent and line break and print '}'. - * - Array value: - * - if empty then print [] without indent and line break - * - if the array contains no object value, empty array or some other value types, - * and all the values fit on one lines, then print the array on a single line. - * - otherwise, it the values do not fit on one line, or the array contains - * object or non empty array, then print one value per line. - * - * If the Value have comments then they are outputed according to their #CommentPlacement. - * - * \param indentation Each level will be indented by this amount extra. - * \sa Reader, Value, Value::setComment() - */ - class JSON_API StyledStreamWriter - { - public: - StyledStreamWriter( std::string indentation="\t" ); - ~StyledStreamWriter(){} - - public: - /** \brief Serialize a Value in JSON format. - * \param out Stream to write to. (Can be ostringstream, e.g.) - * \param root Value to serialize. - * \note There is no point in deriving from Writer, since write() should not return a value. - */ - void write( std::ostream &out, const Value &root ); - - private: - void writeValue( const Value &value ); - void writeArrayValue( const Value &value ); - bool isMultineArray( const Value &value ); - void pushValue( const std::string &value ); - void writeIndent(); - void writeWithIndent( const std::string &value ); - void indent(); - void unindent(); - void writeCommentBeforeValue( const Value &root ); - void writeCommentAfterValueOnSameLine( const Value &root ); - bool hasCommentForValue( const Value &value ); - static std::string normalizeEOL( const std::string &text ); - - typedef std::vector ChildValues; - - ChildValues childValues_; - std::ostream* document_; - std::string indentString_; - int rightMargin_; - std::string indentation_; - bool addChildValues_; - }; - -# if defined(JSON_HAS_INT64) - std::string JSON_API valueToString( Int value ); - std::string JSON_API valueToString( UInt value ); -# endif // if defined(JSON_HAS_INT64) - std::string JSON_API valueToString( LargestInt value ); - std::string JSON_API valueToString( LargestUInt value ); - std::string JSON_API valueToString( double value ); - std::string JSON_API valueToString( bool value ); - std::string JSON_API valueToQuotedString( const char *value ); - - /// \brief Output using the StyledStreamWriter. - /// \see Json::operator>>() - std::ostream& operator<<( std::ostream&, const Value &root ); - -} // namespace Json - - - -#endif // JSON_WRITER_H_INCLUDED - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: include/json/writer.h -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: include/json/assertions.h -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#ifndef CPPTL_JSON_ASSERTIONS_H_INCLUDED -# define CPPTL_JSON_ASSERTIONS_H_INCLUDED - -#include - -#if !defined(JSON_IS_AMALGAMATION) -# include -#endif // if !defined(JSON_IS_AMALGAMATION) - -#if JSON_USE_EXCEPTION -#define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw -#define JSON_FAIL_MESSAGE( message ) throw std::runtime_error( message ); -#else // JSON_USE_EXCEPTION -#define JSON_ASSERT( condition ) assert( condition ); - -// The call to assert() will show the failure message in debug builds. In -// release bugs we write to invalid memory in order to crash hard, so that a -// debugger or crash reporter gets the chance to take over. We still call exit() -// afterward in order to tell the compiler that this macro doesn't return. -#define JSON_FAIL_MESSAGE( message ) { assert(false && message); strcpy(reinterpret_cast(666), message); exit(123); } - -#endif - -#define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) { JSON_FAIL_MESSAGE( message ) } - -#endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: include/json/assertions.h -// ////////////////////////////////////////////////////////////////////// - - - - - -#endif //ifndef JSON_AMALGATED_H_INCLUDED diff --git a/src/json/jsoncpp.cpp b/src/json/jsoncpp.cpp deleted file mode 100644 index 7a04736de..000000000 --- a/src/json/jsoncpp.cpp +++ /dev/null @@ -1,4367 +0,0 @@ -/// Json-cpp amalgated source (http://jsoncpp.sourceforge.net/). -/// It is intented to be used with #include - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: LICENSE -// ////////////////////////////////////////////////////////////////////// - -/* -The JsonCpp library's source code, including accompanying documentation, -tests and demonstration applications, are licensed under the following -conditions... - -The author (Baptiste Lepilleur) explicitly disclaims copyright in all -jurisdictions which recognize such a disclaimer. In such jurisdictions, -this software is released into the Public Domain. - -In jurisdictions which do not recognize Public Domain property (e.g. Germany as of -2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is -released under the terms of the MIT License (see below). - -In jurisdictions which recognize Public Domain property, the user of this -software may choose to accept it either as 1) Public Domain, 2) under the -conditions of the MIT License (see below), or 3) under the terms of dual -Public Domain/MIT License conditions described here, as they choose. - -The MIT License is about as close to Public Domain as a license can get, and is -described in clear, concise terms at: - - http://en.wikipedia.org/wiki/MIT_License - -The full text of the MIT License follows: - -======================================================================== -Copyright (c) 2007-2010 Baptiste Lepilleur - -Permission is hereby granted, free of charge, to any person -obtaining a copy of this software and associated documentation -files (the "Software"), to deal in the Software without -restriction, including without limitation the rights to use, copy, -modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS -BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -======================================================================== -(END LICENSE TEXT) - -The MIT license is compatible with both the GPL and commercial -software, affording one all of the rights of Public Domain with the -minor nuisance of being required to keep the above copyright notice -and license text in the source code. Note also that by accepting the -Public Domain "license" you can re-license your copy using whatever -license you like. - -*/ - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: LICENSE -// ////////////////////////////////////////////////////////////////////// - - - - - - -#include "json.h" - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: src/lib_json/json_tool.h -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED -# define LIB_JSONCPP_JSON_TOOL_H_INCLUDED - -/* This header provides common string manipulation support, such as UTF-8, - * portable conversion from/to string... - * - * It is an internal header that must not be exposed. - */ - -namespace Json { - -/// Converts a unicode code-point to UTF-8. -static inline std::string -codePointToUTF8(unsigned int cp) -{ - std::string result; - - // based on description from http://en.wikipedia.org/wiki/UTF-8 - - if (cp <= 0x7f) - { - result.resize(1); - result[0] = static_cast(cp); - } - else if (cp <= 0x7FF) - { - result.resize(2); - result[1] = static_cast(0x80 | (0x3f & cp)); - result[0] = static_cast(0xC0 | (0x1f & (cp >> 6))); - } - else if (cp <= 0xFFFF) - { - result.resize(3); - result[2] = static_cast(0x80 | (0x3f & cp)); - result[1] = 0x80 | static_cast((0x3f & (cp >> 6))); - result[0] = 0xE0 | static_cast((0xf & (cp >> 12))); - } - else if (cp <= 0x10FFFF) - { - result.resize(4); - result[3] = static_cast(0x80 | (0x3f & cp)); - result[2] = static_cast(0x80 | (0x3f & (cp >> 6))); - result[1] = static_cast(0x80 | (0x3f & (cp >> 12))); - result[0] = static_cast(0xF0 | (0x7 & (cp >> 18))); - } - - return result; -} - - -/// Returns true if ch is a control character (in range [0,32[). -static inline bool -isControlCharacter(char ch) -{ - return ch > 0 && ch <= 0x1F; -} - - -enum { - /// Constant that specify the size of the buffer that must be passed to uintToString. - uintToStringBufferSize = 3*sizeof(LargestUInt)+1 -}; - -// Defines a char buffer for use with uintToString(). -typedef char UIntToStringBuffer[uintToStringBufferSize]; - - -/** Converts an unsigned integer to string. - * @param value Unsigned interger to convert to string - * @param current Input/Output string buffer. - * Must have at least uintToStringBufferSize chars free. - */ -static inline void -uintToString( LargestUInt value, - char *¤t ) -{ - *--current = 0; - do - { - *--current = char(value % 10) + '0'; - value /= 10; - } - while ( value != 0 ); -} - -} // namespace Json { - -#endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: src/lib_json/json_tool.h -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: src/lib_json/json_reader.cpp -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2011 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#if !defined(JSON_IS_AMALGAMATION) -# include -# include -# include -# include "json_tool.h" -#endif // if !defined(JSON_IS_AMALGAMATION) -#include -#include -#include -#include -#include - -#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0 -#pragma warning( disable : 4996 ) // disable warning about strdup being deprecated. -#endif - -namespace Json { - -// Implementation of class Features -// //////////////////////////////// - -Features::Features() - : allowComments_( true ) - , strictRoot_( false ) -{ -} - - -Features -Features::all() -{ - return Features(); -} - - -Features -Features::strictMode() -{ - Features features; - features.allowComments_ = false; - features.strictRoot_ = true; - return features; -} - -// Implementation of class Reader -// //////////////////////////////// - - -static inline bool -in( Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4 ) -{ - return c == c1 || c == c2 || c == c3 || c == c4; -} - -static inline bool -in( Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4, Reader::Char c5 ) -{ - return c == c1 || c == c2 || c == c3 || c == c4 || c == c5; -} - - -static bool -containsNewLine( Reader::Location begin, - Reader::Location end ) -{ - for ( ;begin < end; ++begin ) - if ( *begin == '\n' || *begin == '\r' ) - return true; - return false; -} - - -// Class Reader -// ////////////////////////////////////////////////////////////////// - -Reader::Reader() - : errors_(), - document_(), - begin_(), - end_(), - current_(), - lastValueEnd_(), - lastValue_(), - commentsBefore_(), - features_( Features::all() ), - collectComments_() -{ -} - - -Reader::Reader( const Features &features ) - : errors_(), - document_(), - begin_(), - end_(), - current_(), - lastValueEnd_(), - lastValue_(), - commentsBefore_(), - features_( features ), - collectComments_() -{ -} - - -bool -Reader::parse( const std::string &document, - Value &root, - bool collectComments ) -{ - document_ = document; - const char *begin = document_.c_str(); - const char *end = begin + document_.length(); - return parse( begin, end, root, collectComments ); -} - - -bool -Reader::parse( std::istream& sin, - Value &root, - bool collectComments ) -{ - //std::istream_iterator begin(sin); - //std::istream_iterator end; - // Those would allow streamed input from a file, if parse() were a - // template function. - - // Since std::string is reference-counted, this at least does not - // create an extra copy. - std::string doc; - std::getline(sin, doc, (char)EOF); - return parse( doc, root, collectComments ); -} - -bool -Reader::parse( const char *beginDoc, const char *endDoc, - Value &root, - bool collectComments ) -{ - if ( !features_.allowComments_ ) - { - collectComments = false; - } - - begin_ = beginDoc; - end_ = endDoc; - collectComments_ = collectComments; - current_ = begin_; - lastValueEnd_ = 0; - lastValue_ = 0; - commentsBefore_ = ""; - errors_.clear(); - while ( !nodes_.empty() ) - nodes_.pop(); - nodes_.push( &root ); - - bool successful = readValue(); - Token token; - skipCommentTokens( token ); - if ( collectComments_ && !commentsBefore_.empty() ) - root.setComment( commentsBefore_, commentAfter ); - if ( features_.strictRoot_ ) - { - if ( !root.isArray() && !root.isObject() ) - { - // Set error location to start of doc, ideally should be first token found in doc - token.type_ = tokenError; - token.start_ = beginDoc; - token.end_ = endDoc; - addError( "A valid JSON document must be either an array or an object value.", - token ); - return false; - } - } - return successful; -} - - -bool -Reader::readValue() -{ - Token token; - skipCommentTokens( token ); - bool successful = true; - - if ( collectComments_ && !commentsBefore_.empty() ) - { - currentValue().setComment( commentsBefore_, commentBefore ); - commentsBefore_ = ""; - } - - - switch ( token.type_ ) - { - case tokenObjectBegin: - successful = readObject( token ); - break; - case tokenArrayBegin: - successful = readArray( token ); - break; - case tokenNumber: - successful = decodeNumber( token ); - break; - case tokenString: - successful = decodeString( token ); - break; - case tokenTrue: - currentValue() = true; - break; - case tokenFalse: - currentValue() = false; - break; - case tokenNull: - currentValue() = Value(); - break; - default: - return addError( "Syntax error: value, object or array expected.", token ); - } - - if ( collectComments_ ) - { - lastValueEnd_ = current_; - lastValue_ = ¤tValue(); - } - - return successful; -} - - -void -Reader::skipCommentTokens( Token &token ) -{ - if ( features_.allowComments_ ) - { - do - { - readToken( token ); - } - while ( token.type_ == tokenComment ); - } - else - { - readToken( token ); - } -} - - -bool -Reader::expectToken( TokenType type, Token &token, const char *message ) -{ - readToken( token ); - if ( token.type_ != type ) - return addError( message, token ); - return true; -} - - -bool -Reader::readToken( Token &token ) -{ - skipSpaces(); - token.start_ = current_; - Char c = getNextChar(); - bool ok = true; - switch ( c ) - { - case '{': - token.type_ = tokenObjectBegin; - break; - case '}': - token.type_ = tokenObjectEnd; - break; - case '[': - token.type_ = tokenArrayBegin; - break; - case ']': - token.type_ = tokenArrayEnd; - break; - case '"': - token.type_ = tokenString; - ok = readString(); - break; - case '/': - token.type_ = tokenComment; - ok = readComment(); - break; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '-': - token.type_ = tokenNumber; - readNumber(); - break; - case 't': - token.type_ = tokenTrue; - ok = match( "rue", 3 ); - break; - case 'f': - token.type_ = tokenFalse; - ok = match( "alse", 4 ); - break; - case 'n': - token.type_ = tokenNull; - ok = match( "ull", 3 ); - break; - case ',': - token.type_ = tokenArraySeparator; - break; - case ':': - token.type_ = tokenMemberSeparator; - break; - case 0: - token.type_ = tokenEndOfStream; - break; - default: - ok = false; - break; - } - if ( !ok ) - token.type_ = tokenError; - token.end_ = current_; - return true; -} - - -void -Reader::skipSpaces() -{ - while ( current_ != end_ ) - { - Char c = *current_; - if ( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) - ++current_; - else - break; - } -} - - -bool -Reader::match( Location pattern, - int patternLength ) -{ - if ( end_ - current_ < patternLength ) - return false; - int index = patternLength; - while ( index-- ) - if ( current_[index] != pattern[index] ) - return false; - current_ += patternLength; - return true; -} - - -bool -Reader::readComment() -{ - Location commentBegin = current_ - 1; - Char c = getNextChar(); - bool successful = false; - if ( c == '*' ) - successful = readCStyleComment(); - else if ( c == '/' ) - successful = readCppStyleComment(); - if ( !successful ) - return false; - - if ( collectComments_ ) - { - CommentPlacement placement = commentBefore; - if ( lastValueEnd_ && !containsNewLine( lastValueEnd_, commentBegin ) ) - { - if ( c != '*' || !containsNewLine( commentBegin, current_ ) ) - placement = commentAfterOnSameLine; - } - - addComment( commentBegin, current_, placement ); - } - return true; -} - - -void -Reader::addComment( Location begin, - Location end, - CommentPlacement placement ) -{ - assert( collectComments_ ); - if ( placement == commentAfterOnSameLine ) - { - assert( lastValue_ != 0 ); - lastValue_->setComment( std::string( begin, end ), placement ); - } - else - { - if ( !commentsBefore_.empty() ) - commentsBefore_ += "\n"; - commentsBefore_ += std::string( begin, end ); - } -} - - -bool -Reader::readCStyleComment() -{ - while ( current_ != end_ ) - { - Char c = getNextChar(); - if ( c == '*' && *current_ == '/' ) - break; - } - return getNextChar() == '/'; -} - - -bool -Reader::readCppStyleComment() -{ - while ( current_ != end_ ) - { - Char c = getNextChar(); - if ( c == '\r' || c == '\n' ) - break; - } - return true; -} - - -void -Reader::readNumber() -{ - while ( current_ != end_ ) - { - if ( !(*current_ >= '0' && *current_ <= '9') && - !in( *current_, '.', 'e', 'E', '+', '-' ) ) - break; - ++current_; - } -} - -bool -Reader::readString() -{ - Char c = 0; - while ( current_ != end_ ) - { - c = getNextChar(); - if ( c == '\\' ) - getNextChar(); - else if ( c == '"' ) - break; - } - return c == '"'; -} - - -bool -Reader::readObject( Token &/*tokenStart*/ ) -{ - Token tokenName; - std::string name; - currentValue() = Value( objectValue ); - while ( readToken( tokenName ) ) - { - bool initialTokenOk = true; - while ( tokenName.type_ == tokenComment && initialTokenOk ) - initialTokenOk = readToken( tokenName ); - if ( !initialTokenOk ) - break; - if ( tokenName.type_ == tokenObjectEnd && name.empty() ) // empty object - return true; - if ( tokenName.type_ != tokenString ) - break; - - name = ""; - if ( !decodeString( tokenName, name ) ) - return recoverFromError( tokenObjectEnd ); - - Token colon; - if ( !readToken( colon ) || colon.type_ != tokenMemberSeparator ) - { - return addErrorAndRecover( "Missing ':' after object member name", - colon, - tokenObjectEnd ); - } - Value &value = currentValue()[ name ]; - nodes_.push( &value ); - bool ok = readValue(); - nodes_.pop(); - if ( !ok ) // error already set - return recoverFromError( tokenObjectEnd ); - - Token comma; - if ( !readToken( comma ) - || ( comma.type_ != tokenObjectEnd && - comma.type_ != tokenArraySeparator && - comma.type_ != tokenComment ) ) - { - return addErrorAndRecover( "Missing ',' or '}' in object declaration", - comma, - tokenObjectEnd ); - } - bool finalizeTokenOk = true; - while ( comma.type_ == tokenComment && - finalizeTokenOk ) - finalizeTokenOk = readToken( comma ); - if ( comma.type_ == tokenObjectEnd ) - return true; - } - return addErrorAndRecover( "Missing '}' or object member name", - tokenName, - tokenObjectEnd ); -} - - -bool -Reader::readArray( Token &/*tokenStart*/ ) -{ - currentValue() = Value( arrayValue ); - skipSpaces(); - if ( *current_ == ']' ) // empty array - { - Token endArray; - readToken( endArray ); - return true; - } - int index = 0; - for (;;) - { - Value &value = currentValue()[ index++ ]; - nodes_.push( &value ); - bool ok = readValue(); - nodes_.pop(); - if ( !ok ) // error already set - return recoverFromError( tokenArrayEnd ); - - Token token; - // Accept Comment after last item in the array. - ok = readToken( token ); - while ( token.type_ == tokenComment && ok ) - { - ok = readToken( token ); - } - bool badTokenType = ( token.type_ != tokenArraySeparator && - token.type_ != tokenArrayEnd ); - if ( !ok || badTokenType ) - { - return addErrorAndRecover( "Missing ',' or ']' in array declaration", - token, - tokenArrayEnd ); - } - if ( token.type_ == tokenArrayEnd ) - break; - } - return true; -} - - -bool -Reader::decodeNumber( Token &token ) -{ - bool isDouble = false; - for ( Location inspect = token.start_; inspect != token.end_; ++inspect ) - { - isDouble = isDouble - || in( *inspect, '.', 'e', 'E', '+' ) - || ( *inspect == '-' && inspect != token.start_ ); - } - if ( isDouble ) - return decodeDouble( token ); - // Attempts to parse the number as an integer. If the number is - // larger than the maximum supported value of an integer then - // we decode the number as a double. - Location current = token.start_; - bool isNegative = *current == '-'; - if ( isNegative ) - ++current; - Value::LargestUInt maxIntegerValue = isNegative ? Value::LargestUInt(-Value::minLargestInt) - : Value::maxLargestUInt; - Value::LargestUInt threshold = maxIntegerValue / 10; - Value::LargestUInt value = 0; - while ( current < token.end_ ) - { - Char c = *current++; - if ( c < '0' || c > '9' ) - return addError( "'" + std::string( token.start_, token.end_ ) + "' is not a number.", token ); - Value::UInt digit(c - '0'); - if ( value >= threshold ) - { - // We've hit or exceeded the max value divided by 10 (rounded down). If - // a) we've only just touched the limit, b) this is the last digit, and - // c) it's small enough to fit in that rounding delta, we're okay. - // Otherwise treat this number as a double to avoid overflow. - if (value > threshold || - current != token.end_ || - digit > maxIntegerValue % 10) - { - return decodeDouble( token ); - } - } - value = value * 10 + digit; - } - if ( isNegative ) - currentValue() = -Value::LargestInt( value ); - else if ( value <= Value::LargestUInt(Value::maxInt) ) - currentValue() = Value::LargestInt( value ); - else - currentValue() = value; - return true; -} - - -bool -Reader::decodeDouble( Token &token ) -{ - double value = 0; - const int bufferSize = 32; - int count; - int length = int(token.end_ - token.start_); - - // Sanity check to avoid buffer overflow exploits. - if (length < 0) { - return addError( "Unable to parse token length", token ); - } - - // Avoid using a string constant for the format control string given to - // sscanf, as this can cause hard to debug crashes on OS X. See here for more - // info: - // - // http://developer.apple.com/library/mac/#DOCUMENTATION/DeveloperTools/gcc-4.0.1/gcc/Incompatibilities.html - char format[] = "%lf"; - - if ( length <= bufferSize ) - { - Char buffer[bufferSize+1]; - memcpy( buffer, token.start_, length ); - buffer[length] = 0; - count = sscanf( buffer, format, &value ); - } - else - { - std::string buffer( token.start_, token.end_ ); - count = sscanf( buffer.c_str(), format, &value ); - } - - if ( count != 1 ) - return addError( "'" + std::string( token.start_, token.end_ ) + "' is not a number.", token ); - currentValue() = value; - return true; -} - - -bool -Reader::decodeString( Token &token ) -{ - std::string decoded; - if ( !decodeString( token, decoded ) ) - return false; - currentValue() = decoded; - return true; -} - - -bool -Reader::decodeString( Token &token, std::string &decoded ) -{ - decoded.reserve( token.end_ - token.start_ - 2 ); - Location current = token.start_ + 1; // skip '"' - Location end = token.end_ - 1; // do not include '"' - while ( current != end ) - { - Char c = *current++; - if ( c == '"' ) - break; - else if ( c == '\\' ) - { - if ( current == end ) - return addError( "Empty escape sequence in string", token, current ); - Char escape = *current++; - switch ( escape ) - { - case '"': decoded += '"'; break; - case '/': decoded += '/'; break; - case '\\': decoded += '\\'; break; - case 'b': decoded += '\b'; break; - case 'f': decoded += '\f'; break; - case 'n': decoded += '\n'; break; - case 'r': decoded += '\r'; break; - case 't': decoded += '\t'; break; - case 'u': - { - unsigned int unicode; - if ( !decodeUnicodeCodePoint( token, current, end, unicode ) ) - return false; - decoded += codePointToUTF8(unicode); - } - break; - default: - return addError( "Bad escape sequence in string", token, current ); - } - } - else - { - decoded += c; - } - } - return true; -} - -bool -Reader::decodeUnicodeCodePoint( Token &token, - Location ¤t, - Location end, - unsigned int &unicode ) -{ - - if ( !decodeUnicodeEscapeSequence( token, current, end, unicode ) ) - return false; - if (unicode >= 0xD800 && unicode <= 0xDBFF) - { - // surrogate pairs - if (end - current < 6) - return addError( "additional six characters expected to parse unicode surrogate pair.", token, current ); - unsigned int surrogatePair; - if (*(current++) == '\\' && *(current++)== 'u') - { - if (decodeUnicodeEscapeSequence( token, current, end, surrogatePair )) - { - unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF); - } - else - return false; - } - else - return addError( "expecting another \\u token to begin the second half of a unicode surrogate pair", token, current ); - } - return true; -} - -bool -Reader::decodeUnicodeEscapeSequence( Token &token, - Location ¤t, - Location end, - unsigned int &unicode ) -{ - if ( end - current < 4 ) - return addError( "Bad unicode escape sequence in string: four digits expected.", token, current ); - unicode = 0; - for ( int index =0; index < 4; ++index ) - { - Char c = *current++; - unicode *= 16; - if ( c >= '0' && c <= '9' ) - unicode += c - '0'; - else if ( c >= 'a' && c <= 'f' ) - unicode += c - 'a' + 10; - else if ( c >= 'A' && c <= 'F' ) - unicode += c - 'A' + 10; - else - return addError( "Bad unicode escape sequence in string: hexadecimal digit expected.", token, current ); - } - return true; -} - - -bool -Reader::addError( const std::string &message, - Token &token, - Location extra ) -{ - ErrorInfo info; - info.token_ = token; - info.message_ = message; - info.extra_ = extra; - errors_.push_back( info ); - return false; -} - - -bool -Reader::recoverFromError( TokenType skipUntilToken ) -{ - int errorCount = int(errors_.size()); - Token skip; - for (;;) - { - if ( !readToken(skip) ) - errors_.resize( errorCount ); // discard errors caused by recovery - if ( skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream ) - break; - } - errors_.resize( errorCount ); - return false; -} - - -bool -Reader::addErrorAndRecover( const std::string &message, - Token &token, - TokenType skipUntilToken ) -{ - addError( message, token ); - return recoverFromError( skipUntilToken ); -} - - -Value & -Reader::currentValue() -{ - return *(nodes_.top()); -} - - -Reader::Char -Reader::getNextChar() -{ - if ( current_ == end_ ) - return 0; - return *current_++; -} - - -void -Reader::getLocationLineAndColumn( Location location, - int &line, - int &column ) const -{ - Location current = begin_; - Location lastLineStart = current; - line = 0; - while ( current < location && current != end_ ) - { - Char c = *current++; - if ( c == '\r' ) - { - if ( *current == '\n' ) - ++current; - lastLineStart = current; - ++line; - } - else if ( c == '\n' ) - { - lastLineStart = current; - ++line; - } - } - // column & line start at 1 - column = int(location - lastLineStart) + 1; - ++line; -} - - -std::string -Reader::getLocationLineAndColumn( Location location ) const -{ - int line, column; - getLocationLineAndColumn( location, line, column ); - char buffer[18+16+16+1]; - sprintf( buffer, "Line %d, Column %d", line, column ); - return buffer; -} - - -// Deprecated. Preserved for backward compatibility -std::string -Reader::getFormatedErrorMessages() const -{ - return getFormattedErrorMessages(); -} - - -std::string -Reader::getFormattedErrorMessages() const -{ - std::string formattedMessage; - for ( Errors::const_iterator itError = errors_.begin(); - itError != errors_.end(); - ++itError ) - { - const ErrorInfo &error = *itError; - formattedMessage += "* " + getLocationLineAndColumn( error.token_.start_ ) + "\n"; - formattedMessage += " " + error.message_ + "\n"; - if ( error.extra_ ) - formattedMessage += "See " + getLocationLineAndColumn( error.extra_ ) + " for detail.\n"; - } - return formattedMessage; -} - - -std::istream& operator>>( std::istream &sin, Value &root ) -{ - Json::Reader reader; - bool ok = reader.parse(sin, root, true); - if (!ok) { - fprintf( - stderr, - "Error from reader: %s", - reader.getFormattedErrorMessages().c_str()); - - JSON_FAIL_MESSAGE("reader error"); - } - return sin; -} - - -} // namespace Json - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: src/lib_json/json_reader.cpp -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: src/lib_json/json_batchallocator.h -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#ifndef JSONCPP_BATCHALLOCATOR_H_INCLUDED -# define JSONCPP_BATCHALLOCATOR_H_INCLUDED - -# include -# include - -# ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION - -namespace Json { - -/* Fast memory allocator. - * - * This memory allocator allocates memory for a batch of object (specified by - * the page size, the number of object in each page). - * - * It does not allow the destruction of a single object. All the allocated objects - * can be destroyed at once. The memory can be either released or reused for future - * allocation. - * - * The in-place new operator must be used to construct the object using the pointer - * returned by allocate. - */ -template -class BatchAllocator -{ -public: - BatchAllocator( unsigned int objectsPerPage = 255 ) - : freeHead_( 0 ) - , objectsPerPage_( objectsPerPage ) - { -// printf( "Size: %d => %s\n", sizeof(AllocatedType), typeid(AllocatedType).name() ); - assert( sizeof(AllocatedType) * objectPerAllocation >= sizeof(AllocatedType *) ); // We must be able to store a slist in the object free space. - assert( objectsPerPage >= 16 ); - batches_ = allocateBatch( 0 ); // allocated a dummy page - currentBatch_ = batches_; - } - - ~BatchAllocator() - { - for ( BatchInfo *batch = batches_; batch; ) - { - BatchInfo *nextBatch = batch->next_; - free( batch ); - batch = nextBatch; - } - } - - /// allocate space for an array of objectPerAllocation object. - /// @warning it is the responsability of the caller to call objects constructors. - AllocatedType *allocate() - { - if ( freeHead_ ) // returns node from free list. - { - AllocatedType *object = freeHead_; - freeHead_ = *(AllocatedType **)object; - return object; - } - if ( currentBatch_->used_ == currentBatch_->end_ ) - { - currentBatch_ = currentBatch_->next_; - while ( currentBatch_ && currentBatch_->used_ == currentBatch_->end_ ) - currentBatch_ = currentBatch_->next_; - - if ( !currentBatch_ ) // no free batch found, allocate a new one - { - currentBatch_ = allocateBatch( objectsPerPage_ ); - currentBatch_->next_ = batches_; // insert at the head of the list - batches_ = currentBatch_; - } - } - AllocatedType *allocated = currentBatch_->used_; - currentBatch_->used_ += objectPerAllocation; - return allocated; - } - - /// Release the object. - /// @warning it is the responsability of the caller to actually destruct the object. - void release( AllocatedType *object ) - { - assert( object != 0 ); - *(AllocatedType **)object = freeHead_; - freeHead_ = object; - } - -private: - struct BatchInfo - { - BatchInfo *next_; - AllocatedType *used_; - AllocatedType *end_; - AllocatedType buffer_[objectPerAllocation]; - }; - - // disabled copy constructor and assignement operator. - BatchAllocator( const BatchAllocator & ); - void operator =( const BatchAllocator &); - - static BatchInfo *allocateBatch( unsigned int objectsPerPage ) - { - const unsigned int mallocSize = sizeof(BatchInfo) - sizeof(AllocatedType)* objectPerAllocation - + sizeof(AllocatedType) * objectPerAllocation * objectsPerPage; - BatchInfo *batch = static_cast( malloc( mallocSize ) ); - batch->next_ = 0; - batch->used_ = batch->buffer_; - batch->end_ = batch->buffer_ + objectsPerPage; - return batch; - } - - BatchInfo *batches_; - BatchInfo *currentBatch_; - /// Head of a single linked list within the allocated space of freeed object - AllocatedType *freeHead_; - unsigned int objectsPerPage_; -}; - - -} // namespace Json - -# endif // ifndef JSONCPP_DOC_INCLUDE_IMPLEMENTATION - -#endif // JSONCPP_BATCHALLOCATOR_H_INCLUDED - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: src/lib_json/json_batchallocator.h -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: src/lib_json/json_valueiterator.inl -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2007-2010 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -// included by json_value.cpp - -namespace Json { - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class ValueIteratorBase -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// - -ValueIteratorBase::ValueIteratorBase() -#ifndef JSON_VALUE_USE_INTERNAL_MAP - : current_() - , isNull_( true ) -{ -} -#else - : isArray_( true ) - , isNull_( true ) -{ - iterator_.array_ = ValueInternalArray::IteratorState(); -} -#endif - - -#ifndef JSON_VALUE_USE_INTERNAL_MAP -ValueIteratorBase::ValueIteratorBase( const Value::ObjectValues::iterator ¤t ) - : current_( current ) - , isNull_( false ) -{ -} -#else -ValueIteratorBase::ValueIteratorBase( const ValueInternalArray::IteratorState &state ) - : isArray_( true ) -{ - iterator_.array_ = state; -} - - -ValueIteratorBase::ValueIteratorBase( const ValueInternalMap::IteratorState &state ) - : isArray_( false ) -{ - iterator_.map_ = state; -} -#endif - -Value & -ValueIteratorBase::deref() const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - return current_->second; -#else - if ( isArray_ ) - return ValueInternalArray::dereference( iterator_.array_ ); - return ValueInternalMap::value( iterator_.map_ ); -#endif -} - - -void -ValueIteratorBase::increment() -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - ++current_; -#else - if ( isArray_ ) - ValueInternalArray::increment( iterator_.array_ ); - ValueInternalMap::increment( iterator_.map_ ); -#endif -} - - -void -ValueIteratorBase::decrement() -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - --current_; -#else - if ( isArray_ ) - ValueInternalArray::decrement( iterator_.array_ ); - ValueInternalMap::decrement( iterator_.map_ ); -#endif -} - - -ValueIteratorBase::difference_type -ValueIteratorBase::computeDistance( const SelfType &other ) const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP -# ifdef JSON_USE_CPPTL_SMALLMAP - return current_ - other.current_; -# else - // Iterator for null value are initialized using the default - // constructor, which initialize current_ to the default - // std::map::iterator. As begin() and end() are two instance - // of the default std::map::iterator, they can not be compared. - // To allow this, we handle this comparison specifically. - if ( isNull_ && other.isNull_ ) - { - return 0; - } - - - // Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL, - // which is the one used by default). - // Using a portable hand-made version for non random iterator instead: - // return difference_type( std::distance( current_, other.current_ ) ); - difference_type myDistance = 0; - for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it ) - { - ++myDistance; - } - return myDistance; -# endif -#else - if ( isArray_ ) - return ValueInternalArray::distance( iterator_.array_, other.iterator_.array_ ); - return ValueInternalMap::distance( iterator_.map_, other.iterator_.map_ ); -#endif -} - - -bool -ValueIteratorBase::isEqual( const SelfType &other ) const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - if ( isNull_ ) - { - return other.isNull_; - } - return current_ == other.current_; -#else - if ( isArray_ ) - return ValueInternalArray::equals( iterator_.array_, other.iterator_.array_ ); - return ValueInternalMap::equals( iterator_.map_, other.iterator_.map_ ); -#endif -} - - -void -ValueIteratorBase::copy( const SelfType &other ) -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - current_ = other.current_; -#else - if ( isArray_ ) - iterator_.array_ = other.iterator_.array_; - iterator_.map_ = other.iterator_.map_; -#endif -} - - -Value -ValueIteratorBase::key() const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - const Value::CZString czstring = (*current_).first; - if ( czstring.c_str() ) - { - if ( czstring.isStaticString() ) - return Value( StaticString( czstring.c_str() ) ); - return Value( czstring.c_str() ); - } - return Value( czstring.index() ); -#else - if ( isArray_ ) - return Value( ValueInternalArray::indexOf( iterator_.array_ ) ); - bool isStatic; - const char *memberName = ValueInternalMap::key( iterator_.map_, isStatic ); - if ( isStatic ) - return Value( StaticString( memberName ) ); - return Value( memberName ); -#endif -} - - -UInt -ValueIteratorBase::index() const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - const Value::CZString czstring = (*current_).first; - if ( !czstring.c_str() ) - return czstring.index(); - return Value::UInt( -1 ); -#else - if ( isArray_ ) - return Value::UInt( ValueInternalArray::indexOf( iterator_.array_ ) ); - return Value::UInt( -1 ); -#endif -} - - -const char * -ValueIteratorBase::memberName() const -{ -#ifndef JSON_VALUE_USE_INTERNAL_MAP - const char *name = (*current_).first.c_str(); - return name ? name : ""; -#else - if ( !isArray_ ) - return ValueInternalMap::key( iterator_.map_ ); - return ""; -#endif -} - - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class ValueConstIterator -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// - -ValueConstIterator::ValueConstIterator() -{ -} - - -#ifndef JSON_VALUE_USE_INTERNAL_MAP -ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator ¤t ) - : ValueIteratorBase( current ) -{ -} -#else -ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state ) - : ValueIteratorBase( state ) -{ -} - -ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state ) - : ValueIteratorBase( state ) -{ -} -#endif - -ValueConstIterator & -ValueConstIterator::operator =( const ValueIteratorBase &other ) -{ - copy( other ); - return *this; -} - - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class ValueIterator -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// - -ValueIterator::ValueIterator() -{ -} - - -#ifndef JSON_VALUE_USE_INTERNAL_MAP -ValueIterator::ValueIterator( const Value::ObjectValues::iterator ¤t ) - : ValueIteratorBase( current ) -{ -} -#else -ValueIterator::ValueIterator( const ValueInternalArray::IteratorState &state ) - : ValueIteratorBase( state ) -{ -} - -ValueIterator::ValueIterator( const ValueInternalMap::IteratorState &state ) - : ValueIteratorBase( state ) -{ -} -#endif - -ValueIterator::ValueIterator( const ValueConstIterator &other ) - : ValueIteratorBase( other ) -{ -} - -ValueIterator::ValueIterator( const ValueIterator &other ) - : ValueIteratorBase( other ) -{ -} - -ValueIterator & -ValueIterator::operator =( const SelfType &other ) -{ - copy( other ); - return *this; -} - -} // namespace Json - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: src/lib_json/json_valueiterator.inl -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: src/lib_json/json_value.cpp -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2011 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#if !defined(JSON_IS_AMALGAMATION) -# include -# include -# include -# ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR -# include "json_batchallocator.h" -# endif // #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR -#endif // if !defined(JSON_IS_AMALGAMATION) -#include -#include -#include -#include -#include -#include -#ifdef JSON_USE_CPPTL -# include -#endif -#include // size_t - -#define JSON_ASSERT_UNREACHABLE assert( false ) - -namespace Json { - -const Value Value::null; -const Int Value::minInt = Int( ~(UInt(-1)/2) ); -const Int Value::maxInt = Int( UInt(-1)/2 ); -const UInt Value::maxUInt = UInt(-1); -# if defined(JSON_HAS_INT64) -const Int64 Value::minInt64 = Int64( ~(UInt64(-1)/2) ); -const Int64 Value::maxInt64 = Int64( UInt64(-1)/2 ); -const UInt64 Value::maxUInt64 = UInt64(-1); -// The constant is hard-coded because some compiler have trouble -// converting Value::maxUInt64 to a double correctly (AIX/xlC). -// Assumes that UInt64 is a 64 bits integer. -static const double maxUInt64AsDouble = 18446744073709551615.0; -#endif // defined(JSON_HAS_INT64) -const LargestInt Value::minLargestInt = LargestInt( ~(LargestUInt(-1)/2) ); -const LargestInt Value::maxLargestInt = LargestInt( LargestUInt(-1)/2 ); -const LargestUInt Value::maxLargestUInt = LargestUInt(-1); - - -/// Unknown size marker -static const unsigned int unknown = (unsigned)-1; - -#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) -template -static inline bool InRange(double d, T min, U max) { - return d >= min && d <= max; -} -#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) -static inline double integerToDouble( Json::UInt64 value ) -{ - return static_cast( Int64(value/2) ) * 2.0 + Int64(value & 1); -} - -template -static inline double integerToDouble( T value ) -{ - return static_cast( value ); -} - -template -static inline bool InRange(double d, T min, U max) { - return d >= integerToDouble(min) && d <= integerToDouble(max); -} -#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - - -/** Duplicates the specified string value. - * @param value Pointer to the string to duplicate. Must be zero-terminated if - * length is "unknown". - * @param length Length of the value. if equals to unknown, then it will be - * computed using strlen(value). - * @return Pointer on the duplicate instance of string. - */ -static inline char * -duplicateStringValue( const char *value, - unsigned int length = unknown ) -{ - if ( length == unknown ) - length = (unsigned int)strlen(value); - - // Avoid an integer overflow in the call to malloc below by limiting length - // to a sane value. - if (length >= (unsigned)Value::maxInt) - length = Value::maxInt - 1; - - char *newString = static_cast( malloc( length + 1 ) ); - JSON_ASSERT_MESSAGE( newString != 0, "Failed to allocate string value buffer" ); - memcpy( newString, value, length ); - newString[length] = 0; - return newString; -} - - -/** Free the string duplicated by duplicateStringValue(). - */ -static inline void -releaseStringValue( char *value ) -{ - if ( value ) - free( value ); -} - -} // namespace Json - - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ValueInternals... -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -#if !defined(JSON_IS_AMALGAMATION) -# ifdef JSON_VALUE_USE_INTERNAL_MAP -# include "json_internalarray.inl" -# include "json_internalmap.inl" -# endif // JSON_VALUE_USE_INTERNAL_MAP - -# include "json_valueiterator.inl" -#endif // if !defined(JSON_IS_AMALGAMATION) - -namespace Json { - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class Value::CommentInfo -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// - - -Value::CommentInfo::CommentInfo() - : comment_( 0 ) -{ -} - -Value::CommentInfo::~CommentInfo() -{ - if ( comment_ ) - releaseStringValue( comment_ ); -} - - -void -Value::CommentInfo::setComment( const char *text ) -{ - if ( comment_ ) - releaseStringValue( comment_ ); - JSON_ASSERT( text != 0 ); - JSON_ASSERT_MESSAGE( text[0]=='\0' || text[0]=='/', "Comments must start with /"); - // It seems that /**/ style comments are acceptable as well. - comment_ = duplicateStringValue( text ); -} - - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class Value::CZString -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -# ifndef JSON_VALUE_USE_INTERNAL_MAP - -// Notes: index_ indicates if the string was allocated when -// a string is stored. - -Value::CZString::CZString( ArrayIndex index ) - : cstr_( 0 ) - , index_( index ) -{ -} - -Value::CZString::CZString( const char *cstr, DuplicationPolicy allocate ) - : cstr_( allocate == duplicate ? duplicateStringValue(cstr) - : cstr ) - , index_( allocate ) -{ -} - -Value::CZString::CZString( const CZString &other ) -: cstr_( other.index_ != noDuplication && other.cstr_ != 0 - ? duplicateStringValue( other.cstr_ ) - : other.cstr_ ) - , index_( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate) - : other.index_ ) -{ -} - -Value::CZString::~CZString() -{ - if ( cstr_ && index_ == duplicate ) - releaseStringValue( const_cast( cstr_ ) ); -} - -void -Value::CZString::swap( CZString &other ) -{ - std::swap( cstr_, other.cstr_ ); - std::swap( index_, other.index_ ); -} - -Value::CZString & -Value::CZString::operator =( const CZString &other ) -{ - CZString temp( other ); - swap( temp ); - return *this; -} - -bool -Value::CZString::operator<( const CZString &other ) const -{ - if ( cstr_ ) - return strcmp( cstr_, other.cstr_ ) < 0; - return index_ < other.index_; -} - -bool -Value::CZString::operator==( const CZString &other ) const -{ - if ( cstr_ ) - return strcmp( cstr_, other.cstr_ ) == 0; - return index_ == other.index_; -} - - -ArrayIndex -Value::CZString::index() const -{ - return index_; -} - - -const char * -Value::CZString::c_str() const -{ - return cstr_; -} - -bool -Value::CZString::isStaticString() const -{ - return index_ == noDuplication; -} - -#endif // ifndef JSON_VALUE_USE_INTERNAL_MAP - - -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// class Value::Value -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// -// ////////////////////////////////////////////////////////////////// - -/*! \internal Default constructor initialization must be equivalent to: - * memset( this, 0, sizeof(Value) ) - * This optimization is used in ValueInternalMap fast allocator. - */ -Value::Value( ValueType type ) - : type_( type ) - , allocated_( false ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - switch ( type ) - { - case nullValue: - break; - case intValue: - case uintValue: - value_.int_ = 0; - break; - case realValue: - value_.real_ = 0.0; - break; - case stringValue: - value_.string_ = 0; - break; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - value_.map_ = new ObjectValues(); - break; -#else - case arrayValue: - value_.array_ = arrayAllocator()->newArray(); - break; - case objectValue: - value_.map_ = mapAllocator()->newMap(); - break; -#endif - case booleanValue: - value_.bool_ = false; - break; - default: - JSON_ASSERT_UNREACHABLE; - } -} - - -Value::Value( UInt value ) - : type_( uintValue ) - , allocated_( false ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.uint_ = value; -} - -Value::Value( Int value ) - : type_( intValue ) - , allocated_( false ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.int_ = value; -} - - -# if defined(JSON_HAS_INT64) -Value::Value( Int64 value ) - : type_( intValue ) - , allocated_( false ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.int_ = value; -} - - -Value::Value( UInt64 value ) - : type_( uintValue ) - , allocated_( false ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.uint_ = value; -} -#endif // defined(JSON_HAS_INT64) - -Value::Value( double value ) - : type_( realValue ) - , allocated_( false ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.real_ = value; -} - -Value::Value( const char *value ) - : type_( stringValue ) - , allocated_( true ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.string_ = duplicateStringValue( value ); -} - - -Value::Value( const char *beginValue, - const char *endValue ) - : type_( stringValue ) - , allocated_( true ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.string_ = duplicateStringValue( beginValue, - (unsigned int)(endValue - beginValue) ); -} - - -Value::Value( const std::string &value ) - : type_( stringValue ) - , allocated_( true ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.string_ = duplicateStringValue( value.c_str(), - (unsigned int)value.length() ); - -} - -Value::Value( const StaticString &value ) - : type_( stringValue ) - , allocated_( false ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.string_ = const_cast( value.c_str() ); -} - - -# ifdef JSON_USE_CPPTL -Value::Value( const CppTL::ConstString &value ) - : type_( stringValue ) - , allocated_( true ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.string_ = duplicateStringValue( value, value.length() ); -} -# endif - -Value::Value( bool value ) - : type_( booleanValue ) - , allocated_( false ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - value_.bool_ = value; -} - - -Value::Value( const Value &other ) - : type_( other.type_ ) - , allocated_( false ) -# ifdef JSON_VALUE_USE_INTERNAL_MAP - , itemIsUsed_( 0 ) -#endif - , comments_( 0 ) -{ - switch ( type_ ) - { - case nullValue: - case intValue: - case uintValue: - case realValue: - case booleanValue: - value_ = other.value_; - break; - case stringValue: - if ( other.value_.string_ ) - { - value_.string_ = duplicateStringValue( other.value_.string_ ); - allocated_ = true; - } - else - value_.string_ = 0; - break; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - value_.map_ = new ObjectValues( *other.value_.map_ ); - break; -#else - case arrayValue: - value_.array_ = arrayAllocator()->newArrayCopy( *other.value_.array_ ); - break; - case objectValue: - value_.map_ = mapAllocator()->newMapCopy( *other.value_.map_ ); - break; -#endif - default: - JSON_ASSERT_UNREACHABLE; - } - if ( other.comments_ ) - { - comments_ = new CommentInfo[numberOfCommentPlacement]; - for ( int comment =0; comment < numberOfCommentPlacement; ++comment ) - { - const CommentInfo &otherComment = other.comments_[comment]; - if ( otherComment.comment_ ) - comments_[comment].setComment( otherComment.comment_ ); - } - } -} - - -Value::~Value() -{ - switch ( type_ ) - { - case nullValue: - case intValue: - case uintValue: - case realValue: - case booleanValue: - break; - case stringValue: - if ( allocated_ ) - releaseStringValue( value_.string_ ); - break; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - delete value_.map_; - break; -#else - case arrayValue: - arrayAllocator()->destructArray( value_.array_ ); - break; - case objectValue: - mapAllocator()->destructMap( value_.map_ ); - break; -#endif - default: - JSON_ASSERT_UNREACHABLE; - } - - if ( comments_ ) - delete[] comments_; -} - -Value & -Value::operator=( const Value &other ) -{ - Value temp( other ); - swap( temp ); - return *this; -} - -void -Value::swap( Value &other ) -{ - ValueType temp = type_; - type_ = other.type_; - other.type_ = temp; - std::swap( value_, other.value_ ); - int temp2 = allocated_; - allocated_ = other.allocated_; - other.allocated_ = temp2; -} - -ValueType -Value::type() const -{ - return type_; -} - - -int -Value::compare( const Value &other ) const -{ - if ( *this < other ) - return -1; - if ( *this > other ) - return 1; - return 0; -} - - -bool -Value::operator <( const Value &other ) const -{ - int typeDelta = type_ - other.type_; - if ( typeDelta ) - return typeDelta < 0 ? true : false; - switch ( type_ ) - { - case nullValue: - return false; - case intValue: - return value_.int_ < other.value_.int_; - case uintValue: - return value_.uint_ < other.value_.uint_; - case realValue: - return value_.real_ < other.value_.real_; - case booleanValue: - return value_.bool_ < other.value_.bool_; - case stringValue: - return ( value_.string_ == 0 && other.value_.string_ ) - || ( other.value_.string_ - && value_.string_ - && strcmp( value_.string_, other.value_.string_ ) < 0 ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - { - int delta = int( value_.map_->size() - other.value_.map_->size() ); - if ( delta ) - return delta < 0; - return (*value_.map_) < (*other.value_.map_); - } -#else - case arrayValue: - return value_.array_->compare( *(other.value_.array_) ) < 0; - case objectValue: - return value_.map_->compare( *(other.value_.map_) ) < 0; -#endif - default: - JSON_ASSERT_UNREACHABLE; - } - return false; // unreachable -} - -bool -Value::operator <=( const Value &other ) const -{ - return !(other < *this); -} - -bool -Value::operator >=( const Value &other ) const -{ - return !(*this < other); -} - -bool -Value::operator >( const Value &other ) const -{ - return other < *this; -} - -bool -Value::operator ==( const Value &other ) const -{ - //if ( type_ != other.type_ ) - // GCC 2.95.3 says: - // attempt to take address of bit-field structure member `Json::Value::type_' - // Beats me, but a temp solves the problem. - int temp = other.type_; - if ( type_ != temp ) - return false; - switch ( type_ ) - { - case nullValue: - return true; - case intValue: - return value_.int_ == other.value_.int_; - case uintValue: - return value_.uint_ == other.value_.uint_; - case realValue: - return value_.real_ == other.value_.real_; - case booleanValue: - return value_.bool_ == other.value_.bool_; - case stringValue: - return ( value_.string_ == other.value_.string_ ) - || ( other.value_.string_ - && value_.string_ - && strcmp( value_.string_, other.value_.string_ ) == 0 ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - return value_.map_->size() == other.value_.map_->size() - && (*value_.map_) == (*other.value_.map_); -#else - case arrayValue: - return value_.array_->compare( *(other.value_.array_) ) == 0; - case objectValue: - return value_.map_->compare( *(other.value_.map_) ) == 0; -#endif - default: - JSON_ASSERT_UNREACHABLE; - } - return false; // unreachable -} - -bool -Value::operator !=( const Value &other ) const -{ - return !( *this == other ); -} - -const char * -Value::asCString() const -{ - JSON_ASSERT( type_ == stringValue ); - return value_.string_; -} - - -std::string -Value::asString() const -{ - switch ( type_ ) - { - case nullValue: - return ""; - case stringValue: - return value_.string_ ? value_.string_ : ""; - case booleanValue: - return value_.bool_ ? "true" : "false"; - case intValue: - return valueToString( value_.int_ ); - case uintValue: - return valueToString( value_.uint_ ); - case realValue: - return valueToString( value_.real_ ); - default: - JSON_FAIL_MESSAGE( "Type is not convertible to string" ); - } -} - -# ifdef JSON_USE_CPPTL -CppTL::ConstString -Value::asConstString() const -{ - return CppTL::ConstString( asString().c_str() ); -} -# endif - - -Value::Int -Value::asInt() const -{ - switch ( type_ ) - { - case intValue: - JSON_ASSERT_MESSAGE(isInt(), "LargestInt out of Int range"); - return Int(value_.int_); - case uintValue: - JSON_ASSERT_MESSAGE(isInt(), "LargestUInt out of Int range"); - return Int(value_.uint_); - case realValue: - JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt, maxInt), "double out of Int range"); - return Int(value_.real_); - case nullValue: - return 0; - case booleanValue: - return value_.bool_ ? 1 : 0; - default: - break; - } - JSON_FAIL_MESSAGE("Value is not convertible to Int."); -} - - -Value::UInt -Value::asUInt() const -{ - switch ( type_ ) - { - case intValue: - JSON_ASSERT_MESSAGE(isUInt(), "LargestInt out of UInt range"); - return UInt(value_.int_); - case uintValue: - JSON_ASSERT_MESSAGE(isUInt(), "LargestUInt out of UInt range"); - return UInt(value_.uint_); - case realValue: - JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt), "double out of UInt range"); - return UInt( value_.real_ ); - case nullValue: - return 0; - case booleanValue: - return value_.bool_ ? 1 : 0; - default: - break; - } - JSON_FAIL_MESSAGE("Value is not convertible to UInt."); -} - - -# if defined(JSON_HAS_INT64) - -Value::Int64 -Value::asInt64() const -{ - switch ( type_ ) - { - case intValue: - return Int64(value_.int_); - case uintValue: - JSON_ASSERT_MESSAGE(isInt64(), "LargestUInt out of Int64 range"); - return Int64(value_.uint_); - case realValue: - JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt64, maxInt64), "double out of Int64 range"); - return Int64(value_.real_); - case nullValue: - return 0; - case booleanValue: - return value_.bool_ ? 1 : 0; - default: - break; - } - JSON_FAIL_MESSAGE("Value is not convertible to Int64."); -} - - -Value::UInt64 -Value::asUInt64() const -{ - switch ( type_ ) - { - case intValue: - JSON_ASSERT_MESSAGE(isUInt64(), "LargestInt out of UInt64 range"); - return UInt64(value_.int_); - case uintValue: - return UInt64(value_.uint_); - case realValue: - JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt64), "double out of UInt64 range"); - return UInt64( value_.real_ ); - case nullValue: - return 0; - case booleanValue: - return value_.bool_ ? 1 : 0; - default: - break; - } - JSON_FAIL_MESSAGE("Value is not convertible to UInt64."); -} -# endif // if defined(JSON_HAS_INT64) - - -LargestInt -Value::asLargestInt() const -{ -#if defined(JSON_NO_INT64) - return asInt(); -#else - return asInt64(); -#endif -} - - -LargestUInt -Value::asLargestUInt() const -{ -#if defined(JSON_NO_INT64) - return asUInt(); -#else - return asUInt64(); -#endif -} - - -double -Value::asDouble() const -{ - switch ( type_ ) - { - case intValue: - return static_cast( value_.int_ ); - case uintValue: -#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - return static_cast( value_.uint_ ); -#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - return integerToDouble( value_.uint_ ); -#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - case realValue: - return value_.real_; - case nullValue: - return 0.0; - case booleanValue: - return value_.bool_ ? 1.0 : 0.0; - default: - break; - } - JSON_FAIL_MESSAGE("Value is not convertible to double."); -} - -float -Value::asFloat() const -{ - switch ( type_ ) - { - case intValue: - return static_cast( value_.int_ ); - case uintValue: -#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - return static_cast( value_.uint_ ); -#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - return integerToDouble( value_.uint_ ); -#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) - case realValue: - return static_cast( value_.real_ ); - case nullValue: - return 0.0; - case booleanValue: - return value_.bool_ ? 1.0f : 0.0f; - default: - break; - } - JSON_FAIL_MESSAGE("Value is not convertible to float."); -} - -bool -Value::asBool() const -{ - switch ( type_ ) - { - case booleanValue: - return value_.bool_; - case nullValue: - return false; - case intValue: - return value_.int_ ? true : false; - case uintValue: - return value_.uint_ ? true : false; - case realValue: - return value_.real_ ? true : false; - default: - break; - } - JSON_FAIL_MESSAGE("Value is not convertible to bool."); -} - - -bool -Value::isConvertibleTo( ValueType other ) const -{ - switch ( other ) - { - case nullValue: - return ( isNumeric() && asDouble() == 0.0 ) - || ( type_ == booleanValue && value_.bool_ == false ) - || ( type_ == stringValue && asString() == "" ) - || ( type_ == arrayValue && value_.map_->size() == 0 ) - || ( type_ == objectValue && value_.map_->size() == 0 ) - || type_ == nullValue; - case intValue: - return isInt() - || (type_ == realValue && InRange(value_.real_, minInt, maxInt)) - || type_ == booleanValue - || type_ == nullValue; - case uintValue: - return isUInt() - || (type_ == realValue && InRange(value_.real_, 0, maxUInt)) - || type_ == booleanValue - || type_ == nullValue; - case realValue: - return isNumeric() - || type_ == booleanValue - || type_ == nullValue; - case booleanValue: - return isNumeric() - || type_ == booleanValue - || type_ == nullValue; - case stringValue: - return isNumeric() - || type_ == booleanValue - || type_ == stringValue - || type_ == nullValue; - case arrayValue: - return type_ == arrayValue - || type_ == nullValue; - case objectValue: - return type_ == objectValue - || type_ == nullValue; - } - JSON_ASSERT_UNREACHABLE; - return false; -} - - -/// Number of values in array or object -ArrayIndex -Value::size() const -{ - switch ( type_ ) - { - case nullValue: - case intValue: - case uintValue: - case realValue: - case booleanValue: - case stringValue: - return 0; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: // size of the array is highest index + 1 - if ( !value_.map_->empty() ) - { - ObjectValues::const_iterator itLast = value_.map_->end(); - --itLast; - return (*itLast).first.index()+1; - } - return 0; - case objectValue: - return ArrayIndex( value_.map_->size() ); -#else - case arrayValue: - return Int( value_.array_->size() ); - case objectValue: - return Int( value_.map_->size() ); -#endif - } - JSON_ASSERT_UNREACHABLE; - return 0; // unreachable; -} - - -bool -Value::empty() const -{ - if ( isNull() || isArray() || isObject() ) - return size() == 0u; - else - return false; -} - - -bool -Value::operator!() const -{ - return isNull(); -} - - -void -Value::clear() -{ - JSON_ASSERT( type_ == nullValue || type_ == arrayValue || type_ == objectValue ); - - switch ( type_ ) - { -#ifndef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - case objectValue: - value_.map_->clear(); - break; -#else - case arrayValue: - value_.array_->clear(); - break; - case objectValue: - value_.map_->clear(); - break; -#endif - default: - break; - } -} - -void -Value::resize( ArrayIndex newSize ) -{ - JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); - if ( type_ == nullValue ) - *this = Value( arrayValue ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - ArrayIndex oldSize = size(); - if ( newSize == 0 ) - clear(); - else if ( newSize > oldSize ) - (*this)[ newSize - 1 ]; - else - { - for ( ArrayIndex index = newSize; index < oldSize; ++index ) - { - value_.map_->erase( index ); - } - assert( size() == newSize ); - } -#else - value_.array_->resize( newSize ); -#endif -} - - -Value & -Value::operator[]( ArrayIndex index ) -{ - JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); - if ( type_ == nullValue ) - *this = Value( arrayValue ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - CZString key( index ); - ObjectValues::iterator it = value_.map_->lower_bound( key ); - if ( it != value_.map_->end() && (*it).first == key ) - return (*it).second; - - ObjectValues::value_type defaultValue( key, null ); - it = value_.map_->insert( it, defaultValue ); - return (*it).second; -#else - return value_.array_->resolveReference( index ); -#endif -} - - -Value & -Value::operator[]( int index ) -{ - JSON_ASSERT( index >= 0 ); - return (*this)[ ArrayIndex(index) ]; -} - - -const Value & -Value::operator[]( ArrayIndex index ) const -{ - JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); - if ( type_ == nullValue ) - return null; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - CZString key( index ); - ObjectValues::const_iterator it = value_.map_->find( key ); - if ( it == value_.map_->end() ) - return null; - return (*it).second; -#else - Value *value = value_.array_->find( index ); - return value ? *value : null; -#endif -} - - -const Value & -Value::operator[]( int index ) const -{ - JSON_ASSERT( index >= 0 ); - return (*this)[ ArrayIndex(index) ]; -} - - -Value & -Value::operator[]( const char *key ) -{ - return resolveReference( key, false ); -} - - -Value & -Value::resolveReference( const char *key, - bool isStatic ) -{ - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); - if ( type_ == nullValue ) - *this = Value( objectValue ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - CZString actualKey( key, isStatic ? CZString::noDuplication - : CZString::duplicateOnCopy ); - ObjectValues::iterator it = value_.map_->lower_bound( actualKey ); - if ( it != value_.map_->end() && (*it).first == actualKey ) - return (*it).second; - - ObjectValues::value_type defaultValue( actualKey, null ); - it = value_.map_->insert( it, defaultValue ); - Value &value = (*it).second; - return value; -#else - return value_.map_->resolveReference( key, isStatic ); -#endif -} - - -Value -Value::get( ArrayIndex index, - const Value &defaultValue ) const -{ - const Value *value = &((*this)[index]); - return value == &null ? defaultValue : *value; -} - - -bool -Value::isValidIndex( ArrayIndex index ) const -{ - return index < size(); -} - - - -const Value & -Value::operator[]( const char *key ) const -{ - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); - if ( type_ == nullValue ) - return null; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - CZString actualKey( key, CZString::noDuplication ); - ObjectValues::const_iterator it = value_.map_->find( actualKey ); - if ( it == value_.map_->end() ) - return null; - return (*it).second; -#else - const Value *value = value_.map_->find( key ); - return value ? *value : null; -#endif -} - - -Value & -Value::operator[]( const std::string &key ) -{ - return (*this)[ key.c_str() ]; -} - - -const Value & -Value::operator[]( const std::string &key ) const -{ - return (*this)[ key.c_str() ]; -} - -Value & -Value::operator[]( const StaticString &key ) -{ - return resolveReference( key, true ); -} - - -# ifdef JSON_USE_CPPTL -Value & -Value::operator[]( const CppTL::ConstString &key ) -{ - return (*this)[ key.c_str() ]; -} - - -const Value & -Value::operator[]( const CppTL::ConstString &key ) const -{ - return (*this)[ key.c_str() ]; -} -# endif - - -Value & -Value::append( const Value &value ) -{ - return (*this)[size()] = value; -} - - -Value -Value::get( const char *key, - const Value &defaultValue ) const -{ - const Value *value = &((*this)[key]); - return value == &null ? defaultValue : *value; -} - - -Value -Value::get( const std::string &key, - const Value &defaultValue ) const -{ - return get( key.c_str(), defaultValue ); -} - -Value -Value::removeMember( const char* key ) -{ - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); - if ( type_ == nullValue ) - return null; -#ifndef JSON_VALUE_USE_INTERNAL_MAP - CZString actualKey( key, CZString::noDuplication ); - ObjectValues::iterator it = value_.map_->find( actualKey ); - if ( it == value_.map_->end() ) - return null; - Value old(it->second); - value_.map_->erase(it); - return old; -#else - Value *value = value_.map_->find( key ); - if (value){ - Value old(*value); - value_.map_.remove( key ); - return old; - } else { - return null; - } -#endif -} - -Value -Value::removeMember( const std::string &key ) -{ - return removeMember( key.c_str() ); -} - -# ifdef JSON_USE_CPPTL -Value -Value::get( const CppTL::ConstString &key, - const Value &defaultValue ) const -{ - return get( key.c_str(), defaultValue ); -} -# endif - -bool -Value::isMember( const char *key ) const -{ - const Value *value = &((*this)[key]); - return value != &null; -} - - -bool -Value::isMember( const std::string &key ) const -{ - return isMember( key.c_str() ); -} - - -# ifdef JSON_USE_CPPTL -bool -Value::isMember( const CppTL::ConstString &key ) const -{ - return isMember( key.c_str() ); -} -#endif - -Value::Members -Value::getMemberNames() const -{ - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); - if ( type_ == nullValue ) - return Value::Members(); - Members members; - members.reserve( value_.map_->size() ); -#ifndef JSON_VALUE_USE_INTERNAL_MAP - ObjectValues::const_iterator it = value_.map_->begin(); - ObjectValues::const_iterator itEnd = value_.map_->end(); - for ( ; it != itEnd; ++it ) - members.push_back( std::string( (*it).first.c_str() ) ); -#else - ValueInternalMap::IteratorState it; - ValueInternalMap::IteratorState itEnd; - value_.map_->makeBeginIterator( it ); - value_.map_->makeEndIterator( itEnd ); - for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) ) - members.push_back( std::string( ValueInternalMap::key( it ) ) ); -#endif - return members; -} -// -//# ifdef JSON_USE_CPPTL -//EnumMemberNames -//Value::enumMemberNames() const -//{ -// if ( type_ == objectValue ) -// { -// return CppTL::Enum::any( CppTL::Enum::transform( -// CppTL::Enum::keys( *(value_.map_), CppTL::Type() ), -// MemberNamesTransform() ) ); -// } -// return EnumMemberNames(); -//} -// -// -//EnumValues -//Value::enumValues() const -//{ -// if ( type_ == objectValue || type_ == arrayValue ) -// return CppTL::Enum::anyValues( *(value_.map_), -// CppTL::Type() ); -// return EnumValues(); -//} -// -//# endif - -static bool IsIntegral(double d) { - double integral_part; - return modf(d, &integral_part) == 0.0; -} - - -bool -Value::isNull() const -{ - return type_ == nullValue; -} - - -bool -Value::isBool() const -{ - return type_ == booleanValue; -} - - -bool -Value::isInt() const -{ - switch ( type_ ) - { - case intValue: - return value_.int_ >= minInt && value_.int_ <= maxInt; - case uintValue: - return value_.uint_ <= UInt(maxInt); - case realValue: - return value_.real_ >= minInt && - value_.real_ <= maxInt && - IsIntegral(value_.real_); - default: - break; - } - return false; -} - - -bool -Value::isUInt() const -{ - switch ( type_ ) - { - case intValue: - return value_.int_ >= 0 && LargestUInt(value_.int_) <= LargestUInt(maxUInt); - case uintValue: - return value_.uint_ <= maxUInt; - case realValue: - return value_.real_ >= 0 && - value_.real_ <= maxUInt && - IsIntegral(value_.real_); - default: - break; - } - return false; -} - -bool -Value::isInt64() const -{ -# if defined(JSON_HAS_INT64) - switch ( type_ ) - { - case intValue: - return true; - case uintValue: - return value_.uint_ <= UInt64(maxInt64); - case realValue: - // Note that maxInt64 (= 2^63 - 1) is not exactly representable as a - // double, so double(maxInt64) will be rounded up to 2^63. Therefore we - // require the value to be strictly less than the limit. - return value_.real_ >= double(minInt64) && - value_.real_ < double(maxInt64) && - IsIntegral(value_.real_); - default: - break; - } -# endif // JSON_HAS_INT64 - return false; -} - -bool -Value::isUInt64() const -{ -# if defined(JSON_HAS_INT64) - switch ( type_ ) - { - case intValue: - return value_.int_ >= 0; - case uintValue: - return true; - case realValue: - // Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a - // double, so double(maxUInt64) will be rounded up to 2^64. Therefore we - // require the value to be strictly less than the limit. - return value_.real_ >= 0 && - value_.real_ < maxUInt64AsDouble && - IsIntegral(value_.real_); - default: - break; - } -# endif // JSON_HAS_INT64 - return false; -} - - -bool -Value::isIntegral() const -{ -#if defined(JSON_HAS_INT64) - return isInt64() || isUInt64(); -#else - return isInt() || isUInt(); -#endif -} - - -bool -Value::isDouble() const -{ - return type_ == realValue || isIntegral(); -} - - -bool -Value::isNumeric() const -{ - return isIntegral() || isDouble(); -} - - -bool -Value::isString() const -{ - return type_ == stringValue; -} - - -bool -Value::isArray() const -{ - return type_ == arrayValue; -} - - -bool -Value::isObject() const -{ - return type_ == objectValue; -} - - -void -Value::setComment( const char *comment, - CommentPlacement placement ) -{ - if ( !comments_ ) - comments_ = new CommentInfo[numberOfCommentPlacement]; - comments_[placement].setComment( comment ); -} - - -void -Value::setComment( const std::string &comment, - CommentPlacement placement ) -{ - setComment( comment.c_str(), placement ); -} - - -bool -Value::hasComment( CommentPlacement placement ) const -{ - return comments_ != 0 && comments_[placement].comment_ != 0; -} - -std::string -Value::getComment( CommentPlacement placement ) const -{ - if ( hasComment(placement) ) - return comments_[placement].comment_; - return ""; -} - - -std::string -Value::toStyledString() const -{ - StyledWriter writer; - return writer.write( *this ); -} - - -Value::const_iterator -Value::begin() const -{ - switch ( type_ ) - { -#ifdef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - if ( value_.array_ ) - { - ValueInternalArray::IteratorState it; - value_.array_->makeBeginIterator( it ); - return const_iterator( it ); - } - break; - case objectValue: - if ( value_.map_ ) - { - ValueInternalMap::IteratorState it; - value_.map_->makeBeginIterator( it ); - return const_iterator( it ); - } - break; -#else - case arrayValue: - case objectValue: - if ( value_.map_ ) - return const_iterator( value_.map_->begin() ); - break; -#endif - default: - break; - } - return const_iterator(); -} - -Value::const_iterator -Value::end() const -{ - switch ( type_ ) - { -#ifdef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - if ( value_.array_ ) - { - ValueInternalArray::IteratorState it; - value_.array_->makeEndIterator( it ); - return const_iterator( it ); - } - break; - case objectValue: - if ( value_.map_ ) - { - ValueInternalMap::IteratorState it; - value_.map_->makeEndIterator( it ); - return const_iterator( it ); - } - break; -#else - case arrayValue: - case objectValue: - if ( value_.map_ ) - return const_iterator( value_.map_->end() ); - break; -#endif - default: - break; - } - return const_iterator(); -} - - -Value::iterator -Value::begin() -{ - switch ( type_ ) - { -#ifdef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - if ( value_.array_ ) - { - ValueInternalArray::IteratorState it; - value_.array_->makeBeginIterator( it ); - return iterator( it ); - } - break; - case objectValue: - if ( value_.map_ ) - { - ValueInternalMap::IteratorState it; - value_.map_->makeBeginIterator( it ); - return iterator( it ); - } - break; -#else - case arrayValue: - case objectValue: - if ( value_.map_ ) - return iterator( value_.map_->begin() ); - break; -#endif - default: - break; - } - return iterator(); -} - -Value::iterator -Value::end() -{ - switch ( type_ ) - { -#ifdef JSON_VALUE_USE_INTERNAL_MAP - case arrayValue: - if ( value_.array_ ) - { - ValueInternalArray::IteratorState it; - value_.array_->makeEndIterator( it ); - return iterator( it ); - } - break; - case objectValue: - if ( value_.map_ ) - { - ValueInternalMap::IteratorState it; - value_.map_->makeEndIterator( it ); - return iterator( it ); - } - break; -#else - case arrayValue: - case objectValue: - if ( value_.map_ ) - return iterator( value_.map_->end() ); - break; -#endif - default: - break; - } - return iterator(); -} - - -// class PathArgument -// ////////////////////////////////////////////////////////////////// - -PathArgument::PathArgument() - : key_() - , index_() - , kind_( kindNone ) -{ -} - - -PathArgument::PathArgument( ArrayIndex index ) - : key_() - , index_( index ) - , kind_( kindIndex ) -{ -} - - -PathArgument::PathArgument( const char *key ) - : key_( key ) - , index_() - , kind_( kindKey ) -{ -} - - -PathArgument::PathArgument( const std::string &key ) - : key_( key.c_str() ) - , index_() - , kind_( kindKey ) -{ -} - -// class Path -// ////////////////////////////////////////////////////////////////// - -Path::Path( const std::string &path, - const PathArgument &a1, - const PathArgument &a2, - const PathArgument &a3, - const PathArgument &a4, - const PathArgument &a5 ) -{ - InArgs in; - in.push_back( &a1 ); - in.push_back( &a2 ); - in.push_back( &a3 ); - in.push_back( &a4 ); - in.push_back( &a5 ); - makePath( path, in ); -} - - -void -Path::makePath( const std::string &path, - const InArgs &in ) -{ - const char *current = path.c_str(); - const char *end = current + path.length(); - InArgs::const_iterator itInArg = in.begin(); - while ( current != end ) - { - if ( *current == '[' ) - { - ++current; - if ( *current == '%' ) - addPathInArg( path, in, itInArg, PathArgument::kindIndex ); - else - { - ArrayIndex index = 0; - for ( ; current != end && *current >= '0' && *current <= '9'; ++current ) - index = index * 10 + ArrayIndex(*current - '0'); - args_.push_back( index ); - } - if ( current == end || *current++ != ']' ) - invalidPath( path, int(current - path.c_str()) ); - } - else if ( *current == '%' ) - { - addPathInArg( path, in, itInArg, PathArgument::kindKey ); - ++current; - } - else if ( *current == '.' ) - { - ++current; - } - else - { - const char *beginName = current; - while ( current != end && !strchr( "[.", *current ) ) - ++current; - args_.push_back( std::string( beginName, current ) ); - } - } -} - - -void -Path::addPathInArg( const std::string &path, - const InArgs &in, - InArgs::const_iterator &itInArg, - PathArgument::Kind kind ) -{ - if ( itInArg == in.end() ) - { - // Error: missing argument %d - } - else if ( (*itInArg)->kind_ != kind ) - { - // Error: bad argument type - } - else - { - args_.push_back( **itInArg ); - } -} - - -void -Path::invalidPath( const std::string &path, - int location ) -{ - // Error: invalid path. -} - - -const Value & -Path::resolve( const Value &root ) const -{ - const Value *node = &root; - for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it ) - { - const PathArgument &arg = *it; - if ( arg.kind_ == PathArgument::kindIndex ) - { - if ( !node->isArray() || !node->isValidIndex( arg.index_ ) ) - { - // Error: unable to resolve path (array value expected at position... - } - node = &((*node)[arg.index_]); - } - else if ( arg.kind_ == PathArgument::kindKey ) - { - if ( !node->isObject() ) - { - // Error: unable to resolve path (object value expected at position...) - } - node = &((*node)[arg.key_]); - if ( node == &Value::null ) - { - // Error: unable to resolve path (object has no member named '' at position...) - } - } - } - return *node; -} - - -Value -Path::resolve( const Value &root, - const Value &defaultValue ) const -{ - const Value *node = &root; - for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it ) - { - const PathArgument &arg = *it; - if ( arg.kind_ == PathArgument::kindIndex ) - { - if ( !node->isArray() || !node->isValidIndex( arg.index_ ) ) - return defaultValue; - node = &((*node)[arg.index_]); - } - else if ( arg.kind_ == PathArgument::kindKey ) - { - if ( !node->isObject() ) - return defaultValue; - node = &((*node)[arg.key_]); - if ( node == &Value::null ) - return defaultValue; - } - } - return *node; -} - - -Value & -Path::make( Value &root ) const -{ - Value *node = &root; - for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it ) - { - const PathArgument &arg = *it; - if ( arg.kind_ == PathArgument::kindIndex ) - { - if ( !node->isArray() ) - { - // Error: node is not an array at position ... - } - node = &((*node)[arg.index_]); - } - else if ( arg.kind_ == PathArgument::kindKey ) - { - if ( !node->isObject() ) - { - // Error: node is not an object at position... - } - node = &((*node)[arg.key_]); - } - } - return *node; -} - - -} // namespace Json - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: src/lib_json/json_value.cpp -// ////////////////////////////////////////////////////////////////////// - - - - - - -// ////////////////////////////////////////////////////////////////////// -// Beginning of content of file: src/lib_json/json_writer.cpp -// ////////////////////////////////////////////////////////////////////// - -// Copyright 2011 Baptiste Lepilleur -// Distributed under MIT license, or public domain if desired and -// recognized in your jurisdiction. -// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE - -#if !defined(JSON_IS_AMALGAMATION) -# include -# include "json_tool.h" -#endif // if !defined(JSON_IS_AMALGAMATION) -#include -#include -#include -#include -#include -#include - -#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0 -#pragma warning( disable : 4996 ) // disable warning about strdup being deprecated. -#endif - -namespace Json { - -static bool containsControlCharacter( const char* str ) -{ - while ( *str ) - { - if ( isControlCharacter( *(str++) ) ) - return true; - } - return false; -} - - -std::string valueToString( LargestInt value ) -{ - UIntToStringBuffer buffer; - char *current = buffer + sizeof(buffer); - bool isNegative = value < 0; - if ( isNegative ) - value = -value; - uintToString( LargestUInt(value), current ); - if ( isNegative ) - *--current = '-'; - assert( current >= buffer ); - return current; -} - - -std::string valueToString( LargestUInt value ) -{ - UIntToStringBuffer buffer; - char *current = buffer + sizeof(buffer); - uintToString( value, current ); - assert( current >= buffer ); - return current; -} - -#if defined(JSON_HAS_INT64) - -std::string valueToString( Int value ) -{ - return valueToString( LargestInt(value) ); -} - - -std::string valueToString( UInt value ) -{ - return valueToString( LargestUInt(value) ); -} - -#endif // # if defined(JSON_HAS_INT64) - - -std::string valueToString( double value ) -{ - char buffer[32]; -#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) // Use secure version with visual studio 2005 to avoid warning. - sprintf_s(buffer, sizeof(buffer), "%#.16g", value); -#else - sprintf(buffer, "%#.16g", value); -#endif - char* ch = buffer + strlen(buffer) - 1; - if (*ch != '0') return buffer; // nothing to truncate, so save time - while(ch > buffer && *ch == '0'){ - --ch; - } - char* last_nonzero = ch; - while(ch >= buffer){ - switch(*ch){ - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - --ch; - continue; - case '.': - // Truncate zeroes to save bytes in output, but keep one. - *(last_nonzero+2) = '\0'; - return buffer; - default: - return buffer; - } - } - return buffer; -} - - -std::string valueToString( bool value ) -{ - return value ? "true" : "false"; -} - -std::string valueToQuotedString( const char *value ) -{ - if (value == NULL) - return ""; - // Not sure how to handle unicode... - if (strpbrk(value, "\"\\\b\f\n\r\t") == NULL && !containsControlCharacter( value )) - return std::string("\"") + value + "\""; - // We have to walk value and escape any special characters. - // Appending to std::string is not efficient, but this should be rare. - // (Note: forward slashes are *not* rare, but I am not escaping them.) - std::string::size_type maxsize = strlen(value)*2 + 3; // allescaped+quotes+NULL - std::string result; - result.reserve(maxsize); // to avoid lots of mallocs - result += "\""; - for (const char* c=value; *c != 0; ++c) - { - switch(*c) - { - case '\"': - result += "\\\""; - break; - case '\\': - result += "\\\\"; - break; - case '\b': - result += "\\b"; - break; - case '\f': - result += "\\f"; - break; - case '\n': - result += "\\n"; - break; - case '\r': - result += "\\r"; - break; - case '\t': - result += "\\t"; - break; - //case '/': - // Even though \/ is considered a legal escape in JSON, a bare - // slash is also legal, so I see no reason to escape it. - // (I hope I am not misunderstanding something. - // blep notes: actually escaping \/ may be useful in javascript to avoid (*c); - result += oss.str(); - } - else - { - result += *c; - } - break; - } - } - result += "\""; - return result; -} - -// Class Writer -// ////////////////////////////////////////////////////////////////// -Writer::~Writer() -{ -} - - -// Class FastWriter -// ////////////////////////////////////////////////////////////////// - -FastWriter::FastWriter() - : yamlCompatiblityEnabled_( false ), - dropNullPlaceholders_( false ) -{ -} - - -void -FastWriter::enableYAMLCompatibility() -{ - yamlCompatiblityEnabled_ = true; -} - - -void -FastWriter::dropNullPlaceholders() -{ - dropNullPlaceholders_ = true; -} - - -std::string -FastWriter::write( const Value &root ) -{ - document_ = ""; - writeValue( root ); - document_ += "\n"; - return document_; -} - - -void -FastWriter::writeValue( const Value &value ) -{ - switch ( value.type() ) - { - case nullValue: - if (!dropNullPlaceholders_) document_ += "null"; - break; - case intValue: - document_ += valueToString( value.asLargestInt() ); - break; - case uintValue: - document_ += valueToString( value.asLargestUInt() ); - break; - case realValue: - document_ += valueToString( value.asDouble() ); - break; - case stringValue: - document_ += valueToQuotedString( value.asCString() ); - break; - case booleanValue: - document_ += valueToString( value.asBool() ); - break; - case arrayValue: - { - document_ += "["; - int size = value.size(); - for ( int index =0; index < size; ++index ) - { - if ( index > 0 ) - document_ += ","; - writeValue( value[index] ); - } - document_ += "]"; - } - break; - case objectValue: - { - Value::Members members( value.getMemberNames() ); - document_ += "{"; - for ( Value::Members::iterator it = members.begin(); - it != members.end(); - ++it ) - { - const std::string &name = *it; - if ( it != members.begin() ) - document_ += ","; - document_ += valueToQuotedString( name.c_str() ); - document_ += yamlCompatiblityEnabled_ ? ": " - : ":"; - writeValue( value[name] ); - } - document_ += "}"; - } - break; - } -} - - -// Class StyledWriter -// ////////////////////////////////////////////////////////////////// - -StyledWriter::StyledWriter() - : rightMargin_( 74 ) - , indentSize_( 3 ) - , addChildValues_() -{ -} - - -std::string -StyledWriter::write( const Value &root ) -{ - document_ = ""; - addChildValues_ = false; - indentString_ = ""; - writeCommentBeforeValue( root ); - writeValue( root ); - writeCommentAfterValueOnSameLine( root ); - document_ += "\n"; - return document_; -} - - -void -StyledWriter::writeValue( const Value &value ) -{ - switch ( value.type() ) - { - case nullValue: - pushValue( "null" ); - break; - case intValue: - pushValue( valueToString( value.asLargestInt() ) ); - break; - case uintValue: - pushValue( valueToString( value.asLargestUInt() ) ); - break; - case realValue: - pushValue( valueToString( value.asDouble() ) ); - break; - case stringValue: - pushValue( valueToQuotedString( value.asCString() ) ); - break; - case booleanValue: - pushValue( valueToString( value.asBool() ) ); - break; - case arrayValue: - writeArrayValue( value); - break; - case objectValue: - { - Value::Members members( value.getMemberNames() ); - if ( members.empty() ) - pushValue( "{}" ); - else - { - writeWithIndent( "{" ); - indent(); - Value::Members::iterator it = members.begin(); - for (;;) - { - const std::string &name = *it; - const Value &childValue = value[name]; - writeCommentBeforeValue( childValue ); - writeWithIndent( valueToQuotedString( name.c_str() ) ); - document_ += " : "; - writeValue( childValue ); - if ( ++it == members.end() ) - { - writeCommentAfterValueOnSameLine( childValue ); - break; - } - document_ += ","; - writeCommentAfterValueOnSameLine( childValue ); - } - unindent(); - writeWithIndent( "}" ); - } - } - break; - } -} - - -void -StyledWriter::writeArrayValue( const Value &value ) -{ - unsigned size = value.size(); - if ( size == 0 ) - pushValue( "[]" ); - else - { - bool isArrayMultiLine = isMultineArray( value ); - if ( isArrayMultiLine ) - { - writeWithIndent( "[" ); - indent(); - bool hasChildValue = !childValues_.empty(); - unsigned index =0; - for (;;) - { - const Value &childValue = value[index]; - writeCommentBeforeValue( childValue ); - if ( hasChildValue ) - writeWithIndent( childValues_[index] ); - else - { - writeIndent(); - writeValue( childValue ); - } - if ( ++index == size ) - { - writeCommentAfterValueOnSameLine( childValue ); - break; - } - document_ += ","; - writeCommentAfterValueOnSameLine( childValue ); - } - unindent(); - writeWithIndent( "]" ); - } - else // output on a single line - { - assert( childValues_.size() == size ); - document_ += "[ "; - for ( unsigned index =0; index < size; ++index ) - { - if ( index > 0 ) - document_ += ", "; - document_ += childValues_[index]; - } - document_ += " ]"; - } - } -} - - -bool -StyledWriter::isMultineArray( const Value &value ) -{ - int size = value.size(); - bool isMultiLine = size*3 >= rightMargin_ ; - childValues_.clear(); - for ( int index =0; index < size && !isMultiLine; ++index ) - { - const Value &childValue = value[index]; - isMultiLine = isMultiLine || - ( (childValue.isArray() || childValue.isObject()) && - childValue.size() > 0 ); - } - if ( !isMultiLine ) // check if line length > max line length - { - childValues_.reserve( size ); - addChildValues_ = true; - int lineLength = 4 + (size-1)*2; // '[ ' + ', '*n + ' ]' - for ( int index =0; index < size && !isMultiLine; ++index ) - { - writeValue( value[index] ); - lineLength += int( childValues_[index].length() ); - isMultiLine = isMultiLine && hasCommentForValue( value[index] ); - } - addChildValues_ = false; - isMultiLine = isMultiLine || lineLength >= rightMargin_; - } - return isMultiLine; -} - - -void -StyledWriter::pushValue( const std::string &value ) -{ - if ( addChildValues_ ) - childValues_.push_back( value ); - else - document_ += value; -} - - -void -StyledWriter::writeIndent() -{ - if ( !document_.empty() ) - { - char last = document_[document_.length()-1]; - if ( last == ' ' ) // already indented - return; - if ( last != '\n' ) // Comments may add new-line - document_ += '\n'; - } - document_ += indentString_; -} - - -void -StyledWriter::writeWithIndent( const std::string &value ) -{ - writeIndent(); - document_ += value; -} - - -void -StyledWriter::indent() -{ - indentString_ += std::string( indentSize_, ' ' ); -} - - -void -StyledWriter::unindent() -{ - assert( int(indentString_.size()) >= indentSize_ ); - indentString_.resize( indentString_.size() - indentSize_ ); -} - - -void -StyledWriter::writeCommentBeforeValue( const Value &root ) -{ - if ( !root.hasComment( commentBefore ) ) - return; - document_ += normalizeEOL( root.getComment( commentBefore ) ); - document_ += "\n"; -} - - -void -StyledWriter::writeCommentAfterValueOnSameLine( const Value &root ) -{ - if ( root.hasComment( commentAfterOnSameLine ) ) - document_ += " " + normalizeEOL( root.getComment( commentAfterOnSameLine ) ); - - if ( root.hasComment( commentAfter ) ) - { - document_ += "\n"; - document_ += normalizeEOL( root.getComment( commentAfter ) ); - document_ += "\n"; - } -} - - -bool -StyledWriter::hasCommentForValue( const Value &value ) -{ - return value.hasComment( commentBefore ) - || value.hasComment( commentAfterOnSameLine ) - || value.hasComment( commentAfter ); -} - - -std::string -StyledWriter::normalizeEOL( const std::string &text ) -{ - std::string normalized; - normalized.reserve( text.length() ); - const char *begin = text.c_str(); - const char *end = begin + text.length(); - const char *current = begin; - while ( current != end ) - { - char c = *current++; - if ( c == '\r' ) // mac or dos EOL - { - if ( *current == '\n' ) // convert dos EOL - ++current; - normalized += '\n'; - } - else // handle unix EOL & other char - normalized += c; - } - return normalized; -} - - -// Class StyledStreamWriter -// ////////////////////////////////////////////////////////////////// - -StyledStreamWriter::StyledStreamWriter( std::string indentation ) - : document_(NULL) - , rightMargin_( 74 ) - , indentation_( indentation ) - , addChildValues_() -{ -} - - -void -StyledStreamWriter::write( std::ostream &out, const Value &root ) -{ - document_ = &out; - addChildValues_ = false; - indentString_ = ""; - writeCommentBeforeValue( root ); - writeValue( root ); - writeCommentAfterValueOnSameLine( root ); - *document_ << "\n"; - document_ = NULL; // Forget the stream, for safety. -} - - -void -StyledStreamWriter::writeValue( const Value &value ) -{ - switch ( value.type() ) - { - case nullValue: - pushValue( "null" ); - break; - case intValue: - pushValue( valueToString( value.asLargestInt() ) ); - break; - case uintValue: - pushValue( valueToString( value.asLargestUInt() ) ); - break; - case realValue: - pushValue( valueToString( value.asDouble() ) ); - break; - case stringValue: - pushValue( valueToQuotedString( value.asCString() ) ); - break; - case booleanValue: - pushValue( valueToString( value.asBool() ) ); - break; - case arrayValue: - writeArrayValue( value); - break; - case objectValue: - { - Value::Members members( value.getMemberNames() ); - if ( members.empty() ) - pushValue( "{}" ); - else - { - writeWithIndent( "{" ); - indent(); - Value::Members::iterator it = members.begin(); - for (;;) - { - const std::string &name = *it; - const Value &childValue = value[name]; - writeCommentBeforeValue( childValue ); - writeWithIndent( valueToQuotedString( name.c_str() ) ); - *document_ << " : "; - writeValue( childValue ); - if ( ++it == members.end() ) - { - writeCommentAfterValueOnSameLine( childValue ); - break; - } - *document_ << ","; - writeCommentAfterValueOnSameLine( childValue ); - } - unindent(); - writeWithIndent( "}" ); - } - } - break; - } -} - - -void -StyledStreamWriter::writeArrayValue( const Value &value ) -{ - unsigned size = value.size(); - if ( size == 0 ) - pushValue( "[]" ); - else - { - bool isArrayMultiLine = isMultineArray( value ); - if ( isArrayMultiLine ) - { - writeWithIndent( "[" ); - indent(); - bool hasChildValue = !childValues_.empty(); - unsigned index =0; - for (;;) - { - const Value &childValue = value[index]; - writeCommentBeforeValue( childValue ); - if ( hasChildValue ) - writeWithIndent( childValues_[index] ); - else - { - writeIndent(); - writeValue( childValue ); - } - if ( ++index == size ) - { - writeCommentAfterValueOnSameLine( childValue ); - break; - } - *document_ << ","; - writeCommentAfterValueOnSameLine( childValue ); - } - unindent(); - writeWithIndent( "]" ); - } - else // output on a single line - { - assert( childValues_.size() == size ); - *document_ << "[ "; - for ( unsigned index =0; index < size; ++index ) - { - if ( index > 0 ) - *document_ << ", "; - *document_ << childValues_[index]; - } - *document_ << " ]"; - } - } -} - - -bool -StyledStreamWriter::isMultineArray( const Value &value ) -{ - int size = value.size(); - bool isMultiLine = size*3 >= rightMargin_ ; - childValues_.clear(); - for ( int index =0; index < size && !isMultiLine; ++index ) - { - const Value &childValue = value[index]; - isMultiLine = isMultiLine || - ( (childValue.isArray() || childValue.isObject()) && - childValue.size() > 0 ); - } - if ( !isMultiLine ) // check if line length > max line length - { - childValues_.reserve( size ); - addChildValues_ = true; - int lineLength = 4 + (size-1)*2; // '[ ' + ', '*n + ' ]' - for ( int index =0; index < size && !isMultiLine; ++index ) - { - writeValue( value[index] ); - lineLength += int( childValues_[index].length() ); - isMultiLine = isMultiLine && hasCommentForValue( value[index] ); - } - addChildValues_ = false; - isMultiLine = isMultiLine || lineLength >= rightMargin_; - } - return isMultiLine; -} - - -void -StyledStreamWriter::pushValue( const std::string &value ) -{ - if ( addChildValues_ ) - childValues_.push_back( value ); - else - *document_ << value; -} - - -void -StyledStreamWriter::writeIndent() -{ - /* - Some comments in this method would have been nice. ;-) - - if ( !document_.empty() ) - { - char last = document_[document_.length()-1]; - if ( last == ' ' ) // already indented - return; - if ( last != '\n' ) // Comments may add new-line - *document_ << '\n'; - } - */ - *document_ << '\n' << indentString_; -} - - -void -StyledStreamWriter::writeWithIndent( const std::string &value ) -{ - writeIndent(); - *document_ << value; -} - - -void -StyledStreamWriter::indent() -{ - indentString_ += indentation_; -} - - -void -StyledStreamWriter::unindent() -{ - assert( indentString_.size() >= indentation_.size() ); - indentString_.resize( indentString_.size() - indentation_.size() ); -} - - -void -StyledStreamWriter::writeCommentBeforeValue( const Value &root ) -{ - if ( !root.hasComment( commentBefore ) ) - return; - *document_ << normalizeEOL( root.getComment( commentBefore ) ); - *document_ << "\n"; -} - - -void -StyledStreamWriter::writeCommentAfterValueOnSameLine( const Value &root ) -{ - if ( root.hasComment( commentAfterOnSameLine ) ) - *document_ << " " + normalizeEOL( root.getComment( commentAfterOnSameLine ) ); - - if ( root.hasComment( commentAfter ) ) - { - *document_ << "\n"; - *document_ << normalizeEOL( root.getComment( commentAfter ) ); - *document_ << "\n"; - } -} - - -bool -StyledStreamWriter::hasCommentForValue( const Value &value ) -{ - return value.hasComment( commentBefore ) - || value.hasComment( commentAfterOnSameLine ) - || value.hasComment( commentAfter ); -} - - -std::string -StyledStreamWriter::normalizeEOL( const std::string &text ) -{ - std::string normalized; - normalized.reserve( text.length() ); - const char *begin = text.c_str(); - const char *end = begin + text.length(); - const char *current = begin; - while ( current != end ) - { - char c = *current++; - if ( c == '\r' ) // mac or dos EOL - { - if ( *current == '\n' ) // convert dos EOL - ++current; - normalized += '\n'; - } - else // handle unix EOL & other char - normalized += c; - } - return normalized; -} - - -std::ostream& operator<<( std::ostream &sout, const Value &root ) -{ - Json::StyledStreamWriter writer; - writer.write(sout, root); - return sout; -} - - -} // namespace Json - -// ////////////////////////////////////////////////////////////////////// -// End of content of file: src/lib_json/json_writer.cpp -// ////////////////////////////////////////////////////////////////////// - - - - - diff --git a/src/jsoncpp/json/CMakeLists.txt b/src/jsoncpp/json/CMakeLists.txt new file mode 100644 index 000000000..9056e4b6d --- /dev/null +++ b/src/jsoncpp/json/CMakeLists.txt @@ -0,0 +1,7 @@ +if(MSVC) + set(CMAKE_CXX_FLAGS_RELEASE "/MT /O2 /Ob2 /D NDEBUG") +endif() + +add_library(jsoncpp jsoncpp.cpp) +target_link_libraries(jsoncpp) + diff --git a/src/jsoncpp/json/UPDATING b/src/jsoncpp/json/UPDATING new file mode 100644 index 000000000..d00076601 --- /dev/null +++ b/src/jsoncpp/json/UPDATING @@ -0,0 +1,16 @@ +#!/bin/sh +cd .. +svn co https://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/jsoncpp jsoncpp +svn up jsoncpp +cd jsoncpp +python amalgamate.py +cp -R dist/json .. +cp dist/jsoncpp.cpp ../json + +# maybe you need to patch: +# src/json/jsoncpp.cpp: +# -#include +# +#include "json/json.h" + +#svn export --force https://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/jsoncpp/src/lib_json json +#svn export --force https://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/jsoncpp/include/json json diff --git a/src/jsoncpp/json/json.h b/src/jsoncpp/json/json.h new file mode 100644 index 000000000..396aafa82 --- /dev/null +++ b/src/jsoncpp/json/json.h @@ -0,0 +1,1914 @@ +/// Json-cpp amalgated header (http://jsoncpp.sourceforge.net/). +/// It is intented to be used with #include + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: LICENSE +// ////////////////////////////////////////////////////////////////////// + +/* +The JsonCpp library's source code, including accompanying documentation, +tests and demonstration applications, are licensed under the following +conditions... + +The author (Baptiste Lepilleur) explicitly disclaims copyright in all +jurisdictions which recognize such a disclaimer. In such jurisdictions, +this software is released into the Public Domain. + +In jurisdictions which do not recognize Public Domain property (e.g. Germany as of +2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is +released under the terms of the MIT License (see below). + +In jurisdictions which recognize Public Domain property, the user of this +software may choose to accept it either as 1) Public Domain, 2) under the +conditions of the MIT License (see below), or 3) under the terms of dual +Public Domain/MIT License conditions described here, as they choose. + +The MIT License is about as close to Public Domain as a license can get, and is +described in clear, concise terms at: + + http://en.wikipedia.org/wiki/MIT_License + +The full text of the MIT License follows: + +======================================================================== +Copyright (c) 2007-2010 Baptiste Lepilleur + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, copy, +modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +======================================================================== +(END LICENSE TEXT) + +The MIT license is compatible with both the GPL and commercial +software, affording one all of the rights of Public Domain with the +minor nuisance of being required to keep the above copyright notice +and license text in the source code. Note also that by accepting the +Public Domain "license" you can re-license your copy using whatever +license you like. + +*/ + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: LICENSE +// ////////////////////////////////////////////////////////////////////// + + + + + +#ifndef JSON_AMALGATED_H_INCLUDED +# define JSON_AMALGATED_H_INCLUDED +/// If defined, indicates that the source file is amalgated +/// to prevent private header inclusion. +#define JSON_IS_AMALGAMATION + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: include/json/config.h +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef JSON_CONFIG_H_INCLUDED +# define JSON_CONFIG_H_INCLUDED + +/// If defined, indicates that json library is embedded in CppTL library. +//# define JSON_IN_CPPTL 1 + +/// If defined, indicates that json may leverage CppTL library +//# define JSON_USE_CPPTL 1 +/// If defined, indicates that cpptl vector based map should be used instead of std::map +/// as Value container. +//# define JSON_USE_CPPTL_SMALLMAP 1 +/// If defined, indicates that Json specific container should be used +/// (hash table & simple deque container with customizable allocator). +/// THIS FEATURE IS STILL EXPERIMENTAL! There is know bugs: See #3177332 +//# define JSON_VALUE_USE_INTERNAL_MAP 1 +/// Force usage of standard new/malloc based allocator instead of memory pool based allocator. +/// The memory pools allocator used optimization (initializing Value and ValueInternalLink +/// as if it was a POD) that may cause some validation tool to report errors. +/// Only has effects if JSON_VALUE_USE_INTERNAL_MAP is defined. +//# define JSON_USE_SIMPLE_INTERNAL_ALLOCATOR 1 + +// If non-zero, the library uses exceptions to report bad input instead of C +// assertion macros. The default is to use exceptions. +# ifndef JSON_USE_EXCEPTION +# define JSON_USE_EXCEPTION 1 +# endif + +/// If defined, indicates that the source file is amalgated +/// to prevent private header inclusion. +/// Remarks: it is automatically defined in the generated amalgated header. +// #define JSON_IS_AMALGAMATION + + +# ifdef JSON_IN_CPPTL +# include +# ifndef JSON_USE_CPPTL +# define JSON_USE_CPPTL 1 +# endif +# endif + +# ifdef JSON_IN_CPPTL +# define JSON_API CPPTL_API +# elif defined(JSON_DLL_BUILD) +# define JSON_API __declspec(dllexport) +# elif defined(JSON_DLL) +# define JSON_API __declspec(dllimport) +# else +# define JSON_API +# endif + +// If JSON_NO_INT64 is defined, then Json only support C++ "int" type for integer +// Storages, and 64 bits integer support is disabled. +// #define JSON_NO_INT64 1 + +#if defined(_MSC_VER) && _MSC_VER <= 1200 // MSVC 6 +// Microsoft Visual Studio 6 only support conversion from __int64 to double +// (no conversion from unsigned __int64). +#define JSON_USE_INT64_DOUBLE_CONVERSION 1 +#endif // if defined(_MSC_VER) && _MSC_VER < 1200 // MSVC 6 + +#if defined(_MSC_VER) && _MSC_VER >= 1500 // MSVC 2008 +/// Indicates that the following function is deprecated. +# define JSONCPP_DEPRECATED(message) __declspec(deprecated(message)) +#endif + +#if !defined(JSONCPP_DEPRECATED) +# define JSONCPP_DEPRECATED(message) +#endif // if !defined(JSONCPP_DEPRECATED) + +namespace Json { + typedef int Int; + typedef unsigned int UInt; +# if defined(JSON_NO_INT64) + typedef int LargestInt; + typedef unsigned int LargestUInt; +# undef JSON_HAS_INT64 +# else // if defined(JSON_NO_INT64) + // For Microsoft Visual use specific types as long long is not supported +# if defined(_MSC_VER) // Microsoft Visual Studio + typedef __int64 Int64; + typedef unsigned __int64 UInt64; +# else // if defined(_MSC_VER) // Other platforms, use long long + typedef long long int Int64; + typedef unsigned long long int UInt64; +# endif // if defined(_MSC_VER) + typedef Int64 LargestInt; + typedef UInt64 LargestUInt; +# define JSON_HAS_INT64 +# endif // if defined(JSON_NO_INT64) +} // end namespace Json + + +#endif // JSON_CONFIG_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: include/json/config.h +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: include/json/forwards.h +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef JSON_FORWARDS_H_INCLUDED +# define JSON_FORWARDS_H_INCLUDED + +#if !defined(JSON_IS_AMALGAMATION) +# include "config.h" +#endif // if !defined(JSON_IS_AMALGAMATION) + +namespace Json { + + // writer.h + class FastWriter; + class StyledWriter; + + // reader.h + class Reader; + + // features.h + class Features; + + // value.h + typedef unsigned int ArrayIndex; + class StaticString; + class Path; + class PathArgument; + class Value; + class ValueIteratorBase; + class ValueIterator; + class ValueConstIterator; +#ifdef JSON_VALUE_USE_INTERNAL_MAP + class ValueMapAllocator; + class ValueInternalLink; + class ValueInternalArray; + class ValueInternalMap; +#endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP + +} // namespace Json + + +#endif // JSON_FORWARDS_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: include/json/forwards.h +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: include/json/features.h +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef CPPTL_JSON_FEATURES_H_INCLUDED +# define CPPTL_JSON_FEATURES_H_INCLUDED + +#if !defined(JSON_IS_AMALGAMATION) +# include "forwards.h" +#endif // if !defined(JSON_IS_AMALGAMATION) + +namespace Json { + + /** \brief Configuration passed to reader and writer. + * This configuration object can be used to force the Reader or Writer + * to behave in a standard conforming way. + */ + class JSON_API Features + { + public: + /** \brief A configuration that allows all features and assumes all strings are UTF-8. + * - C & C++ comments are allowed + * - Root object can be any JSON value + * - Assumes Value strings are encoded in UTF-8 + */ + static Features all(); + + /** \brief A configuration that is strictly compatible with the JSON specification. + * - Comments are forbidden. + * - Root object must be either an array or an object value. + * - Assumes Value strings are encoded in UTF-8 + */ + static Features strictMode(); + + /** \brief Initialize the configuration like JsonConfig::allFeatures; + */ + Features(); + + /// \c true if comments are allowed. Default: \c true. + bool allowComments_; + + /// \c true if root must be either an array or an object value. Default: \c false. + bool strictRoot_; + }; + +} // namespace Json + +#endif // CPPTL_JSON_FEATURES_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: include/json/features.h +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: include/json/value.h +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef CPPTL_JSON_H_INCLUDED +# define CPPTL_JSON_H_INCLUDED + +#if !defined(JSON_IS_AMALGAMATION) +# include "forwards.h" +#endif // if !defined(JSON_IS_AMALGAMATION) +# include +# include + +# ifndef JSON_USE_CPPTL_SMALLMAP +# include +# else +# include +# endif +# ifdef JSON_USE_CPPTL +# include +# endif + +/** \brief JSON (JavaScript Object Notation). + */ +namespace Json { + + /** \brief Type of the value held by a Value object. + */ + enum ValueType + { + nullValue = 0, ///< 'null' value + intValue, ///< signed integer value + uintValue, ///< unsigned integer value + realValue, ///< double value + stringValue, ///< UTF-8 string value + booleanValue, ///< bool value + arrayValue, ///< array value (ordered list) + objectValue ///< object value (collection of name/value pairs). + }; + + enum CommentPlacement + { + commentBefore = 0, ///< a comment placed on the line before a value + commentAfterOnSameLine, ///< a comment just after a value on the same line + commentAfter, ///< a comment on the line after a value (only make sense for root value) + numberOfCommentPlacement + }; + +//# ifdef JSON_USE_CPPTL +// typedef CppTL::AnyEnumerator EnumMemberNames; +// typedef CppTL::AnyEnumerator EnumValues; +//# endif + + /** \brief Lightweight wrapper to tag static string. + * + * Value constructor and objectValue member assignement takes advantage of the + * StaticString and avoid the cost of string duplication when storing the + * string or the member name. + * + * Example of usage: + * \code + * Json::Value aValue( StaticString("some text") ); + * Json::Value object; + * static const StaticString code("code"); + * object[code] = 1234; + * \endcode + */ + class JSON_API StaticString + { + public: + explicit StaticString( const char *czstring ) + : str_( czstring ) + { + } + + operator const char *() const + { + return str_; + } + + const char *c_str() const + { + return str_; + } + + private: + const char *str_; + }; + + /** \brief Represents a JSON value. + * + * This class is a discriminated union wrapper that can represents a: + * - signed integer [range: Value::minInt - Value::maxInt] + * - unsigned integer (range: 0 - Value::maxUInt) + * - double + * - UTF-8 string + * - boolean + * - 'null' + * - an ordered list of Value + * - collection of name/value pairs (javascript object) + * + * The type of the held value is represented by a #ValueType and + * can be obtained using type(). + * + * values of an #objectValue or #arrayValue can be accessed using operator[]() methods. + * Non const methods will automatically create the a #nullValue element + * if it does not exist. + * The sequence of an #arrayValue will be automatically resize and initialized + * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. + * + * The get() methods can be used to obtanis default value in the case the required element + * does not exist. + * + * It is possible to iterate over the list of a #objectValue values using + * the getMemberNames() method. + */ + class JSON_API Value + { + friend class ValueIteratorBase; +# ifdef JSON_VALUE_USE_INTERNAL_MAP + friend class ValueInternalLink; + friend class ValueInternalMap; +# endif + public: + typedef std::vector Members; + typedef ValueIterator iterator; + typedef ValueConstIterator const_iterator; + typedef Json::UInt UInt; + typedef Json::Int Int; +# if defined(JSON_HAS_INT64) + typedef Json::UInt64 UInt64; + typedef Json::Int64 Int64; +#endif // defined(JSON_HAS_INT64) + typedef Json::LargestInt LargestInt; + typedef Json::LargestUInt LargestUInt; + typedef Json::ArrayIndex ArrayIndex; + + static const Value null; + /// Minimum signed integer value that can be stored in a Json::Value. + static const LargestInt minLargestInt; + /// Maximum signed integer value that can be stored in a Json::Value. + static const LargestInt maxLargestInt; + /// Maximum unsigned integer value that can be stored in a Json::Value. + static const LargestUInt maxLargestUInt; + + /// Minimum signed int value that can be stored in a Json::Value. + static const Int minInt; + /// Maximum signed int value that can be stored in a Json::Value. + static const Int maxInt; + /// Maximum unsigned int value that can be stored in a Json::Value. + static const UInt maxUInt; + +# if defined(JSON_HAS_INT64) + /// Minimum signed 64 bits int value that can be stored in a Json::Value. + static const Int64 minInt64; + /// Maximum signed 64 bits int value that can be stored in a Json::Value. + static const Int64 maxInt64; + /// Maximum unsigned 64 bits int value that can be stored in a Json::Value. + static const UInt64 maxUInt64; +#endif // defined(JSON_HAS_INT64) + + private: +#ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION +# ifndef JSON_VALUE_USE_INTERNAL_MAP + class CZString + { + public: + enum DuplicationPolicy + { + noDuplication = 0, + duplicate, + duplicateOnCopy + }; + CZString( ArrayIndex index ); + CZString( const char *cstr, DuplicationPolicy allocate ); + CZString( const CZString &other ); + ~CZString(); + CZString &operator =( const CZString &other ); + bool operator<( const CZString &other ) const; + bool operator==( const CZString &other ) const; + ArrayIndex index() const; + const char *c_str() const; + bool isStaticString() const; + private: + void swap( CZString &other ); + const char *cstr_; + ArrayIndex index_; + }; + + public: +# ifndef JSON_USE_CPPTL_SMALLMAP + typedef std::map ObjectValues; +# else + typedef CppTL::SmallMap ObjectValues; +# endif // ifndef JSON_USE_CPPTL_SMALLMAP +# endif // ifndef JSON_VALUE_USE_INTERNAL_MAP +#endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION + + public: + /** \brief Create a default Value of the given type. + + This is a very useful constructor. + To create an empty array, pass arrayValue. + To create an empty object, pass objectValue. + Another Value can then be set to this one by assignment. + This is useful since clear() and resize() will not alter types. + + Examples: + \code + Json::Value null_value; // null + Json::Value arr_value(Json::arrayValue); // [] + Json::Value obj_value(Json::objectValue); // {} + \endcode + */ + Value( ValueType type = nullValue ); + Value( Int value ); + Value( UInt value ); +#if defined(JSON_HAS_INT64) + Value( Int64 value ); + Value( UInt64 value ); +#endif // if defined(JSON_HAS_INT64) + Value( double value ); + Value( const char *value ); + Value( const char *beginValue, const char *endValue ); + /** \brief Constructs a value from a static string. + + * Like other value string constructor but do not duplicate the string for + * internal storage. The given string must remain alive after the call to this + * constructor. + * Example of usage: + * \code + * Json::Value aValue( StaticString("some text") ); + * \endcode + */ + Value( const StaticString &value ); + Value( const std::string &value ); +# ifdef JSON_USE_CPPTL + Value( const CppTL::ConstString &value ); +# endif + Value( bool value ); + Value( const Value &other ); + ~Value(); + + Value &operator=( const Value &other ); + /// Swap values. + /// \note Currently, comments are intentionally not swapped, for + /// both logic and efficiency. + void swap( Value &other ); + + ValueType type() const; + + bool operator <( const Value &other ) const; + bool operator <=( const Value &other ) const; + bool operator >=( const Value &other ) const; + bool operator >( const Value &other ) const; + + bool operator ==( const Value &other ) const; + bool operator !=( const Value &other ) const; + + int compare( const Value &other ) const; + + const char *asCString() const; + std::string asString() const; +# ifdef JSON_USE_CPPTL + CppTL::ConstString asConstString() const; +# endif + Int asInt() const; + UInt asUInt() const; +#if defined(JSON_HAS_INT64) + Int64 asInt64() const; + UInt64 asUInt64() const; +#endif // if defined(JSON_HAS_INT64) + LargestInt asLargestInt() const; + LargestUInt asLargestUInt() const; + float asFloat() const; + double asDouble() const; + bool asBool() const; + + bool isNull() const; + bool isBool() const; + bool isInt() const; + bool isInt64() const; + bool isUInt() const; + bool isUInt64() const; + bool isIntegral() const; + bool isDouble() const; + bool isNumeric() const; + bool isString() const; + bool isArray() const; + bool isObject() const; + + bool isConvertibleTo( ValueType other ) const; + + /// Number of values in array or object + ArrayIndex size() const; + + /// \brief Return true if empty array, empty object, or null; + /// otherwise, false. + bool empty() const; + + /// Return isNull() + bool operator!() const; + + /// Remove all object members and array elements. + /// \pre type() is arrayValue, objectValue, or nullValue + /// \post type() is unchanged + void clear(); + + /// Resize the array to size elements. + /// New elements are initialized to null. + /// May only be called on nullValue or arrayValue. + /// \pre type() is arrayValue or nullValue + /// \post type() is arrayValue + void resize( ArrayIndex size ); + + /// Access an array element (zero based index ). + /// If the array contains less than index element, then null value are inserted + /// in the array so that its size is index+1. + /// (You may need to say 'value[0u]' to get your compiler to distinguish + /// this from the operator[] which takes a string.) + Value &operator[]( ArrayIndex index ); + + /// Access an array element (zero based index ). + /// If the array contains less than index element, then null value are inserted + /// in the array so that its size is index+1. + /// (You may need to say 'value[0u]' to get your compiler to distinguish + /// this from the operator[] which takes a string.) + Value &operator[]( int index ); + + /// Access an array element (zero based index ) + /// (You may need to say 'value[0u]' to get your compiler to distinguish + /// this from the operator[] which takes a string.) + const Value &operator[]( ArrayIndex index ) const; + + /// Access an array element (zero based index ) + /// (You may need to say 'value[0u]' to get your compiler to distinguish + /// this from the operator[] which takes a string.) + const Value &operator[]( int index ) const; + + /// If the array contains at least index+1 elements, returns the element value, + /// otherwise returns defaultValue. + Value get( ArrayIndex index, + const Value &defaultValue ) const; + /// Return true if index < size(). + bool isValidIndex( ArrayIndex index ) const; + /// \brief Append value to array at the end. + /// + /// Equivalent to jsonvalue[jsonvalue.size()] = value; + Value &append( const Value &value ); + + /// Access an object value by name, create a null member if it does not exist. + Value &operator[]( const char *key ); + /// Access an object value by name, returns null if there is no member with that name. + const Value &operator[]( const char *key ) const; + /// Access an object value by name, create a null member if it does not exist. + Value &operator[]( const std::string &key ); + /// Access an object value by name, returns null if there is no member with that name. + const Value &operator[]( const std::string &key ) const; + /** \brief Access an object value by name, create a null member if it does not exist. + + * If the object as no entry for that name, then the member name used to store + * the new entry is not duplicated. + * Example of use: + * \code + * Json::Value object; + * static const StaticString code("code"); + * object[code] = 1234; + * \endcode + */ + Value &operator[]( const StaticString &key ); +# ifdef JSON_USE_CPPTL + /// Access an object value by name, create a null member if it does not exist. + Value &operator[]( const CppTL::ConstString &key ); + /// Access an object value by name, returns null if there is no member with that name. + const Value &operator[]( const CppTL::ConstString &key ) const; +# endif + /// Return the member named key if it exist, defaultValue otherwise. + Value get( const char *key, + const Value &defaultValue ) const; + /// Return the member named key if it exist, defaultValue otherwise. + Value get( const std::string &key, + const Value &defaultValue ) const; +# ifdef JSON_USE_CPPTL + /// Return the member named key if it exist, defaultValue otherwise. + Value get( const CppTL::ConstString &key, + const Value &defaultValue ) const; +# endif + /// \brief Remove and return the named member. + /// + /// Do nothing if it did not exist. + /// \return the removed Value, or null. + /// \pre type() is objectValue or nullValue + /// \post type() is unchanged + Value removeMember( const char* key ); + /// Same as removeMember(const char*) + Value removeMember( const std::string &key ); + + /// Return true if the object has a member named key. + bool isMember( const char *key ) const; + /// Return true if the object has a member named key. + bool isMember( const std::string &key ) const; +# ifdef JSON_USE_CPPTL + /// Return true if the object has a member named key. + bool isMember( const CppTL::ConstString &key ) const; +# endif + + /// \brief Return a list of the member names. + /// + /// If null, return an empty list. + /// \pre type() is objectValue or nullValue + /// \post if type() was nullValue, it remains nullValue + Members getMemberNames() const; + +//# ifdef JSON_USE_CPPTL +// EnumMemberNames enumMemberNames() const; +// EnumValues enumValues() const; +//# endif + + /// Comments must be //... or /* ... */ + void setComment( const char *comment, + CommentPlacement placement ); + /// Comments must be //... or /* ... */ + void setComment( const std::string &comment, + CommentPlacement placement ); + bool hasComment( CommentPlacement placement ) const; + /// Include delimiters and embedded newlines. + std::string getComment( CommentPlacement placement ) const; + + std::string toStyledString() const; + + const_iterator begin() const; + const_iterator end() const; + + iterator begin(); + iterator end(); + + private: + Value &resolveReference( const char *key, + bool isStatic ); + +# ifdef JSON_VALUE_USE_INTERNAL_MAP + inline bool isItemAvailable() const + { + return itemIsUsed_ == 0; + } + + inline void setItemUsed( bool isUsed = true ) + { + itemIsUsed_ = isUsed ? 1 : 0; + } + + inline bool isMemberNameStatic() const + { + return memberNameIsStatic_ == 0; + } + + inline void setMemberNameIsStatic( bool isStatic ) + { + memberNameIsStatic_ = isStatic ? 1 : 0; + } +# endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP + + private: + struct CommentInfo + { + CommentInfo(); + ~CommentInfo(); + + void setComment( const char *text ); + + char *comment_; + }; + + //struct MemberNamesTransform + //{ + // typedef const char *result_type; + // const char *operator()( const CZString &name ) const + // { + // return name.c_str(); + // } + //}; + + union ValueHolder + { + LargestInt int_; + LargestUInt uint_; + double real_; + bool bool_; + char *string_; +# ifdef JSON_VALUE_USE_INTERNAL_MAP + ValueInternalArray *array_; + ValueInternalMap *map_; +#else + ObjectValues *map_; +# endif + } value_; + ValueType type_ : 8; + int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. +# ifdef JSON_VALUE_USE_INTERNAL_MAP + unsigned int itemIsUsed_ : 1; // used by the ValueInternalMap container. + int memberNameIsStatic_ : 1; // used by the ValueInternalMap container. +# endif + CommentInfo *comments_; + }; + + + /** \brief Experimental and untested: represents an element of the "path" to access a node. + */ + class PathArgument + { + public: + friend class Path; + + PathArgument(); + PathArgument( ArrayIndex index ); + PathArgument( const char *key ); + PathArgument( const std::string &key ); + + private: + enum Kind + { + kindNone = 0, + kindIndex, + kindKey + }; + std::string key_; + ArrayIndex index_; + Kind kind_; + }; + + /** \brief Experimental and untested: represents a "path" to access a node. + * + * Syntax: + * - "." => root node + * - ".[n]" => elements at index 'n' of root node (an array value) + * - ".name" => member named 'name' of root node (an object value) + * - ".name1.name2.name3" + * - ".[0][1][2].name1[3]" + * - ".%" => member name is provided as parameter + * - ".[%]" => index is provied as parameter + */ + class Path + { + public: + Path( const std::string &path, + const PathArgument &a1 = PathArgument(), + const PathArgument &a2 = PathArgument(), + const PathArgument &a3 = PathArgument(), + const PathArgument &a4 = PathArgument(), + const PathArgument &a5 = PathArgument() ); + + const Value &resolve( const Value &root ) const; + Value resolve( const Value &root, + const Value &defaultValue ) const; + /// Creates the "path" to access the specified node and returns a reference on the node. + Value &make( Value &root ) const; + + private: + typedef std::vector InArgs; + typedef std::vector Args; + + void makePath( const std::string &path, + const InArgs &in ); + void addPathInArg( const std::string &path, + const InArgs &in, + InArgs::const_iterator &itInArg, + PathArgument::Kind kind ); + void invalidPath( const std::string &path, + int location ); + + Args args_; + }; + + + +#ifdef JSON_VALUE_USE_INTERNAL_MAP + /** \brief Allocator to customize Value internal map. + * Below is an example of a simple implementation (default implementation actually + * use memory pool for speed). + * \code + class DefaultValueMapAllocator : public ValueMapAllocator + { + public: // overridden from ValueMapAllocator + virtual ValueInternalMap *newMap() + { + return new ValueInternalMap(); + } + + virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) + { + return new ValueInternalMap( other ); + } + + virtual void destructMap( ValueInternalMap *map ) + { + delete map; + } + + virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) + { + return new ValueInternalLink[size]; + } + + virtual void releaseMapBuckets( ValueInternalLink *links ) + { + delete [] links; + } + + virtual ValueInternalLink *allocateMapLink() + { + return new ValueInternalLink(); + } + + virtual void releaseMapLink( ValueInternalLink *link ) + { + delete link; + } + }; + * \endcode + */ + class JSON_API ValueMapAllocator + { + public: + virtual ~ValueMapAllocator(); + virtual ValueInternalMap *newMap() = 0; + virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) = 0; + virtual void destructMap( ValueInternalMap *map ) = 0; + virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) = 0; + virtual void releaseMapBuckets( ValueInternalLink *links ) = 0; + virtual ValueInternalLink *allocateMapLink() = 0; + virtual void releaseMapLink( ValueInternalLink *link ) = 0; + }; + + /** \brief ValueInternalMap hash-map bucket chain link (for internal use only). + * \internal previous_ & next_ allows for bidirectional traversal. + */ + class JSON_API ValueInternalLink + { + public: + enum { itemPerLink = 6 }; // sizeof(ValueInternalLink) = 128 on 32 bits architecture. + enum InternalFlags { + flagAvailable = 0, + flagUsed = 1 + }; + + ValueInternalLink(); + + ~ValueInternalLink(); + + Value items_[itemPerLink]; + char *keys_[itemPerLink]; + ValueInternalLink *previous_; + ValueInternalLink *next_; + }; + + + /** \brief A linked page based hash-table implementation used internally by Value. + * \internal ValueInternalMap is a tradional bucket based hash-table, with a linked + * list in each bucket to handle collision. There is an addional twist in that + * each node of the collision linked list is a page containing a fixed amount of + * value. This provides a better compromise between memory usage and speed. + * + * Each bucket is made up of a chained list of ValueInternalLink. The last + * link of a given bucket can be found in the 'previous_' field of the following bucket. + * The last link of the last bucket is stored in tailLink_ as it has no following bucket. + * Only the last link of a bucket may contains 'available' item. The last link always + * contains at least one element unless is it the bucket one very first link. + */ + class JSON_API ValueInternalMap + { + friend class ValueIteratorBase; + friend class Value; + public: + typedef unsigned int HashKey; + typedef unsigned int BucketIndex; + +# ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION + struct IteratorState + { + IteratorState() + : map_(0) + , link_(0) + , itemIndex_(0) + , bucketIndex_(0) + { + } + ValueInternalMap *map_; + ValueInternalLink *link_; + BucketIndex itemIndex_; + BucketIndex bucketIndex_; + }; +# endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION + + ValueInternalMap(); + ValueInternalMap( const ValueInternalMap &other ); + ValueInternalMap &operator =( const ValueInternalMap &other ); + ~ValueInternalMap(); + + void swap( ValueInternalMap &other ); + + BucketIndex size() const; + + void clear(); + + bool reserveDelta( BucketIndex growth ); + + bool reserve( BucketIndex newItemCount ); + + const Value *find( const char *key ) const; + + Value *find( const char *key ); + + Value &resolveReference( const char *key, + bool isStatic ); + + void remove( const char *key ); + + void doActualRemove( ValueInternalLink *link, + BucketIndex index, + BucketIndex bucketIndex ); + + ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex ); + + Value &setNewItem( const char *key, + bool isStatic, + ValueInternalLink *link, + BucketIndex index ); + + Value &unsafeAdd( const char *key, + bool isStatic, + HashKey hashedKey ); + + HashKey hash( const char *key ) const; + + int compare( const ValueInternalMap &other ) const; + + private: + void makeBeginIterator( IteratorState &it ) const; + void makeEndIterator( IteratorState &it ) const; + static bool equals( const IteratorState &x, const IteratorState &other ); + static void increment( IteratorState &iterator ); + static void incrementBucket( IteratorState &iterator ); + static void decrement( IteratorState &iterator ); + static const char *key( const IteratorState &iterator ); + static const char *key( const IteratorState &iterator, bool &isStatic ); + static Value &value( const IteratorState &iterator ); + static int distance( const IteratorState &x, const IteratorState &y ); + + private: + ValueInternalLink *buckets_; + ValueInternalLink *tailLink_; + BucketIndex bucketsSize_; + BucketIndex itemCount_; + }; + + /** \brief A simplified deque implementation used internally by Value. + * \internal + * It is based on a list of fixed "page", each page contains a fixed number of items. + * Instead of using a linked-list, a array of pointer is used for fast item look-up. + * Look-up for an element is as follow: + * - compute page index: pageIndex = itemIndex / itemsPerPage + * - look-up item in page: pages_[pageIndex][itemIndex % itemsPerPage] + * + * Insertion is amortized constant time (only the array containing the index of pointers + * need to be reallocated when items are appended). + */ + class JSON_API ValueInternalArray + { + friend class Value; + friend class ValueIteratorBase; + public: + enum { itemsPerPage = 8 }; // should be a power of 2 for fast divide and modulo. + typedef Value::ArrayIndex ArrayIndex; + typedef unsigned int PageIndex; + +# ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION + struct IteratorState // Must be a POD + { + IteratorState() + : array_(0) + , currentPageIndex_(0) + , currentItemIndex_(0) + { + } + ValueInternalArray *array_; + Value **currentPageIndex_; + unsigned int currentItemIndex_; + }; +# endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION + + ValueInternalArray(); + ValueInternalArray( const ValueInternalArray &other ); + ValueInternalArray &operator =( const ValueInternalArray &other ); + ~ValueInternalArray(); + void swap( ValueInternalArray &other ); + + void clear(); + void resize( ArrayIndex newSize ); + + Value &resolveReference( ArrayIndex index ); + + Value *find( ArrayIndex index ) const; + + ArrayIndex size() const; + + int compare( const ValueInternalArray &other ) const; + + private: + static bool equals( const IteratorState &x, const IteratorState &other ); + static void increment( IteratorState &iterator ); + static void decrement( IteratorState &iterator ); + static Value &dereference( const IteratorState &iterator ); + static Value &unsafeDereference( const IteratorState &iterator ); + static int distance( const IteratorState &x, const IteratorState &y ); + static ArrayIndex indexOf( const IteratorState &iterator ); + void makeBeginIterator( IteratorState &it ) const; + void makeEndIterator( IteratorState &it ) const; + void makeIterator( IteratorState &it, ArrayIndex index ) const; + + void makeIndexValid( ArrayIndex index ); + + Value **pages_; + ArrayIndex size_; + PageIndex pageCount_; + }; + + /** \brief Experimental: do not use. Allocator to customize Value internal array. + * Below is an example of a simple implementation (actual implementation use + * memory pool). + \code +class DefaultValueArrayAllocator : public ValueArrayAllocator +{ +public: // overridden from ValueArrayAllocator + virtual ~DefaultValueArrayAllocator() + { + } + + virtual ValueInternalArray *newArray() + { + return new ValueInternalArray(); + } + + virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) + { + return new ValueInternalArray( other ); + } + + virtual void destruct( ValueInternalArray *array ) + { + delete array; + } + + virtual void reallocateArrayPageIndex( Value **&indexes, + ValueInternalArray::PageIndex &indexCount, + ValueInternalArray::PageIndex minNewIndexCount ) + { + ValueInternalArray::PageIndex newIndexCount = (indexCount*3)/2 + 1; + if ( minNewIndexCount > newIndexCount ) + newIndexCount = minNewIndexCount; + void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount ); + if ( !newIndexes ) + throw std::bad_alloc(); + indexCount = newIndexCount; + indexes = static_cast( newIndexes ); + } + virtual void releaseArrayPageIndex( Value **indexes, + ValueInternalArray::PageIndex indexCount ) + { + if ( indexes ) + free( indexes ); + } + + virtual Value *allocateArrayPage() + { + return static_cast( malloc( sizeof(Value) * ValueInternalArray::itemsPerPage ) ); + } + + virtual void releaseArrayPage( Value *value ) + { + if ( value ) + free( value ); + } +}; + \endcode + */ + class JSON_API ValueArrayAllocator + { + public: + virtual ~ValueArrayAllocator(); + virtual ValueInternalArray *newArray() = 0; + virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) = 0; + virtual void destructArray( ValueInternalArray *array ) = 0; + /** \brief Reallocate array page index. + * Reallocates an array of pointer on each page. + * \param indexes [input] pointer on the current index. May be \c NULL. + * [output] pointer on the new index of at least + * \a minNewIndexCount pages. + * \param indexCount [input] current number of pages in the index. + * [output] number of page the reallocated index can handle. + * \b MUST be >= \a minNewIndexCount. + * \param minNewIndexCount Minimum number of page the new index must be able to + * handle. + */ + virtual void reallocateArrayPageIndex( Value **&indexes, + ValueInternalArray::PageIndex &indexCount, + ValueInternalArray::PageIndex minNewIndexCount ) = 0; + virtual void releaseArrayPageIndex( Value **indexes, + ValueInternalArray::PageIndex indexCount ) = 0; + virtual Value *allocateArrayPage() = 0; + virtual void releaseArrayPage( Value *value ) = 0; + }; +#endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP + + + /** \brief base class for Value iterators. + * + */ + class ValueIteratorBase + { + public: + typedef unsigned int size_t; + typedef int difference_type; + typedef ValueIteratorBase SelfType; + + ValueIteratorBase(); +#ifndef JSON_VALUE_USE_INTERNAL_MAP + explicit ValueIteratorBase( const Value::ObjectValues::iterator ¤t ); +#else + ValueIteratorBase( const ValueInternalArray::IteratorState &state ); + ValueIteratorBase( const ValueInternalMap::IteratorState &state ); +#endif + + bool operator ==( const SelfType &other ) const + { + return isEqual( other ); + } + + bool operator !=( const SelfType &other ) const + { + return !isEqual( other ); + } + + difference_type operator -( const SelfType &other ) const + { + return computeDistance( other ); + } + + /// Return either the index or the member name of the referenced value as a Value. + Value key() const; + + /// Return the index of the referenced Value. -1 if it is not an arrayValue. + UInt index() const; + + /// Return the member name of the referenced Value. "" if it is not an objectValue. + const char *memberName() const; + + protected: + Value &deref() const; + + void increment(); + + void decrement(); + + difference_type computeDistance( const SelfType &other ) const; + + bool isEqual( const SelfType &other ) const; + + void copy( const SelfType &other ); + + private: +#ifndef JSON_VALUE_USE_INTERNAL_MAP + Value::ObjectValues::iterator current_; + // Indicates that iterator is for a null value. + bool isNull_; +#else + union + { + ValueInternalArray::IteratorState array_; + ValueInternalMap::IteratorState map_; + } iterator_; + bool isArray_; +#endif + }; + + /** \brief const iterator for object and array value. + * + */ + class ValueConstIterator : public ValueIteratorBase + { + friend class Value; + public: + typedef unsigned int size_t; + typedef int difference_type; + typedef const Value &reference; + typedef const Value *pointer; + typedef ValueConstIterator SelfType; + + ValueConstIterator(); + private: + /*! \internal Use by Value to create an iterator. + */ +#ifndef JSON_VALUE_USE_INTERNAL_MAP + explicit ValueConstIterator( const Value::ObjectValues::iterator ¤t ); +#else + ValueConstIterator( const ValueInternalArray::IteratorState &state ); + ValueConstIterator( const ValueInternalMap::IteratorState &state ); +#endif + public: + SelfType &operator =( const ValueIteratorBase &other ); + + SelfType operator++( int ) + { + SelfType temp( *this ); + ++*this; + return temp; + } + + SelfType operator--( int ) + { + SelfType temp( *this ); + --*this; + return temp; + } + + SelfType &operator--() + { + decrement(); + return *this; + } + + SelfType &operator++() + { + increment(); + return *this; + } + + reference operator *() const + { + return deref(); + } + }; + + + /** \brief Iterator for object and array value. + */ + class ValueIterator : public ValueIteratorBase + { + friend class Value; + public: + typedef unsigned int size_t; + typedef int difference_type; + typedef Value &reference; + typedef Value *pointer; + typedef ValueIterator SelfType; + + ValueIterator(); + ValueIterator( const ValueConstIterator &other ); + ValueIterator( const ValueIterator &other ); + private: + /*! \internal Use by Value to create an iterator. + */ +#ifndef JSON_VALUE_USE_INTERNAL_MAP + explicit ValueIterator( const Value::ObjectValues::iterator ¤t ); +#else + ValueIterator( const ValueInternalArray::IteratorState &state ); + ValueIterator( const ValueInternalMap::IteratorState &state ); +#endif + public: + + SelfType &operator =( const SelfType &other ); + + SelfType operator++( int ) + { + SelfType temp( *this ); + ++*this; + return temp; + } + + SelfType operator--( int ) + { + SelfType temp( *this ); + --*this; + return temp; + } + + SelfType &operator--() + { + decrement(); + return *this; + } + + SelfType &operator++() + { + increment(); + return *this; + } + + reference operator *() const + { + return deref(); + } + }; + + +} // namespace Json + + +#endif // CPPTL_JSON_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: include/json/value.h +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: include/json/reader.h +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef CPPTL_JSON_READER_H_INCLUDED +# define CPPTL_JSON_READER_H_INCLUDED + +#if !defined(JSON_IS_AMALGAMATION) +# include "features.h" +# include "value.h" +#endif // if !defined(JSON_IS_AMALGAMATION) +# include +# include +# include + +namespace Json { + + /** \brief Unserialize a JSON document into a Value. + * + */ + class JSON_API Reader + { + public: + typedef char Char; + typedef const Char *Location; + + /** \brief Constructs a Reader allowing all features + * for parsing. + */ + Reader(); + + /** \brief Constructs a Reader allowing the specified feature set + * for parsing. + */ + Reader( const Features &features ); + + /** \brief Read a Value from a JSON document. + * \param document UTF-8 encoded string containing the document to read. + * \param root [out] Contains the root value of the document if it was + * successfully parsed. + * \param collectComments \c true to collect comment and allow writing them back during + * serialization, \c false to discard comments. + * This parameter is ignored if Features::allowComments_ + * is \c false. + * \return \c true if the document was successfully parsed, \c false if an error occurred. + */ + bool parse( const std::string &document, + Value &root, + bool collectComments = true ); + + /** \brief Read a Value from a JSON document. + * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the document to read. + * \param endDoc Pointer on the end of the UTF-8 encoded string of the document to read. + \ Must be >= beginDoc. + * \param root [out] Contains the root value of the document if it was + * successfully parsed. + * \param collectComments \c true to collect comment and allow writing them back during + * serialization, \c false to discard comments. + * This parameter is ignored if Features::allowComments_ + * is \c false. + * \return \c true if the document was successfully parsed, \c false if an error occurred. + */ + bool parse( const char *beginDoc, const char *endDoc, + Value &root, + bool collectComments = true ); + + /// \brief Parse from input stream. + /// \see Json::operator>>(std::istream&, Json::Value&). + bool parse( std::istream &is, + Value &root, + bool collectComments = true ); + + /** \brief Returns a user friendly string that list errors in the parsed document. + * \return Formatted error message with the list of errors with their location in + * the parsed document. An empty string is returned if no error occurred + * during parsing. + * \deprecated Use getFormattedErrorMessages() instead (typo fix). + */ + JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead") + std::string getFormatedErrorMessages() const; + + /** \brief Returns a user friendly string that list errors in the parsed document. + * \return Formatted error message with the list of errors with their location in + * the parsed document. An empty string is returned if no error occurred + * during parsing. + */ + std::string getFormattedErrorMessages() const; + + private: + enum TokenType + { + tokenEndOfStream = 0, + tokenObjectBegin, + tokenObjectEnd, + tokenArrayBegin, + tokenArrayEnd, + tokenString, + tokenNumber, + tokenTrue, + tokenFalse, + tokenNull, + tokenArraySeparator, + tokenMemberSeparator, + tokenComment, + tokenError + }; + + class Token + { + public: + TokenType type_; + Location start_; + Location end_; + }; + + class ErrorInfo + { + public: + Token token_; + std::string message_; + Location extra_; + }; + + typedef std::deque Errors; + + bool expectToken( TokenType type, Token &token, const char *message ); + bool readToken( Token &token ); + void skipSpaces(); + bool match( Location pattern, + int patternLength ); + bool readComment(); + bool readCStyleComment(); + bool readCppStyleComment(); + bool readString(); + void readNumber(); + bool readValue(); + bool readObject( Token &token ); + bool readArray( Token &token ); + bool decodeNumber( Token &token ); + bool decodeString( Token &token ); + bool decodeString( Token &token, std::string &decoded ); + bool decodeDouble( Token &token ); + bool decodeUnicodeCodePoint( Token &token, + Location ¤t, + Location end, + unsigned int &unicode ); + bool decodeUnicodeEscapeSequence( Token &token, + Location ¤t, + Location end, + unsigned int &unicode ); + bool addError( const std::string &message, + Token &token, + Location extra = 0 ); + bool recoverFromError( TokenType skipUntilToken ); + bool addErrorAndRecover( const std::string &message, + Token &token, + TokenType skipUntilToken ); + void skipUntilSpace(); + Value ¤tValue(); + Char getNextChar(); + void getLocationLineAndColumn( Location location, + int &line, + int &column ) const; + std::string getLocationLineAndColumn( Location location ) const; + void addComment( Location begin, + Location end, + CommentPlacement placement ); + void skipCommentTokens( Token &token ); + + typedef std::stack Nodes; + Nodes nodes_; + Errors errors_; + std::string document_; + Location begin_; + Location end_; + Location current_; + Location lastValueEnd_; + Value *lastValue_; + std::string commentsBefore_; + Features features_; + bool collectComments_; + }; + + /** \brief Read from 'sin' into 'root'. + + Always keep comments from the input JSON. + + This can be used to read a file into a particular sub-object. + For example: + \code + Json::Value root; + cin >> root["dir"]["file"]; + cout << root; + \endcode + Result: + \verbatim + { + "dir": { + "file": { + // The input stream JSON would be nested here. + } + } + } + \endverbatim + \throw std::exception on parse error. + \see Json::operator<<() + */ + std::istream& operator>>( std::istream&, Value& ); + +} // namespace Json + +#endif // CPPTL_JSON_READER_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: include/json/reader.h +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: include/json/writer.h +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef JSON_WRITER_H_INCLUDED +# define JSON_WRITER_H_INCLUDED + +#if !defined(JSON_IS_AMALGAMATION) +# include "value.h" +#endif // if !defined(JSON_IS_AMALGAMATION) +# include +# include + +namespace Json { + + class Value; + + /** \brief Abstract class for writers. + */ + class JSON_API Writer + { + public: + virtual ~Writer(); + + virtual std::string write( const Value &root ) = 0; + }; + + /** \brief Outputs a Value in JSON format without formatting (not human friendly). + * + * The JSON document is written in a single line. It is not intended for 'human' consumption, + * but may be usefull to support feature such as RPC where bandwith is limited. + * \sa Reader, Value + */ + class JSON_API FastWriter : public Writer + { + public: + FastWriter(); + virtual ~FastWriter(){} + + void enableYAMLCompatibility(); + + /** \brief Drop the "null" string from the writer's output for nullValues. + * Strictly speaking, this is not valid JSON. But when the output is being + * fed to a browser's Javascript, it makes for smaller output and the + * browser can handle the output just fine. + */ + void dropNullPlaceholders(); + + public: // overridden from Writer + virtual std::string write( const Value &root ); + + private: + void writeValue( const Value &value ); + + std::string document_; + bool yamlCompatiblityEnabled_; + bool dropNullPlaceholders_; + }; + + /** \brief Writes a Value in JSON format in a human friendly way. + * + * The rules for line break and indent are as follow: + * - Object value: + * - if empty then print {} without indent and line break + * - if not empty the print '{', line break & indent, print one value per line + * and then unindent and line break and print '}'. + * - Array value: + * - if empty then print [] without indent and line break + * - if the array contains no object value, empty array or some other value types, + * and all the values fit on one lines, then print the array on a single line. + * - otherwise, it the values do not fit on one line, or the array contains + * object or non empty array, then print one value per line. + * + * If the Value have comments then they are outputed according to their #CommentPlacement. + * + * \sa Reader, Value, Value::setComment() + */ + class JSON_API StyledWriter: public Writer + { + public: + StyledWriter(); + virtual ~StyledWriter(){} + + public: // overridden from Writer + /** \brief Serialize a Value in JSON format. + * \param root Value to serialize. + * \return String containing the JSON document that represents the root value. + */ + virtual std::string write( const Value &root ); + + private: + void writeValue( const Value &value ); + void writeArrayValue( const Value &value ); + bool isMultineArray( const Value &value ); + void pushValue( const std::string &value ); + void writeIndent(); + void writeWithIndent( const std::string &value ); + void indent(); + void unindent(); + void writeCommentBeforeValue( const Value &root ); + void writeCommentAfterValueOnSameLine( const Value &root ); + bool hasCommentForValue( const Value &value ); + static std::string normalizeEOL( const std::string &text ); + + typedef std::vector ChildValues; + + ChildValues childValues_; + std::string document_; + std::string indentString_; + int rightMargin_; + int indentSize_; + bool addChildValues_; + }; + + /** \brief Writes a Value in JSON format in a human friendly way, + to a stream rather than to a string. + * + * The rules for line break and indent are as follow: + * - Object value: + * - if empty then print {} without indent and line break + * - if not empty the print '{', line break & indent, print one value per line + * and then unindent and line break and print '}'. + * - Array value: + * - if empty then print [] without indent and line break + * - if the array contains no object value, empty array or some other value types, + * and all the values fit on one lines, then print the array on a single line. + * - otherwise, it the values do not fit on one line, or the array contains + * object or non empty array, then print one value per line. + * + * If the Value have comments then they are outputed according to their #CommentPlacement. + * + * \param indentation Each level will be indented by this amount extra. + * \sa Reader, Value, Value::setComment() + */ + class JSON_API StyledStreamWriter + { + public: + StyledStreamWriter( std::string indentation="\t" ); + ~StyledStreamWriter(){} + + public: + /** \brief Serialize a Value in JSON format. + * \param out Stream to write to. (Can be ostringstream, e.g.) + * \param root Value to serialize. + * \note There is no point in deriving from Writer, since write() should not return a value. + */ + void write( std::ostream &out, const Value &root ); + + private: + void writeValue( const Value &value ); + void writeArrayValue( const Value &value ); + bool isMultineArray( const Value &value ); + void pushValue( const std::string &value ); + void writeIndent(); + void writeWithIndent( const std::string &value ); + void indent(); + void unindent(); + void writeCommentBeforeValue( const Value &root ); + void writeCommentAfterValueOnSameLine( const Value &root ); + bool hasCommentForValue( const Value &value ); + static std::string normalizeEOL( const std::string &text ); + + typedef std::vector ChildValues; + + ChildValues childValues_; + std::ostream* document_; + std::string indentString_; + int rightMargin_; + std::string indentation_; + bool addChildValues_; + }; + +# if defined(JSON_HAS_INT64) + std::string JSON_API valueToString( Int value ); + std::string JSON_API valueToString( UInt value ); +# endif // if defined(JSON_HAS_INT64) + std::string JSON_API valueToString( LargestInt value ); + std::string JSON_API valueToString( LargestUInt value ); + std::string JSON_API valueToString( double value ); + std::string JSON_API valueToString( bool value ); + std::string JSON_API valueToQuotedString( const char *value ); + + /// \brief Output using the StyledStreamWriter. + /// \see Json::operator>>() + std::ostream& operator<<( std::ostream&, const Value &root ); + +} // namespace Json + + + +#endif // JSON_WRITER_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: include/json/writer.h +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: include/json/assertions.h +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef CPPTL_JSON_ASSERTIONS_H_INCLUDED +# define CPPTL_JSON_ASSERTIONS_H_INCLUDED + +#include + +#if !defined(JSON_IS_AMALGAMATION) +# include +#endif // if !defined(JSON_IS_AMALGAMATION) + +#if JSON_USE_EXCEPTION +#define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw +#define JSON_FAIL_MESSAGE( message ) throw std::runtime_error( message ); +#else // JSON_USE_EXCEPTION +#define JSON_ASSERT( condition ) assert( condition ); + +// The call to assert() will show the failure message in debug builds. In +// release bugs we write to invalid memory in order to crash hard, so that a +// debugger or crash reporter gets the chance to take over. We still call exit() +// afterward in order to tell the compiler that this macro doesn't return. +#define JSON_FAIL_MESSAGE( message ) { assert(false && message); strcpy(reinterpret_cast(666), message); exit(123); } + +#endif + +#define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) { JSON_FAIL_MESSAGE( message ) } + +#endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: include/json/assertions.h +// ////////////////////////////////////////////////////////////////////// + + + + + +#endif //ifndef JSON_AMALGATED_H_INCLUDED diff --git a/src/jsoncpp/json/jsoncpp.cpp b/src/jsoncpp/json/jsoncpp.cpp new file mode 100644 index 000000000..7a04736de --- /dev/null +++ b/src/jsoncpp/json/jsoncpp.cpp @@ -0,0 +1,4367 @@ +/// Json-cpp amalgated source (http://jsoncpp.sourceforge.net/). +/// It is intented to be used with #include + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: LICENSE +// ////////////////////////////////////////////////////////////////////// + +/* +The JsonCpp library's source code, including accompanying documentation, +tests and demonstration applications, are licensed under the following +conditions... + +The author (Baptiste Lepilleur) explicitly disclaims copyright in all +jurisdictions which recognize such a disclaimer. In such jurisdictions, +this software is released into the Public Domain. + +In jurisdictions which do not recognize Public Domain property (e.g. Germany as of +2010), this software is Copyright (c) 2007-2010 by Baptiste Lepilleur, and is +released under the terms of the MIT License (see below). + +In jurisdictions which recognize Public Domain property, the user of this +software may choose to accept it either as 1) Public Domain, 2) under the +conditions of the MIT License (see below), or 3) under the terms of dual +Public Domain/MIT License conditions described here, as they choose. + +The MIT License is about as close to Public Domain as a license can get, and is +described in clear, concise terms at: + + http://en.wikipedia.org/wiki/MIT_License + +The full text of the MIT License follows: + +======================================================================== +Copyright (c) 2007-2010 Baptiste Lepilleur + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, copy, +modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +======================================================================== +(END LICENSE TEXT) + +The MIT license is compatible with both the GPL and commercial +software, affording one all of the rights of Public Domain with the +minor nuisance of being required to keep the above copyright notice +and license text in the source code. Note also that by accepting the +Public Domain "license" you can re-license your copy using whatever +license you like. + +*/ + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: LICENSE +// ////////////////////////////////////////////////////////////////////// + + + + + + +#include "json.h" + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: src/lib_json/json_tool.h +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef LIB_JSONCPP_JSON_TOOL_H_INCLUDED +# define LIB_JSONCPP_JSON_TOOL_H_INCLUDED + +/* This header provides common string manipulation support, such as UTF-8, + * portable conversion from/to string... + * + * It is an internal header that must not be exposed. + */ + +namespace Json { + +/// Converts a unicode code-point to UTF-8. +static inline std::string +codePointToUTF8(unsigned int cp) +{ + std::string result; + + // based on description from http://en.wikipedia.org/wiki/UTF-8 + + if (cp <= 0x7f) + { + result.resize(1); + result[0] = static_cast(cp); + } + else if (cp <= 0x7FF) + { + result.resize(2); + result[1] = static_cast(0x80 | (0x3f & cp)); + result[0] = static_cast(0xC0 | (0x1f & (cp >> 6))); + } + else if (cp <= 0xFFFF) + { + result.resize(3); + result[2] = static_cast(0x80 | (0x3f & cp)); + result[1] = 0x80 | static_cast((0x3f & (cp >> 6))); + result[0] = 0xE0 | static_cast((0xf & (cp >> 12))); + } + else if (cp <= 0x10FFFF) + { + result.resize(4); + result[3] = static_cast(0x80 | (0x3f & cp)); + result[2] = static_cast(0x80 | (0x3f & (cp >> 6))); + result[1] = static_cast(0x80 | (0x3f & (cp >> 12))); + result[0] = static_cast(0xF0 | (0x7 & (cp >> 18))); + } + + return result; +} + + +/// Returns true if ch is a control character (in range [0,32[). +static inline bool +isControlCharacter(char ch) +{ + return ch > 0 && ch <= 0x1F; +} + + +enum { + /// Constant that specify the size of the buffer that must be passed to uintToString. + uintToStringBufferSize = 3*sizeof(LargestUInt)+1 +}; + +// Defines a char buffer for use with uintToString(). +typedef char UIntToStringBuffer[uintToStringBufferSize]; + + +/** Converts an unsigned integer to string. + * @param value Unsigned interger to convert to string + * @param current Input/Output string buffer. + * Must have at least uintToStringBufferSize chars free. + */ +static inline void +uintToString( LargestUInt value, + char *¤t ) +{ + *--current = 0; + do + { + *--current = char(value % 10) + '0'; + value /= 10; + } + while ( value != 0 ); +} + +} // namespace Json { + +#endif // LIB_JSONCPP_JSON_TOOL_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: src/lib_json/json_tool.h +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: src/lib_json/json_reader.cpp +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2011 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#if !defined(JSON_IS_AMALGAMATION) +# include +# include +# include +# include "json_tool.h" +#endif // if !defined(JSON_IS_AMALGAMATION) +#include +#include +#include +#include +#include + +#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0 +#pragma warning( disable : 4996 ) // disable warning about strdup being deprecated. +#endif + +namespace Json { + +// Implementation of class Features +// //////////////////////////////// + +Features::Features() + : allowComments_( true ) + , strictRoot_( false ) +{ +} + + +Features +Features::all() +{ + return Features(); +} + + +Features +Features::strictMode() +{ + Features features; + features.allowComments_ = false; + features.strictRoot_ = true; + return features; +} + +// Implementation of class Reader +// //////////////////////////////// + + +static inline bool +in( Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4 ) +{ + return c == c1 || c == c2 || c == c3 || c == c4; +} + +static inline bool +in( Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4, Reader::Char c5 ) +{ + return c == c1 || c == c2 || c == c3 || c == c4 || c == c5; +} + + +static bool +containsNewLine( Reader::Location begin, + Reader::Location end ) +{ + for ( ;begin < end; ++begin ) + if ( *begin == '\n' || *begin == '\r' ) + return true; + return false; +} + + +// Class Reader +// ////////////////////////////////////////////////////////////////// + +Reader::Reader() + : errors_(), + document_(), + begin_(), + end_(), + current_(), + lastValueEnd_(), + lastValue_(), + commentsBefore_(), + features_( Features::all() ), + collectComments_() +{ +} + + +Reader::Reader( const Features &features ) + : errors_(), + document_(), + begin_(), + end_(), + current_(), + lastValueEnd_(), + lastValue_(), + commentsBefore_(), + features_( features ), + collectComments_() +{ +} + + +bool +Reader::parse( const std::string &document, + Value &root, + bool collectComments ) +{ + document_ = document; + const char *begin = document_.c_str(); + const char *end = begin + document_.length(); + return parse( begin, end, root, collectComments ); +} + + +bool +Reader::parse( std::istream& sin, + Value &root, + bool collectComments ) +{ + //std::istream_iterator begin(sin); + //std::istream_iterator end; + // Those would allow streamed input from a file, if parse() were a + // template function. + + // Since std::string is reference-counted, this at least does not + // create an extra copy. + std::string doc; + std::getline(sin, doc, (char)EOF); + return parse( doc, root, collectComments ); +} + +bool +Reader::parse( const char *beginDoc, const char *endDoc, + Value &root, + bool collectComments ) +{ + if ( !features_.allowComments_ ) + { + collectComments = false; + } + + begin_ = beginDoc; + end_ = endDoc; + collectComments_ = collectComments; + current_ = begin_; + lastValueEnd_ = 0; + lastValue_ = 0; + commentsBefore_ = ""; + errors_.clear(); + while ( !nodes_.empty() ) + nodes_.pop(); + nodes_.push( &root ); + + bool successful = readValue(); + Token token; + skipCommentTokens( token ); + if ( collectComments_ && !commentsBefore_.empty() ) + root.setComment( commentsBefore_, commentAfter ); + if ( features_.strictRoot_ ) + { + if ( !root.isArray() && !root.isObject() ) + { + // Set error location to start of doc, ideally should be first token found in doc + token.type_ = tokenError; + token.start_ = beginDoc; + token.end_ = endDoc; + addError( "A valid JSON document must be either an array or an object value.", + token ); + return false; + } + } + return successful; +} + + +bool +Reader::readValue() +{ + Token token; + skipCommentTokens( token ); + bool successful = true; + + if ( collectComments_ && !commentsBefore_.empty() ) + { + currentValue().setComment( commentsBefore_, commentBefore ); + commentsBefore_ = ""; + } + + + switch ( token.type_ ) + { + case tokenObjectBegin: + successful = readObject( token ); + break; + case tokenArrayBegin: + successful = readArray( token ); + break; + case tokenNumber: + successful = decodeNumber( token ); + break; + case tokenString: + successful = decodeString( token ); + break; + case tokenTrue: + currentValue() = true; + break; + case tokenFalse: + currentValue() = false; + break; + case tokenNull: + currentValue() = Value(); + break; + default: + return addError( "Syntax error: value, object or array expected.", token ); + } + + if ( collectComments_ ) + { + lastValueEnd_ = current_; + lastValue_ = ¤tValue(); + } + + return successful; +} + + +void +Reader::skipCommentTokens( Token &token ) +{ + if ( features_.allowComments_ ) + { + do + { + readToken( token ); + } + while ( token.type_ == tokenComment ); + } + else + { + readToken( token ); + } +} + + +bool +Reader::expectToken( TokenType type, Token &token, const char *message ) +{ + readToken( token ); + if ( token.type_ != type ) + return addError( message, token ); + return true; +} + + +bool +Reader::readToken( Token &token ) +{ + skipSpaces(); + token.start_ = current_; + Char c = getNextChar(); + bool ok = true; + switch ( c ) + { + case '{': + token.type_ = tokenObjectBegin; + break; + case '}': + token.type_ = tokenObjectEnd; + break; + case '[': + token.type_ = tokenArrayBegin; + break; + case ']': + token.type_ = tokenArrayEnd; + break; + case '"': + token.type_ = tokenString; + ok = readString(); + break; + case '/': + token.type_ = tokenComment; + ok = readComment(); + break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '-': + token.type_ = tokenNumber; + readNumber(); + break; + case 't': + token.type_ = tokenTrue; + ok = match( "rue", 3 ); + break; + case 'f': + token.type_ = tokenFalse; + ok = match( "alse", 4 ); + break; + case 'n': + token.type_ = tokenNull; + ok = match( "ull", 3 ); + break; + case ',': + token.type_ = tokenArraySeparator; + break; + case ':': + token.type_ = tokenMemberSeparator; + break; + case 0: + token.type_ = tokenEndOfStream; + break; + default: + ok = false; + break; + } + if ( !ok ) + token.type_ = tokenError; + token.end_ = current_; + return true; +} + + +void +Reader::skipSpaces() +{ + while ( current_ != end_ ) + { + Char c = *current_; + if ( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) + ++current_; + else + break; + } +} + + +bool +Reader::match( Location pattern, + int patternLength ) +{ + if ( end_ - current_ < patternLength ) + return false; + int index = patternLength; + while ( index-- ) + if ( current_[index] != pattern[index] ) + return false; + current_ += patternLength; + return true; +} + + +bool +Reader::readComment() +{ + Location commentBegin = current_ - 1; + Char c = getNextChar(); + bool successful = false; + if ( c == '*' ) + successful = readCStyleComment(); + else if ( c == '/' ) + successful = readCppStyleComment(); + if ( !successful ) + return false; + + if ( collectComments_ ) + { + CommentPlacement placement = commentBefore; + if ( lastValueEnd_ && !containsNewLine( lastValueEnd_, commentBegin ) ) + { + if ( c != '*' || !containsNewLine( commentBegin, current_ ) ) + placement = commentAfterOnSameLine; + } + + addComment( commentBegin, current_, placement ); + } + return true; +} + + +void +Reader::addComment( Location begin, + Location end, + CommentPlacement placement ) +{ + assert( collectComments_ ); + if ( placement == commentAfterOnSameLine ) + { + assert( lastValue_ != 0 ); + lastValue_->setComment( std::string( begin, end ), placement ); + } + else + { + if ( !commentsBefore_.empty() ) + commentsBefore_ += "\n"; + commentsBefore_ += std::string( begin, end ); + } +} + + +bool +Reader::readCStyleComment() +{ + while ( current_ != end_ ) + { + Char c = getNextChar(); + if ( c == '*' && *current_ == '/' ) + break; + } + return getNextChar() == '/'; +} + + +bool +Reader::readCppStyleComment() +{ + while ( current_ != end_ ) + { + Char c = getNextChar(); + if ( c == '\r' || c == '\n' ) + break; + } + return true; +} + + +void +Reader::readNumber() +{ + while ( current_ != end_ ) + { + if ( !(*current_ >= '0' && *current_ <= '9') && + !in( *current_, '.', 'e', 'E', '+', '-' ) ) + break; + ++current_; + } +} + +bool +Reader::readString() +{ + Char c = 0; + while ( current_ != end_ ) + { + c = getNextChar(); + if ( c == '\\' ) + getNextChar(); + else if ( c == '"' ) + break; + } + return c == '"'; +} + + +bool +Reader::readObject( Token &/*tokenStart*/ ) +{ + Token tokenName; + std::string name; + currentValue() = Value( objectValue ); + while ( readToken( tokenName ) ) + { + bool initialTokenOk = true; + while ( tokenName.type_ == tokenComment && initialTokenOk ) + initialTokenOk = readToken( tokenName ); + if ( !initialTokenOk ) + break; + if ( tokenName.type_ == tokenObjectEnd && name.empty() ) // empty object + return true; + if ( tokenName.type_ != tokenString ) + break; + + name = ""; + if ( !decodeString( tokenName, name ) ) + return recoverFromError( tokenObjectEnd ); + + Token colon; + if ( !readToken( colon ) || colon.type_ != tokenMemberSeparator ) + { + return addErrorAndRecover( "Missing ':' after object member name", + colon, + tokenObjectEnd ); + } + Value &value = currentValue()[ name ]; + nodes_.push( &value ); + bool ok = readValue(); + nodes_.pop(); + if ( !ok ) // error already set + return recoverFromError( tokenObjectEnd ); + + Token comma; + if ( !readToken( comma ) + || ( comma.type_ != tokenObjectEnd && + comma.type_ != tokenArraySeparator && + comma.type_ != tokenComment ) ) + { + return addErrorAndRecover( "Missing ',' or '}' in object declaration", + comma, + tokenObjectEnd ); + } + bool finalizeTokenOk = true; + while ( comma.type_ == tokenComment && + finalizeTokenOk ) + finalizeTokenOk = readToken( comma ); + if ( comma.type_ == tokenObjectEnd ) + return true; + } + return addErrorAndRecover( "Missing '}' or object member name", + tokenName, + tokenObjectEnd ); +} + + +bool +Reader::readArray( Token &/*tokenStart*/ ) +{ + currentValue() = Value( arrayValue ); + skipSpaces(); + if ( *current_ == ']' ) // empty array + { + Token endArray; + readToken( endArray ); + return true; + } + int index = 0; + for (;;) + { + Value &value = currentValue()[ index++ ]; + nodes_.push( &value ); + bool ok = readValue(); + nodes_.pop(); + if ( !ok ) // error already set + return recoverFromError( tokenArrayEnd ); + + Token token; + // Accept Comment after last item in the array. + ok = readToken( token ); + while ( token.type_ == tokenComment && ok ) + { + ok = readToken( token ); + } + bool badTokenType = ( token.type_ != tokenArraySeparator && + token.type_ != tokenArrayEnd ); + if ( !ok || badTokenType ) + { + return addErrorAndRecover( "Missing ',' or ']' in array declaration", + token, + tokenArrayEnd ); + } + if ( token.type_ == tokenArrayEnd ) + break; + } + return true; +} + + +bool +Reader::decodeNumber( Token &token ) +{ + bool isDouble = false; + for ( Location inspect = token.start_; inspect != token.end_; ++inspect ) + { + isDouble = isDouble + || in( *inspect, '.', 'e', 'E', '+' ) + || ( *inspect == '-' && inspect != token.start_ ); + } + if ( isDouble ) + return decodeDouble( token ); + // Attempts to parse the number as an integer. If the number is + // larger than the maximum supported value of an integer then + // we decode the number as a double. + Location current = token.start_; + bool isNegative = *current == '-'; + if ( isNegative ) + ++current; + Value::LargestUInt maxIntegerValue = isNegative ? Value::LargestUInt(-Value::minLargestInt) + : Value::maxLargestUInt; + Value::LargestUInt threshold = maxIntegerValue / 10; + Value::LargestUInt value = 0; + while ( current < token.end_ ) + { + Char c = *current++; + if ( c < '0' || c > '9' ) + return addError( "'" + std::string( token.start_, token.end_ ) + "' is not a number.", token ); + Value::UInt digit(c - '0'); + if ( value >= threshold ) + { + // We've hit or exceeded the max value divided by 10 (rounded down). If + // a) we've only just touched the limit, b) this is the last digit, and + // c) it's small enough to fit in that rounding delta, we're okay. + // Otherwise treat this number as a double to avoid overflow. + if (value > threshold || + current != token.end_ || + digit > maxIntegerValue % 10) + { + return decodeDouble( token ); + } + } + value = value * 10 + digit; + } + if ( isNegative ) + currentValue() = -Value::LargestInt( value ); + else if ( value <= Value::LargestUInt(Value::maxInt) ) + currentValue() = Value::LargestInt( value ); + else + currentValue() = value; + return true; +} + + +bool +Reader::decodeDouble( Token &token ) +{ + double value = 0; + const int bufferSize = 32; + int count; + int length = int(token.end_ - token.start_); + + // Sanity check to avoid buffer overflow exploits. + if (length < 0) { + return addError( "Unable to parse token length", token ); + } + + // Avoid using a string constant for the format control string given to + // sscanf, as this can cause hard to debug crashes on OS X. See here for more + // info: + // + // http://developer.apple.com/library/mac/#DOCUMENTATION/DeveloperTools/gcc-4.0.1/gcc/Incompatibilities.html + char format[] = "%lf"; + + if ( length <= bufferSize ) + { + Char buffer[bufferSize+1]; + memcpy( buffer, token.start_, length ); + buffer[length] = 0; + count = sscanf( buffer, format, &value ); + } + else + { + std::string buffer( token.start_, token.end_ ); + count = sscanf( buffer.c_str(), format, &value ); + } + + if ( count != 1 ) + return addError( "'" + std::string( token.start_, token.end_ ) + "' is not a number.", token ); + currentValue() = value; + return true; +} + + +bool +Reader::decodeString( Token &token ) +{ + std::string decoded; + if ( !decodeString( token, decoded ) ) + return false; + currentValue() = decoded; + return true; +} + + +bool +Reader::decodeString( Token &token, std::string &decoded ) +{ + decoded.reserve( token.end_ - token.start_ - 2 ); + Location current = token.start_ + 1; // skip '"' + Location end = token.end_ - 1; // do not include '"' + while ( current != end ) + { + Char c = *current++; + if ( c == '"' ) + break; + else if ( c == '\\' ) + { + if ( current == end ) + return addError( "Empty escape sequence in string", token, current ); + Char escape = *current++; + switch ( escape ) + { + case '"': decoded += '"'; break; + case '/': decoded += '/'; break; + case '\\': decoded += '\\'; break; + case 'b': decoded += '\b'; break; + case 'f': decoded += '\f'; break; + case 'n': decoded += '\n'; break; + case 'r': decoded += '\r'; break; + case 't': decoded += '\t'; break; + case 'u': + { + unsigned int unicode; + if ( !decodeUnicodeCodePoint( token, current, end, unicode ) ) + return false; + decoded += codePointToUTF8(unicode); + } + break; + default: + return addError( "Bad escape sequence in string", token, current ); + } + } + else + { + decoded += c; + } + } + return true; +} + +bool +Reader::decodeUnicodeCodePoint( Token &token, + Location ¤t, + Location end, + unsigned int &unicode ) +{ + + if ( !decodeUnicodeEscapeSequence( token, current, end, unicode ) ) + return false; + if (unicode >= 0xD800 && unicode <= 0xDBFF) + { + // surrogate pairs + if (end - current < 6) + return addError( "additional six characters expected to parse unicode surrogate pair.", token, current ); + unsigned int surrogatePair; + if (*(current++) == '\\' && *(current++)== 'u') + { + if (decodeUnicodeEscapeSequence( token, current, end, surrogatePair )) + { + unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF); + } + else + return false; + } + else + return addError( "expecting another \\u token to begin the second half of a unicode surrogate pair", token, current ); + } + return true; +} + +bool +Reader::decodeUnicodeEscapeSequence( Token &token, + Location ¤t, + Location end, + unsigned int &unicode ) +{ + if ( end - current < 4 ) + return addError( "Bad unicode escape sequence in string: four digits expected.", token, current ); + unicode = 0; + for ( int index =0; index < 4; ++index ) + { + Char c = *current++; + unicode *= 16; + if ( c >= '0' && c <= '9' ) + unicode += c - '0'; + else if ( c >= 'a' && c <= 'f' ) + unicode += c - 'a' + 10; + else if ( c >= 'A' && c <= 'F' ) + unicode += c - 'A' + 10; + else + return addError( "Bad unicode escape sequence in string: hexadecimal digit expected.", token, current ); + } + return true; +} + + +bool +Reader::addError( const std::string &message, + Token &token, + Location extra ) +{ + ErrorInfo info; + info.token_ = token; + info.message_ = message; + info.extra_ = extra; + errors_.push_back( info ); + return false; +} + + +bool +Reader::recoverFromError( TokenType skipUntilToken ) +{ + int errorCount = int(errors_.size()); + Token skip; + for (;;) + { + if ( !readToken(skip) ) + errors_.resize( errorCount ); // discard errors caused by recovery + if ( skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream ) + break; + } + errors_.resize( errorCount ); + return false; +} + + +bool +Reader::addErrorAndRecover( const std::string &message, + Token &token, + TokenType skipUntilToken ) +{ + addError( message, token ); + return recoverFromError( skipUntilToken ); +} + + +Value & +Reader::currentValue() +{ + return *(nodes_.top()); +} + + +Reader::Char +Reader::getNextChar() +{ + if ( current_ == end_ ) + return 0; + return *current_++; +} + + +void +Reader::getLocationLineAndColumn( Location location, + int &line, + int &column ) const +{ + Location current = begin_; + Location lastLineStart = current; + line = 0; + while ( current < location && current != end_ ) + { + Char c = *current++; + if ( c == '\r' ) + { + if ( *current == '\n' ) + ++current; + lastLineStart = current; + ++line; + } + else if ( c == '\n' ) + { + lastLineStart = current; + ++line; + } + } + // column & line start at 1 + column = int(location - lastLineStart) + 1; + ++line; +} + + +std::string +Reader::getLocationLineAndColumn( Location location ) const +{ + int line, column; + getLocationLineAndColumn( location, line, column ); + char buffer[18+16+16+1]; + sprintf( buffer, "Line %d, Column %d", line, column ); + return buffer; +} + + +// Deprecated. Preserved for backward compatibility +std::string +Reader::getFormatedErrorMessages() const +{ + return getFormattedErrorMessages(); +} + + +std::string +Reader::getFormattedErrorMessages() const +{ + std::string formattedMessage; + for ( Errors::const_iterator itError = errors_.begin(); + itError != errors_.end(); + ++itError ) + { + const ErrorInfo &error = *itError; + formattedMessage += "* " + getLocationLineAndColumn( error.token_.start_ ) + "\n"; + formattedMessage += " " + error.message_ + "\n"; + if ( error.extra_ ) + formattedMessage += "See " + getLocationLineAndColumn( error.extra_ ) + " for detail.\n"; + } + return formattedMessage; +} + + +std::istream& operator>>( std::istream &sin, Value &root ) +{ + Json::Reader reader; + bool ok = reader.parse(sin, root, true); + if (!ok) { + fprintf( + stderr, + "Error from reader: %s", + reader.getFormattedErrorMessages().c_str()); + + JSON_FAIL_MESSAGE("reader error"); + } + return sin; +} + + +} // namespace Json + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: src/lib_json/json_reader.cpp +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: src/lib_json/json_batchallocator.h +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#ifndef JSONCPP_BATCHALLOCATOR_H_INCLUDED +# define JSONCPP_BATCHALLOCATOR_H_INCLUDED + +# include +# include + +# ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION + +namespace Json { + +/* Fast memory allocator. + * + * This memory allocator allocates memory for a batch of object (specified by + * the page size, the number of object in each page). + * + * It does not allow the destruction of a single object. All the allocated objects + * can be destroyed at once. The memory can be either released or reused for future + * allocation. + * + * The in-place new operator must be used to construct the object using the pointer + * returned by allocate. + */ +template +class BatchAllocator +{ +public: + BatchAllocator( unsigned int objectsPerPage = 255 ) + : freeHead_( 0 ) + , objectsPerPage_( objectsPerPage ) + { +// printf( "Size: %d => %s\n", sizeof(AllocatedType), typeid(AllocatedType).name() ); + assert( sizeof(AllocatedType) * objectPerAllocation >= sizeof(AllocatedType *) ); // We must be able to store a slist in the object free space. + assert( objectsPerPage >= 16 ); + batches_ = allocateBatch( 0 ); // allocated a dummy page + currentBatch_ = batches_; + } + + ~BatchAllocator() + { + for ( BatchInfo *batch = batches_; batch; ) + { + BatchInfo *nextBatch = batch->next_; + free( batch ); + batch = nextBatch; + } + } + + /// allocate space for an array of objectPerAllocation object. + /// @warning it is the responsability of the caller to call objects constructors. + AllocatedType *allocate() + { + if ( freeHead_ ) // returns node from free list. + { + AllocatedType *object = freeHead_; + freeHead_ = *(AllocatedType **)object; + return object; + } + if ( currentBatch_->used_ == currentBatch_->end_ ) + { + currentBatch_ = currentBatch_->next_; + while ( currentBatch_ && currentBatch_->used_ == currentBatch_->end_ ) + currentBatch_ = currentBatch_->next_; + + if ( !currentBatch_ ) // no free batch found, allocate a new one + { + currentBatch_ = allocateBatch( objectsPerPage_ ); + currentBatch_->next_ = batches_; // insert at the head of the list + batches_ = currentBatch_; + } + } + AllocatedType *allocated = currentBatch_->used_; + currentBatch_->used_ += objectPerAllocation; + return allocated; + } + + /// Release the object. + /// @warning it is the responsability of the caller to actually destruct the object. + void release( AllocatedType *object ) + { + assert( object != 0 ); + *(AllocatedType **)object = freeHead_; + freeHead_ = object; + } + +private: + struct BatchInfo + { + BatchInfo *next_; + AllocatedType *used_; + AllocatedType *end_; + AllocatedType buffer_[objectPerAllocation]; + }; + + // disabled copy constructor and assignement operator. + BatchAllocator( const BatchAllocator & ); + void operator =( const BatchAllocator &); + + static BatchInfo *allocateBatch( unsigned int objectsPerPage ) + { + const unsigned int mallocSize = sizeof(BatchInfo) - sizeof(AllocatedType)* objectPerAllocation + + sizeof(AllocatedType) * objectPerAllocation * objectsPerPage; + BatchInfo *batch = static_cast( malloc( mallocSize ) ); + batch->next_ = 0; + batch->used_ = batch->buffer_; + batch->end_ = batch->buffer_ + objectsPerPage; + return batch; + } + + BatchInfo *batches_; + BatchInfo *currentBatch_; + /// Head of a single linked list within the allocated space of freeed object + AllocatedType *freeHead_; + unsigned int objectsPerPage_; +}; + + +} // namespace Json + +# endif // ifndef JSONCPP_DOC_INCLUDE_IMPLEMENTATION + +#endif // JSONCPP_BATCHALLOCATOR_H_INCLUDED + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: src/lib_json/json_batchallocator.h +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: src/lib_json/json_valueiterator.inl +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2007-2010 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +// included by json_value.cpp + +namespace Json { + +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class ValueIteratorBase +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// + +ValueIteratorBase::ValueIteratorBase() +#ifndef JSON_VALUE_USE_INTERNAL_MAP + : current_() + , isNull_( true ) +{ +} +#else + : isArray_( true ) + , isNull_( true ) +{ + iterator_.array_ = ValueInternalArray::IteratorState(); +} +#endif + + +#ifndef JSON_VALUE_USE_INTERNAL_MAP +ValueIteratorBase::ValueIteratorBase( const Value::ObjectValues::iterator ¤t ) + : current_( current ) + , isNull_( false ) +{ +} +#else +ValueIteratorBase::ValueIteratorBase( const ValueInternalArray::IteratorState &state ) + : isArray_( true ) +{ + iterator_.array_ = state; +} + + +ValueIteratorBase::ValueIteratorBase( const ValueInternalMap::IteratorState &state ) + : isArray_( false ) +{ + iterator_.map_ = state; +} +#endif + +Value & +ValueIteratorBase::deref() const +{ +#ifndef JSON_VALUE_USE_INTERNAL_MAP + return current_->second; +#else + if ( isArray_ ) + return ValueInternalArray::dereference( iterator_.array_ ); + return ValueInternalMap::value( iterator_.map_ ); +#endif +} + + +void +ValueIteratorBase::increment() +{ +#ifndef JSON_VALUE_USE_INTERNAL_MAP + ++current_; +#else + if ( isArray_ ) + ValueInternalArray::increment( iterator_.array_ ); + ValueInternalMap::increment( iterator_.map_ ); +#endif +} + + +void +ValueIteratorBase::decrement() +{ +#ifndef JSON_VALUE_USE_INTERNAL_MAP + --current_; +#else + if ( isArray_ ) + ValueInternalArray::decrement( iterator_.array_ ); + ValueInternalMap::decrement( iterator_.map_ ); +#endif +} + + +ValueIteratorBase::difference_type +ValueIteratorBase::computeDistance( const SelfType &other ) const +{ +#ifndef JSON_VALUE_USE_INTERNAL_MAP +# ifdef JSON_USE_CPPTL_SMALLMAP + return current_ - other.current_; +# else + // Iterator for null value are initialized using the default + // constructor, which initialize current_ to the default + // std::map::iterator. As begin() and end() are two instance + // of the default std::map::iterator, they can not be compared. + // To allow this, we handle this comparison specifically. + if ( isNull_ && other.isNull_ ) + { + return 0; + } + + + // Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL, + // which is the one used by default). + // Using a portable hand-made version for non random iterator instead: + // return difference_type( std::distance( current_, other.current_ ) ); + difference_type myDistance = 0; + for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it ) + { + ++myDistance; + } + return myDistance; +# endif +#else + if ( isArray_ ) + return ValueInternalArray::distance( iterator_.array_, other.iterator_.array_ ); + return ValueInternalMap::distance( iterator_.map_, other.iterator_.map_ ); +#endif +} + + +bool +ValueIteratorBase::isEqual( const SelfType &other ) const +{ +#ifndef JSON_VALUE_USE_INTERNAL_MAP + if ( isNull_ ) + { + return other.isNull_; + } + return current_ == other.current_; +#else + if ( isArray_ ) + return ValueInternalArray::equals( iterator_.array_, other.iterator_.array_ ); + return ValueInternalMap::equals( iterator_.map_, other.iterator_.map_ ); +#endif +} + + +void +ValueIteratorBase::copy( const SelfType &other ) +{ +#ifndef JSON_VALUE_USE_INTERNAL_MAP + current_ = other.current_; +#else + if ( isArray_ ) + iterator_.array_ = other.iterator_.array_; + iterator_.map_ = other.iterator_.map_; +#endif +} + + +Value +ValueIteratorBase::key() const +{ +#ifndef JSON_VALUE_USE_INTERNAL_MAP + const Value::CZString czstring = (*current_).first; + if ( czstring.c_str() ) + { + if ( czstring.isStaticString() ) + return Value( StaticString( czstring.c_str() ) ); + return Value( czstring.c_str() ); + } + return Value( czstring.index() ); +#else + if ( isArray_ ) + return Value( ValueInternalArray::indexOf( iterator_.array_ ) ); + bool isStatic; + const char *memberName = ValueInternalMap::key( iterator_.map_, isStatic ); + if ( isStatic ) + return Value( StaticString( memberName ) ); + return Value( memberName ); +#endif +} + + +UInt +ValueIteratorBase::index() const +{ +#ifndef JSON_VALUE_USE_INTERNAL_MAP + const Value::CZString czstring = (*current_).first; + if ( !czstring.c_str() ) + return czstring.index(); + return Value::UInt( -1 ); +#else + if ( isArray_ ) + return Value::UInt( ValueInternalArray::indexOf( iterator_.array_ ) ); + return Value::UInt( -1 ); +#endif +} + + +const char * +ValueIteratorBase::memberName() const +{ +#ifndef JSON_VALUE_USE_INTERNAL_MAP + const char *name = (*current_).first.c_str(); + return name ? name : ""; +#else + if ( !isArray_ ) + return ValueInternalMap::key( iterator_.map_ ); + return ""; +#endif +} + + +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class ValueConstIterator +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// + +ValueConstIterator::ValueConstIterator() +{ +} + + +#ifndef JSON_VALUE_USE_INTERNAL_MAP +ValueConstIterator::ValueConstIterator( const Value::ObjectValues::iterator ¤t ) + : ValueIteratorBase( current ) +{ +} +#else +ValueConstIterator::ValueConstIterator( const ValueInternalArray::IteratorState &state ) + : ValueIteratorBase( state ) +{ +} + +ValueConstIterator::ValueConstIterator( const ValueInternalMap::IteratorState &state ) + : ValueIteratorBase( state ) +{ +} +#endif + +ValueConstIterator & +ValueConstIterator::operator =( const ValueIteratorBase &other ) +{ + copy( other ); + return *this; +} + + +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class ValueIterator +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// + +ValueIterator::ValueIterator() +{ +} + + +#ifndef JSON_VALUE_USE_INTERNAL_MAP +ValueIterator::ValueIterator( const Value::ObjectValues::iterator ¤t ) + : ValueIteratorBase( current ) +{ +} +#else +ValueIterator::ValueIterator( const ValueInternalArray::IteratorState &state ) + : ValueIteratorBase( state ) +{ +} + +ValueIterator::ValueIterator( const ValueInternalMap::IteratorState &state ) + : ValueIteratorBase( state ) +{ +} +#endif + +ValueIterator::ValueIterator( const ValueConstIterator &other ) + : ValueIteratorBase( other ) +{ +} + +ValueIterator::ValueIterator( const ValueIterator &other ) + : ValueIteratorBase( other ) +{ +} + +ValueIterator & +ValueIterator::operator =( const SelfType &other ) +{ + copy( other ); + return *this; +} + +} // namespace Json + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: src/lib_json/json_valueiterator.inl +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: src/lib_json/json_value.cpp +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2011 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#if !defined(JSON_IS_AMALGAMATION) +# include +# include +# include +# ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR +# include "json_batchallocator.h" +# endif // #ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR +#endif // if !defined(JSON_IS_AMALGAMATION) +#include +#include +#include +#include +#include +#include +#ifdef JSON_USE_CPPTL +# include +#endif +#include // size_t + +#define JSON_ASSERT_UNREACHABLE assert( false ) + +namespace Json { + +const Value Value::null; +const Int Value::minInt = Int( ~(UInt(-1)/2) ); +const Int Value::maxInt = Int( UInt(-1)/2 ); +const UInt Value::maxUInt = UInt(-1); +# if defined(JSON_HAS_INT64) +const Int64 Value::minInt64 = Int64( ~(UInt64(-1)/2) ); +const Int64 Value::maxInt64 = Int64( UInt64(-1)/2 ); +const UInt64 Value::maxUInt64 = UInt64(-1); +// The constant is hard-coded because some compiler have trouble +// converting Value::maxUInt64 to a double correctly (AIX/xlC). +// Assumes that UInt64 is a 64 bits integer. +static const double maxUInt64AsDouble = 18446744073709551615.0; +#endif // defined(JSON_HAS_INT64) +const LargestInt Value::minLargestInt = LargestInt( ~(LargestUInt(-1)/2) ); +const LargestInt Value::maxLargestInt = LargestInt( LargestUInt(-1)/2 ); +const LargestUInt Value::maxLargestUInt = LargestUInt(-1); + + +/// Unknown size marker +static const unsigned int unknown = (unsigned)-1; + +#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) +template +static inline bool InRange(double d, T min, U max) { + return d >= min && d <= max; +} +#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) +static inline double integerToDouble( Json::UInt64 value ) +{ + return static_cast( Int64(value/2) ) * 2.0 + Int64(value & 1); +} + +template +static inline double integerToDouble( T value ) +{ + return static_cast( value ); +} + +template +static inline bool InRange(double d, T min, U max) { + return d >= integerToDouble(min) && d <= integerToDouble(max); +} +#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + + +/** Duplicates the specified string value. + * @param value Pointer to the string to duplicate. Must be zero-terminated if + * length is "unknown". + * @param length Length of the value. if equals to unknown, then it will be + * computed using strlen(value). + * @return Pointer on the duplicate instance of string. + */ +static inline char * +duplicateStringValue( const char *value, + unsigned int length = unknown ) +{ + if ( length == unknown ) + length = (unsigned int)strlen(value); + + // Avoid an integer overflow in the call to malloc below by limiting length + // to a sane value. + if (length >= (unsigned)Value::maxInt) + length = Value::maxInt - 1; + + char *newString = static_cast( malloc( length + 1 ) ); + JSON_ASSERT_MESSAGE( newString != 0, "Failed to allocate string value buffer" ); + memcpy( newString, value, length ); + newString[length] = 0; + return newString; +} + + +/** Free the string duplicated by duplicateStringValue(). + */ +static inline void +releaseStringValue( char *value ) +{ + if ( value ) + free( value ); +} + +} // namespace Json + + +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ValueInternals... +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +#if !defined(JSON_IS_AMALGAMATION) +# ifdef JSON_VALUE_USE_INTERNAL_MAP +# include "json_internalarray.inl" +# include "json_internalmap.inl" +# endif // JSON_VALUE_USE_INTERNAL_MAP + +# include "json_valueiterator.inl" +#endif // if !defined(JSON_IS_AMALGAMATION) + +namespace Json { + +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class Value::CommentInfo +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// + + +Value::CommentInfo::CommentInfo() + : comment_( 0 ) +{ +} + +Value::CommentInfo::~CommentInfo() +{ + if ( comment_ ) + releaseStringValue( comment_ ); +} + + +void +Value::CommentInfo::setComment( const char *text ) +{ + if ( comment_ ) + releaseStringValue( comment_ ); + JSON_ASSERT( text != 0 ); + JSON_ASSERT_MESSAGE( text[0]=='\0' || text[0]=='/', "Comments must start with /"); + // It seems that /**/ style comments are acceptable as well. + comment_ = duplicateStringValue( text ); +} + + +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class Value::CZString +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +# ifndef JSON_VALUE_USE_INTERNAL_MAP + +// Notes: index_ indicates if the string was allocated when +// a string is stored. + +Value::CZString::CZString( ArrayIndex index ) + : cstr_( 0 ) + , index_( index ) +{ +} + +Value::CZString::CZString( const char *cstr, DuplicationPolicy allocate ) + : cstr_( allocate == duplicate ? duplicateStringValue(cstr) + : cstr ) + , index_( allocate ) +{ +} + +Value::CZString::CZString( const CZString &other ) +: cstr_( other.index_ != noDuplication && other.cstr_ != 0 + ? duplicateStringValue( other.cstr_ ) + : other.cstr_ ) + , index_( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate) + : other.index_ ) +{ +} + +Value::CZString::~CZString() +{ + if ( cstr_ && index_ == duplicate ) + releaseStringValue( const_cast( cstr_ ) ); +} + +void +Value::CZString::swap( CZString &other ) +{ + std::swap( cstr_, other.cstr_ ); + std::swap( index_, other.index_ ); +} + +Value::CZString & +Value::CZString::operator =( const CZString &other ) +{ + CZString temp( other ); + swap( temp ); + return *this; +} + +bool +Value::CZString::operator<( const CZString &other ) const +{ + if ( cstr_ ) + return strcmp( cstr_, other.cstr_ ) < 0; + return index_ < other.index_; +} + +bool +Value::CZString::operator==( const CZString &other ) const +{ + if ( cstr_ ) + return strcmp( cstr_, other.cstr_ ) == 0; + return index_ == other.index_; +} + + +ArrayIndex +Value::CZString::index() const +{ + return index_; +} + + +const char * +Value::CZString::c_str() const +{ + return cstr_; +} + +bool +Value::CZString::isStaticString() const +{ + return index_ == noDuplication; +} + +#endif // ifndef JSON_VALUE_USE_INTERNAL_MAP + + +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// class Value::Value +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// +// ////////////////////////////////////////////////////////////////// + +/*! \internal Default constructor initialization must be equivalent to: + * memset( this, 0, sizeof(Value) ) + * This optimization is used in ValueInternalMap fast allocator. + */ +Value::Value( ValueType type ) + : type_( type ) + , allocated_( false ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + switch ( type ) + { + case nullValue: + break; + case intValue: + case uintValue: + value_.int_ = 0; + break; + case realValue: + value_.real_ = 0.0; + break; + case stringValue: + value_.string_ = 0; + break; +#ifndef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: + case objectValue: + value_.map_ = new ObjectValues(); + break; +#else + case arrayValue: + value_.array_ = arrayAllocator()->newArray(); + break; + case objectValue: + value_.map_ = mapAllocator()->newMap(); + break; +#endif + case booleanValue: + value_.bool_ = false; + break; + default: + JSON_ASSERT_UNREACHABLE; + } +} + + +Value::Value( UInt value ) + : type_( uintValue ) + , allocated_( false ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.uint_ = value; +} + +Value::Value( Int value ) + : type_( intValue ) + , allocated_( false ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.int_ = value; +} + + +# if defined(JSON_HAS_INT64) +Value::Value( Int64 value ) + : type_( intValue ) + , allocated_( false ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.int_ = value; +} + + +Value::Value( UInt64 value ) + : type_( uintValue ) + , allocated_( false ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.uint_ = value; +} +#endif // defined(JSON_HAS_INT64) + +Value::Value( double value ) + : type_( realValue ) + , allocated_( false ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.real_ = value; +} + +Value::Value( const char *value ) + : type_( stringValue ) + , allocated_( true ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.string_ = duplicateStringValue( value ); +} + + +Value::Value( const char *beginValue, + const char *endValue ) + : type_( stringValue ) + , allocated_( true ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.string_ = duplicateStringValue( beginValue, + (unsigned int)(endValue - beginValue) ); +} + + +Value::Value( const std::string &value ) + : type_( stringValue ) + , allocated_( true ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.string_ = duplicateStringValue( value.c_str(), + (unsigned int)value.length() ); + +} + +Value::Value( const StaticString &value ) + : type_( stringValue ) + , allocated_( false ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.string_ = const_cast( value.c_str() ); +} + + +# ifdef JSON_USE_CPPTL +Value::Value( const CppTL::ConstString &value ) + : type_( stringValue ) + , allocated_( true ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.string_ = duplicateStringValue( value, value.length() ); +} +# endif + +Value::Value( bool value ) + : type_( booleanValue ) + , allocated_( false ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + value_.bool_ = value; +} + + +Value::Value( const Value &other ) + : type_( other.type_ ) + , allocated_( false ) +# ifdef JSON_VALUE_USE_INTERNAL_MAP + , itemIsUsed_( 0 ) +#endif + , comments_( 0 ) +{ + switch ( type_ ) + { + case nullValue: + case intValue: + case uintValue: + case realValue: + case booleanValue: + value_ = other.value_; + break; + case stringValue: + if ( other.value_.string_ ) + { + value_.string_ = duplicateStringValue( other.value_.string_ ); + allocated_ = true; + } + else + value_.string_ = 0; + break; +#ifndef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: + case objectValue: + value_.map_ = new ObjectValues( *other.value_.map_ ); + break; +#else + case arrayValue: + value_.array_ = arrayAllocator()->newArrayCopy( *other.value_.array_ ); + break; + case objectValue: + value_.map_ = mapAllocator()->newMapCopy( *other.value_.map_ ); + break; +#endif + default: + JSON_ASSERT_UNREACHABLE; + } + if ( other.comments_ ) + { + comments_ = new CommentInfo[numberOfCommentPlacement]; + for ( int comment =0; comment < numberOfCommentPlacement; ++comment ) + { + const CommentInfo &otherComment = other.comments_[comment]; + if ( otherComment.comment_ ) + comments_[comment].setComment( otherComment.comment_ ); + } + } +} + + +Value::~Value() +{ + switch ( type_ ) + { + case nullValue: + case intValue: + case uintValue: + case realValue: + case booleanValue: + break; + case stringValue: + if ( allocated_ ) + releaseStringValue( value_.string_ ); + break; +#ifndef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: + case objectValue: + delete value_.map_; + break; +#else + case arrayValue: + arrayAllocator()->destructArray( value_.array_ ); + break; + case objectValue: + mapAllocator()->destructMap( value_.map_ ); + break; +#endif + default: + JSON_ASSERT_UNREACHABLE; + } + + if ( comments_ ) + delete[] comments_; +} + +Value & +Value::operator=( const Value &other ) +{ + Value temp( other ); + swap( temp ); + return *this; +} + +void +Value::swap( Value &other ) +{ + ValueType temp = type_; + type_ = other.type_; + other.type_ = temp; + std::swap( value_, other.value_ ); + int temp2 = allocated_; + allocated_ = other.allocated_; + other.allocated_ = temp2; +} + +ValueType +Value::type() const +{ + return type_; +} + + +int +Value::compare( const Value &other ) const +{ + if ( *this < other ) + return -1; + if ( *this > other ) + return 1; + return 0; +} + + +bool +Value::operator <( const Value &other ) const +{ + int typeDelta = type_ - other.type_; + if ( typeDelta ) + return typeDelta < 0 ? true : false; + switch ( type_ ) + { + case nullValue: + return false; + case intValue: + return value_.int_ < other.value_.int_; + case uintValue: + return value_.uint_ < other.value_.uint_; + case realValue: + return value_.real_ < other.value_.real_; + case booleanValue: + return value_.bool_ < other.value_.bool_; + case stringValue: + return ( value_.string_ == 0 && other.value_.string_ ) + || ( other.value_.string_ + && value_.string_ + && strcmp( value_.string_, other.value_.string_ ) < 0 ); +#ifndef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: + case objectValue: + { + int delta = int( value_.map_->size() - other.value_.map_->size() ); + if ( delta ) + return delta < 0; + return (*value_.map_) < (*other.value_.map_); + } +#else + case arrayValue: + return value_.array_->compare( *(other.value_.array_) ) < 0; + case objectValue: + return value_.map_->compare( *(other.value_.map_) ) < 0; +#endif + default: + JSON_ASSERT_UNREACHABLE; + } + return false; // unreachable +} + +bool +Value::operator <=( const Value &other ) const +{ + return !(other < *this); +} + +bool +Value::operator >=( const Value &other ) const +{ + return !(*this < other); +} + +bool +Value::operator >( const Value &other ) const +{ + return other < *this; +} + +bool +Value::operator ==( const Value &other ) const +{ + //if ( type_ != other.type_ ) + // GCC 2.95.3 says: + // attempt to take address of bit-field structure member `Json::Value::type_' + // Beats me, but a temp solves the problem. + int temp = other.type_; + if ( type_ != temp ) + return false; + switch ( type_ ) + { + case nullValue: + return true; + case intValue: + return value_.int_ == other.value_.int_; + case uintValue: + return value_.uint_ == other.value_.uint_; + case realValue: + return value_.real_ == other.value_.real_; + case booleanValue: + return value_.bool_ == other.value_.bool_; + case stringValue: + return ( value_.string_ == other.value_.string_ ) + || ( other.value_.string_ + && value_.string_ + && strcmp( value_.string_, other.value_.string_ ) == 0 ); +#ifndef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: + case objectValue: + return value_.map_->size() == other.value_.map_->size() + && (*value_.map_) == (*other.value_.map_); +#else + case arrayValue: + return value_.array_->compare( *(other.value_.array_) ) == 0; + case objectValue: + return value_.map_->compare( *(other.value_.map_) ) == 0; +#endif + default: + JSON_ASSERT_UNREACHABLE; + } + return false; // unreachable +} + +bool +Value::operator !=( const Value &other ) const +{ + return !( *this == other ); +} + +const char * +Value::asCString() const +{ + JSON_ASSERT( type_ == stringValue ); + return value_.string_; +} + + +std::string +Value::asString() const +{ + switch ( type_ ) + { + case nullValue: + return ""; + case stringValue: + return value_.string_ ? value_.string_ : ""; + case booleanValue: + return value_.bool_ ? "true" : "false"; + case intValue: + return valueToString( value_.int_ ); + case uintValue: + return valueToString( value_.uint_ ); + case realValue: + return valueToString( value_.real_ ); + default: + JSON_FAIL_MESSAGE( "Type is not convertible to string" ); + } +} + +# ifdef JSON_USE_CPPTL +CppTL::ConstString +Value::asConstString() const +{ + return CppTL::ConstString( asString().c_str() ); +} +# endif + + +Value::Int +Value::asInt() const +{ + switch ( type_ ) + { + case intValue: + JSON_ASSERT_MESSAGE(isInt(), "LargestInt out of Int range"); + return Int(value_.int_); + case uintValue: + JSON_ASSERT_MESSAGE(isInt(), "LargestUInt out of Int range"); + return Int(value_.uint_); + case realValue: + JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt, maxInt), "double out of Int range"); + return Int(value_.real_); + case nullValue: + return 0; + case booleanValue: + return value_.bool_ ? 1 : 0; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to Int."); +} + + +Value::UInt +Value::asUInt() const +{ + switch ( type_ ) + { + case intValue: + JSON_ASSERT_MESSAGE(isUInt(), "LargestInt out of UInt range"); + return UInt(value_.int_); + case uintValue: + JSON_ASSERT_MESSAGE(isUInt(), "LargestUInt out of UInt range"); + return UInt(value_.uint_); + case realValue: + JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt), "double out of UInt range"); + return UInt( value_.real_ ); + case nullValue: + return 0; + case booleanValue: + return value_.bool_ ? 1 : 0; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to UInt."); +} + + +# if defined(JSON_HAS_INT64) + +Value::Int64 +Value::asInt64() const +{ + switch ( type_ ) + { + case intValue: + return Int64(value_.int_); + case uintValue: + JSON_ASSERT_MESSAGE(isInt64(), "LargestUInt out of Int64 range"); + return Int64(value_.uint_); + case realValue: + JSON_ASSERT_MESSAGE(InRange(value_.real_, minInt64, maxInt64), "double out of Int64 range"); + return Int64(value_.real_); + case nullValue: + return 0; + case booleanValue: + return value_.bool_ ? 1 : 0; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to Int64."); +} + + +Value::UInt64 +Value::asUInt64() const +{ + switch ( type_ ) + { + case intValue: + JSON_ASSERT_MESSAGE(isUInt64(), "LargestInt out of UInt64 range"); + return UInt64(value_.int_); + case uintValue: + return UInt64(value_.uint_); + case realValue: + JSON_ASSERT_MESSAGE(InRange(value_.real_, 0, maxUInt64), "double out of UInt64 range"); + return UInt64( value_.real_ ); + case nullValue: + return 0; + case booleanValue: + return value_.bool_ ? 1 : 0; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to UInt64."); +} +# endif // if defined(JSON_HAS_INT64) + + +LargestInt +Value::asLargestInt() const +{ +#if defined(JSON_NO_INT64) + return asInt(); +#else + return asInt64(); +#endif +} + + +LargestUInt +Value::asLargestUInt() const +{ +#if defined(JSON_NO_INT64) + return asUInt(); +#else + return asUInt64(); +#endif +} + + +double +Value::asDouble() const +{ + switch ( type_ ) + { + case intValue: + return static_cast( value_.int_ ); + case uintValue: +#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + return static_cast( value_.uint_ ); +#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + return integerToDouble( value_.uint_ ); +#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + case realValue: + return value_.real_; + case nullValue: + return 0.0; + case booleanValue: + return value_.bool_ ? 1.0 : 0.0; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to double."); +} + +float +Value::asFloat() const +{ + switch ( type_ ) + { + case intValue: + return static_cast( value_.int_ ); + case uintValue: +#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + return static_cast( value_.uint_ ); +#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + return integerToDouble( value_.uint_ ); +#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION) + case realValue: + return static_cast( value_.real_ ); + case nullValue: + return 0.0; + case booleanValue: + return value_.bool_ ? 1.0f : 0.0f; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to float."); +} + +bool +Value::asBool() const +{ + switch ( type_ ) + { + case booleanValue: + return value_.bool_; + case nullValue: + return false; + case intValue: + return value_.int_ ? true : false; + case uintValue: + return value_.uint_ ? true : false; + case realValue: + return value_.real_ ? true : false; + default: + break; + } + JSON_FAIL_MESSAGE("Value is not convertible to bool."); +} + + +bool +Value::isConvertibleTo( ValueType other ) const +{ + switch ( other ) + { + case nullValue: + return ( isNumeric() && asDouble() == 0.0 ) + || ( type_ == booleanValue && value_.bool_ == false ) + || ( type_ == stringValue && asString() == "" ) + || ( type_ == arrayValue && value_.map_->size() == 0 ) + || ( type_ == objectValue && value_.map_->size() == 0 ) + || type_ == nullValue; + case intValue: + return isInt() + || (type_ == realValue && InRange(value_.real_, minInt, maxInt)) + || type_ == booleanValue + || type_ == nullValue; + case uintValue: + return isUInt() + || (type_ == realValue && InRange(value_.real_, 0, maxUInt)) + || type_ == booleanValue + || type_ == nullValue; + case realValue: + return isNumeric() + || type_ == booleanValue + || type_ == nullValue; + case booleanValue: + return isNumeric() + || type_ == booleanValue + || type_ == nullValue; + case stringValue: + return isNumeric() + || type_ == booleanValue + || type_ == stringValue + || type_ == nullValue; + case arrayValue: + return type_ == arrayValue + || type_ == nullValue; + case objectValue: + return type_ == objectValue + || type_ == nullValue; + } + JSON_ASSERT_UNREACHABLE; + return false; +} + + +/// Number of values in array or object +ArrayIndex +Value::size() const +{ + switch ( type_ ) + { + case nullValue: + case intValue: + case uintValue: + case realValue: + case booleanValue: + case stringValue: + return 0; +#ifndef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: // size of the array is highest index + 1 + if ( !value_.map_->empty() ) + { + ObjectValues::const_iterator itLast = value_.map_->end(); + --itLast; + return (*itLast).first.index()+1; + } + return 0; + case objectValue: + return ArrayIndex( value_.map_->size() ); +#else + case arrayValue: + return Int( value_.array_->size() ); + case objectValue: + return Int( value_.map_->size() ); +#endif + } + JSON_ASSERT_UNREACHABLE; + return 0; // unreachable; +} + + +bool +Value::empty() const +{ + if ( isNull() || isArray() || isObject() ) + return size() == 0u; + else + return false; +} + + +bool +Value::operator!() const +{ + return isNull(); +} + + +void +Value::clear() +{ + JSON_ASSERT( type_ == nullValue || type_ == arrayValue || type_ == objectValue ); + + switch ( type_ ) + { +#ifndef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: + case objectValue: + value_.map_->clear(); + break; +#else + case arrayValue: + value_.array_->clear(); + break; + case objectValue: + value_.map_->clear(); + break; +#endif + default: + break; + } +} + +void +Value::resize( ArrayIndex newSize ) +{ + JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); + if ( type_ == nullValue ) + *this = Value( arrayValue ); +#ifndef JSON_VALUE_USE_INTERNAL_MAP + ArrayIndex oldSize = size(); + if ( newSize == 0 ) + clear(); + else if ( newSize > oldSize ) + (*this)[ newSize - 1 ]; + else + { + for ( ArrayIndex index = newSize; index < oldSize; ++index ) + { + value_.map_->erase( index ); + } + assert( size() == newSize ); + } +#else + value_.array_->resize( newSize ); +#endif +} + + +Value & +Value::operator[]( ArrayIndex index ) +{ + JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); + if ( type_ == nullValue ) + *this = Value( arrayValue ); +#ifndef JSON_VALUE_USE_INTERNAL_MAP + CZString key( index ); + ObjectValues::iterator it = value_.map_->lower_bound( key ); + if ( it != value_.map_->end() && (*it).first == key ) + return (*it).second; + + ObjectValues::value_type defaultValue( key, null ); + it = value_.map_->insert( it, defaultValue ); + return (*it).second; +#else + return value_.array_->resolveReference( index ); +#endif +} + + +Value & +Value::operator[]( int index ) +{ + JSON_ASSERT( index >= 0 ); + return (*this)[ ArrayIndex(index) ]; +} + + +const Value & +Value::operator[]( ArrayIndex index ) const +{ + JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); + if ( type_ == nullValue ) + return null; +#ifndef JSON_VALUE_USE_INTERNAL_MAP + CZString key( index ); + ObjectValues::const_iterator it = value_.map_->find( key ); + if ( it == value_.map_->end() ) + return null; + return (*it).second; +#else + Value *value = value_.array_->find( index ); + return value ? *value : null; +#endif +} + + +const Value & +Value::operator[]( int index ) const +{ + JSON_ASSERT( index >= 0 ); + return (*this)[ ArrayIndex(index) ]; +} + + +Value & +Value::operator[]( const char *key ) +{ + return resolveReference( key, false ); +} + + +Value & +Value::resolveReference( const char *key, + bool isStatic ) +{ + JSON_ASSERT( type_ == nullValue || type_ == objectValue ); + if ( type_ == nullValue ) + *this = Value( objectValue ); +#ifndef JSON_VALUE_USE_INTERNAL_MAP + CZString actualKey( key, isStatic ? CZString::noDuplication + : CZString::duplicateOnCopy ); + ObjectValues::iterator it = value_.map_->lower_bound( actualKey ); + if ( it != value_.map_->end() && (*it).first == actualKey ) + return (*it).second; + + ObjectValues::value_type defaultValue( actualKey, null ); + it = value_.map_->insert( it, defaultValue ); + Value &value = (*it).second; + return value; +#else + return value_.map_->resolveReference( key, isStatic ); +#endif +} + + +Value +Value::get( ArrayIndex index, + const Value &defaultValue ) const +{ + const Value *value = &((*this)[index]); + return value == &null ? defaultValue : *value; +} + + +bool +Value::isValidIndex( ArrayIndex index ) const +{ + return index < size(); +} + + + +const Value & +Value::operator[]( const char *key ) const +{ + JSON_ASSERT( type_ == nullValue || type_ == objectValue ); + if ( type_ == nullValue ) + return null; +#ifndef JSON_VALUE_USE_INTERNAL_MAP + CZString actualKey( key, CZString::noDuplication ); + ObjectValues::const_iterator it = value_.map_->find( actualKey ); + if ( it == value_.map_->end() ) + return null; + return (*it).second; +#else + const Value *value = value_.map_->find( key ); + return value ? *value : null; +#endif +} + + +Value & +Value::operator[]( const std::string &key ) +{ + return (*this)[ key.c_str() ]; +} + + +const Value & +Value::operator[]( const std::string &key ) const +{ + return (*this)[ key.c_str() ]; +} + +Value & +Value::operator[]( const StaticString &key ) +{ + return resolveReference( key, true ); +} + + +# ifdef JSON_USE_CPPTL +Value & +Value::operator[]( const CppTL::ConstString &key ) +{ + return (*this)[ key.c_str() ]; +} + + +const Value & +Value::operator[]( const CppTL::ConstString &key ) const +{ + return (*this)[ key.c_str() ]; +} +# endif + + +Value & +Value::append( const Value &value ) +{ + return (*this)[size()] = value; +} + + +Value +Value::get( const char *key, + const Value &defaultValue ) const +{ + const Value *value = &((*this)[key]); + return value == &null ? defaultValue : *value; +} + + +Value +Value::get( const std::string &key, + const Value &defaultValue ) const +{ + return get( key.c_str(), defaultValue ); +} + +Value +Value::removeMember( const char* key ) +{ + JSON_ASSERT( type_ == nullValue || type_ == objectValue ); + if ( type_ == nullValue ) + return null; +#ifndef JSON_VALUE_USE_INTERNAL_MAP + CZString actualKey( key, CZString::noDuplication ); + ObjectValues::iterator it = value_.map_->find( actualKey ); + if ( it == value_.map_->end() ) + return null; + Value old(it->second); + value_.map_->erase(it); + return old; +#else + Value *value = value_.map_->find( key ); + if (value){ + Value old(*value); + value_.map_.remove( key ); + return old; + } else { + return null; + } +#endif +} + +Value +Value::removeMember( const std::string &key ) +{ + return removeMember( key.c_str() ); +} + +# ifdef JSON_USE_CPPTL +Value +Value::get( const CppTL::ConstString &key, + const Value &defaultValue ) const +{ + return get( key.c_str(), defaultValue ); +} +# endif + +bool +Value::isMember( const char *key ) const +{ + const Value *value = &((*this)[key]); + return value != &null; +} + + +bool +Value::isMember( const std::string &key ) const +{ + return isMember( key.c_str() ); +} + + +# ifdef JSON_USE_CPPTL +bool +Value::isMember( const CppTL::ConstString &key ) const +{ + return isMember( key.c_str() ); +} +#endif + +Value::Members +Value::getMemberNames() const +{ + JSON_ASSERT( type_ == nullValue || type_ == objectValue ); + if ( type_ == nullValue ) + return Value::Members(); + Members members; + members.reserve( value_.map_->size() ); +#ifndef JSON_VALUE_USE_INTERNAL_MAP + ObjectValues::const_iterator it = value_.map_->begin(); + ObjectValues::const_iterator itEnd = value_.map_->end(); + for ( ; it != itEnd; ++it ) + members.push_back( std::string( (*it).first.c_str() ) ); +#else + ValueInternalMap::IteratorState it; + ValueInternalMap::IteratorState itEnd; + value_.map_->makeBeginIterator( it ); + value_.map_->makeEndIterator( itEnd ); + for ( ; !ValueInternalMap::equals( it, itEnd ); ValueInternalMap::increment(it) ) + members.push_back( std::string( ValueInternalMap::key( it ) ) ); +#endif + return members; +} +// +//# ifdef JSON_USE_CPPTL +//EnumMemberNames +//Value::enumMemberNames() const +//{ +// if ( type_ == objectValue ) +// { +// return CppTL::Enum::any( CppTL::Enum::transform( +// CppTL::Enum::keys( *(value_.map_), CppTL::Type() ), +// MemberNamesTransform() ) ); +// } +// return EnumMemberNames(); +//} +// +// +//EnumValues +//Value::enumValues() const +//{ +// if ( type_ == objectValue || type_ == arrayValue ) +// return CppTL::Enum::anyValues( *(value_.map_), +// CppTL::Type() ); +// return EnumValues(); +//} +// +//# endif + +static bool IsIntegral(double d) { + double integral_part; + return modf(d, &integral_part) == 0.0; +} + + +bool +Value::isNull() const +{ + return type_ == nullValue; +} + + +bool +Value::isBool() const +{ + return type_ == booleanValue; +} + + +bool +Value::isInt() const +{ + switch ( type_ ) + { + case intValue: + return value_.int_ >= minInt && value_.int_ <= maxInt; + case uintValue: + return value_.uint_ <= UInt(maxInt); + case realValue: + return value_.real_ >= minInt && + value_.real_ <= maxInt && + IsIntegral(value_.real_); + default: + break; + } + return false; +} + + +bool +Value::isUInt() const +{ + switch ( type_ ) + { + case intValue: + return value_.int_ >= 0 && LargestUInt(value_.int_) <= LargestUInt(maxUInt); + case uintValue: + return value_.uint_ <= maxUInt; + case realValue: + return value_.real_ >= 0 && + value_.real_ <= maxUInt && + IsIntegral(value_.real_); + default: + break; + } + return false; +} + +bool +Value::isInt64() const +{ +# if defined(JSON_HAS_INT64) + switch ( type_ ) + { + case intValue: + return true; + case uintValue: + return value_.uint_ <= UInt64(maxInt64); + case realValue: + // Note that maxInt64 (= 2^63 - 1) is not exactly representable as a + // double, so double(maxInt64) will be rounded up to 2^63. Therefore we + // require the value to be strictly less than the limit. + return value_.real_ >= double(minInt64) && + value_.real_ < double(maxInt64) && + IsIntegral(value_.real_); + default: + break; + } +# endif // JSON_HAS_INT64 + return false; +} + +bool +Value::isUInt64() const +{ +# if defined(JSON_HAS_INT64) + switch ( type_ ) + { + case intValue: + return value_.int_ >= 0; + case uintValue: + return true; + case realValue: + // Note that maxUInt64 (= 2^64 - 1) is not exactly representable as a + // double, so double(maxUInt64) will be rounded up to 2^64. Therefore we + // require the value to be strictly less than the limit. + return value_.real_ >= 0 && + value_.real_ < maxUInt64AsDouble && + IsIntegral(value_.real_); + default: + break; + } +# endif // JSON_HAS_INT64 + return false; +} + + +bool +Value::isIntegral() const +{ +#if defined(JSON_HAS_INT64) + return isInt64() || isUInt64(); +#else + return isInt() || isUInt(); +#endif +} + + +bool +Value::isDouble() const +{ + return type_ == realValue || isIntegral(); +} + + +bool +Value::isNumeric() const +{ + return isIntegral() || isDouble(); +} + + +bool +Value::isString() const +{ + return type_ == stringValue; +} + + +bool +Value::isArray() const +{ + return type_ == arrayValue; +} + + +bool +Value::isObject() const +{ + return type_ == objectValue; +} + + +void +Value::setComment( const char *comment, + CommentPlacement placement ) +{ + if ( !comments_ ) + comments_ = new CommentInfo[numberOfCommentPlacement]; + comments_[placement].setComment( comment ); +} + + +void +Value::setComment( const std::string &comment, + CommentPlacement placement ) +{ + setComment( comment.c_str(), placement ); +} + + +bool +Value::hasComment( CommentPlacement placement ) const +{ + return comments_ != 0 && comments_[placement].comment_ != 0; +} + +std::string +Value::getComment( CommentPlacement placement ) const +{ + if ( hasComment(placement) ) + return comments_[placement].comment_; + return ""; +} + + +std::string +Value::toStyledString() const +{ + StyledWriter writer; + return writer.write( *this ); +} + + +Value::const_iterator +Value::begin() const +{ + switch ( type_ ) + { +#ifdef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: + if ( value_.array_ ) + { + ValueInternalArray::IteratorState it; + value_.array_->makeBeginIterator( it ); + return const_iterator( it ); + } + break; + case objectValue: + if ( value_.map_ ) + { + ValueInternalMap::IteratorState it; + value_.map_->makeBeginIterator( it ); + return const_iterator( it ); + } + break; +#else + case arrayValue: + case objectValue: + if ( value_.map_ ) + return const_iterator( value_.map_->begin() ); + break; +#endif + default: + break; + } + return const_iterator(); +} + +Value::const_iterator +Value::end() const +{ + switch ( type_ ) + { +#ifdef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: + if ( value_.array_ ) + { + ValueInternalArray::IteratorState it; + value_.array_->makeEndIterator( it ); + return const_iterator( it ); + } + break; + case objectValue: + if ( value_.map_ ) + { + ValueInternalMap::IteratorState it; + value_.map_->makeEndIterator( it ); + return const_iterator( it ); + } + break; +#else + case arrayValue: + case objectValue: + if ( value_.map_ ) + return const_iterator( value_.map_->end() ); + break; +#endif + default: + break; + } + return const_iterator(); +} + + +Value::iterator +Value::begin() +{ + switch ( type_ ) + { +#ifdef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: + if ( value_.array_ ) + { + ValueInternalArray::IteratorState it; + value_.array_->makeBeginIterator( it ); + return iterator( it ); + } + break; + case objectValue: + if ( value_.map_ ) + { + ValueInternalMap::IteratorState it; + value_.map_->makeBeginIterator( it ); + return iterator( it ); + } + break; +#else + case arrayValue: + case objectValue: + if ( value_.map_ ) + return iterator( value_.map_->begin() ); + break; +#endif + default: + break; + } + return iterator(); +} + +Value::iterator +Value::end() +{ + switch ( type_ ) + { +#ifdef JSON_VALUE_USE_INTERNAL_MAP + case arrayValue: + if ( value_.array_ ) + { + ValueInternalArray::IteratorState it; + value_.array_->makeEndIterator( it ); + return iterator( it ); + } + break; + case objectValue: + if ( value_.map_ ) + { + ValueInternalMap::IteratorState it; + value_.map_->makeEndIterator( it ); + return iterator( it ); + } + break; +#else + case arrayValue: + case objectValue: + if ( value_.map_ ) + return iterator( value_.map_->end() ); + break; +#endif + default: + break; + } + return iterator(); +} + + +// class PathArgument +// ////////////////////////////////////////////////////////////////// + +PathArgument::PathArgument() + : key_() + , index_() + , kind_( kindNone ) +{ +} + + +PathArgument::PathArgument( ArrayIndex index ) + : key_() + , index_( index ) + , kind_( kindIndex ) +{ +} + + +PathArgument::PathArgument( const char *key ) + : key_( key ) + , index_() + , kind_( kindKey ) +{ +} + + +PathArgument::PathArgument( const std::string &key ) + : key_( key.c_str() ) + , index_() + , kind_( kindKey ) +{ +} + +// class Path +// ////////////////////////////////////////////////////////////////// + +Path::Path( const std::string &path, + const PathArgument &a1, + const PathArgument &a2, + const PathArgument &a3, + const PathArgument &a4, + const PathArgument &a5 ) +{ + InArgs in; + in.push_back( &a1 ); + in.push_back( &a2 ); + in.push_back( &a3 ); + in.push_back( &a4 ); + in.push_back( &a5 ); + makePath( path, in ); +} + + +void +Path::makePath( const std::string &path, + const InArgs &in ) +{ + const char *current = path.c_str(); + const char *end = current + path.length(); + InArgs::const_iterator itInArg = in.begin(); + while ( current != end ) + { + if ( *current == '[' ) + { + ++current; + if ( *current == '%' ) + addPathInArg( path, in, itInArg, PathArgument::kindIndex ); + else + { + ArrayIndex index = 0; + for ( ; current != end && *current >= '0' && *current <= '9'; ++current ) + index = index * 10 + ArrayIndex(*current - '0'); + args_.push_back( index ); + } + if ( current == end || *current++ != ']' ) + invalidPath( path, int(current - path.c_str()) ); + } + else if ( *current == '%' ) + { + addPathInArg( path, in, itInArg, PathArgument::kindKey ); + ++current; + } + else if ( *current == '.' ) + { + ++current; + } + else + { + const char *beginName = current; + while ( current != end && !strchr( "[.", *current ) ) + ++current; + args_.push_back( std::string( beginName, current ) ); + } + } +} + + +void +Path::addPathInArg( const std::string &path, + const InArgs &in, + InArgs::const_iterator &itInArg, + PathArgument::Kind kind ) +{ + if ( itInArg == in.end() ) + { + // Error: missing argument %d + } + else if ( (*itInArg)->kind_ != kind ) + { + // Error: bad argument type + } + else + { + args_.push_back( **itInArg ); + } +} + + +void +Path::invalidPath( const std::string &path, + int location ) +{ + // Error: invalid path. +} + + +const Value & +Path::resolve( const Value &root ) const +{ + const Value *node = &root; + for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it ) + { + const PathArgument &arg = *it; + if ( arg.kind_ == PathArgument::kindIndex ) + { + if ( !node->isArray() || !node->isValidIndex( arg.index_ ) ) + { + // Error: unable to resolve path (array value expected at position... + } + node = &((*node)[arg.index_]); + } + else if ( arg.kind_ == PathArgument::kindKey ) + { + if ( !node->isObject() ) + { + // Error: unable to resolve path (object value expected at position...) + } + node = &((*node)[arg.key_]); + if ( node == &Value::null ) + { + // Error: unable to resolve path (object has no member named '' at position...) + } + } + } + return *node; +} + + +Value +Path::resolve( const Value &root, + const Value &defaultValue ) const +{ + const Value *node = &root; + for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it ) + { + const PathArgument &arg = *it; + if ( arg.kind_ == PathArgument::kindIndex ) + { + if ( !node->isArray() || !node->isValidIndex( arg.index_ ) ) + return defaultValue; + node = &((*node)[arg.index_]); + } + else if ( arg.kind_ == PathArgument::kindKey ) + { + if ( !node->isObject() ) + return defaultValue; + node = &((*node)[arg.key_]); + if ( node == &Value::null ) + return defaultValue; + } + } + return *node; +} + + +Value & +Path::make( Value &root ) const +{ + Value *node = &root; + for ( Args::const_iterator it = args_.begin(); it != args_.end(); ++it ) + { + const PathArgument &arg = *it; + if ( arg.kind_ == PathArgument::kindIndex ) + { + if ( !node->isArray() ) + { + // Error: node is not an array at position ... + } + node = &((*node)[arg.index_]); + } + else if ( arg.kind_ == PathArgument::kindKey ) + { + if ( !node->isObject() ) + { + // Error: node is not an object at position... + } + node = &((*node)[arg.key_]); + } + } + return *node; +} + + +} // namespace Json + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: src/lib_json/json_value.cpp +// ////////////////////////////////////////////////////////////////////// + + + + + + +// ////////////////////////////////////////////////////////////////////// +// Beginning of content of file: src/lib_json/json_writer.cpp +// ////////////////////////////////////////////////////////////////////// + +// Copyright 2011 Baptiste Lepilleur +// Distributed under MIT license, or public domain if desired and +// recognized in your jurisdiction. +// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE + +#if !defined(JSON_IS_AMALGAMATION) +# include +# include "json_tool.h" +#endif // if !defined(JSON_IS_AMALGAMATION) +#include +#include +#include +#include +#include +#include + +#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0 +#pragma warning( disable : 4996 ) // disable warning about strdup being deprecated. +#endif + +namespace Json { + +static bool containsControlCharacter( const char* str ) +{ + while ( *str ) + { + if ( isControlCharacter( *(str++) ) ) + return true; + } + return false; +} + + +std::string valueToString( LargestInt value ) +{ + UIntToStringBuffer buffer; + char *current = buffer + sizeof(buffer); + bool isNegative = value < 0; + if ( isNegative ) + value = -value; + uintToString( LargestUInt(value), current ); + if ( isNegative ) + *--current = '-'; + assert( current >= buffer ); + return current; +} + + +std::string valueToString( LargestUInt value ) +{ + UIntToStringBuffer buffer; + char *current = buffer + sizeof(buffer); + uintToString( value, current ); + assert( current >= buffer ); + return current; +} + +#if defined(JSON_HAS_INT64) + +std::string valueToString( Int value ) +{ + return valueToString( LargestInt(value) ); +} + + +std::string valueToString( UInt value ) +{ + return valueToString( LargestUInt(value) ); +} + +#endif // # if defined(JSON_HAS_INT64) + + +std::string valueToString( double value ) +{ + char buffer[32]; +#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) // Use secure version with visual studio 2005 to avoid warning. + sprintf_s(buffer, sizeof(buffer), "%#.16g", value); +#else + sprintf(buffer, "%#.16g", value); +#endif + char* ch = buffer + strlen(buffer) - 1; + if (*ch != '0') return buffer; // nothing to truncate, so save time + while(ch > buffer && *ch == '0'){ + --ch; + } + char* last_nonzero = ch; + while(ch >= buffer){ + switch(*ch){ + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + --ch; + continue; + case '.': + // Truncate zeroes to save bytes in output, but keep one. + *(last_nonzero+2) = '\0'; + return buffer; + default: + return buffer; + } + } + return buffer; +} + + +std::string valueToString( bool value ) +{ + return value ? "true" : "false"; +} + +std::string valueToQuotedString( const char *value ) +{ + if (value == NULL) + return ""; + // Not sure how to handle unicode... + if (strpbrk(value, "\"\\\b\f\n\r\t") == NULL && !containsControlCharacter( value )) + return std::string("\"") + value + "\""; + // We have to walk value and escape any special characters. + // Appending to std::string is not efficient, but this should be rare. + // (Note: forward slashes are *not* rare, but I am not escaping them.) + std::string::size_type maxsize = strlen(value)*2 + 3; // allescaped+quotes+NULL + std::string result; + result.reserve(maxsize); // to avoid lots of mallocs + result += "\""; + for (const char* c=value; *c != 0; ++c) + { + switch(*c) + { + case '\"': + result += "\\\""; + break; + case '\\': + result += "\\\\"; + break; + case '\b': + result += "\\b"; + break; + case '\f': + result += "\\f"; + break; + case '\n': + result += "\\n"; + break; + case '\r': + result += "\\r"; + break; + case '\t': + result += "\\t"; + break; + //case '/': + // Even though \/ is considered a legal escape in JSON, a bare + // slash is also legal, so I see no reason to escape it. + // (I hope I am not misunderstanding something. + // blep notes: actually escaping \/ may be useful in javascript to avoid (*c); + result += oss.str(); + } + else + { + result += *c; + } + break; + } + } + result += "\""; + return result; +} + +// Class Writer +// ////////////////////////////////////////////////////////////////// +Writer::~Writer() +{ +} + + +// Class FastWriter +// ////////////////////////////////////////////////////////////////// + +FastWriter::FastWriter() + : yamlCompatiblityEnabled_( false ), + dropNullPlaceholders_( false ) +{ +} + + +void +FastWriter::enableYAMLCompatibility() +{ + yamlCompatiblityEnabled_ = true; +} + + +void +FastWriter::dropNullPlaceholders() +{ + dropNullPlaceholders_ = true; +} + + +std::string +FastWriter::write( const Value &root ) +{ + document_ = ""; + writeValue( root ); + document_ += "\n"; + return document_; +} + + +void +FastWriter::writeValue( const Value &value ) +{ + switch ( value.type() ) + { + case nullValue: + if (!dropNullPlaceholders_) document_ += "null"; + break; + case intValue: + document_ += valueToString( value.asLargestInt() ); + break; + case uintValue: + document_ += valueToString( value.asLargestUInt() ); + break; + case realValue: + document_ += valueToString( value.asDouble() ); + break; + case stringValue: + document_ += valueToQuotedString( value.asCString() ); + break; + case booleanValue: + document_ += valueToString( value.asBool() ); + break; + case arrayValue: + { + document_ += "["; + int size = value.size(); + for ( int index =0; index < size; ++index ) + { + if ( index > 0 ) + document_ += ","; + writeValue( value[index] ); + } + document_ += "]"; + } + break; + case objectValue: + { + Value::Members members( value.getMemberNames() ); + document_ += "{"; + for ( Value::Members::iterator it = members.begin(); + it != members.end(); + ++it ) + { + const std::string &name = *it; + if ( it != members.begin() ) + document_ += ","; + document_ += valueToQuotedString( name.c_str() ); + document_ += yamlCompatiblityEnabled_ ? ": " + : ":"; + writeValue( value[name] ); + } + document_ += "}"; + } + break; + } +} + + +// Class StyledWriter +// ////////////////////////////////////////////////////////////////// + +StyledWriter::StyledWriter() + : rightMargin_( 74 ) + , indentSize_( 3 ) + , addChildValues_() +{ +} + + +std::string +StyledWriter::write( const Value &root ) +{ + document_ = ""; + addChildValues_ = false; + indentString_ = ""; + writeCommentBeforeValue( root ); + writeValue( root ); + writeCommentAfterValueOnSameLine( root ); + document_ += "\n"; + return document_; +} + + +void +StyledWriter::writeValue( const Value &value ) +{ + switch ( value.type() ) + { + case nullValue: + pushValue( "null" ); + break; + case intValue: + pushValue( valueToString( value.asLargestInt() ) ); + break; + case uintValue: + pushValue( valueToString( value.asLargestUInt() ) ); + break; + case realValue: + pushValue( valueToString( value.asDouble() ) ); + break; + case stringValue: + pushValue( valueToQuotedString( value.asCString() ) ); + break; + case booleanValue: + pushValue( valueToString( value.asBool() ) ); + break; + case arrayValue: + writeArrayValue( value); + break; + case objectValue: + { + Value::Members members( value.getMemberNames() ); + if ( members.empty() ) + pushValue( "{}" ); + else + { + writeWithIndent( "{" ); + indent(); + Value::Members::iterator it = members.begin(); + for (;;) + { + const std::string &name = *it; + const Value &childValue = value[name]; + writeCommentBeforeValue( childValue ); + writeWithIndent( valueToQuotedString( name.c_str() ) ); + document_ += " : "; + writeValue( childValue ); + if ( ++it == members.end() ) + { + writeCommentAfterValueOnSameLine( childValue ); + break; + } + document_ += ","; + writeCommentAfterValueOnSameLine( childValue ); + } + unindent(); + writeWithIndent( "}" ); + } + } + break; + } +} + + +void +StyledWriter::writeArrayValue( const Value &value ) +{ + unsigned size = value.size(); + if ( size == 0 ) + pushValue( "[]" ); + else + { + bool isArrayMultiLine = isMultineArray( value ); + if ( isArrayMultiLine ) + { + writeWithIndent( "[" ); + indent(); + bool hasChildValue = !childValues_.empty(); + unsigned index =0; + for (;;) + { + const Value &childValue = value[index]; + writeCommentBeforeValue( childValue ); + if ( hasChildValue ) + writeWithIndent( childValues_[index] ); + else + { + writeIndent(); + writeValue( childValue ); + } + if ( ++index == size ) + { + writeCommentAfterValueOnSameLine( childValue ); + break; + } + document_ += ","; + writeCommentAfterValueOnSameLine( childValue ); + } + unindent(); + writeWithIndent( "]" ); + } + else // output on a single line + { + assert( childValues_.size() == size ); + document_ += "[ "; + for ( unsigned index =0; index < size; ++index ) + { + if ( index > 0 ) + document_ += ", "; + document_ += childValues_[index]; + } + document_ += " ]"; + } + } +} + + +bool +StyledWriter::isMultineArray( const Value &value ) +{ + int size = value.size(); + bool isMultiLine = size*3 >= rightMargin_ ; + childValues_.clear(); + for ( int index =0; index < size && !isMultiLine; ++index ) + { + const Value &childValue = value[index]; + isMultiLine = isMultiLine || + ( (childValue.isArray() || childValue.isObject()) && + childValue.size() > 0 ); + } + if ( !isMultiLine ) // check if line length > max line length + { + childValues_.reserve( size ); + addChildValues_ = true; + int lineLength = 4 + (size-1)*2; // '[ ' + ', '*n + ' ]' + for ( int index =0; index < size && !isMultiLine; ++index ) + { + writeValue( value[index] ); + lineLength += int( childValues_[index].length() ); + isMultiLine = isMultiLine && hasCommentForValue( value[index] ); + } + addChildValues_ = false; + isMultiLine = isMultiLine || lineLength >= rightMargin_; + } + return isMultiLine; +} + + +void +StyledWriter::pushValue( const std::string &value ) +{ + if ( addChildValues_ ) + childValues_.push_back( value ); + else + document_ += value; +} + + +void +StyledWriter::writeIndent() +{ + if ( !document_.empty() ) + { + char last = document_[document_.length()-1]; + if ( last == ' ' ) // already indented + return; + if ( last != '\n' ) // Comments may add new-line + document_ += '\n'; + } + document_ += indentString_; +} + + +void +StyledWriter::writeWithIndent( const std::string &value ) +{ + writeIndent(); + document_ += value; +} + + +void +StyledWriter::indent() +{ + indentString_ += std::string( indentSize_, ' ' ); +} + + +void +StyledWriter::unindent() +{ + assert( int(indentString_.size()) >= indentSize_ ); + indentString_.resize( indentString_.size() - indentSize_ ); +} + + +void +StyledWriter::writeCommentBeforeValue( const Value &root ) +{ + if ( !root.hasComment( commentBefore ) ) + return; + document_ += normalizeEOL( root.getComment( commentBefore ) ); + document_ += "\n"; +} + + +void +StyledWriter::writeCommentAfterValueOnSameLine( const Value &root ) +{ + if ( root.hasComment( commentAfterOnSameLine ) ) + document_ += " " + normalizeEOL( root.getComment( commentAfterOnSameLine ) ); + + if ( root.hasComment( commentAfter ) ) + { + document_ += "\n"; + document_ += normalizeEOL( root.getComment( commentAfter ) ); + document_ += "\n"; + } +} + + +bool +StyledWriter::hasCommentForValue( const Value &value ) +{ + return value.hasComment( commentBefore ) + || value.hasComment( commentAfterOnSameLine ) + || value.hasComment( commentAfter ); +} + + +std::string +StyledWriter::normalizeEOL( const std::string &text ) +{ + std::string normalized; + normalized.reserve( text.length() ); + const char *begin = text.c_str(); + const char *end = begin + text.length(); + const char *current = begin; + while ( current != end ) + { + char c = *current++; + if ( c == '\r' ) // mac or dos EOL + { + if ( *current == '\n' ) // convert dos EOL + ++current; + normalized += '\n'; + } + else // handle unix EOL & other char + normalized += c; + } + return normalized; +} + + +// Class StyledStreamWriter +// ////////////////////////////////////////////////////////////////// + +StyledStreamWriter::StyledStreamWriter( std::string indentation ) + : document_(NULL) + , rightMargin_( 74 ) + , indentation_( indentation ) + , addChildValues_() +{ +} + + +void +StyledStreamWriter::write( std::ostream &out, const Value &root ) +{ + document_ = &out; + addChildValues_ = false; + indentString_ = ""; + writeCommentBeforeValue( root ); + writeValue( root ); + writeCommentAfterValueOnSameLine( root ); + *document_ << "\n"; + document_ = NULL; // Forget the stream, for safety. +} + + +void +StyledStreamWriter::writeValue( const Value &value ) +{ + switch ( value.type() ) + { + case nullValue: + pushValue( "null" ); + break; + case intValue: + pushValue( valueToString( value.asLargestInt() ) ); + break; + case uintValue: + pushValue( valueToString( value.asLargestUInt() ) ); + break; + case realValue: + pushValue( valueToString( value.asDouble() ) ); + break; + case stringValue: + pushValue( valueToQuotedString( value.asCString() ) ); + break; + case booleanValue: + pushValue( valueToString( value.asBool() ) ); + break; + case arrayValue: + writeArrayValue( value); + break; + case objectValue: + { + Value::Members members( value.getMemberNames() ); + if ( members.empty() ) + pushValue( "{}" ); + else + { + writeWithIndent( "{" ); + indent(); + Value::Members::iterator it = members.begin(); + for (;;) + { + const std::string &name = *it; + const Value &childValue = value[name]; + writeCommentBeforeValue( childValue ); + writeWithIndent( valueToQuotedString( name.c_str() ) ); + *document_ << " : "; + writeValue( childValue ); + if ( ++it == members.end() ) + { + writeCommentAfterValueOnSameLine( childValue ); + break; + } + *document_ << ","; + writeCommentAfterValueOnSameLine( childValue ); + } + unindent(); + writeWithIndent( "}" ); + } + } + break; + } +} + + +void +StyledStreamWriter::writeArrayValue( const Value &value ) +{ + unsigned size = value.size(); + if ( size == 0 ) + pushValue( "[]" ); + else + { + bool isArrayMultiLine = isMultineArray( value ); + if ( isArrayMultiLine ) + { + writeWithIndent( "[" ); + indent(); + bool hasChildValue = !childValues_.empty(); + unsigned index =0; + for (;;) + { + const Value &childValue = value[index]; + writeCommentBeforeValue( childValue ); + if ( hasChildValue ) + writeWithIndent( childValues_[index] ); + else + { + writeIndent(); + writeValue( childValue ); + } + if ( ++index == size ) + { + writeCommentAfterValueOnSameLine( childValue ); + break; + } + *document_ << ","; + writeCommentAfterValueOnSameLine( childValue ); + } + unindent(); + writeWithIndent( "]" ); + } + else // output on a single line + { + assert( childValues_.size() == size ); + *document_ << "[ "; + for ( unsigned index =0; index < size; ++index ) + { + if ( index > 0 ) + *document_ << ", "; + *document_ << childValues_[index]; + } + *document_ << " ]"; + } + } +} + + +bool +StyledStreamWriter::isMultineArray( const Value &value ) +{ + int size = value.size(); + bool isMultiLine = size*3 >= rightMargin_ ; + childValues_.clear(); + for ( int index =0; index < size && !isMultiLine; ++index ) + { + const Value &childValue = value[index]; + isMultiLine = isMultiLine || + ( (childValue.isArray() || childValue.isObject()) && + childValue.size() > 0 ); + } + if ( !isMultiLine ) // check if line length > max line length + { + childValues_.reserve( size ); + addChildValues_ = true; + int lineLength = 4 + (size-1)*2; // '[ ' + ', '*n + ' ]' + for ( int index =0; index < size && !isMultiLine; ++index ) + { + writeValue( value[index] ); + lineLength += int( childValues_[index].length() ); + isMultiLine = isMultiLine && hasCommentForValue( value[index] ); + } + addChildValues_ = false; + isMultiLine = isMultiLine || lineLength >= rightMargin_; + } + return isMultiLine; +} + + +void +StyledStreamWriter::pushValue( const std::string &value ) +{ + if ( addChildValues_ ) + childValues_.push_back( value ); + else + *document_ << value; +} + + +void +StyledStreamWriter::writeIndent() +{ + /* + Some comments in this method would have been nice. ;-) + + if ( !document_.empty() ) + { + char last = document_[document_.length()-1]; + if ( last == ' ' ) // already indented + return; + if ( last != '\n' ) // Comments may add new-line + *document_ << '\n'; + } + */ + *document_ << '\n' << indentString_; +} + + +void +StyledStreamWriter::writeWithIndent( const std::string &value ) +{ + writeIndent(); + *document_ << value; +} + + +void +StyledStreamWriter::indent() +{ + indentString_ += indentation_; +} + + +void +StyledStreamWriter::unindent() +{ + assert( indentString_.size() >= indentation_.size() ); + indentString_.resize( indentString_.size() - indentation_.size() ); +} + + +void +StyledStreamWriter::writeCommentBeforeValue( const Value &root ) +{ + if ( !root.hasComment( commentBefore ) ) + return; + *document_ << normalizeEOL( root.getComment( commentBefore ) ); + *document_ << "\n"; +} + + +void +StyledStreamWriter::writeCommentAfterValueOnSameLine( const Value &root ) +{ + if ( root.hasComment( commentAfterOnSameLine ) ) + *document_ << " " + normalizeEOL( root.getComment( commentAfterOnSameLine ) ); + + if ( root.hasComment( commentAfter ) ) + { + *document_ << "\n"; + *document_ << normalizeEOL( root.getComment( commentAfter ) ); + *document_ << "\n"; + } +} + + +bool +StyledStreamWriter::hasCommentForValue( const Value &value ) +{ + return value.hasComment( commentBefore ) + || value.hasComment( commentAfterOnSameLine ) + || value.hasComment( commentAfter ); +} + + +std::string +StyledStreamWriter::normalizeEOL( const std::string &text ) +{ + std::string normalized; + normalized.reserve( text.length() ); + const char *begin = text.c_str(); + const char *end = begin + text.length(); + const char *current = begin; + while ( current != end ) + { + char c = *current++; + if ( c == '\r' ) // mac or dos EOL + { + if ( *current == '\n' ) // convert dos EOL + ++current; + normalized += '\n'; + } + else // handle unix EOL & other char + normalized += c; + } + return normalized; +} + + +std::ostream& operator<<( std::ostream &sout, const Value &root ) +{ + Json::StyledStreamWriter writer; + writer.write(sout, root); + return sout; +} + + +} // namespace Json + +// ////////////////////////////////////////////////////////////////////// +// End of content of file: src/lib_json/json_writer.cpp +// ////////////////////////////////////////////////////////////////////// + + + + + diff --git a/src/mods.h b/src/mods.h index 12576516d..af7777d18 100644 --- a/src/mods.h +++ b/src/mods.h @@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include -#include "json/json.h" +#include #include "config.h" #define MODNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_" diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 06e20c2a0..c664101ea 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -33,7 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" #include "mg_schematic.h" #include "noise.h" -#include "json/json.h" +#include struct EnumString es_TileAnimationType[] = { @@ -1250,8 +1250,13 @@ static bool push_json_value_helper(lua_State *L, const Json::Value &value, lua_newtable(L); for (Json::Value::const_iterator it = value.begin(); it != value.end(); ++it) { +#ifndef JSONCPP_STRING const char *str = it.memberName(); lua_pushstring(L, str ? str : ""); +#else + std::string str = it.name(); + lua_pushstring(L, str.c_str()); +#endif push_json_value_helper(L, *it, nullindex); lua_rawset(L, -3); } diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 95a5fc4d1..13c0d702f 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/c_content.h" #include "cpp_api/s_async.h" #include "serialization.h" -#include "json/json.h" +#include #include "cpp_api/s_security.h" #include "porting.h" #include "debug.h" diff --git a/src/serverlist.cpp b/src/serverlist.cpp index de7962a68..87ca5dc04 100644 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" #include "log.h" #include "network/networkprotocol.h" -#include "json/json.h" +#include #include "convert_json.h" #include "httpfetch.h" #include "util/string.h" diff --git a/src/serverlist.h b/src/serverlist.h index 8ffea44cc..0747c3920 100644 --- a/src/serverlist.h +++ b/src/serverlist.h @@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "config.h" #include "mods.h" -#include "json/json.h" +#include #ifndef SERVERLIST_HEADER #define SERVERLIST_HEADER -- cgit v1.2.3 From c013c73f338b1c2227662458ebc650883f83271c Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 10 Aug 2016 12:17:48 +0200 Subject: Lua->C getintfield() use lua_tointeger (#4408) previously function used tonumber which returned float this caused errors in large numbers and resulted in obj-def-handlers being invalid when retrived from lua tables in c --- src/script/common/c_converter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/script') diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index 55c4a5f5a..857300fa5 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -391,7 +391,7 @@ bool getintfield(lua_State *L, int table, lua_getfield(L, table, fieldname); bool got = false; if(lua_isnumber(L, -1)){ - result = lua_tonumber(L, -1); + result = lua_tointeger(L, -1); got = true; } lua_pop(L, 1); @@ -404,7 +404,7 @@ bool getintfield(lua_State *L, int table, lua_getfield(L, table, fieldname); bool got = false; if(lua_isnumber(L, -1)){ - result = lua_tonumber(L, -1); + result = lua_tointeger(L, -1); got = true; } lua_pop(L, 1); @@ -417,7 +417,7 @@ bool getintfield(lua_State *L, int table, lua_getfield(L, table, fieldname); bool got = false; if(lua_isnumber(L, -1)){ - result = lua_tonumber(L, -1); + result = lua_tointeger(L, -1); got = true; } lua_pop(L, 1); @@ -430,7 +430,7 @@ bool getintfield(lua_State *L, int table, lua_getfield(L, table, fieldname); bool got = false; if(lua_isnumber(L, -1)){ - result = lua_tonumber(L, -1); + result = lua_tointeger(L, -1); got = true; } lua_pop(L, 1); -- cgit v1.2.3 From 48b3bb980d4a026d32739acc1982f16e3c303c5b Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 28 Jul 2016 08:56:22 +0100 Subject: couple of memory leaks fixes. --- src/script/cpp_api/s_security.cpp | 18 +++++++++++++++++- src/sound_openal.cpp | 1 + src/unittest/test_map_settings_manager.cpp | 4 +++- src/util/areastore.cpp | 1 + src/util/srp.cpp | 2 +- 5 files changed, 23 insertions(+), 3 deletions(-) (limited to 'src/script') diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index e117a4811..0e64c4c61 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -285,6 +285,10 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path) if (c == LUA_SIGNATURE[0]) { lua_pushliteral(L, "Bytecode prohibited when mod security is enabled."); + std::fclose(fp); + if (path) { + delete [] chunk_name; + } return false; } @@ -295,7 +299,15 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path) size_t size = std::ftell(fp) - start; char *code = new char[size]; ret = std::fseek(fp, start, SEEK_SET); - CHECK_FILE_ERR(ret, fp); + if (ret) { + lua_pushfstring(L, "%s: %s", path, strerror(errno)); + std::fclose(fp); + delete [] code; + if (path) { + delete [] chunk_name; + } + return false; + } size_t num_read = std::fread(code, 1, size, fp); if (path) { @@ -303,6 +315,10 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path) } if (num_read != size) { lua_pushliteral(L, "Error reading file to load."); + delete [] code; + if (path) { + delete [] chunk_name; + } return false; } diff --git a/src/sound_openal.cpp b/src/sound_openal.cpp index e2b6d937a..1832a0c77 100644 --- a/src/sound_openal.cpp +++ b/src/sound_openal.cpp @@ -144,6 +144,7 @@ SoundBuffer *load_opened_ogg_file(OggVorbis_File *oggFile, ov_clear(oggFile); infostream << "Audio: Error decoding " << filename_for_logging << std::endl; + delete snd; return NULL; } diff --git a/src/unittest/test_map_settings_manager.cpp b/src/unittest/test_map_settings_manager.cpp index 9292bf87c..4f5ac80f2 100644 --- a/src/unittest/test_map_settings_manager.cpp +++ b/src/unittest/test_map_settings_manager.cpp @@ -75,8 +75,10 @@ std::string read_file_to_string(const std::string &filepath) fseek(f, 0, SEEK_END); long filesize = ftell(f); - if (filesize == -1) + if (filesize == -1) { + fclose(f); return ""; + } rewind(f); buf.resize(filesize); diff --git a/src/util/areastore.cpp b/src/util/areastore.cpp index 58f08a8c2..cef67da2c 100644 --- a/src/util/areastore.cpp +++ b/src/util/areastore.cpp @@ -95,6 +95,7 @@ void AreaStore::deserialize(std::istream &is) is.read(data, data_len); a.data = std::string(data, data_len); insertArea(&a); + delete [] data; } } diff --git a/src/util/srp.cpp b/src/util/srp.cpp index 0d3c938a3..77c1816e8 100644 --- a/src/util/srp.cpp +++ b/src/util/srp.cpp @@ -542,7 +542,7 @@ static SRP_Result fill_buff() if (!fp) return SRP_ERR; - if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) return SRP_ERR; + if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) { fclose(fp); return SRP_ERR; } if (fclose(fp)) return SRP_ERR; #endif return SRP_OK; -- cgit v1.2.3 From 5c0e659516857733d968707490b16bcf62f33c92 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sat, 30 Jul 2016 12:50:22 +0200 Subject: Script API: Make the craft recipe field 'method' consistent --- src/script/lua_api/l_craft.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/script') diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp index d135c689f..2236566de 100644 --- a/src/script/lua_api/l_craft.cpp +++ b/src/script/lua_api/l_craft.cpp @@ -422,20 +422,28 @@ static void push_craft_recipe(lua_State *L, IGameDef *gdef, } lua_setfield(L, -2, "items"); setintfield(L, -1, "width", input.width); + + std::string method_s; switch (input.method) { case CRAFT_METHOD_NORMAL: - lua_pushstring(L, "normal"); + method_s = "normal"; break; case CRAFT_METHOD_COOKING: - lua_pushstring(L, "cooking"); + method_s = "cooking"; break; case CRAFT_METHOD_FUEL: - lua_pushstring(L, "fuel"); + method_s = "fuel"; break; default: - lua_pushstring(L, "unknown"); + method_s = "unknown"; } + lua_pushstring(L, method_s.c_str()); + lua_setfield(L, -2, "method"); + + // Deprecated, only for compatibility's sake + lua_pushstring(L, method_s.c_str()); lua_setfield(L, -2, "type"); + lua_pushstring(L, output.item.c_str()); lua_setfield(L, -2, "output"); } -- cgit v1.2.3 From e58a55aa82dfc66325a694dcc3519d3c0f3388a6 Mon Sep 17 00:00:00 2001 From: Auke Kok Date: Thu, 10 Dec 2015 22:58:11 -0800 Subject: Make plantlike drawtype more fun Adds several new ways that the plantlike drawtype mesh can be changed. This requires paramtype2 = "meshoptions" to be set in the node definition. The drawtype for these nodes should be "plantlike". These modifications are all done using param2. This field is now a complex bitfield that allows some or more of the combinations to be chosen, and the mesh draw code will choose the options based as neeeded for each plantlike node. bit layout: bits 0, 1 and 2 (values 0x1 through 0x7) are for choosing the plant mesh shape: 0 - ordinary plantlike plant ("x" shaped) 1 - ordinary plant, but rotated 45 degrees ("+" shaped) 2 - a plant with 3 faces ("*" shaped) 3 - a plant with 4 faces ("#" shaped) 4 - a plant with 4 faces ("#" shaped, leaning outwards) 5 through 7 are unused and reserved for future mesh shapes. bit 3 (0x8) causes the plant to be randomly offset in the x,z plane. The plant should fall within the 1x1x1 nodebox if regularly sized. bit 4 (0x10) causes the plant mesh to grow by sqrt(2), and will cause the plant mesh to fill out 1x1x1, and appear slightly larger. Texture makers will want to make their plant texture 23x16 pixels to have the best visual fit in 1x1x1 size. bit 5 (0x20) causes each face of the plant to have a slight negative Y offset in position, descending up to 0.125 downwards into the node below. Because this is per face, this causes the plant model to be less symmetric. bit 6 (0x40) through bit 7 (0x80) are unused and reserved for future use. !(https://youtu.be/qWuI664krsI) --- doc/lua_api.txt | 16 +++++ src/content_mapblock.cpp | 152 +++++++++++++++++++++++++++++++++++++++--- src/network/networkprotocol.h | 4 +- src/nodedef.cpp | 5 +- src/nodedef.h | 2 + src/script/cpp_api/s_node.cpp | 1 + 6 files changed, 167 insertions(+), 13 deletions(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 38f19aadc..440edd963 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -578,6 +578,22 @@ node definition: ^ The rotation of this node is stored in param2. Plants are rotated this way. Values range 0 - 179. The value stored in param2 is multiplied by two to get the actual rotation of the node. + paramtype2 == "meshoptions" + ^ Only valid for "plantlike". The value of param2 becomes a bitfield which can + be used to change how the client draws plantlike nodes. Bits 0, 1 and 2 form + a mesh selector. Currently the following meshes are choosable: + 0 = a "x" shaped plant (ordinary plant) + 1 = a "+" shaped plant (just rotated 45 degrees) + 2 = a "*" shaped plant with 3 faces instead of 2 + 3 = a "#" shaped plant with 4 faces instead of 2 + 4 = a "#" shaped plant with 4 faces that lean outwards + 5-7 are unused and reserved for future meshes. + Bits 3 through 7 are optional flags that can be combined and give these + effects: + bit 3 (0x08) - Makes the plant slightly vary placement horizontally + bit 4 (0x10) - Makes the plant mesh 1.4x larger + bit 5 (0x20) - Moves each face randomly a small bit down (1/8 max) + bits 6-7 are reserved for future use. collision_box = { type = "fixed", fixed = { diff --git a/src/content_mapblock.cpp b/src/content_mapblock.cpp index 6a83bd8f3..b86b97822 100644 --- a/src/content_mapblock.cpp +++ b/src/content_mapblock.cpp @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "gamedef.h" #include "log.h" +#include "noise.h" // Create a cuboid. @@ -1104,6 +1105,8 @@ void mapblock_mesh_generate_special(MeshMakeData *data, break;} case NDT_PLANTLIKE: { + PseudoRandom rng(x<<8 | z | y<<16); + TileSpec tile = getNodeTileN(n, p, 0, data); tile.material_flags |= MATERIAL_FLAG_CRACK_OVERLAY; @@ -1111,9 +1114,18 @@ void mapblock_mesh_generate_special(MeshMakeData *data, video::SColor c = MapBlock_LightColor(255, l, f.light_source); float s = BS / 2 * f.visual_scale; + // add sqrt(2) visual scale + if ((f.param_type_2 == CPT2_MESHOPTIONS) && ((n.param2 & 0x10) != 0)) + s *= 1.41421; + + float random_offset_X = .0; + float random_offset_Z = .0; + if ((f.param_type_2 == CPT2_MESHOPTIONS) && ((n.param2 & 0x8) != 0)) { + random_offset_X = BS * ((rng.next() % 16 / 16.0) * 0.29 - 0.145); + random_offset_Z = BS * ((rng.next() % 16 / 16.0) * 0.29 - 0.145); + } - for (int j = 0; j < 2; j++) - { + for (int j = 0; j < 4; j++) { video::S3DVertex vertices[4] = { video::S3DVertex(-s,-BS/2, 0, 0,0,0, c, 0,1), @@ -1121,28 +1133,146 @@ void mapblock_mesh_generate_special(MeshMakeData *data, video::S3DVertex( s,-BS/2 + s*2,0, 0,0,0, c, 1,0), video::S3DVertex(-s,-BS/2 + s*2,0, 0,0,0, c, 0,0), }; + float rotate_degree = 0; + u8 p2mesh = 0; if (f.param_type_2 == CPT2_DEGROTATE) rotate_degree = n.param2 * 2; - - if (j == 0) { - for(u16 i = 0; i < 4; i++) - vertices[i].Pos.rotateXZBy(46 + rotate_degree); - } else if (j == 1) { - for(u16 i = 0; i < 4; i++) - vertices[i].Pos.rotateXZBy(-44 + rotate_degree); + else if (f.param_type_2 != CPT2_MESHOPTIONS) { + if (j == 0) { + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(46 + rotate_degree); + } else if (j == 1) { + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(-44 + rotate_degree); + } + } else { + p2mesh = n.param2 & 0x7; + switch (p2mesh) { + case 0: + // x + if (j == 0) { + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(46); + } else if (j == 1) { + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(-44); + } + break; + case 1: + // + + if (j == 0) { + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(91); + } else if (j == 1) { + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(1); + } + break; + case 2: + // * + if (j == 0) { + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(121); + } else if (j == 1) { + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(241); + } else { // (j == 2) + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(1); + } + break; + case 3: + // # + switch (j) { + case 0: + for (u16 i = 0; i < 4; i++) { + vertices[i].Pos.rotateXZBy(1); + vertices[i].Pos.Z += BS / 4; + } + break; + case 1: + for (u16 i = 0; i < 4; i++) { + vertices[i].Pos.rotateXZBy(91); + vertices[i].Pos.X += BS / 4; + } + break; + case 2: + for (u16 i = 0; i < 4; i++) { + vertices[i].Pos.rotateXZBy(181); + vertices[i].Pos.Z -= BS / 4; + } + break; + case 3: + for (u16 i = 0; i < 4; i++) { + vertices[i].Pos.rotateXZBy(271); + vertices[i].Pos.X -= BS / 4; + } + break; + } + break; + case 4: + // outward leaning #-like + switch (j) { + case 0: + for (u16 i = 2; i < 4; i++) + vertices[i].Pos.Z -= BS / 2; + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(1); + break; + case 1: + for (u16 i = 2; i < 4; i++) + vertices[i].Pos.Z -= BS / 2; + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(91); + break; + case 2: + for (u16 i = 2; i < 4; i++) + vertices[i].Pos.Z -= BS / 2; + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(181); + break; + case 3: + for (u16 i = 2; i < 4; i++) + vertices[i].Pos.Z -= BS / 2; + for (u16 i = 0; i < 4; i++) + vertices[i].Pos.rotateXZBy(271); + break; + } + break; + } } - for (int i = 0; i < 4; i++) - { + for (int i = 0; i < 4; i++) { vertices[i].Pos *= f.visual_scale; vertices[i].Pos.Y += BS/2 * (f.visual_scale - 1); vertices[i].Pos += intToFloat(p, BS); + // move to a random spot to avoid moire + if ((f.param_type_2 == CPT2_MESHOPTIONS) && ((n.param2 & 0x8) != 0)) { + vertices[i].Pos.X += random_offset_X; + vertices[i].Pos.Z += random_offset_Z; + } + // randomly move each face up/down + if ((f.param_type_2 == CPT2_MESHOPTIONS) && ((n.param2 & 0x20) != 0)) { + PseudoRandom yrng(j | x<<16 | z<<8 | y<<24 ); + vertices[i].Pos.Y -= BS * ((yrng.next() % 16 / 16.0) * 0.125); + } } u16 indices[] = {0, 1, 2, 2, 3, 0}; // Add to mesh collector collector.append(tile, vertices, 4, indices, 6); + + // stop adding faces for meshes with less than 4 faces + if (f.param_type_2 == CPT2_MESHOPTIONS) { + if (((p2mesh == 0) || (p2mesh == 1)) && (j == 1)) + break; + else if ((p2mesh == 2) && (j == 2)) + break; + } else if (j == 1) { + break; + } + } break;} case NDT_FIRELIKE: diff --git a/src/network/networkprotocol.h b/src/network/networkprotocol.h index 3a9483efb..e3fcae0c6 100644 --- a/src/network/networkprotocol.h +++ b/src/network/networkprotocol.h @@ -136,9 +136,11 @@ with this program; if not, write to the Free Software Foundation, Inc., backface_culling: backwards compatibility for playing with newer client on pre-27 servers. Add nodedef v3 - connected nodeboxes + PROTOCOL_VERSION 28: + CPT2_MESHOPTIONS */ -#define LATEST_PROTOCOL_VERSION 27 +#define LATEST_PROTOCOL_VERSION 28 // Server's supported network protocol range #define SERVER_PROTOCOL_VERSION_MIN 13 diff --git a/src/nodedef.cpp b/src/nodedef.cpp index 764203efc..646375575 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -387,7 +387,10 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const writeU8(os, post_effect_color.getGreen()); writeU8(os, post_effect_color.getBlue()); writeU8(os, param_type); - writeU8(os, param_type_2); + if ((protocol_version < 28) && (param_type_2 == CPT2_MESHOPTIONS)) + writeU8(os, CPT2_NONE); + else + writeU8(os, param_type_2); writeU8(os, is_ground_content); writeU8(os, light_propagates); writeU8(os, sunlight_propagates); diff --git a/src/nodedef.h b/src/nodedef.h index 1c2f792d8..f17c53727 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -65,6 +65,8 @@ enum ContentParamType2 CPT2_LEVELED, // 2D rotation for things like plants CPT2_DEGROTATE, + // Mesh options for plants + CPT2_MESHOPTIONS }; enum LiquidType diff --git a/src/script/cpp_api/s_node.cpp b/src/script/cpp_api/s_node.cpp index 17f0f0dac..379ed773f 100644 --- a/src/script/cpp_api/s_node.cpp +++ b/src/script/cpp_api/s_node.cpp @@ -58,6 +58,7 @@ struct EnumString ScriptApiNode::es_ContentParamType2[] = {CPT2_WALLMOUNTED, "wallmounted"}, {CPT2_LEVELED, "leveled"}, {CPT2_DEGROTATE, "degrotate"}, + {CPT2_MESHOPTIONS, "meshoptions"}, {0, NULL}, }; -- cgit v1.2.3 From aa33166386f737f213f1f3005ffd6a6adfd2d97f Mon Sep 17 00:00:00 2001 From: paly2 Date: Sun, 10 Jul 2016 15:15:43 +0200 Subject: Add minetest.unregister_item and minetest.register_alias_force --- builtin/game/register.lua | 38 ++++++++++++++++++++++++++++++++++++++ doc/lua_api.txt | 15 ++++++++++++++- src/itemdef.cpp | 10 ++++++++-- src/itemdef.h | 1 + src/nodedef.cpp | 35 +++++++++++++++++++++++++++++++++++ src/nodedef.h | 2 ++ src/script/lua_api/l_item.cpp | 22 ++++++++++++++++++++++ src/script/lua_api/l_item.h | 1 + 8 files changed, 121 insertions(+), 3 deletions(-) (limited to 'src/script') diff --git a/builtin/game/register.lua b/builtin/game/register.lua index f330491a2..05dc5fef8 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -7,6 +7,9 @@ local register_item_raw = core.register_item_raw core.register_item_raw = nil +local unregister_item_raw = core.unregister_item_raw +core.unregister_item_raw = nil + local register_alias_raw = core.register_alias_raw core.register_alias_raw = nil @@ -172,6 +175,27 @@ function core.register_item(name, itemdef) register_item_raw(itemdef) end +function core.unregister_item(name) + if not core.registered_items[name] then + core.log("warning", "Not unregistering item " ..name.. + " because it doesn't exist.") + return + end + -- Erase from registered_* table + local type = core.registered_items[name].type + if type == "node" then + core.registered_nodes[name] = nil + elseif type == "craft" then + core.registered_craftitems[name] = nil + elseif type == "tool" then + core.registered_tools[name] = nil + end + core.registered_items[name] = nil + + + unregister_item_raw(name) +end + function core.register_node(name, nodedef) nodedef.type = "node" core.register_item(name, nodedef) @@ -242,6 +266,20 @@ function core.register_alias(name, convert_to) end end +function core.register_alias_force(name, convert_to) + if forbidden_item_names[name] then + error("Unable to register alias: Name is forbidden: " .. name) + end + if core.registered_items[name] ~= nil then + core.unregister_item(name) + core.log("info", "Removed item " ..name.. + " while attempting to force add an alias") + end + --core.log("Registering alias: " .. name .. " -> " .. convert_to) + core.registered_aliases[name] = convert_to + register_alias_raw(name, convert_to) +end + function core.on_craft(itemstack, player, old_craft_list, craft_inv) for _, func in ipairs(core.registered_on_crafts) do itemstack = func(itemstack, player, old_craft_list, craft_inv) or itemstack diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 579fe796e..da9ebb9f1 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -208,11 +208,17 @@ when registering it. The `:` prefix can also be used for maintaining backwards compatibility. ### Aliases -Aliases can be added by using `minetest.register_alias(name, convert_to)`. +Aliases can be added by using `minetest.register_alias(name, convert_to)` or +`minetest.register_alias_force(name, convert_to). This will make Minetest to convert things called name to things called `convert_to`. +The only difference between `minetest.register_alias` and +`minetest.register_alias_force` is that if an item called `name` exists, +`minetest.register_alias` will do nothing while +`minetest.register_alias_force` will unregister it. + This can be used for maintaining backwards compatibility. This can be also used for setting quick access names for things, e.g. if @@ -464,6 +470,11 @@ the global `minetest.registered_*` tables. * `minetest.register_craftitem(name, item definition)` * added to `minetest.registered_items[name]` +* `minetest.unregister_item(name)` + * Unregisters the item name from engine, and deletes the entry with key + * `name` from `minetest.registered_items` and from the associated item + * table according to its nature: minetest.registered_nodes[] etc + * `minetest.register_biome(biome definition)` * returns an integer uniquely identifying the registered biome * added to `minetest.registered_biome` with the key of `biome.name` @@ -1883,7 +1894,9 @@ Call these functions only at load time! * `minetest.register_node(name, node definition)` * `minetest.register_tool(name, item definition)` * `minetest.register_craftitem(name, item definition)` +* `minetest.unregister_item(name)` * `minetest.register_alias(name, convert_to)` +* `minetest.register_alias_force(name, convert_to)` * `minetest.register_craft(recipe)` * Check recipe table syntax for different types below. * `minetest.clear_craft(recipe)` diff --git a/src/itemdef.cpp b/src/itemdef.cpp index a618ad631..a6c627a03 100644 --- a/src/itemdef.cpp +++ b/src/itemdef.cpp @@ -466,11 +466,17 @@ public: infostream<<"ItemDefManager: erased alias "< "< will load as . // Alias is not set if has already been defined. // Alias will be removed if is defined at a later point of time. diff --git a/src/nodedef.cpp b/src/nodedef.cpp index 646375575..bfb2999bd 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -778,6 +778,7 @@ public: content_t allocateId(); virtual content_t set(const std::string &name, const ContentFeatures &def); virtual content_t allocateDummy(const std::string &name); + virtual void removeNode(const std::string &name); virtual void updateAliases(IItemDefManager *idef); virtual void applyTextureOverrides(const std::string &override_filepath); virtual void updateTextures(IGameDef *gamedef, @@ -1072,6 +1073,40 @@ content_t CNodeDefManager::allocateDummy(const std::string &name) } +void CNodeDefManager::removeNode(const std::string &name) +{ + // Pre-condition + assert(name != ""); + + // Erase name from name ID mapping + content_t id = CONTENT_IGNORE; + if (m_name_id_mapping.getId(name, id)) { + m_name_id_mapping.eraseName(name); + m_name_id_mapping_with_aliases.erase(name); + } + + // Erase node content from all groups it belongs to + for (std::map::iterator iter_groups = + m_group_to_items.begin(); + iter_groups != m_group_to_items.end();) { + GroupItems &items = iter_groups->second; + for (GroupItems::iterator iter_groupitems = items.begin(); + iter_groupitems != items.end();) { + if (iter_groupitems->first == id) + items.erase(iter_groupitems++); + else + iter_groupitems++; + } + + // Check if group is empty + if (items.size() == 0) + m_group_to_items.erase(iter_groups++); + else + iter_groups++; + } +} + + void CNodeDefManager::updateAliases(IItemDefManager *idef) { std::set all = idef->getAll(); diff --git a/src/nodedef.h b/src/nodedef.h index f17c53727..80396f992 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -384,6 +384,8 @@ public: const ContentFeatures &def)=0; // If returns CONTENT_IGNORE, could not allocate id virtual content_t allocateDummy(const std::string &name)=0; + // Remove a node + virtual void removeNode(const std::string &name)=0; /* Update item alias mapping. diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 5381cba76..ff0baea14 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -525,6 +525,27 @@ int ModApiItemMod::l_register_item_raw(lua_State *L) return 0; /* number of results */ } +// unregister_item(name) +int ModApiItemMod::l_unregister_item_raw(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + std::string name = luaL_checkstring(L, 1); + + IWritableItemDefManager *idef = + getServer(L)->getWritableItemDefManager(); + + // Unregister the node + if (idef->get(name).type == ITEM_NODE) { + IWritableNodeDefManager *ndef = + getServer(L)->getWritableNodeDefManager(); + ndef->removeNode(name); + } + + idef->unregisterItem(name); + + return 0; /* number of results */ +} + // register_alias_raw(name, convert_to_name) int ModApiItemMod::l_register_alias_raw(lua_State *L) { @@ -570,6 +591,7 @@ int ModApiItemMod::l_get_name_from_content_id(lua_State *L) void ModApiItemMod::Initialize(lua_State *L, int top) { API_FCT(register_item_raw); + API_FCT(unregister_item_raw); API_FCT(register_alias_raw); API_FCT(get_content_id); API_FCT(get_name_from_content_id); diff --git a/src/script/lua_api/l_item.h b/src/script/lua_api/l_item.h index 0f9e4ba9b..be919b701 100644 --- a/src/script/lua_api/l_item.h +++ b/src/script/lua_api/l_item.h @@ -135,6 +135,7 @@ public: class ModApiItemMod : public ModApiBase { private: static int l_register_item_raw(lua_State *L); + static int l_unregister_item_raw(lua_State *L); static int l_register_alias_raw(lua_State *L); static int l_get_content_id(lua_State *L); static int l_get_name_from_content_id(lua_State *L); -- cgit v1.2.3 From 2de8c22a9971153d594b2bb4736eb293753f1212 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Tue, 6 Sep 2016 19:13:52 +0200 Subject: Make getStackMax return the correct maximal stack size --- src/inventory.h | 5 ++--- src/itemdef.h | 2 +- src/script/common/c_content.cpp | 5 ++--- 3 files changed, 5 insertions(+), 7 deletions(-) (limited to 'src/script') diff --git a/src/inventory.h b/src/inventory.h index a690eb5ae..7d7e58d61 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -80,15 +80,14 @@ struct ItemStack // Maximum size of a stack u16 getStackMax(IItemDefManager *itemdef) const { - s16 max = itemdef->get(name).stack_max; - return (max >= 0) ? max : 0; + return itemdef->get(name).stack_max; } // Number of items that can be added to this stack u16 freeSpace(IItemDefManager *itemdef) const { u16 max = getStackMax(itemdef); - if(count > max) + if (count >= max) return 0; return max - count; } diff --git a/src/itemdef.h b/src/itemdef.h index b14ed41f7..dcb98e8a9 100644 --- a/src/itemdef.h +++ b/src/itemdef.h @@ -61,7 +61,7 @@ struct ItemDefinition /* Item stack and interaction properties */ - s16 stack_max; + u16 stack_max; bool usable; bool liquids_pointable; // May be NULL. If non-NULL, deleted by destructor diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index c664101ea..19873abc5 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -65,9 +65,8 @@ ItemDefinition read_item_definition(lua_State* L,int index, } lua_pop(L, 1); - def.stack_max = getintfield_default(L, index, "stack_max", def.stack_max); - if(def.stack_max == 0) - def.stack_max = 1; + int stack_max = getintfield_default(L, index, "stack_max", def.stack_max); + def.stack_max = rangelim(stack_max, 1, U16_MAX); lua_getfield(L, index, "on_use"); def.usable = lua_isfunction(L, -1); -- cgit v1.2.3 From 403dada85a1e75859573a26ed54a72caa693da91 Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Fri, 9 Sep 2016 11:47:13 -0400 Subject: Return nil on empty get_area() (#4508) --- doc/lua_api.txt | 1 + src/script/lua_api/l_areastore.cpp | 3 +++ 2 files changed, 4 insertions(+) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index da9ebb9f1..c4bc06695 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2886,6 +2886,7 @@ chosen for you. #### Methods * `get_area(id, include_borders, include_data)`: returns the area with the id `id`. (optional) Boolean values `include_borders` and `include_data` control what's copied. + Returns nil if specified area id does not exist. * `get_areas_for_pos(pos, include_borders, include_data)`: returns all areas that contain the position `pos`. (optional) Boolean values `include_borders` and `include_data` control what's copied. diff --git a/src/script/lua_api/l_areastore.cpp b/src/script/lua_api/l_areastore.cpp index 20e7875c7..0912e2ab0 100644 --- a/src/script/lua_api/l_areastore.cpp +++ b/src/script/lua_api/l_areastore.cpp @@ -111,6 +111,9 @@ int LuaAreaStore::l_get_area(lua_State *L) const Area *res; res = ast->getArea(id); + if (!res) + return 0; + push_area(L, res, include_borders, include_data); return 1; -- cgit v1.2.3 From b88595050f3af5ccac06aac331ead4ebdcb9deb9 Mon Sep 17 00:00:00 2001 From: paramat Date: Sun, 11 Sep 2016 23:34:43 +0100 Subject: Decorations: Generalise 'spawn by' to be used by all decoration types In lua_api.txt, make clear that 'place on' and 'spawn by' can be lists. --- doc/lua_api.txt | 16 +++--- src/mg_decoration.cpp | 119 ++++++++++++++++++++-------------------- src/mg_decoration.h | 9 ++- src/script/lua_api/l_mapgen.cpp | 21 ++++--- 4 files changed, 80 insertions(+), 85 deletions(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 74d4b90d5..aa4f129f0 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -3909,7 +3909,7 @@ The Biome API is still in an experimental phase and subject to change. { deco_type = "simple", -- See "Decoration types" place_on = "default:dirt_with_grass", - -- ^ Node that decoration can be placed on + -- ^ Node (or list of nodes) that the decoration can be placed on sidelen = 8, -- ^ Size of divisions made in the chunk being generated. -- ^ If the chunk size is not evenly divisible by sidelen, sidelen is made equal to the chunk size. @@ -3928,6 +3928,13 @@ The Biome API is still in an experimental phase and subject to change. -- ^ Minimum and maximum `y` positions these decorations can be generated at. -- ^ This parameter refers to the `y` position of the decoration base, so -- the actual maximum height would be `height_max + size.Y`. + spawn_by = "default:water", + -- ^ Node (or list of nodes) that the decoration only spawns next to. + -- ^ Checks two horizontal planes of neighbouring nodes (including diagonal neighbours), + -- ^ one plane at Y = surface and one plane at Y = surface = + 1. + num_spawn_by = 1, + -- ^ Number of spawn_by nodes that must be surrounding the decoration position to occur. + -- ^ If absent or -1, decorations occur next to any nodes. flags = "liquid_surface, force_placement", -- ^ Flags for all decoration types. -- ^ "liquid_surface": Instead of placement on the highest solid surface @@ -3945,13 +3952,6 @@ The Biome API is still in an experimental phase and subject to change. height_max = 0, -- ^ Number of nodes the decoration can be at maximum. -- ^ If absent, the parameter 'height' is used as a constant. - spawn_by = "default:water", - -- ^ Node that the decoration only spawns next to. - -- ^ The neighbours checked are the 8 nodes horizontally surrounding the lowest node of the - -- ^ decoration, and the 8 nodes horizontally surrounding the ground node below the decoration. - num_spawn_by = 1, - -- ^ Number of spawn_by nodes that must be surrounding the decoration position to occur. - -- ^ If absent or -1, decorations occur next to any nodes. ----- Schematic-type parameters schematic = "foobar.mts", diff --git a/src/mg_decoration.cpp b/src/mg_decoration.cpp index 127915b51..92483abc3 100644 --- a/src/mg_decoration.cpp +++ b/src/mg_decoration.cpp @@ -26,12 +26,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" FlagDesc flagdesc_deco[] = { - {"place_center_x", DECO_PLACE_CENTER_X}, - {"place_center_y", DECO_PLACE_CENTER_Y}, - {"place_center_z", DECO_PLACE_CENTER_Z}, + {"place_center_x", DECO_PLACE_CENTER_X}, + {"place_center_y", DECO_PLACE_CENTER_Y}, + {"place_center_z", DECO_PLACE_CENTER_Z}, {"force_placement", DECO_FORCE_PLACEMENT}, - {"liquid_surface", DECO_LIQUID_SURFACE}, - {NULL, 0} + {"liquid_surface", DECO_LIQUID_SURFACE}, + {NULL, 0} }; @@ -82,6 +82,56 @@ Decoration::~Decoration() void Decoration::resolveNodeNames() { getIdsFromNrBacklog(&c_place_on); + getIdsFromNrBacklog(&c_spawnby); +} + + +bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p) +{ + // Check if the decoration can be placed on this node + u32 vi = vm->m_area.index(p); + if (!CONTAINS(c_place_on, vm->m_data[vi].getContent())) + return false; + + // Don't continue if there are no spawnby constraints + if (nspawnby == -1) + return true; + + int nneighs = 0; + static const v3s16 dirs[16] = { + v3s16( 0, 0, 1), + v3s16( 0, 0, -1), + v3s16( 1, 0, 0), + v3s16(-1, 0, 0), + v3s16( 1, 0, 1), + v3s16(-1, 0, 1), + v3s16(-1, 0, -1), + v3s16( 1, 0, -1), + + v3s16( 0, 1, 1), + v3s16( 0, 1, -1), + v3s16( 1, 1, 0), + v3s16(-1, 1, 0), + v3s16( 1, 1, 1), + v3s16(-1, 1, 1), + v3s16(-1, 1, -1), + v3s16( 1, 1, -1) + }; + + // Check these 16 neighbouring nodes for enough spawnby nodes + for (size_t i = 0; i != ARRLEN(dirs); i++) { + u32 index = vm->m_area.index(p + dirs[i]); + if (!vm->m_area.contains(index)) + continue; + + if (CONTAINS(c_spawnby, vm->m_data[index].getContent())) + nneighs++; + } + + if (nneighs < nspawnby) + return false; + + return true; } @@ -236,66 +286,15 @@ void DecoSimple::resolveNodeNames() { Decoration::resolveNodeNames(); getIdsFromNrBacklog(&c_decos); - getIdsFromNrBacklog(&c_spawnby); } -bool DecoSimple::canPlaceDecoration(MMVManip *vm, v3s16 p) +size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p) { // Don't bother if there aren't any decorations to place if (c_decos.size() == 0) - return false; - - u32 vi = vm->m_area.index(p); - - // Check if the decoration can be placed on this node - if (!CONTAINS(c_place_on, vm->m_data[vi].getContent())) - return false; - - // Don't continue if there are no spawnby constraints - if (nspawnby == -1) - return true; - - int nneighs = 0; - v3s16 dirs[16] = { - v3s16( 0, 0, 1), - v3s16( 0, 0, -1), - v3s16( 1, 0, 0), - v3s16(-1, 0, 0), - v3s16( 1, 0, 1), - v3s16(-1, 0, 1), - v3s16(-1, 0, -1), - v3s16( 1, 0, -1), - - v3s16( 0, 1, 1), - v3s16( 0, 1, -1), - v3s16( 1, 1, 0), - v3s16(-1, 1, 0), - v3s16( 1, 1, 1), - v3s16(-1, 1, 1), - v3s16(-1, 1, -1), - v3s16( 1, 1, -1) - }; - - // Check a Moore neighborhood if there are enough spawnby nodes - for (size_t i = 0; i != ARRLEN(dirs); i++) { - u32 index = vm->m_area.index(p + dirs[i]); - if (!vm->m_area.contains(index)) - continue; - - if (CONTAINS(c_spawnby, vm->m_data[index].getContent())) - nneighs++; - } - - if (nneighs < nspawnby) - return false; - - return true; -} - + return 0; -size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p) -{ if (!canPlaceDecoration(vm, p)) return 0; @@ -345,9 +344,7 @@ size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p) if (schematic == NULL) return 0; - u32 vi = vm->m_area.index(p); - content_t c = vm->m_data[vi].getContent(); - if (!CONTAINS(c_place_on, c)) + if (!canPlaceDecoration(vm, p)) return 0; if (flags & DECO_PLACE_CENTER_X) diff --git a/src/mg_decoration.h b/src/mg_decoration.h index da98fd482..be0ba44d7 100644 --- a/src/mg_decoration.h +++ b/src/mg_decoration.h @@ -68,6 +68,7 @@ public: virtual void resolveNodeNames(); + bool canPlaceDecoration(MMVManip *vm, v3s16 p); size_t placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax); //size_t placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax); @@ -82,6 +83,8 @@ public: s16 y_max; float fill_ratio; NoiseParams np; + std::vector c_spawnby; + s16 nspawnby; UNORDERED_SET biomes; //std::list cutoffs; @@ -90,17 +93,13 @@ public: class DecoSimple : public Decoration { public: + virtual void resolveNodeNames(); virtual size_t generate(MMVManip *vm, PcgRandom *pr, v3s16 p); - bool canPlaceDecoration(MMVManip *vm, v3s16 p); virtual int getHeight(); - virtual void resolveNodeNames(); - std::vector c_decos; - std::vector c_spawnby; s16 deco_height; s16 deco_height_max; - s16 nspawnby; }; class DecoSchematic : public Decoration { diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 9f28231eb..a176f4f52 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -902,6 +902,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L) deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02); deco->y_min = getintfield_default(L, index, "y_min", -31000); deco->y_max = getintfield_default(L, index, "y_max", 31000); + deco->nspawnby = getintfield_default(L, index, "num_spawn_by", -1); deco->sidelen = getintfield_default(L, index, "sidelen", 8); if (deco->sidelen <= 0) { errorstream << "register_decoration: sidelen must be " @@ -929,6 +930,14 @@ int ModApiMapgen::l_register_decoration(lua_State *L) errorstream << "register_decoration: couldn't get all biomes " << std::endl; lua_pop(L, 1); + //// Get node name(s) to 'spawn by' + size_t nnames = getstringlistfield(L, index, "spawn_by", &deco->m_nodenames); + deco->m_nnlistsizes.push_back(nnames); + if (nnames == 0 && deco->nspawnby != -1) { + errorstream << "register_decoration: no spawn_by nodes defined," + " but num_spawn_by specified" << std::endl; + } + //// Handle decoration type-specific parameters bool success = false; switch (decotype) { @@ -962,12 +971,10 @@ int ModApiMapgen::l_register_decoration(lua_State *L) bool read_deco_simple(lua_State *L, DecoSimple *deco) { - size_t nnames; int index = 1; deco->deco_height = getintfield_default(L, index, "height", 1); deco->deco_height_max = getintfield_default(L, index, "height_max", 0); - deco->nspawnby = getintfield_default(L, index, "num_spawn_by", -1); if (deco->deco_height <= 0) { errorstream << "register_decoration: simple decoration height" @@ -975,7 +982,7 @@ bool read_deco_simple(lua_State *L, DecoSimple *deco) return false; } - nnames = getstringlistfield(L, index, "decoration", &deco->m_nodenames); + size_t nnames = getstringlistfield(L, index, "decoration", &deco->m_nodenames); deco->m_nnlistsizes.push_back(nnames); if (nnames == 0) { errorstream << "register_decoration: no decoration nodes " @@ -983,14 +990,6 @@ bool read_deco_simple(lua_State *L, DecoSimple *deco) return false; } - nnames = getstringlistfield(L, index, "spawn_by", &deco->m_nodenames); - deco->m_nnlistsizes.push_back(nnames); - if (nnames == 0 && deco->nspawnby != -1) { - errorstream << "register_decoration: no spawn_by nodes defined," - " but num_spawn_by specified" << std::endl; - return false; - } - return true; } -- cgit v1.2.3 From 5f084cd98d7b3326b51320455364337539710efd Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 5 Oct 2016 00:13:10 +0200 Subject: Make some maps unordered to improve performance * This permit to improve performance on C++11 builds * use some existing typedefs in tools maps * minor code style changes --- src/script/common/c_content.cpp | 18 ++++++++---------- src/settings.h | 4 ++-- src/sound_openal.cpp | 26 +++++++++++--------------- src/tool.cpp | 23 +++++++++++------------ src/tool.h | 15 ++++++--------- 5 files changed, 38 insertions(+), 48 deletions(-) (limited to 'src/script') diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 19873abc5..6fb9080bc 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -829,20 +829,18 @@ void push_tool_capabilities(lua_State *L, // Create groupcaps table lua_newtable(L); // For each groupcap - for(std::map::const_iterator - i = toolcap.groupcaps.begin(); i != toolcap.groupcaps.end(); i++){ + for (ToolGCMap::const_iterator i = toolcap.groupcaps.begin(); + i != toolcap.groupcaps.end(); i++) { // Create groupcap table lua_newtable(L); const std::string &name = i->first; const ToolGroupCap &groupcap = i->second; // Create subtable "times" lua_newtable(L); - for(std::map::const_iterator - i = groupcap.times.begin(); i != groupcap.times.end(); i++){ - int rating = i->first; - float time = i->second; - lua_pushinteger(L, rating); - lua_pushnumber(L, time); + for (UNORDERED_MAP::const_iterator + i = groupcap.times.begin(); i != groupcap.times.end(); i++) { + lua_pushinteger(L, i->first); + lua_pushnumber(L, i->second); lua_settable(L, -3); } // Set subtable "times" @@ -858,8 +856,8 @@ void push_tool_capabilities(lua_State *L, //Create damage_groups table lua_newtable(L); // For each damage group - for(std::map::const_iterator - i = toolcap.damageGroups.begin(); i != toolcap.damageGroups.end(); i++){ + for (DamageGroup::const_iterator i = toolcap.damageGroups.begin(); + i != toolcap.damageGroups.end(); i++) { // Create damage group table lua_pushinteger(L, i->second); lua_setfield(L, -2, i->first.c_str()); diff --git a/src/settings.h b/src/settings.h index 0af861a58..c6c044779 100644 --- a/src/settings.h +++ b/src/settings.h @@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/string.h" #include "threading/mutex.h" #include -#include +#include "util/cpp11_container.h" #include #include @@ -45,7 +45,7 @@ typedef std::vector< > > SettingsCallbackList; -typedef std::map SettingsCallbackMap; +typedef UNORDERED_MAP SettingsCallbackMap; enum ValueType { VALUETYPE_STRING, diff --git a/src/sound_openal.cpp b/src/sound_openal.cpp index 1832a0c77..317667f52 100644 --- a/src/sound_openal.cpp +++ b/src/sound_openal.cpp @@ -41,9 +41,9 @@ with this program; ifnot, write to the Free Software Foundation, Inc., #include "log.h" #include "util/numeric.h" // myrand() #include "porting.h" -#include #include #include +#include "util/cpp11_container.h" #define BUFFER_SIZE 30000 @@ -271,8 +271,8 @@ private: ALCdevice *m_device; ALCcontext *m_context; int m_next_id; - std::map > m_buffers; - std::map m_sounds_playing; + UNORDERED_MAP > m_buffers; + UNORDERED_MAP m_sounds_playing; v3f m_listener_pos; public: bool m_is_initialized; @@ -337,7 +337,7 @@ public: alcCloseDevice(m_device); m_device = NULL; - for (std::map >::iterator i = m_buffers.begin(); + for (UNORDERED_MAP >::iterator i = m_buffers.begin(); i != m_buffers.end(); ++i) { for (std::vector::iterator iter = (*i).second.begin(); iter != (*i).second.end(); ++iter) { @@ -351,7 +351,7 @@ public: void addBuffer(const std::string &name, SoundBuffer *buf) { - std::map >::iterator i = + UNORDERED_MAP >::iterator i = m_buffers.find(name); if(i != m_buffers.end()){ i->second.push_back(buf); @@ -365,7 +365,7 @@ public: SoundBuffer* getBuffer(const std::string &name) { - std::map >::iterator i = + UNORDERED_MAP >::iterator i = m_buffers.find(name); if(i == m_buffers.end()) return NULL; @@ -443,8 +443,7 @@ public: void deleteSound(int id) { - std::map::iterator i = - m_sounds_playing.find(id); + UNORDERED_MAP::iterator i = m_sounds_playing.find(id); if(i == m_sounds_playing.end()) return; PlayingSound *sound = i->second; @@ -484,10 +483,8 @@ public: < del_list; - for(std::map::iterator - i = m_sounds_playing.begin(); - i != m_sounds_playing.end(); ++i) - { + for(UNORDERED_MAP::iterator i = m_sounds_playing.begin(); + i != m_sounds_playing.end(); ++i) { int id = i->first; PlayingSound *sound = i->second; // If not playing, remove it @@ -583,9 +580,8 @@ public: } void updateSoundPosition(int id, v3f pos) { - std::map::iterator i = - m_sounds_playing.find(id); - if(i == m_sounds_playing.end()) + UNORDERED_MAP::iterator i = m_sounds_playing.find(id); + if (i == m_sounds_playing.end()) return; PlayingSound *sound = i->second; diff --git a/src/tool.cpp b/src/tool.cpp index 54b9f15f4..20b71fb31 100644 --- a/src/tool.cpp +++ b/src/tool.cpp @@ -34,24 +34,23 @@ void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const writeF1000(os, full_punch_interval); writeS16(os, max_drop_level); writeU32(os, groupcaps.size()); - for(std::map::const_iterator - i = groupcaps.begin(); i != groupcaps.end(); ++i){ + for (ToolGCMap::const_iterator i = groupcaps.begin(); i != groupcaps.end(); ++i) { const std::string *name = &i->first; const ToolGroupCap *cap = &i->second; os<uses); writeS16(os, cap->maxlevel); writeU32(os, cap->times.size()); - for(std::map::const_iterator - i = cap->times.begin(); i != cap->times.end(); ++i){ + for (UNORDERED_MAP::const_iterator + i = cap->times.begin(); i != cap->times.end(); ++i) { writeS16(os, i->first); writeF1000(os, i->second); } } if(protocol_version > 17){ writeU32(os, damageGroups.size()); - for(std::map::const_iterator - i = damageGroups.begin(); i != damageGroups.end(); ++i){ + for (DamageGroup::const_iterator i = damageGroups.begin(); + i != damageGroups.end(); ++i) { os<first); writeS16(os, i->second); } @@ -106,7 +105,7 @@ DigParams getDigParams(const ItemGroupList &groups, default: break; } - + // Values to be returned (with a bit of conversion) bool result_diggable = false; float result_time = 0.0; @@ -115,8 +114,8 @@ DigParams getDigParams(const ItemGroupList &groups, int level = itemgroup_get(groups, "level"); //infostream<<"level="<::const_iterator - i = tp->groupcaps.begin(); i != tp->groupcaps.end(); ++i){ + for (ToolGCMap::const_iterator i = tp->groupcaps.begin(); + i != tp->groupcaps.end(); ++i) { const std::string &name = i->first; //infostream<<"group="<second; @@ -163,8 +162,8 @@ HitParams getHitParams(const ItemGroupList &armor_groups, s16 damage = 0; float full_punch_interval = tp->full_punch_interval; - for(std::map::const_iterator - i = tp->damageGroups.begin(); i != tp->damageGroups.end(); ++i){ + for (DamageGroup::const_iterator i = tp->damageGroups.begin(); + i != tp->damageGroups.end(); ++i) { s16 armor = itemgroup_get(armor_groups, i->first); damage += i->second * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0) * armor / 100.0; @@ -197,7 +196,7 @@ PunchDamageResult getPunchDamage( do_hit = false; } } - + PunchDamageResult result; if(do_hit) { diff --git a/src/tool.h b/src/tool.h index 509561a16..ebba5b749 100644 --- a/src/tool.h +++ b/src/tool.h @@ -23,12 +23,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes.h" #include #include -#include +#include "util/cpp11_container.h" #include "itemgroup.h" struct ToolGroupCap { - std::map times; + UNORDERED_MAP times; int maxlevel; int uses; @@ -39,8 +39,8 @@ struct ToolGroupCap bool getTime(int rating, float *time) const { - std::map::const_iterator i = times.find(rating); - if(i == times.end()){ + UNORDERED_MAP::const_iterator i = times.find(rating); + if (i == times.end()) { *time = 0; return false; } @@ -50,22 +50,19 @@ struct ToolGroupCap }; -// CLANG SUCKS DONKEY BALLS -typedef std::map ToolGCMap; -typedef std::map DamageGroup; +typedef UNORDERED_MAP ToolGCMap; +typedef UNORDERED_MAP DamageGroup; struct ToolCapabilities { float full_punch_interval; int max_drop_level; - // CLANG SUCKS DONKEY BALLS ToolGCMap groupcaps; DamageGroup damageGroups; ToolCapabilities( float full_punch_interval_=1.4, int max_drop_level_=1, - // CLANG SUCKS DONKEY BALLS ToolGCMap groupcaps_=ToolGCMap(), DamageGroup damageGroups_=DamageGroup() ): -- cgit v1.2.3 From 613797a3048907275ceebe29582b9fc2761b1f25 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 5 Oct 2016 09:03:55 +0200 Subject: Replace various std::map with UNORDERED_MAP + various cleanups This is part 2 for 5f084cd98d7b3326b51320455364337539710efd Other improvements: * Use the defined ItemGroupList when used * make Client::checkPrivilege const * inline some trivial functions * Add ActiveObjectMap typedef * Add SettingsEntries typedef --- src/client.cpp | 4 +-- src/client.h | 6 ++--- src/clientiface.cpp | 55 ++++++++++++++----------------------- src/clientiface.h | 7 +++-- src/content_cao.cpp | 6 ++--- src/content_cao.h | 2 +- src/emerge.cpp | 6 ++--- src/emerge.h | 2 +- src/environment.cpp | 60 ++++++++++++++--------------------------- src/environment.h | 8 +++--- src/game.cpp | 9 +++---- src/itemdef.cpp | 4 +-- src/itemgroup.h | 6 ++--- src/mapsector.cpp | 28 +++++-------------- src/mapsector.h | 4 +-- src/mg_schematic.cpp | 4 +-- src/script/common/c_content.cpp | 8 +++--- src/script/common/c_content.h | 6 ++--- src/script/common/c_converter.h | 4 +-- src/script/lua_api/l_util.cpp | 4 +-- src/server.cpp | 12 ++++----- src/settings.cpp | 26 +++++++++--------- src/settings.h | 6 +++-- 23 files changed, 110 insertions(+), 167 deletions(-) (limited to 'src/script') diff --git a/src/client.cpp b/src/client.cpp index 483b22caa..a599e21dc 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -303,7 +303,7 @@ Client::~Client() delete m_inventory_from_server; // Delete detached inventories - for (std::map::iterator + for (UNORDERED_MAP::iterator i = m_detached_inventories.begin(); i != m_detached_inventories.end(); ++i) { delete i->second; @@ -1437,7 +1437,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc) break; case InventoryLocation::DETACHED: { - if(m_detached_inventories.count(loc.name) == 0) + if (m_detached_inventories.count(loc.name) == 0) return NULL; return m_detached_inventories[loc.name]; } diff --git a/src/client.h b/src/client.h index b479062a0..fb479068f 100644 --- a/src/client.h +++ b/src/client.h @@ -462,7 +462,7 @@ public: u16 getHP(); u16 getBreath(); - bool checkPrivilege(const std::string &priv) + bool checkPrivilege(const std::string &priv) const { return (m_privileges.count(priv) != 0); } bool getChatMessage(std::wstring &message); @@ -670,11 +670,11 @@ private: std::map m_sounds_to_objects; // Privileges - std::set m_privileges; + UNORDERED_SET m_privileges; // Detached inventories // key = name - std::map m_detached_inventories; + UNORDERED_MAP m_detached_inventories; // Storage for mesh data for creating multiple instances of the same mesh StringMap m_mesh_data; diff --git a/src/clientiface.cpp b/src/clientiface.cpp index a3a17d435..e7ad39579 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -605,11 +605,8 @@ ClientInterface::~ClientInterface() { MutexAutoLock clientslock(m_clients_mutex); - for(std::map::iterator - i = m_clients.begin(); - i != m_clients.end(); ++i) - { - + for (UNORDERED_MAP::iterator i = m_clients.begin(); + i != m_clients.end(); ++i) { // Delete client delete i->second; } @@ -621,10 +618,8 @@ std::vector ClientInterface::getClientIDs(ClientState min_state) std::vector reply; MutexAutoLock clientslock(m_clients_mutex); - for(std::map::iterator - i = m_clients.begin(); - i != m_clients.end(); ++i) - { + for(UNORDERED_MAP::iterator i = m_clients.begin(); + i != m_clients.end(); ++i) { if (i->second->getState() >= min_state) reply.push_back(i->second->peer_id); } @@ -691,8 +686,7 @@ void ClientInterface::sendToAll(u16 channelnum, NetworkPacket* pkt, bool reliable) { MutexAutoLock clientslock(m_clients_mutex); - for(std::map::iterator - i = m_clients.begin(); + for(UNORDERED_MAP::iterator i = m_clients.begin(); i != m_clients.end(); ++i) { RemoteClient *client = i->second; @@ -705,11 +699,10 @@ void ClientInterface::sendToAll(u16 channelnum, RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min) { MutexAutoLock clientslock(m_clients_mutex); - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. - if(n == m_clients.end()) + if (n == m_clients.end()) return NULL; if (n->second->getState() >= state_min) @@ -720,11 +713,10 @@ RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min) RemoteClient* ClientInterface::lockedGetClientNoEx(u16 peer_id, ClientState state_min) { - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. - if(n == m_clients.end()) + if (n == m_clients.end()) return NULL; if (n->second->getState() >= state_min) @@ -736,11 +728,10 @@ RemoteClient* ClientInterface::lockedGetClientNoEx(u16 peer_id, ClientState stat ClientState ClientInterface::getClientState(u16 peer_id) { MutexAutoLock clientslock(m_clients_mutex); - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. - if(n == m_clients.end()) + if (n == m_clients.end()) return CS_Invalid; return n->second->getState(); @@ -749,11 +740,10 @@ ClientState ClientInterface::getClientState(u16 peer_id) void ClientInterface::setPlayerName(u16 peer_id,std::string name) { MutexAutoLock clientslock(m_clients_mutex); - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. - if(n != m_clients.end()) + if (n != m_clients.end()) n->second->setName(name); } @@ -762,11 +752,10 @@ void ClientInterface::DeleteClient(u16 peer_id) MutexAutoLock conlock(m_clients_mutex); // Error check - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. - if(n == m_clients.end()) + if (n == m_clients.end()) return; /* @@ -797,10 +786,9 @@ void ClientInterface::CreateClient(u16 peer_id) MutexAutoLock conlock(m_clients_mutex); // Error check - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client shouldn't already exist - if(n != m_clients.end()) return; + if (n != m_clients.end()) return; // Create client RemoteClient *client = new RemoteClient(); @@ -814,8 +802,7 @@ void ClientInterface::event(u16 peer_id, ClientStateEvent event) MutexAutoLock clientlock(m_clients_mutex); // Error check - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // No client to deliver event if (n == m_clients.end()) @@ -836,8 +823,7 @@ u16 ClientInterface::getProtocolVersion(u16 peer_id) MutexAutoLock conlock(m_clients_mutex); // Error check - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // No client to get version if (n == m_clients.end()) @@ -851,8 +837,7 @@ void ClientInterface::setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch MutexAutoLock conlock(m_clients_mutex); // Error check - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // No client to set versions if (n == m_clients.end()) diff --git a/src/clientiface.h b/src/clientiface.h index c09942909..8985ef71f 100644 --- a/src/clientiface.h +++ b/src/clientiface.h @@ -25,10 +25,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serialization.h" // for SER_FMT_VER_INVALID #include "threading/mutex.h" #include "network/networkpacket.h" +#include "util/cpp11_container.h" #include #include -#include #include class MapBlock; @@ -502,8 +502,7 @@ protected: void lock() { m_clients_mutex.lock(); } void unlock() { m_clients_mutex.unlock(); } - std::map& getClientList() - { return m_clients; } + UNORDERED_MAP& getClientList() { return m_clients; } private: /* update internal player list */ @@ -513,7 +512,7 @@ private: con::Connection* m_con; Mutex m_clients_mutex; // Connected clients (behind the con mutex) - std::map m_clients; + UNORDERED_MAP m_clients; std::vector m_clients_names; //for announcing masterserver // Environment diff --git a/src/content_cao.cpp b/src/content_cao.cpp index f414b2b9b..609422f26 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -1505,10 +1505,8 @@ void GenericCAO::updateBonePosition() return; m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render - for(std::map >::const_iterator ii = m_bone_position.begin(); - ii != m_bone_position.end(); ++ii) - { + for(UNORDERED_MAP >::const_iterator + ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) { std::string bone_name = (*ii).first; v3f bone_pos = (*ii).second.X; v3f bone_rot = (*ii).second.Y; diff --git a/src/content_cao.h b/src/content_cao.h index bf99fd3ba..cf14a1e18 100644 --- a/src/content_cao.h +++ b/src/content_cao.h @@ -90,7 +90,7 @@ private: int m_animation_speed; int m_animation_blend; bool m_animation_loop; - std::map > m_bone_position; // stores position and rotation for each bone name + UNORDERED_MAP > m_bone_position; // stores position and rotation for each bone name std::string m_attachment_bone; v3f m_attachment_position; v3f m_attachment_rotation; diff --git a/src/emerge.cpp b/src/emerge.cpp index daf42f5e2..bdb5e0729 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -369,12 +369,10 @@ bool EmergeManager::pushBlockEmergeData( } -bool EmergeManager::popBlockEmergeData( - v3s16 pos, - BlockEmergeData *bedata) +bool EmergeManager::popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata) { std::map::iterator it; - std::map::iterator it2; + UNORDERED_MAP::iterator it2; it = m_blocks_enqueued.find(pos); if (it == m_blocks_enqueued.end()) diff --git a/src/emerge.h b/src/emerge.h index cf0677145..71ad97da3 100644 --- a/src/emerge.h +++ b/src/emerge.h @@ -156,7 +156,7 @@ private: Mutex m_queue_mutex; std::map m_blocks_enqueued; - std::map m_peer_queue_count; + UNORDERED_MAP m_peer_queue_count; u16 m_qlimit_total; u16 m_qlimit_diskonly; diff --git a/src/environment.cpp b/src/environment.cpp index eea264699..34b3c34f4 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -1124,14 +1124,12 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n) void ServerEnvironment::getObjectsInsideRadius(std::vector &objects, v3f pos, float radius) { - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for (ActiveObjectMap::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; u16 id = i->first; v3f objectpos = obj->getBasePosition(); - if(objectpos.getDistanceFrom(pos) > radius) + if (objectpos.getDistanceFrom(pos) > radius) continue; objects.push_back(id); } @@ -1142,8 +1140,7 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode) infostream << "ServerEnvironment::clearObjects(): " << "Removing all active objects" << std::endl; std::vector objects_to_remove; - for (std::map::iterator - i = m_active_objects.begin(); + for (ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; if (obj->getType() == ACTIVEOBJECT_TYPE_PLAYER) @@ -1518,10 +1515,8 @@ void ServerEnvironment::step(float dtime) send_recommended = true; } - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for(ActiveObjectMap::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; // Don't step if is to be removed or stored statically if(obj->m_removed || obj->m_pending_deactivation) @@ -1554,7 +1549,7 @@ void ServerEnvironment::step(float dtime) Manage particle spawner expiration */ if (m_particle_management_interval.step(dtime, 1.0)) { - for (std::map::iterator i = m_particle_spawners.begin(); + for (UNORDERED_MAP::iterator i = m_particle_spawners.begin(); i != m_particle_spawners.end(); ) { //non expiring spawners if (i->second == PARTICLE_SPAWNER_NO_EXPIRY) { @@ -1579,8 +1574,7 @@ u32 ServerEnvironment::addParticleSpawner(float exptime) u32 id = 0; for (;;) { // look for unused particlespawner id id++; - std::map::iterator f; - f = m_particle_spawners.find(id); + UNORDERED_MAP::iterator f = m_particle_spawners.find(id); if (f == m_particle_spawners.end()) { m_particle_spawners[id] = time; break; @@ -1589,31 +1583,21 @@ u32 ServerEnvironment::addParticleSpawner(float exptime) return id; } -void ServerEnvironment::deleteParticleSpawner(u32 id) -{ - m_particle_spawners.erase(id); -} - ServerActiveObject* ServerEnvironment::getActiveObject(u16 id) { - std::map::iterator n; - n = m_active_objects.find(id); - if(n == m_active_objects.end()) - return NULL; - return n->second; + ActiveObjectMap::iterator n = m_active_objects.find(id); + return (n != m_active_objects.end() ? n->second : NULL); } -bool isFreeServerActiveObjectId(u16 id, - std::map &objects) +bool isFreeServerActiveObjectId(u16 id, ActiveObjectMap &objects) { - if(id == 0) + if (id == 0) return false; return objects.find(id) == objects.end(); } -u16 getFreeServerActiveObjectId( - std::map &objects) +u16 getFreeServerActiveObjectId(ActiveObjectMap &objects) { //try to reuse id's as late as possible static u16 last_used_id = 0; @@ -1659,8 +1643,7 @@ void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius, - discard objects that are found in current_objects. - add remaining objects to added_objects */ - for(std::map::iterator - i = m_active_objects.begin(); + for(ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { u16 id = i->first; @@ -1756,8 +1739,7 @@ void ServerEnvironment::setStaticForActiveObjectsInBlock( so_it = block->m_static_objects.m_active.begin(); so_it != block->m_static_objects.m_active.end(); ++so_it) { // Get the ServerActiveObject counterpart to this StaticObject - std::map::iterator ao_it; - ao_it = m_active_objects.find(so_it->first); + ActiveObjectMap::iterator ao_it = m_active_objects.find(so_it->first); if (ao_it == m_active_objects.end()) { // If this ever happens, there must be some kind of nasty bug. errorstream << "ServerEnvironment::setStaticForObjectsInBlock(): " @@ -1806,8 +1788,8 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, verbosestream<<"ServerEnvironment::addActiveObjectRaw(): " <<"supplied with id "<getId()<getId(), m_active_objects) == false) - { + + if(!isFreeServerActiveObjectId(object->getId(), m_active_objects)) { errorstream<<"ServerEnvironment::addActiveObjectRaw(): " <<"id is not free ("<getId()<<")"<environmentDeletes()) @@ -1875,8 +1857,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, void ServerEnvironment::removeRemovedObjects() { std::vector objects_to_remove; - for(std::map::iterator - i = m_active_objects.begin(); + for(ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { u16 id = i->first; ServerActiveObject* obj = i->second; @@ -1894,7 +1875,7 @@ void ServerEnvironment::removeRemovedObjects() We will delete objects that are marked as removed or thatare waiting for deletion after deactivation */ - if(obj->m_removed == false && obj->m_pending_deactivation == false) + if (!obj->m_removed && !obj->m_pending_deactivation) continue; /* @@ -2094,8 +2075,7 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) void ServerEnvironment::deactivateFarObjects(bool force_delete) { std::vector objects_to_remove; - for(std::map::iterator - i = m_active_objects.begin(); + for(ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; assert(obj); diff --git a/src/environment.h b/src/environment.h index c6786faed..1ba7b196f 100644 --- a/src/environment.h +++ b/src/environment.h @@ -300,6 +300,8 @@ enum ClearObjectsMode { This is not thread-safe. Server uses an environment mutex. */ +typedef UNORDERED_MAP ActiveObjectMap; + class ServerEnvironment : public Environment { public: @@ -338,7 +340,7 @@ public: void loadDefaultMeta(); u32 addParticleSpawner(float exptime); - void deleteParticleSpawner(u32 id); + void deleteParticleSpawner(u32 id) { m_particle_spawners.erase(id); } /* External ActiveObject interface @@ -491,7 +493,7 @@ private: // World path const std::string m_path_world; // Active object list - std::map m_active_objects; + ActiveObjectMap m_active_objects; // Outgoing network message buffer for active objects std::queue m_active_object_messages; // Some timers @@ -522,7 +524,7 @@ private: // Particles IntervalLimiter m_particle_management_interval; - std::map m_particle_spawners; + UNORDERED_MAP m_particle_spawners; }; #ifndef SERVER diff --git a/src/game.cpp b/src/game.cpp index 5a3b10879..22d9ffef6 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -605,7 +605,7 @@ public: void draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver, gui::IGUIFont *font) const { - std::map m_meta; + UNORDERED_MAP m_meta; for (std::deque::const_iterator k = m_log.begin(); k != m_log.end(); ++k) { @@ -615,8 +615,7 @@ public: i != piece.values.end(); ++i) { const std::string &id = i->first; const float &value = i->second; - std::map::iterator j = - m_meta.find(id); + UNORDERED_MAP::iterator j = m_meta.find(id); if (j == m_meta.end()) { m_meta[id] = Meta(value); @@ -643,7 +642,7 @@ public: sizeof(usable_colors) / sizeof(*usable_colors); u32 next_color_i = 0; - for (std::map::iterator i = m_meta.begin(); + for (UNORDERED_MAP::iterator i = m_meta.begin(); i != m_meta.end(); ++i) { Meta &meta = i->second; video::SColor color(255, 200, 200, 200); @@ -659,7 +658,7 @@ public: s32 textx2 = textx + 200 - 15; s32 meta_i = 0; - for (std::map::const_iterator i = m_meta.begin(); + for (UNORDERED_MAP::const_iterator i = m_meta.begin(); i != m_meta.end(); ++i) { const std::string &id = i->first; const Meta &meta = i->second; diff --git a/src/itemdef.cpp b/src/itemdef.cpp index a6c627a03..1aa6331dc 100644 --- a/src/itemdef.cpp +++ b/src/itemdef.cpp @@ -146,9 +146,9 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const } os<::const_iterator + for (ItemGroupList::const_iterator i = groups.begin(); i != groups.end(); ++i){ - os<first); + os << serializeString(i->first); writeS16(os, i->second); } os< -#include +#include "util/cpp11_container.h" -typedef std::map ItemGroupList; +typedef UNORDERED_MAP ItemGroupList; static inline int itemgroup_get(const ItemGroupList &groups, const std::string &name) { - std::map::const_iterator i = groups.find(name); + ItemGroupList::const_iterator i = groups.find(name); if(i == groups.end()) return 0; return i->second; diff --git a/src/mapsector.cpp b/src/mapsector.cpp index 1588a5962..410689f5e 100644 --- a/src/mapsector.cpp +++ b/src/mapsector.cpp @@ -42,9 +42,8 @@ void MapSector::deleteBlocks() m_block_cache = NULL; // Delete all - for(std::map::iterator i = m_blocks.begin(); - i != m_blocks.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_blocks.begin(); + i != m_blocks.end(); ++i) { delete i->second; } @@ -56,20 +55,13 @@ MapBlock * MapSector::getBlockBuffered(s16 y) { MapBlock *block; - if(m_block_cache != NULL && y == m_block_cache_y){ + if (m_block_cache != NULL && y == m_block_cache_y) { return m_block_cache; } // If block doesn't exist, return NULL - std::map::iterator n = m_blocks.find(y); - if(n == m_blocks.end()) - { - block = NULL; - } - // If block exists, return it - else{ - block = n->second; - } + UNORDERED_MAP::iterator n = m_blocks.find(y); + block = (n != m_blocks.end() ? n->second : NULL); // Cache the last result m_block_cache_y = y; @@ -135,18 +127,12 @@ void MapSector::deleteBlock(MapBlock *block) void MapSector::getBlocks(MapBlockVect &dest) { - for(std::map::iterator bi = m_blocks.begin(); - bi != m_blocks.end(); ++bi) - { + for (UNORDERED_MAP::iterator bi = m_blocks.begin(); + bi != m_blocks.end(); ++bi) { dest.push_back(bi->second); } } -bool MapSector::empty() -{ - return m_blocks.empty(); -} - /* ServerMapSector */ diff --git a/src/mapsector.h b/src/mapsector.h index 4c1ce86a3..c3bff3575 100644 --- a/src/mapsector.h +++ b/src/mapsector.h @@ -63,7 +63,7 @@ public: void getBlocks(MapBlockVect &dest); - bool empty(); + bool empty() const { return m_blocks.empty(); } // Always false at the moment, because sector contains no metadata. bool differs_from_disk; @@ -71,7 +71,7 @@ public: protected: // The pile of MapBlocks - std::map m_blocks; + UNORDERED_MAP m_blocks; Map *m_parent; // Position on parent (in MapBlock widths) diff --git a/src/mg_schematic.cpp b/src/mg_schematic.cpp index 0b95fa267..e028215dc 100644 --- a/src/mg_schematic.cpp +++ b/src/mg_schematic.cpp @@ -564,14 +564,14 @@ void Schematic::applyProbabilities(v3s16 p0, void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount, std::vector *usednodes, INodeDefManager *ndef) { - std::map nodeidmap; + UNORDERED_MAP nodeidmap; content_t numids = 0; for (size_t i = 0; i != nodecount; i++) { content_t id; content_t c = nodes[i].getContent(); - std::map::const_iterator it = nodeidmap.find(c); + UNORDERED_MAP::const_iterator it = nodeidmap.find(c); if (it == nodeidmap.end()) { id = numids; numids++; diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 6fb9080bc..f20a65903 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1063,8 +1063,7 @@ void push_flags_string(lua_State *L, FlagDesc *flagdesc, u32 flags, u32 flagmask /******************************************************************************/ /******************************************************************************/ -void read_groups(lua_State *L, int index, - std::map &result) +void read_groups(lua_State *L, int index, ItemGroupList &result) { if (!lua_istable(L,index)) return; @@ -1083,11 +1082,10 @@ void read_groups(lua_State *L, int index, } /******************************************************************************/ -void push_groups(lua_State *L, const std::map &groups) +void push_groups(lua_State *L, const ItemGroupList &groups) { lua_newtable(L); - std::map::const_iterator it; - for (it = groups.begin(); it != groups.end(); ++it) { + for (ItemGroupList::const_iterator it = groups.begin(); it != groups.end(); ++it) { lua_pushnumber(L, it->second); lua_setfield(L, -2, it->first.c_str()); } diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 46416ad8e..2a2228b6d 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -33,11 +33,11 @@ extern "C" { } #include -#include #include #include "irrlichttypes_bloated.h" #include "util/string.h" +#include "itemgroup.h" namespace Json { class Value; } @@ -106,10 +106,10 @@ void pushnode (lua_State *L, const MapNode &n, NodeBox read_nodebox (lua_State *L, int index); void read_groups (lua_State *L, int index, - std::map &result); + ItemGroupList &result); void push_groups (lua_State *L, - const std::map &groups); + const ItemGroupList &groups); //TODO rename to "read_enum_field" int getenumfield (lua_State *L, int table, diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h index eefac0ed7..a5fbee765 100644 --- a/src/script/common/c_converter.h +++ b/src/script/common/c_converter.h @@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define C_CONVERTER_H_ #include -#include +#include "util/cpp11_container.h" #include "irrlichttypes_bloated.h" #include "common/c_types.h" @@ -60,7 +60,7 @@ bool getintfield(lua_State *L, int table, bool getintfield(lua_State *L, int table, const char *fieldname, u32 &result); void read_groups(lua_State *L, int index, - std::map &result); + UNORDERED_MAP &result); bool getboolfield(lua_State *L, int table, const char *fieldname, bool &result); bool getfloatfield(lua_State *L, int table, diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 13c0d702f..fa2d15b03 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -220,7 +220,7 @@ int ModApiUtil::l_write_json(lua_State *L) int ModApiUtil::l_get_dig_params(lua_State *L) { NO_MAP_LOCK_REQUIRED; - std::map groups; + ItemGroupList groups; read_groups(L, 1, groups); ToolCapabilities tp = read_tool_capabilities(L, 2); if(lua_isnoneornil(L, 3)) @@ -235,7 +235,7 @@ int ModApiUtil::l_get_dig_params(lua_State *L) int ModApiUtil::l_get_hit_params(lua_State *L) { NO_MAP_LOCK_REQUIRED; - std::map groups; + UNORDERED_MAP groups; read_groups(L, 1, groups); ToolCapabilities tp = read_tool_capabilities(L, 2); if(lua_isnoneornil(L, 3)) diff --git a/src/server.cpp b/src/server.cpp index c615aee13..5b67b321a 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -669,7 +669,7 @@ void Server::AsyncRunStep(bool initial_step) MutexAutoLock envlock(m_env_mutex); m_clients.lock(); - std::map clients = m_clients.getClientList(); + UNORDERED_MAP clients = m_clients.getClientList(); ScopeProfiler sp(g_profiler, "Server: checking added and deleted objs"); // Radius inside which objects are active @@ -685,8 +685,7 @@ void Server::AsyncRunStep(bool initial_step) if (player_radius == 0 && is_transfer_limited) player_radius = radius; - for (std::map::iterator - i = clients.begin(); + for (UNORDERED_MAP::iterator i = clients.begin(); i != clients.end(); ++i) { RemoteClient *client = i->second; @@ -696,7 +695,7 @@ void Server::AsyncRunStep(bool initial_step) continue; Player *player = m_env->getPlayer(client->peer_id); - if(player == NULL) { + if (player == NULL) { // This can happen if the client timeouts somehow /*warningstream<peer_id @@ -817,10 +816,9 @@ void Server::AsyncRunStep(bool initial_step) } m_clients.lock(); - std::map clients = m_clients.getClientList(); + UNORDERED_MAP clients = m_clients.getClientList(); // Route data to every client - for (std::map::iterator - i = clients.begin(); + for (UNORDERED_MAP::iterator i = clients.begin(); i != clients.end(); ++i) { RemoteClient *client = i->second; std::string reliable_data; diff --git a/src/settings.cpp b/src/settings.cpp index 91ea9c58b..c4c3c9073 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -196,9 +196,8 @@ void Settings::writeLines(std::ostream &os, u32 tab_depth) const { MutexAutoLock lock(m_mutex); - for (std::map::const_iterator - it = m_settings.begin(); - it != m_settings.end(); ++it) + for (SettingEntries::const_iterator it = m_settings.begin(); + it != m_settings.end(); ++it) printEntry(os, it->first, it->second, tab_depth); } @@ -231,7 +230,7 @@ void Settings::printEntry(std::ostream &os, const std::string &name, bool Settings::updateConfigObject(std::istream &is, std::ostream &os, const std::string &end, u32 tab_depth) { - std::map::const_iterator it; + SettingEntries::const_iterator it; std::set present_entries; std::string line, name, value; bool was_modified = false; @@ -381,7 +380,7 @@ const SettingsEntry &Settings::getEntry(const std::string &name) const { MutexAutoLock lock(m_mutex); - std::map::const_iterator n; + SettingEntries::const_iterator n; if ((n = m_settings.find(name)) == m_settings.end()) { if ((n = m_defaults.find(name)) == m_defaults.end()) throw SettingNotFoundException("Setting [" + name + "] not found."); @@ -572,9 +571,8 @@ bool Settings::exists(const std::string &name) const std::vector Settings::getNames() const { std::vector names; - for (std::map::const_iterator - i = m_settings.begin(); - i != m_settings.end(); ++i) { + for (SettingEntries::const_iterator i = m_settings.begin(); + i != m_settings.end(); ++i) { names.push_back(i->first); } return names; @@ -880,7 +878,7 @@ bool Settings::remove(const std::string &name) { MutexAutoLock lock(m_mutex); - std::map::iterator it = m_settings.find(name); + SettingEntries::iterator it = m_settings.find(name); if (it != m_settings.end()) { delete it->second.group; m_settings.erase(it); @@ -912,7 +910,6 @@ void Settings::updateValue(const Settings &other, const std::string &name) try { std::string val = other.get(name); - m_settings[name] = val; } catch (SettingNotFoundException &e) { } @@ -968,8 +965,9 @@ void Settings::updateNoLock(const Settings &other) void Settings::clearNoLock() { - std::map::const_iterator it; - for (it = m_settings.begin(); it != m_settings.end(); ++it) + + for (SettingEntries::const_iterator it = m_settings.begin(); + it != m_settings.end(); ++it) delete it->second.group; m_settings.clear(); @@ -978,8 +976,8 @@ void Settings::clearNoLock() void Settings::clearDefaultsNoLock() { - std::map::const_iterator it; - for (it = m_defaults.begin(); it != m_defaults.end(); ++it) + for (SettingEntries::const_iterator it = m_defaults.begin(); + it != m_defaults.end(); ++it) delete it->second.group; m_defaults.clear(); } diff --git a/src/settings.h b/src/settings.h index c6c044779..b19733514 100644 --- a/src/settings.h +++ b/src/settings.h @@ -98,6 +98,8 @@ struct SettingsEntry { bool is_group; }; +typedef UNORDERED_MAP SettingEntries; + class Settings { public: Settings() {} @@ -231,8 +233,8 @@ private: void doCallbacks(const std::string &name) const; - std::map m_settings; - std::map m_defaults; + SettingEntries m_settings; + SettingEntries m_defaults; SettingsCallbackMap m_callbacks; -- cgit v1.2.3 From 667975fe3adee935a3f4d2b1a421a295771c664d Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 6 Oct 2016 08:48:20 +0200 Subject: Use more unordered_maps to improve performance in c++11 builds --- src/client.cpp | 9 +++------ src/client.h | 6 +++--- src/clientobject.cpp | 10 ++++------ src/clientobject.h | 3 ++- src/content_cao.cpp | 2 +- src/network/clientpackethandler.cpp | 4 +--- src/script/lua_api/l_mapgen.cpp | 4 ++-- src/server.cpp | 8 ++++---- src/util/string.h | 3 ++- 9 files changed, 22 insertions(+), 27 deletions(-) (limited to 'src/script') diff --git a/src/client.cpp b/src/client.cpp index a599e21dc..63653998a 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -623,10 +623,8 @@ void Client::step(float dtime) Update positions of sounds attached to objects */ { - for(std::map::iterator - i = m_sounds_to_objects.begin(); - i != m_sounds_to_objects.end(); ++i) - { + for(UNORDERED_MAP::iterator i = m_sounds_to_objects.begin(); + i != m_sounds_to_objects.end(); ++i) { int client_id = i->first; u16 object_id = i->second; ClientActiveObject *cao = m_env.getActiveObject(object_id); @@ -645,8 +643,7 @@ void Client::step(float dtime) m_removed_sounds_check_timer = 0; // Find removed sounds and clear references to them std::vector removed_server_ids; - for(std::map::iterator - i = m_sounds_server_to_client.begin(); + for(UNORDERED_MAP::iterator i = m_sounds_server_to_client.begin(); i != m_sounds_server_to_client.end();) { s32 server_id = i->first; int client_id = i->second; diff --git a/src/client.h b/src/client.h index fb479068f..a71c1dcbc 100644 --- a/src/client.h +++ b/src/client.h @@ -663,11 +663,11 @@ private: // Sounds float m_removed_sounds_check_timer; // Mapping from server sound ids to our sound ids - std::map m_sounds_server_to_client; + UNORDERED_MAP m_sounds_server_to_client; // And the other way! - std::map m_sounds_client_to_server; + UNORDERED_MAP m_sounds_client_to_server; // And relations to objects - std::map m_sounds_to_objects; + UNORDERED_MAP m_sounds_to_objects; // Privileges UNORDERED_SET m_privileges; diff --git a/src/clientobject.cpp b/src/clientobject.cpp index a11757ea6..ff3f47187 100644 --- a/src/clientobject.cpp +++ b/src/clientobject.cpp @@ -43,12 +43,11 @@ ClientActiveObject* ClientActiveObject::create(ActiveObjectType type, IGameDef *gamedef, ClientEnvironment *env) { // Find factory function - std::map::iterator n; - n = m_types.find(type); + UNORDERED_MAP::iterator n = m_types.find(type); if(n == m_types.end()) { // If factory is not found, just return. - warningstream<<"ClientActiveObject: No factory for type=" - <<(int)type< +#include "util/cpp11_container.h" /* @@ -103,7 +104,7 @@ protected: ClientEnvironment *m_env; private: // Used for creating objects based on type - static std::map m_types; + static UNORDERED_MAP m_types; }; struct DistanceSortedActiveObject diff --git a/src/content_cao.cpp b/src/content_cao.cpp index a141690f6..33dae6822 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -51,7 +51,7 @@ struct ToolCapabilities; #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" -std::map ClientActiveObject::m_types; +UNORDERED_MAP ClientActiveObject::m_types; SmoothTranslator::SmoothTranslator(): vect_old(0,0,0), diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 48c573da5..6d42edd7d 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -812,9 +812,7 @@ void Client::handleCommand_StopSound(NetworkPacket* pkt) *pkt >> server_id; - std::map::iterator i = - m_sounds_server_to_client.find(server_id); - + UNORDERED_MAP::iterator i = m_sounds_server_to_client.find(server_id); if (i != m_sounds_server_to_client.end()) { int client_id = i->second; m_sound->stopSound(client_id); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index a176f4f52..da8e71cdc 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -244,7 +244,7 @@ bool read_schematic_def(lua_State *L, int index, schem->schemdata = new MapNode[numnodes]; size_t names_base = names->size(); - std::map name_id_map; + UNORDERED_MAP name_id_map; u32 i = 0; for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) { @@ -266,7 +266,7 @@ bool read_schematic_def(lua_State *L, int index, u8 param2 = getintfield_default(L, -1, "param2", 0); //// Find or add new nodename-to-ID mapping - std::map::iterator it = name_id_map.find(name); + UNORDERED_MAP::iterator it = name_id_map.find(name); content_t name_index; if (it != name_id_map.end()) { name_index = it->second; diff --git a/src/server.cpp b/src/server.cpp index a8494f76e..e9983ba11 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -794,7 +794,7 @@ void Server::AsyncRunStep(bool initial_step) // Key = object id // Value = data sent by object - std::map* > buffered_messages; + UNORDERED_MAP* > buffered_messages; // Get active object messages from environment for(;;) { @@ -803,7 +803,7 @@ void Server::AsyncRunStep(bool initial_step) break; std::vector* message_list = NULL; - std::map* >::iterator n; + UNORDERED_MAP* >::iterator n; n = buffered_messages.find(aom.id); if (n == buffered_messages.end()) { message_list = new std::vector; @@ -824,7 +824,7 @@ void Server::AsyncRunStep(bool initial_step) std::string reliable_data; std::string unreliable_data; // Go through all objects in message buffer - for (std::map* >::iterator + for (UNORDERED_MAP* >::iterator j = buffered_messages.begin(); j != buffered_messages.end(); ++j) { // If object is not known by client, skip it @@ -868,7 +868,7 @@ void Server::AsyncRunStep(bool initial_step) m_clients.unlock(); // Clear buffered_messages - for(std::map* >::iterator + for(UNORDERED_MAP* >::iterator i = buffered_messages.begin(); i != buffered_messages.end(); ++i) { delete i->second; diff --git a/src/util/string.h b/src/util/string.h index 724543a36..572c37150 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define UTIL_STRING_HEADER #include "irrlichttypes_bloated.h" +#include "cpp11_container.h" #include #include #include @@ -54,7 +55,7 @@ with this program; if not, write to the Free Software Foundation, Inc., (((unsigned char)(x) < 0xe0) ? 2 : \ (((unsigned char)(x) < 0xf0) ? 3 : 4)) -typedef std::map StringMap; +typedef UNORDERED_MAP StringMap; struct FlagDesc { const char *name; -- cgit v1.2.3 From 155288ee981c70f505526347cb2bcda4df1c8e6b Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 6 Oct 2016 19:20:12 +0200 Subject: use unordered containers where possible (patch 4 on X) Also remove some unused parameters/functions --- src/content_sao.cpp | 28 ++++++++++++++++++---------- src/content_sao.h | 5 +++-- src/guiFormSpecMenu.h | 4 +--- src/map.h | 1 + src/mapblock_mesh.cpp | 24 ++++++++++-------------- src/mapblock_mesh.h | 15 ++++++++------- src/network/serverpackethandler.cpp | 4 +--- src/script/cpp_api/s_async.cpp | 3 ++- src/script/cpp_api/s_async.h | 2 +- src/server.cpp | 21 +++++++++------------ src/server.h | 16 ++++++---------- src/util/numeric.cpp | 2 +- src/util/numeric.h | 37 ++----------------------------------- 13 files changed, 63 insertions(+), 99 deletions(-) (limited to 'src/script') diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 1e7e788e9..895c26044 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -347,8 +347,10 @@ void LuaEntitySAO::step(float dtime, bool send_recommended) if(m_bone_position_sent == false){ m_bone_position_sent = true; - for(std::map >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ - std::string str = gob_cmd_update_bone_position((*ii).first, (*ii).second.X, (*ii).second.Y); + for (UNORDERED_MAP >::const_iterator + ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ + std::string str = gob_cmd_update_bone_position((*ii).first, + (*ii).second.X, (*ii).second.Y); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); @@ -383,8 +385,10 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version) os< >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ - os< >::const_iterator + ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) { + os << serializeLongString(gob_cmd_update_bone_position((*ii).first, + (*ii).second.X, (*ii).second.Y)); // m_bone_position.size } os< >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ + for (UNORDERED_MAP >::const_iterator + ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) { os< >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ - std::string str = gob_cmd_update_bone_position((*ii).first, (*ii).second.X, (*ii).second.Y); + for (UNORDERED_MAP >::const_iterator + ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) { + std::string str = gob_cmd_update_bone_position((*ii).first, + (*ii).second.X, (*ii).second.Y); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); } } - if(m_attachment_sent == false){ + if (!m_attachment_sent){ m_attachment_sent = true; - std::string str = gob_cmd_update_attachment(m_attachment_parent_id, m_attachment_bone, m_attachment_position, m_attachment_rotation); + std::string str = gob_cmd_update_attachment(m_attachment_parent_id, + m_attachment_bone, m_attachment_position, m_attachment_rotation); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); diff --git a/src/content_sao.h b/src/content_sao.h index 44d40d332..ccae90b77 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -112,7 +112,7 @@ private: bool m_animation_loop; bool m_animation_sent; - std::map > m_bone_position; + UNORDERED_MAP > m_bone_position; bool m_bone_position_sent; int m_attachment_parent_id; @@ -321,7 +321,8 @@ private: bool m_animation_loop; bool m_animation_sent; - std::map > m_bone_position; // Stores position and rotation for each bone name + // Stores position and rotation for each bone name + UNORDERED_MAP > m_bone_position; bool m_bone_position_sent; int m_attachment_parent_id; diff --git a/src/guiFormSpecMenu.h b/src/guiFormSpecMenu.h index 21054b725..153720975 100644 --- a/src/guiFormSpecMenu.h +++ b/src/guiFormSpecMenu.h @@ -409,8 +409,6 @@ protected: std::vector > > m_dropdowns; ItemSpec *m_selected_item; - f32 m_timer1; - f32 m_timer2; u32 m_selected_amount; bool m_selected_dragging; @@ -462,7 +460,7 @@ private: GUITable::TableOptions table_options; GUITable::TableColumns table_columns; // used to restore table selection/scroll/treeview state - std::map table_dyndata; + UNORDERED_MAP table_dyndata; } parserData; typedef struct { diff --git a/src/map.h b/src/map.h index 13775fde1..c73fa92bf 100644 --- a/src/map.h +++ b/src/map.h @@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "voxel.h" #include "modifiedstate.h" #include "util/container.h" +#include "util/cpp11_container.h" #include "nodetimer.h" #include "map_settings_manager.h" diff --git a/src/mapblock_mesh.cpp b/src/mapblock_mesh.cpp index a11fb5887..00f83e7ab 100644 --- a/src/mapblock_mesh.cpp +++ b/src/mapblock_mesh.cpp @@ -1033,7 +1033,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): m_enable_shaders = data->m_use_shaders; m_use_tangent_vertices = data->m_use_tangent_vertices; m_enable_vbo = g_settings->getBool("enable_vbo"); - + if (g_settings->getBool("enable_minimap")) { m_minimap_mapblock = new MinimapMapblock; m_minimap_mapblock->getMinimapNodes( @@ -1298,10 +1298,8 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat // Cracks if(crack != m_last_crack) { - for(std::map::iterator - i = m_crack_materials.begin(); - i != m_crack_materials.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_crack_materials.begin(); + i != m_crack_materials.end(); ++i) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); std::string basename = i->second; @@ -1315,9 +1313,9 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat // If the current material is also animated, // update animation info - std::map::iterator anim_iter = - m_animation_tiles.find(i->first); - if(anim_iter != m_animation_tiles.end()){ + UNORDERED_MAP::iterator anim_iter = + m_animation_tiles.find(i->first); + if (anim_iter != m_animation_tiles.end()){ TileSpec &tile = anim_iter->second; tile.texture = new_texture; tile.texture_id = new_texture_id; @@ -1330,10 +1328,8 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat } // Texture animation - for(std::map::iterator - i = m_animation_tiles.begin(); - i != m_animation_tiles.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_animation_tiles.begin(); + i != m_animation_tiles.end(); ++i) { const TileSpec &tile = i->second; // Figure out current frame int frameoffset = m_animation_frame_offsets[i->first]; @@ -1443,7 +1439,7 @@ void MeshCollector::append(const TileSpec &tile, vertices[i].Color, vertices[i].TCoords); p->vertices.push_back(vert); } - } + } for (u32 i = 0; i < numIndices; i++) { u32 j = indices[i] + vertex_count; @@ -1499,7 +1495,7 @@ void MeshCollector::append(const TileSpec &tile, vertices[i].Normal, c, vertices[i].TCoords); p->vertices.push_back(vert); } - } + } for (u32 i = 0; i < numIndices; i++) { u32 j = indices[i] + vertex_count; diff --git a/src/mapblock_mesh.h b/src/mapblock_mesh.h index f89fbe669..8376468da 100644 --- a/src/mapblock_mesh.h +++ b/src/mapblock_mesh.h @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes_extrabloated.h" #include "client/tile.h" #include "voxel.h" +#include "util/cpp11_container.h" #include class IGameDef; @@ -121,7 +122,7 @@ public: if(m_animation_force_timer > 0) m_animation_force_timer--; } - + void updateCameraOffset(v3s16 camera_offset); private: @@ -144,20 +145,20 @@ private: // Last crack value passed to animate() int m_last_crack; // Maps mesh buffer (i.e. material) indices to base texture names - std::map m_crack_materials; + UNORDERED_MAP m_crack_materials; // Animation info: texture animationi // Maps meshbuffers to TileSpecs - std::map m_animation_tiles; - std::map m_animation_frames; // last animation frame - std::map m_animation_frame_offsets; - + UNORDERED_MAP m_animation_tiles; + UNORDERED_MAP m_animation_frames; // last animation frame + UNORDERED_MAP m_animation_frame_offsets; + // Animation info: day/night transitions // Last daynight_ratio value passed to animate() u32 m_last_daynight_ratio; // For each meshbuffer, maps vertex indices to (day,night) pairs std::map > > m_daynight_diffs; - + // Camera offset info -> do we have to translate the mesh? v3s16 m_camera_offset; }; diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index a8bfd9068..f9061cc4f 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -1693,9 +1693,7 @@ void Server::handleCommand_RemovedSounds(NetworkPacket* pkt) *pkt >> id; - std::map::iterator i = - m_playing_sounds.find(id); - + UNORDERED_MAP::iterator i = m_playing_sounds.find(id); if (i == m_playing_sounds.end()) continue; diff --git a/src/script/cpp_api/s_async.cpp b/src/script/cpp_api/s_async.cpp index 9bf3fcf49..1fb84fab6 100644 --- a/src/script/cpp_api/s_async.cpp +++ b/src/script/cpp_api/s_async.cpp @@ -81,6 +81,7 @@ bool AsyncEngine::registerFunction(const char* name, lua_CFunction func) if (initDone) { return false; } + functionList[name] = func; return true; } @@ -203,7 +204,7 @@ void AsyncEngine::pushFinishedJobs(lua_State* L) { /******************************************************************************/ void AsyncEngine::prepareEnvironment(lua_State* L, int top) { - for (std::map::iterator it = functionList.begin(); + for (UNORDERED_MAP::iterator it = functionList.begin(); it != functionList.end(); it++) { lua_pushstring(L, it->first.c_str()); lua_pushcfunction(L, it->second); diff --git a/src/script/cpp_api/s_async.h b/src/script/cpp_api/s_async.h index 8d612d58c..016381e5f 100644 --- a/src/script/cpp_api/s_async.h +++ b/src/script/cpp_api/s_async.h @@ -132,7 +132,7 @@ private: bool initDone; // Internal store for registred functions - std::map functionList; + UNORDERED_MAP functionList; // Internal counter to create job IDs unsigned int jobIdCounter; diff --git a/src/server.cpp b/src/server.cpp index 639e6462a..2dd070b1a 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -868,7 +868,7 @@ void Server::AsyncRunStep(bool initial_step) m_clients.unlock(); // Clear buffered_messages - for(UNORDERED_MAP* >::iterator + for (UNORDERED_MAP* >::iterator i = buffered_messages.begin(); i != buffered_messages.end(); ++i) { delete i->second; @@ -2016,16 +2016,15 @@ s32 Server::playSound(const SimpleSoundSpec &spec, void Server::stopSound(s32 handle) { // Get sound reference - std::map::iterator i = - m_playing_sounds.find(handle); - if(i == m_playing_sounds.end()) + UNORDERED_MAP::iterator i = m_playing_sounds.find(handle); + if (i == m_playing_sounds.end()) return; ServerPlayingSound &psound = i->second; NetworkPacket pkt(TOCLIENT_STOP_SOUND, 4); pkt << handle; - for(std::set::iterator i = psound.clients.begin(); + for (UNORDERED_SET::iterator i = psound.clients.begin(); i != psound.clients.end(); ++i) { // Send as reliable m_clients.send(*i, 0, &pkt, true); @@ -2322,7 +2321,7 @@ void Server::sendMediaAnnouncement(u16 peer_id) NetworkPacket pkt(TOCLIENT_ANNOUNCE_MEDIA, 0, peer_id); pkt << (u16) m_media.size(); - for (std::map::iterator i = m_media.begin(); + for (UNORDERED_MAP::iterator i = m_media.begin(); i != m_media.end(); ++i) { pkt << i->first << i->second.sha1_digest; } @@ -2367,7 +2366,7 @@ void Server::sendRequestedMedia(u16 peer_id, i != tosend.end(); ++i) { const std::string &name = *i; - if(m_media.find(name) == m_media.end()) { + if (m_media.find(name) == m_media.end()) { errorstream<<"Server::sendRequestedMedia(): Client asked for " <<"unknown file \""<<(name)<<"\""<::iterator - i = m_playing_sounds.begin(); - i != m_playing_sounds.end();) - { + for (UNORDERED_MAP::iterator + i = m_playing_sounds.begin(); i != m_playing_sounds.end();) { ServerPlayingSound &psound = i->second; psound.clients.erase(peer_id); - if(psound.clients.empty()) + if (psound.clients.empty()) m_playing_sounds.erase(i++); else ++i; diff --git a/src/server.h b/src/server.h index 3ad894b38..34427a71a 100644 --- a/src/server.h +++ b/src/server.h @@ -157,7 +157,7 @@ struct ServerSoundParams struct ServerPlayingSound { ServerSoundParams params; - std::set clients; // peer ids + UNORDERED_SET clients; // peer ids }; class Server : public con::PeerHandler, public MapEventReceiver, @@ -243,11 +243,9 @@ public: std::wstring getStatusString(); // read shutdown state - inline bool getShutdownRequested() - { return m_shutdown_requested; } + inline bool getShutdownRequested() const { return m_shutdown_requested; } // request server to shutdown - inline void requestShutdown() { m_shutdown_requested = true; } void requestShutdown(const std::string &msg, bool reconnect) { m_shutdown_requested = true; @@ -323,8 +321,7 @@ public: const ModSpec* getModSpec(const std::string &modname) const; void getModNames(std::vector &modlist); std::string getBuiltinLuaPath(); - inline std::string getWorldPath() const - { return m_path_world; } + inline std::string getWorldPath() const { return m_path_world; } inline bool isSingleplayer() { return m_simple_singleplayer_mode; } @@ -356,8 +353,7 @@ public: bool setSky(Player *player, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms); - bool overrideDayNightRatio(Player *player, bool do_override, - float brightness); + bool overrideDayNightRatio(Player *player, bool do_override, float brightness); /* con::PeerHandler implementation. */ void peerAdded(con::Peer *peer); @@ -654,12 +650,12 @@ private: u16 m_ignore_map_edit_events_peer_id; // media files known to server - std::map m_media; + UNORDERED_MAP m_media; /* Sounds */ - std::map m_playing_sounds; + UNORDERED_MAP m_playing_sounds; s32 m_next_sound_id; /* diff --git a/src/util/numeric.cpp b/src/util/numeric.cpp index 42ebd9022..6a7bfcac9 100644 --- a/src/util/numeric.cpp +++ b/src/util/numeric.cpp @@ -27,7 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include -std::map > FacePositionCache::m_cache; +UNORDERED_MAP > FacePositionCache::m_cache; Mutex FacePositionCache::m_cache_mutex; // Calculate the borders of a "d-radius" cube // TODO: Make it work without mutex and data races, probably thread-local diff --git a/src/util/numeric.h b/src/util/numeric.h index 615327864..4cdc254c3 100644 --- a/src/util/numeric.h +++ b/src/util/numeric.h @@ -26,8 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "../irr_v3d.h" #include "../irr_aabb3d.h" #include "../threading/mutex.h" +#include "cpp11_container.h" #include -#include #include @@ -41,26 +41,10 @@ public: static std::vector getFacePositions(u16 d); private: static void generateFacePosition(u16 d); - static std::map > m_cache; + static UNORDERED_MAP > m_cache; static Mutex m_cache_mutex; }; -class IndentationRaiser -{ -public: - IndentationRaiser(u16 *indentation) - { - m_indentation = indentation; - (*m_indentation)++; - } - ~IndentationRaiser() - { - (*m_indentation)--; - } -private: - u16 *m_indentation; -}; - inline s16 getContainerPos(s16 p, s16 d) { return (p>=0 ? p : p-d+1) / d; @@ -149,23 +133,6 @@ inline bool isInArea(v3s16 p, v3s16 d) #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d)>(max)?(max):(d))) #define myfloor(x) ((x) > 0.0 ? (int)(x) : (int)(x) - 1) -inline v3s16 arealim(v3s16 p, s16 d) -{ - if(p.X < 0) - p.X = 0; - if(p.Y < 0) - p.Y = 0; - if(p.Z < 0) - p.Z = 0; - if(p.X > d-1) - p.X = d-1; - if(p.Y > d-1) - p.Y = d-1; - if(p.Z > d-1) - p.Z = d-1; - return p; -} - // The naive swap performs better than the xor version #define SWAP(t, x, y) do { \ t temp = x; \ -- cgit v1.2.3 From 8bcd10b872bc88c6f474913d6efb8d53c50c5ae1 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 8 Oct 2016 10:38:04 +0200 Subject: Player/LocalPlayer/RemotePlayer inheritance cleanup (part 1 on X) * LocalPlayer take ownership of maxHudId as it's the only caller * RemotePlayer take ownership of day night ratio as it's the only user * Pass getPlayerControl as const reference to prevent object copy on each call (perf improvement in ObjectRef::l_get_player_control call) * getPlayerSAO is now only RemotePlayer call * get/setHotbarItemCount is now RemotePlayer owned * Server: Use RemotePlayer instead of Player object on concerned call to properly fix the object type * PlayerSAO now uses RemotePlayer instead of Player because it's only server side * ObjectRef::getplayer also returns RemotePlayer as it's linked with PlayerSAO --- src/content_sao.cpp | 7 ++--- src/content_sao.h | 14 +++------ src/game.cpp | 2 +- src/localplayer.h | 2 ++ src/network/clientpackethandler.cpp | 6 ++-- src/network/serverpackethandler.cpp | 31 ++++++++++++++----- src/player.h | 62 ++++++++++++++----------------------- src/script/lua_api/l_env.cpp | 4 +-- src/script/lua_api/l_object.cpp | 16 +++++----- src/script/lua_api/l_object.h | 3 +- src/server.cpp | 52 ++++++++++++++----------------- src/server.h | 13 ++++---- 12 files changed, 103 insertions(+), 109 deletions(-) (limited to 'src/script') diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 895c26044..2317cbdfe 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -751,7 +751,7 @@ bool LuaEntitySAO::collideWithObjects(){ // No prototype, PlayerSAO does not need to be deserialized -PlayerSAO::PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_, +PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id_, const std::set &privs, bool is_singleplayer): ServerActiveObject(env_, v3f(0,0,0)), m_player(player_), @@ -833,11 +833,10 @@ void PlayerSAO::addedToEnvironment(u32 dtime_s) void PlayerSAO::removingFromEnvironment() { ServerActiveObject::removingFromEnvironment(); - if(m_player->getPlayerSAO() == this) - { + if (m_player->getPlayerSAO() == this) { m_player->setPlayerSAO(NULL); m_player->peer_id = 0; - m_env->savePlayer((RemotePlayer*)m_player); + m_env->savePlayer(m_player); m_env->removePlayer(m_player); } } diff --git a/src/content_sao.h b/src/content_sao.h index ccae90b77..c97db4922 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -160,7 +160,7 @@ public: class PlayerSAO : public ServerActiveObject { public: - PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_, + PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id_, const std::set &privs, bool is_singleplayer); ~PlayerSAO(); ActiveObjectType getType() const @@ -231,14 +231,8 @@ public: void disconnected(); - Player* getPlayer() - { - return m_player; - } - u16 getPeerID() const - { - return m_peer_id; - } + RemotePlayer* getPlayer() { return m_player; } + u16 getPeerID() const { return m_peer_id; } // Cheat prevention @@ -291,7 +285,7 @@ public: private: std::string getPropertyPacket(); - Player *m_player; + RemotePlayer *m_player; u16 m_peer_id; Inventory *m_inventory; s16 m_damage; diff --git a/src/game.cpp b/src/game.cpp index 22d9ffef6..5bb08e27a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2851,7 +2851,7 @@ void Game::processItemSelection(u16 *new_playeritem) s32 wheel = input->getMouseWheel(); u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE - 1, - player->hud_hotbar_itemcount - 1); + player->hud_hotbar_itemcount - 1); s32 dir = wheel; diff --git a/src/localplayer.h b/src/localplayer.h index 3ae0c4e51..8897adc5e 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -80,6 +80,8 @@ public: m_cao = toset; } + u32 maxHudId() const { return hud.size(); } + private: void accelerateHorizontal(const v3f &target_speed, const f32 max_increase); void accelerateVertical(const v3f &target_speed, const f32 max_increase); diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 6d42edd7d..35e350f20 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -1122,7 +1122,7 @@ void Client::handleCommand_HudSetParam(NetworkPacket* pkt) *pkt >> param >> value; - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); if (param == HUD_PARAM_HOTBAR_ITEMCOUNT && value.size() == 4) { @@ -1131,10 +1131,10 @@ void Client::handleCommand_HudSetParam(NetworkPacket* pkt) player->hud_hotbar_itemcount = hotbar_itemcount; } else if (param == HUD_PARAM_HOTBAR_IMAGE) { - ((LocalPlayer *) player)->hotbar_image = value; + player->hotbar_image = value; } else if (param == HUD_PARAM_HOTBAR_SELECTED_IMAGE) { - ((LocalPlayer *) player)->hotbar_selected_image = value; + player->hotbar_selected_image = value; } } diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index f9061cc4f..3fba7f720 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -800,7 +800,8 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt) pitch = modulo360f(pitch); yaw = modulo360f(yaw); - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = + dynamic_cast(m_env->getPlayer(pkt->getPeerId())); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -879,7 +880,9 @@ void Server::handleCommand_DeletedBlocks(NetworkPacket* pkt) void Server::handleCommand_InventoryAction(NetworkPacket* pkt) { - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = + dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -1078,7 +1081,9 @@ void Server::handleCommand_Damage(NetworkPacket* pkt) *pkt >> damage; - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = + dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -1112,7 +1117,9 @@ void Server::handleCommand_Breath(NetworkPacket* pkt) *pkt >> breath; - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = + dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -1224,7 +1231,9 @@ void Server::handleCommand_PlayerItem(NetworkPacket* pkt) if (pkt->getSize() < 2) return; - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = + dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -1299,7 +1308,9 @@ void Server::handleCommand_Interact(NetworkPacket* pkt) verbosestream << "TOSERVER_INTERACT: action=" << (int)action << ", item=" << item_i << ", pointed=" << pointed.dump() << std::endl; - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = + dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -1719,7 +1730,9 @@ void Server::handleCommand_NodeMetaFields(NetworkPacket* pkt) fields[fieldname] = pkt->readLongString(); } - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = + dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -1769,7 +1782,9 @@ void Server::handleCommand_InventoryFields(NetworkPacket* pkt) fields[fieldname] = pkt->readLongString(); } - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = + dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() diff --git a/src/player.h b/src/player.h index f38effa9d..fbd88fc71 100644 --- a/src/player.h +++ b/src/player.h @@ -233,16 +233,6 @@ public: return size; } - void setHotbarItemcount(s32 hotbar_itemcount) - { - hud_hotbar_itemcount = hotbar_itemcount; - } - - s32 getHotbarItemcount() - { - return hud_hotbar_itemcount; - } - void setHotbarImage(const std::string &name) { hud_hotbar_image = name; @@ -278,18 +268,6 @@ public: *params = m_sky_params; } - void overrideDayNightRatio(bool do_override, float ratio) - { - m_day_night_ratio_do_override = do_override; - m_day_night_ratio = ratio; - } - - void getDayNightRatio(bool *do_override, float *ratio) - { - *do_override = m_day_night_ratio_do_override; - *ratio = m_day_night_ratio; - } - void setLocalAnimations(v2s32 frames[4], float frame_speed) { for (int i = 0; i < 4; i++) @@ -309,11 +287,6 @@ public: return false; } - virtual PlayerSAO *getPlayerSAO() - { - return NULL; - } - virtual void setPlayerSAO(PlayerSAO *sao) { FATAL_ERROR("FIXME"); @@ -335,7 +308,7 @@ public: void setModified(const bool x) { m_dirty = x; - if (x == false) + if (!x) inventory.setModified(x); } @@ -391,10 +364,7 @@ public: std::string inventory_formspec; PlayerControl control; - PlayerControl getPlayerControl() - { - return control; - } + const PlayerControl& getPlayerControl() { return control; } u32 keyPressed; @@ -403,9 +373,6 @@ public: u32 addHud(HudElement* hud); HudElement* removeHud(u32 id); void clearHud(); - u32 maxHudId() { - return hud.size(); - } u32 hud_flags; s32 hud_hotbar_itemcount; @@ -429,9 +396,6 @@ protected: std::string m_sky_type; video::SColor m_sky_bgcolor; std::vector m_sky_params; - - bool m_day_night_ratio_do_override; - float m_day_night_ratio; private: // Protect some critical areas // hud for example can be modified by EmergeThread @@ -463,6 +427,25 @@ public: const RemotePlayerChatResult canSendChatMessage(); + void setHotbarItemcount(s32 hotbar_itemcount) + { + hud_hotbar_itemcount = hotbar_itemcount; + } + + s32 getHotbarItemcount() const { return hud_hotbar_itemcount; } + + void overrideDayNightRatio(bool do_override, float ratio) + { + m_day_night_ratio_do_override = do_override; + m_day_night_ratio = ratio; + } + + void getDayNightRatio(bool *do_override, float *ratio) + { + *do_override = m_day_night_ratio_do_override; + *ratio = m_day_night_ratio; + } + private: PlayerSAO *m_sao; @@ -473,6 +456,9 @@ private: u32 m_last_chat_message_sent; float m_chat_message_allowance; u16 m_message_rate_overhead; + + bool m_day_night_ratio_do_override; + float m_day_night_ratio; }; #endif diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index ebdea09e4..f6ea23e95 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -494,8 +494,8 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L) // Do it const char *name = luaL_checkstring(L, 1); - Player *player = env->getPlayer(name); - if(player == NULL){ + RemotePlayer *player = dynamic_cast(env->getPlayer(name)); + if (player == NULL){ lua_pushnil(L); return 1; } diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index befae4253..4e1a1c159 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -107,7 +107,7 @@ PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref) return (PlayerSAO*)obj; } -Player* ObjectRef::getplayer(ObjectRef *ref) +RemotePlayer* ObjectRef::getplayer(ObjectRef *ref) { PlayerSAO *playersao = getplayersao(ref); if (playersao == NULL) @@ -1212,8 +1212,8 @@ int ObjectRef::l_get_player_control(lua_State *L) lua_pushlstring(L, "", 0); return 1; } - // Do it - PlayerControl control = player->getPlayerControl(); + + const PlayerControl &control = player->getPlayerControl(); lua_newtable(L); lua_pushboolean(L, control.up); lua_setfield(L, -2, "up"); @@ -1467,7 +1467,7 @@ int ObjectRef::l_hud_set_flags(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1519,7 +1519,7 @@ int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1537,7 +1537,7 @@ int ObjectRef::l_hud_get_hotbar_itemcount(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1677,7 +1677,7 @@ int ObjectRef::l_override_day_night_ratio(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1700,7 +1700,7 @@ int ObjectRef::l_get_day_night_ratio(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h index a37d29535..a7ac9dff9 100644 --- a/src/script/lua_api/l_object.h +++ b/src/script/lua_api/l_object.h @@ -27,6 +27,7 @@ class ServerActiveObject; class LuaEntitySAO; class PlayerSAO; class Player; +class RemotePlayer; /* ObjectRef @@ -47,7 +48,7 @@ private: static PlayerSAO* getplayersao(ObjectRef *ref); - static Player* getplayer(ObjectRef *ref); + static RemotePlayer* getplayer(ObjectRef *ref); // Exported functions diff --git a/src/server.cpp b/src/server.cpp index 2dd070b1a..11ffba343 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1256,7 +1256,7 @@ Inventory* Server::getInventory(const InventoryLocation &loc) break; case InventoryLocation::PLAYER: { - Player *player = m_env->getPlayer(loc.name.c_str()); + RemotePlayer *player = dynamic_cast(m_env->getPlayer(loc.name.c_str())); if(!player) return NULL; PlayerSAO *playersao = player->getPlayerSAO(); @@ -1296,9 +1296,12 @@ void Server::setInventoryModified(const InventoryLocation &loc, bool playerSend) if (!playerSend) return; - Player *player = m_env->getPlayer(loc.name.c_str()); - if(!player) + RemotePlayer *player = + dynamic_cast(m_env->getPlayer(loc.name.c_str())); + + if (!player) return; + PlayerSAO *playersao = player->getPlayerSAO(); if(!playersao) return; @@ -2637,19 +2640,16 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason) ++i; } - Player *player = m_env->getPlayer(peer_id); + RemotePlayer *player = dynamic_cast(m_env->getPlayer(peer_id)); /* Run scripts and remove from environment */ - { - if(player != NULL) - { - PlayerSAO *playersao = player->getPlayerSAO(); - assert(playersao); + if(player != NULL) { + PlayerSAO *playersao = player->getPlayerSAO(); + assert(playersao); - m_script->on_leaveplayer(playersao, reason == CDR_TIMEOUT); + m_script->on_leaveplayer(playersao, reason == CDR_TIMEOUT); - playersao->disconnected(); - } + playersao->disconnected(); } /* @@ -2691,7 +2691,7 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason) SendChatMessage(PEER_ID_INEXISTENT,message); } -void Server::UpdateCrafting(Player* player) +void Server::UpdateCrafting(RemotePlayer* player) { DSTACK(FUNCTION_NAME); @@ -2701,7 +2701,8 @@ void Server::UpdateCrafting(Player* player) loc.setPlayer(player->getName()); std::vector output_replacements; getCraftingResult(&player->inventory, preview, output_replacements, false, this); - m_env->getScriptIface()->item_CraftPredict(preview, player->getPlayerSAO(), (&player->inventory)->getList("craft"), loc); + m_env->getScriptIface()->item_CraftPredict(preview, player->getPlayerSAO(), + (&player->inventory)->getList("craft"), loc); // Put the new preview in InventoryList *plist = player->inventory.getList("craftpreview"); @@ -2851,8 +2852,8 @@ std::string Server::getPlayerName(u16 peer_id) PlayerSAO* Server::getPlayerSAO(u16 peer_id) { - Player *player = m_env->getPlayer(peer_id); - if(player == NULL) + RemotePlayer *player = dynamic_cast(m_env->getPlayer(peer_id)); + if (player == NULL) return NULL; return player->getPlayerSAO(); } @@ -2917,8 +2918,9 @@ void Server::reportPrivsModified(const std::string &name) reportPrivsModified(player->getName()); } } else { - Player *player = m_env->getPlayer(name.c_str()); - if(!player) + RemotePlayer *player = + dynamic_cast(m_env->getPlayer(name.c_str())); + if (!player) return; SendPlayerPrivileges(player->peer_id); PlayerSAO *sao = player->getPlayerSAO(); @@ -3025,7 +3027,7 @@ bool Server::hudChange(Player *player, u32 id, HudElementStat stat, void *data) return true; } -bool Server::hudSetFlags(Player *player, u32 flags, u32 mask) +bool Server::hudSetFlags(RemotePlayer *player, u32 flags, u32 mask) { if (!player) return false; @@ -3043,10 +3045,11 @@ bool Server::hudSetFlags(Player *player, u32 flags, u32 mask) return true; } -bool Server::hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount) +bool Server::hudSetHotbarItemcount(RemotePlayer *player, s32 hotbar_itemcount) { if (!player) return false; + if (hotbar_itemcount <= 0 || hotbar_itemcount > HUD_HOTBAR_ITEMCOUNT_MAX) return false; @@ -3057,13 +3060,6 @@ bool Server::hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount) return true; } -s32 Server::hudGetHotbarItemcount(Player *player) -{ - if (!player) - return 0; - return player->getHotbarItemcount(); -} - void Server::hudSetHotbarImage(Player *player, std::string name) { if (!player) @@ -3130,7 +3126,7 @@ bool Server::setSky(Player *player, const video::SColor &bgcolor, return true; } -bool Server::overrideDayNightRatio(Player *player, bool do_override, +bool Server::overrideDayNightRatio(RemotePlayer *player, bool do_override, float ratio) { if (!player) diff --git a/src/server.h b/src/server.h index 34427a71a..331a91a52 100644 --- a/src/server.h +++ b/src/server.h @@ -34,6 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "environment.h" #include "chat_interface.h" #include "clientiface.h" +#include "player.h" #include "network/networkpacket.h" #include #include @@ -48,7 +49,6 @@ class IWritableCraftDefManager; class BanManager; class EventManager; class Inventory; -class Player; class PlayerSAO; class IRollbackManager; struct RollbackAction; @@ -336,9 +336,10 @@ public: u32 hudAdd(Player *player, HudElement *element); bool hudRemove(Player *player, u32 id); bool hudChange(Player *player, u32 id, HudElementStat stat, void *value); - bool hudSetFlags(Player *player, u32 flags, u32 mask); - bool hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount); - s32 hudGetHotbarItemcount(Player *player); + bool hudSetFlags(RemotePlayer *player, u32 flags, u32 mask); + bool hudSetHotbarItemcount(RemotePlayer *player, s32 hotbar_itemcount); + s32 hudGetHotbarItemcount(RemotePlayer *player) const + { return player->getHotbarItemcount(); } void hudSetHotbarImage(Player *player, std::string name); std::string hudGetHotbarImage(Player *player); void hudSetHotbarSelectedImage(Player *player, std::string name); @@ -353,7 +354,7 @@ public: bool setSky(Player *player, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms); - bool overrideDayNightRatio(Player *player, bool do_override, float brightness); + bool overrideDayNightRatio(RemotePlayer *player, bool do_override, float brightness); /* con::PeerHandler implementation. */ void peerAdded(con::Peer *peer); @@ -475,7 +476,7 @@ private: void DiePlayer(u16 peer_id); void RespawnPlayer(u16 peer_id); void DeleteClient(u16 peer_id, ClientDeletionReason reason); - void UpdateCrafting(Player *player); + void UpdateCrafting(RemotePlayer *player); void handleChatInterfaceEvent(ChatEvent *evt); -- cgit v1.2.3 From ad163ee5c3f7d6ca31e0add052fb76466a9bfcc8 Mon Sep 17 00:00:00 2001 From: Foghrye4 Date: Sat, 8 Oct 2016 16:51:25 +0400 Subject: Prevent attached models from disappearing during parent reload (#4128) --- src/content_cao.cpp | 41 ++++++++++++++++++++++------------------- src/content_sao.cpp | 20 ++++++++++++++++---- src/content_sao.h | 8 ++++---- src/genericobject.cpp | 12 ++++++++++++ src/genericobject.h | 5 ++++- src/script/lua_api/l_object.cpp | 4 ++-- src/serverobject.h | 4 ++-- 7 files changed, 62 insertions(+), 32 deletions(-) (limited to 'src/script') diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 33dae6822..207a630d7 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -1567,8 +1567,7 @@ void GenericCAO::processMessage(const std::string &data) std::istringstream is(data, std::ios::binary); // command u8 cmd = readU8(is); - if(cmd == GENERIC_CMD_SET_PROPERTIES) - { + if (cmd == GENERIC_CMD_SET_PROPERTIES) { m_prop = gob_read_set_properties(is); m_selection_box = m_prop.collisionbox; @@ -1587,9 +1586,7 @@ void GenericCAO::processMessage(const std::string &data) m_prop.nametag = m_name; expireVisuals(); - } - else if(cmd == GENERIC_CMD_UPDATE_POSITION) - { + } else if (cmd == GENERIC_CMD_UPDATE_POSITION) { // Not sent by the server if this object is an attachment. // We might however get here if the server notices the object being detached before the client. m_position = readV3F1000(is); @@ -1619,12 +1616,10 @@ void GenericCAO::processMessage(const std::string &data) pos_translator.init(m_position); } updateNodePos(); - } - else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) { + } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) { std::string mod = deSerializeString(is); updateTextures(mod); - } - else if(cmd == GENERIC_CMD_SET_SPRITE) { + } else if (cmd == GENERIC_CMD_SET_SPRITE) { v2s16 p = readV2S16(is); int num_frames = readU16(is); float framelength = readF1000(is); @@ -1636,8 +1631,7 @@ void GenericCAO::processMessage(const std::string &data) m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch; updateTexturePos(); - } - else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) { + } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) { float override_speed = readF1000(is); float override_jump = readF1000(is); float override_gravity = readF1000(is); @@ -1655,8 +1649,7 @@ void GenericCAO::processMessage(const std::string &data) player->physics_override_sneak = sneak; player->physics_override_sneak_glitch = sneak_glitch; } - } - else if(cmd == GENERIC_CMD_SET_ANIMATION) { + } else if (cmd == GENERIC_CMD_SET_ANIMATION) { // TODO: change frames send as v2s32 value v2f range = readV2F1000(is); if (!m_is_local_player) { @@ -1690,8 +1683,7 @@ void GenericCAO::processMessage(const std::string &data) updateAnimation(); } } - } - else if(cmd == GENERIC_CMD_SET_BONE_POSITION) { + } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) { std::string bone = deSerializeString(is); v3f position = readV3F1000(is); v3f rotation = readV3F1000(is); @@ -1724,8 +1716,7 @@ void GenericCAO::processMessage(const std::string &data) } updateAttachments(); - } - else if(cmd == GENERIC_CMD_PUNCHED) { + } else if (cmd == GENERIC_CMD_PUNCHED) { /*s16 damage =*/ readS16(is); s16 result_hp = readS16(is); @@ -1753,8 +1744,7 @@ void GenericCAO::processMessage(const std::string &data) updateTextures("^[brighten"); } } - } - else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) { + } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) { m_armor_groups.clear(); int armor_groups_size = readU16(is); for(int i=0; inametag_color = m_prop.nametag_color; } + } else if (cmd == GENERIC_CMD_SPAWN_INFANT) { + u16 child_id = readU16(is); + u8 type = readU8(is); + + if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) { + childobj->initialize(deSerializeLongString(is)); + } else { + m_env->addActiveObject(child_id, type, deSerializeLongString(is)); + } + } else { + warningstream << FUNCTION_NAME + << ": unknown command or outdated client \"" + << cmd << std::endl; } } diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 2317cbdfe..1664f5993 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -380,7 +380,7 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version) writeF1000(os, m_yaw); writeS16(os, m_hp); - writeU8(os, 4 + m_bone_position.size()); // number of messages stuffed in here + writeU8(os, 4 + m_bone_position.size() + m_attachment_child_ids.size()); // number of messages stuffed in here os<::const_iterator ii = m_attachment_child_ids.begin(); + (ii != m_attachment_child_ids.end()); ++ii) { + if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) { + os << serializeLongString(gob_cmd_update_infant(*ii, obj->getSendType(), obj->getClientInitializationData(protocol_version))); + } + } } else { @@ -618,7 +624,7 @@ void LuaEntitySAO::removeAttachmentChild(int child_id) m_attachment_child_ids.erase(child_id); } -std::set LuaEntitySAO::getAttachmentChildIds() +UNORDERED_SET LuaEntitySAO::getAttachmentChildIds() { return m_attachment_child_ids; } @@ -860,7 +866,7 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version) writeF1000(os, m_player->getYaw()); writeS16(os, getHP()); - writeU8(os, 6 + m_bone_position.size()); // number of messages stuffed in here + writeU8(os, 6 + m_bone_position.size() + m_attachment_child_ids.size()); // number of messages stuffed in here os<::const_iterator ii = m_attachment_child_ids.begin(); + ii != m_attachment_child_ids.end(); ++ii) { + if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) { + os << serializeLongString(gob_cmd_update_infant(*ii, obj->getSendType(), obj->getClientInitializationData(protocol_version))); + } + } } else { @@ -1266,7 +1278,7 @@ void PlayerSAO::removeAttachmentChild(int child_id) m_attachment_child_ids.erase(child_id); } -std::set PlayerSAO::getAttachmentChildIds() +UNORDERED_SET PlayerSAO::getAttachmentChildIds() { return m_attachment_child_ids; } diff --git a/src/content_sao.h b/src/content_sao.h index c97db4922..341ebb5da 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -67,7 +67,7 @@ public: void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation); void addAttachmentChild(int child_id); void removeAttachmentChild(int child_id); - std::set getAttachmentChildIds(); + UNORDERED_SET getAttachmentChildIds(); ObjectProperties* accessObjectProperties(); void notifyObjectPropertiesModified(); /* LuaEntitySAO-specific */ @@ -116,7 +116,7 @@ private: bool m_bone_position_sent; int m_attachment_parent_id; - std::set m_attachment_child_ids; + UNORDERED_SET m_attachment_child_ids; std::string m_attachment_bone; v3f m_attachment_position; v3f m_attachment_rotation; @@ -210,7 +210,7 @@ public: void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation); void addAttachmentChild(int child_id); void removeAttachmentChild(int child_id); - std::set getAttachmentChildIds(); + UNORDERED_SET getAttachmentChildIds(); ObjectProperties* accessObjectProperties(); void notifyObjectPropertiesModified(); @@ -320,7 +320,7 @@ private: bool m_bone_position_sent; int m_attachment_parent_id; - std::set m_attachment_child_ids; + UNORDERED_SET m_attachment_child_ids; std::string m_attachment_bone; v3f m_attachment_position; v3f m_attachment_rotation; diff --git a/src/genericobject.cpp b/src/genericobject.cpp index 368cae1ff..c4660cf44 100644 --- a/src/genericobject.cpp +++ b/src/genericobject.cpp @@ -182,3 +182,15 @@ std::string gob_cmd_update_nametag_attributes(video::SColor color) writeARGB8(os, color); return os.str(); } + +std::string gob_cmd_update_infant(u16 id, u8 type, std::string client_initialization_data) +{ + std::ostringstream os(std::ios::binary); + // command + writeU8(os, GENERIC_CMD_SPAWN_INFANT); + // parameters + writeU16(os, id); + writeU8(os, type); + os<getType() == ACTIVEOBJECT_TYPE_PLAYER) return 0; - std::set child_ids = co->getAttachmentChildIds(); - std::set::iterator it; + UNORDERED_SET child_ids = co->getAttachmentChildIds(); + UNORDERED_SET::iterator it; for (it = child_ids.begin(); it != child_ids.end(); ++it) { ServerActiveObject *child = env->getActiveObject(*it); child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0)); diff --git a/src/serverobject.h b/src/serverobject.h index 597eb63a8..9f8d5403c 100644 --- a/src/serverobject.h +++ b/src/serverobject.h @@ -167,8 +167,8 @@ public: {} virtual void removeAttachmentChild(int child_id) {} - virtual std::set getAttachmentChildIds() - { return std::set(); } + virtual UNORDERED_SET getAttachmentChildIds() + { return UNORDERED_SET(); } virtual ObjectProperties* accessObjectProperties() { return NULL; } virtual void notifyObjectPropertiesModified() -- cgit v1.2.3 From 656faf7373587bc59b47986a28dbd2fce4c45474 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 8 Oct 2016 12:21:41 +0200 Subject: Player/LocalPlayer/RemotePlayer inheritance cleanup (part 2 on X) * Server/Client Environments now have an helper to cast Player object in the right type to use it * Server: use RemotePlayer everywhere and remove previous added casts * Client: use LocalPlayer where needed * Environment: remove unused functions (getPlayers(), getRandomConnectedPlayer(), getNearestConnectedPlayer()) --- src/client.cpp | 2 +- src/environment.cpp | 84 +++++++++---------------------------- src/environment.h | 18 ++++---- src/network/serverpackethandler.cpp | 24 ++++------- src/script/lua_api/l_inventory.cpp | 2 +- src/script/lua_api/l_inventory.h | 9 +--- src/script/lua_api/l_object.cpp | 20 ++++----- src/script/lua_api/l_object.h | 1 - src/script/lua_api/l_server.cpp | 21 +++++----- src/server.cpp | 26 ++++++------ src/server.h | 21 +++++----- src/serverobject.h | 25 ++++++----- 12 files changed, 97 insertions(+), 156 deletions(-) (limited to 'src/script') diff --git a/src/client.cpp b/src/client.cpp index 63653998a..392dabde6 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1418,7 +1418,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc) break; case InventoryLocation::PLAYER: { - Player *player = m_env.getPlayer(loc.name.c_str()); + LocalPlayer *player = m_env.getPlayer(loc.name.c_str()); if(!player) return NULL; return &player->inventory; diff --git a/src/environment.cpp b/src/environment.cpp index 34b3c34f4..d30b70527 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -127,65 +127,6 @@ Player * Environment::getPlayer(const char *name) return NULL; } -Player * Environment::getRandomConnectedPlayer() -{ - std::vector connected_players = getPlayers(true); - u32 chosen_one = myrand() % connected_players.size(); - u32 j = 0; - for(std::vector::iterator - i = connected_players.begin(); - i != connected_players.end(); ++i) { - if(j == chosen_one) { - Player *player = *i; - return player; - } - j++; - } - return NULL; -} - -Player * Environment::getNearestConnectedPlayer(v3f pos) -{ - std::vector connected_players = getPlayers(true); - f32 nearest_d = 0; - Player *nearest_player = NULL; - for(std::vector::iterator - i = connected_players.begin(); - i != connected_players.end(); ++i) { - Player *player = *i; - f32 d = player->getPosition().getDistanceFrom(pos); - if(d < nearest_d || nearest_player == NULL) { - nearest_d = d; - nearest_player = player; - } - } - return nearest_player; -} - -std::vector Environment::getPlayers() -{ - return m_players; -} - -std::vector Environment::getPlayers(bool ignore_disconnected) -{ - std::vector newlist; - for(std::vector::iterator - i = m_players.begin(); - i != m_players.end(); ++i) { - Player *player = *i; - - if(ignore_disconnected) { - // Ignore disconnected players - if(player->peer_id == 0) - continue; - } - - newlist.push_back(player); - } - return newlist; -} - u32 Environment::getDayNightRatio() { MutexAutoLock lock(this->m_time_lock); @@ -199,11 +140,6 @@ void Environment::setTimeOfDaySpeed(float speed) m_time_of_day_speed = speed; } -float Environment::getTimeOfDaySpeed() -{ - return m_time_of_day_speed; -} - void Environment::setDayNightRatioOverride(bool enable, u32 value) { MutexAutoLock lock(this->m_time_lock); @@ -625,6 +561,16 @@ ServerMap & ServerEnvironment::getServerMap() return *m_map; } +RemotePlayer *ServerEnvironment::getPlayer(const u16 peer_id) +{ + return dynamic_cast(Environment::getPlayer(peer_id)); +} + +RemotePlayer *ServerEnvironment::getPlayer(const char* name) +{ + return dynamic_cast(Environment::getPlayer(name)); +} + bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 *p) { float distance = pos1.getDistanceFrom(pos2); @@ -2349,6 +2295,16 @@ ClientMap & ClientEnvironment::getClientMap() return *m_map; } +LocalPlayer *ClientEnvironment::getPlayer(const u16 peer_id) +{ + return dynamic_cast(Environment::getPlayer(peer_id)); +} + +LocalPlayer *ClientEnvironment::getPlayer(const char* name) +{ + return dynamic_cast(Environment::getPlayer(name)); +} + void ClientEnvironment::addPlayer(Player *player) { DSTACK(FUNCTION_NAME); diff --git a/src/environment.h b/src/environment.h index 1ba7b196f..66d9c19c0 100644 --- a/src/environment.h +++ b/src/environment.h @@ -74,12 +74,6 @@ public: virtual void addPlayer(Player *player); void removePlayer(Player *player); - Player * getPlayer(u16 peer_id); - Player * getPlayer(const char *name); - Player * getRandomConnectedPlayer(); - Player * getNearestConnectedPlayer(v3f pos); - std::vector getPlayers(); - std::vector getPlayers(bool ignore_disconnected); u32 getDayNightRatio(); @@ -91,7 +85,6 @@ public: void stepTimeOfDay(float dtime); void setTimeOfDaySpeed(float speed); - float getTimeOfDaySpeed(); void setDayNightRatioOverride(bool enable, u32 value); @@ -101,6 +94,9 @@ public: u32 m_added_objects; protected: + Player * getPlayer(u16 peer_id); + Player * getPlayer(const char *name); + // peer_ids in here should be unique, except that there may be many 0s std::vector m_players; @@ -440,6 +436,8 @@ public: void setStaticForActiveObjectsInBlock(v3s16 blockpos, bool static_exists, v3s16 static_block=v3s16(0,0,0)); + RemotePlayer *getPlayer(const u16 peer_id); + RemotePlayer *getPlayer(const char* name); private: /* @@ -640,8 +638,10 @@ public: { m_player_names.remove(name); } void updateCameraOffset(v3s16 camera_offset) { m_camera_offset = camera_offset; } - v3s16 getCameraOffset() - { return m_camera_offset; } + v3s16 getCameraOffset() const { return m_camera_offset; } + + LocalPlayer *getPlayer(const u16 peer_id); + LocalPlayer *getPlayer(const char* name); private: ClientMap *m_map; diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index 3fba7f720..6ef768295 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -800,8 +800,7 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt) pitch = modulo360f(pitch); yaw = modulo360f(yaw); - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -880,8 +879,7 @@ void Server::handleCommand_DeletedBlocks(NetworkPacket* pkt) void Server::handleCommand_InventoryAction(NetworkPacket* pkt) { - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1081,8 +1079,7 @@ void Server::handleCommand_Damage(NetworkPacket* pkt) *pkt >> damage; - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1117,8 +1114,7 @@ void Server::handleCommand_Breath(NetworkPacket* pkt) *pkt >> breath; - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1231,8 +1227,7 @@ void Server::handleCommand_PlayerItem(NetworkPacket* pkt) if (pkt->getSize() < 2) return; - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1308,8 +1303,7 @@ void Server::handleCommand_Interact(NetworkPacket* pkt) verbosestream << "TOSERVER_INTERACT: action=" << (int)action << ", item=" << item_i << ", pointed=" << pointed.dump() << std::endl; - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1730,8 +1724,7 @@ void Server::handleCommand_NodeMetaFields(NetworkPacket* pkt) fields[fieldname] = pkt->readLongString(); } - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1782,8 +1775,7 @@ void Server::handleCommand_InventoryFields(NetworkPacket* pkt) fields[fieldname] = pkt->readLongString(); } - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index de9f9374a..110e68d23 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -420,7 +420,7 @@ void InvRef::create(lua_State *L, const InventoryLocation &loc) luaL_getmetatable(L, className); lua_setmetatable(L, -2); } -void InvRef::createPlayer(lua_State *L, Player *player) +void InvRef::createPlayer(lua_State *L, RemotePlayer *player) { NO_MAP_LOCK_REQUIRED; InventoryLocation loc; diff --git a/src/script/lua_api/l_inventory.h b/src/script/lua_api/l_inventory.h index 2d4b29d0c..cc5333965 100644 --- a/src/script/lua_api/l_inventory.h +++ b/src/script/lua_api/l_inventory.h @@ -25,7 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "inventory.h" #include "inventorymanager.h" -class Player; +class RemotePlayer; /* InvRef @@ -112,7 +112,7 @@ public: // Creates an InvRef and leaves it on top of stack // Not callable from Lua; all references are created on the C side. static void create(lua_State *L, const InventoryLocation &loc); - static void createPlayer(lua_State *L, Player *player); + static void createPlayer(lua_State *L, RemotePlayer *player); static void createNodeMeta(lua_State *L, v3s16 p); static void Register(lua_State *L); }; @@ -123,11 +123,6 @@ private: static int l_get_inventory(lua_State *L); - static void inventory_set_list_from_lua(Inventory *inv, const char *name, - lua_State *L, int tableindex, int forcesize); - static void inventory_get_list_to_lua(Inventory *inv, const char *name, - lua_State *L); - public: static void Initialize(lua_State *L, int top); }; diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 34e175ad0..b58d8e6cd 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -508,7 +508,7 @@ int ObjectRef::l_set_local_animation(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it @@ -554,7 +554,7 @@ int ObjectRef::l_set_eye_offset(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it @@ -1256,7 +1256,7 @@ int ObjectRef::l_hud_add(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1319,7 +1319,7 @@ int ObjectRef::l_hud_remove(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1339,7 +1339,7 @@ int ObjectRef::l_hud_change(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1552,7 +1552,7 @@ int ObjectRef::l_hud_set_hotbar_image(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1567,7 +1567,7 @@ int ObjectRef::l_hud_get_hotbar_image(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1581,7 +1581,7 @@ int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1596,7 +1596,7 @@ int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1610,7 +1610,7 @@ int ObjectRef::l_set_sky(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h index a7ac9dff9..dfc1b49d2 100644 --- a/src/script/lua_api/l_object.h +++ b/src/script/lua_api/l_object.h @@ -26,7 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc., class ServerActiveObject; class LuaEntitySAO; class PlayerSAO; -class Player; class RemotePlayer; /* diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index 59d3f5c70..95e5da07f 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -106,7 +106,7 @@ int ModApiServer::l_get_player_ip(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char * name = luaL_checkstring(L, 1); - Player *player = getEnv(L)->getPlayer(name); + RemotePlayer *player = dynamic_cast(getEnv(L))->getPlayer(name); if(player == NULL) { lua_pushnil(L); // no such player @@ -133,9 +133,8 @@ int ModApiServer::l_get_player_information(lua_State *L) NO_MAP_LOCK_REQUIRED; const char * name = luaL_checkstring(L, 1); - Player *player = getEnv(L)->getPlayer(name); - if(player == NULL) - { + RemotePlayer *player = dynamic_cast(getEnv(L))->getPlayer(name); + if (player == NULL) { lua_pushnil(L); // no such player return 1; } @@ -278,15 +277,15 @@ int ModApiServer::l_ban_player(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char * name = luaL_checkstring(L, 1); - Player *player = getEnv(L)->getPlayer(name); - if(player == NULL) - { + RemotePlayer *player = dynamic_cast(getEnv(L))->getPlayer(name); + if (player == NULL) { lua_pushboolean(L, false); // no such player return 1; } try { - Address addr = getServer(L)->getPeerAddress(getEnv(L)->getPlayer(name)->peer_id); + Address addr = getServer(L)->getPeerAddress( + dynamic_cast(getEnv(L))->getPlayer(name)->peer_id); std::string ip_str = addr.serializeString(); getServer(L)->setIpBanned(ip_str, name); } @@ -314,9 +313,9 @@ int ModApiServer::l_kick_player(lua_State *L) { message = "Kicked."; } - Player *player = getEnv(L)->getPlayer(name); - if (player == NULL) - { + + RemotePlayer *player = dynamic_cast(getEnv(L))->getPlayer(name); + if (player == NULL) { lua_pushboolean(L, false); // No such player return 1; } diff --git a/src/server.cpp b/src/server.cpp index 540d23b9d..dee8a3d70 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2638,7 +2638,7 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason) ++i; } - RemotePlayer *player = dynamic_cast(m_env->getPlayer(peer_id)); + RemotePlayer *player = m_env->getPlayer(peer_id); /* Run scripts and remove from environment */ if(player != NULL) { @@ -2850,7 +2850,7 @@ std::string Server::getPlayerName(u16 peer_id) PlayerSAO* Server::getPlayerSAO(u16 peer_id) { - RemotePlayer *player = dynamic_cast(m_env->getPlayer(peer_id)); + RemotePlayer *player = m_env->getPlayer(peer_id); if (player == NULL) return NULL; return player->getPlayerSAO(); @@ -2989,7 +2989,7 @@ bool Server::showFormspec(const char *playername, const std::string &formspec, return true; } -u32 Server::hudAdd(Player *player, HudElement *form) +u32 Server::hudAdd(RemotePlayer *player, HudElement *form) { if (!player) return -1; @@ -3001,7 +3001,7 @@ u32 Server::hudAdd(Player *player, HudElement *form) return id; } -bool Server::hudRemove(Player *player, u32 id) { +bool Server::hudRemove(RemotePlayer *player, u32 id) { if (!player) return false; @@ -3016,7 +3016,7 @@ bool Server::hudRemove(Player *player, u32 id) { return true; } -bool Server::hudChange(Player *player, u32 id, HudElementStat stat, void *data) +bool Server::hudChange(RemotePlayer *player, u32 id, HudElementStat stat, void *data) { if (!player) return false; @@ -3058,7 +3058,7 @@ bool Server::hudSetHotbarItemcount(RemotePlayer *player, s32 hotbar_itemcount) return true; } -void Server::hudSetHotbarImage(Player *player, std::string name) +void Server::hudSetHotbarImage(RemotePlayer *player, std::string name) { if (!player) return; @@ -3067,14 +3067,14 @@ void Server::hudSetHotbarImage(Player *player, std::string name) SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_IMAGE, name); } -std::string Server::hudGetHotbarImage(Player *player) +std::string Server::hudGetHotbarImage(RemotePlayer *player) { if (!player) return ""; return player->getHotbarImage(); } -void Server::hudSetHotbarSelectedImage(Player *player, std::string name) +void Server::hudSetHotbarSelectedImage(RemotePlayer *player, std::string name) { if (!player) return; @@ -3083,7 +3083,7 @@ void Server::hudSetHotbarSelectedImage(Player *player, std::string name) SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_SELECTED_IMAGE, name); } -std::string Server::hudGetHotbarSelectedImage(Player *player) +std::string Server::hudGetHotbarSelectedImage(RemotePlayer *player) { if (!player) return ""; @@ -3091,8 +3091,8 @@ std::string Server::hudGetHotbarSelectedImage(Player *player) return player->getHotbarSelectedImage(); } -bool Server::setLocalPlayerAnimations(Player *player, - v2s32 animation_frames[4], f32 frame_speed) +bool Server::setLocalPlayerAnimations(RemotePlayer *player, + v2s32 animation_frames[4], f32 frame_speed) { if (!player) return false; @@ -3102,7 +3102,7 @@ bool Server::setLocalPlayerAnimations(Player *player, return true; } -bool Server::setPlayerEyeOffset(Player *player, v3f first, v3f third) +bool Server::setPlayerEyeOffset(RemotePlayer *player, v3f first, v3f third) { if (!player) return false; @@ -3113,7 +3113,7 @@ bool Server::setPlayerEyeOffset(Player *player, v3f first, v3f third) return true; } -bool Server::setSky(Player *player, const video::SColor &bgcolor, +bool Server::setSky(RemotePlayer *player, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms) { if (!player) diff --git a/src/server.h b/src/server.h index 555aab692..8eb1afc9f 100644 --- a/src/server.h +++ b/src/server.h @@ -307,25 +307,26 @@ public: Map & getMap() { return m_env->getMap(); } ServerEnvironment & getEnv() { return *m_env; } - u32 hudAdd(Player *player, HudElement *element); - bool hudRemove(Player *player, u32 id); - bool hudChange(Player *player, u32 id, HudElementStat stat, void *value); + u32 hudAdd(RemotePlayer *player, HudElement *element); + bool hudRemove(RemotePlayer *player, u32 id); + bool hudChange(RemotePlayer *player, u32 id, HudElementStat stat, void *value); bool hudSetFlags(RemotePlayer *player, u32 flags, u32 mask); bool hudSetHotbarItemcount(RemotePlayer *player, s32 hotbar_itemcount); s32 hudGetHotbarItemcount(RemotePlayer *player) const { return player->getHotbarItemcount(); } - void hudSetHotbarImage(Player *player, std::string name); - std::string hudGetHotbarImage(Player *player); - void hudSetHotbarSelectedImage(Player *player, std::string name); - std::string hudGetHotbarSelectedImage(Player *player); + void hudSetHotbarImage(RemotePlayer *player, std::string name); + std::string hudGetHotbarImage(RemotePlayer *player); + void hudSetHotbarSelectedImage(RemotePlayer *player, std::string name); + std::string hudGetHotbarSelectedImage(RemotePlayer *player); inline Address getPeerAddress(u16 peer_id) { return m_con.GetPeerAddress(peer_id); } - bool setLocalPlayerAnimations(Player *player, v2s32 animation_frames[4], f32 frame_speed); - bool setPlayerEyeOffset(Player *player, v3f first, v3f third); + bool setLocalPlayerAnimations(RemotePlayer *player, v2s32 animation_frames[4], + f32 frame_speed); + bool setPlayerEyeOffset(RemotePlayer *player, v3f first, v3f third); - bool setSky(Player *player, const video::SColor &bgcolor, + bool setSky(RemotePlayer *player, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms); bool overrideDayNightRatio(RemotePlayer *player, bool do_override, float brightness); diff --git a/src/serverobject.h b/src/serverobject.h index 9f8d5403c..cfe2b6bcc 100644 --- a/src/serverobject.h +++ b/src/serverobject.h @@ -44,7 +44,6 @@ Some planning class ServerEnvironment; struct ItemStack; -class Player; struct ToolCapabilities; struct ObjectProperties; @@ -69,23 +68,23 @@ public: // environment virtual bool environmentDeletes() const { return true; } - + // Create a certain type of ServerActiveObject static ServerActiveObject* create(ActiveObjectType type, ServerEnvironment *env, u16 id, v3f pos, const std::string &data); - + /* Some simple getters/setters */ v3f getBasePosition(){ return m_base_position; } void setBasePosition(v3f pos){ m_base_position = pos; } ServerEnvironment* getEnv(){ return m_env; } - + /* Some more dynamic interface */ - + virtual void setPos(v3f pos) { setBasePosition(pos); } // continuous: if true, object does not stop immediately at pos @@ -96,7 +95,7 @@ public: virtual float getMinimumSavedMovement(); virtual std::string getDescription(){return "SAO";} - + /* Step object in time. Messages added to messages are sent to client over network. @@ -108,13 +107,13 @@ public: packet. */ virtual void step(float dtime, bool send_recommended){} - + /* The return value of this is passed to the client-side object when it is created */ virtual std::string getClientInitializationData(u16 protocol_version){return "";} - + /* The return value of this is passed to the server-side object when it is created (converted from static to active - actually @@ -131,7 +130,7 @@ public: */ virtual bool isStaticAllowed() const {return true;} - + // Returns tool wear virtual int punch(v3f dir, const ToolCapabilities *toolcap=NULL, @@ -207,7 +206,7 @@ public: - This can be set to true by anything else too. */ bool m_removed; - + /* This is set to true when an object should be removed from the active object list but couldn't be removed because the id has to be @@ -218,7 +217,7 @@ public: list. */ bool m_pending_deactivation; - + /* Whether the object's static data has been stored to a block */ @@ -228,12 +227,12 @@ public: a copy of the static data resides. */ v3s16 m_static_block; - + /* Queue of messages to be sent to the client */ std::queue m_messages_out; - + protected: // Used for creating objects based on type typedef ServerActiveObject* (*Factory) -- cgit v1.2.3 From fd5a130b86c08f0b3190c3d81affd4869c139fb7 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 8 Oct 2016 16:31:22 +0200 Subject: More code cleanup (UNORDERED + RemotePlayer/LocalPlayer) * ClientEnvironment now uses UNORDERED MAP for active objects * Use RemotePlayer and LocalPlayer everywhere it's possible * Minor code style fixes * Drop Client::getBreath() unused function --- src/client.cpp | 21 +++----- src/client.h | 1 - src/clientiface.cpp | 13 ++--- src/content_cao.cpp | 7 ++- src/environment.cpp | 105 ++++++++++++++++-------------------- src/environment.h | 12 ++--- src/network/clientpackethandler.cpp | 18 +++---- src/network/serverpackethandler.cpp | 6 +-- src/script/lua_api/l_object.cpp | 36 ++++++------- src/server.cpp | 57 ++++++++++---------- 10 files changed, 124 insertions(+), 152 deletions(-) (limited to 'src/script') diff --git a/src/client.cpp b/src/client.cpp index 392dabde6..cd010e592 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -383,7 +383,7 @@ void Client::step(float dtime) if(counter <= 0.0) { counter = 2.0; - Player *myplayer = m_env.getLocalPlayer(); + LocalPlayer *myplayer = m_env.getLocalPlayer(); FATAL_ERROR_IF(myplayer == NULL, "Local player not found in environment."); u16 proto_version_min = g_settings->getFlag("send_pre_v25_init") ? @@ -613,7 +613,7 @@ void Client::step(float dtime) { // Do this every seconds after TOCLIENT_INVENTORY // Reset the locally changed inventory to the authoritative inventory - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); player->inventory = *m_inventory_from_server; m_inventory_updated = true; } @@ -1191,7 +1191,7 @@ void Client::sendChatMessage(const std::wstring &message) void Client::sendChangePassword(const std::string &oldpassword, const std::string &newpassword) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); if (player == NULL) return; @@ -1317,7 +1317,7 @@ void Client::sendPlayerPos() void Client::sendPlayerItem(u16 item) { - Player *myplayer = m_env.getLocalPlayer(); + LocalPlayer *myplayer = m_env.getLocalPlayer(); if(myplayer == NULL) return; @@ -1398,7 +1398,7 @@ bool Client::getLocalInventoryUpdated() // Copies the inventory of the local player to parameter void Client::getLocalInventory(Inventory &dst) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); dst = player->inventory; } @@ -1411,7 +1411,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc) break; case InventoryLocation::CURRENT_PLAYER: { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); return &player->inventory; } @@ -1537,18 +1537,11 @@ void Client::setCrack(int level, v3s16 pos) u16 Client::getHP() { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); return player->hp; } -u16 Client::getBreath() -{ - Player *player = m_env.getLocalPlayer(); - assert(player != NULL); - return player->getBreath(); -} - bool Client::getChatMessage(std::wstring &message) { if(m_chat_queue.size() == 0) diff --git a/src/client.h b/src/client.h index a71c1dcbc..72baac2d2 100644 --- a/src/client.h +++ b/src/client.h @@ -460,7 +460,6 @@ public: void setCrack(int level, v3s16 pos); u16 getHP(); - u16 getBreath(); bool checkPrivilege(const std::string &priv) const { return (m_privileges.count(priv) != 0); } diff --git a/src/clientiface.cpp b/src/clientiface.cpp index e7ad39579..e55c07cb6 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -77,9 +77,9 @@ void RemoteClient::GetNextBlocks ( if(m_nothing_to_send_pause_timer >= 0) return; - Player *player = env->getPlayer(peer_id); + RemotePlayer *player = env->getPlayer(peer_id); // This can happen sometimes; clients and players are not in perfect sync. - if(player == NULL) + if (player == NULL) return; // Won't send anything if already sending @@ -645,8 +645,7 @@ void ClientInterface::step(float dtime) void ClientInterface::UpdatePlayerList() { - if (m_env != NULL) - { + if (m_env != NULL) { std::vector clients = getClientIDs(); m_clients_names.clear(); @@ -654,10 +653,8 @@ void ClientInterface::UpdatePlayerList() if(!clients.empty()) infostream<<"Players:"<::iterator - i = clients.begin(); - i != clients.end(); ++i) { - Player *player = m_env->getPlayer(*i); + for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { + RemotePlayer *player = m_env->getPlayer(*i); if (player == NULL) continue; diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 207a630d7..a53768149 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -655,12 +655,11 @@ void GenericCAO::initialize(const std::string &data) if(m_is_player) { - Player *player = m_env->getPlayer(m_name.c_str()); - if(player && player->isLocal()) - { + LocalPlayer *player = m_env->getPlayer(m_name.c_str()); + if (player && player->isLocal()) { m_is_local_player = true; m_is_visible = false; - LocalPlayer* localplayer = dynamic_cast(player); + LocalPlayer* localplayer = player; assert( localplayer != NULL ); localplayer->setCAO(this); diff --git a/src/environment.cpp b/src/environment.cpp index d30b70527..bc246f66c 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -105,20 +105,20 @@ void Environment::removePlayer(Player* player) } } -Player * Environment::getPlayer(u16 peer_id) +Player *Environment::getPlayer(u16 peer_id) { - for(std::vector::iterator i = m_players.begin(); + for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { Player *player = *i; - if(player->peer_id == peer_id) + if (player->peer_id == peer_id) return player; } return NULL; } -Player * Environment::getPlayer(const char *name) +Player *Environment::getPlayer(const char *name) { - for(std::vector::iterator i = m_players.begin(); + for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { Player *player = *i; if(strcmp(player->getName(), name) == 0) @@ -602,11 +602,9 @@ void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason, const std::string &str_reason, bool reconnect) { for (std::vector::iterator it = m_players.begin(); - it != m_players.end(); - ++it) { + it != m_players.end(); ++it) { ((Server*)m_gamedef)->DenyAccessVerCompliant((*it)->peer_id, - (*it)->protocol_version, (AccessDeniedCode)reason, - str_reason, reconnect); + (*it)->protocol_version, reason, str_reason, reconnect); } } @@ -633,14 +631,14 @@ void ServerEnvironment::savePlayer(RemotePlayer *player) player->save(players_path); } -Player *ServerEnvironment::loadPlayer(const std::string &playername) +RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) { bool newplayer = false; bool found = false; std::string players_path = m_path_world + DIR_DELIM "players" DIR_DELIM; std::string path = players_path + playername; - RemotePlayer *player = static_cast(getPlayer(playername.c_str())); + RemotePlayer *player = getPlayer(playername.c_str()); if (!player) { player = new RemotePlayer(m_gamedef, ""); newplayer = true; @@ -1254,10 +1252,10 @@ void ServerEnvironment::step(float dtime) */ { ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG); - for(std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { - Player *player = *i; + for (std::vector::iterator i = m_players.begin(); + i != m_players.end(); ++i) { + RemotePlayer *player = dynamic_cast(*i); + assert(player); // Ignore disconnected players if(player->peer_id == 0) @@ -1277,12 +1275,12 @@ void ServerEnvironment::step(float dtime) Get player block positions */ std::vector players_blockpos; - for(std::vector::iterator - i = m_players.begin(); + for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { - Player *player = *i; + RemotePlayer *player = dynamic_cast(*i); + assert(player); // Ignore disconnected players - if(player->peer_id == 0) + if (player->peer_id == 0) continue; v3s16 blockpos = getNodeBlockPos( @@ -1381,8 +1379,7 @@ void ServerEnvironment::step(float dtime) block->m_node_timers.step((float)dtime); if (!elapsed_timers.empty()) { MapNode n; - for (std::vector::iterator - i = elapsed_timers.begin(); + for (std::vector::iterator i = elapsed_timers.begin(); i != elapsed_timers.end(); ++i) { n = block->getNodeNoEx(i->position); p = i->position + block->getPosRelative(); @@ -1571,7 +1568,7 @@ u16 ServerEnvironment::addActiveObject(ServerActiveObject *object) Finds out what new objects have been added to inside a radius around a position */ -void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius, +void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &added_objects) @@ -1624,7 +1621,7 @@ void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius, Finds out what objects have been removed from inside a radius around a position */ -void ServerEnvironment::getRemovedActiveObjects(Player *player, s16 radius, +void ServerEnvironment::getRemovedActiveObjects(RemotePlayer *player, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &removed_objects) @@ -2269,10 +2266,8 @@ ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr, ClientEnvironment::~ClientEnvironment() { // delete active objects - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { delete i->second; } @@ -2312,18 +2307,18 @@ void ClientEnvironment::addPlayer(Player *player) It is a failure if player is local and there already is a local player */ - FATAL_ERROR_IF(player->isLocal() == true && getLocalPlayer() != NULL, - "Player is local but there is already a local player"); + FATAL_ERROR_IF(player->isLocal() && getLocalPlayer() != NULL, + "Player is local but there is already a local player"); Environment::addPlayer(player); } -LocalPlayer * ClientEnvironment::getLocalPlayer() +LocalPlayer *ClientEnvironment::getLocalPlayer() { for(std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { Player *player = *i; - if(player->isLocal()) + if (player->isLocal()) return (LocalPlayer*)player; } return NULL; @@ -2407,11 +2402,11 @@ void ClientEnvironment::step(float dtime) { // Apply physics - if(free_move == false && is_climbing == false) + if(!free_move && !is_climbing) { // Gravity v3f speed = lplayer->getSpeed(); - if(lplayer->in_liquid == false) + if(!lplayer->in_liquid) speed.Y -= lplayer->movement_gravity * lplayer->physics_override_gravity * dtime_part * 2; // Liquid floating / sinking @@ -2566,14 +2561,15 @@ void ClientEnvironment::step(float dtime) /* Stuff that can be done in an arbitarily large dtime */ - for(std::vector::iterator i = m_players.begin(); + for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { - Player *player = *i; + LocalPlayer *player = dynamic_cast(*i); + assert(player); /* Handle non-local players */ - if(player->isLocal() == false) { + if (!player->isLocal()) { // Move player->move(dtime, this, 100*BS); @@ -2604,10 +2600,8 @@ void ClientEnvironment::step(float dtime) g_profiler->avg("CEnv: num of objects", m_active_objects.size()); bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21); - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { ClientActiveObject* obj = i->second; // Step object obj->step(dtime, this); @@ -2666,15 +2660,14 @@ GenericCAO* ClientEnvironment::getGenericCAO(u16 id) ClientActiveObject* ClientEnvironment::getActiveObject(u16 id) { - std::map::iterator n; - n = m_active_objects.find(id); - if(n == m_active_objects.end()) + UNORDERED_MAP::iterator n = m_active_objects.find(id); + if (n == m_active_objects.end()) return NULL; return n->second; } -bool isFreeClientActiveObjectId(u16 id, - std::map &objects) +bool isFreeClientActiveObjectId(const u16 id, + UNORDERED_MAP &objects) { if(id == 0) return false; @@ -2682,19 +2675,17 @@ bool isFreeClientActiveObjectId(u16 id, return objects.find(id) == objects.end(); } -u16 getFreeClientActiveObjectId( - std::map &objects) +u16 getFreeClientActiveObjectId(UNORDERED_MAP &objects) { //try to reuse id's as late as possible static u16 last_used_id = 0; u16 startid = last_used_id; - for(;;) - { + for(;;) { last_used_id ++; - if(isFreeClientActiveObjectId(last_used_id, objects)) + if (isFreeClientActiveObjectId(last_used_id, objects)) return last_used_id; - if(last_used_id == startid) + if (last_used_id == startid) return 0; } } @@ -2714,8 +2705,7 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object) } object->setId(new_id); } - if(isFreeClientActiveObjectId(object->getId(), m_active_objects) == false) - { + if(!isFreeClientActiveObjectId(object->getId(), m_active_objects)) { infostream<<"ClientEnvironment::addActiveObject(): " <<"id is not free ("<getId()<<")"< &dest) { - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { ClientActiveObject* obj = i->second; f32 d = (obj->getPosition() - origin).getLength(); diff --git a/src/environment.h b/src/environment.h index 66d9c19c0..99066c367 100644 --- a/src/environment.h +++ b/src/environment.h @@ -94,8 +94,8 @@ public: u32 m_added_objects; protected: - Player * getPlayer(u16 peer_id); - Player * getPlayer(const char *name); + Player *getPlayer(u16 peer_id); + Player *getPlayer(const char *name); // peer_ids in here should be unique, except that there may be many 0s std::vector m_players; @@ -324,7 +324,7 @@ public: // Save players void saveLoadedPlayers(); void savePlayer(RemotePlayer *player); - Player *loadPlayer(const std::string &playername); + RemotePlayer *loadPlayer(const std::string &playername); /* Save and load time of day and game timer @@ -368,7 +368,7 @@ public: Find out what new objects have been added to inside a radius around a position */ - void getAddedActiveObjects(Player *player, s16 radius, + void getAddedActiveObjects(RemotePlayer *player, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &added_objects); @@ -377,7 +377,7 @@ public: Find out what new objects have been removed from inside a radius around a position */ - void getRemovedActiveObjects(Player* player, s16 radius, + void getRemovedActiveObjects(RemotePlayer* player, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &removed_objects); @@ -649,7 +649,7 @@ private: ITextureSource *m_texturesource; IGameDef *m_gamedef; IrrlichtDevice *m_irr; - std::map m_active_objects; + UNORDERED_MAP m_active_objects; std::vector m_simple_objects; std::queue m_client_event_queue; IntervalLimiter m_active_object_light_update_interval; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 35e350f20..b39356e92 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -110,7 +110,7 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt) playerpos -= v3f(0, BS / 2, 0); // Set player position - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->setPosition(playerpos); @@ -176,7 +176,7 @@ void Client::handleCommand_InitLegacy(NetworkPacket* pkt) // Set player position - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->setPosition(playerpos_f); @@ -333,7 +333,7 @@ void Client::handleCommand_Inventory(NetworkPacket* pkt) std::string datastring(pkt->getString(0), pkt->getSize()); std::istringstream is(datastring, std::ios_base::binary); - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->inventory.deSerialize(is); @@ -486,7 +486,7 @@ void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt) void Client::handleCommand_Movement(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); float mad, maa, maf, msw, mscr, msf, mscl, msj, lf, lfs, ls, g; @@ -511,7 +511,7 @@ void Client::handleCommand_Movement(NetworkPacket* pkt) void Client::handleCommand_HP(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); u8 oldhp = player->hp; @@ -532,7 +532,7 @@ void Client::handleCommand_HP(NetworkPacket* pkt) void Client::handleCommand_Breath(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); u16 breath; @@ -544,7 +544,7 @@ void Client::handleCommand_Breath(NetworkPacket* pkt) void Client::handleCommand_MovePlayer(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); v3f pos; @@ -840,7 +840,7 @@ void Client::handleCommand_Privileges(NetworkPacket* pkt) void Client::handleCommand_InventoryFormSpec(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); // Store formspec in LocalPlayer @@ -1098,7 +1098,7 @@ void Client::handleCommand_HudSetFlags(NetworkPacket* pkt) *pkt >> flags >> mask; - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); bool was_minimap_visible = player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE; diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index 6ef768295..554025747 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -1052,7 +1052,7 @@ void Server::handleCommand_ChatMessage(NetworkPacket* pkt) message += (wchar_t)tmp_wchar; } - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -1179,7 +1179,7 @@ void Server::handleCommand_Password(NetworkPacket* pkt) newpwd += c; } - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -1255,7 +1255,7 @@ void Server::handleCommand_PlayerItem(NetworkPacket* pkt) void Server::handleCommand_Respawn(NetworkPacket* pkt) { - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index b58d8e6cd..74b33da37 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -533,7 +533,7 @@ int ObjectRef::l_get_local_animation(lua_State *L) { NO_MAP_LOCK_REQUIRED ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -584,7 +584,7 @@ int ObjectRef::l_get_eye_offset(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it @@ -762,7 +762,7 @@ int ObjectRef::l_is_player(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); lua_pushboolean(L, (player != NULL)); return 1; } @@ -973,7 +973,7 @@ int ObjectRef::l_is_player_connected(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); lua_pushboolean(L, (player != NULL && player->peer_id != 0)); return 1; } @@ -983,7 +983,7 @@ int ObjectRef::l_get_player_name(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) { lua_pushlstring(L, "", 0); return 1; @@ -998,7 +998,7 @@ int ObjectRef::l_get_player_velocity(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) { lua_pushnil(L); return 1; @@ -1013,7 +1013,7 @@ int ObjectRef::l_get_look_dir(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it float pitch = player->getRadPitchDep(); @@ -1033,7 +1033,7 @@ int ObjectRef::l_get_look_pitch(lua_State *L) "Deprecated call to get_look_pitch, use get_look_vertical instead"); ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it lua_pushnumber(L, player->getRadPitchDep()); @@ -1050,7 +1050,7 @@ int ObjectRef::l_get_look_yaw(lua_State *L) "Deprecated call to get_look_yaw, use get_look_horizontal instead"); ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it lua_pushnumber(L, player->getRadYawDep()); @@ -1062,7 +1062,7 @@ int ObjectRef::l_get_look_vertical(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it lua_pushnumber(L, player->getRadPitch()); @@ -1074,7 +1074,7 @@ int ObjectRef::l_get_look_horizontal(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it lua_pushnumber(L, player->getRadYaw()); @@ -1179,7 +1179,7 @@ int ObjectRef::l_set_inventory_formspec(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; std::string formspec = luaL_checkstring(L, 2); @@ -1194,7 +1194,7 @@ int ObjectRef::l_get_inventory_formspec(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; std::string formspec = player->inventory_formspec; @@ -1207,7 +1207,7 @@ int ObjectRef::l_get_player_control(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) { lua_pushlstring(L, "", 0); return 1; @@ -1241,7 +1241,7 @@ int ObjectRef::l_get_player_control_bits(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) { lua_pushlstring(L, "", 0); return 1; @@ -1416,7 +1416,7 @@ int ObjectRef::l_hud_get(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1493,7 +1493,7 @@ int ObjectRef::l_hud_get_flags(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1649,7 +1649,7 @@ int ObjectRef::l_get_sky(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; video::SColor bgcolor(255, 255, 255, 255); diff --git a/src/server.cpp b/src/server.cpp index dee8a3d70..67dbe1545 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -692,7 +692,7 @@ void Server::AsyncRunStep(bool initial_step) if (client->getState() < CS_DefinitionsSent) continue; - Player *player = m_env->getPlayer(client->peer_id); + RemotePlayer *player = m_env->getPlayer(client->peer_id); if (player == NULL) { // This can happen if the client timeouts somehow /*warningstream<getPlayer(peer_id); + RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); NetworkPacket pkt(TOCLIENT_MOVE_PLAYER, sizeof(v3f) + sizeof(f32) * 2, peer_id); @@ -1896,7 +1896,7 @@ void Server::SendEyeOffset(u16 peer_id, v3f first, v3f third) } void Server::SendPlayerPrivileges(u16 peer_id) { - Player *player = m_env->getPlayer(peer_id); + RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); if(player->peer_id == PEER_ID_INEXISTENT) return; @@ -1917,7 +1917,7 @@ void Server::SendPlayerPrivileges(u16 peer_id) void Server::SendPlayerInventoryFormspec(u16 peer_id) { - Player *player = m_env->getPlayer(peer_id); + RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); if(player->peer_id == PEER_ID_INEXISTENT) return; @@ -1962,7 +1962,7 @@ s32 Server::playSound(const SimpleSoundSpec &spec, std::vector dst_clients; if(params.to_player != "") { - Player *player = m_env->getPlayer(params.to_player.c_str()); + RemotePlayer *player = m_env->getPlayer(params.to_player.c_str()); if(!player){ infostream<<"Server::playSound: Player \""< clients = m_clients.getClientIDs(); - for(std::vector::iterator - i = clients.begin(); i != clients.end(); ++i) { - Player *player = m_env->getPlayer(*i); - if(!player) + for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { + RemotePlayer *player = m_env->getPlayer(*i); + if (!player) continue; - if(pos_exists) { + if (pos_exists) { if(player->getPosition().getDistanceFrom(pos) > params.max_hear_distance) continue; @@ -2048,7 +2047,7 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id, i != clients.end(); ++i) { if (far_players) { // Get player - if(Player *player = m_env->getPlayer(*i)) { + if (RemotePlayer *player = m_env->getPlayer(*i)) { // If player is far away, only set modified blocks not sent v3f player_pos = player->getPosition(); if(player_pos.getDistanceFrom(p_f) > maxd) { @@ -2076,7 +2075,7 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id, if(far_players) { // Get player - if(Player *player = m_env->getPlayer(*i)) { + if (RemotePlayer *player = m_env->getPlayer(*i)) { // If player is far away, only set modified blocks not sent v3f player_pos = player->getPosition(); if(player_pos.getDistanceFrom(p_f) > maxd) { @@ -2661,8 +2660,8 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason) for(std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { // Get player - Player *player = m_env->getPlayer(*i); - if(!player) + RemotePlayer *player = m_env->getPlayer(*i); + if (!player) continue; // Get name of player @@ -2842,8 +2841,8 @@ RemoteClient* Server::getClientNoEx(u16 peer_id, ClientState state_min) std::string Server::getPlayerName(u16 peer_id) { - Player *player = m_env->getPlayer(peer_id); - if(player == NULL) + RemotePlayer *player = m_env->getPlayer(peer_id); + if (player == NULL) return "[id="+itos(peer_id)+"]"; return player->getName(); } @@ -2870,13 +2869,12 @@ std::wstring Server::getStatusString() bool first = true; os< clients = m_clients.getClientIDs(); - for(std::vector::iterator i = clients.begin(); - i != clients.end(); ++i) { + for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { // Get player - Player *player = m_env->getPlayer(*i); + RemotePlayer *player = m_env->getPlayer(*i); // Get name of player std::wstring name = L"unknown"; - if(player != NULL) + if (player != NULL) name = narrow_to_wide(player->getName()); // Add name to information string if(!first) @@ -2912,12 +2910,11 @@ void Server::reportPrivsModified(const std::string &name) std::vector clients = m_clients.getClientIDs(); for(std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { - Player *player = m_env->getPlayer(*i); + RemotePlayer *player = m_env->getPlayer(*i); reportPrivsModified(player->getName()); } } else { - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(name.c_str())); + RemotePlayer *player = m_env->getPlayer(name.c_str()); if (!player) return; SendPlayerPrivileges(player->peer_id); @@ -2932,8 +2929,8 @@ void Server::reportPrivsModified(const std::string &name) void Server::reportInventoryFormspecModified(const std::string &name) { - Player *player = m_env->getPlayer(name.c_str()); - if(!player) + RemotePlayer *player = m_env->getPlayer(name.c_str()); + if (!player) return; SendPlayerInventoryFormspec(player->peer_id); } @@ -2963,7 +2960,7 @@ void Server::notifyPlayer(const char *name, const std::wstring &msg) m_admin_chat->outgoing_queue.push_back(new ChatEventChat("", msg)); } - Player *player = m_env->getPlayer(name); + RemotePlayer *player = m_env->getPlayer(name); if (!player) { return; } @@ -2981,7 +2978,7 @@ bool Server::showFormspec(const char *playername, const std::string &formspec, if (!m_env) return false; - Player *player = m_env->getPlayer(playername); + RemotePlayer *player = m_env->getPlayer(playername); if (!player) return false; @@ -3152,7 +3149,7 @@ void Server::spawnParticle(const std::string &playername, v3f pos, u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - Player* player = m_env->getPlayer(playername.c_str()); + RemotePlayer* player = m_env->getPlayer(playername.c_str()); if (!player) return; peer_id = player->peer_id; @@ -3176,7 +3173,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - Player* player = m_env->getPlayer(playername.c_str()); + RemotePlayer* player = m_env->getPlayer(playername.c_str()); if (!player) return -1; peer_id = player->peer_id; @@ -3199,7 +3196,7 @@ void Server::deleteParticleSpawner(const std::string &playername, u32 id) u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - Player* player = m_env->getPlayer(playername.c_str()); + RemotePlayer* player = m_env->getPlayer(playername.c_str()); if (!player) return; peer_id = player->peer_id; -- cgit v1.2.3 From 7bbd716426bf989bf071e2322a9b797cc5f78acb Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 8 Oct 2016 17:56:38 +0200 Subject: RemotePlayer/LocalPlayer Player base class proper separation (code cleanup) (patch 3 of X) * remove IGameDef from Player class, only LocalPlayer has it now * move many attributes/functions only used by LocalPlayer from Player to LocalPlayer * move many attributes/functions only used by RemotePlayer from Player to RemotePlayer * make some functions const * hudGetHotbarSelectedImage now returns const ref * RemotePlayer getHotbarSelectedImage now returns const ref * various code style fixes --- src/environment.cpp | 22 ++-- src/environment.h | 2 +- src/localplayer.cpp | 24 +++- src/localplayer.h | 24 +++- src/player.cpp | 176 ++++++++++++-------------- src/player.h | 270 ++++++++++++++++------------------------ src/script/lua_api/l_object.cpp | 2 +- src/server.cpp | 22 +--- src/server.h | 5 +- 9 files changed, 255 insertions(+), 292 deletions(-) (limited to 'src/script') diff --git a/src/environment.cpp b/src/environment.cpp index bc246f66c..514aa918a 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -603,8 +603,9 @@ void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason, { for (std::vector::iterator it = m_players.begin(); it != m_players.end(); ++it) { - ((Server*)m_gamedef)->DenyAccessVerCompliant((*it)->peer_id, - (*it)->protocol_version, reason, str_reason, reconnect); + RemotePlayer *player = dynamic_cast(*it); + ((Server*)m_gamedef)->DenyAccessVerCompliant(player->peer_id, + player->protocol_version, reason, str_reason, reconnect); } } @@ -618,7 +619,7 @@ void ServerEnvironment::saveLoadedPlayers() ++it) { RemotePlayer *player = static_cast(*it); if (player->checkModified()) { - player->save(players_path); + player->save(players_path, m_gamedef); } } } @@ -628,7 +629,7 @@ void ServerEnvironment::savePlayer(RemotePlayer *player) std::string players_path = m_path_world + DIR_DELIM "players"; fs::CreateDir(players_path); - player->save(players_path); + player->save(players_path, m_gamedef); } RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) @@ -640,7 +641,7 @@ RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) RemotePlayer *player = getPlayer(playername.c_str()); if (!player) { - player = new RemotePlayer(m_gamedef, ""); + player = new RemotePlayer("", m_gamedef->idef()); newplayer = true; } @@ -2300,15 +2301,14 @@ LocalPlayer *ClientEnvironment::getPlayer(const char* name) return dynamic_cast(Environment::getPlayer(name)); } -void ClientEnvironment::addPlayer(Player *player) +void ClientEnvironment::addPlayer(LocalPlayer *player) { DSTACK(FUNCTION_NAME); /* - It is a failure if player is local and there already is a local - player + It is a failure if already is a local player */ - FATAL_ERROR_IF(player->isLocal() && getLocalPlayer() != NULL, - "Player is local but there is already a local player"); + FATAL_ERROR_IF(getLocalPlayer() != NULL, + "Player is local but there is already a local player"); Environment::addPlayer(player); } @@ -2563,7 +2563,7 @@ void ClientEnvironment::step(float dtime) */ for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { - LocalPlayer *player = dynamic_cast(*i); + Player *player = *i; assert(player); /* diff --git a/src/environment.h b/src/environment.h index 99066c367..7ed38ad5d 100644 --- a/src/environment.h +++ b/src/environment.h @@ -579,7 +579,7 @@ public: void step(f32 dtime); - virtual void addPlayer(Player *player); + virtual void addPlayer(LocalPlayer *player); LocalPlayer * getLocalPlayer(); /* diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 732ca8acf..bc242a59d 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -26,16 +26,29 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "settings.h" #include "environment.h" #include "map.h" -#include "util/numeric.h" +#include "client.h" /* LocalPlayer */ -LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name): - Player(gamedef, name), +LocalPlayer::LocalPlayer(Client *gamedef, const char *name): + Player(name, gamedef->idef()), parent(0), + got_teleported(false), isAttached(false), + touching_ground(false), + in_liquid(false), + in_liquid_stable(false), + liquid_viscosity(0), + is_climbing(false), + swimming_vertical(false), + // Movement overrides are multipliers and must be 1 by default + physics_override_speed(1.0f), + physics_override_jump(1.0f), + physics_override_gravity(1.0f), + physics_override_sneak(true), + physics_override_sneak_glitch(true), overridePosition(v3f(0,0,0)), last_position(v3f(0,0,0)), last_speed(v3f(0,0,0)), @@ -47,6 +60,8 @@ LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name): hotbar_image(""), hotbar_selected_image(""), light_color(255,255,255,255), + hurt_tilt_timer(0.0f), + hurt_tilt_strength(0.0f), m_sneak_node(32767,32767,32767), m_sneak_node_exists(false), m_need_to_get_new_sneak_node(true), @@ -54,7 +69,8 @@ LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name): m_old_node_below(32767,32767,32767), m_old_node_below_type("air"), m_can_jump(false), - m_cao(NULL) + m_cao(NULL), + m_gamedef(gamedef) { // Initialize hp to 0, so that no hearts will be shown if server // doesn't support health points diff --git a/src/localplayer.h b/src/localplayer.h index 8897adc5e..182b51d4d 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -21,8 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #define LOCALPLAYER_HEADER #include "player.h" +#include "environment.h" #include +class Client; class Environment; class GenericCAO; class ClientActiveObject; @@ -32,7 +34,7 @@ enum LocalPlayerAnimations {NO_ANIM, WALK_ANIM, DIG_ANIM, WD_ANIM}; // no local class LocalPlayer : public Player { public: - LocalPlayer(IGameDef *gamedef, const char *name); + LocalPlayer(Client *gamedef, const char *name); virtual ~LocalPlayer(); bool isLocal() const @@ -42,7 +44,23 @@ public: ClientActiveObject *parent; + bool got_teleported; bool isAttached; + bool touching_ground; + // This oscillates so that the player jumps a bit above the surface + bool in_liquid; + // This is more stable and defines the maximum speed of the player + bool in_liquid_stable; + // Gets the viscosity of water to calculate friction + u8 liquid_viscosity; + bool is_climbing; + bool swimming_vertical; + + float physics_override_speed; + float physics_override_jump; + float physics_override_gravity; + bool physics_override_sneak; + bool physics_override_sneak_glitch; v3f overridePosition; @@ -71,6 +89,9 @@ public: video::SColor light_color; + float hurt_tilt_timer; + float hurt_tilt_strength; + GenericCAO* getCAO() const { return m_cao; } @@ -102,6 +123,7 @@ private: bool m_can_jump; GenericCAO* m_cao; + Client *m_gamedef; }; #endif diff --git a/src/player.cpp b/src/player.cpp index fd72d63b6..c0d367134 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -32,31 +32,19 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" // strlcpy -Player::Player(IGameDef *gamedef, const char *name): - got_teleported(false), - touching_ground(false), - in_liquid(false), - in_liquid_stable(false), - liquid_viscosity(0), - is_climbing(false), - swimming_vertical(false), +Player::Player(const char *name, IItemDefManager *idef): camera_barely_in_ceiling(false), - inventory(gamedef->idef()), + inventory(idef), hp(PLAYER_MAX_HP), - hurt_tilt_timer(0), - hurt_tilt_strength(0), - protocol_version(0), peer_id(PEER_ID_INEXISTENT), keyPressed(0), // protected - m_gamedef(gamedef), m_breath(PLAYER_MAX_BREATH), m_pitch(0), m_yaw(0), m_speed(0,0,0), m_position(0,0,0), - m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30), - m_dirty(false) + m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30) { strlcpy(m_name, name, PLAYERNAME_SIZE); @@ -92,13 +80,6 @@ Player::Player(IGameDef *gamedef, const char *name): movement_gravity = 9.81 * BS; local_animation_speed = 0.0; - // Movement overrides are multipliers and must be 1 by default - physics_override_speed = 1; - physics_override_jump = 1; - physics_override_gravity = 1; - physics_override_sneak = true; - physics_override_sneak_glitch = true; - hud_flags = HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE | HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE | @@ -117,70 +98,6 @@ v3s16 Player::getLightPosition() const return floatToInt(m_position + v3f(0,BS+BS/2,0), BS); } -void Player::serialize(std::ostream &os) -{ - // Utilize a Settings object for storing values - Settings args; - args.setS32("version", 1); - args.set("name", m_name); - //args.set("password", m_password); - args.setFloat("pitch", m_pitch); - args.setFloat("yaw", m_yaw); - args.setV3F("position", m_position); - args.setS32("hp", hp); - args.setS32("breath", m_breath); - - args.writeLines(os); - - os<<"PlayerArgsEnd\n"; - - inventory.serialize(os); -} - -void Player::deSerialize(std::istream &is, std::string playername) -{ - Settings args; - - if (!args.parseConfigLines(is, "PlayerArgsEnd")) { - throw SerializationError("PlayerArgsEnd of player " + - playername + " not found!"); - } - - m_dirty = true; - //args.getS32("version"); // Version field value not used - std::string name = args.get("name"); - strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); - setPitch(args.getFloat("pitch")); - setYaw(args.getFloat("yaw")); - setPosition(args.getV3F("position")); - try{ - hp = args.getS32("hp"); - }catch(SettingNotFoundException &e) { - hp = PLAYER_MAX_HP; - } - try{ - m_breath = args.getS32("breath"); - }catch(SettingNotFoundException &e) { - m_breath = PLAYER_MAX_BREATH; - } - - inventory.deSerialize(is); - - if(inventory.getList("craftpreview") == NULL) { - // Convert players without craftpreview - inventory.addList("craftpreview", 1); - - bool craftresult_is_preview = true; - if(args.exists("craftresult_is_preview")) - craftresult_is_preview = args.getBool("craftresult_is_preview"); - if(craftresult_is_preview) - { - // Clear craftresult - inventory.getList("craftresult")->changeItem(0, ItemStack()); - } - } -} - u32 Player::addHud(HudElement *toadd) { MutexAutoLock lock(m_mutex); @@ -227,17 +144,24 @@ void Player::clearHud() } } +/* + RemotePlayer +*/ // static config cache for remoteplayer bool RemotePlayer::m_setting_cache_loaded = false; float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f; u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0; -RemotePlayer::RemotePlayer(IGameDef *gamedef, const char *name): - Player(gamedef, name), +RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef): + Player(name, idef), + protocol_version(0), m_sao(NULL), + m_dirty(false), m_last_chat_message_sent(time(NULL)), m_chat_message_allowance(5.0f), - m_message_rate_overhead(0) + m_message_rate_overhead(0), + hud_hotbar_image(""), + hud_hotbar_selected_image("") { if (!RemotePlayer::m_setting_cache_loaded) { RemotePlayer::m_setting_chat_message_limit_per_10sec = @@ -260,7 +184,7 @@ RemotePlayer::RemotePlayer(IGameDef *gamedef, const char *name): movement_gravity = g_settings->getFloat("movement_gravity") * BS; } -void RemotePlayer::save(std::string savedir) +void RemotePlayer::save(std::string savedir, IGameDef *gamedef) { /* * We have to open all possible player files in the players directory @@ -269,7 +193,7 @@ void RemotePlayer::save(std::string savedir) */ // A player to deserialize files into to check their names - RemotePlayer testplayer(m_gamedef, ""); + RemotePlayer testplayer("", gamedef->idef()); savedir += DIR_DELIM; std::string path = savedir + m_name; @@ -309,11 +233,75 @@ void RemotePlayer::save(std::string savedir) return; } -/* - RemotePlayer -*/ +void RemotePlayer::deSerialize(std::istream &is, const std::string &playername) +{ + Settings args; + + if (!args.parseConfigLines(is, "PlayerArgsEnd")) { + throw SerializationError("PlayerArgsEnd of player " + + playername + " not found!"); + } + + m_dirty = true; + //args.getS32("version"); // Version field value not used + std::string name = args.get("name"); + strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); + setPitch(args.getFloat("pitch")); + setYaw(args.getFloat("yaw")); + setPosition(args.getV3F("position")); + try{ + hp = args.getS32("hp"); + }catch(SettingNotFoundException &e) { + hp = PLAYER_MAX_HP; + } + try{ + m_breath = args.getS32("breath"); + }catch(SettingNotFoundException &e) { + m_breath = PLAYER_MAX_BREATH; + } + + inventory.deSerialize(is); + + if(inventory.getList("craftpreview") == NULL) { + // Convert players without craftpreview + inventory.addList("craftpreview", 1); + + bool craftresult_is_preview = true; + if(args.exists("craftresult_is_preview")) + craftresult_is_preview = args.getBool("craftresult_is_preview"); + if(craftresult_is_preview) + { + // Clear craftresult + inventory.getList("craftresult")->changeItem(0, ItemStack()); + } + } +} + +void RemotePlayer::serialize(std::ostream &os) +{ + // Utilize a Settings object for storing values + Settings args; + args.setS32("version", 1); + args.set("name", m_name); + //args.set("password", m_password); + args.setFloat("pitch", m_pitch); + args.setFloat("yaw", m_yaw); + args.setV3F("position", m_position); + args.setS32("hp", hp); + args.setS32("breath", m_breath); + + args.writeLines(os); + + os<<"PlayerArgsEnd\n"; + + inventory.serialize(os); +} + void RemotePlayer::setPosition(const v3f &position) { + if (position != m_position) + m_dirty = true; + Player::setPosition(position); if(m_sao) m_sao->setBasePosition(position); diff --git a/src/player.h b/src/player.h index fbd88fc71..1980a86a3 100644 --- a/src/player.h +++ b/src/player.h @@ -112,7 +112,7 @@ class Player { public: - Player(IGameDef *gamedef, const char *name); + Player(const char *name, IItemDefManager *idef); virtual ~Player() = 0; virtual void move(f32 dtime, Environment *env, f32 pos_max_d) @@ -151,80 +151,32 @@ public: virtual void setPosition(const v3f &position) { - if (position != m_position) - m_dirty = true; m_position = position; } - void setPitch(f32 pitch) + virtual void setPitch(f32 pitch) { - if (pitch != m_pitch) - m_dirty = true; m_pitch = pitch; } virtual void setYaw(f32 yaw) { - if (yaw != m_yaw) - m_dirty = true; m_yaw = yaw; } - f32 getPitch() - { - return m_pitch; - } - - f32 getYaw() - { - return m_yaw; - } - - u16 getBreath() - { - return m_breath; - } - - virtual void setBreath(u16 breath) - { - if (breath != m_breath) - m_dirty = true; - m_breath = breath; - } - - // Deprecated - f32 getRadPitchDep() - { - return -1.0 * m_pitch * core::DEGTORAD; - } - - // Deprecated - f32 getRadYawDep() - { - return (m_yaw + 90.) * core::DEGTORAD; - } - - f32 getRadPitch() - { - return m_pitch * core::DEGTORAD; - } + f32 getPitch() const { return m_pitch; } + f32 getYaw() const { return m_yaw; } + u16 getBreath() const { return m_breath; } - f32 getRadYaw() - { - return m_yaw * core::DEGTORAD; - } + virtual void setBreath(u16 breath) { m_breath = breath; } - const char *getName() const - { - return m_name; - } + f32 getRadPitch() const { return m_pitch * core::DEGTORAD; } + f32 getRadYaw() const { return m_yaw * core::DEGTORAD; } + const char *getName() const { return m_name; } + aabb3f getCollisionbox() const { return m_collisionbox; } - aabb3f getCollisionbox() + u32 getFreeHudID() { - return m_collisionbox; - } - - u32 getFreeHudID() { size_t size = hud.size(); for (size_t i = 0; i != size; i++) { if (!hud[i]) @@ -233,41 +185,6 @@ public: return size; } - void setHotbarImage(const std::string &name) - { - hud_hotbar_image = name; - } - - std::string getHotbarImage() - { - return hud_hotbar_image; - } - - void setHotbarSelectedImage(const std::string &name) - { - hud_hotbar_selected_image = name; - } - - std::string getHotbarSelectedImage() { - return hud_hotbar_selected_image; - } - - void setSky(const video::SColor &bgcolor, const std::string &type, - const std::vector ¶ms) - { - m_sky_bgcolor = bgcolor; - m_sky_type = type; - m_sky_params = params; - } - - void getSky(video::SColor *bgcolor, std::string *type, - std::vector *params) - { - *bgcolor = m_sky_bgcolor; - *type = m_sky_type; - *params = m_sky_params; - } - void setLocalAnimations(v2s32 frames[4], float frame_speed) { for (int i = 0; i < 4; i++) @@ -282,49 +199,8 @@ public: *frame_speed = local_animation_speed; } - virtual bool isLocal() const - { - return false; - } + virtual bool isLocal() const { return false; } - virtual void setPlayerSAO(PlayerSAO *sao) - { - FATAL_ERROR("FIXME"); - } - - /* - serialize() writes a bunch of text that can contain - any characters except a '\0', and such an ending that - deSerialize stops reading exactly at the right point. - */ - void serialize(std::ostream &os); - void deSerialize(std::istream &is, std::string playername); - - bool checkModified() const - { - return m_dirty || inventory.checkModified(); - } - - void setModified(const bool x) - { - m_dirty = x; - if (!x) - inventory.setModified(x); - } - - // Use a function, if isDead can be defined by other conditions - bool isDead() { return hp == 0; } - - bool got_teleported; - bool touching_ground; - // This oscillates so that the player jumps a bit above the surface - bool in_liquid; - // This is more stable and defines the maximum speed of the player - bool in_liquid_stable; - // Gets the viscosity of water to calculate friction - u8 liquid_viscosity; - bool is_climbing; - bool swimming_vertical; bool camera_barely_in_ceiling; v3f eye_offset_first; v3f eye_offset_third; @@ -344,21 +220,11 @@ public: f32 movement_liquid_sink; f32 movement_gravity; - float physics_override_speed; - float physics_override_jump; - float physics_override_gravity; - bool physics_override_sneak; - bool physics_override_sneak_glitch; - v2s32 local_animations[4]; float local_animation_speed; u16 hp; - float hurt_tilt_timer; - float hurt_tilt_strength; - - u16 protocol_version; u16 peer_id; std::string inventory_formspec; @@ -368,7 +234,6 @@ public: u32 keyPressed; - HudElement* getHud(u32 id); u32 addHud(HudElement* hud); HudElement* removeHud(u32 id); @@ -376,11 +241,7 @@ public: u32 hud_flags; s32 hud_hotbar_itemcount; - std::string hud_hotbar_image; - std::string hud_hotbar_selected_image; protected: - IGameDef *m_gamedef; - char m_name[PLAYERNAME_SIZE]; u16 m_breath; f32 m_pitch; @@ -389,13 +250,7 @@ protected: v3f m_position; aabb3f m_collisionbox; - bool m_dirty; - std::vector hud; - - std::string m_sky_type; - video::SColor m_sky_bgcolor; - std::vector m_sky_params; private: // Protect some critical areas // hud for example can be modified by EmergeThread @@ -414,15 +269,14 @@ enum RemotePlayerChatResult { class RemotePlayer : public Player { public: - RemotePlayer(IGameDef *gamedef, const char *name); + RemotePlayer(const char *name, IItemDefManager *idef); virtual ~RemotePlayer() {} - void save(std::string savedir); + void save(std::string savedir, IGameDef *gamedef); + void deSerialize(std::istream &is, const std::string &playername); - PlayerSAO *getPlayerSAO() - { return m_sao; } - void setPlayerSAO(PlayerSAO *sao) - { m_sao = sao; } + PlayerSAO *getPlayerSAO() { return m_sao; } + void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; } void setPosition(const v3f &position); const RemotePlayerChatResult canSendChatMessage(); @@ -446,8 +300,92 @@ public: *ratio = m_day_night_ratio; } + // Use a function, if isDead can be defined by other conditions + bool isDead() const { return hp == 0; } + + void setHotbarImage(const std::string &name) + { + hud_hotbar_image = name; + } + + std::string getHotbarImage() const + { + return hud_hotbar_image; + } + + void setHotbarSelectedImage(const std::string &name) + { + hud_hotbar_selected_image = name; + } + + const std::string &getHotbarSelectedImage() const + { + return hud_hotbar_selected_image; + } + + // Deprecated + f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; } + + // Deprecated + f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; } + + void setSky(const video::SColor &bgcolor, const std::string &type, + const std::vector ¶ms) + { + m_sky_bgcolor = bgcolor; + m_sky_type = type; + m_sky_params = params; + } + + void getSky(video::SColor *bgcolor, std::string *type, + std::vector *params) + { + *bgcolor = m_sky_bgcolor; + *type = m_sky_type; + *params = m_sky_params; + } + + bool checkModified() const { return m_dirty || inventory.checkModified(); } + + void setModified(const bool x) + { + m_dirty = x; + if (!x) + inventory.setModified(x); + } + + virtual void setBreath(u16 breath) + { + if (breath != m_breath) + m_dirty = true; + Player::setBreath(breath); + } + + virtual void setPitch(f32 pitch) + { + if (pitch != m_pitch) + m_dirty = true; + Player::setPitch(pitch); + } + + virtual void setYaw(f32 yaw) + { + if (yaw != m_yaw) + m_dirty = true; + Player::setYaw(yaw); + } + + u16 protocol_version; private: + /* + serialize() writes a bunch of text that can contain + any characters except a '\0', and such an ending that + deSerialize stops reading exactly at the right point. + */ + void serialize(std::ostream &os); + PlayerSAO *m_sao; + bool m_dirty; static bool m_setting_cache_loaded; static float m_setting_chat_message_limit_per_10sec; @@ -459,6 +397,12 @@ private: bool m_day_night_ratio_do_override; float m_day_night_ratio; + std::string hud_hotbar_image; + std::string hud_hotbar_selected_image; + + std::string m_sky_type; + video::SColor m_sky_bgcolor; + std::vector m_sky_params; }; #endif diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 74b33da37..a1f83919c 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1600,7 +1600,7 @@ int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L) if (player == NULL) return 0; - std::string name = getServer(L)->hudGetHotbarSelectedImage(player); + const std::string &name = getServer(L)->hudGetHotbarSelectedImage(player); lua_pushlstring(L, name.c_str(), name.size()); return 1; } diff --git a/src/server.cpp b/src/server.cpp index edd97e225..71e71f43e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1108,7 +1108,7 @@ PlayerSAO* Server::StageTwoClientInit(u16 peer_id) SendPlayerBreath(peer_id); // Show death screen if necessary - if(player->isDead()) + if (player->isDead()) SendDeathscreen(peer_id, false, v3f(0,0,0)); // Note things in chat if not in simple singleplayer mode @@ -3080,14 +3080,6 @@ void Server::hudSetHotbarSelectedImage(RemotePlayer *player, std::string name) SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_SELECTED_IMAGE, name); } -std::string Server::hudGetHotbarSelectedImage(RemotePlayer *player) -{ - if (!player) - return ""; - - return player->getHotbarSelectedImage(); -} - bool Server::setLocalPlayerAnimations(RemotePlayer *player, v2s32 animation_frames[4], f32 frame_speed) { @@ -3408,11 +3400,10 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id, u16 proto_version /* Try to get an existing player */ - RemotePlayer *player = static_cast(m_env->getPlayer(name)); + RemotePlayer *player = m_env->getPlayer(name); // If player is already connected, cancel - if(player != NULL && player->peer_id != 0) - { + if (player != NULL && player->peer_id != 0) { infostream<<"emergePlayer(): Player already connected"<getPlayer(peer_id) != NULL) - { + if (m_env->getPlayer(peer_id) != NULL) { infostream<<"emergePlayer(): Player with wrong name but same" " peer_id already exists"<(m_env->loadPlayer(name)); + player = m_env->loadPlayer(name); } // Create player if it doesn't exist if (!player) { newplayer = true; - player = new RemotePlayer(this, name); + player = new RemotePlayer(name, this->idef()); // Set player position infostream<<"Server: Finding spawn place for player \"" <getHotbarSelectedImage(); + } inline Address getPeerAddress(u16 peer_id) { return m_con.GetPeerAddress(peer_id); } -- cgit v1.2.3 From 569b89b36fff058390cb90458da4285552a9c97e Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 8 Oct 2016 19:08:23 +0200 Subject: Move RemotePlayer code to its own cpp/header --- src/CMakeLists.txt | 1 + src/clientiface.cpp | 2 +- src/content_sao.cpp | 6 +- src/content_sao.h | 5 +- src/environment.cpp | 2 +- src/environment.h | 2 +- src/localplayer.h | 1 + src/player.cpp | 206 ----------------------------------- src/player.h | 149 -------------------------- src/remoteplayer.cpp | 230 ++++++++++++++++++++++++++++++++++++++++ src/remoteplayer.h | 175 ++++++++++++++++++++++++++++++ src/script/lua_api/l_object.cpp | 2 +- src/script/lua_api/l_object.h | 2 +- src/server.cpp | 8 +- src/server.h | 2 +- 15 files changed, 423 insertions(+), 370 deletions(-) create mode 100644 src/remoteplayer.cpp create mode 100644 src/remoteplayer.h (limited to 'src/script') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 753291cba..8e3ae95ab 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -445,6 +445,7 @@ set(common_SRCS porting.cpp profiler.cpp quicktune.cpp + remoteplayer.cpp rollback.cpp rollback_interface.cpp serialization.cpp diff --git a/src/clientiface.cpp b/src/clientiface.cpp index fbfc16770..d78cf1c53 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "clientiface.h" #include "util/numeric.h" #include "util/mathconstants.h" -#include "player.h" +#include "remoteplayer.h" #include "settings.h" #include "mapblock.h" #include "network/connection.h" diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 1664f5993..5d3ed38bc 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serialization.h" // For compressZlib #include "tool.h" // For ToolCapabilities #include "gamedef.h" -#include "player.h" +#include "remoteplayer.h" #include "server.h" #include "scripting_game.h" #include "genericobject.h" @@ -391,7 +391,7 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version) (*ii).second.X, (*ii).second.Y)); // m_bone_position.size } os<::const_iterator ii = m_attachment_child_ids.begin(); + for (UNORDERED_SET::const_iterator ii = m_attachment_child_ids.begin(); (ii != m_attachment_child_ids.end()); ++ii) { if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) { os << serializeLongString(gob_cmd_update_infant(*ii, obj->getSendType(), obj->getClientInitializationData(protocol_version))); @@ -880,7 +880,7 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version) m_physics_override_jump, m_physics_override_gravity, m_physics_override_sneak, m_physics_override_sneak_glitch)); // 5 os << serializeLongString(gob_cmd_update_nametag_attributes(m_prop.nametag_color)); // 6 (GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) : Deprecated, for backwards compatibility only. - for (UNORDERED_SET::const_iterator ii = m_attachment_child_ids.begin(); + for (UNORDERED_SET::const_iterator ii = m_attachment_child_ids.begin(); ii != m_attachment_child_ids.end(); ++ii) { if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) { os << serializeLongString(gob_cmd_update_infant(*ii, obj->getSendType(), obj->getClientInitializationData(protocol_version))); diff --git a/src/content_sao.h b/src/content_sao.h index 341ebb5da..76a3a37da 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -22,7 +22,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serverobject.h" #include "itemgroup.h" -#include "player.h" #include "object_properties.h" /* @@ -157,6 +156,8 @@ public: } }; +class RemotePlayer; + class PlayerSAO : public ServerActiveObject { public: @@ -231,7 +232,7 @@ public: void disconnected(); - RemotePlayer* getPlayer() { return m_player; } + RemotePlayer *getPlayer() { return m_player; } u16 getPeerID() const { return m_peer_id; } // Cheat prevention diff --git a/src/environment.cpp b/src/environment.cpp index 514aa918a..ff43ac516 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -2705,7 +2705,7 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object) } object->setId(new_id); } - if(!isFreeClientActiveObjectId(object->getId(), m_active_objects)) { + if (!isFreeClientActiveObjectId(object->getId(), m_active_objects)) { infostream<<"ClientEnvironment::addActiveObject(): " <<"id is not free ("<getId()<<")"< ¤t_objects, std::queue &removed_objects); diff --git a/src/localplayer.h b/src/localplayer.h index 182b51d4d..9d43128aa 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -28,6 +28,7 @@ class Client; class Environment; class GenericCAO; class ClientActiveObject; +class IGameDef; enum LocalPlayerAnimations {NO_ANIM, WALK_ANIM, DIG_ANIM, WD_ANIM}; // no local animation, walking, digging, both diff --git a/src/player.cpp b/src/player.cpp index c0d367134..fa82a79f4 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -19,15 +19,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "player.h" -#include #include "threading/mutex_auto_lock.h" #include "util/numeric.h" #include "hud.h" #include "constants.h" #include "gamedef.h" #include "settings.h" -#include "content_sao.h" -#include "filesys.h" #include "log.h" #include "porting.h" // strlcpy @@ -143,206 +140,3 @@ void Player::clearHud() hud.pop_back(); } } - -/* - RemotePlayer -*/ -// static config cache for remoteplayer -bool RemotePlayer::m_setting_cache_loaded = false; -float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f; -u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0; - -RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef): - Player(name, idef), - protocol_version(0), - m_sao(NULL), - m_dirty(false), - m_last_chat_message_sent(time(NULL)), - m_chat_message_allowance(5.0f), - m_message_rate_overhead(0), - hud_hotbar_image(""), - hud_hotbar_selected_image("") -{ - if (!RemotePlayer::m_setting_cache_loaded) { - RemotePlayer::m_setting_chat_message_limit_per_10sec = - g_settings->getFloat("chat_message_limit_per_10sec"); - RemotePlayer::m_setting_chat_message_limit_trigger_kick = - g_settings->getU16("chat_message_limit_trigger_kick"); - RemotePlayer::m_setting_cache_loaded = true; - } - movement_acceleration_default = g_settings->getFloat("movement_acceleration_default") * BS; - movement_acceleration_air = g_settings->getFloat("movement_acceleration_air") * BS; - movement_acceleration_fast = g_settings->getFloat("movement_acceleration_fast") * BS; - movement_speed_walk = g_settings->getFloat("movement_speed_walk") * BS; - movement_speed_crouch = g_settings->getFloat("movement_speed_crouch") * BS; - movement_speed_fast = g_settings->getFloat("movement_speed_fast") * BS; - movement_speed_climb = g_settings->getFloat("movement_speed_climb") * BS; - movement_speed_jump = g_settings->getFloat("movement_speed_jump") * BS; - movement_liquid_fluidity = g_settings->getFloat("movement_liquid_fluidity") * BS; - movement_liquid_fluidity_smooth = g_settings->getFloat("movement_liquid_fluidity_smooth") * BS; - movement_liquid_sink = g_settings->getFloat("movement_liquid_sink") * BS; - movement_gravity = g_settings->getFloat("movement_gravity") * BS; -} - -void RemotePlayer::save(std::string savedir, IGameDef *gamedef) -{ - /* - * We have to open all possible player files in the players directory - * and check their player names because some file systems are not - * case-sensitive and player names are case-sensitive. - */ - - // A player to deserialize files into to check their names - RemotePlayer testplayer("", gamedef->idef()); - - savedir += DIR_DELIM; - std::string path = savedir + m_name; - for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) { - if (!fs::PathExists(path)) { - // Open file and serialize - std::ostringstream ss(std::ios_base::binary); - serialize(ss); - if (!fs::safeWriteToFile(path, ss.str())) { - infostream << "Failed to write " << path << std::endl; - } - setModified(false); - return; - } - // Open file and deserialize - std::ifstream is(path.c_str(), std::ios_base::binary); - if (!is.good()) { - infostream << "Failed to open " << path << std::endl; - return; - } - testplayer.deSerialize(is, path); - is.close(); - if (strcmp(testplayer.getName(), m_name) == 0) { - // Open file and serialize - std::ostringstream ss(std::ios_base::binary); - serialize(ss); - if (!fs::safeWriteToFile(path, ss.str())) { - infostream << "Failed to write " << path << std::endl; - } - setModified(false); - return; - } - path = savedir + m_name + itos(i); - } - - infostream << "Didn't find free file for player " << m_name << std::endl; - return; -} - -void RemotePlayer::deSerialize(std::istream &is, const std::string &playername) -{ - Settings args; - - if (!args.parseConfigLines(is, "PlayerArgsEnd")) { - throw SerializationError("PlayerArgsEnd of player " + - playername + " not found!"); - } - - m_dirty = true; - //args.getS32("version"); // Version field value not used - std::string name = args.get("name"); - strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); - setPitch(args.getFloat("pitch")); - setYaw(args.getFloat("yaw")); - setPosition(args.getV3F("position")); - try{ - hp = args.getS32("hp"); - }catch(SettingNotFoundException &e) { - hp = PLAYER_MAX_HP; - } - try{ - m_breath = args.getS32("breath"); - }catch(SettingNotFoundException &e) { - m_breath = PLAYER_MAX_BREATH; - } - - inventory.deSerialize(is); - - if(inventory.getList("craftpreview") == NULL) { - // Convert players without craftpreview - inventory.addList("craftpreview", 1); - - bool craftresult_is_preview = true; - if(args.exists("craftresult_is_preview")) - craftresult_is_preview = args.getBool("craftresult_is_preview"); - if(craftresult_is_preview) - { - // Clear craftresult - inventory.getList("craftresult")->changeItem(0, ItemStack()); - } - } -} - -void RemotePlayer::serialize(std::ostream &os) -{ - // Utilize a Settings object for storing values - Settings args; - args.setS32("version", 1); - args.set("name", m_name); - //args.set("password", m_password); - args.setFloat("pitch", m_pitch); - args.setFloat("yaw", m_yaw); - args.setV3F("position", m_position); - args.setS32("hp", hp); - args.setS32("breath", m_breath); - - args.writeLines(os); - - os<<"PlayerArgsEnd\n"; - - inventory.serialize(os); -} - -void RemotePlayer::setPosition(const v3f &position) -{ - if (position != m_position) - m_dirty = true; - - Player::setPosition(position); - if(m_sao) - m_sao->setBasePosition(position); -} - -const RemotePlayerChatResult RemotePlayer::canSendChatMessage() -{ - // Rate limit messages - u32 now = time(NULL); - float time_passed = now - m_last_chat_message_sent; - m_last_chat_message_sent = now; - - // If this feature is disabled - if (m_setting_chat_message_limit_per_10sec <= 0.0) { - return RPLAYER_CHATRESULT_OK; - } - - m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f); - if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) { - m_chat_message_allowance = m_setting_chat_message_limit_per_10sec; - } - - if (m_chat_message_allowance < 1.0f) { - infostream << "Player " << m_name - << " chat limited due to excessive message amount." << std::endl; - - // Kick player if flooding is too intensive - m_message_rate_overhead++; - if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) { - return RPLAYER_CHATRESULT_KICK; - } - - return RPLAYER_CHATRESULT_FLOODING; - } - - // Reinit message overhead - if (m_message_rate_overhead > 0) { - m_message_rate_overhead = 0; - } - - m_chat_message_allowance -= 1.0f; - return RPLAYER_CHATRESULT_OK; -} - diff --git a/src/player.h b/src/player.h index 1980a86a3..3c945b100 100644 --- a/src/player.h +++ b/src/player.h @@ -99,9 +99,7 @@ struct PlayerControl }; class Map; -class IGameDef; struct CollisionInfo; -class PlayerSAO; struct HudElement; class Environment; @@ -258,152 +256,5 @@ private: Mutex m_mutex; }; -enum RemotePlayerChatResult { - RPLAYER_CHATRESULT_OK, - RPLAYER_CHATRESULT_FLOODING, - RPLAYER_CHATRESULT_KICK, -}; -/* - Player on the server -*/ -class RemotePlayer : public Player -{ -public: - RemotePlayer(const char *name, IItemDefManager *idef); - virtual ~RemotePlayer() {} - - void save(std::string savedir, IGameDef *gamedef); - void deSerialize(std::istream &is, const std::string &playername); - - PlayerSAO *getPlayerSAO() { return m_sao; } - void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; } - void setPosition(const v3f &position); - - const RemotePlayerChatResult canSendChatMessage(); - - void setHotbarItemcount(s32 hotbar_itemcount) - { - hud_hotbar_itemcount = hotbar_itemcount; - } - - s32 getHotbarItemcount() const { return hud_hotbar_itemcount; } - - void overrideDayNightRatio(bool do_override, float ratio) - { - m_day_night_ratio_do_override = do_override; - m_day_night_ratio = ratio; - } - - void getDayNightRatio(bool *do_override, float *ratio) - { - *do_override = m_day_night_ratio_do_override; - *ratio = m_day_night_ratio; - } - - // Use a function, if isDead can be defined by other conditions - bool isDead() const { return hp == 0; } - - void setHotbarImage(const std::string &name) - { - hud_hotbar_image = name; - } - - std::string getHotbarImage() const - { - return hud_hotbar_image; - } - - void setHotbarSelectedImage(const std::string &name) - { - hud_hotbar_selected_image = name; - } - - const std::string &getHotbarSelectedImage() const - { - return hud_hotbar_selected_image; - } - - // Deprecated - f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; } - - // Deprecated - f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; } - - void setSky(const video::SColor &bgcolor, const std::string &type, - const std::vector ¶ms) - { - m_sky_bgcolor = bgcolor; - m_sky_type = type; - m_sky_params = params; - } - - void getSky(video::SColor *bgcolor, std::string *type, - std::vector *params) - { - *bgcolor = m_sky_bgcolor; - *type = m_sky_type; - *params = m_sky_params; - } - - bool checkModified() const { return m_dirty || inventory.checkModified(); } - - void setModified(const bool x) - { - m_dirty = x; - if (!x) - inventory.setModified(x); - } - - virtual void setBreath(u16 breath) - { - if (breath != m_breath) - m_dirty = true; - Player::setBreath(breath); - } - - virtual void setPitch(f32 pitch) - { - if (pitch != m_pitch) - m_dirty = true; - Player::setPitch(pitch); - } - - virtual void setYaw(f32 yaw) - { - if (yaw != m_yaw) - m_dirty = true; - Player::setYaw(yaw); - } - - u16 protocol_version; -private: - /* - serialize() writes a bunch of text that can contain - any characters except a '\0', and such an ending that - deSerialize stops reading exactly at the right point. - */ - void serialize(std::ostream &os); - - PlayerSAO *m_sao; - bool m_dirty; - - static bool m_setting_cache_loaded; - static float m_setting_chat_message_limit_per_10sec; - static u16 m_setting_chat_message_limit_trigger_kick; - - u32 m_last_chat_message_sent; - float m_chat_message_allowance; - u16 m_message_rate_overhead; - - bool m_day_night_ratio_do_override; - float m_day_night_ratio; - std::string hud_hotbar_image; - std::string hud_hotbar_selected_image; - - std::string m_sky_type; - video::SColor m_sky_bgcolor; - std::vector m_sky_params; -}; - #endif diff --git a/src/remoteplayer.cpp b/src/remoteplayer.cpp new file mode 100644 index 000000000..f64d1d690 --- /dev/null +++ b/src/remoteplayer.cpp @@ -0,0 +1,230 @@ +/* +Minetest +Copyright (C) 2010-2016 celeron55, Perttu Ahola +Copyright (C) 2014-2016 nerzhul, Loic Blot + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program 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 Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "remoteplayer.h" +#include "content_sao.h" +#include "filesys.h" +#include "gamedef.h" +#include "porting.h" // strlcpy +#include "settings.h" + + +/* + RemotePlayer +*/ +// static config cache for remoteplayer +bool RemotePlayer::m_setting_cache_loaded = false; +float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f; +u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0; + +RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef): + Player(name, idef), + protocol_version(0), + m_sao(NULL), + m_dirty(false), + m_last_chat_message_sent(time(NULL)), + m_chat_message_allowance(5.0f), + m_message_rate_overhead(0), + hud_hotbar_image(""), + hud_hotbar_selected_image("") +{ + if (!RemotePlayer::m_setting_cache_loaded) { + RemotePlayer::m_setting_chat_message_limit_per_10sec = + g_settings->getFloat("chat_message_limit_per_10sec"); + RemotePlayer::m_setting_chat_message_limit_trigger_kick = + g_settings->getU16("chat_message_limit_trigger_kick"); + RemotePlayer::m_setting_cache_loaded = true; + } + movement_acceleration_default = g_settings->getFloat("movement_acceleration_default") * BS; + movement_acceleration_air = g_settings->getFloat("movement_acceleration_air") * BS; + movement_acceleration_fast = g_settings->getFloat("movement_acceleration_fast") * BS; + movement_speed_walk = g_settings->getFloat("movement_speed_walk") * BS; + movement_speed_crouch = g_settings->getFloat("movement_speed_crouch") * BS; + movement_speed_fast = g_settings->getFloat("movement_speed_fast") * BS; + movement_speed_climb = g_settings->getFloat("movement_speed_climb") * BS; + movement_speed_jump = g_settings->getFloat("movement_speed_jump") * BS; + movement_liquid_fluidity = g_settings->getFloat("movement_liquid_fluidity") * BS; + movement_liquid_fluidity_smooth = g_settings->getFloat("movement_liquid_fluidity_smooth") * BS; + movement_liquid_sink = g_settings->getFloat("movement_liquid_sink") * BS; + movement_gravity = g_settings->getFloat("movement_gravity") * BS; +} + +void RemotePlayer::save(std::string savedir, IGameDef *gamedef) +{ + /* + * We have to open all possible player files in the players directory + * and check their player names because some file systems are not + * case-sensitive and player names are case-sensitive. + */ + + // A player to deserialize files into to check their names + RemotePlayer testplayer("", gamedef->idef()); + + savedir += DIR_DELIM; + std::string path = savedir + m_name; + for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) { + if (!fs::PathExists(path)) { + // Open file and serialize + std::ostringstream ss(std::ios_base::binary); + serialize(ss); + if (!fs::safeWriteToFile(path, ss.str())) { + infostream << "Failed to write " << path << std::endl; + } + setModified(false); + return; + } + // Open file and deserialize + std::ifstream is(path.c_str(), std::ios_base::binary); + if (!is.good()) { + infostream << "Failed to open " << path << std::endl; + return; + } + testplayer.deSerialize(is, path); + is.close(); + if (strcmp(testplayer.getName(), m_name) == 0) { + // Open file and serialize + std::ostringstream ss(std::ios_base::binary); + serialize(ss); + if (!fs::safeWriteToFile(path, ss.str())) { + infostream << "Failed to write " << path << std::endl; + } + setModified(false); + return; + } + path = savedir + m_name + itos(i); + } + + infostream << "Didn't find free file for player " << m_name << std::endl; + return; +} + +void RemotePlayer::deSerialize(std::istream &is, const std::string &playername) +{ + Settings args; + + if (!args.parseConfigLines(is, "PlayerArgsEnd")) { + throw SerializationError("PlayerArgsEnd of player " + + playername + " not found!"); + } + + m_dirty = true; + //args.getS32("version"); // Version field value not used + std::string name = args.get("name"); + strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); + setPitch(args.getFloat("pitch")); + setYaw(args.getFloat("yaw")); + setPosition(args.getV3F("position")); + try { + hp = args.getS32("hp"); + } catch(SettingNotFoundException &e) { + hp = PLAYER_MAX_HP; + } + + try { + m_breath = args.getS32("breath"); + } catch(SettingNotFoundException &e) { + m_breath = PLAYER_MAX_BREATH; + } + + inventory.deSerialize(is); + + if(inventory.getList("craftpreview") == NULL) { + // Convert players without craftpreview + inventory.addList("craftpreview", 1); + + bool craftresult_is_preview = true; + if(args.exists("craftresult_is_preview")) + craftresult_is_preview = args.getBool("craftresult_is_preview"); + if(craftresult_is_preview) + { + // Clear craftresult + inventory.getList("craftresult")->changeItem(0, ItemStack()); + } + } +} + +void RemotePlayer::serialize(std::ostream &os) +{ + // Utilize a Settings object for storing values + Settings args; + args.setS32("version", 1); + args.set("name", m_name); + //args.set("password", m_password); + args.setFloat("pitch", m_pitch); + args.setFloat("yaw", m_yaw); + args.setV3F("position", m_position); + args.setS32("hp", hp); + args.setS32("breath", m_breath); + + args.writeLines(os); + + os<<"PlayerArgsEnd\n"; + + inventory.serialize(os); +} + +void RemotePlayer::setPosition(const v3f &position) +{ + if (position != m_position) + m_dirty = true; + + Player::setPosition(position); + if(m_sao) + m_sao->setBasePosition(position); +} + +const RemotePlayerChatResult RemotePlayer::canSendChatMessage() +{ + // Rate limit messages + u32 now = time(NULL); + float time_passed = now - m_last_chat_message_sent; + m_last_chat_message_sent = now; + + // If this feature is disabled + if (m_setting_chat_message_limit_per_10sec <= 0.0) { + return RPLAYER_CHATRESULT_OK; + } + + m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f); + if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) { + m_chat_message_allowance = m_setting_chat_message_limit_per_10sec; + } + + if (m_chat_message_allowance < 1.0f) { + infostream << "Player " << m_name + << " chat limited due to excessive message amount." << std::endl; + + // Kick player if flooding is too intensive + m_message_rate_overhead++; + if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) { + return RPLAYER_CHATRESULT_KICK; + } + + return RPLAYER_CHATRESULT_FLOODING; + } + + // Reinit message overhead + if (m_message_rate_overhead > 0) { + m_message_rate_overhead = 0; + } + + m_chat_message_allowance -= 1.0f; + return RPLAYER_CHATRESULT_OK; +} diff --git a/src/remoteplayer.h b/src/remoteplayer.h new file mode 100644 index 000000000..f6c70b0e9 --- /dev/null +++ b/src/remoteplayer.h @@ -0,0 +1,175 @@ +/* +Minetest +Copyright (C) 2010-2016 celeron55, Perttu Ahola +Copyright (C) 2014-2016 nerzhul, Loic Blot + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program 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 Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef REMOTEPLAYER_HEADER +#define REMOTEPLAYER_HEADER + +#include "player.h" + +class PlayerSAO; + +enum RemotePlayerChatResult { + RPLAYER_CHATRESULT_OK, + RPLAYER_CHATRESULT_FLOODING, + RPLAYER_CHATRESULT_KICK, +}; +/* + Player on the server +*/ +class RemotePlayer : public Player +{ +public: + RemotePlayer(const char *name, IItemDefManager *idef); + virtual ~RemotePlayer() {} + + void save(std::string savedir, IGameDef *gamedef); + void deSerialize(std::istream &is, const std::string &playername); + + PlayerSAO *getPlayerSAO() { return m_sao; } + void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; } + void setPosition(const v3f &position); + + const RemotePlayerChatResult canSendChatMessage(); + + void setHotbarItemcount(s32 hotbar_itemcount) + { + hud_hotbar_itemcount = hotbar_itemcount; + } + + s32 getHotbarItemcount() const { return hud_hotbar_itemcount; } + + void overrideDayNightRatio(bool do_override, float ratio) + { + m_day_night_ratio_do_override = do_override; + m_day_night_ratio = ratio; + } + + void getDayNightRatio(bool *do_override, float *ratio) + { + *do_override = m_day_night_ratio_do_override; + *ratio = m_day_night_ratio; + } + + // Use a function, if isDead can be defined by other conditions + bool isDead() const { return hp == 0; } + + void setHotbarImage(const std::string &name) + { + hud_hotbar_image = name; + } + + std::string getHotbarImage() const + { + return hud_hotbar_image; + } + + void setHotbarSelectedImage(const std::string &name) + { + hud_hotbar_selected_image = name; + } + + const std::string &getHotbarSelectedImage() const + { + return hud_hotbar_selected_image; + } + + // Deprecated + f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; } + + // Deprecated + f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; } + + void setSky(const video::SColor &bgcolor, const std::string &type, + const std::vector ¶ms) + { + m_sky_bgcolor = bgcolor; + m_sky_type = type; + m_sky_params = params; + } + + void getSky(video::SColor *bgcolor, std::string *type, + std::vector *params) + { + *bgcolor = m_sky_bgcolor; + *type = m_sky_type; + *params = m_sky_params; + } + + bool checkModified() const { return m_dirty || inventory.checkModified(); } + + void setModified(const bool x) + { + m_dirty = x; + if (!x) + inventory.setModified(x); + } + + virtual void setBreath(u16 breath) + { + if (breath != m_breath) + m_dirty = true; + Player::setBreath(breath); + } + + virtual void setPitch(f32 pitch) + { + if (pitch != m_pitch) + m_dirty = true; + Player::setPitch(pitch); + } + + virtual void setYaw(f32 yaw) + { + if (yaw != m_yaw) + m_dirty = true; + Player::setYaw(yaw); + } + + u16 protocol_version; +private: + /* + serialize() writes a bunch of text that can contain + any characters except a '\0', and such an ending that + deSerialize stops reading exactly at the right point. + */ + void serialize(std::ostream &os); + + PlayerSAO *m_sao; + bool m_dirty; + + static bool m_setting_cache_loaded; + static float m_setting_chat_message_limit_per_10sec; + static u16 m_setting_chat_message_limit_trigger_kick; + + u32 m_last_chat_message_sent; + float m_chat_message_allowance; + u16 m_message_rate_overhead; + + bool m_day_night_ratio_do_override; + float m_day_night_ratio; + std::string hud_hotbar_image; + std::string hud_hotbar_selected_image; + + std::string m_sky_type; + video::SColor m_sky_bgcolor; + std::vector m_sky_params; +}; + +#endif diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index a1f83919c..bb352e429 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -107,7 +107,7 @@ PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref) return (PlayerSAO*)obj; } -RemotePlayer* ObjectRef::getplayer(ObjectRef *ref) +RemotePlayer *ObjectRef::getplayer(ObjectRef *ref) { PlayerSAO *playersao = getplayersao(ref); if (playersao == NULL) diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h index dfc1b49d2..09f10e417 100644 --- a/src/script/lua_api/l_object.h +++ b/src/script/lua_api/l_object.h @@ -47,7 +47,7 @@ private: static PlayerSAO* getplayersao(ObjectRef *ref); - static RemotePlayer* getplayer(ObjectRef *ref); + static RemotePlayer *getplayer(ObjectRef *ref); // Exported functions diff --git a/src/server.cpp b/src/server.cpp index 71e71f43e..a93c143c7 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2688,7 +2688,7 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason) SendChatMessage(PEER_ID_INEXISTENT,message); } -void Server::UpdateCrafting(RemotePlayer* player) +void Server::UpdateCrafting(RemotePlayer *player) { DSTACK(FUNCTION_NAME); @@ -3141,7 +3141,7 @@ void Server::spawnParticle(const std::string &playername, v3f pos, u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - RemotePlayer* player = m_env->getPlayer(playername.c_str()); + RemotePlayer *player = m_env->getPlayer(playername.c_str()); if (!player) return; peer_id = player->peer_id; @@ -3165,7 +3165,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - RemotePlayer* player = m_env->getPlayer(playername.c_str()); + RemotePlayer *player = m_env->getPlayer(playername.c_str()); if (!player) return -1; peer_id = player->peer_id; @@ -3188,7 +3188,7 @@ void Server::deleteParticleSpawner(const std::string &playername, u32 id) u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - RemotePlayer* player = m_env->getPlayer(playername.c_str()); + RemotePlayer *player = m_env->getPlayer(playername.c_str()); if (!player) return; peer_id = player->peer_id; diff --git a/src/server.h b/src/server.h index fc4758c5f..6ee61a0eb 100644 --- a/src/server.h +++ b/src/server.h @@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "environment.h" #include "chat_interface.h" #include "clientiface.h" -#include "player.h" +#include "remoteplayer.h" #include "network/networkpacket.h" #include #include -- cgit v1.2.3 From c9e7a27eeb628be78a835abadf8afe1177eb90c5 Mon Sep 17 00:00:00 2001 From: raymoo Date: Thu, 4 Aug 2016 13:09:21 -0700 Subject: Attached particle spawners --- doc/lua_api.txt | 2 + src/client.h | 1 + src/content_sao.cpp | 10 ++++- src/environment.cpp | 24 +++++++++++ src/environment.h | 4 +- src/network/clientpackethandler.cpp | 3 ++ src/particles.cpp | 81 ++++++++++++++++++++++++------------- src/particles.h | 3 +- src/script/lua_api/l_particles.cpp | 11 +++++ src/server.cpp | 17 ++++++-- src/server.h | 2 + src/serverobject.cpp | 1 - src/serverobject.h | 10 +++++ 13 files changed, 132 insertions(+), 37 deletions(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 6bdcd4fe4..a1d598e7d 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -4120,6 +4120,8 @@ The Biome API is still in an experimental phase and subject to change. collision_removal = false, -- ^ collision_removal: if true then particle is removed when it collides, -- ^ requires collisiondetection = true to have any effect + attached = ObjectRef, + -- ^ attached: if defined, makes particle positions relative to this object. vertical = false, -- ^ vertical: if true faces player using y axis only texture = "image.png", diff --git a/src/client.h b/src/client.h index 6d24c0b1d..9f5bda059 100644 --- a/src/client.h +++ b/src/client.h @@ -201,6 +201,7 @@ struct ClientEvent f32 maxsize; bool collisiondetection; bool collision_removal; + u16 attached_id; bool vertical; std::string *texture; u32 id; diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 5d3ed38bc..375a43c90 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -156,6 +156,11 @@ LuaEntitySAO::~LuaEntitySAO() if(m_registered){ m_env->getScriptIface()->luaentity_Remove(m_id); } + + for (UNORDERED_SET::iterator it = m_attached_particle_spawners.begin(); + it != m_attached_particle_spawners.end(); ++it) { + m_env->deleteParticleSpawner(*it, false); + } } void LuaEntitySAO::addedToEnvironment(u32 dtime_s) @@ -817,7 +822,6 @@ PlayerSAO::~PlayerSAO() { if(m_inventory != &m_player->inventory) delete m_inventory; - } std::string PlayerSAO::getDescription() @@ -844,6 +848,10 @@ void PlayerSAO::removingFromEnvironment() m_player->peer_id = 0; m_env->savePlayer(m_player); m_env->removePlayer(m_player); + for (UNORDERED_SET::iterator it = m_attached_particle_spawners.begin(); + it != m_attached_particle_spawners.end(); ++it) { + m_env->deleteParticleSpawner(*it, false); + } } } diff --git a/src/environment.cpp b/src/environment.cpp index ceaa01d7a..ceaf40d89 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -1518,6 +1518,30 @@ u32 ServerEnvironment::addParticleSpawner(float exptime) return id; } +u32 ServerEnvironment::addParticleSpawner(float exptime, u16 attached_id) +{ + u32 id = addParticleSpawner(exptime); + m_particle_spawner_attachments[id] = attached_id; + if (ServerActiveObject *obj = getActiveObject(attached_id)) { + obj->attachParticleSpawner(id); + } + return id; +} + +void ServerEnvironment::deleteParticleSpawner(u32 id, bool remove_from_object) +{ + m_particle_spawners.erase(id); + UNORDERED_MAP::iterator it = m_particle_spawner_attachments.find(id); + if (it != m_particle_spawner_attachments.end()) { + u16 obj_id = (*it).second; + ServerActiveObject *sao = getActiveObject(obj_id); + if (sao != NULL && remove_from_object) { + sao->detachParticleSpawner(id); + } + m_particle_spawner_attachments.erase(id); + } +} + ServerActiveObject* ServerEnvironment::getActiveObject(u16 id) { ActiveObjectMap::iterator n = m_active_objects.find(id); diff --git a/src/environment.h b/src/environment.h index 83ad69562..3f3c1cf2c 100644 --- a/src/environment.h +++ b/src/environment.h @@ -329,7 +329,8 @@ public: void loadDefaultMeta(); u32 addParticleSpawner(float exptime); - void deleteParticleSpawner(u32 id) { m_particle_spawners.erase(id); } + u32 addParticleSpawner(float exptime, u16 attached_id); + void deleteParticleSpawner(u32 id, bool remove_from_object = true); /* External ActiveObject interface @@ -519,6 +520,7 @@ private: // Particles IntervalLimiter m_particle_management_interval; UNORDERED_MAP m_particle_spawners; + UNORDERED_MAP m_particle_spawner_attachments; }; #ifndef SERVER diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index b39356e92..090741f9f 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -944,9 +944,11 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) bool vertical = false; bool collision_removal = false; + u16 attached_id = 0; try { *pkt >> vertical; *pkt >> collision_removal; + *pkt >> attached_id; } catch (...) {} @@ -966,6 +968,7 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) event.add_particlespawner.maxsize = maxsize; event.add_particlespawner.collisiondetection = collisiondetection; event.add_particlespawner.collision_removal = collision_removal; + event.add_particlespawner.attached_id = attached_id; event.add_particlespawner.vertical = vertical; event.add_particlespawner.texture = new std::string(texture); event.add_particlespawner.id = id; diff --git a/src/particles.cpp b/src/particles.cpp index ccca691d1..2efee6ada 100644 --- a/src/particles.cpp +++ b/src/particles.cpp @@ -213,7 +213,7 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, u16 amount, float time, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, - bool collisiondetection, bool collision_removal, bool vertical, + bool collisiondetection, bool collision_removal, u16 attached_id, bool vertical, video::ITexture *texture, u32 id, ParticleManager *p_manager) : m_particlemanager(p_manager) { @@ -234,6 +234,7 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, m_maxsize = maxsize; m_collisiondetection = collisiondetection; m_collision_removal = collision_removal; + m_attached_id = attached_id; m_vertical = vertical; m_texture = texture; m_time = 0; @@ -251,6 +252,15 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) { m_time += dtime; + bool unloaded = false; + v3f attached_offset = v3f(0,0,0); + if (m_attached_id != 0) { + if (ClientActiveObject *attached = env->getActiveObject(m_attached_id)) + attached_offset = attached->getPosition() / BS; + else + unloaded = true; + } + if (m_spawntime != 0) // Spawner exists for a predefined timespan { for(std::vector::iterator i = m_spawntimes.begin(); @@ -260,33 +270,41 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) { m_amount--; - v3f pos = random_v3f(m_minpos, m_maxpos); - v3f vel = random_v3f(m_minvel, m_maxvel); - v3f acc = random_v3f(m_minacc, m_maxacc); - float exptime = rand()/(float)RAND_MAX - *(m_maxexptime-m_minexptime) - +m_minexptime; - float size = rand()/(float)RAND_MAX - *(m_maxsize-m_minsize) - +m_minsize; - - Particle* toadd = new Particle( - m_gamedef, - m_smgr, - m_player, - env, - pos, - vel, - acc, - exptime, - size, - m_collisiondetection, - m_collision_removal, - m_vertical, - m_texture, - v2f(0.0, 0.0), - v2f(1.0, 1.0)); - m_particlemanager->addParticle(toadd); + // Pretend to, but don't actually spawn a + // particle if it is attached to an unloaded + // object. + if (!unloaded) { + v3f pos = random_v3f(m_minpos, m_maxpos) + + attached_offset; + v3f vel = random_v3f(m_minvel, m_maxvel); + v3f acc = random_v3f(m_minacc, m_maxacc); + // Make relative to offest + pos += attached_offset; + float exptime = rand()/(float)RAND_MAX + *(m_maxexptime-m_minexptime) + +m_minexptime; + float size = rand()/(float)RAND_MAX + *(m_maxsize-m_minsize) + +m_minsize; + + Particle* toadd = new Particle( + m_gamedef, + m_smgr, + m_player, + env, + pos, + vel, + acc, + exptime, + size, + m_collisiondetection, + m_collision_removal, + m_vertical, + m_texture, + v2f(0.0, 0.0), + v2f(1.0, 1.0)); + m_particlemanager->addParticle(toadd); + } i = m_spawntimes.erase(i); } else @@ -297,11 +315,15 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) } else // Spawner exists for an infinity timespan, spawn on a per-second base { + // Skip this step if attached to an unloaded object + if (unloaded) + return; for (int i = 0; i <= m_amount; i++) { if (rand()/(float)RAND_MAX < dtime) { - v3f pos = random_v3f(m_minpos, m_maxpos); + v3f pos = random_v3f(m_minpos, m_maxpos) + + attached_offset; v3f vel = random_v3f(m_minvel, m_maxvel); v3f acc = random_v3f(m_minacc, m_maxacc); float exptime = rand()/(float)RAND_MAX @@ -453,6 +475,7 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, event->add_particlespawner.maxsize, event->add_particlespawner.collisiondetection, event->add_particlespawner.collision_removal, + event->add_particlespawner.attached_id, event->add_particlespawner.vertical, texture, event->add_particlespawner.id, diff --git a/src/particles.h b/src/particles.h index bc3ca53b7..eb8c6665d 100644 --- a/src/particles.h +++ b/src/particles.h @@ -119,6 +119,7 @@ class ParticleSpawner float minsize, float maxsize, bool collisiondetection, bool collision_removal, + u16 attached_id, bool vertical, video::ITexture *texture, u32 id, @@ -154,7 +155,7 @@ class ParticleSpawner bool m_collisiondetection; bool m_collision_removal; bool m_vertical; - + u16 m_attached_id; }; /** diff --git a/src/script/lua_api/l_particles.cpp b/src/script/lua_api/l_particles.cpp index 263e35407..667ac7272 100644 --- a/src/script/lua_api/l_particles.cpp +++ b/src/script/lua_api/l_particles.cpp @@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "lua_api/l_particles.h" +#include "lua_api/l_object.h" #include "lua_api/l_internal.h" #include "common/c_converter.h" #include "server.h" @@ -138,6 +139,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) time= minexptime= maxexptime= minsize= maxsize= 1; bool collisiondetection, vertical, collision_removal; collisiondetection = vertical = collision_removal = false; + ServerActiveObject *attached = NULL; std::string texture = ""; std::string playername = ""; @@ -198,6 +200,14 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) "collisiondetection", collisiondetection); collision_removal = getboolfield_default(L, 1, "collision_removal", collision_removal); + + lua_getfield(L, 1, "attached"); + if (!lua_isnil(L, -1)) { + ObjectRef *ref = ObjectRef::checkobject(L, -1); + lua_pop(L, 1); + attached = ObjectRef::getobject(ref); + } + vertical = getboolfield_default(L, 1, "vertical", vertical); texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); @@ -211,6 +221,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) minsize, maxsize, collisiondetection, collision_removal, + attached, vertical, texture, playername); lua_pushnumber(L, id); diff --git a/src/server.cpp b/src/server.cpp index a93c143c7..e67f37d56 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1678,7 +1678,7 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture, u32 id) + u16 attached_id, bool vertical, const std::string &texture, u32 id) { DSTACK(FUNCTION_NAME); @@ -1692,6 +1692,7 @@ void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3 pkt << id << vertical; pkt << collision_removal; + pkt << attached_id; if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); @@ -3156,7 +3157,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture, + ServerActiveObject *attached, bool vertical, const std::string &texture, const std::string &playername) { // m_env will be NULL if the server is initializing @@ -3171,11 +3172,19 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, peer_id = player->peer_id; } - u32 id = m_env->addParticleSpawner(spawntime); + u16 attached_id = attached ? attached->getId() : 0; + + u32 id; + if (attached_id == 0) + id = m_env->addParticleSpawner(spawntime); + else + id = m_env->addParticleSpawner(spawntime, attached_id); + SendAddParticleSpawner(peer_id, amount, spawntime, minpos, maxpos, minvel, maxvel, minacc, maxacc, minexptime, maxexptime, minsize, maxsize, - collisiondetection, collision_removal, vertical, texture, id); + collisiondetection, collision_removal, attached_id, vertical, + texture, id); return id; } diff --git a/src/server.h b/src/server.h index 6ee61a0eb..5fc2a9133 100644 --- a/src/server.h +++ b/src/server.h @@ -258,6 +258,7 @@ public: float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, + ServerActiveObject *attached, bool vertical, const std::string &texture, const std::string &playername); @@ -434,6 +435,7 @@ private: float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, + u16 attached_id, bool vertical, const std::string &texture, u32 id); void SendDeleteParticleSpawner(u16 peer_id, u32 id); diff --git a/src/serverobject.cpp b/src/serverobject.cpp index 236d7e8dc..191247829 100644 --- a/src/serverobject.cpp +++ b/src/serverobject.cpp @@ -98,4 +98,3 @@ bool ServerActiveObject::setWieldedItem(const ItemStack &item) } return false; } - diff --git a/src/serverobject.h b/src/serverobject.h index cfe2b6bcc..63650e3be 100644 --- a/src/serverobject.h +++ b/src/serverobject.h @@ -188,6 +188,15 @@ public: { return 0; } virtual ItemStack getWieldedItem() const; virtual bool setWieldedItem(const ItemStack &item); + inline void attachParticleSpawner(u32 id) + { + m_attached_particle_spawners.insert(id); + } + inline void detachParticleSpawner(u32 id) + { + m_attached_particle_spawners.erase(id); + } + /* Number of players which know about this object. Object won't be @@ -242,6 +251,7 @@ protected: ServerEnvironment *m_env; v3f m_base_position; + UNORDERED_SET m_attached_particle_spawners; private: // Used for creating objects based on type -- cgit v1.2.3 From 4b17105dc45976e399dab68f85b3030fa27f0ae2 Mon Sep 17 00:00:00 2001 From: Rogier Date: Mon, 25 Jul 2016 18:39:29 +0200 Subject: Emergeblocks: Fix occasional crash Modification of the emergeblocks internal state was not protected by a lock, causing a race condition. This can be reproduced by repeatedly running emergeblocks for an already-generated section of the map (with multiple emerge threads). --- src/script/cpp_api/s_env.cpp | 4 +++- src/script/lua_api/l_env.cpp | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'src/script') diff --git a/src/script/cpp_api/s_env.cpp b/src/script/cpp_api/s_env.cpp index 82d0d4f0e..913d8539d 100644 --- a/src/script/cpp_api/s_env.cpp +++ b/src/script/cpp_api/s_env.cpp @@ -212,11 +212,13 @@ void ScriptApiEnv::on_emerge_area_completion( { Server *server = getServer(); + // This function should be executed with envlock held. + // The caller (LuaEmergeAreaCallback in src/script/lua_api/l_env.cpp) + // should have obtained the lock. // Note that the order of these locks is important! Envlock must *ALWAYS* // be acquired before attempting to acquire scriptlock, or else ServerThread // will try to acquire scriptlock after it already owns envlock, thus // deadlocking EmergeThread and ServerThread - MutexAutoLock envlock(server->m_env_mutex); SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index f6ea23e95..68d10308c 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -137,6 +137,10 @@ void LuaEmergeAreaCallback(v3s16 blockpos, EmergeAction action, void *param) assert(state->script != NULL); assert(state->refcount > 0); + // state must be protected by envlock + Server *server = state->script->getServer(); + MutexAutoLock envlock(server->m_env_mutex); + state->refcount--; state->script->on_emerge_area_completion(blockpos, action, state); -- cgit v1.2.3 From 6eb6e75fff91f86d0e59d337f12ec93fcf9dc46e Mon Sep 17 00:00:00 2001 From: Foghrye4 Date: Sun, 23 Oct 2016 20:45:25 +0300 Subject: Adding LuaError on attempt to assign vectors with values out of range --- src/script/common/c_converter.cpp | 12 ++++++++++++ src/script/lua_api/l_object.cpp | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src/script') diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index 857300fa5..f36298915 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -23,6 +23,7 @@ extern "C" { } #include "util/numeric.h" +#include "util/serialize.h" #include "util/string.h" #include "common/c_converter.h" #include "constants.h" @@ -37,6 +38,14 @@ extern "C" { } \ } while(0) #define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER) +#define CHECK_FLOAT_RANGE(value, name) \ +if (value < F1000_MIN || value > F1000_MAX) { \ + std::ostringstream error_text; \ + error_text << "Invalid float vector dimension range '" name "' " << \ + "(expected " << F1000_MIN << " < " name " < " << F1000_MAX << \ + " got " << value << ")." << std::endl; \ + throw LuaError(error_text.str()); \ +} #define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE) @@ -170,14 +179,17 @@ v3f check_v3f(lua_State *L, int index) lua_getfield(L, index, "x"); CHECK_POS_COORD("x"); pos.X = lua_tonumber(L, -1); + CHECK_FLOAT_RANGE(pos.X, "x") lua_pop(L, 1); lua_getfield(L, index, "y"); CHECK_POS_COORD("y"); pos.Y = lua_tonumber(L, -1); + CHECK_FLOAT_RANGE(pos.Y, "y") lua_pop(L, 1); lua_getfield(L, index, "z"); CHECK_POS_COORD("z"); pos.Z = lua_tonumber(L, -1); + CHECK_FLOAT_RANGE(pos.Z, "z") lua_pop(L, 1); return pos; } diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index bb352e429..23994181c 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -606,10 +606,10 @@ int ObjectRef::l_set_bone_position(lua_State *L) bone = lua_tostring(L, 2); v3f position = v3f(0, 0, 0); if (!lua_isnil(L, 3)) - position = read_v3f(L, 3); + position = check_v3f(L, 3); v3f rotation = v3f(0, 0, 0); if (!lua_isnil(L, 4)) - rotation = read_v3f(L, 4); + rotation = check_v3f(L, 4); co->setBonePosition(bone, position, rotation); return 0; } -- cgit v1.2.3 From 9d25242c5c1411d692254cf910345d51c9a24fa3 Mon Sep 17 00:00:00 2001 From: Ner'zhul Date: Sun, 30 Oct 2016 14:53:26 +0100 Subject: PlayerSAO/LocalPlayer refactor: (#4612) * Create UnitSAO, a common part between PlayerSAO & LuaEntitySAO * Move breath to PlayerSAO & LocalPlayer * Migrate m_yaw from (Remote)Player & LuaEntitySAO to UnitSAO * Migrate m_yaw from Player to LocalPlayer for client * Move some functions outside of player class to PlayerSAO/RemotePlayer or LocalPlayer depending on which class needs it * Move pitch to LocalPlayer & PlayerSAO * Move m_position from Player to LocalPlayer * Move camera_barely_in_ceiling to LocalPlayer as it's used only there * use PlayerSAO::m_base_position for Server side positions * remove a unused variable * ServerActiveObject::setPos now uses const ref * use ServerEnv::loadPlayer unconditionnaly as it creates RemotePlayer only if it's not already loaded * Move hp from Player to LocalPlayer * Move m_hp from LuaEntitySAO to UnitSAO * Use m_hp from PlayerSAO/UnitSAO instead of RemotePlayer --- src/clientiface.cpp | 14 ++-- src/collision.cpp | 2 +- src/content_sao.cpp | 155 +++++++++++++++++++----------------- src/content_sao.h | 62 +++++++++++---- src/environment.cpp | 35 ++++---- src/environment.h | 7 +- src/localplayer.cpp | 7 ++ src/localplayer.h | 41 ++++++++++ src/network/clientpackethandler.cpp | 1 - src/network/serverpackethandler.cpp | 55 +++++++------ src/player.cpp | 14 +--- src/player.h | 50 ------------ src/remoteplayer.cpp | 66 +++++++-------- src/remoteplayer.h | 35 +------- src/script/lua_api/l_object.cpp | 32 ++++---- src/server.cpp | 71 +++++++++-------- src/serverobject.h | 2 +- src/unittest/test_player.cpp | 39 +++++---- 18 files changed, 353 insertions(+), 335 deletions(-) (limited to 'src/script') diff --git a/src/clientiface.cpp b/src/clientiface.cpp index d78cf1c53..d2e3a6da0 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "environment.h" #include "map.h" #include "emerge.h" -#include "serverobject.h" // TODO this is used for cleanup of only +#include "content_sao.h" // TODO this is used for cleanup of only #include "log.h" #include "util/srp.h" @@ -82,6 +82,10 @@ void RemoteClient::GetNextBlocks ( if (player == NULL) return; + PlayerSAO *sao = player->getPlayerSAO(); + if (sao == NULL) + return; + // Won't send anything if already sending if(m_blocks_sending.size() >= g_settings->getU16 ("max_simultaneous_block_sends_per_client")) @@ -90,7 +94,7 @@ void RemoteClient::GetNextBlocks ( return; } - v3f playerpos = player->getPosition(); + v3f playerpos = sao->getBasePosition(); v3f playerspeed = player->getSpeed(); v3f playerspeeddir(0,0,0); if(playerspeed.getLength() > 1.0*BS) @@ -103,10 +107,10 @@ void RemoteClient::GetNextBlocks ( v3s16 center = getNodeBlockPos(center_nodepos); // Camera position and direction - v3f camera_pos = player->getEyePosition(); + v3f camera_pos = sao->getEyePosition(); v3f camera_dir = v3f(0,0,1); - camera_dir.rotateYZBy(player->getPitch()); - camera_dir.rotateXZBy(player->getYaw()); + camera_dir.rotateYZBy(sao->getPitch()); + camera_dir.rotateXZBy(sao->getYaw()); /*infostream<<"camera_dir=("<(env); - if (s_env != 0) { + if (s_env != NULL) { f32 distance = speed_f->getLength(); std::vector s_objects; s_env->getObjectsInsideRadius(s_objects, *pos_f, distance * 1.5); diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 375a43c90..23a064085 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -118,14 +118,12 @@ LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", ""); LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &name, const std::string &state): - ServerActiveObject(env, pos), + UnitSAO(env, pos), m_init_name(name), m_init_state(state), m_registered(false), - m_hp(-1), m_velocity(0,0,0), m_acceleration(0,0,0), - m_yaw(0), m_properties_sent(true), m_last_sent_yaw(0), m_last_sent_position(0,0,0), @@ -664,16 +662,6 @@ v3f LuaEntitySAO::getAcceleration() return m_acceleration; } -void LuaEntitySAO::setYaw(float yaw) -{ - m_yaw = yaw; -} - -float LuaEntitySAO::getYaw() -{ - return m_yaw; -} - void LuaEntitySAO::setTextureMod(const std::string &mod) { std::string str = gob_cmd_set_texture_mod(mod); @@ -762,10 +750,9 @@ bool LuaEntitySAO::collideWithObjects(){ // No prototype, PlayerSAO does not need to be deserialized -PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id_, - const std::set &privs, bool is_singleplayer): - ServerActiveObject(env_, v3f(0,0,0)), - m_player(player_), +PlayerSAO::PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer): + UnitSAO(env_, v3f(0,0,0)), + m_player(NULL), m_peer_id(peer_id_), m_inventory(NULL), m_damage(0), @@ -777,7 +764,6 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id m_position_not_sent(false), m_armor_groups_sent(false), m_properties_sent(true), - m_privs(privs), m_is_singleplayer(is_singleplayer), m_animation_speed(0), m_animation_blend(0), @@ -786,6 +772,8 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id m_bone_position_sent(false), m_attachment_parent_id(0), m_attachment_sent(false), + m_breath(PLAYER_MAX_BREATH), + m_pitch(0), // public m_physics_override_speed(1), m_physics_override_jump(1), @@ -794,10 +782,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id m_physics_override_sneak_glitch(true), m_physics_override_sent(false) { - assert(m_player); // pre-condition assert(m_peer_id != 0); // pre-condition - setBasePosition(m_player->getPosition()); - m_inventory = &m_player->inventory; m_armor_groups["fleshy"] = 100; m_prop.hp_max = PLAYER_MAX_HP; @@ -816,6 +801,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id // end of default appearance m_prop.is_visible = true; m_prop.makes_footstep_sound = true; + m_hp = PLAYER_MAX_HP; } PlayerSAO::~PlayerSAO() @@ -824,6 +810,14 @@ PlayerSAO::~PlayerSAO() delete m_inventory; } +void PlayerSAO::initialize(RemotePlayer *player, const std::set &privs) +{ + assert(player); + m_player = player; + m_privs = privs; + m_inventory = &m_player->inventory; +} + std::string PlayerSAO::getDescription() { return std::string("player ") + m_player->getName(); @@ -833,10 +827,10 @@ std::string PlayerSAO::getDescription() void PlayerSAO::addedToEnvironment(u32 dtime_s) { ServerActiveObject::addedToEnvironment(dtime_s); - ServerActiveObject::setBasePosition(m_player->getPosition()); + ServerActiveObject::setBasePosition(m_base_position); m_player->setPlayerSAO(this); m_player->peer_id = m_peer_id; - m_last_good_position = m_player->getPosition(); + m_last_good_position = m_base_position; } // Called before removing from environment @@ -844,9 +838,9 @@ void PlayerSAO::removingFromEnvironment() { ServerActiveObject::removingFromEnvironment(); if (m_player->getPlayerSAO() == this) { - m_player->setPlayerSAO(NULL); m_player->peer_id = 0; m_env->savePlayer(m_player); + m_player->setPlayerSAO(NULL); m_env->removePlayer(m_player); for (UNORDERED_SET::iterator it = m_attached_particle_spawners.begin(); it != m_attached_particle_spawners.end(); ++it) { @@ -870,8 +864,8 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version) os<getName()); // name writeU8(os, 1); // is_player writeS16(os, getId()); //id - writeV3F1000(os, m_player->getPosition() + v3f(0,BS*1,0)); - writeF1000(os, m_player->getYaw()); + writeV3F1000(os, m_base_position + v3f(0,BS*1,0)); + writeF1000(os, m_yaw); writeS16(os, getHP()); writeU8(os, 6 + m_bone_position.size() + m_attachment_child_ids.size()); // number of messages stuffed in here @@ -900,8 +894,8 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version) writeU8(os, 0); // version os<getName()); // name writeU8(os, 1); // is_player - writeV3F1000(os, m_player->getPosition() + v3f(0,BS*1,0)); - writeF1000(os, m_player->getYaw()); + writeV3F1000(os, m_base_position + v3f(0,BS*1,0)); + writeF1000(os, m_yaw); writeS16(os, getHP()); writeU8(os, 2); // number of messages stuffed in here os<setPosition(m_last_good_position); + setBasePosition(m_last_good_position); ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } @@ -969,14 +963,13 @@ void PlayerSAO::step(float dtime, bool send_recommended) // Each frame, parent position is copied if the object is attached, otherwise it's calculated normally // If the object gets detached this comes into effect automatically from the last known origin - if(isAttached()) - { + if (isAttached()) { v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition(); m_last_good_position = pos; - m_player->setPosition(pos); + setBasePosition(pos); } - if(send_recommended == false) + if (!send_recommended) return; // If the object is attached client-side, don't waste bandwidth sending its position to clients @@ -988,12 +981,12 @@ void PlayerSAO::step(float dtime, bool send_recommended) if(isAttached()) // Just in case we ever do send attachment position too pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition(); else - pos = m_player->getPosition() + v3f(0,BS*1,0); + pos = m_base_position + v3f(0,BS*1,0); std::string str = gob_cmd_update_position( pos, v3f(0,0,0), v3f(0,0,0), - m_player->getYaw(), + m_yaw, true, false, update_interval @@ -1003,7 +996,7 @@ void PlayerSAO::step(float dtime, bool send_recommended) m_messages_out.push(aom); } - if(m_armor_groups_sent == false) { + if (!m_armor_groups_sent) { m_armor_groups_sent = true; std::string str = gob_cmd_update_armor_groups( m_armor_groups); @@ -1012,7 +1005,7 @@ void PlayerSAO::step(float dtime, bool send_recommended) m_messages_out.push(aom); } - if(m_physics_override_sent == false){ + if (!m_physics_override_sent) { m_physics_override_sent = true; std::string str = gob_cmd_update_physics_override(m_physics_override_speed, m_physics_override_jump, m_physics_override_gravity, @@ -1022,7 +1015,7 @@ void PlayerSAO::step(float dtime, bool send_recommended) m_messages_out.push(aom); } - if(m_animation_sent == false){ + if (!m_animation_sent) { m_animation_sent = true; std::string str = gob_cmd_update_animation( m_animation_range, m_animation_speed, m_animation_blend, m_animation_loop); @@ -1055,16 +1048,20 @@ void PlayerSAO::step(float dtime, bool send_recommended) void PlayerSAO::setBasePosition(const v3f &position) { + if (m_player && position != m_base_position) + m_player->setDirty(true); + // This needs to be ran for attachments too ServerActiveObject::setBasePosition(position); m_position_not_sent = true; } -void PlayerSAO::setPos(v3f pos) +void PlayerSAO::setPos(const v3f &pos) { if(isAttached()) return; - m_player->setPosition(pos); + + setBasePosition(pos); // Movement caused by this command is always valid m_last_good_position = pos; ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); @@ -1074,22 +1071,34 @@ void PlayerSAO::moveTo(v3f pos, bool continuous) { if(isAttached()) return; - m_player->setPosition(pos); + + setBasePosition(pos); // Movement caused by this command is always valid m_last_good_position = pos; ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } -void PlayerSAO::setYaw(float yaw) +void PlayerSAO::setYaw(const float yaw, bool send_data) { - m_player->setYaw(yaw); - ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); + if (m_player && yaw != m_yaw) + m_player->setDirty(true); + + UnitSAO::setYaw(yaw); + + // Datas should not be sent at player initialization + if (send_data) + ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } -void PlayerSAO::setPitch(float pitch) +void PlayerSAO::setPitch(const float pitch, bool send_data) { - m_player->setPitch(pitch); - ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); + if (m_player && pitch != m_pitch) + m_player->setDirty(true); + + m_pitch = pitch; + + if (send_data) + ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } int PlayerSAO::punch(v3f dir, @@ -1153,13 +1162,8 @@ int PlayerSAO::punch(v3f dir, return hitparams.wear; } -void PlayerSAO::rightClick(ServerActiveObject *clicker) -{ -} - -s16 PlayerSAO::getHP() const +void PlayerSAO::rightClick(ServerActiveObject *) { - return m_player->hp; } s16 PlayerSAO::readDamage() @@ -1169,12 +1173,16 @@ s16 PlayerSAO::readDamage() return damage; } -void PlayerSAO::setHP(s16 hp) +void PlayerSAO::setHP(s16 hp, bool direct) { - s16 oldhp = m_player->hp; + if (direct) { + m_hp = hp; + return; + } + + s16 oldhp = m_hp; - s16 hp_change = m_env->getScriptIface()->on_player_hpchange(this, - hp - oldhp); + s16 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - oldhp); if (hp_change == 0) return; hp = oldhp + hp_change; @@ -1184,11 +1192,11 @@ void PlayerSAO::setHP(s16 hp) else if (hp > PLAYER_MAX_HP) hp = PLAYER_MAX_HP; - if(hp < oldhp && g_settings->getBool("enable_damage") == false) { + if (hp < oldhp && !g_settings->getBool("enable_damage")) { return; } - m_player->hp = hp; + m_hp = hp; if (oldhp > hp) m_damage += (oldhp - hp); @@ -1198,14 +1206,12 @@ void PlayerSAO::setHP(s16 hp) m_properties_sent = false; } -u16 PlayerSAO::getBreath() const +void PlayerSAO::setBreath(const u16 breath) { - return m_player->getBreath(); -} + if (m_player && breath != m_breath) + m_player->setDirty(true); -void PlayerSAO::setBreath(u16 breath) -{ - m_player->setBreath(breath); + m_breath = breath; } void PlayerSAO::setArmorGroups(const ItemGroupList &armor_groups) @@ -1355,7 +1361,7 @@ bool PlayerSAO::checkMovementCheat() { if (isAttached() || m_is_singleplayer || g_settings->getBool("disable_anticheat")) { - m_last_good_position = m_player->getPosition(); + m_last_good_position = m_base_position; return false; } @@ -1382,7 +1388,7 @@ bool PlayerSAO::checkMovementCheat() // Tolerance. The lag pool does this a bit. //player_max_speed *= 2.5; - v3f diff = (m_player->getPosition() - m_last_good_position); + v3f diff = (m_base_position - m_last_good_position); float d_vert = diff.Y; diff.Y = 0; float d_horiz = diff.getLength(); @@ -1392,27 +1398,26 @@ bool PlayerSAO::checkMovementCheat() required_time = d_vert / player_max_speed; // Moving upwards if (m_move_pool.grab(required_time)) { - m_last_good_position = m_player->getPosition(); + m_last_good_position = m_base_position; } else { actionstream << "Player " << m_player->getName() << " moved too fast; resetting position" << std::endl; - m_player->setPosition(m_last_good_position); + setBasePosition(m_last_good_position); cheated = true; } return cheated; } -bool PlayerSAO::getCollisionBox(aabb3f *toset) { - //update collision box - *toset = m_player->getCollisionbox(); - +bool PlayerSAO::getCollisionBox(aabb3f *toset) +{ + *toset = aabb3f(-BS * 0.30, 0.0, -BS * 0.30, BS * 0.30, BS * 1.75, BS * 0.30); toset->MinEdge += m_base_position; toset->MaxEdge += m_base_position; - return true; } -bool PlayerSAO::collideWithObjects(){ +bool PlayerSAO::collideWithObjects() +{ return true; } diff --git a/src/content_sao.h b/src/content_sao.h index 76a3a37da..4ea6277ff 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -23,12 +23,35 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serverobject.h" #include "itemgroup.h" #include "object_properties.h" +#include "constants.h" + +class UnitSAO: public ServerActiveObject +{ +public: + UnitSAO(ServerEnvironment *env, v3f pos): + ServerActiveObject(env, pos), + m_hp(-1), m_yaw(0) {} + virtual ~UnitSAO() {} + + virtual void setYaw(const float yaw) { m_yaw = yaw; } + float getYaw() const { return m_yaw; }; + f32 getRadYaw() const { return m_yaw * core::DEGTORAD; } + // Deprecated + f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; } + + s16 getHP() const { return m_hp; } + // Use a function, if isDead can be defined by other conditions + bool isDead() const { return m_hp == 0; } +protected: + s16 m_hp; + float m_yaw; +}; /* LuaEntitySAO needs some internals exposed. */ -class LuaEntitySAO : public ServerActiveObject +class LuaEntitySAO : public UnitSAO { public: LuaEntitySAO(ServerEnvironment *env, v3f pos, @@ -74,8 +97,7 @@ public: v3f getVelocity(); void setAcceleration(v3f acceleration); v3f getAcceleration(); - void setYaw(float yaw); - float getYaw(); + void setTextureMod(const std::string &mod); void setSprite(v2s16 p, int num_frames, float framelength, bool select_horiz_by_yawpitch); @@ -91,10 +113,9 @@ private: bool m_registered; struct ObjectProperties m_prop; - s16 m_hp; v3f m_velocity; v3f m_acceleration; - float m_yaw; + ItemGroupList m_armor_groups; bool m_properties_sent; @@ -158,11 +179,10 @@ public: class RemotePlayer; -class PlayerSAO : public ServerActiveObject +class PlayerSAO : public UnitSAO { public: - PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id_, - const std::set &privs, bool is_singleplayer); + PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer); ~PlayerSAO(); ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_PLAYER; } @@ -182,10 +202,14 @@ public: bool isAttached(); void step(float dtime, bool send_recommended); void setBasePosition(const v3f &position); - void setPos(v3f pos); + void setPos(const v3f &pos); void moveTo(v3f pos, bool continuous); - void setYaw(float); - void setPitch(float); + void setYaw(const float yaw, bool send_data = true); + void setPitch(const float pitch, bool send_data = true); + f32 getPitch() const { return m_pitch; } + f32 getRadPitch() const { return m_pitch * core::DEGTORAD; } + // Deprecated + f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; } /* Interaction interface @@ -196,11 +220,10 @@ public: ServerActiveObject *puncher, float time_from_last_punch); void rightClick(ServerActiveObject *clicker); - s16 getHP() const; - void setHP(s16 hp); + void setHP(s16 hp, bool direct = false); s16 readDamage(); - u16 getBreath() const; - void setBreath(u16 breath); + u16 getBreath() const { return m_breath; } + void setBreath(const u16 breath); void setArmorGroups(const ItemGroupList &armor_groups); ItemGroupList getArmorGroups(); void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop); @@ -283,6 +306,11 @@ public: bool getCollisionBox(aabb3f *toset); bool collideWithObjects(); + void initialize(RemotePlayer *player, const std::set &privs); + + v3f getEyePosition() const { return m_base_position + getEyeOffset(); } + v3f getEyeOffset() const { return v3f(0, BS * 1.625f, 0); } + private: std::string getPropertyPacket(); @@ -326,8 +354,8 @@ private: v3f m_attachment_position; v3f m_attachment_rotation; bool m_attachment_sent; - - + u16 m_breath; + f32 m_pitch; public: float m_physics_override_speed; float m_physics_override_jump; diff --git a/src/environment.cpp b/src/environment.cpp index ecda1b6a4..13c64b37c 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -608,9 +608,8 @@ void ServerEnvironment::saveLoadedPlayers() for (std::vector::iterator it = m_players.begin(); it != m_players.end(); ++it) { - RemotePlayer *player = static_cast(*it); - if (player->checkModified()) { - player->save(players_path, m_gamedef); + if ((*it)->checkModified()) { + (*it)->save(players_path, m_gamedef); } } } @@ -623,7 +622,7 @@ void ServerEnvironment::savePlayer(RemotePlayer *player) player->save(players_path, m_gamedef); } -RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) +RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername, PlayerSAO *sao) { bool newplayer = false; bool found = false; @@ -641,7 +640,8 @@ RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) std::ifstream is(path.c_str(), std::ios_base::binary); if (!is.good()) continue; - player->deSerialize(is, path); + + player->deSerialize(is, path, sao); is.close(); if (player->getName() == playername) { @@ -657,11 +657,13 @@ RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) << " not found" << std::endl; if (newplayer) delete player; + return NULL; } - if (newplayer) + if (newplayer) { addPlayer(player); + } player->setModified(false); return player; } @@ -1271,12 +1273,16 @@ void ServerEnvironment::step(float dtime) i != m_players.end(); ++i) { RemotePlayer *player = dynamic_cast(*i); assert(player); + // Ignore disconnected players if (player->peer_id == 0) continue; + PlayerSAO *playersao = player->getPlayerSAO(); + assert(playersao); + v3s16 blockpos = getNodeBlockPos( - floatToInt(player->getPosition(), BS)); + floatToInt(playersao->getBasePosition(), BS)); players_blockpos.push_back(blockpos); } @@ -1584,7 +1590,7 @@ u16 ServerEnvironment::addActiveObject(ServerActiveObject *object) Finds out what new objects have been added to inside a radius around a position */ -void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius, +void ServerEnvironment::getAddedActiveObjects(PlayerSAO *playersao, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &added_objects) @@ -1594,7 +1600,6 @@ void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius, if (player_radius_f < 0) player_radius_f = 0; - /* Go through the object list, - discard m_removed objects, @@ -1602,20 +1607,21 @@ void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius, - discard objects that are found in current_objects. - add remaining objects to added_objects */ - for(ActiveObjectMap::iterator i = m_active_objects.begin(); + for (ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { u16 id = i->first; // Get object ServerActiveObject *object = i->second; - if(object == NULL) + if (object == NULL) continue; // Discard if removed or deactivating if(object->m_removed || object->m_pending_deactivation) continue; - f32 distance_f = object->getBasePosition().getDistanceFrom(player->getPosition()); + f32 distance_f = object->getBasePosition(). + getDistanceFrom(playersao->getBasePosition()); if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) { // Discard if too far if (distance_f > player_radius_f && player_radius_f != 0) @@ -1637,7 +1643,7 @@ void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius, Finds out what objects have been removed from inside a radius around a position */ -void ServerEnvironment::getRemovedActiveObjects(RemotePlayer *player, s16 radius, +void ServerEnvironment::getRemovedActiveObjects(PlayerSAO *playersao, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &removed_objects) @@ -1647,7 +1653,6 @@ void ServerEnvironment::getRemovedActiveObjects(RemotePlayer *player, s16 radius if (player_radius_f < 0) player_radius_f = 0; - /* Go through current_objects; object is removed if: - object is not found in m_active_objects (this is actually an @@ -1675,7 +1680,7 @@ void ServerEnvironment::getRemovedActiveObjects(RemotePlayer *player, s16 radius continue; } - f32 distance_f = object->getBasePosition().getDistanceFrom(player->getPosition()); + f32 distance_f = object->getBasePosition().getDistanceFrom(playersao->getBasePosition()); if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) { if (distance_f <= player_radius_f || player_radius_f == 0) continue; diff --git a/src/environment.h b/src/environment.h index 3f3c1cf2c..4bee40e57 100644 --- a/src/environment.h +++ b/src/environment.h @@ -54,6 +54,7 @@ class ClientMap; class GameScripting; class Player; class RemotePlayer; +class PlayerSAO; class Environment { @@ -315,7 +316,7 @@ public: // Save players void saveLoadedPlayers(); void savePlayer(RemotePlayer *player); - RemotePlayer *loadPlayer(const std::string &playername); + RemotePlayer *loadPlayer(const std::string &playername, PlayerSAO *sao); void addPlayer(RemotePlayer *player); void removePlayer(RemotePlayer *player); @@ -362,7 +363,7 @@ public: Find out what new objects have been added to inside a radius around a position */ - void getAddedActiveObjects(RemotePlayer *player, s16 radius, + void getAddedActiveObjects(PlayerSAO *playersao, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &added_objects); @@ -371,7 +372,7 @@ public: Find out what new objects have been removed from inside a radius around a position */ - void getRemovedActiveObjects(RemotePlayer *player, s16 radius, + void getRemovedActiveObjects(PlayerSAO *playersao, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &removed_objects); diff --git a/src/localplayer.cpp b/src/localplayer.cpp index bc242a59d..71efb2343 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc., LocalPlayer::LocalPlayer(Client *gamedef, const char *name): Player(name, gamedef->idef()), parent(0), + hp(PLAYER_MAX_HP), got_teleported(false), isAttached(false), touching_ground(false), @@ -62,6 +63,7 @@ LocalPlayer::LocalPlayer(Client *gamedef, const char *name): light_color(255,255,255,255), hurt_tilt_timer(0.0f), hurt_tilt_strength(0.0f), + m_position(0,0,0), m_sneak_node(32767,32767,32767), m_sneak_node_exists(false), m_need_to_get_new_sneak_node(true), @@ -69,6 +71,11 @@ LocalPlayer::LocalPlayer(Client *gamedef, const char *name): m_old_node_below(32767,32767,32767), m_old_node_below_type("air"), m_can_jump(false), + m_breath(PLAYER_MAX_BREATH), + m_yaw(0), + m_pitch(0), + camera_barely_in_ceiling(false), + m_collisionbox(-BS * 0.30, 0.0, -BS * 0.30, BS * 0.30, BS * 1.75, BS * 0.30), m_cao(NULL), m_gamedef(gamedef) { diff --git a/src/localplayer.h b/src/localplayer.h index eb727d7e3..749f8f8ce 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -40,6 +40,7 @@ public: ClientActiveObject *parent; + u16 hp; bool got_teleported; bool isAttached; bool touching_ground; @@ -99,10 +100,45 @@ public: u32 maxHudId() const { return hud.size(); } + u16 getBreath() const { return m_breath; } + void setBreath(u16 breath) { m_breath = breath; } + + v3s16 getLightPosition() const + { + return floatToInt(m_position + v3f(0,BS+BS/2,0), BS); + } + + void setYaw(f32 yaw) + { + m_yaw = yaw; + } + + f32 getYaw() const { return m_yaw; } + + void setPitch(f32 pitch) + { + m_pitch = pitch; + } + + f32 getPitch() const { return m_pitch; } + + void setPosition(const v3f &position) + { + m_position = position; + } + + v3f getPosition() const { return m_position; } + v3f getEyePosition() const { return m_position + getEyeOffset(); } + v3f getEyeOffset() const + { + float eye_height = camera_barely_in_ceiling ? 1.5f : 1.625f; + return v3f(0, BS * eye_height, 0); + } private: void accelerateHorizontal(const v3f &target_speed, const f32 max_increase); void accelerateVertical(const v3f &target_speed, const f32 max_increase); + v3f m_position; // This is used for determining the sneaking range v3s16 m_sneak_node; // Whether the player is allowed to sneak @@ -117,6 +153,11 @@ private: v3s16 m_old_node_below; std::string m_old_node_below_type; bool m_can_jump; + u16 m_breath; + f32 m_yaw; + f32 m_pitch; + bool camera_barely_in_ceiling; + aabb3f m_collisionbox; GenericCAO* m_cao; Client *m_gamedef; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 090741f9f..411982f69 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -634,7 +634,6 @@ void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt) m_media_downloader->addFile(name, sha1_raw); } - std::vector remote_media; try { std::string str; diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index 554025747..5e70b4c6c 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -809,13 +809,6 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt) return; } - // If player is dead we don't care of this packet - if (player->isDead()) { - verbosestream << "TOSERVER_PLAYERPOS: " << player->getName() - << " is dead. Ignoring packet"; - return; - } - PlayerSAO *playersao = player->getPlayerSAO(); if (playersao == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -825,10 +818,17 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt) return; } - player->setPosition(position); + // If player is dead we don't care of this packet + if (playersao->isDead()) { + verbosestream << "TOSERVER_PLAYERPOS: " << player->getName() + << " is dead. Ignoring packet"; + return; + } + + playersao->setBasePosition(position); player->setSpeed(speed); - player->setPitch(pitch); - player->setYaw(yaw); + playersao->setPitch(pitch, false); + playersao->setYaw(yaw, false); player->keyPressed = keyPressed; player->control.up = (keyPressed & 1); player->control.down = (keyPressed & 2); @@ -1100,7 +1100,7 @@ void Server::handleCommand_Damage(NetworkPacket* pkt) if (g_settings->getBool("enable_damage")) { actionstream << player->getName() << " damaged by " - << (int)damage << " hp at " << PP(player->getPosition() / BS) + << (int)damage << " hp at " << PP(playersao->getBasePosition() / BS) << std::endl; playersao->setHP(playersao->getHP() - damage); @@ -1124,16 +1124,6 @@ void Server::handleCommand_Breath(NetworkPacket* pkt) return; } - /* - * If player is dead, we don't need to update the breath - * He is dead ! - */ - if (player->isDead()) { - verbosestream << "TOSERVER_BREATH: " << player->getName() - << " is dead. Ignoring packet"; - return; - } - PlayerSAO *playersao = player->getPlayerSAO(); if (playersao == NULL) { @@ -1144,6 +1134,16 @@ void Server::handleCommand_Breath(NetworkPacket* pkt) return; } + /* + * If player is dead, we don't need to update the breath + * He is dead ! + */ + if (playersao->isDead()) { + verbosestream << "TOSERVER_BREATH: " << player->getName() + << " is dead. Ignoring packet"; + return; + } + playersao->setBreath(breath); SendPlayerBreath(pkt->getPeerId()); } @@ -1264,13 +1264,16 @@ void Server::handleCommand_Respawn(NetworkPacket* pkt) return; } - if (!player->isDead()) + PlayerSAO *playersao = player->getPlayerSAO(); + assert(playersao); + + if (!playersao->isDead()) return; RespawnPlayer(pkt->getPeerId()); actionstream << player->getName() << " respawns at " - << PP(player->getPosition()/BS) << std::endl; + << PP(playersao->getBasePosition() / BS) << std::endl; // ActiveObject is added to environment in AsyncRunStep after // the previous addition has been successfully removed @@ -1322,9 +1325,9 @@ void Server::handleCommand_Interact(NetworkPacket* pkt) return; } - if (player->isDead()) { + if (playersao->isDead()) { verbosestream << "TOSERVER_INTERACT: " << player->getName() - << " is dead. Ignoring packet"; + << " is dead. Ignoring packet"; return; } @@ -1455,7 +1458,7 @@ void Server::handleCommand_Interact(NetworkPacket* pkt) ToolCapabilities toolcap = punchitem.getToolCapabilities(m_itemdef); v3f dir = (pointed_object->getBasePosition() - - (player->getPosition() + player->getEyeOffset()) + (playersao->getBasePosition() + playersao->getEyeOffset()) ).normalize(); float time_from_last_punch = playersao->resetTimeFromLastPunch(); diff --git a/src/player.cpp b/src/player.cpp index fa82a79f4..9c321d571 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -30,18 +30,11 @@ with this program; if not, write to the Free Software Foundation, Inc., Player::Player(const char *name, IItemDefManager *idef): - camera_barely_in_ceiling(false), inventory(idef), - hp(PLAYER_MAX_HP), peer_id(PEER_ID_INEXISTENT), keyPressed(0), // protected - m_breath(PLAYER_MAX_BREATH), - m_pitch(0), - m_yaw(0), - m_speed(0,0,0), - m_position(0,0,0), - m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30) + m_speed(0,0,0) { strlcpy(m_name, name, PLAYERNAME_SIZE); @@ -90,11 +83,6 @@ Player::~Player() clearHud(); } -v3s16 Player::getLightPosition() const -{ - return floatToInt(m_position + v3f(0,BS+BS/2,0), BS); -} - u32 Player::addHud(HudElement *toadd) { MutexAutoLock lock(m_mutex); diff --git a/src/player.h b/src/player.h index 6ac5dfe65..5f9bb7ec9 100644 --- a/src/player.h +++ b/src/player.h @@ -129,49 +129,7 @@ public: m_speed = speed; } - v3f getPosition() - { - return m_position; - } - - v3s16 getLightPosition() const; - - v3f getEyeOffset() - { - float eye_height = camera_barely_in_ceiling ? 1.5f : 1.625f; - return v3f(0, BS * eye_height, 0); - } - - v3f getEyePosition() - { - return m_position + getEyeOffset(); - } - - virtual void setPosition(const v3f &position) - { - m_position = position; - } - - virtual void setPitch(f32 pitch) - { - m_pitch = pitch; - } - - virtual void setYaw(f32 yaw) - { - m_yaw = yaw; - } - - f32 getPitch() const { return m_pitch; } - f32 getYaw() const { return m_yaw; } - u16 getBreath() const { return m_breath; } - - virtual void setBreath(u16 breath) { m_breath = breath; } - - f32 getRadPitch() const { return m_pitch * core::DEGTORAD; } - f32 getRadYaw() const { return m_yaw * core::DEGTORAD; } const char *getName() const { return m_name; } - aabb3f getCollisionbox() const { return m_collisionbox; } u32 getFreeHudID() { @@ -183,7 +141,6 @@ public: return size; } - bool camera_barely_in_ceiling; v3f eye_offset_first; v3f eye_offset_third; @@ -205,8 +162,6 @@ public: v2s32 local_animations[4]; float local_animation_speed; - u16 hp; - u16 peer_id; std::string inventory_formspec; @@ -225,12 +180,7 @@ public: s32 hud_hotbar_itemcount; protected: char m_name[PLAYERNAME_SIZE]; - u16 m_breath; - f32 m_pitch; - f32 m_yaw; v3f m_speed; - v3f m_position; - aabb3f m_collisionbox; std::vector hud; private: diff --git a/src/remoteplayer.cpp b/src/remoteplayer.cpp index f64d1d690..605346928 100644 --- a/src/remoteplayer.cpp +++ b/src/remoteplayer.cpp @@ -96,7 +96,7 @@ void RemotePlayer::save(std::string savedir, IGameDef *gamedef) infostream << "Failed to open " << path << std::endl; return; } - testplayer.deSerialize(is, path); + testplayer.deSerialize(is, path, NULL); is.close(); if (strcmp(testplayer.getName(), m_name) == 0) { // Open file and serialize @@ -115,37 +115,46 @@ void RemotePlayer::save(std::string savedir, IGameDef *gamedef) return; } -void RemotePlayer::deSerialize(std::istream &is, const std::string &playername) +void RemotePlayer::deSerialize(std::istream &is, const std::string &playername, + PlayerSAO *sao) { Settings args; if (!args.parseConfigLines(is, "PlayerArgsEnd")) { - throw SerializationError("PlayerArgsEnd of player " + - playername + " not found!"); + throw SerializationError("PlayerArgsEnd of player " + playername + " not found!"); } m_dirty = true; //args.getS32("version"); // Version field value not used std::string name = args.get("name"); strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); - setPitch(args.getFloat("pitch")); - setYaw(args.getFloat("yaw")); - setPosition(args.getV3F("position")); - try { - hp = args.getS32("hp"); - } catch(SettingNotFoundException &e) { - hp = PLAYER_MAX_HP; - } - try { - m_breath = args.getS32("breath"); - } catch(SettingNotFoundException &e) { - m_breath = PLAYER_MAX_BREATH; + if (sao) { + try { + sao->setHP(args.getS32("hp"), true); + } catch(SettingNotFoundException &e) { + sao->setHP(PLAYER_MAX_HP, true); + } + + try { + sao->setBasePosition(args.getV3F("position")); + } catch (SettingNotFoundException &e) {} + + try { + sao->setPitch(args.getFloat("pitch"), false); + } catch (SettingNotFoundException &e) {} + try { + sao->setYaw(args.getFloat("yaw"), false); + } catch (SettingNotFoundException &e) {} + + try { + sao->setBreath(args.getS32("breath")); + } catch (SettingNotFoundException &e) {} } inventory.deSerialize(is); - if(inventory.getList("craftpreview") == NULL) { + if (inventory.getList("craftpreview") == NULL) { // Convert players without craftpreview inventory.addList("craftpreview", 1); @@ -167,11 +176,14 @@ void RemotePlayer::serialize(std::ostream &os) args.setS32("version", 1); args.set("name", m_name); //args.set("password", m_password); - args.setFloat("pitch", m_pitch); - args.setFloat("yaw", m_yaw); - args.setV3F("position", m_position); - args.setS32("hp", hp); - args.setS32("breath", m_breath); + + if (m_sao) { + args.setS32("hp", m_sao->getHP()); + args.setV3F("position", m_sao->getBasePosition()); + args.setFloat("pitch", m_sao->getPitch()); + args.setFloat("yaw", m_sao->getYaw()); + args.setS32("breath", m_sao->getBreath()); + } args.writeLines(os); @@ -180,16 +192,6 @@ void RemotePlayer::serialize(std::ostream &os) inventory.serialize(os); } -void RemotePlayer::setPosition(const v3f &position) -{ - if (position != m_position) - m_dirty = true; - - Player::setPosition(position); - if(m_sao) - m_sao->setBasePosition(position); -} - const RemotePlayerChatResult RemotePlayer::canSendChatMessage() { // Rate limit messages diff --git a/src/remoteplayer.h b/src/remoteplayer.h index 1b1a90de3..61b5a23de 100644 --- a/src/remoteplayer.h +++ b/src/remoteplayer.h @@ -40,11 +40,10 @@ public: virtual ~RemotePlayer() {} void save(std::string savedir, IGameDef *gamedef); - void deSerialize(std::istream &is, const std::string &playername); + void deSerialize(std::istream &is, const std::string &playername, PlayerSAO *sao); PlayerSAO *getPlayerSAO() { return m_sao; } void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; } - void setPosition(const v3f &position); const RemotePlayerChatResult canSendChatMessage(); @@ -67,9 +66,6 @@ public: *ratio = m_day_night_ratio; } - // Use a function, if isDead can be defined by other conditions - bool isDead() const { return hp == 0; } - void setHotbarImage(const std::string &name) { hud_hotbar_image = name; @@ -90,12 +86,6 @@ public: return hud_hotbar_selected_image; } - // Deprecated - f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; } - - // Deprecated - f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; } - void setSky(const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms) { @@ -121,27 +111,6 @@ public: inventory.setModified(x); } - virtual void setBreath(u16 breath) - { - if (breath != m_breath) - m_dirty = true; - Player::setBreath(breath); - } - - virtual void setPitch(f32 pitch) - { - if (pitch != m_pitch) - m_dirty = true; - Player::setPitch(pitch); - } - - virtual void setYaw(f32 yaw) - { - if (yaw != m_yaw) - m_dirty = true; - Player::setYaw(yaw); - } - void setLocalAnimations(v2s32 frames[4], float frame_speed) { for (int i = 0; i < 4; i++) @@ -156,6 +125,8 @@ public: *frame_speed = local_animation_speed; } + void setDirty(bool dirty) { m_dirty = true; } + u16 protocol_version; private: /* diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 23994181c..cf124f17c 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1013,11 +1013,11 @@ int ObjectRef::l_get_look_dir(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - RemotePlayer *player = getplayer(ref); - if (player == NULL) return 0; + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; // Do it - float pitch = player->getRadPitchDep(); - float yaw = player->getRadYawDep(); + float pitch = co->getRadPitchDep(); + float yaw = co->getRadYawDep(); v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw)); push_v3f(L, v); return 1; @@ -1033,10 +1033,10 @@ int ObjectRef::l_get_look_pitch(lua_State *L) "Deprecated call to get_look_pitch, use get_look_vertical instead"); ObjectRef *ref = checkobject(L, 1); - RemotePlayer *player = getplayer(ref); - if (player == NULL) return 0; + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; // Do it - lua_pushnumber(L, player->getRadPitchDep()); + lua_pushnumber(L, co->getRadPitchDep()); return 1; } @@ -1050,10 +1050,10 @@ int ObjectRef::l_get_look_yaw(lua_State *L) "Deprecated call to get_look_yaw, use get_look_horizontal instead"); ObjectRef *ref = checkobject(L, 1); - RemotePlayer *player = getplayer(ref); - if (player == NULL) return 0; + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; // Do it - lua_pushnumber(L, player->getRadYawDep()); + lua_pushnumber(L, co->getRadYawDep()); return 1; } @@ -1062,10 +1062,10 @@ int ObjectRef::l_get_look_vertical(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - RemotePlayer *player = getplayer(ref); - if (player == NULL) return 0; + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; // Do it - lua_pushnumber(L, player->getRadPitch()); + lua_pushnumber(L, co->getRadPitch()); return 1; } @@ -1074,10 +1074,10 @@ int ObjectRef::l_get_look_horizontal(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - RemotePlayer *player = getplayer(ref); - if (player == NULL) return 0; + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; // Do it - lua_pushnumber(L, player->getRadYaw()); + lua_pushnumber(L, co->getRadYaw()); return 1; } diff --git a/src/server.cpp b/src/server.cpp index e67f37d56..cd526ad77 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -701,11 +701,15 @@ void Server::AsyncRunStep(bool initial_step) continue; } + PlayerSAO *playersao = player->getPlayerSAO(); + if (playersao == NULL) + continue; + std::queue removed_objects; std::queue added_objects; - m_env->getRemovedActiveObjects(player, radius, player_radius, + m_env->getRemovedActiveObjects(playersao, radius, player_radius, client->m_known_objects, removed_objects); - m_env->getAddedActiveObjects(player, radius, player_radius, + m_env->getAddedActiveObjects(playersao, radius, player_radius, client->m_known_objects, added_objects); // Ignore if nothing happened @@ -1108,7 +1112,7 @@ PlayerSAO* Server::StageTwoClientInit(u16 peer_id) SendPlayerBreath(peer_id); // Show death screen if necessary - if (player->isDead()) + if (playersao->isDead()) SendDeathscreen(peer_id, false, v3f(0,0,0)); // Note things in chat if not in simple singleplayer mode @@ -1860,18 +1864,18 @@ void Server::SendMovePlayer(u16 peer_id) DSTACK(FUNCTION_NAME); RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); + PlayerSAO *sao = player->getPlayerSAO(); + assert(sao); NetworkPacket pkt(TOCLIENT_MOVE_PLAYER, sizeof(v3f) + sizeof(f32) * 2, peer_id); - pkt << player->getPosition() << player->getPitch() << player->getYaw(); + pkt << sao->getBasePosition() << sao->getPitch() << sao->getYaw(); { - v3f pos = player->getPosition(); - f32 pitch = player->getPitch(); - f32 yaw = player->getYaw(); + v3f pos = sao->getBasePosition(); verbosestream << "Server: Sending TOCLIENT_MOVE_PLAYER" << " pos=(" << pos.X << "," << pos.Y << "," << pos.Z << ")" - << " pitch=" << pitch - << " yaw=" << yaw + << " pitch=" << sao->getPitch() + << " yaw=" << sao->getYaw() << std::endl; } @@ -1984,8 +1988,12 @@ s32 Server::playSound(const SimpleSoundSpec &spec, if (!player) continue; + PlayerSAO *sao = player->getPlayerSAO(); + if (!sao) + continue; + if (pos_exists) { - if(player->getPosition().getDistanceFrom(pos) > + if(sao->getBasePosition().getDistanceFrom(pos) > params.max_hear_distance) continue; } @@ -2044,14 +2052,17 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id, pkt << p; std::vector clients = m_clients.getClientIDs(); - for(std::vector::iterator i = clients.begin(); - i != clients.end(); ++i) { + for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { if (far_players) { // Get player if (RemotePlayer *player = m_env->getPlayer(*i)) { + PlayerSAO *sao = player->getPlayerSAO(); + if (!sao) + continue; + // If player is far away, only set modified blocks not sent - v3f player_pos = player->getPosition(); - if(player_pos.getDistanceFrom(p_f) > maxd) { + v3f player_pos = sao->getBasePosition(); + if (player_pos.getDistanceFrom(p_f) > maxd) { far_players->push_back(*i); continue; } @@ -2071,14 +2082,16 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id, v3f p_f = intToFloat(p, BS); std::vector clients = m_clients.getClientIDs(); - for(std::vector::iterator i = clients.begin(); - i != clients.end(); ++i) { - - if(far_players) { + for(std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { + if (far_players) { // Get player if (RemotePlayer *player = m_env->getPlayer(*i)) { + PlayerSAO *sao = player->getPlayerSAO(); + if (!sao) + continue; + // If player is far away, only set modified blocks not sent - v3f player_pos = player->getPosition(); + v3f player_pos = sao->getBasePosition(); if(player_pos.getDistanceFrom(p_f) > maxd) { far_players->push_back(*i); continue; @@ -3426,10 +3439,9 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id, u16 proto_version return NULL; } - // Load player if it isn't already loaded - if (!player) { - player = m_env->loadPlayer(name); - } + // Create a new player active object + PlayerSAO *playersao = new PlayerSAO(m_env, peer_id, isSingleplayer()); + player = m_env->loadPlayer(name, playersao); // Create player if it doesn't exist if (!player) { @@ -3438,8 +3450,7 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id, u16 proto_version // Set player position infostream<<"Server: Finding spawn place for player \"" <setPosition(pos); + playersao->setBasePosition(findSpawnPos()); // Make sure the player is saved player->setModified(true); @@ -3450,18 +3461,14 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id, u16 proto_version // If the player exists, ensure that they respawn inside legal bounds // This fixes an assert crash when the player can't be added // to the environment - if (objectpos_over_limit(player->getPosition())) { + if (objectpos_over_limit(playersao->getBasePosition())) { actionstream << "Respawn position for player \"" << name << "\" outside limits, resetting" << std::endl; - v3f pos = findSpawnPos(); - player->setPosition(pos); + playersao->setBasePosition(findSpawnPos()); } } - // Create a new player active object - PlayerSAO *playersao = new PlayerSAO(m_env, player, peer_id, - getPlayerEffectivePrivs(player->getName()), - isSingleplayer()); + playersao->initialize(player, getPlayerEffectivePrivs(player->getName())); player->protocol_version = proto_version; diff --git a/src/serverobject.h b/src/serverobject.h index 63650e3be..9884eb0a1 100644 --- a/src/serverobject.h +++ b/src/serverobject.h @@ -85,7 +85,7 @@ public: Some more dynamic interface */ - virtual void setPos(v3f pos) + virtual void setPos(const v3f &pos) { setBasePosition(pos); } // continuous: if true, object does not stop immediately at pos virtual void moveTo(v3f pos, bool continuous) diff --git a/src/unittest/test_player.cpp b/src/unittest/test_player.cpp index 5de9eaaf2..fba422475 100644 --- a/src/unittest/test_player.cpp +++ b/src/unittest/test_player.cpp @@ -46,11 +46,14 @@ void TestPlayer::runTests(IGameDef *gamedef) void TestPlayer::testSave(IGameDef *gamedef) { RemotePlayer rplayer("testplayer_save", gamedef->idef()); - rplayer.setBreath(10); - rplayer.hp = 8; - rplayer.setYaw(0.1f); - rplayer.setPitch(0.6f); - rplayer.setPosition(v3f(450.2f, -15.7f, 68.1f)); + PlayerSAO sao(NULL, 1, false); + sao.initialize(&rplayer, std::set()); + rplayer.setPlayerSAO(&sao); + sao.setBreath(10); + sao.setHP(8, true); + sao.setYaw(0.1f, false); + sao.setPitch(0.6f, false); + sao.setBasePosition(v3f(450.2f, -15.7f, 68.1f)); rplayer.save(".", gamedef); UASSERT(fs::PathExists("testplayer_save")); } @@ -58,24 +61,28 @@ void TestPlayer::testSave(IGameDef *gamedef) void TestPlayer::testLoad(IGameDef *gamedef) { RemotePlayer rplayer("testplayer_load", gamedef->idef()); - rplayer.setBreath(10); - rplayer.hp = 8; - rplayer.setYaw(0.1f); - rplayer.setPitch(0.6f); - rplayer.setPosition(v3f(450.2f, -15.7f, 68.1f)); + PlayerSAO sao(NULL, 1, false); + sao.initialize(&rplayer, std::set()); + rplayer.setPlayerSAO(&sao); + sao.setBreath(10); + sao.setHP(8, true); + sao.setYaw(0.1f, false); + sao.setPitch(0.6f, false); + sao.setBasePosition(v3f(450.2f, -15.7f, 68.1f)); rplayer.save(".", gamedef); UASSERT(fs::PathExists("testplayer_load")); RemotePlayer rplayer_load("testplayer_load", gamedef->idef()); + PlayerSAO sao_load(NULL, 2, false); std::ifstream is("testplayer_load", std::ios_base::binary); UASSERT(is.good()); - rplayer_load.deSerialize(is, "testplayer_load"); + rplayer_load.deSerialize(is, "testplayer_load", &sao_load); is.close(); UASSERT(strcmp(rplayer_load.getName(), "testplayer_load") == 0); - UASSERT(rplayer.getBreath() == 10); - UASSERT(rplayer.hp == 8); - UASSERT(rplayer.getYaw() == 0.1f); - UASSERT(rplayer.getPitch() == 0.6f); - UASSERT(rplayer.getPosition() == v3f(450.2f, -15.7f, 68.1f)); + UASSERT(sao_load.getBreath() == 10); + UASSERT(sao_load.getHP() == 8); + UASSERT(sao_load.getYaw() == 0.1f); + UASSERT(sao_load.getPitch() == 0.6f); + UASSERT(sao_load.getBasePosition() == v3f(450.2f, -15.7f, 68.1f)); } -- cgit v1.2.3 From 595932a8602292f28333ce14e20cee4b6d8820c1 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sun, 30 Oct 2016 16:12:09 +0100 Subject: Fix overloading problems mentioned by clang --- src/content_sao.cpp | 28 ++++++++++++++-------------- src/content_sao.h | 13 +++++++++---- src/network/serverpackethandler.cpp | 4 ++-- src/remoteplayer.cpp | 8 ++++---- src/script/lua_api/l_object.cpp | 8 ++++---- src/unittest/test_player.cpp | 12 ++++++------ 6 files changed, 39 insertions(+), 34 deletions(-) (limited to 'src/script') diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 23a064085..5fb8f936e 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -507,7 +507,7 @@ void LuaEntitySAO::rightClick(ServerActiveObject *clicker) m_env->getScriptIface()->luaentity_Rightclick(m_id, clicker); } -void LuaEntitySAO::setPos(v3f pos) +void LuaEntitySAO::setPos(const v3f &pos) { if(isAttached()) return; @@ -1078,27 +1078,32 @@ void PlayerSAO::moveTo(v3f pos, bool continuous) ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } -void PlayerSAO::setYaw(const float yaw, bool send_data) +void PlayerSAO::setYaw(const float yaw) { if (m_player && yaw != m_yaw) m_player->setDirty(true); UnitSAO::setYaw(yaw); +} - // Datas should not be sent at player initialization - if (send_data) - ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); +void PlayerSAO::setYawAndSend(const float yaw) +{ + setYaw(yaw); + ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } -void PlayerSAO::setPitch(const float pitch, bool send_data) +void PlayerSAO::setPitch(const float pitch) { if (m_player && pitch != m_pitch) m_player->setDirty(true); m_pitch = pitch; +} - if (send_data) - ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); +void PlayerSAO::setPitchAndSend(const float pitch) +{ + setPitch(pitch); + ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } int PlayerSAO::punch(v3f dir, @@ -1173,13 +1178,8 @@ s16 PlayerSAO::readDamage() return damage; } -void PlayerSAO::setHP(s16 hp, bool direct) +void PlayerSAO::setHP(s16 hp) { - if (direct) { - m_hp = hp; - return; - } - s16 oldhp = m_hp; s16 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - oldhp); diff --git a/src/content_sao.h b/src/content_sao.h index 4ea6277ff..5d837a466 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -73,7 +73,7 @@ public: ServerActiveObject *puncher=NULL, float time_from_last_punch=1000000); void rightClick(ServerActiveObject *clicker); - void setPos(v3f pos); + void setPos(const v3f &pos); void moveTo(v3f pos, bool continuous); float getMinimumSavedMovement(); std::string getDescription(); @@ -204,8 +204,12 @@ public: void setBasePosition(const v3f &position); void setPos(const v3f &pos); void moveTo(v3f pos, bool continuous); - void setYaw(const float yaw, bool send_data = true); - void setPitch(const float pitch, bool send_data = true); + void setYaw(const float yaw); + // Data should not be sent at player initialization + void setYawAndSend(const float yaw); + void setPitch(const float pitch); + // Data should not be sent at player initialization + void setPitchAndSend(const float pitch); f32 getPitch() const { return m_pitch; } f32 getRadPitch() const { return m_pitch * core::DEGTORAD; } // Deprecated @@ -220,7 +224,8 @@ public: ServerActiveObject *puncher, float time_from_last_punch); void rightClick(ServerActiveObject *clicker); - void setHP(s16 hp, bool direct = false); + void setHP(s16 hp); + void setHPRaw(s16 hp) { m_hp = hp; } s16 readDamage(); u16 getBreath() const { return m_breath; } void setBreath(const u16 breath); diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index 5e70b4c6c..80eec140d 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -827,8 +827,8 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt) playersao->setBasePosition(position); player->setSpeed(speed); - playersao->setPitch(pitch, false); - playersao->setYaw(yaw, false); + playersao->setPitch(pitch); + playersao->setYaw(yaw); player->keyPressed = keyPressed; player->control.up = (keyPressed & 1); player->control.down = (keyPressed & 2); diff --git a/src/remoteplayer.cpp b/src/remoteplayer.cpp index 605346928..f4a79dd08 100644 --- a/src/remoteplayer.cpp +++ b/src/remoteplayer.cpp @@ -131,9 +131,9 @@ void RemotePlayer::deSerialize(std::istream &is, const std::string &playername, if (sao) { try { - sao->setHP(args.getS32("hp"), true); + sao->setHPRaw(args.getS32("hp")); } catch(SettingNotFoundException &e) { - sao->setHP(PLAYER_MAX_HP, true); + sao->setHPRaw(PLAYER_MAX_HP); } try { @@ -141,10 +141,10 @@ void RemotePlayer::deSerialize(std::istream &is, const std::string &playername, } catch (SettingNotFoundException &e) {} try { - sao->setPitch(args.getFloat("pitch"), false); + sao->setPitch(args.getFloat("pitch")); } catch (SettingNotFoundException &e) {} try { - sao->setYaw(args.getFloat("yaw"), false); + sao->setYaw(args.getFloat("yaw")); } catch (SettingNotFoundException &e) {} try { diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index cf124f17c..42395717f 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1090,7 +1090,7 @@ int ObjectRef::l_set_look_vertical(lua_State *L) if (co == NULL) return 0; float pitch = luaL_checknumber(L, 2) * core::RADTODEG; // Do it - co->setPitch(pitch); + co->setPitchAndSend(pitch); return 1; } @@ -1103,7 +1103,7 @@ int ObjectRef::l_set_look_horizontal(lua_State *L) if (co == NULL) return 0; float yaw = luaL_checknumber(L, 2) * core::RADTODEG; // Do it - co->setYaw(yaw); + co->setYawAndSend(yaw); return 1; } @@ -1121,7 +1121,7 @@ int ObjectRef::l_set_look_pitch(lua_State *L) if (co == NULL) return 0; float pitch = luaL_checknumber(L, 2) * core::RADTODEG; // Do it - co->setPitch(pitch); + co->setPitchAndSend(pitch); return 1; } @@ -1139,7 +1139,7 @@ int ObjectRef::l_set_look_yaw(lua_State *L) if (co == NULL) return 0; float yaw = luaL_checknumber(L, 2) * core::RADTODEG; // Do it - co->setYaw(yaw); + co->setYawAndSend(yaw); return 1; } diff --git a/src/unittest/test_player.cpp b/src/unittest/test_player.cpp index fba422475..85fbc8b2d 100644 --- a/src/unittest/test_player.cpp +++ b/src/unittest/test_player.cpp @@ -50,9 +50,9 @@ void TestPlayer::testSave(IGameDef *gamedef) sao.initialize(&rplayer, std::set()); rplayer.setPlayerSAO(&sao); sao.setBreath(10); - sao.setHP(8, true); - sao.setYaw(0.1f, false); - sao.setPitch(0.6f, false); + sao.setHPRaw(8); + sao.setYaw(0.1f); + sao.setPitch(0.6f); sao.setBasePosition(v3f(450.2f, -15.7f, 68.1f)); rplayer.save(".", gamedef); UASSERT(fs::PathExists("testplayer_save")); @@ -65,9 +65,9 @@ void TestPlayer::testLoad(IGameDef *gamedef) sao.initialize(&rplayer, std::set()); rplayer.setPlayerSAO(&sao); sao.setBreath(10); - sao.setHP(8, true); - sao.setYaw(0.1f, false); - sao.setPitch(0.6f, false); + sao.setHPRaw(8); + sao.setYaw(0.1f); + sao.setPitch(0.6f); sao.setBasePosition(v3f(450.2f, -15.7f, 68.1f)); rplayer.save(".", gamedef); UASSERT(fs::PathExists("testplayer_load")); -- cgit v1.2.3 From 70e2df4f86cf4101a10c7e1f7a6ac5cc03992793 Mon Sep 17 00:00:00 2001 From: paramat Date: Sat, 29 Oct 2016 15:22:18 +0100 Subject: Lua voxelmanip: Add optional buffer param for 'get param2 data' Update lua_api.txt. --- doc/lua_api.txt | 4 +++- src/script/lua_api/l_vmanip.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 43ff66bb9..da90f93d2 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -3263,7 +3263,9 @@ will place the schematic inside of the VoxelManip. * `set_light_data(light_data)`: Sets the `param1` (light) contents of each node in the `VoxelManip` * expects lighting data in the same format that `get_light_data()` returns -* `get_param2_data()`: Gets the raw `param2` data read into the `VoxelManip` object +* `get_param2_data([buffer])`: Gets the raw `param2` data read into the `VoxelManip` object + * Returns an array (indices 1 to volume) of integers ranging from `0` to `255` + * If the param `buffer` is present, this table will be used to store the result instead * `set_param2_data(param2_data)`: Sets the `param2` contents of each node in the `VoxelManip` * `calc_lighting([p1, p2], [propagate_shadow])`: Calculate lighting within the `VoxelManip` * To be used only by a `VoxelManip` object from `minetest.get_mapgen_object` diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index 0d8123acd..bdf720f0a 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -277,11 +277,17 @@ int LuaVoxelManip::l_get_param2_data(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaVoxelManip *o = checkobject(L, 1); + bool use_buffer = lua_istable(L, 2); + MMVManip *vm = o->vm; u32 volume = vm->m_area.getVolume(); - lua_newtable(L); + if (use_buffer) + lua_pushvalue(L, 2); + else + lua_newtable(L); + for (u32 i = 0; i != volume; i++) { lua_Integer param2 = vm->m_data[i].param2; lua_pushinteger(L, param2); -- cgit v1.2.3 From 7607b0ac2065a50a9b68b22909ab40738f2d08e8 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Tue, 6 May 2014 22:31:35 -0400 Subject: Add version API --- builtin/mainmenu/tab_credits.lua | 3 ++- doc/lua_api.txt | 11 +++++++++++ src/script/lua_api/l_mainmenu.cpp | 9 --------- src/script/lua_api/l_mainmenu.h | 2 -- src/script/lua_api/l_util.cpp | 29 +++++++++++++++++++++++++++++ src/script/lua_api/l_util.h | 3 +++ 6 files changed, 45 insertions(+), 12 deletions(-) (limited to 'src/script') diff --git a/builtin/mainmenu/tab_credits.lua b/builtin/mainmenu/tab_credits.lua index 4d2ffd7f4..9b6045263 100644 --- a/builtin/mainmenu/tab_credits.lua +++ b/builtin/mainmenu/tab_credits.lua @@ -76,8 +76,9 @@ return { caption = fgettext("Credits"), cbf_formspec = function(tabview, name, tabdata) local logofile = defaulttexturedir .. "logo.png" + local version = core.get_version() return "image[0.5,1;" .. core.formspec_escape(logofile) .. "]" .. - "label[0.5,3.2;Minetest " .. core.get_version() .. "]" .. + "label[0.5,3.2;" .. version.project .. " " .. version.string .. "]" .. "label[0.5,3.5;http://minetest.net]" .. "tablecolumns[color;text]" .. "tableoptions[background=#00000000;highlight=#00000000;border=false]" .. diff --git a/doc/lua_api.txt b/doc/lua_api.txt index da90f93d2..497864fac 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1908,6 +1908,17 @@ Helper functions * nil: return all entries, * true: return only subdirectory names, or * false: return only file names. +* `minetest.get_version()`: returns a table containing components of the + engine version. Components: + * `project`: Name of the project, eg, "Minetest" + * `string`: Simple version, eg, "1.2.3-dev" + * `hash`: Full git version (only set if available), eg, "1.2.3-dev-01234567-dirty" + Use this for informational purposes only. The information in the returned + table does not represent the capabilities of the engine, nor is it + reliable or verifyable. Compatible forks will have a different name and + version entirely. To check for the presence of engine features, test + whether the functions exported by the wanted features exist. For example: + `if core.nodeupdate then ... end`. ### Logging * `minetest.debug(...)` diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 8b078eafd..4a2484613 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -955,13 +955,6 @@ int ModApiMainMenu::l_show_file_open_dialog(lua_State *L) return 0; } -/******************************************************************************/ -int ModApiMainMenu::l_get_version(lua_State *L) -{ - lua_pushstring(L, g_version_string); - return 1; -} - /******************************************************************************/ int ModApiMainMenu::l_sound_play(lua_State *L) { @@ -1157,7 +1150,6 @@ void ModApiMainMenu::Initialize(lua_State *L, int top) API_FCT(extract_zip); API_FCT(get_mainmenu_path); API_FCT(show_file_open_dialog); - API_FCT(get_version); API_FCT(download_file); API_FCT(get_modstore_details); API_FCT(get_modstore_list); @@ -1188,7 +1180,6 @@ void ModApiMainMenu::InitializeAsync(AsyncEngine& engine) ASYNC_API_FCT(delete_dir); ASYNC_API_FCT(copy_dir); //ASYNC_API_FCT(extract_zip); //TODO remove dependency to GuiEngine - ASYNC_API_FCT(get_version); ASYNC_API_FCT(download_file); ASYNC_API_FCT(get_modstore_details); ASYNC_API_FCT(get_modstore_list); diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h index 405af25e8..ad5155ac6 100644 --- a/src/script/lua_api/l_mainmenu.h +++ b/src/script/lua_api/l_mainmenu.h @@ -79,8 +79,6 @@ private: static int l_delete_favorite(lua_State *L); - static int l_get_version(lua_State *L); - static int l_sound_play(lua_State *L); static int l_sound_stop(lua_State *L); diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index fa2d15b03..818c1aeeb 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -33,8 +33,11 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "settings.h" #include "util/auth.h" #include "util/base64.h" +#include "config.h" +#include "version.h" #include + // log([level,] text) // Writes a line to the logger. // The one-argument version logs to infostream. @@ -302,12 +305,14 @@ int ModApiUtil::l_is_yes(lua_State *L) return 1; } +// get_builtin_path() int ModApiUtil::l_get_builtin_path(lua_State *L) { NO_MAP_LOCK_REQUIRED; std::string path = porting::path_share + DIR_DELIM + "builtin"; lua_pushstring(L, path.c_str()); + return 1; } @@ -460,6 +465,26 @@ int ModApiUtil::l_request_insecure_environment(lua_State *L) return 1; } +// get_version() +int ModApiUtil::l_get_version(lua_State *L) +{ + lua_createtable(L, 0, 3); + int table = lua_gettop(L); + + lua_pushstring(L, PROJECT_NAME_C); + lua_setfield(L, table, "project"); + + lua_pushstring(L, g_version_string); + lua_setfield(L, table, "string"); + + if (strcmp(g_version_string, g_version_hash)) { + lua_pushstring(L, g_version_hash); + lua_setfield(L, table, "hash"); + } + + return 1; +} + void ModApiUtil::Initialize(lua_State *L, int top) { @@ -496,6 +521,8 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(encode_base64); API_FCT(decode_base64); + + API_FCT(get_version); } void ModApiUtil::InitializeAsync(AsyncEngine& engine) @@ -525,5 +552,7 @@ void ModApiUtil::InitializeAsync(AsyncEngine& engine) ASYNC_API_FCT(encode_base64); ASYNC_API_FCT(decode_base64); + + ASYNC_API_FCT(get_version); } diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index 3012d55aa..9910704b3 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -104,6 +104,9 @@ private: // decode_base64(string) static int l_decode_base64(lua_State *L); + // get_version() + static int l_get_version(lua_State *L); + public: static void Initialize(lua_State *L, int top); -- cgit v1.2.3 From bf315c05f17549d37d7b4c4bd7938df0b5b32c5a Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 2 Nov 2016 11:36:58 -0500 Subject: Add minetest.get_server_uptime() function to Lua API (#4702) Add minetest.get_server_uptime() function to Lua API --- doc/lua_api.txt | 1 + src/script/lua_api/l_server.cpp | 10 ++++++++++ src/script/lua_api/l_server.h | 3 +++ src/server.h | 1 + 4 files changed, 15 insertions(+) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 497864fac..119e69f6f 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2448,6 +2448,7 @@ These functions return the leftover itemstack. * `minetest.request_shutdown([message],[reconnect])`: request for server shutdown. Will display `message` to clients, and `reconnect` == true displays a reconnect button. * `minetest.get_server_status()`: returns server status string +* `minetest.get_server_uptime()`: returns the server uptime in seconds ### Bans * `minetest.get_ban_list()`: returns the ban list (same as `minetest.get_ban_description("")`) diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index 95e5da07f..b6d44e0ff 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -45,6 +45,15 @@ int ModApiServer::l_get_server_status(lua_State *L) return 1; } +// get_server_uptime() +int ModApiServer::l_get_server_uptime(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + lua_pushnumber(L, getServer(L)->getUptime()); + return 1; +} + + // print(text) int ModApiServer::l_print(lua_State *L) { @@ -507,6 +516,7 @@ void ModApiServer::Initialize(lua_State *L, int top) { API_FCT(request_shutdown); API_FCT(get_server_status); + API_FCT(get_server_uptime); API_FCT(get_worldpath); API_FCT(is_singleplayer); diff --git a/src/script/lua_api/l_server.h b/src/script/lua_api/l_server.h index 06a5ddc24..1ad46d440 100644 --- a/src/script/lua_api/l_server.h +++ b/src/script/lua_api/l_server.h @@ -30,6 +30,9 @@ private: // get_server_status() static int l_get_server_status(lua_State *L); + // get_server_uptime() + static int l_get_server_uptime(lua_State *L); + // get_worldpath() static int l_get_worldpath(lua_State *L); diff --git a/src/server.h b/src/server.h index 5fc2a9133..f8b3ec106 100644 --- a/src/server.h +++ b/src/server.h @@ -216,6 +216,7 @@ public: // Connection must be locked when called std::wstring getStatusString(); + inline double getUptime() const { return m_uptime.m_value; } // read shutdown state inline bool getShutdownRequested() const { return m_shutdown_requested; } -- cgit v1.2.3 From b5c84c34ce283a56317902d24067d9a0795a44c5 Mon Sep 17 00:00:00 2001 From: Zeno- Date: Sat, 5 Nov 2016 15:10:49 +1000 Subject: Fix memory leak in ::safeLoadFile (#4730) --- src/script/cpp_api/s_security.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/script') diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index 0e64c4c61..3a8f724fe 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -323,9 +323,12 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path) } if (luaL_loadbuffer(L, code, size, chunk_name)) { + delete [] code; return false; } + delete [] code; + if (path) { delete [] chunk_name; } -- cgit v1.2.3 From 1980d9ea31e969c5b604f6ee01693cbcfc2c795a Mon Sep 17 00:00:00 2001 From: Rogier Date: Sun, 6 Nov 2016 16:18:29 +0100 Subject: Fix crash when attached object no longer exists Active objects that are attached to other objects are not safe from deletion. As a result, the parent object may have a reference to an id of a child's that no longer exists. If at some point an attempt is made to manipulate the child, enviromment->getActiveObject(child-id) returns NULL. Using the NULL pointer causes the crash... --- src/script/lua_api/l_object.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/script') diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 42395717f..2a8b8a64e 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -140,8 +140,9 @@ int ObjectRef::l_remove(lua_State *L) UNORDERED_SET child_ids = co->getAttachmentChildIds(); UNORDERED_SET::iterator it; for (it = child_ids.begin(); it != child_ids.end(); ++it) { - ServerActiveObject *child = env->getActiveObject(*it); - child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0)); + // Child can be NULL if it was deleted earlier + if (ServerActiveObject *child = env->getActiveObject(*it)) + child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0)); } verbosestream<<"ObjectRef::l_remove(): id="<getId()< Date: Mon, 14 Nov 2016 18:09:59 +0400 Subject: Adding particle blend, glow and animation (#4705) --- builtin/common/misc_helpers.lua | 37 +++++++ doc/lua_api.txt | 189 ++++++++++++++++++++++++++++++++- src/client.h | 18 ++++ src/network/clientpackethandler.cpp | 111 ++++++++++++++------ src/nodedef.cpp | 4 +- src/nodedef.h | 11 +- src/particles.cpp | 203 +++++++++++++++++++++++++++++++++--- src/particles.h | 32 +++++- src/script/common/c_content.cpp | 13 +-- src/script/common/c_content.h | 2 +- src/script/common/c_converter.cpp | 22 ++++ src/script/common/c_converter.h | 2 + src/script/lua_api/l_particles.cpp | 166 ++++++++++++++++++++++++++++- src/server.cpp | 44 ++++++-- src/server.h | 27 ++++- 15 files changed, 800 insertions(+), 81 deletions(-) (limited to 'src/script') diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index c2dc7514d..a495058d9 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -237,6 +237,43 @@ function math.sign(x, tolerance) return 0 end +-------------------------------------------------------------------------------- +-- Video enums and pack function + +-- E_BLEND_FACTOR +minetest.ebf = { + zero = 0, -- src & dest (0, 0, 0, 0) + one = 1, -- src & dest (1, 1, 1, 1) + dst_color = 2, -- src (destR, destG, destB, destA) + one_minus_dst_color = 3, -- src (1-destR, 1-destG, 1-destB, 1-destA) + src_color = 4, -- dest (srcR, srcG, srcB, srcA) + one_minus_src_color = 5, -- dest (1-srcR, 1-srcG, 1-srcB, 1-srcA) + src_alpha = 6, -- src & dest (srcA, srcA, srcA, srcA) + one_minus_src_alpha = 7, -- src & dest (1-srcA, 1-srcA, 1-srcA, 1-srcA) + dst_alpha = 8, -- src & dest (destA, destA, destA, destA) + one_minus_dst_alpha = 9, -- src & dest (1-destA, 1-destA, 1-destA, 1-destA) + src_alpha_saturate = 10,-- src (min(srcA, 1-destA), idem, ...) +} + +-- E_MODULATE_FUNC +minetest.emfn = { + modulate_1x = 1, + modulate_2x = 2, + modulate_4x = 4, +} + +-- E_ALPHA_SOURCE +minetest.eas = { + none = 0, + vertex_color = 1, + texture = 2, +} + +-- BlendFunc = source * sourceFactor + dest * destFactor +function minetest.pack_texture_blend_func(srcFact, dstFact, modulate, alphaSource) + return alphaSource * 4096 + modulate * 256 + srcFact * 16 + dstFact +end + -------------------------------------------------------------------------------- function get_last_folder(text,count) local parts = text:split(DIR_DELIM) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 7d552c980..3b3f17634 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -414,6 +414,119 @@ the word "`alpha`", then each texture pixel will contain the RGB of `` and the alpha of `` multiplied by the alpha of the texture pixel. +Particle blend +-------------- +Blend function is defined by integer number. +There is a huge number of acceptable blend modificators. +Colour of a resulting pixel calculated using formulae: + + red = source_red * source_factor + destination_red * destination_factor + +and so on for every channel. + +Here is a some examples: + +Default value: + + material_type_param = 0, + +Use this value to disable blending. Texture will be applied to existing pixels +using alpha channel of it. Its recomended to use 1-bit alpha +in that case. This value will leave z-buffer writeable. + +Additive blend: + + material_type_param = 12641, + +Source = src_alpha, destination = one, alpha source is a texture and +vertex_color, modulate_1x. +Black color is completely transparent, white color is completely opaque. +Alpha channel still used to calculate result color, but not nessesary. +'destination = one' means that resulting color will be calculated using +overwritten pixels values. +For example with color of source (our texture) RGBA = (0,192,255,63) +"blue-cyan", 1/4 opaque. +and already rendered pixel color (40,192,0) "dark lime green" we will get color: + +R = source_red(0) * source_factor(src_alpha=63/255) + + destination_red(40) * destination_factor(one) = + 0 * 63/255 + 40 * 1 = 40 + +G = 192 * 63/255 + 192 * 1 = 239 +B = 255 * 63/255 + 0 * 1 = 63 + +Result: (40,239,63), "green" (a kind of). +Note, if you made a texture with some kind of shape with colour 662211h +it will appear dark red with a single particle, then yellow with a +several of them and white if player looking thru a lot of them. With +this you could made a nice-looking fire. + +Substractive blend: + + material_type_param = 12548, + +Source = zero, destination = src_color, alpha source is a texture and +vertex_color, modulate_1x. +Texture darkness act like an alpha channel. +Black color is completely opaque, white color is completely transparent. +'destination = src_color' means that destination in multiplied by +a source values. 'source = zero' means that source values ignored +(multiplied by 0). + +Invert blend: + + material_type_param = 12597, + +Source = one_minus_dst_color, destination = one_minus_src_alpha, alpha source +is a texture and vertex_color, modulate_1x. +Pixels invert color if source color value is big enough. If not, they just +black. +'destination = one_minus_src_alpha' means, that effect is masked by a +source alpha channel. + +You can design and use your own blend using those enum values and function +'minetest.pack_texture_blend_func'. Returned value of a function is +your 'material_type_param'. + +A values in a brackets is a multiplicators of a red, green, blue +and alpha channels respectively. + +* 'minetest.ebf': global table, containing blend factor enum values. Such as: + * zero = 0 -- src & dest (0, 0, 0, 0) + * one = 1 -- src & dest (1, 1, 1, 1) + * dst_color = 2 -- src (destR, destG, destB, destA) + * one_minus_dst_color = 3 -- src (1-destR, 1-destG, 1-destB, 1-destA) + * src_color = 4 -- dest (srcR, srcG, srcB, srcA) + * one_minus_src_color = 5 -- dest (1-srcR, 1-srcG, 1-srcB, 1-srcA) + * src_alpha = 6 -- src & dest (srcA, srcA, srcA, srcA) + * one_minus_src_alpha = 7 -- src & dest (1-srcA, 1-srcA, 1-srcA, 1-srcA) + * dst_alpha = 8 -- src & dest (destA, destA, destA, destA) + * one_minus_dst_alpha = 9 -- src & dest (1-destA, 1-destA, 1-destA, 1-destA) + * src_alpha_saturate = 10 -- src (min(srcA, 1-destA), idem, ...) + +* 'minetest.emfn': global table, containing modulate enum values. + * Multiply the components of the arguments, and shift the products to the + * left by x bits for brightening. Contain: + * modulate_1x = 1 -- no bit shift + * modulate_2x = 2 -- 1 bits shift + * modulate_4x = 4 -- 2 bits shift + +'modulate_4x' is quite useful when you want to make additive blend stronger +with a lower amount of particles. + +* 'minetest.eas': global table, containing alpha source enum values. Such as: + * none = 0 -- do not use alpha. + * vertex_color = 1 -- use vertex color alpha. + * texture = 2 -- use texture alpha. + +You can use both 'vertex_color' and 'texture' source by using value 3. + +* 'minetest.pack_texture_blend_func(srcFact, dstFact, modulate, alphaSource)': return integer + * Pack texture blend funcion variable. Depending from that variable blend + * function will be applied in time of a render poligons with selected material. + * Therefore resulting pixel will be 'source * srcFact + destination * dstFact' + * Use result of this function as 'material_type_param'. + Sounds ------ Only Ogg Vorbis files are supported. @@ -3650,7 +3763,7 @@ Definition tables ### Tile definition * `"image.png"` -* `{name="image.png", animation={Tile Animation definition}}` +* `{name="image.png", animation={Animation definition}}` * `{name="image.png", backface_culling=bool, tileable_vertical=bool, tileable_horizontal=bool}` * backface culling enabled by default for most nodes @@ -3661,8 +3774,50 @@ Definition tables * deprecated, yet still supported field names: * `image` (name) -### Tile animation definition -* `{type="vertical_frames", aspect_w=16, aspect_h=16, length=3.0}` +### Animation definition + +#### Node animation, particle and particle spawners +* `{ type="vertical_frames", + aspect_w=16, + -- ^ specify width of a picture in pixels. + aspect_h=16, + -- ^ specify height of a frame in pixels. + length=3.0 + -- ^ specify full loop length. + first_frame = 0, -- <- only for particles, use + min_first_frame = 0, -- <- for particle spawners + max_first_frame = 0, + loop_animation = true, -- <- only for particles and particle spawners + -- specify if animation should start from beginning after last frame. +}` + +#### Particle and particle spawners only +* `{ + type="2d_animation_sheet", -- <- only for particles and particle spawners + vertical_frame_num = 1, + horizontal_frame_num = 1, + -- ^ specify amount of frames in texture. + -- Can be used both for animation or for texture transform + -- together with first_frame variable. + -- A animation texture separated on equal parts of frames, + -- by horizontal and vertical numbers. For example with + -- vertical_frame_num = 4 and horizontal_frame_num = 3 we got + -- 4*3 = 12 frames in total. Animation sequence start from + -- left top frame and go on to the right until reach end of + -- row. Next row also start from left frame. + first_frame = 0, -- <- only for particles, use + min_first_frame = 0, -- <- for particle spawners + max_first_frame = 0, + -- ^ specify first frame to start animation. + frame_length = -1, + -- ^ specify length of a frame in seconds. Negative and zero values + -- disable animation. A sequence with vertical_frame_num = 4 and + -- horizontal_frame_num = 3, first_frame = 4 and frame_length = 0.1 + -- will end in (4*3-4)*0.1 = 0.8 seconds. + loop_animation = true, + -- specify if animation should start from beginning after last frame. +}` + * All settings are optional. Default values is located in this example. ### Node definition (`register_node`) @@ -4117,6 +4272,20 @@ The Biome API is still in an experimental phase and subject to change. -- ^ Uses texture (string) playername = "singleplayer" -- ^ optional, if specified spawns particle only on the player's client + material_type_param = 12641, + -- ^ optional, if specified spawns particle with + -- specified material type param and disable z-buffer. + -- Some examples: + -- Default value: 0, + -- Additive blend: 12641, + -- Substractive blend: 12548, + -- Invert blend: 12597, + -- See also "Particle blend". + animation = {Animation definition}, + -- ^ see above. Note, that particle and particle spawners have differences. + glow = 15, + -- ^ optional, specify particle self-luminescence in darkness. + values may vary from 0 (no glow) to 15 (bright glow). } ### `ParticleSpawner` definition (`add_particlespawner`) @@ -4151,6 +4320,20 @@ The Biome API is still in an experimental phase and subject to change. -- ^ Uses texture (string) playername = "singleplayer" -- ^ Playername is optional, if specified spawns particle only on the player's client + material_type_param = 12641, + -- ^ optional, if specified spawns particle with specified material type + -- param and disable z-buffer. + -- Some examples: + -- Default value: 0, + -- Additive blend: 12641, + -- Substractive blend: 12548, + -- Invert blend: 12597, + -- See also "Particle blend". + animation = {Animation definition}, + -- ^ see above. Note, that particle and particle spawners have differences. + glow = 15, + -- ^ optional, specify particle self-luminescence in darkness. + values may vary from 0 (no glow) to 15 (bright glow). } ### `HTTPRequest` definition (`HTTPApiTable.fetch_async`, `HTTPApiTable.fetch_async`) diff --git a/src/client.h b/src/client.h index 9f5bda059..c51daf7bc 100644 --- a/src/client.h +++ b/src/client.h @@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "hud.h" #include "particles.h" #include "network/networkpacket.h" +#include "nodedef.h" // AnimationType struct MeshMakeData; class MapBlockMesh; @@ -185,6 +186,14 @@ struct ClientEvent bool collision_removal; bool vertical; std::string *texture; + u32 material_type_param; + AnimationType animation_type; + u16 vertical_frame_num; + u16 horizontal_frame_num; + u16 first_frame; + float frame_length; + bool loop_animation; + u8 glow; } spawn_particle; struct{ u16 amount; @@ -205,6 +214,15 @@ struct ClientEvent bool vertical; std::string *texture; u32 id; + u32 material_type_param; + AnimationType animation_type; + u16 vertical_frame_num; + u16 horizontal_frame_num; + u16 min_first_frame; + u16 max_first_frame; + float frame_length; + bool loop_animation; + u8 glow; } add_particlespawner; struct{ u32 id; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 411982f69..03baf078a 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -896,23 +896,46 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt) std::string texture = deSerializeLongString(is); bool vertical = false; bool collision_removal = false; + u32 material_type_param = 0; + AnimationType animation_type = AT_NONE; + u16 vertical_frame_num = 1; + u16 horizontal_frame_num = 1; + u16 first_frame = 0; + float frame_length = -1; + bool loop_animation = true; + u8 glow = 0; try { vertical = readU8(is); collision_removal = readU8(is); + material_type_param = readU32(is); + animation_type = (AnimationType)readU8(is); + vertical_frame_num = readU16(is); + horizontal_frame_num = readU16(is); + first_frame = readU16(is); + frame_length = readF1000(is); + loop_animation = readU8(is); + glow = readU8(is); } catch (...) {} ClientEvent event; - event.type = CE_SPAWN_PARTICLE; - event.spawn_particle.pos = new v3f (pos); - event.spawn_particle.vel = new v3f (vel); - event.spawn_particle.acc = new v3f (acc); - event.spawn_particle.expirationtime = expirationtime; - event.spawn_particle.size = size; - event.spawn_particle.collisiondetection = collisiondetection; - event.spawn_particle.collision_removal = collision_removal; - event.spawn_particle.vertical = vertical; - event.spawn_particle.texture = new std::string(texture); - + event.type = CE_SPAWN_PARTICLE; + event.spawn_particle.pos = new v3f (pos); + event.spawn_particle.vel = new v3f (vel); + event.spawn_particle.acc = new v3f (acc); + event.spawn_particle.expirationtime = expirationtime; + event.spawn_particle.size = size; + event.spawn_particle.collisiondetection = collisiondetection; + event.spawn_particle.collision_removal = collision_removal; + event.spawn_particle.vertical = vertical; + event.spawn_particle.texture = new std::string(texture); + event.spawn_particle.material_type_param = material_type_param; + event.spawn_particle.animation_type = animation_type; + event.spawn_particle.vertical_frame_num = vertical_frame_num; + event.spawn_particle.horizontal_frame_num = horizontal_frame_num; + event.spawn_particle.first_frame = first_frame; + event.spawn_particle.frame_length = frame_length; + event.spawn_particle.loop_animation = loop_animation; + event.spawn_particle.glow = glow; m_client_event_queue.push(event); } @@ -932,6 +955,15 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) float maxsize; bool collisiondetection; u32 id; + u32 material_type_param = 0; + u8 animation_type = (u8)AT_NONE; + u16 vertical_frame_num = 1; + u16 horizontal_frame_num = 1; + u16 min_first_frame = 0; + u16 max_first_frame = 0; + float frame_length = -1; + bool loop_animation = true; + u8 glow = 0; *pkt >> amount >> spawntime >> minpos >> maxpos >> minvel >> maxvel >> minacc >> maxacc >> minexptime >> maxexptime >> minsize @@ -948,29 +980,46 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) *pkt >> vertical; *pkt >> collision_removal; *pkt >> attached_id; - + *pkt >> material_type_param; + *pkt >> animation_type; + *pkt >> vertical_frame_num; + *pkt >> horizontal_frame_num; + *pkt >> min_first_frame; + *pkt >> max_first_frame; + *pkt >> frame_length; + *pkt >> loop_animation; + *pkt >> glow; } catch (...) {} ClientEvent event; - event.type = CE_ADD_PARTICLESPAWNER; - event.add_particlespawner.amount = amount; - event.add_particlespawner.spawntime = spawntime; - event.add_particlespawner.minpos = new v3f (minpos); - event.add_particlespawner.maxpos = new v3f (maxpos); - event.add_particlespawner.minvel = new v3f (minvel); - event.add_particlespawner.maxvel = new v3f (maxvel); - event.add_particlespawner.minacc = new v3f (minacc); - event.add_particlespawner.maxacc = new v3f (maxacc); - event.add_particlespawner.minexptime = minexptime; - event.add_particlespawner.maxexptime = maxexptime; - event.add_particlespawner.minsize = minsize; - event.add_particlespawner.maxsize = maxsize; - event.add_particlespawner.collisiondetection = collisiondetection; - event.add_particlespawner.collision_removal = collision_removal; - event.add_particlespawner.attached_id = attached_id; - event.add_particlespawner.vertical = vertical; - event.add_particlespawner.texture = new std::string(texture); - event.add_particlespawner.id = id; + event.type = CE_ADD_PARTICLESPAWNER; + event.add_particlespawner.amount = amount; + event.add_particlespawner.spawntime = spawntime; + event.add_particlespawner.minpos = new v3f (minpos); + event.add_particlespawner.maxpos = new v3f (maxpos); + event.add_particlespawner.minvel = new v3f (minvel); + event.add_particlespawner.maxvel = new v3f (maxvel); + event.add_particlespawner.minacc = new v3f (minacc); + event.add_particlespawner.maxacc = new v3f (maxacc); + event.add_particlespawner.minexptime = minexptime; + event.add_particlespawner.maxexptime = maxexptime; + event.add_particlespawner.minsize = minsize; + event.add_particlespawner.maxsize = maxsize; + event.add_particlespawner.collisiondetection = collisiondetection; + event.add_particlespawner.collision_removal = collision_removal; + event.add_particlespawner.attached_id = attached_id; + event.add_particlespawner.vertical = vertical; + event.add_particlespawner.texture = new std::string(texture); + event.add_particlespawner.id = id; + event.add_particlespawner.material_type_param = material_type_param; + event.add_particlespawner.animation_type = (AnimationType)animation_type; + event.add_particlespawner.vertical_frame_num = vertical_frame_num; + event.add_particlespawner.horizontal_frame_num = horizontal_frame_num; + event.add_particlespawner.min_first_frame = min_first_frame; + event.add_particlespawner.max_first_frame = max_first_frame; + event.add_particlespawner.frame_length = frame_length; + event.add_particlespawner.loop_animation = loop_animation; + event.add_particlespawner.glow = glow; m_client_event_queue.push(event); } diff --git a/src/nodedef.cpp b/src/nodedef.cpp index 39ea1a60e..c690e6720 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -211,7 +211,7 @@ void TileDef::deSerialize(std::istream &is, const u8 contenfeatures_version, con { int version = readU8(is); name = deSerializeString(is); - animation.type = (TileAnimationType)readU8(is); + animation.type = (AnimationType)readU8(is); animation.aspect_w = readU16(is); animation.aspect_h = readU16(is); animation.length = readF1000(is); @@ -531,7 +531,7 @@ void ContentFeatures::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, tile->material_flags = 0; if (backface_culling) tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING; - if (tiledef->animation.type == TAT_VERTICAL_FRAMES) + if (tiledef->animation.type == AT_VERTICAL_FRAMES) tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES; if (tiledef->tileable_horizontal) tile->material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL; diff --git a/src/nodedef.h b/src/nodedef.h index 80396f992..f47517c4a 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -161,9 +161,10 @@ enum NodeDrawType /* Stand-alone definition of a TileSpec (basically a server-side TileSpec) */ -enum TileAnimationType{ - TAT_NONE=0, - TAT_VERTICAL_FRAMES=1, +enum AnimationType{ + AT_NONE = 0, + AT_VERTICAL_FRAMES = 1, + AT_2D_ANIMATION_SHEET = 2, }; struct TileDef { @@ -172,7 +173,7 @@ struct TileDef bool tileable_horizontal; bool tileable_vertical; struct{ - enum TileAnimationType type; + enum AnimationType type; int aspect_w; // width for aspect ratio int aspect_h; // height for aspect ratio float length; // seconds @@ -184,7 +185,7 @@ struct TileDef backface_culling = true; tileable_horizontal = true; tileable_vertical = true; - animation.type = TAT_NONE; + animation.type = AT_NONE; animation.aspect_w = 1; animation.aspect_h = 1; animation.length = 1.0; diff --git a/src/particles.cpp b/src/particles.cpp index f20fb4083..538487028 100644 --- a/src/particles.cpp +++ b/src/particles.cpp @@ -43,6 +43,22 @@ v3f random_v3f(v3f min, v3f max) rand()/(float)RAND_MAX*(max.Z-min.Z)+min.Z); } +u32 check_material_type_param(u32 material_type_param) +{ + u32 alphaSource = (material_type_param & 0x0000F000) >> 12; + u32 modulo = (material_type_param & 0x00000F00) >> 8; + u32 srcFact = (material_type_param & 0x000000F0) >> 4; + u32 dstFact = material_type_param & 0x0000000F; + if (alphaSource <= 3 && modulo <= 4 && srcFact <= 10 && dstFact <= 10) { + return material_type_param; + } else { + errorstream << "Server send incorrect "; + errorstream << "material_type_param value for particle."; + errorstream << std::endl; + return 0; + } +} + Particle::Particle( IGameDef *gamedef, scene::ISceneManager* smgr, @@ -58,7 +74,14 @@ Particle::Particle( bool vertical, video::ITexture *texture, v2f texpos, - v2f texsize + v2f texsize, + u32 material_type_param, + u16 vertical_frame_num, + u16 horizontal_frame_num, + u16 first_frame, + float frame_length, + bool loop_animation, + u8 glow ): scene::ISceneNode(smgr->getRootSceneNode(), smgr) { @@ -71,11 +94,26 @@ Particle::Particle( m_material.setFlag(video::EMF_BACK_FACE_CULLING, false); m_material.setFlag(video::EMF_BILINEAR_FILTER, false); m_material.setFlag(video::EMF_FOG_ENABLE, true); - m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; + if (material_type_param != 0) { + m_material.MaterialType = video::EMT_ONETEXTURE_BLEND; + m_material.MaterialTypeParam = irr::core::FR(material_type_param); + // We must disable z-buffer if we want to avoid transparent pixels + // to overlap pixels with lower z-value. + m_material.setFlag(video::EMF_ZWRITE_ENABLE, false); + } else { + m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; + } m_material.setTexture(0, texture); + m_texpos = texpos; m_texsize = texsize; - + m_vertical_frame_num = vertical_frame_num; + m_horizontal_frame_num = horizontal_frame_num; + m_first_frame = first_frame; + m_frame_length = frame_length; + m_loop_animation = loop_animation; + m_texsize.Y /= m_vertical_frame_num; + m_texsize.X /= m_horizontal_frame_num; // Particle related m_pos = pos; @@ -88,6 +126,7 @@ Particle::Particle( m_collisiondetection = collisiondetection; m_collision_removal = collision_removal; m_vertical = vertical; + m_glow = glow; // Irrlicht stuff m_collisionbox = aabb3f @@ -170,16 +209,29 @@ void Particle::updateLight() else light = blend_light(m_env->getDayNightRatio(), LIGHT_SUN, 0); - m_light = decode_light(light); + m_light = decode_light(light + m_glow); } void Particle::updateVertices() { video::SColor c(255, m_light, m_light, m_light); - f32 tx0 = m_texpos.X; - f32 tx1 = m_texpos.X + m_texsize.X; - f32 ty0 = m_texpos.Y; - f32 ty1 = m_texpos.Y + m_texsize.Y; + u16 frame = m_first_frame; + if (m_frame_length > 0) { + if (m_loop_animation) + frame = m_first_frame + (u32)(m_time / m_frame_length) + % (m_vertical_frame_num * + m_horizontal_frame_num - m_first_frame); + else if (m_time >= + (m_vertical_frame_num * m_horizontal_frame_num + - m_first_frame) * m_frame_length) + frame = m_vertical_frame_num * m_horizontal_frame_num - 1; + else + frame = m_first_frame + (u16)(m_time / m_frame_length); + } + f32 tx0 = m_texpos.X + m_texsize.X * (frame % m_horizontal_frame_num); + f32 tx1 = m_texpos.X + m_texsize.X * (frame % m_horizontal_frame_num + 1); + f32 ty0 = m_texpos.Y + m_texsize.Y * (frame / m_horizontal_frame_num); + f32 ty1 = m_texpos.Y + m_texsize.Y * (frame / m_horizontal_frame_num + 1); m_vertices[0] = video::S3DVertex(-m_size/2,-m_size/2,0, 0,0,0, c, tx0, ty1); @@ -214,7 +266,16 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, u16 attached_id, bool vertical, - video::ITexture *texture, u32 id, ParticleManager *p_manager) : + video::ITexture *texture, u32 id, + u32 material_type_param, + u16 vertical_frame_num, + u16 horizontal_frame_num, + u16 min_first_frame, + u16 max_first_frame, + float frame_length, + bool loop_animation, + u8 glow, + ParticleManager *p_manager) : m_particlemanager(p_manager) { m_gamedef = gamedef; @@ -238,6 +299,14 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, m_vertical = vertical; m_texture = texture; m_time = 0; + m_vertical_frame_num = vertical_frame_num; + m_horizontal_frame_num = horizontal_frame_num; + m_min_first_frame = min_first_frame; + m_max_first_frame = max_first_frame; + m_frame_length = frame_length; + m_loop_animation = loop_animation; + m_material_type_param = material_type_param; + m_glow = glow; for (u16 i = 0; i<=m_amount; i++) { @@ -251,7 +320,6 @@ ParticleSpawner::~ParticleSpawner() {} void ParticleSpawner::step(float dtime, ClientEnvironment* env) { m_time += dtime; - bool unloaded = false; v3f attached_offset = v3f(0,0,0); if (m_attached_id != 0) { @@ -285,7 +353,10 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) float size = rand()/(float)RAND_MAX *(m_maxsize-m_minsize) +m_minsize; - + u16 first_frame = m_min_first_frame + + rand() % + (m_max_first_frame - + m_min_first_frame + 1); Particle* toadd = new Particle( m_gamedef, m_smgr, @@ -301,7 +372,14 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) m_vertical, m_texture, v2f(0.0, 0.0), - v2f(1.0, 1.0)); + v2f(1.0, 1.0), + m_material_type_param, + m_vertical_frame_num, + m_horizontal_frame_num, + first_frame, + m_frame_length, + m_loop_animation, + m_glow); m_particlemanager->addParticle(toadd); } i = m_spawntimes.erase(i); @@ -331,7 +409,10 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) float size = rand()/(float)RAND_MAX *(m_maxsize-m_minsize) +m_minsize; - + u16 first_frame = m_min_first_frame + + rand() % + (m_max_first_frame - + m_min_first_frame + 1); Particle* toadd = new Particle( m_gamedef, m_smgr, @@ -347,7 +428,14 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) m_vertical, m_texture, v2f(0.0, 0.0), - v2f(1.0, 1.0)); + v2f(1.0, 1.0), + m_material_type_param, + m_vertical_frame_num, + m_horizontal_frame_num, + first_frame, + m_frame_length, + m_loop_animation, + m_glow); m_particlemanager->addParticle(toadd); } } @@ -459,6 +547,39 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, video::ITexture *texture = gamedef->tsrc()->getTextureForMesh(*(event->add_particlespawner.texture)); + float frame_length = -1; + u16 vertical_frame_num = 1; + u16 horizontal_frame_num = 1; + u32 material_type_param = + check_material_type_param(event->add_particlespawner.material_type_param); + + switch (event->add_particlespawner.animation_type) { + case AT_NONE: + break; + case AT_VERTICAL_FRAMES: { + v2u32 size = texture->getOriginalSize(); + int frame_height = (float)size.X / + (float)event->add_particlespawner.vertical_frame_num * + (float)event->add_particlespawner.horizontal_frame_num; + vertical_frame_num = size.Y / frame_height; + frame_length = + event->add_particlespawner.frame_length / + vertical_frame_num; + break; + } + case AT_2D_ANIMATION_SHEET: { + vertical_frame_num = + event->add_particlespawner.vertical_frame_num; + horizontal_frame_num = + event->add_particlespawner.horizontal_frame_num; + frame_length = + event->add_particlespawner.frame_length; + break; + } + default: + break; + } + ParticleSpawner* toadd = new ParticleSpawner(gamedef, smgr, player, event->add_particlespawner.amount, event->add_particlespawner.spawntime, @@ -478,6 +599,14 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, event->add_particlespawner.vertical, texture, event->add_particlespawner.id, + material_type_param, + vertical_frame_num, + horizontal_frame_num, + event->add_particlespawner.min_first_frame, + event->add_particlespawner.max_first_frame, + frame_length, + event->add_particlespawner.loop_animation, + event->add_particlespawner.glow, this); /* delete allocated content of event */ @@ -502,6 +631,39 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, video::ITexture *texture = gamedef->tsrc()->getTextureForMesh(*(event->spawn_particle.texture)); + float frame_length = -1; + u16 vertical_frame_num = 1; + u16 horizontal_frame_num = 1; + u32 material_type_param = + check_material_type_param(event->spawn_particle.material_type_param); + + switch (event->spawn_particle.animation_type) { + case AT_NONE: + break; + case AT_VERTICAL_FRAMES: { + v2u32 size = texture->getOriginalSize(); + int frame_height = (float)size.X / + (float)event->spawn_particle.vertical_frame_num * + (float)event->spawn_particle.horizontal_frame_num; + vertical_frame_num = size.Y / frame_height; + frame_length = + event->spawn_particle.frame_length / + vertical_frame_num; + break; + } + case AT_2D_ANIMATION_SHEET: { + vertical_frame_num = + event->spawn_particle.vertical_frame_num; + horizontal_frame_num = + event->spawn_particle.horizontal_frame_num; + frame_length = + event->spawn_particle.frame_length; + break; + } + default: + break; + } + Particle* toadd = new Particle(gamedef, smgr, player, m_env, *event->spawn_particle.pos, *event->spawn_particle.vel, @@ -513,13 +675,21 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, event->spawn_particle.vertical, texture, v2f(0.0, 0.0), - v2f(1.0, 1.0)); + v2f(1.0, 1.0), + material_type_param, + vertical_frame_num, + horizontal_frame_num, + event->spawn_particle.first_frame, + frame_length, + event->spawn_particle.loop_animation, + event->spawn_particle.glow); addParticle(toadd); delete event->spawn_particle.pos; delete event->spawn_particle.vel; delete event->spawn_particle.acc; + delete event->spawn_particle.texture; break; } @@ -588,7 +758,8 @@ void ParticleManager::addNodeParticle(IGameDef* gamedef, scene::ISceneManager* s false, texture, texpos, - texsize); + texsize, + 0, 1, 1, 0, -1, true, 0); addParticle(toadd); } diff --git a/src/particles.h b/src/particles.h index eb8c6665d..6d8c6139f 100644 --- a/src/particles.h +++ b/src/particles.h @@ -50,7 +50,14 @@ class Particle : public scene::ISceneNode bool vertical, video::ITexture *texture, v2f texpos, - v2f texsize + v2f texsize, + u32 material_type_param, + u16 vertical_frame_num, + u16 horizontal_frame_num, + u16 first_frame, + float frame_length, + bool loop_animation, + u8 glow ); ~Particle(); @@ -102,6 +109,12 @@ private: bool m_collision_removal; bool m_vertical; v3s16 m_camera_offset; + u16 m_vertical_frame_num; + u16 m_horizontal_frame_num; + u16 m_first_frame; + float m_frame_length; + bool m_loop_animation; + u8 m_glow; }; class ParticleSpawner @@ -123,8 +136,15 @@ class ParticleSpawner bool vertical, video::ITexture *texture, u32 id, + u32 material_type_param, + u16 vertical_frame_num, + u16 horizontal_frame_num, + u16 min_first_frame, + u16 max_first_frame, + float frame_length, + bool loop_animation, + u8 glow, ParticleManager* p_manager); - ~ParticleSpawner(); void step(float dtime, ClientEnvironment *env); @@ -156,6 +176,14 @@ class ParticleSpawner bool m_collision_removal; bool m_vertical; u16 m_attached_id; + u32 m_material_type_param; + u16 m_vertical_frame_num; + u16 m_horizontal_frame_num; + u16 m_min_first_frame; + u16 m_max_first_frame; + float m_frame_length; + bool m_loop_animation; + u8 m_glow; }; /** diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index f20a65903..d4a25b68b 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -35,10 +35,11 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "noise.h" #include -struct EnumString es_TileAnimationType[] = +struct EnumString es_AnimationType[] = { - {TAT_NONE, "none"}, - {TAT_VERTICAL_FRAMES, "vertical_frames"}, + {AT_NONE, "none"}, + {AT_VERTICAL_FRAMES, "vertical_frames"}, + {AT_2D_ANIMATION_SHEET, "2d_animation_sheet"}, {0, NULL}, }; @@ -335,9 +336,9 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype) lua_getfield(L, index, "animation"); if(lua_istable(L, -1)){ // {type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0} - tiledef.animation.type = (TileAnimationType) - getenumfield(L, -1, "type", es_TileAnimationType, - TAT_NONE); + tiledef.animation.type = (AnimationType) + getenumfield(L, -1, "type", es_AnimationType, + AT_NONE); tiledef.animation.aspect_w = getintfield_default(L, -1, "aspect_w", 16); tiledef.animation.aspect_h = diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 2a2228b6d..32fdb4f04 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -159,6 +159,6 @@ bool push_json_value (lua_State *L, void read_json_value (lua_State *L, Json::Value &root, int index, u8 recursion = 0); -extern struct EnumString es_TileAnimationType[]; +extern struct EnumString es_AnimationType[]; #endif /* C_CONTENT_H_ */ diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index f36298915..cfb5e26db 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -513,6 +513,28 @@ int getintfield_default(lua_State *L, int table, return result; } +int check_material_type_param(lua_State *L, int table, + const char *fieldname, int default_) +{ + int material_type_param = + getintfield_default(L, table, fieldname, default_); + u32 alphaSource = (material_type_param & 0x0000F000) >> 12; + u32 modulo = (material_type_param & 0x00000F00) >> 8; + u32 srcFact = (material_type_param & 0x000000F0) >> 4; + u32 dstFact = material_type_param & 0x0000000F; + if (alphaSource <= 3 && modulo <= 4 && srcFact <= 10 && dstFact <= 10) { + return material_type_param; + } else { + std::ostringstream error_text; + error_text << "Incorrect material_type_param value "; + error_text << "for particle or particle spawner."; + error_text << std::endl; + throw LuaError(error_text.str()); + return 0; + } +} + + float getfloatfield_default(lua_State *L, int table, const char *fieldname, float default_) { diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h index a5fbee765..71ac735c1 100644 --- a/src/script/common/c_converter.h +++ b/src/script/common/c_converter.h @@ -45,6 +45,8 @@ float getfloatfield_default(lua_State *L, int table, const char *fieldname, float default_); int getintfield_default (lua_State *L, int table, const char *fieldname, int default_); +int check_material_type_param(lua_State *L, int table, + const char *fieldname, int default_); bool getstringfield(lua_State *L, int table, const char *fieldname, std::string &result); diff --git a/src/script/lua_api/l_particles.cpp b/src/script/lua_api/l_particles.cpp index 667ac7272..b0a57ce6d 100644 --- a/src/script/lua_api/l_particles.cpp +++ b/src/script/lua_api/l_particles.cpp @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_object.h" #include "lua_api/l_internal.h" #include "common/c_converter.h" +#include "common/c_content.h" #include "server.h" #include "particles.h" @@ -34,6 +35,9 @@ with this program; if not, write to the Free Software Foundation, Inc., // collision_removal = bool // vertical = bool // texture = e.g."default_wood.png" +// material_type_param = num +// animation = animation definition +// glow = indexed color or color string int ModApiParticles::l_add_particle(lua_State *L) { MAP_LOCK_REQUIRED; @@ -44,13 +48,24 @@ int ModApiParticles::l_add_particle(lua_State *L) float expirationtime, size; expirationtime = size = 1; + float frame_or_loop_length = -1; + + AnimationType animation_type = AT_NONE; + + u16 vertical_frame_num_or_aspect = 1; + u16 horizontal_frame_num_or_aspect = 1; + u16 first_frame = 0; bool collisiondetection, vertical, collision_removal; collisiondetection = vertical = collision_removal = false; + bool loop_animation = true; std::string texture = ""; std::string playername = ""; + u32 material_type_param = 0; + u8 glow = 0; + if (lua_gettop(L) > 1) // deprecated { log_deprecated(L, "Deprecated add_particle call with individual parameters instead of definition"); @@ -94,8 +109,61 @@ int ModApiParticles::l_add_particle(lua_State *L) acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc; lua_pop(L, 1); - expirationtime = getfloatfield_default(L, 1, "expirationtime", 1); + expirationtime = getfloatfield_default(L, 1, "expirationtime", 1); size = getfloatfield_default(L, 1, "size", 1); + + lua_getfield(L, 1, "animation"); + if (lua_istable(L, -1)) { + animation_type = (AnimationType) + getenumfield(L, -1, "type", es_AnimationType, + AT_NONE); + } + switch (animation_type) { + case AT_NONE: + break; + case AT_2D_ANIMATION_SHEET: + frame_or_loop_length = + getfloatfield_default(L, -1, "frame_length", -1); + vertical_frame_num_or_aspect = + getintfield_default(L, -1, "vertical_frame_num", 1); + horizontal_frame_num_or_aspect = + getintfield_default(L, -1, "horizontal_frame_num", 1); + first_frame = + getintfield_default(L, -1, "first_frame", 0); + loop_animation = + getboolfield_default(L, -1, "loop_animation", true); + break; + case AT_VERTICAL_FRAMES: + frame_or_loop_length = + getfloatfield_default(L, -1, "length", -1); + vertical_frame_num_or_aspect = + getintfield_default(L, -1, "aspect_w", 1); + horizontal_frame_num_or_aspect = + getintfield_default(L, -1, "aspect_h", 1); + first_frame = + getintfield_default(L, -1, "first_frame", 0); + loop_animation = + getboolfield_default(L, -1, "loop_animation", true); + break; + default: + break; + } + lua_pop(L, 1); + + if (animation_type == AT_2D_ANIMATION_SHEET && + first_frame >= vertical_frame_num_or_aspect * + horizontal_frame_num_or_aspect) { + std::ostringstream error_text; + error_text << "first_frame should be lower, than " + << "vertical_frame_num * horizontal_frame_num. " + << "Got first_frame=" << first_frame + << ", vertical_frame_num=" + << vertical_frame_num_or_aspect + << " and horizontal_frame_num=" + << horizontal_frame_num_or_aspect << std::endl; + throw LuaError(error_text.str()); + } + collisiondetection = getboolfield_default(L, 1, "collisiondetection", collisiondetection); collision_removal = getboolfield_default(L, 1, @@ -103,9 +171,16 @@ int ModApiParticles::l_add_particle(lua_State *L) vertical = getboolfield_default(L, 1, "vertical", vertical); texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); + material_type_param = check_material_type_param(L, 1, "material_type_param", 0); + glow = getintfield_default (L, 1, "glow", 0); } - getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, size, - collisiondetection, collision_removal, vertical, texture); + getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, + size, collisiondetection, collision_removal, vertical, + texture, material_type_param, + animation_type, + vertical_frame_num_or_aspect, + horizontal_frame_num_or_aspect, + first_frame, frame_or_loop_length, loop_animation, glow); return 1; } @@ -127,21 +202,33 @@ int ModApiParticles::l_add_particle(lua_State *L) // collision_removal = bool // vertical = bool // texture = e.g."default_wood.png" +// material_type_param = num +// animation = animation definition +// glow = indexed color or color string int ModApiParticles::l_add_particlespawner(lua_State *L) { MAP_LOCK_REQUIRED; // Get parameters u16 amount = 1; + u16 vertical_frame_num_or_aspect = 1; + u16 horizontal_frame_num_or_aspect = 1; + u16 min_first_frame = 0; + u16 max_first_frame = 0; v3f minpos, maxpos, minvel, maxvel, minacc, maxacc; minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0); float time, minexptime, maxexptime, minsize, maxsize; time= minexptime= maxexptime= minsize= maxsize= 1; + AnimationType animation_type = AT_NONE; + float frame_or_loop_length = -1; bool collisiondetection, vertical, collision_removal; collisiondetection = vertical = collision_removal = false; + bool loop_animation = true; ServerActiveObject *attached = NULL; std::string texture = ""; std::string playername = ""; + u32 material_type_param = 0; + u8 glow = 0; if (lua_gettop(L) > 1) //deprecated { @@ -196,6 +283,65 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) maxexptime = getfloatfield_default(L, 1, "maxexptime", maxexptime); minsize = getfloatfield_default(L, 1, "minsize", minsize); maxsize = getfloatfield_default(L, 1, "maxsize", maxsize); + + + lua_getfield(L, 1, "animation"); + if (lua_istable(L, -1)) { + animation_type = (AnimationType) + getenumfield(L, -1, "type", es_AnimationType, + AT_NONE); + } + switch (animation_type) { + case AT_NONE: + break; + case AT_2D_ANIMATION_SHEET: + frame_or_loop_length = + getfloatfield_default(L, -1, "frame_length", -1); + vertical_frame_num_or_aspect = + getintfield_default(L, -1, "vertical_frame_num", 1); + horizontal_frame_num_or_aspect = + getintfield_default(L, -1, "horizontal_frame_num", 1); + min_first_frame = + getintfield_default(L, -1, "min_first_frame", 0); + max_first_frame = + getintfield_default(L, -1, "max_first_frame", 0); + loop_animation = + getboolfield_default(L, -1, "loop_animation", true); + break; + case AT_VERTICAL_FRAMES: + frame_or_loop_length = + getfloatfield_default(L, -1, "length", -1); + vertical_frame_num_or_aspect = + getintfield_default(L, -1, "aspect_w", 1); + horizontal_frame_num_or_aspect = + getintfield_default(L, -1, "aspect_h", 1); + min_first_frame = + getintfield_default(L, -1, "min_first_frame", 0); + max_first_frame = + getintfield_default(L, -1, "max_first_frame", 0); + loop_animation = + getboolfield_default(L, -1, "loop_animation", true); + break; + default: + break; + } + lua_pop(L, 1); + + if (animation_type == AT_2D_ANIMATION_SHEET && + max_first_frame >= vertical_frame_num_or_aspect * + horizontal_frame_num_or_aspect) { + std::ostringstream error_text; + error_text << "max_first_frame should be lower, than " + << "vertical_frame_num * horizontal_frame_num. " + << "Got max_first_frame=" + << max_first_frame + << ", vertical_frame_num=" + << vertical_frame_num_or_aspect + << " and horizontal_frame_num=" + << horizontal_frame_num_or_aspect << std::endl; + throw LuaError(error_text.str()); + } + collisiondetection = getboolfield_default(L, 1, "collisiondetection", collisiondetection); collision_removal = getboolfield_default(L, 1, @@ -211,6 +357,8 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) vertical = getboolfield_default(L, 1, "vertical", vertical); texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); + material_type_param = check_material_type_param(L, 1, "material_type_param", 0); + glow = getintfield_default(L, 1, "glow", 0); } u32 id = getServer(L)->addParticleSpawner(amount, time, @@ -223,9 +371,17 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) collision_removal, attached, vertical, - texture, playername); + texture, + playername, + material_type_param, + animation_type, + vertical_frame_num_or_aspect, + horizontal_frame_num_or_aspect, + min_first_frame, max_first_frame, + frame_or_loop_length, + loop_animation, + glow); lua_pushnumber(L, id); - return 1; } diff --git a/src/server.cpp b/src/server.cpp index 48331e4f8..cef57be88 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1658,7 +1658,11 @@ void Server::SendShowFormspecMessage(u16 peer_id, const std::string &formspec, void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture) + bool vertical, const std::string &texture, + u32 material_type_param, AnimationType animation_type, + u16 vertical_frame_num, u16 horizontal_frame_num, u16 first_frame, + float frame_length, bool loop_animation, + u8 glow) { DSTACK(FUNCTION_NAME); @@ -1670,6 +1674,12 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat pkt << vertical; pkt << collision_removal; + pkt << material_type_param + << (u8)animation_type + << vertical_frame_num + << horizontal_frame_num << first_frame + << frame_length << loop_animation << glow; + if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); } @@ -1682,7 +1692,10 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, - u16 attached_id, bool vertical, const std::string &texture, u32 id) + u16 attached_id, bool vertical, const std::string &texture, u32 id, + u32 material_type_param, AnimationType animation_type, u16 vertical_frame_num, u16 horizontal_frame_num, + u16 min_first_frame, u16 max_first_frame, float frame_length, + bool loop_animation, u8 glow) { DSTACK(FUNCTION_NAME); @@ -1698,6 +1711,12 @@ void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3 pkt << collision_removal; pkt << attached_id; + pkt << material_type_param + << (u8)animation_type + << vertical_frame_num << horizontal_frame_num + << min_first_frame << max_first_frame + << frame_length << loop_animation << glow; + if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); } @@ -3147,7 +3166,11 @@ void Server::spawnParticle(const std::string &playername, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture) + bool vertical, const std::string &texture, + u32 material_type_param, AnimationType animation_type, + u16 vertical_frame_num, u16 horizontal_frame_num, u16 first_frame, + float frame_length, bool loop_animation, + u8 glow) { // m_env will be NULL if the server is initializing if (!m_env) @@ -3163,7 +3186,11 @@ void Server::spawnParticle(const std::string &playername, v3f pos, SendSpawnParticle(peer_id, pos, velocity, acceleration, expirationtime, size, collisiondetection, - collision_removal, vertical, texture); + collision_removal, vertical, texture, + material_type_param, animation_type, + vertical_frame_num, horizontal_frame_num, + first_frame, frame_length, loop_animation, + glow); } u32 Server::addParticleSpawner(u16 amount, float spawntime, @@ -3171,7 +3198,9 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, ServerActiveObject *attached, bool vertical, const std::string &texture, - const std::string &playername) + const std::string &playername, u32 material_type_param, AnimationType animation_type, + u16 vertical_frame_num, u16 horizontal_frame_num, u16 min_first_frame, u16 max_first_frame, + float frame_length, bool loop_animation, u8 glow) { // m_env will be NULL if the server is initializing if (!m_env) @@ -3197,7 +3226,10 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, minpos, maxpos, minvel, maxvel, minacc, maxacc, minexptime, maxexptime, minsize, maxsize, collisiondetection, collision_removal, attached_id, vertical, - texture, id); + texture, id, material_type_param, animation_type, + vertical_frame_num, horizontal_frame_num, + min_first_frame, max_first_frame, frame_length, loop_animation, + glow); return id; } diff --git a/src/server.h b/src/server.h index 9e844e36c..9a8d22b2e 100644 --- a/src/server.h +++ b/src/server.h @@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "hud.h" #include "gamedef.h" #include "serialization.h" // For SER_FMT_VER_INVALID +#include "nodedef.h" // AnimationType #include "mods.h" #include "inventorymanager.h" #include "subgame.h" @@ -254,7 +255,11 @@ public: v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture); + bool vertical, const std::string &texture, + u32 material_type_param, AnimationType animation_type, + u16 vertical_frame_num, u16 horizontal_frame_num, u16 first_frame, + float frame_length, bool loop_animation, + u8 glow); u32 addParticleSpawner(u16 amount, float spawntime, v3f minpos, v3f maxpos, @@ -265,7 +270,12 @@ public: bool collisiondetection, bool collision_removal, ServerActiveObject *attached, bool vertical, const std::string &texture, - const std::string &playername); + const std::string &playername, + u32 material_type_param, AnimationType animation_type, + u16 vertical_frame_num, u16 horizontal_frame_num, + u16 min_first_frame, u16 max_first_frame, + float frame_length, bool loop_animation, + u8 glow); void deleteParticleSpawner(const std::string &playername, u32 id); @@ -441,7 +451,12 @@ private: float minsize, float maxsize, bool collisiondetection, bool collision_removal, u16 attached_id, - bool vertical, const std::string &texture, u32 id); + bool vertical, const std::string &texture, u32 id, + u32 material_type_param, AnimationType animation_type, + u16 vertical_frame_num, u16 horizontal_frame_num, + u16 min_first_frame, u16 max_first_frame, + float frame_length, bool loop_animation, + u8 glow); void SendDeleteParticleSpawner(u16 peer_id, u32 id); @@ -450,7 +465,11 @@ private: v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture); + bool vertical, const std::string &texture, + u32 material_type_param, AnimationType animation_type, + u16 vertical_frame_num, u16 horizontal_frame_num, u16 first_frame, + float frame_length, bool loop_animation, + u8 glow); u32 SendActiveObjectRemoveAdd(u16 peer_id, const std::string &datas); void SendActiveObjectMessages(u16 peer_id, const std::string &datas, bool reliable = true); -- cgit v1.2.3 From 5fd1ef9b589419e2464f5599ea47a2f28f4d7b7b Mon Sep 17 00:00:00 2001 From: sfan5 Date: Mon, 14 Nov 2016 15:28:06 +0100 Subject: Revert "Adding particle blend, glow and animation (#4705)" This reverts commit 93e3555eae2deaeca69ee252cfa9cc9c3e0e49ef. --- builtin/common/misc_helpers.lua | 37 ------- doc/lua_api.txt | 189 +-------------------------------- src/client.h | 18 ---- src/network/clientpackethandler.cpp | 111 ++++++-------------- src/nodedef.cpp | 4 +- src/nodedef.h | 11 +- src/particles.cpp | 203 +++--------------------------------- src/particles.h | 32 +----- src/script/common/c_content.cpp | 13 ++- src/script/common/c_content.h | 2 +- src/script/common/c_converter.cpp | 22 ---- src/script/common/c_converter.h | 2 - src/script/lua_api/l_particles.cpp | 166 +---------------------------- src/server.cpp | 44 ++------ src/server.h | 27 +---- 15 files changed, 81 insertions(+), 800 deletions(-) (limited to 'src/script') diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index a495058d9..c2dc7514d 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -237,43 +237,6 @@ function math.sign(x, tolerance) return 0 end --------------------------------------------------------------------------------- --- Video enums and pack function - --- E_BLEND_FACTOR -minetest.ebf = { - zero = 0, -- src & dest (0, 0, 0, 0) - one = 1, -- src & dest (1, 1, 1, 1) - dst_color = 2, -- src (destR, destG, destB, destA) - one_minus_dst_color = 3, -- src (1-destR, 1-destG, 1-destB, 1-destA) - src_color = 4, -- dest (srcR, srcG, srcB, srcA) - one_minus_src_color = 5, -- dest (1-srcR, 1-srcG, 1-srcB, 1-srcA) - src_alpha = 6, -- src & dest (srcA, srcA, srcA, srcA) - one_minus_src_alpha = 7, -- src & dest (1-srcA, 1-srcA, 1-srcA, 1-srcA) - dst_alpha = 8, -- src & dest (destA, destA, destA, destA) - one_minus_dst_alpha = 9, -- src & dest (1-destA, 1-destA, 1-destA, 1-destA) - src_alpha_saturate = 10,-- src (min(srcA, 1-destA), idem, ...) -} - --- E_MODULATE_FUNC -minetest.emfn = { - modulate_1x = 1, - modulate_2x = 2, - modulate_4x = 4, -} - --- E_ALPHA_SOURCE -minetest.eas = { - none = 0, - vertex_color = 1, - texture = 2, -} - --- BlendFunc = source * sourceFactor + dest * destFactor -function minetest.pack_texture_blend_func(srcFact, dstFact, modulate, alphaSource) - return alphaSource * 4096 + modulate * 256 + srcFact * 16 + dstFact -end - -------------------------------------------------------------------------------- function get_last_folder(text,count) local parts = text:split(DIR_DELIM) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 3b3f17634..7d552c980 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -414,119 +414,6 @@ the word "`alpha`", then each texture pixel will contain the RGB of `` and the alpha of `` multiplied by the alpha of the texture pixel. -Particle blend --------------- -Blend function is defined by integer number. -There is a huge number of acceptable blend modificators. -Colour of a resulting pixel calculated using formulae: - - red = source_red * source_factor + destination_red * destination_factor - -and so on for every channel. - -Here is a some examples: - -Default value: - - material_type_param = 0, - -Use this value to disable blending. Texture will be applied to existing pixels -using alpha channel of it. Its recomended to use 1-bit alpha -in that case. This value will leave z-buffer writeable. - -Additive blend: - - material_type_param = 12641, - -Source = src_alpha, destination = one, alpha source is a texture and -vertex_color, modulate_1x. -Black color is completely transparent, white color is completely opaque. -Alpha channel still used to calculate result color, but not nessesary. -'destination = one' means that resulting color will be calculated using -overwritten pixels values. -For example with color of source (our texture) RGBA = (0,192,255,63) -"blue-cyan", 1/4 opaque. -and already rendered pixel color (40,192,0) "dark lime green" we will get color: - -R = source_red(0) * source_factor(src_alpha=63/255) + - destination_red(40) * destination_factor(one) = - 0 * 63/255 + 40 * 1 = 40 - -G = 192 * 63/255 + 192 * 1 = 239 -B = 255 * 63/255 + 0 * 1 = 63 - -Result: (40,239,63), "green" (a kind of). -Note, if you made a texture with some kind of shape with colour 662211h -it will appear dark red with a single particle, then yellow with a -several of them and white if player looking thru a lot of them. With -this you could made a nice-looking fire. - -Substractive blend: - - material_type_param = 12548, - -Source = zero, destination = src_color, alpha source is a texture and -vertex_color, modulate_1x. -Texture darkness act like an alpha channel. -Black color is completely opaque, white color is completely transparent. -'destination = src_color' means that destination in multiplied by -a source values. 'source = zero' means that source values ignored -(multiplied by 0). - -Invert blend: - - material_type_param = 12597, - -Source = one_minus_dst_color, destination = one_minus_src_alpha, alpha source -is a texture and vertex_color, modulate_1x. -Pixels invert color if source color value is big enough. If not, they just -black. -'destination = one_minus_src_alpha' means, that effect is masked by a -source alpha channel. - -You can design and use your own blend using those enum values and function -'minetest.pack_texture_blend_func'. Returned value of a function is -your 'material_type_param'. - -A values in a brackets is a multiplicators of a red, green, blue -and alpha channels respectively. - -* 'minetest.ebf': global table, containing blend factor enum values. Such as: - * zero = 0 -- src & dest (0, 0, 0, 0) - * one = 1 -- src & dest (1, 1, 1, 1) - * dst_color = 2 -- src (destR, destG, destB, destA) - * one_minus_dst_color = 3 -- src (1-destR, 1-destG, 1-destB, 1-destA) - * src_color = 4 -- dest (srcR, srcG, srcB, srcA) - * one_minus_src_color = 5 -- dest (1-srcR, 1-srcG, 1-srcB, 1-srcA) - * src_alpha = 6 -- src & dest (srcA, srcA, srcA, srcA) - * one_minus_src_alpha = 7 -- src & dest (1-srcA, 1-srcA, 1-srcA, 1-srcA) - * dst_alpha = 8 -- src & dest (destA, destA, destA, destA) - * one_minus_dst_alpha = 9 -- src & dest (1-destA, 1-destA, 1-destA, 1-destA) - * src_alpha_saturate = 10 -- src (min(srcA, 1-destA), idem, ...) - -* 'minetest.emfn': global table, containing modulate enum values. - * Multiply the components of the arguments, and shift the products to the - * left by x bits for brightening. Contain: - * modulate_1x = 1 -- no bit shift - * modulate_2x = 2 -- 1 bits shift - * modulate_4x = 4 -- 2 bits shift - -'modulate_4x' is quite useful when you want to make additive blend stronger -with a lower amount of particles. - -* 'minetest.eas': global table, containing alpha source enum values. Such as: - * none = 0 -- do not use alpha. - * vertex_color = 1 -- use vertex color alpha. - * texture = 2 -- use texture alpha. - -You can use both 'vertex_color' and 'texture' source by using value 3. - -* 'minetest.pack_texture_blend_func(srcFact, dstFact, modulate, alphaSource)': return integer - * Pack texture blend funcion variable. Depending from that variable blend - * function will be applied in time of a render poligons with selected material. - * Therefore resulting pixel will be 'source * srcFact + destination * dstFact' - * Use result of this function as 'material_type_param'. - Sounds ------ Only Ogg Vorbis files are supported. @@ -3763,7 +3650,7 @@ Definition tables ### Tile definition * `"image.png"` -* `{name="image.png", animation={Animation definition}}` +* `{name="image.png", animation={Tile Animation definition}}` * `{name="image.png", backface_culling=bool, tileable_vertical=bool, tileable_horizontal=bool}` * backface culling enabled by default for most nodes @@ -3774,50 +3661,8 @@ Definition tables * deprecated, yet still supported field names: * `image` (name) -### Animation definition - -#### Node animation, particle and particle spawners -* `{ type="vertical_frames", - aspect_w=16, - -- ^ specify width of a picture in pixels. - aspect_h=16, - -- ^ specify height of a frame in pixels. - length=3.0 - -- ^ specify full loop length. - first_frame = 0, -- <- only for particles, use - min_first_frame = 0, -- <- for particle spawners - max_first_frame = 0, - loop_animation = true, -- <- only for particles and particle spawners - -- specify if animation should start from beginning after last frame. -}` - -#### Particle and particle spawners only -* `{ - type="2d_animation_sheet", -- <- only for particles and particle spawners - vertical_frame_num = 1, - horizontal_frame_num = 1, - -- ^ specify amount of frames in texture. - -- Can be used both for animation or for texture transform - -- together with first_frame variable. - -- A animation texture separated on equal parts of frames, - -- by horizontal and vertical numbers. For example with - -- vertical_frame_num = 4 and horizontal_frame_num = 3 we got - -- 4*3 = 12 frames in total. Animation sequence start from - -- left top frame and go on to the right until reach end of - -- row. Next row also start from left frame. - first_frame = 0, -- <- only for particles, use - min_first_frame = 0, -- <- for particle spawners - max_first_frame = 0, - -- ^ specify first frame to start animation. - frame_length = -1, - -- ^ specify length of a frame in seconds. Negative and zero values - -- disable animation. A sequence with vertical_frame_num = 4 and - -- horizontal_frame_num = 3, first_frame = 4 and frame_length = 0.1 - -- will end in (4*3-4)*0.1 = 0.8 seconds. - loop_animation = true, - -- specify if animation should start from beginning after last frame. -}` - * All settings are optional. Default values is located in this example. +### Tile animation definition +* `{type="vertical_frames", aspect_w=16, aspect_h=16, length=3.0}` ### Node definition (`register_node`) @@ -4272,20 +4117,6 @@ The Biome API is still in an experimental phase and subject to change. -- ^ Uses texture (string) playername = "singleplayer" -- ^ optional, if specified spawns particle only on the player's client - material_type_param = 12641, - -- ^ optional, if specified spawns particle with - -- specified material type param and disable z-buffer. - -- Some examples: - -- Default value: 0, - -- Additive blend: 12641, - -- Substractive blend: 12548, - -- Invert blend: 12597, - -- See also "Particle blend". - animation = {Animation definition}, - -- ^ see above. Note, that particle and particle spawners have differences. - glow = 15, - -- ^ optional, specify particle self-luminescence in darkness. - values may vary from 0 (no glow) to 15 (bright glow). } ### `ParticleSpawner` definition (`add_particlespawner`) @@ -4320,20 +4151,6 @@ The Biome API is still in an experimental phase and subject to change. -- ^ Uses texture (string) playername = "singleplayer" -- ^ Playername is optional, if specified spawns particle only on the player's client - material_type_param = 12641, - -- ^ optional, if specified spawns particle with specified material type - -- param and disable z-buffer. - -- Some examples: - -- Default value: 0, - -- Additive blend: 12641, - -- Substractive blend: 12548, - -- Invert blend: 12597, - -- See also "Particle blend". - animation = {Animation definition}, - -- ^ see above. Note, that particle and particle spawners have differences. - glow = 15, - -- ^ optional, specify particle self-luminescence in darkness. - values may vary from 0 (no glow) to 15 (bright glow). } ### `HTTPRequest` definition (`HTTPApiTable.fetch_async`, `HTTPApiTable.fetch_async`) diff --git a/src/client.h b/src/client.h index c51daf7bc..9f5bda059 100644 --- a/src/client.h +++ b/src/client.h @@ -35,7 +35,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "hud.h" #include "particles.h" #include "network/networkpacket.h" -#include "nodedef.h" // AnimationType struct MeshMakeData; class MapBlockMesh; @@ -186,14 +185,6 @@ struct ClientEvent bool collision_removal; bool vertical; std::string *texture; - u32 material_type_param; - AnimationType animation_type; - u16 vertical_frame_num; - u16 horizontal_frame_num; - u16 first_frame; - float frame_length; - bool loop_animation; - u8 glow; } spawn_particle; struct{ u16 amount; @@ -214,15 +205,6 @@ struct ClientEvent bool vertical; std::string *texture; u32 id; - u32 material_type_param; - AnimationType animation_type; - u16 vertical_frame_num; - u16 horizontal_frame_num; - u16 min_first_frame; - u16 max_first_frame; - float frame_length; - bool loop_animation; - u8 glow; } add_particlespawner; struct{ u32 id; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 03baf078a..411982f69 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -896,46 +896,23 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt) std::string texture = deSerializeLongString(is); bool vertical = false; bool collision_removal = false; - u32 material_type_param = 0; - AnimationType animation_type = AT_NONE; - u16 vertical_frame_num = 1; - u16 horizontal_frame_num = 1; - u16 first_frame = 0; - float frame_length = -1; - bool loop_animation = true; - u8 glow = 0; try { vertical = readU8(is); collision_removal = readU8(is); - material_type_param = readU32(is); - animation_type = (AnimationType)readU8(is); - vertical_frame_num = readU16(is); - horizontal_frame_num = readU16(is); - first_frame = readU16(is); - frame_length = readF1000(is); - loop_animation = readU8(is); - glow = readU8(is); } catch (...) {} ClientEvent event; - event.type = CE_SPAWN_PARTICLE; - event.spawn_particle.pos = new v3f (pos); - event.spawn_particle.vel = new v3f (vel); - event.spawn_particle.acc = new v3f (acc); - event.spawn_particle.expirationtime = expirationtime; - event.spawn_particle.size = size; - event.spawn_particle.collisiondetection = collisiondetection; - event.spawn_particle.collision_removal = collision_removal; - event.spawn_particle.vertical = vertical; - event.spawn_particle.texture = new std::string(texture); - event.spawn_particle.material_type_param = material_type_param; - event.spawn_particle.animation_type = animation_type; - event.spawn_particle.vertical_frame_num = vertical_frame_num; - event.spawn_particle.horizontal_frame_num = horizontal_frame_num; - event.spawn_particle.first_frame = first_frame; - event.spawn_particle.frame_length = frame_length; - event.spawn_particle.loop_animation = loop_animation; - event.spawn_particle.glow = glow; + event.type = CE_SPAWN_PARTICLE; + event.spawn_particle.pos = new v3f (pos); + event.spawn_particle.vel = new v3f (vel); + event.spawn_particle.acc = new v3f (acc); + event.spawn_particle.expirationtime = expirationtime; + event.spawn_particle.size = size; + event.spawn_particle.collisiondetection = collisiondetection; + event.spawn_particle.collision_removal = collision_removal; + event.spawn_particle.vertical = vertical; + event.spawn_particle.texture = new std::string(texture); + m_client_event_queue.push(event); } @@ -955,15 +932,6 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) float maxsize; bool collisiondetection; u32 id; - u32 material_type_param = 0; - u8 animation_type = (u8)AT_NONE; - u16 vertical_frame_num = 1; - u16 horizontal_frame_num = 1; - u16 min_first_frame = 0; - u16 max_first_frame = 0; - float frame_length = -1; - bool loop_animation = true; - u8 glow = 0; *pkt >> amount >> spawntime >> minpos >> maxpos >> minvel >> maxvel >> minacc >> maxacc >> minexptime >> maxexptime >> minsize @@ -980,46 +948,29 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) *pkt >> vertical; *pkt >> collision_removal; *pkt >> attached_id; - *pkt >> material_type_param; - *pkt >> animation_type; - *pkt >> vertical_frame_num; - *pkt >> horizontal_frame_num; - *pkt >> min_first_frame; - *pkt >> max_first_frame; - *pkt >> frame_length; - *pkt >> loop_animation; - *pkt >> glow; + } catch (...) {} ClientEvent event; - event.type = CE_ADD_PARTICLESPAWNER; - event.add_particlespawner.amount = amount; - event.add_particlespawner.spawntime = spawntime; - event.add_particlespawner.minpos = new v3f (minpos); - event.add_particlespawner.maxpos = new v3f (maxpos); - event.add_particlespawner.minvel = new v3f (minvel); - event.add_particlespawner.maxvel = new v3f (maxvel); - event.add_particlespawner.minacc = new v3f (minacc); - event.add_particlespawner.maxacc = new v3f (maxacc); - event.add_particlespawner.minexptime = minexptime; - event.add_particlespawner.maxexptime = maxexptime; - event.add_particlespawner.minsize = minsize; - event.add_particlespawner.maxsize = maxsize; - event.add_particlespawner.collisiondetection = collisiondetection; - event.add_particlespawner.collision_removal = collision_removal; - event.add_particlespawner.attached_id = attached_id; - event.add_particlespawner.vertical = vertical; - event.add_particlespawner.texture = new std::string(texture); - event.add_particlespawner.id = id; - event.add_particlespawner.material_type_param = material_type_param; - event.add_particlespawner.animation_type = (AnimationType)animation_type; - event.add_particlespawner.vertical_frame_num = vertical_frame_num; - event.add_particlespawner.horizontal_frame_num = horizontal_frame_num; - event.add_particlespawner.min_first_frame = min_first_frame; - event.add_particlespawner.max_first_frame = max_first_frame; - event.add_particlespawner.frame_length = frame_length; - event.add_particlespawner.loop_animation = loop_animation; - event.add_particlespawner.glow = glow; + event.type = CE_ADD_PARTICLESPAWNER; + event.add_particlespawner.amount = amount; + event.add_particlespawner.spawntime = spawntime; + event.add_particlespawner.minpos = new v3f (minpos); + event.add_particlespawner.maxpos = new v3f (maxpos); + event.add_particlespawner.minvel = new v3f (minvel); + event.add_particlespawner.maxvel = new v3f (maxvel); + event.add_particlespawner.minacc = new v3f (minacc); + event.add_particlespawner.maxacc = new v3f (maxacc); + event.add_particlespawner.minexptime = minexptime; + event.add_particlespawner.maxexptime = maxexptime; + event.add_particlespawner.minsize = minsize; + event.add_particlespawner.maxsize = maxsize; + event.add_particlespawner.collisiondetection = collisiondetection; + event.add_particlespawner.collision_removal = collision_removal; + event.add_particlespawner.attached_id = attached_id; + event.add_particlespawner.vertical = vertical; + event.add_particlespawner.texture = new std::string(texture); + event.add_particlespawner.id = id; m_client_event_queue.push(event); } diff --git a/src/nodedef.cpp b/src/nodedef.cpp index c690e6720..39ea1a60e 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -211,7 +211,7 @@ void TileDef::deSerialize(std::istream &is, const u8 contenfeatures_version, con { int version = readU8(is); name = deSerializeString(is); - animation.type = (AnimationType)readU8(is); + animation.type = (TileAnimationType)readU8(is); animation.aspect_w = readU16(is); animation.aspect_h = readU16(is); animation.length = readF1000(is); @@ -531,7 +531,7 @@ void ContentFeatures::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, tile->material_flags = 0; if (backface_culling) tile->material_flags |= MATERIAL_FLAG_BACKFACE_CULLING; - if (tiledef->animation.type == AT_VERTICAL_FRAMES) + if (tiledef->animation.type == TAT_VERTICAL_FRAMES) tile->material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES; if (tiledef->tileable_horizontal) tile->material_flags |= MATERIAL_FLAG_TILEABLE_HORIZONTAL; diff --git a/src/nodedef.h b/src/nodedef.h index f47517c4a..80396f992 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -161,10 +161,9 @@ enum NodeDrawType /* Stand-alone definition of a TileSpec (basically a server-side TileSpec) */ -enum AnimationType{ - AT_NONE = 0, - AT_VERTICAL_FRAMES = 1, - AT_2D_ANIMATION_SHEET = 2, +enum TileAnimationType{ + TAT_NONE=0, + TAT_VERTICAL_FRAMES=1, }; struct TileDef { @@ -173,7 +172,7 @@ struct TileDef bool tileable_horizontal; bool tileable_vertical; struct{ - enum AnimationType type; + enum TileAnimationType type; int aspect_w; // width for aspect ratio int aspect_h; // height for aspect ratio float length; // seconds @@ -185,7 +184,7 @@ struct TileDef backface_culling = true; tileable_horizontal = true; tileable_vertical = true; - animation.type = AT_NONE; + animation.type = TAT_NONE; animation.aspect_w = 1; animation.aspect_h = 1; animation.length = 1.0; diff --git a/src/particles.cpp b/src/particles.cpp index 538487028..f20fb4083 100644 --- a/src/particles.cpp +++ b/src/particles.cpp @@ -43,22 +43,6 @@ v3f random_v3f(v3f min, v3f max) rand()/(float)RAND_MAX*(max.Z-min.Z)+min.Z); } -u32 check_material_type_param(u32 material_type_param) -{ - u32 alphaSource = (material_type_param & 0x0000F000) >> 12; - u32 modulo = (material_type_param & 0x00000F00) >> 8; - u32 srcFact = (material_type_param & 0x000000F0) >> 4; - u32 dstFact = material_type_param & 0x0000000F; - if (alphaSource <= 3 && modulo <= 4 && srcFact <= 10 && dstFact <= 10) { - return material_type_param; - } else { - errorstream << "Server send incorrect "; - errorstream << "material_type_param value for particle."; - errorstream << std::endl; - return 0; - } -} - Particle::Particle( IGameDef *gamedef, scene::ISceneManager* smgr, @@ -74,14 +58,7 @@ Particle::Particle( bool vertical, video::ITexture *texture, v2f texpos, - v2f texsize, - u32 material_type_param, - u16 vertical_frame_num, - u16 horizontal_frame_num, - u16 first_frame, - float frame_length, - bool loop_animation, - u8 glow + v2f texsize ): scene::ISceneNode(smgr->getRootSceneNode(), smgr) { @@ -94,26 +71,11 @@ Particle::Particle( m_material.setFlag(video::EMF_BACK_FACE_CULLING, false); m_material.setFlag(video::EMF_BILINEAR_FILTER, false); m_material.setFlag(video::EMF_FOG_ENABLE, true); - if (material_type_param != 0) { - m_material.MaterialType = video::EMT_ONETEXTURE_BLEND; - m_material.MaterialTypeParam = irr::core::FR(material_type_param); - // We must disable z-buffer if we want to avoid transparent pixels - // to overlap pixels with lower z-value. - m_material.setFlag(video::EMF_ZWRITE_ENABLE, false); - } else { - m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; - } + m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; m_material.setTexture(0, texture); - m_texpos = texpos; m_texsize = texsize; - m_vertical_frame_num = vertical_frame_num; - m_horizontal_frame_num = horizontal_frame_num; - m_first_frame = first_frame; - m_frame_length = frame_length; - m_loop_animation = loop_animation; - m_texsize.Y /= m_vertical_frame_num; - m_texsize.X /= m_horizontal_frame_num; + // Particle related m_pos = pos; @@ -126,7 +88,6 @@ Particle::Particle( m_collisiondetection = collisiondetection; m_collision_removal = collision_removal; m_vertical = vertical; - m_glow = glow; // Irrlicht stuff m_collisionbox = aabb3f @@ -209,29 +170,16 @@ void Particle::updateLight() else light = blend_light(m_env->getDayNightRatio(), LIGHT_SUN, 0); - m_light = decode_light(light + m_glow); + m_light = decode_light(light); } void Particle::updateVertices() { video::SColor c(255, m_light, m_light, m_light); - u16 frame = m_first_frame; - if (m_frame_length > 0) { - if (m_loop_animation) - frame = m_first_frame + (u32)(m_time / m_frame_length) - % (m_vertical_frame_num * - m_horizontal_frame_num - m_first_frame); - else if (m_time >= - (m_vertical_frame_num * m_horizontal_frame_num - - m_first_frame) * m_frame_length) - frame = m_vertical_frame_num * m_horizontal_frame_num - 1; - else - frame = m_first_frame + (u16)(m_time / m_frame_length); - } - f32 tx0 = m_texpos.X + m_texsize.X * (frame % m_horizontal_frame_num); - f32 tx1 = m_texpos.X + m_texsize.X * (frame % m_horizontal_frame_num + 1); - f32 ty0 = m_texpos.Y + m_texsize.Y * (frame / m_horizontal_frame_num); - f32 ty1 = m_texpos.Y + m_texsize.Y * (frame / m_horizontal_frame_num + 1); + f32 tx0 = m_texpos.X; + f32 tx1 = m_texpos.X + m_texsize.X; + f32 ty0 = m_texpos.Y; + f32 ty1 = m_texpos.Y + m_texsize.Y; m_vertices[0] = video::S3DVertex(-m_size/2,-m_size/2,0, 0,0,0, c, tx0, ty1); @@ -266,16 +214,7 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, u16 attached_id, bool vertical, - video::ITexture *texture, u32 id, - u32 material_type_param, - u16 vertical_frame_num, - u16 horizontal_frame_num, - u16 min_first_frame, - u16 max_first_frame, - float frame_length, - bool loop_animation, - u8 glow, - ParticleManager *p_manager) : + video::ITexture *texture, u32 id, ParticleManager *p_manager) : m_particlemanager(p_manager) { m_gamedef = gamedef; @@ -299,14 +238,6 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, m_vertical = vertical; m_texture = texture; m_time = 0; - m_vertical_frame_num = vertical_frame_num; - m_horizontal_frame_num = horizontal_frame_num; - m_min_first_frame = min_first_frame; - m_max_first_frame = max_first_frame; - m_frame_length = frame_length; - m_loop_animation = loop_animation; - m_material_type_param = material_type_param; - m_glow = glow; for (u16 i = 0; i<=m_amount; i++) { @@ -320,6 +251,7 @@ ParticleSpawner::~ParticleSpawner() {} void ParticleSpawner::step(float dtime, ClientEnvironment* env) { m_time += dtime; + bool unloaded = false; v3f attached_offset = v3f(0,0,0); if (m_attached_id != 0) { @@ -353,10 +285,7 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) float size = rand()/(float)RAND_MAX *(m_maxsize-m_minsize) +m_minsize; - u16 first_frame = m_min_first_frame + - rand() % - (m_max_first_frame - - m_min_first_frame + 1); + Particle* toadd = new Particle( m_gamedef, m_smgr, @@ -372,14 +301,7 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) m_vertical, m_texture, v2f(0.0, 0.0), - v2f(1.0, 1.0), - m_material_type_param, - m_vertical_frame_num, - m_horizontal_frame_num, - first_frame, - m_frame_length, - m_loop_animation, - m_glow); + v2f(1.0, 1.0)); m_particlemanager->addParticle(toadd); } i = m_spawntimes.erase(i); @@ -409,10 +331,7 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) float size = rand()/(float)RAND_MAX *(m_maxsize-m_minsize) +m_minsize; - u16 first_frame = m_min_first_frame + - rand() % - (m_max_first_frame - - m_min_first_frame + 1); + Particle* toadd = new Particle( m_gamedef, m_smgr, @@ -428,14 +347,7 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) m_vertical, m_texture, v2f(0.0, 0.0), - v2f(1.0, 1.0), - m_material_type_param, - m_vertical_frame_num, - m_horizontal_frame_num, - first_frame, - m_frame_length, - m_loop_animation, - m_glow); + v2f(1.0, 1.0)); m_particlemanager->addParticle(toadd); } } @@ -547,39 +459,6 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, video::ITexture *texture = gamedef->tsrc()->getTextureForMesh(*(event->add_particlespawner.texture)); - float frame_length = -1; - u16 vertical_frame_num = 1; - u16 horizontal_frame_num = 1; - u32 material_type_param = - check_material_type_param(event->add_particlespawner.material_type_param); - - switch (event->add_particlespawner.animation_type) { - case AT_NONE: - break; - case AT_VERTICAL_FRAMES: { - v2u32 size = texture->getOriginalSize(); - int frame_height = (float)size.X / - (float)event->add_particlespawner.vertical_frame_num * - (float)event->add_particlespawner.horizontal_frame_num; - vertical_frame_num = size.Y / frame_height; - frame_length = - event->add_particlespawner.frame_length / - vertical_frame_num; - break; - } - case AT_2D_ANIMATION_SHEET: { - vertical_frame_num = - event->add_particlespawner.vertical_frame_num; - horizontal_frame_num = - event->add_particlespawner.horizontal_frame_num; - frame_length = - event->add_particlespawner.frame_length; - break; - } - default: - break; - } - ParticleSpawner* toadd = new ParticleSpawner(gamedef, smgr, player, event->add_particlespawner.amount, event->add_particlespawner.spawntime, @@ -599,14 +478,6 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, event->add_particlespawner.vertical, texture, event->add_particlespawner.id, - material_type_param, - vertical_frame_num, - horizontal_frame_num, - event->add_particlespawner.min_first_frame, - event->add_particlespawner.max_first_frame, - frame_length, - event->add_particlespawner.loop_animation, - event->add_particlespawner.glow, this); /* delete allocated content of event */ @@ -631,39 +502,6 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, video::ITexture *texture = gamedef->tsrc()->getTextureForMesh(*(event->spawn_particle.texture)); - float frame_length = -1; - u16 vertical_frame_num = 1; - u16 horizontal_frame_num = 1; - u32 material_type_param = - check_material_type_param(event->spawn_particle.material_type_param); - - switch (event->spawn_particle.animation_type) { - case AT_NONE: - break; - case AT_VERTICAL_FRAMES: { - v2u32 size = texture->getOriginalSize(); - int frame_height = (float)size.X / - (float)event->spawn_particle.vertical_frame_num * - (float)event->spawn_particle.horizontal_frame_num; - vertical_frame_num = size.Y / frame_height; - frame_length = - event->spawn_particle.frame_length / - vertical_frame_num; - break; - } - case AT_2D_ANIMATION_SHEET: { - vertical_frame_num = - event->spawn_particle.vertical_frame_num; - horizontal_frame_num = - event->spawn_particle.horizontal_frame_num; - frame_length = - event->spawn_particle.frame_length; - break; - } - default: - break; - } - Particle* toadd = new Particle(gamedef, smgr, player, m_env, *event->spawn_particle.pos, *event->spawn_particle.vel, @@ -675,21 +513,13 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, event->spawn_particle.vertical, texture, v2f(0.0, 0.0), - v2f(1.0, 1.0), - material_type_param, - vertical_frame_num, - horizontal_frame_num, - event->spawn_particle.first_frame, - frame_length, - event->spawn_particle.loop_animation, - event->spawn_particle.glow); + v2f(1.0, 1.0)); addParticle(toadd); delete event->spawn_particle.pos; delete event->spawn_particle.vel; delete event->spawn_particle.acc; - delete event->spawn_particle.texture; break; } @@ -758,8 +588,7 @@ void ParticleManager::addNodeParticle(IGameDef* gamedef, scene::ISceneManager* s false, texture, texpos, - texsize, - 0, 1, 1, 0, -1, true, 0); + texsize); addParticle(toadd); } diff --git a/src/particles.h b/src/particles.h index 6d8c6139f..eb8c6665d 100644 --- a/src/particles.h +++ b/src/particles.h @@ -50,14 +50,7 @@ class Particle : public scene::ISceneNode bool vertical, video::ITexture *texture, v2f texpos, - v2f texsize, - u32 material_type_param, - u16 vertical_frame_num, - u16 horizontal_frame_num, - u16 first_frame, - float frame_length, - bool loop_animation, - u8 glow + v2f texsize ); ~Particle(); @@ -109,12 +102,6 @@ private: bool m_collision_removal; bool m_vertical; v3s16 m_camera_offset; - u16 m_vertical_frame_num; - u16 m_horizontal_frame_num; - u16 m_first_frame; - float m_frame_length; - bool m_loop_animation; - u8 m_glow; }; class ParticleSpawner @@ -136,15 +123,8 @@ class ParticleSpawner bool vertical, video::ITexture *texture, u32 id, - u32 material_type_param, - u16 vertical_frame_num, - u16 horizontal_frame_num, - u16 min_first_frame, - u16 max_first_frame, - float frame_length, - bool loop_animation, - u8 glow, ParticleManager* p_manager); + ~ParticleSpawner(); void step(float dtime, ClientEnvironment *env); @@ -176,14 +156,6 @@ class ParticleSpawner bool m_collision_removal; bool m_vertical; u16 m_attached_id; - u32 m_material_type_param; - u16 m_vertical_frame_num; - u16 m_horizontal_frame_num; - u16 m_min_first_frame; - u16 m_max_first_frame; - float m_frame_length; - bool m_loop_animation; - u8 m_glow; }; /** diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index d4a25b68b..f20a65903 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -35,11 +35,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "noise.h" #include -struct EnumString es_AnimationType[] = +struct EnumString es_TileAnimationType[] = { - {AT_NONE, "none"}, - {AT_VERTICAL_FRAMES, "vertical_frames"}, - {AT_2D_ANIMATION_SHEET, "2d_animation_sheet"}, + {TAT_NONE, "none"}, + {TAT_VERTICAL_FRAMES, "vertical_frames"}, {0, NULL}, }; @@ -336,9 +335,9 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype) lua_getfield(L, index, "animation"); if(lua_istable(L, -1)){ // {type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0} - tiledef.animation.type = (AnimationType) - getenumfield(L, -1, "type", es_AnimationType, - AT_NONE); + tiledef.animation.type = (TileAnimationType) + getenumfield(L, -1, "type", es_TileAnimationType, + TAT_NONE); tiledef.animation.aspect_w = getintfield_default(L, -1, "aspect_w", 16); tiledef.animation.aspect_h = diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 32fdb4f04..2a2228b6d 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -159,6 +159,6 @@ bool push_json_value (lua_State *L, void read_json_value (lua_State *L, Json::Value &root, int index, u8 recursion = 0); -extern struct EnumString es_AnimationType[]; +extern struct EnumString es_TileAnimationType[]; #endif /* C_CONTENT_H_ */ diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index cfb5e26db..f36298915 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -513,28 +513,6 @@ int getintfield_default(lua_State *L, int table, return result; } -int check_material_type_param(lua_State *L, int table, - const char *fieldname, int default_) -{ - int material_type_param = - getintfield_default(L, table, fieldname, default_); - u32 alphaSource = (material_type_param & 0x0000F000) >> 12; - u32 modulo = (material_type_param & 0x00000F00) >> 8; - u32 srcFact = (material_type_param & 0x000000F0) >> 4; - u32 dstFact = material_type_param & 0x0000000F; - if (alphaSource <= 3 && modulo <= 4 && srcFact <= 10 && dstFact <= 10) { - return material_type_param; - } else { - std::ostringstream error_text; - error_text << "Incorrect material_type_param value "; - error_text << "for particle or particle spawner."; - error_text << std::endl; - throw LuaError(error_text.str()); - return 0; - } -} - - float getfloatfield_default(lua_State *L, int table, const char *fieldname, float default_) { diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h index 71ac735c1..a5fbee765 100644 --- a/src/script/common/c_converter.h +++ b/src/script/common/c_converter.h @@ -45,8 +45,6 @@ float getfloatfield_default(lua_State *L, int table, const char *fieldname, float default_); int getintfield_default (lua_State *L, int table, const char *fieldname, int default_); -int check_material_type_param(lua_State *L, int table, - const char *fieldname, int default_); bool getstringfield(lua_State *L, int table, const char *fieldname, std::string &result); diff --git a/src/script/lua_api/l_particles.cpp b/src/script/lua_api/l_particles.cpp index b0a57ce6d..667ac7272 100644 --- a/src/script/lua_api/l_particles.cpp +++ b/src/script/lua_api/l_particles.cpp @@ -21,7 +21,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_object.h" #include "lua_api/l_internal.h" #include "common/c_converter.h" -#include "common/c_content.h" #include "server.h" #include "particles.h" @@ -35,9 +34,6 @@ with this program; if not, write to the Free Software Foundation, Inc., // collision_removal = bool // vertical = bool // texture = e.g."default_wood.png" -// material_type_param = num -// animation = animation definition -// glow = indexed color or color string int ModApiParticles::l_add_particle(lua_State *L) { MAP_LOCK_REQUIRED; @@ -48,24 +44,13 @@ int ModApiParticles::l_add_particle(lua_State *L) float expirationtime, size; expirationtime = size = 1; - float frame_or_loop_length = -1; - - AnimationType animation_type = AT_NONE; - - u16 vertical_frame_num_or_aspect = 1; - u16 horizontal_frame_num_or_aspect = 1; - u16 first_frame = 0; bool collisiondetection, vertical, collision_removal; collisiondetection = vertical = collision_removal = false; - bool loop_animation = true; std::string texture = ""; std::string playername = ""; - u32 material_type_param = 0; - u8 glow = 0; - if (lua_gettop(L) > 1) // deprecated { log_deprecated(L, "Deprecated add_particle call with individual parameters instead of definition"); @@ -109,61 +94,8 @@ int ModApiParticles::l_add_particle(lua_State *L) acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc; lua_pop(L, 1); - expirationtime = getfloatfield_default(L, 1, "expirationtime", 1); + expirationtime = getfloatfield_default(L, 1, "expirationtime", 1); size = getfloatfield_default(L, 1, "size", 1); - - lua_getfield(L, 1, "animation"); - if (lua_istable(L, -1)) { - animation_type = (AnimationType) - getenumfield(L, -1, "type", es_AnimationType, - AT_NONE); - } - switch (animation_type) { - case AT_NONE: - break; - case AT_2D_ANIMATION_SHEET: - frame_or_loop_length = - getfloatfield_default(L, -1, "frame_length", -1); - vertical_frame_num_or_aspect = - getintfield_default(L, -1, "vertical_frame_num", 1); - horizontal_frame_num_or_aspect = - getintfield_default(L, -1, "horizontal_frame_num", 1); - first_frame = - getintfield_default(L, -1, "first_frame", 0); - loop_animation = - getboolfield_default(L, -1, "loop_animation", true); - break; - case AT_VERTICAL_FRAMES: - frame_or_loop_length = - getfloatfield_default(L, -1, "length", -1); - vertical_frame_num_or_aspect = - getintfield_default(L, -1, "aspect_w", 1); - horizontal_frame_num_or_aspect = - getintfield_default(L, -1, "aspect_h", 1); - first_frame = - getintfield_default(L, -1, "first_frame", 0); - loop_animation = - getboolfield_default(L, -1, "loop_animation", true); - break; - default: - break; - } - lua_pop(L, 1); - - if (animation_type == AT_2D_ANIMATION_SHEET && - first_frame >= vertical_frame_num_or_aspect * - horizontal_frame_num_or_aspect) { - std::ostringstream error_text; - error_text << "first_frame should be lower, than " - << "vertical_frame_num * horizontal_frame_num. " - << "Got first_frame=" << first_frame - << ", vertical_frame_num=" - << vertical_frame_num_or_aspect - << " and horizontal_frame_num=" - << horizontal_frame_num_or_aspect << std::endl; - throw LuaError(error_text.str()); - } - collisiondetection = getboolfield_default(L, 1, "collisiondetection", collisiondetection); collision_removal = getboolfield_default(L, 1, @@ -171,16 +103,9 @@ int ModApiParticles::l_add_particle(lua_State *L) vertical = getboolfield_default(L, 1, "vertical", vertical); texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); - material_type_param = check_material_type_param(L, 1, "material_type_param", 0); - glow = getintfield_default (L, 1, "glow", 0); } - getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, - size, collisiondetection, collision_removal, vertical, - texture, material_type_param, - animation_type, - vertical_frame_num_or_aspect, - horizontal_frame_num_or_aspect, - first_frame, frame_or_loop_length, loop_animation, glow); + getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, size, + collisiondetection, collision_removal, vertical, texture); return 1; } @@ -202,33 +127,21 @@ int ModApiParticles::l_add_particle(lua_State *L) // collision_removal = bool // vertical = bool // texture = e.g."default_wood.png" -// material_type_param = num -// animation = animation definition -// glow = indexed color or color string int ModApiParticles::l_add_particlespawner(lua_State *L) { MAP_LOCK_REQUIRED; // Get parameters u16 amount = 1; - u16 vertical_frame_num_or_aspect = 1; - u16 horizontal_frame_num_or_aspect = 1; - u16 min_first_frame = 0; - u16 max_first_frame = 0; v3f minpos, maxpos, minvel, maxvel, minacc, maxacc; minpos= maxpos= minvel= maxvel= minacc= maxacc= v3f(0, 0, 0); float time, minexptime, maxexptime, minsize, maxsize; time= minexptime= maxexptime= minsize= maxsize= 1; - AnimationType animation_type = AT_NONE; - float frame_or_loop_length = -1; bool collisiondetection, vertical, collision_removal; collisiondetection = vertical = collision_removal = false; - bool loop_animation = true; ServerActiveObject *attached = NULL; std::string texture = ""; std::string playername = ""; - u32 material_type_param = 0; - u8 glow = 0; if (lua_gettop(L) > 1) //deprecated { @@ -283,65 +196,6 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) maxexptime = getfloatfield_default(L, 1, "maxexptime", maxexptime); minsize = getfloatfield_default(L, 1, "minsize", minsize); maxsize = getfloatfield_default(L, 1, "maxsize", maxsize); - - - lua_getfield(L, 1, "animation"); - if (lua_istable(L, -1)) { - animation_type = (AnimationType) - getenumfield(L, -1, "type", es_AnimationType, - AT_NONE); - } - switch (animation_type) { - case AT_NONE: - break; - case AT_2D_ANIMATION_SHEET: - frame_or_loop_length = - getfloatfield_default(L, -1, "frame_length", -1); - vertical_frame_num_or_aspect = - getintfield_default(L, -1, "vertical_frame_num", 1); - horizontal_frame_num_or_aspect = - getintfield_default(L, -1, "horizontal_frame_num", 1); - min_first_frame = - getintfield_default(L, -1, "min_first_frame", 0); - max_first_frame = - getintfield_default(L, -1, "max_first_frame", 0); - loop_animation = - getboolfield_default(L, -1, "loop_animation", true); - break; - case AT_VERTICAL_FRAMES: - frame_or_loop_length = - getfloatfield_default(L, -1, "length", -1); - vertical_frame_num_or_aspect = - getintfield_default(L, -1, "aspect_w", 1); - horizontal_frame_num_or_aspect = - getintfield_default(L, -1, "aspect_h", 1); - min_first_frame = - getintfield_default(L, -1, "min_first_frame", 0); - max_first_frame = - getintfield_default(L, -1, "max_first_frame", 0); - loop_animation = - getboolfield_default(L, -1, "loop_animation", true); - break; - default: - break; - } - lua_pop(L, 1); - - if (animation_type == AT_2D_ANIMATION_SHEET && - max_first_frame >= vertical_frame_num_or_aspect * - horizontal_frame_num_or_aspect) { - std::ostringstream error_text; - error_text << "max_first_frame should be lower, than " - << "vertical_frame_num * horizontal_frame_num. " - << "Got max_first_frame=" - << max_first_frame - << ", vertical_frame_num=" - << vertical_frame_num_or_aspect - << " and horizontal_frame_num=" - << horizontal_frame_num_or_aspect << std::endl; - throw LuaError(error_text.str()); - } - collisiondetection = getboolfield_default(L, 1, "collisiondetection", collisiondetection); collision_removal = getboolfield_default(L, 1, @@ -357,8 +211,6 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) vertical = getboolfield_default(L, 1, "vertical", vertical); texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); - material_type_param = check_material_type_param(L, 1, "material_type_param", 0); - glow = getintfield_default(L, 1, "glow", 0); } u32 id = getServer(L)->addParticleSpawner(amount, time, @@ -371,17 +223,9 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) collision_removal, attached, vertical, - texture, - playername, - material_type_param, - animation_type, - vertical_frame_num_or_aspect, - horizontal_frame_num_or_aspect, - min_first_frame, max_first_frame, - frame_or_loop_length, - loop_animation, - glow); + texture, playername); lua_pushnumber(L, id); + return 1; } diff --git a/src/server.cpp b/src/server.cpp index cef57be88..48331e4f8 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1658,11 +1658,7 @@ void Server::SendShowFormspecMessage(u16 peer_id, const std::string &formspec, void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture, - u32 material_type_param, AnimationType animation_type, - u16 vertical_frame_num, u16 horizontal_frame_num, u16 first_frame, - float frame_length, bool loop_animation, - u8 glow) + bool vertical, const std::string &texture) { DSTACK(FUNCTION_NAME); @@ -1674,12 +1670,6 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat pkt << vertical; pkt << collision_removal; - pkt << material_type_param - << (u8)animation_type - << vertical_frame_num - << horizontal_frame_num << first_frame - << frame_length << loop_animation << glow; - if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); } @@ -1692,10 +1682,7 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, - u16 attached_id, bool vertical, const std::string &texture, u32 id, - u32 material_type_param, AnimationType animation_type, u16 vertical_frame_num, u16 horizontal_frame_num, - u16 min_first_frame, u16 max_first_frame, float frame_length, - bool loop_animation, u8 glow) + u16 attached_id, bool vertical, const std::string &texture, u32 id) { DSTACK(FUNCTION_NAME); @@ -1711,12 +1698,6 @@ void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3 pkt << collision_removal; pkt << attached_id; - pkt << material_type_param - << (u8)animation_type - << vertical_frame_num << horizontal_frame_num - << min_first_frame << max_first_frame - << frame_length << loop_animation << glow; - if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); } @@ -3166,11 +3147,7 @@ void Server::spawnParticle(const std::string &playername, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture, - u32 material_type_param, AnimationType animation_type, - u16 vertical_frame_num, u16 horizontal_frame_num, u16 first_frame, - float frame_length, bool loop_animation, - u8 glow) + bool vertical, const std::string &texture) { // m_env will be NULL if the server is initializing if (!m_env) @@ -3186,11 +3163,7 @@ void Server::spawnParticle(const std::string &playername, v3f pos, SendSpawnParticle(peer_id, pos, velocity, acceleration, expirationtime, size, collisiondetection, - collision_removal, vertical, texture, - material_type_param, animation_type, - vertical_frame_num, horizontal_frame_num, - first_frame, frame_length, loop_animation, - glow); + collision_removal, vertical, texture); } u32 Server::addParticleSpawner(u16 amount, float spawntime, @@ -3198,9 +3171,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, ServerActiveObject *attached, bool vertical, const std::string &texture, - const std::string &playername, u32 material_type_param, AnimationType animation_type, - u16 vertical_frame_num, u16 horizontal_frame_num, u16 min_first_frame, u16 max_first_frame, - float frame_length, bool loop_animation, u8 glow) + const std::string &playername) { // m_env will be NULL if the server is initializing if (!m_env) @@ -3226,10 +3197,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, minpos, maxpos, minvel, maxvel, minacc, maxacc, minexptime, maxexptime, minsize, maxsize, collisiondetection, collision_removal, attached_id, vertical, - texture, id, material_type_param, animation_type, - vertical_frame_num, horizontal_frame_num, - min_first_frame, max_first_frame, frame_length, loop_animation, - glow); + texture, id); return id; } diff --git a/src/server.h b/src/server.h index 9a8d22b2e..9e844e36c 100644 --- a/src/server.h +++ b/src/server.h @@ -26,7 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "hud.h" #include "gamedef.h" #include "serialization.h" // For SER_FMT_VER_INVALID -#include "nodedef.h" // AnimationType #include "mods.h" #include "inventorymanager.h" #include "subgame.h" @@ -255,11 +254,7 @@ public: v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture, - u32 material_type_param, AnimationType animation_type, - u16 vertical_frame_num, u16 horizontal_frame_num, u16 first_frame, - float frame_length, bool loop_animation, - u8 glow); + bool vertical, const std::string &texture); u32 addParticleSpawner(u16 amount, float spawntime, v3f minpos, v3f maxpos, @@ -270,12 +265,7 @@ public: bool collisiondetection, bool collision_removal, ServerActiveObject *attached, bool vertical, const std::string &texture, - const std::string &playername, - u32 material_type_param, AnimationType animation_type, - u16 vertical_frame_num, u16 horizontal_frame_num, - u16 min_first_frame, u16 max_first_frame, - float frame_length, bool loop_animation, - u8 glow); + const std::string &playername); void deleteParticleSpawner(const std::string &playername, u32 id); @@ -451,12 +441,7 @@ private: float minsize, float maxsize, bool collisiondetection, bool collision_removal, u16 attached_id, - bool vertical, const std::string &texture, u32 id, - u32 material_type_param, AnimationType animation_type, - u16 vertical_frame_num, u16 horizontal_frame_num, - u16 min_first_frame, u16 max_first_frame, - float frame_length, bool loop_animation, - u8 glow); + bool vertical, const std::string &texture, u32 id); void SendDeleteParticleSpawner(u16 peer_id, u32 id); @@ -465,11 +450,7 @@ private: v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture, - u32 material_type_param, AnimationType animation_type, - u16 vertical_frame_num, u16 horizontal_frame_num, u16 first_frame, - float frame_length, bool loop_animation, - u8 glow); + bool vertical, const std::string &texture); u32 SendActiveObjectRemoveAdd(u16 peer_id, const std::string &datas); void SendActiveObjectMessages(u16 peer_id, const std::string &datas, bool reliable = true); -- cgit v1.2.3 From 9e10f9f49a558050d36a49db619bf8f5eb3853c0 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Thu, 24 Nov 2016 09:58:21 -0500 Subject: Fix secure io.open without mode --- src/script/cpp_api/s_security.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/script') diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index 3a8f724fe..5a64c249c 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -525,14 +525,19 @@ int ScriptApiSecurity::sl_g_require(lua_State *L) int ScriptApiSecurity::sl_io_open(lua_State *L) { + bool with_mode = lua_gettop(L) > 1; + luaL_checktype(L, 1, LUA_TSTRING); const char *path = lua_tostring(L, 1); CHECK_SECURE_PATH(L, path); push_original(L, "io", "open"); lua_pushvalue(L, 1); - lua_pushvalue(L, 2); - lua_call(L, 2, 2); + if (with_mode) { + lua_pushvalue(L, 2); + } + + lua_call(L, with_mode ? 2 : 1, 2); return 2; } -- cgit v1.2.3 From 3af5eef96463510a27cf06b9c3ecc9f1d04cdac6 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Thu, 24 Nov 2016 10:10:20 -0500 Subject: Fix secure io.lines It used to drop all of the return values from the insecure version of the function. --- src/script/cpp_api/s_security.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/script') diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index 5a64c249c..c9816f89b 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -577,13 +577,13 @@ int ScriptApiSecurity::sl_io_lines(lua_State *L) CHECK_SECURE_PATH(L, path); } + int top_precall = lua_gettop(L); push_original(L, "io", "lines"); lua_pushvalue(L, 1); - int top_precall = lua_gettop(L); lua_call(L, 1, LUA_MULTRET); // Return number of arguments returned by the function, // adjusting for the function being poped. - return lua_gettop(L) - (top_precall - 1); + return lua_gettop(L) - top_precall; } -- cgit v1.2.3 From 2fe3bf5a18eb9aa9f38654b3c0a0729c42408cd6 Mon Sep 17 00:00:00 2001 From: juhdanad Date: Mon, 28 Nov 2016 09:43:33 +0100 Subject: Limit light_source in the engine (#4814) Since light_source>15 causes crash, it must be limited. --- src/nodedef.cpp | 2 ++ src/script/common/c_content.cpp | 6 ++++++ src/voxelalgorithms.cpp | 1 + 3 files changed, 9 insertions(+) (limited to 'src/script') diff --git a/src/nodedef.cpp b/src/nodedef.cpp index 39ea1a60e..ccbb42c66 100644 --- a/src/nodedef.cpp +++ b/src/nodedef.cpp @@ -482,6 +482,7 @@ void ContentFeatures::deSerialize(std::istream &is) liquid_viscosity = readU8(is); liquid_renewable = readU8(is); light_source = readU8(is); + light_source = MYMIN(light_source, LIGHT_MAX); damage_per_second = readU32(is); node_box.deSerialize(is); selection_box.deSerialize(is); @@ -1442,6 +1443,7 @@ void ContentFeatures::deSerializeOld(std::istream &is, int version) liquid_alternative_source = deSerializeString(is); liquid_viscosity = readU8(is); light_source = readU8(is); + light_source = MYMIN(light_source, LIGHT_MAX); damage_per_second = readU32(is); node_box.deSerialize(is); selection_box.deSerialize(is); diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index f20a65903..541744895 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -526,6 +526,12 @@ ContentFeatures read_content_features(lua_State *L, int index) // Amount of light the node emits f.light_source = getintfield_default(L, index, "light_source", f.light_source); + if (f.light_source > LIGHT_MAX) { + warningstream << "Node " << f.name.c_str() + << " had greater light_source than " << LIGHT_MAX + << ", it was reduced." << std::endl; + f.light_source = LIGHT_MAX; + } f.damage_per_second = getintfield_default(L, index, "damage_per_second", f.damage_per_second); diff --git a/src/voxelalgorithms.cpp b/src/voxelalgorithms.cpp index 55f0d1c98..93cc33acc 100644 --- a/src/voxelalgorithms.cpp +++ b/src/voxelalgorithms.cpp @@ -264,6 +264,7 @@ struct LightQueue { const mapblock_v3 &block_pos, MapBlock *block, direction source_dir) { + assert(light <= LIGHT_SUN); lights[light].push_back( ChangingLight(rel_pos, block_pos, block, source_dir)); } -- cgit v1.2.3 From c38985825f299999135cc01aaf0052ec9138135a Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 26 Nov 2016 17:35:25 +0100 Subject: Allow restricting detached inventories to one player This combats the problem of sending the hundreds of "creative" / "armor" or whatever detached invs that exist on popular servers to each and every player on join or on change of said invs. --- builtin/game/detached_inventory.lua | 4 ++-- doc/lua_api.txt | 5 ++++- src/script/lua_api/l_inventory.cpp | 7 ++++--- src/server.cpp | 18 ++++++++++++------ src/server.h | 4 +++- 5 files changed, 25 insertions(+), 13 deletions(-) (limited to 'src/script') diff --git a/builtin/game/detached_inventory.lua b/builtin/game/detached_inventory.lua index b5d106b04..420e89ff2 100644 --- a/builtin/game/detached_inventory.lua +++ b/builtin/game/detached_inventory.lua @@ -2,7 +2,7 @@ core.detached_inventories = {} -function core.create_detached_inventory(name, callbacks) +function core.create_detached_inventory(name, callbacks, player_name) local stuff = {} stuff.name = name if callbacks then @@ -15,6 +15,6 @@ function core.create_detached_inventory(name, callbacks) end stuff.mod_origin = core.get_current_modname() or "??" core.detached_inventories[name] = stuff - return core.create_detached_inventory_raw(name) + return core.create_detached_inventory_raw(name, player_name) end diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 9da0fb4f9..eb47270f4 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2310,8 +2310,11 @@ and `minetest.auth_reload` call the authetification handler. * `{type="player", name="celeron55"}` * `{type="node", pos={x=, y=, z=}}` * `{type="detached", name="creative"}` -* `minetest.create_detached_inventory(name, callbacks)`: returns an `InvRef` +* `minetest.create_detached_inventory(name, callbacks, [player_name])`: returns an `InvRef` * callbacks: See "Detached inventory callbacks" + * player_name: Make detached inventory available to one player exclusively, + by default they will be sent to every player (even if not used). + Note that this parameter is mostly just a workaround and will be removed in future releases. * Creates a detached inventory. If it already exists, it is cleared. * `minetest.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)`: returns left over ItemStack diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index 110e68d23..38eade609 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -520,16 +520,17 @@ int ModApiInventory::l_get_inventory(lua_State *L) } } -// create_detached_inventory_raw(name) +// create_detached_inventory_raw(name, [player_name]) int ModApiInventory::l_create_detached_inventory_raw(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char *name = luaL_checkstring(L, 1); - if(getServer(L)->createDetachedInventory(name) != NULL){ + const char *player = lua_isstring(L, 2) ? lua_tostring(L, 2) : ""; + if (getServer(L)->createDetachedInventory(name, player) != NULL) { InventoryLocation loc; loc.setDetached(name); InvRef::create(L, loc); - }else{ + } else { lua_pushnil(L); } return 1; diff --git a/src/server.cpp b/src/server.cpp index fae375425..fe67ac96e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2487,11 +2487,16 @@ void Server::sendDetachedInventory(const std::string &name, u16 peer_id) NetworkPacket pkt(TOCLIENT_DETACHED_INVENTORY, 0, peer_id); pkt.putRawString(s.c_str(), s.size()); - if (peer_id != PEER_ID_INEXISTENT) { - Send(&pkt); - } - else { - m_clients.sendToAll(0, &pkt, true); + const std::string &check = m_detached_inventories_player[name]; + if (peer_id == PEER_ID_INEXISTENT) { + if (check == "") + return m_clients.sendToAll(0, &pkt, true); + RemotePlayer *p = m_env->getPlayer(check.c_str()); + if (p) + m_clients.send(p->peer_id, 0, &pkt, true); + } else { + if (check == "" || getPlayerName(peer_id) == check) + Send(&pkt); } } @@ -3224,7 +3229,7 @@ void Server::deleteParticleSpawner(const std::string &playername, u32 id) SendDeleteParticleSpawner(peer_id, id); } -Inventory* Server::createDetachedInventory(const std::string &name) +Inventory* Server::createDetachedInventory(const std::string &name, const std::string &player) { if(m_detached_inventories.count(name) > 0){ infostream<<"Server clearing detached inventory \""< m_detached_inventories; + // value = "" (visible to all players) or player name + std::map m_detached_inventories_player; DISABLE_CLASS_COPY(Server); }; -- cgit v1.2.3 From 2e69711613c18e1075f245d196823ea23675f027 Mon Sep 17 00:00:00 2001 From: Auke Kok Date: Tue, 6 Dec 2016 16:39:33 -0800 Subject: Simple deco: Allow setting param2 value on placement Schematics can already be placed with a param2 value, but not simple 1-node plant decorations of the simple type. This adds a `param2` field to the simple deco type that is checked to be between 0 and 255, and put to the placed node at mapgen. This can be used to put a degrotate value in, or e.g. a fill value for leveltype nodes, or a place_param2 value at mapgen placement, or vary the shape of meshoptions plantlike drawtype. --- doc/lua_api.txt | 4 +++- src/mg_decoration.cpp | 2 +- src/mg_decoration.h | 1 + src/script/lua_api/l_mapgen.cpp | 7 +++++++ 4 files changed, 12 insertions(+), 2 deletions(-) (limited to 'src/script') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index bab3e11f7..62cd6bb46 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -4040,8 +4040,10 @@ The Biome API is still in an experimental phase and subject to change. -- ^ Number of nodes high the decoration is made. -- ^ If height_max is not 0, this is the lower bound of the randomly selected height. height_max = 0, - -- ^ Number of nodes the decoration can be at maximum. + -- ^ Number of nodes the decoration can be at maximum. -- ^ If absent, the parameter 'height' is used as a constant. + param2 = 0, + -- ^ Param2 value of placed decoration node. ----- Schematic-type parameters schematic = "foobar.mts", diff --git a/src/mg_decoration.cpp b/src/mg_decoration.cpp index 92483abc3..51e4fbbcc 100644 --- a/src/mg_decoration.cpp +++ b/src/mg_decoration.cpp @@ -315,7 +315,7 @@ size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p) !force_placement) break; - vm->m_data[vi] = MapNode(c_place); + vm->m_data[vi] = MapNode(c_place, 0, deco_param2); } return 1; diff --git a/src/mg_decoration.h b/src/mg_decoration.h index be0ba44d7..986328ec3 100644 --- a/src/mg_decoration.h +++ b/src/mg_decoration.h @@ -100,6 +100,7 @@ public: std::vector c_decos; s16 deco_height; s16 deco_height_max; + u8 deco_param2; }; class DecoSchematic : public Decoration { diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index da8e71cdc..cefea3da9 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -975,6 +975,7 @@ bool read_deco_simple(lua_State *L, DecoSimple *deco) deco->deco_height = getintfield_default(L, index, "height", 1); deco->deco_height_max = getintfield_default(L, index, "height_max", 0); + deco->deco_param2 = getintfield_default(L, index, "param2", 0); if (deco->deco_height <= 0) { errorstream << "register_decoration: simple decoration height" @@ -990,6 +991,12 @@ bool read_deco_simple(lua_State *L, DecoSimple *deco) return false; } + if ((deco->deco_param2 < 0) || (deco->deco_param2 > 255)) { + errorstream << "register_decoration: param2 out of bounds (0-255)" + << std::endl; + return false; + } + return true; } -- cgit v1.2.3 From 5a2431a9bd9a019a47cd279ac988435b075cdabe Mon Sep 17 00:00:00 2001 From: Auke Kok Date: Tue, 6 Dec 2016 22:08:16 -0800 Subject: Simple decorations: Fix range check for deco->deco_param2 Allow any int value, and properly range check it before casting. --- src/script/lua_api/l_mapgen.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/script') diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index cefea3da9..281f68e46 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -972,10 +972,10 @@ int ModApiMapgen::l_register_decoration(lua_State *L) bool read_deco_simple(lua_State *L, DecoSimple *deco) { int index = 1; + int param2; deco->deco_height = getintfield_default(L, index, "height", 1); deco->deco_height_max = getintfield_default(L, index, "height_max", 0); - deco->deco_param2 = getintfield_default(L, index, "param2", 0); if (deco->deco_height <= 0) { errorstream << "register_decoration: simple decoration height" @@ -991,11 +991,13 @@ bool read_deco_simple(lua_State *L, DecoSimple *deco) return false; } - if ((deco->deco_param2 < 0) || (deco->deco_param2 > 255)) { + param2 = getintfield_default(L, index, "param2", 0); + if ((param2 < 0) || (param2 > 255)) { errorstream << "register_decoration: param2 out of bounds (0-255)" << std::endl; return false; } + deco->deco_param2 = (u8)param2; return true; } -- cgit v1.2.3 From 59f84ca0a07e50dd5ce050d38ae1aeb529bd25ac Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Mon, 5 Dec 2016 19:59:15 +0000 Subject: Mod security: Allow read-only access to all mod paths --- src/script/cpp_api/s_security.cpp | 82 +++++++++++++++++++++++++++----------- src/script/cpp_api/s_security.h | 21 ++++++---- src/script/lua_api/l_areastore.cpp | 4 +- src/script/lua_api/l_mapgen.cpp | 2 +- src/script/lua_api/l_settings.cpp | 13 ++++-- src/script/lua_api/l_settings.h | 3 +- src/script/lua_api/l_util.cpp | 4 +- src/server.h | 1 + 8 files changed, 89 insertions(+), 41 deletions(-) (limited to 'src/script') diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index c9816f89b..b915747c6 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -336,8 +336,12 @@ bool ScriptApiSecurity::safeLoadFile(lua_State *L, const char *path) } -bool ScriptApiSecurity::checkPath(lua_State *L, const char *path) +bool ScriptApiSecurity::checkPath(lua_State *L, const char *path, + bool write_required, bool *write_allowed) { + if (write_allowed) + *write_allowed = false; + std::string str; // Transient std::string norel_path = fs::RemoveRelativePathComponents(path); @@ -380,32 +384,53 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path) // Builtin can access anything if (mod_name == BUILTIN_MOD_NAME) { + if (write_allowed) *write_allowed = true; return true; } // Allow paths in mod path - const ModSpec *mod = server->getModSpec(mod_name); - if (mod) { - str = fs::AbsolutePath(mod->path); + // Don't bother if write access isn't important, since it will be handled later + if (write_required || write_allowed != NULL) { + const ModSpec *mod = server->getModSpec(mod_name); + if (mod) { + str = fs::AbsolutePath(mod->path); + if (!str.empty() && fs::PathStartsWith(abs_path, str)) { + if (write_allowed) *write_allowed = true; + return true; + } + } + } + } + lua_pop(L, 1); // Pop mod name + + // Allow read-only access to all mod directories + if (!write_required) { + const std::vector mods = server->getMods(); + for (size_t i = 0; i < mods.size(); ++i) { + str = fs::AbsolutePath(mods[i].path); if (!str.empty() && fs::PathStartsWith(abs_path, str)) { return true; } } } - lua_pop(L, 1); // Pop mod name str = fs::AbsolutePath(server->getWorldPath()); - if (str.empty()) return false; - // Don't allow access to world mods. We add to the absolute path - // of the world instead of getting the absolute paths directly - // because that won't work if they don't exist. - if (fs::PathStartsWith(abs_path, str + DIR_DELIM + "worldmods") || - fs::PathStartsWith(abs_path, str + DIR_DELIM + "game")) { - return false; - } - // Allow all other paths in world path - if (fs::PathStartsWith(abs_path, str)) { - return true; + if (!str.empty()) { + // Don't allow access to other paths in the world mod/game path. + // These have to be blocked so you can't override a trusted mod + // by creating a mod with the same name in a world mod directory. + // We add to the absolute path of the world instead of getting + // the absolute paths directly because that won't work if they + // don't exist. + if (fs::PathStartsWith(abs_path, str + DIR_DELIM + "worldmods") || + fs::PathStartsWith(abs_path, str + DIR_DELIM + "game")) { + return false; + } + // Allow all other paths in world path + if (fs::PathStartsWith(abs_path, str)) { + if (write_allowed) *write_allowed = true; + return true; + } } // Default to disallowing @@ -476,7 +501,7 @@ int ScriptApiSecurity::sl_g_loadfile(lua_State *L) if (lua_isstring(L, 1)) { path = lua_tostring(L, 1); - CHECK_SECURE_PATH(L, path); + CHECK_SECURE_PATH_INTERNAL(L, path, false, NULL); } if (!safeLoadFile(L, path)) { @@ -529,7 +554,16 @@ int ScriptApiSecurity::sl_io_open(lua_State *L) luaL_checktype(L, 1, LUA_TSTRING); const char *path = lua_tostring(L, 1); - CHECK_SECURE_PATH(L, path); + + bool write_requested = false; + if (with_mode) { + luaL_checktype(L, 2, LUA_TSTRING); + const char *mode = lua_tostring(L, 2); + write_requested = strchr(mode, 'w') != NULL || + strchr(mode, '+') != NULL || + strchr(mode, 'a') != NULL; + } + CHECK_SECURE_PATH_INTERNAL(L, path, write_requested, NULL); push_original(L, "io", "open"); lua_pushvalue(L, 1); @@ -546,7 +580,7 @@ int ScriptApiSecurity::sl_io_input(lua_State *L) { if (lua_isstring(L, 1)) { const char *path = lua_tostring(L, 1); - CHECK_SECURE_PATH(L, path); + CHECK_SECURE_PATH_INTERNAL(L, path, false, NULL); } push_original(L, "io", "input"); @@ -560,7 +594,7 @@ int ScriptApiSecurity::sl_io_output(lua_State *L) { if (lua_isstring(L, 1)) { const char *path = lua_tostring(L, 1); - CHECK_SECURE_PATH(L, path); + CHECK_SECURE_PATH_INTERNAL(L, path, true, NULL); } push_original(L, "io", "output"); @@ -574,7 +608,7 @@ int ScriptApiSecurity::sl_io_lines(lua_State *L) { if (lua_isstring(L, 1)) { const char *path = lua_tostring(L, 1); - CHECK_SECURE_PATH(L, path); + CHECK_SECURE_PATH_INTERNAL(L, path, false, NULL); } int top_precall = lua_gettop(L); @@ -591,11 +625,11 @@ int ScriptApiSecurity::sl_os_rename(lua_State *L) { luaL_checktype(L, 1, LUA_TSTRING); const char *path1 = lua_tostring(L, 1); - CHECK_SECURE_PATH(L, path1); + CHECK_SECURE_PATH_INTERNAL(L, path1, true, NULL); luaL_checktype(L, 2, LUA_TSTRING); const char *path2 = lua_tostring(L, 2); - CHECK_SECURE_PATH(L, path2); + CHECK_SECURE_PATH_INTERNAL(L, path2, true, NULL); push_original(L, "os", "rename"); lua_pushvalue(L, 1); @@ -609,7 +643,7 @@ int ScriptApiSecurity::sl_os_remove(lua_State *L) { luaL_checktype(L, 1, LUA_TSTRING); const char *path = lua_tostring(L, 1); - CHECK_SECURE_PATH(L, path); + CHECK_SECURE_PATH_INTERNAL(L, path, true, NULL); push_original(L, "os", "remove"); lua_pushvalue(L, 1); diff --git a/src/script/cpp_api/s_security.h b/src/script/cpp_api/s_security.h index 97bc5c067..6876108e8 100644 --- a/src/script/cpp_api/s_security.h +++ b/src/script/cpp_api/s_security.h @@ -23,14 +23,18 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "cpp_api/s_base.h" -#define CHECK_SECURE_PATH(L, path) \ - if (!ScriptApiSecurity::checkPath(L, path)) { \ - throw LuaError(std::string("Attempt to access external file ") + \ - path + " with mod security on."); \ +#define CHECK_SECURE_PATH_INTERNAL(L, path, write_required, ptr) \ + if (!ScriptApiSecurity::checkPath(L, path, write_required, ptr)) { \ + throw LuaError(std::string("Mod security: Blocked attempted ") + \ + (write_required ? "write to " : "read from ") + path); \ } -#define CHECK_SECURE_PATH_OPTIONAL(L, path) \ +#define CHECK_SECURE_PATH(L, path, write_required) \ if (ScriptApiSecurity::isSecure(L)) { \ - CHECK_SECURE_PATH(L, path); \ + CHECK_SECURE_PATH_INTERNAL(L, path, write_required, NULL); \ + } +#define CHECK_SECURE_PATH_POSSIBLE_WRITE(L, path, ptr) \ + if (ScriptApiSecurity::isSecure(L)) { \ + CHECK_SECURE_PATH_INTERNAL(L, path, false, ptr); \ } @@ -43,8 +47,9 @@ public: static bool isSecure(lua_State *L); // Loads a file as Lua code safely (doesn't allow bytecode). static bool safeLoadFile(lua_State *L, const char *path); - // Checks if mods are allowed to read and write to the path - static bool checkPath(lua_State *L, const char *path); + // Checks if mods are allowed to read (and optionally write) to the path + static bool checkPath(lua_State *L, const char *path, bool write_required, + bool *write_allowed=NULL); private: // Syntax: "sl_" '_' diff --git a/src/script/lua_api/l_areastore.cpp b/src/script/lua_api/l_areastore.cpp index 0912e2ab0..09a5c78f9 100644 --- a/src/script/lua_api/l_areastore.cpp +++ b/src/script/lua_api/l_areastore.cpp @@ -263,7 +263,7 @@ int LuaAreaStore::l_to_file(lua_State *L) AreaStore *ast = o->as; const char *filename = luaL_checkstring(L, 2); - CHECK_SECURE_PATH_OPTIONAL(L, filename); + CHECK_SECURE_PATH(L, filename, true); std::ostringstream os(std::ios_base::binary); ast->serialize(os); @@ -294,7 +294,7 @@ int LuaAreaStore::l_from_file(lua_State *L) LuaAreaStore *o = checkobject(L, 1); const char *filename = luaL_checkstring(L, 2); - CHECK_SECURE_PATH_OPTIONAL(L, filename); + CHECK_SECURE_PATH(L, filename, false); std::ifstream is(filename, std::ios::binary); return deserialization_helper(L, o->as, is); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 281f68e46..bc1c32f03 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -1295,7 +1295,7 @@ int ModApiMapgen::l_create_schematic(lua_State *L) INodeDefManager *ndef = getServer(L)->getNodeDefManager(); const char *filename = luaL_checkstring(L, 4); - CHECK_SECURE_PATH_OPTIONAL(L, filename); + CHECK_SECURE_PATH(L, filename, true); Map *map = &(getEnv(L)->getMap()); Schematic schem; diff --git a/src/script/lua_api/l_settings.cpp b/src/script/lua_api/l_settings.cpp index 35b82b435..ea3d50857 100644 --- a/src/script/lua_api/l_settings.cpp +++ b/src/script/lua_api/l_settings.cpp @@ -118,6 +118,11 @@ int LuaSettings::l_write(lua_State* L) NO_MAP_LOCK_REQUIRED; LuaSettings* o = checkobject(L, 1); + if (!o->m_write_allowed) { + throw LuaError("Settings: writing " + o->m_filename + + " not allowed with mod security on."); + } + bool success = o->m_settings->updateConfigFile(o->m_filename.c_str()); lua_pushboolean(L, success); @@ -142,8 +147,9 @@ int LuaSettings::l_to_table(lua_State* L) return 1; } -LuaSettings::LuaSettings(const char* filename) +LuaSettings::LuaSettings(const char* filename, bool write_allowed) { + m_write_allowed = write_allowed; m_filename = std::string(filename); m_settings = new Settings(); @@ -188,9 +194,10 @@ void LuaSettings::Register(lua_State* L) int LuaSettings::create_object(lua_State* L) { NO_MAP_LOCK_REQUIRED; + bool write_allowed; const char* filename = luaL_checkstring(L, 1); - CHECK_SECURE_PATH_OPTIONAL(L, filename); - LuaSettings* o = new LuaSettings(filename); + CHECK_SECURE_PATH_POSSIBLE_WRITE(L, filename, &write_allowed); + LuaSettings* o = new LuaSettings(filename, write_allowed); *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, className); lua_setmetatable(L, -2); diff --git a/src/script/lua_api/l_settings.h b/src/script/lua_api/l_settings.h index cb0c09a73..bca333e31 100644 --- a/src/script/lua_api/l_settings.h +++ b/src/script/lua_api/l_settings.h @@ -53,11 +53,12 @@ private: // to_table(self) -> {[key1]=value1,...} static int l_to_table(lua_State* L); + bool m_write_allowed; Settings* m_settings; std::string m_filename; public: - LuaSettings(const char* filename); + LuaSettings(const char* filename, bool write_allowed); ~LuaSettings(); // LuaSettings(filename) diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 818c1aeeb..26e2b985c 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -388,7 +388,7 @@ int ModApiUtil::l_mkdir(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char *path = luaL_checkstring(L, 1); - CHECK_SECURE_PATH_OPTIONAL(L, path); + CHECK_SECURE_PATH(L, path, true); lua_pushboolean(L, fs::CreateAllDirs(path)); return 1; } @@ -400,7 +400,7 @@ int ModApiUtil::l_get_dir_list(lua_State *L) const char *path = luaL_checkstring(L, 1); short is_dir = lua_isboolean(L, 2) ? lua_toboolean(L, 2) : -1; - CHECK_SECURE_PATH_OPTIONAL(L, path); + CHECK_SECURE_PATH(L, path, false); std::vector list = fs::GetDirListing(path); diff --git a/src/server.h b/src/server.h index cc2bcef25..4425d139b 100644 --- a/src/server.h +++ b/src/server.h @@ -298,6 +298,7 @@ public: IWritableNodeDefManager* getWritableNodeDefManager(); IWritableCraftDefManager* getWritableCraftDefManager(); + const std::vector &getMods() const { return m_mods; } const ModSpec* getModSpec(const std::string &modname) const; void getModNames(std::vector &modlist); std::string getBuiltinLuaPath(); -- cgit v1.2.3 From 0f0502109eac44128e87906fff30b5d049392f1d Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 16 Dec 2016 17:43:39 -0500 Subject: Security: Fix resolving of some relative paths Trying to resolve a path with RemoveRelativePathComponents that can't be resolved without leaving leading parent components (e.g. "../worlds/foo" or "bar/../../worlds/foo") will fail. To work around this, we leave the relative components and simply remove the trailing components one at a time, and bail out when we find a parent component. This will still fail for paths like "worlds/foo/noexist/../auth.txt" (the path before the last parent component must not exist), but this is fine since you won't be able to open a file with a path like that anyways (the O.S. will determine that the path doesn't exist. Try `cat /a/../etc/passwd`). --- src/script/cpp_api/s_security.cpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src/script') diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index b915747c6..1b1f148cd 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -344,8 +344,7 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path, std::string str; // Transient - std::string norel_path = fs::RemoveRelativePathComponents(path); - std::string abs_path = fs::AbsolutePath(norel_path); + std::string abs_path = fs::AbsolutePath(path); if (!abs_path.empty()) { // Don't allow accessing the settings file @@ -356,18 +355,29 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path, // If we couldn't find the absolute path (path doesn't exist) then // try removing the last components until it works (to allow // non-existent files/folders for mkdir). - std::string cur_path = norel_path; + std::string cur_path = path; std::string removed; while (abs_path.empty() && !cur_path.empty()) { - std::string tmp_rmed; - cur_path = fs::RemoveLastPathComponent(cur_path, &tmp_rmed); - removed = tmp_rmed + (removed.empty() ? "" : DIR_DELIM + removed); + std::string component; + cur_path = fs::RemoveLastPathComponent(cur_path, &component); + if (component == "..") { + // Parent components can't be allowed or we could allow something like + // /home/user/minetest/worlds/foo/noexist/../../../../../../etc/passwd. + // If we have previous non-relative elements in the path we might be + // able to remove them so that things like worlds/foo/noexist/../auth.txt + // could be allowed, but those paths will be interpreted as nonexistent + // by the operating system anyways. + return false; + } + removed = component + (removed.empty() ? "" : DIR_DELIM + removed); abs_path = fs::AbsolutePath(cur_path); } - if (abs_path.empty()) return false; + if (abs_path.empty()) + return false; // Add the removed parts back so that you can't, eg, create a // directory in worldmods if worldmods doesn't exist. - if (!removed.empty()) abs_path += DIR_DELIM + removed; + if (!removed.empty()) + abs_path += DIR_DELIM + removed; // Get server from registry lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI); -- cgit v1.2.3 From cc7c31a5bc44ab0010997e86c0a8c9f5f832b398 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 21 Dec 2016 10:20:06 +0100 Subject: Fix warning reported by clang (possible bug in Settings lua api) --- src/script/lua_api/l_settings.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/script') diff --git a/src/script/lua_api/l_settings.cpp b/src/script/lua_api/l_settings.cpp index ea3d50857..d3fe03005 100644 --- a/src/script/lua_api/l_settings.cpp +++ b/src/script/lua_api/l_settings.cpp @@ -194,7 +194,7 @@ void LuaSettings::Register(lua_State* L) int LuaSettings::create_object(lua_State* L) { NO_MAP_LOCK_REQUIRED; - bool write_allowed; + bool write_allowed = true; const char* filename = luaL_checkstring(L, 1); CHECK_SECURE_PATH_POSSIBLE_WRITE(L, filename, &write_allowed); LuaSettings* o = new LuaSettings(filename, write_allowed); -- cgit v1.2.3