示例#1
0
bool variant::operator==(const variant& v) const
{
	if(type_ != v.type_) {
		if(type_ == TYPE_DECIMAL || v.type_ == TYPE_DECIMAL) {
			return as_decimal() == v.as_decimal();
		}

		return false;
	}

	switch(type_) {
	case TYPE_NULL: {
		return v.is_null();
	}

	case TYPE_STRING: {
		return string_->str == v.string_->str;
	}

	case TYPE_INT: {
		return int_value_ == v.int_value_;
	}

	case TYPE_DECIMAL: {
		return decimal_value_ == v.decimal_value_;
	}

	case TYPE_LIST: {
		if(num_elements() != v.num_elements()) {
			return false;
		}

		for(size_t n = 0; n != num_elements(); ++n) {
			if((*this)[n] != v[n]) {
				return false;
			}
		}

		return true;
	}

	case TYPE_MAP: {
		return map_->elements == v.map_->elements;
	}

	case TYPE_CALLABLE_LOADING: {
		return false;
	}

	case TYPE_CALLABLE: {
		return callable_->equals(v.callable_);
	}
	case TYPE_FUNCTION: {
		return fn_ == v.fn_;
	}
	}

	assert(false);
	return false;
}
示例#2
0
   oWalletContactEntry WalletDb::lookup_contact( const variant& data )const
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");
       FC_ASSERT( !data.is_null() );

       const auto iter = contacts.find( data.as_string() );
       if( iter != contacts.end() ) return iter->second;

       return oWalletContactEntry();
   } FC_CAPTURE_AND_RETHROW( (data) ) }
void formula_variable_storage::read(variant node)
{
	if(node.is_null()) {
		return;
	}

	foreach(const variant_pair& val, node.as_map()) {
		add(val.first.as_string(), val.second);
	}
}
示例#4
0
   owallet_contact_record wallet_db::lookup_contact( const variant& data )const
   { try {
       FC_ASSERT( is_open() );
       FC_ASSERT( !data.is_null() );

       const auto iter = contacts.find( data.as_string() );
       if( iter != contacts.end() ) return iter->second;

       return owallet_contact_record();
   } FC_CAPTURE_AND_RETHROW( (data) ) }
示例#5
0
void luaW_pushfaivariant(lua_State* L, variant val) {
	if(val.is_int()) {
		lua_pushinteger(L, val.as_int());
	} else if(val.is_decimal()) {
		lua_pushnumber(L, val.as_decimal() / 1000.0);
	} else if(val.is_string()) {
		const std::string result_string = val.as_string();
		lua_pushlstring(L, result_string.c_str(), result_string.size());
	} else if(val.is_list()) {
		lua_newtable(L);
		for(const variant& v : val.as_list()) {
			lua_pushinteger(L, lua_rawlen(L, -1) + 1);
			luaW_pushfaivariant(L, v);
			lua_settable(L, -3);
		}
	} else if(val.is_map()) {
		typedef std::map<variant,variant>::value_type kv_type;
		lua_newtable(L);
		for(const kv_type& v : val.as_map()) {
			luaW_pushfaivariant(L, v.first);
			luaW_pushfaivariant(L, v.second);
			lua_settable(L, -3);
		}
	} else if(val.is_callable()) {
		// First try a few special cases
		if(unit_callable* u_ref = val.try_convert<unit_callable>()) {
			const unit& u = u_ref->get_unit();
			unit_map::iterator un_it = resources::gameboard->units().find(u.get_location());
			if(&*un_it == &u) {
				luaW_pushunit(L, u.underlying_id());
			} else {
				luaW_pushunit(L, u.side(), u.underlying_id());
			}
		} else if(location_callable* loc_ref = val.try_convert<location_callable>()) {
			luaW_pushlocation(L, loc_ref->loc());
		} else {
			// If those fail, convert generically to a map
			const formula_callable* obj = val.as_callable();
			std::vector<formula_input> inputs;
			obj->get_inputs(&inputs);
			lua_newtable(L);
			for(const formula_input& attr : inputs) {
				if(attr.access == FORMULA_WRITE_ONLY) {
					continue;
				}
				lua_pushstring(L, attr.name.c_str());
				luaW_pushfaivariant(L, obj->query_value(attr.name));
				lua_settable(L, -3);
			}
		}
	} else if(val.is_null()) {
		lua_pushnil(L);
	}
}
示例#6
0
   oWalletContactEntry WalletDb::remove_contact( const variant& data )
   { try {
       FC_ASSERT( is_open() ,"Wallet not open!");
       FC_ASSERT( !data.is_null() );

       oWalletContactEntry entry = lookup_contact( data );
       if( entry.valid() )
       {
           label_to_account_or_contact.erase( entry->label );
           contacts.erase( data.as_string() );
           remove_item( entry->wallet_entry_index );
       }

       return entry;
   } FC_CAPTURE_AND_RETHROW( (data) ) }
