Example #1
0
        /// <summary>
        /// Invokes a class method that doesn't take an instance as a receiver.
        /// </summary>
        /// <param name="vm">The environment in which to invoke the method.</param>
        /// <param name="value">The class the method belongs to.</param>
        /// <param name="methodName">The name of the method.</param>
        /// <returns>The method's return value.</returns>
        /// <exception cref="Exception">
        /// If the methodName isn't a method of the class or
        /// if Rhs isn't a <see cref="Block"/>.
        /// </exception>
        private Value InvokeBoundMethodClass(VM vm, ClassValue value, string methodName)
        {
            if (!value.ClassMethods.ContainsKey(methodName))
            {
                if (value.Methods.ContainsKey(methodName))
                {
                    throw new Exception(string.Format(
                                            "'{0}' is not a class method of '{1}'. Requires an instance of the class.",
                                            methodName,
                                            value.Name));
                }
                else
                {
                    throw new Exception(string.Format("'{0}' is not a method of '{1}'.", methodName, value.Name));
                }
            }

            LambdaExpression method = value.ClassMethods[methodName].Lambda;

            Block args = Rhs as Block;

            if (args == null)
            {
                throw new Exception("Internal: 'rhs' of 'Invocation' was not a 'Block'.");
            }

            VM @new = new VM(null, vm.Global);

            SetArguments(vm, @new, method, args.Nodes);
            return(CallWith(method, @new));
        }
Example #2
0
        /// <summary>
        /// Invokes a given class.
        /// </summary>
        /// <param name="vm">The environment in which to invoke the class.</param>
        /// <param name="value">The class to invoke.</param>
        /// <returns>A new <see cref="InstanceValue"/> of the given class.</returns>
        /// <exception cref="Exception">
        /// If Rhs doesn't return a <see cref="Block"/> or
        /// if 'init()' doesn't return a <see cref="NilValue"/>.
        /// </exception>
        private Value InvokeClass(VM vm, ClassValue value)
        {
            var self = new InstanceValue(value);

            if (value.Methods.ContainsKey("init"))
            {
                var init = value.Methods["init"].Lambda;

                Block args = Rhs as Block;
                if (args == null)
                {
                    throw new Exception("Internal: 'rhs' of 'Invocation' was not a 'Block'.");
                }

                VM @new = new VM(null, vm.Global);
                @new.Constants.Add(value.Name, value);
                @new.Constants.Add("self", self);
                SetArguments(vm, @new, init, args.Nodes);

                try
                {
                    init.Body.Execute(@new);
                }
                catch (ReturnStatement.Signal sig)
                {
                    if (!sig.Value.IsNil())
                    {
                        throw new Exception("Cannot return a non-nil value from class initializer.");
                    }
                }
            }

            return(self);
        }
Example #3
0
    /// <summary>
    /// 将一个场景物体转换为信息(string)
    /// </summary>
    /// <param name="root"></param>
    /// <returns></returns>
    public string GetSceneInfo()
    {
        MapWorld mapWorld = new MapWorld();

        List <MapObject> mapobjs = new List <MapObject>();
        int childCount           = mapNameRoot.childCount;

        for (int i = 0; i < childCount; i++)
        {
            Transform item = mapNameRoot.GetChild(i);
            MapObject mop  = new MapObject();
            mop.name     = item.name;
            mop.isStatic = item.gameObject.isStatic;
            mop.transformInfo.position = item.localPosition;
            mop.transformInfo.rotation = item.localRotation.eulerAngles;
            mop.transformInfo.scale    = item.localScale;

            MonoBehaviour[] mos = item.GetComponents <MonoBehaviour>();
            foreach (MonoBehaviour m in mos)
            {
                ClassValue cv = new ClassValue(m);
                mop.behavirComponets.Add(cv);
            }
            mapobjs.Add(mop);
        }
        mapWorld.mapObjects = mapobjs;

        mapWorld.lightPrefab = mapLightRoot.name;
        return(JsonUtils.ClassOrStructToJson(mapWorld));
    }
Example #4
0
 public ClassValue(string name, ClassValue superClass)
     : base(ValueType.Class)
 {
     Name         = name;
     Methods      = new Dictionary <string, LambdaValue>();
     ClassMethods = new Dictionary <string, LambdaValue>();
     SuperClass   = superClass;
 }
