Skip to content

Commit

Permalink
data files moved into data folder
Browse files Browse the repository at this point in the history
current state of data files will be embedded into JAR file as a set
predefined files
new data will be written into a newly created folder "data" near JAR
file
  • Loading branch information
Hendrik2319 committed Jun 18, 2023
1 parent aff1e0b commit 6cf78d8
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 51 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="data"/>
<classpathentry kind="src" path="res"/>
<classpathentry combineaccessrules="false" kind="src" path="/JavaLib_JSON_Parser"/>
<classpathentry combineaccessrules="false" kind="src" path="/JavaLib_Common_Dialogs"/>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 5 additions & 6 deletions src/net/schwarzbaer/java/games/snowrunner/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -30,8 +29,8 @@
import net.schwarzbaer.java.games.snowrunner.SnowRunner.TextOutput;
import net.schwarzbaer.java.games.snowrunner.XMLTemplateStructure.Class_;
import net.schwarzbaer.java.games.snowrunner.XMLTemplateStructure.Class_.Item;
import net.schwarzbaer.java.lib.gui.ValueListOutput;
import net.schwarzbaer.java.games.snowrunner.XMLTemplateStructure.GenericXmlNode;
import net.schwarzbaer.java.lib.gui.ValueListOutput;

public class Data {

Expand Down Expand Up @@ -305,11 +304,11 @@ public static class UserDefinedValues extends SnowRunner.Initializable

void read()
{
File file = new File(SnowRunner.UserDefinedValuesFile);
System.out.printf("Read UserDefinedValues from file \"%s\" ...%n", file.getAbsolutePath());
DataFiles.DataSource ds = DataFiles.DataFile.UserDefinedValuesFile.getDataSourceForReading();
System.out.printf("Read UserDefinedValues from %s ...%n", ds);
truckValues.clear();

try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(ds.createInputStream(), StandardCharsets.UTF_8))) {

Truck.UDV truckValues = null;
String line, valueStr;
Expand Down Expand Up @@ -346,7 +345,7 @@ void read()
public void write()
{
checkInitialized();
File file = new File(SnowRunner.UserDefinedValuesFile);
File file = DataFiles.DataFile.UserDefinedValuesFile.getFileForWriting();
System.out.printf("Write UserDefinedValues to file \"%s\" ...%n", file.getAbsolutePath());
try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {

Expand Down
106 changes: 106 additions & 0 deletions src/net/schwarzbaer/java/games/snowrunner/DataFiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package net.schwarzbaer.java.games.snowrunner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Objects;

public class DataFiles
{
private static final File dataFolder = new File("data");

public enum DataFile
{
DLCAssignmentsFile ("DLCAssignments.dat" ),
UserDefinedValuesFile ("UserDefinedValues.dat" ),
FilterRowsPresetsFile ("FilterRowsPresets.dat" ),
ColumnHidePresetsFile ("ColumnHidePresets.dat" ),
RowColoringsFile ("RowColorings.dat" ),
SpecialTruckAddonsFile ("SpecialTruckAddons.dat" ),
WheelsQualityRangesFile("WheelsQualityRanges.dat"),
;
private final String name;
private DataFile( String name ) { this.name = name; }

private File getFile() { return new File(dataFolder, name); }
private String getResourcePath() { return String.format("/%s", name); }

public File getFileForWriting () { return DataFiles.getFileForWriting (this); }
public DataSource getDataSourceForReading() { return DataFiles.getDataSourceForReading(this); }
}

public static void checkDataFolder()
{
if (!dataFolder.isDirectory() && dataFolder.exists())
{
System.err.printf("WARNING:%n");
System.err.printf(" Will not be able to create data folder at \"%s\".%n", dataFolder.getAbsolutePath());
System.err.printf(" A file or something else exists at this position.%n");
System.err.printf(" Writing of any data files will cause an exception, that will stop the application.%n");
}
}

public static File getFileForWriting(DataFile dataFile)
{
if (dataFile==null)
throw new IllegalArgumentException();

if (!dataFolder.isDirectory())
{
if (dataFolder.exists())
throw new RuntimeException(String.format("Can't write \"%s\". Could not create data folder at \"%s\". A file or something else exists at this position.", dataFile, dataFolder.getAbsolutePath()));

boolean success = dataFolder.mkdir();
if (!success)
throw new RuntimeException(String.format("Can't write \"%s\". Could not create data folder at \"%s\".", dataFile, dataFolder.getAbsolutePath()));
}

return dataFile.getFile();
}

public static DataSource getDataSourceForReading(DataFile dataFile)
{
if (dataFile==null)
throw new IllegalArgumentException();

System.out.printf("getDataSourceForReading: %s%n", dataFile.getFile().getAbsolutePath());
if (dataFile.getFile().isFile())
return new DataSource(dataFile.getFile());

return new DataSource(dataFile.getResourcePath());
}


public static class DataSource
{
private final File file;
private final String resourcePath;

private DataSource(File file)
{
this.file = Objects.requireNonNull(file);
this.resourcePath = null;
}

private DataSource(String resourcePath)
{
this.resourcePath = Objects.requireNonNull(resourcePath);
this.file = null;
}

public InputStream createInputStream() throws FileNotFoundException
{
if (file !=null) return new FileInputStream(file);
if (resourcePath!=null) return getClass().getResourceAsStream(resourcePath);
throw new IllegalStateException();
}

@Override public String toString()
{
if (file !=null) return String.format("file \"%s\"", file.getAbsolutePath());
if (resourcePath!=null) return String.format("resource \"%s\"", resourcePath);
throw new IllegalStateException();
}
}
}
58 changes: 26 additions & 32 deletions src/net/schwarzbaer/java/games/snowrunner/SnowRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -94,6 +93,13 @@
import net.schwarzbaer.java.games.snowrunner.tables.SetInstancesTableModel.GearboxesTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.SetInstancesTableModel.SuspensionsTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.SetInstancesTableModel.WinchesTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.TableSimplifier;
import net.schwarzbaer.java.games.snowrunner.tables.TrailersTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.TruckAddonsTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.TruckPanelProto;
import net.schwarzbaer.java.games.snowrunner.tables.TruckTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.VerySimpleTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.WheelsTableModel;
import net.schwarzbaer.java.lib.gui.ContextMenu;
import net.schwarzbaer.java.lib.gui.Disabler;
import net.schwarzbaer.java.lib.gui.ImageViewDialog;
Expand All @@ -103,24 +109,10 @@
import net.schwarzbaer.java.lib.gui.Tables;
import net.schwarzbaer.java.lib.system.DateTimeFormatter;
import net.schwarzbaer.java.lib.system.Settings;
import net.schwarzbaer.java.games.snowrunner.tables.TableSimplifier;
import net.schwarzbaer.java.games.snowrunner.tables.TrailersTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.TruckAddonsTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.TruckPanelProto;
import net.schwarzbaer.java.games.snowrunner.tables.TruckTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.VerySimpleTableModel;
import net.schwarzbaer.java.games.snowrunner.tables.WheelsTableModel;

public class SnowRunner {

public static final String DLCAssignmentsFile = "SnowRunner - DLCAssignments.dat";
public static final String UserDefinedValuesFile = "SnowRunner - UserDefinedValues.dat";
public static final String FilterRowsPresetsFile = "SnowRunner - FilterRowsPresets.dat";
public static final String ColumnHidePresetsFile = "SnowRunner - ColumnHidePresets.dat";
public static final String RowColoringsFile = "SnowRunner - RowColorings.dat";
public static final String SpecialTruckAddonsFile = "SnowRunner - SpecialTruckAddons.dat";
public static final String TruckImagesFile = "SnowRunner - TruckImages.zip";
public static final String WheelsQualityRangesFile = "SnowRunner - WheelsQualityRanges.dat";
public static final String TruckImagesFile = "SnowRunner - TruckImages.zip";

public static final Color COLOR_FG_DLCTRUCK = new Color(0x0070FF);
public static final Color COLOR_FG_OWNEDTRUCK = new Color(0x00AB00);
Expand Down Expand Up @@ -277,6 +269,7 @@ private void initialize()
}

private void initialize() {
DataFiles.checkDataFolder();
gfds.initialize();

if (loadInitialPAK()) updateAfterDataChange();
Expand Down Expand Up @@ -1270,7 +1263,7 @@ void saveData()
{
checkInitialized();

File file = new File(DLCAssignmentsFile);
File file = DataFiles.DataFile.DLCAssignmentsFile.getFileForWriting();
System.out.printf("Write DLCs to file \"%s\" ...%n", file.getAbsolutePath());

HashMap<String,Vector<String>> reversedTruckMap = getReversedMap(trucks );
Expand Down Expand Up @@ -1311,14 +1304,14 @@ private void writeIDs(HashMap<String, Vector<String>> reversedMap, String dlc, P

void loadStoredData()
{
File file = new File(DLCAssignmentsFile);
System.out.printf("Read DLCs from file \"%s\" ...%n", file.getAbsolutePath());
DataFiles.DataSource ds = DataFiles.DataFile.DLCAssignmentsFile.getDataSourceForReading();
System.out.printf("Read DLCs from %s ...%n", ds);

trucks .clear();
regions.clear();
maps .clear();

try (BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( file ), StandardCharsets.UTF_8) )) {
try (BufferedReader in = new BufferedReader( new InputStreamReader( ds.createInputStream(), StandardCharsets.UTF_8) )) {

String line, value, lastDLC=null;
while ( (line=in.readLine())!=null ) {
Expand Down Expand Up @@ -1404,10 +1397,11 @@ public SpecialTruckAddons(Listener listenersController) {

public void readFromFile()
{
File file = new File(SpecialTruckAddonsFile);
try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {

System.out.printf("Read SpecialTruckAddons from file \"%s\" ...%n", file.getAbsolutePath());
DataFiles.DataSource ds = DataFiles.DataFile.SpecialTruckAddonsFile.getDataSourceForReading();

try (BufferedReader in = new BufferedReader(new InputStreamReader(ds.createInputStream(), StandardCharsets.UTF_8)))
{
System.out.printf("Read SpecialTruckAddons from %s ...%n", ds);

SpecialTruckAddonList list = null;
String line, valueStr;
Expand All @@ -1420,9 +1414,9 @@ public void readFromFile()
AddonCategory listID = AddonCategory.valueOf(valueStr);
list = lists.get(listID);
if (list==null)
throw new IllegalStateException(String.format("Found List ID (\"%s\") with no assigned SpecialTruckAddonList in file \"%s\".", listID, file.getAbsolutePath()));
throw new IllegalStateException(String.format("Found List ID (\"%s\") with no assigned SpecialTruckAddonList in %s.", listID, ds));
} catch (Exception e) {
throw new IllegalStateException(String.format("Found unknown List ID (\"%s\") in file \"%s\".", valueStr, file.getAbsolutePath()));
throw new IllegalStateException(String.format("Found unknown List ID (\"%s\") in %s.", valueStr, ds));
}
}
if (line.startsWith(ValuePrefix) && list!=null) {
Expand All @@ -1437,7 +1431,7 @@ public void readFromFile()
} catch (FileNotFoundException ex) {
//ex.printStackTrace();
} catch (IOException ex) {
System.err.printf("IOException while reading SpecialTruckAddons from file \"%s\": %s", file.getAbsolutePath(), ex.getMessage());
System.err.printf("IOException while reading SpecialTruckAddons from %s: %s", ds, ex.getMessage());
//ex.printStackTrace();
}
setInitialized();
Expand All @@ -1446,7 +1440,7 @@ public void readFromFile()
public void writeToFile()
{
checkInitialized();
File file = new File(SpecialTruckAddonsFile);
File file = DataFiles.DataFile.SpecialTruckAddonsFile.getFileForWriting();
try (PrintWriter out = new PrintWriter(file, StandardCharsets.UTF_8)) {

System.out.printf("Write SpecialTruckAddons to file \"%s\" ...%n", file.getAbsolutePath());
Expand Down Expand Up @@ -2270,12 +2264,12 @@ private QualityData getOrCreateQualityData(DataMapIndex index)

private void readFile()
{
File file = new File(WheelsQualityRangesFile);
System.out.printf("Read WheelsQualityRanges from file \"%s\" ...%n", file.getAbsolutePath());
DataFiles.DataSource ds = DataFiles.DataFile.WheelsQualityRangesFile.getDataSourceForReading();
System.out.printf("Read WheelsQualityRanges from %s ...%n", ds);

data.clear();

try (BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( file ), StandardCharsets.UTF_8) ))
try (BufferedReader in = new BufferedReader( new InputStreamReader( ds.createInputStream(), StandardCharsets.UTF_8) ))
{

String line, value;
Expand Down Expand Up @@ -2346,7 +2340,7 @@ private void writeFile()
{
checkInitialized();

File file = new File(WheelsQualityRangesFile);
File file = DataFiles.DataFile.WheelsQualityRangesFile.getFileForWriting();
System.out.printf("Write WheelsQualityRanges to file \"%s\" ...%n", file.getAbsolutePath());

try (PrintWriter out = new PrintWriter( new OutputStreamWriter( new FileOutputStream( file ), StandardCharsets.UTF_8) ))
Expand Down
Loading

0 comments on commit 6cf78d8

Please sign in to comment.