Ejemplo n.º 1
0
        private static string GetMotion(byte[] fc, int address, uint key, string fn, List <NJS_MOTION> motions, int cnt)
        {
            NJS_MOTION mtn = null;

            if (motions != null)
            {
                mtn = motions[ByteConverter.ToInt32(fc, address)];
            }
            else
            {
                int ptr3 = fc.GetPointer(address, key);
                if (ptr3 != 0)
                {
                    mtn = new NJS_MOTION(fc, ptr3, key, cnt);
                }
            }
            if (mtn == null)
            {
                return(null);
            }
            if (!motionfiles.ContainsKey(mtn.Name) || motionfiles[mtn.Name].Filename == null)
            {
                motionfiles[mtn.Name] = new MotionInfo(fn, mtn);
            }
            return(mtn.Name);
        }
Ejemplo n.º 2
0
 public EventEntity(byte[] file, int address, uint imageBase, bool battle, Dictionary <string, NJS_OBJECT> models, List <NJS_MOTION> motions)
 {
     Model = Event.GetModel(file, address, imageBase, models);
     if (battle)
     {
         Motion      = motions[ByteConverter.ToInt32(file, address + 4)];
         ShapeMotion = motions[ByteConverter.ToInt32(file, address + 8)];
         ShadowModel = Event.GetModel(file, address + 16, imageBase, models);
         Position    = new Vertex(file, address + 24);
         Flags       = ByteConverter.ToUInt32(file, address + 40);
     }
     else
     {
         int ptr = file.GetPointer(address + 4, imageBase);
         if (ptr != 0)
         {
             Motion = new NJS_MOTION(file, ptr, imageBase, Model.CountAnimated());
         }
         ptr = file.GetPointer(address + 8, imageBase);
         if (ptr != 0)
         {
             ShapeMotion = new NJS_MOTION(file, ptr, imageBase, Model.CountMorph());
         }
         Position = new Vertex(file, address + 16);
         Flags    = ByteConverter.ToUInt32(file, address + 28);
     }
 }
Ejemplo n.º 3
0
        public Event(string filename)
        {
            byte[] fc;
            if (Path.GetExtension(filename).Equals(".prs", StringComparison.OrdinalIgnoreCase))
            {
                fc = Prs.Decompress(filename);
            }
            else
            {
                fc = File.ReadAllBytes(filename);
            }
            bool battle = false;
            uint key;

            if (fc[0] == 0x81)
            {
                ByteConverter.BigEndian = true;
                key    = 0x8125FE60;
                battle = true;
            }
            else
            {
                ByteConverter.BigEndian = false;
                key = 0xC600000;
            }
            List <NJS_MOTION> motions = null;

            if (battle)
            {
                motions = ReadMotionFile(Path.ChangeExtension(filename, null) + "motion.bin");
            }
            Dictionary <string, NJS_OBJECT> models = new Dictionary <string, NJS_OBJECT>();
            int ptr = fc.GetPointer(0x20, key);

            if (ptr != 0)
            {
                int cnt = battle ? 18 : 16;
                Upgrades = new EventUpgrade[cnt];
                for (int i = 0; i < cnt; i++)
                {
                    Upgrades[i] = new EventUpgrade(fc, ptr, key, models);
                    ptr        += EventUpgrade.Size;
                }
            }
            int gcnt = ByteConverter.ToInt32(fc, 8);

            ptr = fc.GetPointer(0, key);
            if (ptr != 0)
            {
                Scenes = new List <EventScene>();
                for (int gn = 0; gn <= gcnt; gn++)
                {
                    Scenes.Add(new EventScene(fc, ptr, key, battle, models, motions));
                    ptr += EventScene.Size;
                }
            }
        }
Ejemplo n.º 4
0
        public EventScene(byte[] file, int address, uint imageBase, bool battle, Dictionary <string, NJS_OBJECT> models, List <NJS_MOTION> motions)
        {
            int ptr = file.GetPointer(address, imageBase);

            if (ptr != 0)
            {
                int cnt = ByteConverter.ToInt32(file, address + 4);
                Entities = new List <EventEntity>(cnt);
                for (int i = 0; i < cnt; i++)
                {
                    Entities.Add(new EventEntity(file, ptr, imageBase, battle, models, motions));
                    ptr += EventEntity.Size(battle);
                }
            }
            ptr = file.GetPointer(address + 8, imageBase);
            if (ptr != 0)
            {
                int cnt = ByteConverter.ToInt32(file, address + 12);
                CameraMotions = new List <NJS_MOTION>(cnt);
                for (int i = 0; i < cnt; i++)
                {
                    if (battle)
                    {
                        CameraMotions.Add(motions[ByteConverter.ToInt32(file, ptr)]);
                    }
                    else
                    {
                        CameraMotions.Add(new NJS_MOTION(file, file.GetPointer(ptr, imageBase), imageBase, 1));
                    }
                    ptr += sizeof(int);
                }
            }
            ptr = file.GetPointer(address + 24, imageBase);
            if (ptr != 0)
            {
                Big = new EventBig(file, ptr, imageBase, battle, models, motions);
            }
            FrameCount = ByteConverter.ToInt32(file, address + 28);
        }
