1 /** 2 Copyright (c) 2018 Clipsey (clipseypone@gmail.com) 3 4 Permission is hereby granted, free of charge, to any person or organization 5 obtaining a copy of the software and accompanying documentation covered by 6 this license (the "Software") to use, reproduce, display, distribute, 7 execute, and transmit the Software, and to prepare derivative works of the 8 Software, and to permit third-parties to whom the Software is furnished to 9 do so, all subject to the following: 10 11 The copyright notices in the Software and this entire statement, including 12 the above license grant, this restriction and the following disclaimer, 13 must be included in all copies of the Software, in whole or in part, and 14 all derivative works of the Software, unless such copies or derivative 15 works are solely in the form of machine-executable object code generated by 16 a source language processor. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 21 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 22 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 DEALINGS IN THE SOFTWARE. 25 */ 26 module ppc.types; 27 public import ppc.types.audio; 28 public import ppc.types.font; 29 public import ppc.types.image; 30 public import ppc.types.model; 31 public import ppc.types.script; 32 public import ppc.types.shader; 33 34 import ppc.backend.cfile; 35 import ppc.backend.signatures; 36 37 /// The types available to ppc 38 public enum Types : ubyte { 39 Audio, 40 Font, 41 Image, 42 Model, 43 Script, 44 Shader, 45 Raw, 46 47 48 Undecisive = 255 49 } 50 51 public Types fileSigToType(MemFile mf) { 52 if (mf.hasSignature(FileSignature.AudioOGG) || 53 mf.hasSignature(FileSignature.AudioWAV)) 54 return Types.Audio; 55 if (mf.hasSignature(FileSignature.ImageBMP) || 56 mf.hasSignature(FileSignature.ImagePNG) || 57 mf.hasSignature(FileSignature.ImagePTI)) 58 return Types.Image; 59 if (mf.hasSignature(FileSignature.ShaderPSGL)) 60 return Types.Shader; 61 return Types.Undecisive; 62 } 63 64 public Types fileExtToType(string filename) { 65 import std.path : extension; 66 67 switch(filename.extension) { 68 case ".wav": 69 case ".ogg": 70 case ".pcm": 71 return Types.Audio; 72 73 case ".png": 74 case ".tga": 75 case ".pti": 76 case ".bmp": 77 return Types.Image; 78 79 // Vertex shaders 80 case ".vsh": 81 case ".vert": 82 83 // Fragment shaders 84 case ".fsh": 85 case ".frag": 86 87 // Geometry shaders 88 case ".gsh": 89 case ".geom": 90 91 // Tesselation shaders 92 case ".tsh": 93 case ".tesc": 94 case ".tese": 95 96 // Compute shaders 97 case ".csh": 98 case ".comp": 99 100 // SPIR-V compiled shaders 101 case ".spv": 102 case ".sprv": 103 return Types.Shader; 104 105 case ".fbx": 106 case ".obj": 107 return Types.Model; 108 109 default: 110 return Types.Raw; 111 } 112 } 113 114 public Types getTypeOf(string filename) { 115 import std.stdio; 116 import std.conv; 117 MemFile mf = loadFile(filename); 118 Types ts = fileSigToType(mf); 119 scope(exit) destroy(mf); 120 return (ts == Types.Undecisive) ? fileExtToType(filename) : ts; 121 }