-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.cs
More file actions
134 lines (130 loc) · 4.13 KB
/
Convert.cs
File metadata and controls
134 lines (130 loc) · 4.13 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
namespace bmdl;
using System.Reflection;
public class Convert {
[AttributeUsage(AttributeTargets.Class)]
public class AssociationAttribute(string[] extensions) : Attribute {
public HashSet<string> Types = [.. extensions];
}
public class Source {
public virtual Model Read(string path) {return null;}
public virtual bool CanRead(string path) => true;
}
public static bool Supports(string file) {
foreach (var type in Assembly.GetAssembly(typeof(Source)).GetTypes()) {
if (!type.IsAssignableTo(typeof(Source)))
continue;
var assoc = type.GetCustomAttribute<AssociationAttribute>();
if (assoc is null)
continue;
if (!assoc.Types.Contains(file.Split('.').Last()))
continue;
var inst = (Source)type.GetConstructor([]).Invoke([]);
if (!inst.CanRead(file))
continue;
return true;
}
return false;
}
public static void Main(string[] args) => Run(args);
public static string Run(string[] args) {
List<string> argacc = [.. args];
Console.WriteLine("-- bmdl converter utility --");
if (argacc.Count == 0) {
Console.WriteLine("cmd args");
Console.WriteLine(" <input file>");
}
while (!File.Exists(argacc.ElementAtOrDefault(0))) {
if (argacc.Count > 0) {
Console.WriteLine($"{argacc[0]} is not a valid file");
argacc.RemoveAt(0);
}
Console.Write("input file: ");
argacc.Insert(0, Console.ReadLine().Trim());
}
return Write(argacc[0]);
}
public static string Write(string input) {
Source inst = null;
foreach (var type in Assembly.GetAssembly(typeof(Source)).GetTypes()) {
if (!type.IsAssignableTo(typeof(Source)))
continue;
var assoc = type.GetCustomAttribute<AssociationAttribute>();
if (assoc is null)
continue;
if (!assoc.Types.Contains(input.Split('.').Last()))
continue;
Console.WriteLine($"using {type} converter");
inst = (Source)type.GetConstructor([]).Invoke([]);
break;
}
if (inst is null) {
Console.WriteLine($"could not find converter source for '{input.Split('.').Last()}'");
return null;
}
var path = $"{input.Split('.').First()}.bmdl";
if (File.Exists(path))
File.Delete(path);
using var f = new BinaryWriter(File.OpenWrite(path));
Write(f, inst.Read(input));
return path;
}
private static void Write(BinaryWriter f, Model m) {
f.Write("bmdl".ToArray());
byte flags = 0;
if (m.Skeleton.Length > 0)
flags |= (byte)Model.Flags.Skeleton;
foreach (var mesh in m.Meshes) {
if ((flags & (byte)Model.Flags.BigIndicies) == 0 && mesh.Indices.Any((i) => i > ushort.MaxValue))
flags |= (byte)Model.Flags.BigIndicies;
if ((flags & (byte)Model.Flags.Color) == 0 && mesh.Vertices.Any((v) => v.Color != int.MaxValue))
flags |= (byte)Model.Flags.Color;
if ((flags & (byte)Model.Flags.Weights) == 0 && (flags & (byte)Model.Flags.Skeleton) != 0) {
foreach (var vertex in mesh.Vertices) {
if (vertex.Bones.All((b) => b == 0) && vertex.Weights.All((w) => w == 0))
continue;
flags |= (byte)Model.Flags.Weights;
break;
}
}
}
f.Write(flags);
if ((flags & (byte)Model.Flags.Skeleton) != 0) {
//TODO write skeleton
}
f.Write((byte)m.Meshes.Length);
foreach (var mesh in m.Meshes) {
f.Write(mesh.Name);
f.Write(mesh.Material);
f.Write(mesh.Indices.Length);
foreach (var index in mesh.Indices) {
if ((flags & (byte)Model.Flags.BigIndicies) != 0)
f.Write(index);
else
f.Write((ushort)index);
}
f.Write(mesh.Vertices.Length);
foreach (var vertex in mesh.Vertices) {
f.Write((Half)vertex.Position.X);
f.Write((Half)vertex.Position.Y);
f.Write((Half)vertex.Position.Z);
f.Write((Half)vertex.Normal.X);
f.Write((Half)vertex.Normal.Y);
f.Write((Half)vertex.Normal.Z);
f.Write((Half)vertex.TexCoord0.X);
f.Write((Half)vertex.TexCoord0.Y);
if ((flags & (byte)Model.Flags.Color) != 0)
f.Write(vertex.Color);
if ((flags & (byte)Model.Flags.Weights) != 0) {
f.Write((byte)vertex.Bones.Length);
foreach (var bone in vertex.Bones)
f.Write(bone);
foreach (var weight in vertex.Weights)
f.Write((Half)weight);
}
}
if ((flags & (byte)Model.Flags.Morphs) != 0) {
//TODO write morphs
}
}
}
}