Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed Mar 11, 2021
1 parent 295bed2 commit bfbfcbb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 48 deletions.
73 changes: 38 additions & 35 deletions src/GDSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// for SHOpenFolderAndSelectItems
#include <Shlobj.h>

static constexpr const char* allowedPathChars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/\\.:-_,;'=%&";

static std::string workdir() {
char buff[FILENAME_MAX];
_getcwd(buff, FILENAME_MAX);
Expand Down Expand Up @@ -215,9 +218,13 @@ class ExportSettingsLayer : public FLAlertLayer {

auto lvl = std::get<0>(*tuple);

if (std::filesystem::exists(outputPath))
if (std::filesystem::is_directory(outputPath)) {
outputPath += "\\" + lvl->levelName + "." +
// making a copy cuz we don't want to add
// the filename to outputPath
std::string path = outputPath;

if (std::filesystem::exists(path))
if (std::filesystem::is_directory(path)) {
path += "\\" + lvl->levelName + "." +
gdshare::filetypes::typemap[selectedFormat];
}

Expand Down Expand Up @@ -272,13 +279,13 @@ class ExportSettingsLayer : public FLAlertLayer {
<< "</d>";
}

if (gdshare::saveFile(outputPath, data.str(), selectedFormat))
if (gdshare::saveFile(path, data.str(), selectedFormat))
FLAlertLayer::create(
new ExportSuccessDialog(outputPath.c_str()),
new ExportSuccessDialog(path.c_str()),
"Exported",
"OK", "Show file",
280.0,
"Succesfully <cl>exported</c> to <cy>" + outputPath + "</c>!"
"Succesfully <cl>exported</c> to <cy>" + path + "</c>!"
)->show();

else
Expand Down Expand Up @@ -415,7 +422,7 @@ class ExportSettingsLayer : public FLAlertLayer {

pathInput->setLabelPlaceholderColor({ 120, 180, 255 });
pathInput->setLabelPlaceholerScale(.8f);
pathInput->setAllowedChars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/\\.:-_,;'=%&");
pathInput->setAllowedChars(allowedPathChars);
pathInput->setPositionY(-40);

this->m_pButtonMenu->addChild(pathInput);
Expand Down Expand Up @@ -689,26 +696,30 @@ class EditLevelLayer : public cocos2d::CCLayer {
}
};

class MyLevelsLayer : public cocos2d::CCLayer {
static inline bool (__thiscall* init)(cocos2d::CCLayer*);
static bool __fastcall initHook(cocos2d::CCLayer* _self) {
auto res = init(_self);
class LevelBrowserLayer : public cocos2d::CCLayer {
static inline bool (__thiscall* init)(cocos2d::CCLayer*, uintptr_t);
static bool __fastcall initHook(cocos2d::CCLayer* _self, void*, uintptr_t _array_probs) {
auto res = init(_self, _array_probs);

cocos2d::CCMenu* btnMenu = getChild<cocos2d::CCMenu*>(
_self, 8
);
unsigned int screen_id = *reinterpret_cast<uintptr_t*>(_array_probs + 0xEC);

auto importButton = CCMenuItemSpriteExtraGD::create(
cocos2d::CCSprite::create("BE_Import_File.png"),
btnMenu,
(cocos2d::SEL_MenuHandler)&MyLevelsLayer::importLevel
);
if (screen_id == 0x62) {
cocos2d::CCMenu* btnMenu = getChild<cocos2d::CCMenu*>(
_self, 8
);

cocos2d::CCNode* newBtn = getChild<cocos2d::CCNode*>(btnMenu, 0);
auto importButton = CCMenuItemSpriteExtraGD::create(
cocos2d::CCSprite::create("BE_Import_File.png"),
btnMenu,
(cocos2d::SEL_MenuHandler)&LevelBrowserLayer::importLevel
);

btnMenu->addChild(importButton);
cocos2d::CCNode* newBtn = getChild<cocos2d::CCNode*>(btnMenu, 0);

importButton->setPosition(newBtn->getPositionX(), newBtn->getPositionY() + 60.0f);
btnMenu->addChild(importButton);

importButton->setPosition(newBtn->getPositionX(), newBtn->getPositionY() + 60.0f);
}

return res;
}
Expand Down Expand Up @@ -765,9 +776,7 @@ class MyLevelsLayer : public cocos2d::CCLayer {

auto scene = cocos2d::CCScene::create();

//auto layer = EditLevelLayer::create(addedLevel);

auto layer = MyLevelsLayer::create();
auto layer = EditLevelLayer::create(addedLevel);

scene->addChild(layer);

Expand All @@ -788,17 +797,11 @@ class MyLevelsLayer : public cocos2d::CCLayer {
}

public:
static MyLevelsLayer* __stdcall create() {
return reinterpret_cast<MyLevelsLayer*(__stdcall*)()>(
base + 0x1977c0
)();
}

static MH_STATUS load() {
return MH_CreateHook(
(PVOID)(base + 0x1978a0),
(LPVOID)MyLevelsLayer::initHook,
(LPVOID*)&MyLevelsLayer::init
(PVOID)(base + 0x15a040),
(LPVOID)LevelBrowserLayer::initHook,
(LPVOID*)&LevelBrowserLayer::init
);
}
};
Expand All @@ -817,7 +820,7 @@ bool GDShare::live::init() {
if ((s = EditLevelLayer::load()) != MH_OK)
return false;

if ((s = MyLevelsLayer::load()) != MH_OK)
if ((s = LevelBrowserLayer::load()) != MH_OK)
return false;

return MH_EnableHook(MH_ALL_HOOKS) == MH_OK;
Expand Down
44 changes: 31 additions & 13 deletions src/gdshare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static std::string readFileString(const std::string & _path) {
return "";
}

static bool saveFileText(std::string _path, std::string _cont) {
static bool saveFileText(const std::string & _path, const std::string & _cont) {
std::ofstream file;
file.open(_path);
if (file.is_open()) {
Expand All @@ -35,7 +35,7 @@ static bool saveFileText(std::string _path, std::string _cont) {
return false;
}

static bool saveFileBinary(std::string _path, std::vector<uint8_t> _bytes) {
static bool saveFileBinary(const std::string & _path, std::vector<uint8_t> _bytes) {
std::ofstream file;
file.open(_path, std::ios::out | std::ios::binary);
if (file.is_open()) {
Expand All @@ -52,7 +52,7 @@ static constexpr unsigned int h$(const char* str, int h = 0) {
return !str[h] ? 5381 : (h$(str, h+1) * 33) ^ str[h];
}

static std::string sanitizeString(std::string _text, bool _actually_do_it = true) {
static std::string sanitizeString(const std::string & _text, bool _actually_do_it = true) {
if (_actually_do_it) {
std::string s = _text;
std::transform(s.begin(), s.end(), s.begin(),
Expand Down Expand Up @@ -235,10 +235,12 @@ std::vector<uint8_t> gdshare::encoder::XOR(const std::vector<uint8_t> & _data, i

std::string gdshare::decodeFile(const std::string & _path) {
if (!std::filesystem::exists(_path))
return nullptr;
return "";

std::string type = std::filesystem::path(_path).extension().string();

if (!type.length())
return "";
// remove .
type = type.substr(1);

Expand All @@ -249,14 +251,27 @@ std::string gdshare::decodeFile(const std::string & _path) {
size_t metaBufSize, dataBufSize;

struct zip_t *zip = zip_open(_path.c_str(), 0, 'r');

if (zip == nullptr)
return "";

{
zip_entry_open(zip, "level.meta");
if (zip_entry_open(zip, "level.meta") != 0) {
zip_close(zip);
return "";
}

{
zip_entry_read(zip, &metaBuffer, &metaBufSize);
}

zip_entry_close(zip);

zip_entry_open(zip, "level.data");
if (zip_entry_open(zip, "level.data") != 0) {
zip_close(zip);
return "";
}

{
zip_entry_read(zip, &dataBuffer, &dataBufSize);
}
Expand All @@ -268,18 +283,18 @@ std::string gdshare::decodeFile(const std::string & _path) {
if (metaBuffer)
free(metaBuffer);

return nullptr;
return "";
}

nlohmann::json metaj;
if (metaBuffer)
metaj = nlohmann::json::parse((const char*)metaBuffer);
metaj = nlohmann::json::parse(reinterpret_cast<const char*>(metaBuffer));

free(metaBuffer);

std::string compr = metaj["compression"];

std::string data = (const char*)dataBuffer;
std::string data = reinterpret_cast<const char*>(dataBuffer);

free(dataBuffer);

Expand All @@ -288,7 +303,7 @@ std::string gdshare::decodeFile(const std::string & _path) {
data = decodeCompression(data, compr);

if (data == "")
return nullptr;
return "";

return data;
} break;
Expand All @@ -302,6 +317,9 @@ std::string gdshare::decodeFile(const std::string & _path) {
case h$(gdshare::filetypes::LvlShare): {
std::string datac = readFileString(_path);

if (!datac.length())
return "";

std::string data = gdshare::decoder::GZip(datac);

data = "<d>" + data + "</d>";
Expand All @@ -310,10 +328,10 @@ std::string gdshare::decodeFile(const std::string & _path) {
} break;

default:
return nullptr;
return "";
}

return nullptr;
return "";
}

DS_Dictionary* gdshare::parseFile(const std::string & _path) {
Expand Down

0 comments on commit bfbfcbb

Please sign in to comment.