示例#1
0
        static void Main(string[] args)
        {
            string[] arguments = Environment.GetCommandLineArgs();
            string   filename;
            bool     dx = true;
            string   outputfile;
            string   extension;
            string   dir = Environment.CurrentDirectory;

            if (args.Length == 0)
            {
                Console.WriteLine("Struct Exporter is a tool that lets you convert level, model and animation files to C structs.");
                Console.WriteLine("Usage: StructExporter <filename> [output path] [-nodx]\n");
                Console.WriteLine("Arguments: -nodx to output Basic models without SADX additions\n");
                Console.WriteLine("Supported file types: sa1lvl, sa2lvl, sa1mdl, sa2mdl, saanim\n");
                Console.WriteLine("Examples:");
                Console.WriteLine("StructExporter mylevel.sa1lvl");
                Console.WriteLine("StructExporter mylevel.sa1lvl D:\\mylevel.c");
                Console.WriteLine("StructExporter mymodel.sa1mdl D:\\mymodel.c -nodx\n");
                Console.WriteLine("You can also drag your file onto StructExporter.exe to get it converted.");
                Console.WriteLine("Press ENTER to exit");
                Console.ReadLine();
                return;
            }
            //Args list: game, filename, key, type, address, [address2/count], [language], [name]
            filename   = args[0];
            outputfile = Path.GetFileNameWithoutExtension(filename) + ".c";
            if (args.Length > 1)
            {
                if (args[args.Length - 1] == "-nodx")
                {
                    dx = false;
                }
                if (args[1] != "-nodx")
                {
                    outputfile = args[1];
                }
            }
            byte[] file = File.ReadAllBytes(filename);
            extension = Path.GetExtension(filename);
            switch (extension)
            {
            case ".sa2lvl":
            case ".sa1lvl":
                LandTable     land   = LandTable.LoadFromFile(filename);
                List <string> labels = new List <string>()
                {
                    land.Name
                };
                using (StreamWriter sw = File.CreateText(outputfile))
                {
                    sw.Write("/* Sonic Adventure ");
                    LandTableFormat fmt = land.Format;
                    switch (land.Format)
                    {
                    case LandTableFormat.SA1:
                    case LandTableFormat.SADX:
                        if (dx)
                        {
                            sw.Write("DX");
                            fmt = LandTableFormat.SADX;
                        }
                        else
                        {
                            sw.Write("1");
                            fmt = LandTableFormat.SA1;
                        }
                        break;

                    case LandTableFormat.SA2:
                        sw.Write("2");
                        fmt = LandTableFormat.SA2;
                        break;

                    case LandTableFormat.SA2B:
                        sw.Write("2 Battle");
                        fmt = LandTableFormat.SA2B;
                        break;
                    }
                    sw.WriteLine(" LandTable");
                    sw.WriteLine(" * ");
                    sw.WriteLine(" * Generated by StructExporter");
                    sw.WriteLine(" * ");
                    if (!string.IsNullOrEmpty(land.Description))
                    {
                        sw.Write(" * Description: ");
                        sw.WriteLine(land.Description);
                        sw.WriteLine(" * ");
                    }
                    if (!string.IsNullOrEmpty(land.Author))
                    {
                        sw.Write(" * Author: ");
                        sw.WriteLine(land.Author);
                        sw.WriteLine(" * ");
                    }
                    sw.WriteLine(" */");
                    sw.WriteLine();
                    land.ToStructVariables(sw, fmt, labels, null);
                }
                break;

            case ".sa1mdl":
            case ".sa2mdl":
                ModelFile         modelFile  = new ModelFile(filename);
                NJS_OBJECT        model      = modelFile.Model;
                List <NJS_MOTION> animations = new List <NJS_MOTION>(modelFile.Animations);
                using (StreamWriter sw = File.CreateText(outputfile))
                {
                    sw.Write("/* NINJA ");
                    switch (modelFile.Format)
                    {
                    case ModelFormat.Basic:
                    case ModelFormat.BasicDX:
                        if (dx)
                        {
                            sw.Write("Basic (with Sonic Adventure DX additions)");
                        }
                        else
                        {
                            sw.Write("Basic");
                        }
                        break;

                    case ModelFormat.Chunk:
                        sw.Write("Chunk");
                        break;

                    case ModelFormat.GC:
                        sw.Write("GC");
                        break;
                    }
                    sw.WriteLine(" model");
                    sw.WriteLine(" * ");
                    sw.WriteLine(" * Generated by StructExporter");
                    sw.WriteLine(" * ");
                    if (modelFile != null)
                    {
                        if (!string.IsNullOrEmpty(modelFile.Description))
                        {
                            sw.Write(" * Description: ");
                            sw.WriteLine(modelFile.Description);
                            sw.WriteLine(" * ");
                        }
                        if (!string.IsNullOrEmpty(modelFile.Author))
                        {
                            sw.Write(" * Author: ");
                            sw.WriteLine(modelFile.Author);
                            sw.WriteLine(" * ");
                        }
                    }
                    sw.WriteLine(" */");
                    sw.WriteLine();
                    List <string> labels_m = new List <string>()
                    {
                        model.Name
                    };
                    model.ToStructVariables(sw, dx, labels_m, null);
                    foreach (NJS_MOTION anim in animations)
                    {
                        anim.ToStructVariables(sw);
                    }
                }
                break;

            case ".saanim":
                NJS_MOTION animation = NJS_MOTION.Load(filename);
                using (StreamWriter sw = File.CreateText(outputfile))
                {
                    sw.WriteLine("/* NINJA Motion");
                    sw.WriteLine(" * ");
                    sw.WriteLine(" * Generated by StructExporter");
                    sw.WriteLine(" * ");
                    sw.WriteLine(" */");
                    sw.WriteLine();
                    animation.ToStructVariables(sw);
                    break;
                }
            }
        }
