Skip to content

Commit 20b4c53

Browse files
committed
Include profile when making a mod
- Since the profile may now specify how mesh data needs to be packed into vertex buffers, it will be needed in the mod file - The profile is optional since many older mods won't have it and don't need it (but newer mods may require it) - Change SnapshotProfile format into something more easily deserialized without a lot of manual code - Same for SnapMeta
1 parent 546af25 commit 20b4c53

File tree

7 files changed

+73
-44
lines changed

7 files changed

+73
-44
lines changed

MMLaunch/ModUtil.fs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module ModUtil =
3939
Ref: string
4040
ModType: string // subtype of the mod (gpureplacement, etc)
4141
MeshPath: string
42+
Profile: ModelMod.SnapshotProfile.Profile
4243
}
4344

4445
let getOutputPath modRoot modName = Path.GetFullPath(Path.Combine(modRoot, modName))
@@ -199,6 +200,17 @@ mods:"""
199200
sr.Serialize(sw, refObj)
200201
refYamlFile
201202

203+
// look for a snapshot meta file which may contain information about what profile was used
204+
let metaFile = Path.Combine(snapSrcDir, srcBasename + "_Meta.yaml")
205+
let snapProfile =
206+
if File.Exists(metaFile) then
207+
let sd = new Deserializer()
208+
use f = File.OpenText(metaFile);
209+
let p = sd.Deserialize<ModelMod.Snapshot.SnapMeta>(f)
210+
p.Profile
211+
else
212+
ModelMod.SnapshotProfile.EmptyProfile
213+
202214
// generate mod yaml
203215
let modYamlFile =
204216
let modYamlFile = Path.Combine(modOutDir, modBasename + ".yaml")
@@ -207,6 +219,7 @@ mods:"""
207219
ModType = "GPUReplacement"
208220
Ref = refBasename
209221
MeshPath = Path.GetFileName(modMMObjFile)
222+
Profile = snapProfile
210223
}
211224
let sr = new Serializer()
212225
use sw = new StreamWriter(modYamlFile)

MMManaged/MeshTransform.fs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,47 +169,47 @@ module MeshTransform =
169169
/// to change the order in which they are applied (see buildInvocation,buildReverseInvocation).
170170
/// This function operates on spatial mesh data (positions and normals).
171171
// NOTE: this implementation has some good points, but I'm sure it could be done more simply.
172-
let private applyMeshTransformsInternal (xforms:string list) (invokeBuilder) (mesh:Mesh) =
173-
if xforms.IsEmpty then
172+
let private applyMeshTransformsInternal (xforms:string array) (invokeBuilder) (mesh:Mesh) =
173+
if xforms.Length = 0 then
174174
mesh
175175
else
176176
// helper function to allow the mesh to be passed to parseXformFunc
177177
let getMeshXformFunc xname = parseVec3XformFunc false mesh xname
178178

179179
// convert all the names (and arguments) into functions
180-
let funcs = xforms |> List.map getMeshXformFunc
180+
let funcs = xforms |> Array.map getMeshXformFunc
181181
// reduce to apply the functions in order with a single function
182182
let compositeTransform = funcs |> Seq.map invokeBuilder |> Seq.reduce (>>)
183183

184184
let mesh = MeshUtil.applyPositionTransformation compositeTransform mesh
185185

186186
// also need to do normals
187187
let getMeshXformFunc xname = parseVec3XformFunc true mesh xname
188-
let funcs = xforms |> List.map getMeshXformFunc
188+
let funcs = xforms |> Array.map getMeshXformFunc
189189
let compositeTransform = funcs |> Seq.map invokeBuilder |> Seq.reduce (>>)
190190
let mesh = MeshUtil.applyNormalTransformation compositeTransform mesh
191191
mesh
192192

193193
/// As for applyMeshTransformsInternal, but for uv coordinates.
194-
let private applyUVTransformsInternal (uv_xforms:string list) (invokeBuilder) (mesh:Mesh) =
195-
if uv_xforms.IsEmpty then
194+
let private applyUVTransformsInternal (uv_xforms:string []) (invokeBuilder) (mesh:Mesh) =
195+
if uv_xforms.Length = 0 then
196196
mesh
197197
else
198198
let getMeshXformFunc xname = parseVec2XformFunc false mesh xname
199-
let funcs = uv_xforms |> List.map getMeshXformFunc
199+
let funcs = uv_xforms |> Array.map getMeshXformFunc
200200
let compositeTransform = funcs |> Seq.map invokeBuilder |> Seq.reduce (>>)
201201

202202
let mesh = MeshUtil.applyUVTransformation compositeTransform mesh
203203
mesh
204204

205205
/// Apply the specified list of transforms to the mesh.
206-
let applyMeshTransforms (xforms:string list) (uvXforms:string list) (mesh:Mesh) =
206+
let applyMeshTransforms (xforms:string []) (uvXforms:string []) (mesh:Mesh) =
207207
let mesh = applyMeshTransformsInternal xforms buildInvocation mesh
208208
let mesh = applyUVTransformsInternal uvXforms buildInvocation mesh
209209
mesh
210210

