Ejemplo n.º 1
0
        /// <summary>
        /// Writes the DLCSong to disk within the given directory.
        /// For example given a song called "custom" and a directory called J:\customs,
        /// you'll end up with J:\customs\custom\custom.mogg, J:\customs\custom\custom.rbsong, etc
        /// </summary>
        /// <param name="song">The song to write</param>
        /// <param name="dir">The parent directory of the song directory</param>
        public static void WriteDLCSong(DLCSong song, string dir)
        {
            var shortname = song.SongData.Shortname;
            var songPath  = Path.Combine(dir, "songs", shortname);

            Directory.CreateDirectory(songPath);
            using (var lipsyncFile = File.OpenWrite(Path.Combine(songPath, $"{shortname}.lipsync_ps4")))
            {
                new LipsyncWriter(lipsyncFile).WriteStream(song.Lipsync);
            }
            using (var mogg = File.OpenWrite(Path.Combine(songPath, $"{shortname}.mogg")))
                using (var conMogg = song.Mogg.GetStream())
                {
                    conMogg.CopyTo(mogg);
                }
            File.WriteAllText(Path.Combine(songPath, $"{shortname}.mogg.dta"), song.MoggDta.ToFileString());
            File.WriteAllText(Path.Combine(songPath, shortname + ".moggsong"), song.MoggSong.ToFileString());
            using (var rbmid = File.OpenWrite(Path.Combine(songPath, $"{shortname}.rbmid_ps4")))
                RBMidWriter.WriteStream(song.RBMidi, rbmid);
            using (var rbsongFile = File.OpenWrite(Path.Combine(songPath, $"{shortname}.rbsong")))
                new RBSongWriter(rbsongFile).WriteStream(song.RBSong);
            using (var songdtaFile = File.OpenWrite(Path.Combine(songPath, $"{shortname}.songdta_ps4")))
                SongDataWriter.WriteStream(song.SongData, songdtaFile);
            if (song.SongData.AlbumArt)
            {
                using (var artFile = File.OpenWrite(Path.Combine(songPath, $"{shortname}.png_ps4")))
                    Texture.TextureWriter.WriteStream(song.Artwork, artFile);
            }
        }
Ejemplo n.º 2
0
        public static void DLCSongToFsFiles(DLCSong song, FSDir songsDir)
        {
            var shortname = song.SongData.Shortname;
            var songDir   = new FSDir()
            {
                name = shortname, Parent = songsDir
            };

            songsDir.Dirs.Add(songDir);
            songDir.Files.Add(WriterToFile(
                                  $"{shortname}.lipsync_ps4",
                                  s => new LipsyncWriter(s).WriteStream(song.Lipsync)));
            songDir.Files.Add(new FSFile(
                                  s => { using (var mogg = song.Mogg.GetStream()) mogg.CopyTo(s); },
                                  $"{shortname}.mogg",
                                  song.Mogg.Size));
            var moggFileString = Encoding.UTF8.GetBytes(song.MoggDta.ToFileString());

            songDir.Files.Add(new FSFile(
                                  s => s.Write(moggFileString, 0, moggFileString.Length),
                                  $"{shortname}.mogg.dta",
                                  moggFileString.Length));
            var moggSongFileString = Encoding.UTF8.GetBytes(song.MoggSong.ToFileString());

            songDir.Files.Add(new FSFile(
                                  s => s.Write(moggSongFileString, 0, moggSongFileString.Length),
                                  $"{shortname}.moggsong",
                                  moggSongFileString.Length));
            songDir.Files.Add(WriterToFile(
                                  $"{shortname}.rbmid_ps4",
                                  s => RBMidWriter.WriteStream(song.RBMidi, s)));
            songDir.Files.Add(WriterToFile(
                                  $"{shortname}.rbsong",
                                  s => new RBSongWriter(s).WriteStream(song.RBSong)));
            songDir.Files.Add(WriterToFile(
                                  $"{shortname}.songdta_ps4",
                                  s => SongDataWriter.WriteStream(song.SongData, s)));
            if (song.SongData.AlbumArt)
            {
                songDir.Files.Add(WriterToFile(
                                      $"{shortname}.png_ps4",
                                      s => Texture.TextureWriter.WriteStream(song.Artwork, s)));
            }
            foreach (var f in songDir.Files)
            {
                f.Parent = songDir;
            }
        }
