From 8761220deba955b4a71d0949accbb5a87526f3be Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Tue, 19 Jun 2012 21:15:56 -0300 Subject: [PATCH] Refactoring and flexibility changes * Split game module into game and game_interface * Move core_lib to corelib * Move miniwindow to corelib * Introduce init.lua script for initializing the client, giving much more flexibility * OTClient is no longer Application derived and is much simpler --- CMakeLists.txt | 4 +- init.lua | 48 +++++++ modules/client/client.lua | 9 +- modules/client/client.otmod | 2 - modules/client_locales/locales.lua | 8 +- modules/client_skins/skins.lua | 13 +- .../skins/default}/images/bottompanel.png | Bin .../skins/default}/images/item.png | Bin .../skins/default}/images/logout.png | Bin .../skins/default}/images/mappanel.png | Bin .../skins/default}/images/miniwindow.png | Bin .../default}/images/miniwindowbuttons.png | Bin .../skins/default}/images/sidepanel.png | Bin modules/{core_lib => corelib}/const.lua | 0 .../core_lib.otmod => corelib/corelib.otmod} | 4 +- modules/{core_lib => corelib}/ext/os.lua | 0 modules/{core_lib => corelib}/ext/string.lua | 0 modules/{core_lib => corelib}/ext/table.lua | 0 modules/{core_lib => corelib}/globals.lua | 0 modules/{core_lib => corelib}/keyboard.lua | 18 +-- modules/{core_lib => corelib}/math/color.lua | 0 modules/{core_lib => corelib}/math/math.lua | 0 modules/{core_lib => corelib}/math/point.lua | 0 modules/{core_lib => corelib}/math/rect.lua | 0 modules/{core_lib => corelib}/math/size.lua | 0 modules/{core_lib => corelib}/mouse.lua | 0 modules/{core_lib => corelib}/settings.lua | 0 modules/{core_lib => corelib}/ui/effects.lua | 0 .../{core_lib => corelib}/ui/radiogroup.lua | 0 modules/{core_lib => corelib}/ui/tooltip.lua | 2 +- modules/{core_lib => corelib}/util.lua | 17 ++- .../widgets/uibutton.lua | 0 .../widgets/uicheckbox.lua | 0 .../widgets/uicombobox.lua | 0 .../{core_lib => corelib}/widgets/uilabel.lua | 0 .../widgets/uimessagebox.lua | 0 .../widgets/uiminiwindow.lua | 0 .../widgets/uiminiwindowcontainer.lua | 0 .../widgets/uipopupmenu.lua | 0 .../widgets/uiprogressbar.lua | 0 .../widgets/uiresizeborder.lua | 0 .../widgets/uiscrollarea.lua | 0 .../widgets/uiscrollbar.lua | 0 .../widgets/uispinbox.lua | 0 .../widgets/uisplitter.lua | 0 .../widgets/uitabbar.lua | 0 .../widgets/uiwidget.lua | 0 .../widgets/uiwindow.lua | 0 modules/game/game.otmod | 22 +--- modules/game_battle/battle.otmod | 2 +- modules/game_bugreport/bugreport.otmod | 2 +- .../game_combatcontrols/combatcontrols.otmod | 2 +- modules/game_console/console.lua | 8 +- modules/game_console/console.otmod | 2 +- modules/game_containers/container.otui | 2 +- modules/game_containers/containers.otmod | 2 +- modules/game_healthbar/healthbar.otmod | 2 +- modules/game_hotkeys/hotkeys_manager.otmod | 2 +- .../gameinterface.lua | 2 +- .../gameinterface.otui | 6 +- modules/game_interface/interface.otmod | 21 +++ .../styles/countwindow.otui | 0 .../styles/creatures.otui | 0 .../styles/items.otui | 2 +- .../styles/miniwindow.otui | 6 +- .../widgets/uigamemap.lua | 0 .../widgets/uiitem.lua | 0 modules/game_inventory/inventory.otmod | 2 +- modules/game_minimap/minimap.otmod | 2 +- modules/game_npctrade/npctrade.otmod | 2 +- modules/game_outfit/outfit.otmod | 2 +- modules/game_playertrade/playertrade.otmod | 2 +- modules/game_questlog/questlog.otmod | 2 +- .../game_ruleviolation/ruleviolation.otmod | 2 +- modules/game_shaders/shaders.otmod | 2 +- modules/game_skills/skills.otmod | 2 +- modules/game_textbooks/textbooks.otmod | 2 +- modules/game_textmessage/textmessage.otmod | 2 +- src/framework/CMakeLists.txt | 9 +- src/framework/application.cpp | 58 ++++----- src/framework/application.h | 33 +++-- src/framework/core/configmanager.cpp | 2 +- src/framework/core/eventdispatcher.h | 2 +- src/framework/core/logger.cpp | 5 + src/framework/core/module.cpp | 2 +- src/framework/core/modulemanager.cpp | 38 ------ src/framework/core/modulemanager.h | 3 - src/framework/core/resourcemanager.cpp | 96 +++++++++++--- src/framework/core/resourcemanager.h | 7 + src/framework/graphics/framebuffer.cpp | 2 +- src/framework/graphics/hardwarebuffer.cpp | 2 +- src/framework/graphics/shader.cpp | 2 +- src/framework/graphics/shaderprogram.cpp | 2 +- src/framework/graphics/texture.cpp | 2 +- src/framework/luafunctions.cpp | 76 ++++++----- src/framework/luascript/luainterface.cpp | 13 +- src/framework/luascript/luainterface.h | 3 + src/framework/luascript/luaobject.cpp | 2 +- src/framework/net/connection.cpp | 2 +- src/framework/platform/unixcrashhandler.cpp | 4 +- src/framework/platform/win32crashhandler.cpp | 4 +- src/framework/platform/win32window.cpp | 6 +- src/framework/ui/uimanager.cpp | 2 +- src/framework/ui/uitextedit.cpp | 4 +- src/framework/ui/uiwidget.cpp | 6 +- src/framework/ui/uiwidgettext.cpp | 2 +- src/main.cpp | 29 ++++- src/otclient/const.h | 4 - src/otclient/core/game.cpp | 2 +- src/otclient/core/map.cpp | 16 ++- src/otclient/core/map.h | 4 +- src/otclient/core/mapview.cpp | 2 +- src/otclient/luafunctions.cpp | 5 +- src/otclient/otclient.cpp | 120 ++++++------------ src/otclient/otclient.h | 6 +- 115 files changed, 446 insertions(+), 361 deletions(-) create mode 100644 init.lua rename modules/{game => client_skins/skins/default}/images/bottompanel.png (100%) rename modules/{game => client_skins/skins/default}/images/item.png (100%) rename modules/{game => client_skins/skins/default}/images/logout.png (100%) rename modules/{game => client_skins/skins/default}/images/mappanel.png (100%) rename modules/{game => client_skins/skins/default}/images/miniwindow.png (100%) rename modules/{game => client_skins/skins/default}/images/miniwindowbuttons.png (100%) rename modules/{game => client_skins/skins/default}/images/sidepanel.png (100%) rename modules/{core_lib => corelib}/const.lua (100%) rename modules/{core_lib/core_lib.otmod => corelib/corelib.otmod} (86%) rename modules/{core_lib => corelib}/ext/os.lua (100%) rename modules/{core_lib => corelib}/ext/string.lua (100%) rename modules/{core_lib => corelib}/ext/table.lua (100%) rename modules/{core_lib => corelib}/globals.lua (100%) rename modules/{core_lib => corelib}/keyboard.lua (90%) rename modules/{core_lib => corelib}/math/color.lua (100%) rename modules/{core_lib => corelib}/math/math.lua (100%) rename modules/{core_lib => corelib}/math/point.lua (100%) rename modules/{core_lib => corelib}/math/rect.lua (100%) rename modules/{core_lib => corelib}/math/size.lua (100%) rename modules/{core_lib => corelib}/mouse.lua (100%) rename modules/{core_lib => corelib}/settings.lua (100%) rename modules/{core_lib => corelib}/ui/effects.lua (100%) rename modules/{core_lib => corelib}/ui/radiogroup.lua (100%) rename modules/{core_lib => corelib}/ui/tooltip.lua (97%) rename modules/{core_lib => corelib}/util.lua (94%) rename modules/{core_lib => corelib}/widgets/uibutton.lua (100%) rename modules/{core_lib => corelib}/widgets/uicheckbox.lua (100%) rename modules/{core_lib => corelib}/widgets/uicombobox.lua (100%) rename modules/{core_lib => corelib}/widgets/uilabel.lua (100%) rename modules/{core_lib => corelib}/widgets/uimessagebox.lua (100%) rename modules/{game => corelib}/widgets/uiminiwindow.lua (100%) rename modules/{game => corelib}/widgets/uiminiwindowcontainer.lua (100%) rename modules/{core_lib => corelib}/widgets/uipopupmenu.lua (100%) rename modules/{core_lib => corelib}/widgets/uiprogressbar.lua (100%) rename modules/{core_lib => corelib}/widgets/uiresizeborder.lua (100%) rename modules/{core_lib => corelib}/widgets/uiscrollarea.lua (100%) rename modules/{core_lib => corelib}/widgets/uiscrollbar.lua (100%) rename modules/{core_lib => corelib}/widgets/uispinbox.lua (100%) rename modules/{core_lib => corelib}/widgets/uisplitter.lua (100%) rename modules/{core_lib => corelib}/widgets/uitabbar.lua (100%) rename modules/{core_lib => corelib}/widgets/uiwidget.lua (100%) rename modules/{core_lib => corelib}/widgets/uiwindow.lua (100%) rename modules/{game => game_interface}/gameinterface.lua (99%) rename modules/{game => game_interface}/gameinterface.otui (93%) create mode 100644 modules/game_interface/interface.otmod rename modules/{game => game_interface}/styles/countwindow.otui (100%) rename modules/{game => game_interface}/styles/creatures.otui (100%) rename modules/{game => game_interface}/styles/items.otui (70%) rename modules/{game => game_interface}/styles/miniwindow.otui (91%) rename modules/{game => game_interface}/widgets/uigamemap.lua (100%) rename modules/{game => game_interface}/widgets/uiitem.lua (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index dcc3a632..b18bb8a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,9 @@ SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -Wl,-Map=otclient.map") OPTION(PCH "Use precompiled header (speed up compile)" OFF) -SET(executable_SOURCES src/main.cpp) +SET(executable_SOURCES + src/main.cpp +) # add executable icon for win32 platforms IF(WIN32) diff --git a/init.lua b/init.lua new file mode 100644 index 00000000..75515ad2 --- /dev/null +++ b/init.lua @@ -0,0 +1,48 @@ +-- this is the first file executed when the application starts +-- we have to load the first modules form here + +-- setup application name and version +g_app.setName('OTClient') +g_app.setCompactName('otclient') +g_app.setVersion('0.4.0_dev') + +-- setup logger +g_logger.setLogFile(g_resources.getWorkDir() .. g_app.getCompactName() .. ".log") + +-- print first terminal message +g_logger.info(g_app.getName() .. ' ' .. g_app.getVersion() .. ' (rev ' .. g_app.getBuildRevision() .. ') built on ' .. g_app.getBuildDate()) + +-- add modules directory to the search path +if not g_resources.addToSearchPath(g_resources.getWorkDir() .. "modules", true) then + g_logger.fatal("Unable to add modules directory to the search path.") +end + +-- try to add addons path too +g_resources.addToSearchPath(g_resources.getWorkDir() .. "addons", true) + +-- setup directory for saving configurations +g_resources.setupWriteDir(g_app.getCompactName()) + +-- load configurations +g_configs.load("/config.otml") + +g_modules.discoverModules() + +-- core modules 0-99 +g_modules.autoLoadModules(99); +g_modules.ensureModuleLoaded("corelib") + +-- client modules 100-499 +g_modules.autoLoadModules(499); +g_modules.ensureModuleLoaded("client") + +-- game modules 500-999 +g_modules.autoLoadModules(999); +g_modules.ensureModuleLoaded("game") + +-- addons 1000-9999 +g_modules.autoLoadModules(9999) + +if g_resources.fileExists("/otclientrc.lua") then + dofile("/otclientrc.lua") +end diff --git a/modules/client/client.lua b/modules/client/client.lua index cf9aae22..ac6acb2d 100644 --- a/modules/client/client.lua +++ b/modules/client/client.lua @@ -1,8 +1,8 @@ Client = {} function Client.reloadScripts() - dofile '/otclientrc' reloadModules() + dofile '/otclientrc' local message = tr('All modules and scripts were reloaded.') TextMessage.displayEventAdvance(message) print(message) @@ -35,16 +35,13 @@ function Client.init() g_window.setTitle('OTClient') g_window.setIcon(resolvepath('clienticon.png')) - -- show the only window after the first frame is rendered - scheduleEvent(function() + addEvent(function() scheduleEvent(function() - g_window.show() - -- Play startup music (The Silver Tree, by Mattias Westlund) g_sounds.playMusic("startup.ogg", 3) connect(g_game, { onGameStart = function() g_sounds.stopMusic(3) end }) connect(g_game, { onGameEnd= function() g_sounds.playMusic("startup.ogg", 3) end }) - end, 0) + end, 100) end, 0) end diff --git a/modules/client/client.otmod b/modules/client/client.otmod index ced80071..8c80bf23 100644 --- a/modules/client/client.otmod +++ b/modules/client/client.otmod @@ -3,8 +3,6 @@ Module description: Initialize the client and setups its main window author: edubart website: www.otclient.info - autoload: true - autoload-priority: 100 reloadable: false load-later: diff --git a/modules/client_locales/locales.lua b/modules/client_locales/locales.lua index dfa468a6..1e1982b9 100644 --- a/modules/client_locales/locales.lua +++ b/modules/client_locales/locales.lua @@ -47,9 +47,9 @@ function Locales.init() local userLocaleName = Settings.get('locale') if userLocaleName and Locales.setLocale(userLocaleName) then - --info('Using configured locale: ' .. userLocaleName) + pdebug('Using configured locale: ' .. userLocaleName) else - --info('Using default locale: ' .. defaultLocaleName) + pdebug('Using default locale: ' .. defaultLocaleName) Locales.setLocale(defaultLocaleName) Settings.set('locale', defaultLocaleName) end @@ -100,7 +100,7 @@ end function Locales.setLocale(name) local locale = installedLocales[name] if not locale then - warning("Locale " .. name .. ' does not exist.') + pwarning("Locale " .. name .. ' does not exist.') return false end currentLocale = locale @@ -125,7 +125,7 @@ function tr(text, ...) local translation = currentLocale.translation[text] if not translation then if currentLocale.name ~= defaultLocaleName then - warning('Unable to translate: \"' .. text .. '\"') + pwarning('Unable to translate: \"' .. text .. '\"') end translation = text end diff --git a/modules/client_skins/skins.lua b/modules/client_skins/skins.lua index 93f0d841..9e37ea8f 100644 --- a/modules/client_skins/skins.lua +++ b/modules/client_skins/skins.lua @@ -15,7 +15,8 @@ local function onSkinComboBoxOptionChange(self, optionText, optionData) end local function getSkinPath(name) - return g_modules.getModulesPath() .. g_lua.getCurrentSourcePath(0) .. '/skins/' .. string.lower(name) .. '/' + local current = getfsrcpath() + return g_resources.getRealDir(current) .. current .. '/skins/' .. string.lower(name) end -- public functions @@ -30,9 +31,9 @@ function Skins.init() local userSkinName = Settings.get('skin') if userSkinName and Skins.setSkin(userSkinName) then - info('Using configured skin: ' .. userSkinName) + pdebug('Using configured skin: ' .. userSkinName) else - info('Using default skin: ' .. defaultSkinName) + pdebug('Using default skin: ' .. defaultSkinName) Skins.setSkin(defaultSkinName) Settings.set('skin', defaultSkinName) end @@ -65,7 +66,7 @@ function Skins.installSkin(skin) end if installedSkins[skin.name] then - warning(skin.name .. ' has been replaced.') + pwarning(skin.name .. ' has been replaced.') end installedSkins[skin.name] = skin @@ -79,12 +80,12 @@ end function Skins.setSkin(name) local skin = installedSkins[name] if not skin then - warning("Skin " .. name .. ' does not exist.') + pwarning("Skin " .. name .. ' does not exist.') return false end g_fonts.clearFonts() - g_ui.clearStyles() + g_ui.clearStyles() if name ~= defaultSkinName then local defaultSkin = installedSkins[defaultSkinName] diff --git a/modules/game/images/bottompanel.png b/modules/client_skins/skins/default/images/bottompanel.png similarity index 100% rename from modules/game/images/bottompanel.png rename to modules/client_skins/skins/default/images/bottompanel.png diff --git a/modules/game/images/item.png b/modules/client_skins/skins/default/images/item.png similarity index 100% rename from modules/game/images/item.png rename to modules/client_skins/skins/default/images/item.png diff --git a/modules/game/images/logout.png b/modules/client_skins/skins/default/images/logout.png similarity index 100% rename from modules/game/images/logout.png rename to modules/client_skins/skins/default/images/logout.png diff --git a/modules/game/images/mappanel.png b/modules/client_skins/skins/default/images/mappanel.png similarity index 100% rename from modules/game/images/mappanel.png rename to modules/client_skins/skins/default/images/mappanel.png diff --git a/modules/game/images/miniwindow.png b/modules/client_skins/skins/default/images/miniwindow.png similarity index 100% rename from modules/game/images/miniwindow.png rename to modules/client_skins/skins/default/images/miniwindow.png diff --git a/modules/game/images/miniwindowbuttons.png b/modules/client_skins/skins/default/images/miniwindowbuttons.png similarity index 100% rename from modules/game/images/miniwindowbuttons.png rename to modules/client_skins/skins/default/images/miniwindowbuttons.png diff --git a/modules/game/images/sidepanel.png b/modules/client_skins/skins/default/images/sidepanel.png similarity index 100% rename from modules/game/images/sidepanel.png rename to modules/client_skins/skins/default/images/sidepanel.png diff --git a/modules/core_lib/const.lua b/modules/corelib/const.lua similarity index 100% rename from modules/core_lib/const.lua rename to modules/corelib/const.lua diff --git a/modules/core_lib/core_lib.otmod b/modules/corelib/corelib.otmod similarity index 86% rename from modules/core_lib/core_lib.otmod rename to modules/corelib/corelib.otmod index ab005700..9d7ad0af 100644 --- a/modules/core_lib/core_lib.otmod +++ b/modules/corelib/corelib.otmod @@ -1,10 +1,8 @@ Module - name: core_lib + name: corelib description: Contains core lua classes, functions and constants used by other modules author: OTClient team website: www.otclient.info - autoload: true - autoload-priority: 10 reloadable: false @onLoad: | diff --git a/modules/core_lib/ext/os.lua b/modules/corelib/ext/os.lua similarity index 100% rename from modules/core_lib/ext/os.lua rename to modules/corelib/ext/os.lua diff --git a/modules/core_lib/ext/string.lua b/modules/corelib/ext/string.lua similarity index 100% rename from modules/core_lib/ext/string.lua rename to modules/corelib/ext/string.lua diff --git a/modules/core_lib/ext/table.lua b/modules/corelib/ext/table.lua similarity index 100% rename from modules/core_lib/ext/table.lua rename to modules/corelib/ext/table.lua diff --git a/modules/core_lib/globals.lua b/modules/corelib/globals.lua similarity index 100% rename from modules/core_lib/globals.lua rename to modules/corelib/globals.lua diff --git a/modules/core_lib/keyboard.lua b/modules/corelib/keyboard.lua similarity index 90% rename from modules/core_lib/keyboard.lua rename to modules/corelib/keyboard.lua index 5f7dabdc..1f91151d 100644 --- a/modules/core_lib/keyboard.lua +++ b/modules/corelib/keyboard.lua @@ -15,7 +15,9 @@ function translateKeyCombo(keyCombo) end local function retranslateKeyComboDesc(keyComboDesc) - if keyComboDesc == nil then return nil end + if keyComboDesc == nil then + error('Unable to translate key combo \'' .. keyComboDesc .. '\'') + end local keyCombo = {} for i,currentKeyDesc in ipairs(keyComboDesc:split('+')) do for keyCode, keyDesc in pairs(KeyCodeDescs) do @@ -97,11 +99,7 @@ function Keyboard.bindKeyDown(keyComboDesc, callback, widget) widget = widget or rootWidget connectKeyDownEvent(widget) local keyComboDesc = retranslateKeyComboDesc(keyComboDesc) - if keyComboDesc then - widget.boundKeyDownCombos[keyComboDesc] = callback - else - error('key combo \'' .. keyComboDesc .. '\' is failed') - end + widget.boundKeyDownCombos[keyComboDesc] = callback end function Keyboard.bindKeyPress(keyComboDesc, callback, widget, autoRepeatDelay) @@ -109,12 +107,8 @@ function Keyboard.bindKeyPress(keyComboDesc, callback, widget, autoRepeatDelay) widget = widget or rootWidget connectKeyPressEvent(widget) local keyComboDesc = retranslateKeyComboDesc(keyComboDesc) - if keyComboDesc then - widget.boundKeyPressCombos[keyComboDesc] = { callback = callback, autoRepeatDelay = autoRepeatDelay } - widget:setAutoRepeatDelay(math.min(autoRepeatDelay, widget:getAutoRepeatDelay())) - else - error('key combo \'' .. keyComboDesc .. '\' is failed') - end + widget.boundKeyPressCombos[keyComboDesc] = { callback = callback, autoRepeatDelay = autoRepeatDelay } + widget:setAutoRepeatDelay(math.min(autoRepeatDelay, widget:getAutoRepeatDelay())) end function Keyboard.unbindKeyDown(keyComboDesc, widget) diff --git a/modules/core_lib/math/color.lua b/modules/corelib/math/color.lua similarity index 100% rename from modules/core_lib/math/color.lua rename to modules/corelib/math/color.lua diff --git a/modules/core_lib/math/math.lua b/modules/corelib/math/math.lua similarity index 100% rename from modules/core_lib/math/math.lua rename to modules/corelib/math/math.lua diff --git a/modules/core_lib/math/point.lua b/modules/corelib/math/point.lua similarity index 100% rename from modules/core_lib/math/point.lua rename to modules/corelib/math/point.lua diff --git a/modules/core_lib/math/rect.lua b/modules/corelib/math/rect.lua similarity index 100% rename from modules/core_lib/math/rect.lua rename to modules/corelib/math/rect.lua diff --git a/modules/core_lib/math/size.lua b/modules/corelib/math/size.lua similarity index 100% rename from modules/core_lib/math/size.lua rename to modules/corelib/math/size.lua diff --git a/modules/core_lib/mouse.lua b/modules/corelib/mouse.lua similarity index 100% rename from modules/core_lib/mouse.lua rename to modules/corelib/mouse.lua diff --git a/modules/core_lib/settings.lua b/modules/corelib/settings.lua similarity index 100% rename from modules/core_lib/settings.lua rename to modules/corelib/settings.lua diff --git a/modules/core_lib/ui/effects.lua b/modules/corelib/ui/effects.lua similarity index 100% rename from modules/core_lib/ui/effects.lua rename to modules/corelib/ui/effects.lua diff --git a/modules/core_lib/ui/radiogroup.lua b/modules/corelib/ui/radiogroup.lua similarity index 100% rename from modules/core_lib/ui/radiogroup.lua rename to modules/corelib/ui/radiogroup.lua diff --git a/modules/core_lib/ui/tooltip.lua b/modules/corelib/ui/tooltip.lua similarity index 97% rename from modules/core_lib/ui/tooltip.lua rename to modules/corelib/ui/tooltip.lua index cd53ad36..d8874c14 100644 --- a/modules/core_lib/ui/tooltip.lua +++ b/modules/corelib/ui/tooltip.lua @@ -45,7 +45,7 @@ function ToolTip.init() onHoverChange = onWidgetHoverChange}) addEvent(function() - toolTipLabel = createWidget('Label', rootWidget) + toolTipLabel = createWidget('UILabel', rootWidget) toolTipLabel:setId('toolTip') toolTipLabel:setBackgroundColor('#111111cc') toolTipLabel:setTextAlign(AlignCenter) diff --git a/modules/core_lib/util.lua b/modules/corelib/util.lua similarity index 94% rename from modules/core_lib/util.lua rename to modules/corelib/util.lua index b73cb1ec..fffc8bce 100644 --- a/modules/core_lib/util.lua +++ b/modules/corelib/util.lua @@ -6,18 +6,27 @@ function print(...) g_logger.log(LogInfo, msg) end -function info(msg) +function pinfo(msg) g_logger.log(LogInfo, msg) end -function warning(msg) +function perror(msg) + g_logger.log(LogError, msg) +end + +function pwarning(msg) g_logger.log(LogWarning, msg) end +function pdebug(msg) + g_logger.log(LogDebug, msg) +end + function fatal(msg) g_logger.log(LogFatal, msg) end + exit = g_app.exit quit = g_app.exit @@ -161,3 +170,7 @@ function signalcall(param, ...) end return false end + +function tr(s) + return s +end diff --git a/modules/core_lib/widgets/uibutton.lua b/modules/corelib/widgets/uibutton.lua similarity index 100% rename from modules/core_lib/widgets/uibutton.lua rename to modules/corelib/widgets/uibutton.lua diff --git a/modules/core_lib/widgets/uicheckbox.lua b/modules/corelib/widgets/uicheckbox.lua similarity index 100% rename from modules/core_lib/widgets/uicheckbox.lua rename to modules/corelib/widgets/uicheckbox.lua diff --git a/modules/core_lib/widgets/uicombobox.lua b/modules/corelib/widgets/uicombobox.lua similarity index 100% rename from modules/core_lib/widgets/uicombobox.lua rename to modules/corelib/widgets/uicombobox.lua diff --git a/modules/core_lib/widgets/uilabel.lua b/modules/corelib/widgets/uilabel.lua similarity index 100% rename from modules/core_lib/widgets/uilabel.lua rename to modules/corelib/widgets/uilabel.lua diff --git a/modules/core_lib/widgets/uimessagebox.lua b/modules/corelib/widgets/uimessagebox.lua similarity index 100% rename from modules/core_lib/widgets/uimessagebox.lua rename to modules/corelib/widgets/uimessagebox.lua diff --git a/modules/game/widgets/uiminiwindow.lua b/modules/corelib/widgets/uiminiwindow.lua similarity index 100% rename from modules/game/widgets/uiminiwindow.lua rename to modules/corelib/widgets/uiminiwindow.lua diff --git a/modules/game/widgets/uiminiwindowcontainer.lua b/modules/corelib/widgets/uiminiwindowcontainer.lua similarity index 100% rename from modules/game/widgets/uiminiwindowcontainer.lua rename to modules/corelib/widgets/uiminiwindowcontainer.lua diff --git a/modules/core_lib/widgets/uipopupmenu.lua b/modules/corelib/widgets/uipopupmenu.lua similarity index 100% rename from modules/core_lib/widgets/uipopupmenu.lua rename to modules/corelib/widgets/uipopupmenu.lua diff --git a/modules/core_lib/widgets/uiprogressbar.lua b/modules/corelib/widgets/uiprogressbar.lua similarity index 100% rename from modules/core_lib/widgets/uiprogressbar.lua rename to modules/corelib/widgets/uiprogressbar.lua diff --git a/modules/core_lib/widgets/uiresizeborder.lua b/modules/corelib/widgets/uiresizeborder.lua similarity index 100% rename from modules/core_lib/widgets/uiresizeborder.lua rename to modules/corelib/widgets/uiresizeborder.lua diff --git a/modules/core_lib/widgets/uiscrollarea.lua b/modules/corelib/widgets/uiscrollarea.lua similarity index 100% rename from modules/core_lib/widgets/uiscrollarea.lua rename to modules/corelib/widgets/uiscrollarea.lua diff --git a/modules/core_lib/widgets/uiscrollbar.lua b/modules/corelib/widgets/uiscrollbar.lua similarity index 100% rename from modules/core_lib/widgets/uiscrollbar.lua rename to modules/corelib/widgets/uiscrollbar.lua diff --git a/modules/core_lib/widgets/uispinbox.lua b/modules/corelib/widgets/uispinbox.lua similarity index 100% rename from modules/core_lib/widgets/uispinbox.lua rename to modules/corelib/widgets/uispinbox.lua diff --git a/modules/core_lib/widgets/uisplitter.lua b/modules/corelib/widgets/uisplitter.lua similarity index 100% rename from modules/core_lib/widgets/uisplitter.lua rename to modules/corelib/widgets/uisplitter.lua diff --git a/modules/core_lib/widgets/uitabbar.lua b/modules/corelib/widgets/uitabbar.lua similarity index 100% rename from modules/core_lib/widgets/uitabbar.lua rename to modules/corelib/widgets/uitabbar.lua diff --git a/modules/core_lib/widgets/uiwidget.lua b/modules/corelib/widgets/uiwidget.lua similarity index 100% rename from modules/core_lib/widgets/uiwidget.lua rename to modules/corelib/widgets/uiwidget.lua diff --git a/modules/core_lib/widgets/uiwindow.lua b/modules/corelib/widgets/uiwindow.lua similarity index 100% rename from modules/core_lib/widgets/uiwindow.lua rename to modules/corelib/widgets/uiwindow.lua diff --git a/modules/game/game.otmod b/modules/game/game.otmod index 40577848..a6391262 100644 --- a/modules/game/game.otmod +++ b/modules/game/game.otmod @@ -1,15 +1,16 @@ Module name: game - description: Create the game interface, where the ingame stuff starts + description: Contains game related classes author: OTClient team website: www.otclient.info dependencies: - client_extended - - game_tibiafiles - client_background + - game_tibiafiles load-later: + - game_interface - game_textmessage - game_console - game_outfit @@ -31,26 +32,9 @@ Module - game_shaders @onLoad: | - importStyle 'styles/items.otui' - importStyle 'styles/creatures.otui' - importStyle 'styles/miniwindow.otui' - importStyle 'styles/countwindow.otui' - dofile 'const' dofile 'protocollogin' - dofile 'widgets/uigamemap' - dofile 'widgets/uiitem' - dofile 'widgets/uiminiwindow' - dofile 'widgets/uiminiwindowcontainer' - dofile 'creature' dofile 'player' - - dofile 'gameinterface' - - GameInterface.init() - - @onUnload: | - GameInterface.terminate() diff --git a/modules/game_battle/battle.otmod b/modules/game_battle/battle.otmod index 953335b8..dfc181e9 100644 --- a/modules/game_battle/battle.otmod +++ b/modules/game_battle/battle.otmod @@ -6,7 +6,7 @@ Module icon: battle.png dependencies: - - game + - game_interface @onLoad: | dofile 'battle' diff --git a/modules/game_bugreport/bugreport.otmod b/modules/game_bugreport/bugreport.otmod index d6fb62c6..829fbb51 100644 --- a/modules/game_bugreport/bugreport.otmod +++ b/modules/game_bugreport/bugreport.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'bugreport' diff --git a/modules/game_combatcontrols/combatcontrols.otmod b/modules/game_combatcontrols/combatcontrols.otmod index 23153692..e6846ed3 100644 --- a/modules/game_combatcontrols/combatcontrols.otmod +++ b/modules/game_combatcontrols/combatcontrols.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'combatcontrols' diff --git a/modules/game_console/console.lua b/modules/game_console/console.lua index 004eca74..09fcac43 100644 --- a/modules/game_console/console.lua +++ b/modules/game_console/console.lua @@ -106,7 +106,7 @@ local function onCreatureSpeak(name, level, speaktype, message, channelId, creat Console.addText(composedMessage, speaktype, channel, name) elseif channelId ~= 0 then -- server sent a message on a channel that is not open - warning('message in channel id ' .. channelId .. ' which is unknown, this is a server bug, relogin if you want to see messages in this channel') + pwarning('message in channel id ' .. channelId .. ' which is unknown, this is a server bug, relogin if you want to see messages in this channel') end end end @@ -272,7 +272,7 @@ function Console.clear() local tab = consoleTabBar:getTab(channelname) consoleTabBar:removeTab(tab) end - + channels = {} consoleTabBar:getTab(tr('Default')).tabPanel:getChildById('consoleBuffer'):destroyChildren() @@ -407,12 +407,12 @@ function Console.popupMenu(mousePos, mouseButton, creatureName, text) end --TODO select all menu:addOption(tr('Copy message'), function () g_window.setClipboardText(text) end) - + if RuleViolation.hasWindowAccess() then menu:addSeparator() menu:addOption(tr('Rule Violation'), function() RuleViolation.show(creatureName, text:match('.+%:%s(.+)')) end) end - + menu:addSeparator() menu:addOption(tr('Copy name'), function () g_window.setClipboardText(creatureName) end) else diff --git a/modules/game_console/console.otmod b/modules/game_console/console.otmod index f3a7058b..42605be9 100644 --- a/modules/game_console/console.otmod +++ b/modules/game_console/console.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'console' diff --git a/modules/game_containers/container.otui b/modules/game_containers/container.otui index 534b031d..818fe4d7 100644 --- a/modules/game_containers/container.otui +++ b/modules/game_containers/container.otui @@ -17,7 +17,7 @@ ContainerWindow < MiniWindow anchors.right: minimizeButton.left margin-right: 3 size: 14 14 - image-source: /game/images/miniwindowbuttons.png + image-source: /images/miniwindowbuttons.png image-clip: 42 0 14 14 $hover: diff --git a/modules/game_containers/containers.otmod b/modules/game_containers/containers.otmod index a6172b7b..da52077c 100644 --- a/modules/game_containers/containers.otmod +++ b/modules/game_containers/containers.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'containers' diff --git a/modules/game_healthbar/healthbar.otmod b/modules/game_healthbar/healthbar.otmod index 4f150be0..13a55972 100644 --- a/modules/game_healthbar/healthbar.otmod +++ b/modules/game_healthbar/healthbar.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'healthbar' diff --git a/modules/game_hotkeys/hotkeys_manager.otmod b/modules/game_hotkeys/hotkeys_manager.otmod index 88e3f1df..614df919 100644 --- a/modules/game_hotkeys/hotkeys_manager.otmod +++ b/modules/game_hotkeys/hotkeys_manager.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'hotkeys_manager' diff --git a/modules/game/gameinterface.lua b/modules/game_interface/gameinterface.lua similarity index 99% rename from modules/game/gameinterface.lua rename to modules/game_interface/gameinterface.lua index 67f0bce6..9eeb2c92 100644 --- a/modules/game/gameinterface.lua +++ b/modules/game_interface/gameinterface.lua @@ -25,7 +25,7 @@ function GameInterface.init() gameLeftPanel = gameRootPanel:getChildById('gameLeftPanel') gameBottomPanel = gameRootPanel:getChildById('gameBottomPanel') - logoutButton = TopMenu.addRightButton('logoutButton', 'Logout', 'images/logout.png', GameInterface.tryLogout) + logoutButton = TopMenu.addRightButton('logoutButton', 'Logout', '/images/logout.png', GameInterface.tryLogout) logoutButton:hide() Keyboard.bindKeyPress('Up', function() g_game.walk(North) end, gameRootPanel, WALK_AUTO_REPEAT_DELAY) diff --git a/modules/game/gameinterface.otui b/modules/game_interface/gameinterface.otui similarity index 93% rename from modules/game/gameinterface.otui rename to modules/game_interface/gameinterface.otui index 45ad54a8..3ebd5213 100644 --- a/modules/game/gameinterface.otui +++ b/modules/game_interface/gameinterface.otui @@ -1,15 +1,15 @@ GameSidePanel < UIMiniWindowContainer - image-source: images/sidepanel.png + image-source: /images/sidepanel.png image-border: 4 padding: 4 GameBottomPanel < Panel - image-source: images/bottompanel.png + image-source: /images/bottompanel.png image-border: 4 GameMapPanel < UIGameMap padding: 4 - image-source: images/mappanel.png + image-source: /images/mappanel.png image-border: 4 UIWidget diff --git a/modules/game_interface/interface.otmod b/modules/game_interface/interface.otmod new file mode 100644 index 00000000..531b1a7e --- /dev/null +++ b/modules/game_interface/interface.otmod @@ -0,0 +1,21 @@ +Module + name: game_interface + description: Create the game interface, where the ingame stuff starts + author: OTClient team + website: www.otclient.info + + @onLoad: | + importStyle 'styles/items.otui' + importStyle 'styles/creatures.otui' + importStyle 'styles/miniwindow.otui' + importStyle 'styles/countwindow.otui' + + dofile 'widgets/uigamemap' + dofile 'widgets/uiitem' + + dofile 'gameinterface' + + GameInterface.init() + + @onUnload: | + GameInterface.terminate() diff --git a/modules/game/styles/countwindow.otui b/modules/game_interface/styles/countwindow.otui similarity index 100% rename from modules/game/styles/countwindow.otui rename to modules/game_interface/styles/countwindow.otui diff --git a/modules/game/styles/creatures.otui b/modules/game_interface/styles/creatures.otui similarity index 100% rename from modules/game/styles/creatures.otui rename to modules/game_interface/styles/creatures.otui diff --git a/modules/game/styles/items.otui b/modules/game_interface/styles/items.otui similarity index 70% rename from modules/game/styles/items.otui rename to modules/game_interface/styles/items.otui index 740fc269..305df01e 100644 --- a/modules/game/styles/items.otui +++ b/modules/game_interface/styles/items.otui @@ -1,6 +1,6 @@ Item < UIItem size: 34 34 padding: 1 - image-source: /game/images/item.png + image-source: /images/item.png font: verdana-11px-rounded border-color: white diff --git a/modules/game/styles/miniwindow.otui b/modules/game_interface/styles/miniwindow.otui similarity index 91% rename from modules/game/styles/miniwindow.otui rename to modules/game_interface/styles/miniwindow.otui index f8aa79dd..3180258f 100644 --- a/modules/game/styles/miniwindow.otui +++ b/modules/game_interface/styles/miniwindow.otui @@ -5,7 +5,7 @@ MiniWindow < UIMiniWindow height: 200 text-offset: 24 5 text-align: topLeft - image-source: /game/images/miniwindow.png + image-source: /images/miniwindow.png image-border: 4 image-border-top: 23 image-border-bottom: 4 @@ -22,7 +22,7 @@ MiniWindow < UIMiniWindow margin-top: 5 margin-right: 5 size: 14 14 - image-source: /game/images/miniwindowbuttons.png + image-source: /images/miniwindowbuttons.png image-clip: 28 0 14 14 $hover: @@ -37,7 +37,7 @@ MiniWindow < UIMiniWindow anchors.right: closeButton.left margin-right: 3 size: 14 14 - image-source: /game/images/miniwindowbuttons.png + image-source: /images/miniwindowbuttons.png image-clip: 0 0 14 14 $hover: diff --git a/modules/game/widgets/uigamemap.lua b/modules/game_interface/widgets/uigamemap.lua similarity index 100% rename from modules/game/widgets/uigamemap.lua rename to modules/game_interface/widgets/uigamemap.lua diff --git a/modules/game/widgets/uiitem.lua b/modules/game_interface/widgets/uiitem.lua similarity index 100% rename from modules/game/widgets/uiitem.lua rename to modules/game_interface/widgets/uiitem.lua diff --git a/modules/game_inventory/inventory.otmod b/modules/game_inventory/inventory.otmod index 4780813d..72912535 100644 --- a/modules/game_inventory/inventory.otmod +++ b/modules/game_inventory/inventory.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'inventory' diff --git a/modules/game_minimap/minimap.otmod b/modules/game_minimap/minimap.otmod index 4c8e69e7..e67e9bc4 100644 --- a/modules/game_minimap/minimap.otmod +++ b/modules/game_minimap/minimap.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'minimap' diff --git a/modules/game_npctrade/npctrade.otmod b/modules/game_npctrade/npctrade.otmod index 367c76bd..c7d703a9 100644 --- a/modules/game_npctrade/npctrade.otmod +++ b/modules/game_npctrade/npctrade.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'npctrade' diff --git a/modules/game_outfit/outfit.otmod b/modules/game_outfit/outfit.otmod index 8559c680..2ed05cde 100644 --- a/modules/game_outfit/outfit.otmod +++ b/modules/game_outfit/outfit.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'outfit' diff --git a/modules/game_playertrade/playertrade.otmod b/modules/game_playertrade/playertrade.otmod index 8a36e1a9..5222063e 100644 --- a/modules/game_playertrade/playertrade.otmod +++ b/modules/game_playertrade/playertrade.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'playertrade' diff --git a/modules/game_questlog/questlog.otmod b/modules/game_questlog/questlog.otmod index cbc36591..be6576f0 100644 --- a/modules/game_questlog/questlog.otmod +++ b/modules/game_questlog/questlog.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'questlog' diff --git a/modules/game_ruleviolation/ruleviolation.otmod b/modules/game_ruleviolation/ruleviolation.otmod index 7efb1d5f..4260a252 100644 --- a/modules/game_ruleviolation/ruleviolation.otmod +++ b/modules/game_ruleviolation/ruleviolation.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'ruleviolation' diff --git a/modules/game_shaders/shaders.otmod b/modules/game_shaders/shaders.otmod index f63e3ba6..6a7c0baf 100644 --- a/modules/game_shaders/shaders.otmod +++ b/modules/game_shaders/shaders.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'shaders' diff --git a/modules/game_skills/skills.otmod b/modules/game_skills/skills.otmod index 75ce8f40..04cff361 100644 --- a/modules/game_skills/skills.otmod +++ b/modules/game_skills/skills.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'skills' diff --git a/modules/game_textbooks/textbooks.otmod b/modules/game_textbooks/textbooks.otmod index 1a470e93..c0891d62 100644 --- a/modules/game_textbooks/textbooks.otmod +++ b/modules/game_textbooks/textbooks.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'textbooks' diff --git a/modules/game_textmessage/textmessage.otmod b/modules/game_textmessage/textmessage.otmod index 6d4cfe2b..0dd03944 100644 --- a/modules/game_textmessage/textmessage.otmod +++ b/modules/game_textmessage/textmessage.otmod @@ -5,7 +5,7 @@ Module website: www.otclient.info dependencies: - - game + - game_interface @onLoad: | dofile 'textmessage' diff --git a/src/framework/CMakeLists.txt b/src/framework/CMakeLists.txt index c2c1263d..72c9d3bb 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -10,11 +10,7 @@ OPTION(CRASH_HANDLER "Generate crash reports" ON) OPTION(LUAJIT "Use lua jit" OFF) SET(OPENGLES "OFF" CACHE "Use OpenGL ES 1.0 or 2.0 (for mobiles devices)" STRING) SET(BUILD_REVISION "custom" CACHE "Git revision string (intended for releases)" STRING) - -# set debug as default build type -IF(NOT CMAKE_BUILD_TYPE) - SET(CMAKE_BUILD_TYPE RelWithDebInfo) -ENDIF() +SET(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE "Build type (Release, MinSizeRel, RelWithDebInfo or Debug)" STRING) IF(CMAKE_SIZEOF_VOID_P EQUAL 8) SET(ARCH_FLAGS "-m64 -march=x86-64 -mtune=generic") @@ -26,10 +22,11 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNS_FLAGS} ${ARCH_ SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -ggdb") SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O1 -ggdb -fno-omit-frame-pointer") SET(CMAKE_CXX_FLAGS_RELEASE "-O2") +SET(CMAKE_CXX_FLAGS_MINSIZEREL "-Os") SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -static-libgcc -static-libstdc++ -Wl,--as-needed") -IF(CMAKE_BUILD_TYPE STREQUAL "Release") +IF(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "MinSizeRel") SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -Wl,-s") ENDIF() diff --git a/src/framework/application.cpp b/src/framework/application.cpp index 1e086a55..625b7470 100644 --- a/src/framework/application.cpp +++ b/src/framework/application.cpp @@ -37,7 +37,7 @@ #include #include -Application *g_app = nullptr; +Application g_app; void exitSignalHandler(int sig) { @@ -53,18 +53,14 @@ void exitSignalHandler(int sig) } } -Application::Application(const std::string& appName) +Application::Application() { - g_app = this; - m_appName = appName; + m_appName = "application"; + m_appCompactName = "app"; + m_appVersion = "none"; m_foregroundFrameCounter.setMaxFps(60); } -Application::~Application() -{ - g_app = nullptr; -} - void Application::init(const std::vector& args) { // capture exit signals @@ -75,24 +71,28 @@ void Application::init(const std::vector& args) installCrashHandler(); #endif - // initialize lua - g_lua.init(); - registerLuaFunctions(); + std::string startupOptions; + for(uint i=1;i 0) + g_logger.info(stdext::format("Startup options: %s", startupOptions)); + + m_startupOptions = startupOptions; // initialize resources g_resources.init(args[0].c_str()); - // setup configs write directory - if(!g_resources.setupWriteDir(m_appName)) - g_logger.error("Could not setup write directory"); - - // load configs - if(!g_configs.load("/config.otml")) - g_logger.info("Using default configurations."); + // initialize lua + g_lua.init(); + registerLuaFunctions(); - // setup platform window + // initialize ui g_ui.init(); + // setup platform window g_window.init(); g_window.hide(); g_window.setOnResize(std::bind(&Application::resize, this, std::placeholders::_1)); @@ -105,14 +105,9 @@ void Application::init(const std::vector& args) // initialize sound g_sounds.init(); - // fire first resize + // fire first resize event resize(g_window.getSize()); - // display window when the application starts running - //g_eventDispatcher.addEvent([]{ g_window.show(); }); - - g_modules.discoverModulesPath(); - m_initialized = true; } @@ -132,15 +127,15 @@ void Application::deinit() // poll remaining events poll(); - - // destroy any remaining widget - g_ui.terminate(); } void Application::terminate() { assert(m_initialized); + // destroy any remaining widget + g_ui.terminate(); + // terminate network Connection::terminate(); @@ -164,7 +159,7 @@ void Application::terminate() g_graphics.terminate(); g_window.terminate(); - g_logger.info("Application ended successfully."); + g_logger.debug("Application ended successfully."); m_terminated = true; } @@ -184,6 +179,9 @@ void Application::run() // first clock update g_clock.update(); + // show the application window + g_window.show(); + while(!m_stopping) { // poll all events before rendering poll(); diff --git a/src/framework/application.h b/src/framework/application.h index ebae01b3..da074eca 100644 --- a/src/framework/application.h +++ b/src/framework/application.h @@ -34,29 +34,31 @@ class Application }; public: - Application(const std::string& appName); - ~Application(); + Application(); - virtual void init(const std::vector& args); - virtual void registerLuaFunctions(); - virtual void deinit(); - virtual void terminate(); - virtual void run(); - virtual void exit(); - virtual void poll(); - virtual void close(); + void init(const std::vector& args); + void deinit(); + void terminate(); + void run(); + void exit(); + void poll(); + void close(); bool willRepaint() { return m_mustRepaint; } void repaint() { m_mustRepaint = true; } void setForegroundPaneMaxFps(int maxFps) { m_foregroundFrameCounter.setMaxFps(maxFps); } void setBackgroundPaneMaxFps(int maxFps) { m_backgroundFrameCounter.setMaxFps(maxFps); } + void setName(const std::string& name) { m_appName = name; } + void setCompactName(const std::string& compactName) { m_appCompactName = compactName; } + void setVersion(const std::string& version) { m_appVersion = version; } bool isRunning() { return m_running; } bool isStopping() { return m_stopping; } bool isTermianted() { return m_terminated; } bool isOnInputEvent() { return m_onInputEvent; } const std::string& getName() { return m_appName; } + const std::string& getCompactName() { return m_appCompactName; } const std::string& getVersion() { return m_appVersion; } int getForegroundPaneFps() { return m_foregroundFrameCounter.getLastFps(); } @@ -67,14 +69,17 @@ public: std::string getBuildDate() { return BUILD_DATE; } std::string getBuildRevision() { return BUILD_REVISION; } std::string getBuildType() { return BUILD_TYPE; } + std::string getStartupOptions() { return m_startupOptions; } protected: - virtual void resize(const Size& size); - virtual void inputEvent(const InputEvent& event); + void resize(const Size& size); + void inputEvent(const InputEvent& event); + void registerLuaFunctions(); std::string m_appName; + std::string m_appCompactName; std::string m_appVersion; - std::string m_appBuildDate; + std::string m_startupOptions; Boolean m_initialized; Boolean m_running; Boolean m_stopping; @@ -86,7 +91,7 @@ protected: TexturePtr m_foreground; }; -extern Application *g_app; +extern Application g_app; #endif diff --git a/src/framework/core/configmanager.cpp b/src/framework/core/configmanager.cpp index c62f9936..69de564d 100644 --- a/src/framework/core/configmanager.cpp +++ b/src/framework/core/configmanager.cpp @@ -45,7 +45,7 @@ bool ConfigManager::load(const std::string& file) m_confsDoc = confsDoc; return true; } catch(stdext::exception& e) { - g_logger.error(stdext::format("Unable to load configuration file: %s", e.what())); + g_logger.error(stdext::format("Unable to parse configuration file '%s'", e.what())); return false; } } diff --git a/src/framework/core/eventdispatcher.h b/src/framework/core/eventdispatcher.h index 5a4ec2d2..937a9d37 100644 --- a/src/framework/core/eventdispatcher.h +++ b/src/framework/core/eventdispatcher.h @@ -33,7 +33,7 @@ public: Event(const std::function& callback) : m_callback(callback), m_canceled(false), m_executed(false) { } virtual ~Event() { // assure that we lost callback refs - assert(m_callback == nullptr); + //assert(m_callback == nullptr); } virtual void execute() { diff --git a/src/framework/core/logger.cpp b/src/framework/core/logger.cpp index 277c526e..fc4b2d12 100644 --- a/src/framework/core/logger.cpp +++ b/src/framework/core/logger.cpp @@ -28,6 +28,11 @@ Logger g_logger; void Logger::log(Fw::LogLevel level, const std::string& message) { +#ifdef NDEBUG + if(level == Fw::LogDebug) + return; +#endif + static bool ignoreLogs = false; if(ignoreLogs) return; diff --git a/src/framework/core/module.cpp b/src/framework/core/module.cpp index 8d4adbd4..d9126226 100644 --- a/src/framework/core/module.cpp +++ b/src/framework/core/module.cpp @@ -53,7 +53,7 @@ bool Module::load() m_loadCallback(); m_loaded = true; - //g_logger.info(stdext::format("Loaded module '%s'", m_name)); + g_logger.debug(stdext::format("Loaded module '%s'", m_name)); g_modules.updateModuleLoadOrder(asModule()); for(const std::string& modName : m_loadLaterModules) { diff --git a/src/framework/core/modulemanager.cpp b/src/framework/core/modulemanager.cpp index f00e691d..df30c45d 100644 --- a/src/framework/core/modulemanager.cpp +++ b/src/framework/core/modulemanager.cpp @@ -64,44 +64,6 @@ void ModuleManager::autoLoadModules(int maxPriority) } } -void ModuleManager::discoverModulesPath() -{ - // search for modules directory - std::string possibleModulesDirs[] = { "modules", - g_resources.getBaseDir() + "modules", - g_resources.getBaseDir() + "../modules", - g_resources.getBaseDir() + "../share/" + g_app->getName() + "/modules", - "" }; - bool found = false; - for(const std::string& dir : possibleModulesDirs) { - // try to add module directory - if(g_resources.addToSearchPath(dir, false)) { - //g_logger.info(stdext::format("Using modules directory '%s'", dir.c_str())); - m_modulesPath = dir; - found = true; - break; - } - } - - if(!found) - g_logger.fatal("Could not find modules directory"); - - // search for addons directory - std::string possibleAddonsDirs[] = { "addons", - g_resources.getBaseDir() + "addons", - g_resources.getBaseDir() + "../addons", - g_resources.getBaseDir() + "../share/" + g_app->getName() + "/addons", - "" }; - for(const std::string& dir : possibleAddonsDirs) { - // try to add module directory - if(g_resources.addToSearchPath(dir, true)) { - //g_logger.info(stdext::format("Using addons directory '%s'", dir.c_str())); - found = true; - break; - } - } -} - ModulePtr ModuleManager::discoverModule(const std::string& moduleFile) { ModulePtr module; diff --git a/src/framework/core/modulemanager.h b/src/framework/core/modulemanager.h index 8804b851..e8a8be2a 100644 --- a/src/framework/core/modulemanager.h +++ b/src/framework/core/modulemanager.h @@ -30,14 +30,12 @@ class ModuleManager public: void clear(); - void discoverModulesPath(); void discoverModules(); void autoLoadModules(int maxPriority); ModulePtr discoverModule(const std::string& moduleFile); void ensureModuleLoaded(const std::string& moduleName); void unloadModules(); void reloadModules(); - std::string getModulesPath() { return m_modulesPath; } ModulePtr getModule(const std::string& moduleName); std::deque getModules() { return m_modules; } @@ -48,7 +46,6 @@ protected: friend class Module; private: - std::string m_modulesPath; std::deque m_modules; std::multimap m_autoLoadModules; }; diff --git a/src/framework/core/resourcemanager.cpp b/src/framework/core/resourcemanager.cpp index ec7a1da3..2bb5ea6f 100644 --- a/src/framework/core/resourcemanager.cpp +++ b/src/framework/core/resourcemanager.cpp @@ -40,22 +40,58 @@ void ResourceManager::terminate() PHYSFS_deinit(); } +void ResourceManager::discoverWorkDir(const std::string& appName, const std::string& existentFile) +{ + // search for modules directory + std::string sep = PHYSFS_getDirSeparator(); + std::string possiblePaths[] = { "", + g_resources.getBaseDir(), + g_resources.getBaseDir() + ".." + sep, + g_resources.getBaseDir() + ".." + sep + "share" + sep + appName + sep, + g_resources.getBaseDir() + appName + sep }; + bool found = false; + for(const std::string& dir : possiblePaths) { + // try to directory to modules path to see if it exists + std::ifstream fin(dir + existentFile); + if(fin) { + g_logger.debug(stdext::format("Found work dir at '%s'", dir.c_str())); + m_workDir = dir; + found = true; + break; + } + } + + if(!found) + g_logger.fatal("Unable to find application work directory."); +} + bool ResourceManager::setupWriteDir(const std::string& appWriteDirName) { std::string userDir = PHYSFS_getUserDir(); - std::string dirName = stdext::format(".%s", appWriteDirName); + std::string dirName; +#ifndef WIN32 + dirName = stdext::format(".%s", appWriteDirName); +#else + dirName = appWriteDirName; +#endif std::string writeDir = userDir + dirName; + if(!PHYSFS_setWriteDir(writeDir.c_str())) { - if(!PHYSFS_setWriteDir(userDir.c_str())) + if(!PHYSFS_setWriteDir(userDir.c_str())) { + g_logger.error("User directory not found."); return false; + } if(!PHYSFS_mkdir(dirName.c_str())) { - PHYSFS_setWriteDir(NULL); + g_logger.error("Cannot create directory for saving configurations."); return false; } - if(!PHYSFS_setWriteDir(writeDir.c_str())) + if(!PHYSFS_setWriteDir(writeDir.c_str())) { + g_logger.error("Unable to set write directory."); return false; + } } - addToSearchPath(writeDir); + addToSearchPath(writeDir, true); + //g_logger.debug(stdext::format("Setup write dir %s", writeDir)); return true; } @@ -63,6 +99,8 @@ bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFron { if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1)) return false; + //g_logger.debug(stdext::format("Add search path %s", path)); + m_hasSearchPath = true; return true; } @@ -70,6 +108,7 @@ bool ResourceManager::removeFromSearchPath(const std::string& path) { if(!PHYSFS_removeFromSearchPath(path.c_str())) return false; + //g_logger.debug(stdext::format("Remove search path %s", path)); return true; } @@ -94,22 +133,32 @@ bool ResourceManager::directoryExists(const std::string& directoryName) void ResourceManager::loadFile(const std::string& fileName, std::iostream& out) { - std::string fullPath = resolvePath(fileName); out.clear(std::ios::goodbit); - PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str()); - if(!file) { - out.clear(std::ios::failbit); - stdext::throw_exception(stdext::format("failed to load file '%s': %s", fullPath.c_str(), PHYSFS_getLastError())); + if(m_hasSearchPath) { + std::string fullPath = resolvePath(fileName); + PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str()); + if(!file) { + out.clear(std::ios::failbit); + stdext::throw_exception(stdext::format("failed to load file '%s': %s", fullPath.c_str(), PHYSFS_getLastError())); + } else { + int fileSize = PHYSFS_fileLength(file); + if(fileSize > 0) { + std::vector buffer(fileSize); + PHYSFS_read(file, (void*)&buffer[0], 1, fileSize); + out.write(&buffer[0], fileSize); + } else + out.clear(std::ios::eofbit); + PHYSFS_close(file); + out.seekg(0, std::ios::beg); + } } else { - int fileSize = PHYSFS_fileLength(file); - if(fileSize > 0) { - std::vector buffer(fileSize); - PHYSFS_read(file, (void*)&buffer[0], 1, fileSize); - out.write(&buffer[0], fileSize); - } else - out.clear(std::ios::eofbit); - PHYSFS_close(file); - out.seekg(0, std::ios::beg); + std::ifstream fin(fileName); + if(!fin) { + out.clear(std::ios::failbit); + stdext::throw_exception(stdext::format("failed to load file '%s': %s", fileName.c_str(), PHYSFS_getLastError())); + } else { + out << fin.rdbuf(); + } } } @@ -223,6 +272,15 @@ std::string ResourceManager::resolvePath(const std::string& path) return fullPath; } +std::string ResourceManager::getRealDir(const std::string& path) +{ + std::string dir; + const char *cdir = PHYSFS_getRealDir(path.c_str()); + if(cdir) + dir = cdir; + return dir; +} + std::string ResourceManager::getBaseDir() { return PHYSFS_getBaseDir(); diff --git a/src/framework/core/resourcemanager.h b/src/framework/core/resourcemanager.h index ac5882e7..af433c1a 100644 --- a/src/framework/core/resourcemanager.h +++ b/src/framework/core/resourcemanager.h @@ -31,6 +31,7 @@ public: void init(const char *argv0); void terminate(); + void discoverWorkDir(const std::string& appName, const std::string& existentFile); bool setupWriteDir(const std::string& appWriteDirName); bool addToSearchPath(const std::string& path, bool insertInFront = true); @@ -55,7 +56,13 @@ public: std::list listDirectoryFiles(const std::string& directoryPath = ""); std::string resolvePath(const std::string& path); + std::string getRealDir(const std::string& path); std::string getBaseDir(); + std::string getWorkDir() { return m_workDir; } + +private: + std::string m_workDir; + Boolean m_hasSearchPath; }; extern ResourceManager g_resources; diff --git a/src/framework/graphics/framebuffer.cpp b/src/framework/graphics/framebuffer.cpp index 23f93f70..66d1cf28 100644 --- a/src/framework/graphics/framebuffer.cpp +++ b/src/framework/graphics/framebuffer.cpp @@ -47,7 +47,7 @@ void FrameBuffer::internalCreate() FrameBuffer::~FrameBuffer() { - assert(!g_app->isTermianted()); + assert(!g_app.isTermianted()); if(g_graphics.ok() && m_fbo != 0) glDeleteFramebuffers(1, &m_fbo); } diff --git a/src/framework/graphics/hardwarebuffer.cpp b/src/framework/graphics/hardwarebuffer.cpp index db902dd2..9c10d12a 100644 --- a/src/framework/graphics/hardwarebuffer.cpp +++ b/src/framework/graphics/hardwarebuffer.cpp @@ -37,7 +37,7 @@ HardwareBuffer::HardwareBuffer(Type type) HardwareBuffer::~HardwareBuffer() { - assert(!g_app->isTermianted()); + assert(!g_app.isTermianted()); if(g_graphics.ok()) glDeleteBuffers(1, &m_id); } diff --git a/src/framework/graphics/shader.cpp b/src/framework/graphics/shader.cpp index d860856c..0372264b 100644 --- a/src/framework/graphics/shader.cpp +++ b/src/framework/graphics/shader.cpp @@ -44,7 +44,7 @@ Shader::Shader(Shader::ShaderType shaderType) Shader::~Shader() { - assert(!g_app->isTermianted()); + assert(!g_app.isTermianted()); if(g_graphics.ok()) glDeleteShader(m_shaderId); } diff --git a/src/framework/graphics/shaderprogram.cpp b/src/framework/graphics/shaderprogram.cpp index 2e6430b0..8706a814 100644 --- a/src/framework/graphics/shaderprogram.cpp +++ b/src/framework/graphics/shaderprogram.cpp @@ -38,7 +38,7 @@ ShaderProgram::ShaderProgram() ShaderProgram::~ShaderProgram() { - assert(!g_app->isTermianted()); + assert(!g_app.isTermianted()); if(g_graphics.ok()) glDeleteProgram(m_programId); } diff --git a/src/framework/graphics/texture.cpp b/src/framework/graphics/texture.cpp index cdd3e515..089f4f59 100644 --- a/src/framework/graphics/texture.cpp +++ b/src/framework/graphics/texture.cpp @@ -79,7 +79,7 @@ Texture::Texture(const ImagePtr& image, bool buildMipmaps) Texture::~Texture() { - assert(!g_app->isTermianted()); + assert(!g_app.isTermianted()); // free texture from gl memory if(g_graphics.ok() && m_id != 0) glDeleteTextures(1, &m_id); diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index 505336cb..847990dc 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -20,8 +20,8 @@ * THE SOFTWARE. */ +#include "application.h" #include -#include #include #include #include @@ -480,35 +480,41 @@ void Application::registerLuaFunctions() // Application g_lua.registerSingletonClass("g_app"); - g_lua.bindSingletonFunction("g_app", "exit", &Application::exit, g_app); - g_lua.bindSingletonFunction("g_app", "setForegroundPaneMaxFps", &Application::setForegroundPaneMaxFps, g_app); - g_lua.bindSingletonFunction("g_app", "setBackgroundPaneMaxFps", &Application::setBackgroundPaneMaxFps, g_app); - g_lua.bindSingletonFunction("g_app", "isRunning", &Application::isRunning, g_app); - g_lua.bindSingletonFunction("g_app", "isStopping", &Application::isStopping, g_app); - g_lua.bindSingletonFunction("g_app", "isOnInputEvent", &Application::isOnInputEvent, g_app); - g_lua.bindSingletonFunction("g_app", "getName", &Application::getName, g_app); - g_lua.bindSingletonFunction("g_app", "getVersion", &Application::getVersion, g_app); - g_lua.bindSingletonFunction("g_app", "getForegroundPaneFps", &Application::getForegroundPaneFps, g_app); - g_lua.bindSingletonFunction("g_app", "getBackgroundPaneFps", &Application::getBackgroundPaneFps, g_app); - g_lua.bindSingletonFunction("g_app", "getForegroundPaneMaxFps", &Application::getForegroundPaneMaxFps, g_app); - g_lua.bindSingletonFunction("g_app", "getBackgroundPaneMaxFps", &Application::getBackgroundPaneMaxFps, g_app); - g_lua.bindSingletonFunction("g_app", "getBuildCompiler", &Application::getBuildCompiler, g_app); - g_lua.bindSingletonFunction("g_app", "getBuildDate", &Application::getBuildDate, g_app); - g_lua.bindSingletonFunction("g_app", "getBuildRevision", &Application::getBuildRevision, g_app); - g_lua.bindSingletonFunction("g_app", "getBuildType", &Application::getBuildType, g_app); - - g_lua.bindSingletonFunction("g_app", "exit", &Application::exit, g_app); - g_lua.bindSingletonFunction("g_app", "isRunning", &Application::isRunning, g_app); - g_lua.bindSingletonFunction("g_app", "isStopping", &Application::isStopping, g_app); - g_lua.bindSingletonFunction("g_app", "getName", &Application::getName, g_app); - g_lua.bindSingletonFunction("g_app", "getVersion", &Application::getVersion, g_app); - g_lua.bindSingletonFunction("g_app", "getBuildCompiler", &Application::getBuildCompiler, g_app); - g_lua.bindSingletonFunction("g_app", "getBuildDate", &Application::getBuildDate, g_app); - g_lua.bindSingletonFunction("g_app", "getBuildRevision", &Application::getBuildRevision, g_app); - g_lua.bindSingletonFunction("g_app", "getBuildType", &Application::getBuildType, g_app); + g_lua.bindSingletonFunction("g_app", "exit", &Application::exit, &g_app); + g_lua.bindSingletonFunction("g_app", "setForegroundPaneMaxFps", &Application::setForegroundPaneMaxFps, &g_app); + g_lua.bindSingletonFunction("g_app", "setBackgroundPaneMaxFps", &Application::setBackgroundPaneMaxFps, &g_app); + g_lua.bindSingletonFunction("g_app", "setName", &Application::setName, &g_app); + g_lua.bindSingletonFunction("g_app", "setCompactName", &Application::setCompactName, &g_app); + g_lua.bindSingletonFunction("g_app", "setVersion", &Application::setVersion, &g_app); + g_lua.bindSingletonFunction("g_app", "isRunning", &Application::isRunning, &g_app); + g_lua.bindSingletonFunction("g_app", "isStopping", &Application::isStopping, &g_app); + g_lua.bindSingletonFunction("g_app", "isOnInputEvent", &Application::isOnInputEvent, &g_app); + g_lua.bindSingletonFunction("g_app", "getName", &Application::getName, &g_app); + g_lua.bindSingletonFunction("g_app", "getCompactName", &Application::getName, &g_app); + g_lua.bindSingletonFunction("g_app", "getVersion", &Application::getVersion, &g_app); + g_lua.bindSingletonFunction("g_app", "getForegroundPaneFps", &Application::getForegroundPaneFps, &g_app); + g_lua.bindSingletonFunction("g_app", "getBackgroundPaneFps", &Application::getBackgroundPaneFps, &g_app); + g_lua.bindSingletonFunction("g_app", "getForegroundPaneMaxFps", &Application::getForegroundPaneMaxFps, &g_app); + g_lua.bindSingletonFunction("g_app", "getBackgroundPaneMaxFps", &Application::getBackgroundPaneMaxFps, &g_app); + g_lua.bindSingletonFunction("g_app", "getBuildCompiler", &Application::getBuildCompiler, &g_app); + g_lua.bindSingletonFunction("g_app", "getBuildDate", &Application::getBuildDate, &g_app); + g_lua.bindSingletonFunction("g_app", "getBuildRevision", &Application::getBuildRevision, &g_app); + g_lua.bindSingletonFunction("g_app", "getBuildType", &Application::getBuildType, &g_app); + g_lua.bindSingletonFunction("g_app", "exit", &Application::exit, &g_app); + g_lua.bindSingletonFunction("g_app", "isRunning", &Application::isRunning, &g_app); + g_lua.bindSingletonFunction("g_app", "isStopping", &Application::isStopping, &g_app); + g_lua.bindSingletonFunction("g_app", "getName", &Application::getName, &g_app); + g_lua.bindSingletonFunction("g_app", "getCompactName", &Application::getCompactName, &g_app); + g_lua.bindSingletonFunction("g_app", "getVersion", &Application::getVersion, &g_app); + g_lua.bindSingletonFunction("g_app", "getBuildCompiler", &Application::getBuildCompiler, &g_app); + g_lua.bindSingletonFunction("g_app", "getBuildDate", &Application::getBuildDate, &g_app); + g_lua.bindSingletonFunction("g_app", "getBuildRevision", &Application::getBuildRevision, &g_app); + g_lua.bindSingletonFunction("g_app", "getBuildType", &Application::getBuildType, &g_app); // ConfigManager g_lua.registerSingletonClass("g_configs"); + g_lua.bindSingletonFunction("g_configs", "load", &ConfigManager::load, &g_configs); + g_lua.bindSingletonFunction("g_configs", "save", &ConfigManager::save, &g_configs); g_lua.bindSingletonFunction("g_configs", "set", &ConfigManager::set, &g_configs); g_lua.bindSingletonFunction("g_configs", "setList", &ConfigManager::setList, &g_configs); g_lua.bindSingletonFunction("g_configs", "get", &ConfigManager::get, &g_configs); @@ -574,7 +580,13 @@ void Application::registerLuaFunctions() g_lua.registerSingletonClass("g_logger"); g_lua.bindSingletonFunction("g_logger", "log", &Logger::log, &g_logger); g_lua.bindSingletonFunction("g_logger", "fireOldMessages", &Logger::fireOldMessages, &g_logger); + g_lua.bindSingletonFunction("g_logger", "setLogFile", &Logger::setLogFile, &g_logger); g_lua.bindSingletonFunction("g_logger", "setOnLog", &Logger::setOnLog, &g_logger); + g_lua.bindSingletonFunction("g_logger", "debug", &Logger::debug, &g_logger); + g_lua.bindSingletonFunction("g_logger", "info", &Logger::info, &g_logger); + g_lua.bindSingletonFunction("g_logger", "warning", &Logger::warning, &g_logger); + g_lua.bindSingletonFunction("g_logger", "error", &Logger::error, &g_logger); + g_lua.bindSingletonFunction("g_logger", "fatal", &Logger::fatal, &g_logger); // UI g_lua.registerSingletonClass("g_ui"); @@ -593,7 +605,6 @@ void Application::registerLuaFunctions() // ModuleManager g_lua.registerSingletonClass("g_modules"); - g_lua.bindSingletonFunction("g_modules", "discoverModulesPath", &ModuleManager::discoverModulesPath, &g_modules); g_lua.bindSingletonFunction("g_modules", "discoverModules", &ModuleManager::discoverModules, &g_modules); g_lua.bindSingletonFunction("g_modules", "autoLoadModules", &ModuleManager::autoLoadModules, &g_modules); g_lua.bindSingletonFunction("g_modules", "discoverModule", &ModuleManager::discoverModule, &g_modules); @@ -602,7 +613,6 @@ void Application::registerLuaFunctions() g_lua.bindSingletonFunction("g_modules", "reloadModules", &ModuleManager::reloadModules, &g_modules); g_lua.bindSingletonFunction("g_modules", "getModule", &ModuleManager::getModule, &g_modules); g_lua.bindSingletonFunction("g_modules", "getModules", &ModuleManager::getModules, &g_modules); - g_lua.bindSingletonFunction("g_modules", "getModulesPath", &ModuleManager::getModulesPath, &g_modules); // FontManager g_lua.registerSingletonClass("g_fonts"); @@ -633,11 +643,9 @@ void Application::registerLuaFunctions() // ResourceManager g_lua.registerSingletonClass("g_resources"); g_lua.bindSingletonFunction("g_resources", "addToSearchPath", &ResourceManager::addToSearchPath, &g_resources); + g_lua.bindSingletonFunction("g_resources", "setupWriteDir", &ResourceManager::setupWriteDir, &g_resources); g_lua.bindSingletonFunction("g_resources", "removeFromSearchPath", &ResourceManager::removeFromSearchPath, &g_resources); g_lua.bindSingletonFunction("g_resources", "fileExists", &ResourceManager::fileExists, &g_resources); - - // LuaInterface - g_lua.registerSingletonClass("g_lua"); - g_lua.bindSingletonFunction("g_lua", "getCurrentSourcePath", &LuaInterface::getCurrentSourcePath, &g_lua); - + g_lua.bindSingletonFunction("g_resources", "getRealDir", &ResourceManager::getRealDir, &g_resources); + g_lua.bindSingletonFunction("g_resources", "getWorkDir", &ResourceManager::getWorkDir, &g_resources); } diff --git a/src/framework/luascript/luainterface.cpp b/src/framework/luascript/luainterface.cpp index 0a6281be..65c1e416 100644 --- a/src/framework/luascript/luainterface.cpp +++ b/src/framework/luascript/luainterface.cpp @@ -292,6 +292,17 @@ int LuaInterface::luaObjectCollectEvent(LuaInterface* lua) /////////////////////////////////////////////////////////////////////////////// +bool LuaInterface::safeRunScript(const std::string& fileName) +{ + try { + runScript(fileName); + return true; + } catch(LuaException& e) { + g_logger.error(stdext::format("Failed to load script '%s': %s", fileName, e.what())); + return false; + } +} + void LuaInterface::runScript(const std::string& fileName) { loadScript(fileName); @@ -312,7 +323,7 @@ void LuaInterface::loadScript(const std::string& fileName) filePath = getCurrentSourcePath() + "/" + filePath; try { - std::string buffer = g_resources.loadFile(filePath); + std::string buffer = g_resources.loadFile(fileName); std::string source = "@" + filePath; loadBuffer(buffer, source); } catch(stdext::exception& e) { diff --git a/src/framework/luascript/luainterface.h b/src/framework/luascript/luainterface.h index d0178276..bb4509c2 100644 --- a/src/framework/luascript/luainterface.h +++ b/src/framework/luascript/luainterface.h @@ -132,6 +132,9 @@ private: static int luaObjectCollectEvent(LuaInterface* lua); public: + /// Loads and runs a script, any errors are printed to stdout and returns false + bool safeRunScript(const std::string& fileName); + /// Loads and runs a script /// @exception LuaException is thrown on any lua error void runScript(const std::string& fileName); diff --git a/src/framework/luascript/luaobject.cpp b/src/framework/luascript/luaobject.cpp index dcf208b3..c48a09b8 100644 --- a/src/framework/luascript/luaobject.cpp +++ b/src/framework/luascript/luaobject.cpp @@ -31,7 +31,7 @@ LuaObject::LuaObject() : m_fieldsTableRef(-1) LuaObject::~LuaObject() { - assert(!g_app->isTermianted()); + assert(!g_app.isTermianted()); releaseLuaFieldsTable(); } diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp index 79977046..8828a561 100644 --- a/src/framework/net/connection.cpp +++ b/src/framework/net/connection.cpp @@ -41,7 +41,7 @@ Connection::Connection() : Connection::~Connection() { - assert(!g_app->isTermianted()); + assert(!g_app.isTermianted()); close(); } diff --git a/src/framework/platform/unixcrashhandler.cpp b/src/framework/platform/unixcrashhandler.cpp index 8c6b7442..86f20b2e 100644 --- a/src/framework/platform/unixcrashhandler.cpp +++ b/src/framework/platform/unixcrashhandler.cpp @@ -41,8 +41,8 @@ void crashHandler(int signum, siginfo_t* info, void* secret) g_logger.error("Application crashed"); std::stringstream ss; - ss << stdext::format("app name: %s\n", g_app->getName()); - ss << stdext::format("app version: %s\n", g_app->getVersion()); + ss << stdext::format("app name: %s\n", g_app.getName()); + ss << stdext::format("app version: %s\n", g_app.getVersion()); ss << stdext::format("build compiler: %s\n", BUILD_COMPILER); ss << stdext::format("build date: %s\n", BUILD_DATE); ss << stdext::format("build type: %s\n", BUILD_TYPE); diff --git a/src/framework/platform/win32crashhandler.cpp b/src/framework/platform/win32crashhandler.cpp index 7537358a..aabe8bc8 100644 --- a/src/framework/platform/win32crashhandler.cpp +++ b/src/framework/platform/win32crashhandler.cpp @@ -111,8 +111,8 @@ LONG CALLBACK ExceptionHandler(LPEXCEPTION_POINTERS e) SymInitialize(GetCurrentProcess(), 0, TRUE); std::stringstream ss; ss << "== application crashed\n"; - ss << stdext::format("app name: %s\n", g_app->getName().c_str()); - ss << stdext::format("app version: %s\n", g_app->getVersion().c_str()); + ss << stdext::format("app name: %s\n", g_app.getName().c_str()); + ss << stdext::format("app version: %s\n", g_app.getVersion().c_str()); ss << stdext::format("build compiler: %s\n", BUILD_COMPILER); ss << stdext::format("build date: %s\n", BUILD_DATE); ss << stdext::format("build type: %s\n", BUILD_TYPE); diff --git a/src/framework/platform/win32window.cpp b/src/framework/platform/win32window.cpp index d88fa9ef..75f725e6 100644 --- a/src/framework/platform/win32window.cpp +++ b/src/framework/platform/win32window.cpp @@ -230,7 +230,7 @@ void WIN32Window::terminate() } if(m_instance) { - if(!UnregisterClassA(g_app->getName().c_str(), m_instance)) + if(!UnregisterClassA(g_app.getName().c_str(), m_instance)) g_logger.error("UnregisterClassA failed"); m_instance = NULL; } @@ -256,7 +256,7 @@ void WIN32Window::internalCreateWindow() wc.hCursor = m_defaultCursor; wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; - wc.lpszClassName = g_app->getName().c_str(); + wc.lpszClassName = g_app.getName().c_str(); if(!RegisterClassA(&wc)) g_logger.fatal("Failed to register the window class."); @@ -272,7 +272,7 @@ void WIN32Window::internalCreateWindow() updateUnmaximizedCoords(); m_window = CreateWindowExA(dwExStyle, - g_app->getName().c_str(), + g_app.getName().c_str(), NULL, dwStyle, windowRect.left, diff --git a/src/framework/ui/uimanager.cpp b/src/framework/ui/uimanager.cpp index db04f02a..d0ea2ac8 100644 --- a/src/framework/ui/uimanager.cpp +++ b/src/framework/ui/uimanager.cpp @@ -336,7 +336,7 @@ void UIManager::importStyleFromOTML(const OTMLNodePtr& styleNode) } // Warn about redefined styles - if(!g_app->isRunning() && !unique) { + if(!g_app.isRunning() && !unique) { auto it = m_styles.find(name); if(it != m_styles.end()) g_logger.warning(stdext::format("style '%s' is being redefined", name)); diff --git a/src/framework/ui/uitextedit.cpp b/src/framework/ui/uitextedit.cpp index 4e37c025..1a2cbd7d 100644 --- a/src/framework/ui/uitextedit.cpp +++ b/src/framework/ui/uitextedit.cpp @@ -253,7 +253,7 @@ void UITextEdit::setCursorPos(int pos) else m_cursorPos = pos; update(); - g_app->repaint(); + g_app.repaint(); } } @@ -516,5 +516,5 @@ bool UITextEdit::onMousePress(const Point& mousePos, Fw::MouseButton button) void UITextEdit::blinkCursor() { m_cursorTicks = g_clock.millis(); - g_app->repaint(); + g_app.repaint(); } diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp index ac9a66d3..bea52718 100644 --- a/src/framework/ui/uiwidget.cpp +++ b/src/framework/ui/uiwidget.cpp @@ -46,7 +46,7 @@ UIWidget::UIWidget() UIWidget::~UIWidget() { - assert(!g_app->isTermianted()); + assert(!g_app.isTermianted()); #ifdef DEBUG if(!m_destroyed) g_logger.warning(stdext::format("widget '%s' was not explicitly destroyed", m_id)); @@ -1375,7 +1375,7 @@ void UIWidget::onStyleApply(const std::string& styleName, const OTMLNodePtr& sty parseImageStyle(styleNode); parseTextStyle(styleNode); - g_app->repaint(); + g_app.repaint(); } void UIWidget::onGeometryChange(const Rect& oldRect, const Rect& newRect) @@ -1391,7 +1391,7 @@ void UIWidget::onGeometryChange(const Rect& oldRect, const Rect& newRect) callLuaField("onGeometryChange", oldRect, newRect); - g_app->repaint(); + g_app.repaint(); } void UIWidget::onLayoutUpdate() diff --git a/src/framework/ui/uiwidgettext.cpp b/src/framework/ui/uiwidgettext.cpp index 50575cca..10f1bffd 100644 --- a/src/framework/ui/uiwidgettext.cpp +++ b/src/framework/ui/uiwidgettext.cpp @@ -94,7 +94,7 @@ void UIWidget::drawText(const Rect& screenCoords) void UIWidget::onTextChange(const std::string& text, const std::string& oldText) { - g_app->repaint(); + g_app.repaint(); callLuaField("onTextChange", text, oldText); } diff --git a/src/main.cpp b/src/main.cpp index 0cd538ed..cdfa50f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,15 +20,32 @@ * THE SOFTWARE. */ -#include +#include "framework/application.h" +#include "framework/luascript/luainterface.h" +#include "framework/core/resourcemanager.h" +#include "otclient/otclient.h" int main(int argc, const char* argv[]) { std::vector args(argv, argv + argc); - OTClient app; - app.init(args); - app.run(); - app.deinit(); - app.terminate(); + + // initialize application framework and otclient + g_app.init(args); + g_otclient.init(args); + + // find script init.lua and run it + g_resources.discoverWorkDir("otclient", "init.lua"); + if(!g_lua.safeRunScript(g_resources.getWorkDir() + "init.lua")) + g_logger.fatal("Unable to run script init.lua!"); + + // the run application main loop + g_app.run(); + + // unload modules + g_app.deinit(); + + // terminate everything and free memory + g_otclient.terminate(); + g_app.terminate(); return 0; } diff --git a/src/otclient/const.h b/src/otclient/const.h index de067a0b..6da38ff3 100644 --- a/src/otclient/const.h +++ b/src/otclient/const.h @@ -25,10 +25,6 @@ namespace Otc { - constexpr const char* AppName = "OTClient"; - constexpr const char* AppCompactName = "otclient"; - constexpr const char* AppVersion = "0.4.0_dev"; - enum { TILE_PIXELS = 32, MAX_ELEVATION = 24, diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 5ef0cd96..b3dbdc4f 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -1033,7 +1033,7 @@ bool Game::checkBotProtection() #ifdef BOT_PROTECTION // accepts calls comming from a stacktrace containing only C++ functions, // if the stacktrace contains a lua function, then only accept if the engine is processing an input event - if(m_denyBotCall && g_lua.isInCppCallback() && !g_app->isOnInputEvent()) { + if(m_denyBotCall && g_lua.isInCppCallback() && !g_app.isOnInputEvent()) { g_logger.error(g_lua.traceback("caught a lua call to a bot protected game function, the call was canceled")); return false; } diff --git a/src/otclient/core/map.cpp b/src/otclient/core/map.cpp index d8cda4bc..d3199068 100644 --- a/src/otclient/core/map.cpp +++ b/src/otclient/core/map.cpp @@ -52,13 +52,15 @@ void Map::notificateTileUpdateToMapViews(const Position& pos) mapView->onTileUpdate(pos); } -void Map::load() +bool Map::load(const std::string& fileName) { - if(!g_resources.fileExists("/map.otcmap")) - return; + if(!g_resources.fileExists(fileName)) { + g_logger.error(stdext::format("Unable to load map '%s'", fileName)); + return false; + } std::stringstream in; - g_resources.loadFile("/map.otcmap", in); + g_resources.loadFile(fileName, in); while(!in.eof()) { Position pos; @@ -77,9 +79,11 @@ void Map::load() in.read((char*)&id, sizeof(id)); } } + + return true; } -void Map::save() +void Map::save(const std::string& fileName) { std::stringstream out; @@ -104,7 +108,7 @@ void Map::save() out.write((char*)&id, sizeof(id)); } - g_resources.saveFile("/map.otcmap", out); + g_resources.saveFile(fileName, out); } void Map::clean() diff --git a/src/otclient/core/map.h b/src/otclient/core/map.h index e0680b2d..60c69704 100644 --- a/src/otclient/core/map.h +++ b/src/otclient/core/map.h @@ -34,8 +34,8 @@ public: void removeMapView(const MapViewPtr& mapView); void notificateTileUpdateToMapViews(const Position& pos); - void load(); - void save(); + bool load(const std::string& fileName); + void save(const std::string& fileName); void clean(); void cleanDynamicThings(); void cleanTexts(); diff --git a/src/otclient/core/mapview.cpp b/src/otclient/core/mapview.cpp index d3121b9e..55d7e91e 100644 --- a/src/otclient/core/mapview.cpp +++ b/src/otclient/core/mapview.cpp @@ -51,7 +51,7 @@ MapView::MapView() MapView::~MapView() { - assert(!g_app->isTermianted()); + assert(!g_app.isTermianted()); } void MapView::draw(const Rect& rect) diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index 1a2372a8..8b0f4e6c 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -47,8 +47,6 @@ void OTClient::registerLuaFunctions() { - Application::registerLuaFunctions(); - g_lua.registerSingletonClass("g_thingsType"); g_lua.bindSingletonFunction("g_thingsType", "load", &ThingsType::load, &g_thingsType); g_lua.bindSingletonFunction("g_thingsType", "isLoaded", &ThingsType::isLoaded, &g_thingsType); @@ -78,6 +76,8 @@ void OTClient::registerLuaFunctions() g_lua.bindSingletonFunction("g_map", "removeCreatureById", &Map::removeCreatureById, &g_map); g_lua.bindSingletonFunction("g_map", "getSpectators", &Map::getSpectators, &g_map); g_lua.bindSingletonFunction("g_map", "findPath", &Map::findPath, &g_map); + g_lua.bindSingletonFunction("g_map", "load", &Map::load, &g_map); + g_lua.bindSingletonFunction("g_map", "save", &Map::save, &g_map); g_lua.registerSingletonClass("g_game"); g_lua.bindSingletonFunction("g_game", "loginWorld", &Game::loginWorld, &g_game); @@ -327,6 +327,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("addThing", &Tile::addThing); g_lua.bindClassMemberFunction("getThing", &Tile::getThing); g_lua.bindClassMemberFunction("getThingStackpos", &Tile::getThingStackpos); + g_lua.bindClassMemberFunction("getThingCount", &Tile::getThingCount); g_lua.bindClassMemberFunction("getTopThing", &Tile::getTopThing); g_lua.bindClassMemberFunction("removeThing", &Tile::removeThing); g_lua.bindClassMemberFunction("getTopLookThing", &Tile::getTopLookThing); diff --git a/src/otclient/otclient.cpp b/src/otclient/otclient.cpp index e1833ec9..676fd7e9 100644 --- a/src/otclient/otclient.cpp +++ b/src/otclient/otclient.cpp @@ -24,94 +24,55 @@ #include #include #include -#include "core/game.h" -#include "core/map.h" -#include "core/shadermanager.h" +#include +#include +#include +#include -OTClient::OTClient() : Application(Otc::AppCompactName) -{ - m_appVersion = Otc::AppVersion; -} +OTClient g_otclient; void OTClient::init(const std::vector& args) { - std::string startupOptions; - for(uint i=1;i 0) - g_logger.info(stdext::format("Startup options: %s", startupOptions)); - - g_logger.setLogFile(stdext::format("%s.txt", Otc::AppCompactName)); - Application::init(args); + // register needed lua functions + registerLuaFunctions(); g_shaders.init(); - g_modules.discoverModules(); - - // core modules 0-99 - g_modules.autoLoadModules(99); - g_modules.ensureModuleLoaded("core_lib"); - // client modules 100-499 - g_modules.autoLoadModules(499); - g_modules.ensureModuleLoaded("client"); - // game modules 500-999 - g_modules.autoLoadModules(999); - g_modules.ensureModuleLoaded("game"); - // addons 1000-9999 - g_modules.autoLoadModules(9999); + //TODO: restore options +/* + if(g_graphics.parseOption(arg)) + continue; - // load otclientrc.lua - if(g_resources.fileExists("/otclientrc.lua")) { - try { - g_lua.runScript("/otclientrc.lua"); - } catch(LuaException& e) { - g_logger.error(stdext::format("failed to load otclientrc.lua: %s", e.what())); - } + if(arg == "-version" || arg == "--version" || arg == "-v") { + stdext::print( + m_appName, " ", m_appVersion, "\n" + "Buitt on: ", BUILD_DATE, "\n", + "Revision: ", BUILD_REVISION, "\n", + "Compiled by: ", BUILD_COMPILER, "\n", + "Build type: ", BUILD_TYPE, "\n"); + return; + } else if(arg == "-help" || arg == "--help" || arg == "-h" || arg == "-?" || arg == "/?") { + stdext::print( + "Usage: ", args[0], " [options]\n" + "Options:\n" + " -help Display this information and exit\n" + " -version Display version and exit\n" + " \n" + " -no-fbos Disable usage of opengl framebuffer objects\n" + " -no-mipmaps Disable texture mipmaping\n" + " -no-smooth Disable texture smoothing (bilinear filter)\n" + " -no-non-power-of-two-textures Use only power of two textures\n" + " -no-clamp-to-edge Don't use GL_CLAMP_TO_EDGE\n" + " -no-backbuffer-cache Don't allow backbuffer caching\n" + " -hardware-buffers Cache vertex arrays in hardware\n" + " -opengl1 Use OpenGL 1.x painter\n" + " -opengl2 Use OpenGL 2.0 painter\n"); + return; + } else { + stdext::println("Unrecognized option '", arg, "', please see -help for available options list"); + return; } + */ } void OTClient::terminate() @@ -119,5 +80,4 @@ void OTClient::terminate() g_shaders.terminate(); g_map.clean(); g_thingsType.unload(); - Application::terminate(); } diff --git a/src/otclient/otclient.h b/src/otclient/otclient.h index 2e6ed4e1..355af86f 100644 --- a/src/otclient/otclient.h +++ b/src/otclient/otclient.h @@ -23,16 +23,16 @@ #ifndef OTCLIENT_H #define OTCLIENT_H -#include #include -class OTClient : public Application +class OTClient { public: - OTClient(); void init(const std::vector& args); void terminate(); void registerLuaFunctions(); }; +extern OTClient g_otclient; + #endif