Example #1
0
// native open_dir(dir[], firstfile[], length, &FileType:type = FileType_Unknown, bool:use_valve_fs=false, const valve_path_id[] = "GAME");
static cell AMX_NATIVE_CALL amx_open_dir(AMX *amx, cell *params)
{
	int length;
	const char* path = get_amxstring(amx, params[1], 0, length);

	if (!*path)
	{
		return 0;
	}

	size_t numParams = *params / sizeof(cell);

	if (numParams >= 4 && params[5] > 0)
	{
		const char* wildcardedPath = g_LibSys.PathFormat("%s%s*", path, (path[length - 1] != '/' && path[length - 1] != '\\') ? "/" : "");
		const char* pathID = get_amxstring_null(amx, params[6], 1, length);

		static FileFindHandle_t handle;
		const char* pFirst = g_FileSystem->FindFirst(wildcardedPath, &handle, pathID);

		if (!pFirst)
		{
			return 0;
		}

		set_amxstring_utf8(amx, params[2], pFirst, strlen(pFirst), params[3] + 1);
		cell* fileType = get_amxaddr(amx, params[4]);

		*fileType = g_FileSystem->FindIsDirectory(handle) ? FileType_Directory : FileType_File;

		return reinterpret_cast<cell>(new DirectoryHandle(reinterpret_cast<void*>(&handle), true));
	}

	CDirectory* dir = g_LibSys.OpenDirectory(build_pathname("%s", path));

	if (!dir)
	{
		return 0;
	}

	if (numParams >= 4)
	{
		cell* fileType = get_amxaddr(amx, params[4]);
		*fileType = dir->IsEntryDirectory() ? FileType_Directory : FileType_File;
	}

	const char* entry = dir->GetEntryName();
	set_amxstring_utf8(amx, params[2], entry, strlen(entry), params[3] + 1);

	return reinterpret_cast<cell>(new DirectoryHandle(reinterpret_cast<void*>(dir), false));
}
Example #2
0
// native next_file(dirh, buffer[], length, &FileType:type = FileType_Unknown);
static cell AMX_NATIVE_CALL amx_get_dir(AMX *amx, cell *params)
{
	DirectoryHandle* p = reinterpret_cast<DirectoryHandle*>(params[1]);

	if (!p)
	{
		return 0;
	}

	size_t numParams = *params / sizeof(cell);

	if (p->valvefs)
	{
		FileFindHandle_t* handle = reinterpret_cast<FileFindHandle_t*>(p->handle);

		if (!handle)
		{
			return 0;
		}

		const char* entry = g_FileSystem->FindNext(*handle);

		if (!entry)
		{
			return 0;
		}

		if (numParams >= 4)
		{
			cell* fileType = get_amxaddr(amx, params[4]);
			*fileType = g_FileSystem->FindIsDirectory(*handle) ? FileType_Directory : FileType_File;
		}

		set_amxstring_utf8(amx, params[2], entry, strlen(entry), params[3] + 1);
	}
	else
	{
		CDirectory* handle = reinterpret_cast<CDirectory*>(p->handle);

		if (!handle)
		{
			return 0;
		}

		handle->NextEntry();

		if (!handle->MoreFiles())
		{
			return 0;
		}

		if (numParams >= 4)
		{
			cell* fileType = get_amxaddr(amx, params[4]);
			*fileType = handle->IsEntryDirectory() ? FileType_Directory : FileType_File;
		}

		const char* entry = handle->GetEntryName();

		set_amxstring_utf8(amx, params[2], entry, strlen(entry), params[3] + 1);
	}

	return 1;
}