From 6ba2593738f582a8c2f7edf1352652106dbdf79c Mon Sep 17 00:00:00 2001
From: David Anderson <dvander@alliedmods.net>
Date: Sun, 16 Dec 2007 23:45:41 +0000
Subject: [PATCH] added amb1289 - FindPluginByNumber

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401804
---
 core/smn_core.cpp             | 13 +++++++++++++
 plugins/include/sourcemod.inc | 12 ++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/core/smn_core.cpp b/core/smn_core.cpp
index b55042ed..cd8de231 100644
--- a/core/smn_core.cpp
+++ b/core/smn_core.cpp
@@ -585,6 +585,18 @@ static cell_t GetExtensionFileStatus(IPluginContext *pContext, const cell_t *par
 	return 1;
 }
 
+static cell_t FindPluginByNumber(IPluginContext *pContext, const cell_t *params)
+{
+	CPlugin *pPlugin = g_PluginSys.GetPluginByOrder(params[1]);
+	
+	if (pPlugin == NULL)
+	{
+		return BAD_HANDLE;
+	}
+
+	return pPlugin->GetMyHandle();
+}
+
 REGISTER_NATIVES(coreNatives)
 {
 	{"AutoExecConfig",			AutoExecConfig},
@@ -607,6 +619,7 @@ REGISTER_NATIVES(coreNatives)
 	{"LogToFile",				LogToFile},
 	{"LogToFileEx",				LogToFileEx},
 	{"GetExtensionFileStatus",	GetExtensionFileStatus},
+	{"FindPluginByNumber",		FindPluginByNumber},
 	{NULL,						NULL},
 };
 
diff --git a/plugins/include/sourcemod.inc b/plugins/include/sourcemod.inc
index 842c5740..aab0d7ab 100644
--- a/plugins/include/sourcemod.inc
+++ b/plugins/include/sourcemod.inc
@@ -266,6 +266,18 @@ native bool:IsPluginDebugging(Handle:hndl);
  */
 native bool:GetPluginInfo(Handle:plugin, PluginInfo:info, String:buffer[], maxlength);
 
+/**
+ * Finds a plugin by its order in the list from the "plugins list" server 
+ * "sm" command.  You should not use this function to loop through all plugins, 
+ * use the iterator instead.  Looping through all plugins using this native 
+ * is O(n^2), whereas using the iterator is O(n).
+ *
+ * @param order_num		Number of the plugin as it appears in "sm plugins list".
+ * @return				Plugin Handle on success, INVALID_HANDLE if no plugin 
+ *						matches the given number.
+ */
+native Handle:FindPluginByNumber(order_num);
+
 /**
  * Causes the plugin to enter a failed state.  An error will be thrown and
  * the plugin will be paused until it is unloaded or reloaded.