Skip to content

Commit

Permalink
オブファスケーターのユーザー関数のインライン展開対応、コマンドライン引数の整理
Browse files Browse the repository at this point in the history
  • Loading branch information
r-koubou committed Mar 26, 2018
1 parent e836875 commit 91a85c1
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
48 changes: 43 additions & 5 deletions src/java/net/rkoubou/kspparser/KSPSyntaxParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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=#### の指定があった場合、そのエンコードを標準出力・エラーに再設定する
Expand All @@ -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;
}
Expand Down Expand Up @@ -126,11 +134,41 @@ 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 );
if( stdout != null ){ try{ stdout.close(); } catch( Throwable e ){} }
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1438,7 +1439,7 @@ public Object visit( ASTUserFunctionDeclaration node, Object data )
}

/**
* ーザー定義関数呼び出し
* ユーザー定義関数呼び出し
*/
@Override
public Object visit( ASTCallUserFunctionStatement node, Object data )
Expand All @@ -1451,6 +1452,7 @@ public Object visit( ASTCallUserFunctionStatement node, Object data )
return node;
}
f.referenced = true;
f.referenceCount++;
return node;
}

Expand Down
2 changes: 2 additions & 0 deletions src/java/net/rkoubou/kspparser/analyzer/SymbolDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) */
Expand Down
16 changes: 13 additions & 3 deletions src/java/net/rkoubou/kspparser/obfuscator/Obfuscator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* オブファスケーター実行クラス
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static public CmdLineParser setup( String[] args ) throws CmdLineException

if( options.obfuscate )
{
// 警告メッセージ出力を抑制&オブファスケート時にシュリンクする
options.strict = false;
}
if( options.strict )
Expand Down
22 changes: 16 additions & 6 deletions src/java/net/rkoubou/kspparser/options/KSPParserOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package net.rkoubou.kspparser.options;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

/**
Expand All @@ -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;

}

0 comments on commit 91a85c1

Please sign in to comment.