Beispiel #1
0
            void insert(ILuaTable table, ILuaValue pos, ILuaValue value = null)
            {
                CheckNotNull("table.insert", table);
                CheckNotNull("table.insert", pos);
                double i;
                double len = table.Length().AsDouble() ?? 0;

                if (value == null)
                {
                    value = pos;
                    i     = len + 1;
                }
                else
                {
                    i = pos.AsDouble() ?? 0;
                }

                if (i > len + 1 || i < 1 || i % 1 != 0)
                {
                    throw new ArgumentException(
                              "Position given to function 'table.insert' is outside valid range.");
                }

                for (double d = len; d >= i; d--)
                {
                    var temp = table.GetItemRaw(E.Runtime.CreateValue(d));
                    table.SetItemRaw(E.Runtime.CreateValue(d + 1), temp);
                }
                table.SetItemRaw(pos, value);
            }
Beispiel #2
0
            ILuaValue pack(params ILuaValue[] args)
            {
                ILuaTable ret = E.Runtime.CreateTable();

                for (int i = 0; i < args.Length; i++)
                {
                    ret.SetItemRaw(E.Runtime.CreateValue(i + 1), args[i]);
                }
                ret.SetItemRaw(E.Runtime.CreateValue("n"), E.Runtime.CreateValue(args.Length));
                return(ret);
            }
Beispiel #3
0
            void sort(ILuaTable table, ILuaValue comp = null)
            {
                CheckNotNull("table.sort", table);
                var comparer = new SortComparer(E, comp);

                ILuaValue[] elems = unpack(table).OrderBy(k => k, comparer).ToArray();
                for (int i = 0; i < elems.Length; i++)
                {
                    ILuaValue ind = E.Runtime.CreateValue(i + 1);
                    table.SetItemRaw(ind, elems[i]);
                }
            }
Beispiel #4
0
            ILuaValue remove(ILuaTable table, int?pos = null)
            {
                CheckNotNull("table.remove", table);

                double len = table.Length().AsDouble() ?? 0;

                pos = pos ?? (int)len;
                if (pos > len + 1 || pos < 1)
                {
                    throw new ArgumentException(
                              "Position given to function 'table.remove' is outside valid range.");
                }

                ILuaValue prev = LuaNil.Nil;

                for (double d = len; d >= pos; d--)
                {
                    ILuaValue ind  = E.Runtime.CreateValue(d);
                    ILuaValue temp = table.GetItemRaw(ind);
                    table.SetItemRaw(ind, prev);
                    prev = temp;
                }
                return(prev);
            }
Beispiel #5
0
            object date(string format = "%c", object source = null)
            {
                DateTime time;

                if (source is double d)
                {
                    time = new DateTime((long)d);
                }
                else if (source is DateTime dt)
                {
                    time = dt;
                }
                else
                {
                    time = DateTime.Now;
                }

                if (format.StartsWith("!"))
                {
                    format = format.Substring(1);
                    time   = time.ToUniversalTime();
                }

                if (format == "*t")
                {
                    ILuaTable            table = _env.Runtime.CreateTable();
                    Action <string, int> set   = (a, b) => {
                        table.SetItemRaw(_env.Runtime.CreateValue(a), _env.Runtime.CreateValue(b));
                    };
                    set("year", time.Year);
                    set("month", time.Month);
                    set("day", time.Day);
                    set("hour", time.Hour);
                    set("min", time.Minute);
                    set("sec", time.Second);
                    set("wday", ((int)time.DayOfWeek) + 1);
                    set("yday", time.DayOfYear);

                    return(table);
                }

                StringBuilder ret = new StringBuilder();

                for (int i = 0; i < format.Length; i++)
                {
                    if (format[i] == '%')
                    {
                        i++;
                        switch (format[i])
                        {
                        case 'a':
                            ret.Append(time.ToString("ddd", CultureInfo.CurrentCulture));
                            break;

                        case 'A':
                            ret.Append(time.ToString("dddd", CultureInfo.CurrentCulture));
                            break;

                        case 'b':
                            ret.Append(time.ToString("MMM", CultureInfo.CurrentCulture));
                            break;

                        case 'B':
                            ret.Append(time.ToString("MMMM", CultureInfo.CurrentCulture));
                            break;

                        case 'c':
                            ret.Append(time.ToString("F", CultureInfo.CurrentCulture));
                            break;

                        case 'd':
                            ret.Append(time.ToString("dd", CultureInfo.CurrentCulture));
                            break;

                        case 'H':
                            ret.Append(time.ToString("HH", CultureInfo.CurrentCulture));
                            break;

                        case 'I':
                            ret.Append(time.ToString("hh", CultureInfo.CurrentCulture));
                            break;

                        case 'j':
                            ret.Append(time.DayOfYear.ToString("d3", CultureInfo.CurrentCulture));
                            break;

                        case 'm':
                            ret.Append(time.Month.ToString("d2", CultureInfo.CurrentCulture));
                            break;

                        case 'M':
                            ret.Append(time.Minute.ToString("d2", CultureInfo.CurrentCulture));
                            break;

                        case 'p':
                            ret.Append(time.ToString("tt", CultureInfo.CurrentCulture));
                            break;

                        case 'S':
                            ret.Append(time.ToString("ss", CultureInfo.CurrentCulture));
                            break;

                        case 'U': {
                            // See strftime
                            DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
                            ret.AppendFormat(
                                "{0:02}",
                                dfi.Calendar.GetWeekOfYear(
                                    time, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday));
                            break;
                        }

                        case 'V': {
                            // See strftime
                            DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
                            ret.AppendFormat(
                                "{0:02}", dfi.Calendar.GetWeekOfYear(
                                    time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday));
                            break;
                        }

                        case 'w':
                            ret.Append((int)time.DayOfWeek);
                            break;

                        case 'W': {
                            // See strftime
                            DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
                            ret.AppendFormat(
                                "{0:02}",
                                dfi.Calendar.GetWeekOfYear(
                                    time, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday));
                            break;
                        }

                        case 'x':
                            ret.Append(time.ToString("d", CultureInfo.CurrentCulture));
                            break;

                        case 'X':
                            ret.Append(time.ToString("T", CultureInfo.CurrentCulture));
                            break;

                        case 'y':
                            ret.Append(time.ToString("yy", CultureInfo.CurrentCulture));
                            break;

                        case 'Y':
                            ret.Append(time.ToString("yyyy", CultureInfo.CurrentCulture));
                            break;

                        case 'Z':
                            ret.Append(time.ToString("%K", CultureInfo.CurrentCulture));
                            break;

                        case '%':
                            ret.Append('%');
                            break;

                        default:
                            throw new ArgumentException(
                                      $"Unrecognized format specifier %{format[i]} in function 'os.date'.");
                        }
                    }
                    else
                    {
                        ret.Append(format[i]);
                    }
                }
                return(ret.ToString());
            }
 static ILuaValue rawset(ILuaTable table, ILuaValue index, ILuaValue value)
 {
     table.SetItemRaw(index, value);
     return table;
 }
 static ILuaValue rawset(ILuaTable table, ILuaValue index, ILuaValue value)
 {
     table.SetItemRaw(index, value);
     return(table);
 }