示例#2
0
        /// <summary>
        /// Exports a single level, model or animation file as text.
        /// </summary>
        /// <param name="source">Source pathname.</param>
        /// <param name="type">Type of text conversion.</param>
        /// <param name="destination">Destination pathname. Leave blank to export in the same folder with a swapped extension.</param>
        /// <param name="basicDX">Use the SADX2004 format for Basic models.</param>
        public static void ConvertFileToText(string source, TextType type, string destination = "", bool basicDX = true, bool overwrite = true)
        {
            string outext    = ".c";
            string extension = Path.GetExtension(source);

            switch (extension.ToLowerInvariant())
            {
            case ".sa2lvl":
            case ".sa1lvl":
                if (type == TextType.CStructs)
                {
                    if (destination == "")
                    {
                        destination = Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source) + outext);
                    }
                    if (!overwrite && File.Exists(destination))
                    {
                        while (File.Exists(destination))
                        {
                            destination = destination = Path.Combine(Path.GetDirectoryName(destination), Path.GetFileNameWithoutExtension(destination) + "_" + outext);
                        }
                    }
                    LandTable     land   = LandTable.LoadFromFile(source);
                    List <string> labels = new List <string>()
                    {
                        land.Name
                    };
                    using (StreamWriter sw = File.CreateText(destination))
                    {
                        sw.Write("/* Sonic Adventure ");
                        LandTableFormat fmt = land.Format;
                        switch (land.Format)
                        {
                        case LandTableFormat.SA1:
                        case LandTableFormat.SADX:
                            if (basicDX)
                            {
                                sw.Write("DX");
                                fmt = LandTableFormat.SADX;
                            }
                            else
                            {
                                sw.Write("1");
                                fmt = LandTableFormat.SA1;
                            }
                            break;

                        case LandTableFormat.SA2:
                            sw.Write("2");
                            fmt = LandTableFormat.SA2;
                            break;

                        case LandTableFormat.SA2B:
                            sw.Write("2 Battle");
                            fmt = LandTableFormat.SA2B;
                            break;
                        }
                        sw.WriteLine(" LandTable");
                        sw.WriteLine(" * ");
                        sw.WriteLine(" * Generated by DataToolbox");
                        sw.WriteLine(" * ");
                        if (!string.IsNullOrEmpty(land.Description))
                        {
                            sw.Write(" * Description: ");
                            sw.WriteLine(land.Description);
                            sw.WriteLine(" * ");
                        }
                        if (!string.IsNullOrEmpty(land.Author))
                        {
                            sw.Write(" * Author: ");
                            sw.WriteLine(land.Author);
                            sw.WriteLine(" * ");
                        }
                        sw.WriteLine(" */");
                        sw.WriteLine();
                        land.ToStructVariables(sw, fmt, labels, null);
                    }
                }
                break;

            case ".sa1mdl":
            case ".sa2mdl":
                ModelFile         modelFile  = new ModelFile(source);
                NJS_OBJECT        model      = modelFile.Model;
                List <NJS_MOTION> animations = new List <NJS_MOTION>(modelFile.Animations);
                if (type == TextType.CStructs)
                {
                    outext = ".c";
                    if (destination == "")
                    {
                        destination = Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source) + outext);
                    }
                    if (!overwrite && File.Exists(destination))
                    {
                        while (File.Exists(destination))
                        {
                            destination = destination = Path.Combine(Path.GetDirectoryName(destination), Path.GetFileNameWithoutExtension(destination) + "_" + outext);
                        }
                    }
                    using (StreamWriter sw = File.CreateText(destination))
                    {
                        sw.Write("/* NINJA ");
                        switch (modelFile.Format)
                        {
                        case ModelFormat.Basic:
                        case ModelFormat.BasicDX:
                            if (basicDX)
                            {
                                sw.Write("Basic (with Sonic Adventure DX additions)");
                            }
                            else
                            {
                                sw.Write("Basic");
                            }
                            break;

                        case ModelFormat.Chunk:
                            sw.Write("Chunk");
                            break;

                        case ModelFormat.GC:
                            sw.Write("GC");
                            break;
                        }
                        sw.WriteLine(" model");
                        sw.WriteLine(" * ");
                        sw.WriteLine(" * Generated by DataToolbox");
                        sw.WriteLine(" * ");
                        if (modelFile != null)
                        {
                            if (!string.IsNullOrEmpty(modelFile.Description))
                            {
                                sw.Write(" * Description: ");
                                sw.WriteLine(modelFile.Description);
                                sw.WriteLine(" * ");
                            }
                            if (!string.IsNullOrEmpty(modelFile.Author))
                            {
                                sw.Write(" * Author: ");
                                sw.WriteLine(modelFile.Author);
                                sw.WriteLine(" * ");
                            }
                        }
                        sw.WriteLine(" */");
                        sw.WriteLine();
                        List <string> labels_m = new List <string>()
                        {
                            model.Name
                        };
                        model.ToStructVariables(sw, basicDX, labels_m, null);
                        foreach (NJS_MOTION anim in animations)
                        {
                            anim.ToStructVariables(sw);
                        }
                    }
                }
                else if (type == TextType.NJA)
                {
                    outext = ".nja";
                    if (destination == "")
                    {
                        destination = Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source) + outext);
                    }
                    if (!overwrite && File.Exists(destination))
                    {
                        while (File.Exists(destination))
                        {
                            destination = destination = Path.Combine(Path.GetDirectoryName(destination), Path.GetFileNameWithoutExtension(destination) + "_" + outext);
                        }
                    }
                    using (StreamWriter sw2 = File.CreateText(destination))
                    {
                        List <string> labels_nj = new List <string>()
                        {
                            model.Name
                        };
                        model.ToNJA(sw2, basicDX, labels_nj, null);
                    }
                }
                break;

            case ".saanim":
                NJS_MOTION animation = NJS_MOTION.Load(source);
                if (type == TextType.CStructs)
                {
                    outext = ".c";
                    if (destination == "")
                    {
                        destination = Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source) + outext);
                    }
                    if (!overwrite && File.Exists(destination))
                    {
                        while (File.Exists(destination))
                        {
                            destination = destination = Path.Combine(Path.GetDirectoryName(destination), Path.GetFileNameWithoutExtension(destination) + "_" + outext);
                        }
                    }
                    using (StreamWriter sw = File.CreateText(destination))
                    {
                        sw.WriteLine("/* NINJA Motion");
                        sw.WriteLine(" * ");
                        sw.WriteLine(" * Generated by DataToolbox");
                        sw.WriteLine(" * ");
                        sw.WriteLine(" */");
                        sw.WriteLine();
                        animation.ToStructVariables(sw);
                    }
                }
                else if (type == TextType.JSON)
                {
                    outext = ".json";
                    if (destination == "")
                    {
                        destination = Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source) + outext);
                    }
                    if (!overwrite && File.Exists(destination))
                    {
                        while (File.Exists(destination))
                        {
                            destination = destination = Path.Combine(Path.GetDirectoryName(destination), Path.GetFileNameWithoutExtension(destination) + "_" + outext);
                        }
                    }
                    JsonSerializer js = new JsonSerializer()
                    {
                        Culture = System.Globalization.CultureInfo.InvariantCulture
                    };
                    using (TextWriter tw = File.CreateText(destination))
                        using (JsonTextWriter jtw = new JsonTextWriter(tw)
                        {
                            Formatting = Formatting.Indented
                        })
                            js.Serialize(jtw, animation);
                }
                break;
            }
        }
