Exemple #1
0
        public override void Read(DataStream strIn)
        {
            DataStream data;

            // Simple NOT encoding
            if (isEncoded) {
                data = new DataStream(new System.IO.MemoryStream(), 0, 0);
                Codec(strIn, data);	// Decode strIn to data
            } else {
                data = strIn;
            }

            data.Seek(0, SeekMode.Origin);
            var reader = new DataReader(data, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            int blockSize = textSize + dataSize;
            int numBlocks = hasNumBlock ? reader.ReadUInt16() : (int)(data.Length / blockSize);

            blocks = new Block[numBlocks];
            for (int i = 0; i < numBlocks; i++)
                blocks[i] = new Block(
                    reader.ReadString(textSize).ApplyTable("replace", false),
                    reader.ReadBytes(dataSize)
                );

            // Free the decoded data
            if (isEncoded)
                data.Dispose();
        }
Exemple #2
0
        private static void TestNdsRomWrite(string romPath)
        {
            DataStream outStream = new DataStream(new MemoryStream(), 0, 0);
            DataStream romStream = new DataStream(romPath, FileMode.Open, FileAccess.Read);
            Format romFormat = FileManager.GetFormat("Rom");

            GameFile rom = new GameFile(Path.GetFileName(romPath), romStream, romFormat);
            romFormat.Initialize(rom);

            DateTime t1 = DateTime.Now;
            romFormat.Read();
            DateTime t2 = DateTime.Now;
            romFormat.Write(outStream);
            DateTime t3 = DateTime.Now;
            outStream.WriteTo("/lab/nds/test.nds");
            DateTime t4 = DateTime.Now;

            outStream.Dispose();
            romStream.Dispose();

            // Display time result
            Console.WriteLine("Time results:");
            Console.WriteLine("\tRead                    -> {0}", t2 - t1);
            Console.WriteLine("\tWrite into MemoryStream -> {0}", t3 - t2);
            Console.WriteLine("\tWrite into FileStream   -> {0}", t4 - t3);
        }
Exemple #3
0
        public override void Read(DataStream strIn)
        {
            DataReader reader = new DataReader(strIn, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));
            this.id = reader.ReadUInt32();

            this.startBlocks = new string[4];
            for (int i = 0; i < this.startBlocks.Length; i++) {
                ushort textSize = reader.ReadUInt16();
                this.startBlocks[i] = reader.ReadString(textSize).ApplyTable("replace", false);
            }

            this.unknown = reader.ReadBytes(0x0D);

            byte numEndBlocks = reader.ReadByte();
            this.endBlocks = new string[numEndBlocks];
            for (int i = 0; i < this.endBlocks.Length; i++) {
                ushort size = reader.ReadUInt16();
                this.endBlocks[i] = reader.ReadString(size).ApplyTable("replace", false);
            }

            byte numUnknown = reader.ReadByte();
            this.unknown2 = new byte[numUnknown][];
            for (int i = 0; i < this.unknown2.Length; i++) {
                byte dataSize = reader.ReadByte();
                this.unknown2[i] = reader.ReadBytes(dataSize + 4);
            }
        }
		void WriteChunk(short[,] samples, int channel, DataStream strOut)
		{
			var writer = new DataWriter(strOut);

			byte scale;
			byte coefIdx;
			byte[] values = SearchBestParameters(samples, channel, out scale, out coefIdx);

			uint currentWord = 0;
			for (int i = 0; i < Format.SamplesPerChunk; i++) {
				if (i != 0 && i % 8 == 0) {
					currentWord ^= 0x80808080;
					writer.Write(currentWord);
					currentWord = 0;
				}

				byte val = (i >= values.Length) ? (byte)0 : (byte)(values[i] & 0xF);
				currentWord |= (uint)(val << (i * 4));
			}

			// Set the header
			byte header = (byte)((coefIdx << 4) | scale);
			currentWord |= (uint)(header << 24);

			// Write the last word
			currentWord ^= 0x80808080;
			writer.Write(currentWord);
		}
		public void Run(DataStream strOut)
		{
			if (ProgressNotifier != null)
				ProgressNotifier.Reset();

			for (int i = 0; i < Format.Channels; i++) {
				Format.HistoricalValues[i, 0] = 0;
				Format.HistoricalValues[i, 1] = 0;
			}

			int samplesEncoded = 0;
			var samples = new SampleEnumerator(Samples);
			foreach (var sampleBlock in samples.Get(Format.SamplesPerChunk)) {
				// Mix chunk channels
				for (int i = 0; i < Format.Channels; i++)
					WriteChunk(sampleBlock, i, strOut);

				samplesEncoded += sampleBlock.GetLength(0);
				if (ProgressNotifier != null)
					ProgressNotifier.Update(samplesEncoded, SamplesToEncode);
			}

			if (ProgressNotifier != null)
				ProgressNotifier.End();
		}
