03.04.2022, 17:59
Hmm. I just tried it and I have this symbol » normally displayed with json from the post above.
Is it because I have Cyrillic windows?
Is it because I have Cyrillic windows?
std::vector<std::string> Key;
std::vector<std::string> defname;
...
int i = 0;
defname[i] = Era::tr(Key[i].c_str());
(25.02.2023 20:02)wessonsm Wrote: [ -> ]У меня в json несколько ключей с именами типа "wss.defname1", "wss.defname2", "wss.defname3" и так далее.
Как мне прочитать их значения в цикле и записать в массив?
Если я пытаюсь получить имя ключа так:
sprintf(Key[i], "wss.defname%d", i);
defname[i] = tr(Key[i]);
у меня не получается тип const char*, который требует функция tr.
(26.02.2023 18:14)wessonsm Wrote: [ -> ]С трудом мне эти json даются.
Пока все в коде плагина - все работает, как только пытаюсь сделать загрузку параметров из json - все ломается.
(26.02.2023 20:23)XEPOMAHT Wrote: [ -> ]Не мучайся и подгружай обычный txt. Лично у меня в ERA+ json не используются как раз по причине их сложности: не могу сделать их загрузку в память программы и в блокноте создавать сам json - писец какой-то из-за многочисленных фигурных скобочек, в которых я постоянно ошибаюсь....Возможно, я так и сделаю.
Quote:Получение строк из json и так работает не очень быстроМаксимально быстро. По готовому ключу в словаре, реализованному как быстрая хэш-карта.
// dllmain.cpp : Определяет точку входа для приложения DLL.
#define _CRT_SECURE_NO_WARNINGS
#include "..\include\homm3.h"
#include "..\include\era.h"
using namespace Era;
Patcher* _P;
PatcherInstance* _PI;
// types:
struct _background_{
std::string name;
int x;
int y;
int id;
};
struct _Button_{
std::string name;
int x;
int y;
int id;
std::string caption_text;
std::string caption_font;
int caption_color;
std::string popup_text;
std::string hint_text;
};
// список параметров, которые нужно получить из json
int Qty = 5; // количество кнопок
int Align = 1;
_background_ BkgPcx = {"bkgpcx.pcx", 184, 538, 4000};
_Button_ Buttons[5] = {{"adbut1.def", 194, 538, 4001, "111", "smalfont.fnt", 2, "", ""},
{"adbut1.def", 238, 538, 4002, "222", "smalfont.fnt", 2, "", ""},
{"adbut1.def", 282, 538, 4003, "333", "smalfont.fnt", 2, "", ""},
{"adbut1.def", 326, 538, 4004, "444", "smalfont.fnt", 2, "", ""},
{"adbut1.def", 370, 538, 4005, "555", "smalfont.fnt", 2, "", ""}};
// Здесь должна быть функция загрузки параметров из json
// int __stdcall ReadJsonConfig()
// {
//
//
// return 1;
// }
_Dlg_* __stdcall hook_AdvMapDlg_Construct(HiHook* hook, _Dlg_* dlg)
{
int HDResX, HDResY, offsetx, offsety, x, y, id, color;
char *name, *caption, *font;
// HDResX = *(int*)0x4F81BC; HDResY = *(int*)0x4F81C3;
HDResX = o_WndMgr->screen_pcx16->width;
HDResY = o_WndMgr->screen_pcx16->height;
switch (Align) {
case 0: offsetx = 0; break;
case 2: offsetx = HDResX - 800; break;
case 1: default: offsetx = (HDResX - 800)/2; break;
}
offsety = HDResY - 600;
CALL_1(_Dlg_*, __thiscall, hook->GetDefaultFunc(), dlg);
x = BkgPcx.x; y = BkgPcx.y; id = BkgPcx.id; name = (char*)(BkgPcx.name.c_str());
dlg->AddItem(_DlgStaticPcx8_::Create(x + offsetx, y + offsety, id, name) );
for (int i=0; i<Qty; i++) {
x = Buttons[i].x; y = Buttons[i].y; id = Buttons[i].id; color = Buttons[i].caption_color;
name = (char*)(Buttons[i].name.c_str());
caption = (char*)(Buttons[i].caption_text.c_str());
font = (char*)(Buttons[i].caption_font.c_str());
dlg->AddItem(_DlgTextButton_::Create(x + offsetx, y + offsety, id, name, caption, font, 0, 1, 0, 0, color));
}
return dlg;
}
int __stdcall hook_BeforeDrawAgems(HiHook* hook, _AdvMgr_* pAdvManager)
{
_Dlg_* pDlgAdvMap;
_DlgItem_* pItem;
pDlgAdvMap = pAdvManager->dlg;
pItem = pDlgAdvMap->GetItem(BkgPcx.id);
pItem->Draw();
for (int i=0; i<Qty; i++) {
pItem = pDlgAdvMap->GetItem(Buttons[i].id);
pItem->Draw();
}
return CALL_1(int, __thiscall, hook->GetDefaultFunc(), pAdvManager);
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
static _bool_ plugin_On = 0;
if ( (ul_reason_for_call==DLL_PROCESS_ATTACH) && (!plugin_On) ) {
plugin_On = 1;
_P = GetPatcher();
_PI = _P->CreateInstance("new_buttons");
ConnectEra();
// ReadJsonConfig(); // раскомментировать для получения параметров из json
_PI->WriteHiHook(0x401510, SPLICE_, EXTENDED_, THISCALL_, hook_AdvMapDlg_Construct);
_PI->WriteHiHook(0x40F250, SPLICE_, EXTENDED_, THISCALL_, hook_BeforeDrawAgems);
}
return TRUE;
}
{
"namb": {
"qty": "5", // колическтво кнопок
"align": "1", // 0 - left, 1 - center, 2 - right. Выравнивание по левому краю, центру или правому краю карты.
"background": { // фоновый pcx, необязательная секция
"pcxname": "bkgpcx.pcx",
"x": "184", // Координаты нужно указывать для разрешения 800x600.
"y": "538", // Плагин передвинет их автоматически.
"id": "4000",
},
"button0": {
"defname": "adbut1.def",
"x": "194",
"y": "538",
"id": "4001",
"caption_text": "111", // опционально, необязательный параметр
"caption_font": "smalfont.fnt", // опционально, необязательный параметр
"caption_color": "2", // опционально, необязательный параметр
"popup_text": "", // опционально, необязательный параметр
"hint_text": "", // опционально, необязательный параметр
},
"button1": {
"defname": "adbut1.def",
"x": "238",
"y": "538",
"id": "4002",
"caption_text": "222",
"caption_font": "smalfont.fnt",
"caption_color": "2",
},
"button2": {
"defname": "adbut1.def",
"x": "282",
"y": "538",
"id": "4003",
"caption_text": "333",
"caption_font": "smalfont.fnt",
"caption_color": "2",
},
"button3": {
"defname": "adbut1.def",
"x": "326",
"y": "538",
"id": "4004",
"caption_text": "444",
"caption_font": "smalfont.fnt",
"caption_color": "2",
},
"button4": {
"defname": "adbut1.def",
"x": "370",
"y": "538",
"id": "4005",
"caption_text": "555",
"caption_font": "smalfont.fnt",
"caption_color": "2",
},
}
}
(28.02.2023 01:46)wessonsm Wrote: [ -> ]Я хочу сделать примерно вот такие кнопочки для карты приключений, которые нельзя сделать с помощью buttons.dll:
(28.02.2023 01:46)wessonsm Wrote: [ -> ]Ну в целом я их сделал наполовину, кнопочки видны и нажимаются, нажатия отслеживаются из erm, позиция автоматически подстраивается под разрешение HD-мода. Осталось сделать отображение хинт-текста и смену кадра курсора при наведении на кнопки (с чем я надеюсь справиться). Плюс они не перекрашиваются в цвета игрока, но это уже другая проблема.
И загрузку параметров из json, над чем бьюсь безрезультатно третий день.
// изменённая фукнция в era.h для работы с VS2008
char* tr (const char *key) {
char* buf = _tr(key, NULL, -1);
char* result = ToStaticStr(buf);
MemFree(buf);
return result;
}
// функция получения JSON строк методом ERA (в главном файле WND)
char* GetEraJSON(const char* json_string_name) {
return tr(json_string_name);
}
// код из WND
char strPage[1024];
char* textPage = "wog_options.page%d.%s";
sprintf(o_TextBuffer, textPage, 42, "key_name");
// ... или ... получаем структуру диалога ВОГ Опций
_DlgSetup_* ds = o_WogOptions;
SetupJsonText(&ds->Name, "wog_options.main.name");
// ... или ... другая строка (в цикле: i = итератор)
sprintf(strPage, textPage, i, "key_name");
SetupJsonText(&ds->Pages[i]->Name, strPage);
#include "stdafx.h"
#include "..\..\include\homm3.h"
#include "..\..\include\era.h"
using namespace Era;
Patcher* _P;
PatcherInstance* _PI;
///////////////////////////////////////////////////////////////////
class Background {
public:
int id;
int x;
int y;
std::string pcxname;
};
class Button {
public:
int id;
int x;
int y;
int caption_color;
std::string defname;
std::string caption_font;
std::string caption_text;
std::string popup_text;
std::string hint_text;
};
class Namb {
public:
int qty;
int align;
Background *background;
Button *buttons;
Namb (int qty, int align) {
this->qty = qty;
this->align = align;
this->background = new Background;
this->buttons = new Button[qty];
}
~Namb() {
delete background;
delete[] buttons;
}
};
Namb *namb = NULL;
///////////////////////////////////////////////////////////////////
char* GetJsonStr(const char* json_string_name) {
return tr(json_string_name);
}
int GetJsonInt(const char* json_string_name) {
return atoi(GetJsonStr(json_string_name));
}
char* GetButtonsParamStr(int i, const char* key) {
sprintf(o_TextBuffer, "namb.button%d.%s", i, key);
return GetJsonStr(o_TextBuffer);
}
int GetButtonsParamInt(int i, const char* key) {
sprintf(o_TextBuffer, "namb.button%d.%s", i, key);
return GetJsonInt(o_TextBuffer);
}
///////////////////////////////////////////////////////////////////
// функция загрузки параметров из json
int __stdcall ReadJsonConfig()
{
int qty = GetJsonInt("namb.qty");
int align = GetJsonInt("namb.align");
namb = new Namb(qty, align);
namb->background->pcxname = GetJsonStr("namb.background.pcxname");
namb->background->id = GetJsonInt("namb.background.id");
namb->background->x = GetJsonInt("namb.background.x");
namb->background->y = GetJsonInt("namb.background.y");
for(int i = 0; i < qty; i++) {
namb->buttons[i].id = GetButtonsParamInt(i, "id");
namb->buttons[i].x = GetButtonsParamInt(i, "x");
namb->buttons[i].y = GetButtonsParamInt(i, "y");
namb->buttons[i].caption_color = GetButtonsParamInt(i, "caption_color");
namb->buttons[i].defname = GetButtonsParamStr(i, "defname");
namb->buttons[i].caption_font = GetButtonsParamStr(i, "caption_font");
namb->buttons[i].caption_text = GetButtonsParamStr(i, "caption_text");
namb->buttons[i].popup_text = GetButtonsParamStr(i, "popup_text");
namb->buttons[i].hint_text = GetButtonsParamStr(i, "hint_text");
}
return 1;
}
///////////////////////////////////////////////////////////////////
_Dlg_* __stdcall hook_AdvMapDlg_Construct(HiHook* hook, _Dlg_* dlg)
{
int HDResX, HDResY, offsetx, offsety, x, y, id, color;
char *name, *caption, *font;
// HDResX = *(int*)0x4F81BC; HDResY = *(int*)0x4F81C3;
HDResX = o_WndMgr->screen_pcx16->width;
HDResY = o_WndMgr->screen_pcx16->height;
switch (namb->align) {
case 0:
offsetx = 0;
break;
case 2:
offsetx = HDResX - 800;
break;
case 1:
default:
offsetx = (HDResX - 800)/2;
break;
}
offsety = HDResY - 600;
CALL_1(_Dlg_*, __thiscall, hook->GetDefaultFunc(), dlg);
x = namb->background->x;
y = namb->background->y;
id = namb->background->id;
name = (char*)(namb->background->pcxname.c_str());
dlg->AddItem(_DlgStaticPcx8_::Create(x + offsetx, y + offsety, id, name) );
for (int i=0; i < namb->qty; i++) {
x = namb->buttons[i].x;
y = namb->buttons[i].y;
id = namb->buttons[i].id;
color = namb->buttons[i].caption_color;
name = (char*)(namb->buttons[i].defname.c_str());
caption = (char*)(namb->buttons[i].caption_text.c_str());
font = (char*)(namb->buttons[i].caption_font.c_str());
_DlgTextButton_* bttn = _DlgTextButton_::Create(x + offsetx, y + offsety, id, name, caption, font, 0, 1, 0, 0, color);
bttn->short_tip_text = (char*)(namb->buttons[i].caption_text.c_str());
bttn->full_tip_text = (char*)(namb->buttons[i].popup_text.c_str());
dlg->AddItem(bttn);
}
return dlg;
}
int __stdcall hook_BeforeDrawAgems(HiHook* hook, _AdvMgr_* pAdvManager)
{
_Dlg_* pDlgAdvMap;
_DlgItem_* pItem;
pDlgAdvMap = pAdvManager->dlg;
pItem = pDlgAdvMap->GetItem(namb->background->id);
pItem->Draw();
for (int i = 0; i < namb->qty; i++) {
pItem = pDlgAdvMap->GetItem(namb->buttons[i].id);
pItem->Draw();
}
return CALL_1(int, __thiscall, hook->GetDefaultFunc(), pAdvManager);
}
_LHF_(LoHook_InitTxtFiles)
{
ReadJsonConfig();
return EXEC_DEFAULT;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
static _bool_ plugin_On = 0;
if ( (ul_reason_for_call==DLL_PROCESS_ATTACH) && (!plugin_On) ) {
plugin_On = 1;
_P = GetPatcher();
_PI = _P->CreateInstance("new_buttons");
ConnectEra();
_PI->WriteLoHook(0x4EEAC0, LoHook_InitTxtFiles);
_PI->WriteHiHook(0x401510, SPLICE_, EXTENDED_, THISCALL_, hook_AdvMapDlg_Construct);
_PI->WriteHiHook(0x40F250, SPLICE_, EXTENDED_, THISCALL_, hook_BeforeDrawAgems);
}
return TRUE;
}