From 91a85c1e78b9a4251fb9b4b7bbfaeea27d550def Mon Sep 17 00:00:00 2001 From: R-Koubou Date: Mon, 26 Mar 2018 18:52:52 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AA=E3=83=96=E3=83=95=E3=82=A1=E3=82=B9?= =?UTF-8?q?=E3=82=B1=E3=83=BC=E3=82=BF=E3=83=BC=E3=81=AE=E3=83=A6=E3=83=BC?= =?UTF-8?q?=E3=82=B6=E3=83=BC=E9=96=A2=E6=95=B0=E3=81=AE=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=83=B3=E5=B1=95=E9=96=8B=E5=AF=BE=E5=BF=9C?= =?UTF-8?q?=E3=80=81=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89=E3=83=A9=E3=82=A4?= =?UTF-8?q?=E3=83=B3=E5=BC=95=E6=95=B0=E3=81=AE=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rkoubou/kspparser/KSPSyntaxParser.java | 48 +++++++++++++++++-- .../kspparser/analyzer/SemanticAnalyzer.java | 4 +- .../kspparser/analyzer/SymbolDefinition.java | 2 + .../kspparser/obfuscator/Obfuscator.java | 16 +++++-- .../kspparser/options/CommandlineOptions.java | 1 + .../kspparser/options/KSPParserOptions.java | 22 ++++++--- 6 files changed, 78 insertions(+), 15 deletions(-) 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; + }