Commit 10737410 authored by Human Gamer's avatar Human Gamer

Now Remembers Last Directory!

Open is separate from Save
parent e4955686
Pipeline #1740832 (#) skipped
...@@ -21,6 +21,7 @@ import javax.swing.filechooser.FileNameExtensionFilter; ...@@ -21,6 +21,7 @@ import javax.swing.filechooser.FileNameExtensionFilter;
import com.matt.difinspector.dts.DTSShape; import com.matt.difinspector.dts.DTSShape;
import com.matt.difinspector.interior.InteriorResource; import com.matt.difinspector.interior.InteriorResource;
import com.matt.difinspector.render.GLRender; import com.matt.difinspector.render.GLRender;
import com.matt.difinspector.util.Settings;
public class DifInspector public class DifInspector
{ {
...@@ -42,6 +43,7 @@ public class DifInspector ...@@ -42,6 +43,7 @@ public class DifInspector
e.printStackTrace(); e.printStackTrace();
} }
DifInspector.instance = new DifInspector(); DifInspector.instance = new DifInspector();
Settings.loadConfig();
} }
private JFrame frame; private JFrame frame;
...@@ -330,7 +332,11 @@ public class DifInspector ...@@ -330,7 +332,11 @@ public class DifInspector
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileNameExtensionFilter("Interior Files (.dif)", "dif")); chooser.setFileFilter(new FileNameExtensionFilter("Interior Files (.dif)", "dif"));
chooser.showOpenDialog(this.frame); if (!Settings.getLastOpenDir().isEmpty())
chooser.setCurrentDirectory(new File(Settings.getLastOpenDir()));
int option = chooser.showOpenDialog(this.frame);
if (option != JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile(); File file = chooser.getSelectedFile();
if (file != null && file.exists()) if (file != null && file.exists())
...@@ -363,6 +369,10 @@ public class DifInspector ...@@ -363,6 +369,10 @@ public class DifInspector
JOptionPane.showMessageDialog(this.frame, "Failed to load interior!"); JOptionPane.showMessageDialog(this.frame, "Failed to load interior!");
} }
} }
String dir = chooser.getCurrentDirectory().toString();
Settings.setLastOpenDir(dir);
Settings.saveConfig();
} }
public void openCollada() public void openCollada()
...@@ -371,7 +381,11 @@ public class DifInspector ...@@ -371,7 +381,11 @@ public class DifInspector
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileNameExtensionFilter("Collada Files (.dae)", "dae")); chooser.setFileFilter(new FileNameExtensionFilter("Collada Files (.dae)", "dae"));
chooser.showOpenDialog(this.frame); if (!Settings.getLastOpenDir().isEmpty())
chooser.setCurrentDirectory(new File(Settings.getLastOpenDir()));
int option = chooser.showOpenDialog(this.frame);
if (option != JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile(); File file = chooser.getSelectedFile();
if (file != null && file.exists()) if (file != null && file.exists())
...@@ -400,6 +414,10 @@ public class DifInspector ...@@ -400,6 +414,10 @@ public class DifInspector
JOptionPane.showMessageDialog(this.frame, "Failed to load collada!"); JOptionPane.showMessageDialog(this.frame, "Failed to load collada!");
} }
} }
String dir = chooser.getCurrentDirectory().toString();
Settings.setLastOpenDir(dir);
Settings.saveConfig();
} }
public void openDTS() public void openDTS()
...@@ -408,7 +426,11 @@ public class DifInspector ...@@ -408,7 +426,11 @@ public class DifInspector
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileNameExtensionFilter("DTS Files (.dts)", "dts")); chooser.setFileFilter(new FileNameExtensionFilter("DTS Files (.dts)", "dts"));
chooser.showOpenDialog(this.frame); if (!Settings.getLastOpenDir().isEmpty())
chooser.setCurrentDirectory(new File(Settings.getLastOpenDir()));
int option = chooser.showOpenDialog(this.frame);
if (option != JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile(); File file = chooser.getSelectedFile();
if (file != null && file.exists()) if (file != null && file.exists())
...@@ -438,6 +460,10 @@ public class DifInspector ...@@ -438,6 +460,10 @@ public class DifInspector
JOptionPane.showMessageDialog(this.frame, "Failed to load DTS!"); JOptionPane.showMessageDialog(this.frame, "Failed to load DTS!");
} }
} }
String dir = chooser.getCurrentDirectory().toString();
Settings.setLastOpenDir(dir);
Settings.saveConfig();
} }
public void save() public void save()
...@@ -452,7 +478,11 @@ public class DifInspector ...@@ -452,7 +478,11 @@ public class DifInspector
String versionText = ((version == -1) ? "MBG" : ("v" + version)); String versionText = ((version == -1) ? "MBG" : ("v" + version));
chooser.setFileFilter(new FileNameExtensionFilter("Interior " + versionText + " Files (.dif)", "dif")); chooser.setFileFilter(new FileNameExtensionFilter("Interior " + versionText + " Files (.dif)", "dif"));
chooser.showSaveDialog(this.frame); if (!Settings.getLastSaveDir().isEmpty())
chooser.setCurrentDirectory(new File(Settings.getLastSaveDir()));
int option = chooser.showSaveDialog(this.frame);
if (option != JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile(); File file = chooser.getSelectedFile();
if (file != null) if (file != null)
...@@ -465,6 +495,10 @@ public class DifInspector ...@@ -465,6 +495,10 @@ public class DifInspector
JOptionPane.showMessageDialog(this.frame, "Export Failed!"); JOptionPane.showMessageDialog(this.frame, "Export Failed!");
} }
} }
String dir = chooser.getCurrentDirectory().toString();
Settings.setLastSaveDir(dir);
Settings.saveConfig();
} }
public void exportMap() public void exportMap()
...@@ -473,7 +507,11 @@ public class DifInspector ...@@ -473,7 +507,11 @@ public class DifInspector
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileNameExtensionFilter("Map Files (.map)", "map")); chooser.setFileFilter(new FileNameExtensionFilter("Map Files (.map)", "map"));
chooser.showSaveDialog(this.frame); if (!Settings.getLastSaveDir().isEmpty())
chooser.setCurrentDirectory(new File(Settings.getLastSaveDir()));
int option = chooser.showSaveDialog(this.frame);
if (option != JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile(); File file = chooser.getSelectedFile();
if (file != null) if (file != null)
...@@ -486,6 +524,10 @@ public class DifInspector ...@@ -486,6 +524,10 @@ public class DifInspector
JOptionPane.showMessageDialog(this.frame, "Map Export Failed!"); JOptionPane.showMessageDialog(this.frame, "Map Export Failed!");
} }
} }
String dir = chooser.getCurrentDirectory().toString();
Settings.setLastSaveDir(dir);
Settings.saveConfig();
} }
public void exportCsx() public void exportCsx()
...@@ -494,7 +536,11 @@ public class DifInspector ...@@ -494,7 +536,11 @@ public class DifInspector
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileNameExtensionFilter("Csx Files (.csx)", "csx")); chooser.setFileFilter(new FileNameExtensionFilter("Csx Files (.csx)", "csx"));
chooser.showSaveDialog(this.frame); if (!Settings.getLastSaveDir().isEmpty())
chooser.setCurrentDirectory(new File(Settings.getLastSaveDir()));
int option = chooser.showSaveDialog(this.frame);
if (option != JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile(); File file = chooser.getSelectedFile();
if (file != null) if (file != null)
...@@ -507,6 +553,10 @@ public class DifInspector ...@@ -507,6 +553,10 @@ public class DifInspector
JOptionPane.showMessageDialog(this.frame, "Csx Export Failed!"); JOptionPane.showMessageDialog(this.frame, "Csx Export Failed!");
} }
} }
String dir = chooser.getCurrentDirectory().toString();
Settings.setLastSaveDir(dir);
Settings.saveConfig();
} }
public void exportRaw() public void exportRaw()
...@@ -515,7 +565,11 @@ public class DifInspector ...@@ -515,7 +565,11 @@ public class DifInspector
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileNameExtensionFilter("Raw Files (.raw)", "raw")); chooser.setFileFilter(new FileNameExtensionFilter("Raw Files (.raw)", "raw"));
chooser.showSaveDialog(this.frame); if (!Settings.getLastSaveDir().isEmpty())
chooser.setCurrentDirectory(new File(Settings.getLastSaveDir()));
int option = chooser.showSaveDialog(this.frame);
if (option != JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile(); File file = chooser.getSelectedFile();
if (file != null) if (file != null)
...@@ -528,6 +582,10 @@ public class DifInspector ...@@ -528,6 +582,10 @@ public class DifInspector
JOptionPane.showMessageDialog(this.frame, "Raw Export Failed!"); JOptionPane.showMessageDialog(this.frame, "Raw Export Failed!");
} }
} }
String dir = chooser.getCurrentDirectory().toString();
Settings.setLastSaveDir(dir);
Settings.saveConfig();
} }
public void exportDump() public void exportDump()
...@@ -536,7 +594,11 @@ public class DifInspector ...@@ -536,7 +594,11 @@ public class DifInspector
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setFileFilter(new FileNameExtensionFilter("Plain Text Files (.txt)", "txt")); chooser.setFileFilter(new FileNameExtensionFilter("Plain Text Files (.txt)", "txt"));
chooser.showSaveDialog(this.frame); if (!Settings.getLastSaveDir().isEmpty())
chooser.setCurrentDirectory(new File(Settings.getLastSaveDir()));
int option = chooser.showSaveDialog(this.frame);
if (option != JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile(); File file = chooser.getSelectedFile();
if (file != null) if (file != null)
...@@ -549,6 +611,10 @@ public class DifInspector ...@@ -549,6 +611,10 @@ public class DifInspector
JOptionPane.showMessageDialog(this.frame, "Dump Failed!"); JOptionPane.showMessageDialog(this.frame, "Dump Failed!");
} }
} }
String dir = chooser.getCurrentDirectory().toString();
Settings.setLastSaveDir(dir);
Settings.saveConfig();
} }
public void render() public void render()
......
package com.matt.difinspector.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.matt.difinspector.io.ReverseDataInputStream;
import com.matt.difinspector.io.ReverseDataOutputStream;
public class Settings
{
private static String lastOpenDir = "";
private static String lastSaveDir = "";
private static final int VERSION = 0;
public static void loadConfig()
{
File configFile = new File(Util.getAppDir(), "config.cfg");
if (!configFile.exists())
{
Settings.saveConfig();
return;
}
try
{
ReverseDataInputStream dis = new ReverseDataInputStream(new FileInputStream(configFile));
int magic1 = dis.readUnsignedByte();
int magic2 = dis.readUnsignedByte();
int magic3 = dis.readUnsignedByte();
int magic4 = dis.readUnsignedByte();
if (magic1 != 'D' || magic2 != 'I' || magic3 != 'F' || magic4 != 'C')
{
dis.close();
return;
}
int readVersion = dis.readInt();
if (readVersion != Settings.VERSION)
{
dis.close();
return;
}
Settings.lastOpenDir = dis.readString();
Settings.lastSaveDir = dis.readString();
dis.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public static void saveConfig()
{
File configFile = new File(Util.getAppDir(), "config.cfg");
try
{
ReverseDataOutputStream dos = new ReverseDataOutputStream(new FileOutputStream(configFile));
dos.writeByte('D');
dos.writeByte('I');
dos.writeByte('F');
dos.writeByte('C');
dos.writeInt(Settings.VERSION);
dos.writeString(Settings.lastOpenDir);
dos.writeString(Settings.lastSaveDir);
dos.flush();
dos.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
public static void setLastOpenDir(String lastOpenDir)
{
Settings.lastOpenDir = lastOpenDir;
}
public static String getLastOpenDir()
{
return Settings.lastOpenDir;
}
public static void setLastSaveDir(String lastSaveDir)
{
Settings.lastSaveDir = lastSaveDir;
}
public static String getLastSaveDir()
{
return Settings.lastSaveDir;
}
}
package com.matt.difinspector.util; package com.matt.difinspector.util;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.Vector; import java.util.Vector;
...@@ -404,4 +405,70 @@ public final class Util ...@@ -404,4 +405,70 @@ public final class Util
return new TexData(normal, offset, rotate, scale); return new TexData(normal, offset, rotate, scale);
} }
public static enum EnumOS
{
LINUX,
SOLARIS,
WINDOWS,
OSX,
UNKNOWN
}
public static final EnumOS getOS()
{
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win"))
return EnumOS.WINDOWS;
else if (osName.contains("mac"))
return EnumOS.OSX;
else if (osName.contains("solaris") || osName.contains("sunos"))
return EnumOS.SOLARIS;
else if (osName.contains("linux") || osName.contains("unix"))
return EnumOS.LINUX;
else
return EnumOS.UNKNOWN;
}
public static final File getAppDir(String s)
{
EnumOS os = Util.getOS();
String home = System.getProperty("user.home", ".");
File result;
switch (os)
{
case SOLARIS:
case LINUX:
result = new File(home, "." + s + "/");
break;
case WINDOWS:
String appdata = System.getenv("APPDATA");
if (appdata == null)
{
result = new File(home, "." + s + "/");
} else {
result = new File(appdata, "." + s + "/");
}
break;
case OSX:
result = new File(home, "Library/Application Support/" + s);
default:
result = new File(home, s + "/");
break;
}
if (!result.exists() && !result.mkdirs())
{
throw new RuntimeException("The working directory could not be created: " + result.toString());
}
return result;
}
public static final File getAppDir()
{
File file = getAppDir("difinspector");
file.mkdirs();
return file;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment