Ejemplo n.º 1
0
 public ParsedFramesTree(ParsedFramesTree parsedFramesTree)
 {
     ParsedNode    = new ParsedNode(parsedFramesTree.ParsedNode);
     Name          = parsedFramesTree.Name;
     ClassName     = parsedFramesTree.ClassName;
     FieldName     = parsedFramesTree.FieldName;
     ScenePath     = parsedFramesTree.ScenePath;
     BaseClassName = parsedFramesTree.BaseClassName;
 }
Ejemplo n.º 2
0
 public ParsedNode(ParsedNode parsedNode)
 {
     Id           = parsedNode.Id;
     Name         = parsedNode.Name;
     Type         = parsedNode.Type;
     ClassName    = parsedNode.ClassName;
     FieldName    = parsedNode.FieldName;
     ContentsPath = parsedNode.ContentsPath;
     Markers      = parsedNode.Markers;
     NamedMarkers = parsedNode.NamedMarkers;
 }
Ejemplo n.º 3
0
 public ParsedNode(ParsedNode parsedNode)
 {
     Id                = parsedNode.Id;
     Name              = parsedNode.Name;
     Type              = parsedNode.Type;
     TypeFullName      = parsedNode.TypeFullName;
     ClassName         = parsedNode.ClassName;
     FieldName         = parsedNode.FieldName;
     ContentsPath      = parsedNode.ContentsPath;
     IsInExternalScene = parsedNode.IsInExternalScene;
     Markers           = parsedNode.Markers;
     NamedMarkers      = parsedNode.NamedMarkers;
 }
Ejemplo n.º 4
0
        public string GetFullTypeOf(ParsedNode node)
        {
            if (!node.IsExternalScene)
            {
                return(node.ClassName);
            }

            var nodeType   = $"{node.ClassName}<{node.Type}>";
            var bundleName = GetBundleNameOfExternalScene(node);

            if (string.IsNullOrEmpty(bundleName))
            {
                return(nodeType);
            }

            var nodeBundleNamespace = GetBundleNamespace(bundleName);

            return(GetBundleNamespace(sceneToBundleMap[currentCookingScene]) == nodeBundleNamespace ? nodeType : nodeBundleNamespace + '.' + nodeType);
        }
Ejemplo n.º 5
0
        private string GetBundleNameOfExternalScene(ParsedNode node)
        {
            var bundleName = string.Empty;
            var scenePath  = AssetPath.CorrectSlashes(node.ContentsPath);

            foreach (var sceneExtension in scenesExtensions)
            {
                string sceneBundle;
                if (!sceneToBundleMap.TryGetValue(scenePath + sceneExtension, out sceneBundle))
                {
                    continue;
                }

                bundleName = sceneBundle;
                break;
            }
            if (string.IsNullOrEmpty(bundleName))
            {
                Console.WriteLine("Warning! Can not find external scene \'{0}\' in \'{1}!\'", scenePath, currentCookingScene);
            }
            return(bundleName);
        }
Ejemplo n.º 6
0
        private ParsedFramesTree GenerateParsedFramesTreeHelper(Node node, string name, string baseName)
        {
            var parsedFramesTree = new ParsedFramesTree {
                ParsedNode    = new ParsedNode(node, name),
                ClassName     = name,
                Name          = name,
                FieldName     = "_" + name,
                BaseClassName = baseName,
            };

            if (parsedFramesTree.ParsedNode.IsExternalScene)
            {
                AddReferringSceneSafe(parsedFramesTree.ParsedNode.ContentsPath, currentCookingScene);
                string externalName;
                string externalBaseName;
                ParseCommonName(Path.GetFileNameWithoutExtension(parsedFramesTree.ParsedNode.ContentsPath), out externalName, out externalBaseName);
                parsedFramesTree.ClassName     = externalName;
                parsedFramesTree.BaseClassName = externalBaseName;
            }
            if (!baseName.IsNullOrWhiteSpace())
            {
                if (!commonParts.ContainsKey(baseName))
                {
                    commonParts.Add(baseName, new List <ParsedFramesTree>());
                }
                commonParts[baseName].Add(parsedFramesTree);
                if (!commonPartsScenes.ContainsKey(baseName))
                {
                    commonPartsScenes.Add(baseName, new HashSet <string>());
                }
                var path = node.GetRoot().Components.Get <Node.AssetBundlePathComponent>();
                if (path == null)
                {
                    throw new InvalidOperationException($"Asset path not specified via Node.AssetBundlePathComponent for {name}, {baseName}");
                }
                commonPartsScenes[baseName].Add(path.ToString());
            }

            var nodesToParse = new List <Node>();

            if (!parsedFramesTree.ParsedNode.IsExternalScene)
            {
                nodesToParse.Add(node);
            }

            while (nodesToParse.Count > 0)
            {
                var current = nodesToParse[0];
                nodesToParse.RemoveAt(0);

                foreach (var n in current.Nodes)
                {
                    if (!string.IsNullOrEmpty(n.Id) && n.Id.StartsWith(">"))
                    {
                        string           nextName;
                        string           nextBaseName;
                        ParsedFramesTree innerClass;
                        if (ParseCommonName(n.Id.Substring(1), out nextName, out nextBaseName))
                        {
                            innerClass = GenerateParsedFramesTreeHelper(n, nextName, nextBaseName);
                        }
                        else
                        {
                            innerClass = GenerateParsedFramesTreeHelper(n, nextName, null);
                        }
                        parsedFramesTree.InnerClasses.Add(innerClass);
                    }
                    else
                    {
                        ParsedNode parsedNode = null;
                        if (!string.IsNullOrEmpty(n.Id) && n.Id.StartsWith("@"))
                        {
                            parsedNode = new ParsedNode(n, n.Id.Substring(1));
                            parsedFramesTree.ParsedNodes.Add(parsedNode);
                        }
                        if (parsedNode != null && parsedNode.IsExternalScene)
                        {
                            AddReferringSceneSafe(parsedNode.ContentsPath, currentCookingScene);
                        }
                        if (n.Nodes.Count > 0 && (parsedNode == null || !parsedNode.IsExternalScene))
                        {
                            nodesToParse.Add(n);
                        }
                    }
                }
            }
            return(parsedFramesTree);
        }
