Files
2026-03-08 22:38:26 +01:00

460 lines
15 KiB
C#

using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;
using System.Diagnostics;
namespace ARC_Explorer_LCE
{
public partial class Form1 : Form
{
public string filepath = "";
public Point Cursor;
private List<FileListView> FLV = new List<FileListView>();
private ListViewColumnSorter lvcs;
private FolderBrowserDialog fbd;
public Form1()
{
InitializeComponent();
textBox1.Show();
label5.Show();
button1.Show();
listView1.Hide();
panel8.Hide();
button6.Hide();
button7.Hide();
lvcs = new ListViewColumnSorter();
this.listView1.ListViewItemSorter = lvcs;
}
// Makes it so you can actually move the window
private void mouse_Down(object sender, MouseEventArgs e)
{
Cursor = new Point(-e.X, -e.Y);
}
private void mouse_Move(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point mousecursor = Control.MousePosition;
mousecursor.Offset(Cursor.X, Cursor.Y);
Location = mousecursor;
}
}
private void label1_Click(object sender, EventArgs e)
{
CleanUp();
}
private void CleanUp()
{
if (File.Exists("arc.zip"))
{
File.Delete("arc.zip");
}
if (Directory.Exists("Temporary Edit Directory"))
{
Directory.Delete("Temporary Edit Directory", true);
}
Environment.Exit(1);
}
private void extractMenuButton_Click(object sender, EventArgs e)
{
label3.Text = "EXTRACTION";
textBox1.Show();
label5.Show();
button1.Show();
button2.Show();
listView1.Hide();
panel8.Hide();
button6.Hide();
button7.Hide();
}
private void ViewMenuButton_Click(object sender, EventArgs e)
{
label3.Text = "VIEW";
textBox1.Hide();
label5.Hide();
button1.Hide();
button2.Hide();
listView1.Show();
panel8.Show();
button6.Show();
button7.Hide();
}
private void ExportMenuButton_Click(object sender, EventArgs e)
{
label3.Text = "EXPORT";
textBox1.Hide();
label5.Hide();
button1.Hide();
button2.Hide();
listView1.Hide();
panel8.Hide();
button6.Hide();
button7.Show();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = ".arc files (*.arc)|*.arc";
ofd.FilterIndex = 1;
if (ofd.ShowDialog() == DialogResult.OK)
{
filepath = ofd.FileName;
textBox1.Text = filepath;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (filepath != "")
{
if (Path.GetExtension(filepath) == ".arc")
{
if (File.Exists(filepath))
{
extractFiles(filepath);
}
else
{
MessageBox.Show("File doesn't exist", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("You can only choose .arc files", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("You need to choose a file first!", "No File Found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
filepath = textBox1.Text;
}
private async Task extractFiles(string path)
{
await arctozip(path);
if (File.Exists("arc.zip"))
{
await extractZipToDirectory();
if (File.Exists("arc.zip"))
{
File.Delete("arc.zip");
}
if (Directory.Exists("Temporary Edit Directory"))
{
foreach (var file in Directory.GetFiles("Temporary Edit Directory"))
{
int length = (int)new FileInfo(file).Length;
string name = new FileInfo(file).Name;
string extension = new FileInfo(file).Extension;
string fullPathToFile = file;
FLV.Add(new FileListView(name, extension, length, fullPathToFile));
}
await updateListView(FLV);
MessageBox.Show("Files are ready for editing.", "Extracting finished!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private async Task updateListView(IEnumerable<FileListView> list)
{
listView1.Items.Clear();
int i = 0;
foreach (FileListView flv in list)
{
listView1.Items.Add(flv.Name);
listView1.Items[i].SubItems.Add(flv.Type);
listView1.Items[i].SubItems.Add(flv.Size.ToString());
listView1.Items[i].SubItems.Add(flv.fullPathtoFile);
i++;
}
listView1.ItemActivate += new EventHandler(listView1_ItemActivate);
}
private void listView1_ItemActivate(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 1)
{
ListViewItem selectedItem = listView1.SelectedItems[0];
if (File.Exists(listView1.Items[selectedItem.Index].SubItems[3].Text))
{
System.Diagnostics.Process.Start("explorer.exe", string.Format("/select,\"{0}\"", listView1.Items[selectedItem.Index].SubItems[3].Text));
return;
}
}
}
private async Task extractZipToDirectory()
{
using var zipStream = new FileStream("arc.zip", FileMode.Open);
using var archive = new ZipArchive(zipStream, ZipArchiveMode.Read);
archive.ExtractToDirectory("Temporary Edit Directory");
}
private async Task arctozip(string path)
{
// Translation of NessieHax's Script (using Claude AI sadly.)
// forgive me for my sins of using Ai; i don't understand python
byte[] data = File.ReadAllBytes(path);
int offset = 0;
int entryCount = BinaryPrimitives.ReadInt32BigEndian(data);
offset += 4;
using var zipStream = new FileStream("arc.zip", FileMode.Create);
using var archive = new ZipArchive(zipStream, ZipArchiveMode.Create);
for (int i = 0; i < entryCount; i++)
{
short strLen = BinaryPrimitives.ReadInt16BigEndian(data.AsSpan(offset));
offset += 2;
string filePath = Encoding.UTF8.GetString(data, offset, strLen).TrimEnd('\0');
offset += strLen;
int fileOffset = BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(offset));
offset += 4;
int fileSize = BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(offset));
offset += 4;
// Create parent directories in zip if needed
if (filePath.Contains('/') || filePath.Contains('\\'))
{
string normalizedPath = filePath.Replace('\\', '/');
var entry = archive.CreateEntry(normalizedPath);
using var entryStream = entry.Open();
entryStream.Write(data, fileOffset, fileSize);
}
else
{
var entry = archive.CreateEntry(filePath);
using var entryStream = entry.Open();
entryStream.Write(data, fileOffset, fileSize);
}
}
}
private async Task ziptoarc(string path) {
using var zipFile = ZipFile.OpenRead("arc.zip");
var entries = new List<ZipArchiveEntry>(zipFile.Entries);
int entryCount = entries.Count;
int fileOffset = 4; // start after the entry count int
foreach (var entry in entries)
{
int strlen = Encoding.UTF8.GetByteCount(entry.FullName);
fileOffset += 2 + strlen + 4 + 4; // short + name bytes + 2 ints
}
using var ms = new MemoryStream();
using var bw = new BinaryWriter(ms);
// Write entry count as big-endian int
WriteBigEndianInt32(bw, entryCount);
// Write header entries
int currentOffset = fileOffset;
foreach (var entry in entries)
{
string name = entry.FullName.Replace("/", "\\");
byte[] nameBytes = Encoding.UTF8.GetBytes(name);
short strlen = (short)nameBytes.Length;
long fileSize = entry.Length;
WriteBigEndianInt16(bw, strlen);
bw.Write(nameBytes);
WriteBigEndianInt32(bw, currentOffset);
WriteBigEndianInt32(bw, (int)fileSize);
currentOffset += (int)fileSize;
}
// Write file data
foreach (var entry in entries)
{
using var entryStream = entry.Open();
entryStream.CopyTo(ms);
}
File.WriteAllBytes(path, ms.ToArray());
}
static void WriteBigEndianInt32(BinaryWriter bw, int value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
bw.Write(bytes);
}
static void WriteBigEndianInt16(BinaryWriter bw, short value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
bw.Write(bytes);
}
private void button3_Click(object sender, EventArgs e)
{
// Determine if clicked column is already the column that is being sorted.
if (0 == lvcs.SortColumn)
{
// Reverse the current sort direction for this column.
if (lvcs.Order == SortOrder.Ascending)
{
lvcs.Order = SortOrder.Descending;
}
else
{
lvcs.Order = SortOrder.Ascending;
}
}
else
{
// Set the column number that is to be sorted; default to ascending.
lvcs.SortColumn = 0;
lvcs.Order = SortOrder.Ascending;
}
// Perform the sort with these new sort options.
this.listView1.Sort();
}
private void button4_Click(object sender, EventArgs e)
{
// Determine if clicked column is already the column that is being sorted.
if (1 == lvcs.SortColumn)
{
// Reverse the current sort direction for this column.
if (lvcs.Order == SortOrder.Ascending)
{
lvcs.Order = SortOrder.Descending;
}
else
{
lvcs.Order = SortOrder.Ascending;
}
}
else
{
// Set the column number that is to be sorted; default to ascending.
lvcs.SortColumn = 1;
lvcs.Order = SortOrder.Ascending;
}
// Perform the sort with these new sort options.
this.listView1.Sort();
}
private void button5_Click(object sender, EventArgs e)
{
// Determine if clicked column is already the column that is being sorted.
if (2 == lvcs.SortColumn)
{
// Reverse the current sort direction for this column.
if (lvcs.Order == SortOrder.Ascending)
{
lvcs.Order = SortOrder.Descending;
}
else
{
lvcs.Order = SortOrder.Ascending;
}
}
else
{
// Set the column number that is to be sorted; default to ascending.
lvcs.SortColumn = 2;
lvcs.Order = SortOrder.Ascending;
}
// Perform the sort with these new sort options.
this.listView1.Sort();
}
private void button6_Click(object sender, EventArgs e)
{
string folderPath = Directory.GetCurrentDirectory() + "\\Temporary Edit Directory";
if (Directory.Exists(folderPath))
{
System.Diagnostics.Process.Start("explorer.exe", folderPath);
}
}
private void button7_Click(object sender, EventArgs e)
{
fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK) {
if (Directory.Exists("Temporary Edit Directory"))
{
Export();
}
else
{
MessageBox.Show("Please extract before you export!", "No valid file detected.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
private async Task Export() {
await CreateZip();
if (Directory.Exists("Temporary Edit Directory")) {
Directory.Delete("Temporary Edit Directory", true);
}
await ziptoarc(fbd.SelectedPath + "\\" + new FileInfo(filepath).Name);
if (File.Exists("arc.zip"))
{
File.Delete("arc.zip");
}
MessageBox.Show("Edits successfully exported!", "Exporting done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private async Task CreateZip() {
using var zipStream = new FileStream("arc.zip", FileMode.Create);
using var archive = new ZipArchive(zipStream, ZipArchiveMode.Create);
foreach (var file in Directory.GetFiles("Temporary Edit Directory"))
{
archive.CreateEntryFromFile(file, new FileInfo(file).Name);
}
}
}
}