Skip to content

Commit

Permalink
SporeModManager: add update-modapi command
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Jul 12, 2024
1 parent 0edb589 commit 7ec6899
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 2 deletions.
8 changes: 6 additions & 2 deletions SporeModManager/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MINGW_CXX ?= i686-w64-mingw32-g++
MINGW_WINDRES ?= i686-w64-mingw32-windres

ifeq ($(MINGW), 0)
LIBLDFLAGS := $(shell pkg-config --libs libcurl)
LDFLAGS :=
OPTFLAGS := -Os -flto -fno-exceptions
WARNFLAGS := -Wall -Wextra -Wpedantic
Expand All @@ -24,6 +25,7 @@ EXE_EXT :=
else
# mingw doesn't like LTO
# and we need to statically link
LIBLDFLAGS :=
LDFLAGS := -municode -static
OPTFLAGS := -Os -fno-exceptions
WARNFLAGS := -Wall -Wextra -Wpedantic
Expand All @@ -47,6 +49,7 @@ CXXFLAGS := -std=c++17 \
-I$(THIRDPARTY_DIR)/zlib/contrib/minizip

OBJECT_FILES := \
$(SOURCE_DIR)/SporeModManagerHelpers/Download.$(OBJ) \
$(SOURCE_DIR)/SporeModManagerHelpers/FileVersion.$(OBJ) \
$(SOURCE_DIR)/SporeModManagerHelpers/Path.$(OBJ) \
$(SOURCE_DIR)/SporeModManagerHelpers/SporeMod.$(OBJ) \
Expand All @@ -64,6 +67,7 @@ endif

HEADER_FILES := \
$(SOURCE_DIR)/SporeModManager.hpp \
$(SOURCE_DIR)/SporeModManagerHelpers/Download.hpp \
$(SOURCE_DIR)/SporeModManagerHelpers/FileVersion.hpp \
$(SOURCE_DIR)/SporeModManagerHelpers/SporeModXml.hpp \
$(SOURCE_DIR)/SporeModManagerHelpers/SporeMod.hpp \
Expand Down Expand Up @@ -121,12 +125,12 @@ all: $(BINARY_DIR)/$(EXE_FILE)$(EXE_EXT)
$(BINARY_DIR)/$(EXE_FILE): $(THIRDPARTY_OBJECT_FILES) $(OBJECT_FILES)
@echo "LD $@"
$(QUIET)mkdir -p $(BINARY_DIR)
$(QUIET)$(CXX) $(OBJECT_FILES) $(THIRDPARTY_OBJECT_FILES) -o $@ $(LDFLAGS)
$(QUIET)$(CXX) $(OBJECT_FILES) $(THIRDPARTY_OBJECT_FILES) -o $@ $(LDFLAGS) $(LIBLDFLAGS)

$(BINARY_DIR)/$(EXE_FILE).exe: $(THIRDPARTY_OBJECT_FILES) $(OBJECT_FILES)
@echo "MINGW_LD $@"
$(QUIET)mkdir -p $(BINARY_DIR)
$(QUIET)$(MINGW_CXX) $(OBJECT_FILES) $(THIRDPARTY_OBJECT_FILES) -o $@ $(LDFLAGS)
$(QUIET)$(MINGW_CXX) $(OBJECT_FILES) $(THIRDPARTY_OBJECT_FILES) -o $@ $(LDFLAGS) $(LIBLDFLAGS)

clean:
rm -f $(BINARY_DIR)/$(EXE_FILE)$(EXE_EXT) $(OBJECT_FILES) $(THIRDPARTY_OBJECT_FILES)
Expand Down
39 changes: 39 additions & 0 deletions SporeModManager/SporeModManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include <algorithm>

#include "SporeModManagerHelpers/Download.hpp"
#include "SporeModManagerHelpers/SporeMod.hpp"
#include "SporeModManagerHelpers/String.hpp"
#include "SporeModManagerHelpers/Path.hpp"
Expand Down Expand Up @@ -479,3 +480,41 @@ bool SporeModManager::UninstallMods(std::vector<int> ids)

return true;
}

