From 331ada8f91008cd0232fedeb610e745a966dbae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 1 Aug 2011 03:31:52 +0200 Subject: [PATCH] Don't take over console in TEXT mode. --- Memory.xml | 2 + library/Console-linux.cpp | 14 +++++-- library/Core.cpp | 21 ++++++++-- library/FakeSDL-linux.cpp | 4 +- library/FakeSDL-windows.cpp | 2 - library/Process-linux.cpp | 16 ++++---- library/include/dfhack/BitArray.h | 36 +++++++++--------- library/include/dfhack/Console.h | 2 +- library/include/dfhack/modules/Gui.h | 55 ++++++++++++++++++++++++++- library/include/dfhack/modules/Maps.h | 17 ++++++--- library/modules/Gui.cpp | 10 +++++ library/modules/Maps.cpp | 4 +- 12 files changed, 138 insertions(+), 45 deletions(-) diff --git a/Memory.xml b/Memory.xml index b662e7676..a7edf847c 100644 --- a/Memory.xml +++ b/Memory.xml @@ -806,6 +806,7 @@
+
@@ -3055,6 +3056,7 @@
+
Maybe, possibly. diff --git a/library/Console-linux.cpp b/library/Console-linux.cpp index c32a9065f..e9dce679a 100644 --- a/library/Console-linux.cpp +++ b/library/Console-linux.cpp @@ -601,7 +601,7 @@ Console::Console():std::ostream(0), std::ios(0) d = 0; inited = false; // we can't create the mutex at this time. the SDL functions aren't hooked yet. - wlock = 0; + wlock = new mutex(); } Console::~Console() { @@ -613,12 +613,18 @@ Console::~Console() delete d; } -bool Console::init(void) +bool Console::init(bool sharing) { + if(sharing) + { + inited = false; + return false; + } + freopen("stdout.log", "w", stdout); + freopen("stderr.log", "w", stderr); d = new Private(); // make our own weird streams so our IO isn't redirected d->dfout_C = fopen("/dev/tty", "w"); - wlock = new mutex(); rdbuf(d); std::cin.tie(this); clear(); @@ -628,6 +634,8 @@ bool Console::init(void) bool Console::shutdown(void) { + if(!d) + return true; lock_guard g(*wlock); if(d->rawmode) d->disable_raw(); diff --git a/library/Core.cpp b/library/Core.cpp index a07c395de..f26a37a9b 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -387,11 +387,24 @@ bool Core::Init() { if(started) return true; - // init the console. This must be always the first step! - con.init(); + // find out what we are... vif = new DFHack::VersionInfoFactory("Memory.xml"); p = new DFHack::Process(vif); + vinfo = p->getDescriptor(); + + // init the console. + Gui * g = getGui(); + if(g->init) + { + if(g->init->graphics.flags.is_set(GRAPHICS_TEXT)) + { + con.init(true); + } + else con.init(false); + } + else con.init(false); + if (!p->isIdentified()) { con.printerr("Couldn't identify this version of DF.\n"); @@ -400,7 +413,7 @@ bool Core::Init() p = NULL; return false; } - vinfo = p->getDescriptor(); + // create mutex for syncing with interactive tasks StackMutex = new mutex(); AccessMutex = new mutex(); @@ -472,6 +485,8 @@ void Core::Resume() // should always be from simulation thread! int Core::Update() { + if(!started) + Init(); if(errorstate) return -1; diff --git a/library/FakeSDL-linux.cpp b/library/FakeSDL-linux.cpp index 4bb3c4243..8409a2b1e 100644 --- a/library/FakeSDL-linux.cpp +++ b/library/FakeSDL-linux.cpp @@ -108,8 +108,6 @@ DFhackCExport int SDL_PollEvent(SDL::Event* event) static int (*_SDL_Init)(uint32_t flags) = 0; DFhackCExport int SDL_Init(uint32_t flags) { - freopen("stdout.log", "w", stdout); - freopen("stderr.log", "w", stderr); // find real functions _SDL_Init = (int (*)( uint32_t )) dlsym(RTLD_NEXT, "SDL_Init"); _SDL_Quit = (void (*)( void )) dlsym(RTLD_NEXT, "SDL_Quit"); @@ -126,8 +124,10 @@ DFhackCExport int SDL_Init(uint32_t flags) fprintf(stderr,"dfhack: something went horribly wrong\n"); exit(1); } + /* DFHack::Core & c = DFHack::Core::getInstance(); c.Init(); + */ int ret = _SDL_Init(flags); return ret; } diff --git a/library/FakeSDL-windows.cpp b/library/FakeSDL-windows.cpp index 4b7f0bb78..f1abe076a 100644 --- a/library/FakeSDL-windows.cpp +++ b/library/FakeSDL-windows.cpp @@ -596,8 +596,6 @@ DFhackCExport int SDL_Init(uint32_t flags) { if(!inited) FirstCall(); - DFHack::Core & c = DFHack::Core::getInstance(); - c.Init(); return _SDL_Init(flags); } diff --git a/library/Process-linux.cpp b/library/Process-linux.cpp index 937bd155b..f68fd20b7 100644 --- a/library/Process-linux.cpp +++ b/library/Process-linux.cpp @@ -181,12 +181,12 @@ int Process::getPID() bool Process::setPermisions(const t_memrange & range,const t_memrange &trgrange) { - int result; - int protect=0; - if(trgrange.read)protect|=PROT_READ; - if(trgrange.write)protect|=PROT_WRITE; - if(trgrange.execute)protect|=PROT_EXECUTE; - result=mprotect((void *)range.start, range.end-range.start,protect); - - return result==0; + int result; + int protect=0; + if(trgrange.read)protect|=PROT_READ; + if(trgrange.write)protect|=PROT_WRITE; + if(trgrange.execute)protect|=PROT_EXEC; + result=mprotect((void *)range.start, range.end-range.start,protect); + + return result==0; } \ No newline at end of file diff --git a/library/include/dfhack/BitArray.h b/library/include/dfhack/BitArray.h index dffdddb4d..7e4b8a915 100644 --- a/library/include/dfhack/BitArray.h +++ b/library/include/dfhack/BitArray.h @@ -31,6 +31,7 @@ distribution. //#include namespace DFHack { + template class BitArray { public: @@ -49,7 +50,7 @@ namespace DFHack if(bits) memset(bits, 0, size); } - void set (uint32_t index, bool value = true) + void set (T index, bool value = true) { if(!value) { @@ -63,7 +64,7 @@ namespace DFHack bits[byte] |= bit; } } - void clear (uint32_t index) + void clear (T index) { uint32_t byte = index / 8; if(byte < size) @@ -72,7 +73,7 @@ namespace DFHack bits[byte] &= ~bit; } } - void toggle (uint32_t index) + void toggle (T index) { uint32_t byte = index / 8; if(byte < size) @@ -81,7 +82,7 @@ namespace DFHack bits[byte] ^= bit; } } - bool is_set (uint32_t index) + bool is_set (T index) { uint32_t byte = index / 8; if(byte < size) @@ -115,22 +116,21 @@ namespace DFHack memcpy(bits, &data, size); return true; } - friend std::ostream& operator<< (std::ostream &out, BitArray &ba); + friend std::ostream& operator<< (std::ostream &out, BitArray &ba) + { + std::stringstream sstr; + for (int i = 0; i < ba.size * 8; i++) + { + if(ba.is_set((T)i)) + sstr << "1 "; + else + sstr << "0 "; + } + out << sstr.str(); + return out; + } //private: uint8_t * bits; uint32_t size; }; - inline std::ostream& operator<< (std::ostream &out, BitArray &ba) - { - std::stringstream sstr; - for (int i = 0; i < ba.size * 8; i++) - { - if(ba.is_set(i)) - sstr << "1 "; - else - sstr << "0 "; - } - out << sstr.str(); - return out; - } } \ No newline at end of file diff --git a/library/include/dfhack/Console.h b/library/include/dfhack/Console.h index 1a1101637..0c43ccd78 100644 --- a/library/include/dfhack/Console.h +++ b/library/include/dfhack/Console.h @@ -64,7 +64,7 @@ namespace DFHack ///dtor, NOT thread-safe ~Console(); /// initialize the console. NOT thread-safe - bool init( void ); + bool init( bool sharing ); /// shutdown the console. NOT thread-safe bool shutdown( void ); diff --git a/library/include/dfhack/modules/Gui.h b/library/include/dfhack/modules/Gui.h index 49bb050be..4a5ba7340 100644 --- a/library/include/dfhack/modules/Gui.h +++ b/library/include/dfhack/modules/Gui.h @@ -29,6 +29,7 @@ distribution. #include "dfhack/Export.h" #include "dfhack/Module.h" #include "dfhack/Virtual.h" +#include "dfhack/BitArray.h" #include /** @@ -40,6 +41,7 @@ namespace DFHack { class DFContextShared; /** + * A GUI screen * \ingroup grp_gui */ struct t_viewscreen : public t_virtual @@ -50,6 +52,7 @@ namespace DFHack char unk2; // state? }; /** + * Interface - wrapper for the GUI * \ingroup grp_gui */ struct t_interface @@ -59,7 +62,52 @@ namespace DFHack unsigned int flags; // ? // more crud this way ... }; - + enum graphics_flag + { + GRAPHICS_ENABLED = 0, + GRAPHICS_BLACKSPACE = 1, + GRAPHICS_PARTIAL_PRINT = 2, + GRAPHICS_TEXT = 11, + GRAPHICS_FIXED_SIZE = 13 + }; + enum media_flag + { + MEDIA_NO_SOUND, + MEDIA_NO_INTRO, + MEDIA_COMPRESS_WORLDS, + }; + /** + * The init structure - basically DF settings + * \ingroup grp_gui + */ + struct t_init + { + struct + { + BitArray flags; + enum + { + WINDOWED_YES, + WINDOWED_NO, + WINDOWED_PROMPT + } windowed; + // screen size in tiles + int grid_x; + int grid_y; + // in pixels ? + int fullscreen_x; + int fullscreen_y; + int window_x; + int window_y; + char partial_print; + } graphics; + struct + { + BitArray flags; + int32_t volume; + } media; + // much more stuff follows + }; #define NUM_HOTKEYS 16 /** * The hotkey structure @@ -124,6 +172,11 @@ namespace DFHack * Hotkeys (DF's zoom locations) */ hotkey_array * hotkeys; + + /* + * Game settings + */ + t_init * init; /* * Window size in tiles diff --git a/library/include/dfhack/modules/Maps.h b/library/include/dfhack/modules/Maps.h index aa05d7645..0873991e0 100644 --- a/library/include/dfhack/modules/Maps.h +++ b/library/include/dfhack/modules/Maps.h @@ -461,11 +461,18 @@ namespace DFHack unsigned int liquid_1 : 1; unsigned int liquid_2 : 1; /// rest of the flags is completely unknown - unsigned int unk_2: 28; - // there's a possibility that this flags field is shorter than 32 bits - // FIXME: yes, it's a crazy dynamically sized array of flags. DERP. + unsigned int unk_2: 4; + }; + enum e_block_flags + { + /// designated for jobs (digging and stuff like that) + BLOCK_DESIGNATED, + /// possibly related to the designated flag + BLOCK_UNKN1, + /// two flags required for liquid flow. + BLOCK_LIQUIDFLOW_1, + BLOCK_LIQUIDFLOW_2, }; - /** * map block flags wrapper * \ingroup grp_maps @@ -535,7 +542,7 @@ namespace DFHack // one of the vector is the 'effects' vector. another should be item id/index vector struct df_block { - BitArray flags; + BitArray flags; // how to handle this virtual mess? std::vector block_events; // no idea what these are diff --git a/library/modules/Gui.cpp b/library/modules/Gui.cpp index c934adfba..c4a75eb4d 100644 --- a/library/modules/Gui.cpp +++ b/library/modules/Gui.cpp @@ -83,6 +83,16 @@ Gui::Gui() hotkeys = 0; }; + // Setting up init + try + { + init = (t_init *) OG_Gui->getAddress("init"); + } + catch(Error::All &) + { + init = 0; + }; + // Setting up menu state try { diff --git a/library/modules/Maps.cpp b/library/modules/Maps.cpp index 887e8c754..5b68b384f 100644 --- a/library/modules/Maps.cpp +++ b/library/modules/Maps.cpp @@ -339,7 +339,7 @@ bool Maps::ReadDirtyBit(uint32_t x, uint32_t y, uint32_t z, bool &dirtybit) df_block * block = getBlock(x,y,z); if (block) { - dirtybit = block->flags.is_set(1); + dirtybit = block->flags.is_set(BLOCK_DESIGNATED); return true; } return false; @@ -351,7 +351,7 @@ bool Maps::WriteDirtyBit(uint32_t x, uint32_t y, uint32_t z, bool dirtybit) df_block * block = getBlock(x,y,z); if (block) { - block->flags.set(1,dirtybit); + block->flags.set(BLOCK_DESIGNATED,dirtybit); return true; } return false;