-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCsdProject.cs
More file actions
111 lines (97 loc) · 3.32 KB
/
CsdProject.cs
File metadata and controls
111 lines (97 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
namespace SharpNeedle.Framework.Ninja.Csd;
[NeedleResource("nn/csd-project", @"\.[gsxy]ncp$")]
public class CsdProject : ResourceBase, IBinarySerializable
{
public CsdPackage? Package { get; set; }
public ProjectChunk? Project { get; set; }
public ITextureList? Textures { get; set; }
public Endianness Endianness { get; set; }
public override void Read(IFile file)
{
Name = file.Name;
BaseFile = file;
using BinaryObjectReader reader = new(file.Open(), StreamOwnership.Transfer, Endianness.Little);
Read(reader);
}
public override void Write(IFile file)
{
Name = file.Name;
BaseFile = file;
using BinaryObjectWriter writer = new(file.Open(FileAccess.Write), StreamOwnership.Transfer, Endianness);
Write(writer);
}
public void Read(BinaryObjectReader reader)
{
Package = reader.ReadObject<CsdPackage>();
Endianness = Package.Endianness;
ChunkBinaryOptions options = new();
for (int i = 0; i < Package.Files.Count; i++)
{
if (Package.Files[i].Length == 0)
continue;
Stream stream = Package.GetStream(i);
using BinaryObjectReader infoReader = new(stream, StreamOwnership.Transfer, Package.Endianness);
infoReader.OffsetBinaryFormat = reader.OffsetBinaryFormat;
try
{
InfoChunk info = infoReader.ReadObject<InfoChunk, ChunkBinaryOptions>(options);
foreach (IChunk chunk in info.Chunks)
{
switch (chunk)
{
case ProjectChunk project:
Project = project;
options.TextureFormat = project.TextureFormat;
break;
case ITextureList tl:
Textures = tl;
break;
}
}
}
catch (Exception e)
{
if (e is not InvalidDataException)
{
throw;
}
}
}
}
public void Write(BinaryObjectWriter writer)
{
if (Project == null)
{
throw new InvalidOperationException("Project is null!");
}
Package = new CsdPackage
{
Endianness = Endianness
};
if (Textures is TextureListMirage)
{
Project.TextureFormat = TextureFormat.Mirage;
}
else if (Textures is TextureListNN)
{
Project.TextureFormat = TextureFormat.NextNinja;
}
CreatePackageFile(Project);
if (Textures != null)
{
CreatePackageFile(Textures);
}
writer.WriteObject(Package);
void CreatePackageFile(IChunk chunk)
{
using Stream stream = Package.GetStream(Package.Add(), true, true);
using BinaryObjectWriter infoWriter = new(stream, StreamOwnership.Retain, Endianness);
InfoChunk info = new()
{
Signature = BinaryHelper.MakeSignature<uint>(Endianness == Endianness.Little ? "NXIF" : "NYIF"),
};
info.Chunks.Add(chunk);
infoWriter.WriteObject(info);
}
}
}