bool SporeModManager::DownloadSporeModAPI(void)
{
const std::string url = "https://github.com/emd4600/Spore-ModAPI/releases/latest/download/SporeModAPIdlls.zip";
const std::filesystem::path updatePath = Path::Combine({ Path::GetCoreLibsPath(), "update.zip" });
const std::filesystem::path coreLibPath = Path::Combine({ Path::GetCoreLibsPath(), "SporeModAPI.dll" });
std::error_code error;
Zip::ZipFile zipFile;

if (!Download::DownloadFile(url, updatePath))
{
return false;
}

if (!Zip::OpenFile(zipFile, updatePath))
{
return false;
}

std::cout << "-> Extracting SporeModAPI.dll" << std::endl;

if (!Zip::ExtractFile(zipFile, "SporeModAPI.combined.dll", coreLibPath))
{
Zip::CloseFile(zipFile);
return false;
}

Zip::CloseFile(zipFile);

std::filesystem::remove(updatePath, error);
if (error)
{
std::cerr << "Error: failed to remove " << updatePath << ": " << error.message() << std::endl;
return false;
}

return true;
}
5 changes: 5 additions & 0 deletions SporeModManager/SporeModManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ namespace SporeModManager
/// Uninstalls mods with ids
/// </summary>
bool UninstallMods(std::vector<int> ids);

/// <summary>
/// Updates Spore-ModAPI DLLs
/// </summary>
bool DownloadSporeModAPI(void);
}

#endif // SPOREMODMANAGER_HPP
89 changes: 89 additions & 0 deletions SporeModManager/SporeModManagerHelpers/Download.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* SporeModLoader - https://github.com/Rosalie241/SporeModLoader
* Copyright (C) 2022 Rosalie Wanders <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "Download.hpp"
#include "UI.hpp"

#include <iostream>
#include <fstream>

#ifdef _WIN32
// TODO: windows support
#else
#include <curl/curl.h>
#endif // _WIN32

using namespace SporeModManagerHelpers;

//
// Local Variables
//


//
// Local Functions
//

static size_t curl_write_data(char *data, size_t size, size_t nmemb, void* stream)
{
std::ofstream* fileStream = (std::ofstream*)(stream);
std::streamoff position = fileStream->tellp();
fileStream->write(data, size * nmemb);
return fileStream->tellp() - position;
}


//
// Exported Functions
//

bool Download::DownloadFile(std::string url, std::filesystem::path path)
{
std::cout << "-> Downloading " << path.filename() << std::endl;

if (UI::GetVerboseMode())
{
std::cout << "--> Downloading " << url << std::endl;
}

#ifdef _WIN32
// TODO: windows support
#else
CURL* curl = curl_easy_init();
if (curl == nullptr)
{
std::cerr << "Error: failed to initialize cURL!" << std::endl;;
return false;
}

std::ofstream fileStream(path, std::ios::binary);
if (!fileStream.is_open())
{
curl_easy_cleanup(curl);
std::cerr << "Error: failed to open " << path << std::endl;
return false;
}

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&fileStream);
curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS_STR, "https");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

if (curl_easy_perform(curl) != CURLE_OK)
{
curl_easy_cleanup(curl);
std::cerr << "Error: failed to download file!" << std::endl;
return false;
}

curl_easy_cleanup(curl);
return true;
#endif // _WIN32
}
27 changes: 27 additions & 0 deletions SporeModManager/SporeModManagerHelpers/Download.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SporeModLoader - https://github.com/Rosalie241/SporeModLoader
* Copyright (C) 2022 Rosalie Wanders <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SPOREMODMANAGERHELPERS_DOWNLOAD_HPP
#define SPOREMODMANAGERHELPERS_DOWNLOAD_HPP

#include <string>
#include <filesystem>

namespace SporeModManagerHelpers
{
namespace Download
{
/// <summary>
/// Downloads url to path
/// </summary>
bool DownloadFile(std::string url, std::filesystem::path path);
}
}

#endif // SPOREMODMANAGERHELPERS_DOWNLOAD_HPP
13 changes: 13 additions & 0 deletions SporeModManager/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static void show_usage()
<< " install file(s) installs file(s)" << std::endl
<< " update file(s) updates mod(s) using file(s)" << std::endl
<< " uninstall id(s) uninstalls mod with id(s)" << std::endl
<< " update-modapi downloads latest spore modapi dll" << std::endl
<< std::endl
<< " version display version and exit" << std::endl
<< " help display this help and exit" << std::endl
Expand Down Expand Up @@ -390,6 +391,18 @@ int main(int argc, char** argv)
return 1;
}
}
else if (command == arg_str("update-modapi"))
{
if (!Path::CheckIfPathsExist())
{
return 1;
}

if (!SporeModManager::DownloadSporeModAPI())
{
return 1;
}
}
else
{
show_usage();
Expand Down

0 comments on commit 7ec6899

Please sign in to comment.