(svn r9450) [NoAI] -Fix: don't allow static-method calls from SQ to non-static functions noai
authortruelight
Sun, 25 Mar 2007 16:10:40 +0000
branchnoai
changeset 9529 5f26f4bc574b
parent 9528 b2cc6f31363c
child 9530 5b93bc87cc5e
(svn r9450) [NoAI] -Fix: don't allow static-method calls from SQ to non-static functions
-Add: added GetClassName() to all API functions (needed to do the thing above ;))
src/ai/ai_squirrel.hpp
src/ai/api/ai_accounting.hpp
src/ai/api/ai_base.hpp
src/ai/api/ai_cargo.hpp
src/ai/api/ai_company.hpp
src/ai/api/ai_execmode.hpp
src/ai/api/ai_industry.hpp
src/ai/api/ai_map.hpp
src/ai/api/ai_order.hpp
src/ai/api/ai_road.hpp
src/ai/api/ai_settings.hpp
src/ai/api/ai_sign.hpp
src/ai/api/ai_testmode.hpp
src/ai/api/ai_town.hpp
src/ai/api/ai_transactionmode.hpp
src/ai/api/ai_vehicle.hpp
src/squirrel_helper.hpp
--- a/src/ai/ai_squirrel.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/ai_squirrel.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -16,6 +16,8 @@
 	void RegisterClasses();
 
 public:
+	static const char *GetClassName() { return "AIController"; }
+
 	AIControllerSquirrel(const char *script_dir, const char *class_name);
 	~AIControllerSquirrel();
 
--- a/src/ai/api/ai_accounting.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_accounting.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -22,6 +22,11 @@
 	int32 last_costs;
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIAccounting"; }
+
+	/**
 	 * Creating instance of this class starts counting the costs of commands
 	 *   from zero.
 	 * @note when the instance is destroyed, he restores the costs that was
--- a/src/ai/api/ai_base.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_base.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -21,6 +21,11 @@
 class AIBase : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIBase"; }
+
+	/**
 	 * Get a random value.
 	 * @return a random value between 0 and MAX(uint32).
 	 */
--- a/src/ai/api/ai_cargo.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_cargo.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -13,6 +13,11 @@
 class AICargo : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AICargo"; }
