Skip to content

Commit

Permalink
Merge branch 'objects-as-json'
Browse files Browse the repository at this point in the history
  • Loading branch information
ivucica committed Sep 15, 2023
2 parents 8d64e06 + d154a4b commit 192a15e
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ SDL_gfx-2.0.24/
*.o
yatc
yatc_cli
dat2json_cli
Tibia.pic
Tibia.dat
Tibia.spr
Expand Down
121 changes: 121 additions & 0 deletions cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,124 @@ filegroup(
"//net:protocolgame854.cpp",
],
)

cc_library(
name = "dat2json_cli_lib",
defines = ["CLI_ONLY=1", "HAVE_TOMMATH=1", "BAZEL_BUILD=1"] + select({
"//conditions:default": [],
"//:windows": ["WIN32=1",],
"//:windows_msys": ["WIN32=1",],
"//:windows_msvc": ["WIN32=1",],
}),
srcs = [
"creatureui.cpp", "//:creatureui.h",
"thingui.cpp", "//:thingui.h",
"effectui.cpp", "//:effectui.h",
#"objects.cpp",
"//:objects.h",
"distanceui.cpp", "//:distanceui.h",
"itemui.cpp", "//:itemui.h",
"notifications.cpp", "//:notifications.h",

"//gamecontent:inventory.cpp",
"//gamecontent:globalvars.cpp",
"//gamecontent:creature.cpp",
"//gamecontent:map.cpp",
"//gamecontent:container.cpp",

"//:stdinttypes.h",
"//:sprite.h",
"//:defines.h",

"//gamecontent:creature.h",
"//gamecontent:thing.h",
"//gamecontent:enums.h",
"//gamecontent:position.h",
"//gamecontent:globalvars.h",
"//gamecontent:container.h",
"//gamecontent:itemcontainer.h",
"//gamecontent:shop.h",
"//gamecontent:inventory.h",
"//gamecontent:map.h",
"//gamecontent:item.h",
"//gamecontent:viplist.h",

"//net:encryption.cpp",
"//net:networkmessage.cpp",
"//net:connection.cpp",

"//net:connection.h",
"//net:networkmessage.h",
"//net:encryption.h",
"//net:protocolconfig.h",
"//net:protocolconfig.cpp",
"//net:protocolgame.h",
"//net:protocollogin.h",
"//net:protocollogin.cpp",
"//net:rsa.h",
"//net:rsa.cpp",

"//:util.cpp",
"//:util.h",
"//:product.h",
"//:macutil.h",

"//gamecontent:item.cpp",
"//gamecontent:viplist.cpp",
"//:objects.cpp",
"//:options.cpp", "//:options.h", "//:confighandler.h", "//:confighandler.cpp",
"//:bigint.h",
"//:bigint.cpp",

":protocolgamehdrs",
":protocolgamesrcs", # why?
"dat2json_cli.cpp",
],
deps = [
"//:debugprint",
"//:fassert",

"//net:enum_hdr",
"@tommath//:tommath",
#"//:yatc_lib", # We could pull in far less stuff. See Makefile.
] + select({
"//:darwin": [
"@libsdl12//:sdlmain",
"//:macclipboard",
"//:macutil",
"@libsdl12//:sdl", # util.h is pulling in SDL/SDL_endian.h
],
"//conditions:default": [
"@libsdl12//:sdl", # util.h is pulling in SDL/SDL_endian.h
"@libsdl12//:sdlmain", # and because of that we pull in SDL_main too
],
}) + select({
":login_only": [],
"//conditions:default": [
"//net:protocols", # FIXME: This is pulling in REALLY a lot, incl chunks/all of glict. And yet it doesn't build.
],
}),
data = [
"@tibia854//:Tibia.dat",
"@tibia854//:Tibia.spr",
"@tibia854//:Tibia.pic",
],
copts = select({
"//conditions:default": ["-isystem external/tommath"],
"//:windows": ["-I external/tommath"],
"//:windows_msvc": ["-I external/tommath"],
}),
linkopts = select({
"//conditions:default": [],
"//:windows": ["-DEFAULTLIB:ws2_32", "-DEFAULTLIB:shell32"],
"//:windows_msvc": ["-DEFAULTLIB:ws2_32", "-DEFAULTLIB:shell32"],
}),
)

