forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtitle-folder.cpp
More file actions
116 lines (98 loc) · 2.86 KB
/
Copy pathtitle-folder.cpp
File metadata and controls
116 lines (98 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "Console.h"
#include "Core.h"
#include "DataDefs.h"
#include "Export.h"
#include "MemAccess.h"
#include "PluginManager.h"
using namespace DFHack;
DFHACK_PLUGIN("title-folder");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
// SDL frees the old window title when changed
static std::string original_title;
static DFLibrary *sdl_handle = NULL;
static const std::vector<std::string> sdl_libs {
"SDLreal.dll",
"SDL.framework/Versions/A/SDL",
"SDL.framework/SDL",
"libSDL-1.2.so.0"
};
void (*_SDL_WM_GetCaption)(const char**, const char**) = NULL;
void SDL_WM_GetCaption(const char **title, const char **icon) {
_SDL_WM_GetCaption(title, icon);
}
void (*_SDL_WM_SetCaption)(const char*, const char*) = NULL;
void SDL_WM_SetCaption(const char *title, const char *icon) {
_SDL_WM_SetCaption(title, icon);
}
DFhackCExport command_result plugin_enable (color_ostream &out, bool state);
DFhackCExport command_result plugin_shutdown (color_ostream &out);
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{
for (auto it = sdl_libs.begin(); it != sdl_libs.end(); ++it)
{
if ((sdl_handle = OpenPlugin(it->c_str())))
break;
}
if (!sdl_handle)
{
out.printerr("title-folder: Could not load SDL.\n");
return CR_FAILURE;
}
#define bind(name) \
_##name = (decltype(_##name))LookupPlugin(sdl_handle, #name); \
if (!_##name) { \
out.printerr("title-folder: Bind failed: " #name "\n"); \
plugin_shutdown(out); \
return CR_FAILURE; \
}
bind(SDL_WM_GetCaption);
bind(SDL_WM_SetCaption);
#undef bind
const char *title = NULL;
SDL_WM_GetCaption(&title, NULL);
if (!title)
{
out.printerr("title-folder: Failed to get original title\n");
title = "Dwarf Fortress";
}
original_title = title;
return CR_OK;
}
DFhackCExport command_result plugin_shutdown (color_ostream &out)
{
if (is_enabled)
{
plugin_enable(out, false);
}
if (sdl_handle)
{
ClosePlugin(sdl_handle);
sdl_handle = NULL;
}
return CR_OK;
}
DFhackCExport command_result plugin_enable (color_ostream &out, bool state)
{
if (state == is_enabled)
return CR_OK;
if (state)
{
std::string path = Core::getInstance().p->getPath();
std::string folder;
size_t pos = path.find_last_of('/');
if (pos == std::string::npos)
pos = path.find_last_of('\\');
if (pos != std::string::npos)
folder = path.substr(pos + 1);
else
folder = path;
std::string title = original_title + " (" + folder + ")";
SDL_WM_SetCaption(title.c_str(), NULL);
}
else
{
SDL_WM_SetCaption(original_title.c_str(), NULL);
}
is_enabled = state;
return CR_OK;
}