]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/services_discovery.c
Export VLC's md5 API.
[vlc] / modules / misc / lua / services_discovery.c
index 5f4e66de48d29689d469ddfd068615a5ddd592ff..ebe248419113825534413a0b8486ec344c637811 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * services_discovery.c : Services discovery using lua scripts
  *****************************************************************************
- * Copyright (C) 2009 the VideoLAN team
+ * Copyright (C) 2009 VideoLAN and AUTHORS
  *
  * Authors: Fabio Ritrovato <sephiroth87 at videolan dot org>
  *
@@ -56,13 +56,28 @@ int Open_LuaSD( vlc_object_t *p_this )
 {
     services_discovery_t *p_sd = ( services_discovery_t * )p_this;
     services_discovery_sys_t *p_sys;
-    lua_State *L;
-    char *psz_name = NULL;
+    lua_State *L = NULL;
+    char *psz_name = strdup(p_sd->psz_name);
+
+    if( !strcmp(p_sd->psz_name, "lua"))
+    {
+        // We want to load the module name "lua"
+        // This module can be used to load lua script not registered
+        // as builtin lua SD modules.
+        config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
+        psz_name = var_CreateGetString( p_sd, "lua-sd" );
+    }
+    else
+    {
+        // We are loading a builtin lua sd module.
+        psz_name = strdup(p_sd->psz_name);
+    }
 
-    config_ChainParse( p_sd, "lua-", ppsz_sd_options, p_sd->p_cfg );
-    psz_name = var_CreateGetString( p_sd, "lua-sd" );
     if( !( p_sys = malloc( sizeof( services_discovery_sys_t ) ) ) )
+    {
+        free( psz_name );
         return VLC_ENOMEM;
+    }
     p_sd->p_sys = p_sys;
     p_sys->psz_filename = vlclua_find_file( p_this, "sd", psz_name );
     if( !p_sys->psz_filename )
@@ -70,17 +85,14 @@ int Open_LuaSD( vlc_object_t *p_this )
         msg_Err( p_sd, "Couldn't find lua services discovery script \"%s\".",
                  psz_name );
         free( psz_name );
-        free( p_sys );
-        return VLC_EGENERIC;
+        goto error;
     }
     free( psz_name );
     L = luaL_newstate();
     if( !L )
     {
         msg_Err( p_sd, "Could not create new Lua State" );
-        free( p_sys->psz_filename );
-        free( p_sys );
-        return VLC_EGENERIC;
+        goto error;
     }
     luaL_openlibs( L );
     luaL_register( L, "vlc", p_reg );
@@ -98,25 +110,35 @@ int Open_LuaSD( vlc_object_t *p_this )
     luaopen_stream( L );
     luaopen_gettext( L );
     luaopen_xml( L );
+    luaopen_md5( L );
     lua_pop( L, 1 );
+
+    if( vlclua_add_modules_path( p_sd, L, p_sys->psz_filename ) )
+    {
+        msg_Warn( p_sd, "Error while setting the module search path for %s",
+                  p_sys->psz_filename );
+        goto error;
+    }
     if( luaL_dofile( L, p_sys->psz_filename ) )
     {
 
         msg_Err( p_sd, "Error loading script %s: %s", p_sys->psz_filename,
                   lua_tostring( L, lua_gettop( L ) ) );
         lua_pop( L, 1 );
-        free( p_sys->psz_filename );
-        free( p_sys );
-        return VLC_EGENERIC;
+        goto error;
     }
     p_sys->L = L;
     if( vlc_clone (&p_sd->p_sys->thread, Run, p_sd, VLC_THREAD_PRIORITY_LOW) )
     {
-        free( p_sys->psz_filename );
-        free( p_sys );
-        return VLC_EGENERIC;
+        goto error;
     }
     return VLC_SUCCESS;
+error:
+    if( L )
+        lua_close( L );
+    free( p_sys->psz_filename );
+    free( p_sys );
+    return VLC_EGENERIC;
 }
 
 /*****************************************************************************
@@ -150,6 +172,6 @@ static void* Run( void *data )
         lua_pop( L, 1 );
         return NULL;
     }
-    //msg_Info( p_sd, "LuaSD script loaded: %s", p_sd->psz_name );
+    msg_Dbg( p_sd, "LuaSD script loaded: %s", p_sd->psz_name );
     return NULL;
 }