Ejemplo n.º 5
0
        public static List <NJS_MOTION> ReadMotionFile(string filename)
        {
            List <NJS_MOTION> motions = new List <NJS_MOTION>();

            byte[] fc   = File.ReadAllBytes(filename);
            int    addr = 0;

            while (ByteConverter.ToInt64(fc, addr) != 0)
            {
                int ptr = ByteConverter.ToInt32(fc, addr);
                if (ptr == -1)
                {
                    motions.Add(null);
                }
                else
                {
                    motions.Add(new NJS_MOTION(fc, ptr, 0, ByteConverter.ToInt32(fc, addr + 4)));
                }
                addr += 8;
            }
            return(motions);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.Write("Filename: ");
                args = new string[] { Console.ReadLine().Trim('"') };
            }
            foreach (string filename in args)
            {
                Console.WriteLine("Splitting file {0}...", filename);
                byte[] fc;
                if (Path.GetExtension(filename).Equals(".prs", StringComparison.OrdinalIgnoreCase))
                {
                    fc = Prs.Decompress(filename);
                }
                else
                {
                    fc = File.ReadAllBytes(filename);
                }
                EventIniData ini = new EventIniData()
                {
                    Name = Path.GetFileNameWithoutExtension(filename)
                };
                string            path = Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(Path.GetFullPath(filename)), Path.GetFileNameWithoutExtension(filename))).FullName;
                uint              key;
                List <NJS_MOTION> motions = null;
                bool              battle;
                if (fc[0] == 0x81)
                {
                    Console.WriteLine("File is in GC/PC format.");
                    ByteConverter.BigEndian = true;
                    key         = 0x8125FE60;
                    ini.Game    = Game.SA2B;
                    battle      = true;
                    motions     = ReadMotionFile(Path.ChangeExtension(filename, null) + "motion.bin");
                    ini.Motions = motions.Select(a => a?.Name).ToList();
                    foreach (var mtn in motions.Where(a => a != null))
                    {
                        motionfiles[mtn.Name] = new MotionInfo(null, mtn);
                    }
                }
                else
                {
                    Console.WriteLine("File is in DC format.");
                    ByteConverter.BigEndian = false;
                    key      = 0xC600000;
                    ini.Game = Game.SA2;
                    battle   = false;
                }
                int ptr = fc.GetPointer(0x20, key);
                if (ptr != 0)
                {
                    for (int i = 0; i < (battle ? 18 : 16); i++)
                    {
                        string upnam = upgradenames[i];
                        string chnam = upnam;
                        switch (i)
                        {
                        case 0:
                            chnam = "Sonic";
                            break;

                        case 4:
                            chnam = "Shadow";
                            break;

                        case 6:
                            chnam = "Knuckles";
                            break;

                        case 12:
                            chnam = "Rouge";
                            break;

                        case 16:
                            chnam = "Mech Tails";
                            break;

                        case 17:
                            chnam = "Mech Eggman";
                            break;
                        }
                        UpgradeInfo info = new UpgradeInfo();
                        info.RootNode = GetModel(fc, ptr, key, $"{chnam} Root.sa2mdl");
                        if (info.RootNode != null)
                        {
                            int ptr2 = fc.GetPointer(ptr + 4, key);
                            if (ptr2 != 0)
                            {
                                info.AttachNode1 = $"object_{ptr2:X8}";
                            }
                            info.Model1 = GetModel(fc, ptr + 8, key, $"{upnam} Model 1.sa2mdl");
                            ptr2        = fc.GetPointer(ptr + 0xC, key);
                            if (ptr2 != 0)
                            {
                                info.AttachNode2 = $"object_{ptr2:X8}";
                            }
                            info.Model2 = GetModel(fc, ptr + 0x10, key, $"{upnam} Model 2.sa2mdl");
                        }
                        ini.Upgrades.Add(info);
                        ptr += 0x14;
                    }
                }
                else
                {
                    Console.WriteLine("Event contains no character upgrades.");
                }
                ptr = fc.GetPointer(0x18, key);
                if (ptr != 0)
                {
                    for (int i = 0; i < 93; i++)
                    {
                        string name = GetModel(fc, ptr, key, $"Mech Part {i + 1}.sa2mdl");
                        if (name != null)
                        {
                            ini.MechParts.Add(i, name);
                        }
                        ptr += 4;
                    }
                }
                else
                {
                    Console.WriteLine("Event contains no mech parts.");
                }
                int gcnt = ByteConverter.ToInt32(fc, 8);
                ptr = fc.GetPointer(0, key);
                if (ptr != 0)
                {
                    Console.WriteLine("Event contains {0} scene(s).", gcnt + 1);
                    for (int gn = 0; gn <= gcnt; gn++)
                    {
                        Directory.CreateDirectory(Path.Combine(path, $"Scene {gn + 1}"));
                        SceneInfo scn  = new SceneInfo();
                        int       ptr2 = fc.GetPointer(ptr, key);
                        int       ecnt = ByteConverter.ToInt32(fc, ptr + 4);
                        if (ptr2 != 0)
                        {
                            Console.WriteLine("Scene {0} contains {1} entit{2}.", gn + 1, ecnt, ecnt == 1 ? "y" : "ies");
                            for (int en = 0; en < ecnt; en++)
                            {
                                EntityInfo ent = new EntityInfo();
                                ent.Model = GetModel(fc, ptr2, key, $"Scene {gn + 1}\\Entity {en + 1} Model.sa2mdl");
                                if (ent.Model != null)
                                {
                                    ent.Motion = GetMotion(fc, ptr2 + 4, key, $"Scene {gn + 1}\\Entity {en + 1} Motion.saanim", motions, modelfiles[ent.Model].Model.CountAnimated());
                                    if (ent.Motion != null)
                                    {
                                        modelfiles[ent.Model].Motions.Add(motionfiles[ent.Motion].Filename);
                                    }
                                    ent.ShapeMotion = GetMotion(fc, ptr2 + 8, key, $"Scene {gn + 1}\\Entity {en + 1} Shape Motion.saanim", motions, modelfiles[ent.Model].Model.CountMorph());
                                    if (ent.ShapeMotion != null)
                                    {
                                        modelfiles[ent.Model].Motions.Add(motionfiles[ent.ShapeMotion].Filename);
                                    }
                                }
                                if (battle)
                                {
                                    ent.GCModel     = GetGCModel(fc, ptr2 + 12, key, $"Scene {gn + 1}\\Entity {en + 1} GC Model.sa2bmdl");
                                    ent.ShadowModel = GetModel(fc, ptr2 + 16, key, $"Scene {gn + 1}\\Entity {en + 1} Shadow Model.sa2mdl");
                                    ent.Position    = new Vertex(fc, ptr2 + 24);
                                    ent.Flags       = ByteConverter.ToUInt32(fc, ptr2 + 40);
                                }
                                else
                                {
                                    ent.Position = new Vertex(fc, ptr2 + 16);
                                    ent.Flags    = ByteConverter.ToUInt32(fc, ptr2 + 28);
                                }
                                scn.Entities.Add(ent);
                                ptr2 += ini.Game == Game.SA2B ? 0x2C : 0x20;
                            }
                        }
                        else
                        {
                            Console.WriteLine("Scene {0} contains no entities.", gn + 1);
                        }
                        ptr2 = fc.GetPointer(ptr + 8, key);
                        if (ptr2 != 0)
                        {
                            int cnt = ByteConverter.ToInt32(fc, ptr + 12);
                            for (int i = 0; i < cnt; i++)
                            {
                                scn.CameraMotions.Add(GetMotion(fc, ptr2, key, $"Scene {gn + 1}\\Camera Motion {i + 1}.saanim", motions, 1));
                                ptr2 += sizeof(int);
                            }
                        }
                        ptr2 = fc.GetPointer(ptr + 0x18, key);
                        if (ptr2 != 0)
                        {
                            BigInfo big = new BigInfo();
                            big.Model = GetModel(fc, ptr2, key, $"Scene {gn + 1}\\Big Model.sa2mdl");
                            if (big.Model != null)
                            {
                                int anicnt = modelfiles[big.Model].Model.CountAnimated();
                                int ptr3   = fc.GetPointer(ptr2 + 4, key);
                                if (ptr3 != 0)
                                {
                                    int cnt = ByteConverter.ToInt32(fc, ptr2 + 8);
                                    for (int i = 0; i < cnt; i++)
                                    {
                                        big.Motions.Add(new string[] { GetMotion(fc, ptr3, key, $"Scene {gn + 1}\\Big Motion {i + 1}a.saanim", motions, anicnt), GetMotion(fc, ptr3 + 4, key, $"Scene {gn + 1}\\Big Motion {i + 1}b.saanim", motions, anicnt) });
                                        ptr3 += 8;
                                    }
                                }
                            }
                            big.Unknown = ByteConverter.ToInt32(fc, ptr2 + 12);
                            scn.Big     = big;
                        }
                        scn.FrameCount = ByteConverter.ToInt32(fc, ptr + 28);
                        ini.Scenes.Add(scn);
                        ptr += 0x20;
                    }
                }
                else
                {
                    Console.WriteLine("Event contains no scenes.");
                }
                ptr = fc.GetPointer(0x1C, key);
                if (ptr != 0)
                {
                    ini.TailsTails = GetModel(fc, ptr, key, $"Tails' tails.sa2mdl");
                }
                else
                {
                    Console.WriteLine("Event does not contain Tails' tails.");
                }
                foreach (var item in motionfiles.Values)
                {
                    string fn = item.Filename ?? $"Unknown Motion {motions.IndexOf(item.Motion)}.saanim";
                    string fp = Path.Combine(path, fn);
                    item.Motion.Save(fp);
                    ini.Files.Add(fn, HelperFunctions.FileHash(fp));
                }
                foreach (var item in modelfiles.Values)
                {
                    string fp = Path.Combine(path, item.Filename);
                    ModelFile.CreateFile(fp, item.Model, item.Motions.ToArray(), null, null, null, item.Format);
                    ini.Files.Add(item.Filename, HelperFunctions.FileHash(fp));
                }
                JsonSerializer js = new JsonSerializer
                {
                    Formatting        = Formatting.Indented,
                    NullValueHandling = NullValueHandling.Ignore
                };
                using (var tw = File.CreateText(Path.Combine(path, Path.ChangeExtension(Path.GetFileName(filename), ".json"))))
                    js.Serialize(tw, ini);
            }
        }
