Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify engine version requirement method #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions demo/lua/main.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
-- We may want to make sure we're using a specific version of the engine
major, minor, patch = getVersion()
if major ~= 0 or minor ~= 1 or patch ~= 0 then
kiavcLog('Unsupported KIAVC engine version')
return
end
requireVersion(0, 1, 0)

-- Let's load all the resources first
kiavcRequire('game/resources')
Expand Down
33 changes: 33 additions & 0 deletions src/scripts.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static int kiavc_lua_method_kiavcrequire(lua_State *s);
static int kiavc_lua_method_getversion(lua_State *s);
/* Return version string */
static int kiavc_lua_method_getversionstring(lua_State *s);
/* Require version as major, minor, patch */
static int kiavc_lua_method_requireversion(lua_State *s);
/* Logging, to use the same SDL-based logging as the rest of the application */
static int kiavc_lua_method_kiavclog(lua_State *s);
/* Error logging, to use the same SDL-based logging as the rest of the application */
Expand Down Expand Up @@ -313,6 +315,7 @@ int kiavc_scripts_load(const char *path, const kiavc_scripts_callbacks *callback
lua_register(lua_state, "kiavcRequire", kiavc_lua_method_kiavcrequire);
lua_register(lua_state, "getVersion", kiavc_lua_method_getversion);
lua_register(lua_state, "getVersionString", kiavc_lua_method_getversionstring);
lua_register(lua_state, "requireVersion", kiavc_lua_method_requireversion);
lua_register(lua_state, "kiavcLog", kiavc_lua_method_kiavclog);
lua_register(lua_state, "kiavcError", kiavc_lua_method_kiavcerror);
lua_register(lua_state, "kiavcWarn", kiavc_lua_method_kiavcwarn);
Expand Down Expand Up @@ -556,6 +559,36 @@ static int kiavc_lua_method_getversionstring(lua_State *s) {
return 1;
}

/* Require version as major, minor, patch */
static int kiavc_lua_method_requireversion(lua_State *s) {
/* This method allows the Lua script to check whether the engine is of a compatible version (and quit if not) */
int n = lua_gettop(s), exp = 3;
if(n != exp) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[Lua] Wrong number of arguments in requireVersion: %d (expected %d)\n", n, exp);
kiavc_cb->quit();
return 0;
}
int major = luaL_checknumber(s, 1);
if(major != KIAVC_VERSION_MAJOR) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[Lua] KIAVC major version number incompatible: %d (expected %d)\n", KIAVC_VERSION_MAJOR, major);
kiavc_cb->quit();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation seems wrong in this checks and the ones below? Looks like you're using spaces instead of tabs (which I'd rather we use in this project, since it's what I'm used to).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm used to the opposite, so I didn't notice, but it's fixed now.

return 0;
}
int minor = luaL_checknumber(s, 2);
if(minor > KIAVC_VERSION_MINOR) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the SemVer reference you made, but I think it can cause ambiguity if someone is interested in a specific version. Maybe there could be different checks, where one checks if the version is exactly that, and another one if it is at least that (which could loosen the check on the major version as well). We could even just keep the "exact version" check, and leave more flexible checks to the application (using the getters for the three version numbers that exist).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't want to loosen the major version in a minimum requirement, because that number is meant to signal breaking API changes that are not backwards-compatible.

My proposed solution is to have two formats, like you said, one being the exact one (now requireEngineVersion), but the other one being the SemVer one, which I renamed to requireEngineCompatible. See commits.

SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[Lua] KIAVC minor version number too low: %d (expected %d)\n", KIAVC_VERSION_MINOR, minor);
kiavc_cb->quit();
return 0;
}
int patch = luaL_checknumber(s, 3);
if(patch > KIAVC_VERSION_PATCH) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[Lua] KIAVC patch version number too low: %d (expected %d)\n", KIAVC_VERSION_PATCH, patch);
kiavc_cb->quit();
return 0;
}
return 0;
}

/* Logging, to use the same SDL-based logging as the rest of the application */
static int kiavc_lua_method_kiavclog(lua_State *s) {
/* This method allows the Lua script to use the Janus internal logger */
Expand Down