Example #1
0
        private WherigoObject CreateWherigoObjectCore(string classname, Type typeToCompare, object[] arguments)
        {
            // Gets the table for the class to get.
            LuaTable classLt = _luaState.SafeGetGlobal <LuaTable>("Wherigo." + classname);

            // Gets the newInstance function of the class.
            string      newInstanceFuncName = "Wherigo.newInstance";
            LuaFunction newInstanceLf       = _luaState.SafeGetGlobal <LuaFunction>(newInstanceFuncName);

            // Checks that the function is valid.
            if (newInstanceLf == null)
            {
                throw new InvalidOperationException("No " + newInstanceFuncName + " function could be found.");
            }

            // Conforms the arguments.
            List <LuaValue> conformedArguments = new List <LuaValue>();

            conformedArguments.Add(classLt); // Self comes first.
            if (IsWherigoClassnameZObject(classname))
            {
                // The ZObject constructor requires the current cartridge object as only parameter.
                // Therefore, let's discard the arguments and only supply the cartridge table.
                conformedArguments.Add(GetNativeContainerCore(_helper.Cartridge.DataContainer));
            }
            else
            {
                // Non-ZObject constructors take their parameters in a row.
                // All supplied arguments are wrapped.
                arguments = arguments ?? new object[] { };
                conformedArguments.AddRange(arguments.Select(o => GetNativeValueFromValue(o)));
            }

            // Calls the function to create a new instance.
            LuaTable wlt;

            try
            {
                IList <object> ret = _luaState.SafeCallRaw(newInstanceLf, conformedArguments.ToArray());
                wlt = ret.FirstOrDefault() as LuaTable;
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("An exception occured while constructing an instance of " + classname, e);
            }

            // Checks if the object is valid.
            if (wlt == null)
            {
                throw new InvalidOperationException("An object of class " + classname + " could not be constructed.");
            }

            // Returns the wrapped wherigo object.
            //System.Diagnostics.Debug.WriteLine("LuaDataFactory: Creating an instance of " + classname);
            return(GetWherigoObjectCore(wlt, typeToCompare: typeToCompare));
        }
Example #2
0
        private IList <object> ExecuteCore(params object[] args)
        {
            // Conforms the parameters: wrappers from the Data layer
            // are converted to their native equivalents.
            // The self table is added if it is specified.
            List <LuaValue> parameters = new List <LuaValue>();

            if (_selfTable != null)
            {
                parameters.Add(_selfTable);
            }
            foreach (var item in args)
            {
                parameters.Add(_dataFactory.GetNativeValueFromValue(item));
            }

            // Runs the provider and gets the list of objects.
            IList <object> retValues;

            try
            {
                retValues = _luaState.SafeCallRaw(_luaFunction, parameters.ToArray());
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("An exception occured while executing the provider.", e);
            }

            // Converts the different values to types from the data layer.
            List <object> ret = new List <object>();

            foreach (var item in retValues)
            {
                if (item is LuaValue)
                {
                    ret.Add(_dataFactory.GetValueFromNativeValue((LuaValue)item));
                }
                else
                {
                    ret.Add(item);
                }
            }

            // Returns the list.
            return(ret);
        }