Example #1
0
IPAddress::IPAddress(StringRef ip, ushort port, bool isIP6 /*= false*/)
{
	if (isIP6)
	{
		Memory::ClearZero(&mAddress6);
		mAddress6.sin6_family = (int)SocketAddressFamily::IP6;
		mAddress6.sin6_port = BitConverter::HostToNetwork(port);
		if (!ip.IsEmpty())	//0.0.0.0 or "" to bind to any local ip
		{
			if (inet_pton(mAddress6.sin6_family, ip.c_str(), &mAddress6.sin6_addr) <= 0)
			{
				Log::AssertFailedFormat("Invalid ip6:{}", ip);
			}
		}
		
	}
	else
	{
		Memory::ClearZero(&mAddress);
		mAddress.sin_family = (int)SocketAddressFamily::IP;
		mAddress.sin_port = BitConverter::HostToNetwork(port);

		if (!ip.IsEmpty())
		{
			if (inet_pton(mAddress.sin_family, ip.c_str(), &mAddress.sin_addr) <= 0)
			{
				Log::AssertFailedFormat("Invalid ip:{}", ip);
			}
		}
		
	}
}
Example #2
0
StringRef SirenTextParser::ReadToken(StringRef& refProto)
{
	refProto = refProto.TrimBegin();
	if (refProto.IsEmpty())
	{
		return StringRef::Empty;
	}
	if (StdString::IsDigit(refProto[0]))
	{
		Log::FormatError("Token cannot begin with number:{}", refProto[0]);
		return StringRef::Empty;
	}

	size_t tokenLength = 0;
	size_t length = refProto.Length();
	for (size_t i = 0; i < length - 1; ++i)
	{
		int c = refProto[i];
		if (StdString::IsToken(c))
		{
			++tokenLength;
		}
		else
		{
			break;
		}
	}
	StringRef token = refProto.SubString(0, tokenLength);
	refProto = refProto.SubString(tokenLength);
	return token;

}
Example #3
0
MemoryData ZipReader::ReadAllData(StringRef fileName)const
{
	MemoryData result;

	if (mZipFile==nullptr||fileName.IsEmpty())
	{
		return result;
	}


	const ZipFileInfo* zipEntryInfo=mFileDict.TryGetByOtherKey(fileName,fileName.HashCode());
	if (zipEntryInfo==nullptr)
	{
		return result;
	}

	int err = unzGoToFilePos(mZipFile, (unz_file_pos*)&zipEntryInfo->Pos);
	if (err!=UNZ_OK)
	{
		return result;
	}

	err = unzOpenCurrentFile(mZipFile);
	if (err!=UNZ_OK)
	{
		return result;
	}

	result=MemoryData::Alloc(zipEntryInfo->UncompressedSize);
	int readSize = unzReadCurrentFile(mZipFile, result.MutableData(), (uint)zipEntryInfo->UncompressedSize);
	Log::Assert(readSize==(int)zipEntryInfo->UncompressedSize,"Invalid zip file size.");	//readSize could be 0 because we may have zero file such as "StringTable-enus.bin"
	unzCloseCurrentFile(mZipFile);

	return result;
}
bool StringPropertySet::ToBool(const StringRef& key, bool optional /*= false*/) const
{
	StringRef val = Get(key);
	if (val.IsEmpty())
	{
		return optional;
	}
	return StringParser::StringTo<bool>(val);
}
Example #5
0
bool SirenTextParser::ReadExpectedChar(StringRef& refProto, char val)
{
	refProto = refProto.TrimBegin();
	if (refProto.IsEmpty() || refProto[0] != val)
	{
		return false;
	}
	refProto = refProto.SubString(1);
	return true;
}
Example #6
0
char SirenTextParser::ReadNextPrintChar(StringRef& refProto)
{
	refProto = refProto.TrimBegin();
	if (refProto.IsEmpty())
	{
		return '\0';
	}

	char c = refProto[0];
	refProto = refProto.SubString(1);
	return c;
}
Example #7
0
bool FontId::IsBitmapFont(const StringRef& name)
{
	if (name.IsEmpty())
	{
		return false;
	}
	auto ext = Path::GetExtension(name);
	if (ext == ".pvr" || ext == "fnt")
	{
		return true;
	}
	return false;
}
Example #8
0
bool TiledImage::Parse(const pugi::xml_node& node)
{
	// Read all the attribute into member variables.
	mSource= FileId::ParseFrom(node.attribute("source").as_string(nullptr));
	if (mSource.IsEmpty())
	{
		Log::AssertFailed("Invalid image xml node source attribute");
		return false;
	}

	mSize.Width = node.attribute("width").as_int(0);
	mSize.Height = node.attribute("height").as_int(0);

	const char* transparentColorStr = node.attribute("trans").as_string(nullptr);
	mTransparentColor = TiledMap::ParseColor(transparentColorStr);


	StringRef formatStr = node.attribute("format").as_string(nullptr);

	if (formatStr == "png")
	{
		mEmbeddedFileType=FileType::png;
	}
	else if (formatStr == "jpg")
	{
		mEmbeddedFileType=FileType::jpeg;
	}
	else if (!formatStr.IsEmpty())
	{
		Log::FormatError("Unsupported image type:{}", formatStr.c_str());
		return false;
	}

	pugi::xml_node dataNode = node.child("data");
	if (!dataNode.empty())
	{
		StringRef encodingStr = node.attribute("encoding").as_string(nullptr);
		StringRef compressionStr = node.attribute("compression").as_string(nullptr);
		if (encodingStr != "base64")
		{
			Log::FormatError("Unsupported encoding type:{}", encodingStr.c_str());
			return false;
		}

		const char* text = dataNode.value();
		Base64Decoder decoder;
		mEmbeddedImageData = decoder.Code(text);
	}

	return true;
}
Example #9
0
void ThreadPool::DeleteWork(ThreadPoolWork* work)
{
	RETURN_IF_NULL(work);
	StringRef name = work->Name();
	if (name.IsEmpty())
	{
		mUnnamedWorks.Remove(work);
	}
	else
	{
		mNamedWorks.RemoveKey(name);
	}

	SAFE_RELEASE(work);
}
Example #10
0
bool BehaviorConfig::LoadFromData(const FileIdRef& fileId, const MemoryData& data, uint format /*= 0*/)
{
	Unload();
	RETURN_FALSE_IF(data.IsNull());

	pugi::xml_document doc;
	pugi::xml_parse_result result = doc.load_buffer(data.Data(), data.Size());
	if (!result)
	{
		Log::AssertFailedFormat("Cannot parse xml:{} because {}", fileId, result.description());
		return false;
	}
	for (const auto& child : doc.first_child().children())
	{
		StringRef typeName = child.name();
		StringRef id = child.attribute("Id").value();
		if (id.IsEmpty())
		{
			id = typeName;
		}

#ifdef MEDUSA_SAFE_CHECK
		if (ContainsId(id))
		{
			Log::AssertFailedFormat("Duplicate id:{} in {}", id.c_str(), typeName.c_str());
		}
#endif

		IBehavior* behavior = BehaviorFactory::Instance().SmartCreate(typeName);
		behavior->LoadFromXmlNode(child);
		behavior->Initialize();
		if (id.EndWith("Behavior"))
		{
			Add(id, behavior);
		}
		else
		{
			Add(id + "Behavior", behavior);
		}
	}


	return true;
}
Example #11
0
bool IActBehavior::LoadFromXmlNode(pugi::xml_node node)
{
	RETURN_FALSE_IF_FALSE(IBehavior::LoadFromXmlNode(node));
	StringRef typeName = node.name();
	StringRef predicateId = node.attribute("Predicate").value();

	if (!predicateId.IsEmpty())
	{
		Log::FormatError("Invalid predicate:{} on {}", predicateId.c_str(), typeName.c_str());
		return false;
	}
	if (node.first_child() != nullptr)
	{
		Log::FormatError("Act behavior cannot have children. {}", typeName.c_str());
		return false;
	}

	return true;
}
Example #12
0
void SkeletonSlot::SetAttachment(const StringRef& attachmentName)
{
	if (attachmentName.IsEmpty())
	{
		SetAttachment(nullptr);
		return;
	}

	if (mAttachment != nullptr&&mAttachment->Name() == attachmentName)
	{
		return;
	}

	ISkeletonAttachmentModel* attachment = mSkeleton->AvatarModel()->FindAttachment(mModel, attachmentName);
	if (attachment == nullptr)
	{
		Log::AssertFailedFormat("Cannot find slot attachment:{}", attachmentName.c_str());
		return;
	}
	SetAttachment(attachment);
}
Example #13
0
void INode::RemoveAllChilds(NodeRemoveFlags flags /*= NodeRemoveFlags::OnlyChildren*/)
{
	RETURN_IF_EMPTY(mNodes);
	if (mManagedNodes.Count() == mNodes.Count())
	{
		flags = NodeRemoveFlags::All;
	}

	bool hasManaged = !mManagedNodes.IsEmpty();

	switch (flags.ToInt())
	{
		case NodeRemoveFlags::OnlyChildren.IntValue:
			if (hasManaged)
			{
				List<size_t> removedIndices;
				size_t count = mNodes.Count();
				FOR_EACH_SIZE(i, count)
				{
					INode* node = mNodes[i];
					if (!mManagedNodes.Contains(node))
					{
						node->SetParent(nullptr);
						removedIndices.Add(i);
						StringRef name = node->Name();
						if (!name.IsEmpty())
						{
							mNodeDict.RemoveKey(name);
						}
					}
				}

				mNodes.RemoveIndexes(removedIndices);
			}
			else
			{