forked from drunderscore/ImGTA
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added LuaGenerator from Dr. Underscore and generated natives.lua
- Loading branch information
Showing
6 changed files
with
134,131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>xyz.jame.gtaluagen</groupId> | ||
<artifactId>GTALuaGen</artifactId> | ||
<version>1.0</version> | ||
|
||
<name>GTALuaGen</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.8.6</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli --> | ||
<dependency> | ||
<groupId>commons-cli</groupId> | ||
<artifactId>commons-cli</artifactId> | ||
<version>1.4</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> | ||
<plugins> | ||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> | ||
<plugin> | ||
<artifactId>maven-clean-plugin</artifactId> | ||
<version>3.1.0</version> | ||
</plugin> | ||
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> | ||
<plugin> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
<version>3.0.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.0.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-install-plugin</artifactId> | ||
<version>2.5.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>2.8.2</version> | ||
</plugin> | ||
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> | ||
<plugin> | ||
<artifactId>maven-site-plugin</artifactId> | ||
<version>3.7.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-project-info-reports-plugin</artifactId> | ||
<version>3.0.0</version> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: xyz.jame.gtaluagen.GTALuaGen | ||
|
138 changes: 138 additions & 0 deletions
138
LuaGenerator/src/main/java/xyz/jame/gtaluagen/GTALuaGen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package xyz.jame.gtaluagen; | ||
|
||
import com.google.gson.*; | ||
import java.io.*; | ||
import java.nio.file.*; | ||
import java.text.*; | ||
import java.util.*; | ||
import org.apache.commons.cli.ParseException; | ||
import org.apache.commons.cli.*; | ||
|
||
public class GTALuaGen | ||
{ | ||
public static final Options CLI_OPTIONS = new Options().addOption( Option.builder( "i" ).longOpt( "input" ).desc( "Input natives.json file." ).hasArg().required().argName( "path" ).build() ).addOption( Option.builder( "o" ).longOpt( "output" ).desc( "Output natives.lua file." ).hasArg().argName( "path" ).build() ).addOption( Option.builder( "h" ).longOpt( "help" ).desc( "Show this help information." ).build() ); | ||
public static final String SIGNATURE_STRING = "Generated by GTALuaGen, by James Puleo, for ImGta."; | ||
public static final String[] LUA_RESERVED = new String[]{ "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while" }; | ||
|
||
public static void main( String[] args ) | ||
{ | ||
CommandLineParser parser = new DefaultParser(); | ||
HelpFormatter help = new HelpFormatter(); | ||
try | ||
{ | ||
CommandLine line = parser.parse( CLI_OPTIONS, args ); | ||
if ( line.hasOption( 'h' ) ) | ||
{ | ||
help.printHelp( "GTALuaGen", CLI_OPTIONS ); | ||
System.exit( 0 ); | ||
} | ||
File input = new File( line.getOptionValue( 'i' ) ); | ||
File output = new File( line.getOptionValue( 'o', "natives.lua" ) ); | ||
if ( !input.exists() ) | ||
{ | ||
System.err.println( "Input file does not exist." ); | ||
System.exit( 0 ); | ||
} | ||
|
||
writeLuaNatives( input, output ); | ||
} | ||
catch ( ParseException e ) | ||
{ | ||
System.err.println( e.getMessage() ); | ||
help.printHelp( "GTALuaGen", CLI_OPTIONS ); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void writeLuaNatives( File input, File output ) throws IOException | ||
{ | ||
int nativeCount = 0; | ||
BufferedWriter writer = null; | ||
try | ||
{ | ||
JsonObject json = JsonParser.parseString( new String( Files.readAllBytes( input.toPath() ) ) ).getAsJsonObject(); | ||
writer = new BufferedWriter( new FileWriter( output ) ); | ||
writer.write( "-- " + SIGNATURE_STRING + "\n" ); | ||
writer.write( "-- " + new SimpleDateFormat( "MM/dd/yyyy hh:mm aa" ).format( new Date( System.currentTimeMillis() ) ) + "\n\n" ); | ||
|
||
for ( String namespace : json.keySet() ) | ||
{ | ||
writer.write( namespace.toLowerCase() + " = {}\n" ); | ||
JsonObject jsonNamespace = json.getAsJsonObject( namespace ); | ||
for ( String nativeAddr : jsonNamespace.keySet() ) | ||
{ | ||
nativeCount++; | ||
JsonObject jsonNative = jsonNamespace.getAsJsonObject( nativeAddr ); | ||
String nativeName = jsonNative.get( "name" ).getAsString(); | ||
if ( nativeName.isEmpty() ) | ||
nativeName = "_" + nativeAddr; | ||
String nativeJenkins = jsonNative.get( "jhash" ).getAsString(); | ||
|
||
writer.write( "function " + namespace.toLowerCase() + "." + nativeName.toLowerCase() + "(" ); | ||
|
||
JsonArray nativeParams = jsonNative.getAsJsonArray( "params" ); | ||
for ( int i = 0; i < nativeParams.size(); i++ ) | ||
{ | ||
JsonObject p = nativeParams.get( i ).getAsJsonObject(); | ||
String paramName = p.get( "name" ).getAsString(); | ||
final String finalParamName = paramName; | ||
if ( Arrays.stream( LUA_RESERVED ).anyMatch( resv -> resv.equalsIgnoreCase( finalParamName ) ) ) | ||
paramName = "_" + paramName; | ||
|
||
writer.write( paramName ); | ||
if ( i != nativeParams.size() - 1 ) | ||
writer.write( ", " ); | ||
} | ||
writer.write( ")" ); | ||
if ( !nativeJenkins.isEmpty() ) | ||
writer.write( " -- " + nativeJenkins ); | ||
writer.write( "\n" ); | ||
|
||
String retType = jsonNative.get( "return_type" ).getAsString(); | ||
//String retType = jsonNative.get( "results" ).getAsString(); | ||
String funcName; | ||
if ( retType.startsWith( "float" ) ) | ||
funcName = "invoke"; | ||
else if ( retType.equalsIgnoreCase( "char*" ) ) | ||
funcName = "invokeString"; | ||
else | ||
funcName = "invokeInteger"; | ||
|
||
writer.write( "\treturn " + funcName + "(" + nativeAddr ); | ||
if ( nativeParams.size() != 0 ) | ||
writer.write( ", " ); | ||
|
||
// FIXME: duplicate code! | ||
for ( int i = 0; i < nativeParams.size(); i++ ) | ||
{ | ||
JsonObject p = nativeParams.get( i ).getAsJsonObject(); | ||
String paramName = p.get( "name" ).getAsString(); | ||
final String finalParamName = paramName; | ||
if ( Arrays.stream( LUA_RESERVED ).anyMatch( resv -> resv.equalsIgnoreCase( finalParamName ) ) ) | ||
paramName = "_" + paramName; | ||
writer.write( paramName ); | ||
|
||
if ( i != nativeParams.size() - 1 ) | ||
writer.write( ", " ); | ||
} | ||
writer.write( ")\n" ); | ||
writer.write( "end\n\n" ); | ||
} | ||
} | ||
System.out.println( "Created natives.lua for " + nativeCount + " functions." ); | ||
} | ||
catch ( JsonParseException e ) | ||
{ | ||
System.err.println( "Unable to parse JSON input." ); | ||
System.err.println( e.getMessage() ); | ||
} | ||
finally | ||
{ | ||
if ( writer != null ) | ||
writer.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.