bool InstalledWizardThread::selectBranch(gcRefPtr<UserCore::Item::ItemInfoI> &item)
{
	std::vector<uint32> vBranchIdList;

	for (uint32 x=0; x<item->getBranchCount(); x++)
	{
		auto bi = item->getBranch(x);

		if (!bi)
			continue;

		uint32 flags = bi->getFlags();

		if (HasAnyFlags(flags, UserCore::Item::BranchInfoI::BF_NORELEASES|UserCore::Item::BranchInfoI::BF_DEMO|UserCore::Item::BranchInfoI::BF_TEST))
			continue;

		if (!HasAllFlags(flags, UserCore::Item::BranchInfoI::BF_ONACCOUNT))
		{
			if (HasAnyFlags(flags, (UserCore::Item::BranchInfoI::BF_MEMBERLOCK|UserCore::Item::BranchInfoI::BF_REGIONLOCK)))
				continue;
		}

		if (!HasAllFlags(flags, UserCore::Item::BranchInfoI::BF_ONACCOUNT) && !(HasAllFlags(flags, UserCore::Item::BranchInfoI::BF_FREE)))
			continue;

		vBranchIdList.push_back(bi->getBranchId());
	}

	if (vBranchIdList.size() == 0)
		return false;

	//select the first free branch
	item->setInstalledMcf(MCFBranch::BranchFromInt(vBranchIdList[0]));
	return true;
}
void ToolManager::findJSTools(gcRefPtr<UserCore::Item::ItemInfoI> item)
{
	bool validPath = false;

	for (size_t x=0; x<item->getBranchCount(); x++)
	{
		gcString path = item->getBranch(x)->getInstallScriptPath();

		if (UTIL::FS::isValidFile(path))
			validPath = true;
	}

	if (!validPath)
		return;

	m_ScriptLock.lock();

	if (!m_pFactory)
	{
		Warning("Failed to load scriptcore for find JS Tools\n");
		m_ScriptLock.unlock();
		return;
	}

	m_uiInstanceCount++;
	m_ScriptLock.unlock();

	for (size_t x=0; x<item->getBranchCount(); x++)
	{
		gcString path = item->getBranch(x)->getInstallScriptPath();

		if (!UTIL::FS::isValidFile(path))
			continue;

		ScriptCoreI* instance = (ScriptCoreI*)m_pFactory(SCRIPT_CORE);

		UserItem* userItem = new UserItem();
		userItem->m_pItem = gcRefPtr<UserCore::Item::ItemInfo>::dyn_cast(item);
		userItem->m_pBranch = item->getBranch(x);
		userItem->m_pToolManager = this;

		try
		{
			instance->setItem(userItem);
			instance->executeScript(path.c_str());
			instance->executeString("ToolSetup();");
		}
		catch (gcException &e)
		{
			gcString errMsg(e.getErrMsg());

			if (errMsg.find("ToolSetup is not defined") == std::string::npos)
				Warning("Failed to execute toolsetup: {0}\n", e);
		}

		instance->destory();
	}

	m_ScriptLock.lock();
	m_uiInstanceCount--;
	m_ScriptLock.unlock();

	if (m_uiInstanceCount == 0)
		unloadJSEngine();
}