Skip to content

Commit

Permalink
feat: Workshop Map Support
Browse files Browse the repository at this point in the history
  • Loading branch information
andsfonseca committed May 19, 2022
1 parent 43fe801 commit 824f83d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lang/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@
"npc_dota_hero_zuus_2": "Zeus"
},
"CUSTOM_MAP":{
"hero_demo":"Testing a hero"
"hero_demo":"Testing a hero",
"WORKSHOP": "Playing {{MAPNAME}}"
},
"NEUTRALS": [
"npc_dota_neutral_alpha_wolf",
Expand Down
3 changes: 2 additions & 1 deletion lang/pt-br.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@
"npc_dota_hero_zuus_2": "Zeus"
},
"CUSTOM_MAP":{
"hero_demo":"Testando um herói"
"hero_demo":"Testando um herói",
"WORKSHOP": "Jogando {{MAPNAME}}"
},
"NEUTRALS": [
"npc_dota_neutral_alpha_wolf",
Expand Down
61 changes: 58 additions & 3 deletions src/services/DotaService.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <chrono>
#include <map>
#include <regex>

#include "DiscordService.cpp"
// #include "../utils/StringExtensions.cpp"
Expand Down Expand Up @@ -35,7 +37,7 @@ class DotaService
static DotaService *instance;

int64_t currentMatchTime = 0;

std::map<std::string, std::string> WorkshopMapsCache;
DotaService()
{
}
Expand Down Expand Up @@ -431,6 +433,42 @@ class DotaService
return data["player"]["gold"].asInt();
}

std::string GetWorkshopMapName(std::string path)
{
if (WorkshopMapsCache.count(path))
{
return WorkshopMapsCache.find(path)->second;
}

const size_t last_slash_idx = path.rfind('/');

if (std::string::npos == last_slash_idx)
return "";

std::string directory = path.substr(0, last_slash_idx + 1);
std::string file;

if (Templates::LoadFile(directory + "publish_data.txt", file))
{
std::regex title_regex("\"(title)\"\\s+\"((\\\"|[^\"])*)\"");

std::istringstream f(file);
std::string line;

while (std::getline(f, line))
{
std::smatch sm;
std::regex_search(line, sm, title_regex);
if (sm[2] != ""){
WorkshopMapsCache.insert({path, sm[2]});
return sm[2];
}
}
}

return "";
}

std::string GetMapName(Json::Value data)
{
if (data["map"].isNull())
Expand All @@ -448,11 +486,28 @@ class DotaService
if (mappath == "")
return "";

// Try find in custom map string
Extensions::FindAndReplaceAll(mappath, "\\", "/");

std::string mapName(mappath.substr(mappath.rfind("/") + 1));

return LocalizedStrings::Get("DOTA_2:CUSTOM_MAP:hero_demo");
std::string customMapName = LocalizedStrings::Get("DOTA_2:CUSTOM_MAP:" + mapName);

if (customMapName != "")
{
return customMapName;
}

//Try check in Workshop files
mapName = GetWorkshopMapName(mappath);

if(mapName == ""){
return "";
}

customMapName = LocalizedStrings::Get("DOTA_2:CUSTOM_MAP:WORKSHOP");

Extensions::FindAndReplaceAll(customMapName, "{{MAPNAME}}", mapName);
return customMapName;
}

void FixGameTimeIfNecessary(int64_t &matchTime)
Expand Down
2 changes: 1 addition & 1 deletion src/templates/Templates.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <string>
#include <filesystem>
#include <fstream>

#include <sstream>
#include "../extensions/StringExtensions.cpp"

#if defined(_WIN32) || defined(_WIN64)
Expand Down

0 comments on commit 824f83d

Please sign in to comment.