Skip to content

Commit

Permalink
make a first time setup
Browse files Browse the repository at this point in the history
  • Loading branch information
noisecode3 committed Dec 23, 2023
1 parent 870b9fc commit 7fcad90
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 277 deletions.
25 changes: 13 additions & 12 deletions TombRaiderPWModder/files.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
#pragma once
#include <string>
#include <QString>
#include <vector>
#include <algorithm>
class file
{
public:
file(const std::string path, const std::string md5sum)
: path(path), md5sum(md5sum) {}
const std::string& getPath() const { return path; }
const std::string& getMd5sum() const { return md5sum; }
file(const QString md5sum, const QString path)
: md5sum(md5sum), path(path) {}
const QString& getPath() const { return path; }
const QString& getMd5sum() const { return md5sum; }
private:
const std::string path; //path + name
const std::string md5sum;
const QString md5sum;
const QString path; //path + name
};

class gameFileList
{
public:
gameFileList(const std::string name) : name(name) {}
const std::string getMd5sum(std::string& path)
gameFileList(const QString name) : name(name) {}
const QString getMd5sum(QString& path)
{
auto it = std::find_if(list.begin(), list.end(),
[&path](const file& obj) { return obj.getPath() == path; });
if (it != list.end())
return it->getMd5sum();
return "";
}
size_t getIndex(std::string path)
size_t getIndex(QString path)
{
auto it = std::find_if(list.begin(), list.end(),
[&path](const file& obj) { return obj.getPath() == path; });
Expand All @@ -38,7 +39,7 @@ class gameFileList
{
return list.size();
}
void addFile(const std::string path, const std::string md5sum)
void addFile(const QString path, const QString md5sum)
{
list.push_back(file(path,md5sum));
}
Expand All @@ -47,6 +48,6 @@ class gameFileList
return list.at(index);
}
private:
const std::string name;
const QString name;
std::vector<file> list;
};
2 changes: 1 addition & 1 deletion TombRaiderPWModder/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main(int argc, char *argv[])
QApplication::setApplicationName("TombRaiderLinuxLauncher");

// Construct the QSettings object
TombRaiderPWModder w;
TombRaiderLinuxLauncher w;
w.show();
return a.exec();
}
177 changes: 2 additions & 175 deletions TombRaiderPWModder/networkTR3.cpp
Original file line number Diff line number Diff line change
@@ -1,178 +1,4 @@
#include <iostream>
#include <iomanip>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <filesystem>
#include <cstring>
#include <openssl/md5.h>
#include <openssl/evp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sstream>
#include <unistd.h>
#include <curl/curl.h> //depend
#include <zip.h> //depend
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curl/curl.h>

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}

size_t downloadFile(const char* url, const char* filename)
{
CURL *curl_handle;
static const char *pagefilename = filename;
FILE *pagefile;
if (filename[0] == '/')
{
curl_global_init(CURL_GLOBAL_ALL);

/* init the curl session */
curl_handle = curl_easy_init();

/* set URL to get here */
curl_easy_setopt(curl_handle, CURLOPT_URL, url);

/* Switch on full protocol/debug output while testing */
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);

/* disable progress meter, set to 0L to enable it */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

/* open the file */
pagefile = fopen(pagefilename, "wb");
if(pagefile) {

/* write the page body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

/* get it! */
curl_easy_perform(curl_handle);

/* close the header file */
fclose(pagefile);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
}
return 0;
}

// Function to calculate MD5 checksum of a file
std::string calculateMD5(const char* filename)
{
FILE* file = fopen(filename, "rb");
if (!file)
{
std::cerr << "Error opening file for reading." << std::endl;
return "";
}
fseek(file, 0, SEEK_END);
rewind(file);

unsigned char md5sum[EVP_MAX_MD_SIZE];
EVP_MD_CTX* md5Context;
const EVP_MD* md5Type = EVP_md5();

md5Context = EVP_MD_CTX_new();
EVP_DigestInit_ex(md5Context, md5Type, NULL);

char buffer[1024];
size_t bytesRead;

while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) > 0)
{
EVP_DigestUpdate(md5Context, buffer, bytesRead);
}

EVP_DigestFinal_ex(md5Context, md5sum, NULL);
EVP_MD_CTX_free(md5Context);
fclose(file);

std::stringstream md5string;
for (int i = 0; i < EVP_MD_size(md5Type); ++i)
{
md5string << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(md5sum[i]);
}

return md5string.str();
}

void extractZip(const char* zipFilename, const char* extractPath)
{
zip_t* archive = zip_open(zipFilename, 0, NULL);

if (!archive)
{
std::cerr << "Error opening ZIP file" << std::endl;
return;
}

struct zip_stat zipStat;
zip_stat_init(&zipStat);

// Get the number of entries in the ZIP file
int numEntries = zip_get_num_entries(archive, 0);

// Extract each entry in the ZIP file
for (int i = 0; i < numEntries; ++i)
{
if (zip_stat_index(archive, i, 0, &zipStat) == 0)
{
// Allocate memory for the data
char* data = new char[zipStat.size];
// Create directories if they don't exist
std::string filePath = extractPath + std::string("/") + zipStat.name;
std::string directory = filePath.substr(0, filePath.find_last_of("/\\"));
if (!std::filesystem::exists(directory))
{
std::filesystem::create_directories(directory);
}

// Open the file in the ZIP archive
zip_file_t* zipFile = zip_fopen_index(archive, i, 0);

if (zipFile)
{
// Read the data from the file in the ZIP archive
zip_fread(zipFile, data, zipStat.size);

// Close the file in the ZIP archive
zip_fclose(zipFile);

// Create a new file on disk and write the data
std::string filePath = extractPath + std::string("/") + zipStat.name;
std::ofstream outFile(filePath, std::ios::binary);
outFile.write(data, zipStat.size);
outFile.close();

delete[] data;// leak
}
else
{
std::cerr << "Error opening file in ZIP archive" << std::endl;
}
}
else
{
std::cerr << "Error getting information for entry " << i << std::endl;
}
}
// Close the ZIP archive
zip_close(archive);
}

/*
int test() {
// We need a class for hanling map download link or local file.
// Checking files checksum and or remove them
Expand Down Expand Up @@ -359,3 +185,4 @@ int test() {
return 0;
}
*/
1 change: 0 additions & 1 deletion TombRaiderPWModder/originalFileList.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
gameFileList TR3FileList ("TR3.Original");
void work()
{
TR3FileList.addFile("acc750411877527f118028aee957a854", "installscript.vdf");
TR3FileList.addFile("61f1377c2123f1c971cbfbf9c7ce8faf", "DATA.TAG");
TR3FileList.addFile("d09677d10864caa509ab2ffbd7ad37e7", "DEC130.DLL");
TR3FileList.addFile("4ee5d4026f15c967ed3ae599885018b0", "WINPLAY.DLL");
Expand Down
Loading

0 comments on commit 7fcad90

Please sign in to comment.