Example #5
0
        /// <summary>
        /// Adds a new <see cref="ClassValue"/> to <see cref="VM.Constants"/>.
        /// </summary>
        /// <param name="vm">The environment in which to add the new class.</param>
        /// <returns>A <see cref="NilValue"/>.</returns>
        /// <exception cref="Exception">
        /// If there is an unresolved identifier for its super class.
        /// If super class identifier doesn't correspond to a <see cref="ClassValue"/>.
        /// </exception>
        public Value Execute(VM vm)
        {
            ClassValue super = null;

            if (superClass != null)
            {
                if (!vm.Constants.ContainsKey(superClass))
                {
                    throw new Exception(string.Format("Unresolved identifier '{0}'.", superClass));
                }

                Value superClassValue = vm.Constants[superClass];
                Value.AssertType(Value.ValueType.Class, superClassValue,
                                 "Cannot inherit from something of type '{0}'.", superClassValue.Type);
                super = superClassValue.Class;
            }

            var @class = new ClassValue(name, super);

            if (super != null)
            {
                foreach (var method in super.Methods)
                {
                    string methodName = method.Key;
                    if (methodName == "<SUPER>")
                    {
                        continue;
                    }
                    if (method.Key == "init")
                    {
                        methodName = "<SUPER>";
                    }
                    @class.Methods[methodName] = method.Value;
                }

                foreach (var method in super.ClassMethods)
                {
                    @class.ClassMethods[method.Key] = method.Value;
                }
            }

            foreach (var method in methods)
            {
                string      methodName = method.Id;
                LambdaValue methodVal  = method.Execute(vm) as LambdaValue;
                @class.Methods[methodName] = methodVal;
            }

            foreach (var method in classMethods)
            {
                string      methodName = method.Id;
                LambdaValue methodVal  = method.Execute(vm) as LambdaValue;
                @class.ClassMethods[methodName] = methodVal;
            }

            vm.Constants.Add(name, @class);
            return(new NilValue());
        }
 public override void OnClose()
 {
     ecs.ECSComponentData.Clear();
     ecs.LS4ECS_Name = LS4ECS_Name;
     foreach (var item in componentData.Keys)
     {
         ClassValue classValue = new ClassValue(componentData[item]);
         ecs.ECSComponentData.Add(classValue);
     }
 }
Example #7
0
        private void Save()
        {
            config.allAppModuleSetting.Clear();
            foreach (var item in allModules)
            {
                ClassValue value = new ClassValue(item.Value, false);
                config.allAppModuleSetting.Add(item.Key.Name, value);
            }

            GameBootConfig.Save(config);
        }
Example #8
0
        /// <summary>
        /// Calls the init method of an instance's superclass.
        /// </summary>
        /// <remarks>
        /// Expects a constant called 'self' to be present in the VM.
        /// Expects it to have a superclass with an 'init()' method.
        /// </remarks>
        /// <param name="vm">Used to access global scope.</param>
        /// <returns>A <see cref="NilValue"/>.</returns>
        /// <exception cref="Exception">If an expectation is not met or another runtime error occurs.</exception>
        public override Value Execute(VM vm)
        {
            if (!vm.Parent.Constants.ContainsKey("self") ||
                !(vm.Parent.Constants["self"] is InstanceValue))
            {
                throw new Exception("Can only call 'super()' inside a class.");
            }

            InstanceValue self      = vm.Parent.Constants["self"].Instance;
            ClassValue    selfClass = self.UpCast();

            if (!selfClass.Methods.ContainsKey("<SUPER>"))
            {
                throw new Exception(string.Format("Cannot call 'super()'. '{0}' is not a subclass.", selfClass.Name));
            }

            LambdaExpression super = selfClass.Methods["<SUPER>"].Lambda;

            if (Nodes.Count != super.Args.Count)
            {
                throw new Exception(string.Format(
                                        "Argument Mistmatch! {0}.super takes {1} argument(s) but was given {2}.",
                                        selfClass.Name, super.Args.Count, Nodes.Count));
            }

            VM @new = new VM(null, vm.Global);

            @new.Constants.Add("self", self);

            for (int i = 0; i < super.Args.Count; i++)
            {
                string argId = super.Args[i];
                Value  arg   = Nodes[i].Execute(vm);
                @new.Variables.Add(argId, arg);
            }

            try
            {
                super.Body.Execute(@new);
            }
            catch (ReturnStatement.Signal sig)
            {
                if (!sig.Value.IsNil())
                {
                    throw new Exception("Cannot return a value from class initializer.");
                }
            }

            self.Cast(selfClass);

            return(new NilValue());
        }
        protected override void Action()
        {
            Entity entity = WorldManager.CurrentRunWorld.CreateEntity();

            for (int i = 0; i < ECSComponentData.Count; i++)
            {
                ClassValue cv        = ECSComponentData[i];
                IComponent component = (IComponent)cv.GetValue();
                if (component != null)
                {
                    entity.AddComponent(component);
                }
            }
        }
Example #10
0
        /// <summary>
        /// Invokes a method with an <see cref="InstanceValue"/> as the receiver.
        /// </summary>
        /// <param name="vm">The environment in which to invoke the method.</param>
        /// <param name="receiver">The receiver of the method.</param>
        /// <param name="methodName">The name of the method.</param>
        /// <returns>The method's return value.</returns>
        /// <exception cref="Exception">
        /// If the methodName isn't a method of the class or
        /// if Rhs isn't a <see cref="Block"/>.
        /// </exception>
        private Value InvokeBoundMethodInstance(VM vm, InstanceValue receiver, string methodName)
        {
            ClassValue @class = receiver.Class;

            if ([email protected](methodName))
            {
                throw new Exception(string.Format("'{0}' is not a method of '{1}'.", methodName, @class.Name));
            }

            var method = @class.Methods[methodName].Lambda;

            Block args = Rhs as Block;

            if (args == null)
            {
                throw new Exception("Internal: 'rhs' of 'Invocation' was not a 'Block'.");
            }

            VM @new = new VM(null, vm.Global);

            @new.Constants.Add("self", receiver);
            SetArguments(vm, @new, method, args.Nodes);
            return(CallWith(method, @new));
        }
				public void ReadXml (XmlTextReader xtr)
				{
					while (xtr.Read ()) {
						switch (xtr.NodeType) {
							case XmlNodeType.Element:
								string name = xtr.GetAttribute ("name");
								
								ClassValue class_value = classvalues_hashtable [name] as ClassValue;
								
								if (class_value == null) {
									class_value = new ClassValue ();
									class_value.Name = name;
									classvalues_hashtable [name] = class_value;
								}
								
								class_value.ReadXml (xtr);
								break;
								
							case XmlNodeType.EndElement:
								return;
						}
					}
				}
				public void SetValue (string value_name, object value)
				{
					ClassValue class_value = classvalues_hashtable [value_name] as ClassValue;
					
					if (class_value == null) {
						class_value = new ClassValue ();
						class_value.Name = value_name;
						classvalues_hashtable [value_name] = class_value;
					}
					
					class_value.SetValue (value);
				}
