Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
user-grinch committed May 12, 2022
1 parent bf8ae12 commit 3453b55
Show file tree
Hide file tree
Showing 19 changed files with 908 additions and 864 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"${PLUGIN_SDK_DIR}/plugin_sa/*",
"${PLUGIN_SDK_DIR}/plugin_sa/game_sa/*",
"${PLUGIN_SDK_DIR}/shared/*",
"${PLUGIN_SDK_DIR}/shared/game/*",
"${PLUGIN_SDK_DIR}/shared/game/*"
],
"defines": [
"_DEBUG",
Expand Down
2 changes: 1 addition & 1 deletion resource/MapEditor/json/favourites.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"All": {
"Custom": {
"1344 - CJ_Dumpster2": "1344",
"5490 - laeroad25": "5490",
"6136 - gaz15_law": "6136"
Expand Down
13 changes: 6 additions & 7 deletions src/d3dhook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void D3dHook::ProcessFrame(void* ptr)
ImVec2 size(screen::GetScreenWidth(), screen::GetScreenHeight());
if ((fScreenSize.x != size.x && fScreenSize.y != size.y) || FontMgr::IsReloadNeeded())
{
if (gRenderer == Render_DirectX9)
if (gRenderer == eRenderer::DirectX9)
{
ImGui_ImplDX9_InvalidateDeviceObjects();
}
Expand All @@ -85,7 +85,7 @@ void D3dHook::ProcessFrame(void* ptr)
}

ImGui_ImplWin32_NewFrame();
if (gRenderer == Render_DirectX9)
if (gRenderer == eRenderer::DirectX9)
{
ImGui_ImplDX9_NewFrame();
}
Expand All @@ -104,7 +104,7 @@ void D3dHook::ProcessFrame(void* ptr)
ImGui::EndFrame();
ImGui::Render();

if (gRenderer == Render_DirectX9)
if (gRenderer == eRenderer::DirectX9)
{
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
}
Expand All @@ -122,7 +122,7 @@ void D3dHook::ProcessFrame(void* ptr)
patch::Nop(0x00531155, 5); // shift trigger fix
#endif

if (gRenderer == Render_DirectX9)
if (gRenderer == eRenderer::DirectX9)
{
ImGui_ImplDX9_Init(reinterpret_cast<IDirect3DDevice9*>(ptr));
}
Expand Down Expand Up @@ -216,7 +216,6 @@ void D3dHook::ProcessMouse()
}
else
{

patch::SetUChar(BY_GAME(0x6194A0, 0x6020A0, 0x580D20), BY_GAME(0xE9, 0x53, 0x53));
#ifdef GTASA
patch::SetRaw(0x541DD7, (char*)"\xE8\xE4\xD5\xFF\xFF", 5);
Expand Down Expand Up @@ -256,7 +255,7 @@ bool D3dHook::InjectHook(void *pCallback)
*/
if (init(kiero::RenderType::D3D9) == kiero::Status::Success)
{
gRenderer = Render_DirectX9;
gRenderer = eRenderer::DirectX9;
kiero::bind(16, (void**)&oReset, hkReset);
kiero::bind(42, (void**)&oEndScene, hkEndScene);
pCallbackFunc = pCallback;
Expand All @@ -267,7 +266,7 @@ bool D3dHook::InjectHook(void *pCallback)

if (init(kiero::RenderType::D3D11) == kiero::Status::Success)
{
gRenderer = Render_DirectX11;
gRenderer = eRenderer::DirectX11;
kiero::bind(8, (void**)&oPresent, hkPresent);
pCallbackFunc = pCallback;
hookInjected = true;
Expand Down
12 changes: 7 additions & 5 deletions src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ void EditorThread(void* param)
{
ObjManager::Init();
Updater::CheckUpdate();
/*
Wait for game init
Doing it like this doesn't prevent you to attach debugger
*/

/*
Wait for game init
Sleep() bugs when debugger is attached
*/
static bool gameStarted = false;
Events::processScriptsEvent +=[]{
Events::initGameEvent +=[]
{
gameStarted = true;
};

Expand Down
70 changes: 21 additions & 49 deletions src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,27 @@
#include <CMenuManager.h>
#include "filemgr.h"

bool Editor::IsOpen()
{
return m_bOpened;
}

void Editor::Init()
{
D3dHook::InjectHook(&DrawWindow);
D3dHook::InjectHook(&Draw);
ApplyStyle();
Updater::CheckUpdate();

// Load config data
Interface::m_bAutoSave = gConfig.GetValue("editor.autoSave", true);
Interface::m_bAutoTpToLoc = gConfig.GetValue("editor.autoTpToLoc", false);
Interface::m_bAutoSnapToGround = gConfig.GetValue("editor.autoSnap", true);
Interface::m_bShowInfoMenu = gConfig.GetValue("editor.showInfoMenu", true);
Interface::m_bFramerate = gConfig.GetValue("editor.showFPS", false);
ObjManager::m_bDrawBoundingBox = gConfig.GetValue("editor.drawBoundingBox", true);
ObjManager::m_bDrawAxisLines = gConfig.GetValue("editor.drawAxisLines", true);
Viewport::m_bShowHoverMenu = gConfig.GetValue("editor.showHoverMenu", true);
Interface::m_bWelcomeScreenDisplayed = gConfig.GetValue("editor.welcomeDisplayed", false);
Viewport::m_nMul = gConfig.GetValue("editor.moveSpeed", 1.0f);
Viewport::m_nMoveSpeed = gConfig.GetValue("editor.moveSpeed", 1.0f);
FontMgr::SetMultiplier(gConfig.GetValue("editor.fontMul", 1.0f));

if (!Interface::m_bWelcomeScreenDisplayed)
{
Interface::m_bShowPopup = true;
Interface::m_popupTitle = "Map Editor";
Interface::m_pPopupFunc = Interface::WelcomeMenu;
Interface::m_bWelcomeScreenDisplayed = true;
gConfig.SetValue("editor.welcomeDisplayed", Interface::m_bWelcomeScreenDisplayed);
}
Interface::Init();

Events::processScriptsEvent += []()
{
if (Editor::m_bShowEditor)
{
Viewport::Process();
}

if (toggleUIKey.Pressed())
{
Editor::m_bShowGUI = !Editor::m_bShowGUI;
Interface::Interface::m_bShowGUI = !Interface::Interface::m_bShowGUI;
}

if (copyHoveredObjName.Pressed())
Expand All @@ -56,11 +39,11 @@ void Editor::Init()
CHud::SetHelpMessage("Copied to clipboard", false, false, false);
}

if (editorOpenKey.Pressed() && !Interface::m_bIsInputLocked)
if (editorOpenKey.Pressed() && !Interface::m_bInputLocked)
{
Editor::m_bShowEditor = !Editor::m_bShowEditor;
m_bOpened = !m_bOpened;

if (Editor::m_bShowEditor)
if (m_bOpened)
{
if (Interface::m_bAutoTpToLoc)
{
Expand All @@ -84,45 +67,32 @@ void Editor::Init()

Events::shutdownRwEvent += []()
{
gConfig.WriteToDisk();
gConfig.WriteToDisk();
};
};

void Editor::Cleanup()
{
Viewport::m_eViewportMode = EDIT_MODE;
Interface::Browser::m_bShown = false;
Interface::Browser::m_bShowNextFrame = false;
D3dHook::SetMouseState(false);
gConfig.WriteToDisk();
Viewport::Shutdown();
Viewport::Cleanup();
}

void Editor::DrawWindow()
void Editor::Draw()
{
static bool bTriedtoHideCursor;
if (!FrontEndMenuManager.m_bMenuActive)
{
if (Editor::m_bShowEditor)
if (m_bOpened)
{
if (Viewport::m_eViewportMode == EDIT_MODE)
if (Viewport::m_eState == eViewportState::Edit)
{
D3dHook::SetMouseState(true);
bTriedtoHideCursor = false;
}
Interface::m_bIsInputLocked = false;
Interface::m_bInputLocked = false;

if (m_bShowGUI)
{
Interface::DrawMainMenuBar();
Viewport::DrawOverlay();
Viewport::ProcessSelectedObjectInputs();
Interface::DrawPopupMenu();
Interface::DrawInfoMenu();
Viewport::DrawHoverMenu();
}

if (Interface::m_bAutoSave && ObjManager::m_pVecEntities.size() > 0)
if (Interface::m_bAutoSave && ObjManager::m_pPlacedObjs.size() > 0)
{
static size_t timer = CTimer::m_snTimeInMilliseconds;
size_t curTimer = CTimer::m_snTimeInMilliseconds;
Expand All @@ -133,6 +103,8 @@ void Editor::DrawWindow()
timer = curTimer;
}
}
Interface::Process();
Viewport::Process();
}
}
else
Expand Down
7 changes: 3 additions & 4 deletions src/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ class Editor
{
private:
static void ApplyStyle();
static inline bool m_bOpened; // Is the editor being shown

public:
static inline bool m_bShowEditor;
static inline bool m_bShowGUI = true;

Editor() = delete;
Editor(Editor&) = delete;

static void Init();
static void CheckForUpdate();
static void DrawWindow();
static bool IsOpen();
static void Draw();
static void Cleanup();
};
8 changes: 4 additions & 4 deletions src/filemgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void FileMgr::ImportIDE(std::string path, bool logImports)
}

ObjManager::m_vecModelNames.push_back({dirEntry.path().stem().string(), std::move(temp)});
ObjManager::totalIDELinesLoaded++;
ObjManager::m_nTotalIDELine++;
}
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ void FileMgr::ImportIPL(std::string fileName, bool logImports)
gLog << "Pasing line: " << line << std::endl;
}

int hObj;
int hObj;
Command<Commands::REQUEST_MODEL>(model);
Command<Commands::LOAD_ALL_MODELS_NOW>();
Command<Commands::CREATE_OBJECT>(model, pos.x, pos.y, pos.z, &hObj);
Expand Down Expand Up @@ -108,7 +108,7 @@ void FileMgr::ImportIPL(std::string fileName, bool logImports)
// Setting quat messes with z coord?
Command<Commands::SET_OBJECT_COORDINATES>(hObj, pos.x, pos.y, pos.z);
Command<Commands::MARK_MODEL_AS_NO_LONGER_NEEDED>(model);
ObjManager::m_pVecEntities.push_back(CPools::GetObject(hObj));
ObjManager::m_pPlacedObjs.push_back(CPools::GetObject(hObj));
}
}
CHud::SetHelpMessage("IPL imported", false, false, false);
Expand All @@ -119,7 +119,7 @@ void FileMgr::ExportIPL(const char* fileName)
std::fstream file;
file.open(std::string(PLUGIN_PATH((char*)"MapEditor/")) + fileName, std::ios::out);
file << "# Generated using Map Editor by Grinch_\ninst" << std::endl;
for (CObject *pObj : ObjManager::m_pVecEntities)
for (CObject *pObj : ObjManager::m_pPlacedObjs)
{
int model = pObj->m_nModelIndex;
auto &data = ObjManager::m_objData.Get(pObj);
Expand Down
2 changes: 1 addition & 1 deletion src/fontmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FontMgr
};
static inline std::vector<FontInfo> m_vecFonts;
static inline bool m_bMulChangedExternal;

public:
FontMgr() = delete;
FontMgr(FontMgr&) = delete;
Expand Down
Loading

0 comments on commit 3453b55

Please sign in to comment.