Ejemplo n.º 7
0
        public NJS_MESHSET(byte[] file, int address, uint imageBase, Dictionary <int, string> labels)
        {
            MaterialID  = ByteConverter.ToUInt16(file, address);
            PolyType    = (Basic_PolyType)(MaterialID >> 0xE);
            MaterialID &= 0x3FFF;
            Poly[] polys   = new Poly[ByteConverter.ToInt16(file, address + 2)];
            int    tmpaddr = (int)(ByteConverter.ToUInt32(file, address + 4) - imageBase);

            if (labels.ContainsKey(tmpaddr))
            {
                PolyName = labels[tmpaddr];
            }
            else
            {
                PolyName = "poly_" + tmpaddr.ToString("X8");
            }
            int striptotal = 0;

            for (int i = 0; i < polys.Length; i++)
            {
                polys[i]    = SAModel.Poly.CreatePoly(PolyType, file, tmpaddr);
                striptotal += polys[i].Indexes.Length;
                tmpaddr    += polys[i].Size;
            }
            Poly    = new ReadOnlyCollection <Poly>(polys);
            PAttr   = ByteConverter.ToInt32(file, address + 8);
            tmpaddr = ByteConverter.ToInt32(file, address + 0xC);
            if (tmpaddr != 0)
            {
                tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase);
                if (labels.ContainsKey(tmpaddr))
                {
                    PolyNormalName = labels[tmpaddr];
                }
                else
                {
                    PolyNormalName = "polynormal_" + tmpaddr.ToString("X8");
                }
                PolyNormal = new Vertex[polys.Length];
                for (int i = 0; i < polys.Length; i++)
                {
                    PolyNormal[i] = new Vertex(file, tmpaddr);
                    tmpaddr      += Vertex.Size;
                }
            }
            else
            {
                PolyNormalName = "polynormal_" + Extensions.GenerateIdentifier();
            }
            tmpaddr = ByteConverter.ToInt32(file, address + 0x10);
            if (tmpaddr != 0)
            {
                tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase);
                if (labels.ContainsKey(tmpaddr))
                {
                    VColorName = labels[tmpaddr];
                }
                else
                {
                    VColorName = "vcolor_" + tmpaddr.ToString("X8");
                }
                VColor = new Color[striptotal];
                for (int i = 0; i < striptotal; i++)
                {
                    VColor[i] = SAModel.VColor.FromBytes(file, tmpaddr);
                    tmpaddr  += SAModel.VColor.Size(ColorType.ARGB8888_32);
                }
            }
            else
            {
                VColorName = "vcolor_" + Extensions.GenerateIdentifier();
            }
            tmpaddr = ByteConverter.ToInt32(file, address + 0x14);
            if (tmpaddr != 0)
            {
                tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase);
                if (labels.ContainsKey(tmpaddr))
                {
                    UVName = labels[tmpaddr];
                }
                else
                {
                    UVName = "uv_" + tmpaddr.ToString("X8");
                }
                UV = new UV[striptotal];
                for (int i = 0; i < striptotal; i++)
                {
                    UV[i]    = new UV(file, tmpaddr);
                    tmpaddr += SAModel.UV.Size;
                }
            }
            else
            {
                UVName = "uv_" + Extensions.GenerateIdentifier();
            }
        }
Ejemplo n.º 8
0
        public AnimModelData(byte[] file, int address, uint imageBase, AnimFlags animtype)
        {
            uint posoff = 0, rotoff = 0, scaoff = 0;

            if ((animtype & AnimFlags.Translate) == AnimFlags.Translate)
            {
                posoff = ByteConverter.ToUInt32(file, address);
                if (posoff > 0)
                {
                    posoff = posoff - imageBase;
                }
                address += 4;
            }
            if ((animtype & AnimFlags.Rotate) == AnimFlags.Rotate)
            {
                rotoff = ByteConverter.ToUInt32(file, address);
                if (rotoff > 0)
                {
                    rotoff = rotoff - imageBase;
                }
                address += 4;
            }
            if ((animtype & AnimFlags.Scale) == AnimFlags.Scale)
            {
                scaoff = ByteConverter.ToUInt32(file, address);
                if (scaoff > 0)
                {
                    scaoff = scaoff - imageBase;
                }
                address += 4;
            }
            try
            {
                int tmpaddr;
                if ((animtype & AnimFlags.Translate) == AnimFlags.Translate)
                {
                    int posframes = ByteConverter.ToInt32(file, address);
                    if (posframes > 0)
                    {
                        tmpaddr = (int)posoff;
                        for (int i = 0; i < posframes; i++)
                        {
                            Position.Add(ByteConverter.ToInt32(file, tmpaddr), new Vertex(file, tmpaddr + 4));
                            tmpaddr += 16;
                        }
                    }
                    address += 4;
                }
                if ((animtype & AnimFlags.Rotate) == AnimFlags.Rotate)
                {
                    int rotframes = ByteConverter.ToInt32(file, address);
                    if (rotframes > 0)
                    {
                        tmpaddr = (int)rotoff;
                        for (int i = 0; i < rotframes; i++)
                        {
                            Rotation.Add(ByteConverter.ToInt32(file, tmpaddr), new Rotation(file, tmpaddr + 4));
                            tmpaddr += 16;
                        }
                    }
                    address += 4;
                }
                if ((animtype & AnimFlags.Scale) == AnimFlags.Scale)
                {
                    int scaframes = ByteConverter.ToInt32(file, address);
                    if (scaframes > 0)
                    {
                        tmpaddr = (int)scaoff;
                        for (int i = 0; i < scaframes; i++)
                        {
                            Scale.Add(ByteConverter.ToInt32(file, tmpaddr), new Vertex(file, tmpaddr + 4));
                            tmpaddr += 16;
                        }
                    }
                    address += 4;
                }
            }
            catch (ArgumentOutOfRangeException)
            {
            }
        }
Ejemplo n.º 9
0
        public static LandTable LoadFromFile(string filename)
        {
            bool be = ByteConverter.BigEndian;

            ByteConverter.BigEndian = false;
            byte[] file    = File.ReadAllBytes(filename);
            ulong  magic   = ByteConverter.ToUInt64(file, 0) & FormatMask;
            byte   version = file[7];

            if (version > CurrentVersion)
            {
                throw new FormatException("Not a valid SA1LVL/SA2LVL file.");
            }
            Dictionary <int, string> labels = new Dictionary <int, string>();
            string author = null, description = null;
            Dictionary <uint, byte[]> meta = new Dictionary <uint, byte[]>();

            if (version < 2)
            {
                if (version == 1)
                {
                    int tmpaddr = ByteConverter.ToInt32(file, 0xC);
                    if (tmpaddr != 0)
                    {
                        int addr = ByteConverter.ToInt32(file, tmpaddr);
                        while (addr != -1)
                        {
                            labels.Add(addr, file.GetCString(ByteConverter.ToInt32(file, tmpaddr + 4)));
                            tmpaddr += 8;
                            addr     = ByteConverter.ToInt32(file, tmpaddr);
                        }
                    }
                }
            }
            else
            {
                int tmpaddr = ByteConverter.ToInt32(file, 0xC);
                if (tmpaddr != 0)
                {
                    bool finished = false;
                    while (!finished)
                    {
                        ChunkTypes type      = (ChunkTypes)ByteConverter.ToUInt32(file, tmpaddr);
                        int        chunksz   = ByteConverter.ToInt32(file, tmpaddr + 4);
                        int        nextchunk = tmpaddr + 8 + chunksz;
                        tmpaddr += 8;
                        if (version == 2)
                        {
                            switch (type)
                            {
                            case ChunkTypes.Label:
                                while (ByteConverter.ToInt64(file, tmpaddr) != -1)
                                {
                                    labels.Add(ByteConverter.ToInt32(file, tmpaddr), file.GetCString(ByteConverter.ToInt32(file, tmpaddr + 4)));
                                    tmpaddr += 8;
                                }
                                break;

                            case ChunkTypes.Author:
                                author = file.GetCString(tmpaddr);
                                break;

                            case ChunkTypes.Tool:
                                break;

                            case ChunkTypes.Description:
                                description = file.GetCString(tmpaddr);
                                break;

                            case ChunkTypes.End:
                                finished = true;
                                break;
                            }
                        }
                        else
                        {
                            byte[] chunk = new byte[chunksz];
                            Array.Copy(file, tmpaddr, chunk, 0, chunksz);
                            int chunkaddr = 0;
                            switch (type)
                            {
                            case ChunkTypes.Label:
                                while (ByteConverter.ToInt64(chunk, chunkaddr) != -1)
                                {
                                    labels.Add(ByteConverter.ToInt32(chunk, chunkaddr),
                                               chunk.GetCString(ByteConverter.ToInt32(chunk, chunkaddr + 4)));
                                    chunkaddr += 8;
                                }
                                break;

                            case ChunkTypes.Author:
                                author = chunk.GetCString(0);
                                break;

                            case ChunkTypes.Tool:
                                break;

                            case ChunkTypes.Description:
                                description = chunk.GetCString(0);
                                break;

                            case ChunkTypes.End:
                                finished = true;
                                break;

                            default:
                                meta.Add((uint)type, chunk);
                                break;
                            }
                        }
                        tmpaddr = nextchunk;
                    }
                }
            }
            if (magic == SA1LVL)
            {
                LandTable table = new LandTable(file, ByteConverter.ToInt32(file, 8), 0, LandTableFormat.SA1, labels)
                {
                    Author      = author,
                    Description = description,
                    Metadata    = meta
                };
                ByteConverter.BigEndian = be;
                return(table);
            }
            if (magic == SA2LVL)
            {
                LandTable table = new LandTable(file, ByteConverter.ToInt32(file, 8), 0, LandTableFormat.SA2, labels)
                {
                    Author      = author,
                    Description = description,
                    Metadata    = meta
                };
                ByteConverter.BigEndian = be;
                return(table);
            }
            if (magic == SA2BLVL)
            {
                LandTable table = new LandTable(file, ByteConverter.ToInt32(file, 8), 0, LandTableFormat.SA2B, labels)
                {
                    Author      = author,
                    Description = description,
                    Metadata    = meta
                };
                ByteConverter.BigEndian = be;
                return(table);
            }
            ByteConverter.BigEndian = be;
            throw new FormatException("Not a valid SA1LVL/SA2LVL file.");
        }
