diff --git a/Rez/CMakeLists.txt b/Rez/CMakeLists.txt index 4b59cb4f7b..2e7059a25e 100644 --- a/Rez/CMakeLists.txt +++ b/Rez/CMakeLists.txt @@ -56,6 +56,6 @@ add_executable(Rez ResourceCompiler.cc ResourceCompiler.h ) -target_link_libraries(Rez ${Boost_LIBRARIES}) +target_link_libraries(Rez ResourceFiles ${Boost_LIBRARIES}) install(TARGETS Rez RUNTIME DESTINATION bin) diff --git a/Rez/ResourceCompiler.cc b/Rez/ResourceCompiler.cc index d54798d05e..bfb73cfd53 100644 --- a/Rez/ResourceCompiler.cc +++ b/Rez/ResourceCompiler.cc @@ -8,10 +8,31 @@ ResourceCompiler::ResourceCompiler(TypeDefinitionPtr type, CompoundExprPtr body) } +std::string ResourceCompiler::resourceData() +{ + return std::string(data.begin(), data.end()); +} + void ResourceCompiler::write(int nBits, int value) { std::cout << "[" << nBits << " bits] = " << std::hex << value << std::dec << std::endl; - currentOffset += nBits; + + unsigned mask = 1 << (nBits-1); + + for(int i = 0; i < nBits; i++) + { + bool bit = (value & mask) != 0; + + if(currentOffset % 8 == 0) + data.push_back(bit ? 0x80 : 0); + else if(bit) + data.back() |= (0x80 >> (currentOffset % 8)); + ++currentOffset; + + mask >>= 1; + } + + //currentOffset += nBits; } ExprPtr ResourceCompiler::lookupIdentifier(std::string name, const Subscripts &sub) @@ -19,13 +40,18 @@ ExprPtr ResourceCompiler::lookupIdentifier(std::string name, const Subscripts &s if(currentField) { if(ExprPtr val = currentField->lookupNamedValue(name)) + { + std::cout << "current field: " << name << " found."; return val; + } } auto p = labelValues.find(std::make_pair(name, sub)); if(p != labelValues.end()) return p->second; + std::cout << "ID lookup failed: " << name << std::endl; + return nullptr; } @@ -38,9 +64,11 @@ void ResourceCompiler::compile() { std::cout << "(first pass)\n"; currentOffset = 0; + data.clear(); typeDefinition->compile(body, this, true); std::cout << "(second pass)\n"; currentOffset = 0; + data.clear(); typeDefinition->compile(body, this, false); std::cout << "(done)\n"; diff --git a/Rez/ResourceCompiler.h b/Rez/ResourceCompiler.h index 697dc9e556..bafb10e5ce 100644 --- a/Rez/ResourceCompiler.h +++ b/Rez/ResourceCompiler.h @@ -31,10 +31,14 @@ class ResourceCompiler Field* currentField; Subscripts currentSubscripts; + std::vector data; + void beginArrayScope(std::string& arrayName, int index); public: ResourceCompiler(TypeDefinitionPtr type, CompoundExprPtr body); + std::string resourceData(); + void reserve(int nBits) { write(nBits, 0); } void write(int nBits, int value); int tell() { return currentOffset; } diff --git a/Rez/ResourceDefinitions.cc b/Rez/ResourceDefinitions.cc index 36078bd934..08c8ce44d8 100644 --- a/Rez/ResourceDefinitions.cc +++ b/Rez/ResourceDefinitions.cc @@ -4,16 +4,7 @@ #include "ResourceCompiler.h" -std::ostream &operator<<(std::ostream &out, ResType t) -{ - char c1 = static_cast((int)t >> 24); - char c2 = static_cast((int)t >> 16); - char c3 = static_cast((int)t >> 8); - char c4 = static_cast((int)t); - out << "'" << c1 << c2 << c3 << c4 << "'"; - return out; -} std::ostream &operator<<(std::ostream &out, TypeSpec ts) { diff --git a/Rez/ResourceDefinitions.h b/Rez/ResourceDefinitions.h index d2456e2419..e2d5bb828f 100644 --- a/Rez/ResourceDefinitions.h +++ b/Rez/ResourceDefinitions.h @@ -6,18 +6,8 @@ #include #include "Expression.h" +#include "ResType.h" -class ResType -{ - int x; -public: - ResType() : x(0) {} - ResType(int x) : x(x) {} - operator int() const { return x; } - bool operator<(ResType y) const { return x < y.x; } -}; - -std::ostream& operator<<(std::ostream& out, ResType t); class TypeSpec { diff --git a/Rez/Rez.cc b/Rez/Rez.cc index ee617ec62d..69a75e2a38 100644 --- a/Rez/Rez.cc +++ b/Rez/Rez.cc @@ -6,6 +6,9 @@ #include "RezLexer.h" #include "RezWorld.h" +#include "ResourceFiles.h" +#include "BinaryIO.h" + int main() { //RezLexer lexer("/home/wolfgang/Projects/Retro68/RIncludes/Types.r"); @@ -14,5 +17,18 @@ int main() RezParser parser(lexer, world); parser.parse(); + { + std::ofstream dataOut("test.rsrc"); + system("mkdir -p .rsrc"); + std::ofstream rsrcOut(".rsrc/test.rsrc"); + + world.getResources().writeFork(rsrcOut); + system("mkdir -p .finf"); + std::ofstream finfOut(".finf/test.rsrc"); + ostype(finfOut, "rsrc"); + ostype(finfOut, "RSED"); + for(int i = 8; i < 32; i++) + byte(finfOut, 0); + } return 0; } diff --git a/Rez/RezLexer.cc b/Rez/RezLexer.cc index 201bad5723..a53a2cdd04 100644 --- a/Rez/RezLexer.cc +++ b/Rez/RezLexer.cc @@ -88,11 +88,10 @@ RezLexer::RezLexer(std::string filename) filename)); pImpl->ctx.add_include_path("/home/wolfgang/Projects/Retro68/RIncludes"); - // ctx.add_macro_definition(...); - pImpl->ctx.add_macro_definition("DeRez", "0"); - pImpl->ctx.add_macro_definition("Rez", "1"); - pImpl->ctx.add_macro_definition("true", "1"); - pImpl->ctx.add_macro_definition("false", "0"); + pImpl->ctx.add_macro_definition("DeRez=0"); + pImpl->ctx.add_macro_definition("Rez=1"); + pImpl->ctx.add_macro_definition("true=1"); + pImpl->ctx.add_macro_definition("false=0"); pImpl->iter = pImpl->ctx.begin(); } @@ -109,7 +108,14 @@ bool RezLexer::atEnd() RezLexer::WaveToken RezLexer::nextWave() { - return pImpl->iter == pImpl->ctx.end() ? WaveToken() : (*pImpl->iter++); + if(pImpl->iter == pImpl->ctx.end()) + return WaveToken(); + else + { + WaveToken tok = *pImpl->iter++; + std::cout << tok.get_value(); + return tok; + } } RezLexer::WaveToken RezLexer::peekWave() diff --git a/Rez/RezParser.yy b/Rez/RezParser.yy index 6aa71657b7..68a3ff69e5 100644 --- a/Rez/RezParser.yy +++ b/Rez/RezParser.yy @@ -346,10 +346,7 @@ function_argument_list1 : expression resource : "resource" res_type "(" expression resource_attributes ")" "{" resource_body "}" { int id = $expression->evaluateInt(nullptr); - std::cout << "RESOURCE " << $2 << "(" << id << ")" << std::endl; - TypeDefinitionPtr type = world.getTypeDefinition($res_type, id); - ResourceCompiler compiler(type, $resource_body); - compiler.compile(); + world.addResource($res_type, id, $resource_body); } ; diff --git a/Rez/RezWorld.cc b/Rez/RezWorld.cc index 90e6148b1a..68b77897ea 100644 --- a/Rez/RezWorld.cc +++ b/Rez/RezWorld.cc @@ -1,4 +1,8 @@ #include "RezWorld.h" +#include "ResourceCompiler.h" +#include "ResourceFiles.h" + +#include RezWorld::RezWorld() { @@ -20,3 +24,13 @@ TypeDefinitionPtr RezWorld::getTypeDefinition(ResType type, int id) return nullptr; } + +void RezWorld::addResource(ResType type, int id, CompoundExprPtr body) +{ + std::cout << "RESOURCE " << type << "(" << id << ")" << std::endl; + TypeDefinitionPtr def = getTypeDefinition(type, id); + ResourceCompiler compiler(def, body); + compiler.compile(); + + resources.addResource(Resource(type, id, compiler.resourceData())); +} diff --git a/Rez/RezWorld.h b/Rez/RezWorld.h index ec44502369..fa0cf24608 100644 --- a/Rez/RezWorld.h +++ b/Rez/RezWorld.h @@ -5,6 +5,7 @@ #include #include "ResourceDefinitions.h" #include "Expression.h" +#include "ResourceFiles.h" class RezWorld { @@ -14,11 +15,17 @@ class RezWorld std::stack fieldLists; std::stack functionCalls; std::stack switches; + + Resources resources; public: RezWorld(); void addTypeDefinition(TypeSpec spec, TypeDefinitionPtr type); TypeDefinitionPtr getTypeDefinition(ResType type, int id); + + void addResource(ResType type, int id, CompoundExprPtr body); + + Resources& getResources() { return resources; } };