Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/youneuoy/M2TWEOP-library
Browse files Browse the repository at this point in the history
…into fix-discord-bugs
  • Loading branch information
EddieEldridge committed Oct 29, 2023
2 parents e7a35f4 + e744ef4 commit ccb174c
Show file tree
Hide file tree
Showing 80 changed files with 12,575 additions and 4,739 deletions.
4 changes: 2 additions & 2 deletions M2TWEOP Code/M2TWEOP GUI/M2TWEOP GUI.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>SFML_STATIC;WIN32;WIN32_LEAN_AND_MEAN;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SFML_STATIC;WIN32;WIN32_LEAN_AND_MEAN;_SILENCE_ALL_CXX23_DEPRECATION_WARNINGS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<AdditionalIncludeDirectories>$(SolutionDir)\3rd\sfml\include;..\3rd\glfw\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
Expand All @@ -119,7 +119,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>SFML_STATIC;WIN32;WIN32_LEAN_AND_MEAN;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>SFML_STATIC;WIN32;WIN32_LEAN_AND_MEAN;NDEBUG;_SILENCE_ALL_CXX23_DEPRECATION_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpplatest</LanguageStandard>
<AdditionalIncludeDirectories>$(SolutionDir)\3rd\sfml\include;..\3rd\glfw\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
Expand Down
119 changes: 113 additions & 6 deletions M2TWEOP Code/M2TWEOP GUI/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,18 +356,125 @@ bool helpers::compareFiles(string& oneFile, string& nextFile)
return true;
}

void helpers::getCurrentPath(string& path)
void helpers::setModFolder(string& modFolder)
{
TCHAR currentPath[MAX_PATH] = { 0 };
DWORD ret = GetCurrentDirectoryA(MAX_PATH, currentPath);
path = currentPath;
modFolder = currentPath;
size_t pos = modFolder.find_last_of("\\/")+1;
modFolder = modFolder.substr(pos);
}

void helpers::setModFolder(string& modFolder)
void helpers::getCurrentPath(string& path)
{
TCHAR currentPath[MAX_PATH] = { 0 };
DWORD ret = GetCurrentDirectoryA(MAX_PATH, currentPath);
modFolder = currentPath;
size_t pos = modFolder.find_last_of("\\/")+1;
modFolder = modFolder.substr(pos);
path = currentPath;
}

vector<std::string> helpers::getCfgFilesInFolder()
{
string path;
getCurrentPath(path);
path += "\\*.cfg";

WIN32_FIND_DATAA findFileData;
HANDLE hFind = FindFirstFileA(path.c_str(), &findFileData);

std::vector<std::string> cfgFiles;

if (hFind != INVALID_HANDLE_VALUE)
{
do {
const std::string name = findFileData.cFileName;

if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
string currentPath;
getCurrentPath(currentPath);
if (checkCfgFileForMod(currentPath + "\\" + name) != "")
{
cfgFiles.push_back(name);
}
}

} while (FindNextFileA(hFind, &findFileData) != 0);

FindClose(hFind);
}

return cfgFiles;
}

std::string helpers::checkCfgFileForMod(const std::string& filePath)
{
std::ifstream file(filePath);
if (!file.is_open())
return "";

std::string line;
while (std::getline(file, line))
{
line.erase(std::remove_if(line.begin(), line.end(), ::isspace), line.end());

if (line.find("mod=") == 0)
return extractModPathFromLine(line);
}

return "";
}

std::vector<std::string> helpers::split(const std::string& s, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}

bool helpers::verifyModPath(const std::string& modPath)
{
string currentPath;
getCurrentPath(currentPath);
auto modPathDirs = split(modPath, '\\');
auto currentPathDirs = split(currentPath, '\\');

if (currentPathDirs.size() < 2)
return false;

std::string modPathCheck = modPath + "";

size_t pos = modPathCheck.find("#");
if (pos != std::string::npos)
{
modPathCheck = modPathCheck.substr(0, pos);
}
pos = modPathCheck.find(";");
if (pos != std::string::npos)
{
modPathCheck = modPathCheck.substr(0, pos);
}

std::string lastDir = currentPathDirs[currentPathDirs.size() - 1];
std::string secondLastDir = currentPathDirs[currentPathDirs.size() - 2];
std::string expectedPath = secondLastDir + "\\" + lastDir;
std::replace(expectedPath.begin(), expectedPath.end(), '/', '\\');
std::replace(modPathCheck.begin(), modPathCheck.end(), '/', '\\');
std::transform(expectedPath.begin(), expectedPath.end(), expectedPath.begin(), ::tolower);
std::transform(modPathCheck.begin(), modPathCheck.end(), modPathCheck.begin(), ::tolower);

return modPathCheck == expectedPath;
}

