Skip to content

Commit

Permalink
Revert "Add first write implementation"
Browse files Browse the repository at this point in the history
This reverts commit f262977.
  • Loading branch information
TheIndra55 committed Feb 4, 2023
1 parent 89bbc67 commit b4f7a61
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 172 deletions.
12 changes: 0 additions & 12 deletions Yura/Archive/ArchiveFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,13 @@ public ArchiveFile(string path)
/// <returns>The content of the file</returns>
public abstract byte[] Read(ArchiveRecord record);

/// <summary>
/// Writes the data to a file in the archive
/// </summary>
/// <param name="record">The file record, must be an existing record</param>
/// <param name="data">The data to write</param>
public abstract void Write(ArchiveRecord record, byte[] data);

/// <summary>
/// Gets the specialisation mask of a record
/// </summary>
/// <param name="record">The record to get the specialisation of</param>
/// <returns>The specialisation mask</returns>
public abstract uint GetSpecialisationMask(ArchiveRecord record);

/// <summary>
/// Whether the archive implementation supports writing
/// </summary>
public abstract bool CanWrite { get; }

/// <summary>
/// Gets or sets the file list
/// </summary>
Expand Down
10 changes: 1 addition & 9 deletions Yura/Archive/DefianceArchive.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using StreamReader = Yura.IO.StreamReader;

Expand Down Expand Up @@ -71,8 +70,6 @@ public override IReadOnlyList<ArchiveRecord> Records
}
}

public override bool CanWrite => false;

public override byte[] Read(ArchiveRecord record)
{
var file = record as DefianceRecord;
Expand All @@ -88,11 +85,6 @@ public override byte[] Read(ArchiveRecord record)
return bytes;
}

public override void Write(ArchiveRecord record, byte[] data)
{
throw new NotImplementedException();
}

public override uint GetSpecialisationMask(ArchiveRecord record)
{
// defiance does not use specialisation
Expand Down
7 changes: 0 additions & 7 deletions Yura/Archive/DeusExArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ public override IReadOnlyList<ArchiveRecord> Records
}
}

public override bool CanWrite => false;

public override byte[] Read(ArchiveRecord record)
{
var file = record as DeusExRecord;
Expand All @@ -108,11 +106,6 @@ public override byte[] Read(ArchiveRecord record)
return bytes;
}

public override void Write(ArchiveRecord record, byte[] data)
{
throw new NotImplementedException();
}

public override uint GetSpecialisationMask(ArchiveRecord record)
{
var file = record as DeusExRecord;
Expand Down
88 changes: 0 additions & 88 deletions Yura/Archive/LegendArchive.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using StreamReader = Yura.IO.StreamReader;

namespace Yura.Archive
Expand Down Expand Up @@ -79,8 +78,6 @@ public override IReadOnlyList<ArchiveRecord> Records
}
}

public override bool CanWrite => true;

public override byte[] Read(ArchiveRecord record)
{
var file = record as LegendRecord;
Expand Down Expand Up @@ -115,91 +112,6 @@ public override byte[] Read(ArchiveRecord record)
return bytes;
}

public override void Write(ArchiveRecord record, byte[] data)
{
var file = record as LegendRecord;

// find offset to write the file
var offset = FindFirstSpace(data.Length, file);

file.Offset = (uint)(offset >> 11);
file.Size = (uint)data.Length;

var bigfile = (file.Offset << 11) / _alignment;

// find right bigfile
var name = Path.GetFileNameWithoutExtension(_file);
var filename = Path.GetDirectoryName(_file) + Path.DirectorySeparatorChar + name + "." + bigfile.ToString("000");

// write the file data
var stream = File.OpenWrite(filename);

stream.Position = offset % _alignment;
stream.Write(data);

stream.Close();

// update file header
stream = File.OpenWrite(_file);

// find position of record in header
stream.Position = 4 + (_files.Count * 4) + (_files.IndexOf(file) * 16);

var size = BitConverter.GetBytes(file.Size);
stream.Write(size);

var bOffset = BitConverter.GetBytes(file.Offset);
stream.Write(bOffset);

stream.Close();
}

