Пример #1
0
            double time(object source)
            {
                DateTime time;

                if (source is ILuaTable table)
                {
                    int year, month, day, hour, min, sec;
                    Func <string, bool, int> get = (name, req) => {
                        ILuaValue value = table.GetItemRaw(_env.Runtime.CreateValue(name));
                        if (value == null || value.ValueType != LuaValueType.Number)
                        {
                            if (req)
                            {
                                throw new ArgumentException(
                                          "First argument to function 'os.time' is not a valid time table.");
                            }
                            else
                            {
                                return(0);
                            }
                        }
                        else
                        {
                            return(value.As <int>());
                        }
                    };

                    year  = get("year", true);
                    month = get("month", true);
                    day   = get("day", true);
                    hour  = get("hour", false);
                    min   = get("min", false);
                    sec   = get("sec", false);

                    time = new DateTime(year, month, day, hour, min, sec);
                }
                else if (source is DateTime dt)
                {
                    time = dt;
                }
                else if (source is DateTimeOffset dto)
                {
                    time = dto.LocalDateTime;
                }
                else if (source != null)
                {
                    throw new ArgumentException("First argument to function 'os.time' must be a table.");
                }
                else
                {
                    time = DateTime.Now;
                }

                return(time.Ticks);
            }
Пример #2
0
            object[] rename(string old, string new_)
            {
                if (File.Exists(old))
                {
                    try
                    {
                        File.Move(old, new_);
                        return(new object[] { true });
                    }
                    catch (Exception e)
                    {
                        return(new object[] { null, e.Message, e });
                    }
                }
                else if (Directory.Exists(old))
                {
                    try
                    {
                        Directory.Move(old, new_);
                        return(new object[] { true });
                    }
                    catch (Exception e)
                    {
                        return(new object[] { null, e.Message, e });
                    }
                }
                else
                {
                    return new object[] { null, "Specified path does not exist." }
                };
            }

            string setlocale(string name)
            {
                try
                {
                    CultureInfo ci = CultureInfo.GetCultureInfo(name);

                    if (ci == null)
                    {
                        return(null);
                    }

                    Thread.CurrentThread.CurrentCulture = ci;
                    return(ci.Name);
                }
                catch (Exception)
                {
                    return(null);
                }
            }

            double time(object source)
            {
                DateTime time;

                if (source is ILuaTable)
                {
                    ILuaTable table = source as ILuaTable;
                    int       year, month, day, hour, min, sec;
                    Func <string, bool, int> get = (name, req) =>
                    {
                        ILuaValue value = table.GetItemRaw(E.Runtime.CreateValue(name));
                        if (value == null || value.ValueType != LuaValueType.Number)
                        {
                            if (req)
                            {
                                throw new ArgumentException("First argument to function 'os.time' is not a valid time table.");
                            }
                            else
                            {
                                return(0);
                            }
                        }
                        else
                        {
                            return(value.As <int>());
                        }
                    };

                    year  = get("year", true);
                    month = get("month", true);
                    day   = get("day", true);
                    hour  = get("hour", false);
                    min   = get("min", false);
                    sec   = get("sec", false);

                    time = new DateTime(year, month, day, hour, min, sec);
                }
                else if (source is DateTime)
                {
                    time = (DateTime)source;
                }
                else if (source is DateTimeOffset)
                {
                    time = ((DateTimeOffset)source).LocalDateTime;
                }
                else if (source != null)
                {
                    throw new ArgumentException("First argument to function 'os.time' must be a table.");
                }
                else
                {
                    time = DateTime.Now;
                }

                return(time.Ticks);
            }

            string tmpname()
            {
                return(Path.GetTempFileName());
            }
        }