public static List <string> GetGlobalStringArray(string varName) { List <string> ret = new List <string>(); MoonSharp.Interpreter.Table t = Instance.BootstrapScript.Globals[varName] as MoonSharp.Interpreter.Table; foreach (var res in t.Values) { ret.Add(res.CastToString()); } return(ret); }
//public Autodesk.Revit.DB.XYZ GetLocation(Element e) //{ // var lp = e.Location as LocationPoint; // if (lp == null) // { // return new Autodesk.Revit.DB.XYZ(); // } // else // return lp.Point; //} public Element[] GetFilteredElements(MoonSharp.Interpreter.Table t) { var filter = new FilteredElementCollector(UIApp.ActiveUIDocument.Document); for (int i = 0; i < t.Keys.Count(); i++) { switch (t.Keys.ElementAt(i).String) { case "full": filter = filter.WherePasses( new LogicalOrFilter( new ElementIsElementTypeFilter(false), new ElementIsElementTypeFilter(true))); break; case "class": filter = filter.OfClass((from x in TypeForm.GlobalTypesList where x.Name == t.Values.ElementAt(i).String select x).ToArray()[0]); break; case "category": filter = filter.OfCategory((BuiltInCategory)Enum.Parse(typeof(BuiltInCategory), t.Values.ElementAt(i).String)); break; case "filter": var func = t.Values.ElementAt(i).Function; List <Element> ret = new List <Element>(); foreach (var e in filter) { if (func.Call(e).Boolean) { ret.Add(e); } } return(ret.ToArray()); } } if (t.Keys.Count() == 0) { filter = filter.WherePasses( new LogicalOrFilter( new ElementIsElementTypeFilter(false), new ElementIsElementTypeFilter(true))); } return(filter.ToElements().ToArray()); }
public object CLRObject(string name, MoonSharp.Interpreter.Table t, string ns = "") { var tmp = (from x in TypeForm.GlobalTypesList where x.Name == name && (ns == "" || x.Namespace == ns) select x); if (tmp.Count() == 0) { throw new System.InvalidOperationException("This type is not imported: " + name); } var clsType = tmp.ToArray()[0]; List <object> _params = new List <object>(); foreach (var v in t.Values) { _params.Add(v.ToObject()); } return(Activator.CreateInstance(clsType, _params.ToArray())); }
public static void SetRow(Gwen.Control.Table table, Gwen.Control.TableRow row, object item) { string[] displayMembers = table.DisplayMembers; if (displayMembers == null || displayMembers.Length == 0) { string text = item.ToString(); row.Text = text; row.Name = text; row.UserData = item; } else { if (!(item is KeyValuePair <object, object>)) { throw new ScriptRuntimeException("Expecting table item"); } var keyValuePair = (KeyValuePair <object, object>)item; MoonSharp.Interpreter.Table luaTable = keyValuePair.Value as MoonSharp.Interpreter.Table; if (luaTable == null) { throw new ScriptRuntimeException("Expecting table"); } string col = luaTable[displayMembers[0]].ToString(); row.Name = col; row.UserData = item; row.SetCellText(0, col); for (int i = 1; i < displayMembers.Length; i++) { row.SetCellText(i, luaTable[displayMembers[i]].ToString()); } } }
public Table(MoonSharp.Interpreter.Table luaTable) : base() { IEnumerable <DynValue> keys = luaTable.Keys.AsEnumerable(); foreach (DynValue key in keys) { DynValue value = luaTable.Get(key); switch (value.Type) { case DataType.Table: this.Add(key.String, new Table(value.Table)); break; case DataType.String: this.Add(key.String, value.String); break; case DataType.Number: this.Add(key.String, value.Number); break; } } }
public static DynValue ClassToLuaTable(Script script, object obj) { MoonSharp.Interpreter.Table table = new MoonSharp.Interpreter.Table(script); var properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); for (int i = 0; i < properties.Length; i++) { var property = properties[i]; var value = property.GetValue(obj, null); if (value == null) { continue; } if (value.GetType() == typeof(string)) { table.Set(property.Name, DynValue.NewString((string)value)); } else if (value.GetType() == typeof(int)) { table.Set(property.Name, DynValue.NewNumber((int)value)); } else if (value.GetType() == typeof(float)) { table.Set(property.Name, DynValue.NewNumber((float)value)); } else if (value.GetType() == typeof(bool)) { table.Set(property.Name, DynValue.NewBoolean((bool)value)); } else if (value is ProtoBuf.IExtensible) { table.Set(property.Name, ClassToLuaTable(script, value)); } else if (value is IList) { IList list = value as IList; MoonSharp.Interpreter.Table value_table = new MoonSharp.Interpreter.Table(script); for (int j = 0; j < list.Count; ++j) { DynValue item = null; if (list[j] is ProtoBuf.IExtensible) { item = ClassToLuaTable(script, list[j]); } else if (list[j] is int) { item = DynValue.NewNumber((int)list[j]); } else if (list[j] is float) { item = DynValue.NewNumber((float)list[j]); } else if (list[j] is string) { item = DynValue.NewString((string)list[j]); } else if (list[j] is bool) { item = DynValue.NewBoolean((bool)list[j]); } else { Debug.LogError("Convert List to lua table error: unsupported types " + list[j].GetType()); } value_table.Append(item); } table.Set(property.Name, DynValue.NewTable(value_table)); } else { Debug.LogError("Convert class to lua table error: unsupported types " + value); } } return(DynValue.NewTable(table)); }
public MoonSharp.Interpreter.Table GetCLRObjectTable(object obj, int level = 1, bool err = false) { level--; MoonSharp.Interpreter.Table ret = new MoonSharp.Interpreter.Table(MoonScript); foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj)) { string name = descriptor.Name; if (TypeForm.OmittedProperties.Contains(name)) { continue; } object _value = null; try { _value = descriptor.GetValue(obj); } catch (Exception e) { ret.Set(name, MoonSharp.Interpreter.DynValue.NewString("[Error]")); continue; } if (_value is int) { ret.Set(name, MoonSharp.Interpreter.DynValue.NewNumber((int)_value)); } else if (_value is double) { ret.Set(name, MoonSharp.Interpreter.DynValue.NewNumber((double)_value)); } else if (_value is byte) { ret.Set(name, MoonSharp.Interpreter.DynValue.NewNumber((byte)_value)); } else if (_value is string) { ret.Set(name, MoonSharp.Interpreter.DynValue.NewString((string)_value)); } else if (_value is bool) { ret.Set(name, MoonSharp.Interpreter.DynValue.NewBoolean((bool)_value)); } else if (_value != null && _value.GetType().IsEnum) { ret.Set(name, MoonSharp.Interpreter.DynValue.NewNumber(Convert.ToInt32(_value))); } else if (level > 0) { try { ret.Set(name, MoonSharp.Interpreter.DynValue.NewTable(GetCLRObjectTable(_value, level, err))); } catch (Exception e) { if (err) { ret.Set(name, MoonSharp.Interpreter.DynValue.NewString("[Cannot get inside]")); } } } else { ret.Set(name, MoonSharp.Interpreter.DynValue.NewString(_value?.ToString())); } } return(ret); }