changeset 11297:96912fe1e316 draft

(svn r15646) -Fix: Be lenient on users who do stupid things like loading newgrfs statically, which modify engine names, while dynamic_engines is enabled or the to be modified engine is not (yet) present.
author frosch <frosch@openttd.org>
date Sun, 08 Mar 2009 18:08:30 +0000
parents 1b6a11831215
children c93033de3e1b
files src/newgrf.cpp
diffstat 1 files changed, 17 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -324,7 +324,15 @@
 	grfmsg(5, "SetNewGRFOverride: Added override of 0x%X to 0x%X", BSWAP32(source_grfid), BSWAP32(target_grfid));
 }
 
-static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16 internal_id)
+/**
+ * Returns the engine associated to a certain internal_id, resp. allocates it.
+ * @param file NewGRF that wants to change the engine
+ * @param type Vehicle type
+ * @param internal_id Engine ID inside the NewGRF
+ * @param static_access If the engine is not present, return NULL instead of allocating a new engine. (Used for static Action 0x04)
+ * @return The requested engine
+ */
+static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16 internal_id, bool static_access = false)
 {
 	/* Hack for add-on GRFs that need to modify another GRF's engines. This lets
 	 * them use the same engine slots. */
@@ -359,12 +367,16 @@
 		}
 
 		/* Reserve the engine slot */
-		EngineIDMapping *eid = _engine_mngr.Get(engine);
-		eid->grfid           = scope_grfid; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
+		if (!static_access) {
+			EngineIDMapping *eid = _engine_mngr.Get(engine);
+			eid->grfid           = scope_grfid; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
+		}
 
 		return e;
 	}
 
+	if (static_access) return NULL;
+
 	uint engine_pool_size = GetEnginePoolSize();
 
 	/* ... it's not, so create a new one based off an existing engine */
@@ -3443,7 +3455,8 @@
 			case GSF_SHIP:
 			case GSF_AIRCRAFT:
 				if (!generic) {
-					Engine *e = GetNewEngine(_cur_grffile, (VehicleType)feature, id);
+					Engine *e = GetNewEngine(_cur_grffile, (VehicleType)feature, id, HasBit(_cur_grfconfig->flags, GCF_STATIC));
+					if (e == NULL) break;
 					StringID string = AddGRFString(_cur_grffile->grfid, e->index, lang, new_scheme, name, e->info.string_id);
 					e->info.string_id = string;
 				} else {