Ejemplo n.º 10
0
 static void Main(string[] args)
 {
     if (args.Length == 0)
     {
         Console.Write("Filename: ");
         args = new string[] { Console.ReadLine().Trim('"') };
     }
     foreach (string filename in args)
     {
         byte[] fc;
         if (Path.GetExtension(filename).Equals(".prs", StringComparison.OrdinalIgnoreCase))
         {
             fc = Prs.Decompress(filename);
         }
         else
         {
             fc = File.ReadAllBytes(filename);
         }
         string       path = Path.Combine(Path.GetDirectoryName(Path.GetFullPath(filename)), Path.GetFileNameWithoutExtension(filename));
         EventIniData ini  = IniSerializer.Deserialize <EventIniData>(Path.Combine(path, Path.ChangeExtension(Path.GetFileName(filename), ".ini")));
         uint         key;
         if (fc[0] == 0x81)
         {
             ByteConverter.BigEndian = true;
             key = 0x8125FE60;
         }
         else
         {
             ByteConverter.BigEndian = false;
             key = 0xC600000;
         }
         List <byte> modelbytes           = new List <byte>(fc);
         Dictionary <string, uint> labels = new Dictionary <string, uint>();
         foreach (string file in ini.Files.Where(a => HelperFunctions.FileHash(Path.Combine(path, a.Key)) != a.Value).Select(a => a.Key))
         {
             modelbytes.AddRange(new ModelFile(Path.Combine(path, file)).Model.GetBytes((uint)(key + modelbytes.Count), false, labels, out uint address));
         }
         fc = modelbytes.ToArray();
         int ptr = fc.GetPointer(0x20, key);
         if (ptr != 0)
         {
             for (int i = 0; i < (ini.Game == Game.SA2B ? 18 : 16); i++)
             {
                 UpgradeInfo info = ini.Upgrades[i];
                 if (info.RootNode != null)
                 {
                     if (labels.ContainsKey(info.RootNode))
                     {
                         ByteConverter.GetBytes(labels[info.RootNode]).CopyTo(fc, ptr);
                     }
                     if (labels.ContainsKey(info.AttachNode1))
                     {
                         ByteConverter.GetBytes(labels[info.AttachNode1]).CopyTo(fc, ptr + 4);
                     }
                     if (labels.ContainsKey(info.Model1))
                     {
                         ByteConverter.GetBytes(labels[info.Model1]).CopyTo(fc, ptr + 8);
                     }
                     if (info.AttachNode2 != null && labels.ContainsKey(info.AttachNode2))
                     {
                         ByteConverter.GetBytes(labels[info.AttachNode2]).CopyTo(fc, ptr + 12);
                     }
                     if (info.Model2 != null && labels.ContainsKey(info.Model2))
                     {
                         ByteConverter.GetBytes(labels[info.Model2]).CopyTo(fc, ptr + 16);
                     }
                 }
                 ptr += 0x14;
             }
         }
         int gcnt = ByteConverter.ToInt32(fc, 8);
         ptr = fc.GetPointer(0, key);
         if (ptr != 0)
         {
             for (int gn = 0; gn <= gcnt; gn++)
             {
                 GroupInfo info = ini.Groups[gn];
                 int       ptr2 = fc.GetPointer(ptr, key);
                 int       ecnt = Math.Min(ByteConverter.ToInt32(fc, ptr + 4), info.Entities?.Count ?? 0);
                 if (ptr2 != 0)
                 {
                     for (int en = 0; en < ecnt; en++)
                     {
                         if (labels.ContainsKey(info.Entities[en]))
                         {
                             ByteConverter.GetBytes(labels[info.Entities[en]]).CopyTo(fc, ptr2);
                         }
                         ptr2 += ini.Game == Game.SA2B ? 0x2C : 0x20;
                     }
                 }
                 ptr2 = fc.GetPointer(ptr + 0x18, key);
                 if (ptr2 != 0 && info.Big != null && labels.ContainsKey(info.Big))
                 {
                     ByteConverter.GetBytes(labels[info.Big]).CopyTo(fc, ptr2);
                 }
                 ptr += 0x20;
             }
         }
         ptr = fc.GetPointer(0x18, key);
         if (ptr != 0)
         {
             for (int i = 0; i < 18; i++)
             {
                 if (ini.MechParts.ContainsKey(i) && labels.ContainsKey(ini.MechParts[i]))
                 {
                     ByteConverter.GetBytes(labels[ini.MechParts[i]]).CopyTo(fc, ptr);
                 }
                 ptr += 4;
             }
         }
         ptr = fc.GetPointer(0x1C, key);
         if (ptr != 0 && ini.TailsTails != null && labels.ContainsKey(ini.TailsTails))
         {
             ByteConverter.GetBytes(labels[ini.TailsTails]).CopyTo(fc, ptr);
         }
         if (Path.GetExtension(filename).Equals(".prs", StringComparison.OrdinalIgnoreCase))
         {
             Prs.Compress(fc, filename);
         }
         else
         {
             File.WriteAllBytes(filename, fc);
         }
     }
 }
