Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="status"></param>
        /// <param name="message"></param>
        /// <param name="freObject"></param>
        /// <exception cref="FreActionscriptErrorException"></exception>
        /// <exception cref="NoSuchNameException"></exception>
        /// <exception cref="FreInvalidObjectException"></exception>
        /// <exception cref="FreTypeMismatchException"></exception>
        /// <exception cref="FreInvalidArgumentException"></exception>
        /// <exception cref="FreReadOnlyException"></exception>
        /// <exception cref="FreWrongThreadException"></exception>
        /// <exception cref="FreIllegalStateException"></exception>
        /// <exception cref="FreInsufficientMemoryException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static void ThrowFreException(FreResultSharp status, string message, FREObject freObject)
        {
            if (FreObjectTypeSharp.Class == freObject.Type())
            {
                try {
                    var hasStackTrace = GetAsBool(freObject.Call("hasOwnProperty", "getStackTrace"));

                    if (hasStackTrace)
                    {
                        var asStackTrace = freObject.Call("getStackTrace");
                        if (FreObjectTypeSharp.String == asStackTrace.Type())
                        {
                            message = GetAsString(asStackTrace);
                        }
                    }
                }
                catch (Exception) {
                    //ignored
                }
            }

            switch (status)
            {
            case FreResultSharp.FreActionscriptError:
                throw new FreActionscriptErrorException(message);

            case FreResultSharp.NoSuchName:
                throw new NoSuchNameException(message);

            case FreResultSharp.FreInvalidObject:
                throw new FreInvalidObjectException(message);

            case FreResultSharp.FreTypeMismatch:
                throw new FreTypeMismatchException(message);

            case FreResultSharp.FreInvalidArgument:
                throw new FreInvalidArgumentException(message);

            case FreResultSharp.FreReadOnly:
                throw new FreReadOnlyException(message);

            case FreResultSharp.FreWrongThread:
                throw new FreWrongThreadException(message);

            case FreResultSharp.FreIllegalState:
                throw new FreIllegalStateException(message);

            case FreResultSharp.FreInsufficientMemory:
                throw new FreInsufficientMemoryException(message);

            case FreResultSharp.Ok:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(status), status, null);
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rawValue"></param>
        /// <returns></returns>
        public static Dictionary <string, object> GetAsDictionary(FREObject rawValue)
        {
            var ret         = new Dictionary <string, object>();
            var aneUtils    = new FREObject().Init("com.tuarua.fre.ANEUtils", null);
            var paramsArray = new ArrayList {
                new FreObjectSharp(rawValue)
            };
            var classProps = aneUtils.Call("getClassProps", paramsArray);

            if (classProps == null)
            {
                return(ret);
            }

            var arrayLength = classProps.Length;

            for (uint i = 0; i < arrayLength; i++)
            {
                var elem       = classProps.At(i);
                var propNameAs = elem.GetProp("name");
                var propName   = GetAsString(propNameAs);

                var propVal = GetProperty(rawValue, propName);
                ret.Add(propName, GetAsObject(propVal));
            }

            return(ret);
        }
Example #3
0
    /// <summary>
    /// 把指定索引的值强制转成字符串
    /// </summary>
    public static string ToLuaString(this ILuaState self, int index)
    {
        LuaTypes luaT = self.Type(index);

        switch (luaT)
        {
        case LuaTypes.LUA_TSTRING:
            return(self.ToString(index));

        case LuaTypes.LUA_TNIL:
            return(string.Empty);

        case LuaTypes.LUA_TNUMBER:
            return(self.ToNumber(index).ToString());

        case LuaTypes.LUA_TBOOLEAN:
            return(self.ToBoolean(index).ToString());

        case LuaTypes.LUA_TNONE:
            return(null);

        default:
            self.GetGlobal("tostring");
            self.PushValue(index);
            self.Call(1, 1);
            var ret = self.ToString(-1);
            self.Pop(1);
            return(ret);
        }
    }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rawValue"></param>
        /// <returns></returns>
        public static FreObjectTypeSharp GetActionscriptType(FREObject rawValue)
        {
            var aneUtils  = new FREObject().Init("com.tuarua.fre.ANEUtils", null);
            var classType = aneUtils.Call("getClassType", rawValue);
            var type      = GetAsString(classType).ToLower();

            switch (type)
            {
            case "object":
                return(FreObjectTypeSharp.Object);

            case "int":
                return(FreObjectTypeSharp.Int);

            case "number":
                return(FreObjectTypeSharp.Number);

            case "boolean":
                return(FreObjectTypeSharp.Boolean);

            case "flash.geom::rectangle":
                return(FreObjectTypeSharp.Rectangle);

            case "flash.geom::point":
                return(FreObjectTypeSharp.Point);

            case "date":
                return(FreObjectTypeSharp.Date);

            default:
                return(FreObjectTypeSharp.Class);
            }
        }
Example #5
0
    public static int __newindex(ILuaState L)
    {
        // 从元表取出field的值
        string field = L.ToString(2);

        L.GetMetaTable(1);
        L.PushString(field);
        L.RawGet(-2);
        L.Remove(-2);

        var luaT = L.Type(-1);

        if (luaT == LuaTypes.LUA_TTABLE)
        {
            // 该field只能是个属性
            L.RawGetI(-1, 2);
            L.PushValue(1);
            L.Call(1, 1);
            L.Remove(-2);
        }
        else
        {
            L.L_Error(string.Format("field or property {0} does not exist, get {1}", field, luaT));
            L.Pop(1);
            return(0);
        }

        return(1);
    }
Example #6
0
 /// <summary>
 /// Calls toString() on a FREObject
 /// </summary>
 /// <param name="freObject"></param>
 /// <returns></returns>
 public static string toString(this FREObject freObject)
 {
     if (freObject.Type() == FreObjectTypeSharp.String ||
         freObject.Type() == FreObjectTypeSharp.Null)
     {
         return("");
     }
     return(freObject.Call("toString").AsString());
 }
Example #7
0
    private static IEnumerator LuaCoroutine(ILuaState lua, LuaFunction luaRef, object yieldRet)
    {
        lua.GetGlobal("coroutine", "status");
        var coro_status = lua.L_Ref(LuaIndexes.LUA_REGISTRYINDEX);

        lua.GetGlobal("coroutine", "resume");
        var corou_resume = lua.L_Ref(LuaIndexes.LUA_REGISTRYINDEX);

        for (bool coroRet = true; ;)
        {
            yield return(yieldRet);

            var oldTop = lua.GetTop();

            // 检查协程状态
            lua.GetRef(coro_status);
            luaRef.push(lua);
            lua.Call(1, 1);
            var coStat = lua.ToString(-1);
            lua.Pop(1);
            if (coStat == "dead")
            {
                break;
            }

            // 再启动协程
            lua.GetRef(corou_resume);
            luaRef.push(lua);
            var status = lua.PCall(1, 2, 0);
            if (status != LuaThreadStatus.LUA_OK)
            {
                LogMgr.E(lua.ToString(-1));
                lua.SetTop(oldTop);
                break;
            }
            coroRet  = lua.ToBoolean(-2);
            yieldRet = lua.ToAnyObject(-1);
            // 弹出返回值
            lua.Pop(2);
            if (!coroRet)
            {
                LogMgr.E("{0}", yieldRet);
                break;
            }
        }

        lua.L_Unref(LuaIndexes.LUA_REGISTRYINDEX, coro_status);
        lua.L_Unref(LuaIndexes.LUA_REGISTRYINDEX, corou_resume);
        luaRef.Dispose();
    }
Example #8
0
 /// <summary>
 /// Traces the message to the console.
 /// </summary>
 /// <param name="message">Message to log.</param>
 /// <param name="type">Type of error</param>
 /// <param name="freException">The Exception</param>
 public void Error(string message, FreResultSharp type, FREObject freException)
 {
     Context?.DispatchEvent("TRACE", $"[FreSharp] ‼ {type} {message}");
     if (FreObjectTypeSharp.Class != freException.Type())
     {
         return;
     }
     try {
         if (!freException.hasOwnProperty("getStackTrace"))
         {
             return;
         }
         var asStackTrace = freException.Call("getStackTrace");
         if (FreObjectTypeSharp.String == asStackTrace.Type())
         {
             Context?.DispatchEvent("TRACE", $"[FreSharp] ‼ {FreSharpHelper.GetAsString(asStackTrace)}");
         }
     }
     catch (Exception) {
         //ignored
     }
 }
Example #9
0
 /// <summary>
 /// Indicates whether an object has a specified property defined.
 /// </summary>
 /// <param name="freObject"></param>
 /// <param name="name">The property of the FREObject. </param>
 /// <returns></returns>
 public static bool hasOwnProperty(this FREObject freObject, string name) =>
 freObject.Call("hasOwnProperty", name).AsBool();