211211
/// Reverse each supplied transform, then apply them to the mesh.
212-
let reverseMeshTransforms (xforms:string list) (uvXforms:string list) (mesh:Mesh) =
213-
let mesh = applyMeshTransformsInternal (List.rev xforms) buildReverseInvocation mesh
214-
let mesh = applyUVTransformsInternal (List.rev uvXforms) buildReverseInvocation mesh
212+
let reverseMeshTransforms (xforms:string []) (uvXforms:string []) (mesh:Mesh) =
213+
let mesh = applyMeshTransformsInternal (Array.rev xforms) buildReverseInvocation mesh
214+
let mesh = applyUVTransformsInternal (Array.rev uvXforms) buildReverseInvocation mesh
215215
mesh

MMManaged/ModDB.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ module ModDB =
9696
let mesh = MeshUtil.readFrom(path, modType, flags)
9797
if flags.ReverseTransform &&
9898
(mesh.AppliedPositionTransforms.Length > 0 || mesh.AppliedUVTransforms.Length > 0) then
99-
let mesh = MeshTransform.reverseMeshTransforms (List.ofArray mesh.AppliedPositionTransforms) (List.ofArray mesh.AppliedUVTransforms) mesh
99+
let mesh = MeshTransform.reverseMeshTransforms (mesh.AppliedPositionTransforms) (mesh.AppliedUVTransforms) mesh
100100
// clear out applied transforms, since they have been reversed.
101101
{ mesh with AppliedPositionTransforms = [||]; AppliedUVTransforms = [||] }
102102
else

MMManaged/Snapshot.fs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,18 @@ module Snapshot =
118118
BlendWeight: float32 * float32 * float32 * float32 -> unit
119119
}
120120

121-
type SnapMeta(profile:SnapshotProfile.Profile) =
122-
member x.Profile with get() = profile
123-
member x.Context with get() = CoreState.Context
121+
type SnapMeta() =
122+
let mutable profile:SnapshotProfile.Profile = SnapshotProfile.EmptyProfile
123+
let mutable context:string = ""
124+
125+
static member Create(profile):SnapMeta =
126+
let p = SnapMeta()
127+
p.Profile <- profile
128+
p.Context <- CoreState.Context
129+
p
130+
131+
member x.Profile with get() = profile and set v = profile <- v
132+
member x.Context with get() = context and set v = context <- v
124133

125134
/// Reads a vertex element. Uses the read output functions to pipe the data to an appropriate handler
126135
/// function, depending on the type.
@@ -795,8 +804,8 @@ module Snapshot =
795804
BlendWeights = blendWeights.ToArray()
796805
Declaration = None
797806
BinaryVertexData = None
798-
AppliedPositionTransforms = Array.ofList appliedPosTransforms
799-
AppliedUVTransforms = Array.ofList appliedUVTransforms
807+
AppliedPositionTransforms = appliedPosTransforms.ToArray()
808+
AppliedUVTransforms = appliedUVTransforms.ToArray()
800809
Tex0Path = texName 0
801810
Tex1Path = texName 1
802811
Tex2Path = texName 2
@@ -806,7 +815,7 @@ module Snapshot =
806815
}
807816

808817
// apply tranforms
809-
let mesh = MeshTransform.applyMeshTransforms appliedPosTransforms appliedUVTransforms mesh
818+
let mesh = MeshTransform.applyMeshTransforms (appliedPosTransforms.ToArray()) (appliedUVTransforms.ToArray()) mesh
810819

811820
// write mesh
812821
let meshfile = sprintf "%s.mmobj" sbasename
@@ -858,7 +867,7 @@ module Snapshot =
858867
let metaFile = Path.Combine(baseDir, (sprintf "%s_Meta.yaml" sbasename))
859868
let serializer = Serializer()
860869
let sw = new StringWriter();
861-
serializer.Serialize(sw, SnapMeta(snapProfile))
870+
serializer.Serialize(sw, SnapMeta.Create(snapProfile))
862871
File.WriteAllText(metaFile, (sw.ToString()))
863872

864873
log.Info "Wrote snapshot %d to %s" snapshotNum.Value baseDir

MMManaged/SnapshotProfile.fs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,35 @@ open System.IO
1717
module SnapshotProfile =
1818
let private log = Logging.getLogger("SnapshotProfile")
1919

