Skip to content

Commit

Permalink
Tankson.compatibility (#65)
Browse files Browse the repository at this point in the history
Added tankson.Compatibility to address cross version compatability
  • Loading branch information
ghostlypi authored Jan 23, 2025
1 parent 5fd857b commit 5570f9e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/main/java/tanks/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import tanks.rendering.ShaderGroundOutOfBounds;
import tanks.rendering.ShaderTracks;
import tanks.tank.*;
import tanks.tankson.Compatibility;

import java.io.*;
import java.util.*;
Expand Down Expand Up @@ -518,6 +519,7 @@ public static void initScript()
Drawing.initialize();
Panel.initialize();
Game.exitToTitle();
Compatibility.init();

Hotbar.toggle = new Button(Drawing.drawing.interfaceSizeX / 2, Drawing.drawing.interfaceSizeY - 20, 150, 40, "", () -> Game.player.hotbar.persistent = !Game.player.hotbar.persistent);

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/tanks/tankson/Compatibility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package tanks.tankson;

import tanks.tank.Explosion;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.function.Function;

public class Compatibility {
public static final HashMap<String, Function<Object, Object>> compatibility_table = new HashMap<>();
public static final HashMap<String, String> field_table = new HashMap<>();

public static void init() {
compatibility_table.put("explode_on_destroy", Compatibility::updateExplosion);
}

public static Object convert(Field f, Object v) {
return compatibility_table.get(Serializer.getid(f)).apply(v);
}

public static String convert(String f) {
return field_table.get(f);
}

// Explosions 1.6.b -> 1.6.c
public static Object updateExplosion(Object o) {
if (o instanceof Boolean) {
if ((Boolean) o) {
return new Explosion();
} else {
return null;
}
} else {
throw new RuntimeException("Update Explosions from 1.6.b --> 1.6.c");
}
}
}
33 changes: 29 additions & 4 deletions src/main/java/tanks/tankson/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import tanks.Game;
import tanks.bullet.Bullet;
import tanks.item.Item;
import tanks.item.ItemBullet;
import tanks.item.ItemEmpty;
import tanks.tank.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.*;

Expand Down Expand Up @@ -242,17 +239,25 @@ public static Object parseObject(Map<String, Object> m)
default:
throw new RuntimeException("Bad object type: " + (String) m.get("obj_type"));
}

Set<String> processed = new HashSet<>();

for (Field f : o.getClass().getFields())
{
if (f.isAnnotationPresent(Property.class) && m.containsKey(getid(f)))
{
processed.add(getid(f));
try
{
Object o2 = f.get(o);
if (isTanksONable(f))
{
Object o3 = m.get(getid(f));
f.set(o, parseObject((Map) o3));
try {
f.set(o, parseObject((Map<String, Object>) o3));
} catch (ClassCastException e) {
f.set(o, Compatibility.convert(f, o3));
}
}
else if (o2 instanceof ArrayList)
{
Expand Down Expand Up @@ -284,10 +289,30 @@ else if (o2 instanceof Boolean)
}
catch (Exception e)
{
System.out.println(getid(f));
throw new RuntimeException(e);
}
}
}

Set<String> unused = m.keySet();
unused.removeAll(processed);
for (String k : unused) {
try {
o.getClass().getField(Compatibility.convert(k)).set(o, m.get(k));
} catch (ClassCastException e) {
try {
Field f = o.getClass().getField(Compatibility.convert(k));
f.set(o, Compatibility.convert(f, m.get(k)));
} catch (NoSuchFieldException | IllegalAccessException f) {
throw new RuntimeException(f);
}
} catch (NoSuchFieldException | NullPointerException | IllegalAccessException e) {
System.out.println("Unconvertable field found!");
}
}


return o;
}

Expand Down

0 comments on commit 5570f9e

Please sign in to comment.