<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Luin&#039;s Blog &#187; 学习&amp;技术</title>
	<atom:link href="http://luinlee.com/category/study-tech/feed/" rel="self" type="application/rss+xml" />
	<link>http://luinlee.com</link>
	<description>A long river</description>
	<lastBuildDate>Sat, 07 Aug 2010 04:55:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>有关中文分词技术和搜索结果按相似度排序</title>
		<link>http://luinlee.com/448/%e6%9c%89%e5%85%b3%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e6%8a%80%e6%9c%af%e5%92%8c%e6%90%9c%e7%b4%a2%e7%bb%93%e6%9e%9c%e6%8c%89%e7%9b%b8%e4%bc%bc%e5%ba%a6%e6%8e%92%e5%ba%8f/</link>
		<comments>http://luinlee.com/448/%e6%9c%89%e5%85%b3%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e6%8a%80%e6%9c%af%e5%92%8c%e6%90%9c%e7%b4%a2%e7%bb%93%e6%9e%9c%e6%8c%89%e7%9b%b8%e4%bc%bc%e5%ba%a6%e6%8e%92%e5%ba%8f/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 08:19:15 +0000</pubDate>
		<dc:creator>Luin</dc:creator>
				<category><![CDATA[LAMP]]></category>

		<guid isPermaLink="false">http://luinlee.com/?p=448</guid>
		<description><![CDATA[做小网站的站内搜索时要考虑到用户的查询关键字未必和数据库中相应数据准确对应，所以搜索时就要用到模糊查询。但是很多情况下Like语句也未必管用，比如用户搜索“打篮球”，可是数据库里只有“篮球”，这时做查询“LIKE &#8216;%打篮球%&#8217;”肯定不会找到结果。这时就需要对关键字进行分词。对于英文来说可以简单的根据空格来划分单词，而对于中文就有些麻烦。 好在现在有些开源的中文分词引擎分词的准确度已经相当高了，如PHP的SCWS可以把“打篮球”分成“打”和“篮球”，这样再分别查找就可以把“篮球”条目找到。然后由于SCWS是基于词频词典的，所以如果有些新词就分不出来了，比如“我爱美女”可以分解成“我爱”和“美女”，可是“我爱凤姐”就只能分成“我爱”“凤”和“姐”了。 在无法根据词库分词的情况下，我们可以采用二元分词法，即全部分成两个字组成的词，如“我爱凤姐”分成“我爱”“爱凤”“凤姐”。代码也很简单： function Fenci&#40;$word&#41; &#123; $len = mb_strlen&#40;$word, 'GBK'&#41;; for&#40;$i = 0; $i &#38;lt; $len - 1; ++$i&#41; $result&#91;&#93; = mb_substr&#40;$word, $i, 2, 'GBK'&#41;; return $result; &#125; 然而由于词语的错分也产生了一些无关的结果，如搜索“全国计算机等级考试”，SCWS会把它分成“全国”“计算机”“等级”“考试”，而数据库里有“计算机等级考试”和“大学英语水平考试”，这时这两条结果全部命中，可是后者显然不是我们想要的结果，这就需要多结果进行相似的排序。 PHP提供了一个函数可以轻易实现这个功能。它的名字就是&#8230;similar_text，如similar_text(&#8216;全国计算机等级考试&#8217;, &#8216;大学英语水平考试&#8217;, $percent), $percent返回两个字符串的相似度（百分比），测试后可知“计算机等级考试”更接近搜索词，所以把它排前面。]]></description>
			<content:encoded><![CDATA[<p><a class="highslide img_2" href="http://luinlee.com/wp-content/uploads/2010/07/Untitled-1.png" onclick="return hs.expand(this)"><img class="alignleft size-full wp-image-451" title="我爱凤姐" src="http://luinlee.com/wp-content/uploads/2010/07/Untitled-1.png" alt="" width="328" height="158" /></a>做小网站的站内搜索时要考虑到用户的查询关键字未必和数据库中相应数据准确对应，所以搜索时就要用到模糊查询。但是很多情况下Like语句也未必管用，比如用户搜索“打篮球”，可是数据库里只有“篮球”，这时做查询“LIKE &#8216;%打篮球%&#8217;”肯定不会找到结果。这时就需要对关键字进行分词。对于英文来说可以简单的根据空格来划分单词，而对于中文就有些麻烦。</p>
<p>好在现在有些开源的中文分词引擎分词的准确度已经相当高了，如PHP的<a href="http://www.ftphp.com/scws/">SCWS</a>可以把“打篮球”分成“打”和“篮球”，这样再分别查找就可以把“篮球”条目找到。然后由于SCWS是基于词频词典的，所以如果有些新词就分不出来了，比如“我爱美女”可以分解成“我爱”和“美女”，可是“我爱凤姐”就只能分成“我爱”“凤”和“姐”了。</p>
<p>在无法根据词库分词的情况下，我们可以采用二元分词法，即全部分成两个字组成的词，如“我爱凤姐”分成“我爱”“爱凤”“凤姐”。代码也很简单：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> Fenci<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$len</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'GBK'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #000088;">$len</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'GBK'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>然而由于词语的错分也产生了一些无关的结果，如搜索“全国计算机等级考试”，SCWS会把它分成“全国”“计算机”“等级”“考试”，而数据库里有“计算机等级考试”和“大学英语水平考试”，这时这两条结果全部命中，可是后者显然不是我们想要的结果，这就需要多结果进行相似的排序。<br />
PHP提供了一个函数可以轻易实现这个功能。它的名字就是&#8230;similar_text，如similar_text(&#8216;全国计算机等级考试&#8217;, &#8216;大学英语水平考试&#8217;, $percent), $percent返回两个字符串的相似度（百分比），测试后可知“计算机等级考试”更接近搜索词，所以把它排前面。</p>
]]></content:encoded>
			<wfw:commentRss>http://luinlee.com/448/%e6%9c%89%e5%85%b3%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e6%8a%80%e6%9c%af%e5%92%8c%e6%90%9c%e7%b4%a2%e7%bb%93%e6%9e%9c%e6%8c%89%e7%9b%b8%e4%bc%bc%e5%ba%a6%e6%8e%92%e5%ba%8f/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>140个字符能干啥，能写个程序你信不信？</title>
		<link>http://luinlee.com/333/program-written-in-140-chars/</link>
		<comments>http://luinlee.com/333/program-written-in-140-chars/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 15:08:59 +0000</pubDate>
		<dc:creator>Luin</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[闲言碎语]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://luinlee.com/?p=333</guid>
		<description><![CDATA[前几天在网上看到一个网站征集140字写成的小说，觉得挺有意思。获得第一名的Ron Gould(墙外)大牛写的小说着实“惊艳”(尽管我英语很烂，但为了显示我坚决备战四级的决心，还是决定把它译成中文)： “时间旅行成功了！”一张纸条上写着，“不过你只能回到过去并且不能再回来了。” 当我发觉这是我自己的笔迹时，不禁打了个寒颤。 “Time travel works!” the note read. “However you can only travel to the past and one-way.” I recognized my own handwriting and felt a chill. 其它作品访问：The Winners of the Twitter Writing Contest Are… 不过有没有想过用140个字符写一个程序？ “啊！这是不可能的！”在我看到某墙外网站的一篇文章前如果有人那样问我我一定会这样回答。 可惜，事实是： 第一个程序，但不是最好的： MINI TWITTER(啊不，是迷你饭否！啊不，是迷你叽歪&#8230;啊！管它呢&#8230;) 136 chars &#60;form&#62;&#60;input name=a&#62;&#60;input type=submit&#62;&#60;/form&#62; &#60;?if&#40;strlen&#40;$_GET&#91;a&#93;&#41;&#60;140&#41; &#123;$h=fopen&#40;a,“a”&#41;;fwrite&#40;$h,$_GET&#91;a&#93;.”&#60;hr&#62;”&#41;;&#125; echo@readfile&#40;a&#41;?&#62; 它可以实现微博最简单的功能：发表消息，显示消息以及做些必要验证。 第二个程序，已经开始出人意料了： RSS/RDF parser [...]]]></description>
			<content:encoded><![CDATA[<p>前几天在网上看到一个网站征集140字写成的小说，觉得挺有意思。获得第一名的<a href="http://twitter.com/rgouldtx/statuses/818253230">Ron Gould</a>(墙外)大牛写的小说着实“惊艳”(尽管我英语很烂，但为了显示我坚决备战四级的决心，还是决定把它译成中文)：</p>
<blockquote><p>
“时间旅行成功了！”一张纸条上写着，“不过你只能回到过去并且不能再回来了。” 当我发觉这是我自己的笔迹时，不禁打了个寒颤。<br />
“Time travel works!” the note read. “However you can only travel to the past and one-way.” I recognized my own handwriting and felt a chill.
</p></blockquote>
<p>其它作品访问：<a href="http://www.copyblogger.com/twitter-writing-contest-winners/">The Winners of the Twitter Writing Contest Are…</a></p>
<h3>不过有没有想过用140个字符写一个程序？</h3>
<p>“啊！这是不可能的！”在我看到某墙外网站的一篇文章前如果有人那样问我我一定会这样回答。<br />
可惜，事实是：</p>
<p>第一个程序，但不是最好的：<br />
<strong>MINI TWITTER</strong>(啊不，是迷你饭否！啊不，是迷你叽歪&#8230;啊！管它呢&#8230;) <em>136 chars</em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;form&gt;&lt;input name=a&gt;&lt;input type=submit&gt;&lt;/form&gt;
<span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">140</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$h</span><span style="color: #339933;">=</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span>“a”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>fwrite<span style="color: #009900;">&#40;</span><span style="color: #000088;">$h</span><span style="color: #339933;">,</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span>”<span style="color: #339933;">&lt;</span>hr<span style="color: #339933;">&gt;</span>”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span><span style="color: #339933;">@</span><span style="color: #990000;">readfile</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>它可以实现微博最简单的功能：发表消息，显示消息以及做些必要验证。</p>
<p>第二个程序，已经开始出人意料了：<br />
<strong>RSS/RDF parser with formatted output in 135 chars of PHP</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>p<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #b1b100;">as</span><span style="color: #000088;">$l</span><span style="color: #009900;">&#41;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span>’<span style="color: #339933;">/&lt;</span><span style="color: #009900;">&#40;</span>title<span style="color: #339933;">|</span>link<span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>^<span style="color: #339933;">&lt;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span>’<span style="color: #339933;">,</span><span style="color: #000088;">$l</span><span style="color: #339933;">,</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #b1b100;">echo</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span>‘<span style="color: #990000;">link</span>’?” <span style="color: #339933;">&lt;</span>a<span style="color: #339933;">&gt;</span>link<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;</span>hr<span style="color: #339933;">&gt;</span>”<span style="color: #339933;">:</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>RSS解析器，怎么样？看看Demo:<br />
<a href="http://test.neziric.org/140/feed-parser.php?u=http://feeds.delicious.com/v2/rss/?count=15">http://test.neziric.org/140/feed-parser.php?u=http://feeds.delicious.com/v2/rss/?count=15</a></p>
<p>第三个程序：<br />
<strong>JPG压缩器</strong>,可以对半压缩，不过它用了186个字符。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span>‘Content<span style="color: #339933;">-</span>type<span style="color: #339933;">:</span>’<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>list<span style="color: #009900;">&#40;</span><span style="color: #000088;">$w</span><span style="color: #339933;">,</span><span style="color: #000088;">$h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">=</span><span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$w</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #000088;">$h</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagecopyresized</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #339933;">,</span><span style="color: #990000;">imagecreatefromjpeg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$w</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #000088;">$h</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #000088;">$w</span><span style="color: #339933;">,</span><span style="color: #000088;">$h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>imagejpeg<span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>最后一个，我最喜欢的：<br />
<strong>A PHP web framework in 131 chars!!!!!</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> __DIR__<span style="color: #339933;">.</span>’<span style="color: #339933;">/</span>c<span style="color: #339933;">.</span>php’<span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_callable</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span>‘c’<span style="color: #009900;">&#93;</span> ?<span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> ‘Woah<span style="color: #339933;">!</span>’<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span>‘Error’<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>靠，它有一个默认控制器，访问不可用的控制器时会抛异常。不仅用了PHP5.3的新特性，而且效率相当高，这就是国外的PHP程序员(劝别真的在你的网站上用它&#8230;)。</p>
<p>P.S. 提到了国外，又想起了昨天的经历：三个寂寞男生(好吧，我不是很寂寞&#8230;)走在和谐的长安街上，迎面走来了一个可爱的武警叔叔，和蔼的盘问了我们好久，又问我们要了身份证登记。哦，明明都过去两天了！</p>
]]></content:encoded>
			<wfw:commentRss>http://luinlee.com/333/program-written-in-140-chars/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lua在游戏开发中的应用</title>
		<link>http://luinlee.com/412/lua-in-game/</link>
		<comments>http://luinlee.com/412/lua-in-game/#comments</comments>
		<pubDate>Tue, 18 May 2010 05:25:25 +0000</pubDate>
		<dc:creator>Luin</dc:creator>
				<category><![CDATA[C++ and C#]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[mstc]]></category>

		<guid isPermaLink="false">http://luinlee.com/?p=412</guid>
		<description><![CDATA[前些日子一直忙于开发BigTank项目(下载地址参见劣质设计网站：http://www.buaa-mstc.com，不支持IE)，总结了一些Lua在C#项目中的应用方法。 Lua 是一个小巧的脚本语言。作者是巴西人。该语言的设计目的是为了嵌入应用程序中，从而为应用程序提供灵活的扩展和定制功能。它的主页是 www.lua.org。 Lua脚本可以很容易的被C/C++代码调用，也可以反过来调用C/C++的函数，这使得Lua在应用程序中可以被广泛应用。不仅仅作为扩展脚本，也可以作为普通的配置文件，代替XML,Ini等文件格式，并且更容易理解和维护。 在C#中使用Lua也十分简单。 LuaInterface is a library for integration between the Lua language and Microsoft .NET platform&#8217;s Common Language Runtime (CLR). Lua scripts can use it to instantiate CLR objects, access properties, call methods, and even handle events with Lua functions. 从LuaInterface网站上可以下载到这个库。在你的项目中引用LuaInterface.dll后就可以开始了。 BigTank项目还没有确定是否要开源，所以我拿自己写的电子宠物程序演示一下（它也用了Lua，你可以在实验室页面找到它的全部源代码）。 C#: //... /// &#60;summary&#62; /// Lua虚拟机 /// &#60;/summary&#62; private [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="LuaLogo" src="http://www.lua.org/images/lua.gif" alt="" width="154" height="153" />前些日子一直忙于开发BigTank项目(下载地址参见劣质设计网站：<a href="http://www.buaa-mstc.com" target="_blank">http://www.buaa-mstc.com</a>，不支持IE)，总结了一些Lua在C#项目中的应用方法。</p>
<blockquote><p>Lua 是一个小巧的脚本语言。作者是巴西人。该语言的设计目的是为了嵌入应用程序中，从而为应用程序提供灵活的扩展和定制功能。它的主页是 www.lua.org。<br />
Lua脚本可以很容易的被C/C++代码调用，也可以反过来调用C/C++的函数，这使得Lua在应用程序中可以被广泛应用。不仅仅作为扩展脚本，也可以作为普通的配置文件，代替XML,Ini等文件格式，并且更容易理解和维护。</p></blockquote>
<p>在C#中使用Lua也十分简单。</p>
<blockquote><p>LuaInterface is a library for integration between the Lua language and Microsoft .NET platform&#8217;s Common Language Runtime (CLR). Lua scripts can use it to instantiate CLR objects, access properties, call methods, and even handle events with Lua functions.</p></blockquote>
<p>从<a href="http://luaforge.net/projects/luainterface/" target="_blank">LuaInterface网站</a>上可以下载到这个库。在你的项目中引用LuaInterface.dll后就可以开始了。</p>
<p>BigTank项目还没有确定是否要开源，所以我拿自己写的电子宠物程序演示一下（它也用了Lua，你可以在实验室页面找到它的全部源代码）。</p>
<p>C#:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//...</span>
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Lua虚拟机</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> Lua luaVM <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// 宠物的构造函数</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> Pet<span style="color: #000000;">&#40;</span>PetForm _petForm, <span style="color: #FF0000;">string</span> _petName, <span style="color: #FF0000;">string</span> _petPath<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    petState <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PetState<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    petForm <span style="color: #008000;">=</span> _petForm<span style="color: #008000;">;</span>
    petName <span style="color: #008000;">=</span> _petName<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">//构造Lua虚拟机以解析宠物AI</span>
    luaVM <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Lua<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//注册提供给宠物AI的API函数</span>
    Type tThis <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    luaVM.<span style="color: #0000FF;">RegisterFunction</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;PetDo&quot;</span>, <span style="color: #0600FF;">this</span>, tThis.<span style="color: #0000FF;">GetMethod</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;LuaPetDo&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    luaVM.<span style="color: #0000FF;">RegisterFunction</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;PetDoFrame&quot;</span>, <span style="color: #0600FF;">this</span>, tThis.<span style="color: #0000FF;">GetMethod</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;LuaPetDoFrame&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    luaVM.<span style="color: #0000FF;">RegisterFunction</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Sleep&quot;</span>, <span style="color: #0600FF;">this</span>, tThis.<span style="color: #0000FF;">GetMethod</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;LuaSleep&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//载入AI文件</span>
    luaVM.<span style="color: #0000FF;">DoFile</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">AppDomain</span>.<span style="color: #0000FF;">CurrentDomain</span>.<span style="color: #0000FF;">BaseDirectory</span> <span style="color: #008000;">+</span> _petPath <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>ai.lua&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>其中RegisterFunction作用是注册C#代码中的一个public(最新版本的LuaInterface支持private)函数来供Lua脚本使用，其中无需关心参数的个数以及类型。</p>
<p>Lua:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">PetDo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Sleep&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>执行DoFile后会调用Lua脚本，后者则调用C#中的PetDo函数完成指定动作。</p>
]]></content:encoded>
			<wfw:commentRss>http://luinlee.com/412/lua-in-game/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
