From f721d0494e8f0d135e01137411fc51ddc2c50174 Mon Sep 17 00:00:00 2001
From: Phillip Stephens <antidote.crk@gmail.com>
Date: Fri, 16 Sep 2016 23:40:45 -0700
Subject: [PATCH] Make CMain a polymorphic class

---
 DataSpec/DNAMP1/DNAMP1.cpp       |  3 ++
 Runtime/Audio/CAudioStateWin.cpp |  7 ++--
 Runtime/Audio/CSfxManager.hpp    |  4 ++-
 Runtime/CMakeLists.txt           |  1 +
 Runtime/GameGlobalObjects.cpp    |  2 +-
 Runtime/GameGlobalObjects.hpp    |  2 +-
 Runtime/IMain.hpp                | 58 ++++++++++++++++++++++++++++++++
 Runtime/MP1/CFrontEndUI.cpp      |  3 +-
 Runtime/MP1/CMFGame.cpp          |  4 +--
 Runtime/MP1/MP1.hpp              | 43 +++++------------------
 Runtime/World/CAi.cpp            |  3 ++
 Runtime/World/CAi.hpp            |  2 +-
 12 files changed, 87 insertions(+), 45 deletions(-)
 create mode 100644 Runtime/IMain.hpp

diff --git a/DataSpec/DNAMP1/DNAMP1.cpp b/DataSpec/DNAMP1/DNAMP1.cpp
index 6123ad52d..55208daa5 100644
--- a/DataSpec/DNAMP1/DNAMP1.cpp
+++ b/DataSpec/DNAMP1/DNAMP1.cpp
@@ -28,6 +28,7 @@
 #include "Tweaks/CTweakGunRes.hpp"
 #include "Tweaks/CTweakPlayer.hpp"
 #include "Tweaks/CTweakCameraBob.hpp"
+#include "Tweaks/CTweakSlideShow.hpp"
 
 namespace DataSpec
 {
@@ -321,6 +322,8 @@ ResExtractor<PAKBridge> PAKBridge::LookupExtractor(const PAK& pak, const PAK::En
                 return {ExtractTweak<CTweakPlayer>, nullptr, {_S(".yaml")}};
             if (!name.compare("CameraBob"))
                 return {ExtractTweak<CTweakCameraBob>, nullptr, {_S(".yaml")}};
+            if (!name.compare("SlideShow"))
+                return {ExtractTweak<CTweakSlideShow>, nullptr, {_S(".yaml")}};
         }
         break;
     }
diff --git a/Runtime/Audio/CAudioStateWin.cpp b/Runtime/Audio/CAudioStateWin.cpp
index 93dc02695..06d7c7a6d 100644
--- a/Runtime/Audio/CAudioStateWin.cpp
+++ b/Runtime/Audio/CAudioStateWin.cpp
@@ -4,13 +4,13 @@
 #include "CArchitectureQueue.hpp"
 #include "GameGlobalObjects.hpp"
 #include "CGameState.hpp"
