string concat(ILuaTable table, string sep = null, int i = 1, int j = -1) { CheckNotNull("table.concat", table); int len = (int)(table.Length().AsDouble() ?? 0); if (i >= len) { return(""); } i = normalizeIndex_(len, i); j = normalizeIndex_(len, j); StringBuilder str = new StringBuilder(); for (; i <= j; i++) { ILuaValue temp = table.GetItemRaw(E.Runtime.CreateValue(i)); if (temp.ValueType != LuaValueType.String && temp.ValueType != LuaValueType.Number) { throw new ArgumentException( "Invalid '" + temp.ValueType + "' value for function 'table.concat'."); } if (str.Length > 0) { str.Append(sep); } str.Append(temp); } return(str.ToString()); }
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); }
public static void LuaSetSpriteAnimation(ILuaExecuter executer) { var parameters = executer.PopParameters(); var img = parameters[0] as IImageAnimation; if (null != img && null != parameters[1]) { ILuaTable spriteTb = parameters[1] as ILuaTable; string[] spritesTb = spriteTb.ToArray() as string[]; List <SpriteLoadingInformation> spriteLoadingInformatonList = new List <SpriteLoadingInformation>(); for (int i = 0; i < spritesTb.Length; i++) { var spriteLoadingInformation = new SpriteLoadingInformation(); var spriteInfo = spritesTb[i]; var spriteInfoArray = spriteInfo.Split('|'); if (spriteInfoArray.Length < 2) { continue; } spriteLoadingInformation.path = spriteInfoArray[0]; spriteLoadingInformation.name = spriteInfoArray[1]; spriteLoadingInformatonList.Add(spriteLoadingInformation); } var framesPerSecond = parameters.Length > 2 && null != parameters[2] ? Convert.ToInt32(parameters[2]) : 10; img.SetSpriteAnimation(spriteLoadingInformatonList, framesPerSecond); } }
/// <summary> /// Creates a new LuaEnvironment without initializing the state, for use with a derived type. /// </summary> protected LuaEnvironmentNet() { _compiler = new CodeCompiler(); _parser = new PlainParser(); _runtime = LuaRuntimeNet.Create(this); Settings = new LuaSettings().AsReadOnly(); _globals = new LuaValues.LuaTable(); _modules = new ModuleBinder(); }
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); }
IEnumerable <ILuaValue> unpack(ILuaTable table, int i = 1, int?jOrNull = null) { CheckNotNull("table.unpack", table); int len = (int)(table.Length().AsDouble() ?? 0); int j = jOrNull ?? len; for (; i <= j; i++) { yield return(table.GetItemRaw(E.Runtime.CreateValue(i))); } }
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]); } }
public void Initialize() { ILuaTable coroutine = E_.Runtime.CreateTable(); Register(E_, coroutine, (Func <ILuaValue, ILuaValue>)create); Register(E_, coroutine, (Func <ILuaThread, ILuaValue[], IEnumerable <ILuaValue> >)resume); Register(E_, coroutine, (Func <object[]>)running); Register(E_, coroutine, (Func <ILuaThread, string>)status); Register(E_, coroutine, (Func <ILuaValue, object>)wrap); Register(E_, coroutine, (Func <ILuaValue[], ILuaMultiValue>)yield); E_.GlobalsTable.SetItemRaw(E_.Runtime.CreateValue("coroutine"), coroutine); }
public ILuaTable CreateContext(ILuaTable scope) { var env = _env; var tbl = env.NewTable(); ILuaTable meta = NewTable(); meta.Set("__index", scope != null ? (scope as XLuaTable).SourceTable : env.Environment.Global); tbl.SetMetaTable(meta); meta.Dispose(); return(tbl); }
static object[] pairs(ILuaTable table) { ILuaTable meta = table.MetaTable; if (meta != null) { ILuaValue p = meta.GetItemRaw(_pairs); if (p != null && p != LuaNil.Nil) { var ret = p.Invoke(table, true, -1, LuaMultiValue.Empty); return(new object[] { ret.AdjustResults(3) }); } } return(new object[] { (Func <ILuaTable, ILuaValue, object[]>)next, table }); }
public void Initialize() { ILuaTable os = _env.Runtime.CreateTable(); Register(_env, os, (Func <double>)clock); Register(_env, os, (Func <string, object, object>)date); Register(_env, os, (Func <double, double, double>)difftime); Register(_env, os, (Action <object, object>)exit); Register(_env, os, (Func <string, string>)getenv); Register(_env, os, (Func <string, object[]>)remove); Register(_env, os, (Func <string, string, object[]>)rename); Register(_env, os, (Func <string, string>)setlocale); Register(_env, os, (Func <object, double>)time); Register(_env, os, (Func <string>)tmpname); _env.GlobalsTable.SetItemRaw(_env.Runtime.CreateValue("os"), os); }
static ILuaValue setmetatable(ILuaTable table, ILuaValue metatable) { if (metatable == LuaNil.Nil) { table.MetaTable = null; } else if (metatable.ValueType == LuaValueType.Table) { table.MetaTable = (ILuaTable)metatable; } else { throw new ArgumentException("Second argument to 'setmetatable' must be a table."); } return(table); }
static object[] ipairs(ILuaTable table) { ILuaTable meta = table.MetaTable; if (meta != null) { ILuaValue method = meta.GetItemRaw(_ipairs); if (method != null && method != LuaNil.Nil) { var ret = method.Invoke(table, true, -1, LuaMultiValue.Empty); // The runtime will correctly expand the results (because the multi-value // is at the end). return(new object[] { ret.AdjustResults(3) }); } } return(new object[] { (Func <ILuaTable, double, object[]>)_ipairs_itr, table, 0 }); }
/// <summary> /// Creates a new environment with the given settings. /// </summary> /// <param name="settings">The settings to give the Environment.</param> /// <exception cref="System.ArgumentNullException">If settings is null.</exception> public LuaEnvironmentNet(LuaSettings settings) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } _globals = new LuaTable(); _runtime = LuaRuntimeNet.Create(this); _compiler = new CodeCompiler(); _parser = new PlainParser(); _modules = new ModuleBinder(); Settings = settings.AsReadOnly(); // initialize the global variables. LuaStaticLibraries.Initialize(this); _initializeTypes(); }
static object[] next(ILuaTable table, ILuaValue index) { bool return_next = index == LuaNil.Nil; foreach (var item in table) { if (return_next) { return(new object[] { item.Key, item.Value }); } else if (item.Key == index) { return_next = true; } } // return nil, nil; return(new object[0]); }
static object[] _ipairs_itr(ILuaTable table, double index) { if (index < 0 || index % 1 != 0) { throw new ArgumentException("Second argument to function 'ipairs iterator' must be a positive integer."); } index++; ILuaValue ret = table.GetItemRaw(new LuaNumber(index)); if (ret == null || ret == LuaNil.Nil) { return(new object[0]); } else { return new object[] { index, ret } }; } }
static ILuaValue getmetatable(ILuaValue value) { if (value.ValueType != LuaValueType.Table) { return(LuaNil.Nil); } ILuaTable meta = ((ILuaTable)value).MetaTable; if (meta != null) { ILuaValue method = meta.GetItemRaw(_metamethod); if (method != null && method != LuaNil.Nil) { return(method); } } return(meta); }
public void Execute(List <Entity <IUIPool> > entities) { foreach (var scopeEntity in entities) { ILuaTable props = null; ILuaTable state = _lua.NewTable(); ILuaTable context = _lua.CreateContext(); if (scopeEntity.Has <Parent>()) { // Child scope (embeded) - requesting props is required! var parent = _uiPool.GetParent(scopeEntity); props = parent.Has <LuaScopeProps>() ? parent.GetAttribute <LuaScopeProps, ILuaTable>() : _lua.NewTable(); } else if (scopeEntity.Has <LuaScopeProps>()) { props = scopeEntity.GetAttribute <LuaScopeProps, ILuaTable>(); } else { // Root scope must create clean props table props = _lua.NewTable(); } props.Set("Test", (Action)(() => Debug.Log("test"))); context.Set("state", state); context.Set("props", props); context.Set("scope", new ScopeTable(scopeEntity, _uiPool)); scopeEntity.SetAttribute <LuaScopeProps, ILuaTable>(props); scopeEntity.SetAttribute <LuaScopeState, ILuaTable>(state); scopeEntity.SetAttribute <LuaScopeContext, ILuaTable>(context); } }
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); }
public object[] Execute(string code, string key, ILuaTable context) { return(_env.DoString(code, key, context)); }
public ILuaTable GetGlobal() { return(_cachedGlobal ?? (_cachedGlobal = XLuaTable.Create(Environment.Global))); }
public void SetMetaTable(ILuaTable meta) { SourceTable.SetMetaTable((meta as XLuaTable).SourceTable); }
static object[] next(ILuaTable table, ILuaValue index) { bool return_next = index == LuaNil.Nil; foreach (var item in table) { if (return_next) return new object[] { item.Key, item.Value }; else if (item.Key == index) return_next = true; } // return nil, nil; return new object[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 object[] pairs(ILuaTable table) { ILuaTable meta = table.MetaTable; if (meta != null) { ILuaValue p = meta.GetItemRaw(_pairs); if (p != null && p != LuaNil.Nil) { var ret = p.Invoke(table, true, -1, LuaMultiValue.Empty); return new object[] { ret.AdjustResults(3) }; } } return new object[] { (Func<ILuaTable, ILuaValue, object[]>)next, table }; }
static object[] ipairs(ILuaTable table) { ILuaTable meta = table.MetaTable; if (meta != null) { ILuaValue method = meta.GetItemRaw(_ipairs); if (method != null && method != LuaNil.Nil) { var ret = method.Invoke(table, true, -1, LuaMultiValue.Empty); // The runtime will correctly expand the results (because the multi-value // is at the end). return new object[] { ret.AdjustResults(3) }; } } return new object[] { (Func<ILuaTable, double, object[]>)_ipairs_itr, table, 0 }; }
static ILuaValue rawlen(ILuaTable table) { return table.RawLength(); }
static ILuaValue rawget(ILuaTable table, ILuaValue index) { return table.GetItemRaw(index); }
public void GetLuaTable() { var panelName = gameObject.name.Split('(')[0]; this.LuaTable = HotfixManager.Inst.GetLuaTable(panelName); }
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()); } }
protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args) { Stream s = args[0].GetValue() as Stream; SeekOrigin origin = SeekOrigin.Current; long off = 0; if (s == null) { ILuaTable table = args[0] as ILuaTable; if (table != null) { s = table.GetItemRaw(_stream) as Stream; } if (s == null) { throw new ArgumentException("First real argument to function file:seek must be a file-stream, make sure to use file:seek."); } } if (args.Count > 1) { string str = args[1].GetValue() as string; if (str == "set") { origin = SeekOrigin.Begin; } else if (str == "cur") { origin = SeekOrigin.Current; } else if (str == "end") { origin = SeekOrigin.End; } else { throw new ArgumentException("First argument to function file:seek must be a string."); } if (args.Count > 2) { object obj = args[2].GetValue(); if (obj is double) { off = Convert.ToInt64((double)obj); } else { throw new ArgumentException("Second argument to function file:seek must be a number."); } } } if (!s.CanSeek) { return(Environment.Runtime.CreateMultiValueFromObj(null, "Specified stream cannot be seeked.")); } try { return(Environment.Runtime.CreateMultiValueFromObj(Convert.ToDouble(s.Seek(off, origin)))); } catch (Exception e) { return(Environment.Runtime.CreateMultiValueFromObj(null, e.Message, e)); } }
public object[] Execute(string code, string key, ILuaTable context) { return(Environment.DoString(code, key, (context as XLuaTable).SourceTable)); }
/// <summary> /// Creates a new environment with the given settings. /// </summary> /// <param name="settings">The settings to give the Environment.</param> /// <exception cref="System.ArgumentNullException">If settings is null.</exception> public LuaEnvironmentNet(LuaSettings settings) { if (settings == null) throw new ArgumentNullException("settings"); this._globals = new LuaValues.LuaTable(); this._runtime = LuaRuntimeNet.Create(this); this._compiler = new CodeCompiler(); this._parser = new PlainParser(); this._modules = new ModuleBinder(); this.Settings = settings.AsReadOnly(); // initialize the global variables. LuaStaticLibraries.Initialize(this); InitializeTypes(); }
public object[] DoString(string code, string key, ILuaTable context) { return(Environment.DoString(code, key, ((XLuaTable)context).SourceTable)); }
/// <summary> /// Creates a new LuaEnvironment without initializing the state, /// for use with a derrived type. /// </summary> protected LuaEnvironmentNet() { this._compiler = new CodeCompiler(); this._parser = new PlainParser(); this._runtime = LuaRuntimeNet.Create(this); this.Settings = new LuaSettings().AsReadOnly(); this._globals = new LuaValues.LuaTable(); this._modules = new ModuleBinder(); }
static ILuaValue rawset(ILuaTable table, ILuaValue index, ILuaValue value) { table.SetItemRaw(index, value); return(table); }
public void Set(object key, ILuaTable value) { SourceTable.Set(key, (value as XLuaTable)?.SourceTable); }
static object[] _ipairs_itr(ILuaTable table, double index) { if (index < 0 || index % 1 != 0) throw new ArgumentException("Second argument to function 'ipairs iterator' must be a positive integer."); index++; ILuaValue ret = table.GetItemRaw(new LuaNumber(index)); if (ret == null || ret == LuaNil.Nil) return new object[0]; else return new object[] { index, ret }; }
static ILuaValue setmetatable(ILuaTable table, ILuaValue metatable) { if (metatable == LuaNil.Nil) table.MetaTable = null; else if (metatable.ValueType == LuaValueType.Table) table.MetaTable = (ILuaTable)metatable; else { throw new ArgumentException( "Second argument to 'setmetatable' must be a table."); } return table; }