Ejemplo n.º 11
0
        public BasicAttach(byte[] file, int address, uint imageBase, bool DX, Dictionary <int, string> labels)
            : this()
        {
            if (labels.ContainsKey(address))
            {
                Name = labels[address];
            }
            else
            {
                Name = "attach_" + address.ToString("X8");
            }
            Vertex = new Vertex[ByteConverter.ToInt32(file, address + 8)];
            Normal = new Vertex[Vertex.Length];
            int tmpaddr = (int)(ByteConverter.ToUInt32(file, address) - imageBase);

            if (labels.ContainsKey(tmpaddr))
            {
                VertexName = labels[tmpaddr];
            }
            else
            {
                VertexName = "vertex_" + tmpaddr.ToString("X8");
            }
            for (int i = 0; i < Vertex.Length; i++)
            {
                Vertex[i] = new Vertex(file, tmpaddr);
                tmpaddr  += SAModel.Vertex.Size;
            }
            tmpaddr = ByteConverter.ToInt32(file, address + 4);
            if (tmpaddr != 0)
            {
                tmpaddr = (int)((uint)tmpaddr - imageBase);
                if (labels.ContainsKey(tmpaddr))
                {
                    NormalName = labels[tmpaddr];
                }
                else
                {
                    NormalName = "normal_" + tmpaddr.ToString("X8");
                }
                for (int i = 0; i < Vertex.Length; i++)
                {
                    Normal[i] = new Vertex(file, tmpaddr);
                    tmpaddr  += SAModel.Vertex.Size;
                }
            }
            else
            {
                for (int i = 0; i < Vertex.Length; i++)
                {
                    Normal[i] = new Vertex(0, 1, 0);
                }
            }
            int maxmat  = -1;
            int meshcnt = ByteConverter.ToInt16(file, address + 0x14);

            tmpaddr = ByteConverter.ToInt32(file, address + 0xC);
            if (tmpaddr != 0)
            {
                tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase);
                if (labels.ContainsKey(tmpaddr))
                {
                    MeshName = labels[tmpaddr];
                }
                else
                {
                    MeshName = "meshlist_" + tmpaddr.ToString("X8");
                }
                for (int i = 0; i < meshcnt; i++)
                {
                    Mesh.Add(new NJS_MESHSET(file, tmpaddr, imageBase, labels));
                    maxmat   = Math.Max(maxmat, Mesh[i].MaterialID);
                    tmpaddr += NJS_MESHSET.Size(DX);
                }
            }
            // fixes case where model declares material array as shorter than it really is
            int matcnt = Math.Max(ByteConverter.ToInt16(file, address + 0x16), maxmat + 1);

            tmpaddr = ByteConverter.ToInt32(file, address + 0x10);
            if (tmpaddr != 0)
            {
                tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase);
                if (labels.ContainsKey(tmpaddr))
                {
                    MaterialName = labels[tmpaddr];
                }
                else
                {
                    MaterialName = "matlist_" + tmpaddr.ToString("X8");
                }
                for (int i = 0; i < matcnt; i++)
                {
                    Material.Add(new NJS_MATERIAL(file, tmpaddr, labels));
                    tmpaddr += NJS_MATERIAL.Size;
                }
            }
            Bounds = new BoundingSphere(file, address + 0x18);
        }
Ejemplo n.º 12
0
        public COL(byte[] file, int address, uint imageBase, LandTableFormat format, Dictionary <int, string> labels, bool?forceBasic, Dictionary <int, Attach> attaches)
        {
            Bounds = new BoundingSphere(file, address);
            ModelFormat mfmt = 0;

            switch (format)
            {
            case LandTableFormat.SA1:
                mfmt = ModelFormat.Basic;
                break;

            case LandTableFormat.SADX:
                mfmt = ModelFormat.BasicDX;
                break;

            case LandTableFormat.SA2:
                if (forceBasic.HasValue && forceBasic.Value)
                {
                    mfmt = ModelFormat.Basic;
                }
                else
                {
                    mfmt = ModelFormat.Chunk;
                }
                break;

            case LandTableFormat.SA2B:
                if (forceBasic.HasValue && forceBasic.Value)
                {
                    mfmt = ModelFormat.Basic;
                }
                else
                {
                    mfmt = ModelFormat.GC;
                }
                break;
            }
            switch (format)
            {
            case LandTableFormat.SA1:
            case LandTableFormat.SADX:
                Unknown1 = ByteConverter.ToInt32(file, address + 0x10);
                Unknown2 = ByteConverter.ToInt32(file, address + 0x14);
                uint tmpaddr = ByteConverter.ToUInt32(file, address + 0x18) - imageBase;
                Model    = new NJS_OBJECT(file, (int)tmpaddr, imageBase, mfmt, labels, attaches);
                Unknown3 = ByteConverter.ToInt32(file, address + 0x1C);
                Flags    = ByteConverter.ToInt32(file, address + 0x20);
                break;

            case LandTableFormat.SA2:
            case LandTableFormat.SA2B:
                Flags = ByteConverter.ToInt32(file, address + 0x1C);
                if (!forceBasic.HasValue && Flags >= 0)
                {
                    mfmt = ModelFormat.Basic;
                }
                tmpaddr  = ByteConverter.ToUInt32(file, address + 0x10) - imageBase;
                Model    = new NJS_OBJECT(file, (int)tmpaddr, imageBase, mfmt, labels, attaches);
                Unknown2 = ByteConverter.ToInt32(file, address + 0x14);
                Unknown3 = ByteConverter.ToInt32(file, address + 0x18);
                break;
            }
        }
Ejemplo n.º 13
0
        public ChunkAttach(byte[] file, int address, uint imageBase, Dictionary <int, string> labels)
            : this()
        {
            if (labels.ContainsKey(address))
            {
                Name = labels[address];
            }
            else
            {
                Name = "attach_" + address.ToString("X8");
            }
            ChunkType ctype;
            int       tmpaddr = ByteConverter.ToInt32(file, address);

            if (tmpaddr != 0)
            {
                tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase);
                Vertex  = new List <VertexChunk>();
                if (labels.ContainsKey(tmpaddr))
                {
                    VertexName = labels[tmpaddr];
                }
                else
                {
                    VertexName = "vertex_" + tmpaddr.ToString("X8");
                }
                ctype = (ChunkType)(ByteConverter.ToUInt32(file, tmpaddr) & 0xFF);
                while (ctype != ChunkType.End)
                {
                    VertexChunk chunk = new VertexChunk(file, tmpaddr);
                    Vertex.Add(chunk);
                    tmpaddr += (chunk.Size * 4) + 4;
                    ctype    = (ChunkType)(ByteConverter.ToUInt32(file, tmpaddr) & 0xFF);
                }
            }
            tmpaddr = ByteConverter.ToInt32(file, address + 4);
            if (tmpaddr != 0)
            {
                tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase);
                Poly    = new List <PolyChunk>();
                if (labels.ContainsKey(tmpaddr))
                {
                    PolyName = labels[tmpaddr];
                }
                else
                {
                    PolyName = "poly_" + tmpaddr.ToString("X8");
                }
                PolyChunk chunk = PolyChunk.Load(file, tmpaddr);
                while (chunk.Type != ChunkType.End)
                {
                    if (chunk.Type != ChunkType.Null)
                    {
                        Poly.Add(chunk);
                    }
                    tmpaddr += chunk.ByteSize;
                    chunk    = PolyChunk.Load(file, tmpaddr);
                }
            }
            Bounds = new BoundingSphere(file, address + 8);
        }
