Exemplo n.º 1
0
        static void GenerateBindings(string inputPath, string outputPath)
        {
            inputPath = FixPath(inputPath);
            if (!Directory.Exists(inputPath))
            {
                return;
            }

            var files = Directory.GetFiles(inputPath);

            outputPath = FixPath(outputPath);
            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }

            string replacementFile = inputPath + "language.ini";
            var    replacements    = new Dictionary <string, string>();
            var    replacementsRef = new Dictionary <string, string>();

            if (File.Exists(replacementFile))
            {
                var lines = File.ReadAllLines(replacementFile);
                foreach (var line in lines)
                {
                    if (line.Contains(","))
                    {
                        var temp = line.Split(new[] { ',' }, 2);
                        if (temp[0].StartsWith("@"))
                        {
                            temp[0] = temp[0].Substring(1);
                            replacementsRef[temp[0]] = temp[1];
                        }
                        else
                        {
                            replacements[temp[0]] = temp[1];
                        }
                    }
                }
            }

            var nexus = new Nexus("dummy");
            var api   = new NexusAPI(nexus);

            foreach (var v in replacements)
            {
                if (!replacementsRef.ContainsKey(v.Key))
                {
                    replacementsRef.Add(v.Key, v.Value);
                }
            }
            var typeDic  = new Dictionary <string, IEnumerable <MetaField> >();
            var apiTypes = api.GetType().Assembly.GetTypes().Where(x => !x.IsInterface && x != typeof(SingleResult) && x != typeof(ArrayResult) && x != typeof(ErrorResult) && typeof(IAPIResult).IsAssignableFrom(x)).ToList();

            foreach (var entry in apiTypes)
            {
                typeDic[entry.Name /*.Replace("Result", "")*/] = GetMetaFields(entry);
            }

            var compiler = new Compiler();

            compiler.ParseNewLines = true;
            compiler.RegisterCaseTags();
            compiler.RegisterTag("fix-ref", (doc, x) => new FixTypeNode(doc, x, replacementsRef));
            compiler.RegisterTag("fix-type", (doc, x) => new FixTypeNode(doc, x, replacements));
            compiler.RegisterTag("fix-array", (doc, x) => new FixArrayNode(doc, x));

            var data = new Dictionary <string, object>();

            data["methods"] = api.Methods.Select(x => new MethodEntry()
            {
                Info = x, ResultFields = x.ReturnType.GetFields()
            });
            data["types"]   = typeDic;
            data["opcodes"] = Enum.GetValues(typeof(Opcode)).Cast <Opcode>().Select(x => new KeyValuePair <string, byte>(x.ToString(), (byte)x));

            foreach (var file in files)
            {
                if (file == replacementFile)
                {
                    continue;
                }

                var filePath = file;

                var content = File.ReadAllText(filePath);

                var template = compiler.CompileTemplate(content);
                var queue    = new Queue <Document>();

                var context = new RenderingContext();
                context.DataRoot  = data;
                context.DataStack = new List <object>();
                context.DataStack.Add(data);
                context.queue  = queue;
                context.output = new StringBuilder();
                template.Execute(context);

                filePath = file.Replace(inputPath, outputPath);
                File.WriteAllText(filePath, context.output.ToString());
            }
        }