Exemple #6
0
        public override void Read(DataStream strIn)
        {
            this.entries = new List<SubtitleEntry>();

            // Gets all the lines
            DataReader reader = new DataReader(strIn, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));
            string[] lines = reader.ReadString().Split(ExtraSplit);

            for (int i = 0; i < lines.Length; i++) {
                SubtitleEntry entry = new SubtitleEntry();

                if (string.IsNullOrEmpty(lines[i]) || lines[i] == "\n")
                    continue;

                string line = lines[i].Substring(1);
                if (line[0] == '#') {
                    entry.Type = SubtitleType.Comment;
                    entry.Data = line.Substring(1).ApplyTable("replace", false);
                } else if (line.StartsWith("/stream", StringComparison.Ordinal)) {
                    entry.Type = SubtitleType.Voice;
                    entry.Data = line.Substring(8);
                } else if (line.StartsWith("/sync", StringComparison.Ordinal)) {
                    entry.Type = SubtitleType.SyncTime;
                    entry.Data = line.Substring(6);
                } else if (line.StartsWith("/clear", StringComparison.Ordinal)) {
                    entry.Type = SubtitleType.Clear;
                } else {
                    entry.Type = SubtitleType.Text;
                    entry.Data = line.ApplyTable("replace", false);
                }

                this.entries.Add(entry);
            }
        }
Exemple #7
0
        public override void Write(DataStream strOut)
        {
            DataWriter writer = new DataWriter(strOut);
            writer.Write(this.type);

            foreach (Block b in this.blocks)
                b.Write(strOut);
        }
Exemple #8
0
 protected override void Dispose(bool freeManagedResourcesAlso)
 {
     if (freeManagedResourcesAlso) {
         if (this.data != null) {
             this.data.Dispose();
             this.data = null;
         }
     }
 }
Exemple #9
0
        public override void Read(DataStream strIn)
        {
            DataReader reader = new DataReader(strIn);
            this.type = reader.ReadUInt32();

            this.blocks = new Block[3];
            this.blocks[0] = Block.FromStream(strIn, typeof(Entry1));
            this.blocks[1] = Block.FromStream(strIn, typeof(Entry2));
            this.blocks[2] = Block.FromStream(strIn, typeof(Entry3));
        }
