Browse Source

Introjucer: when saving very large amounts of binary data, it now splits it across multiple cpp files

tags/2021-05-28
jules 13 years ago
parent
commit
828d6f8e18
4 changed files with 126 additions and 53 deletions
  1. +10
    -6
      extras/Introjucer/Source/Project Saving/jucer_ProjectSaver.h
  2. +113
    -44
      extras/Introjucer/Source/Project Saving/jucer_ResourceFile.cpp
  3. +3
    -2
      extras/Introjucer/Source/Project Saving/jucer_ResourceFile.h
  4. +0
    -1
      extras/Introjucer/Source/Project/jucer_ProjectTreeViewBase.h

+ 10
- 6
extras/Introjucer/Source/Project Saving/jucer_ProjectSaver.h View File

@@ -469,13 +469,17 @@ private:
{ {
resourceFile.setClassName ("BinaryData"); resourceFile.setClassName ("BinaryData");
if (resourceFile.write (binaryDataCpp))
Array<File> binaryDataFiles;
if (resourceFile.write (binaryDataCpp, binaryDataFiles))
{ {
filesCreated.add (binaryDataH);
filesCreated.add (binaryDataCpp);
generatedFilesGroup.addFile (binaryDataCpp, -1, true);
generatedFilesGroup.addFile (binaryDataH, -1, false);
for (int i = 0; i < binaryDataFiles.size(); ++i)
{
const File& f = binaryDataFiles.getReference(i);
filesCreated.add (f);
generatedFilesGroup.addFile (f, -1, ! f.hasFileExtension (".h"));
}
} }
else else
{ {


+ 113
- 44
extras/Introjucer/Source/Project Saving/jucer_ResourceFile.cpp View File

@@ -116,7 +116,7 @@ int64 ResourceFile::getTotalDataSize() const
return total; return total;
} }
bool ResourceFile::write (const File& cppFile, OutputStream& cpp, OutputStream& header)
static String getComment()
{ {
String comment; String comment;
comment << newLine << newLine comment << newLine << newLine
@@ -124,19 +124,17 @@ bool ResourceFile::write (const File& cppFile, OutputStream& cpp, OutputStream&
<< newLine << newLine
<< "*/" << newLine << "*/" << newLine
<< newLine; << newLine;
return comment;
}
bool ResourceFile::writeHeader (MemoryOutputStream& header)
{
header << "/* =========================================================================================" header << "/* ========================================================================================="
<< comment;
cpp << "/* ==================================== " << resourceFileIdentifierString << " ===================================="
<< comment;
<< getComment()
<< "namespace " << className << newLine
<< "{" << newLine;
const String namespaceName (className);
cpp << "namespace " << namespaceName << newLine << "{" << newLine;
header << "namespace " << namespaceName << newLine << "{" << newLine;
StringArray returnCodes;
bool containsAnyImages = false; bool containsAnyImages = false;
for (int i = 0; i < files.size(); ++i) for (int i = 0; i < files.size(); ++i)
@@ -150,8 +148,6 @@ bool ResourceFile::write (const File& cppFile, OutputStream& cpp, OutputStream&
if (fileStream.openedOk()) if (fileStream.openedOk())
{ {
returnCodes.add ("numBytes = " + String (dataSize) + "; return " + variableName + ";");
containsAnyImages = containsAnyImages containsAnyImages = containsAnyImages
|| (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr); || (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr);
@@ -159,6 +155,42 @@ bool ResourceFile::write (const File& cppFile, OutputStream& cpp, OutputStream&
header << " extern const char* " << variableName << ";" << newLine; header << " extern const char* " << variableName << ";" << newLine;
header << " const int " << variableName << "Size = " << (int) dataSize << ";" << newLine << newLine; header << " const int " << variableName << "Size = " << (int) dataSize << ";" << newLine << newLine;
}
}
header << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
<< " // return the corresponding data and its size (or a null pointer if the name isn't found)." << newLine
<< " const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes) throw();" << newLine
<< "}" << newLine;
return true;
}
bool ResourceFile::writeCpp (MemoryOutputStream& cpp, const File& headerFile, int& i)
{
const int maxFileSize = 10 * 1024 * 1024;
const bool isFirstFile = (i == 0);
cpp << "/* ==================================== " << resourceFileIdentifierString << " ===================================="
<< getComment()
<< "namespace " << className << newLine
<< "{" << newLine;
bool containsAnyImages = false;
while (i < files.size())
{
const File& file = files.getReference(i);
const String variableName (variableNames[i]);
FileInputStream fileStream (file);
if (fileStream.openedOk())
{
containsAnyImages = containsAnyImages
|| (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr);
const String tempVariable ("temp_" + String::toHexString (file.hashCode()));
cpp << newLine << "//================== " << file.getFileName() << " ==================" << newLine cpp << newLine << "//================== " << file.getFileName() << " ==================" << newLine
<< "static const unsigned char " << tempVariable << "[] =" << newLine; << "static const unsigned char " << tempVariable << "[] =" << newLine;
@@ -172,49 +204,86 @@ bool ResourceFile::write (const File& cppFile, OutputStream& cpp, OutputStream&
cpp << newLine << newLine cpp << newLine << newLine
<< "const char* " << variableName << " = (const char*) " << tempVariable << ";" << newLine; << "const char* " << variableName << " = (const char*) " << tempVariable << ";" << newLine;
} }
++i;
if (cpp.getPosition() > maxFileSize)
break;
} }
if (isFirstFile)
{
if (i < files.size())
{
cpp << newLine
<< "}" << newLine
<< newLine
<< "#include \"" << headerFile.getFileName() << "\"" << newLine
<< newLine
<< "namespace " << className << newLine
<< "{";
}
cpp << newLine
<< newLine
<< "const char* getNamedResource (const char*, int&) throw();" << newLine
<< "const char* getNamedResource (const char* resourceNameUTF8, int& numBytes) throw()" << newLine
<< "{" << newLine;
cpp << newLine
<< newLine
<< "const char* getNamedResource (const char*, int&) throw();" << newLine
<< "const char* getNamedResource (const char* resourceNameUTF8, int& numBytes) throw()" << newLine
<< "{" << newLine;
StringArray returnCodes;
for (int j = 0; j < files.size(); ++j)
{
const File& file = files.getReference(j);
const int64 dataSize = file.getSize();
returnCodes.add ("numBytes = " + String (dataSize) + "; return " + variableNames[j] + ";");
}
CodeHelpers::createStringMatcher (cpp, "resourceNameUTF8", variableNames, returnCodes, 4);
CodeHelpers::createStringMatcher (cpp, "resourceNameUTF8", variableNames, returnCodes, 4);
cpp << " numBytes = 0;" << newLine
<< " return 0;" << newLine
<< "}" << newLine
<< newLine
cpp << " numBytes = 0;" << newLine
<< " return 0;" << newLine
<< "}" << newLine;
}
cpp << newLine
<< "}" << newLine; << "}" << newLine;
header << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
<< " // return the corresponding data and its size (or a null pointer if the name isn't found)." << newLine
<< " const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes) throw();" << newLine
<< "}" << newLine;
return true; return true;
} }
bool ResourceFile::write (const File& cppFile)
bool ResourceFile::write (const File& cppFile, Array<File>& filesCreated)
{ {
TemporaryFile tempH (cppFile.withFileExtension (".h"), TemporaryFile::useHiddenFile);
TemporaryFile tempCpp (cppFile, TemporaryFile::useHiddenFile);
ScopedPointer <FileOutputStream> cppOut (tempCpp.getFile().createOutputStream (32768));
ScopedPointer <FileOutputStream> hppOut (tempH.getFile().createOutputStream (32768));
const File headerFile (cppFile.withFileExtension (".h"));
if (cppOut != nullptr && hppOut != nullptr)
{ {
if (write (cppFile, *cppOut, *hppOut))
{
cppOut = nullptr;
hppOut = nullptr;
return (tempCpp.getFile().hasIdenticalContentTo (tempCpp.getTargetFile()) || tempCpp.overwriteTargetFileWithTemporary())
&& (tempH.getFile().hasIdenticalContentTo (tempH.getTargetFile()) || tempH.overwriteTargetFileWithTemporary());
}
MemoryOutputStream mo;
if (! (writeHeader (mo) && FileHelpers::overwriteFileWithNewDataIfDifferent (headerFile, mo)))
return false;
filesCreated.add (headerFile);
}
int i = 0;
int fileIndex = 0;
for (;;)
{
File cpp (cppFile);
if (fileIndex > 0)
cpp = cpp.getSiblingFile (cppFile.getFileNameWithoutExtension() + String (fileIndex + 1))
.withFileExtension (cppFile.getFileExtension());
MemoryOutputStream mo;
if (! (writeCpp (mo, headerFile, i) && FileHelpers::overwriteFileWithNewDataIfDifferent (cpp, mo)))
return false;
filesCreated.add (cpp);
++fileIndex;
if (i >= files.size())
break;
} }
return false;
return true;
} }

+ 3
- 2
extras/Introjucer/Source/Project Saving/jucer_ResourceFile.h View File

@@ -52,8 +52,7 @@ public:
int getNumFiles() const { return files.size(); } int getNumFiles() const { return files.size(); }
int64 getTotalDataSize() const; int64 getTotalDataSize() const;
bool write (const File& cppFile);
bool write (const File& cppFile, OutputStream& cpp, OutputStream& header);
bool write (const File& cppFile, Array<File>& filesCreated);
//============================================================================== //==============================================================================
private: private:
@@ -62,6 +61,8 @@ private:
Project& project; Project& project;
String className; String className;
bool writeHeader (MemoryOutputStream&);
bool writeCpp (MemoryOutputStream&, const File& headerFile, int& index);
void addResourcesFromProjectItem (const Project::Item& node); void addResourcesFromProjectItem (const Project::Item& node);
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResourceFile); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ResourceFile);


+ 0
- 1
extras/Introjucer/Source/Project/jucer_ProjectTreeViewBase.h View File

@@ -29,7 +29,6 @@
#include "../jucer_Headers.h" #include "../jucer_Headers.h"
#include "../Utility/jucer_JucerTreeViewBase.h" #include "../Utility/jucer_JucerTreeViewBase.h"
#include "jucer_Project.h" #include "jucer_Project.h"
#include "../Project Saving/jucer_ResourceFile.h"
//============================================================================== //==============================================================================


Loading…
Cancel
Save