コード例 #1
0
ファイル: RBMidiFileTests.cs プロジェクト: nrabreu/LibForge
        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);
            }
        }
コード例 #2
0
 public static object LoadObject(GameArchives.IFile i)
 {
     if (i.Name.Contains(".bmp_") || i.Name.Contains(".png_"))
     {
         using (var s = i.GetStream())
         {
             try
             {
                 return(TextureReader.ReadStream(s));
             }
             catch (Exception ex)
             {
                 System.Windows.Forms.MessageBox.Show("Couldn't load texture: " + ex.Message);
                 return(null);
             }
         }
     }
     else if (i.Name.Contains("_dta_") || i.Name.EndsWith(".dtb"))
     {
         using (var s = i.GetStream())
         {
             var data = DtxCS.DTX.FromDtb(s);
             var sb   = new StringBuilder();
             foreach (var x in data.Children)
             {
                 sb.AppendLine(x.ToString(0));
             }
             return(sb.ToString());
         }
     }
     else if (i.Name.EndsWith(".dta") || i.Name.EndsWith(".moggsong"))
     {
         using (var s = i.GetStream())
             using (var r = new System.IO.StreamReader(s))
             {
                 return(r.ReadToEnd());
             }
     }
     else if (i.Name.Contains(".songdta"))
     {
         using (var s = i.GetStream())
         {
             var songData = SongDataReader.ReadStream(s);
             return(songData);
         }
     }
     else if (i.Name.Contains(".fbx"))
     {
         using (var s = i.GetStream())
         {
             return(HxMeshReader.ReadStream(s));
         }
     }
     else if (i.Name.Contains(".rbmid_"))
     {
         using (var s = i.GetStream())
         {
             return(RBMidReader.ReadStream(s));
         }
     }
     else if (i.Name.Contains(".lipsync"))
     {
         using (var s = i.GetStream())
         {
             return(new LipsyncReader(s).Read());
         }
     }
     else if (i.Name.Contains(".rbsong"))
     {
         using (var s = i.GetStream())
         {
             return(new RBSongReader(s).Read());
         }
     }
     else if (i.Name.Contains(".gp4"))
     {
         using (var s = i.GetStream())
         {
             return(LibOrbisPkg.GP4.Gp4Project.ReadFrom(s));
         }
     }
     else if (i.Name.Contains(".pkg"))
     {
         using (var s = i.GetStream())
         {
             return(new LibOrbisPkg.PKG.PkgReader(s).ReadHeader());
         }
     }
     else
     {
         return(null);
     }
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: PikminGuts92/LibForge
        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;
            }
        }
コード例 #4
0
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            switch (e.Node.Tag)
            {
            case null:
                break;

            case GameArchives.IFile i:
                if (i.Name.Contains(".bmp_") || i.Name.Contains(".png_"))
                {
                    using (var s = i.GetStream())
                    {
                        var tex = TextureReader.ReadStream(s);
                        pictureBox1.Width  = tex.Mipmaps[0].Width;
                        pictureBox1.Height = tex.Mipmaps[0].Height;
                        tabControl1.SelectTab(0);
                        fileTreeView.Select();
                        try
                        {
                            pictureBox1.Image = TextureConverter.ToBitmap(tex, 0);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Couldn't load texture: " + ex.Message);
                        }
                    }
                }
                else if (i.Name.Contains("_dta_") || i.Name.EndsWith(".dtb"))
                {
                    using (var s = i.GetStream())
                    {
                        var data = DtxCS.DTX.FromDtb(s);
                        tabControl1.SelectTab(1);
                        fileTreeView.Select();
                        var sb = new StringBuilder();
                        foreach (var x in data.Children)
                        {
                            sb.AppendLine(x.ToString(0));
                        }
                        dataTextBox.Text = sb.ToString();
                    }
                }
                else if (i.Name.EndsWith(".dta") || i.Name.EndsWith(".moggsong"))
                {
                    using (var s = i.GetStream())
                        using (var r = new System.IO.StreamReader(s))
                        {
                            tabControl1.SelectTab(1);
                            fileTreeView.Select();
                            dataTextBox.Text = r.ReadToEnd();
                        }
                }
                else if (i.Name.Contains(".songdta"))
                {
                    using (var s = i.GetStream())
                    {
                        var songData = SongDataReader.ReadStream(s);
                        songDataInspector1.UpdateValues(songData);
                        tabControl1.SelectTab(2);
                        fileTreeView.Select();
                    }
                }
                else if (i.Name.Contains(".fbx"))
                {
                    using (var s = i.GetStream())
                    {
                        var mesh = HxMeshReader.ReadStream(s);
                        meshTextBox.Text = HxMeshConverter.ToObj(mesh);
                        tabControl1.SelectTab(3);
                        fileTreeView.Select();
                    }
                }
                else if (i.Name.Contains(".rbmid_"))
                {
                    using (var s = i.GetStream())
                    {
                        var rbmid = RBMidReader.ReadStream(s);
                        ObjectPreview(rbmid);
                    }
                }
                else if (i.Name.Contains(".lipsync"))
                {
                    using (var s = i.GetStream())
                    {
                        var lipsync = new LibForge.Lipsync.LipsyncReader(s).Read();
                        ObjectPreview(lipsync);
                    }
                }
                else if (i.Name.Contains(".rbsong"))
                {
                    using (var s = i.GetStream())
                    {
                        var rbsong = new LibForge.RBSong.RBSongReader(s).Read();
                        ObjectPreview(rbsong);
                    }
                }
                break;
            }
        }
コード例 #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;
            }
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: abefacciazzi/LibForge
        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;
            }
        }