public static int DumpTxt( String Filename, String TxtFilename )
        {
            LIN lin;
            try {
                lin = new LIN( Filename );
            } catch ( Exception ex ) {
                Console.WriteLine( ex.Message );
                Console.WriteLine( "Failed loading text file!" );
                return -1;
            }

            List<string> Output = new List<string>();

            foreach ( ScriptEntry s in lin.ScriptData ) {
                if ( s.Type == 0x02 ) {
                    Output.Add( "Text: " + s.Text );
                } else {
                    Output.Add( s.FormatForGraceNote() );
                }
            }

            if ( lin.UnreferencedText != null ) {
                foreach ( KeyValuePair<int, string> u in lin.UnreferencedText ) {
                    Output.Add( "Unreferenced Text (" + u.Value + "): " + u.Key );
                }
            }

            System.IO.File.WriteAllLines( TxtFilename, Output.ToArray() );
            return 0;
        }
        public static int Check( String Filename )
        {
            Byte[] OriginalFile = System.IO.File.ReadAllBytes( Filename );
            LIN lin = new LIN( OriginalFile );
            Byte[] RecreatedFile = lin.CreateFile( 1024 );
            LIN linOrig = new LIN( OriginalFile );

            return Compare( OriginalFile, RecreatedFile, lin.UnalignedFilesize, Filename + ".fail" );
        }
 public static int Export( String InFilename, String OutFilename, String DB, int Alignment, bool RefreshNames, bool RefreshCode )
 {
     LIN lin;
     try {
         lin = new LIN( InFilename );
         if ( RefreshNames ) { lin.ReinsertNamesIntoDatabase( "Data Source=" + DB ); }
         if ( RefreshCode ) { lin.ReinsertCodeIntoDatabase( "Data Source=" + DB ); }
         lin.GetSQL( "Data Source=" + DB );
     } catch ( Exception ex ) {
         Console.WriteLine( ex.Message );
         Console.WriteLine( "Failed loading text file!" );
         return -1;
     }
     byte[] newfile = lin.CreateFile( Alignment );
     System.IO.File.WriteAllBytes( OutFilename, newfile );
     return 0;
 }
        public static int Import( String Filename, String NewDB, String GracesDB )
        {
            LIN lin;
            try {
                lin = new LIN( Filename );
            } catch ( Exception ex ) {
                Console.WriteLine( ex.Message );
                Console.WriteLine( "Failed loading text file!" );
                return -1;
            }

            Console.WriteLine( "Importing..." );
            System.IO.File.WriteAllBytes( NewDB, Properties.Resources.gndb_template );

            List<GraceNoteDatabaseEntry> Entries = new List<GraceNoteDatabaseEntry>();

            string TextToInsert = "";
            foreach ( ScriptEntry s in lin.ScriptData ) {
                if ( s.Type == 0x02 ) {
                    Entries.Add( new GraceNoteDatabaseEntry( TextToInsert, TextToInsert, "", -1, 1, "[Game Code]", 0 ) );
                    Entries.Add( new GraceNoteDatabaseEntry( s.Text, s.Text, "", 0, 2, s.IdentifyString, 0 ) );
                    TextToInsert = "";
                    continue;
                }
                TextToInsert = TextToInsert + s.FormatForGraceNote() + '\n';
            }
            if ( TextToInsert != null ) {
                Entries.Add( new GraceNoteDatabaseEntry( TextToInsert, TextToInsert, "", -1, 1, "[Game Code]", 0 ) );
            }
            if ( lin.UnreferencedText != null ) {
                foreach ( KeyValuePair<int, string> u in lin.UnreferencedText ) {
                    Entries.Add( new GraceNoteDatabaseEntry( u.Value, u.Value, "", 0, 3, "[Unreferenced Text]", u.Key ) );
                }
            }
            GraceNoteDatabaseEntry.InsertSQL( Entries.ToArray(), "Data Source=" + NewDB, "Data Source=" + GracesDB );

            Console.WriteLine( "Successfully imported entries!" );
            return 0;
        }
        public static int AutoImport( List<string> args )
        {
            string dir = @"d:\_svn\GraceNote\GraceNote\DanganRonpaBestOfRebuild\umdimage.dat.ex\";
            string voicedir = @"d:\_svn\GraceNote\GraceNote\Voices\";
            string[] files = System.IO.Directory.GetFiles( dir );

            List<String> dbsToUp = new List<string>();

            foreach ( var x in nonstopDict ) {
                string nonstopFile = GetFromSubstring( files, x.Key );
                string scriptFile = GetFromSubstring( files, x.Value );
                string scriptFileFilename = new System.IO.FileInfo( scriptFile ).Name;
                string databaseId = scriptFileFilename.Substring( 0, 4 );
                string databaseFile = @"d:\_svn\GraceNote\GraceNote\DanganRonpaBestOfDB\DRBO" + databaseId;
                dbsToUp.Add( "DRBO" + databaseId );
                //continue;

                LIN lin = new LIN( scriptFile );
                Nonstop nonstop = new Nonstop( nonstopFile );

                int lastScriptEntry = 0;
                foreach ( var item in nonstop.items ) {
                    int stringId = item.data[(int)NonstopSingleStructure.StringID] + 1;
                    int correspondingTextEntry = stringId * 2;
                    int correspondingScriptEntry = correspondingTextEntry - 1;
                    if ( item.data[(int)NonstopSingleStructure.Type] == 0 ) {
                        lastScriptEntry = correspondingTextEntry;
                    }

                    // --- insert comment info ---
                    string comment = (string)SqliteUtil.SelectScalar(
                        "Data Source=" + databaseFile,
                        "SELECT comment FROM Text WHERE id = ?",
                        new object[] { correspondingTextEntry } );

                    bool weakpt = item.data[(int)NonstopSingleStructure.HasWeakPoint] > 0;
                    comment = ( comment == "" ? "" : comment + "\n\n" )
                        + "Autogenerated Info:\n"
                        + ( lastScriptEntry == 0 ? "Corresponds to file: " + scriptFileFilename : "" )
                        + ( item.data[(int)NonstopSingleStructure.Type] == 0 ? "Normal Line\n" : "Background Noise\n" )
                        + ( weakpt ? "Has a Weak Point\n" : "No Weakpoint\n" )
                        + ( weakpt && ( item.data[(int)NonstopSingleStructure.ShootWithEvidence] & 0xFF ) != 255 ? "Shot with Evidence Bullet: " + item.data[(int)NonstopSingleStructure.ShootWithEvidence] + "\n" : "" )
                        + ( weakpt && ( item.data[(int)NonstopSingleStructure.ShootWithWeakpoint] & 0xFF ) != 255 ? "Shot with Weak Point: " + item.data[(int)NonstopSingleStructure.ShootWithWeakpoint] + "\n" : "" )
                        + ( weakpt && ( item.data[(int)NonstopSingleStructure.ShootWithWeakpoint] & 0xFF ) == 255 && ( item.data[(int)NonstopSingleStructure.ShootWithEvidence] & 0xFF ) == 255 ? "Can't be shot\n" : "" )
                        + ( item.data[(int)NonstopSingleStructure.Type] == 0 ? "" : "Appears around Entry #" + lastScriptEntry + "\n" )
                        + ( item.data[(int)NonstopSingleStructure.Type] == 0 ? "Sprite: " + DanganUtil.CharacterIdToName( (byte)item.data[(int)NonstopSingleStructure.Character] ) + " " + item.data[(int)NonstopSingleStructure.Sprite] + "\n" : "" )
                        ;
                    SqliteUtil.Update(
                        "Data Source=" + databaseFile,
                        "UPDATE Text SET comment = ?, updated = 1 WHERE id = ?",
                        new object[] { comment, correspondingTextEntry } );

                    // --- insert voice info ---
                    string script = (string)SqliteUtil.SelectScalar(
                        "Data Source=" + databaseFile,
                        "SELECT english FROM Text WHERE id = ?",
                        new object[] { correspondingScriptEntry } );
                    string voicename;
                    string voicefilecheck;

                    byte charid = (byte)item.data[(int)NonstopSingleStructure.Character];
                    if ( item.data[(int)NonstopSingleStructure.Type] == 0 ) {
                        while ( true ) {
                            string charac = DanganUtil.CharacterIdToName( charid );
                            if ( charac == "Naegi" ) { charac = "Neagi"; }
                            voicename = "[" + charac + "] " + item.data[(int)NonstopSingleStructure.Chapter] + " "
                                + ( item.data[(int)NonstopSingleStructure.AudioSampleId] >> 8 ) + " " + ( item.data[(int)NonstopSingleStructure.AudioSampleId] & 0xFF ) + " 100";
                            voicefilecheck = voicedir + voicename + ".mp3";
                            if ( System.IO.File.Exists( voicefilecheck ) ) {
                                break;
                            }
                            charid = 0x12;
                        }

                        script += "<__END__>\n"
                            + "<Voice: " + voicename + ">"
                            ;
                        SqliteUtil.Update(
                            "Data Source=" + databaseFile,
                            "UPDATE Text SET english = ?, updated = 1 WHERE id = ?",
                            new object[] { script, correspondingScriptEntry } );

                        // update the header name thingy
                        string header = DanganUtil.CharacterIdToName( charid );
                        SqliteUtil.Update(
                            "Data Source=" + databaseFile,
                            "UPDATE Text SET IdentifyString = ?, updated = 1 WHERE id = ?",
                            new object[] { header, correspondingTextEntry } );
                    } else {
                        string header = "Background Noise";
                        SqliteUtil.Update(
                            "Data Source=" + databaseFile,
                            "UPDATE Text SET IdentifyString = ?, updated = 1 WHERE id = ?",
                            new object[] { header, correspondingTextEntry } );
                    }

                }
            }

            System.IO.File.WriteAllLines(
                @"d:\_svn\GraceNote\GraceNote\temp.txt", dbsToUp.ToArray());
            return 0;
        }
        public static int Execute( List<string> args )
        {
            if ( args.Count < 1 ) {
                Console.WriteLine( "Usage: DanganRonpaText_GraceNote -dumptxt text.lin out.txt" );
                return -1;
            }

            if ( args[0] == "-check" ) {
                return Check( args[1] );
            } else if ( args[0] == "-dumpinsertcheck" ) {
                int Alignment = 16;
                LinImport.Importer.Import( args[1], args[2], args[3] );
                LinExport.Exporter.Export( args[1], args[1] + ".new", args[2], Alignment, false, false );

                Byte[] OriginalFile = System.IO.File.ReadAllBytes( args[1] );
                LIN lin = new LIN( OriginalFile );
                lin.CreateFile( Alignment );
                return Compare( OriginalFile, System.IO.File.ReadAllBytes( args[1] + ".new" ), lin.UnalignedFilesize, args[1] + ".fail" );
            } else if ( args[0] == "-dumptxt" ) {
                return DumpTxt( args[1], args[2] );
            }

            return -1;
        }