//Returns the given property (as reference) of the object identified by objectName public ObjectVariable GetPropertyOf(string propertyName, string objectName) { //First we identify the object Object @object = null; ObjectVariable objectVariable = GetVariableByName(objectName); if (objectVariable == null) { throw CreateException("Object not found"); } @object = (Object)objectVariable.Value; return(@object.GetVariableByName(propertyName, Game.Config)); }
public void TestConfigSerialization() { Config config = new Config(); //Add a string const Text text = new Text("Hello"); config.AddStringConst("hello", text); //Add a scheme Scheme scheme = new Scheme("MyScheme"); scheme.Code = "scheme MyScheme { }"; config.AddScheme(scheme); //Add an object magicedit.Object @object = new magicedit.Object("obj_id", "obj_name"); @object.Scheme = scheme; @object.Variables.Add(new ObjectVariable("number", "n", 10)); config.AddObject(@object); config.Save("myconfig.mec"); Config loadedConfig = Config.Load("myconfig.mec"); Assert.IsNotNull(loadedConfig.GetStringConstByName("hello")); Scheme loadedScheme = loadedConfig.GetSchemeByName("MyScheme"); Assert.IsNotNull(loadedScheme); Assert.AreEqual(scheme.Code, loadedScheme.Code); magicedit.Object loadedObject = loadedConfig.GetObjectById(@object.Id); Assert.IsNotNull(loadedObject); Assert.AreSame(loadedObject.Scheme, loadedScheme); ObjectVariable loadedVariable = loadedObject.GetVariableByName("n", loadedConfig); Assert.IsNotNull(loadedVariable); Assert.AreEqual((Int64)10, (Int64)loadedVariable.Value); }
public ObjectVariable GetVariableByName(string name) { //If variable is "actor" or "me" if (name == "actor") { return(new ObjectVariable(Actor.Scheme.Name, "actor", Actor)); } if (name == "me") { return(new ObjectVariable(Object.Scheme?.Name == null ? VariableTypes.Object : Object.Scheme.Name, "me", Object)); } //If it is a register (_0, _1 ... _r0, _r1 ...) we return its value if (IsRegister(name)) { if (Registers.ContainsKey(name)) { return(Registers[name]); } else { throw CreateException($"Register '{name}' does not exist."); } } //Check if object (me) has such variable ObjectVariable objectVariable = Object.GetVariableByName(name, Game.Config); if (objectVariable != null) { return(objectVariable); } //Check local variables foreach (var localVariable in LocalVariables) { if (localVariable.Name == name) { return(localVariable); } } //Check if it is a global object Object @object = Game.GetObjectById(name); if (@object != null) { //Type of an object is its scheme if (@object.Scheme?.Name != null) { return(new ObjectVariable(@object.Scheme.Name, name, @object)); } return(new ObjectVariable(VariableTypes.Object, name, @object)); } //Check if variable is a classvar constant foreach (ClassList classlist in Game.Config.ClassLists) { foreach (Class @class in classlist.Classes) { if (@class.Name == name) { return(new ObjectVariable(classlist.Name, name, @class)); } } } return(null); }