@ -1,17 +1,11 @@
using System ;
using System.Text ;
using System.Drawing ;
using System.IO ;
using System.Text ;
using static Program ;
using static System . Net . Mime . MediaTypeNames ;
using static System . Runtime . InteropServices . JavaScript . JSType ;
class Program
class Program
{
{
static void Main ( )
static void Main ( )
{
{
//TEST REMOVE LATER
//TEST REMOVE LATER
string [ ] args = new string [ ] { "--all" , "C:\\Users\\Lauren\\Desktop\\Ken Ga Kimi\\ TEST", "C:\\Users\\Lauren\\Desktop\\Ken Ga Kimi\\TEST \\OUTPUT" } ;
string [ ] args = new string [ ] { "--all" , "C:\\Users\\Lauren\\Desktop\\Ken Ga Kimi\\ PATCH\\EN Files", "C:\\Users\\Lauren\\Desktop\\Ken Ga Kimi\\PATCH\\EN Files \\OUTPUT" } ;
if ( args . Length < 1 )
if ( args . Length < 1 )
{
{
@ -187,7 +181,7 @@ class Program
offset = RoundUpToNextMultipleOf4 ( offset ) ;
offset = RoundUpToNextMultipleOf4 ( offset ) ;
int size = BitConverter . ToInt32 ( data , offset ) ;
int size = BitConverter . ToInt32 ( data , offset ) ;
offset + = 4 ;
offset + = 4 ;
if ( data [ offset +1 ] = = 255 & & data [ offset + 2 ] = = 254 ) {
if ( data [ offset ] = = 255 & & data [ offset + 1 ] = = 254 ) {
offset + = 2 ;
offset + = 2 ;
}
}
//offset = RoundUpToNextMultipleOf4(offset);
//offset = RoundUpToNextMultipleOf4(offset);
@ -233,84 +227,86 @@ class Program
}
}
}
}
List < string > searchStrings = new List < string > ( ) {
List < string > searchStrings = new List < string > ( ) {
"\n@ " ,
"@ " ,
"\n<voice name=" ,
"<voice name=" ,
"\n<textboxtype type=\"monologue" ,
"<textboxtype type=\"monologue\">" ,
"\n<textboxtype type=\"dialogue\">" ,
"<textboxtype type=\"monologue2\">" ,
"\n<monologue background=\"on\">" ,
"<textboxtype type=\"monologue3\">" ,
"<monologue background=\"on\">" ,
} ;
} ;
List < ( string , int ) > stringPositions = FindStringPositions ( script . ScriptText , searchStrings ) ;
List < ( string , int ) > stringPositions = FindStringPositions ( script . ScriptText ) ;
List < ( string , int ) > filteredStringPositions = stringPositions
. Where ( pos = > searchStrings . Any ( s = > pos . Item1 . Contains ( s ) ) )
. ToList ( ) ;
//Add length of data
//Add length of data
newAlr . AddRange ( BitConverter . GetBytes ( stringPositions . Count * 8 ) ) ;
newAlr . AddRange ( BitConverter . GetBytes ( filteredS tringPositions. Count * 8 ) ) ;
if ( alr . ScriptTitle = = "KEN_00_00_00_alr" )
{
Console . WriteLine ( "Found KEN_00_00_00" ) ;
}
//position += 1;
//position += 1;
for ( int i = 0 ; i < stringPositions . Count ; i + + )
for ( int i = 0 ; i < stringPositions . Count ; i + + )
{
{
newAlr . AddRange ( BitConverter . GetBytes ( stringPositions [ i ] . Item2 ) ) ;
if ( searchStrings . Any ( s = > stringPositions [ i ] . Item1 . Contains ( s ) ) )
newAlr . AddRange ( BitConverter . GetBytes ( position ) ) ;
if ( stringPositions [ i ] . Item1 ! = "\n@ " )
{
position + = 1 ;
}
else if ( stringPositions [ i ] . Item1 = = "\n@ " & & i > 0 & & stringPositions [ i - 1 ] . Item1 ! = "\n@ " )
{
{
//Don't add to position if the previous string was a tag
newAlr . AddRange ( BitConverter . GetBytes ( stringPositions [ i ] . Item2 ) ) ;
}
newAlr . AddRange ( BitConverter . GetBytes ( position ) ) ;
else if ( stringPositions . Count = = 1 )
var isNormalTag = searchStrings . Any ( t = > stringPositions [ i ] . Item1 . Contains ( t ) ) ;
{
if ( stringPositions [ i ] . Item1 . Contains ( "<voice name=" ) )
position + = 1 ;
{
continue ;
}
else if ( isNormalTag )
{
position + = 1 ;
}
else
{
//Don't add to position if the previous string was a tag
//Console.WriteLine("OOPS");
}
}
}
}
}
return newAlr . ToArray ( ) ;
return newAlr . ToArray ( ) ;
}
}
public static List < ( string SearchString , int Position ) > FindStringPositions ( byte [ ] scriptBytes , IEnumerable < string > searchStrings )
public static List < ( string SearchString , int Position ) > FindStringPositions ( byte [ ] scriptBytes )
{
{
var results = new List < ( string , int ) > ( ) ;
var results = new List < ( string , int ) > ( ) ;
// Decode the script bytes to string for line analysis
string scriptText = Encoding . Unicode . GetString ( scriptBytes ) ;
string scriptText = Encoding . Unicode . GetString ( scriptBytes ) ;
foreach ( var search in searchStrings )
// 1. Find all <...> tags not commented out
var tagRegex = new System . Text . RegularExpressions . Regex ( @"<[^>\r\n]+>" ) ;
foreach ( System . Text . RegularExpressions . Match match in tagRegex . Matches ( scriptText ) )
{
{
byte [ ] searchBytes = Encoding . Unicode . GetBytes ( search ) ;
if ( match . Value = = "<pb>" ) continue ; // Skip <pb> tags
int index = 0 ;
while ( index < = scriptBytes . Length - searchBytes . Length )
int lineStart = scriptText . LastIndexOf ( '\n' , match . Index ) ;
if ( lineStart = = - 1 ) lineStart = 0 ; else lineStart + + ;
int lineEnd = scriptText . IndexOf ( '\n' , match . Index ) ;
if ( lineEnd = = - 1 ) lineEnd = scriptText . Length ;
string line = scriptText . Substring ( lineStart , lineEnd - lineStart ) . TrimStart ( ) ;
if ( ! line . StartsWith ( "//" ) )
{
{
bool match = true ;
int byteIndex = Encoding . Unicode . GetByteCount ( scriptText . Substring ( 0 , match . Index ) ) ;
for ( int j = 0 ; j < searchBytes . Length ; j + + )
results . Add ( ( match . Value , byteIndex ) ) ;
{
}
if ( scriptBytes [ index + j ] ! = searchBytes [ j ] )
}
{
match = false ;
// 2. Find all lines that start with @ and are not commented out
break ;
int currentIndex = 0 ;
}
foreach ( var line in scriptText . Split ( '\n' ) )
}
{
if ( match )
string trimmedLine = line . TrimStart ( ) ;
{
if ( ! trimmedLine . StartsWith ( "//" ) & & trimmedLine . StartsWith ( "@ " ) )
// Find the character index in the string corresponding to this byte index
{
int charIndex = Encoding . Unicode . GetString ( scriptBytes , 0 , index ) . Length ;
int byteIndex = Encoding . Unicode . GetByteCount ( scriptText . Substring ( 0 , currentIndex ) ) ;
// Find the start of the line
results . Add ( ( line , byteIndex ) ) ;
int lineStart = scriptText . LastIndexOf ( '\n' , charIndex - 1 ) ;
if ( lineStart = = - 1 ) lineStart = 0 ; else lineStart + + ;
int lineEnd = scriptText . IndexOf ( '\n' , charIndex ) ;
if ( lineEnd = = - 1 ) lineEnd = scriptText . Length ;
string line = scriptText . Substring ( lineStart , lineEnd - lineStart ) . TrimStart ( ) ;
// Only add if the line does NOT start with //
if ( ! line . StartsWith ( "//" ) )
{
results . Add ( ( search , index ) ) ;
}
index + = searchBytes . Length ; // Move past this match
}
else
{
index + + ;
}
}
}
currentIndex + = line . Length + 1 ; // +1 for '\n'
}
}
// Sort by position in order of appearance
return results . OrderBy ( r = > r . Item2 ) . ToList ( ) ;
return results . OrderBy ( r = > r . Item2 ) . ToList ( ) ;
}
}