Example #1
0
        public static void CreateFile(string filename, NJS_OBJECT model, string[] animationFiles, string[] morphFiles,
                                      string author, string description, string tool, Dictionary <uint, byte[]> metadata, ModelFormat format)
        {
            bool be = ByteConverter.BigEndian;

            ByteConverter.BigEndian = false;
            if (format == ModelFormat.BasicDX)
            {
                format = ModelFormat.Basic;
            }
            List <byte> file = new List <byte>();
            ulong       magic;

            switch (format)
            {
            case ModelFormat.Basic:
            case ModelFormat.BasicDX:
                magic = SA1MDLVer;
                break;

            case ModelFormat.Chunk:
                magic = SA2MDLVer;
                break;

            default:
                throw new ArgumentException("Cannot save " + format.ToString() + " format models to file!", "format");
            }
            file.AddRange(ByteConverter.GetBytes(magic));
            Dictionary <string, uint> labels = new Dictionary <string, uint>();

            byte[] mdl = model.GetBytes(0x10, false, labels, out uint addr);
            file.AddRange(ByteConverter.GetBytes(addr + 0x10));
            file.AddRange(ByteConverter.GetBytes(mdl.Length + 0x10));
            file.AddRange(mdl);

            if (labels.Count > 0)
            {
                List <byte> chunk    = new List <byte>((labels.Count * 8) + 8);
                int         straddr  = (labels.Count * 8) + 8;
                List <byte> strbytes = new List <byte>();
                foreach (KeyValuePair <string, uint> label in labels)
                {
                    chunk.AddRange(ByteConverter.GetBytes(label.Value));
                    chunk.AddRange(ByteConverter.GetBytes(straddr + strbytes.Count));
                    strbytes.AddRange(Encoding.UTF8.GetBytes(label.Key));
                    strbytes.Add(0);
                    strbytes.Align(4);
                }
                chunk.AddRange(ByteConverter.GetBytes(-1L));
                chunk.AddRange(strbytes);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Label));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (animationFiles != null && animationFiles.Length > 0)
            {
                List <byte> chunk    = new List <byte>((animationFiles.Length + 1) * 4);
                int         straddr  = (animationFiles.Length + 1) * 4;
                List <byte> strbytes = new List <byte>();
                for (int i = 0; i < animationFiles.Length; i++)
                {
                    chunk.AddRange(ByteConverter.GetBytes(straddr + strbytes.Count));
                    strbytes.AddRange(Encoding.UTF8.GetBytes(animationFiles[i]));
                    strbytes.Add(0);
                    strbytes.Align(4);
                }
                chunk.AddRange(ByteConverter.GetBytes(-1));
                chunk.AddRange(strbytes);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Animation));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (morphFiles != null && morphFiles.Length > 0)
            {
                List <byte> chunk    = new List <byte>((morphFiles.Length + 1) * 4);
                int         straddr  = (morphFiles.Length + 1) * 4;
                List <byte> strbytes = new List <byte>();
                for (int i = 0; i < morphFiles.Length; i++)
                {
                    chunk.AddRange(ByteConverter.GetBytes(straddr + strbytes.Count));
                    strbytes.AddRange(Encoding.UTF8.GetBytes(morphFiles[i]));
                    strbytes.Add(0);
                    strbytes.Align(4);
                }
                chunk.AddRange(ByteConverter.GetBytes(-1));
                chunk.AddRange(strbytes);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Morph));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (!string.IsNullOrEmpty(author))
            {
                List <byte> chunk = new List <byte>(author.Length + 1);
                chunk.AddRange(Encoding.UTF8.GetBytes(author));
                chunk.Add(0);
                chunk.Align(4);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Author));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (!string.IsNullOrEmpty(description))
            {
                List <byte> chunk = new List <byte>(description.Length + 1);
                chunk.AddRange(Encoding.UTF8.GetBytes(description));
                chunk.Add(0);
                chunk.Align(4);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Description));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (!string.IsNullOrEmpty(tool))
            {
                List <byte> chunk = new List <byte>(tool.Length + 1);
                chunk.AddRange(Encoding.UTF8.GetBytes(tool));
                chunk.Add(0);
                chunk.Align(4);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Tool));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (metadata != null)
            {
                foreach (KeyValuePair <uint, byte[]> item in metadata)
                {
                    file.AddRange(ByteConverter.GetBytes(item.Key));
                    file.AddRange(ByteConverter.GetBytes(item.Value.Length));
                    file.AddRange(item.Value);
                }
            }
            file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.End));
            file.AddRange(new byte[4]);
            File.WriteAllBytes(filename, file.ToArray());
            ByteConverter.BigEndian = be;
        }
