Skip to content

Commit

Permalink
Added LuaGenerator from Dr. Underscore and generated natives.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
Rayope committed May 22, 2021
1 parent 03b075d commit a187aeb
Show file tree
Hide file tree
Showing 6 changed files with 134,131 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ImGTA/lua_console_mod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ bool LuaConsoleMod::Draw()
ImGui::SetWindowFontScale(m_contentFontSize);

ImGui::TextColored(ImVec4(255, 0, 0, 255), "Needs testing, can crash your game.");
ImGui::TextColored(ImVec4(0, 25, 0, 255), "Look at natives.lua for function names. Examples:");
ImGui::TextColored(ImVec4(0, 25, 0, 255), "val = entity.get_entity_health(2)");
ImGui::TextColored(ImVec4(0, 25, 0, 255), "print(val)");
ImGui::TextColored(ImVec4(0, 25, 0, 255), "entity.set_entity_health(2, 150)");

const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); // thanks omar
if (ImGui::BeginChild("ConsoleText", ImVec2(0.0f, -footer_height_to_reserve)))
{
ImGui::TextUnformatted(lua_stream.str().c_str());
ImGui::EndChild(); // legacy dont-care-about-return-value end.
ImGui::SetScrollHereY(0.999f);
ImGui::EndChild(); // legacy dont-care-about-return-value end.

}

ImGui::Separator();
ImGui::SetNextItemWidth(ImGui::GetCurrentWindow()->Size.x - 75.0f);

Expand Down
85 changes: 85 additions & 0 deletions LuaGenerator/pom.xml
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>
3 changes: 3 additions & 0 deletions LuaGenerator/src/main/java/META-INF/MANIFEST.MF
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 LuaGenerator/src/main/java/xyz/jame/gtaluagen/GTALuaGen.java
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();
}
}
}
Loading

0 comments on commit a187aeb

Please sign in to comment.