diff --git a/src/java/net/rkoubou/kspparser/KSPSyntaxParser.java b/src/java/net/rkoubou/kspparser/KSPSyntaxParser.java index 3c7b32f..a181274 100644 --- a/src/java/net/rkoubou/kspparser/KSPSyntaxParser.java +++ b/src/java/net/rkoubou/kspparser/KSPSyntaxParser.java @@ -8,10 +8,12 @@ package net.rkoubou.kspparser; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.PrintStream; import java.io.Writer; +import org.kohsuke.args4j.CmdLineException; import org.kohsuke.args4j.CmdLineParser; import net.rkoubou.kspparser.analyzer.AnalyzeErrorCounter; @@ -33,8 +35,9 @@ public class KSPSyntaxParser */ static public void main( String[] args ) throws Throwable { - PrintStream stdout = null; - PrintStream stderr = null; + PrintStream stdout = null; + PrintStream stderr = null; + CmdLineParser cmdLineParser = null; try { // -Dkspparser.stdout.encoding=#### の指定があった場合、そのエンコードを標準出力・エラーに再設定する @@ -47,11 +50,16 @@ static public void main( String[] args ) throws Throwable System.setErr( stderr ); } // コマンドライン引数の解析 - CmdLineParser cmdLineParser = CommandlineOptions.setup( args ); + cmdLineParser = CommandlineOptions.setup( args ); + if( CommandlineOptions.options.usage ) + { + usage( cmdLineParser ); + System.exit( 1 ); + return; + } if( CommandlineOptions.options.sourceFile == null ) { - cmdLineParser.printUsage( System.err ); - System.err.println(); + usage( cmdLineParser ); System.exit( 1 ); return; } @@ -126,6 +134,16 @@ static public void main( String[] args ) throws Throwable return; } } + catch( CmdLineException cmd ) + { + System.err.println( cmd.getMessage() ); + usage( cmdLineParser ); + System.exit( 1 ); + } + catch( FileNotFoundException fne ) + { + System.err.println( "script not found : " + fne.getMessage() ); + } finally { //AnalyzeErrorCounter.dump( System.out ); @@ -133,4 +151,24 @@ static public void main( String[] args ) throws Throwable if( stderr != null ){ try{ stdout.close(); } catch( Throwable e ){} } } } + + /** + * コマンドライン引数の表示 + */ + static private void usage( CmdLineParser p ) + { + System.err.println( "usage" ); + System.err.println( " java -jar ./KSPSyntaxParser.jar [options] source" ); + if( p != null ) + { + System.err.println(); + p.printUsage( System.err ); + } + else + { + System.err.println( "please type -h to show usage" ); + } + System.err.println(); + } + } diff --git a/src/java/net/rkoubou/kspparser/analyzer/SemanticAnalyzer.java b/src/java/net/rkoubou/kspparser/analyzer/SemanticAnalyzer.java index a190a70..2194760 100644 --- a/src/java/net/rkoubou/kspparser/analyzer/SemanticAnalyzer.java +++ b/src/java/net/rkoubou/kspparser/analyzer/SemanticAnalyzer.java @@ -1138,6 +1138,7 @@ public Object visit( ASTRefVariable node, Object data ) } ret.symbol.reserved = v.reserved; v.referenced = true; + v.referenceCount++; v.state = SymbolState.LOADED; return ret; } @@ -1438,7 +1439,7 @@ public Object visit( ASTUserFunctionDeclaration node, Object data ) } /** - * ーザー定義関数呼び出し + * ユーザー定義関数呼び出し */ @Override public Object visit( ASTCallUserFunctionStatement node, Object data ) @@ -1451,6 +1452,7 @@ public Object visit( ASTCallUserFunctionStatement node, Object data ) return node; } f.referenced = true; + f.referenceCount++; return node; } diff --git a/src/java/net/rkoubou/kspparser/analyzer/SymbolDefinition.java b/src/java/net/rkoubou/kspparser/analyzer/SymbolDefinition.java index 27dbd03..143aaac 100644 --- a/src/java/net/rkoubou/kspparser/analyzer/SymbolDefinition.java +++ b/src/java/net/rkoubou/kspparser/analyzer/SymbolDefinition.java @@ -44,6 +44,8 @@ public enum SymbolType public SymbolState state = SymbolState.UNLOADED; /** 意味解析フェーズ中に走査し参照されたかを記録する */ public boolean referenced = false; + /** 意味解析フェーズ中に走査し参照された回数を記録する */ + public int referenceCount = 0; /** accessFlagにACCESS_ATTR_UIが含まれている場合のUIタイプの識別子名 */ public String uiTypeName = ""; /** 値がある場合はその値(Integer,Double,String,int[],double[],String[]) */ diff --git a/src/java/net/rkoubou/kspparser/obfuscator/Obfuscator.java b/src/java/net/rkoubou/kspparser/obfuscator/Obfuscator.java index f27383f..0727ad5 100644 --- a/src/java/net/rkoubou/kspparser/obfuscator/Obfuscator.java +++ b/src/java/net/rkoubou/kspparser/obfuscator/Obfuscator.java @@ -59,6 +59,7 @@ import net.rkoubou.kspparser.javacc.generated.ASTWhileStatement; import net.rkoubou.kspparser.javacc.generated.Node; import net.rkoubou.kspparser.javacc.generated.SimpleNode; +import net.rkoubou.kspparser.options.CommandlineOptions; /** * オブファスケーター実行クラス @@ -872,7 +873,7 @@ public Object visit( ASTUserFunctionDeclaration node, Object data ) Node block = node.jjtGetChild( 0 ); UserFunction func = userFunctionTable.search( node.symbol.getName() ); - if( func.referenced ) + if( !CommandlineOptions.options.inlineUserFunction && func.referenced ) { outputCode.append( "function " ).append( func.getName() ); appendEOL(); @@ -891,8 +892,17 @@ public Object visit( ASTUserFunctionDeclaration node, Object data ) public Object visit( ASTCallUserFunctionStatement node, Object data ) { UserFunction func = userFunctionTable.search( node.symbol ); - outputCode.append( "call " ).append( func.getName() ); - appendEOL(); + if( CommandlineOptions.options.inlineUserFunction ) + { + // インライン展開 + Node block = func.astNode.jjtGetChild( 0 ); + block.jjtAccept( this, data ); + } + else + { + outputCode.append( "call " ).append( func.getName() ); + appendEOL(); + } return node; } diff --git a/src/java/net/rkoubou/kspparser/options/CommandlineOptions.java b/src/java/net/rkoubou/kspparser/options/CommandlineOptions.java index 669adad..627587a 100644 --- a/src/java/net/rkoubou/kspparser/options/CommandlineOptions.java +++ b/src/java/net/rkoubou/kspparser/options/CommandlineOptions.java @@ -33,6 +33,7 @@ static public CmdLineParser setup( String[] args ) throws CmdLineException if( options.obfuscate ) { + // 警告メッセージ出力を抑制&オブファスケート時にシュリンクする options.strict = false; } if( options.strict ) diff --git a/src/java/net/rkoubou/kspparser/options/KSPParserOptions.java b/src/java/net/rkoubou/kspparser/options/KSPParserOptions.java index 597c7f7..b17791f 100644 --- a/src/java/net/rkoubou/kspparser/options/KSPParserOptions.java +++ b/src/java/net/rkoubou/kspparser/options/KSPParserOptions.java @@ -7,6 +7,7 @@ package net.rkoubou.kspparser.options; +import org.kohsuke.args4j.Argument; import org.kohsuke.args4j.Option; /** @@ -15,26 +16,35 @@ public class KSPParserOptions { /** 解析対象のスクリプトファイルパス */ - @Option( name = "--source", usage = "[REQUIRED] ksp script file path" ) + @Option( name = "-h",aliases = "--help", usage = "print commandline options" ) + public boolean usage; + + /** 解析対象のスクリプトファイルパス */ + @Argument( index = 0, metaVar = "source", usage = "ksp script file path" ) public String sourceFile; /** 解析後、ファイルを出力する場合に使用するスクリプトファイルパス */ - @Option( name = "--output", usage = "output file path when --obfuscate is enabled" ) + @Option( name = "-o", aliases = "--output", usage = "output file path when --obfuscate is enabled" ) public String outputFile; /** 意味解析を行わず、構文解析のみ実行するかどうか */ - @Option( name = "--parseonly", usage = "syntax analysis only" ) + @Option( name = "-p", aliases = "--parseonly", usage = "syntax analysis only" ) public Boolean parseonly = false; /** 厳密なチェックを行うかどうか */ - @Option( name = "--strict", usage = "strict mode" ) + @Option( name = "-s", aliases = "--strict", usage = "strict mode" ) public Boolean strict = false; /** 未使用変数、ユーザー定義関数を警告扱いにするかどうか */ - @Option( name = "--unused", usage = "scan unused variable and function" ) + @Option( name = "-u", aliases = "--unused", usage = "scan unused variable and function" ) public Boolean unused = false; /** オブファスケートを行うかどうか */ - @Option( name = "--obfuscate", usage = "obfuscate script" ) + @Option( name = "-O", aliases = "--obfuscate", usage = "obfuscate script" ) public Boolean obfuscate = false; + + /** 全てのユーザー定義関数をインライン展開するかどうか */ + @Option( name = "-OI", aliases = "--inline-userfunction", usage = "inline user definition user functions will be expanded (require --obfuscate)" ) + public Boolean inlineUserFunction = false; + }