Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: Blitz3DToCSharp <input.bb> [output dir]");
                return;
            }
            string inputFile    = args[0].Replace('\\', '/');
            string outputFolder = "output";

            if (args.Length > 1)
            {
                outputFolder = args[1];
            }

            outputFolder = Path.GetFullPath(outputFolder).Replace('\\', '/');
            if (!outputFolder.EndsWith('/'))
            {
                outputFolder += '/';
            }

            if (!Directory.Exists(outputFolder))
            {
                Directory.CreateDirectory(outputFolder);
            }

            string inputFolder = Path.GetDirectoryName(inputFile).Replace('\\', '/');

            if (!string.IsNullOrEmpty(inputFolder))
            {
                inputFolder += '/';
            }

            Dictionary <string, BlitzBasicFile> files = new Dictionary <string, BlitzBasicFile>();

            BlitzBasicFile mainFile = BlitzBasicFile.Read(inputFile);

            files.Add(inputFile, mainFile);

            List <string> includes = new List <string>();

            includes.AddRange(mainFile.Includes);

            for (int i = 0; i < includes.Count; i++)
            {
                string fileName = inputFolder + includes[i];
                if (files.Keys.ToArray().ContainsIgnoreCase(fileName))
                {
                    continue;
                }
                BlitzBasicFile file = BlitzBasicFile.Read(fileName);
                files.Add(fileName, file);
                foreach (var include2 in file.Includes)
                {
                    if (!includes.ContainsIgnoreCase(include2))
                    {
                        includes.Add(include2);
                    }
                }
            }

            BlitzEnvironment environment = new BlitzEnvironment(files.Values.ToArray());

            Dictionary <string, string> CSharpOutput = new Dictionary <string, string>();

            foreach (var file in files)
            {
                string fileName = Path.ChangeExtension(file.Key, "cs").Replace("-", "");
                CSharpOutput.Add(fileName, file.Value.ToCSharp(environment));
            }

            foreach (var output in CSharpOutput)
            {
                var outputPath = outputFolder + output.Key;

                var outputBase = Path.GetDirectoryName(outputPath);
                if (!Directory.Exists(outputBase))
                {
                    Directory.CreateDirectory(outputBase);
                }

                using (Stream s = File.Open(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
                    using (StreamWriter sw = new StreamWriter(s))
                    {
                        sw.Write(output.Value);
                    }
            }
        }
Ejemplo n.º 2
0
        public string ToCSharp(BlitzEnvironment environment)
        {
            /*string fullFile = _lines.Join();
             * Tokenizer tokenizer = new Tokenizer(fullFile);
             * StringBuilder sb = new StringBuilder();
             * try
             * {
             *  while (tokenizer.HasNext())
             *  {
             *      char c = tokenizer.Next();
             *      if (c == ';')
             *          sb.Append("//");
             *  }
             * } catch (IndexOutOfRangeException ex)
             * {
             *  Console.WriteLine(ex.Message + ":\n" + ex.StackTrace);
             * }
             *
             * return sb.ToString();*/

            List <string> output = new List <string>();

            foreach (var l in _lines)
            {
                var line = l.Trim();

                if (line.StartsWith(";"))
                {
                    if (line.Length > 1)
                    {
                        output.Add("//" + line.Substring(1));
                    }
                    else
                    {
                        output.Add("//");
                    }
                    continue;
                }

                if (line.ToLower().StartsWith("global "))
                {
                    int indexInt    = line.IndexOf('%');
                    int indexFloat  = line.IndexOf('#');
                    int indexString = line.IndexOf('$');
                    int indexEquals = line.IndexOf('=');

                    BlitzType type;

                    string rest  = line.Substring(7);
                    string rest2 = rest;
                    if (indexEquals != -1)
                    {
                        rest = rest.Substring(0, rest.IndexOf('=')).Trim();
                    }

                    rest2 = rest2.Substring(rest.Length).Trim();

                    if (rest.EndsWith('%'))
                    {
                        type = BlitzType.Integer;
                        rest = rest.Substring(0, rest.IndexOf('%'));
                    }
                    else if (rest.EndsWith('#'))
                    {
                        type = BlitzType.Float;
                        rest = rest.Substring(0, rest.IndexOf('#'));
                    }
                    else if (rest.EndsWith('$'))
                    {
                        type = BlitzType.String;
                        rest = rest.Substring(0, rest.IndexOf('$'));
                    }
                    else
                    {
                        type = BlitzType.Integer;
                    }

                    string typeName = "int";
                    if (type == BlitzType.Float)
                    {
                        typeName = "float";
                    }
                    else if (type == BlitzType.String)
                    {
                        typeName = "string";
                    }

                    string[] split = rest2.Split(';', StringSplitOptions.RemoveEmptyEntries);
                    if (split.Length > 0)
                    {
                        rest += split[0].Trim();
                    }

                    if (split.Length > 1)
                    {
                        rest2 = " // " + split[1].Trim();
                    }
                    else
                    {
                        rest2 = "";
                    }

                    output.Add(typeName + " " + rest + ";" + rest2);
                    continue;
                }

                output.Add("");
            }

            StringBuilder sb = new StringBuilder();

            foreach (var line in output)
            {
                sb.Append(line + '\n');
            }

            return(sb.ToString());
        }