summaryrefslogtreecommitdiff
path: root/tools/tmwcon/src/converter/Main.java
blob: cb226776f6baea5d9a90de82fc61cdf5af91f704 (plain) (blame)
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
/*
 * Converter from Tiled .tmx files to tmwAthena .wlk and mob/warp scripts
 * Copyright (c) 2008 Jared Adams
 * Copyright (c) 2011 Ben Longbons
 * License: GNU GPL, v2 or later
 */

package converter;

import java.io.*;
import java.util.*;

import tiled.io.xml.*;

public class Main {
    public static XMLMapTransformer reader = null;

    private static tiled.core.Map loadMap(File file) {
        tiled.core.Map map = null;
        try {
            map = reader.readMap(file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    public static boolean isTMX(File in) {
        if (in.isDirectory()) return false;

        return in.getName().matches(".*\\.tmx(\\.gz)?$");
    }

    public static Collection<File> getTMXFiles(File directory) {
        if (!directory.isDirectory()) return Collections.emptyList();

        List<File> ret = new Vector<File>();

        for (File f : directory.listFiles()) {
            if (f.isDirectory()) {
                ret.addAll(getTMXFiles(f));
            } else if (isTMX(f)) {
                ret.add(f);
            }
        }

        return ret;
    }

    public static PrintWriter getWriter(File f) {
        try {
            f.getParentFile().mkdir();
            f.createNewFile();
            return new PrintWriter(f);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String getName(File folder, File file) {
        String path = folder.getAbsolutePath();
        String name = file.getAbsolutePath();
        if (name.startsWith(path)) name = name.substring(path.length() + 1);
        if (name.endsWith(".gz")) name = name.substring(0, name.length() - 3);
        if (name.endsWith(".tmx")) name = name.substring(0, name.length() - 4);
        return name;
    }

    public static void main(String[] args) throws IOException {
        if (args.length != 2) {
            System.out.println("Usage: java Converter client-data-dir server-data-dir");
            System.exit(1);
        }

        File client_data = new File(args[0]);
        File server_data = new File(args[1]);

        reader = new XMLMapTransformer();

        PrintWriter summary = new PrintWriter("converter.txt");

        Process.setServerData(server_data);

        File folder = new File(client_data, "maps/");

        Collection<File> tmxs = getTMXFiles(folder);
        ArrayList<String> folders = new ArrayList<String>();
        String name;
        for (File f : tmxs) {
            name = getName(folder, f);
            System.out.printf("== %s ==\n", name);
            folders.add(Process.processMap(name, loadMap(f), f, summary));
        }

        summary.flush();
        summary.close();

        Process.writeMasterImport(folders);
    }
}