Ejemplo n.º 7
0
        private void GenerateCommonParts(string scenesPath)
        {
            if (commonParts.Count <= 0)
            {
                return;
            }
            Action <ParsedNode, List <ParsedNode> > UniteMarkers = (dest, source) => {
                var markersUnion = new Dictionary <string, int>();
                foreach (var n in source)
                {
                    foreach (var marker in n.Markers)
                    {
                        if (!markersUnion.ContainsKey(marker))
                        {
                            markersUnion.Add(marker, 0);
                        }
                        markersUnion[marker]++;
                    }
                }
                dest.Markers.Clear();
                foreach (var mkv in markersUnion)
                {
                    if (mkv.Value == source.Count)
                    {
                        dest.Markers.Add(mkv.Key);
                    }
                }
            };
            var codeHeader = $"using Lime;\n namespace {(GetBundleNamespace(mainBundleName))}.Common\n{{\n" +
                             $"<%GEN%>\n" +
                             $"}}\n";
            var commonDirectory = $"{scenesPath}/Common";

            RetryUntilSuccessCreateDirectory(commonDirectory);
            foreach (var kv in commonParts)
            {
                var root = new ParsedFramesTree {
                    ClassName  = kv.Key,
                    ParsedNode = new ParsedNode(kv.Value.First().ParsedNode),
                };
                UniteMarkers(root.ParsedNode, kv.Value.Select(j => j.ParsedNode).ToList());
                var queue = new Queue <List <ParsedFramesTree> >();
                var sync  = new Queue <ParsedFramesTree>();
                queue.Enqueue(kv.Value);
                sync.Enqueue(root);
                while (queue.Count != 0)
                {
                    var s         = queue.Dequeue();
                    var nextRoot  = sync.Dequeue();
                    var treeUnion = new Dictionary <string, List <ParsedFramesTree> >();
                    var leafUnion = new Dictionary <string, List <ParsedNode> >();
                    foreach (var ft in s)
                    {
                        foreach (var leafNode in ft.ParsedNodes)
                        {
                            var k = leafNode.Name + GetFullTypeOf(leafNode);
                            if (!leafUnion.ContainsKey(k))
                            {
                                leafUnion.Add(k, new List <ParsedNode>());
                            }
                            leafUnion[k].Add(leafNode);
                        }
                        foreach (var treeNode in ft.InnerClasses)
                        {
                            var k = treeNode.Name + GetFullTypeOf(treeNode);
                            if (!treeUnion.ContainsKey(k))
                            {
                                treeUnion.Add(k, new List <ParsedFramesTree>());
                            }
                            treeUnion[k].Add(treeNode);
                        }
                    }
                    foreach (var i in leafUnion)
                    {
                        if (i.Value.Count == s.Count)
                        {
                            var leafClone = new ParsedNode(i.Value.First())
                            {
                                Type = GetCommonBaseClass(i.Value.Select(j => j.Type).ToList()).Name
                            };
                            UniteMarkers(leafClone, i.Value);
                            nextRoot.ParsedNodes.Add(leafClone);
                        }
                    }
                    foreach (var i in treeUnion)
                    {
                        if (i.Value.Count == s.Count)
                        {
                            var subtreeClone = new ParsedFramesTree(i.Value.First())
                            {
                                ParsedNode = { Type = GetCommonBaseClass(i.Value.Select(j => j.ParsedNode.Type).ToList()).Name }
                            };
                            UniteMarkers(subtreeClone.ParsedNode, i.Value.Select(j => j.ParsedNode).ToList());
                            nextRoot.InnerClasses.Add(subtreeClone);
                            queue.Enqueue(i.Value);
                            sync.Enqueue(subtreeClone);
                        }
                    }
                }
                if (root.InnerClasses.Count != 0 || root.ParsedNodes.Count != 0 || root.ParsedNode.Markers.Count != 0)
                {
                    var code = codeHeader.Replace("<%GEN%>", root.GenerateCode(this, false) + "\n<%GEN%>");
                    code = code.Replace("<%GEN%>", "");
                    code = new CodeFormatter(code).FormattedCode;
                    File.WriteAllText($"{commonDirectory}/{kv.Key}.cs", code);
                }
            }
        }