Exemplo n.º 1
0
        public async Task TestEachLine()
        {
            var garcGameText = await ORASConfig.GameConfig.GetGarc(GarcNames.GameText);

            byte[] tf0Data = await garcGameText.GetFile(0);

            TextFile tf0 = await ORASConfig.GameConfig.GetTextFile(0);

            byte[] textData = tf0Data.Skip(0x10 /* SectionDataOffset */).ToArray();
            ushort key      = TextFileHelper.KeyBase;

            for (int i = 0; i < tf0.Lines.Count; i++)
            {
                TestContext.Progress.WriteLine($"Testing line {i}...");
                TextFile.LineInfo lineInfo = tf0.LineInfos[i];
                byte[]            lineData = textData.Skip((int)lineInfo.Offset).Take(lineInfo.Length * 2).ToArray();

                if (i < tf0.Lines.Count - 1)
                {
                    TextFile.LineInfo next = tf0.LineInfos[i + 1];
                    long padding           = next.Offset - (lineInfo.Offset + lineInfo.Length * 2);
                    TestContext.Progress.WriteLine($"               Length: {lineInfo.Length * 2}");
                    TestContext.Progress.WriteLine($"              Padding: {padding}");
                    TestContext.Progress.WriteLine($"                 Flag: {lineInfo.Flag}");
                    TestContext.Progress.WriteLine($"    Length w/ padding: {lineInfo.Length * 2 + padding}");
                    TestContext.Progress.WriteLine($"         Char Len % 2: {lineInfo.Length % 2}");
                    TestContext.Progress.WriteLine($"       Len w/ pad % 2: {( lineInfo.Length + ( padding / 2 ) ) % 2}");
                }

                string tfhString = TextFileHelper.GetLineString(tf0, TextFileHelper.CryptLineData(lineData, key));

                Assert.AreEqual(tf0.Lines[i], tfhString, "Directly decoded string not equal");

                var    newData = TextFileHelper.GetLineData(tf0, tf0.Lines[i]);
                byte[] tfhData = TextFileHelper.CryptLineData(newData, key);

                if (lineData.Length == tfhData.Length + 2 &&
                    lineData[lineData.Length - 1] == 0 &&
                    lineData[lineData.Length - 2] == 0)
                {
                    tfhData = tfhData.Concat(new byte[] { 0, 0 }).ToArray();                       // add padding
                }
                Assert.AreEqual(lineData, tfhData, "Directly encoded string not equal");

                key += TextFileHelper.KeyAdvance;
            }
        }
Exemplo n.º 2
0
        public async Task TestAllLines()
        {
            var garcGameText = await ORASConfig.GameConfig.GetGarc(GarcNames.GameText);

            byte[] tf0Data = await garcGameText.GetFile(0);

            TextFile tf0 = await ORASConfig.GameConfig.GetTextFile(0);

            byte[][] encData = TextFileHelper.EncryptLines(tf0, tf0.Lines).Select(el => el.Item2).ToArray();
            byte[][] oriData = new byte[tf0.Lines.Count][];

            Assert.AreEqual(tf0.Lines.Count, encData.Length, "Number of data blobs returned does not equal number of lines");

            for (int l = 0; l < encData.Length; l++)
            {
                byte[]            testData = encData[l];
                TextFile.LineInfo lineInfo = tf0.LineInfos[l];
                oriData[l] = tf0Data.Skip((int)lineInfo.Offset + 0x10).Take(lineInfo.Length * 2).ToArray();

                if (testData.Length == oriData[l].Length + 2 &&
                    testData[testData.Length - 1] == 0 &&
                    testData[testData.Length - 2] == 0)
                {
                    testData = testData.Take(testData.Length - 2).ToArray();                       // don't compare the padding thing
                }
                Assert.AreEqual(oriData[l], testData, $"Blob for line {l} does not match original file");
            }

            string[] tfhText = TextFileHelper.DecryptLines(tf0, oriData);

            Assert.AreEqual(tf0.Lines.Count, tfhText.Length, "Number of strings returned does not equal number of lines");

            for (int l = 0; l < tfhText.Length; l++)
            {
                Assert.AreEqual(tf0.Lines[l], tfhText[l], $"Line {l} does not match original file");
            }
        }
Exemplo n.º 3
0
        public async Task DiffGameText()
        {
            using (var fs = new FileStream(Path.Combine(this.path, "GameText.diff.txt"), FileMode.Create, FileAccess.Write, FileShare.None))
                using (var sw = new StreamWriter(fs))
                {
                    async Task WriteLine(string line = "")
                    {
                        TestContext.Progress.WriteLine(line);
                        await sw.WriteLineAsync(line);
                    }

                    var cfg      = ORASConfig.GameConfig;
                    var garcRef  = ORASConfig.GameConfig.GetGarcReference(GarcNames.GameText);
                    var origPath = Path.Combine(this.path, $"{garcRef.RomFsPath}.orig");
                    var newPath  = Path.Combine(this.path, garcRef.RomFsPath);
                    var origGarc = await GarcFile.FromFile(origPath);

                    var newGarc = await GarcFile.FromFile(newPath);

                    Assert.AreEqual(origGarc.FileCount, newGarc.FileCount, "Re-written GameText GARC has different file count than original");

                    for (int file = 0; file < origGarc.FileCount; file++)
                    {
                        await WriteLine($"File #{file} {"-".Repeat( 50 )}");

                        TextFile origFile = new TextFile(cfg.Version, cfg.Variables);
                        TextFile newFile  = new TextFile(cfg.Version, cfg.Variables);

                        origFile.Read(await origGarc.GetFile(file));
                        newFile.Read(await newGarc.GetFile(file));

                        Assert.AreEqual(origFile.LineCount, newFile.LineCount, $"File {file} has different number of lines than original");

                        for (int line = 0; line < origFile.LineCount; line++)
                        {
                            TextFile.LineInfo origInfo = origFile.LineInfos[line];
                            TextFile.LineInfo newInfo  = newFile.LineInfos[line];
                            string            origLine = origFile.Lines[line];
                            string            newLine  = newFile.Lines[line];
                            bool dLength = origInfo.Length != newInfo.Length;
                            bool dOffset = origInfo.Offset != newInfo.Offset;
                            bool dFlag   = origInfo.Flag != newInfo.Flag;
                            bool dLine   = origLine != newLine;
                            bool diff    = dLength || dOffset || dFlag || dLine;

                            if (diff)
                            {
                                await WriteLine($"[DIFF] line #{line}");

                                if (dLine)
                                {
                                    await WriteLine($"  Line Difference");
                                    await WriteLine($"    Original: {origLine}");
                                    await WriteLine($"         New: {newLine}");
                                }

                                if (dLength)
                                {
                                    await WriteLine($"  Length Difference");
                                    await WriteLine($"    Original: {origInfo.Length}");
                                    await WriteLine($"         New: {newInfo.Length}");
                                }

                                if (dOffset)
                                {
                                    await WriteLine($"  Offset Difference");
                                    await WriteLine($"    Original: {origInfo.Offset}");
                                    await WriteLine($"         New: {newInfo.Offset}");
                                }

                                if (dFlag)
                                {
                                    await WriteLine($"  Flag Difference");
                                    await WriteLine($"    Original: {origInfo.Flag}");
                                    await WriteLine($"         New: {newInfo.Flag}");
                                }
                            }
                            else
                            {
                                // don't bother spamming test output with this
                                await sw.WriteLineAsync($"[SAME] line #{line}");
                            }
                        }
                    }
                }
        }