20-
type Profile(name,posX,uvX,flipTangent,vecEncoding:string) =
21-
let vecEncoding = vecEncoding.Trim()
22-
let isPacked = vecEncoding.ToLowerInvariant() = "packed"
23-
let isOcta = vecEncoding.ToLowerInvariant() = "octa"
24-
25-
do
26-
if vecEncoding = "" && not isPacked && not isOcta then
27-
log.Warn "Illegal vector encoding for specified profile %A: %A" name vecEncoding
28-
29-
member x.Name with get() = name
30-
member x.PosXForm with get() = posX
31-
member x.UVXForm with get() = uvX
32-
member x.FlipTang with get() = flipTangent
33-
member x.VecEncoding with get() = vecEncoding
34-
35-
member x.IsPackedVec() = isPacked
36-
member x.IsOctaVec() = isOcta
20+
type Profile() =
21+
let mutable name:string = "";
22+
let mutable posX:ResizeArray<String> = new ResizeArray<string>();
23+
let mutable uvX:ResizeArray<String> = new ResizeArray<string>();
24+
let mutable flipTangent:bool = false;
25+
let mutable vecEncoding:string = "";
26+
27+
static member Create(name,posX,uvX,flipTangent,vecEncoding:string):Profile =
28+
let p = new Profile()
29+
p.Name <- name
30+
p.PosXForm <- posX
31+
p.UVXForm <- uvX
32+
p.FlipTang <- flipTangent
33+
p.VecEncoding <- vecEncoding
34+
p
35+
36+
member x.Name with get() = name and set v = name <- v
37+
member x.PosXForm with get() = posX and set v = posX <- v
38+
member x.UVXForm with get() = uvX and set v = uvX <- v
39+
member x.FlipTang with get() = flipTangent and set v = flipTangent <- v
40+
member x.VecEncoding with get() = vecEncoding and set v = vecEncoding <- v
41+
42+
member x.IsPackedVec() = vecEncoding.Trim().ToLowerInvariant() = "packed"
43+
member x.IsOctaVec() = vecEncoding.Trim().ToLowerInvariant() = "octa"
3744

3845
override x.ToString() =
3946
sprintf "[SnapshotProfile: %s; pos: %A; uv: %A, fliptangent: %A, vecencoding: %A]" name posX uvX flipTangent vecEncoding
4047

41-
let EmptyProfile = Profile("",[],[],false,"")
48+
let EmptyProfile = Profile.Create("",new ResizeArray<string>(),new ResizeArray<string>(),false,"")
4249

4350
/// This profile should always exist in SnapshotProfiles.yaml.
4451
/// If it does not, new game profiles will be created with an empty snapshot profile (not an error, but not desirable either)
@@ -62,13 +69,13 @@ module SnapshotProfile =
6269
let pname = p.Key |> Yaml.toString
6370
let pvals = p.Value |> Yaml.toMapping "expected a mapping"
6471

65-
let getStrList def key =
72+
let getStrArray def key =
6673
pvals
6774
|> Yaml.getOptionalValue key
6875
|> Yaml.toOptionalSequence
6976
|> function
7077
| None -> def
71-
| Some s -> s |> Seq.map Yaml.toString |> List.ofSeq
78+
| Some s -> s |> Seq.map Yaml.toString |> Array.ofSeq |> fun a -> new ResizeArray<string>(a)
7279

7380
let getBool def key =
7481
pvals
@@ -87,12 +94,12 @@ module SnapshotProfile =
8794
| Some s -> s
8895

8996

90-
let posX = getStrList [] "pos"
91-
let uvX = getStrList [] "uv"
97+
let posX = getStrArray (new ResizeArray<string>()) "pos"
98+
let uvX = getStrArray (new ResizeArray<string>()) "uv"
9299
let flipTang = getBool false "flipTangent"
93100
let vecEncoding = getString "" "vecEncoding"
94101

95-
profiles.Add(pname,Profile(pname,posX,uvX,flipTang,vecEncoding))
102+
profiles.Add(pname,Profile.Create(pname,posX,uvX,flipTang,vecEncoding))
96103
)
97104
profiles.ToArray() |> Map.ofArray
98105

MMView/MMView.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<RootNamespace>MeshView</RootNamespace>
1111
<AssemblyName>MeshView</AssemblyName>
1212
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13-
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1414
<TargetFSharpCoreVersion>4.6.0.0</TargetFSharpCoreVersion>
1515
<Name>MMView</Name>
1616
</PropertyGroup>

Test.MMManaged/TestMeshTransform.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let ``MeshTransform: basic rotation``() =
2323
[<Test>]
2424
let ``MeshTransform: go ape with the monolith``() =
2525
// this test is too big, but it does exercise the majority of the transforming code, at least
26-
let nm = monolith |> MeshTransform.applyMeshTransforms ["rot x 90"; "rot z 45"; "scale 0.5"] []
26+
let nm = monolith |> MeshTransform.applyMeshTransforms [|"rot x 90"; "rot z 45"; "scale 0.5"|] [||]
2727

2828
// for visual debugging:
2929
//MeshUtil.WriteObj nm (Path.Combine(Util.TestDataDir, "monolith.OUT.mmobj"))

0 commit comments

Comments
 (0)