Browse Source

Added love.isVersionCompatible. Returns true if the running version of love is compatible with the specified version.

Alex Szpakowski 10 years ago
parent
commit
44b9efb43e
3 changed files with 52 additions and 19 deletions
  1. 48 1
      src/modules/love/love.cpp
  2. 1 7
      src/scripts/boot.lua
  3. 3 11
      src/scripts/boot.lua.h

+ 48 - 1
src/modules/love/love.cpp

@@ -25,6 +25,10 @@
 
 
 #include "love.h"
 #include "love.h"
 
 
+// C
+#include <cstdio>
+#include <cstring>
+
 #ifdef LOVE_WINDOWS
 #ifdef LOVE_WINDOWS
 #include <windows.h>
 #include <windows.h>
 #endif // LOVE_WINDOWS
 #endif // LOVE_WINDOWS
@@ -198,6 +202,46 @@ static int w_love_getVersion(lua_State *L)
 	return 4;
 	return 4;
 }
 }
 
 
+static int w_love_isVersionCompatible(lua_State *L)
+{
+	const char *version = nullptr;
+	char versionbuffer[64] = {0};
+
+	if (lua_type(L, 1) == LUA_TSTRING)
+		version = luaL_checkstring(L, 1);
+	else
+	{
+		int major = luaL_checkint(L, 1);
+		int minor = luaL_checkint(L, 2);
+		int rev   = luaL_checkint(L, 3);
+
+		// Convert the numbers to a string, since VERSION_COMPATIBILITY is an
+		// array of version strings.
+		if (snprintf(versionbuffer, 64, "%d.%d.%d", major, minor, rev) < 0)
+		{
+			lua_pushboolean(L, false);
+			return 1;
+		}
+
+		version = versionbuffer;
+	}
+
+	if (version != nullptr)
+	{
+		for (int i = 0; love::VERSION_COMPATIBILITY[i] != nullptr; i++)
+		{
+			if (strcmp(version, love::VERSION_COMPATIBILITY[i]) != 0)
+				continue;
+
+			lua_pushboolean(L, true);
+			return 1;
+		}
+	}
+
+	lua_pushboolean(L, false);
+	return 1;
+}
+
 int luaopen_love(lua_State * L)
 int luaopen_love(lua_State * L)
 {
 {
 	love::luax_insistglobal(L, "love");
 	love::luax_insistglobal(L, "love");
@@ -228,7 +272,7 @@ int luaopen_love(lua_State * L)
 
 
 	lua_newtable(L);
 	lua_newtable(L);
 
 
-	for (int i = 0; love::VERSION_COMPATIBILITY[i] != 0; ++i)
+	for (int i = 0; love::VERSION_COMPATIBILITY[i] != nullptr; i++)
 	{
 	{
 		lua_pushstring(L, love::VERSION_COMPATIBILITY[i]);
 		lua_pushstring(L, love::VERSION_COMPATIBILITY[i]);
 		lua_rawseti(L, -2, i+1);
 		lua_rawseti(L, -2, i+1);
@@ -239,6 +283,9 @@ int luaopen_love(lua_State * L)
 	lua_pushcfunction(L, w_love_getVersion);
 	lua_pushcfunction(L, w_love_getVersion);
 	lua_setfield(L, -2, "getVersion");
 	lua_setfield(L, -2, "getVersion");
 
 
+	lua_pushcfunction(L, w_love_isVersionCompatible);
+	lua_setfield(L, -2, "isVersionCompatible");
+
 #ifdef LOVE_WINDOWS
 #ifdef LOVE_WINDOWS
 	lua_pushstring(L, "Windows");
 	lua_pushstring(L, "Windows");
 #elif defined(LOVE_MACOSX)
 #elif defined(LOVE_MACOSX)

+ 1 - 7
src/scripts/boot.lua

@@ -470,14 +470,8 @@ function love.init()
 	end
 	end
 
 
 	-- Check the version
 	-- Check the version
-	local compat = false
 	c.version = tostring(c.version)
 	c.version = tostring(c.version)
-	for i, v in ipairs(love._version_compat) do
-		if c.version == v then
-			compat = true
-			break
-		end
-	end
+	local compat = love.isVersionCompatible(c.version)
 	if not compat then
 	if not compat then
 		local major, minor, revision = c.version:match("^(%d+)%.(%d+)%.(%d+)$")
 		local major, minor, revision = c.version:match("^(%d+)%.(%d+)%.(%d+)$")
 		if (not major or not minor or not revision) or (major ~= love._version_major and minor ~= love._version_minor) then
 		if (not major or not minor or not revision) or (major ~= love._version_major and minor ~= love._version_minor) then

+ 3 - 11
src/scripts/boot.lua.h

@@ -856,19 +856,11 @@ const unsigned char boot_lua[] =
 	0x09, 0x65, 0x6e, 0x64, 0x0a,
 	0x09, 0x65, 0x6e, 0x64, 0x0a,
 	0x09, 0x2d, 0x2d, 0x20, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x20, 0x74, 0x68, 0x65, 0x20, 0x76, 0x65, 0x72, 0x73, 
 	0x09, 0x2d, 0x2d, 0x20, 0x43, 0x68, 0x65, 0x63, 0x6b, 0x20, 0x74, 0x68, 0x65, 0x20, 0x76, 0x65, 0x72, 0x73, 
 	0x69, 0x6f, 0x6e, 0x0a,
 	0x69, 0x6f, 0x6e, 0x0a,
-	0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x20, 0x3d, 0x20, 0x66, 0x61, 
-	0x6c, 0x73, 0x65, 0x0a,
 	0x09, 0x63, 0x2e, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x74, 0x6f, 0x73, 0x74, 0x72, 
 	0x09, 0x63, 0x2e, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x74, 0x6f, 0x73, 0x74, 0x72, 
 	0x69, 0x6e, 0x67, 0x28, 0x63, 0x2e, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x29, 0x0a,
 	0x69, 0x6e, 0x67, 0x28, 0x63, 0x2e, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x29, 0x0a,
-	0x09, 0x66, 0x6f, 0x72, 0x20, 0x69, 0x2c, 0x20, 0x76, 0x20, 0x69, 0x6e, 0x20, 0x69, 0x70, 0x61, 0x69, 0x72, 
-	0x73, 0x28, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x63, 0x6f, 
-	0x6d, 0x70, 0x61, 0x74, 0x29, 0x20, 0x64, 0x6f, 0x0a,
-	0x09, 0x09, 0x69, 0x66, 0x20, 0x63, 0x2e, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x3d, 0x20, 
-	0x76, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
-	0x09, 0x09, 0x09, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x20, 0x3d, 0x20, 0x74, 0x72, 0x75, 0x65, 0x0a,
-	0x09, 0x09, 0x09, 0x62, 0x72, 0x65, 0x61, 0x6b, 0x0a,
-	0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
-	0x09, 0x65, 0x6e, 0x64, 0x0a,
+	0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x20, 0x3d, 0x20, 0x6c, 0x6f, 
+	0x76, 0x65, 0x2e, 0x69, 0x73, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x74, 
+	0x69, 0x62, 0x6c, 0x65, 0x28, 0x63, 0x2e, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x29, 0x0a,
 	0x09, 0x69, 0x66, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x20, 0x74, 0x68, 0x65, 
 	0x09, 0x69, 0x66, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x20, 0x74, 0x68, 0x65, 
 	0x6e, 0x0a,
 	0x6e, 0x0a,
 	0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x6d, 0x61, 0x6a, 0x6f, 0x72, 0x2c, 0x20, 0x6d, 0x69, 0x6e, 
 	0x09, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x20, 0x6d, 0x61, 0x6a, 0x6f, 0x72, 0x2c, 0x20, 0x6d, 0x69, 0x6e,