| @@ -0,0 +1,596 @@ | |||
| 1 | + | # framework flags | |
| 2 | + | # FRAMEWORK_SOUND | |
| 3 | + | # FRAMEWORK_GRAPHICS | |
| 4 | + | # FRAMEWORK_NET | |
| 5 | + | # FRAMEWORK_XML | |
| 6 | + | ||
| 7 | + | # CMAKE_CURRENT_LIST_DIR cmake 2.6 compatibility | |
| 8 | + | if(${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 6) | |
| 9 | + | get_filename_component(CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_FILE} PATH) | |
| 10 | + | endif(${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 6) | |
| 11 | + | ||
| 12 | + | # add framework cmake modules | |
| 13 | + | set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake;${CMAKE_MODULE_PATH}") | |
| 14 | + | ||
| 15 | + | set(framework_LIBRARIES "") | |
| 16 | + | set(framework_DEFINITIONS "") | |
| 17 | + | set(framework_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/..") | |
| 18 | + | set(framework_SOURCES ${framework_SOURCES} | |
| 19 | + | ${CMAKE_CURRENT_LIST_DIR}/const.h | |
| 20 | + | ${CMAKE_CURRENT_LIST_DIR}/global.h | |
| 21 | + | ${CMAKE_CURRENT_LIST_DIR}/pch.h | |
| 22 | + | ${CMAKE_CURRENT_LIST_DIR}/luafunctions.cpp | |
| 23 | + | ||
| 24 | + | ${CMAKE_CURRENT_LIST_DIR}/util/color.cpp | |
| 25 | + | ${CMAKE_CURRENT_LIST_DIR}/util/color.h | |
| 26 | + | ${CMAKE_CURRENT_LIST_DIR}/util/crypt.cpp | |
| 27 | + | ${CMAKE_CURRENT_LIST_DIR}/util/crypt.h | |
| 28 | + | ${CMAKE_CURRENT_LIST_DIR}/util/databuffer.h | |
| 29 | + | ${CMAKE_CURRENT_LIST_DIR}/util/extras.cpp | |
| 30 | + | ${CMAKE_CURRENT_LIST_DIR}/util/extras.h | |
| 31 | + | ${CMAKE_CURRENT_LIST_DIR}/util/pngunpacker.cpp | |
| 32 | + | ${CMAKE_CURRENT_LIST_DIR}/util/pngunpacker.h | |
| 33 | + | ${CMAKE_CURRENT_LIST_DIR}/util/framecounter.h | |
| 34 | + | ${CMAKE_CURRENT_LIST_DIR}/util/matrix.h | |
| 35 | + | ${CMAKE_CURRENT_LIST_DIR}/util/point.h | |
| 36 | + | ${CMAKE_CURRENT_LIST_DIR}/util/qrcodegen.c | |
| 37 | + | ${CMAKE_CURRENT_LIST_DIR}/util/qrcodegen.h | |
| 38 | + | ${CMAKE_CURRENT_LIST_DIR}/util/rect.h | |
| 39 | + | ${CMAKE_CURRENT_LIST_DIR}/util/size.h | |
| 40 | + | ${CMAKE_CURRENT_LIST_DIR}/util/stats.cpp | |
| 41 | + | ${CMAKE_CURRENT_LIST_DIR}/util/stats.h | |
| 42 | + | ||
| 43 | + | # stdext | |
| 44 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/any.h | |
| 45 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/boolean.h | |
| 46 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/cast.h | |
| 47 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/compiler.h | |
| 48 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/demangle.cpp | |
| 49 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/demangle.h | |
| 50 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/dumper.h | |
| 51 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/dynamic_storage.h | |
| 52 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/exception.h | |
| 53 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/fastrand.h | |
| 54 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/format.h | |
| 55 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/math.cpp | |
| 56 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/math.h | |
| 57 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/net.cpp | |
| 58 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/net.h | |
| 59 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/packed_any.h | |
| 60 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/packed_storage.h | |
| 61 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/shared_object.h | |
| 62 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/stdext.h | |
| 63 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/string.cpp | |
| 64 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/string.h | |
| 65 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/thread.h | |
| 66 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/time.cpp | |
| 67 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/time.h | |
| 68 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/traits.h | |
| 69 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/types.h | |
| 70 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/uri.cpp | |
| 71 | + | ${CMAKE_CURRENT_LIST_DIR}/stdext/uri.h | |
| 72 | + | ||
| 73 | + | # core | |
| 74 | + | ${CMAKE_CURRENT_LIST_DIR}/core/adaptiverenderer.cpp | |
| 75 | + | ${CMAKE_CURRENT_LIST_DIR}/core/adaptiverenderer.h | |
| 76 | + | ${CMAKE_CURRENT_LIST_DIR}/core/application.cpp | |
| 77 | + | ${CMAKE_CURRENT_LIST_DIR}/core/application.h | |
| 78 | + | ${CMAKE_CURRENT_LIST_DIR}/core/asyncdispatcher.cpp | |
| 79 | + | ${CMAKE_CURRENT_LIST_DIR}/core/asyncdispatcher.h | |
| 80 | + | ${CMAKE_CURRENT_LIST_DIR}/core/binarytree.cpp | |
| 81 | + | ${CMAKE_CURRENT_LIST_DIR}/core/binarytree.h | |
| 82 | + | ${CMAKE_CURRENT_LIST_DIR}/core/clock.cpp | |
| 83 | + | ${CMAKE_CURRENT_LIST_DIR}/core/clock.h | |
| 84 | + | ${CMAKE_CURRENT_LIST_DIR}/core/config.cpp | |
| 85 | + | ${CMAKE_CURRENT_LIST_DIR}/core/config.h | |
| 86 | + | ${CMAKE_CURRENT_LIST_DIR}/core/configmanager.cpp | |
| 87 | + | ${CMAKE_CURRENT_LIST_DIR}/core/configmanager.h | |
| 88 | + | ${CMAKE_CURRENT_LIST_DIR}/core/declarations.h | |
| 89 | + | ${CMAKE_CURRENT_LIST_DIR}/core/event.cpp | |
| 90 | + | ${CMAKE_CURRENT_LIST_DIR}/core/event.h | |
| 91 | + | ${CMAKE_CURRENT_LIST_DIR}/core/eventdispatcher.cpp | |
| 92 | + | ${CMAKE_CURRENT_LIST_DIR}/core/eventdispatcher.h | |
| 93 | + | ${CMAKE_CURRENT_LIST_DIR}/core/filestream.cpp | |
| 94 | + | ${CMAKE_CURRENT_LIST_DIR}/core/filestream.h | |
| 95 | + | ${CMAKE_CURRENT_LIST_DIR}/core/graphicalapplication.cpp | |
| 96 | + | ${CMAKE_CURRENT_LIST_DIR}/core/graphicalapplication.h | |
| 97 | + | ${CMAKE_CURRENT_LIST_DIR}/core/inputevent.h | |
| 98 | + | ${CMAKE_CURRENT_LIST_DIR}/core/logger.cpp | |
| 99 | + | ${CMAKE_CURRENT_LIST_DIR}/core/logger.h | |
| 100 | + | ${CMAKE_CURRENT_LIST_DIR}/core/module.cpp | |
| 101 | + | ${CMAKE_CURRENT_LIST_DIR}/core/module.h | |
| 102 | + | ${CMAKE_CURRENT_LIST_DIR}/core/modulemanager.cpp | |
| 103 | + | ${CMAKE_CURRENT_LIST_DIR}/core/modulemanager.h | |
| 104 | + | ${CMAKE_CURRENT_LIST_DIR}/core/resourcemanager.cpp | |
| 105 | + | ${CMAKE_CURRENT_LIST_DIR}/core/resourcemanager.h | |
| 106 | + | ${CMAKE_CURRENT_LIST_DIR}/core/scheduledevent.cpp | |
| 107 | + | ${CMAKE_CURRENT_LIST_DIR}/core/scheduledevent.h | |
| 108 | + | ${CMAKE_CURRENT_LIST_DIR}/core/timer.cpp | |
| 109 | + | ${CMAKE_CURRENT_LIST_DIR}/core/timer.h | |
| 110 | + | ||
| 111 | + | # luaengine | |
| 112 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/declarations.h | |
| 113 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/lbitlib.cpp | |
| 114 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/lbitlib.h | |
| 115 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/luabinder.h | |
| 116 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/luaexception.cpp | |
| 117 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/luaexception.h | |
| 118 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/luainterface.cpp | |
| 119 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/luainterface.h | |
| 120 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/luaobject.cpp | |
| 121 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/luaobject.h | |
| 122 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/luavaluecasts.cpp | |
| 123 | + | ${CMAKE_CURRENT_LIST_DIR}/luaengine/luavaluecasts.h | |
| 124 | + | ||
| 125 | + | # otml | |
| 126 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/declarations.h | |
| 127 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otml.h | |
| 128 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otmldocument.cpp | |
| 129 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otmldocument.h | |
| 130 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otmlemitter.cpp | |
| 131 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otmlemitter.h | |
| 132 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otmlexception.cpp | |
| 133 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otmlexception.h | |
| 134 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otmlnode.cpp | |
| 135 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otmlnode.h | |
| 136 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otmlparser.cpp | |
| 137 | + | ${CMAKE_CURRENT_LIST_DIR}/otml/otmlparser.h | |
| 138 | + | ||
| 139 | + | # crash handler | |
| 140 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/crashhandler.h | |
| 141 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/unixcrashhandler.cpp | |
| 142 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/win32crashhandler.cpp | |
| 143 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/win32platform.cpp | |
| 144 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/unixplatform.cpp | |
| 145 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/platform.cpp | |
| 146 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/platform.h | |
| 147 | + | ) | |
| 148 | + | ||
| 149 | + | set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/luafunctions.cpp | |
| 150 | + | PROPERTIES LANGUAGE CXX COMPILE_FLAGS "-g0 -Os") | |
| 151 | + | ||
| 152 | + | # some build options | |
| 153 | + | option(LUAJIT "Use lua jit" ON) | |
| 154 | + | if(NOT APPLE) | |
| 155 | + | option(CRASH_HANDLER "Generate crash reports" ON) | |
| 156 | + | option(USE_STATIC_LIBS "Don't use shared libraries (dlls)" ON) | |
| 157 | + | option(USE_LIBCPP "Use the new libc++ library instead of stdc++" OFF) | |
| 158 | + | option(USE_LTO "Use link time optimizations" OFF) | |
| 159 | + | else() | |
| 160 | + | set(CRASH_HANDLER OFF) | |
| 161 | + | set(USE_STATIC_LIBS ON) | |
| 162 | + | set(USE_LIBCPP ON) | |
| 163 | + | endif() | |
| 164 | + | set(BUILD_COMMIT "devel" CACHE "Git commit string (intended for releases)" STRING) | |
| 165 | + | set(BUILD_REVISION 0 CACHE "Git revision string (intended for releases)" STRING) | |
| 166 | + | ||
| 167 | + | # default build type | |
| 168 | + | if(NOT CMAKE_BUILD_TYPE) | |
| 169 | + | set(CMAKE_BUILD_TYPE "release") | |
| 170 | + | endif() | |
| 171 | + | ||
| 172 | + | set (CMAKE_CXX_STANDARD 17) | |
| 173 | + | ||
| 174 | + | # gcc compile flags | |
| 175 | + | set(WARNS_FLAGS "-Wall -Wextra -Wno-unused -Wno-unused-parameter -Wno-unused-result") | |
| 176 | + | if(WASM) | |
| 177 | + | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNS_FLAGS} ${ARCH_FLAGS} -s USE_ZLIB=1 -s USE_LIBPNG=1 -s USE_SDL=2 -s USE_BOOST_HEADERS=1 -s USE_PTHREADS=1 -O1") # add -Ofast | |
| 178 | + | set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} | |
| 179 | + | D:\\wasm\\physfs\\src | |
| 180 | + | D:\\wasm\\lua-lua-5.1\\src) | |
| 181 | + | set(framework_LIBRARIES ${framework_LIBRARIES} "D:/wasm/physfs/build/libphysfs.a") | |
| 182 | + | ||
| 183 | + | include_directories("D:\\wasm\\lua-lua-5.1") | |
| 184 | + | set( LUA_DIR "D:/wasm/lua-lua-5.1/src" ) | |
| 185 | + | set ( LUA_SRC ${LUA_DIR}/lapi.c ${LUA_DIR}/lcode.c ${LUA_DIR}/ldebug.c ${LUA_DIR}/ldo.c ${LUA_DIR}/ldump.c ${LUA_DIR}/lfunc.c | |
| 186 | + | ${LUA_DIR}/lgc.c ${LUA_DIR}/llex.c ${LUA_DIR}/lmem.c ${LUA_DIR}/lobject.c ${LUA_DIR}/lopcodes.c ${LUA_DIR}/lparser.c ${LUA_DIR}/lstate.c | |
| 187 | + | ${LUA_DIR}/lstring.c ${LUA_DIR}/ltable.c ${LUA_DIR}/ltm.c ${LUA_DIR}/lundump.c ${LUA_DIR}/lvm.c ${LUA_DIR}/lzio.c ${LUA_DIR}/lauxlib.c | |
| 188 | + | ${LUA_DIR}/lbaselib.c ${LUA_DIR}/ldblib.c ${LUA_DIR}/liolib.c ${LUA_DIR}/lmathlib.c ${LUA_DIR}/loslib.c ${LUA_DIR}/ltablib.c | |
| 189 | + | ${LUA_DIR}/lstrlib.c ${LUA_DIR}/linit.c ) | |
| 190 | + | set_source_files_properties(${LUA_SRC} PROPERTIES LANGUAGE CXX ) | |
| 191 | + | set(framework_SOURCES ${framework_SOURCES} ${LUA_SRC}) | |
| 192 | + | message(STATUS "LUA: " ${LUA_SRC}) | |
| 193 | + | else() | |
| 194 | + | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNS_FLAGS} ${ARCH_FLAGS} -pipe") | |
| 195 | + | set(CMAKE_CXX_FLAGS_COMPILESPEED "-O0") | |
| 196 | + | set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") | |
| 197 | + | set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O1 -g -fno-omit-frame-pointer") | |
| 198 | + | set(CMAKE_CXX_FLAGS_RELEASE "-O2") | |
| 199 | + | set(CMAKE_CXX_FLAGS_PERFORMANCE "-Ofast -march=native") | |
| 200 | + | endif() | |
| 201 | + | ||
| 202 | + | if(USE_LTO) | |
| 203 | + | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fwhole-program -flto") | |
| 204 | + | if(WIN32) | |
| 205 | + | set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -Wl,-O1,--gc-sections,--sort-common,--relax") | |
| 206 | + | else() | |
| 207 | + | set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -Wl,-O1,--gc-sections,--sort-common,--relax,-z,relro") | |
| 208 | + | endif() | |
| 209 | + | endif() | |
| 210 | + | ||
| 211 | + | # process options | |
| 212 | + | if(USE_STATIC_LIBS) | |
| 213 | + | if(NOT APPLE) | |
| 214 | + | set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -static-libgcc -static-libstdc++") | |
| 215 | + | endif() | |
| 216 | + | message(STATUS "Link to static libraries: ON") | |
| 217 | + | set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") | |
| 218 | + | else() | |
| 219 | + | message(STATUS "Link to static libraries: OFF") | |
| 220 | + | endif() | |
| 221 | + | ||
| 222 | + | message(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) | |
| 223 | + | add_definitions(-D"BUILD_TYPE=\\\"${CMAKE_BUILD_TYPE}\\\"") | |
| 224 | + | message(STATUS "Build commit: ${BUILD_COMMIT}") | |
| 225 | + | add_definitions(-D"BUILD_COMMIT=\\\"${BUILD_COMMIT}\\\"") | |
| 226 | + | message(STATUS "Build revision: ${BUILD_REVISION}") | |
| 227 | + | add_definitions(-D"BUILD_REVISION=${BUILD_REVISION}") | |
| 228 | + | ||
| 229 | + | message(STATUS "Build with encryption") | |
| 230 | + | add_definitions(-D"WITH_ENCRYPTION") | |
| 231 | + | ||
| 232 | + | # main libs | |
| 233 | + | if(NOT WASM) | |
| 234 | + | # find boost | |
| 235 | + | set(REQUIRED_BOOST_COMPONENTS system filesystem) | |
| 236 | + | set(Boost_USE_MULTITHREADED ON) | |
| 237 | + | set(Boost_USE_STATIC_LIBS ${USE_STATIC_LIBS}) | |
| 238 | + | find_package(Boost 1.67.0 COMPONENTS ${REQUIRED_BOOST_COMPONENTS} REQUIRED) | |
| 239 | + | ||
| 240 | + | #find zip | |
| 241 | + | find_package(LibZip REQUIRED) | |
| 242 | + | find_package(ZLIB REQUIRED) | |
| 243 | + | find_package(BZip2 REQUIRED) | |
| 244 | + | ||
| 245 | + | #find lua | |
| 246 | + | if(LUAJIT) | |
| 247 | + | find_package(LuaJIT REQUIRED) | |
| 248 | + | set(LUA_INCLUDE_DIR ${LUAJIT_INCLUDE_DIR}) | |
| 249 | + | set(LUA_LIBRARY ${LUAJIT_LIBRARY}) | |
| 250 | + | ||
| 251 | + | if(APPLE) | |
| 252 | + | set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -pagezero_size 10000 -image_base 100000000") | |
| 253 | + | endif() | |
| 254 | + | else() | |
| 255 | + | find_package(Lua REQUIRED) | |
| 256 | + | endif() | |
| 257 | + | message(STATUS "LuaJIT: " ${LUAJIT}) | |
| 258 | + | ||
| 259 | + | find_package(PhysFS REQUIRED) | |
| 260 | + | find_package(OpenSSL REQUIRED) | |
| 261 | + | find_package(ZLIB REQUIRED) | |
| 262 | + | ||
| 263 | + | set(framework_LIBRARIES ${framework_LIBRARIES} | |
| 264 | + | ${Boost_LIBRARIES} | |
| 265 | + | ${LUA_LIBRARY} | |
| 266 | + | ${PHYSFS_LIBRARY} | |
| 267 | + | ${OPENSSL_LIBRARIES} | |
| 268 | + | ${ZLIB_LIBRARY} | |
| 269 | + | ${LIBZIP_LIBRARY} | |
| 270 | + | ${ZLIB_LIBRARY} | |
| 271 | + | ${BZIP2_LIBRARIES} | |
| 272 | + | ) | |
| 273 | + | ||
| 274 | + | if(WIN32) | |
| 275 | + | set(framework_LIBRARIES ${framework_LIBRARIES} -lbcrypt -ldbghelp -lshlwapi -liphlpapi -lpsapi) | |
| 276 | + | endif() | |
| 277 | + | ||
| 278 | + | set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} | |
| 279 | + | ${Boost_INCLUDE_DIRS} | |
| 280 | + | ${LUA_INCLUDE_DIR} | |
| 281 | + | ${PHYSFS_INCLUDE_DIR} | |
| 282 | + | ${OPENSSL_INCLUDE_DIR} | |
| 283 | + | ${framework_INCLUDE_DIRS} | |
| 284 | + | ${LIBZIP_INCLUDE_DIR_ZIP} | |
| 285 | + | ${LIBZIP_INCLUDE_DIR_ZIPCONF} | |
| 286 | + | ${ZLIB_INCLUDE_DIR} | |
| 287 | + | ${BZIP2_INCLUDE_DIR} | |
| 288 | + | ) | |
| 289 | + | endif() | |
| 290 | + | ||
| 291 | + | ||
| 292 | + | if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") | |
| 293 | + | message(STATUS "Debug information: ON") | |
| 294 | + | else() | |
| 295 | + | message(STATUS "Debug information: OFF") | |
| 296 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -DNDEBUG) # NDEBUG disable asserts | |
| 297 | + | if(NOT APPLE) | |
| 298 | + | set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -Wl,-s") # strip all debug information | |
| 299 | + | if(NOT WASM) | |
| 300 | + | set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -Wl,--as-needed") # only link needed libraries | |
| 301 | + | endif() | |
| 302 | + | endif() | |
| 303 | + | endif() | |
| 304 | + | ||
| 305 | + | if(CRASH_HANDLER) | |
| 306 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -DCRASH_HANDLER) | |
| 307 | + | message(STATUS "Crash handler: ON") | |
| 308 | + | if(WIN32) | |
| 309 | + | set(framework_LIBRARIES ${framework_LIBRARIES} imagehlp) | |
| 310 | + | endif() | |
| 311 | + | else() | |
| 312 | + | message(STATUS "Crash handler: OFF") | |
| 313 | + | endif() | |
| 314 | + | ||
| 315 | + | if(USE_LIBCPP) | |
| 316 | + | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") | |
| 317 | + | endif() | |
| 318 | + | ||
| 319 | + | if(WIN32) | |
| 320 | + | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mthreads") | |
| 321 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -D_WIN32_WINNT=0x0501 -DWIN32) | |
| 322 | + | #set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -Wl,--large-address-aware") # strip all debug information | |
| 323 | + | set(SYSTEM_LIBRARIES "") | |
| 324 | + | else() | |
| 325 | + | if(APPLE) | |
| 326 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -D_REENTRANT) # enable thread safe code | |
| 327 | + | set(SYSTEM_LIBRARIES "") | |
| 328 | + | elseif(NOT WASM) | |
| 329 | + | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") | |
| 330 | + | set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -rdynamic -Wl,-rpath,./libs") # rdynamic is needed by backtrace.h used in crash handler | |
| 331 | + | set(SYSTEM_LIBRARIES dl rt) | |
| 332 | + | endif() | |
| 333 | + | endif() | |
| 334 | + | set(framework_LIBRARIES ${framework_LIBRARIES} ${SYSTEM_LIBRARIES}) | |
| 335 | + | ||
| 336 | + | if(FRAMEWORK_THREAD_SAFE) | |
| 337 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -DTHREAD_SAFE) | |
| 338 | + | endif() | |
| 339 | + | ||
| 340 | + | if(FRAMEWORK_GRAPHICS) | |
| 341 | + | set(OPENGLES "OFF" CACHE "Use OpenGL ES 1.0 or 2.0 (for mobiles devices)" STRING) | |
| 342 | + | set(OpenGL_GL_PREFERENCE "LEGACY") | |
| 343 | + | if(WASM) | |
| 344 | + | set(OPENGLES "2.0") | |
| 345 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -DOPENGL_ES=2) | |
| 346 | + | ELSEif(OPENGLES STREQUAL "2.0") | |
| 347 | + | find_package(OpenGLES2 REQUIRED) | |
| 348 | + | find_package(EGL REQUIRED) | |
| 349 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -DOPENGL_ES=2) | |
| 350 | + | set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${EGL_INCLUDE_DIR} ${OPENGLES2_INCLUDE_DIR}) | |
| 351 | + | set(framework_LIBRARIES ${framework_LIBRARIES} ${EGL_LIBRARY} ${OPENGLES2_LIBRARY}) | |
| 352 | + | ELSEif(OPENGLES STREQUAL "1.0") | |
| 353 | + | find_package(OpenGLES1 REQUIRED) | |
| 354 | + | find_package(EGL REQUIRED) | |
| 355 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -DOPENGL_ES=1) | |
| 356 | + | set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${EGL_INCLUDE_DIR} ${OPENGLES1_INCLUDE_DIR}) | |
| 357 | + | set(framework_LIBRARIES ${framework_LIBRARIES} ${EGL_LIBRARY} ${OPENGLES1_LIBRARY}) | |
| 358 | + | else() | |
| 359 | + | ## TODO: CMake Documentation says that this is not the right | |
| 360 | + | # Thing for Mac OS X, but it works for now. | |
| 361 | + | if(APPLE) | |
| 362 | + | include_directories(/usr/X11R6/include/) | |
| 363 | + | link_directories(/usr/X11R6/lib/) | |
| 364 | + | SET(OPENGL_LIBRARIES GL) | |
| 365 | + | else() | |
| 366 | + | if(USE_STATIC_LIBS) | |
| 367 | + | set(CMAKE_FIND_LIBRARY_SUFFIXES ".so") | |
| 368 | + | find_package(OpenGL REQUIRED) | |
| 369 | + | set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") | |
| 370 | + | else() | |
| 371 | + | find_package(OpenGL REQUIRED) | |
| 372 | + | endif() | |
| 373 | + | endif() | |
| 374 | + | set(USE_STATIC_LIBS OFF) | |
| 375 | + | find_package(GLEW REQUIRED) | |
| 376 | + | set(USE_STATIC_LIBS ON) | |
| 377 | + | set(framework_LIBRARIES ${framework_LIBRARIES} ${GLEW_LIBRARY} ${OPENGL_LIBRARIES}) | |
| 378 | + | MESSAGE(${framework_LIBRARIES}) | |
| 379 | + | endif() | |
| 380 | + | message(STATUS "OpenGL ES: " ${OPENGLES}) | |
| 381 | + | ||
| 382 | + | if(WIN32) | |
| 383 | + | option(WINDOWS_CONSOLE "Enables console window on Windows platform" OFF) | |
| 384 | + | if(WINDOWS_CONSOLE) | |
| 385 | + | message(STATUS "Windows console: ON") | |
| 386 | + | else() | |
| 387 | + | set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -mwindows") | |
| 388 | + | message(STATUS "Windows console: OFF") | |
| 389 | + | endif() | |
| 390 | + | elseif(NOT WASM) | |
| 391 | + | set(framework_LIBRARIES ${framework_LIBRARIES} X11) | |
| 392 | + | endif() | |
| 393 | + | ||
| 394 | + | set(framework_SOURCES ${framework_SOURCES} | |
| 395 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/animatedtexture.cpp | |
| 396 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/animatedtexture.h | |
| 397 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/apngloader.cpp | |
| 398 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/apngloader.h | |
| 399 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/atlas.cpp | |
| 400 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/atlas.h | |
| 401 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/bitmapfont.cpp | |
| 402 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/bitmapfont.h | |
| 403 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/cachedtext.cpp | |
| 404 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/cachedtext.h | |
| 405 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/colorarray.h | |
| 406 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/coordsbuffer.cpp | |
| 407 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/coordsbuffer.h | |
| 408 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/declarations.h | |
| 409 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/deptharray.h | |
| 410 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/drawcache.cpp | |
| 411 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/drawcache.h | |
| 412 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/drawqueue.cpp | |
| 413 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/drawqueue.h | |
| 414 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/fontmanager.cpp | |
| 415 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/fontmanager.h | |
| 416 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/framebuffer.cpp | |
| 417 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/framebuffer.h | |
| 418 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/framebuffermanager.cpp | |
| 419 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/framebuffermanager.h | |
| 420 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/glutil.h | |
| 421 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/graphics.cpp | |
| 422 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/graphics.h | |
| 423 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/graph.cpp | |
| 424 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/graph.h | |
| 425 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/hardwarebuffer.cpp | |
| 426 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/hardwarebuffer.h | |
| 427 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/image.cpp | |
| 428 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/image.h | |
| 429 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/painter.cpp | |
| 430 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/painter.h | |
| 431 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/paintershaderprogram.cpp | |
| 432 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/paintershaderprogram.h | |
| 433 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/shader.cpp | |
| 434 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/shader.h | |
| 435 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/shaderprogram.cpp | |
| 436 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/shaderprogram.h | |
| 437 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/shadermanager.cpp | |
| 438 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/shadermanager.h | |
| 439 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/textrender.cpp | |
| 440 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/textrender.h | |
| 441 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/texture.cpp | |
| 442 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/texture.h | |
| 443 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/texturemanager.cpp | |
| 444 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/texturemanager.h | |
| 445 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/vertexarray.h | |
| 446 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/shaders/newshader.h | |
| 447 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/shaders/shaders.h | |
| 448 | + | ${CMAKE_CURRENT_LIST_DIR}/graphics/shaders/shadersources.h | |
| 449 | + | ||
| 450 | + | # ui | |
| 451 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/declarations.h | |
| 452 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/ui.h | |
| 453 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uianchorlayout.cpp | |
| 454 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uianchorlayout.h | |
| 455 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uiboxlayout.cpp | |
| 456 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uiboxlayout.h | |
| 457 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uigridlayout.cpp | |
| 458 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uigridlayout.h | |
| 459 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uihorizontallayout.cpp | |
| 460 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uihorizontallayout.h | |
| 461 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uilayout.cpp | |
| 462 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uilayout.h | |
| 463 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uimanager.cpp | |
| 464 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uimanager.h | |
| 465 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uitextedit.cpp | |
| 466 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uitextedit.h | |
| 467 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uitranslator.cpp | |
| 468 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uitranslator.h | |
| 469 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uiverticallayout.cpp | |
| 470 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uiverticallayout.h | |
| 471 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uiwidget.cpp | |
| 472 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uiwidget.h | |
| 473 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uiwidgetbasestyle.cpp | |
| 474 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uiwidgetimage.cpp | |
| 475 | + | ${CMAKE_CURRENT_LIST_DIR}/ui/uiwidgettext.cpp | |
| 476 | + | ||
| 477 | + | # platform window | |
| 478 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/platformwindow.cpp | |
| 479 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/platformwindow.h | |
| 480 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/win32window.cpp | |
| 481 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/win32window.h | |
| 482 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/x11window.cpp | |
| 483 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/x11window.h | |
| 484 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/sdlwindow.cpp | |
| 485 | + | ${CMAKE_CURRENT_LIST_DIR}/platform/sdlwindow.h | |
| 486 | + | ||
| 487 | + | # window input | |
| 488 | + | ${CMAKE_CURRENT_LIST_DIR}/input/mouse.cpp | |
| 489 | + | ${CMAKE_CURRENT_LIST_DIR}/input/mouse.h | |
| 490 | + | ||
| 491 | + | ${CMAKE_CURRENT_LIST_DIR}/core/graphicalapplication.cpp | |
| 492 | + | ${CMAKE_CURRENT_LIST_DIR}/core/graphicalapplication.h | |
| 493 | + | ) | |
| 494 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -DFW_GRAPHICS) | |
| 495 | + | else() | |
| 496 | + | set(framework_SOURCES ${framework_SOURCES} | |
| 497 | + | ${CMAKE_CURRENT_LIST_DIR}/core/consoleapplication.cpp | |
| 498 | + | ${CMAKE_CURRENT_LIST_DIR}/core/consoleapplication.h | |
| 499 | + | ) | |
| 500 | + | endif() | |
| 501 | + | ||
| 502 | + | if(FRAMEWORK_SOUND) | |
| 503 | + | find_package(OpenAL REQUIRED) | |
| 504 | + | find_package(VorbisFile REQUIRED) | |
| 505 | + | find_package(Vorbis REQUIRED) | |
| 506 | + | find_package(Ogg REQUIRED) | |
| 507 | + | ||
| 508 | + | set(framework_INCLUDE_DIRS ${framework_INCLUDE_DIRS} ${OPENAL_INCLUDE_DIR} ${VORBISFILE_INCLUDE_DIR}) | |
| 509 | + | set(framework_LIBRARIES ${OPENAL_LIBRARY} ${VORBISFILE_LIBRARY} ${VORBIS_LIBRARY} ${OGG_LIBRARY} ${framework_LIBRARIES}) | |
| 510 | + | ||
| 511 | + | if(WIN32) | |
| 512 | + | set(framework_LIBRARIES ${framework_LIBRARIES} winmm) | |
| 513 | + | elseif(APPLE) | |
| 514 | + | set(framework_LIBRARIES ${framework_LIBRARIES} System) | |
| 515 | + | endif() | |
| 516 | + | ||
| 517 | + | set(framework_SOURCES ${framework_SOURCES} | |
| 518 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/combinedsoundsource.cpp | |
| 519 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/combinedsoundsource.h | |
| 520 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/declarations.h | |
| 521 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/oggsoundfile.cpp | |
| 522 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/oggsoundfile.h | |
| 523 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/soundbuffer.cpp | |
| 524 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/soundbuffer.h | |
| 525 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/soundchannel.cpp | |
| 526 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/soundchannel.h | |
| 527 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/soundfile.cpp | |
| 528 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/soundfile.h | |
| 529 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/soundmanager.cpp | |
| 530 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/soundmanager.h | |
| 531 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/soundsource.cpp | |
| 532 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/soundsource.h | |
| 533 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/streamsoundsource.cpp | |
| 534 | + | ${CMAKE_CURRENT_LIST_DIR}/sound/streamsoundsource.h | |
| 535 | + | ) | |
| 536 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -DFW_SOUND) | |
| 537 | + | endif() | |
| 538 | + | ||
| 539 | + | if(FRAMEWORK_NET) | |
| 540 | + | if(WIN32) | |
| 541 | + | set(NET_LIBRARIES ws2_32 mswsock) | |
| 542 | + | else() | |
| 543 | + | set(NET_LIBRARIES "") | |
| 544 | + | endif() | |
| 545 | + | set(framework_LIBRARIES ${framework_LIBRARIES} ${NET_LIBRARIES}) | |
| 546 | + | ||
| 547 | + | set(framework_SOURCES ${framework_SOURCES} | |
| 548 | + | ${CMAKE_CURRENT_LIST_DIR}/net/connection.cpp | |
| 549 | + | ${CMAKE_CURRENT_LIST_DIR}/net/connection.h | |
| 550 | + | ${CMAKE_CURRENT_LIST_DIR}/net/declarations.h | |
| 551 | + | ${CMAKE_CURRENT_LIST_DIR}/net/inputmessage.cpp | |
| 552 | + | ${CMAKE_CURRENT_LIST_DIR}/net/inputmessage.h | |
| 553 | + | ${CMAKE_CURRENT_LIST_DIR}/net/outputmessage.cpp | |
| 554 | + | ${CMAKE_CURRENT_LIST_DIR}/net/outputmessage.h | |
| 555 | + | ${CMAKE_CURRENT_LIST_DIR}/net/protocol.cpp | |
| 556 | + | ${CMAKE_CURRENT_LIST_DIR}/net/protocol.h | |
| 557 | + | ${CMAKE_CURRENT_LIST_DIR}/net/server.cpp | |
| 558 | + | ${CMAKE_CURRENT_LIST_DIR}/net/server.h | |
| 559 | + | ${CMAKE_CURRENT_LIST_DIR}/net/packet_player.cpp | |
| 560 | + | ${CMAKE_CURRENT_LIST_DIR}/net/packet_player.h | |
| 561 | + | ${CMAKE_CURRENT_LIST_DIR}/net/packet_recorder.cpp | |
| 562 | + | ${CMAKE_CURRENT_LIST_DIR}/net/packet_recorder.h | |
| 563 | + | ${CMAKE_CURRENT_LIST_DIR}/proxy/proxy.cpp | |
| 564 | + | ${CMAKE_CURRENT_LIST_DIR}/proxy/proxy.h | |
| 565 | + | ${CMAKE_CURRENT_LIST_DIR}/proxy/proxy_client.cpp | |
| 566 | + | ${CMAKE_CURRENT_LIST_DIR}/proxy/proxy_client.h | |
| 567 | + | ${CMAKE_CURRENT_LIST_DIR}/http/http.cpp | |
| 568 | + | ${CMAKE_CURRENT_LIST_DIR}/http/http.h | |
| 569 | + | ${CMAKE_CURRENT_LIST_DIR}/http/session.cpp | |
| 570 | + | ${CMAKE_CURRENT_LIST_DIR}/http/session.h | |
| 571 | + | ${CMAKE_CURRENT_LIST_DIR}/http/websocket.cpp | |
| 572 | + | ${CMAKE_CURRENT_LIST_DIR}/http/websocket.h | |
| 573 | + | ${CMAKE_CURRENT_LIST_DIR}/http/result.h | |
| 574 | + | ) | |
| 575 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -DFW_NET) | |
| 576 | + | endif() | |
| 577 | + | ||
| 578 | + | if(FRAMEWORK_XML) | |
| 579 | + | set(framework_SOURCES ${framework_SOURCES} | |
| 580 | + | ${CMAKE_CURRENT_LIST_DIR}/xml/tinyxml.cpp | |
| 581 | + | ${CMAKE_CURRENT_LIST_DIR}/xml/tinyxml.h | |
| 582 | + | ${CMAKE_CURRENT_LIST_DIR}/xml/tinystr.cpp | |
| 583 | + | ${CMAKE_CURRENT_LIST_DIR}/xml/tinystr.h | |
| 584 | + | ${CMAKE_CURRENT_LIST_DIR}/xml/tinyxmlerror.cpp | |
| 585 | + | ${CMAKE_CURRENT_LIST_DIR}/xml/tinyxmlparser.cpp | |
| 586 | + | ) | |
| 587 | + | set(framework_DEFINITIONS ${framework_DEFINITIONS} -DFW_XML) | |
| 588 | + | endif() | |
| 589 | + | ||
| 590 | + | if(EXTRA_LIBS) | |
| 591 | + | set(framework_LIBRARIES ${framework_LIBRARIES} ${EXTRA_LIBS}) | |
| 592 | + | endif() | |
| 593 | + | ||
| 594 | + | include_directories(${framework_INCLUDE_DIRS}) | |
| 595 | + | add_definitions(${framework_DEFINITIONS}) | |
| 596 | + |
| @@ -0,0 +1,299 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef FRAMEWORK_CONST_H | |
| 24 | + | #define FRAMEWORK_CONST_H | |
| 25 | + | ||
| 26 | + | #include "stdext/compiler.h" | |
| 27 | + | ||
| 28 | + | #define DEG_TO_RAD (acos(-1)/180.0) | |
| 29 | + | #define RAD_TO_DEC (180.0/acos(-1)) | |
| 30 | + | ||
| 31 | + | #ifndef BUILD_COMMIT | |
| 32 | + | #define BUILD_COMMIT "dev" | |
| 33 | + | #endif | |
| 34 | + | ||
| 35 | + | #ifndef BUILD_REVISION | |
| 36 | + | #define BUILD_REVISION 0 | |
| 37 | + | #endif | |
| 38 | + | ||
| 39 | + | #ifndef BUILD_TYPE | |
| 40 | + | #define BUILD_TYPE "unknown" | |
| 41 | + | #endif | |
| 42 | + | ||
| 43 | + | #ifndef BUILD_ARCH | |
| 44 | + | #if defined(__amd64) || defined(_M_X64) | |
| 45 | + | #define BUILD_ARCH "x64" | |
| 46 | + | #elif defined(__i386) || defined(_M_IX86) || defined(_X86_) | |
| 47 | + | #define BUILD_ARCH "x86" | |
| 48 | + | #else | |
| 49 | + | #define BUILD_ARCH "unknown" | |
| 50 | + | #endif | |
| 51 | + | #endif | |
| 52 | + | ||
| 53 | + | namespace Fw | |
| 54 | + | { | |
| 55 | + | static const float pi = 3.14159265; | |
| 56 | + | static const float MIN_ALPHA = 0.003f; | |
| 57 | + | enum Key : unsigned char { | |
| 58 | + | KeyUnknown = 0, | |
| 59 | + | KeyEscape = 1, | |
| 60 | + | KeyTab = 2, | |
| 61 | + | KeyBackspace = 3, | |
| 62 | + | //KeyReturn = 4, | |
| 63 | + | KeyEnter = 5, | |
| 64 | + | KeyInsert = 6, | |
| 65 | + | KeyDelete = 7, | |
| 66 | + | KeyPause = 8, | |
| 67 | + | KeyPrintScreen = 9, | |
| 68 | + | KeyHome = 10, | |
| 69 | + | KeyEnd = 11, | |
| 70 | + | KeyPageUp = 12, | |
| 71 | + | KeyPageDown = 13, | |
| 72 | + | KeyUp = 14, | |
| 73 | + | KeyDown = 15, | |
| 74 | + | KeyLeft = 16, | |
| 75 | + | KeyRight = 17, | |
| 76 | + | KeyNumLock = 18, | |
| 77 | + | KeyScrollLock = 19, | |
| 78 | + | KeyCapsLock = 20, | |
| 79 | + | KeyCtrl = 21, | |
| 80 | + | KeyShift = 22, | |
| 81 | + | KeyAlt = 23, | |
| 82 | + | //KeyAltGr = 24, | |
| 83 | + | KeyMeta = 25, | |
| 84 | + | KeyMenu = 26, | |
| 85 | + | KeySpace = 32, // ' ' | |
| 86 | + | KeyExclamation = 33, // ! | |
| 87 | + | KeyQuote = 34, // " | |
| 88 | + | KeyNumberSign = 35, // # | |
| 89 | + | KeyDollar = 36, // $ | |
| 90 | + | KeyPercent = 37, // % | |
| 91 | + | KeyAmpersand = 38, // & | |
| 92 | + | KeyApostrophe = 39, // ' | |
| 93 | + | KeyLeftParen = 40, // ( | |
| 94 | + | KeyRightParen = 41, // ) | |
| 95 | + | KeyAsterisk = 42, // * | |
| 96 | + | KeyPlus = 43, // + | |
| 97 | + | KeyComma = 44, // , | |
| 98 | + | KeyMinus = 45, // - | |
| 99 | + | KeyPeriod = 46, // . | |
| 100 | + | KeySlash = 47, // / | |
| 101 | + | Key0 = 48, // 0 | |
| 102 | + | Key1 = 49, // 1 | |
| 103 | + | Key2 = 50, // 2 | |
| 104 | + | Key3 = 51, // 3 | |
| 105 | + | Key4 = 52, // 4 | |
| 106 | + | Key5 = 53, // 5 | |
| 107 | + | Key6 = 54, // 6 | |
| 108 | + | Key7 = 55, // 7 | |
| 109 | + | Key8 = 56, // 8 | |
| 110 | + | Key9 = 57, // 9 | |
| 111 | + | KeyColon = 58, // : | |
| 112 | + | KeySemicolon = 59, // ; | |
| 113 | + | KeyLess = 60, // < | |
| 114 | + | KeyEqual = 61, // = | |
| 115 | + | KeyGreater = 62, // > | |
| 116 | + | KeyQuestion = 63, // ? | |
| 117 | + | KeyAtSign = 64, // @ | |
| 118 | + | KeyA = 65, // a | |
| 119 | + | KeyB = 66, // b | |
| 120 | + | KeyC = 67, // c | |
| 121 | + | KeyD = 68, // d | |
| 122 | + | KeyE = 69, // e | |
| 123 | + | KeyF = 70, // f | |
| 124 | + | KeyG = 71, // g | |
| 125 | + | KeyH = 72, // h | |
| 126 | + | KeyI = 73, // i | |
| 127 | + | KeyJ = 74, // j | |
| 128 | + | KeyK = 75, // k | |
| 129 | + | KeyL = 76, // l | |
| 130 | + | KeyM = 77, // m | |
| 131 | + | KeyN = 78, // n | |
| 132 | + | KeyO = 79, // o | |
| 133 | + | KeyP = 80, // p | |
| 134 | + | KeyQ = 81, // q | |
| 135 | + | KeyR = 82, // r | |
| 136 | + | KeyS = 83, // s | |
| 137 | + | KeyT = 84, // t | |
| 138 | + | KeyU = 85, // u | |
| 139 | + | KeyV = 86, // v | |
| 140 | + | KeyW = 87, // w | |
| 141 | + | KeyX = 88, // x | |
| 142 | + | KeyY = 89, // y | |
| 143 | + | KeyZ = 90, // z | |
| 144 | + | KeyLeftBracket = 91, // [ | |
| 145 | + | KeyBackslash = 92, // '\' | |
| 146 | + | KeyRightBracket = 93, // ] | |
| 147 | + | KeyCaret = 94, // ^ | |
| 148 | + | KeyUnderscore = 95, // _ | |
| 149 | + | KeyGrave = 96, // ` | |
| 150 | + | KeyLeftCurly = 123, // { | |
| 151 | + | KeyBar = 124, // | | |
| 152 | + | KeyRightCurly = 125, // } | |
| 153 | + | KeyTilde = 126, // ~ | |
| 154 | + | KeyF1 = 128, | |
| 155 | + | KeyF2 = 129, | |
| 156 | + | KeyF3 = 130, | |
| 157 | + | KeyF4 = 131, | |
| 158 | + | KeyF5 = 132, | |
| 159 | + | KeyF6 = 134, | |
| 160 | + | KeyF7 = 135, | |
| 161 | + | KeyF8 = 136, | |
| 162 | + | KeyF9 = 137, | |
| 163 | + | KeyF10 = 138, | |
| 164 | + | KeyF11 = 139, | |
| 165 | + | KeyF12 = 140, | |
| 166 | + | KeyNumpad0 = 141, | |
| 167 | + | KeyNumpad1 = 142, | |
| 168 | + | KeyNumpad2 = 143, | |
| 169 | + | KeyNumpad3 = 144, | |
| 170 | + | KeyNumpad4 = 145, | |
| 171 | + | KeyNumpad5 = 146, | |
| 172 | + | KeyNumpad6 = 147, | |
| 173 | + | KeyNumpad7 = 148, | |
| 174 | + | KeyNumpad8 = 149, | |
| 175 | + | KeyNumpad9 = 150 | |
| 176 | + | }; | |
| 177 | + | ||
| 178 | + | enum LogLevel { | |
| 179 | + | LogDebug = 0, | |
| 180 | + | LogInfo, | |
| 181 | + | LogWarning, | |
| 182 | + | LogError, | |
| 183 | + | LogFatal | |
| 184 | + | }; | |
| 185 | + | ||
| 186 | + | enum AspectRatioMode { | |
| 187 | + | IgnoreAspectRatio, | |
| 188 | + | KeepAspectRatio, | |
| 189 | + | KeepAspectRatioByExpanding | |
| 190 | + | }; | |
| 191 | + | ||
| 192 | + | enum AlignmentFlag { | |
| 193 | + | AlignNone = 0, | |
| 194 | + | AlignLeft = 1, | |
| 195 | + | AlignRight = 2, | |
| 196 | + | AlignTop = 4, | |
| 197 | + | AlignBottom = 8, | |
| 198 | + | AlignHorizontalCenter = 16, | |
| 199 | + | AlignVerticalCenter = 32, | |
| 200 | + | AlignTopLeft = AlignTop | AlignLeft, // 5 | |
| 201 | + | AlignTopRight = AlignTop | AlignRight, // 6 | |
| 202 | + | AlignBottomLeft = AlignBottom | AlignLeft, // 9 | |
| 203 | + | AlignBottomRight = AlignBottom | AlignRight, // 10 | |
| 204 | + | AlignLeftCenter = AlignLeft | AlignVerticalCenter, // 33 | |
| 205 | + | AlignRightCenter = AlignRight | AlignVerticalCenter, // 34 | |
| 206 | + | AlignTopCenter = AlignTop | AlignHorizontalCenter, // 20 | |
| 207 | + | AlignBottomCenter = AlignBottom | AlignHorizontalCenter, // 24 | |
| 208 | + | AlignCenter = AlignVerticalCenter | AlignHorizontalCenter // 48 | |
| 209 | + | }; | |
| 210 | + | ||
| 211 | + | enum AnchorEdge { | |
| 212 | + | AnchorNone = 0, | |
| 213 | + | AnchorTop, | |
| 214 | + | AnchorBottom, | |
| 215 | + | AnchorLeft, | |
| 216 | + | AnchorRight, | |
| 217 | + | AnchorVerticalCenter, | |
| 218 | + | AnchorHorizontalCenter | |
| 219 | + | }; | |
| 220 | + | ||
| 221 | + | enum FocusReason { | |
| 222 | + | MouseFocusReason = 0, | |
| 223 | + | KeyboardFocusReason, | |
| 224 | + | ActiveFocusReason, | |
| 225 | + | OtherFocusReason | |
| 226 | + | }; | |
| 227 | + | ||
| 228 | + | enum AutoFocusPolicy { | |
| 229 | + | AutoFocusNone = 0, | |
| 230 | + | AutoFocusFirst, | |
| 231 | + | AutoFocusLast | |
| 232 | + | }; | |
| 233 | + | ||
| 234 | + | enum InputEventType { | |
| 235 | + | NoInputEvent = 0, | |
| 236 | + | KeyTextInputEvent, | |
| 237 | + | KeyDownInputEvent, | |
| 238 | + | KeyPressInputEvent, | |
| 239 | + | KeyUpInputEvent, | |
| 240 | + | MousePressInputEvent, | |
| 241 | + | MouseReleaseInputEvent, | |
| 242 | + | MouseMoveInputEvent, | |
| 243 | + | MouseWheelInputEvent | |
| 244 | + | }; | |
| 245 | + | ||
| 246 | + | enum MouseButton { | |
| 247 | + | MouseNoButton = 0, | |
| 248 | + | MouseLeftButton, | |
| 249 | + | MouseRightButton, | |
| 250 | + | MouseMidButton, | |
| 251 | + | MouseTouch, | |
| 252 | + | MouseTouch2, | |
| 253 | + | MouseTouch3, | |
| 254 | + | MouseButton4, | |
| 255 | + | MouseButton5, | |
| 256 | + | MouseButtonLast = MouseButton5 | |
| 257 | + | }; | |
| 258 | + | ||
| 259 | + | enum MouseWheelDirection { | |
| 260 | + | MouseNoWheel = 0, | |
| 261 | + | MouseWheelUp, | |
| 262 | + | MouseWheelDown | |
| 263 | + | }; | |
| 264 | + | ||
| 265 | + | enum KeyboardModifier { | |
| 266 | + | KeyboardNoModifier = 0, | |
| 267 | + | KeyboardCtrlModifier = 1, | |
| 268 | + | KeyboardAltModifier = 2, | |
| 269 | + | KeyboardShiftModifier = 4 | |
| 270 | + | }; | |
| 271 | + | ||
| 272 | + | enum WidgetState { | |
| 273 | + | InvalidState = -1, | |
| 274 | + | DefaultState = 0, | |
| 275 | + | ActiveState = 1, | |
| 276 | + | FocusState = 2, | |
| 277 | + | HoverState = 4, | |
| 278 | + | PressedState = 8, | |
| 279 | + | DisabledState = 16, | |
| 280 | + | CheckedState = 32, | |
| 281 | + | OnState = 64, | |
| 282 | + | FirstState = 128, | |
| 283 | + | MiddleState = 256, | |
| 284 | + | LastState = 512, | |
| 285 | + | AlternateState = 1024, | |
| 286 | + | DraggingState = 2048, | |
| 287 | + | HiddenState = 4096, | |
| 288 | + | MobileState = 8192, | |
| 289 | + | LastWidgetState = 16384 | |
| 290 | + | }; | |
| 291 | + | ||
| 292 | + | enum DrawPane { | |
| 293 | + | ForegroundPane = 1, | |
| 294 | + | MapBackgroundPane = 2, | |
| 295 | + | MapForegroundPane = 3, | |
| 296 | + | }; | |
| 297 | + | } | |
| 298 | + | ||
| 299 | + | #endif |
| @@ -0,0 +1,91 @@ | |||
| 1 | + | #include <framework/core/logger.h> | |
| 2 | + | #include <framework/core/graphicalapplication.h> | |
| 3 | + | #include <framework/stdext/format.h> | |
| 4 | + | #include <framework/util/extras.h> | |
| 5 | + | ||
| 6 | + | #include "adaptiverenderer.h" | |
| 7 | + | ||
| 8 | + | AdaptiveRenderer g_adaptiveRenderer; | |
| 9 | + | ||
| 10 | + | void AdaptiveRenderer::newFrame() { | |
| 11 | + | auto now = stdext::millis(); | |
| 12 | + | m_frames.push_back(now); | |
| 13 | + | while (m_frames.front() + 5000 < now) { | |
| 14 | + | m_frames.pop_front(); | |
| 15 | + | } | |
| 16 | + | ||
| 17 | + | if (m_forcedSpeed >= 0 && m_forcedSpeed <= 4) { | |
| 18 | + | m_speed = m_forcedSpeed; | |
| 19 | + | return; | |
| 20 | + | } | |
| 21 | + | ||
| 22 | + | if (m_update + 5000 > now) | |
| 23 | + | return; | |
| 24 | + | ||
| 25 | + | m_update = stdext::millis(); | |
| 26 | + | ||
| 27 | + | if (m_speed < 1) | |
| 28 | + | m_speed = 1; | |
| 29 | + | ||
| 30 | + | int maxFps = std::min<int>(100, std::max<int>(10, g_app.getMaxFps() < 10 ? 100 : g_app.getMaxFps())); | |
| 31 | + | if (m_speed >= 2 && maxFps > 60) { // fix for forced vsync | |
| 32 | + | maxFps = 60; | |
| 33 | + | } | |
| 34 | + | ||
| 35 | + | if (m_frames.size() < maxFps * (4.0f - m_speed * 0.3f) && m_speed != RenderSpeeds - 1) { | |
| 36 | + | m_speed += 1; | |
| 37 | + | } | |
| 38 | + | if (m_frames.size() > maxFps * (4.5f - m_speed * 0.1f) && m_speed > 1) { | |
| 39 | + | m_speed -= 1; | |
| 40 | + | } | |
| 41 | + | } | |
| 42 | + | ||
| 43 | + | void AdaptiveRenderer::refresh() { | |
| 44 | + | m_update = stdext::millis(); | |
| 45 | + | } | |
| 46 | + | ||
| 47 | + | int AdaptiveRenderer::effetsLimit() { | |
| 48 | + | static int limits[RenderSpeeds] = { 20, 10, 7, 4, 2 }; | |
| 49 | + | return limits[m_speed]; | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | int AdaptiveRenderer::creaturesLimit() { | |
| 53 | + | static int limits[RenderSpeeds] = { 20, 10, 7, 5, 3 }; | |
| 54 | + | return limits[m_speed]; | |
| 55 | + | } | |
| 56 | + | ||
| 57 | + | int AdaptiveRenderer::itemsLimit() { | |
| 58 | + | static int limits[RenderSpeeds] = { 20, 10, 7, 5, 3 }; | |
| 59 | + | return limits[m_speed]; | |
| 60 | + | } | |
| 61 | + | ||
| 62 | + | int AdaptiveRenderer::mapRenderInterval() { | |
| 63 | + | static int limits[RenderSpeeds] = { 0, 10, 20, 50, 100 }; | |
| 64 | + | return limits[m_speed]; | |
| 65 | + | } | |
| 66 | + | ||
| 67 | + | int AdaptiveRenderer::textsLimit() { | |
| 68 | + | static int limits[RenderSpeeds] = { 1000, 50, 30, 15, 5 }; | |
| 69 | + | return limits[m_speed]; | |
| 70 | + | } | |
| 71 | + | ||
| 72 | + | int AdaptiveRenderer::creaturesRenderInterval() { | |
| 73 | + | // not working yet | |
| 74 | + | static int limits[RenderSpeeds] = { 0, 0, 10, 15, 20 }; | |
| 75 | + | return limits[m_speed]; | |
| 76 | + | } | |
| 77 | + | ||
| 78 | + | bool AdaptiveRenderer::allowFading() { | |
| 79 | + | return m_speed <= 2; | |
| 80 | + | } | |
| 81 | + | ||
| 82 | + | int AdaptiveRenderer::foregroundUpdateInterval() { | |
| 83 | + | static int limits[RenderSpeeds] = { 0, 20, 40, 50, 60 }; | |
| 84 | + | return limits[m_speed]; | |
| 85 | + | } | |
| 86 | + | ||
| 87 | + | std::string AdaptiveRenderer::getDebugInfo() { | |
| 88 | + | std::stringstream ss; | |
| 89 | + | ss << "Frames: " << m_frames.size() << "|" << m_speed << "|" << m_forcedSpeed; | |
| 90 | + | return ss.str(); | |
| 91 | + | } |
| @@ -0,0 +1,50 @@ | |||
| 1 | + | #ifndef ADAPTIVERENDERER_H | |
| 2 | + | #define ADAPTIVERENDERER_H | |
| 3 | + | ||
| 4 | + | #include <list> | |
| 5 | + | ||
| 6 | + | constexpr int RenderSpeeds = 5; | |
| 7 | + | ||
| 8 | + | class AdaptiveRenderer { | |
| 9 | + | public: | |
| 10 | + | void newFrame(); | |
| 11 | + | ||
| 12 | + | void refresh(); | |
| 13 | + | ||
| 14 | + | int effetsLimit(); | |
| 15 | + | ||
| 16 | + | int creaturesLimit(); | |
| 17 | + | ||
| 18 | + | int itemsLimit(); | |
| 19 | + | ||
| 20 | + | int textsLimit(); | |
| 21 | + | ||
| 22 | + | int mapRenderInterval(); | |
| 23 | + | ||
| 24 | + | int creaturesRenderInterval(); | |
| 25 | + | ||
| 26 | + | ||
| 27 | + | bool allowFading(); | |
| 28 | + | ||
| 29 | + | int getLevel() { | |
| 30 | + | return m_speed; | |
| 31 | + | } | |
| 32 | + | ||
| 33 | + | int foregroundUpdateInterval(); | |
| 34 | + | ||
| 35 | + | std::string getDebugInfo(); | |
| 36 | + | ||
| 37 | + | void setForcedLevel(int value) { | |
| 38 | + | m_forcedSpeed = value; | |
| 39 | + | } | |
| 40 | + | ||
| 41 | + | private: | |
| 42 | + | int m_forcedSpeed = -1; | |
| 43 | + | int m_speed = 0; | |
| 44 | + | time_t m_update = 0; | |
| 45 | + | std::list<time_t> m_frames; | |
| 46 | + | }; | |
| 47 | + | ||
| 48 | + | extern AdaptiveRenderer g_adaptiveRenderer; | |
| 49 | + | ||
| 50 | + | #endif |
| @@ -0,0 +1,227 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "application.h" | |
| 24 | + | #include <csignal> | |
| 25 | + | #include <framework/core/clock.h> | |
| 26 | + | #include <framework/core/resourcemanager.h> | |
| 27 | + | #include <framework/core/modulemanager.h> | |
| 28 | + | #include <framework/core/eventdispatcher.h> | |
| 29 | + | #include <framework/core/configmanager.h> | |
| 30 | + | #include "asyncdispatcher.h" | |
| 31 | + | #include <framework/luaengine/luainterface.h> | |
| 32 | + | #include <framework/platform/crashhandler.h> | |
| 33 | + | #include <framework/platform/platform.h> | |
| 34 | + | #include <framework/http/http.h> | |
| 35 | + | ||
| 36 | + | #if not(defined(ANDROID) || defined(FREE_VERSION)) | |
| 37 | + | #include <boost/process.hpp> | |
| 38 | + | #endif | |
| 39 | + | ||
| 40 | + | #include <locale> | |
| 41 | + | ||
| 42 | + | #include <framework/net/connection.h> | |
| 43 | + | #include <framework/proxy/proxy.h> | |
| 44 | + | ||
| 45 | + | void exitSignalHandler(int sig) | |
| 46 | + | { | |
| 47 | + | static bool signaled = false; | |
| 48 | + | switch(sig) { | |
| 49 | + | case SIGTERM: | |
| 50 | + | case SIGINT: | |
| 51 | + | if(!signaled && !g_app.isStopping() && !g_app.isTerminated()) { | |
| 52 | + | signaled = true; | |
| 53 | + | g_dispatcher.addEvent(std::bind(&Application::close, &g_app)); | |
| 54 | + | } | |
| 55 | + | break; | |
| 56 | + | } | |
| 57 | + | } | |
| 58 | + | ||
| 59 | + | Application::Application() | |
| 60 | + | { | |
| 61 | + | m_appName = "application"; | |
| 62 | + | m_appCompactName = "app"; | |
| 63 | + | m_appVersion = "none"; | |
| 64 | + | m_charset = "cp1252"; | |
| 65 | + | m_stopping = false; | |
| 66 | + | #ifdef ANDROID | |
| 67 | + | m_mobile = true; | |
| 68 | + | #endif | |
| 69 | + | } | |
| 70 | + | ||
| 71 | + | void Application::init(std::vector<std::string>& args) | |
| 72 | + | { | |
| 73 | + | // capture exit signals | |
| 74 | + | signal(SIGTERM, exitSignalHandler); | |
| 75 | + | signal(SIGINT, exitSignalHandler); | |
| 76 | + | ||
| 77 | + | // setup locale | |
| 78 | + | std::locale::global(std::locale()); | |
| 79 | + | ||
| 80 | + | // process args encoding | |
| 81 | + | g_platform.processArgs(args); | |
| 82 | + | ||
| 83 | + | g_asyncDispatcher.init(); | |
| 84 | + | ||
| 85 | + | std::string startupOptions; | |
| 86 | + | for(uint i=1;i<args.size();++i) { | |
| 87 | + | const std::string& arg = args[i]; | |
| 88 | + | startupOptions += " "; | |
| 89 | + | startupOptions += arg; | |
| 90 | + | } | |
| 91 | + | if(startupOptions.length() > 0) | |
| 92 | + | g_logger.info(stdext::format("Startup options: %s", startupOptions)); | |
| 93 | + | ||
| 94 | + | m_startupOptions = startupOptions; | |
| 95 | + | ||
| 96 | + | // mobile testing | |
| 97 | + | if (startupOptions.find("-mobile") != std::string::npos) | |
| 98 | + | m_mobile = true; | |
| 99 | + | ||
| 100 | + | // initialize configs | |
| 101 | + | g_configs.init(); | |
| 102 | + | ||
| 103 | + | // initialize lua | |
| 104 | + | g_lua.init(); | |
| 105 | + | registerLuaFunctions(); | |
| 106 | + | ||
| 107 | + | // initalize proxy | |
| 108 | + | g_proxy.init(); | |
| 109 | + | } | |
| 110 | + | ||
| 111 | + | void Application::deinit() | |
| 112 | + | { | |
| 113 | + | g_lua.callGlobalField("g_app", "onTerminate"); | |
| 114 | + | ||
| 115 | + | // run modules unload events | |
| 116 | + | g_modules.unloadModules(); | |
| 117 | + | g_modules.clear(); | |
| 118 | + | ||
| 119 | + | // release remaining lua object references | |
| 120 | + | g_lua.collectGarbage(); | |
| 121 | + | ||
| 122 | + | // poll remaining events | |
| 123 | + | poll(); | |
| 124 | + | ||
| 125 | + | // disable dispatcher events | |
| 126 | + | g_dispatcher.shutdown(); | |
| 127 | + | } | |
| 128 | + | ||
| 129 | + | void Application::terminate() | |
| 130 | + | { | |
| 131 | + | // terminate network | |
| 132 | + | Connection::terminate(); | |
| 133 | + | ||
| 134 | + | // release configs | |
| 135 | + | g_configs.terminate(); | |
| 136 | + | ||
| 137 | + | // release resources | |
| 138 | + | g_resources.terminate(); | |
| 139 | + | ||
| 140 | + | // terminate script environment | |
| 141 | + | g_lua.terminate(); | |
| 142 | + | ||
| 143 | + | // terminate proxy | |
| 144 | + | g_proxy.terminate(); | |
| 145 | + | ||
| 146 | + | m_terminated = true; | |
| 147 | + | ||
| 148 | + | signal(SIGTERM, SIG_DFL); | |
| 149 | + | signal(SIGINT, SIG_DFL); | |
| 150 | + | } | |
| 151 | + | ||
| 152 | + | void Application::poll() | |
| 153 | + | { | |
| 154 | + | Connection::poll(); | |
| 155 | + | ||
| 156 | + | g_dispatcher.poll(); | |
| 157 | + | ||
| 158 | + | // poll connection again to flush pending write | |
| 159 | + | Connection::poll(); | |
| 160 | + | } | |
| 161 | + | ||
| 162 | + | void Application::exit() | |
| 163 | + | { | |
| 164 | + | g_lua.callGlobalField<bool>("g_app", "onExit"); | |
| 165 | + | m_stopping = true; | |
| 166 | + | } | |
| 167 | + | ||
| 168 | + | void Application::quick_exit() | |
| 169 | + | { | |
| 170 | + | #ifdef _MSC_VER | |
| 171 | + | ::quick_exit(0); | |
| 172 | + | #else | |
| 173 | + | ::exit(0); | |
| 174 | + | #endif | |
| 175 | + | } | |
| 176 | + | ||
| 177 | + | void Application::close() | |
| 178 | + | { | |
| 179 | + | if(!g_lua.callGlobalField<bool>("g_app", "onClose")) | |
| 180 | + | exit(); | |
| 181 | + | } | |
| 182 | + | ||
| 183 | + | void Application::restart() | |
| 184 | + | { | |
| 185 | + | #if not(defined(ANDROID) || defined(FREE_VERSION)) | |
| 186 | + | boost::process::child c(g_resources.getBinaryName()); | |
| 187 | + | std::error_code ec2; | |
| 188 | + | if (c.wait_for(std::chrono::seconds(1), ec2)) { | |
| 189 | + | g_logger.fatal("Updater restart error. Please restart application"); | |
| 190 | + | } | |
| 191 | + | c.detach(); | |
| 192 | + | quick_exit(); | |
| 193 | + | #else | |
| 194 | + | exit(); | |
| 195 | + | #endif | |
| 196 | + | } | |
| 197 | + | ||
| 198 | + | void Application::restartArgs(const std::vector<std::string>& args) | |
| 199 | + | { | |
| 200 | + | #if not(defined(ANDROID) || defined(FREE_VERSION)) | |
| 201 | + | boost::process::child c(g_resources.getBinaryName(), boost::process::args(args)); | |
| 202 | + | std::error_code ec2; | |
| 203 | + | if (c.wait_for(std::chrono::seconds(1), ec2)) { | |
| 204 | + | g_logger.fatal("Updater restart error. Please restart application"); | |
| 205 | + | } | |
| 206 | + | c.detach(); | |
| 207 | + | quick_exit(); | |
| 208 | + | #else | |
| 209 | + | exit(); | |
| 210 | + | #endif | |
| 211 | + | } | |
| 212 | + | ||
| 213 | + | std::string Application::getOs() | |
| 214 | + | { | |
| 215 | + | #if defined(ANDROID) | |
| 216 | + | return "android"; | |
| 217 | + | #elif defined(WIN32) | |
| 218 | + | return "windows"; | |
| 219 | + | #elif defined(__APPLE__) | |
| 220 | + | return "mac"; | |
| 221 | + | #elif __linux | |
| 222 | + | return "linux"; | |
| 223 | + | #else | |
| 224 | + | return "unknown"; | |
| 225 | + | #endif | |
| 226 | + | } | |
| 227 | + |
| @@ -0,0 +1,97 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef APPLICATION_H | |
| 24 | + | #define APPLICATION_H | |
| 25 | + | ||
| 26 | + | #include <framework/global.h> | |
| 27 | + | ||
| 28 | + | //@bindsingleton g_app | |
| 29 | + | class Application | |
| 30 | + | { | |
| 31 | + | public: | |
| 32 | + | Application(); | |
| 33 | + | virtual ~Application() {} | |
| 34 | + | ||
| 35 | + | virtual void init(std::vector<std::string>& args); | |
| 36 | + | virtual void deinit(); | |
| 37 | + | virtual void terminate(); | |
| 38 | + | virtual void run() = 0; | |
| 39 | + | virtual void poll(); | |
| 40 | + | virtual void exit(); | |
| 41 | + | virtual void quick_exit(); | |
| 42 | + | virtual void close(); | |
| 43 | + | void restart(); | |
| 44 | + | void restartArgs(const std::vector<std::string>& args); | |
| 45 | + | ||
| 46 | + | void setName(const std::string& name) { m_appName = name; } | |
| 47 | + | void setCompactName(const std::string& compactName) { m_appCompactName = compactName; } | |
| 48 | + | void setVersion(const std::string& version) { m_appVersion = version; } | |
| 49 | + | ||
| 50 | + | bool isRunning() { return m_running; } | |
| 51 | + | bool isStopping() { return m_stopping; } | |
| 52 | + | bool isTerminated() { return m_terminated; } | |
| 53 | + | const std::string& getName() { return m_appName; } | |
| 54 | + | const std::string& getCompactName() { return m_appCompactName; } | |
| 55 | + | const std::string& getVersion() { return m_appVersion; } | |
| 56 | + | ||
| 57 | + | std::string getCharset() { return m_charset; } | |
| 58 | + | std::string getBuildCompiler() { return BUILD_COMPILER; } | |
| 59 | + | std::string getBuildDate() { return std::string(__DATE__); } | |
| 60 | + | std::string getBuildRevision() { return std::to_string(BUILD_REVISION); } | |
| 61 | + | std::string getBuildCommit() { return BUILD_COMMIT; } | |
| 62 | + | #ifdef FREE_VERSION | |
| 63 | + | std::string getBuildType() { return "FREE"; } | |
| 64 | + | #else | |
| 65 | + | std::string getBuildType() { return "FULL"; } | |
| 66 | + | #endif | |
| 67 | + | std::string getBuildArch() { return BUILD_ARCH; } | |
| 68 | + | std::string getAuthor() { return "otclient.net"; } | |
| 69 | + | std::string getOs(); | |
| 70 | + | std::string getStartupOptions() { return m_startupOptions; } | |
| 71 | + | ||
| 72 | + | bool isMobile() | |
| 73 | + | { | |
| 74 | + | return m_mobile; | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | protected: | |
| 78 | + | void registerLuaFunctions(); | |
| 79 | + | ||
| 80 | + | std::string m_charset; | |
| 81 | + | std::string m_appName; | |
| 82 | + | std::string m_appCompactName; | |
| 83 | + | std::string m_appVersion; | |
| 84 | + | std::string m_startupOptions; | |
| 85 | + | stdext::boolean<false> m_running; | |
| 86 | + | stdext::boolean<false> m_stopping; | |
| 87 | + | stdext::boolean<false> m_terminated; | |
| 88 | + | stdext::boolean<false> m_mobile; | |
| 89 | + | }; | |
| 90 | + | ||
| 91 | + | #ifdef FW_GRAPHICS | |
| 92 | + | #include "graphicalapplication.h" | |
| 93 | + | #else | |
| 94 | + | #include "consoleapplication.h" | |
| 95 | + | #endif | |
| 96 | + | ||
| 97 | + | #endif |
| @@ -0,0 +1,71 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "asyncdispatcher.h" | |
| 24 | + | ||
| 25 | + | AsyncDispatcher g_asyncDispatcher; | |
| 26 | + | ||
| 27 | + | void AsyncDispatcher::init() | |
| 28 | + | { | |
| 29 | + | spawn_thread(); | |
| 30 | + | } | |
| 31 | + | ||
| 32 | + | void AsyncDispatcher::terminate() | |
| 33 | + | { | |
| 34 | + | stop(); | |
| 35 | + | m_tasks.clear(); | |
| 36 | + | } | |
| 37 | + | ||
| 38 | + | void AsyncDispatcher::spawn_thread() | |
| 39 | + | { | |
| 40 | + | m_running = true; | |
| 41 | + | m_threads.push_back(std::thread(std::bind(&AsyncDispatcher::exec_loop, this))); | |
| 42 | + | } | |
| 43 | + | ||
| 44 | + | void AsyncDispatcher::stop() | |
| 45 | + | { | |
| 46 | + | m_mutex.lock(); | |
| 47 | + | m_running = false; | |
| 48 | + | m_condition.notify_all(); | |
| 49 | + | m_mutex.unlock(); | |
| 50 | + | for(std::thread& thread : m_threads) | |
| 51 | + | thread.join(); | |
| 52 | + | m_threads.clear(); | |
| 53 | + | }; | |
| 54 | + | ||
| 55 | + | void AsyncDispatcher::exec_loop() { | |
| 56 | + | std::unique_lock<std::mutex> lock(m_mutex); | |
| 57 | + | while(true) { | |
| 58 | + | while(m_tasks.size() == 0 && m_running) | |
| 59 | + | m_condition.wait(lock); | |
| 60 | + | ||
| 61 | + | if(!m_running) | |
| 62 | + | return; | |
| 63 | + | ||
| 64 | + | std::function<void()> task = m_tasks.front(); | |
| 65 | + | m_tasks.pop_front(); | |
| 66 | + | ||
| 67 | + | lock.unlock(); | |
| 68 | + | task(); | |
| 69 | + | lock.lock(); | |
| 70 | + | } | |
| 71 | + | } |
| @@ -0,0 +1,65 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef ASYNCDISPATCHER_H | |
| 24 | + | #define ASYNCDISPATCHER_H | |
| 25 | + | ||
| 26 | + | #include "declarations.h" | |
| 27 | + | #include <framework/stdext/thread.h> | |
| 28 | + | ||
| 29 | + | class AsyncDispatcher { | |
| 30 | + | public: | |
| 31 | + | void init(); | |
| 32 | + | void terminate(); | |
| 33 | + | ||
| 34 | + | void spawn_thread(); | |
| 35 | + | void stop(); | |
| 36 | + | ||
| 37 | + | template<class F> | |
| 38 | + | std::shared_future<typename std::invoke_result<F>::type> schedule(const F& task) { | |
| 39 | + | std::lock_guard<std::mutex> lock(m_mutex); | |
| 40 | + | auto prom = std::make_shared<std::promise<typename std::invoke_result<F>::type>>(); | |
| 41 | + | m_tasks.push_back([=]() { prom->set_value(task()); }); | |
| 42 | + | m_condition.notify_all(); | |
| 43 | + | return std::shared_future<typename std::invoke_result<F>::type>(prom->get_future()); | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | void dispatch(std::function<void()> f) { | |
| 47 | + | std::lock_guard<std::mutex> lock(m_mutex); | |
| 48 | + | m_tasks.push_back(f); | |
| 49 | + | m_condition.notify_all(); | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | protected: | |
| 53 | + | void exec_loop(); | |
| 54 | + | ||
| 55 | + | private: | |
| 56 | + | std::list<std::function<void()>> m_tasks; | |
| 57 | + | std::list<std::thread> m_threads; | |
| 58 | + | std::mutex m_mutex; | |
| 59 | + | std::condition_variable m_condition; | |
| 60 | + | stdext::boolean<false> m_running; | |
| 61 | + | }; | |
| 62 | + | ||
| 63 | + | extern AsyncDispatcher g_asyncDispatcher; | |
| 64 | + | ||
| 65 | + | #endif |
| @@ -0,0 +1,216 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "binarytree.h" | |
| 24 | + | #include "filestream.h" | |
| 25 | + | ||
| 26 | + | BinaryTree::BinaryTree(const FileStreamPtr& fin) | |
| 27 | + | : m_fin(fin), m_pos(0xFFFFFFFF) { | |
| 28 | + | m_startPos = fin->tell(); | |
| 29 | + | } | |
| 30 | + | ||
| 31 | + | BinaryTree::~BinaryTree() {} | |
| 32 | + | ||
| 33 | + | void BinaryTree::skipNodes() { | |
| 34 | + | while (true) { | |
| 35 | + | uint8 byte = m_fin->getU8(); | |
| 36 | + | switch (byte) { | |
| 37 | + | case BINARYTREE_NODE_START: { | |
| 38 | + | skipNodes(); | |
| 39 | + | break; | |
| 40 | + | } | |
| 41 | + | case BINARYTREE_NODE_END: | |
| 42 | + | return; | |
| 43 | + | case BINARYTREE_ESCAPE_CHAR: | |
| 44 | + | m_fin->getU8(); | |
| 45 | + | break; | |
| 46 | + | default: | |
| 47 | + | break; | |
| 48 | + | } | |
| 49 | + | } | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | void BinaryTree::unserialize() { | |
| 53 | + | if (m_pos != 0xFFFFFFFF) return; | |
| 54 | + | m_pos = 0; | |
| 55 | + | ||
| 56 | + | m_fin->seek(m_startPos); | |
| 57 | + | while (true) { | |
| 58 | + | uint8 byte = m_fin->getU8(); | |
| 59 | + | switch (byte) { | |
| 60 | + | case BINARYTREE_NODE_START: { | |
| 61 | + | skipNodes(); | |
| 62 | + | break; | |
| 63 | + | } | |
| 64 | + | case BINARYTREE_NODE_END: | |
| 65 | + | return; | |
| 66 | + | case BINARYTREE_ESCAPE_CHAR: | |
| 67 | + | m_buffer.add(m_fin->getU8()); | |
| 68 | + | break; | |
| 69 | + | default: | |
| 70 | + | m_buffer.add(byte); | |
| 71 | + | break; | |
| 72 | + | } | |
| 73 | + | } | |
| 74 | + | } | |
| 75 | + | ||
| 76 | + | BinaryTreeVec BinaryTree::getChildren() { | |
| 77 | + | BinaryTreeVec children; | |
| 78 | + | m_fin->seek(m_startPos); | |
| 79 | + | while (true) { | |
| 80 | + | uint8 byte = m_fin->getU8(); | |
| 81 | + | switch (byte) { | |
| 82 | + | case BINARYTREE_NODE_START: { | |
| 83 | + | BinaryTreePtr node(new BinaryTree(m_fin)); | |
| 84 | + | children.push_back(node); | |
| 85 | + | node->skipNodes(); | |
| 86 | + | break; | |
| 87 | + | } | |
| 88 | + | case BINARYTREE_NODE_END: | |
| 89 | + | return children; | |
| 90 | + | case BINARYTREE_ESCAPE_CHAR: | |
| 91 | + | m_fin->getU8(); | |
| 92 | + | break; | |
| 93 | + | default: | |
| 94 | + | break; | |
| 95 | + | } | |
| 96 | + | } | |
| 97 | + | } | |
| 98 | + | ||
| 99 | + | void BinaryTree::seek(uint pos) { | |
| 100 | + | unserialize(); | |
| 101 | + | if (pos > m_buffer.size()) stdext::throw_exception("BinaryTree: seek failed"); | |
| 102 | + | m_pos = pos; | |
| 103 | + | } | |
| 104 | + | ||
| 105 | + | void BinaryTree::skip(uint len) { | |
| 106 | + | unserialize(); | |
| 107 | + | seek(tell() + len); | |
| 108 | + | } | |
| 109 | + | ||
| 110 | + | uint8 BinaryTree::getU8() { | |
| 111 | + | unserialize(); | |
| 112 | + | if (m_pos + 1 > m_buffer.size()) | |
| 113 | + | stdext::throw_exception("BinaryTree: getU8 failed"); | |
| 114 | + | uint8 v = m_buffer[m_pos]; | |
| 115 | + | m_pos += 1; | |
| 116 | + | return v; | |
| 117 | + | } | |
| 118 | + | ||
| 119 | + | uint16 BinaryTree::getU16() { | |
| 120 | + | unserialize(); | |
| 121 | + | if (m_pos + 2 > m_buffer.size()) | |
| 122 | + | stdext::throw_exception("BinaryTree: getU16 failed"); | |
| 123 | + | uint16 v = stdext::readULE16(&m_buffer[m_pos]); | |
| 124 | + | m_pos += 2; | |
| 125 | + | return v; | |
| 126 | + | } | |
| 127 | + | ||
| 128 | + | uint32 BinaryTree::getU32() { | |
| 129 | + | unserialize(); | |
| 130 | + | if (m_pos + 4 > m_buffer.size()) | |
| 131 | + | stdext::throw_exception("BinaryTree: getU32 failed"); | |
| 132 | + | uint32 v = stdext::readULE32(&m_buffer[m_pos]); | |
| 133 | + | m_pos += 4; | |
| 134 | + | return v; | |
| 135 | + | } | |
| 136 | + | ||
| 137 | + | uint64 BinaryTree::getU64() { | |
| 138 | + | unserialize(); | |
| 139 | + | if (m_pos + 8 > m_buffer.size()) | |
| 140 | + | stdext::throw_exception("BinaryTree: getU64 failed"); | |
| 141 | + | uint64 v = stdext::readULE64(&m_buffer[m_pos]); | |
| 142 | + | m_pos += 8; | |
| 143 | + | return v; | |
| 144 | + | } | |
| 145 | + | ||
| 146 | + | std::string BinaryTree::getString(uint16 len) { | |
| 147 | + | unserialize(); | |
| 148 | + | if (len == 0) len = getU16(); | |
| 149 | + | ||
| 150 | + | if (m_pos + len > m_buffer.size()) | |
| 151 | + | stdext::throw_exception( | |
| 152 | + | "BinaryTree: getString failed: string length exceeded buffer size."); | |
| 153 | + | ||
| 154 | + | std::string ret((char*)&m_buffer[m_pos], len); | |
| 155 | + | m_pos += len; | |
| 156 | + | return ret; | |
| 157 | + | } | |
| 158 | + | ||
| 159 | + | Point BinaryTree::getPoint() { | |
| 160 | + | Point ret; | |
| 161 | + | ret.x = getU8(); | |
| 162 | + | ret.y = getU8(); | |
| 163 | + | return ret; | |
| 164 | + | } | |
| 165 | + | ||
| 166 | + | OutputBinaryTree::OutputBinaryTree(const FileStreamPtr& fin) : m_fin(fin) { | |
| 167 | + | startNode(0); | |
| 168 | + | } | |
| 169 | + | ||
| 170 | + | void OutputBinaryTree::addU8(uint8 v) { write(&v, 1); } | |
| 171 | + | ||
| 172 | + | void OutputBinaryTree::addU16(uint16 v) { | |
| 173 | + | uint8 data[2]; | |
| 174 | + | stdext::writeULE16(data, v); | |
| 175 | + | write(data, 2); | |
| 176 | + | } | |
| 177 | + | ||
| 178 | + | void OutputBinaryTree::addU32(uint32 v) { | |
| 179 | + | uint8 data[4]; | |
| 180 | + | stdext::writeULE32(data, v); | |
| 181 | + | write(data, 4); | |
| 182 | + | } | |
| 183 | + | ||
| 184 | + | void OutputBinaryTree::addString(const std::string& v) { | |
| 185 | + | if (v.size() > 0xFFFF) stdext::throw_exception("too long string"); | |
| 186 | + | ||
| 187 | + | addU16(v.length()); | |
| 188 | + | write((const uint8*)v.c_str(), v.length()); | |
| 189 | + | } | |
| 190 | + | ||
| 191 | + | void OutputBinaryTree::addPos(uint16 x, uint16 y, uint8 z) { | |
| 192 | + | addU16(x); | |
| 193 | + | addU16(y); | |
| 194 | + | addU8(z); | |
| 195 | + | } | |
| 196 | + | ||
| 197 | + | void OutputBinaryTree::addPoint(const Point& point) { | |
| 198 | + | addU8(point.x); | |
| 199 | + | addU8(point.y); | |
| 200 | + | } | |
| 201 | + | ||
| 202 | + | void OutputBinaryTree::startNode(uint8 node) { | |
| 203 | + | m_fin->addU8(BINARYTREE_NODE_START); | |
| 204 | + | write(&node, 1); | |
| 205 | + | } | |
| 206 | + | ||
| 207 | + | void OutputBinaryTree::endNode() { m_fin->addU8(BINARYTREE_NODE_END); } | |
| 208 | + | ||
| 209 | + | void OutputBinaryTree::write(const uint8* data, size_t size) { | |
| 210 | + | for (size_t i = 0; i < size; ++i) { | |
| 211 | + | if (data[i] == BINARYTREE_NODE_START || data[i] == BINARYTREE_NODE_END || | |
| 212 | + | data[i] == BINARYTREE_ESCAPE_CHAR) | |
| 213 | + | m_fin->addU8(BINARYTREE_ESCAPE_CHAR); | |
| 214 | + | m_fin->addU8(data[i]); | |
| 215 | + | } | |
| 216 | + | } |
| @@ -0,0 +1,89 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef BINARYTREE_H | |
| 24 | + | #define BINARYTREE_H | |
| 25 | + | ||
| 26 | + | #include "declarations.h" | |
| 27 | + | #include <framework/util/databuffer.h> | |
| 28 | + | ||
| 29 | + | enum { | |
| 30 | + | BINARYTREE_ESCAPE_CHAR = 0xFD, | |
| 31 | + | BINARYTREE_NODE_START = 0xFE, | |
| 32 | + | BINARYTREE_NODE_END = 0xFF | |
| 33 | + | }; | |
| 34 | + | ||
| 35 | + | class BinaryTree : public stdext::shared_object | |
| 36 | + | { | |
| 37 | + | public: | |
| 38 | + | BinaryTree(const FileStreamPtr& fin); | |
| 39 | + | ~BinaryTree(); | |
| 40 | + | ||
| 41 | + | void seek(uint pos); | |
| 42 | + | void skip(uint len); | |
| 43 | + | uint tell() { return m_pos; } | |
| 44 | + | uint size() { unserialize(); return m_buffer.size(); } | |
| 45 | + | ||
| 46 | + | uint8 getU8(); | |
| 47 | + | uint16 getU16(); | |
| 48 | + | uint32 getU32(); | |
| 49 | + | uint64 getU64(); | |
| 50 | + | std::string getString(uint16 len = 0); | |
| 51 | + | Point getPoint(); | |
| 52 | + | ||
| 53 | + | BinaryTreeVec getChildren(); | |
| 54 | + | bool canRead() { unserialize(); return m_pos < m_buffer.size(); } | |
| 55 | + | ||
| 56 | + | private: | |
| 57 | + | void unserialize(); | |
| 58 | + | void skipNodes(); | |
| 59 | + | ||
| 60 | + | FileStreamPtr m_fin; | |
| 61 | + | DataBuffer<uint8> m_buffer; | |
| 62 | + | uint m_pos; | |
| 63 | + | uint m_startPos; | |
| 64 | + | }; | |
| 65 | + | ||
| 66 | + | class OutputBinaryTree : public stdext::shared_object | |
| 67 | + | { | |
| 68 | + | public: | |
| 69 | + | OutputBinaryTree(const FileStreamPtr& finish); | |
| 70 | + | ||
| 71 | + | void addU8(uint8 v); | |
| 72 | + | void addU16(uint16 v); | |
| 73 | + | void addU32(uint32 v); | |
| 74 | + | void addString(const std::string& v); | |
| 75 | + | void addPos(uint16 x, uint16 y, uint8 z); | |
| 76 | + | void addPoint(const Point& point); | |
| 77 | + | ||
| 78 | + | void startNode(uint8 node); | |
| 79 | + | void endNode(); | |
| 80 | + | ||
| 81 | + | private: | |
| 82 | + | FileStreamPtr m_fin; | |
| 83 | + | ||
| 84 | + | protected: | |
| 85 | + | void write(const uint8* data, size_t size); | |
| 86 | + | }; | |
| 87 | + | ||
| 88 | + | #endif | |
| 89 | + |
| @@ -0,0 +1,49 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "clock.h" | |
| 24 | + | ||
| 25 | + | Clock g_clock; | |
| 26 | + | ||
| 27 | + | Clock::Clock() | |
| 28 | + | { | |
| 29 | + | m_currentMicros = 0; | |
| 30 | + | m_currentMillis = 0; | |
| 31 | + | m_currentSeconds = 0; | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | void Clock::update() | |
| 35 | + | { | |
| 36 | + | m_currentMicros = stdext::micros(); | |
| 37 | + | m_currentMillis = m_currentMicros / 1000; | |
| 38 | + | m_currentSeconds = m_currentMicros / 1000000.0f; | |
| 39 | + | } | |
| 40 | + | ||
| 41 | + | ticks_t Clock::realMicros() | |
| 42 | + | { | |
| 43 | + | return stdext::micros(); | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | ticks_t Clock::realMillis() | |
| 47 | + | { | |
| 48 | + | return stdext::millis(); | |
| 49 | + | } |
| @@ -0,0 +1,51 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef CLOCK_H | |
| 24 | + | #define CLOCK_H | |
| 25 | + | ||
| 26 | + | #include "declarations.h" | |
| 27 | + | ||
| 28 | + | // @bindsingleton g_clock | |
| 29 | + | class Clock | |
| 30 | + | { | |
| 31 | + | public: | |
| 32 | + | Clock(); | |
| 33 | + | ||
| 34 | + | void update(); | |
| 35 | + | ||
| 36 | + | ticks_t micros() { return m_currentMicros; } | |
| 37 | + | ticks_t millis() { return m_currentMillis; } | |
| 38 | + | float seconds() { return m_currentSeconds; } | |
| 39 | + | ticks_t realMicros(); | |
| 40 | + | ticks_t realMillis(); | |
| 41 | + | ||
| 42 | + | private: | |
| 43 | + | std::atomic<ticks_t> m_currentMicros; | |
| 44 | + | std::atomic<ticks_t> m_currentMillis; | |
| 45 | + | std::atomic<float> m_currentSeconds; | |
| 46 | + | }; | |
| 47 | + | ||
| 48 | + | extern Clock g_clock; | |
| 49 | + | ||
| 50 | + | #endif | |
| 51 | + |
| @@ -0,0 +1,170 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "config.h" | |
| 24 | + | #include "resourcemanager.h" | |
| 25 | + | #include "configmanager.h" | |
| 26 | + | ||
| 27 | + | #include <framework/otml/otml.h> | |
| 28 | + | ||
| 29 | + | Config::Config() | |
| 30 | + | { | |
| 31 | + | m_confsDoc = OTMLDocument::create(); | |
| 32 | + | m_fileName = ""; | |
| 33 | + | } | |
| 34 | + | ||
| 35 | + | bool Config::load(const std::string& file) | |
| 36 | + | { | |
| 37 | + | m_fileName = file; | |
| 38 | + | ||
| 39 | + | if(!g_resources.fileExists(file)) | |
| 40 | + | return false; | |
| 41 | + | ||
| 42 | + | try { | |
| 43 | + | OTMLDocumentPtr confsDoc = OTMLDocument::parse(file); | |
| 44 | + | if(confsDoc) | |
| 45 | + | m_confsDoc = confsDoc; | |
| 46 | + | return true; | |
| 47 | + | } catch(stdext::exception& e) { | |
| 48 | + | g_logger.error(stdext::format("Unable to parse configuration file '%s': ", e.what())); | |
| 49 | + | return false; | |
| 50 | + | } | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | bool Config::unload() | |
| 54 | + | { | |
| 55 | + | if(isLoaded()) { | |
| 56 | + | m_confsDoc = nullptr; | |
| 57 | + | m_fileName = ""; | |
| 58 | + | return true; | |
| 59 | + | } | |
| 60 | + | return false; | |
| 61 | + | } | |
| 62 | + | ||
| 63 | + | bool Config::save() | |
| 64 | + | { | |
| 65 | + | if(m_fileName.length() == 0) | |
| 66 | + | return false; | |
| 67 | + | return m_confsDoc->save(m_fileName); | |
| 68 | + | } | |
| 69 | + | ||
| 70 | + | void Config::clear() | |
| 71 | + | { | |
| 72 | + | m_confsDoc->clear(); | |
| 73 | + | } | |
| 74 | + | ||
| 75 | + | void Config::setValue(const std::string& key, const std::string& value) | |
| 76 | + | { | |
| 77 | + | if (key.empty()) { | |
| 78 | + | return; | |
| 79 | + | } | |
| 80 | + | if(value.empty()) { | |
| 81 | + | remove(key); | |
| 82 | + | return; | |
| 83 | + | } | |
| 84 | + | ||
| 85 | + | OTMLNodePtr child = OTMLNode::create(key, value); | |
| 86 | + | m_confsDoc->addChild(child); | |
| 87 | + | } | |
| 88 | + | ||
| 89 | + | void Config::setList(const std::string& key, const std::vector<std::string>& list) | |
| 90 | + | { | |
| 91 | + | remove(key); | |
| 92 | + | ||
| 93 | + | if(list.size() == 0) | |
| 94 | + | return; | |
| 95 | + | ||
| 96 | + | OTMLNodePtr child = OTMLNode::create(key, true); | |
| 97 | + | for(const std::string& value : list) | |
| 98 | + | child->writeIn(value); | |
| 99 | + | m_confsDoc->addChild(child); | |
| 100 | + | } | |
| 101 | + | ||
| 102 | + | bool Config::exists(const std::string& key) | |
| 103 | + | { | |
| 104 | + | return m_confsDoc->hasChildAt(key); | |
| 105 | + | } | |
| 106 | + | ||
| 107 | + | std::string Config::getValue(const std::string& key) | |
| 108 | + | { | |
| 109 | + | OTMLNodePtr child = m_confsDoc->get(key); | |
| 110 | + | if(child) | |
| 111 | + | return child->value(); | |
| 112 | + | else | |
| 113 | + | return ""; | |
| 114 | + | } | |
| 115 | + | ||
| 116 | + | std::vector<std::string> Config::getList(const std::string& key) | |
| 117 | + | { | |
| 118 | + | std::vector<std::string> list; | |
| 119 | + | OTMLNodePtr child = m_confsDoc->get(key); | |
| 120 | + | if(child) { | |
| 121 | + | for(const OTMLNodePtr& subchild : child->children()) | |
| 122 | + | list.push_back(subchild->value()); | |
| 123 | + | } | |
| 124 | + | return list; | |
| 125 | + | } | |
| 126 | + | ||
| 127 | + | void Config::remove(const std::string& key) | |
| 128 | + | { | |
| 129 | + | OTMLNodePtr child = m_confsDoc->get(key); | |
| 130 | + | if(child) | |
| 131 | + | m_confsDoc->removeChild(child); | |
| 132 | + | } | |
| 133 | + | ||
| 134 | + | void Config::setNode(const std::string& key, const OTMLNodePtr& node) | |
| 135 | + | { | |
| 136 | + | remove(key); | |
| 137 | + | mergeNode(key, node); | |
| 138 | + | } | |
| 139 | + | ||
| 140 | + | void Config::mergeNode(const std::string& key, const OTMLNodePtr& node) | |
| 141 | + | { | |
| 142 | + | OTMLNodePtr clone = node->clone(); | |
| 143 | + | node->setTag(key); | |
| 144 | + | node->setUnique(true); | |
| 145 | + | m_confsDoc->addChild(node); | |
| 146 | + | } | |
| 147 | + | ||
| 148 | + | OTMLNodePtr Config::getNode(const std::string& key) | |
| 149 | + | { | |
| 150 | + | return m_confsDoc->get(key); | |
| 151 | + | } | |
| 152 | + | ||
| 153 | + | int Config::getNodeSize(const std::string& key) | |
| 154 | + | { | |
| 155 | + | auto node = m_confsDoc->get(key); | |
| 156 | + | if (node) | |
| 157 | + | return node->size(); | |
| 158 | + | return 0; | |
| 159 | + | } | |
| 160 | + | ||
| 161 | + | ||
| 162 | + | bool Config::isLoaded() | |
| 163 | + | { | |
| 164 | + | return !m_fileName.empty() && m_confsDoc; | |
| 165 | + | } | |
| 166 | + | ||
| 167 | + | std::string Config::getFileName() | |
| 168 | + | { | |
| 169 | + | return m_fileName; | |
| 170 | + | } |
| @@ -0,0 +1,66 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef CONFIG_H | |
| 24 | + | #define CONFIG_H | |
| 25 | + | ||
| 26 | + | #include "declarations.h" | |
| 27 | + | ||
| 28 | + | #include <framework/luaengine/luaobject.h> | |
| 29 | + | #include <framework/otml/declarations.h> | |
| 30 | + | ||
| 31 | + | // @bindclass | |
| 32 | + | class Config : public LuaObject | |
| 33 | + | { | |
| 34 | + | public: | |
| 35 | + | Config(); | |
| 36 | + | ||
| 37 | + | bool load(const std::string& file); | |
| 38 | + | bool unload(); | |
| 39 | + | bool save(); | |
| 40 | + | void clear(); | |
| 41 | + | ||
| 42 | + | void setValue(const std::string& key, const std::string& value); | |
| 43 | + | void setList(const std::string& key, const std::vector<std::string>& list); | |
| 44 | + | std::string getValue(const std::string& key); | |
| 45 | + | std::vector<std::string> getList(const std::string& key); | |
| 46 | + | ||
| 47 | + | void setNode(const std::string& key, const OTMLNodePtr& node); | |
| 48 | + | void mergeNode(const std::string& key, const OTMLNodePtr& node); | |
| 49 | + | OTMLNodePtr getNode(const std::string& key); | |
| 50 | + | int getNodeSize(const std::string& key); | |
| 51 | + | ||
| 52 | + | bool exists(const std::string& key); | |
| 53 | + | void remove(const std::string& key); | |
| 54 | + | ||
| 55 | + | std::string getFileName(); | |
| 56 | + | bool isLoaded(); | |
| 57 | + | ||
| 58 | + | // @dontbind | |
| 59 | + | ConfigPtr asConfig() { return static_self_cast<Config>(); } | |
| 60 | + | ||
| 61 | + | private: | |
| 62 | + | std::string m_fileName; | |
| 63 | + | OTMLDocumentPtr m_confsDoc; | |
| 64 | + | }; | |
| 65 | + | ||
| 66 | + | #endif |
| @@ -0,0 +1,130 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "configmanager.h" | |
| 24 | + | ||
| 25 | + | ConfigManager g_configs; | |
| 26 | + | ||
| 27 | + | void ConfigManager::init() | |
| 28 | + | { | |
| 29 | + | m_settings = ConfigPtr(new Config()); | |
| 30 | + | } | |
| 31 | + | ||
| 32 | + | void ConfigManager::terminate() | |
| 33 | + | { | |
| 34 | + | if(m_settings) { | |
| 35 | + | // ensure settings are saved | |
| 36 | + | m_settings->save(); | |
| 37 | + | ||
| 38 | + | m_settings->unload(); | |
| 39 | + | m_settings = nullptr; | |
| 40 | + | } | |
| 41 | + | ||
| 42 | + | for(ConfigPtr config : m_configs) { | |
| 43 | + | config->unload(); | |
| 44 | + | config = nullptr; | |
| 45 | + | } | |
| 46 | + | ||
| 47 | + | m_configs.clear(); | |
| 48 | + | } | |
| 49 | + | ||
| 50 | + | ConfigPtr ConfigManager::getSettings() | |
| 51 | + | { | |
| 52 | + | return m_settings; | |
| 53 | + | } | |
| 54 | + | ||
| 55 | + | ConfigPtr ConfigManager::get(const std::string& file) | |
| 56 | + | { | |
| 57 | + | for(const ConfigPtr& config : m_configs) { | |
| 58 | + | if(config->getFileName() == file) { | |
| 59 | + | return config; | |
| 60 | + | } | |
| 61 | + | } | |
| 62 | + | return nullptr; | |
| 63 | + | } | |
| 64 | + | ||
| 65 | + | ConfigPtr ConfigManager::loadSettings(const std::string file) | |
| 66 | + | { | |
| 67 | + | if(file.empty()) { | |
| 68 | + | g_logger.error("Must provide a configuration file to load."); | |
| 69 | + | } | |
| 70 | + | else { | |
| 71 | + | if(m_settings->load(file)) { | |
| 72 | + | return m_settings; | |
| 73 | + | } | |
| 74 | + | } | |
| 75 | + | return nullptr; | |
| 76 | + | } | |
| 77 | + | ||
| 78 | + | ConfigPtr ConfigManager::create(const std::string& file) | |
| 79 | + | { | |
| 80 | + | ConfigPtr config = load(file); | |
| 81 | + | if(!config) { | |
| 82 | + | config = ConfigPtr(new Config()); | |
| 83 | + | ||
| 84 | + | config->load(file); | |
| 85 | + | config->save(); | |
| 86 | + | ||
| 87 | + | m_configs.push_back(config); | |
| 88 | + | } | |
| 89 | + | return config; | |
| 90 | + | } | |
| 91 | + | ||
| 92 | + | ConfigPtr ConfigManager::load(const std::string& file) | |
| 93 | + | { | |
| 94 | + | if(file.empty()) { | |
| 95 | + | g_logger.error("Must provide a configuration file to load."); | |
| 96 | + | return nullptr; | |
| 97 | + | } | |
| 98 | + | else { | |
| 99 | + | ConfigPtr config = get(file); | |
| 100 | + | if(!config) { | |
| 101 | + | config = ConfigPtr(new Config()); | |
| 102 | + | ||
| 103 | + | if(config->load(file)) { | |
| 104 | + | m_configs.push_back(config); | |
| 105 | + | } | |
| 106 | + | else { | |
| 107 | + | // cannot load config | |
| 108 | + | config = nullptr; | |
| 109 | + | } | |
| 110 | + | } | |
| 111 | + | return config; | |
| 112 | + | } | |
| 113 | + | } | |
| 114 | + | ||
| 115 | + | bool ConfigManager::unload(const std::string& file) | |
| 116 | + | { | |
| 117 | + | ConfigPtr config = get(file); | |
| 118 | + | if(config) { | |
| 119 | + | config->unload(); | |
| 120 | + | remove(config); | |
| 121 | + | config = nullptr; | |
| 122 | + | return true; | |
| 123 | + | } | |
| 124 | + | return false; | |
| 125 | + | } | |
| 126 | + | ||
| 127 | + | void ConfigManager::remove(const ConfigPtr config) | |
| 128 | + | { | |
| 129 | + | m_configs.remove(config); | |
| 130 | + | } |
| @@ -0,0 +1,54 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef CONFIGMANAGER_H | |
| 24 | + | #define CONFIGMANAGER_H | |
| 25 | + | ||
| 26 | + | #include "config.h" | |
| 27 | + | ||
| 28 | + | // @bindsingleton g_configs | |
| 29 | + | class ConfigManager | |
| 30 | + | { | |
| 31 | + | public: | |
| 32 | + | void init(); | |
| 33 | + | void terminate(); | |
| 34 | + | ||
| 35 | + | ConfigPtr getSettings(); | |
| 36 | + | ConfigPtr get(const std::string& file); | |
| 37 | + | ||
| 38 | + | ConfigPtr create(const std::string& file); | |
| 39 | + | ConfigPtr loadSettings(const std::string file); | |
| 40 | + | ConfigPtr load(const std::string& file); | |
| 41 | + | ||
| 42 | + | bool unload(const std::string& file); | |
| 43 | + | void remove(const ConfigPtr config); | |
| 44 | + | ||
| 45 | + | protected: | |
| 46 | + | ConfigPtr m_settings; | |
| 47 | + | ||
| 48 | + | private: | |
| 49 | + | std::list<ConfigPtr> m_configs; | |
| 50 | + | }; | |
| 51 | + | ||
| 52 | + | extern ConfigManager g_configs; | |
| 53 | + | ||
| 54 | + | #endif |
| @@ -0,0 +1,55 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | ||
| 24 | + | #include "consoleapplication.h" | |
| 25 | + | #include <framework/core/clock.h> | |
| 26 | + | #include <framework/luaengine/luainterface.h> | |
| 27 | + | ||
| 28 | + | #ifdef FW_NET | |
| 29 | + | #include <framework/net/connection.h> | |
| 30 | + | #endif | |
| 31 | + | ||
| 32 | + | ConsoleApplication g_app; | |
| 33 | + | ||
| 34 | + | void ConsoleApplication::run() | |
| 35 | + | { | |
| 36 | + | m_running = true; | |
| 37 | + | ||
| 38 | + | // run the first poll | |
| 39 | + | poll(); | |
| 40 | + | ||
| 41 | + | // first clock update | |
| 42 | + | g_clock.update(); | |
| 43 | + | ||
| 44 | + | g_lua.callGlobalField("g_app", "onRun"); | |
| 45 | + | ||
| 46 | + | while(!m_stopping) { | |
| 47 | + | poll(); | |
| 48 | + | stdext::millisleep(1); | |
| 49 | + | g_clock.update(); | |
| 50 | + | m_frameCounter.update(); | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | m_stopping = false; | |
| 54 | + | m_running = false; | |
| 55 | + | } |
| @@ -0,0 +1,40 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | ||
| 24 | + | #ifndef CONSOLEAPPLICATION_H | |
| 25 | + | #define CONSOLEAPPLICATION_H | |
| 26 | + | ||
| 27 | + | #include "application.h" | |
| 28 | + | ||
| 29 | + | class ConsoleApplication : public Application | |
| 30 | + | { | |
| 31 | + | public: | |
| 32 | + | void run(); | |
| 33 | + | ||
| 34 | + | protected: | |
| 35 | + | ||
| 36 | + | }; | |
| 37 | + | ||
| 38 | + | extern ConsoleApplication g_app; | |
| 39 | + | ||
| 40 | + | #endif |
| @@ -0,0 +1,49 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef FRAMEWORK_CORE_DECLARATIONS_H | |
| 24 | + | #define FRAMEWORK_CORE_DECLARATIONS_H | |
| 25 | + | ||
| 26 | + | #include <framework/global.h> | |
| 27 | + | ||
| 28 | + | class ConfigManager; | |
| 29 | + | class ModuleManager; | |
| 30 | + | class ResourceManager; | |
| 31 | + | class Module; | |
| 32 | + | class Config; | |
| 33 | + | class Event; | |
| 34 | + | class ScheduledEvent; | |
| 35 | + | class FileStream; | |
| 36 | + | class BinaryTree; | |
| 37 | + | class OutputBinaryTree; | |
| 38 | + | ||
| 39 | + | typedef stdext::shared_object_ptr<Module> ModulePtr; | |
| 40 | + | typedef stdext::shared_object_ptr<Config> ConfigPtr; | |
| 41 | + | typedef stdext::shared_object_ptr<Event> EventPtr; | |
| 42 | + | typedef stdext::shared_object_ptr<ScheduledEvent> ScheduledEventPtr; | |
| 43 | + | typedef stdext::shared_object_ptr<FileStream> FileStreamPtr; | |
| 44 | + | typedef stdext::shared_object_ptr<BinaryTree> BinaryTreePtr; | |
| 45 | + | typedef stdext::shared_object_ptr<OutputBinaryTree> OutputBinaryTreePtr; | |
| 46 | + | ||
| 47 | + | typedef std::vector<BinaryTreePtr> BinaryTreeVec; | |
| 48 | + | ||
| 49 | + | #endif |
| @@ -0,0 +1,55 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "event.h" | |
| 24 | + | ||
| 25 | + | Event::Event(const std::string& function, const std::function<void()>& callback, bool botSafe) : | |
| 26 | + | m_function(function), | |
| 27 | + | m_callback(callback), | |
| 28 | + | m_canceled(false), | |
| 29 | + | m_executed(false), | |
| 30 | + | m_botSafe(botSafe) | |
| 31 | + | { | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | Event::~Event() | |
| 35 | + | { | |
| 36 | + | // assure that we lost callback refs | |
| 37 | + | //VALIDATE(m_callback == nullptr); | |
| 38 | + | } | |
| 39 | + | ||
| 40 | + | void Event::execute() | |
| 41 | + | { | |
| 42 | + | if(!m_canceled && !m_executed && m_callback) { | |
| 43 | + | m_callback(); | |
| 44 | + | m_executed = true; | |
| 45 | + | } | |
| 46 | + | ||
| 47 | + | // reset callback to free object refs | |
| 48 | + | m_callback = nullptr; | |
| 49 | + | } | |
| 50 | + | ||
| 51 | + | void Event::cancel() | |
| 52 | + | { | |
| 53 | + | m_canceled = true; | |
| 54 | + | m_callback = nullptr; | |
| 55 | + | } |
| @@ -0,0 +1,52 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef EVENT_H | |
| 24 | + | #define EVENT_H | |
| 25 | + | ||
| 26 | + | #include <framework/luaengine/luaobject.h> | |
| 27 | + | ||
| 28 | + | // @bindclass | |
| 29 | + | class Event : public LuaObject | |
| 30 | + | { | |
| 31 | + | public: | |
| 32 | + | Event(const std::string& function, const std::function<void()>& callback, bool botSafe = false); | |
| 33 | + | virtual ~Event(); | |
| 34 | + | ||
| 35 | + | virtual void execute(); | |
| 36 | + | void cancel(); | |
| 37 | + | ||
| 38 | + | bool isCanceled() { return m_canceled; } | |
| 39 | + | bool isExecuted() { return m_executed; } | |
| 40 | + | bool isBotSafe() { return m_botSafe; } | |
| 41 | + | ||
| 42 | + | const std::string& getFunction() { return m_function; } | |
| 43 | + | ||
| 44 | + | protected: | |
| 45 | + | std::string m_function; | |
| 46 | + | std::function<void()> m_callback; | |
| 47 | + | bool m_canceled; | |
| 48 | + | bool m_executed; | |
| 49 | + | bool m_botSafe; | |
| 50 | + | }; | |
| 51 | + | ||
| 52 | + | #endif |
| @@ -0,0 +1,161 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #include "eventdispatcher.h" | |
| 24 | + | ||
| 25 | + | #include <framework/core/clock.h> | |
| 26 | + | #include <framework/core/graphicalapplication.h> | |
| 27 | + | #include <framework/graphics/graph.h> | |
| 28 | + | #include <framework/util/stats.h> | |
| 29 | + | #include "timer.h" | |
| 30 | + | ||
| 31 | + | EventDispatcher g_dispatcher; | |
| 32 | + | EventDispatcher g_graphicsDispatcher; | |
| 33 | + | std::thread::id g_mainThreadId = std::this_thread::get_id(); | |
| 34 | + | std::thread::id g_graphicsThreadId = std::this_thread::get_id(); | |
| 35 | + | std::thread::id g_dispatcherThreadId = std::this_thread::get_id(); | |
| 36 | + | ||
| 37 | + | void EventDispatcher::shutdown() | |
| 38 | + | { | |
| 39 | + | while(!m_eventList.empty()) | |
| 40 | + | poll(); | |
| 41 | + | ||
| 42 | + | while(!m_scheduledEventList.empty()) { | |
| 43 | + | ScheduledEventPtr scheduledEvent = m_scheduledEventList.top(); | |
| 44 | + | scheduledEvent->cancel(); | |
| 45 | + | m_scheduledEventList.pop(); | |
| 46 | + | } | |
| 47 | + | m_disabled = true; | |
| 48 | + | } | |
| 49 | + | ||
| 50 | + | void EventDispatcher::poll() | |
| 51 | + | { | |
| 52 | + | AutoStat s(this == &g_dispatcher ? STATS_MAIN : STATS_RENDER, "PollDispatcher"); | |
| 53 | + | std::unique_lock<std::recursive_mutex> lock(m_mutex); | |
| 54 | + | ||
| 55 | + | int events = 0; | |
| 56 | + | int loops = 0; | |
| 57 | + | for(int count = 0, max = m_scheduledEventList.size(); count < max && !m_scheduledEventList.empty(); ++count) { | |
| 58 | + | ScheduledEventPtr scheduledEvent = m_scheduledEventList.top(); | |
| 59 | + | if(scheduledEvent->remainingTicks() > 0) | |
| 60 | + | break; | |
| 61 | + | m_scheduledEventList.pop(); | |
| 62 | + | { | |
| 63 | + | AutoStat s2(STATS_DISPATCHER, scheduledEvent->getFunction()); | |
| 64 | + | m_botSafe = scheduledEvent->isBotSafe(); | |
| 65 | + | lock.unlock(); | |
| 66 | + | scheduledEvent->execute(); | |
| 67 | + | events += 1; | |
| 68 | + | lock.lock(); | |
| 69 | + | } | |
| 70 | + | ||
| 71 | + | if(scheduledEvent->nextCycle()) | |
| 72 | + | m_scheduledEventList.push(scheduledEvent); | |
| 73 | + | } | |
| 74 | + | ||
| 75 | + | // execute events list until all events are out, this is needed because some events can schedule new events that would | |
| 76 | + | // change the UIWidgets layout, in this case we must execute these new events before we continue rendering, | |
| 77 | + | m_pollEventsSize = m_eventList.size(); | |
| 78 | + | loops = 0; | |
| 79 | + | while(m_pollEventsSize > 0) { | |
| 80 | + | if(loops > 100) { | |
| 81 | + | static Timer reportTimer; | |
| 82 | + | if(reportTimer.running() && reportTimer.ticksElapsed() > 500) { | |
| 83 | + | std::stringstream ss; | |
| 84 | + | ss << "ATTENTION the event list is not getting empty, this could be caused by some bad code.\nLog:\n"; | |
| 85 | + | for (auto& event : m_eventList) { | |
| 86 | + | ss << event->getFunction() << "\n"; | |
| 87 | + | if (ss.str().size() > 512) break; | |
| 88 | + | } | |
| 89 | + | g_logger.error(ss.str()); | |
| 90 | + | reportTimer.restart(); | |
| 91 | + | } | |
| 92 | + | break; | |
| 93 | + | } | |
| 94 | + | ||
| 95 | + | for(int i=0;i<m_pollEventsSize;++i) { | |
| 96 | + | EventPtr event = m_eventList.front(); | |
| 97 | + | m_eventList.pop_front(); | |
| 98 | + | { | |
| 99 | + | AutoStat s2(STATS_DISPATCHER, event->getFunction()); | |
| 100 | + | m_botSafe = event->isBotSafe(); | |
| 101 | + | lock.unlock(); | |
| 102 | + | event->execute(); | |
| 103 | + | events += 1; | |
| 104 | + | lock.lock(); | |
| 105 | + | } | |
| 106 | + | } | |
| 107 | + | m_pollEventsSize = m_eventList.size(); | |
| 108 | + | ||
| 109 | + | loops++; | |
| 110 | + | } | |
| 111 | + | ||
| 112 | + | g_graphs[this == &g_dispatcher ? GRAPH_DISPATCHER_EVENTS : GRAPH_GRAPHICS_EVENTS].addValue(events, true); | |
| 113 | + | ||
| 114 | + | m_botSafe = false; | |
| 115 | + | } | |
| 116 | + | ||
| 117 | + | ScheduledEventPtr EventDispatcher::scheduleEventEx(const std::string& function, const std::function<void()>& callback, int delay) | |
| 118 | + | { | |
| 119 | + | if(m_disabled) | |
| 120 | + | return ScheduledEventPtr(new ScheduledEvent("", nullptr, delay, 1)); | |
| 121 | + | ||
| 122 | + | std::lock_guard<std::recursive_mutex> lock(m_mutex); | |
| 123 | + | ||
| 124 | + | VALIDATE(delay >= 0); | |
| 125 | + | ScheduledEventPtr scheduledEvent(new ScheduledEvent(function, callback, delay, 1, g_app.isOnInputEvent())); | |
| 126 | + | m_scheduledEventList.push(scheduledEvent); | |
| 127 | + | return scheduledEvent; | |
| 128 | + | } | |
| 129 | + | ||
| 130 | + | ScheduledEventPtr EventDispatcher::cycleEventEx(const std::string& function, const std::function<void()>& callback, int delay) | |
| 131 | + | { | |
| 132 | + | if(m_disabled) | |
| 133 | + | return ScheduledEventPtr(new ScheduledEvent("", nullptr, delay, 0)); | |
| 134 | + | ||
| 135 | + | std::lock_guard<std::recursive_mutex> lock(m_mutex); | |
| 136 | + | ||
| 137 | + | VALIDATE(delay > 0); | |
| 138 | + | ScheduledEventPtr scheduledEvent(new ScheduledEvent(function, callback, delay, 0, g_app.isOnInputEvent())); | |
| 139 | + | m_scheduledEventList.push(scheduledEvent); | |
| 140 | + | return scheduledEvent; | |
| 141 | + | } | |
| 142 | + | ||
| 143 | + | EventPtr EventDispatcher::addEventEx(const std::string& function, const std::function<void()>& callback, bool pushFront) | |
| 144 | + | { | |
| 145 | + | if(m_disabled) | |
| 146 | + | return EventPtr(new Event("", nullptr)); | |
| 147 | + | ||
| 148 | + | EventPtr event(new Event(function, callback, g_app.isOnInputEvent())); | |
| 149 | + | ||
| 150 | + | std::lock_guard<std::recursive_mutex> lock(m_mutex); | |
| 151 | + | ||
| 152 | + | // front pushing is a way to execute an event before others | |
| 153 | + | if(pushFront) { | |
| 154 | + | m_eventList.push_front(event); | |
| 155 | + | // the poll event list only grows when pushing into front | |
| 156 | + | m_pollEventsSize++; | |
| 157 | + | } else | |
| 158 | + | m_eventList.push_back(event); | |
| 159 | + | return event; | |
| 160 | + | } | |
| 161 | + |
| @@ -0,0 +1,66 @@ | |||
| 1 | + | /* | |
| 2 | + | * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient> | |
| 3 | + | * | |
| 4 | + | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 | + | * of this software and associated documentation files (the "Software"), to deal | |
| 6 | + | * in the Software without restriction, including without limitation the rights | |
| 7 | + | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 | + | * copies of the Software, and to permit persons to whom the Software is | |
| 9 | + | * furnished to do so, subject to the following conditions: | |
| 10 | + | * | |
| 11 | + | * The above copyright notice and this permission notice shall be included in | |
| 12 | + | * all copies or substantial portions of the Software. | |
| 13 | + | * | |
| 14 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 | + | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 | + | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 | + | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | + | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 | + | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 | + | * THE SOFTWARE. | |
| 21 | + | */ | |
| 22 | + | ||
| 23 | + | #ifndef EVENTDISPATCHER_H | |
| 24 | + | #define EVENTDISPATCHER_H | |
| 25 | + | ||
| 26 | + | #include "clock.h" | |
| 27 | + | #include "scheduledevent.h" | |
| 28 | + | ||
| 29 | + | #include <queue> | |
| 30 | + | ||
| 31 | + | // @bindsingleton g_dispatcher | |
| 32 | + | class EventDispatcher | |
| 33 | + | { | |
| 34 | + | public: | |
| 35 | + | void shutdown(); | |
| 36 | + | void poll(); | |
| 37 | + | ||
| 38 | + | EventPtr addEventEx(const std::string& function, const std::function<void()>& callback, bool pushFront = false); | |
| 39 | + | ScheduledEventPtr scheduleEventEx(const std::string& function, const std::function<void()>& callback, int delay); | |
| 40 | + | ScheduledEventPtr cycleEventEx(const std::string& function, const std::function<void()>& callback, int delay); | |
| 41 | + | ||
| 42 | + | bool isBotSafe() { return m_botSafe; } | |
| 43 | + | ||
| 44 | + | private: | |
| 45 | + | std::list<EventPtr> m_eventList; | |
| 46 | + | int m_pollEventsSize; | |
| 47 | + | bool m_disabled = false; | |
| 48 | + | bool m_botSafe = false; | |
| 49 | + | std::recursive_mutex m_mutex; | |
| 50 | + | std::priority_queue<ScheduledEventPtr, std::vector<ScheduledEventPtr>, lessScheduledEvent> m_scheduledEventList; | |
| 51 | + | }; | |
| 52 | + | ||
| 53 | + | extern EventDispatcher g_dispatcher; | |
| 54 | + | extern EventDispatcher g_graphicsDispatcher; | |
| 55 | + | extern std::thread::id g_mainThreadId; | |
| 56 | + | extern std::thread::id g_dispatcherThreadId; | |
| 57 | + | extern std::thread::id g_graphicsThreadId; | |
| 58 | + | ||
| 59 | + | #define addEvent(...) addEventEx(__FUNCTION__, __VA_ARGS__) | |
| 60 | + | #define scheduleEvent(...) scheduleEventEx(__FUNCTION__, __VA_ARGS__) | |
| 61 | + | #define cycleEvent(...) cycleEventEx(__FUNCTION__, __VA_ARGS__) | |
| 62 | + | ||
| 63 | + | #define VALIDATE_GRAPHICS_THREAD() VALIDATE(std::this_thread::get_id() == g_graphicsThreadId) | |
| 64 | + | #define VALIDATE_DISPATCHER_THREAD() VALIDATE(std::this_thread::get_id() == g_dispatcherThreadId) | |
| 65 | + | ||
| 66 | + | #endif |