std::string helpers::extractModPathFromLine(const std::string& line)
{
size_t pos = line.find("mod=");
if (pos == std::string::npos)
return "";

return line.substr(pos + 4);
}
10 changes: 10 additions & 0 deletions M2TWEOP Code/M2TWEOP GUI/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ class helpers

static void closeGame(const string& exeName);

static vector<std::string> getCfgFilesInFolder();

static std::string checkCfgFileForMod(const std::string& filePath);

static std::vector<std::string> split(const std::string& s, char delimiter);

static bool verifyModPath(const std::string& modPath);

static std::string extractModPathFromLine(const std::string& line);

static void updateMetrics();

static screenS& getScreen();
Expand Down
13 changes: 13 additions & 0 deletions M2TWEOP Code/M2TWEOP GUI/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine
}
}
}


std::vector<std::string> cfgFiles = helpers::getCfgFilesInFolder();
std::vector<const char*> items;
for (const auto& file : cfgFiles) {
items.push_back(file.c_str());
}
if (dataG::data.modData.configName == "" && items.size() > 0)
{
dataG::data.modData.configName = items[0];
dataG::data.modData.useVanillaConfig = false;
managerG::saveJsonSettings();
}
// Main loop
runApp(toolRoutine::drawTick);

Expand Down
58 changes: 53 additions & 5 deletions M2TWEOP Code/M2TWEOP GUI/modSettingsUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ namespace modSettingsUI
ImVec2 selectablesSize{};
}settingsUIData;





void initSettingsUI()
{
settingsUIData.settingsPages.clear();


//main page
{
settingsPage generalPage;
Expand Down Expand Up @@ -92,15 +90,56 @@ namespace modSettingsUI

void drawGeneralSettings()
{

//ImGui::InputText("Config file name", &dataG::data.modData.configName);

std::vector<std::string> cfgFiles = helpers::getCfgFilesInFolder();
std::vector<const char*> items;
for (const auto& file : cfgFiles) {
items.push_back(file.c_str());
}
string path;
helpers::getCurrentPath(path);
static int selectedItem = -1;
if (items.size() == 0)
{
dataG::data.modData.useVanillaConfig = true;
}
else
{
if (dataG::data.modData.configName == "")
{
dataG::data.modData.configName = items[0];
dataG::data.modData.useVanillaConfig = false;
selectedItem = 0;
}
else
{
for (const auto& file : items) {
if (file == dataG::data.modData.configName)
{
selectedItem = std::distance(items.begin(), std::find(items.begin(), items.end(), file));
dataG::data.modData.configName = items[selectedItem];
dataG::data.modData.useVanillaConfig = false;
break;
}
}
}
}
if (dataG::data.modData.useVanillaConfig == true)
{
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
ImGui::PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * 0.5f);
}

ImVec2 textSize = ImGui::CalcTextSize("Readme Teutonic");
ImGui::PushItemWidth(textSize.x);
ImGui::InputText("Config file name", &dataG::data.modData.configName);
ImGui::Combo("Config Files", &selectedItem, &items[0], items.size());

if (selectedItem != -1)
{
dataG::data.modData.configName = items[selectedItem];
dataG::data.modData.useVanillaConfig = false;
}

if (dataG::data.modData.useVanillaConfig == true)
{
Expand All @@ -112,6 +151,15 @@ namespace modSettingsUI

ImGui::Checkbox("Use standard config", &dataG::data.modData.useVanillaConfig);
ImGui::NewLine();

if (!helpers::verifyModPath(helpers::checkCfgFileForMod(path + "\\" + dataG::data.modData.configName)))
{
// Warn the user
if (dataG::data.modData.useVanillaConfig == false)
{
ImGui::Text("Warning: Mod path in %s does not match the current directory.", dataG::data.modData.configName.c_str());
}
}
ImGui::Checkbox("Use M2TWEOP", &dataG::data.modData.useM2TWEOP);

ImGui::Checkbox("Hide launcher on startup", &dataG::data.modData.hideLauncherAtStart);
Expand Down
Loading

0 comments on commit ccb174c

Please sign in to comment.