cc_binary(
name = "dat2json_cli",
deps = [
":dat2json_cli_lib",
],
)

13 changes: 8 additions & 5 deletions cli/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
CXXFLAGS=-DCLI_ONLY -DLOGIN_ONLY -DHAVE_GMP_H=1
CXXFLAGS=-DCLI_ONLY -DLOGIN_ONLY -DHAVE_GMP_H=1 -fPIE
CFLAGS=-DCLI_ONLY -DLOGIN_ONLY -DHAVE_GMP_H=1 -fPIE
OBJS=yatc_cli.o ../net/connection.o ../net/networkmessage.o ../debugprint.o ../util.o notifications.o ../gamecontent/creature.o ../gamecontent/container.o ../gamecontent/globalvars.o ../gamecontent/inventory.o ../gamecontent/map.o creatureui.o thingui.o distanceui.o effectui.o objects.o ../net/protocolconfig.o ../net/rsa.o ../net/encryption.o ../net/protocollogin.o
yatc_cli: $(OBJS)
g++ $(OBJS) -lSDL -lgmp -fPIE -o yatc_cli
OBJS=../net/connection.o ../net/networkmessage.o ../debugprint.o ../util.o notifications.o ../gamecontent/creature.o ../gamecontent/container.o ../gamecontent/globalvars.o ../gamecontent/inventory.o ../gamecontent/map.o creatureui.o thingui.o distanceui.o effectui.o ../net/protocolconfig.o ../net/rsa.o ../net/encryption.o ../net/protocollogin.o
all: yatc_cli dat2json_cli
yatc_cli: yatc_cli.o $(OBJS) objects.o
g++ yatc_cli.o $(OBJS) objects.o -lSDL -lgmp -fPIE -o yatc_cli
dat2json_cli: $(OBJS) dat2json_cli.o ../objects.o ../options.o ../confighandler.o
g++ dat2json_cli.o ../objects.o ../options.o ../confighandler.o $(OBJS) -lSDL -lgmp -o dat2json_cli
clean:
rm $(OBJS) yatc_cli
rm $(OBJS) yatc_cli yatc_cli.o dat2json_cli dat2json_cli.o ../objects.o objects.o ../options.o ../confighandler.o
20 changes: 20 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,23 @@ not been eliminated.

The client is just for experimentation, and thus no care was taken to make it
autoconf'igurable.

It can be build with either `make` or:

```
bazel build --define LOGIN_ONLY=1 :yatc_cli
```

`LOGIN_ONLY` is required at this time. Gameworld connections are not supported
at this time.

## dat2json

This is also the directory where, at this time, it made the most sense to place
dat2json. It can be built with either `make` or:

```
bazel build --define LOGIN_ONLY=1 //cli:dat2json_cli
```

`LOGIN_ONLY` is required at this time.
59 changes: 59 additions & 0 deletions cli/dat2json_cli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//////////////////////////////////////////////////////////////////////
// Yet Another Tibia Client
//////////////////////////////////////////////////////////////////////
// CLI version.
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////

#include <string>
#include <iostream>
#include <fstream>
#include "../util.h"
#include "../objects.h"
#include "../options.h"
#include "../net/connection.h"

#if BAZEL_BUILD && WIN32
#include <SDL/SDL.h> // Give SDL a chance to rename main() for sake of SDL_main.
#endif

std::string g_recordfilename="debugrecord.rec";

unsigned int g_frameTime = 0;
unsigned int g_frameDiff = 0;
Connection *g_connection = NULL;

int main(int argc, char** argv) {
yatc_fopen_init(argv[0]);
ClientVersion_t proto = CLIENT_VERSION_854;
auto i = Objects::getInstance();
if (argc != 3) {
fprintf(stderr, "usage: %s datfile jsonfile\n", argv[0]);
return 1;
}
std::cout << "Warning: In case of crash after this line check you put Tibia.{dat,spr,pic} in CWD." << std::endl << std::flush;
ProtocolConfig::getInstance().setServerType(SERVER_OTSERV);
ProtocolConfig::getInstance().setVersion(CLIENT_OS_WIN, proto);
ProtocolConfig::getInstance().setVersionOverride(CLIENT_VERSION_AUTO);
i->loadDat(argv[1]);

std::ofstream outFile;
outFile.open(argv[2]);
i->asJSON(outFile);
outFile.close();

return 0;
}
55 changes: 44 additions & 11 deletions objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@