Ejemplo n.º 14
0
        public ModelFile(byte[] file, string filename = null)
        {
            int  tmpaddr;
            bool be = ByteConverter.BigEndian;

            ByteConverter.BigEndian = false;
            ulong magic   = ByteConverter.ToUInt64(file, 0) & FormatMask;
            byte  version = file[7];

            if (version > CurrentVersion)
            {
                throw new FormatException("Not a valid SA1MDL/SA2MDL file.");
            }
            Metadata = new Dictionary <uint, byte[]>();
            Dictionary <int, string> labels   = new Dictionary <int, string>();
            Dictionary <int, Attach> attaches = new Dictionary <int, Attach>();

            if (version < 2)
            {
                if (version == 1)
                {
                    tmpaddr = ByteConverter.ToInt32(file, 0x14);
                    if (tmpaddr != 0)
                    {
                        int addr = ByteConverter.ToInt32(file, tmpaddr);
                        while (addr != -1)
                        {
                            labels.Add(addr, file.GetCString(ByteConverter.ToInt32(file, tmpaddr + 4)));
                            tmpaddr += 8;
                            addr     = ByteConverter.ToInt32(file, tmpaddr);
                        }
                    }
                }
                switch (magic)
                {
                case SA1MDL:
                    Format = ModelFormat.Basic;
                    break;

                case SA2MDL:
                    Format = ModelFormat.Chunk;
                    break;

                default:
                    throw new FormatException("Not a valid SA1MDL/SA2MDL file.");
                }
                Model = new NJS_OBJECT(file, ByteConverter.ToInt32(file, 8), 0, Format, labels, attaches);
                if (filename != null)
                {
                    tmpaddr = ByteConverter.ToInt32(file, 0xC);
                    if (tmpaddr != 0)
                    {
                        List <string> animfiles = new List <string>();
                        int           addr      = ByteConverter.ToInt32(file, tmpaddr);
                        while (addr != -1)
                        {
                            animfiles.Add(file.GetCString(addr));
                            tmpaddr += 4;
                            addr     = ByteConverter.ToInt32(file, tmpaddr);
                        }
                        animationFiles = animfiles.ToArray();
                    }
                    else
                    {
                        animationFiles = new string[0];
                    }
                    string            path  = Path.GetDirectoryName(filename);
                    List <NJS_MOTION> anims = new List <NJS_MOTION>();
                    try
                    {
                        foreach (string item in animationFiles)
                        {
                            anims.Add(NJS_MOTION.Load(Path.Combine(path, item), Model.CountAnimated()));
                        }
                    }
                    catch
                    {
                        anims.Clear();
                    }
                    Animations = anims.AsReadOnly();
                }
            }
            else
            {
                animationFiles = new string[0];
                tmpaddr        = ByteConverter.ToInt32(file, 0xC);
                if (tmpaddr != 0)
                {
                    bool finished = false;
                    while (!finished)
                    {
                        ChunkTypes type      = (ChunkTypes)ByteConverter.ToUInt32(file, tmpaddr);
                        int        chunksz   = ByteConverter.ToInt32(file, tmpaddr + 4);
                        int        nextchunk = tmpaddr + 8 + chunksz;
                        tmpaddr += 8;
                        if (version == 2)
                        {
                            switch (type)
                            {
                            case ChunkTypes.Label:
                                while (ByteConverter.ToInt64(file, tmpaddr) != -1)
                                {
                                    labels.Add(ByteConverter.ToInt32(file, tmpaddr), file.GetCString(ByteConverter.ToInt32(file, tmpaddr + 4)));
                                    tmpaddr += 8;
                                }
                                break;

                            case ChunkTypes.Animation:
                                List <string> animfiles = new List <string>();
                                while (ByteConverter.ToInt32(file, tmpaddr) != -1)
                                {
                                    animfiles.Add(file.GetCString(ByteConverter.ToInt32(file, tmpaddr)));
                                    tmpaddr += 4;
                                }
                                animationFiles = animfiles.ToArray();
                                break;

                            case ChunkTypes.Morph:
                                break;

                            case ChunkTypes.Author:
                                Author = file.GetCString(tmpaddr);
                                break;

                            case ChunkTypes.Tool:
                                break;

                            case ChunkTypes.Description:
                                Description = file.GetCString(tmpaddr);
                                break;

                            case ChunkTypes.Texture:
                                break;

                            case ChunkTypes.End:
                                finished = true;
                                break;
                            }
                        }
                        else
                        {
                            byte[] chunk = new byte[chunksz];
                            Array.Copy(file, tmpaddr, chunk, 0, chunksz);
                            int chunkaddr = 0;
                            switch (type)
                            {
                            case ChunkTypes.Label:
                                while (ByteConverter.ToInt64(chunk, chunkaddr) != -1)
                                {
                                    labels.Add(ByteConverter.ToInt32(chunk, chunkaddr),
                                               chunk.GetCString(ByteConverter.ToInt32(chunk, chunkaddr + 4)));
                                    chunkaddr += 8;
                                }
                                break;

                            case ChunkTypes.Animation:
                                List <string> animchunks = new List <string>();
                                while (ByteConverter.ToInt32(chunk, chunkaddr) != -1)
                                {
                                    animchunks.Add(chunk.GetCString(ByteConverter.ToInt32(chunk, chunkaddr)));
                                    chunkaddr += 4;
                                }
                                animationFiles = animchunks.ToArray();
                                break;

                            case ChunkTypes.Morph:
                                break;

                            case ChunkTypes.Author:
                                Author = chunk.GetCString(chunkaddr);
                                break;

                            case ChunkTypes.Tool:
                                break;

                            case ChunkTypes.Description:
                                Description = chunk.GetCString(chunkaddr);
                                break;

                            case ChunkTypes.End:
                                finished = true;
                                break;

                            default:
                                Metadata.Add((uint)type, chunk);
                                break;
                            }
                        }
                        tmpaddr = nextchunk;
                    }
                }
                switch (magic)
                {
                case SA1MDL:
                    Format = ModelFormat.Basic;
                    break;

                case SA2MDL:
                    Format = ModelFormat.Chunk;
                    break;

                case SA2BMDL:
                    Format = ModelFormat.GC;
                    break;

                default:
                    throw new FormatException("Not a valid SA1MDL/SA2MDL file.");
                }
                Model = new NJS_OBJECT(file, ByteConverter.ToInt32(file, 8), 0, Format, labels, attaches);
                if (filename != null)
                {
                    string            path  = Path.GetDirectoryName(filename);
                    List <NJS_MOTION> anims = new List <NJS_MOTION>();
                    try
                    {
                        foreach (string item in animationFiles)
                        {
                            anims.Add(NJS_MOTION.Load(Path.Combine(path, item), Model.CountAnimated()));
                        }
                    }
                    catch
                    {
                        anims.Clear();
                    }
                    Animations = anims.AsReadOnly();
                }
            }
            ByteConverter.BigEndian = be;
        }
Ejemplo n.º 15
0
        public LandTable(byte[] file, int address, uint imageBase, LandTableFormat format, Dictionary <int, string> labels)
        {
            Format = format;
            if (labels.ContainsKey(address))
            {
                Name = labels[address];
            }
            else
            {
                Name = "landtable_" + address.ToString("X8");
            }
            short colcnt = ByteConverter.ToInt16(file, address);
            Dictionary <int, Attach> attaches = new Dictionary <int, Attach>();

            switch (format)
            {
            case LandTableFormat.SA1:
            case LandTableFormat.SADX:
                short anicnt = ByteConverter.ToInt16(file, address + 2);
                Flags    = ByteConverter.ToInt32(file, address + 4);
                Unknown1 = ByteConverter.ToSingle(file, address + 8);
                COL      = new List <COL>();
                int tmpaddr = ByteConverter.ToInt32(file, address + 0xC);
                if (tmpaddr != 0)
                {
                    tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase);
                    if (labels.ContainsKey(tmpaddr))
                    {
                        COLName = labels[tmpaddr];
                    }
                    else
                    {
                        COLName = "collist_" + tmpaddr.ToString("X8");
                    }
                    for (int i = 0; i < colcnt; i++)
                    {
                        COL.Add(new COL(file, tmpaddr, imageBase, format, labels, attaches));
                        tmpaddr += SAModel.COL.Size(format);
                    }
                }
                else
                {
                    COLName = "collist_" + Extensions.GenerateIdentifier();
                }
                Anim    = new List <GeoAnimData>();
                tmpaddr = ByteConverter.ToInt32(file, address + 0x10);
                if (tmpaddr != 0)
                {
                    tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase);
                    if (labels.ContainsKey(tmpaddr))
                    {
                        AnimName = labels[tmpaddr];
                    }
                    else
                    {
                        AnimName = "animlist_" + tmpaddr.ToString("X8");
                    }
                    for (int i = 0; i < anicnt; i++)
                    {
                        Anim.Add(new GeoAnimData(file, tmpaddr, imageBase, format, labels, attaches));
                        tmpaddr += GeoAnimData.Size;
                    }
                }
                else
                {
                    AnimName = "animlist_" + Extensions.GenerateIdentifier();
                }
                tmpaddr = ByteConverter.ToInt32(file, address + 0x14);
                if (tmpaddr != 0)
                {
                    tmpaddr         = (int)unchecked ((uint)tmpaddr - imageBase);
                    TextureFileName = file.GetCString(tmpaddr, Encoding.ASCII);
                }
                TextureList = ByteConverter.ToUInt32(file, address + 0x18);
                Unknown2    = ByteConverter.ToInt32(file, address + 0x1C);
                Unknown3    = ByteConverter.ToInt32(file, address + 0x20);
                break;

            case LandTableFormat.SA2:
            case LandTableFormat.SA2B:
                short cnkcnt = ByteConverter.ToInt16(file, address + 2);
                Unknown1 = ByteConverter.ToSingle(file, address + 0xC);
                COL      = new List <COL>();
                tmpaddr  = ByteConverter.ToInt32(file, address + 0x10);
                if (tmpaddr != 0)
                {
                    tmpaddr = (int)unchecked ((uint)tmpaddr - imageBase);
                    if (labels.ContainsKey(tmpaddr))
                    {
                        COLName = labels[tmpaddr];
                    }
                    else
                    {
                        COLName = "collist_" + tmpaddr.ToString("X8");
                    }
                    for (int i = 0; i < colcnt; i++)
                    {
                        COL.Add(new COL(file, tmpaddr, imageBase, format, labels, cnkcnt < 0 ? null : (bool?)(i >= cnkcnt), attaches));
                        tmpaddr += SAModel.COL.Size(format);
                    }
                }
                else
                {
                    COLName = "collist_" + Extensions.GenerateIdentifier();
                }
                Anim     = new List <GeoAnimData>();
                AnimName = "animlist_" + Extensions.GenerateIdentifier();
                tmpaddr  = ByteConverter.ToInt32(file, address + 0x18);
                if (tmpaddr != 0)
                {
                    tmpaddr         = (int)unchecked ((uint)tmpaddr - imageBase);
                    TextureFileName = file.GetCString(tmpaddr, Encoding.ASCII);
                }
                TextureList = ByteConverter.ToUInt32(file, address + 0x1C);
                break;
            }
            Metadata = new Dictionary <uint, byte[]>();
        }
