[AD] version check in al_init

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Just spent some time wondering about weird looking graphics and strange
crashes until I figured out I had compiled my code with the SVN headers
but linked against the 4.9.17 libraries.

The attached patch prevents that, like we do it in A4. It still doesn't
prevent you from loading incompatible addons - not sure how we could
solve that as no macros are involved... maybe add some wrapper macros
for the addon init functions.

-- 
Elias Pschernig <elias.pschernig@xxxxxxxxxx>
diff --git a/include/allegro5/system_new.h b/include/allegro5/system_new.h
index 058cfa9..5191ae3 100644
--- a/include/allegro5/system_new.h
+++ b/include/allegro5/system_new.h
@@ -12,10 +12,12 @@ typedef struct ALLEGRO_SYSTEM ALLEGRO_SYSTEM;
 
 /* Function: al_init
  */
-#define al_init()    (al_install_system(atexit))
+#define al_init()    (_al_install_system_version_check(atexit, ALLEGRO_VERSION_INT))
 
 AL_FUNC(bool, al_install_system, (int (*atexit_ptr)(void (*)(void))));
 AL_FUNC(void, al_uninstall_system, (void));
+AL_FUNC(bool, _al_install_system_version_check,
+   (int (*atexit_ptr)(void (*)(void)), uint32_t header_version));
 AL_FUNC(ALLEGRO_SYSTEM *, al_get_system_driver, (void));
 AL_FUNC(ALLEGRO_CONFIG *, al_get_system_config, (void));
 
diff --git a/src/system_new.c b/src/system_new.c
index aa81d6c..b04bbe6 100644
--- a/src/system_new.c
+++ b/src/system_new.c
@@ -226,6 +226,41 @@ bool al_install_system(int (*atexit_ptr)(void (*)(void)))
 
 
 
+bool compatible_versions(int a, int b)
+{
+   /* An x.y.*.* version is never compatible with anything but
+    * another x.y.*.* version.
+    */
+   if ((a & 0xffff0000) != (b & 0xffff0000)) {
+      return false;
+   }
+   /* An x.y.z.* version is only compatible with x.y.z.* if y is odd.
+    * If y is event then all x.y.*.* are compatible.
+    */
+   if (((a & 0x00ff0000) >> 16) & 1) {
+      
+      if ((a & 0x0000ff00) != (b & 0x0000ff00)) {
+         return false;
+      }
+   }
+   return true;
+}
+
+
+
+bool _al_install_system_version_check(int (*atexit_ptr)(void (*)(void)),
+   uint32_t header_version)
+{
+   uint32_t library_version = al_get_allegro_version();
+   /* Note: We cannot call logging functions yet.
+    * TODO: Maybe we want to do the check after the "bootstrap" system
+    * is available at least?
+    */
+   if (!compatible_versions(header_version, library_version)) return false;
+   return al_install_system(atexit_ptr);
+}
+
+
 /* Function: al_uninstall_system
  */
 void al_uninstall_system(void)


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/