Esempio n. 1
0
qboolean LoadLuaFile(char *path, int num_vm)
{
	int             flen = 0;
	char           *code;
	fileHandle_t    f;
	lvm_t	       *vm;

	flen = trap_FS_FOpenFile(path, &f, FS_READ);
	if(flen < 0)
	{
		LUA_LOG("Lua: can not open file %s\n", path);
		G_Printf(S_COLOR_YELLOW "Lua: can not open file %s\n", path);
		trap_FS_FCloseFile(f);
		return qfalse;
	}
	else
	{
		code = (char *)malloc(flen + 1);
		if(!code) return qfalse;
		trap_FS_Read(code, flen, f);
		*(code + flen) = '\0';
		trap_FS_FCloseFile(f);

		vm = (lvm_t *) malloc(sizeof(lvm_t));
		if(vm == NULL)
		{
			LUA_LOG("Lua: failed to allocate memory for lua VM\n");
			G_Printf(S_COLOR_YELLOW "Lua: failed to allocate memory for lua VM\n");
			return qfalse;
		}
		memset(vm, 0, sizeof(lvm_t));
		vm->id = -1;
		Q_strncpyz(vm->filename, path, sizeof(vm->filename));
		vm->code = code;
		vm->code_size = flen;
		vm->error = 0;

		if(G_LuaStartVM(vm) == qfalse)
		{
			G_LuaStopVM(vm);
			vm = NULL;
			return qfalse;
		}
		else
		{
			vm->id = num_vm;
			lVM[num_vm] = vm;
			return qtrue;
		}
	}
}
Esempio n. 2
0
void G_LuaShutdown()
{
	int             i;
	lvm_t	       *vm;

	for(i = 0; i < NUM_VMS; i++)
	{
		vm = lVM[i];
		if(vm)
		{
			G_LuaStopVM(vm);
		}
	}
}
Esempio n. 3
0
/** G_LuaShutdown()
 * Shuts down everything related to Lua API.
 */
void G_LuaShutdown()
{
	int             i;
	lua_vm_t       *vm;

	for(i = 0; i < LUA_NUM_VM; i++)
	{
		vm = lVM[i];
		if(vm)
		{
			G_LuaStopVM(vm);
		}
	}
}
Esempio n. 4
0
qboolean LoadLuaFile(char *path, int num_vm, vmType_t type)
{
	int             flen = 0;
	char           *code;
	//char           *signature;
	fileHandle_t    f;
	lua_vm_t       *vm;

	// try to open lua file
	flen = trap_FS_FOpenFile(path, &f, FS_READ);
	if(flen < 0)
	{
		LOG("Lua API: can not open file %s\n", path);
		trap_FS_FCloseFile(f);
		return qfalse;
	}
	else if(flen > LUA_MAX_FSIZE)
	{
		// quad: Let's not load arbitrarily big files to memory.
		// If your lua file exceeds the limit, let me know.
		LOG("Lua API: ignoring file %s (too big)\n", path);
		trap_FS_FCloseFile(f);
		return qfalse;
	}
	else
	{
		code = malloc(flen + 1);
		trap_FS_Read(code, flen, f);
		*(code + flen) = '\0';
		trap_FS_FCloseFile(f);
		//signature = sha1(code);

		/*
		if ( Q_stricmp(lua_allowedModules.string, "") &&
		!strstr(lua_allowedModules.string, signature) ) {
		// don't load disallowed lua modules into vm
		LOG("Lua API: Lua module [%s] [%s] disallowed by ACL\n", crt, signature);
		} else {
		*/
		// Init lua_vm_t struct
		vm = (lua_vm_t *) malloc(sizeof(lua_vm_t));
		if(vm == NULL)
		{
			LOG("Lua API: failed to allocate memory for lua VM\n", path);
			return qfalse;
		}
		memset(vm, 0, sizeof(lua_vm_t));
		vm->id = -1;
		Q_strncpyz(vm->file_name, path, sizeof(vm->file_name));
		Q_strncpyz(vm->mod_name, "", sizeof(vm->mod_name));
		Q_strncpyz(vm->mod_signature, "", sizeof(vm->mod_signature));
		//Q_strncpyz(vm->mod_signature, signature, sizeof(vm->mod_signature));
		vm->code = code;
		vm->code_size = flen;
		vm->type = type;
		vm->err = 0;

		// Start lua virtual machine
		if(G_LuaStartVM(vm) == qfalse)
		{
			G_LuaStopVM(vm);
			vm = NULL;
			return qfalse;
		}
		else
		{
			vm->id = num_vm;
			lVM[num_vm] = vm;
			return qtrue;
		}
		/*
		}
		*/
	}
	return qfalse;
}