Ejemplo n.º 3
0
        public void TestRBMidiFileRoundTrip()
        {
            RBMid rbmid = GetMid("hopos.mid");

            try
            {
                using (var ms = new MemoryStream())
                {
                    RBMidWriter.WriteStream(rbmid, ms);
                    ms.Position = 0;
                    var rbmid2 = new RBMidReader(ms).Read();
                    var diff   = rbmid.Compare(rbmid2);
                    Assert.AreEqual(null, diff, "RBMid differed after round-trip at element " + diff);
                }
            }
            catch (Exception e)
            {
                Assert.Fail("Exception occurred writing or reading an RBMid: " + e.Message);
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Usage();
                return;
            }
            void WithIO(Action <Stream, Stream> action)
            {
                using (var fi = File.OpenRead(args[1]))
                    using (var fo = File.OpenWrite(args[2]))
                        action(fi, fo);
            }

            var makepkg = true;

            switch (args[0])
            {
            case "rbmid2mid":
                WithIO((fi, fo) =>
                {
                    var rbmid = RBMidReader.ReadStream(fi);
                    var midi  = RBMidConverter.ToMid(rbmid);
                    MidiCS.MidiFileWriter.WriteSMF(midi, fo);
                });
                break;

            case "mid2rbmid":
                WithIO((fi, fo) =>
                {
                    var mid   = MidiCS.MidiFileReader.FromStream(fi);
                    var rbmid = RBMidConverter.ToRBMid(mid);
                    RBMidWriter.WriteStream(rbmid, fo);
                });
                break;

            case "reprocess":
                WithIO((fi, fo) =>
                {
                    var rbmid     = RBMidReader.ReadStream(fi);
                    var processed = RBMidConverter.ToRBMid(RBMidConverter.ToMid(rbmid), rbmid.HopoThreshold);
                    RBMidWriter.WriteStream(processed, fo);
                });
                break;

            case "tex2png":
                WithIO((fi, fo) =>
                {
                    var tex    = TextureReader.ReadStream(fi);
                    var bitmap = TextureConverter.ToBitmap(tex, 0);
                    bitmap.Save(fo, System.Drawing.Imaging.ImageFormat.Png);
                });
                break;

            case "png2tex":
                WithIO((fi, fo) =>
                {
                    var img = System.Drawing.Image.FromStream(fi);
                    var tex = TextureConverter.ToTexture(img);
                    TextureWriter.WriteStream(tex, fo);
                });
                break;

            case "milopng2tex":
                WithIO((fi, fo) =>
                {
                    var tex = TextureConverter.MiloPngToTexture(fi);
                    TextureWriter.WriteStream(tex, fo);
                });
                break;

            case "mesh2obj":
            {
                var input  = args[1];
                var output = args[2];
                using (var fi = File.OpenRead(input))
                {
                    var mesh = HxMeshReader.ReadStream(fi);
                    var obj  = HxMeshConverter.ToObj(mesh);
                    File.WriteAllText(output, obj);
                }
                break;
            }

            case "milo2lip":
            case "milo2lips":
            case "milo2lipsync":
                WithIO((fi, fo) =>
                {
                    var milo    = MiloFile.ReadFromStream(fi);
                    var lipsync = LipsyncConverter.FromMilo(milo);
                    new LipsyncWriter(fo).WriteStream(lipsync);
                });
                break;

            case "version":
                var assembly    = System.Reflection.Assembly.GetExecutingAssembly();
                var version     = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;
                var libAssembly = System.Reflection.Assembly.GetAssembly(typeof(RBMid));
                var libVersion  = FileVersionInfo.GetVersionInfo(libAssembly.Location).FileVersion;
                Console.WriteLine($"ForgeTool v{version}");
                Console.WriteLine($"LibForge v{libVersion}");
                break;

            case "test":
            {
                var dir = args[1];
                int succ = 0, warn = 0, fail = 0;
                var files = dir.EndsWith(".rbmid_ps4") || dir.EndsWith(".rbmid_pc") ?
                            new[] { dir } : Directory.EnumerateFiles(dir, "*.rbmid_*");
                foreach (var f in files)
                {
                    var info = new FileInfo(f);
                    var name = info.Name;
                    using (var fi = File.OpenRead(f))
                    {
                        try
                        {
                            var rbmid  = RBMidReader.ReadStream(fi);
                            var midi   = RBMidConverter.ToMid(rbmid);
                            var rbmid2 = RBMidConverter.ToRBMid(midi, rbmid.HopoThreshold);
                            using (var ms = new MemoryStream((int)fi.Length))
                            {
                                // TODO: Switch this to rbmid2 once we are generating all events
                                //       (regardless of whether the event data are all correct)
                                RBMidWriter.WriteStream(rbmid, ms);
                                ms.Position = 0;
                                if (ms.Length == fi.Length)
                                {
                                    var comparison = rbmid.Compare(rbmid2);
                                    if (comparison != null)
                                    {
                                        throw new CompareException("File comparison failed at field: " + comparison);
                                    }
                                    Console.WriteLine($"[OK] {name}");
                                    succ++;
                                }
                                else
                                {
                                    Console.Write($"[WARN] {name}:");
                                    Console.WriteLine($" Processed file had different length ({fi.Length} orig, {ms.Length} processed)");
                                    warn++;
                                }
                            }
                        } catch (CompareException e)
                        {
                            Console.WriteLine($"[WARN] {name}: {e.Message}");
                            warn++;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"[ERROR] {name}: {e.Message}");
                            Console.WriteLine(e.StackTrace);
                            fail++;
                        }
                    }
                }
                Console.WriteLine($"Summary: {succ} OK, {warn} WARN, {fail} ERROR");
                if (warn > 0)
                {
                    Console.WriteLine("(a = converted file, b = original)");
                }
            }
            break;

            case "simpletest":
            {
                var dir = args[1];
                int succ = 0, warn = 0, fail = 0;
                foreach (var f in Directory.EnumerateFiles(dir, "*.rbmid_*"))
                {
                    var info = new FileInfo(f);
                    var name = info.Name;
                    using (var fi = File.OpenRead(f))
                    {
                        try
                        {
                            var rbmid = RBMidReader.ReadStream(fi);
                            using (var ms = new MemoryStream((int)fi.Length))
                            {
                                RBMidWriter.WriteStream(rbmid, ms);
                                ms.Position = 0;
                                if (ms.Length == fi.Length)
                                {
                                    succ++;
                                }
                                else
                                {
                                    Console.WriteLine($"[WARN] {name}:");
                                    Console.WriteLine($"  Processed file had different length ({fi.Length} orig, {ms.Length} processed)");
                                    warn++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"[ERROR] {name}:");
                            Console.WriteLine("  " + e.Message);
                            fail++;
                        }
                    }
                }
                Console.WriteLine($"Summary: {succ} OK, {warn} WARN, {fail} ERROR");
            }
            break;

            case "testrbsong":
            {
                var dir = args[1];
                int succ = 0, warn = 0, fail = 0;
                var files = dir.EndsWith(".rbsong") ?
                            new[] { dir } : Directory.EnumerateFiles(dir, "*.rbsong");
                foreach (var f in files)
                {
                    var info = new FileInfo(f);
                    var name = info.Name;
                    using (var fi = File.OpenRead(f))
                    {
                        try
                        {
                            var rbsong = new RBSongReader(fi).Read();
                            using (var ms = new MemoryStream((int)fi.Length))
                            {
                                new RBSongWriter(ms).WriteStream(rbsong);
                                ms.Position = 0;
                                if (ms.Length == fi.Length)
                                {
                                    succ++;
                                }
                                else
                                {
                                    Console.WriteLine($"[WARN] {name}:");
                                    Console.WriteLine($"  Processed file had different length ({fi.Length} orig, {ms.Length} processed)");
                                    warn++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"[ERROR] {name}:");
                            Console.WriteLine("  " + e.Message);
                            fail++;
                        }
                    }
                }
                Console.WriteLine($"Summary: {succ} OK, {warn} WARN, {fail} ERROR");
                if (fail > 0)
                {
                    Console.WriteLine("(a = converted file, b = original)");
                }
            }
            break;

            case "rbsong2rbsong":
                WithIO((i, o) => new RBSongWriter(o).WriteStream(new RBSongReader(i).Read()));
                break;

            case "con2gp4":
                makepkg = false;
                goto case "con2pkg";

            case "con2pkg":
            {
                var    i = 0;
                bool   eu = false;
                string pkgId = null, pkgDesc = null;
                while (++i < args.Length - 4)
                {
                    switch (args[i])
                    {
                    case "--scee":
                        eu = true;
                        continue;

                    case "--id":
                        pkgId = args[++i];
                        continue;

                    case "--desc":
                        pkgDesc = args[++i];
                        continue;
                    }
                    break;
                }
                if (makepkg)
                {
                    var cmd    = args[i++];
                    var con    = args[i++];
                    var dest   = args[i++];
                    var tmpDir = Path.Combine(Path.GetTempPath(), "forgetool_tmp_build");
                    if (!Directory.Exists(con))
                    {
                        var conFilename = Path.GetFileName(con);
                        PkgCreator.ConToGp4(con, tmpDir, eu, pkgId, pkgDesc);
                    }
                    else
                    {
                        PkgCreator.ConsToGp4(con, tmpDir, eu, pkgId, pkgDesc);
                    }
                    PkgCreator.BuildPkg(cmd, Path.Combine(tmpDir, "project.gp4"), dest);
                    Directory.Delete(tmpDir, true);
                }
                else
                {
                    var con  = args[i++];
                    var dest = args[i++];
                    PkgCreator.ConToGp4(con, dest, eu, pkgId, pkgDesc);
                }
            }
            break;

            default:
                Usage();
                break;
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Usage();
                return;
            }
            void WithIO(Action <Stream, Stream> action)
            {
                using (var fi = File.OpenRead(args[1]))
                    using (var fo = File.OpenWrite(args[2]))
                        action(fi, fo);
            }

            var makepkg = true;

            switch (args[0])
            {
            case "rbmid2mid":
                WithIO((fi, fo) =>
                {
                    var rbmid = RBMidReader.ReadStream(fi);
                    var midi  = RBMidConverter.ToMid(rbmid);
                    MidiCS.MidiFileWriter.WriteSMF(midi, fo);
                });
                break;

            case "mid2rbmid":
                WithIO((fi, fo) =>
                {
                    var mid   = MidiCS.MidiFileReader.FromStream(fi);
                    var rbmid = RBMidConverter.ToRBMid(mid);
                    RBMidWriter.WriteStream(rbmid, fo);
                });
                break;

            case "reprocess":
                WithIO((fi, fo) =>
                {
                    var rbmid     = RBMidReader.ReadStream(fi);
                    var processed = RBMidConverter.ToRBMid(RBMidConverter.ToMid(rbmid), rbmid.HopoThreshold);
                    RBMidWriter.WriteStream(processed, fo);
                });
                break;

            case "tex2png":
                WithIO((fi, fo) =>
                {
                    var tex    = TextureReader.ReadStream(fi);
                    var bitmap = TextureConverter.ToBitmap(tex, 0);
                    bitmap.Save(fo, System.Drawing.Imaging.ImageFormat.Png);
                });
                break;

            case "png2tex":
                WithIO((fi, fo) =>
                {
                    var img = System.Drawing.Image.FromStream(fi);
                    var tex = TextureConverter.ToTexture(img);
                    TextureWriter.WriteStream(tex, fo);
                });
                break;

            case "milopng2tex":
                WithIO((fi, fo) =>
                {
                    var tex = TextureConverter.MiloPngToTexture(fi);
                    TextureWriter.WriteStream(tex, fo);
                });
                break;

            case "mesh2obj":
            {
                var input  = args[1];
                var output = args[2];
                using (var fi = File.OpenRead(input))
                {
                    var mesh = HxMeshReader.ReadStream(fi);
                    var obj  = HxMeshConverter.ToObj(mesh);
                    File.WriteAllText(output, obj);
                }
                break;
            }

            case "milo2lip":
            case "milo2lips":
            case "milo2lipsync":
                WithIO((fi, fo) =>
                {
                    var milo    = MiloFile.ReadFromStream(fi);
                    var lipsync = LipsyncConverter.FromMilo(milo);
                    new LipsyncWriter(fo).WriteStream(lipsync);
                });
                break;

            case "version":
                var assembly    = System.Reflection.Assembly.GetExecutingAssembly();
                var version     = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;
                var libAssembly = System.Reflection.Assembly.GetAssembly(typeof(RBMid));
                var libVersion  = FileVersionInfo.GetVersionInfo(libAssembly.Location).FileVersion;
                Console.WriteLine($"ForgeTool v{version}");
                Console.WriteLine($"LibForge v{libVersion}");
                break;

            case "test":
            {
                var dir = args[1];
                int succ = 0, warn = 0, fail = 0;
                var files = dir.EndsWith(".rbmid_ps4") || dir.EndsWith(".rbmid_pc") ?
                            new[] { dir } : Directory.EnumerateFiles(dir, "*.rbmid_*");
                foreach (var f in files)
                {
                    var info = new FileInfo(f);
                    var name = info.Name;
                    using (var fi = File.OpenRead(f))
                    {
                        try
                        {
                            var rbmid  = RBMidReader.ReadStream(fi);
                            var midi   = RBMidConverter.ToMid(rbmid);
                            var rbmid2 = RBMidConverter.ToRBMid(midi, rbmid.HopoThreshold);
                            using (var ms = new MemoryStream((int)fi.Length))
                            {
                                // TODO: Switch this to rbmid2 once we are generating all events
                                //       (regardless of whether the event data are all correct)
                                RBMidWriter.WriteStream(rbmid, ms);
                                ms.Position = 0;
                                if (ms.Length == fi.Length)
                                {
                                    var comparison = rbmid.Compare(rbmid2);
                                    if (comparison != null)
                                    {
                                        throw new CompareException("File comparison failed at field: " + comparison);
                                    }
                                    Console.WriteLine($"[OK] {name}");
                                    succ++;
                                }
                                else
                                {
                                    Console.Write($"[WARN] {name}:");
                                    Console.WriteLine($" Processed file had different length ({fi.Length} orig, {ms.Length} processed)");
                                    warn++;
                                }
                            }
                        } catch (CompareException e)
                        {
                            Console.WriteLine($"[WARN] {name}: {e.Message}");
                            warn++;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"[ERROR] {name}: {e.Message}");
                            Console.WriteLine(e.StackTrace);
                            fail++;
                        }
                    }
                }
                Console.WriteLine($"Summary: {succ} OK, {warn} WARN, {fail} ERROR");
                if (warn > 0)
                {
                    Console.WriteLine("(a = converted file, b = original)");
                }
                if (warn > 0 || fail > 0)
                {
                    Environment.Exit(1);
                }
            }
            break;

            case "simpletest":
            {
                var dir = args[1];
                int succ = 0, warn = 0, fail = 0;
                foreach (var f in Directory.EnumerateFiles(dir, "*.rbmid_*"))
                {
                    var info = new FileInfo(f);
                    var name = info.Name;
                    using (var fi = File.OpenRead(f))
                    {
                        try
                        {
                            var rbmid = RBMidReader.ReadStream(fi);
                            using (var ms = new MemoryStream((int)fi.Length))
                            {
                                RBMidWriter.WriteStream(rbmid, ms);
                                ms.Position = 0;
                                if (ms.Length == fi.Length)
                                {
                                    succ++;
                                }
                                else
                                {
                                    Console.WriteLine($"[WARN] {name}:");
                                    Console.WriteLine($"  Processed file had different length ({fi.Length} orig, {ms.Length} processed)");
                                    warn++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"[ERROR] {name}:");
                            Console.WriteLine("  " + e.Message);
                            fail++;
                        }
                    }
                }
                Console.WriteLine($"Summary: {succ} OK, {warn} WARN, {fail} ERROR");
            }
            break;

            case "testrbsong":
            {
                var dir = args[1];
                int succ = 0, warn = 0, fail = 0;
                var files = dir.EndsWith(".rbsong") ?
                            new[] { dir } : Directory.EnumerateFiles(dir, "*.rbsong");
                foreach (var f in files)
                {
                    var info = new FileInfo(f);
                    var name = info.Name;
                    using (var fi = File.OpenRead(f))
                    {
                        try
                        {
                            var rbsong = new RBSongResource();
                            rbsong.Load(fi);
                            using (var ms = new MemoryStream((int)fi.Length))
                            {
                                new RBSongResourceWriter(ms).WriteStream(rbsong);
                                ms.Position = 0;
                                if (ms.Length == fi.Length)
                                {
                                    succ++;
                                }
                                else
                                {
                                    Console.WriteLine($"[WARN] {name}:");
                                    Console.WriteLine($"  Processed file had different length ({fi.Length} orig, {ms.Length} processed)");
                                    warn++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"[ERROR] {name}:");
                            Console.WriteLine("  " + e.Message);
                            fail++;
                        }
                    }
                }
                Console.WriteLine($"Summary: {succ} OK, {warn} WARN, {fail} ERROR");
                if (fail > 0)
                {
                    Console.WriteLine("(a = converted file, b = original)");
                }
            }
            break;

            case "rbsong2rbsong":
                WithIO((i, o) => {
                    var rbsong = new RBSongResource();
                    rbsong.Load(i);
                    new RBSongResourceWriter(o).WriteStream(rbsong);
                });
                break;

            case "con2gp4":
                makepkg = false;
                goto case "con2pkg";

            case "con2pkg":
            {
                var    i = 0;
                bool   eu = false;
                string pkgId = null, pkgDesc = null;
                while (++i < args.Length - 3)
                {
                    switch (args[i])
                    {
                    case "--scee":
                        eu = true;
                        continue;

                    case "--id":
                        pkgId = args[++i];
                        continue;

                    case "--desc":
                        pkgDesc = args[++i];
                        continue;
                    }
                    break;
                }
                if (makepkg)
                {
                    var con    = args[i++];
                    var dest   = args[i++];
                    var tmpDir = Path.Combine(Path.GetTempPath(), "forgetool_tmp_build");
                    if (!Directory.Exists(con))
                    {
                        var conFilename = Path.GetFileName(con);
                        PkgCreator.ConToGp4(con, tmpDir, eu, pkgId, pkgDesc);
                    }
                    else
                    {
                        PkgCreator.ConsToGp4(con, tmpDir, eu, pkgId, pkgDesc);
                    }
                    PkgCreator.BuildPkg(Path.Combine(tmpDir, "project.gp4"), dest);
                    Directory.Delete(tmpDir, true);
                }
                else
                {
                    var con  = args[i++];
                    var dest = args[i++];
                    PkgCreator.ConToGp4(con, dest, eu, pkgId, pkgDesc);
                }
            }
            break;

            case "csv2txt":
            {
                var input  = args[1];
                var output = args[2];
                using (var fi = File.OpenRead(input))
                {
                    var csv = CsvData.LoadFile(fi);
                    File.WriteAllText(output, csv.ToString());
                }
            }
            break;

            case "arkorder":
            {
                var input   = args[1];
                var output  = args[2];
                var sb      = new StringBuilder();
                var archive = new Archive(input);
                using (var o = File.OpenWrite(output))
                    using (var sw = new StreamWriter(o))
                    {
                        archive.WriteArkorder(sw);
                    }
            }
            break;

            case "arkbuild":
            {
                var filedir  = args[1];
                var arkorder = args[2];
                var outdir   = args[3];
                var name     = args[4];
                if (!Directory.Exists(filedir))
                {
                    Console.WriteLine($"Error: {filedir} does not exist.");
                    return;
                }
                if (!Directory.Exists(outdir))
                {
                    Console.WriteLine($"Error: {outdir} does not exist.");
                    return;
                }
                if (!File.Exists(arkorder))
                {
                    Console.WriteLine($"Error: {arkorder} not found.");
                    return;
                }
                var       arks = new List <List <Tuple <string, IFile> > >();
                DataArray arkOrder;
                using (var order = File.OpenRead(arkorder))
                    using (var reader = new StreamReader(order))
                    {
                        arkOrder = DtxCS.DTX.FromDtaStream(order);
                    }
                int arkIndex = 0;
                arks.Add(new List <Tuple <string, IFile> >());
                foreach (var x in arkOrder.Children)
                {
                    if (x is DataSymbol s)
                    {
                        var fullPath = Path.Combine(filedir, s.Name.Replace('/', Path.DirectorySeparatorChar));
                        if (!File.Exists(fullPath))
                        {
                            Console.WriteLine($"Error: {fullPath} could not be found.");
                            return;
                        }
                        IFile f = Util.LocalFile(fullPath);
                        arks[arkIndex].Add(Tuple.Create(s.Name, f));
                    }
                    else if (x is DataCommand c && c.Symbol(0).Name == "split_ark")
                    {
                        arkIndex++;
                        arks[arkIndex] = new List <Tuple <string, IFile> >();
                    }
                }
                var builder = new ArkBuilder(name, arks);
                Console.Write($"Writing {name}.hdr and ark files to {outdir}...");
                builder.Save(outdir);
            }
            break;

            default:
                Usage();
                break;
            }
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Usage();
                return;
            }
            void WithIO(Action <Stream, Stream> action)
            {
                using (var fi = File.OpenRead(args[1]))
                    using (var fo = File.OpenWrite(args[2]))
                        action(fi, fo);
            }

            switch (args[0])
            {
            case "rbmid2mid":
                WithIO((fi, fo) =>
                {
                    var rbmid = RBMidReader.ReadStream(fi);
                    var midi  = RBMidConverter.ToMid(rbmid);
                    MidiCS.MidiFileWriter.WriteSMF(midi, fo);
                });
                break;

            case "mid2rbmid":
                WithIO((fi, fo) =>
                {
                    var mid   = MidiCS.MidiFileReader.FromStream(fi);
                    var rbmid = RBMidConverter.ToRBMid(mid);
                    RBMidWriter.WriteStream(rbmid, fo);
                });
                break;

            case "reprocess":
                WithIO((fi, fo) =>
                {
                    var rbmid     = RBMidReader.ReadStream(fi);
                    var processed = RBMidConverter.ToRBMid(RBMidConverter.ToMid(rbmid));
                    RBMidWriter.WriteStream(processed, fo);
                });
                break;

            case "tex2png":
                WithIO((fi, fo) =>
                {
                    var tex    = TextureReader.ReadStream(fi);
                    var bitmap = TextureConverter.ToBitmap(tex, 0);
                    bitmap.Save(fo, System.Drawing.Imaging.ImageFormat.Png);
                });
                break;

            case "mesh2obj":
            {
                var input  = args[1];
                var output = args[2];
                using (var fi = File.OpenRead(input))
                {
                    var mesh = HxMeshReader.ReadStream(fi);
                    var obj  = HxMeshConverter.ToObj(mesh);
                    File.WriteAllText(output, obj);
                }
                break;
            }

            case "version":
                var assembly    = System.Reflection.Assembly.GetExecutingAssembly();
                var version     = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;
                var libAssembly = System.Reflection.Assembly.GetAssembly(typeof(RBMid));
                var libVersion  = FileVersionInfo.GetVersionInfo(libAssembly.Location).FileVersion;
                Console.WriteLine($"ForgeTool v{version}");
                Console.WriteLine($"LibForge v{libVersion}");
                break;

            case "test":
            {
                var dir = args[1];
                int succ = 0, warn = 0, fail = 0;
                var files = dir.EndsWith(".rbmid_ps4") || dir.EndsWith(".rbmid_pc") ?
                            new[] { dir } : Directory.EnumerateFiles(dir, "*.rbmid_*");
                foreach (var f in files)
                {
                    var info = new FileInfo(f);
                    var name = info.Name;
                    using (var fi = File.OpenRead(f))
                    {
                        try
                        {
                            var rbmid  = RBMidReader.ReadStream(fi);
                            var midi   = RBMidConverter.ToMid(rbmid);
                            var rbmid2 = RBMidConverter.ToRBMid(midi);
                            using (var ms = new MemoryStream((int)fi.Length))
                            {
                                // TODO: Switch this to rbmid2 once we are generating all events
                                //       (regardless of whether the event data are all correct)
                                RBMidWriter.WriteStream(rbmid, ms);
                                ms.Position = 0;
                                if (ms.Length == fi.Length)
                                {
                                    var comparison = rbmid.Compare(rbmid2);
                                    if (comparison != null)
                                    {
                                        throw new Exception("File comparison failed at field: " + comparison);
                                    }
                                    succ++;
                                }
                                else
                                {
                                    Console.WriteLine($"[WARN] {name}:");
                                    Console.WriteLine($"  Processed file had different length ({fi.Length} orig, {ms.Length} processed)");
                                    warn++;
                                }
                            }
                        } catch (Exception e)
                        {
                            Console.WriteLine($"[ERROR] {name}:");
                            Console.WriteLine("  " + e.Message);
                            fail++;
                        }
                    }
                }
                Console.WriteLine($"Summary: {succ} OK, {warn} WARN, {fail} ERROR");
                if (fail > 0)
                {
                    Console.WriteLine("(a = converted file, b = original)");
                }
            }
            break;

            case "simpletest":
            {
                var dir = args[1];
                int succ = 0, warn = 0, fail = 0;
                foreach (var f in Directory.EnumerateFiles(dir, "*.rbmid_*"))
                {
                    var info = new FileInfo(f);
                    var name = info.Name;
                    using (var fi = File.OpenRead(f))
                    {
                        try
                        {
                            var rbmid = RBMidReader.ReadStream(fi);
                            using (var ms = new MemoryStream((int)fi.Length))
                            {
                                RBMidWriter.WriteStream(rbmid, ms);
                                ms.Position = 0;
                                if (ms.Length == fi.Length)
                                {
                                    succ++;
                                }
                                else
                                {
                                    Console.WriteLine($"[WARN] {name}:");
                                    Console.WriteLine($"  Processed file had different length ({fi.Length} orig, {ms.Length} processed)");
                                    warn++;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"[ERROR] {name}:");
                            Console.WriteLine("  " + e.Message);
                            fail++;
                        }
                    }
                }
                Console.WriteLine($"Summary: {succ} OK, {warn} WARN, {fail} ERROR");
            }
            break;

            default:
                Usage();
                break;
            }
        }