diff --git a/pom.xml b/pom.xml index 3e639b6..501ab28 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ Tihwin Tihwin - 1.1 + 2.0 https://github.com/developersu/${project.artifactId}/ diff --git a/src/main/java/tihwin/MainAppUi.java b/src/main/java/tihwin/MainAppUi.java index 6960abc..79b4d93 100644 --- a/src/main/java/tihwin/MainAppUi.java +++ b/src/main/java/tihwin/MainAppUi.java @@ -82,7 +82,6 @@ public class MainAppUi extends JFrame { destinationDirectoryLbl.setText(FilesHelper.getRealFolder(Settings.INSTANCE.getDestination())); addWindowListener(getWindowListener()); - new UpdateUlTableUi(new File(destinationDirectoryLbl.getText() + File.separator + "ul.cfg")); } private WindowListener getWindowListener() { @@ -219,7 +218,7 @@ public class MainAppUi extends JFrame { } private void ulConfigButtonAction() { - new UpdateUlTableUi(new File(destinationDirectoryLbl.getText() + File.separator + "ul.cfg")); + new UpdateUlTableUi(destinationDirectoryLbl.getText()); } { @@ -266,11 +265,11 @@ public class MainAppUi extends JFrame { this.$$$loadLabelText$$$(label3, this.$$$getMessageFromBundle$$$("locale", "TitleLbl")); mainPanel.add(label3, cc.xy(5, 5)); CDRadioButton = new JRadioButton(); - CDRadioButton.setText("CD"); + this.$$$loadButtonText$$$(CDRadioButton, this.$$$getMessageFromBundle$$$("locale", "CD")); mainPanel.add(CDRadioButton, cc.xy(1, 5)); DVDRadioButton = new JRadioButton(); DVDRadioButton.setSelected(true); - DVDRadioButton.setText("DVD"); + this.$$$loadButtonText$$$(DVDRadioButton, this.$$$getMessageFromBundle$$$("locale", "DVD")); mainPanel.add(DVDRadioButton, cc.xy(3, 5)); convertBtn = new JButton(); convertBtn.setBackground(new Color(-2034433)); diff --git a/src/main/java/tihwin/UpdateUlTableUi.java b/src/main/java/tihwin/UpdateUlTableUi.java index 60f2d4f..7d9e2f2 100644 --- a/src/main/java/tihwin/UpdateUlTableUi.java +++ b/src/main/java/tihwin/UpdateUlTableUi.java @@ -22,37 +22,38 @@ package tihwin; import com.jgoodies.forms.layout.CellConstraints; import com.jgoodies.forms.layout.FormLayout; -import tihwin.ui.*; import tihwin.ui.ulupdater.*; +import tihwin.ul.UlConfiguration; +import tihwin.ul.UlServiceTools; import javax.swing.*; import javax.swing.border.MatteBorder; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.JTableHeader; -import javax.swing.text.AbstractDocument; import java.awt.*; import java.io.File; -import java.util.Objects; -import java.util.ResourceBundle; +import java.util.*; +import java.util.List; public class UpdateUlTableUi extends JFrame { private JTable table; private UlTableModel model; private JButton saveChangesBtn; - private JLabel ulLocationLbl, statusLbl; + private final JLabel ulLocationLbl; + private JLabel statusLbl; private String recentRomLocation; private final ResourceBundle resourceBundle; - public UpdateUlTableUi(File ulCfgFile) { + public UpdateUlTableUi(String ulDestinationLocation) { super(); this.resourceBundle = ResourceBundle.getBundle("locale"); - setupUlLocationLbl(); + this.ulLocationLbl = new JLabel(ulDestinationLocation); setupTable(); setupSaveButton(); FormLayout primaryPanelLayout = new FormLayout( - "75dlu, 4dlu, fill:pref:grow", + "80dlu, 2dlu, fill:pref:grow", "fill:pref:grow, 25dlu:noGrow, 25dlu:noGrow, fill:pref:noGrow" ); JPanel primaryPanel = new JPanel(); @@ -60,12 +61,16 @@ public class UpdateUlTableUi extends JFrame { primaryPanel.add(getScrollPane(), new CellConstraints(1, 1, 3, 1, CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(0, 0, 0, 0))); + primaryPanel.add(getSelectUlLocationButton(), new CellConstraints(1, 2, 1, 1, - CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(5, 5, 5, 5))); + CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(3, 3, 3, 3))); + primaryPanel.add(ulLocationLbl, new CellConstraints(3, 2, 1, 1, CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(0, 0, 0, 0))); + primaryPanel.add(saveChangesBtn, new CellConstraints(1, 3, 3, 1, - CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(5, 5, 5, 5))); + CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(3, 3, 3, 3))); + primaryPanel.add(getStatusPanel(), new CellConstraints(1, 4, 3, 1, CellConstraints.DEFAULT, CellConstraints.DEFAULT, new Insets(0, 0, 0, 0))); @@ -76,20 +81,16 @@ public class UpdateUlTableUi extends JFrame { Image img = new ImageIcon(Objects.requireNonNull( MainAppUi.class.getClassLoader().getResource("tray_icon.gif"))).getImage(); setIconImage(img); - setMinimumSize(new Dimension(800, 500)); + setMinimumSize(new Dimension(800, 400)); setVisible(true); setTitle(resourceBundle.getString("ulManager")); - System.out.println(ulCfgFile.getAbsolutePath()); - this.recentRomLocation = ulCfgFile.getParent(); + + File ulCfgFile = new File(ulDestinationLocation + File.separator + "ul.cfg"); + this.recentRomLocation = ulDestinationLocation; if (ulCfgFile.exists()) showInTableUlCfgFile(ulCfgFile); - setAlwaysOnTop(true); // TODO:DELETE - //statusLbl.setText("TEST"); // TODO:DELETE } - private void setupUlLocationLbl(){ - ulLocationLbl = new JLabel(Settings.INSTANCE.getDestination()); - } private void setupSaveButton(){ saveChangesBtn = new JButton(resourceBundle.getString("ulManagerWindow_SaveBtn")); saveChangesBtn.setBackground(Color.getHSBColor(0.5591398f, 0.12156863f, 1)); @@ -111,10 +112,6 @@ public class UpdateUlTableUi extends JFrame { UlTableColumnModel columnModel = new UlTableColumnModel((DefaultTableColumnModel) table.getColumnModel()); table.setColumnModel(columnModel); table.setRowSelectionAllowed(false); - - JTextField textField = new JTextField(); - ((AbstractDocument) textField.getDocument()).setDocumentFilter(new TitleFieldFilter()); - table.setDefaultEditor(JLabel.class, new DefaultCellEditor(textField)); } private JScrollPane getScrollPane(){ JScrollPane scrollPane = new JScrollPane(table); @@ -137,34 +134,80 @@ public class UpdateUlTableUi extends JFrame { } private void showInTableUlCfgFile(File ulCfgFile){ - for (int i=0; i<15; i++) { - model.addRow( - new UlTableRow(table.getRowCount() + 1, i + "_lent Hi11 **atter** ***ories1", "NORG_000.00", 5, true)); - model.addRow( - new UlTableRow(table.getRowCount() + 1, "name"+i, "NORG_000.11", 5, false)); - model.addRow( - new UlTableRow(table.getRowCount() + 1, i+"name", "NORG_000.99", 5, true)); - } - /*-*-*-*-*-*-*-*-**/ - if (ulCfgFile.length() < 64){ - statusLbl.setText(resourceBundle.getString("ulManagerWindow_EmptyOrIncorrectText")+" "+ulCfgFile.getAbsolutePath()); - return; - } - System.out.println("IMPLMENET ME"); // TODO + try{ + if (ulCfgFile.length() < 64){ + statusLbl.setText(resourceBundle.getString("ulManagerWindow_EmptyOrIncorrectText")+" "+ulCfgFile.getAbsolutePath()); + return; + } - saveChangesBtn.setEnabled(true); - statusLbl.setText(ulCfgFile.getAbsolutePath()); + model.clear(); + + String ulCfgFileLocation = ulCfgFile.getParentFile().getAbsolutePath(); + + for (int i = 0; i < ulCfgFile.length()/64; i++) { + UlConfiguration ulConfiguration = new UlConfiguration(ulCfgFile, i); + boolean isConsistent = UlServiceTools.verifyChunksCount(ulCfgFileLocation, ulConfiguration); + model.addRow(new UlTableModelRecord(ulConfiguration, isConsistent)); + } + + saveChangesBtn.setEnabled(true); + statusLbl.setText(ulCfgFile.getAbsolutePath()); + } + catch (Exception e){ + statusLbl.setText(resourceBundle.getString("ulManagerWindow_EmptyOrIncorrectText")+" "+e.getMessage()); + e.printStackTrace(); + } } private void saveChangesAction(){ - System.out.println("IMPLMENET ME"); // TODO + try{ + String ulLocation = ulLocationLbl.getText(); + + List modelRecords = model.getInitialRows(); + List finalConfigurationSet = new ArrayList<>(); + // Collect what we'll have in the final ul.cfg file + for (int i = 0; i < modelRecords.size(); i++){ + UlConfiguration configuration = new UlConfiguration( + model.getTitle(i), + model.getPublisherTitle(i), + model.getChunksCount(i), + model.getCdDvd(i).equals("DVD") + ); + finalConfigurationSet.add(configuration); + } + // Updating chunk file names if needed + for (int i = 0; i < modelRecords.size(); i++){ + UlTableModelRecord initialRecord = modelRecords.get(i); + String initialRecordTitle = initialRecord.getConfiguration().getTitle(); + if (initialRecord.isConsistent() && ! model.getTitle(i).equals(initialRecordTitle)){ + UlServiceTools.renameChunks(ulLocation, + initialRecord.getConfiguration(), + finalConfigurationSet.get(i)); + } + } + // Remove chunks in case user removed record from the table + List removedRows = model.getRemovedRows(); + for (UlTableModelRecord removedRow : removedRows) { + UlServiceTools.removeChunks(ulLocation, removedRow.getConfiguration()); + } + // Write new ul.cfg + UlServiceTools.writeUlCfgFile(ulLocation, finalConfigurationSet); + File ulCfgFile = new File(ulLocation+File.separator+"ul.cfg"); + showInTableUlCfgFile(ulCfgFile); + + statusLbl.setText(resourceBundle.getString("SuccessText")); + } + catch (Exception e){ + statusLbl.setText(resourceBundle.getString("ulManagerWindow_SaveChangesFailureText")+" "+e.getMessage()); + e.printStackTrace(); + } } private void selectUlCfgAction(){ try { JFileChooser fileChooser = new JFileChooser(FilesHelper.getRealFolder(recentRomLocation)); fileChooser.setDialogTitle(resourceBundle.getString("ulManagerWindow_SelectUlCfgBtn")); - fileChooser.setFileFilter(new ulCfgFileFilter()); + fileChooser.setFileFilter(new UlCfgFileFilter()); if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); recentRomLocation = file.getParent(); diff --git a/src/main/java/tihwin/ui/TitleFieldFilter.java b/src/main/java/tihwin/ui/TitleFieldFilter.java index b836733..91dd043 100644 --- a/src/main/java/tihwin/ui/TitleFieldFilter.java +++ b/src/main/java/tihwin/ui/TitleFieldFilter.java @@ -25,13 +25,12 @@ import javax.swing.text.BadLocationException; import javax.swing.text.DocumentFilter; public class TitleFieldFilter extends DocumentFilter { - private final static int MAX_USER_DEFINED_GAME_TITLE_LENGTH = 31; + private final static int MAX_USER_DEFINED_GAME_TITLE_LENGTH = 32; public TitleFieldFilter() {} protected boolean isNotAscii(String str){ if (str == null) return false; - return ! str.matches("\\A\\p{ASCII}*\\z"); } @@ -40,7 +39,7 @@ public class TitleFieldFilter extends DocumentFilter { if (isNotAscii(str)) return; int documentLength = fb.getDocument().getLength(); - if ((documentLength + str.length()) <= MAX_USER_DEFINED_GAME_TITLE_LENGTH) + if ((documentLength + str.length()) < MAX_USER_DEFINED_GAME_TITLE_LENGTH) super.insertString(fb, offs, str, attr); } @@ -49,7 +48,8 @@ public class TitleFieldFilter extends DocumentFilter { if (isNotAscii(str)) return; int documentLength = fb.getDocument().getLength(); - if ((documentLength + str.length() - length) <= MAX_USER_DEFINED_GAME_TITLE_LENGTH) + if ((documentLength + str.length() - length) < MAX_USER_DEFINED_GAME_TITLE_LENGTH) super.replace(fb, offs, length, str, attr); } + } \ No newline at end of file diff --git a/src/main/java/tihwin/ui/ulupdater/UlButtonCellEditor.java b/src/main/java/tihwin/ui/ulupdater/UlButtonCellEditor.java index d29a7cf..448648e 100644 --- a/src/main/java/tihwin/ui/ulupdater/UlButtonCellEditor.java +++ b/src/main/java/tihwin/ui/ulupdater/UlButtonCellEditor.java @@ -25,10 +25,6 @@ import javax.swing.table.TableCellEditor; import java.awt.*; public class UlButtonCellEditor extends AbstractCellEditor implements TableCellEditor { - public UlButtonCellEditor(){ - - } - @Override public Object getCellEditorValue() { return null; @@ -36,7 +32,6 @@ public class UlButtonCellEditor extends AbstractCellEditor implements TableCellE @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { - ((UlTableModel) table.getModel()).removeRow(row); return null; } diff --git a/src/main/java/tihwin/ui/ulupdater/UlCdDvdCellEditor.java b/src/main/java/tihwin/ui/ulupdater/UlCdDvdCellEditor.java index 79fd81f..82a56ce 100644 --- a/src/main/java/tihwin/ui/ulupdater/UlCdDvdCellEditor.java +++ b/src/main/java/tihwin/ui/ulupdater/UlCdDvdCellEditor.java @@ -21,12 +21,10 @@ package tihwin.ui.ulupdater; import javax.swing.*; -import java.util.ResourceBundle; public class UlCdDvdCellEditor extends DefaultCellEditor { static { - ResourceBundle bundle = ResourceBundle.getBundle("locale"); - CD_DVD = new String[]{bundle.getString("CD"), bundle.getString("DVD")}; + CD_DVD = new String[]{"CD", "DVD"}; } private static final String[] CD_DVD; diff --git a/src/main/java/tihwin/ui/ulupdater/ulCfgFileFilter.java b/src/main/java/tihwin/ui/ulupdater/UlCfgFileFilter.java similarity index 95% rename from src/main/java/tihwin/ui/ulupdater/ulCfgFileFilter.java rename to src/main/java/tihwin/ui/ulupdater/UlCfgFileFilter.java index 084eae4..b931704 100644 --- a/src/main/java/tihwin/ui/ulupdater/ulCfgFileFilter.java +++ b/src/main/java/tihwin/ui/ulupdater/UlCfgFileFilter.java @@ -23,7 +23,7 @@ package tihwin.ui.ulupdater; import javax.swing.filechooser.FileFilter; import java.io.File; -public class ulCfgFileFilter extends FileFilter { +public class UlCfgFileFilter extends FileFilter { @Override public boolean accept(File file) { if (file.isDirectory()) diff --git a/src/main/java/tihwin/ui/ulupdater/UlTableColumnModel.java b/src/main/java/tihwin/ui/ulupdater/UlTableColumnModel.java index 4d19c7c..4a2e4bb 100644 --- a/src/main/java/tihwin/ui/ulupdater/UlTableColumnModel.java +++ b/src/main/java/tihwin/ui/ulupdater/UlTableColumnModel.java @@ -20,9 +20,13 @@ */ package tihwin.ui.ulupdater; +import tihwin.ui.TitleFieldFilter; + +import javax.swing.*; import javax.swing.event.TableColumnModelEvent; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.TableColumn; +import javax.swing.text.AbstractDocument; import java.util.Enumeration; public class UlTableColumnModel extends DefaultTableColumnModel { @@ -37,7 +41,8 @@ public class UlTableColumnModel extends DefaultTableColumnModel { getColumn(0).setMaxWidth(50); getColumn(1).setMinWidth(200); - getColumn(1).setPreferredWidth(225); + getColumn(1).setPreferredWidth(230); + getColumn(1).setMaxWidth(300); getColumn(2).setMinWidth(100); getColumn(2).setPreferredWidth(135); @@ -51,6 +56,14 @@ public class UlTableColumnModel extends DefaultTableColumnModel { getColumn(4).setPreferredWidth(75); getColumn(4).setMaxWidth(100); + getColumn(5).setMinWidth(100); + getColumn(5).setPreferredWidth(135); + getColumn(5).setMaxWidth(150); + + JTextField textField = new JTextField(); + ((AbstractDocument) textField.getDocument()).setDocumentFilter(new TitleFieldFilter()); + getColumn(1).setCellEditor(new DefaultCellEditor(textField)); + getColumn(4).setCellEditor(new UlCdDvdCellEditor()); getColumn(5).setCellEditor(new UlButtonCellEditor()); diff --git a/src/main/java/tihwin/ui/ulupdater/UlTableModel.java b/src/main/java/tihwin/ui/ulupdater/UlTableModel.java index b70f7e0..aeeb19d 100644 --- a/src/main/java/tihwin/ui/ulupdater/UlTableModel.java +++ b/src/main/java/tihwin/ui/ulupdater/UlTableModel.java @@ -22,31 +22,53 @@ package tihwin.ui.ulupdater; import javax.swing.*; import javax.swing.table.DefaultTableModel; +import java.util.ArrayList; +import java.util.List; import java.util.ResourceBundle; public class UlTableModel extends DefaultTableModel { static { ResourceBundle bundle = ResourceBundle.getBundle("locale"); - columns = new String[]{ + COLUMNS = new String[]{ bundle.getString("ulManagerWindow_ColumnNameNumber"), bundle.getString("ulManagerWindow_ColumnNameTitle"), bundle.getString("ulManagerWindow_ColumnNamePublisherTitle"), bundle.getString("ulManagerWindow_ColumnNameChunksCount"), bundle.getString("ulManagerWindow_ColumnCdDvdFlag"), + "", ""}; + REMOVE_ME_BUTTON_TEXT = bundle.getString("ulManagerWindow_Row_RemoveRowBtn"); + OK_STATUS = bundle.getString("Ok"); + INCONSISTENT_STATUS = bundle.getString("ulManagerWindow_InconsistentFileText"); } - private static final String[] columns; + private static final String[] COLUMNS; - private int rowsCount; + private static final String REMOVE_ME_BUTTON_TEXT; + + private static final String OK_STATUS; + private static final String INCONSISTENT_STATUS; + + private final List rows; + private final List removedRows; public UlTableModel(){ super(); - super.setColumnIdentifiers(columns); + super.setColumnIdentifiers(COLUMNS); + this.rows = new ArrayList<>(); + this.removedRows = new ArrayList<>(); } - public void addRow(UlTableRow row){ - super.addRow(row.getRow()); - this.rowsCount++; + public void addRow(UlTableModelRecord record){ + rows.add(record); + super.addRow(new Object[]{ + rows.size(), + record.getConfiguration().getTitle(), + record.getConfiguration().getPublisherTitle(), + record.getConfiguration().getChunksCount(), + record.getConfiguration().isDvd()? "DVD" : "CD", + new JButton(REMOVE_ME_BUTTON_TEXT), + record.isConsistent()?OK_STATUS:INCONSISTENT_STATUS + }); } @Override @@ -70,9 +92,38 @@ public class UlTableModel extends DefaultTableModel { @Override public void removeRow(int row) { + removedRows.add(rows.get(row)); + rows.remove(row); super.removeRow(row); - --rowsCount; - for (int i = row; i < rowsCount; i++) + for (int i = row; i < rows.size(); i++) setValueAt(i+1, i, 0); } + + public void clear(){ + for (int i = getRowCount()-1; i >= 0 ; i--) + super.removeRow(i); + rows.clear(); + removedRows.clear(); + } + + public List getInitialRows(){ + return rows; + } + + public List getRemovedRows() { + return removedRows; + } + + public String getTitle(int row){ + return (String) getValueAt(row, 1); + } + public String getPublisherTitle(int row){ + return (String) getValueAt(row, 2); + } + public byte getChunksCount(int row){ + return (byte) getValueAt(row, 3); + } + public String getCdDvd(int row){ + return (String) getValueAt(row, 4); + } } diff --git a/src/main/java/tihwin/ui/ulupdater/UlTableModelRecord.java b/src/main/java/tihwin/ui/ulupdater/UlTableModelRecord.java new file mode 100644 index 0000000..2afc9b6 --- /dev/null +++ b/src/main/java/tihwin/ui/ulupdater/UlTableModelRecord.java @@ -0,0 +1,41 @@ +/* + + Copyright "2022" Dmitry Isaenko + + This file is part of Tihwin. + + Tihwin is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Tihwin is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Tihwin. If not, see . + + */ +package tihwin.ui.ulupdater; + +import tihwin.ul.UlConfiguration; + +public class UlTableModelRecord { + private final UlConfiguration configuration; + private final boolean isConsistent; + + public UlTableModelRecord(UlConfiguration configuration, boolean isConsistent){ + this.configuration = configuration; + this.isConsistent = isConsistent; + } + + public UlConfiguration getConfiguration() { + return configuration; + } + + public boolean isConsistent() { + return isConsistent; + } +} diff --git a/src/main/java/tihwin/ui/ulupdater/UlTableRow.java b/src/main/java/tihwin/ui/ulupdater/UlTableRow.java deleted file mode 100644 index b7fdca4..0000000 --- a/src/main/java/tihwin/ui/ulupdater/UlTableRow.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - - Copyright "2022" Dmitry Isaenko - - This file is part of Tihwin. - - Tihwin is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Tihwin is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Tihwin. If not, see . - - */ -package tihwin.ui.ulupdater; - -import javax.swing.*; -import java.util.ResourceBundle; - -public class UlTableRow { - static { - ResourceBundle bundle = ResourceBundle.getBundle("locale"); - REMOVE_ME_BUTTON_TEXT = bundle.getString("ulManagerWindow_Row_RemoveRowBtn"); - CD_TEXT = bundle.getString("CD"); - DVD_TEXT = bundle.getString("DVD"); - } - private static final String REMOVE_ME_BUTTON_TEXT; - private static final String CD_TEXT; - private static final String DVD_TEXT; - - private final int rowNumber; - private final String name; - private final String publisherTitle; - private final int chunksCount; - private final String cdDvdFlag; - private final JButton removeRowButton; - - public UlTableRow(int rowNumber, String name, String publisherTitle, int chunksCount, boolean isDvd){ - this.rowNumber = rowNumber; - this.name = name; - this.publisherTitle = publisherTitle; - this.chunksCount = chunksCount; - this.cdDvdFlag = isDvd? DVD_TEXT : CD_TEXT; - this.removeRowButton = new JButton(REMOVE_ME_BUTTON_TEXT); - } - - public Object[] getRow(){ - return new Object[]{ - rowNumber, - name, - publisherTitle, - chunksCount, - cdDvdFlag, - removeRowButton - }; - } -} diff --git a/src/main/java/tihwin/ul/UlConfiguration.java b/src/main/java/tihwin/ul/UlConfiguration.java index 5710ef0..932ca4e 100644 --- a/src/main/java/tihwin/ul/UlConfiguration.java +++ b/src/main/java/tihwin/ul/UlConfiguration.java @@ -20,9 +20,12 @@ */ package tihwin.ul; +import java.io.BufferedInputStream; +import java.io.File; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Arrays; public class UlConfiguration { @@ -34,16 +37,36 @@ public class UlConfiguration { private final String crc32; private final byte chunksCount; private final byte cdDvdFlag; + private final boolean romIsDvdImage; + + public UlConfiguration(File ulCfg, int recordNumber) throws Exception{ + try(BufferedInputStream stream = new BufferedInputStream(Files.newInputStream(ulCfg.toPath()))){ + int offset = recordNumber * 0x40; + int read = 0; + while (offset != read) + read += stream.skip(offset); + byte[] buffer = new byte[0x40]; + if (0x40 != stream.read(buffer)) + throw new Exception(recordNumber+" 0x40"); + this.title = new String(buffer, 0, 0x20, StandardCharsets.US_ASCII).trim(); + this.publisherTitle = new String(buffer, 0x23, 0xB, StandardCharsets.US_ASCII).trim(); + this.crc32 = String.format("%08x", OplCRC32(title)).toUpperCase(); + this.chunksCount = buffer[0x2f]; + this.cdDvdFlag = buffer[0x30]; + this.romIsDvdImage = (cdDvdFlag == DVD_FLAG); + } + } public UlConfiguration(String title, String publisherTitle, byte chunksCount, boolean isDVD) throws Exception{ this.title = title; this.publisherTitle = publisherTitle; - this.crc32 = Integer.toHexString(OplCRC32(title)).toUpperCase(); + this.crc32 = String.format("%08x", OplCRC32(title)).toUpperCase(); this.chunksCount = chunksCount; if (isDVD) cdDvdFlag = DVD_FLAG; else cdDvdFlag = CD_FLAG; + this.romIsDvdImage = isDVD; } private int OplCRC32(String string) throws Exception{ @@ -78,6 +101,14 @@ public class UlConfiguration { return crc32; } + public String getTitle() { + return title; + } + + public boolean isDvd() { + return romIsDvdImage; + } + public String getPublisherTitle() { return publisherTitle; } diff --git a/src/main/java/tihwin/ul/UlServiceTools.java b/src/main/java/tihwin/ul/UlServiceTools.java new file mode 100644 index 0000000..ebfb271 --- /dev/null +++ b/src/main/java/tihwin/ul/UlServiceTools.java @@ -0,0 +1,93 @@ +/* + + Copyright "2022" Dmitry Isaenko + + This file is part of Tihwin. + + Tihwin is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Tihwin is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Tihwin. If not, see . + + */ +package tihwin.ul; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.nio.file.*; +import java.util.List; + +public class UlServiceTools { + public static boolean verifyChunksCount(String ulCfgLocation, UlConfiguration configuration){ + int declaredChunksCount = configuration.getChunksCount(); + + File[] files = collectChunks(ulCfgLocation, configuration); + int realChunkCount = 0; + + for (File chunkFile : files) { + if (chunkFile.exists()) + realChunkCount++; + } + + return declaredChunksCount == realChunkCount; + } + + private static File[] collectChunks(String ulCfgLocation, UlConfiguration configuration){ + String pattern = makePattern(ulCfgLocation, configuration); + + int declaredChunksCount = configuration.getChunksCount(); + File[] files = new File[declaredChunksCount]; + + for (int i = 0; i < declaredChunksCount; i++) { + File chunkFile = new File(String.format(pattern, i)); + files[i] = chunkFile; + } + + return files; + } + + private static String makePattern(String ulCfgLocation, UlConfiguration configuration){ + return ulCfgLocation + + File.separator + + String.format("ul.%s.%s.", configuration.getCrc32(), configuration.getPublisherTitle()) + + "%02d"; + } + + public static void renameChunks(String ulCfgLocation, + UlConfiguration oldConfiguration, + UlConfiguration newConfiguration) throws Exception{ + String pattern = makePattern(ulCfgLocation, newConfiguration); + + File[] files = collectChunks(ulCfgLocation, oldConfiguration); + for (int i = 0; i < files.length; i++){ + String fileName = String.format(pattern, i); + Files.move(files[i].toPath(), Paths.get(fileName), StandardCopyOption.REPLACE_EXISTING); + } + } + + public static void removeChunks(String ulCfgLocation, UlConfiguration configuration) throws Exception{ + File[] files = collectChunks(ulCfgLocation, configuration); + + for (File file : files) { + Files.deleteIfExists(file.toPath()); + } + } + + public static void writeUlCfgFile(String ulCfgLocation, List fileContent) throws Exception{ + String ulCfg = ulCfgLocation + File.separator + "ul.cfg"; + + try (BufferedOutputStream stream = new BufferedOutputStream(Files.newOutputStream(Paths.get(ulCfg)))){ + for (UlConfiguration configuration: fileContent){ + stream.write(configuration.generateUlConfig()); + } + } + } +} diff --git a/src/main/resources/locale.properties b/src/main/resources/locale.properties index 40aac92..e0dc53c 100644 --- a/src/main/resources/locale.properties +++ b/src/main/resources/locale.properties @@ -30,8 +30,11 @@ ulManagerWindow_ColumnNamePublisherTitle=Publisher title ulManagerWindow_ColumnNameChunksCount=Chunks count ulManagerWindow_ColumnCdDvdFlag=CD/DVD ulManagerWindow_Row_RemoveRowBtn=Remove -CD=CD -DVD=DVD ulManagerWindow_SaveBtn=Save changes ulManagerWindow_SelectUlCfgBtn=Select ul.cfg ulManagerWindow_EmptyOrIncorrectText=Empty or incorrect ul.cfg size +ulManagerWindow_InconsistentFileText=One or more chunks not found. It is recommended to delete this entry. +Ok=Ok +CD=CD +DVD=DVD +ulManagerWindow_SaveChangesFailureText=Save changes failure: diff --git a/src/main/resources/locale_ru_RU.properties b/src/main/resources/locale_ru_RU.properties index eb99e6f..3fa85af 100644 --- a/src/main/resources/locale_ru_RU.properties +++ b/src/main/resources/locale_ru_RU.properties @@ -30,8 +30,11 @@ ulManagerWindow_ColumnNamePublisherTitle=\u0418\u043C\u044F \u043E\u0442 \u0438\ ulManagerWindow_ColumnNameChunksCount=\u0412\u0441\u0435\u0433\u043E \u0447\u0430\u0441\u0442\u0435\u0439 ulManagerWindow_ColumnCdDvdFlag=CD/DVD ulManagerWindow_Row_RemoveRowBtn=\u0423\u0434\u0430\u043B\u0438\u0442\u044C -CD=CD -DVD=DVD ulManagerWindow_SaveBtn=\u0421\u043E\u0445\u0440\u0430\u043D\u0438\u0442\u044C \u0438\u0437\u043C\u0435\u043D\u0435\u043D\u0438\u044F ulManagerWindow_SelectUlCfgBtn=\u0412\u044B\u0431\u0440\u0430\u0442\u044C ul.cfg ulManagerWindow_EmptyOrIncorrectText=\u041F\u0443\u0441\u0442\u043E\u0439 \u0438\u043B\u0438 \u043D\u0435\u043A\u043E\u0440\u0440\u0435\u043A\u0442\u043D\u044B\u0439 \u0440\u0430\u0437\u043C\u0435\u0440 \u0444\u0430\u0439\u043B\u0430 ul.cfg +ulManagerWindow_InconsistentFileText=\u041E\u0434\u043D\u0430 \u0438\u043B\u0438 \u043D\u0435\u0441\u043A\u043E\u043B\u044C\u043A\u043E \u0447\u0430\u0441\u0442\u0435\u0439 \u0444\u0430\u0439\u043B\u0430 \u043D\u0430 \u043D\u0430\u0439\u0434\u0435\u043D\u044B. \u0420\u0435\u043A\u043E\u043C\u0435\u043D\u0434\u0443\u0435\u0442\u0441\u044F \u0443\u0434\u0430\u043B\u0438\u0442\u044C \u044D\u0442\u0443 \u0437\u0430\u043F\u0438\u0441\u044C. +Ok=\u041E\u043A +CD=CD +DVD=DVD +ulManagerWindow_SaveChangesFailureText=\u041D\u0435\u0432\u043E\u0437\u043C\u043E\u0436\u043D\u043E \u0441\u043E\u0445\u0440\u0430\u043D\u0438\u0442\u044C \u0438\u0437\u043C\u0435\u043D\u0435\u043D\u0438\u044F: