From 76435d803f768be8da9821d72b98306a4b043ad2 Mon Sep 17 00:00:00 2001
From: Christian Semmler <mail@csemmler.com>
Date: Thu, 30 May 2024 15:03:43 -0400
Subject: [PATCH] Style refactor omni/system components (#974)

* Style refactor omni/system components

* Fix

* Fix
---
 CMakeLists.txt                              |  1 +
 LEGO1/omni/include/mxautolock.h             |  2 +-
 LEGO1/omni/include/mxcriticalsection.h      |  2 +
 LEGO1/omni/include/mxscheduler.h            |  1 +
 LEGO1/omni/include/mxsemaphore.h            |  6 +--
 LEGO1/omni/include/mxthread.h               | 15 -------
 LEGO1/omni/include/mxticklethread.h         | 21 ++++++++++
 LEGO1/omni/src/audio/mxmusicmanager.cpp     |  2 +-
 LEGO1/omni/src/audio/mxsoundmanager.cpp     |  2 +-
 LEGO1/omni/src/event/mxeventmanager.cpp     |  2 +-
 LEGO1/omni/src/system/mxautolock.cpp        | 11 +++---
 LEGO1/omni/src/system/mxcriticalsection.cpp | 44 ++++++++++-----------
 LEGO1/omni/src/system/mxsemaphore.cpp       |  2 +
 LEGO1/omni/src/system/mxthread.cpp          | 36 +----------------
 LEGO1/omni/src/system/mxticklethread.cpp    | 41 +++++++++++++++++++
 LEGO1/omni/src/video/mxvideomanager.cpp     |  2 +-
 16 files changed, 106 insertions(+), 84 deletions(-)
 create mode 100644 LEGO1/omni/include/mxticklethread.h
 create mode 100644 LEGO1/omni/src/system/mxticklethread.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 58d2b662..cc660ede 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -259,6 +259,7 @@ add_library(omni STATIC
   LEGO1/omni/src/system/mxscheduler.cpp
   LEGO1/omni/src/system/mxsemaphore.cpp
   LEGO1/omni/src/system/mxthread.cpp
+  LEGO1/omni/src/system/mxticklethread.cpp
   LEGO1/omni/src/video/flic.cpp
   LEGO1/omni/src/video/mxbitmap.cpp
   LEGO1/omni/src/video/mxdisplaysurface.cpp
diff --git a/LEGO1/omni/include/mxautolock.h b/LEGO1/omni/include/mxautolock.h
index 677804e7..8a2b3c35 100644
--- a/LEGO1/omni/include/mxautolock.h
+++ b/LEGO1/omni/include/mxautolock.h
@@ -11,7 +11,7 @@ public:
 	~MxAutoLock();
 
 private:
-	MxCriticalSection* m_criticalSection;
+	MxCriticalSection* m_criticalSection; // 0x00
 };
 
 #endif // MXAUTOLOCK_H
diff --git a/LEGO1/omni/include/mxcriticalsection.h b/LEGO1/omni/include/mxcriticalsection.h
index 011ac680..23c3fb76 100644
--- a/LEGO1/omni/include/mxcriticalsection.h
+++ b/LEGO1/omni/include/mxcriticalsection.h
@@ -8,7 +8,9 @@ class MxCriticalSection {
 public:
 	MxCriticalSection();
 	~MxCriticalSection();
+
 	static void SetDoMutex();
+
 	void Enter();
 	void Leave();
 
diff --git a/LEGO1/omni/include/mxscheduler.h b/LEGO1/omni/include/mxscheduler.h
index 1f60029d..fbd972e8 100644
--- a/LEGO1/omni/include/mxscheduler.h
+++ b/LEGO1/omni/include/mxscheduler.h
@@ -6,6 +6,7 @@
 class MxScheduler {
 public:
 	static MxScheduler* GetInstance();
+
 	void StartMultiTasking(MxULong);
 };
 
diff --git a/LEGO1/omni/include/mxsemaphore.h b/LEGO1/omni/include/mxsemaphore.h
index 4e692395..a9c5f9d2 100644
--- a/LEGO1/omni/include/mxsemaphore.h
+++ b/LEGO1/omni/include/mxsemaphore.h
@@ -1,5 +1,5 @@
-#ifndef MX_SEMAPHORE_H
-#define MX_SEMAPHORE_H
+#ifndef MXSEMAPHORE_H
+#define MXSEMAPHORE_H
 
 #include "mxtypes.h"
 
@@ -23,4 +23,4 @@ private:
 	HANDLE m_hSemaphore; // 0x04
 };
 
-#endif // MX_SEMAPHORE_H
+#endif // MXSEMAPHORE_H
diff --git a/LEGO1/omni/include/mxthread.h b/LEGO1/omni/include/mxthread.h
index 6016aa8f..5f04a26c 100644
--- a/LEGO1/omni/include/mxthread.h
+++ b/LEGO1/omni/include/mxthread.h
@@ -42,19 +42,4 @@ protected:
 	MxCore* m_target; // 0x18
 };
 
-// VTABLE: LEGO1 0x100dc6d8
-// SIZE 0x20
-class MxTickleThread : public MxThread {
-public:
-	MxTickleThread(MxCore* p_target, MxS32 p_frequencyMS);
-
-	MxResult Run() override;
-
-	// SYNTHETIC: LEGO1 0x100b8c20
-	// MxTickleThread::`scalar deleting destructor'
-
-private:
-	MxS32 m_frequencyMS; // 0x1c
-};
-
 #endif // MXTHREAD_H
diff --git a/LEGO1/omni/include/mxticklethread.h b/LEGO1/omni/include/mxticklethread.h
new file mode 100644
index 00000000..02b495ce
--- /dev/null
+++ b/LEGO1/omni/include/mxticklethread.h
@@ -0,0 +1,21 @@
+#ifndef MXTICKLETHREAD_H
+#define MXTICKLETHREAD_H
+
+#include "mxthread.h"
+
+// VTABLE: LEGO1 0x100dc6d8
+// SIZE 0x20
+class MxTickleThread : public MxThread {
+public:
+	MxTickleThread(MxCore* p_target, MxS32 p_frequencyMS);
+
+	MxResult Run() override;
+
+	// SYNTHETIC: LEGO1 0x100b8c20
+	// MxTickleThread::`scalar deleting destructor'
+
+private:
+	MxS32 m_frequencyMS; // 0x1c
+};
+
+#endif // MXTICKLETHREAD_H
diff --git a/LEGO1/omni/src/audio/mxmusicmanager.cpp b/LEGO1/omni/src/audio/mxmusicmanager.cpp
index fa8bdcbe..94c56f04 100644
--- a/LEGO1/omni/src/audio/mxmusicmanager.cpp
+++ b/LEGO1/omni/src/audio/mxmusicmanager.cpp
@@ -1,8 +1,8 @@
 #include "mxmusicmanager.h"
 
 #include "mxmisc.h"
-#include "mxthread.h"
 #include "mxticklemanager.h"
+#include "mxticklethread.h"
 
 #include <windows.h>
 
diff --git a/LEGO1/omni/src/audio/mxsoundmanager.cpp b/LEGO1/omni/src/audio/mxsoundmanager.cpp
index 33dd2bad..d5d8bd72 100644
--- a/LEGO1/omni/src/audio/mxsoundmanager.cpp
+++ b/LEGO1/omni/src/audio/mxsoundmanager.cpp
@@ -5,8 +5,8 @@
 #include "mxmisc.h"
 #include "mxomni.h"
 #include "mxpresenter.h"
-#include "mxthread.h"
 #include "mxticklemanager.h"
+#include "mxticklethread.h"
 #include "mxwavepresenter.h"
 
 DECOMP_SIZE_ASSERT(MxSoundManager, 0x3c);
diff --git a/LEGO1/omni/src/event/mxeventmanager.cpp b/LEGO1/omni/src/event/mxeventmanager.cpp
index 7e269ce6..43f1e045 100644
--- a/LEGO1/omni/src/event/mxeventmanager.cpp
+++ b/LEGO1/omni/src/event/mxeventmanager.cpp
@@ -2,8 +2,8 @@
 
 #include "mxcriticalsection.h"
 #include "mxmisc.h"
-#include "mxthread.h"
 #include "mxticklemanager.h"
+#include "mxticklethread.h"
 
 // FUNCTION: LEGO1 0x100c0360
 MxEventManager::MxEventManager()
diff --git a/LEGO1/omni/src/system/mxautolock.cpp b/LEGO1/omni/src/system/mxautolock.cpp
index a67c382c..5ab2e4bb 100644
--- a/LEGO1/omni/src/system/mxautolock.cpp
+++ b/LEGO1/omni/src/system/mxautolock.cpp
@@ -5,16 +5,17 @@
 // FUNCTION: LEGO1 0x100b8ed0
 MxAutoLock::MxAutoLock(MxCriticalSection* p_criticalSection)
 {
-	this->m_criticalSection = p_criticalSection;
-	if (this->m_criticalSection != 0) {
-		this->m_criticalSection->Enter();
+	m_criticalSection = p_criticalSection;
+
+	if (m_criticalSection != NULL) {
+		m_criticalSection->Enter();
 	}
 }
 
 // FUNCTION: LEGO1 0x100b8ef0
 MxAutoLock::~MxAutoLock()
 {
-	if (this->m_criticalSection != 0) {
-		this->m_criticalSection->Leave();
+	if (m_criticalSection != NULL) {
+		m_criticalSection->Leave();
 	}
 }
diff --git a/LEGO1/omni/src/system/mxcriticalsection.cpp b/LEGO1/omni/src/system/mxcriticalsection.cpp
index 1038f7ad..f0adc551 100644
--- a/LEGO1/omni/src/system/mxcriticalsection.cpp
+++ b/LEGO1/omni/src/system/mxcriticalsection.cpp
@@ -4,35 +4,35 @@
 
 #include <stdio.h>
 
-DECOMP_SIZE_ASSERT(MxCriticalSection, 0x1c);
+DECOMP_SIZE_ASSERT(MxCriticalSection, 0x1c)
 
 // GLOBAL: LEGO1 0x10101e78
-int g_useMutex = 0;
+BOOL g_useMutex = FALSE;
 
 // FUNCTION: LEGO1 0x100b6d20
 MxCriticalSection::MxCriticalSection()
 {
 	HANDLE mutex;
 
-	if (g_useMutex != 0) {
+	if (g_useMutex) {
 		mutex = CreateMutexA(NULL, FALSE, NULL);
-		this->m_mutex = mutex;
-		return;
+		m_mutex = mutex;
+	}
+	else {
+		InitializeCriticalSection(&m_criticalSection);
+		m_mutex = NULL;
 	}
-
-	InitializeCriticalSection(&this->m_criticalSection);
-	this->m_mutex = NULL;
 }
 
 // FUNCTION: LEGO1 0x100b6d60
 MxCriticalSection::~MxCriticalSection()
 {
-	if (this->m_mutex != NULL) {
-		CloseHandle(this->m_mutex);
-		return;
+	if (m_mutex != NULL) {
+		CloseHandle(m_mutex);
+	}
+	else {
+		DeleteCriticalSection(&m_criticalSection);
 	}
-
-	DeleteCriticalSection(&this->m_criticalSection);
 }
 
 // FUNCTION: LEGO1 0x100b6d80
@@ -41,8 +41,8 @@ void MxCriticalSection::Enter()
 	DWORD result;
 	FILE* file;
 
-	if (this->m_mutex != NULL) {
-		result = WaitForSingleObject(this->m_mutex, 5000);
+	if (m_mutex != NULL) {
+		result = WaitForSingleObject(m_mutex, 5000);
 		if (result == WAIT_FAILED) {
 			file = fopen("C:\\DEADLOCK.TXT", "a");
 			if (file != NULL) {
@@ -54,23 +54,23 @@ void MxCriticalSection::Enter()
 		}
 	}
 	else {
-		EnterCriticalSection(&this->m_criticalSection);
+		EnterCriticalSection(&m_criticalSection);
 	}
 }
 
 // FUNCTION: LEGO1 0x100b6de0
 void MxCriticalSection::Leave()
 {
-	if (this->m_mutex != NULL) {
-		ReleaseMutex(this->m_mutex);
-		return;
+	if (m_mutex != NULL) {
+		ReleaseMutex(m_mutex);
+	}
+	else {
+		LeaveCriticalSection(&m_criticalSection);
 	}
-
-	LeaveCriticalSection(&this->m_criticalSection);
 }
 
 // FUNCTION: LEGO1 0x100b6e00
 void MxCriticalSection::SetDoMutex()
 {
-	g_useMutex = 1;
+	g_useMutex = TRUE;
 }
diff --git a/LEGO1/omni/src/system/mxsemaphore.cpp b/LEGO1/omni/src/system/mxsemaphore.cpp
index daba60f4..5c8b2a1f 100644
--- a/LEGO1/omni/src/system/mxsemaphore.cpp
+++ b/LEGO1/omni/src/system/mxsemaphore.cpp
@@ -15,9 +15,11 @@ MxSemaphore::MxSemaphore()
 MxResult MxSemaphore::Init(MxU32 p_initialCount, MxU32 p_maxCount)
 {
 	MxResult result = FAILURE;
+
 	if ((m_hSemaphore = CreateSemaphoreA(NULL, p_initialCount, p_maxCount, NULL))) {
 		result = SUCCESS;
 	}
+
 	return result;
 }
 
diff --git a/LEGO1/omni/src/system/mxthread.cpp b/LEGO1/omni/src/system/mxthread.cpp
index fe327f2d..b0bd5b51 100644
--- a/LEGO1/omni/src/system/mxthread.cpp
+++ b/LEGO1/omni/src/system/mxthread.cpp
@@ -1,44 +1,10 @@
 #include "mxthread.h"
 
 #include "decomp.h"
-#include "mxmisc.h"
-#include "mxtimer.h"
 
 #include <process.h>
 
 DECOMP_SIZE_ASSERT(MxThread, 0x1c)
-DECOMP_SIZE_ASSERT(MxTickleThread, 0x20)
-
-// FUNCTION: LEGO1 0x100b8bb0
-MxTickleThread::MxTickleThread(MxCore* p_target, MxS32 p_frequencyMS)
-{
-	m_target = p_target;
-	m_frequencyMS = p_frequencyMS;
-}
-
-// Match except for register allocation
-// FUNCTION: LEGO1 0x100b8c90
-MxResult MxTickleThread::Run()
-{
-	MxTimer* timer = Timer();
-	MxS32 lastTickled = -m_frequencyMS;
-	while (IsRunning()) {
-		MxLong currentTime = timer->GetTime();
-
-		if (currentTime < lastTickled) {
-			lastTickled = -m_frequencyMS;
-		}
-
-		MxS32 timeRemainingMS = (m_frequencyMS - currentTime) + lastTickled;
-		if (timeRemainingMS <= 0) {
-			m_target->Tickle();
-			timeRemainingMS = 0;
-			lastTickled = currentTime;
-		}
-		Sleep(timeRemainingMS);
-	}
-	return MxThread::Run();
-}
 
 // FUNCTION: LEGO1 0x100bf510
 MxThread::MxThread()
@@ -62,12 +28,14 @@ typedef unsigned(__stdcall* ThreadFunc)(void*);
 MxResult MxThread::Start(MxS32 p_stack, MxS32 p_flag)
 {
 	MxResult result = FAILURE;
+
 	if (m_semaphore.Init(0, 1) == SUCCESS) {
 		if ((m_hThread =
 				 _beginthreadex(NULL, p_stack << 2, (ThreadFunc) &MxThread::ThreadProc, this, p_flag, &m_threadId))) {
 			result = SUCCESS;
 		}
 	}
+
 	return result;
 }
 
diff --git a/LEGO1/omni/src/system/mxticklethread.cpp b/LEGO1/omni/src/system/mxticklethread.cpp
new file mode 100644
index 00000000..779e0cd8
--- /dev/null
+++ b/LEGO1/omni/src/system/mxticklethread.cpp
@@ -0,0 +1,41 @@
+#include "mxticklethread.h"
+
+#include "decomp.h"
+#include "mxmisc.h"
+#include "mxtimer.h"
+
+DECOMP_SIZE_ASSERT(MxTickleThread, 0x20)
+
+// FUNCTION: LEGO1 0x100b8bb0
+MxTickleThread::MxTickleThread(MxCore* p_target, MxS32 p_frequencyMS)
+{
+	m_target = p_target;
+	m_frequencyMS = p_frequencyMS;
+}
+
+// Match except for register allocation
+// FUNCTION: LEGO1 0x100b8c90
+MxResult MxTickleThread::Run()
+{
+	MxTimer* timer = Timer();
+	MxS32 lastTickled = -m_frequencyMS;
+
+	while (IsRunning()) {
+		MxLong currentTime = timer->GetTime();
+
+		if (currentTime < lastTickled) {
+			lastTickled = -m_frequencyMS;
+		}
+
+		MxS32 timeRemainingMS = (m_frequencyMS - currentTime) + lastTickled;
+		if (timeRemainingMS <= 0) {
+			m_target->Tickle();
+			timeRemainingMS = 0;
+			lastTickled = currentTime;
+		}
+
+		Sleep(timeRemainingMS);
+	}
+
+	return MxThread::Run();
+}
diff --git a/LEGO1/omni/src/video/mxvideomanager.cpp b/LEGO1/omni/src/video/mxvideomanager.cpp
index f3bf04fd..3c57ac52 100644
--- a/LEGO1/omni/src/video/mxvideomanager.cpp
+++ b/LEGO1/omni/src/video/mxvideomanager.cpp
@@ -7,8 +7,8 @@
 #include "mxpalette.h"
 #include "mxpresenter.h"
 #include "mxregion.h"
-#include "mxthread.h"
 #include "mxticklemanager.h"
+#include "mxticklethread.h"
 
 DECOMP_SIZE_ASSERT(MxVideoManager, 0x64)