C#访问lua自定义loader——以及用luac将lua从明文转成字节码

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using XLua;
using System;
using System.IO;

namespace Tutorial
{
    public class CSCallLua : MonoBehaviour
    {
        LuaEnv luaenv = null;
        public class DClass
        {
            public int f1;
            public int f2;
        }

        [CSharpCallLua]
        public delegate int FDelegate(int a, string b, out DClass c, ref int x, ref string y);

        void Start()
        {
            luaenv = new LuaEnv();
            //TextAsset luaScript = Resources.Load<TextAsset>("hello");
            //luaenv.DoString(luaScript.text);
            luaenv.AddLoader(SelfDefineLoader);
            luaenv.DoString("require 'hello'"); //使用requre 'xxxx'的方式,会触发自定义的loader
            FDelegate f = luaenv.Global.Get<FDelegate>("f");
            DClass d_ret;
            int x = 0;
            string y = string.Empty;
            int f_ret = f(100, "John", out d_ret, ref x, ref y);
        }

        /// <summary>
        /// 自定义的路径的加载器
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public byte[] SelfDefineLoader(ref string filepath)
        {
            filepath = Application.dataPath + "/XLua/Tutorial/CSharpCallLua/Resources/" + filepath + ".txt";
            if (File.Exists(filepath))
            {
                return File.ReadAllBytes(filepath);
            }
            else
            {
                return null;
            }
        }

        void OnDestroy()
        {
            luaenv.Dispose();
        }
    }
}

lua文件:

function f(a, b)
	print('a', a, 'b', b)
	return 1, {f1 = 1024}, 999, "hello,world"
end

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
补充1:
更新于2022.2.23
这里的自定loader方式,一定要注意lua源文件的编码个是,有时候,会出现这样的代码:

private byte[] CustomLoader(ref string filename)
{
	string path = string.Format("StreamingAssets/lua/{0}.lua.txt", filename);
	var text = File.ReadAllText(path, System.Text.Encoding.UTF8);
	return System.Text.Encoding.UTF8.GetBytes(text);
}

这里在lua都是明文的情况下,是没有问题的。
但是,如果使用luac,将明文转成了字节码的话。
如果还是使用这个自定义的加载器的话,会出现
请添加图片描述
在这里插入图片描述
经过断点,发现读取内容然后再转码,则会出现上面的报错。
所以要改加载器:

    private byte[] CustomLoader(ref string filename)
    {
		string path = string.Format("StreamingAssets/lua/{0}.lua.txt", filename);
        //var text = File.ReadAllText(path, System.Text.Encoding.UTF8);
        //return System.Text.Encoding.UTF8.GetBytes(text);
        return Zeus.Core.FileSystem.VFileSystem.ReadAllBytes(path);
    }

同事保证,明文的lua文件,要以utf8个的编码格式,这样就不会出错了。

当然,这里的luac,是使用xlua自己编出来的luac,如果5.3.5的lua代码是加固后的,那么luac也要在mac上进下编译之后,在进行转码的操作,这其中一个环节不对,lua执行就会出现错误。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值