Пример #1
0
        public static byte[] Compile(string luaCode, string fileName = "Cartridge")
        {
            // Create Lua runtime
            LuaRuntime luaState = new Eluant.LuaRuntime();

            // Load Wherigo.lua
            var wigInternal = new WIGInternalImpl(luaState);

            // Get string.dump() function
            LuaTable    stringTable = (LuaTable)luaState.Globals["string"];
            LuaFunction stringDump  = (LuaFunction)stringTable["dump"];

            // Compile Lua code
            LuaFunction lf = luaState.CompileString(Encoding.UTF8.GetBytes(luaCode), fileName);

            // Retrive Lua code by string.dump
            var ret = stringDump.Call(new List <LuaValue>()
            {
                lf
            });

            if (ret.Count >= 1)
            {
                string byteArray = (LuaString)ret[0];
                byte[] result    = new byte[byteArray.Length];
                for (int i = 0; i < byteArray.Length; i++)
                {
                    result[i] = (byte)(byteArray[i] & 0xFF);
                }
                return(result);
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
		/// <summary>
		/// Extract cartridge data from the given specified luaCode in given string.
		/// </summary>
		/// <param name="luaCode">String with Lua code.</param>
		public static Cartridge Extract(string luaCode)
		{
			LuaTable ltMedia = null;
			LuaTable ltIcon = null;

			// Create Lua runtime
			LuaRuntime luaState = new Eluant.LuaRuntime ();

			// Load Wherigo.lua
			var wigInternal = new WIGInternalImpl(luaState);

			// Run Lua
			var cart = (LuaTable)((LuaVararg)luaState.DoString(luaCode, "Cartridge"))[0];

			if (cart == null)
				throw new ArgumentNullException("ZCartridge");

			// Get table for all ZObjects
			var objs = (LuaTable)cart["AllZObjects"];

			if (objs == null)
				throw new ArgumentException("AllZObjects");

			// Get all relevant data for cartridge
			Cartridge result = new Cartridge();

			// Now we check the Lua code for old newlines like <BR>\n or long strings with only a \n as content
			// All this should replaced by normal \n newlines
			result.LuaCode = ConvertToPlain(luaCode);

			// Extract variable for cartridge
			var regex = new Regex(@"return\s*([_0-9a-zA-Z]*)\s*$", RegexOptions.Singleline & RegexOptions.IgnoreCase);
			var match = regex.Match(luaCode);

			if (match.Success)
				result.Variable = match.Groups[1].Value;

			result.Id = (LuaString)cart["Id"];
			result.Name = cart["Name"].ToString();
			result.Description = cart["Description"].ToString();
			result.Activity = cart["Activity"].ToString();
			result.StartingLocationDescription = cart["StartingLocationDescription"].ToString();
			LuaTable zp = cart["StartingLocation"] is LuaBoolean ? null : (LuaTable)cart["StartingLocation"];
			result.Latitude = zp == null ? 0 : (double)((LuaNumber)zp["latitude"]).ToNumber();
			result.Longitude = zp == null ? 0 : (double)((LuaNumber)zp["longitude"]).ToNumber();
			result.Altitude = zp == null ? 0 : (cart["StartingLocation"] is LuaBoolean ? 0 : (double)((LuaNumber)((LuaTable)zp["altitude"])["value"]).ToNumber());
			result.Version = cart["Version"].ToString();
			result.Company = cart["Company"].ToString();
			result.Author = cart["Author"].ToString();
			result.BuilderVersion = cart["BuilderVersion"].ToString();
			result.CreateDate = cart["CreateDate"].ToString();
			result.PublishDate = cart["PublishDate"].ToString();
			result.UpdateDate = cart["UpdateDate"].ToString();
			result.LastPlayedDate = cart["LastPlayedDate"].ToString();
			result.TargetDevice = cart["TargetDevice"].ToString();
			result.TargetDeviceVersion = cart["TargetDeviceVersion"].ToString();
			result.StateId = cart["StateId"].ToString();
			result.CountryId = cart["CountryId"].ToString();
			result.Visible = ((LuaBoolean)cart["Visible"]).ToBoolean();
			result.Complete = ((LuaBoolean)cart["Complete"]).ToBoolean();
			result.UseLogging = ((LuaBoolean)cart["UseLogging"]).ToBoolean();
			ltMedia = (cart["Media"] is LuaBoolean || cart["Media"] is LuaNil) ? null : (LuaTable)cart["Media"];
			ltIcon = (cart["Icon"] is LuaBoolean || cart["Icon"] is LuaNil) ? null : (LuaTable)cart["Icon"];

			// Check for medias of cartridge
			var table = (LuaTable)luaState.Globals["table"];
			var contains = (LuaFunction)table["Contains"];

			var p = contains.Call(cart["AllZObjects"], ltMedia);
			if (p[0] is LuaBoolean && p[0].ToBoolean())
				result.Poster = p[1] is LuaNumber ? (int)((LuaNumber)p[1]) : -1;
			else
				result.Poster = -1;

			var i = contains.Call(cart["AllZObjects"], ltIcon);
			if (i[0] is LuaBoolean && i[0].ToBoolean())
				result.Icon = i[1] is LuaNumber ? (int)((LuaNumber)i[1]) : -1;
			else
				result.Icon = -1;

			// Get all other medias
			foreach(KeyValuePair<LuaValue, LuaValue> pair in objs) {
				int idx = (int)(LuaNumber)pair.Key;
				LuaTable obj = ((LuaTable)pair.Value);
				// Get type of ZObject
				string className = (LuaString)obj["ClassName"];
				string name = (LuaString)obj["Name"];
				// Check type of ZObject
				if (className.Equals("ZMedia")) {
					Media media = ExtractMedia (obj);
					result.Medias.Add (media);
				}
			}

			// Get all variable names
			foreach(KeyValuePair<LuaValue, LuaValue> pair in luaState.Globals) {
				var k = pair.Key;
				var v = pair.Value;
				if (v is LuaTable) {
					var className = ((LuaTable)v)["ClassName"];
					if (className != null && className.ToString().Equals("Zone"))
						result.Zones.Add(k.ToString());
					if (className != null && className.ToString().Equals("ZItem"))
						result.Items.Add(k.ToString());
					if (className != null && className.ToString().Equals("ZCharacter")) {
						if (!k.ToString().Equals("Player")) 
							result.Characters.Add(k.ToString());
					}
					if (className != null && className.ToString().Equals("ZTimer"))
						result.Timers.Add(k.ToString());
					if (className != null && className.ToString().Equals("ZInput"))
						result.Inputs.Add(k.ToString());
					if (className != null && className.ToString().Equals("ZCartridge"))
						result.Variable = k.ToString();
				}
			}

			return result;
		}
Пример #3
0
		public static byte[] Compile(string luaCode, string fileName = "Cartridge")
		{
			// Create Lua runtime
			LuaRuntime luaState = new Eluant.LuaRuntime ();

			// Load Wherigo.lua
			var wigInternal = new WIGInternalImpl(luaState);

			// Get string.dump() function
			LuaTable stringTable = (LuaTable)luaState.Globals["string"];
			LuaFunction stringDump = (LuaFunction)stringTable["dump"];

			// Compile Lua code
			LuaFunction lf = luaState.CompileString(Encoding.UTF8.GetBytes(luaCode), fileName);

			// Retrive Lua code by string.dump
			var ret = stringDump.Call(new List<LuaValue>() {lf});

			if (ret.Count >= 1) {
				string byteArray = (LuaString)ret[0];
				byte[] result = new byte[byteArray.Length];
				for (int i = 0; i < byteArray.Length; i++)
					result[i] = (byte)(byteArray[i] & 0xFF);
				return result;
			} else {
				return null;
			}
		}
Пример #4
0
        /// <summary>
        /// Extract cartridge data from the given specified luaCode in given string.
        /// </summary>
        /// <param name="luaCode">String with Lua code.</param>
        public static Cartridge Extract(string luaCode)
        {
            LuaTable ltMedia = null;
            LuaTable ltIcon  = null;

            // Create Lua runtime
            LuaRuntime luaState = new Eluant.LuaRuntime();

            // Load Wherigo.lua
            var wigInternal = new WIGInternalImpl(luaState);

            // Run Lua
            var cart = (LuaTable)((LuaVararg)luaState.DoString(luaCode, "Cartridge"))[0];

            if (cart == null)
            {
                throw new ArgumentNullException("ZCartridge");
            }

            // Get table for all ZObjects
            var objs = (LuaTable)cart["AllZObjects"];

            if (objs == null)
            {
                throw new ArgumentException("AllZObjects");
            }

            // Get all relevant data for cartridge
            Cartridge result = new Cartridge();

            // Now we check the Lua code for old newlines like <BR>\n or long strings with only a \n as content
            // All this should replaced by normal \n newlines
            result.LuaCode = ConvertToPlain(luaCode);

            // Extract variable for cartridge
            var regex = new Regex(@"return\s*([_0-9a-zA-Z]*)\s*$", RegexOptions.Singleline & RegexOptions.IgnoreCase);
            var match = regex.Match(luaCode);

            if (match.Success)
            {
                result.Variable = match.Groups[1].Value;
            }

            result.Id          = (LuaString)cart["Id"];
            result.Name        = cart["Name"].ToString();
            result.Description = cart["Description"].ToString();
            result.Activity    = cart["Activity"].ToString();
            result.StartingLocationDescription = cart["StartingLocationDescription"].ToString();
            LuaTable zp = cart["StartingLocation"] is LuaBoolean ? null : (LuaTable)cart["StartingLocation"];

            result.Latitude            = zp == null ? 0 : (double)((LuaNumber)zp["latitude"]).ToNumber();
            result.Longitude           = zp == null ? 0 : (double)((LuaNumber)zp["longitude"]).ToNumber();
            result.Altitude            = zp == null ? 0 : (cart["StartingLocation"] is LuaBoolean ? 0 : (double)((LuaNumber)((LuaTable)zp["altitude"])["value"]).ToNumber());
            result.Version             = cart["Version"].ToString();
            result.Company             = cart["Company"].ToString();
            result.Author              = cart["Author"].ToString();
            result.BuilderVersion      = cart["BuilderVersion"].ToString();
            result.CreateDate          = cart["CreateDate"].ToString();
            result.PublishDate         = cart["PublishDate"].ToString();
            result.UpdateDate          = cart["UpdateDate"].ToString();
            result.LastPlayedDate      = cart["LastPlayedDate"].ToString();
            result.TargetDevice        = cart["TargetDevice"].ToString();
            result.TargetDeviceVersion = cart["TargetDeviceVersion"].ToString();
            result.StateId             = cart["StateId"].ToString();
            result.CountryId           = cart["CountryId"].ToString();
            result.Visible             = ((LuaBoolean)cart["Visible"]).ToBoolean();
            result.Complete            = ((LuaBoolean)cart["Complete"]).ToBoolean();
            result.UseLogging          = ((LuaBoolean)cart["UseLogging"]).ToBoolean();
            ltMedia = (cart["Media"] is LuaBoolean || cart["Media"] is LuaNil) ? null : (LuaTable)cart["Media"];
            ltIcon  = (cart["Icon"] is LuaBoolean || cart["Icon"] is LuaNil) ? null : (LuaTable)cart["Icon"];

            // Check for medias of cartridge
            var table    = (LuaTable)luaState.Globals["table"];
            var contains = (LuaFunction)table["Contains"];

            var p = contains.Call(cart["AllZObjects"], ltMedia);

            if (p[0] is LuaBoolean && p[0].ToBoolean())
            {
                result.Poster = p[1] is LuaNumber ? (int)((LuaNumber)p[1]) : -1;
            }
            else
            {
                result.Poster = -1;
            }

            var i = contains.Call(cart["AllZObjects"], ltIcon);

            if (i[0] is LuaBoolean && i[0].ToBoolean())
            {
                result.Icon = i[1] is LuaNumber ? (int)((LuaNumber)i[1]) : -1;
            }
            else
            {
                result.Icon = -1;
            }

            // Get all other medias
            foreach (KeyValuePair <LuaValue, LuaValue> pair in objs)
            {
                int      idx = (int)(LuaNumber)pair.Key;
                LuaTable obj = ((LuaTable)pair.Value);
                // Get type of ZObject
                string className = (LuaString)obj["ClassName"];
                string name      = (LuaString)obj["Name"];
                // Check type of ZObject
                if (className.Equals("ZMedia"))
                {
                    Media media = ExtractMedia(obj);
                    result.Medias.Add(media);
                }
            }

            // Get all variable names
            foreach (KeyValuePair <LuaValue, LuaValue> pair in luaState.Globals)
            {
                var k = pair.Key;
                var v = pair.Value;
                if (v is LuaTable)
                {
                    var className = ((LuaTable)v)["ClassName"];
                    if (className != null && className.ToString().Equals("Zone"))
                    {
                        result.Zones.Add(k.ToString());
                    }
                    if (className != null && className.ToString().Equals("ZItem"))
                    {
                        result.Items.Add(k.ToString());
                    }
                    if (className != null && className.ToString().Equals("ZCharacter"))
                    {
                        if (!k.ToString().Equals("Player"))
                        {
                            result.Characters.Add(k.ToString());
                        }
                    }
                    if (className != null && className.ToString().Equals("ZTimer"))
                    {
                        result.Timers.Add(k.ToString());
                    }
                    if (className != null && className.ToString().Equals("ZInput"))
                    {
                        result.Inputs.Add(k.ToString());
                    }
                    if (className != null && className.ToString().Equals("ZCartridge"))
                    {
                        result.Variable = k.ToString();
                    }
                }
            }

            return(result);
        }