Ejemplo n.º 16
0
 static void Main(string[] args)
 {
     if (args.Length == 0)
     {
         Console.Write("Filename: ");
         args = new string[] { Console.ReadLine().Trim('"') };
     }
     foreach (string filename in args)
     {
         Console.WriteLine("Splitting file {0}...", filename);
         byte[] fc;
         if (Path.GetExtension(filename).Equals(".prs", StringComparison.OrdinalIgnoreCase))
         {
             fc = Prs.Decompress(filename);
         }
         else
         {
             fc = File.ReadAllBytes(filename);
         }
         EventIniData ini = new EventIniData()
         {
             Name = Path.GetFileNameWithoutExtension(filename)
         };
         string path = Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(Path.GetFullPath(filename)), Path.GetFileNameWithoutExtension(filename))).FullName;
         uint   key;
         if (fc[0] == 0x81)
         {
             Console.WriteLine("File is in GC/PC format.");
             ByteConverter.BigEndian = true;
             key      = 0x8125FE60;
             ini.Game = Game.SA2B;
         }
         else
         {
             Console.WriteLine("File is in DC format.");
             ByteConverter.BigEndian = false;
             key      = 0xC600000;
             ini.Game = Game.SA2;
         }
         List <string> nodenames = new List <string>();
         Dictionary <string, KeyValuePair <string, NJS_OBJECT> > modelfiles = new Dictionary <string, KeyValuePair <string, NJS_OBJECT> >();
         int ptr = fc.GetPointer(0x20, key);
         if (ptr != 0)
         {
             for (int i = 0; i < (ini.Game == Game.SA2B ? 18 : 16); i++)
             {
                 UpgradeInfo info = new UpgradeInfo();
                 info.RootNode = GetModel(fc, ptr, key, Path.Combine(path, $"Upgrade {i + 1} Root.sa2mdl"), nodenames, modelfiles);
                 if (info.RootNode != null)
                 {
                     int ptr2 = fc.GetPointer(ptr + 4, key);
                     if (ptr2 != 0)
                     {
                         info.AttachNode1 = $"object_{ptr2:X8}";
                     }
                     info.Model1 = GetModel(fc, ptr + 8, key, Path.Combine(path, $"Upgrade {i + 1} Model 1.sa2mdl"), nodenames, modelfiles);
                     ptr2        = fc.GetPointer(ptr + 0xC, key);
                     if (ptr2 != 0)
                     {
                         info.AttachNode2 = $"object_{ptr2:X8}";
                     }
                     info.Model2 = GetModel(fc, ptr + 0x10, key, Path.Combine(path, $"Upgrade {i + 1} Model 2.sa2mdl"), nodenames, modelfiles);
                 }
                 ini.Upgrades.Add(info);
                 ptr += 0x14;
             }
         }
         else
         {
             Console.WriteLine("Event contains no character upgrades.");
         }
         int gcnt = ByteConverter.ToInt32(fc, 8);
         ptr = fc.GetPointer(0, key);
         if (ptr != 0)
         {
             Console.WriteLine("Event contains {0} group(s).", gcnt + 1);
             for (int gn = 0; gn <= gcnt; gn++)
             {
                 GroupInfo info = new GroupInfo();
                 int       ptr2 = fc.GetPointer(ptr, key);
                 int       ecnt = ByteConverter.ToInt32(fc, ptr + 4);
                 if (ptr2 != 0)
                 {
                     Console.WriteLine("Group {0} contains {1} entit{2}.", gn + 1, ecnt, ecnt == 1 ? "y" : "ies");
                     for (int en = 0; en < ecnt; en++)
                     {
                         string name = GetModel(fc, ptr2, key, Path.Combine(path, $"Group {gn + 1} Entity {en + 1} Model.sa2mdl"), nodenames, modelfiles);
                         if (name != null)
                         {
                             info.Entities.Add(name);
                         }
                         ptr2 += ini.Game == Game.SA2B ? 0x2C : 0x20;
                     }
                 }
                 else
                 {
                     Console.WriteLine("Group {0} contains no entities.", gn + 1);
                 }
                 ptr2 = fc.GetPointer(ptr + 0x18, key);
                 if (ptr2 != 0)
                 {
                     info.Big = GetModel(fc, ptr2, key, Path.Combine(path, $"Group {gn + 1} Big Model.sa2mdl"), nodenames, modelfiles);
                 }
                 ini.Groups.Add(info);
                 ptr += 0x20;
             }
         }
         else
         {
             Console.WriteLine("Event contains no groups.");
         }
         ptr = fc.GetPointer(0x18, key);
         if (ptr != 0)
         {
             for (int i = 0; i < 93; i++)
             {
                 string name = GetModel(fc, ptr, key, Path.Combine(path, $"Mech Part {i + 1}.sa2mdl"), nodenames, modelfiles);
                 if (name != null)
                 {
                     ini.MechParts.Add(i, name);
                 }
                 ptr += 4;
             }
         }
         else
         {
             Console.WriteLine("Event contains no mech parts.");
         }
         ptr = fc.GetPointer(0x1C, key);
         if (ptr != 0)
         {
             ini.TailsTails = GetModel(fc, ptr, key, Path.Combine(path, $"Tails tails.sa2mdl"), nodenames, modelfiles);
         }
         else
         {
             Console.WriteLine("Event does not contain Tails' tails.");
         }
         foreach (var item in modelfiles)
         {
             ModelFile.CreateFile(item.Value.Key, item.Value.Value, null, null, null, null, null, ModelFormat.Chunk);
             ini.Files.Add(Path.GetFileName(item.Value.Key), HelperFunctions.FileHash(item.Value.Key));
         }
         IniSerializer.Serialize(ini, Path.Combine(path, Path.ChangeExtension(Path.GetFileName(filename), ".ini")));
     }
 }
