From fca9e101a29eadae1c88bd1e44d16b3105699afd Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Mon, 27 Jul 2015 16:24:36 -1000 Subject: [PATCH] Added CRandom16 implementation --- DataSpec/DNAMP1/CMDL.cpp | 142 +++++++++++++++++++++++++++++++++ DataSpec/DNAMP1/CMDL.hpp | 48 ++++++++++- DataSpec/DNAMP1/CMakeLists.txt | 3 +- DataSpec/DNAMP1/DNAMP1.cpp | 3 + NODLib | 2 +- Runtime/Core/CMakeLists.txt | 2 + Runtime/Core/CRandom16.cpp | 1 + Runtime/Core/CRandom16.hpp | 81 +++++++++++++++++++ 8 files changed, 278 insertions(+), 4 deletions(-) create mode 100644 DataSpec/DNAMP1/CMDL.cpp create mode 100644 Runtime/Core/CRandom16.cpp create mode 100644 Runtime/Core/CRandom16.hpp diff --git a/DataSpec/DNAMP1/CMDL.cpp b/DataSpec/DNAMP1/CMDL.cpp new file mode 100644 index 000000000..3e63ef682 --- /dev/null +++ b/DataSpec/DNAMP1/CMDL.cpp @@ -0,0 +1,142 @@ +#include +#include "CMDL.hpp" +#include "DNAMP1.hpp" + +namespace Retro +{ +namespace DNAMP1 +{ + +bool CMDL::ReadToBlender(HECL::BlenderConnection& conn, Athena::io::IStreamReader& reader) +{ + reader.setEndian(Athena::BigEndian); + + CMDL::Header head; + head.read(reader); + + if (head.magic != 0xDEADBABE) + { + Log.report(LogVisor::Error, "invalid CMDL magic"); + return false; + } + + if (head.version != 2) + { + Log.report(LogVisor::Error, "invalid CMDL version for MP1"); + return false; + } + + /* Open Py Stream */ + HECL::BlenderConnection::PyOutStream os = conn.beginPythonOut(); + os << "bm = bmesh.new();\n"; + + for (size_t s=0 ; s vertPos; + std::unique_ptr vertNorm; + typedef atInt16 ShortVec3[3]; + std::unique_ptr vertNormShort; + std::unique_ptr vertUVs; + typedef atInt16 ShortVec2[2]; + std::unique_ptr vertUVsShort; + bool visitedDLOffsets = false; + if (s < head.matSetCount) + { + MaterialSet matSet; + matSet.read(reader); + } + else + { + switch (s-head.matSetCount) + { + case 0: + { + /* Positions */ + size_t vertCount = head.secSizes[s] / 12; + vertPos.reset(new atVec3f[vertCount]); + for (size_t i=0 ; i magic; + Value version; + struct Flags + { + DECL_DNA + Value flags; + inline bool shortNormals() const {return (flags & 0x2) != 0;} + inline void setShortNormals(bool val) {flags &= ~0x2; flags |= val << 1;} + inline bool shortUVs() const {return (flags & 0x4) != 0;} + inline void setShortUVs(bool val) {flags &= ~0x4; flags |= val << 2;} + } flags; + Value aabbMin; + Value aabbMax; + Value secCount; + Value matSetCount; + Vector secSizes; + }; + + struct SurfaceHeader : BigDNA + { + DECL_DNA + Value centroid; + Value matIdx; + Value qDiv; + Value dlSize; + Seek<8, Athena::Current> seek; + Value aabbSz; + Value reflectionNormal; + Seek seek2; + Align<32> align; + }; + + static bool ReadToBlender(HECL::BlenderConnection& conn, Athena::io::IStreamReader& reader); + + static bool Extract(PAKEntryReadStream& rs, const HECL::ProjectPath& outPath) + { + HECL::BlenderConnection& conn = HECL::BlenderConnection::SharedConnection(); + if (!conn.createBlend(outPath.getAbsolutePath())) + return false; + return ReadToBlender(conn, rs); + } }; } diff --git a/DataSpec/DNAMP1/CMakeLists.txt b/DataSpec/DNAMP1/CMakeLists.txt index fc0650bb1..a4f3bad21 100644 --- a/DataSpec/DNAMP1/CMakeLists.txt +++ b/DataSpec/DNAMP1/CMakeLists.txt @@ -7,4 +7,5 @@ add_library(DNAMP1 DNAMP1.hpp DNAMP1.cpp ${liblist} PAK.cpp - STRG.hpp STRG.cpp) + STRG.hpp STRG.cpp + CMDL.cpp) diff --git a/DataSpec/DNAMP1/DNAMP1.cpp b/DataSpec/DNAMP1/DNAMP1.cpp index 23c7c63d7..9951f79dd 100644 --- a/DataSpec/DNAMP1/DNAMP1.cpp +++ b/DataSpec/DNAMP1/DNAMP1.cpp @@ -5,6 +5,7 @@ #include "STRG.hpp" #include "MLVL.hpp" #include "../DNACommon/TXTR.hpp" +#include "CMDL.hpp" namespace Retro { @@ -52,6 +53,8 @@ ResExtractor PAKBridge::LookupExtractor(const PAK::Entry& entry) return {STRG::Extract, ".as"}; case SBIG('TXTR'): return {TXTR::Extract, ".png"}; + case SBIG('CMDL'): + return {CMDL::Extract, ".blend"}; } return {}; } diff --git a/NODLib b/NODLib index 7fafe3ce0..ebb5c0743 160000 --- a/NODLib +++ b/NODLib @@ -1 +1 @@ -Subproject commit 7fafe3ce0c14f9e244bb47572f5448e82b597a68 +Subproject commit ebb5c0743da7fafbe4c3945d0117700c43845425 diff --git a/Runtime/Core/CMakeLists.txt b/Runtime/Core/CMakeLists.txt index e69de29bb..654f25dbf 100644 --- a/Runtime/Core/CMakeLists.txt +++ b/Runtime/Core/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(RetroCore + CRandom16.hpp CRandom16.cpp) diff --git a/Runtime/Core/CRandom16.cpp b/Runtime/Core/CRandom16.cpp new file mode 100644 index 000000000..af7603c72 --- /dev/null +++ b/Runtime/Core/CRandom16.cpp @@ -0,0 +1 @@ +#include "CRandom16.hpp" diff --git a/Runtime/Core/CRandom16.hpp b/Runtime/Core/CRandom16.hpp new file mode 100644 index 000000000..a99e9e920 --- /dev/null +++ b/Runtime/Core/CRandom16.hpp @@ -0,0 +1,81 @@ +#ifndef RETRO_CRANDOM16_HPP +#define RETRO_CRANDOM16_HPP + +#include +#include "CRandom16.hpp" + +namespace Retro +{ + +extern class CRandom16* GLOBAL_RANDOM; +extern class CGlobalRandom* GLOBAL_RANDOM_TOKEN; + +class CRandom16 +{ + uint32_t m_seed; +public: + CRandom16(uint32_t p) : m_seed(p) {} + + inline uint32_t Next() + { + m_seed = m_seed * 0x41c64e6d + 0x00003039; + return m_seed >> 16; + } + + inline uint32_t GetSeed() const + { + return m_seed; + } + + inline void SetSeed(time_t p) + { + m_seed = p; + } + + inline float Float() + { + return Next() * 0.000015259022; + } + + inline float Range(float min, float max) + { + return min + Float() * (max - min); + } + + inline int32_t Range(int32_t min, int32_t max) + { + uint32_t diff = max - min; + uint32_t rand = 0xffffffff; + while (rand == 0xffffffff) + rand = (Next() << 16) | Next(); + return rand % diff + min; + } + + static CRandom16* GetRandomNumber() {return GLOBAL_RANDOM;} + static void SetRandomNumber(CRandom16* rnd) {GLOBAL_RANDOM = rnd;} +}; + +class CGlobalRandom +{ + CRandom16& m_random; + CGlobalRandom* m_prev; +public: + CGlobalRandom(CRandom16& rand) + : m_random(rand), m_prev(GLOBAL_RANDOM_TOKEN) + { + GLOBAL_RANDOM_TOKEN = this; + CRandom16::SetRandomNumber(&m_random); + } + ~CGlobalRandom() + { + GLOBAL_RANDOM_TOKEN = m_prev; + if (m_prev) + CRandom16::SetRandomNumber(&m_prev->m_random); + else + CRandom16::SetRandomNumber(nullptr); + } +}; + +} + +#endif // RETRO_CRANDOM16_HPP