Exemple #10
0
        public override void Write(DataStream strOut)
        {
            DataWriter writer = new DataWriter(strOut, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            writer.Write((ushort)this.entries.Length);
            foreach (Entry e in this.entries) {
                writer.Write(e.Id);
                writer.Write(e.Text, typeof(ushort), "replace", true);
            }
        }
Exemple #11
0
        public override void Write(DataStream strOut)
        {
            DataWriter writer = new DataWriter(strOut, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            writer.Write((ushort)this.text.Length);
            for (int i = 0; i < this.text.Length; i++) {
                string text = this.text[i].Replace("\n", "\\n");
                text = text.ApplyTable("replace", true);
                writer.Write(text, 0x80);
            }
        }
Exemple #12
0
        public override void Write(DataStream strOut)
        {
            DataWriter writer = new DataWriter(strOut, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            writer.Write((ushort)this.entries.Length);
            foreach (Entry e in this.entries) {
                writer.Write(e.Unknown1);
                writer.Write(e.Unknown2);
                writer.Write(e.Text.ApplyTable("replace", true), 0x30);
            }
        }
		public void Run(DataStream strOut)
		{
			var writer = new DataWriter(strOut);

			// For each block of samples
			foreach (var sample in Samples) {
				// Mix channels
				for (int i = 0; i < sample.GetLength(0); i++)
					for (int c = 0; c < sample.GetLength(1); c++)
						writer.Write(sample[i, c]);
			}
		}
        public static GameFolder FromPath(string dir, string dirName)
        {
            GameFolder folder = new GameFolder(dirName);

            foreach (string filePath in Directory.GetFiles(dir)) {
                string filename = Path.GetFileName(filePath);
                DataStream stream = new DataStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                folder.AddFile(new GameFile(filename, stream));
            }

            return folder;
        }
Exemple #15
0
        public override void Read(DataStream strIn)
        {
            DataReader reader = new DataReader(strIn, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            ushort numEntries = reader.ReadUInt16();
            this.entries = new Entry[numEntries];
            for (int i = 0; i < numEntries; i++) {
                this.entries[i] = new Entry();
                this.entries[i].Id   = reader.ReadUInt32();
                this.entries[i].Text = reader.ReadString(typeof(ushort), "replace", false);
            }
        }
Exemple #16
0
        public override void Read(DataStream strIn)
        {
            DataReader reader = new DataReader(strIn, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            uint numBlocks = reader.ReadUInt32();
            this.entries = new Entry[numBlocks];
            for (int i = 0; i < numBlocks; i++) {
                uint idx     = reader.ReadUInt32();
                int textSize = reader.ReadInt32();
                string text  = reader.ReadString(textSize);
                this.entries[i] = new Entry(idx, text.ApplyTable("replace", false));
            }
        }
Exemple #17
0
        public override void Read(DataStream strIn)
        {
            DataReader reader = new DataReader(strIn, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            ushort numBlock = reader.ReadUInt16();
            this.entries = new Entry[numBlock];
            for (int i = 0; i < numBlock; i++)
                this.entries[i] = new Entry(
                    reader.ReadUInt16(),
                    reader.ReadUInt16(),
                    reader.ReadString(0x30).ApplyTable("replace", false)
                );
        }
Exemple #18
0
        public override void Read(DataStream strIn)
        {
            DataReader reader = new DataReader(strIn, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            ushort num_block = reader.ReadUInt16();
            this.text = new string[num_block];

            for (int i = 0; i < num_block; i++) {
                this.text[i] = reader.ReadString(0x80);
                this.text[i] = this.text[i].Replace("\\n", "\n");
                this.text[i] = this.text[i].ApplyTable("replace", false);
            }
        }
Exemple #19
0
        public override void Write(DataStream strOut)
        {
            DataWriter writer = new DataWriter(strOut, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            writer.Write((ushort)this.blocks.Length);
            foreach (Block b in this.blocks) {
                writer.Write(this.GetBlockSize(b));
                writer.Write(b.Id);

                foreach (string t in b.Elements)
                    writer.Write(t, typeof(ushort), "replace", true);

                writer.Write((byte)0x00);
            }
        }
Exemple #20
0
        public override void Write(DataStream strOut)
        {
            Encoding encoding = Encoding.GetEncoding("shift_jis");
            DataWriter writer = new DataWriter(strOut, EndiannessMode.LittleEndian, encoding);

            writer.Write((uint)this.entries.Length);
            foreach (Entry e in this.entries) {
                string text = e.Text.ApplyTable("replace", true);
                byte[] data = encoding.GetBytes(text);

                writer.Write(e.Index);
                writer.Write((uint)data.Length);
                writer.Write(data);
            }
        }
		public static void Main(string[] args)
		{
			if (args.Length != 2)
				return;

			var stream = new DataStream(args[0], FileMode.Open, FileAccess.Read);
			var file   = new GameFile(Path.GetFileName(args[0]), stream);
			file.SetFormat(typeof(Sadl));
			file.Format.Read();
			((Sadl)file.Format).Decoder.ProgressNotifier = new ConsoleProgressNotifier("");

			PrintInfo((Sadl)file.Format);

			file.Format.Export(args[1]);
		}
Exemple #22
0
        /// <summary>
        /// Read a banner from a stream.
        /// </summary>
        /// <param name="str">Stream to read from.</param>
        public override void Read(DataStream str)
        {
            DataReader dr = new DataReader(str, EndiannessMode.LittleEndian, Encoding.Unicode);

            this.version  = dr.ReadUInt16();
            this.crc16    = dr.ReadUInt16();
            this.crc16v2  = dr.ReadUInt16();
            this.reserved = dr.ReadBytes(0x1A);
            this.tileData = dr.ReadBytes(0x200);
            this.palette  = dr.ReadBytes(0x20);
            this.japaneseTitle = dr.ReadString(0x100);
            this.englishTitle  = dr.ReadString(0x100);
            this.frenchTitle   = dr.ReadString(0x100);
            this.germanTitle   = dr.ReadString(0x100);
            this.italianTitle  = dr.ReadString(0x100);
            this.spanishTitle  = dr.ReadString(0x100);
        }
Exemple #23
0
        /// <summary>
        /// Calculates CRC16 from the stream.
        /// Code from "blz.c" (NDS Compressors) by CUE
        /// </summary>
        /// <param name="data">Data to calculate the checksum</param>
        public static ushort Run(DataStream data, uint length)
        {
            ushort crc;
            uint   nbits;

            crc = 0xFFFF;
            while (length-- != 0) {
                crc ^= data.ReadByte();
                nbits = 8;
                while (nbits-- != 0) {
                    if ((crc & 1) != 0) { crc = (ushort)((crc >> 1) ^ 0xA001); }
                    else           	      crc = (ushort)(crc >> 1);
                }
            }

            return crc;
        }
Exemple #24
0
        public override void Read(DataStream strIn)
        {
            DataReader reader = new DataReader(strIn, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            ushort numBlocks = reader.ReadUInt16();
            this.blocks = new Block[numBlocks];
            for (int i = 0; i < numBlocks; i++) {
                reader.ReadUInt16();	// Block size

                this.blocks[i]    = new Block();
                this.blocks[i].Id = reader.ReadUInt32();
                this.blocks[i].Elements = new string[3];
                for (int j = 0; j < 3; j++)
                    this.blocks[i].Elements[j] = reader.ReadString(typeof(ushort), "replace", false);

                reader.ReadByte();	// 0x00
            }
        }
Exemple #25
0
        /// <summary>
        /// Create a folder with all the overlays of the same ARM.
        /// </summary>
        /// <param name="str">Stream to read the overlay table.</param>
        /// <param name="header">Header of the current ROM.</param>
        /// <param name="isArm9">If must read overlays from the ARM9 or ARM7.</param>
        /// <param name="listFiles">List with all the files in the ROM.</param>
        /// <returns>Folder with overlays.</returns>
        public static OverlayFolder FromTable(DataStream str, RomHeader header, bool isArm9, GameFile[] listFiles)
        {
            OverlayFolder overlays = new OverlayFolder(isArm9);

            int numOverlays;
            if (isArm9) {
                str.Seek(header.Ov9TableOffset, SeekMode.Origin);
                numOverlays = (int)(header.Ov9TableSize / OverlayFile.TableEntrySize);
            } else {
                str.Seek(header.Ov7TableOffset, SeekMode.Origin);
                numOverlays = (int)(header.Ov7TableSize / OverlayFile.TableEntrySize);
            }

            for (int i = 0; i < numOverlays; i++)
                overlays.AddFile(OverlayFile.FromTable(str, isArm9, listFiles));

            return overlays;
        }
Exemple #26
0
        public override void Read(DataStream strIn)
        {
            DataStream temp = new DataStream(new System.IO.MemoryStream(), 0, 0);
            strIn.WriteTo(temp);
            temp.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);

            Configuration config = Configuration.GetInstance();

            script = new NinoScritor.Script(
                temp.BaseStream,
                scriptName,
                System.IO.Path.Combine(config.AppPath, "temp_names.xml"),
                System.IO.Path.Combine(config.AppPath, "temp_context.xml"),
                System.IO.Path.Combine(config.AppPath, "temp_replace.xml"),
                ""
            );

            temp.Dispose();
        }
Exemple #27
0
        private static void TestNdsRomRead(string romPath, string filePath, string outPath)
        {
            DataStream romStream = new DataStream(romPath, FileMode.Open, FileAccess.Read);
            Format romFormat = FileManager.GetFormat("Rom");

            GameFolder main = new GameFolder("main");
            GameFile rom  = new GameFile(Path.GetFileName(romPath), romStream, romFormat);
            main.AddFile(rom);
            romFormat.Initialize(rom);

            XDocument xmlGame = new XDocument();	// TODO: Replace with ExampleGame.xml
            xmlGame.Add(new XElement("GameInfo", new XElement("Files")));
            FileManager.Initialize(main, FileInfoCollection.FromXml(xmlGame));

            GameFile file = FileManager.GetInstance().RescueFile(filePath);
            if (file != null)
                file.Stream.WriteTo(outPath);

            romStream.Dispose();
        }
Exemple #28
0
        public override void Read(DataStream strIn)
        {
            DataReader reader = new DataReader(strIn, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            ushort numBlocks = reader.ReadUInt16();
            reader.ReadUInt16();	// Total number of subblocks
            this.entries = new Entry[numBlocks];

            for (int i = 0; i < numBlocks; i++) {
                this.entries[i] = new Entry();
                this.entries[i].Title = reader.ReadString(0x40).ApplyTable("replace", false);

                ushort subBlocks = reader.ReadUInt16();
                this.entries[i].Dungeons = new Dungeon[subBlocks];
                for (int j = 0; j < subBlocks; j++)
                    this.entries[i].Dungeons[j] = new Dungeon() {
                        Text       = reader.ReadString(0x40).ApplyTable("replace", false),
                        ScriptName = reader.ReadString(0x08)
                    };
            }
        }
Exemple #29
0
        public override void Write(DataStream strOut)
        {
            DataWriter writer = new DataWriter(strOut, EndiannessMode.LittleEndian, Encoding.GetEncoding("shift_jis"));

            int numSubBlocks = 0;
            foreach (Entry e in this.entries)
                numSubBlocks += e.Dungeons.Length;

            writer.Write((ushort)this.entries.Length);
            writer.Write((ushort)numSubBlocks);

            foreach (Entry e in this.entries) {
                writer.Write(e.Title.ApplyTable("replace", true), 0x40);
                writer.Write((ushort)e.Dungeons.Length);

                foreach (Dungeon d in e.Dungeons) {
                    writer.Write(d.Text.ApplyTable("replace", true), 0x40);
                    writer.Write(d.ScriptName.ApplyTable("replace", true), 0x08);
                }
            }
        }
Exemple #30
0
 public void WriteFiles(DataStream strOut)
 {
     // Write every file
     foreach (GameFile file in this.files) {
         file.Stream.WriteTo(strOut);
         strOut.WritePadding(FileSystem.PaddingByte, FileSystem.PaddingAddress);
     }
 }
Exemple #31
0
 public void WriteTo(string fileOut)
 {
     using (DataStream stream = new DataStream(fileOut, FileMode.Create, FileAccess.Write))
         this.WriteTo(stream);
 }
Exemple #32
0
 public void WriteTo(DataStream stream)
 {
     this.WriteTo(stream, this.Length);
 }
Exemple #33
0
 public DataStream(DataStream stream, long offset, long length)
     : this(stream.BaseStream, offset + stream.Offset, length)
 {
 }