public long FindFirstSpace(int size, ArchiveRecord ignore)
{
// order records by offset
var records = _files.Where(x => x != ignore).OrderBy(x => x.Offset).ToList();

for (int i = 0; i < records.Count; i++)
{
var record = records[i];

// get first free position
var offset = (record.Offset << 11) + record.Size;

// find next aligned offset
while ((offset & 2047) != 0) offset++;

var end = offset + size;
var bigfile = (record.Offset << 11) / _alignment;

if (i >= records.Count - 1)
{
for (int j = 0; j < 10; j++)
{
if (end < (bigfile + 1) * _alignment)
{
return offset;
}

bigfile++;
offset = (uint)(bigfile * _alignment);

if (bigfile > 999) break;
}

break;
}

// check if file fits
if (end < records[i + 1].Offset << 11 && end < (bigfile + 1) * _alignment)
{
return offset;
}
}

throw new Exception("Failed to find space in bigfile");
}

public override uint GetSpecialisationMask(ArchiveRecord record)
{
var file = record as LegendRecord;
Expand Down
7 changes: 0 additions & 7 deletions Yura/Archive/TigerArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ public override IReadOnlyList<ArchiveRecord> Records
}
}

public override bool CanWrite => false;

public override byte[] Read(ArchiveRecord record)
{
var file = record as TigerRecord;
Expand All @@ -144,11 +142,6 @@ public override byte[] Read(ArchiveRecord record)
return bytes;
}

public override void Write(ArchiveRecord record, byte[] data)
{
throw new NotImplementedException();
}

public override uint GetSpecialisationMask(ArchiveRecord record)
{
var file = record as TigerRecord;
Expand Down
4 changes: 1 addition & 3 deletions Yura/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@
<GridSplitter Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" Width="5"/>

<!-- main file view -->
<ListView Name="FileView" MouseDoubleClick="FileView_MouseDoubleClick"
Drop="FileView_Drop" AllowDrop="True"
Grid.Row="2" Grid.Column="2">
<ListView Name="FileView" MouseDoubleClick="FileView_MouseDoubleClick" Grid.Row="2" Grid.Column="2">
<ListView.ContextMenu>
<ContextMenu Opened="ContextMenu_ContextMenuOpening">
<MenuItem Name="ExportBtn" Click="ExportBtn_Click" Header="Export"/>
Expand Down
46 changes: 0 additions & 46 deletions Yura/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public partial class MainWindow : Window
private TextureFormat _textureFormat;
private Game _currentGame;

private List<ArchiveRecord> _currentFolder;

// the current open bigfile
private ArchiveFile _bigfile;

Expand Down Expand Up @@ -230,8 +228,6 @@ public void SwitchDirectory(string path, string selectedFile = null)
PathBox.Text = bigfile + "\\" + path;
}

_currentFolder = files;

ShowFiles(files, selectedFile);
}

Expand Down Expand Up @@ -549,48 +545,6 @@ private void SearchBox_KeyDown(object sender, KeyEventArgs e)
// TODO clear selection in directory view
}
}

private void FileView_Drop(object sender, DragEventArgs e)
{
if (_bigfile == null || !_bigfile.CanWrite)
{
return;
}

if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);

foreach (var file in files)
{
// currently only support replacing file, so find the current file record in the folder
var filename = Path.GetFileName(file);
var record = _currentFolder.FirstOrDefault(x => x.Name != null && Path.GetFileName(x.Name) == filename);

if (record == null)
{
MessageBox.Show("Unable to add file, make sure a file exists with same name in the current folder.", "Failed to add file", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

try
{
// read file and write into archive, bigfile implementation will take care of
// finding an offset and writing it
var data = File.ReadAllBytes(file);

_bigfile.Write(record, data);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Failed to add file", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}

MessageBox.Show($"{files.Length} files added", "Files added", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}

public class DirectoryViewFolder
Expand Down

0 comments on commit b4f7a61

Please sign in to comment.