예제 #1
0
    private static bool ExecuteUpdate(string dslFile)
    {
        try {
            if (!File.Exists(dslFile))
            {
                return(false);
            }
            bool        ret  = false;
            string      txt  = File.ReadAllText(dslFile);
            Dsl.DslFile file = new Dsl.DslFile();
            if (file.Load(dslFile, (string msg) => { Debug.LogError(msg); }))
            {
                s_Calculator = new DslExpression.DslCalculator();
                s_Calculator.Init();
                s_Calculator.SetGlobalVariable("@@processkey", s_ProcessKey);
                s_Calculator.SetGlobalVariable("@@haskey", s_HasKey);
                s_Calculator.SetGlobalVariable("global", DslExpression.CalculatorValue.FromObject(GlobalVariables.Instance));
                s_Calculator.Register("verifycacheserver", new DslExpression.ExpressionFactoryHelper <VerifyCacheServerExp>());

                foreach (var info in file.DslInfos)
                {
                    s_Calculator.LoadDsl(info);
                }
                object retObj = s_Calculator.Calc("main");
                if (null != retObj)
                {
                    ret = (bool)System.Convert.ChangeType(retObj, typeof(bool));
                }
            }
            return(ret);
        }
        catch {
            return(false);
        }
    }
예제 #2
0
    internal static void Process(GameObject root, Dsl.DslFile file, Dictionary <string, Color32[, ]> samplers, Dictionary <string, Size> cacheInfos)
    {
        if (null != file)
        {
            List <TreeInstance>          trees  = new List <TreeInstance>();
            Dictionary <string, int[, ]> caches = new Dictionary <string, int[, ]>();
            foreach (var pair in cacheInfos)
            {
                caches.Add(pair.Key, new int[pair.Value.Width, pair.Value.Height]);
            }
            var   terrain     = root.GetComponent <Terrain>();
            var   terrainData = terrain.terrainData;
            var   datas       = terrainData.GetHeights(0, 0, terrainData.heightmapWidth, terrainData.heightmapHeight);
            var   alphamaps   = terrainData.GetAlphamaps(0, 0, terrainData.alphamapWidth, terrainData.alphamapHeight);
            int   alphanum    = alphamaps.GetLength(2);
            int[] layers      = terrainData.GetSupportedLayers(0, 0, terrainData.detailWidth, terrainData.detailHeight);
            Dictionary <int, int[, ]> details = new Dictionary <int, int[, ]>();
            foreach (int layer in layers)
            {
                var ds = terrainData.GetDetailLayer(0, 0, terrainData.detailWidth, terrainData.detailHeight, layer);
                details.Add(layer, ds);
            }
            var calc = new DslExpression.DslCalculator();
            calc.Init();
            calc.Register("getheight", new DslExpression.ExpressionFactoryHelper <GetHeightExp>());
            calc.Register("getalphamap", new DslExpression.ExpressionFactoryHelper <GetAlphamapExp>());
            calc.Register("getalpha", new DslExpression.ExpressionFactoryHelper <GetAlphaExp>());
            calc.Register("setalpha", new DslExpression.ExpressionFactoryHelper <SetAlphaExp>());
            calc.Register("getdetail", new DslExpression.ExpressionFactoryHelper <GetDetailExp>());
            calc.Register("samplered", new DslExpression.ExpressionFactoryHelper <SampleRedExp>());
            calc.Register("samplegreen", new DslExpression.ExpressionFactoryHelper <SampleGreenExp>());
            calc.Register("sampleblue", new DslExpression.ExpressionFactoryHelper <SampleBlueExp>());
            calc.Register("samplealpha", new DslExpression.ExpressionFactoryHelper <SampleAlphaExp>());
            calc.Register("getcache", new DslExpression.ExpressionFactoryHelper <GetCacheExp>());
            calc.Register("setcache", new DslExpression.ExpressionFactoryHelper <SetCacheExp>());
            calc.Register("addtree", new DslExpression.ExpressionFactoryHelper <AddTreeExp>());
            calc.SetGlobalVariable("samplers", samplers);
            calc.SetGlobalVariable("caches", caches);
            calc.SetGlobalVariable("trees", trees);
            calc.SetGlobalVariable("heightscalex", terrainData.heightmapScale.x);
            calc.SetGlobalVariable("heightscaley", terrainData.heightmapScale.y);
            calc.SetGlobalVariable("heightscalez", terrainData.heightmapScale.z);
            calc.SetGlobalVariable("heights", datas);
            calc.SetGlobalVariable("alphamaps", alphamaps);
            calc.SetGlobalVariable("alphanum", alphanum);
            calc.SetGlobalVariable("details", details);
            calc.SetGlobalVariable("height", 0.0f);
            calc.SetGlobalVariable("alphas", new float[alphanum]);
            calc.SetGlobalVariable("detail", 0);
            bool resetTrees  = false;
            bool canContinue = true;
            foreach (var syntaxComponent in file.DslInfos)
            {
                var func = syntaxComponent as Dsl.FunctionData;
                var info = syntaxComponent as Dsl.StatementData;
                if (null == func && null != info)
                {
                    func = info.First;
                }
                int num = null != info?info.GetFunctionNum() : 1;

                bool check = false;
                if (num >= 2)
                {
                    string firstId = info.First.GetId();
                    if (firstId == "input")
                    {
                        check = true;
                        for (int i = 1; i < info.GetFunctionNum(); ++i)
                        {
                            string id = info.GetFunctionId(i);
                            if (id == "height" || id == "alphamap" || id == "detail")
                            {
                            }
                            else
                            {
                                check = false;
                                break;
                            }
                        }
                    }
                }
                if (!check)
                {
                    canContinue = false;
                    Debug.LogErrorFormat("error script:{0}, {1}", info.GetLine(), info.ToScriptString(false));
                }
            }
            if (canContinue)
            {
                int ix = 0;
                foreach (var syntaxComponent in file.DslInfos)
                {
                    var info = syntaxComponent as Dsl.StatementData;
                    if (null == info)
                    {
                        continue;
                    }
                    for (int i = 1; i < info.GetFunctionNum(); ++i)
                    {
                        calc.LoadDsl(ix.ToString(), info.GetFunction(i));
                        ++ix;
                    }
                }
                int ix2 = 0;
                foreach (var syntaxComponent in file.DslInfos)
                {
                    var info = syntaxComponent as Dsl.StatementData;
                    if (null == info)
                    {
                        continue;
                    }
                    for (int i = 1; i < info.GetFunctionNum(); ++i)
                    {
                        ProcessWithDsl(info.First, info.GetFunctionId(i), datas, alphamaps, details, calc, ix2.ToString(), ref resetTrees);
                        ++ix2;
                    }
                }
            }
            terrainData.SetHeights(0, 0, datas);
            terrainData.SetAlphamaps(0, 0, alphamaps);
            foreach (var pair in details)
            {
                terrainData.SetDetailLayer(0, 0, pair.Key, pair.Value);
            }
            if (resetTrees)
            {
                terrainData.treeInstances = trees.ToArray();
            }
            else
            {
                trees.AddRange(terrainData.treeInstances);
                terrainData.treeInstances = trees.ToArray();
            }
        }
    }