LuaSerializer.java 2.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package emu.grasscutter.scripts.serializer;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

public class LuaSerializer implements Serializer {
	
	@Override
	public <T> List<T> toList(Class<T> type, Object obj) {
		return serializeList(type, (LuaTable) obj);
	}

	@Override
	public <T> T toObject(Class<T> type, Object obj) {
		return serialize(type, (LuaTable) obj);
	}
	
	public <T> List<T> serializeList(Class<T> type, LuaTable table) {
		List<T> list = new ArrayList();
		
		try {
			LuaValue[] keys = table.keys();
			for (LuaValue k : keys) {
				try {
					LuaValue keyValue = table.get(k);
					
					T object = null;
					
					if (keyValue.istable()) {
						object = serialize(type, keyValue.checktable());
				    } else if (keyValue.isint()) {
				    	object = (T) (Integer) keyValue.toint();
				    } else if (keyValue.isnumber()) {
				    	object = (T) (Float) keyValue.tofloat(); // terrible...
				    } else if (keyValue.isstring()) {
				    	object = (T) keyValue.tojstring();
				    } else {
				    	object = (T) keyValue;
				    }
					
					if (object != null) {
						list.add(object);
					}
				} catch (Exception ex) {

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

		return list;
	}
	
	public <T> T serialize(Class<T> type, LuaTable table) {
		T object = null;
		
		if (type == List.class) {
			try {
				Class<T> listType = (Class<T>) type.getTypeParameters()[0].getClass();
				return (T) serializeList(listType, table);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		
		try {
			object = type.getDeclaredConstructor().newInstance(null);
			
			LuaValue[] keys = table.keys();
			for (LuaValue k : keys) {
				try {
					Field field = object.getClass().getDeclaredField(k.checkjstring());
					if (field == null) {
						continue;
					}
					
					field.setAccessible(true);
					LuaValue keyValue = table.get(k);

					if (keyValue.istable()) {
				    	field.set(object, serialize(field.getType(), keyValue.checktable()));
				    } else if (field.getType().equals(float.class)) {
				    	field.setFloat(object, keyValue.tofloat());
				    } else if (field.getType().equals(int.class)) {
				    	field.setInt(object, keyValue.toint());
				    } else if (field.getType().equals(String.class)) {
				    	field.set(object, keyValue.tojstring());
				    } else {
				    	field.set(object, keyValue);
				    }
				} catch (Exception ex) {
					//ex.printStackTrace();
					continue;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return object;
	}
}