Ejemplo n.º 17
0
        public ModelFile(byte[] file, string filename = null)
        {
            int  tmpaddr;
            bool be = ByteConverter.BigEndian;

            ByteConverter.BigEndian = false;
            ulong magic   = ByteConverter.ToUInt64(file, 0) & FormatMask;
            byte  version = file[7];

            if (version > CurrentVersion)
            {
                throw new FormatException("Not a valid SA1MDL/SA2MDL file.");
            }
            Metadata = new Dictionary <uint, byte[]>();
            Dictionary <int, string> labels   = new Dictionary <int, string>();
            Dictionary <int, Attach> attaches = new Dictionary <int, Attach>();

            if (version < 2)
            {
                if (version == 1)
                {
                    tmpaddr = ByteConverter.ToInt32(file, 0x14);
                    if (tmpaddr != 0)
                    {
                        int addr = ByteConverter.ToInt32(file, tmpaddr);
                        while (addr != -1)
                        {
                            labels.Add(addr, file.GetCString(ByteConverter.ToInt32(file, tmpaddr + 4)));
                            tmpaddr += 8;
                            addr     = ByteConverter.ToInt32(file, tmpaddr);
                        }
                    }
                }
                switch (magic)
                {
                case SA1MDL:
                    Format = ModelFormat.Basic;
                    break;

                case SA2MDL:
                    Format = ModelFormat.Chunk;
                    break;

                default:
                    throw new FormatException("Not a valid SA1MDL/SA2MDL file.");
                }
                Model = new NJS_OBJECT(file, ByteConverter.ToInt32(file, 8), 0, Format, labels, attaches);
                if (filename != null)
                {
                    tmpaddr = ByteConverter.ToInt32(file, 0xC);
                    if (tmpaddr != 0)
                    {
                        List <string> animfiles = new List <string>();
                        int           addr      = ByteConverter.ToInt32(file, tmpaddr);
                        while (addr != -1)
                        {
                            animfiles.Add(file.GetCString(addr));
                            tmpaddr += 4;
                            addr     = ByteConverter.ToInt32(file, tmpaddr);
                        }
                        animationFiles = animfiles.ToArray();
                    }
                    else
                    {
                        animationFiles = new string[0];
                    }
                    string            path  = Path.GetDirectoryName(filename);
                    List <NJS_MOTION> anims = new List <NJS_MOTION>();
                    try
                    {
                        foreach (string item in animationFiles)
                        {
                            anims.Add(NJS_MOTION.Load(Path.Combine(path, item), Model.CountAnimated()));
                        }
                    }
                    catch
                    {
                        anims.Clear();
                    }
                    Animations = anims.AsReadOnly();
                }
            }
            else
            {
                animationFiles = new string[0];
                tmpaddr        = ByteConverter.ToInt32(file, 0xC);
                if (tmpaddr != 0)
                {
                    bool finished = false;
                    while (!finished)
                    {
                        ChunkTypes type      = (ChunkTypes)ByteConverter.ToUInt32(file, tmpaddr);
                        int        chunksz   = ByteConverter.ToInt32(file, tmpaddr + 4);
                        int        nextchunk = tmpaddr + 8 + chunksz;
                        tmpaddr += 8;
                        if (version == 2)
                        {
                            switch (type)
                            {
                            case ChunkTypes.Label:
                                while (ByteConverter.ToInt64(file, tmpaddr) != -1)
                                {
                                    labels.Add(ByteConverter.ToInt32(file, tmpaddr), file.GetCString(ByteConverter.ToInt32(file, tmpaddr + 4)));
                                    tmpaddr += 8;
                                }
                                break;

                            case ChunkTypes.Animation:
                                List <string> animfiles = new List <string>();
                                while (ByteConverter.ToInt32(file, tmpaddr) != -1)
                                {
                                    animfiles.Add(file.GetCString(ByteConverter.ToInt32(file, tmpaddr)));
                                    tmpaddr += 4;
                                }
                                animationFiles = animfiles.ToArray();
                                break;

                            case ChunkTypes.Morph:
                                break;

                            case ChunkTypes.Author:
                                Author = file.GetCString(tmpaddr);
                                break;

                            case ChunkTypes.Tool:
                                break;

                            case ChunkTypes.Description:
                                Description = file.GetCString(tmpaddr);
                                break;

                            case ChunkTypes.Texture:
                                break;

                            case ChunkTypes.End:
                                finished = true;
                                break;
                            }
                        }
                        else
                        {
                            byte[] chunk = new byte[chunksz];
                            Array.Copy(file, tmpaddr, chunk, 0, chunksz);
                            int chunkaddr = 0;
                            switch (type)
                            {
                            case ChunkTypes.Label:
                                while (ByteConverter.ToInt64(chunk, chunkaddr) != -1)
                                {
                                    labels.Add(ByteConverter.ToInt32(chunk, chunkaddr),
                                               chunk.GetCString(ByteConverter.ToInt32(chunk, chunkaddr + 4)));
                                    chunkaddr += 8;
                                }
                                break;

                            case ChunkTypes.Animation:
                                List <string> animchunks = new List <string>();
                                while (ByteConverter.ToInt32(chunk, chunkaddr) != -1)
                                {
                                    animchunks.Add(chunk.GetCString(ByteConverter.ToInt32(chunk, chunkaddr)));
                                    chunkaddr += 4;
                                }
                                animationFiles = animchunks.ToArray();
                                break;

                            case ChunkTypes.Morph:
                                break;

                            case ChunkTypes.Author:
                                Author = chunk.GetCString(chunkaddr);
                                break;

                            case ChunkTypes.Tool:
                                break;

                            case ChunkTypes.Description:
                                Description = chunk.GetCString(chunkaddr);
                                break;

                            case ChunkTypes.End:
                                finished = true;
                                break;

                            default:
                                Metadata.Add((uint)type, chunk);
                                break;
                            }
                        }
                        tmpaddr = nextchunk;
                    }
                }
                switch (magic)
                {
                case SA1MDL:
                    Format = ModelFormat.Basic;
                    break;

                case SA2MDL:
                    Format = ModelFormat.Chunk;
                    break;

                case SA2BMDL:
                    Format = ModelFormat.GC;
                    break;

                default:
                    throw new FormatException("Not a valid SA1MDL/SA2MDL file.");
                }
                Model = new NJS_OBJECT(file, ByteConverter.ToInt32(file, 8), 0, Format, labels, attaches);
                if (filename != null)
                {
                    string path = Path.GetDirectoryName(filename);
                    if (File.Exists(Path.GetFileNameWithoutExtension(filename) + ".action"))
                    {
                        using (TextReader tr = File.OpenText(Path.GetFileNameWithoutExtension(filename) + ".action"))
                        {
                            List <string> animlist = new List <string>();
                            int           count    = File.ReadLines(Path.GetFileNameWithoutExtension(filename) + ".action").Count();
                            for (int i = 0; i < count; i++)
                            {
                                string line = tr.ReadLine();
                                if (File.Exists(Path.Combine(path, line)))
                                {
                                    animlist.Add(line);
                                }
                            }
                            animationFiles = animlist.ToArray();
                        }
                    }
                    List <NJS_MOTION> anims = new List <NJS_MOTION>();
                    try
                    {
                        foreach (string item in animationFiles)
                        {
                            if (Path.GetExtension(item).ToLowerInvariant() == ".json")
                            {
                                JsonSerializer js = new JsonSerializer()
                                {
                                    Culture = System.Globalization.CultureInfo.InvariantCulture
                                };
                                using (TextReader tr = File.OpenText(Path.Combine(path, item)))
                                {
                                    using (JsonTextReader jtr = new JsonTextReader(tr))
                                        anims.Add(js.Deserialize <NJS_MOTION>(jtr));
                                }
                            }
                            else
                            {
                                anims.Add(NJS_MOTION.Load(Path.Combine(path, item), Model.CountAnimated()));
                            }
                        }
                    }
                    catch
                    {
                        anims.Clear();
                    }
                    Animations = anims.AsReadOnly();
                }
            }
            ByteConverter.BigEndian = be;
        }