yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1#include "slang-filesystem.h" 2 3#include "../../core/slang-io.h" 4#include "../util/record-utility.h" 5#include "output-stream.h" 6 7#include <stdlib.h> 8 9namespace SlangRecord 10{ 11// We don't actually need to record the methods of ISlangFileSystemExt, we just want to record the 12// file content and save them into disk. 13FileSystemRecorder ::FileSystemRecorder ( 14ISlangFileSystemExt * fileSystem , 15RecordManager * recordManager ) 16 :m_actualFileSystem (fileSystem ),m_recordManager (recordManager ) 17{ 18SLANG_RECORD_ASSERT (m_actualFileSystem ); 19SLANG_RECORD_ASSERT (m_recordManager ); 20slangRecordLog (LogLevel ::Verbose ,"%s: %p\n" ,__PRETTY_FUNCTION__ ,m_actualFileSystem .get ()); 21} 22 23void * FileSystemRecorder ::castAs (const Slang ::Guid & guid ) 24{ 25return getInterface (guid ); 26} 27 28ISlangUnknown * FileSystemRecorder ::getInterface (const Slang ::Guid & guid ) 29{ 30if (guid == ISlangUnknown ::getTypeGuid ()|| guid == ISlangFileSystem ::getTypeGuid ()) 31return static_cast < ISlangFileSystem *> (this ); 32return nullptr ; 33} 34 35// TODO: There could be a potential issue that could not be able to dump the generated file content 36// correctly. Details: https://github.com/shader-slang/slang/issues/4423. 37SLANG_NO_THROW SlangResult FileSystemRecorder ::loadFile (char const * path ,ISlangBlob ** outBlob ) 38{ 39slangRecordLog ( 40LogLevel ::Verbose , 41"%p: %s, :%s\n" , 42m_actualFileSystem .get (), 43__PRETTY_FUNCTION__ , 44path ); 45SlangResult res = m_actualFileSystem -> loadFile (path ,outBlob ); 46 47// Since the loadFile method could be implemented by client, we can't guarantee the result is 48// always as expected, we will check every thing to make sure we won't crash at writing file. 49// 50// We can only dump the file content after this 'loadFile' call, no matter this call crashes or 51// file is not found, we can't save the file anyway, so we don't need to pay special care to the 52// crash recovery. We will know something wrong with the loadFile call if we can't find the file 53// in the record directory. 54if ((res == SLANG_OK )&& (* outBlob != nullptr )&& ((* outBlob )-> getBufferSize ()!= 0 )) 55 { 56Slang ::String filePath = 57Slang ::Path ::combine (m_recordManager -> getRecordFileDirectory (),path ); 58Slang ::String dirPath = Slang ::Path ::getParentDirectory (filePath ); 59if (!File ::exists (dirPath )) 60 { 61slangRecordLog ( 62LogLevel ::Debug , 63"Create directory: %s to save captured shader file: %s\n" , 64dirPath .getBuffer (), 65filePath .getBuffer ()); 66 67if (!Path ::createDirectoryRecursive (dirPath )) 68 { 69slangRecordLog ( 70LogLevel ::Error , 71"Fail to create directory: %s\n" , 72dirPath .getBuffer ()); 73return SLANG_FAIL ; 74 } 75 } 76 77FileOutputStream fileStream (filePath ); 78 79fileStream .write ((* outBlob )-> getBufferPointer (), (* outBlob )-> getBufferSize ()); 80fileStream .flush (); 81 } 82return res ; 83} 84 85SLANG_NO_THROW SlangResult 86FileSystemRecorder ::getFileUniqueIdentity (const char * path ,ISlangBlob ** outUniqueIdentity ) 87{ 88slangRecordLog ( 89LogLevel ::Verbose , 90"%p: %s :\"%s\"\n" , 91m_actualFileSystem .get (), 92__PRETTY_FUNCTION__ , 93path ); 94SlangResult res = m_actualFileSystem -> getFileUniqueIdentity (path ,outUniqueIdentity ); 95return res ; 96} 97 98SLANG_NO_THROW SlangResult FileSystemRecorder ::calcCombinedPath ( 99SlangPathType fromPathType , 100const char * fromPath , 101const char * path , 102ISlangBlob ** pathOut ) 103{ 104slangRecordLog ( 105LogLevel ::Verbose , 106"%p: %s, :%s\n" , 107m_actualFileSystem .get (), 108__PRETTY_FUNCTION__ , 109path ); 110SlangResult res = m_actualFileSystem -> calcCombinedPath (fromPathType ,fromPath ,path ,pathOut ); 111return res ; 112} 113 114SLANG_NO_THROW SlangResult 115FileSystemRecorder ::getPathType (const char * path ,SlangPathType * pathTypeOut ) 116{ 117slangRecordLog ( 118LogLevel ::Verbose , 119"%p: %s, :%s\n" , 120m_actualFileSystem .get (), 121__PRETTY_FUNCTION__ , 122path ); 123SlangResult res = m_actualFileSystem -> getPathType (path ,pathTypeOut ); 124return res ; 125} 126 127SLANG_NO_THROW SlangResult 128FileSystemRecorder ::getPath (PathKind kind ,const char * path ,ISlangBlob ** outPath ) 129{ 130slangRecordLog ( 131LogLevel ::Verbose , 132"%p: %s, :%s\n" , 133m_actualFileSystem .get (), 134__PRETTY_FUNCTION__ , 135path ); 136SlangResult res = m_actualFileSystem -> getPath (kind ,path ,outPath ); 137return res ; 138} 139 140SLANG_NO_THROW void FileSystemRecorder ::clearCache () 141{ 142slangRecordLog (LogLevel ::Verbose ,"%p: %s\n" ,m_actualFileSystem .get (),__PRETTY_FUNCTION__ ); 143m_actualFileSystem -> clearCache (); 144} 145 146SLANG_NO_THROW SlangResult FileSystemRecorder ::enumeratePathContents ( 147const char * path , 148FileSystemContentsCallBack callback , 149void * userData ) 150{ 151slangRecordLog ( 152LogLevel ::Verbose , 153"%p: %s, :%s\n" , 154m_actualFileSystem .get (), 155__PRETTY_FUNCTION__ , 156path ); 157SlangResult res = m_actualFileSystem -> enumeratePathContents (path ,callback ,userData ); 158return res ; 159} 160 161SLANG_NO_THROW OSPathKind FileSystemRecorder ::getOSPathKind () 162{ 163slangRecordLog (LogLevel ::Verbose ,"%p: %s\n" ,m_actualFileSystem .get (),__PRETTY_FUNCTION__ ); 164OSPathKind pathKind = m_actualFileSystem -> getOSPathKind (); 165return pathKind ; 166} 167}// namespace SlangRecord