mirror of
https://github.com/jwdeveloper/TikTokLiveJava.git
synced 2026-02-27 08:49:40 -05:00
Events handling
This commit is contained in:
62
Tools/pom.xml
Normal file
62
Tools/pom.xml
Normal file
@@ -0,0 +1,62 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>TikTokLiveJava</artifactId>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>Tools</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.22</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.22</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup</groupId>
|
||||
<artifactId>javapoet</artifactId>
|
||||
<version>1.13.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.reflections</groupId>
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>0.9.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<artifactId>API</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<artifactId>Client</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.github.jwdeveloper.tiktok</groupId>
|
||||
<artifactId>Client</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>16</maven.compiler.source>
|
||||
<maven.compiler.target>16</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,45 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.events_generator.EventGeneratorSettings;
|
||||
import io.github.jwdeveloper.tiktok.events_generator.EventsGenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class EventsGeneratorRun {
|
||||
|
||||
private static boolean lock = false;
|
||||
|
||||
//Run objects
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
if(lock)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//generatesObjects()
|
||||
// generateEventsMessages();
|
||||
}
|
||||
|
||||
|
||||
private static void generatesEventsObjects() throws IOException {
|
||||
var settings = new EventGeneratorSettings();
|
||||
settings.setTikTokEvent(false);
|
||||
settings.setInputDictionary("C:\\Users\\ja\\RiderProjects\\TikTokLiveSharp\\TikTokLiveSharp\\Events\\Objects");
|
||||
settings.setOutputDictionary("C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\API\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\events\\objects");
|
||||
var generator = new EventsGenerator();
|
||||
generator.compile(settings);
|
||||
}
|
||||
|
||||
private static void generateEventsMessages() throws IOException {
|
||||
var settings = new EventGeneratorSettings();
|
||||
settings.setTikTokEvent(true);
|
||||
settings.setPrefix("TikTok");
|
||||
settings.setEndFix("Event");
|
||||
settings.setInputDictionary("C:\\Users\\ja\\RiderProjects\\TikTokLiveSharp\\TikTokLiveSharp\\Events\\Messages");
|
||||
settings.setOutputDictionary("C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\API\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\events\\messages");
|
||||
var generator = new EventsGenerator();
|
||||
generator.compile(settings);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.events_generator.EventGeneratorSettings;
|
||||
import io.github.jwdeveloper.tiktok.intefacee.EventsInterfaceGenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class EventsInterfaceGeneratorRun
|
||||
{
|
||||
public static void main(String[] args) throws IOException {
|
||||
var settings = new EventGeneratorSettings();
|
||||
settings.setTikTokEvent(true);
|
||||
settings.setPrefix("TikTok");
|
||||
settings.setEndFix("Event");
|
||||
settings.setInputDictionary("C:\\Users\\ja\\RiderProjects\\TikTokLiveSharp\\TikTokLiveSharp\\Events\\Messages");
|
||||
settings.setOutputDictionary("C:\\Users\\ja\\IdeaProjects\\TikTokLiveJava\\API\\src\\main\\java\\io\\github\\jwdeveloper\\tiktok\\events\\messages");
|
||||
var generator = new EventsInterfaceGenerator();
|
||||
generator.compile(settings);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FilesUtility
|
||||
{
|
||||
public static List<Path> getFiles(String input) {
|
||||
Path path = Paths.get(input);
|
||||
try (Stream<Path> paths = Files.list(path)) {
|
||||
return paths.filter(Files::isRegularFile).toList();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void saveFile(String path, String content)
|
||||
{
|
||||
Path filePath = Paths.get(path);
|
||||
try {
|
||||
// Write the content to the file
|
||||
Files.write(filePath, content.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.github.jwdeveloper.tiktok;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.github.jwdeveloper.tiktok.events_generator;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class CSharpClassInfo
|
||||
{
|
||||
private String className;
|
||||
private List<FieldInfo> fields = new ArrayList<>();
|
||||
private List<ConstructorInfo> constructors = new ArrayList<>();
|
||||
|
||||
public void addField(String type, String fields)
|
||||
{
|
||||
this.fields.add(new FieldInfo(type,fields));
|
||||
}
|
||||
|
||||
public void addConstructor(List<FieldInfo> arguments)
|
||||
{
|
||||
this.constructors.add(new ConstructorInfo(arguments));
|
||||
}
|
||||
|
||||
public record FieldInfo(String type, String name){};
|
||||
|
||||
public record ConstructorInfo(List<FieldInfo> arguemtns){};
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package io.github.jwdeveloper.tiktok.events_generator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CSharpClassParser {
|
||||
private CSharpClassInfo classInfo;
|
||||
|
||||
public CSharpClassInfo parse(Path filePath) throws IOException {
|
||||
classInfo = new CSharpClassInfo();
|
||||
|
||||
List<String> lines = Files.readAllLines(filePath);
|
||||
String content = String.join("\n", lines);
|
||||
parseClassName(content);
|
||||
parseFields(content);
|
||||
parseConstructors(content);
|
||||
return classInfo;
|
||||
}
|
||||
|
||||
|
||||
private void parseClassName(String content) {
|
||||
Pattern pattern = Pattern.compile("\\b(?:sealed )?class\\s+(\\w+)");
|
||||
Matcher matcher = pattern.matcher(content);
|
||||
if (matcher.find()) {
|
||||
classInfo.setClassName(matcher.group(1));
|
||||
}
|
||||
}
|
||||
|
||||
private void parseFields(String content) {
|
||||
Pattern pattern = Pattern.compile("\\b(public|private|protected)\\s+(readonly\\s+)?(\\w+\\.?\\w*)\\s+(\\w+);");
|
||||
Matcher matcher = pattern.matcher(content);
|
||||
while (matcher.find()) {
|
||||
var typeName = mapTypeToJava(matcher.group(3));
|
||||
var name = lowerCaseFirstLetter(matcher.group(4));
|
||||
classInfo.addField(typeName, name);
|
||||
}
|
||||
}
|
||||
|
||||
private void parseConstructors(String content) {
|
||||
Pattern pattern = Pattern.compile("\\b(public|private|protected|internal)\\s+" + classInfo.getClassName() + "\\s*\\(([^)]*)\\)");
|
||||
Matcher matcher = pattern.matcher(content);
|
||||
while (matcher.find()) {
|
||||
List<CSharpClassInfo.FieldInfo> args = new ArrayList<>();
|
||||
String[] arguments = matcher.group(2).split(",");
|
||||
for (String argument : arguments) {
|
||||
if (argument.trim().length() > 0) {
|
||||
String[] parts = argument.trim().split("\\s+");
|
||||
|
||||
if (parts.length != 2) {
|
||||
args.add(new CSharpClassInfo.FieldInfo("Object", "error"));
|
||||
continue;
|
||||
}
|
||||
var typeName = mapTypeToJava(parts[0]);
|
||||
var name = lowerCaseFirstLetter(parts[1]);
|
||||
args.add(new CSharpClassInfo.FieldInfo(typeName, name));
|
||||
}
|
||||
}
|
||||
classInfo.addConstructor(args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String mapTypeToJava(String type) {
|
||||
if (type.equals("string")) {
|
||||
return "String";
|
||||
}
|
||||
if (type.equals("uint")) {
|
||||
return "Integer";
|
||||
}
|
||||
if (type.equals("int")) {
|
||||
return "Integer";
|
||||
}
|
||||
if (type.equals("ulong")) {
|
||||
return "Long";
|
||||
}
|
||||
if (type.equals("bool")) {
|
||||
return "Boolean";
|
||||
}
|
||||
if (type.contains("Models.Protobuf.Objects")) {
|
||||
return type.replace("Models.Protobuf.Objects", "io.github.jwdeveloper.tiktok.messages");
|
||||
}
|
||||
|
||||
if(type.contains("Objects."))
|
||||
{
|
||||
return type.replace("Objects.","io.github.jwdeveloper.tiktok.events.objects.");
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
public static String lowerCaseFirstLetter(String str) {
|
||||
if (str == null || str.isEmpty()) {
|
||||
return str; // Return original string if it is empty or null
|
||||
}
|
||||
return Character.toLowerCase(str.charAt(0)) + str.substring(1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package io.github.jwdeveloper.tiktok.events_generator;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class EventGeneratorSettings
|
||||
{
|
||||
private String inputDictionary;
|
||||
|
||||
private String outputDictionary;
|
||||
private List<String> ignoredFiles = new ArrayList<>();
|
||||
|
||||
private String prefix;
|
||||
|
||||
private String endFix;
|
||||
|
||||
private boolean isTikTokEvent;
|
||||
|
||||
public void addIgnoredClass(String name)
|
||||
{
|
||||
ignoredFiles.add(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package io.github.jwdeveloper.tiktok.events_generator;
|
||||
|
||||
import io.github.jwdeveloper.tiktok.FilesUtility;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class EventsGenerator
|
||||
{
|
||||
|
||||
|
||||
public void compile(EventGeneratorSettings settings) throws IOException {
|
||||
var files = FilesUtility.getFiles(settings.getInputDictionary());
|
||||
|
||||
var packageName = convertToPackageName(settings.getOutputDictionary());
|
||||
for(var file : files)
|
||||
{
|
||||
var fileName = file.getFileName().toString();
|
||||
if(settings.getIgnoredFiles().contains(fileName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if(fileName.contains("meta"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var parser = new CSharpClassParser();
|
||||
var cSharpClass =parser.parse(file);
|
||||
|
||||
var name = settings.getPrefix()+cSharpClass.getClassName()+settings.getEndFix();
|
||||
cSharpClass.setClassName(name);
|
||||
var javaClassGenerator = new JavaClassGenerator();
|
||||
|
||||
|
||||
var result =javaClassGenerator.generate(cSharpClass, packageName,settings);
|
||||
System.out.println(result);
|
||||
|
||||
var path = settings.getOutputDictionary()+ File.separator+cSharpClass.getClassName()+".java";
|
||||
FilesUtility.saveFile(path, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static String convertToPackageName(String path) {
|
||||
String marker = "src\\main\\java\\";
|
||||
int index = path.indexOf(marker);
|
||||
|
||||
if (index != -1) {
|
||||
String packagePath = path.substring(index + marker.length());
|
||||
return packagePath.replace('\\', '.');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package io.github.jwdeveloper.tiktok.events_generator;
|
||||
|
||||
import com.squareup.javapoet.*;
|
||||
import io.github.jwdeveloper.tiktok.events.TikTokEvent;
|
||||
import lombok.Getter;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
|
||||
|
||||
public class JavaClassGenerator {
|
||||
public String generate(CSharpClassInfo cSharpClassInfo, String packageName, EventGeneratorSettings settings) {
|
||||
|
||||
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(cSharpClassInfo.getClassName())
|
||||
.addAnnotation(Getter.class);
|
||||
if (settings.isTikTokEvent()) {
|
||||
classBuilder.superclass(TikTokEvent.class);
|
||||
}
|
||||
classBuilder.addModifiers(Modifier.PUBLIC);
|
||||
|
||||
// Generate fields
|
||||
for (var field : cSharpClassInfo.getFields()) {
|
||||
|
||||
FieldSpec fieldSpec = FieldSpec.builder(ClassName.bestGuess(field.type()), field.name(), Modifier.PRIVATE).build();
|
||||
classBuilder.addField(fieldSpec);
|
||||
}
|
||||
|
||||
// Generate constructors
|
||||
for (var constructor : cSharpClassInfo.getConstructors()) {
|
||||
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
|
||||
|
||||
if(settings.isTikTokEvent())
|
||||
{
|
||||
constructorBuilder.addStatement("super(msg.getHeader());") ;
|
||||
}
|
||||
|
||||
constructorBuilder.addModifiers(Modifier.PUBLIC);
|
||||
for (var arg : constructor.arguemtns()) {
|
||||
constructorBuilder.addParameter(ClassName.bestGuess(arg.type()), arg.name());
|
||||
}
|
||||
classBuilder.addMethod(constructorBuilder.build());
|
||||
}
|
||||
|
||||
// Generate Java class
|
||||
TypeSpec javaClass = classBuilder.build();
|
||||
var result = JavaFile.builder(packageName, javaClass).build();
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package io.github.jwdeveloper.tiktok.intefacee;
|
||||
|
||||
import com.squareup.javapoet.*;
|
||||
import io.github.jwdeveloper.tiktok.TikTokLiveClientBuilder;
|
||||
import io.github.jwdeveloper.tiktok.events.TikTokEvent;
|
||||
import io.github.jwdeveloper.tiktok.events_generator.EventGeneratorSettings;
|
||||
import org.reflections.Reflections;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
import java.util.Set;
|
||||
|
||||
public class EventsInterfaceGenerator {
|
||||
public void compile(EventGeneratorSettings settings) {
|
||||
Reflections reflections = new Reflections("io.github.jwdeveloper.tiktok.events.messages");
|
||||
|
||||
// Get all types (i.e., classes) in the specified package
|
||||
var classes = reflections.getSubTypesOf(TikTokEvent.class);
|
||||
classes.add(TikTokEvent.class);
|
||||
for (Class<?> clazz : classes) {
|
||||
// System.out.println(clazz.getName());
|
||||
}
|
||||
|
||||
// var result = generateInterface("io.github.jwdeveloper.tiktok.events", classes);System.out.println(result);
|
||||
|
||||
|
||||
var result = getBuilderImplementation("x",classes); System.out.println(result);
|
||||
|
||||
}
|
||||
|
||||
public String generateInterface(String packageName, Set<Class<? extends TikTokEvent>> eventsClasses) {
|
||||
|
||||
TypeSpec.Builder classBuilder = TypeSpec.interfaceBuilder("TikTokEvents");
|
||||
classBuilder.addModifiers(Modifier.PUBLIC);
|
||||
classBuilder.addTypeVariable(TypeVariableName.get("T"));
|
||||
|
||||
// Generate constructors
|
||||
for (var clazz : eventsClasses) {
|
||||
var clazzName = clazz.getSimpleName();
|
||||
var methodName = clazzName.replace("TikTok", "");
|
||||
methodName = methodName.replace("Event", "");
|
||||
MethodSpec.Builder constructorBuilder = MethodSpec.methodBuilder("on" + methodName);
|
||||
|
||||
|
||||
var name = "Consumer<" + clazzName + ">";
|
||||
constructorBuilder.addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC);
|
||||
constructorBuilder.addParameter(ClassName.bestGuess(name), "event");
|
||||
constructorBuilder.returns(TypeVariableName.get("T"));
|
||||
classBuilder.addMethod(constructorBuilder.build());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Generate Java class
|
||||
TypeSpec javaClass = classBuilder.build();
|
||||
var result = JavaFile.builder(packageName, javaClass).build();
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getBuilderImplementation(String packageName, Set<Class<? extends TikTokEvent>> eventsClasses) {
|
||||
|
||||
TypeSpec.Builder classBuilder = TypeSpec.classBuilder("TikTokEvents");
|
||||
classBuilder.addModifiers(Modifier.PUBLIC);
|
||||
|
||||
/*
|
||||
public TikTokClientBuilder onLinkMicFanTicket(Consumer<TikTokLinkMicFanTicketEvent> event) {
|
||||
tikTokEventHandler.subscribe(TikTokEventHandler.class, event);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
|
||||
// Generate constructors
|
||||
for (var clazz : eventsClasses) {
|
||||
var clazzName = clazz.getSimpleName();
|
||||
var methodName = clazzName.replace("TikTok", "");
|
||||
methodName ="on" + methodName.replace("Event", "");
|
||||
MethodSpec.Builder constructorBuilder = MethodSpec.methodBuilder( methodName);
|
||||
|
||||
|
||||
var name = "Consumer<" + clazzName + ">";
|
||||
constructorBuilder.addModifiers( Modifier.PUBLIC);
|
||||
constructorBuilder.addParameter(ClassName.bestGuess(name), "event");
|
||||
constructorBuilder.addStatement("tikTokEventHandler.subscribe("+clazzName+".class,event)");
|
||||
constructorBuilder.addStatement("return this");
|
||||
constructorBuilder.returns(TikTokLiveClientBuilder.class);
|
||||
classBuilder.addMethod(constructorBuilder.build());
|
||||
|
||||
}
|
||||
|
||||
// Generate Java class
|
||||
TypeSpec javaClass = classBuilder.build();
|
||||
var result = JavaFile.builder(packageName, javaClass).build();
|
||||
return result.toString();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Tools/target/classes/io/github/jwdeveloper/tiktok/Main.class
Normal file
BIN
Tools/target/classes/io/github/jwdeveloper/tiktok/Main.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user