+#include "MP1/MP1.hpp"
 
 namespace urde
 {
 
 CIOWin::EMessageReturn CAudioStateWin::OnMessage(const CArchitectureMessage& msg, CArchitectureQueue& queue)
 {
-#if 0
     const EArchMsgType msgType = msg.GetType();
     if (msgType == EArchMsgType::SetGameState)
     {
@@ -19,14 +19,13 @@ CIOWin::EMessageReturn CAudioStateWin::OnMessage(const CArchitectureMessage& msg
     }
     else if (msgType == EArchMsgType::QuitGameplay)
     {
-        if (g_GameState->GetWorldTransitionManager()->GetTransitionType() == CWorldTransManager::ETransType::Disabled ||
-            g_Main->x12c_ != 0)
+        if (g_GameState->GetWorldTransitionManager()->GetTransType() == CWorldTransManager::ETransType::Disabled ||
+            g_Main->GetFlowState() != MP1::CMain::EFlowState::Zero)
         {
             CSfxManager::SetChannel(CSfxManager::ESfxChannels::Zero);
             CSfxManager::KillAll(CSfxManager::ESfxChannels::One);
         }
     }
-#endif
     return EMessageReturn::Normal;
 }
 
diff --git a/Runtime/Audio/CSfxManager.hpp b/Runtime/Audio/CSfxManager.hpp
index 710180c84..119571d3b 100644
--- a/Runtime/Audio/CSfxManager.hpp
+++ b/Runtime/Audio/CSfxManager.hpp
@@ -138,6 +138,9 @@ public:
     static u16 kInternalInvalidSfxId;
     static u32 kAllAreas;
 
+    static void SetChannel(ESfxChannels) {}
+    static void KillAll(ESfxChannels) {}
+    static void TurnOnChannel(ESfxChannels) {}
     static ESfxChannels GetCurrentChannel() {return m_currentChannel;}
     static void AddListener(ESfxChannels,
                             const zeus::CVector3f& pos, const zeus::CVector3f& dir,
@@ -151,7 +154,6 @@ public:
     static void RemoveEmitter(const CSfxHandle&) {}
     static void PitchBend(const CSfxHandle&, s32) {}
     static u16 TranslateSFXID(u16);
-
     static CSfxHandle SfxStop(const CSfxHandle& handle);
     static CSfxHandle SfxStart(u16 id, s16 vol, s16 pan, bool active, s16 prio, bool inArea, s32 areaId);
 };
diff --git a/Runtime/CMakeLists.txt b/Runtime/CMakeLists.txt
index 333aded94..bb81edb98 100644
--- a/Runtime/CMakeLists.txt
+++ b/Runtime/CMakeLists.txt
@@ -47,6 +47,7 @@ add_library(RuntimeCommon
             ${WORLD_SOURCES}
             #CMemory.hpp CMemory.cpp
             ITweak.hpp
+            IMain.hpp
             CMemoryCardSys.hpp
             CScannableObjectInfo.hpp CScannableObjectInfo.cpp
             CSaveWorld.hpp CSaveWorld.cpp
diff --git a/Runtime/GameGlobalObjects.cpp b/Runtime/GameGlobalObjects.cpp
index e1551cb89..7f71e4a83 100644
--- a/Runtime/GameGlobalObjects.cpp
+++ b/Runtime/GameGlobalObjects.cpp
@@ -4,10 +4,10 @@ namespace urde
 {
 namespace MP1
 {
-class CMain* g_Main = nullptr;
 class CGameArchitectureSupport* g_archSupport = nullptr;
 }
 
+class IMain* g_Main = nullptr;
 class CMemoryCardSys* g_MemoryCardSys = nullptr;
 class IFactory* g_ResFactory = nullptr;
 class CSimplePool* g_SimplePool = nullptr;
diff --git a/Runtime/GameGlobalObjects.hpp b/Runtime/GameGlobalObjects.hpp
index a4ce68d22..4c920d591 100644
--- a/Runtime/GameGlobalObjects.hpp
+++ b/Runtime/GameGlobalObjects.hpp
@@ -13,9 +13,9 @@
 
 namespace urde
 {
+extern class IMain* g_Main;
 namespace MP1
 {
-extern class CMain* g_Main;
 extern class CGameArchitectureSupport* g_archSupport;
 }
 
diff --git a/Runtime/IMain.hpp b/Runtime/IMain.hpp
new file mode 100644
index 000000000..ec090de0f
--- /dev/null
+++ b/Runtime/IMain.hpp
@@ -0,0 +1,58 @@
+#ifndef __URDE_IMAIN_HPP__
+#define __URDE_IMAIN_HPP__
+#include <boo/boo.hpp>
+#include <boo/graphicsdev/GL.hpp>
+#include <boo/audiodev/IAudioVoiceEngine.hpp>
+#include <hecl/Runtime.hpp>
+#include <amuse/amuse.hpp>
+#include "RetroTypes.hpp"
+
+namespace urde
+{
+class CStopWatch;
+enum class EGameplayResult
+{
+    None,
+    Win,
+    Lose,
+    Playing
+};
+
+class IMain
+{
+public:
+    enum class EFlowState
+    {
+        Zero,
+        One,
+        Two,
+        Three,
+        Four,
+        Five,
+        Six,
+    };
+
+    virtual void RegisterResourceTweaks() {}
+    virtual void ResetGameState()=0;
+    virtual void StreamNewGameState(CInputStream&) {}
+    virtual void CheckTweakManagerDebugOptions() {}
+    virtual void Init(const hecl::Runtime::FileStoreManager& storeMgr,
+              boo::IAudioVoiceEngine* voiceEngine,
+              amuse::IBackendVoiceAllocator& backend)=0;
+    virtual void Draw()=0;
+    virtual bool Proc()=0;
+    virtual void Shutdown()=0;
+    virtual bool CheckReset()=0;
+    virtual bool CheckTerminate()=0;
+    virtual void DrawDebugMetrics(double, CStopWatch&) {}
+    virtual void DoPreDrawMetrics(){}
+    virtual void FillInAssetIDs()=0;
+    virtual void LoadAudio()=0;
+    virtual void ShutdownSubsystems()=0;
+    virtual EGameplayResult GetGameplayResult() const=0;
+    virtual void SetGameplayResult(EGameplayResult wl)=0;
+    virtual EFlowState GetFlowState() const=0;
+};
+}
+
+#endif // __URDE_IMAIN_HPP__
diff --git a/Runtime/MP1/CFrontEndUI.cpp b/Runtime/MP1/CFrontEndUI.cpp
index 2dd47801f..a78e80fe1 100644
--- a/Runtime/MP1/CFrontEndUI.cpp
+++ b/Runtime/MP1/CFrontEndUI.cpp
@@ -73,8 +73,9 @@ std::string CFrontEndUI::GetAttractMovieFileName(int idx)
 
 std::string CFrontEndUI::GetNextAttractMovieFileName()
 {
-    GetAttractMovieFileName(xbc_nextAttract);
+    std::string ret = GetAttractMovieFileName(xbc_nextAttract);
     xbc_nextAttract = (xbc_nextAttract + 1) % xc0_attractCount;
+    return ret;
 }
 
 void CFrontEndUI::SetCurrentMovie(EMenuMovie movie)
diff --git a/Runtime/MP1/CMFGame.cpp b/Runtime/MP1/CMFGame.cpp
index 55fb36669..e44d94161 100644
--- a/Runtime/MP1/CMFGame.cpp
+++ b/Runtime/MP1/CMFGame.cpp
@@ -31,8 +31,8 @@ CMFGameLoader::CMFGameLoader() : CMFGameLoaderBase("CMFGameLoader")
 {
     switch (g_Main->GetFlowState())
     {
-    case CMain::FlowState::Five:
-    case CMain::FlowState::Six:
+    case CMain::EFlowState::Five:
+    case CMain::EFlowState::Six:
     {
         ResId mlvlId = g_GameState->CurrentWorldAssetId();
         // g_GameState->WorldTransitionManager()->
diff --git a/Runtime/MP1/MP1.hpp b/Runtime/MP1/MP1.hpp
index c8aff2273..24ed1d589 100644
--- a/Runtime/MP1/MP1.hpp
+++ b/Runtime/MP1/MP1.hpp
@@ -3,9 +3,7 @@
 
 #define MP1_USE_BOO 0
 
-#include <boo/boo.hpp>
-#include <boo/graphicsdev/GL.hpp>
-#include <boo/audiodev/IAudioVoiceEngine.hpp>
+#include "IMain.hpp"
 #include "CMemory.hpp"
 #include "CTweaks.hpp"
 #include "CPlayMovie.hpp"
@@ -35,30 +33,18 @@
 #include "Audio/CAudioStateWin.hpp"
 #include "GameGlobalObjects.hpp"
 #include "CArchitectureQueue.hpp"
-#include "MP1.hpp"
 #include "CTimeProvider.hpp"
 #include "GuiSys/CTextExecuteBuffer.hpp"
-
 #include "DataSpec/DNAMP1/Tweaks/CTweakPlayer.hpp"
 #include "DataSpec/DNAMP1/Tweaks/CTweakGame.hpp"
 
 namespace urde
 {
-class CStopwatch;
 class IFactory;
 class IObjectStore;
 
 namespace MP1
 {
-
-enum class EGameplayResult
-{
-    None,
-    Win,
-    Lose,
-    Playing
-};
-
 class CGameGlobalObjects
 {
     CMemoryCardSys x0_memoryCardSys;
@@ -159,9 +145,9 @@ public:
 };
 
 #if MP1_USE_BOO
-class CMain : public boo::IApplicationCallback
+class CMain : public boo::IApplicationCallback, public IMain
 #else
-class CMain
+class CMain : public IMain
 #endif
 {
 #if MP1_USE_BOO
@@ -177,17 +163,6 @@ class CMain
         fprintf(stderr, "\n");
     }
 #endif
-public:
-    enum class FlowState
-    {
-        Zero,
-        One,
-        Two,
-        Three,
-        Four,
-        Five,
-        Six,
-    };
 private:
 
     struct BooSetter
@@ -206,7 +181,7 @@ private:
     CGameGlobalObjects x128_globalObjects;
     std::unique_ptr<CGameArchitectureSupport> m_archSupport;
 
-    FlowState x12c_ = FlowState::Five;
+    EFlowState x12c_flowState = EFlowState::Five;
 
     u32 x130_[10] = { 1000000 };
 
@@ -239,7 +214,7 @@ public:
           boo::ITextureR* spareTex);
     void RegisterResourceTweaks();
     void ResetGameState();
-    void StreamNewGameState(CInputStream&);
+    void StreamNewGameState(CInputStream&) {}
     void CheckTweakManagerDebugOptions() {}
 
     //int RsMain(int argc, const boo::SystemChar* argv[]);
@@ -250,17 +225,17 @@ public:
     void Draw();
     void Shutdown();
 
-    bool CheckReset();
+    bool CheckReset() { return false; }
     bool CheckTerminate() {return false;}
-    void DrawDebugMetrics(double, CStopwatch&) {}
+    void DrawDebugMetrics(double, CStopWatch&) {}
     void DoPredrawMetrics() {}
     void FillInAssetIDs();
     void LoadAudio();
-    void ShutdownSubsystems();
+    void ShutdownSubsystems() {}
     EGameplayResult GetGameplayResult() const {return xe4_gameplayResult;}
     void SetGameplayResult(EGameplayResult wl) {xe4_gameplayResult = wl;}
 
-    FlowState GetFlowState() const { return x12c_; }
+    EFlowState GetFlowState() const { return x12c_flowState; }
 };
 
 }
diff --git a/Runtime/World/CAi.cpp b/Runtime/World/CAi.cpp
index be7cfe400..2311005f1 100644
--- a/Runtime/World/CAi.cpp
+++ b/Runtime/World/CAi.cpp
@@ -1,6 +1,7 @@
 #include "CAi.hpp"
 #include "Character/CModelData.hpp"
 #include "CStateManager.hpp"
+#include "CStateMachine.hpp"
 
 namespace urde
 {
@@ -33,6 +34,8 @@ CAiTriggerFunc CAi::GetTrigerFunc(const char* func)
 {
     return m_FuncMap->GetTriggerFunc(func);
 }
+
+const CStateMachine*CAi::GetStateMachine() const { return x2c8_stateMachine.GetObj(); }
 void CAi::CreateFuncLookup(CAiFuncMap* funcMap)
 {
     m_FuncMap = funcMap;
diff --git a/Runtime/World/CAi.hpp b/Runtime/World/CAi.hpp
index f7b92b59a..b4df4e9ad 100644
--- a/Runtime/World/CAi.hpp
+++ b/Runtime/World/CAi.hpp
@@ -37,7 +37,7 @@ public:
     static CAiStateFunc GetStateFunc(const char* func);
     static CAiTriggerFunc GetTrigerFunc(const char* func);
 
-    void GetStateMachine() {}
+    const CStateMachine* GetStateMachine() const;
 
     virtual void AcceptScriptMsg(EScriptObjectMessage, TUniqueId, CStateManager&) {}
     virtual CHealthInfo HealthInfo(CStateManager&) { return x258_healthInfo; }