示例#3
0
        private void cStructsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog sd = new SaveFileDialog()
            {
                DefaultExt = "c", Filter = "C Files|*.c"
            })
                if (sd.ShowDialog(this) == DialogResult.OK)
                {
                    bool dx = false;
                    if (outfmt == ModelFormat.Basic)
                    {
                        dx = MessageBox.Show(this, "Do you want to export in SADX format?", "SAMDL", MessageBoxButtons.YesNo) == DialogResult.Yes;
                    }
                    List <string> labels = new List <string>()
                    {
                        model.Name
                    };
                    using (StreamWriter sw = File.CreateText(sd.FileName))
                    {
                        sw.Write("/* NINJA ");
                        switch (outfmt)
                        {
                        case ModelFormat.Basic:
                        case ModelFormat.BasicDX:
                            if (dx)
                            {
                                sw.Write("Basic (with Sonic Adventure DX additions)");
                            }
                            else
                            {
                                sw.Write("Basic");
                            }
                            break;

                        case ModelFormat.Chunk:
                            sw.Write("Chunk");
                            break;
                        }
                        sw.WriteLine(" model");
                        sw.WriteLine(" * ");
                        sw.WriteLine(" * Generated by SAMDL");
                        sw.WriteLine(" * ");
                        if (modelFile != null)
                        {
                            if (!string.IsNullOrEmpty(modelFile.Description))
                            {
                                sw.Write(" * Description: ");
                                sw.WriteLine(modelFile.Description);
                                sw.WriteLine(" * ");
                            }
                            if (!string.IsNullOrEmpty(modelFile.Author))
                            {
                                sw.Write(" * Author: ");
                                sw.WriteLine(modelFile.Author);
                                sw.WriteLine(" * ");
                            }
                        }
                        sw.WriteLine(" */");
                        sw.WriteLine();
                        string[] texnames = null;
                        if (TexturePackName != null)
                        {
                            texnames = new string[TextureInfo.Length];
                            for (int i = 0; i < TextureInfo.Length; i++)
                            {
                                texnames[i] = string.Format("{0}TexName_{1}", TexturePackName, TextureInfo[i].Name);
                            }
                            sw.Write("enum {0}TexName", TexturePackName);
                            sw.WriteLine();
                            sw.WriteLine("{");
                            sw.WriteLine("\t" + string.Join("," + Environment.NewLine + "\t", texnames));
                            sw.WriteLine("};");
                            sw.WriteLine();
                        }
                        model.ToStructVariables(sw, dx, labels, texnames);
                    }
                }
        }