+
+	/**
 	 * Checks whether the given cargo type is valid.
 	 * @param cargo_type the cargo to check.
 	 * @return true if and only if the cargo type is valid.
--- a/src/ai/api/ai_company.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_company.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -13,6 +13,11 @@
 class AICompany : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AICompany"; }
+
+	/**
 	 * Set the name of your company.
 	 * @param name the new name of the company.
 	 * @pre name != NULL
--- a/src/ai/api/ai_execmode.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_execmode.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -27,6 +27,11 @@
 
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIExecMode"; }
+
+	/**
 	 * Creating instance of this class switches the build mode to Execute.
 	 * @note when the instance is destroyed, he restores the mode that was
 	 *   current when the instance was created!
--- a/src/ai/api/ai_industry.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_industry.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -13,6 +13,11 @@
 class AIIndustry : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIIndustry"; }
+
+	/**
 	 * Gets the maximum industry index; there are no valid industries with a
 	 *   higher index.
 	 * @return the maximum industry index.
--- a/src/ai/api/ai_map.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_map.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -13,6 +13,11 @@
 class AIMap : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIMap"; }
+
+	/**
 	 * Checks whether the given tile is valid.
 	 * @param t the tile to check.
 	 * @return true is the tile it within the boundaries of the map.
--- a/src/ai/api/ai_order.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_order.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -13,6 +13,11 @@
 class AIOrder : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIOrder"; }
+
+	/**
 	 * Flags that can be used to modify the behaviour of orders.
 	 */
 	enum AIOrderFlags {
--- a/src/ai/api/ai_road.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_road.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -23,6 +23,11 @@
 class AIRoad : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIRoad"; }
+
+	/**
 	 * Checks whether the given tile is actually a tile with road.
 	 * @param tile the tile to check.
 	 * @pre tile is always positive and smaller than AIMap::GetMapSize().
--- a/src/ai/api/ai_settings.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_settings.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -13,6 +13,11 @@
 class AISettings : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AISettings"; }
+
+	/**
 	 * Change the minimum amount of time the AI should be put in suspend mode
 	 *   when you execute a command. Normally in SP this is 1, and in MP it is
 	 *   what ever delay the server has been programmed to delay commands
--- a/src/ai/api/ai_sign.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_sign.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -13,6 +13,11 @@
 class AISign : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AISign"; }
+
+	/**
 	 * Gets the maximum sign index; there are no valid signs with a higher index.
 	 * @return the maximum sign index.
 	 * @post return value is always non-negative.
--- a/src/ai/api/ai_testmode.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_testmode.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -29,6 +29,11 @@
 
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AITestMode"; }
+
+	/**
 	 * Creating instance of this class switches the build mode to Testing.
 	 * @note when the instance is destroyed, he restores the mode that was
 	 *   current when the instance was created!
--- a/src/ai/api/ai_town.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_town.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -13,6 +13,11 @@
 class AITown : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AITown"; }
+
+	/**
 	 * Gets the maximum town index; there are no valid towns with a higher index.
 	 * @return the maximum town index.
 	 * @post return value is always non-negative.
--- a/src/ai/api/ai_transactionmode.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_transactionmode.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -45,6 +45,11 @@
 
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AITransactionMode"; }
+
+	/**
 	 * Creating instance of this class switches the build mode to Transaction.
 	 * @note when the instance is destroyed, he restores the mode that was
 	 *   current when the instance was created!
--- a/src/ai/api/ai_vehicle.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/ai/api/ai_vehicle.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -13,6 +13,11 @@
 class AIVehicle : public AIObject {
 public:
 	/**
+	 * The name of the class, needed by several sub-processes.
+	 */
+	static const char *GetClassName() { return "AIVehicle"; }
+
+	/**
 	 * Checks whether the given engine type is valid and buildable by you.
 	 * @param engine_id the engine to check.
 	 * @return true if and only if the engine type is valid.
--- a/src/squirrel_helper.hpp	Sun Mar 25 15:22:56 2007 +0000
+++ b/src/squirrel_helper.hpp	Sun Mar 25 16:10:40 2007 +0000
@@ -379,14 +379,27 @@
 		/* Find the amount of params we got */
 		int nparam = sq_gettop(vm);
 		SQUserPointer ptr = NULL;
-		SQUserPointer instance = NULL;
+		SQUserPointer real_instance = NULL;
+		HSQOBJECT instance;
+
+		/* Get the 'SQ' instance of this class */
+		Squirrel::GetInstance(vm, &instance);
+
+		/* Protect against calls to a non-static method in a static way */
+		sq_pushroottable(vm);
+		sq_pushstring(vm, Tcls::GetClassName(), -1);
+		sq_get(vm, -2);
+		sq_pushobject(vm, instance);
+		if (sq_instanceof(vm) != SQTrue) return sq_throwerror(vm, "class method is non-static");
+		sq_settop(vm, nparam);
 
 		/* Get the 'real' instance of this class */
-		sq_getinstanceup(vm, 1, &instance, 0);
+		sq_getinstanceup(vm, 1, &real_instance, 0);
 		/* Get the real function pointer */
 		sq_getuserdata(vm, nparam, &ptr, 0);
+
 		/* Delegate it to a template that can handle this specific function */
-		return HelperT<Tmethod>::SQCall((Tcls *)instance, *(Tmethod *)ptr, vm);
+		return HelperT<Tmethod>::SQCall((Tcls *)real_instance, *(Tmethod *)ptr, vm);
 	}
 
 }; // namespace SQConvert