#include <iostream>
#include "objects.h"
#ifndef CLI_ONLY
#include "engine.h" // used to create engine specific sprites
#else
#include <map>
#include "fassert.h"
#endif
#include "util.h"
#include "options.h"
#include "net/protocolconfig.h"
#include "product.h"

#include <fstream>

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Expand Down Expand Up @@ -110,7 +117,9 @@ void ObjectType::loadGfx()

if (m_gfx.size() != numsprites) { // graphics not loaded yet?
for(uint32_t i = 0; i < numsprites; i++){
#ifndef CLI_ONLY
m_gfx.insert(m_gfx.end(), g_engine->createSprite("Tibia.spr", imageData[i]));
#endif
}
}

Expand Down Expand Up @@ -1006,21 +1015,45 @@ ObjectType* Objects::getDistanceType(uint16_t id)

void Objects::asJSON(std::ostream &o) {
o << "{" << std::endl;
o << "\t'items': [" << std::endl;
o << "\t\"items\": [" << std::endl;
bool first = true;
//for (std::vector<ObjectType*>::iterator it = m_item.begin(); it != m_item.end(); it++) {
for (int i = 0; i < m_item.size(); i++) {
for (int i = 100; i < m_item.size(); i++) {
ObjectType *oType = m_item.getElement(i);
if (!oType) {
o << "\t\t{ '_item_is_null': '" << i << "' }," << std::endl;
continue;
if (oType == NULL) continue;

if (first)
o << "\t\t{" << std::endl;
else
o << "," << std::endl << "\t\t{" << std::endl;
first = false;

o << "\t\t\t\"id\": " << oType->id << "," << std::endl;
o << "\t\t\t\"width\": " << oType->width << "," << std::endl;
o << "\t\t\t\"height\": " << oType->height << "," << std::endl;
if((oType->width > 1) || (oType->height > 1)){
o << "\t\t\t\"renderSize\" : " << oType->rendersize << "," << std::endl;
}
o << "\t\t{\n" << std::endl;
o << "\t\t\t'id': " << oType->id << "," << std::endl;
o << "\t\t\t'width': " << oType->width << "," << std::endl;
o << "\t\t\t'height': " << oType->height << "" << std::endl;
o << "\t\t},\n" << std::endl;

o << "\t\t\t\"blendFrames\": " << oType->blendframes << "," << std::endl;
o << "\t\t\t\"xDiv\": " << oType->xdiv << "," << std::endl;
o << "\t\t\t\"yDiv\": " << oType->ydiv << "," << std::endl;
o << "\t\t\t\"zDiv\": " << oType->zdiv << "," << std::endl;
o << "\t\t\t\"animCount\": " << oType->animcount << "," << std::endl;
o << "\t\t\t\"numSprites\": " << oType->numsprites << "," << std::endl;
o << "\t\t\t\"sprites\": [" << std::endl;
for(unsigned int i = 0; i < oType->numsprites; i++) {
o << "\t\t\t\t" << oType->imageData[i];
if (i < oType->numsprites - 1)
o << ",";
o << std::endl;
}
o << "\t\t\t]" << std::endl;

//o << "\t\t\t, \"_comment\": \"" << i << " out of " << m_item.size() << "\"" << std::endl;
o << "\t\t}";
}
o << "\t\t{}\n" << std::endl; // so we don't have to remove the comma
o << std::endl;
o << "\t]" << std::endl;
o << "}" << std::endl;
}
4 changes: 3 additions & 1 deletion objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ class Objects
Array<ObjectType*> m_effect;
Array<ObjectType*> m_distance;

void asJSON(std::ostream &o) ;
public:
// Public for use in dat2json_cli
void asJSON(std::ostream &o);
};

#endif

0 comments on commit 192a15e

Please sign in to comment.