Example #2
0
        public static void CreateFile(string filename, NJS_OBJECT model, string[] animationFiles, string[] morphFiles,
            string author, string description, string tool, Dictionary<uint, byte[]> metadata, ModelFormat format)
        {
            bool be = ByteConverter.BigEndian;
            ByteConverter.BigEndian = false;
            if (format == ModelFormat.BasicDX)
                format = ModelFormat.Basic;
            List<byte> file = new List<byte>();
            ulong magic;
            switch (format)
            {
                case ModelFormat.Basic:
                case ModelFormat.BasicDX:
                    magic = SA1MDLVer;
                    break;
                case ModelFormat.Chunk:
                    magic = SA2MDLVer;
                    break;
                default:
                    throw new ArgumentException("Cannot save " + format.ToString() + " format models to file!", "format");
            }
            file.AddRange(ByteConverter.GetBytes(magic));
            uint addr;
            Dictionary<string, uint> labels = new Dictionary<string, uint>();
            byte[] mdl = model.GetBytes(0x10, false, labels, out addr);
            file.AddRange(ByteConverter.GetBytes(addr + 0x10));
            file.AddRange(ByteConverter.GetBytes(mdl.Length + 0x10));
            file.AddRange(mdl);

            if (labels.Count > 0)
            {
                List<byte> chunk = new List<byte>((labels.Count * 8) + 8);
                int straddr = (labels.Count * 8) + 8;
                List<byte> strbytes = new List<byte>();
                foreach (KeyValuePair<string, uint> label in labels)
                {
                    chunk.AddRange(ByteConverter.GetBytes(label.Value));
                    chunk.AddRange(ByteConverter.GetBytes(straddr + strbytes.Count));
                    strbytes.AddRange(Encoding.UTF8.GetBytes(label.Key));
                    strbytes.Add(0);
                    strbytes.Align(4);
                }
                chunk.AddRange(ByteConverter.GetBytes(-1L));
                chunk.AddRange(strbytes);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Label));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (animationFiles != null && animationFiles.Length > 0)
            {
                List<byte> chunk = new List<byte>((animationFiles.Length + 1) * 4);
                int straddr = (animationFiles.Length + 1) * 4;
                List<byte> strbytes = new List<byte>();
                for (int i = 0; i < animationFiles.Length; i++)
                {
                    chunk.AddRange(ByteConverter.GetBytes(straddr + strbytes.Count));
                    strbytes.AddRange(Encoding.UTF8.GetBytes(animationFiles[i]));
                    strbytes.Add(0);
                    strbytes.Align(4);
                }
                chunk.AddRange(ByteConverter.GetBytes(-1));
                chunk.AddRange(strbytes);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Animation));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (morphFiles != null && morphFiles.Length > 0)
            {
                List<byte> chunk = new List<byte>((morphFiles.Length + 1) * 4);
                int straddr = (morphFiles.Length + 1) * 4;
                List<byte> strbytes = new List<byte>();
                for (int i = 0; i < morphFiles.Length; i++)
                {
                    chunk.AddRange(ByteConverter.GetBytes(straddr + strbytes.Count));
                    strbytes.AddRange(Encoding.UTF8.GetBytes(morphFiles[i]));
                    strbytes.Add(0);
                    strbytes.Align(4);
                }
                chunk.AddRange(ByteConverter.GetBytes(-1));
                chunk.AddRange(strbytes);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Morph));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (!string.IsNullOrEmpty(author))
            {
                List<byte> chunk = new List<byte>(author.Length + 1);
                chunk.AddRange(Encoding.UTF8.GetBytes(author));
                chunk.Add(0);
                chunk.Align(4);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Author));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (!string.IsNullOrEmpty(description))
            {
                List<byte> chunk = new List<byte>(description.Length + 1);
                chunk.AddRange(Encoding.UTF8.GetBytes(description));
                chunk.Add(0);
                chunk.Align(4);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Description));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (!string.IsNullOrEmpty(tool))
            {
                List<byte> chunk = new List<byte>(tool.Length + 1);
                chunk.AddRange(Encoding.UTF8.GetBytes(tool));
                chunk.Add(0);
                chunk.Align(4);
                file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.Tool));
                file.AddRange(ByteConverter.GetBytes(chunk.Count));
                file.AddRange(chunk);
            }
            if (metadata != null)
            {
                foreach (KeyValuePair<uint, byte[]> item in metadata)
                {
                    file.AddRange(ByteConverter.GetBytes(item.Key));
                    file.AddRange(ByteConverter.GetBytes(item.Value.Length));
                    file.AddRange(item.Value);
                }
            }
            file.AddRange(ByteConverter.GetBytes((uint)ChunkTypes.End));
            file.AddRange(new byte[4]);
            File.WriteAllBytes(filename, file.ToArray());
            ByteConverter.BigEndian = be;
        }