Skip to content

Commit

Permalink
Pushing
Browse files Browse the repository at this point in the history
  • Loading branch information
GlennFolker committed Feb 11, 2024
1 parent 0d1dd2d commit 269abcf
Show file tree
Hide file tree
Showing 15 changed files with 695 additions and 51 deletions.
17 changes: 8 additions & 9 deletions annotations/src/gltf/annotations/SpecProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void init(ProcessingEnvironment processingEnv){
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv){
for(var req : annotations){
if(!types.isSameType(req.asType(), elements.getTypeElement(Spec.class.getName()).asType())) continue;
if(!types.isSameType(req.asType(), elements.getTypeElement(Spec.class.getCanonicalName()).asType())) continue;
for(var spec : roundEnv.getElementsAnnotatedWith(req)){
var anno = spec.getAnnotation(Spec.class);
var outType = ClassName.get(packageName, spec.getSimpleName().toString());
Expand Down Expand Up @@ -92,7 +92,6 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
.addMethod(MethodSpec
.constructorBuilder()
.addModifiers(PRIVATE)
.addStatement("throw new $T()", ClassName.get(AssertionError.class))
.build())
.addField(FieldSpec
.builder(ClassName.get(Jval.class), "extensions", PUBLIC)
Expand Down Expand Up @@ -242,38 +241,38 @@ public Void visitPrimitive(PrimitiveType t, Void unused){

@Override
public Void visitDeclared(DeclaredType t, Void unused){
if(types.isSameType(t, elements.getTypeElement(String.class.getName()).asType())){
if(types.isSameType(t, elements.getTypeElement(String.class.getCanonicalName()).asType())){
create.addStatement("$L = $L.asString()", name, json);
}else if(types.isSameType(t, elements.getTypeElement(Color.class.getName()).asType())){
}else if(types.isSameType(t, elements.getTypeElement(Color.class.getCanonicalName()).asType())){
create
.addStatement("$T $L__array = $L.asArray()", ClassName.get(JsonArray.class), name, json)
.addStatement(
"$L = new $T($L__array.get(0).asFloat(), $L__array.get(1).asFloat(), $L__array.get(2).asFloat(), $L__array.get(3).asFloat())",
name, ClassName.get(Color.class), name, name, name, name
);
}else if(types.isSameType(t, elements.getTypeElement(Vec3.class.getName()).asType())){
}else if(types.isSameType(t, elements.getTypeElement(Vec3.class.getCanonicalName()).asType())){
create
.addStatement("$T $L__array = $L.asArray()", ClassName.get(JsonArray.class), name, json)
.addStatement(
"$L = new $T($L__array.get(0).asFloat(), $L__array.get(1).asFloat(), $L__array.get(2).asFloat())",
name, ClassName.get(Vec3.class), name, name, name
);
}else if(types.isSameType(t, elements.getTypeElement(Mat3D.class.getName()).asType())){
}else if(types.isSameType(t, elements.getTypeElement(Mat3D.class.getCanonicalName()).asType())){
create
.addStatement("$T $L__array = $L.asArray()", ClassName.get(JsonArray.class), name, json)
.addStatement("$L = new $T()", name, ClassName.get(Mat3D.class))
.beginControlFlow("for(int $L__i = 0; $L__i < 16; $L__i++)", name, name, name)
.addStatement("$L.val[$L__i] = $L__array.get($L__i).asFloat()", name, name, name, name)
.endControlFlow();
}else if(types.isSameType(t, elements.getTypeElement(Quat.class.getName()).asType())){
}else if(types.isSameType(t, elements.getTypeElement(Quat.class.getCanonicalName()).asType())){
create
.addStatement("$T $L__array = $L.asArray()", ClassName.get(JsonArray.class), name, json)
.addStatement(
"$L = new $T($L__array.get(0).asFloat(), $L__array.get(1).asFloat(), $L__array.get(2).asFloat(), $L__array.get(3).asFloat())",
name, ClassName.get(Quat.class), name, name, name, name
);
}else if(types.isSameType(t, elements.getTypeElement(Jval.class.getName()).asType())){
create.addStatement("$L = $L", name, json);
}else if(types.isSameType(t, elements.getTypeElement(JsonMap.class.getCanonicalName()).asType())){
create.addStatement("$L = $L.asObject()", name, json);
}else{
var type = change(t);
if(type.toString().startsWith("gltfrenzy.data")){
Expand Down
70 changes: 35 additions & 35 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,6 @@ configure(allprojects){
}
}

configure(rootProject){
sourceSets.main.java.srcDirs = [layout.projectDirectory.dir('src')]
version = projectVersion

java{
withSourcesJar()
withJavadocJar()
}

dependencies{
annotationProcessor project(':annotations')
compileOnly project(':annotations')

compileOnlyApi "${arc(':arc-core')}"
compileOnlyApi "${arc(':g3d')}"
}

tasks.withType(JavaCompile).configureEach{
sourceCompatibility = compilerVersion
options.release = 8
options.compilerArgs << '-Xlint:-options'

options.incremental = true
options.encoding = 'UTF-8'
}

tasks.withType(Jar).configureEach{
exclude 'gltfrenzy/spec/**'
}

publishing.publications.create('maven', MavenPublication){
from components.java
}
}

configure(project(':annotations')){
sourceSets.main{
java.srcDirs = [layout.projectDirectory.dir('src')]
Expand Down Expand Up @@ -91,3 +56,38 @@ configure(project(':annotations')){
}
}
}

sourceSets.main.java.srcDirs = [layout.projectDirectory.dir('src')]

group = 'com.github.GlennFolker'
version = projectVersion

java{
withSourcesJar()
withJavadocJar()
}

dependencies{
annotationProcessor project(':annotations')
compileOnly project(':annotations')

compileOnlyApi "${arc(':arc-core')}"
compileOnlyApi "${arc(':g3d')}"
}

tasks.withType(JavaCompile).configureEach{
sourceCompatibility = compilerVersion
options.release = 8
options.compilerArgs << '-Xlint:-options'

options.incremental = true
options.encoding = 'UTF-8'
}

tasks.withType(Jar).configureEach{
exclude 'gltfrenzy/spec/**'
}

publishing.publications.create('maven', MavenPublication){
from components.java
}
6 changes: 6 additions & 0 deletions jitpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
before_install:
- sdk install java 21.0.1-tem
- sdk use java 21.0.1-tem
install:
- ./gradlew publishToMavenLocal
- echo -n > gradle.properties
138 changes: 138 additions & 0 deletions src/gltfrenzy/io/LEDataInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package gltfrenzy.io;

import java.io.*;

/**
* A little-endian {@link DataInputStream}.
* @author GlennFolker
*/
public class LEDataInputStream extends InputStream implements DataInput{
private final byte[] buffer = new byte[8];
private final DataInputStream read;

public LEDataInputStream(InputStream in){
read = new DataInputStream(in);
}

@Override
public int available() throws IOException{
return read.available();
}

@Override
public final short readShort() throws IOException{
read.readFully(buffer, 0, 2);
return (short)(
(buffer[1] & 0xff) << 8 |
(buffer[0] & 0xff)
);
}

@Override
public final int readUnsignedShort() throws IOException{
read.readFully(buffer, 0, 2);
return (
(buffer[1] & 0xff) << 8 |
(buffer[0] & 0xff)
);
}

@Override
public final char readChar() throws IOException{
read.readFully(buffer, 0, 2);
return (char)(
(buffer[1] & 0xff) << 8 |
(buffer[0] & 0xff)
);
}

@Override
public final int readInt() throws IOException{
read.readFully(buffer, 0, 4);
return
(buffer[3]) << 24 |
(buffer[2] & 0xff) << 16 |
(buffer[1] & 0xff) << 8 |
(buffer[0] & 0xff);
}

@Override
public final long readLong() throws IOException{
read.readFully(buffer, 0, 8);
return
(long)(buffer[7]) << 56 |
(long)(buffer[6] & 0xff) << 48 |
(long)(buffer[5] & 0xff) << 40 |
(long)(buffer[4] & 0xff) << 32 |
(long)(buffer[3] & 0xff) << 24 |
(long)(buffer[2] & 0xff) << 16 |
(long)(buffer[1] & 0xff) << 8 |
(long)(buffer[0] & 0xff);
}

@Override
public final float readFloat() throws IOException{
return Float.intBitsToFloat(readInt());
}

@Override
public final double readDouble() throws IOException{
return Double.longBitsToDouble(readLong());
}

@Override
public final int read(byte[] b, int off, int len) throws IOException{
return read.read(b, off, len);
}

@Override
public final void readFully(byte[] b) throws IOException{
read.readFully(b, 0, b.length);
}

@Override
public final void readFully(byte[] b, int off, int len) throws IOException{
read.readFully(b, off, len);
}

@Override
public final int skipBytes(int n) throws IOException{
return read.skipBytes(n);
}

@Override
public final boolean readBoolean() throws IOException{
return read.readBoolean();
}

@Override
public final byte readByte() throws IOException{
return read.readByte();
}

@Override
public int read() throws IOException{
return read.read();
}

@Override
public final int readUnsignedByte() throws IOException{
return read.readUnsignedByte();
}

@Override
@Deprecated
public final String readLine() throws IOException{
return read.readLine();
}

@Override
public final String readUTF() throws IOException{
return read.readUTF();
}

@Override
public final void close() throws IOException{
read.close();
}
}
61 changes: 61 additions & 0 deletions src/gltfrenzy/loader/GlbReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package gltfrenzy.loader;

import arc.files.*;
import arc.util.io.*;
import arc.util.serialization.*;
import gltfrenzy.data.*;
import gltfrenzy.io.*;

import java.io.*;
import java.nio.*;
import java.nio.charset.*;

/**
* Reader implementation for {@code .glb} monolithic binary file.
* @author GlennFolker
*/
public class GlbReader implements Scenes3DReader{
@Override
public Scenes3DData read(Fi file) throws IOException{
try(var stream = new CounterInputStream(file.read());
var read = new LEDataInputStream(stream);
){
int fileLen = readHeader(read);

var data = new Scenes3DData();
for(int i = 0; stream.count < fileLen; i++){
byte[] chunk = new byte[read.readInt()];
int chunkType = read.readInt();

read.readFully(chunk);
switch(chunkType){
case 0x4e4f534a -> {
if(i != 0) throw new IOException("JSON chunk found at index " + i + ".");
data.spec = Gltf.create(Jval.read(new String(chunk, StandardCharsets.UTF_8)));
data.buffers = new ByteBuffer[data.spec.buffers.length];
}
case 0x004e4942 -> {
if(i != 1) throw new IOException("BIN chunk found at index " + i);
var buffer = data.buffers[0] = ByteBuffer.wrap(chunk);
buffer.order(ByteOrder.LITTLE_ENDIAN);
}
default -> {
if(i == 0) throw new IOException("Custom chunk found at index 0.");
}
}
}

return data;
}
}

protected int readHeader(DataInput read) throws IOException{
int magic = read.readInt();
if(magic != 0x46546c67) throw new IOException("Invalid header 0x" + Integer.toHexString(magic) + ".");

int version = read.readInt();
if(version != 2) throw new IOException("Unsupported container version " + version + ".");

return read.readInt();
}
}
36 changes: 36 additions & 0 deletions src/gltfrenzy/loader/GltfReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gltfrenzy.loader;

import arc.files.*;
import arc.util.serialization.*;
import gltfrenzy.data.*;

import java.io.*;
import java.nio.*;

/**
* Reader implementation for {@code .gltf} textual file.
* @author GlennFolker
*/
public class GltfReader implements Scenes3DReader{
@Override
public Scenes3DData read(Fi file) throws IOException{
try(var reader = file.reader("UTF-8")){
int len = (int)file.length();
len = len == 0 ? 4096 : len;

var builder = new StringBuilder(len);
char[] buffer = new char[len];
while(true){
int read = reader.read(buffer);
if(read == -1) break;

builder.append(buffer, 0, read);
}

var data = new Scenes3DData();
data.spec = Gltf.create(Jval.read(builder.toString()));
data.buffers = new ByteBuffer[data.spec.buffers.length];
return data;
}
}
}
Loading

0 comments on commit 269abcf

Please sign in to comment.