Skip to content

Commit

Permalink
Impl restbl save as yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchLeaders committed Jun 3, 2023
1 parent 44356c4 commit 96e34f6
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/ExKingEditor/ViewModels/Editors/RestblViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,42 @@ public RestblViewModel(string file, byte[] data, Action<byte[]>? setSource = nul
}

Yaml = _yaml = yaml.ToString();
SupportedExtensions.Add("Yaml:*.yml;*.yaml|");
}

public override void SaveAs(string path)
{
HashSet<uint> lookup = new();

Restbl restbl = new();
foreach ((var nameStr, var sizeStr) in Yaml.Split('\n')[..(^1)].Select(x => x.Split(": ")).Select(x => (name: x[0], size: x[1])).OrderBy(x => x.name)) {
uint size = Convert.ToUInt32(sizeStr, 10);
uint hash = nameStr.StartsWith("0x") ? Convert.ToUInt32(nameStr.Remove(0, 2), 16) : Crc32.Compute(nameStr);
if (lookup.Contains(hash)) {
restbl.NameTable.Add(new(nameStr, size));
}
else {
lookup.Add(hash);
restbl.CrcTable.Add(new(hash, size));
}
}

Span<byte> data = (_compressed = path.EndsWith(".zs")) ? TotkZstd.Compress(path, restbl.ToBinary()) : restbl.ToBinary();
string ext = Path.GetExtension(path);

if (path == _file) {
WriteToSource(data);
if (ext is ".yaml" or ".yml") {
File.WriteAllText(path, Yaml);
}
else {
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
using FileStream fs = File.Create(path);
fs.Write(data);
HashSet<uint> lookup = new();

Restbl restbl = new();
foreach ((var nameStr, var sizeStr) in Yaml.Split('\n')[..(^1)].Select(x => x.Split(": ")).Select(x => (name: x[0], size: x[1])).OrderBy(x => x.name)) {
uint size = Convert.ToUInt32(sizeStr, 10);
uint hash = nameStr.StartsWith("0x") ? Convert.ToUInt32(nameStr.Remove(0, 2), 16) : Crc32.Compute(nameStr);
if (lookup.Contains(hash)) {
restbl.NameTable.Add(new(nameStr, size));
}
else {
lookup.Add(hash);
restbl.CrcTable.Add(new(hash, size));
}
}

Span<byte> data = (_compressed = path.EndsWith(".zs")) ? TotkZstd.Compress(path, restbl.ToBinary()) : restbl.ToBinary();

if (path == _file) {
WriteToSource(data);
}
else {
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
using FileStream fs = File.Create(path);
fs.Write(data);
}
}

_yaml = Yaml;
Expand Down

0 comments on commit 96e34f6

Please sign in to comment.