Skip to content

Commit

Permalink
Add new features and fix stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
BuilderDemo7 committed Dec 19, 2023
1 parent 0f0fe45 commit 20ff47f
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 119 deletions.
4 changes: 2 additions & 2 deletions WADExplorer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// É possível especificar todos os valores ou usar como padrão os Números de Build e da Revisão
// utilizando o "*" como mostrado abaixo:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.5")]
[assembly: AssemblyFileVersion("1.0.5")]
[assembly: AssemblyVersion("1.0.6")]
[assembly: AssemblyFileVersion("1.0.6")]
20 changes: 10 additions & 10 deletions WADExplorer/REWAD/InsideItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public class InsideItem

//[ReadOnly(true)]
//[Browsable(false)]
public int ParentId { get; set; }
public int Priority { get; set; }

public int Unk2 { get; set; }
public int Unk3 { get; set; }
public int FolderStartIndex { get; set; }
public int FolderNextItemIndex { get; set; }

[ReadOnly(true)]
[Browsable(false)]
Expand All @@ -104,9 +104,9 @@ public InsideItem(
uint totalsize,
uint size,

int parentID,
int unk2,
int unk3,
int priority,
int folderSI,
int folderNII,

byte[] buffer = null,
InsideItem parent = null
Expand All @@ -121,17 +121,17 @@ public InsideItem(
TotalSize = totalsize;
Size = size;

ParentId = parentID;
Unk2 = unk2;
Unk3 = unk3;
Priority = priority;
FolderStartIndex = folderSI;
FolderNextItemIndex = folderNII;

if (buffer != null)
Buffer = buffer;
if (parent != null)
Parent = parent;

// force this to be a folder if
if (Offset == 0 && Size == 0 && Unk2!=-1)
if (FolderStartIndex != -1)
IsFolder = true;
}
}
Expand Down
128 changes: 111 additions & 17 deletions WADExplorer/REWAD/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,18 @@ public virtual void ParseParents()
if (item.IsFolder)
{
item.Children = new List<InsideItem>();
int id = item.Unk2;
int id = item.FolderStartIndex;

if (id != -1)
{
while (id != -1)
{
item.Children.Add(Items[id]);
Items[id].Parent = item;
if (id == Items[id].Unk3)
if (id == Items[id].FolderNextItemIndex)
break;

id = Items[id].Unk3;
id = Items[id].FolderNextItemIndex;
}
}
/*
Expand All @@ -206,21 +206,26 @@ public virtual void RecastParentChainsForItem(InsideItem item, bool loopChain =
if (!item.IsFolder)
return;

item.Unk2 = item.Children[0].Index;
item.FolderStartIndex = item.Children[0].Index;

if (item.Children.Count == 0)
item.FolderStartIndex = -1;

int idx = 0;
foreach (InsideItem child in item.Children)
{
child.Priority = item.Priority;
if (idx != item.Children.Count-1)
{
child.Unk3 = item.Children[idx + 1].Index;
child.FolderNextItemIndex = item.Children[idx + 1].Index;
if (child.IsFolder & loopChain)
RecastParentChainsForItem(child, true);

idx++;
}
else
{
child.Unk3 = -1;
child.FolderNextItemIndex = -1;
break;
}
}
Expand Down Expand Up @@ -329,7 +334,7 @@ public InsideItem GetChildByPath(string path)

if (pathNames[0].Contains(":"))
{
throw new InvalidOperationException("The path cannot be a path to a disk unit");
throw new InvalidOperationException("The path cannot be a path to a hard drive");
}

InsideItem r = Items[0].FindFirstChildByName(pathNames[0].Replace("\0", ""));
Expand Down Expand Up @@ -384,7 +389,7 @@ public virtual byte[] RegenerateAndReturnBuffer(bool saveInNewFormat = true)
dataStream.SetLength(bufferSize);

// regenerate parent chain
RecastParentChainsForItem(Items[0], true);
//RecastParentChainsForItem(Items[0], true);
using (var f = new BinaryWriter(dataStream, Encoding.UTF8, false))
{
f.Write(Magic);
Expand Down Expand Up @@ -418,10 +423,10 @@ public virtual byte[] RegenerateAndReturnBuffer(bool saveInNewFormat = true)
f.Write(item.Buffer.Length);
f.Write(item.Buffer.Length);

f.Write(item.ParentId);
f.Write(item.Priority);

f.Write(item.Unk2);
f.Write(item.Unk3);
f.Write(item.FolderStartIndex);
f.Write(item.FolderNextItemIndex);

lastOffset += (int)item.Buffer.Length;
}
Expand All @@ -444,15 +449,104 @@ public virtual byte[] RegenerateAndReturnBuffer(bool saveInNewFormat = true)
return dataStream.GetBuffer();
}

public virtual void Dispose()
public virtual void DisposeStreams()
{
if (StreamPackage!=null)
StreamPackage.Dispose();
if (f!=null)
f.Dispose();
}

/// <summary>
/// Adds items from a directory.
/// </summary>
/// <param name="directory">The directory to add items as files or folders.</param>
/// <param name="relativeTo">The item where the result is added, will be attached to the root of this package if null.</param>
/// <param name="addSiblings">Specifies if it will add more items from each folder like a chain.</param>
public void AddItemsFromDirectory(string directory, InsideItem relativeTo = null, bool addSiblings = true)
{
if (!Directory.Exists(directory))
throw new InvalidOperationException("The directory does not exist");

string[] files = Directory.GetFiles(directory);
string[] directories = Directory.GetDirectories(directory);

InsideItem _r = Items[0]; // root
if (relativeTo != null)
_r = relativeTo;

// files
foreach (string entry in files)
{
InsideItem item = new InsideItem()
{
Name = Path.GetFileName(entry),

Offset = 0,
Size = 0,
Buffer = new byte[0],
Children = new List<InsideItem>()
};
FileStream file = new FileStream(entry, FileMode.Open, FileAccess.Read);
using (var f = new BinaryReader(file, Encoding.UTF8, false))
{
item.Buffer = f.ReadBytes((int)file.Length);
item.Size = (uint)item.Buffer.Length;
}

item.Index = Items.Count;
Items.Add(item);
_r.Children.Add(item);
}
// directories
foreach (string entry in directories)
{
InsideItem item = new InsideItem()
{
Name = Path.GetFileName(entry),

Offset = 0,
Size = 0,
Buffer = new byte[0],
Children = new List<InsideItem>(),
IsFolder = true
};
if (addSiblings)
AddItemsFromDirectory(entry, item, true);

item.Index = Items.Count;
Items.Add(item);
_r.Children.Add(item);
}
}

public static Package FromDirectory(string directory)
{
Package pkg = new Package(new List<InsideItem>(1) {

new InsideItem()
{
Children = new List<InsideItem>(),
Offset = 0,
Size = 0,
CRC = -1,
Buffer = new byte[0]
}

}

);

// add the items
pkg.AddItemsFromDirectory(directory, null, true);

pkg.RecastParentChainsForItem(pkg.Items[0],true);

return pkg;
}

public Package() { }
public Package(List<InsideItem> items) { Items = items; }
public Package(string filename) {
FileStream fileS = new FileStream(filename, FileMode.Open, FileAccess.Read);
Load(fileS);
Expand Down Expand Up @@ -589,7 +683,7 @@ public override byte[] RegenerateAndReturnBuffer(bool saveInNewFormat = false)
dataStream.SetLength(bufferSize);

// regenerate parent chain
RecastParentChainsForItem(Items[0], true);
//RecastParentChainsForItem(Items[0], true);
using (var f = new BinaryWriter(dataStream, Encoding.UTF8, false))
{
f.Write(Magic);
Expand Down Expand Up @@ -623,10 +717,10 @@ public override byte[] RegenerateAndReturnBuffer(bool saveInNewFormat = false)
//f.Write(item.Buffer.Length);
f.Write(item.Buffer.Length);

f.Write(item.ParentId);
f.Write(item.Priority);

f.Write(item.Unk2);
f.Write(item.Unk3);
f.Write(item.FolderStartIndex);
f.Write(item.FolderNextItemIndex);

lastOffset += (int)item.Buffer.Length;
}
Expand All @@ -650,7 +744,7 @@ public override byte[] RegenerateAndReturnBuffer(bool saveInNewFormat = false)
return dataStream.GetBuffer();
}

public override void Dispose()
public override void DisposeStreams()
{
StreamPackage.Dispose();
f.Dispose();
Expand Down
Loading

0 comments on commit 20ff47f

Please sign in to comment.