Пример #1
0
        static void Main(string[] args)
        {
            if (Directory.Exists("../XPNet.CLR/Data/GeneratedDataRefs"))
            {
                Directory.Delete("../XPNet.CLR/Data/GeneratedDataRefs", true);
            }
            Console.WriteLine($"Reading Datarefs from: {args[0]}");
            IEnumerable <string> dataRefs = Enumerable.Empty <string>();

            if (args.Length > 0 && File.Exists(args[0]))
            {
                dataRefs = File.ReadLines(args[0]);
                Console.WriteLine("Successfully read DataRefs");
            }
            var dataRefClasses = dataRefs
                                 .Where(d => d.StartsWith("sim") && !d.Contains("???"))
                                 .Select(d => d.Replace("\t-", " -")) //replace unit tabs with spaces
                                 .Select(d => d.Split('\t'))
                                 .Select(d => new DataRef
            {
                ParentPath  = d[0],
                Name        = d[0].LastElementOfPath(),
                Type        = d[1],
                IsWriteable = d[2] == "y",
                Units       = d.Skip(3).FirstOrDefault(),
                Description = d.LastOrDefault(f => f.Contains(' '))
            })
                                 .GroupBy(d =>
                                          String.Join("/", d.ParentPath.Split('/').SkipLast(1))
                                          , d => d,
                                          (path, members) => new DictionaryTree <DataRef>(path, members));

            var classTree = new DictionaryTree <DataRef>("Sim");

            foreach (var d in dataRefClasses)
            {
                var path    = d.Name.Split('/');
                var curNode = classTree;
                for (int i = 1; i < path.Length; i++)
                {
                    if (!curNode.Children.ContainsKey(path[i]))
                    {
                        curNode.Children[path[i]] = new DictionaryTree <DataRef>(
                            String.Join('/', path.Take(i + 1)),
                            i == path.Length - 1 ? d.Members : null);
                    }
                    curNode = curNode.Children[path[i]];
                }
            }

            BuildClassDefs(classTree);
            Console.WriteLine("Successfully generated DataRefs");
        }
Пример #2
0
        public static void BuildClassDefs(DictionaryTree <DataRef> node)
        {
            foreach (var child in node.Children.Values)
            {
                BuildClassDefs(child);
            }

            node.ClassDef = (node.Name == "Sim" ? simTemplate : nonSimTemplate)
                            .Replace("{item}", node.Name.Replace('/', '_'))
                            .Replace("{childInits}", string.Join("", node.Children.Values.Select(c => $"\n{initsIndent}{c.Name.LastElementOfPath().FixupSpecialKeywords()} = new {c.Name.Replace('/', '_')}DataRefs(data);")))
                            .Replace("{childProps}", string.Join("", node.Children.Values.Select(c => $"\n{propsIndent}public {c.Name.Replace('/', '_')}DataRefs {c.Name.LastElementOfPath().FixupSpecialKeywords()} {{ get; }}")))
                            .Replace("{members}", string.Join("", node.Members.Select(m =>
            {
                string result = string.Empty;
                if (m.Units == null)
                {
                    return(result);
                }

                if (m.Units.Equals("string", StringComparison.OrdinalIgnoreCase))
                {
                    return(CreateDataProperty <string>(m));
                }

                if (m.Units.StartsWith("string[", StringComparison.OrdinalIgnoreCase))
                {
                    return(CreateDataProperty <string[]>(m));
                }

                if ((m.Units.Equals("bool") || m.Units.Equals("boolean")) && m.Type.Contains("int["))
                {
                    return(CreateDataProperty <bool[]>(m));
                }

                if ((m.Units.Equals("bool") || m.Units.Equals("boolean")) && m.Type.Equals("int"))
                {
                    return(CreateDataProperty <bool>(m));
                }

                if (m.Type.Equals("float", StringComparison.OrdinalIgnoreCase))
                {
                    return(CreateDataProperty <float>(m));
                }

                if (m.Type.StartsWith("float[", StringComparison.OrdinalIgnoreCase))
                {
                    return(CreateDataProperty <float[]>(m));
                }

                if (!m.Units.Contains("bool") && m.Type.Equals("int"))
                {
                    return(CreateDataProperty <int>(m));
                }

                if (!m.Units.Contains("bool") && m.Type.Contains("int["))
                {
                    return(CreateDataProperty <int[]>(m));
                }

                if (m.Type.Equals("double", StringComparison.OrdinalIgnoreCase))
                {
                    return(CreateDataProperty <double>(m));
                }

                if (m.Units.Equals("index", StringComparison.OrdinalIgnoreCase) && m.Type.StartsWith("byte["))
                {
                    return(CreateDataProperty <string[]>(m));
                }

                throw new Exception($"Unhandled type: {m.Type}, Units={m.Units}");
            })
                                                              ));
            if (node.Name == "View")
            {
                Console.WriteLine(node.Members.Count());
            }

            var filePath = $"../XPNet.CLR/Data/GeneratedDataRefs/{node.Name}DataRefs.cs";

            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            File.WriteAllText(filePath, node.ClassDef);
        }