Hard Light Productions Forums

Modding, Mission Design, and Coding => FS2 Open Tools => Topic started by: Fury on October 30, 2010, 01:44:19 am

Title: In the wishlist: TBL to spreadsheet converter
Post by: Fury on October 30, 2010, 01:44:19 am
So I was wondering how much work it would be to implement some sort of ships.tbl, weapons.tbl and tbm to spreadsheet converter? Spreadsheet layout would be something like this
Code: [Select]
$Name: +Title $Model File: $Damage: $Armor factor:
@Subach HL-7 XSTR("GTW Subach HL-7", 3243) none 15 0.9
@Akheton SDG XSTR("GTW Akheton SDG", 3258) none 30 0.0
It'd be so easy to add extra functions to the spreadsheet and have your calculations done for you for all weapons or ships in few seconds. As opposed to having to go through each one of them by hand which may take several days.

Please?
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: Snail on October 30, 2010, 06:32:30 am
TBLView32 did exactly that, but it doesn't parse SCP specific features and didn't do calculations. I think it just tabulated it. Anyhow if the guy who made TBLView (Heiko Hermann?) released the source code, it could be a starting point.
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: Spicious on October 30, 2010, 07:11:41 am
Exporting to csv should be easy enough. It's just a matter of parsing. Figuring out a linear ordering of fields could be interesting too.
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: Goober5000 on October 31, 2010, 12:07:04 am
Here's something I whipped up in about 30 minutes.  Feel free to throw tomatoes at it.

Code: [Select]
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class TBLToCSV
{
    private final File input;
    private final File output;
    private final Map<String, Map<String, String>> chart;
    private final Set<String> labels;
   
    public TBLToCSV(File input, File output)
    {
        this.input = input;
        this.output = output;
        this.chart = new HashMap<String, Map<String, String>>();
        this.labels = new LinkedHashSet<String>();
    }
   
    public void process() throws IOException
    {
        List<String> lines = readTextFileLines(input);
        String currentName = null;
        boolean foundShipClasses = false;
       
        // generate chart
        for (String line: lines)
        {
            line = line.trim();
            if (line.equals("#Ship Classes"))
                foundShipClasses = true;
            if (!foundShipClasses)
                continue;
           
            if (line.startsWith("$Name:"))
            {
                currentName = line.substring(6).trim();
                chart.put(currentName, new HashMap<String, String>());
            }
           
            String firstPart = null;
            String secondPart = null;
            if (line.startsWith("$") || line.startsWith("+"))
            {
                int colonPos = line.indexOf(":");
                if (colonPos >= 0)
                {
                    firstPart = line.substring(0, colonPos + 1).trim();
                    secondPart = line.substring(colonPos + 1).trim();
                   
                    // truncate comments
                    int commentpos = secondPart.indexOf(";");
                    if (commentpos >= 0)
                        secondPart = secondPart.substring(0, commentpos).trim();
                   
                    // avoid errors in CSV!
                    if (secondPart.contains(","))
                        secondPart = secondPart.replace(',', '\'');
                }
            }
           
            if (firstPart == null || secondPart == null)
                continue;
           
            labels.add(firstPart);
            chart.get(currentName).put(firstPart, secondPart);
        }
       
        // save chart to CSV
       
        BufferedWriter writer = new BufferedWriter(new FileWriter(output));
        String header = "";
        for (String label: labels)
            header += (label + ",");
        writer.write(header + "\n");
       
        for (Map.Entry<String, Map<String, String>> entry: chart.entrySet())
        {
            String line = "";
           
            for (String label: labels)
                line += (entry.getValue().get(label) + ",");
            writer.write(line + "\n");
        }
        writer.close();
    }
   
    public static void main(String[] args) throws IOException
    {
        TBLToCSV app = new TBLToCSV(new File("C:\\temp\\ships.tbl"), new File("C:\\temp\\output.csv"));
        app.process();
    }
   
    public static boolean nullSafeEquals(Object obj1, Object obj2)
    {
        if (obj1 == null && obj2 == null)
            return true;
        if (obj1 == null || obj2 == null)
            return false;
        return obj1.equals(obj2);
    }
   
    public static List<String> readTextFileLines(File file)
    {
        String str;
        List<String> lines = new ArrayList<String>();
        try
        {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            while ((str = reader.readLine()) != null)
                lines.add(str);
            reader.close();
        }
        catch (IOException ioe)
        {
            lines.clear();
        }
       
        return lines;
    }
}
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: chief1983 on October 31, 2010, 11:58:59 am
Good thing I just picked some tomatoes :)
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: Fury on October 31, 2010, 12:41:09 pm
So how does one use that java thingy?
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: sigtau on October 31, 2010, 02:16:17 pm
I don't have FSO on here, but if someone could post a sample .tbl from their VP files, then I'd be happy to further test and add onto that code (I'm a five-year experienced Java programmer, so I might be able to help provided I can decipher Goober's code  :nervous:)
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: chief1983 on October 31, 2010, 04:17:01 pm
There's a few tables in the wiki already.  And I posted the weapons.tbl (http://fsscp.pastebin.com/0Hga8jU9) the other day on pastebin.
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: sigtau on October 31, 2010, 11:33:40 pm
(http://img80.imageshack.us/img80/5677/tblview2.png)

hay guys look wat im making
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: Fury on October 31, 2010, 11:59:51 pm
That's nice and all, but you can't add your own functions in there to make the calculations you want to. Which is why I asked for a spreadsheet converter in similar layout to the example in code tags. :(
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: Nuke on November 01, 2010, 06:32:52 am
spreadsheets let you do cool things, like autobalence your weapons. so if you could import, crunch the values then export, that would be awesome. ive used spreadsheets for data analysis before, to figure out where things needed improvement. normally i copy over the values manually, but a method to rapidly convert to and from tables would be awesome.
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: sigtau on November 01, 2010, 08:37:12 am
spreadsheets let you do cool things, like autobalence your weapons. so if you could import, crunch the values then export, that would be awesome. ive used spreadsheets for data analysis before, to figure out where things needed improvement. normally i copy over the values manually, but a method to rapidly convert to and from tables would be awesome.

See the little icon that looks like this?  (http://img229.imageshack.us/img229/1723/tableexport.png)

That's the spreadsheet exporter.  There will be a spreadsheet importer as well (but it will only work as long as 1. it's a CSV file, and 2. it stays in the same general form as the original export).

Also, simply opening and saving the table reformats it to a more readable state, so those mass line-indentations in the retail weapons.tbl can be corrected by simply opening it in this program.
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: Fury on November 01, 2010, 08:58:14 am
Sounds awesome. You're going to make many a tabler much happier, me included. :D
Title: Re: In the wishlist: TBL to spreadsheet converter
Post by: Nuke on November 02, 2010, 07:31:01 pm
should work. might let me improve my armor table somewhat.