summaryrefslogtreecommitdiffstats
path: root/Tools/CompressShaders/LanguageCodes.cs
diff options
context:
space:
mode:
authorKonstantin <const@const.me>2023-01-16 14:52:43 +0100
committerKonstantin <const@const.me>2023-01-16 14:52:43 +0100
commit8c4603c73675958efc960fbd4bb599a2909d106a (patch)
tree714dc6fc9a1672d5fd7f89676b97e10959662abc /Tools/CompressShaders/LanguageCodes.cs
parent990a8d0dbaefc996244097397259e92758b15cce (diff)
Source codes
Diffstat (limited to 'Tools/CompressShaders/LanguageCodes.cs')
-rw-r--r--Tools/CompressShaders/LanguageCodes.cs103
1 files changed, 103 insertions, 0 deletions
diff --git a/Tools/CompressShaders/LanguageCodes.cs b/Tools/CompressShaders/LanguageCodes.cs
new file mode 100644
index 0000000..71a9909
--- /dev/null
+++ b/Tools/CompressShaders/LanguageCodes.cs
@@ -0,0 +1,103 @@
+using System.Globalization;
+using System.Text.RegularExpressions;
+
+namespace CompressShaders
+{
+ static class LanguageCodes
+ {
+ record struct Row
+ {
+ public string keySource;
+ public uint keyValue;
+ public int code;
+ public string name;
+ }
+
+ static uint makeKey( string str )
+ {
+ if( str.Length > 4 )
+ throw new ArgumentException();
+ uint k = 0;
+ int shift = 0;
+ foreach( char c in str )
+ {
+ if( c >= 0x80 )
+ throw new ArgumentException();
+ uint u = (uint)c;
+ k |= ( u << shift );
+ shift += 8;
+ }
+ return k;
+ }
+
+ static IEnumerable<Row> load( string path )
+ {
+ using var stm = File.OpenText( path );
+ while( true )
+ {
+ string? line = stm.ReadLine();
+ if( null == line )
+ break;
+ if( string.IsNullOrWhiteSpace( line ) )
+ continue;
+ string[] fields = line.Split( '\t' );
+ yield return new Row()
+ {
+ keySource = fields[ 0 ],
+ keyValue = makeKey( fields[ 0 ] ),
+ code = int.Parse( fields[ 1 ] ),
+ name = fields[ 2 ]
+ };
+ }
+ }
+
+ static void writeCpp( string inl, Row[] data )
+ {
+ // TODO [very low]: sort them by the key here, then in C++ use binary search instead of the hash map
+ using var stm = File.CreateText( inl );
+ stm.WriteLine( "// This file is generated by a tool, from the `languageCodez.tsv` file in this repository" );
+ foreach( Row row in data )
+ stm.WriteLine( "Lang{{ 0x{0:X}, {1}, \"{2}\" }},", row.keyValue, row.code, row.name );
+ }
+
+ static readonly CultureInfo ci = new CultureInfo( "en-US", false );
+ static string titleCase( this string name ) =>
+ ci.TextInfo.ToTitleCase( name.ToLower( ci ) );
+
+ static void writeCs( string cs, Row[] data )
+ {
+ using var stm = File.CreateText( cs );
+ stm.WriteLine( @"// This file is generated by a tool, from the `languageCodez.tsv` file in this repository
+namespace Whisper
+{
+ /// <summary>Supported languages</summary>
+ public enum eLanguage: uint
+ {" );
+
+ foreach( Row row in data )
+ {
+ string tc = row.name.titleCase();
+ stm.WriteLine( " /// <summary>{0}</summary>", tc );
+ tc = Regex.Replace( tc, @"\s+", string.Empty );
+ stm.WriteLine( " {0} = 0x{1:X},", tc, row.keyValue );
+ }
+ stm.Write( @" }
+}" );
+ }
+
+ static void produce( string tsv, string inl, string cs )
+ {
+ Row[] data = load( tsv ).OrderBy( r => r.name ).ToArray();
+ writeCpp( inl, data );
+ writeCs( cs, data );
+ }
+
+ public static void produce( string solutionRoot )
+ {
+ string tsv = Path.Combine( solutionRoot, "Whisper\\Whisper\\languageCodez.tsv" );
+ string inl = Path.Combine( solutionRoot, "Whisper\\Whisper\\languageCodez.inl" );
+ string cs = Path.Combine( solutionRoot, "WhisperNet\\API\\eLanguage.cs" );
+ produce( tsv, inl, cs );
+ }
+ }
+} \ No newline at end of file