package info.evshiron.ingressdeserializer;

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class IngressDeserializer {

    // Reference to https://gist.github.com/sugyan/8396d473ffae62b664e2, thanks to @sugyan.

    public static String Deserializer(File file) {

        try {

            int count = 0;

            ArrayList<String[]> elements = new ArrayList<String[]>();

            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);

            while (ois.available() == 0) {

                Object obj = ois.readObject();
                Class cls = obj.getClass();
                String[] arr = new String[Array.getLength(obj)];

                for (int i = 0; i < Array.getLength(obj); i++) {

                    if (cls.getComponentType() == float.class) {
                        arr[i] = String.valueOf(Array.getFloat(obj, i));
                    }
                    if (cls.getComponentType() == short.class) {
                        arr[i] = String.valueOf(Array.getShort(obj, i));
                    }

                }

                // 最初の頂点座標は (vx, vy, vz, tx, ty) になっているようなので分離。
                if (count == 0) {

                    ArrayList<String> vertices = new ArrayList<String>();
                    ArrayList<String> texcoords = new ArrayList<String>();

                    for (int i = 0; i < arr.length; i++) {
                        if (i % 5 < 3) {
                            vertices.add(arr[i]);
                        } else {
                            texcoords.add(arr[i]);
                        }
                    }

                    elements.add(vertices.toArray(new String[0]));
                    elements.add(texcoords.toArray(new String[0]));

                } else {

                    elements.add(arr);

                }

                count++;

            }

            fis.close();
            ois.close();

            StringBuilder sb = new StringBuilder();

            sb.append(String.format("# (x, y, z) count: %d.\n", elements.get(0).length / 3));

            for(int i = 0; i < elements.get(0).length; i += 3) {

                sb.append(String.format("v %s %s %s\n", elements.get(0)[i+0], elements.get(0)[i+1], elements.get(0)[i+2]));

            }

            sb.append(String.format("# (u, v) count: %d.\n", elements.get(1).length / 2));

            for(int i = 0; i < elements.get(1).length; i += 2) {

                sb.append(String.format("vt %s %s\n", elements.get(1)[i+0], elements.get(1)[i+1]));

            }

            sb.append(String.format("# Faces count: %d.\n", elements.get(2).length / 3));

            for(int i = 0; i < elements.get(2).length; i += 3) {

                int v1 = Integer.parseInt(elements.get(2)[i+0]) + 1;
                int v2 = Integer.parseInt(elements.get(2)[i+1]) + 1;
                int v3 = Integer.parseInt(elements.get(2)[i+2]) + 1;

                sb.append(String.format("f %d/%d %d/%d %d/%d\n", v1, v1, v2, v2, v3, v3));

            }

            return sb.toString();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;

    }

    public static void main(String[] args) {

            try {

                for(String path : args) {

                    File file = new File(path);

                    if(!file.exists() || !file.getName().endsWith(".obj")) continue;

                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(String.format("out_%s", file.getName())), "utf-8"));

                    writer.write(Deserializer(file));
                    writer.flush();
                    writer.close();

                }

            } catch (IOException e) {
                e.printStackTrace();
            }

    }

}

@@ -976,32 +976,37 @@
                 
+else 
 if (cls.getCompo
@@ -1026,32 +1026,32 @@
  short.class) %7B%0A
-
                 
@@ -1120,32 +1120,146 @@
                %7D
+%0A                    else %7B%0A%0A                        System.out.println(cls.getTypeName());%0A%0A                    %7D
 %0A%0A              
@@ -2100,32 +2100,273 @@
   ois.close();%0A%0A
+            if(elements.get(0).length / 3 != elements.get(1).length / 2) %7B%0A%0A                System.out.println(file.getName());%0A                System.out.println(%22VERTICES_TEXCOORDS_NOT_MATCH%22);%0A                return null;%0A%0A            %7D%0A%0A
             Stri
@@ -2611,20 +2611,39 @@
 %22v %25
-s %25s %25s
+f %25f %25f
 %5Cn%22, 
+Double.parseDouble(
 elem
@@ -2654,26 +2654,46 @@
 .get(0)%5Bi+0%5D
+)
 , 
+Double.parseDouble(
 elements.get
@@ -2700,18 +2700,38 @@
 (0)%5Bi+1%5D
+)
 , 
+Double.parseDouble(
 elements
@@ -2744,16 +2744,17 @@
 )%5Bi+2%5D))
+)
 ;%0A%0A     
@@ -2970,17 +2970,36 @@
 vt %25
-s %25s
+f %25f
 %5Cn%22, 
+Double.parseDouble(
 elem
@@ -3014,18 +3014,38 @@
 (1)%5Bi+0%5D
+)
 , 
+Double.parseDouble(
 elements
@@ -3058,16 +3058,17 @@
 )%5Bi+1%5D))
+)
 ;%0A%0A     
@@ -4089,16 +4089,129 @@
 tinue;%0A%0A
+                    String wavefront = Deserializer(file);%0A%0A                    if(wavefront == null) continue;%0A%0A
         
@@ -4396,34 +4396,25 @@
 r.write(
-Deserializer(file)
+wavefront
 );%0A     

Fix mistakes & fix crashes