Example #13
0
        /// <summary>
        /// 生成场景物体
        /// </summary>
        /// <param name="id"></param>
        public static void CreateMapGameObjectInScene(int id)
        {
            List <GameObject> listObjs = new List <GameObject>();
            LightMapObject    lightMapObject;
            MapWorld          mapWorld = GetMapObject(id, out lightMapObject);

            if (mapWorld == null)
            {
                mapWorld = new MapWorld();
            }
            List <MapObject> mapObjs = mapWorld.mapObjects;

            if (mapRoot == null)
            {
                mapRoot = new GameObject(MapUseConstValues.MapRootObjectName);
            }
            Transform idRoot = mapRoot.transform.Find(id.ToString());

            if (idRoot == null)
            {
                idRoot = new GameObject(id.ToString()).transform;
                idRoot.SetParent(mapRoot.transform);
            }
            if (mapObjs == null)
            {
                mapObjs = new List <MapObject>();
                return;
            }
            GameObject mapObj = new GameObject(MapUseConstValues.MapObjectsObjectName);

            mapObj.transform.SetParent(idRoot);
            string     lightPrefabName = MapUseConstValues.LightPrefabNamePrefix + MapInfosDic[id].mapFileName;
            GameObject mapLights       = null;

            mapLights = PoolObjectManager.GetObject(lightPrefabName);
            if (mapLights == null)
            {
                mapLights = new GameObject(lightPrefabName);
            }
            mapLights.transform.SetParent(idRoot);

            string     colliderPrefabName = MapUseConstValues.ColliderPrefabNamePrefix + MapInfosDic[id].mapFileName;
            GameObject mapColliderObj     = null;

            mapColliderObj = PoolObjectManager.GetObject(colliderPrefabName);
            if (mapColliderObj == null)
            {
                mapColliderObj = new GameObject(colliderPrefabName);
            }
            mapColliderObj.transform.SetParent(idRoot);

            if (lightMapObject != null)
            {
                LightmapSettings.lightmapsMode = lightMapObject.lightmapsMode;

                RenderSettings.fog              = lightMapObject.fogInfo.fog;
                RenderSettings.fogMode          = lightMapObject.fogInfo.fogMode;
                RenderSettings.fogColor         = lightMapObject.fogInfo.fogColor;
                RenderSettings.fogStartDistance = lightMapObject.fogInfo.fogStartDistance;
                RenderSettings.fogEndDistance   = lightMapObject.fogInfo.fogEndDistance;
                RenderSettings.fogDensity       = lightMapObject.fogInfo.fogDensity;

                List <Texture2D> lightmapDir = new List <Texture2D>();
                for (int i = 0; i < lightMapObject.lightmapDir.Count; i++)
                {
                    string    name = lightMapObject.lightmapDir[i];
                    Texture2D tex  = ResourcesManager.LoadUnityAssetByName <Texture2D>(name);
                    lightmapDir.Add(tex);
                }
                List <Texture2D> lightmapColor = new List <Texture2D>();
                for (int i = 0; i < lightMapObject.lightmapColor.Count; i++)
                {
                    string    name = lightMapObject.lightmapColor[i];
                    Texture2D tex  = ResourcesManager.LoadUnityAssetByName <Texture2D>(name);
                    lightmapColor.Add(tex);
                }

                LightmapData[] lightmapData = new LightmapData[lightmapDir.Count > lightmapColor.Count ? lightmapDir.Count : lightmapColor.Count];
                for (int i = 0; i < lightmapData.Length; i++)
                {
                    lightmapData[i] = new LightmapData();
                    lightmapData[i].lightmapColor = i < lightmapColor.Count ? lightmapColor[i] : null;
                    lightmapData[i].lightmapDir   = i < lightmapDir.Count ? lightmapDir[i] : null;
                }
                LightmapSettings.lightmaps = lightmapData;
            }

            for (int i = 0; i < mapObjs.Count; i++)
            {
                MapObject  mp  = mapObjs[i];
                GameObject obj = PoolObjectManager.GetObject(mp.name);
                obj.isStatic = mp.isStatic;
                obj.transform.SetParent(mapObj.transform);
                obj.transform.localPosition = mp.transformInfo.position;
                obj.transform.localRotation = Quaternion.Euler(mp.transformInfo.rotation);
                obj.transform.localScale    = mp.transformInfo.scale;

                for (int j = 0; j < mp.behavirComponets.Count; j++)
                {
                    ClassValue cv = mp.behavirComponets[j];
                    cv.GetValue((type) =>
                    {
                        object v = obj.GetComponent(type);
                        if (v == null)
                        {
                            v = obj.AddComponent(type);
                        }
                        return(v);
                    });
                }
                listObjs.Add(obj);

                if (lightMapObject != null && obj.isStatic)
                {
                    MapObjectRendererLightMapInfo renderInfo = lightMapObject.mapObjectRendererLightMapInfo[i];

                    MeshRenderer[] msR = obj.GetComponentsInChildren <MeshRenderer>();
                    for (int j = 0; j < msR.Length; j++)
                    {
                        RendererLightMapInfo r = renderInfo.renderLightMapInfos[j];
                        msR[j].lightmapIndex       = r.lightmapIndex;
                        msR[j].lightmapScaleOffset = r.lightmapScaleOffset;
                    }
                }
            }
            if (lightMapObject != null)
            {
                Dictionary <string, string> reflectionProbesInfoDic = new Dictionary <string, string>();
                for (int i = 0; i < lightMapObject.reflectionProbeInfos.Count; i++)
                {
                    ReflectionProbeInfo info = lightMapObject.reflectionProbeInfos[i];
                    reflectionProbesInfoDic.Add(info.gameObjectName, info.reflectionProbeFileName);
                }
                ReflectionProbe[] reflectionProbes = mapLights.GetComponentsInChildren <ReflectionProbe>();
                for (int i = 0; i < reflectionProbes.Length; i++)
                {
                    ReflectionProbe re   = reflectionProbes[i];
                    string          name = reflectionProbesInfoDic[re.gameObject.name];
                    Texture         tex  = ResourcesManager.LoadUnityAssetByName <Texture>(name);
                    re.bakedTexture = tex;
                }
            }

            if (!mapInstanceBuffer.ContainsKey(id))
            {
                mapInstanceBuffer.Add(id, listObjs);
            }
        }
Example #14
0
 public ClassField(ClassValue value)
 {
     _value = value;
 }