1+ using Assimp ;
2+ using McMaster . Extensions . CommandLineUtils ;
3+ using OpenKh . Bbs ;
4+ using OpenKh . Common . Utils ;
15using OpenKh . Engine . MonoGame ;
26using OpenKh . Engine . Parsers ;
37using OpenKh . Imaging ;
4-
58using System ;
6- using McMaster . Extensions . CommandLineUtils ;
7- using System . IO ;
8- using System . Reflection ;
9- using System . ComponentModel . DataAnnotations ;
10- using OpenKh . Bbs ;
119using System . Collections . Generic ;
10+ using System . ComponentModel . DataAnnotations ;
11+ using System . IO ;
1212using System . Numerics ;
13- using OpenKh . Common . Utils ;
14- using Assimp ;
13+ using System . Reflection ;
14+ using System . Runtime . InteropServices ;
1515
1616namespace OpenKh . Command . PmpConverter
1717{
@@ -67,7 +67,6 @@ private static void Convert(string fileIn, string fileOut)
6767 Pmp pmp = MeshGroupList2PMP ( p ) ;
6868 using Stream stream = File . Create ( fileOut ) ;
6969 Pmp . Write ( stream , pmp ) ;
70- stream . Close ( ) ;
7170 }
7271
7372 private static Pmp MeshGroupList2PMP ( List < MeshGroup > meshGroup )
@@ -118,10 +117,10 @@ private static Pmp MeshGroupList2PMP(List<MeshGroup> meshGroup)
118117 // Set extra flags.
119118 if ( UsesUniformColor )
120119 {
121- var UniformColor = ( uint ) ( desc . Vertices [ 0 ] . A / 255f ) ;
122- UniformColor + = ( uint ) ( desc . Vertices [ 0 ] . B / 255f ) << 8 ;
123- UniformColor + = ( uint ) ( desc . Vertices [ 0 ] . G / 255f ) << 16 ;
124- UniformColor + = ( uint ) ( desc . Vertices [ 0 ] . R / 255f ) << 24 ;
120+ var UniformColor = ( uint ) ( desc . Vertices [ 0 ] . A * 255f ) ;
121+ UniformColor | = ( uint ) ( desc . Vertices [ 0 ] . B * 255f ) << 8 ;
122+ UniformColor | = ( uint ) ( desc . Vertices [ 0 ] . G * 255f ) << 16 ;
123+ UniformColor | = ( uint ) ( desc . Vertices [ 0 ] . R * 255f ) << 24 ;
125124 chunk . SectionInfo . VertexFlags = BitsUtil . Int . SetBit ( chunk . SectionInfo . VertexFlags , 24 , true ) ;
126125 chunk . SectionInfo_opt2 = new Pmo . MeshSectionOptional2 ( ) ;
127126 chunk . SectionInfo_opt2 . DiffuseColor = UniformColor ;
@@ -331,17 +330,40 @@ public static List<MeshGroup> FromFbx(string filePath)
331330 var assimp = new Assimp . AssimpContext ( ) ;
332331 var scene = assimp . ImportFile ( filePath , Assimp . PostProcessSteps . PreTransformVertices ) ;
333332 var baseFilePath = Path . GetDirectoryName ( filePath ) ;
333+ if ( String . IsNullOrEmpty ( baseFilePath ) )
334+ {
335+ baseFilePath = "." ;
336+ }
334337 TexList = new List < string > ( ) ;
335338 TextureData = new List < Tm2 > ( ) ;
336339
337340 foreach ( Assimp . Material mat in scene . Materials )
338341 {
339- TexList . Add ( Path . GetFileName ( mat . TextureDiffuse . FilePath ) ) ;
340- Stream str = File . OpenRead ( TexList [ TexList . Count - 1 ] ) ;
342+ if ( String . IsNullOrEmpty ( mat . TextureDiffuse . FilePath ) )
343+ {
344+ throw new Exception ( $ "Material '{ mat . Name } ' has no diffuse texture specified") ;
345+ }
346+
347+ var texPath = Path . IsPathRooted ( mat . TextureDiffuse . FilePath )
348+ ? Path . GetFullPath ( mat . TextureDiffuse . FilePath )
349+ : Path . GetFullPath ( Path . Combine ( baseFilePath , mat . TextureDiffuse . FilePath ) ) ;
341350
342- PngImage png = new PngImage ( str ) ;
343- Tm2 tmImage = Tm2 . Create ( png ) ;
344- TextureData . Add ( tmImage ) ;
351+ var comparison = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows )
352+ ? StringComparison . OrdinalIgnoreCase
353+ : StringComparison . Ordinal ;
354+
355+ if ( ! texPath . StartsWith ( Path . GetFullPath ( baseFilePath + Path . DirectorySeparatorChar ) , comparison ) )
356+ {
357+ throw new Exception ( $ "The file { texPath } is outside the output directory") ;
358+ }
359+
360+ TexList . Add ( Path . GetFileName ( texPath ) ) ;
361+ using ( Stream str = new FileStream ( texPath , FileMode . Open , FileAccess . Read , FileShare . Read ) )
362+ {
363+ PngImage png = new PngImage ( str ) ;
364+ Tm2 tmImage = Tm2 . Create ( png ) ;
365+ TextureData . Add ( tmImage ) ;
366+ }
345367 }
346368
347369 for ( int i = 0 ; i < scene . RootNode . ChildCount ; i ++ )
@@ -364,10 +386,10 @@ public static List<MeshGroup> FromFbx(string filePath)
364386 vertices [ k ] . Z = x . Vertices [ k ] . Z * Scale ;
365387 vertices [ k ] . Tu = x . TextureCoordinateChannels [ 0 ] [ k ] . X ;
366388 vertices [ k ] . Tv = 1.0f - x . TextureCoordinateChannels [ 0 ] [ k ] . Y ;
367- vertices [ k ] . R = x . VertexColorChannels [ 0 ] [ i ] . R ;
368- vertices [ k ] . G = x . VertexColorChannels [ 0 ] [ i ] . G ;
369- vertices [ k ] . B = x . VertexColorChannels [ 0 ] [ i ] . B ;
370- vertices [ k ] . A = x . VertexColorChannels [ 0 ] [ i ] . A ;
389+ vertices [ k ] . R = x . VertexColorChannels [ 0 ] [ k ] . R ;
390+ vertices [ k ] . G = x . VertexColorChannels [ 0 ] [ k ] . G ;
391+ vertices [ k ] . B = x . VertexColorChannels [ 0 ] [ k ] . B ;
392+ vertices [ k ] . A = x . VertexColorChannels [ 0 ] [ k ] . A ;
371393 }
372394
373395 meshDescriptor . Vertices = vertices ;
0 commit comments