在XCODE项目中使用lua--xcode 4.2
2014-06-11 22:05
323 查看
从http://www.lua.org 下载Lua ,解压缩
打开你要添加使用lua的项目工程,选择点击左侧工程管理文件,点击右边资源栏下方Add Target,选择添加一个 static Libarary 工程.取名叫lua
将解压后的lua src文件 拖动到工程新生成的lua 文件夹下,
去掉Create external build前面的勾
next 后选择targets里的 lua
编译静态库 lua 项目
选择原本项目工程 target 加入lua 静态库
现在就可以在项目中使用lua了
二、使用Lua
使用Lua,首先要在本地新建Lua文件。比如要在iPhone中使用,可以将文件建立在App下的Documents文件夹下,或者可以放在服务器端,使用时下载下来。
比如此处我新建了两个Lua文件,一个叫Engine.lua,用来驱动真正的lua执行方法的文件,另一个叫todo.lua,用来实现你要让IOS程序执行的方法。
要调用lua,首先你可以像正常那样建立一个ViewController,在ViewController里写调用Engine.lua的方法,将这个方法放在ViewController的一个线程里执行。
ViewController的代码:
// ViewController.m
// wwwwww
//
// Created by Tolecen 12-11-19.
// Copyright (c) 2012年 Tolecen . All rights reserved.
//
#import "ViewController.h"
#include "lua.hpp"
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#define NOERROR 0
#define LUA_PMCNT_CHECK(n)\
{ \
int nParamCnt = lua_gettop(L) ; \
if( nParamCnt != n ) \
{\
lua_pushnumber( L , -1 ) ; \
return 1 ; \
} \
}
#define LUA_RET_VALUE(n)\
{ \
lua_pushnumber( L , n ); \
return 1 ; \
}
#define LUA_OUT_ERROR(id) lua_pushnumber(L, id);if (id != 0) return 1;
int logYouWhat(lua_State *L) //你想要让Lua执行的方法,需用C++写
{
LUA_PMCNT_CHECK(2);
int aa = (int)lua_tonumber(L, 1);
int bb = (int)lua_tonumber(L, 2);
NSLog(@"aa:%d,bb:%d",aa,bb);
[ViewController changeA:aa B:bb]; //在此c++方法里通过这个ViewController的类方法调用OC方法,以实现设置UI等操作
return 0;
}
unsigned long luaWork(void* pParam) //此方法用来调用Engine.lua,以驱动Lua语句
{
int i;
int j;
int s0;
int s1;
int nResult;
lua_State *L;
int nThreadIndex;
lua_State *lLocal;
char strPathName[512];
char strLuaEngine[512];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
memset(strPathName, 0, 512);
const char * tmp= [documentsDirectory UTF8String];
strncpy(strPathName, (char*)tmp, strlen(tmp)); //这是查找本地App的Documents语句,以便将lua文件加到此处
L = luaL_newstate();
if (L == NULL)
{
NSLog(@"error: %s \n" , lua_tostring(L, -1)); //lua程序执行有错误,此处会打印lua的错误行数,下同
return -1;
}
luaL_openlibs(L);
j = 0;
for (i = 0;i < 260;i++)
{
if (strPathName[i] == 0)
break;
if (strPathName[i] == '\\')
j = i;
}
if (j != 0)
strPathName[j] = 0;
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"Engine.lua"];
const char * tmp1= [logPath UTF8String];
strncpy(strLuaEngine, (char*)tmp1, strlen(tmp1));
NSLog(@"strLuaEngine %s \n",strLuaEngine);
s1 = luaL_loadfile(L, strLuaEngine); //通过Engine.lua的路径load lua文件,实现要将Engine.lua和todo.lua存入目录
if (s1 != 0)
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop(L, 1);
return -10;
}
s1 = lua_pcall(L, 0, 0, 0);
if (s1 != 0)
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
getchar();
lua_pop(L, 1);
return -11;
}
lua_register(L, "logSomething", logYouWhat);//将这个Viewcontroller中的方法在Lua中注册,中间那个就是要在lua里执行的方 法名
lua_getglobal(L, "ywyengine");
if (!lua_istable(L, -1))
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop(L, 1);
return -3;
}
lua_pushstring(L, "CreateSession");
lua_gettable(L, -2);
if (!lua_isfunction(L, -1))
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop(L, 2);
return -4;
}
s0 = lua_pcall(L, 0, 2, 0);
if (s0 != 0)
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop( L, 1 );
return -5;
}
lLocal = lua_tothread(L, -2);
nThreadIndex = lua_tonumber(L, -1);
lua_pop(L, 2);
while(1)
{
NSLog(@"run");
lua_getglobal(L, "ywyengine");
if (!lua_istable(L, -1))
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop(L, 1);
return -6;
}
lua_pushstring(L, "ContinueSession");
lua_gettable(L, -2);
if (!lua_isfunction(L, -1))
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop(L, 2);
return -7;
}
lua_pushnumber(L, nThreadIndex);
s0 = lua_pcall(L, 1, 1, 0);
if (s0 != 0)
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop( L, 1 );
return -8;
}
nResult = lua_tonumber(L, -1);
lua_pop(L, 1);
if (nResult)
return -9;
}
return 0;
}
@interface ViewController ()@end
@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; dispatch_queue_t queue = dispatch_queue_create("com.example.dothis", NULL); dispatch_async(queue, ^{ luaWork(NULL); });// Do any additional setup after loading the view, typically from a nib.}
+(void)changeA:(int)as B:(int)bs{ NSLog(@"hahaTHEA:%d",as);}
Engine.lua的代码
require("/var/mobile/Applications/4FDA7C3C-984F-43C9-A306-6C95C758AF78/Documents/todo")--上面是将todo.lua的路径交代给Engine.lua,以便调用todo.lua里的方法,每一个程序的路径不一样,要改成自己程序的路径threadhandle = {}handlecount = 0ywyengine = {}
function ywyengine.Susport( )coroutine.yield()end
function ywyengine.ContinueSession( handleindex )local status, err, result
if (type(threadhandle[handleindex]) ~= "thread") thenprint( "ContinueSession error no thread" ) result = 1return resultendif (threadhandle[handleindex] == nil) thenprint( "ContinueSession error thread nil" ) result = 2return resultend
if (coroutine.status( threadhandle[handleindex] ) == "dead") thenresult = 3print( "ContinueSession error thread dead" ) threadhandle[handleindex] = nilreturn resultendstatus , err = coroutine.resume( threadhandle[handleindex] , 0 )
if status == false then result = 4 print( "ContinueSession error" ) print(err) else result = 0end print( "ContinueSession ok" ) return resultend
function ywyengine.CreateSession() local handle = coroutine.create( StartSession ) local status, err local sessioninfo = {} local coroutine_info = { param = sessioninfo , } handlecount = handlecount + 1 threadhandle[handlecount] = handle status, err = coroutine.resume( threadhandle[handlecount] , coroutine_info ) if status == false then print( "CreateSession error" ) print(err) end print("Start session ok") return handle, handlecountend
以下为todo.lua的代码
function StartSession(args)
while true do
logSomething(45,78);//这就是viewcontroll中注册的那个方法,对应的c++方法在上方已写 print("Continue ok"); ywyengine.Susport();endend
打开你要添加使用lua的项目工程,选择点击左侧工程管理文件,点击右边资源栏下方Add Target,选择添加一个 static Libarary 工程.取名叫lua
将解压后的lua src文件 拖动到工程新生成的lua 文件夹下,
去掉Create external build前面的勾
next 后选择targets里的 lua
编译静态库 lua 项目
选择原本项目工程 target 加入lua 静态库
现在就可以在项目中使用lua了
二、使用Lua
使用Lua,首先要在本地新建Lua文件。比如要在iPhone中使用,可以将文件建立在App下的Documents文件夹下,或者可以放在服务器端,使用时下载下来。
比如此处我新建了两个Lua文件,一个叫Engine.lua,用来驱动真正的lua执行方法的文件,另一个叫todo.lua,用来实现你要让IOS程序执行的方法。
要调用lua,首先你可以像正常那样建立一个ViewController,在ViewController里写调用Engine.lua的方法,将这个方法放在ViewController的一个线程里执行。
ViewController的代码:
// ViewController.m
// wwwwww
//
// Created by Tolecen 12-11-19.
// Copyright (c) 2012年 Tolecen . All rights reserved.
//
#import "ViewController.h"
#include "lua.hpp"
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#define NOERROR 0
#define LUA_PMCNT_CHECK(n)\
{ \
int nParamCnt = lua_gettop(L) ; \
if( nParamCnt != n ) \
{\
lua_pushnumber( L , -1 ) ; \
return 1 ; \
} \
}
#define LUA_RET_VALUE(n)\
{ \
lua_pushnumber( L , n ); \
return 1 ; \
}
#define LUA_OUT_ERROR(id) lua_pushnumber(L, id);if (id != 0) return 1;
int logYouWhat(lua_State *L) //你想要让Lua执行的方法,需用C++写
{
LUA_PMCNT_CHECK(2);
int aa = (int)lua_tonumber(L, 1);
int bb = (int)lua_tonumber(L, 2);
NSLog(@"aa:%d,bb:%d",aa,bb);
[ViewController changeA:aa B:bb]; //在此c++方法里通过这个ViewController的类方法调用OC方法,以实现设置UI等操作
return 0;
}
unsigned long luaWork(void* pParam) //此方法用来调用Engine.lua,以驱动Lua语句
{
int i;
int j;
int s0;
int s1;
int nResult;
lua_State *L;
int nThreadIndex;
lua_State *lLocal;
char strPathName[512];
char strLuaEngine[512];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
memset(strPathName, 0, 512);
const char * tmp= [documentsDirectory UTF8String];
strncpy(strPathName, (char*)tmp, strlen(tmp)); //这是查找本地App的Documents语句,以便将lua文件加到此处
L = luaL_newstate();
if (L == NULL)
{
NSLog(@"error: %s \n" , lua_tostring(L, -1)); //lua程序执行有错误,此处会打印lua的错误行数,下同
return -1;
}
luaL_openlibs(L);
j = 0;
for (i = 0;i < 260;i++)
{
if (strPathName[i] == 0)
break;
if (strPathName[i] == '\\')
j = i;
}
if (j != 0)
strPathName[j] = 0;
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"Engine.lua"];
const char * tmp1= [logPath UTF8String];
strncpy(strLuaEngine, (char*)tmp1, strlen(tmp1));
NSLog(@"strLuaEngine %s \n",strLuaEngine);
s1 = luaL_loadfile(L, strLuaEngine); //通过Engine.lua的路径load lua文件,实现要将Engine.lua和todo.lua存入目录
if (s1 != 0)
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop(L, 1);
return -10;
}
s1 = lua_pcall(L, 0, 0, 0);
if (s1 != 0)
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
getchar();
lua_pop(L, 1);
return -11;
}
lua_register(L, "logSomething", logYouWhat);//将这个Viewcontroller中的方法在Lua中注册,中间那个就是要在lua里执行的方 法名
lua_getglobal(L, "ywyengine");
if (!lua_istable(L, -1))
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop(L, 1);
return -3;
}
lua_pushstring(L, "CreateSession");
lua_gettable(L, -2);
if (!lua_isfunction(L, -1))
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop(L, 2);
return -4;
}
s0 = lua_pcall(L, 0, 2, 0);
if (s0 != 0)
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop( L, 1 );
return -5;
}
lLocal = lua_tothread(L, -2);
nThreadIndex = lua_tonumber(L, -1);
lua_pop(L, 2);
while(1)
{
NSLog(@"run");
lua_getglobal(L, "ywyengine");
if (!lua_istable(L, -1))
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop(L, 1);
return -6;
}
lua_pushstring(L, "ContinueSession");
lua_gettable(L, -2);
if (!lua_isfunction(L, -1))
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop(L, 2);
return -7;
}
lua_pushnumber(L, nThreadIndex);
s0 = lua_pcall(L, 1, 1, 0);
if (s0 != 0)
{
NSLog(@"error: %s \n" , lua_tostring(L, -1));
lua_pop( L, 1 );
return -8;
}
nResult = lua_tonumber(L, -1);
lua_pop(L, 1);
if (nResult)
return -9;
}
return 0;
}
@interface ViewController ()@end
@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; dispatch_queue_t queue = dispatch_queue_create("com.example.dothis", NULL); dispatch_async(queue, ^{ luaWork(NULL); });// Do any additional setup after loading the view, typically from a nib.}
+(void)changeA:(int)as B:(int)bs{ NSLog(@"hahaTHEA:%d",as);}
Engine.lua的代码
require("/var/mobile/Applications/4FDA7C3C-984F-43C9-A306-6C95C758AF78/Documents/todo")--上面是将todo.lua的路径交代给Engine.lua,以便调用todo.lua里的方法,每一个程序的路径不一样,要改成自己程序的路径threadhandle = {}handlecount = 0ywyengine = {}
function ywyengine.Susport( )coroutine.yield()end
function ywyengine.ContinueSession( handleindex )local status, err, result
if (type(threadhandle[handleindex]) ~= "thread") thenprint( "ContinueSession error no thread" ) result = 1return resultendif (threadhandle[handleindex] == nil) thenprint( "ContinueSession error thread nil" ) result = 2return resultend
if (coroutine.status( threadhandle[handleindex] ) == "dead") thenresult = 3print( "ContinueSession error thread dead" ) threadhandle[handleindex] = nilreturn resultendstatus , err = coroutine.resume( threadhandle[handleindex] , 0 )
if status == false then result = 4 print( "ContinueSession error" ) print(err) else result = 0end print( "ContinueSession ok" ) return resultend
function ywyengine.CreateSession() local handle = coroutine.create( StartSession ) local status, err local sessioninfo = {} local coroutine_info = { param = sessioninfo , } handlecount = handlecount + 1 threadhandle[handlecount] = handle status, err = coroutine.resume( threadhandle[handlecount] , coroutine_info ) if status == false then print( "CreateSession error" ) print(err) end print("Start session ok") return handle, handlecountend
以下为todo.lua的代码
function StartSession(args)
while true do
logSomething(45,78);//这就是viewcontroll中注册的那个方法,对应的c++方法在上方已写 print("Continue ok"); ywyengine.Susport();endend
相关文章推荐
- 在XCODE项目中使用lua--xcode 4.2
- 禁止项目在xCode4.2中使用ARC
- 使用LuaPlus在C++项目中集成Lua脚本
- iPhone 项目使用静态库(xcode使用)
- 关于下拉更新框架 EGORefreshTableHeaderView在Xcode4.2中, ARC下面的使用
- 使用xcode4.2检测内存泄露leak的具体位置的两种方法总结
- 使用xcode4.2做ios的国际化
- 使用LuaPlus在C++项目中集成Lua脚本
- Xcode4.2中将Three20开源库导入到工程项目中
- xcode 4.2使用方法初探--控制台程序指定参数
- xcode 4.2使用方法初探--第一个objective-c程序
- Xcode4.2不能使用 release ,retainCount, retain的解决办法
- Xcode4.2 b2下Foundation项目的编译错误的解决方法
- Objective-C学习-如何使用Xcode4.2和Interface Builder创建自己第一个界面产品
- xcode4.2 空项目如何加载view
- 关于Xcode4.2中的release“不能”使用的理解
- xcode4.2 beta 的改进明显,值得使用…
- XCode4.2中使用Empty Application模板替代旧的Window Based Application
- XCode4.2中使用Empty Application模板替代旧的Window Based Application
- xCode4.2中导入OpenFlow库到项目中出现编译警告warning: Semantic Issue: Writable atomic property