示例#7
0
   owallet_contact_record wallet_db::remove_contact( const variant& data )
   { try {
       FC_ASSERT( is_open() );
       FC_ASSERT( !data.is_null() );

       owallet_contact_record record = lookup_contact( data );
       if( record.valid() )
       {
           label_to_account_or_contact.erase( record->label );
           contacts.erase( data.as_string() );
           remove_item( record->wallet_record_index );
       }

       return record;
   } FC_CAPTURE_AND_RETHROW( (data) ) }
void playable_custom_object::set_player_value_by_slot(int slot, const variant& value)
{
	switch(slot) {
	case CUSTOM_OBJECT_PLAYER_DIFFICULTY:
		difficulty_ = value.as_int();
	break;
	case CUSTOM_OBJECT_PLAYER_CAN_INTERACT:
		can_interact_ = value.as_int();
	break;
	case CUSTOM_OBJECT_PLAYER_UNDERWATER_CONTROLS:
		underwater_controls_ = value.as_bool();
	break;
	case CUSTOM_OBJECT_PLAYER_VERTICAL_LOOK:
		vertical_look_ = value.as_int();
	break;
	case CUSTOM_OBJECT_PLAYER_CONTROL_LOCK:
		if(value.is_null()) {
			control_lock_.reset();
		} else if(value.is_list()) {
			unsigned char state = 0;
			for(int n = 0; n != value.num_elements(); ++n) {
				ASSERT_LOG(value[n].is_string(), "MEMBER OF control_lock LIST NOT A STRING");
				const std::string& str = value[n].as_string();
				int control_key = -1;
				for(int m = 0; m != sizeof(ctrl)/sizeof(*ctrl); ++m) {
					if(ctrl[m] == str) {
						control_key = m;
						break;
					}
				}

				ASSERT_LOG(control_key != -1, "ILLEGAL STRING SET FOR control_lock: '" << str << "' LEGAL KEYS ARE ctrl_(up|down|left|right|attack|jump)");
				state |= 1 << control_key;
			}

			//destroy the old one before creating a new control_lock,
			//since control_lock objects must be constructed and destroyed
			//in FIFO order.
			control_lock_.reset();
			control_lock_.reset(new controls::local_controls_lock(state));
		} else {
			ASSERT_LOG(false, "BAD VALUE WHEN SETTING control_lock KEY. A LIST OR NULL IS REQUIRED: " << value.to_debug_string());
		}
	break;
	}
}
示例#9
0
void label::set_value(const std::string& key, const variant& v)
{
	if(key == "text") {
		if(v.is_null()) {
			set_text("");
		} else {
			set_text(v.as_string());
		}
	} else if(key == "color") {
		set_color(graphics::color(v).as_sdl_color());
	} else if(key == "size") {
		set_font_size(v.as_int());
	} else if(key == "font") {
		set_font(v.as_string());
	}
	widget::set_value(key, v);
}
示例#10
0
LayerType read_layer_type(const variant& v) {
	LayerType result;
	result.last_edited_variation = v["last_edited_variation"].as_string_default();
	result.symmetric = v["symmetric"].as_bool(false);
	variant layers_node = v["variations"];
	if(layers_node.is_null()) {
		Layer default_layer;
		default_layer.name = "default";
		result.variations["default"] = default_layer;
		return result;
	}

	for(const std::pair<variant,variant>& p : layers_node.as_map()) {
		Layer layer;
		layer.name = p.first.as_string();
		variant layer_node = p.second;
		if(layer_node["voxels"].is_list()) {
			for(variant v : layer_node["voxels"].as_list()) {
				read_voxels(v, &layer.map);
			}
		}

		result.variations[layer.name] = layer;
	}

	const variant pivots_node = v["pivots"];
	if(!pivots_node.is_null()) {
		for(const variant::map_pair& p : pivots_node.as_map()) {
			std::vector<int> pos = p.second.as_list_int();
			ASSERT_LOG(pos.size() == 3, "Invalid pivot position: " << p.second.write_json() << " " << p.second.debug_location());
			VoxelPos voxel_pos;
			std::copy(pos.begin(), pos.end(), &voxel_pos[0]);
			result.pivots[p.first.as_string()] = voxel_pos;
		}
	}

	return result;
}
示例#11
0
	void load_preferences()
	{
		std::string path;
		if(preferences_path_.empty()) {
#if defined( _WINDOWS )
		preferences_path_ = winPrefs.GetPreferencePath();
		save_file_path_ = winPrefs.GetSaveFilePath();
		auto_save_file_path_ = winPrefs.GetAutoSaveFilePath();
		path = preferences_path_;
#else
		path = PREFERENCES_PATH;
#endif // defined( _WINDOWS )
		} else {
			path = preferences_path_;
		}
		expand_path(path);
		if(!sys::file_exists(path + "preferences.cfg")) {
			return;
		}

		variant node;
		
		try {
			node = json::parse_from_file(path + "preferences.cfg");
		} catch(json::parse_error&) {
			return;
		}

		unique_user_id = node["user_id"].as_int(0);

		use_joystick_ = node["joystick"].as_bool(use_joystick_);
		variant show_control_rects = node["show_iphone_controls"];
		if(show_control_rects.is_null() == false) {
			show_iphone_controls_ = show_control_rects.as_bool(show_iphone_controls_);
		}

		no_sound_ = node["no_sound"].as_bool(no_sound_);
		no_music_ = node["no_music"].as_bool(no_music_);
		reverse_ab_ = node["reverse_ab"].as_bool(reverse_ab_);

		sound::set_music_volume(node["music_volume"].as_int(1000)/1000.0);
		sound::set_sound_volume(node["sound_volume"].as_int(1000)/1000.0);

		const variant registry_node = node["registry"];
		if(registry_node.is_null() == false) {
			game_registry::instance().set_contents(registry_node);
		}

		if(node["code_editor"].is_map()) {
			external_code_editor_ = node["code_editor"];
		}

#if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR
		controls::set_sdlkey(controls::CONTROL_UP, static_cast<SDLKey>(node["key_up"].as_int(SDLK_UP)));
		controls::set_sdlkey(controls::CONTROL_DOWN, static_cast<SDLKey>(node["key_down"].as_int(SDLK_DOWN)));
		controls::set_sdlkey(controls::CONTROL_LEFT, static_cast<SDLKey>(node["key_left"].as_int(SDLK_LEFT)));
		controls::set_sdlkey(controls::CONTROL_RIGHT, static_cast<SDLKey>(node["key_right"].as_int(SDLK_RIGHT)));
		controls::set_sdlkey(controls::CONTROL_ATTACK, static_cast<SDLKey>(node["key_attack"].as_int(SDLK_d)));
		controls::set_sdlkey(controls::CONTROL_JUMP, static_cast<SDLKey>(node["key_jump"].as_int(SDLK_a)));
		controls::set_sdlkey(controls::CONTROL_TONGUE, static_cast<SDLKey>(node["key_tongue"].as_int(SDLK_s)));
#endif
	}