1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #include <steam/steam_api.h> // Required for Steam API functions
#include <vector> // Required for std::vector (to hold file data)
#include <fstream> // Required for std::ifstream (to read local file)
#include <iostream> // Required for std::cout, std::cerr (console output)
#include <string> // Required for std::string
// --- Configuration: Target Steam Application ID ---
// This is the game/application whose cloud storage will be used.
const uint32 TARGET_APP_ID = 1985260;
// --- --- --- --- --- --- --- --- --- --- --- --- ---
// --- Configuration: Local and Cloud Filenames ---
// The name of the file to read from your computer.
const char* LOCAL_FILENAME = "data_1000.jpg";
// The name the file will have in Steam Cloud (can be the same or different).
const char* CLOUD_FILENAME = "data_1000.jpg";
// --- --- --- --- --- --- --- --- --- --- --- ---
int main() {
// --- Prerequisite: steam_appid.txt ---
// For the Steam API to know which game's context to use,
// a file named "steam_appid.txt" MUST exist in the same
// directory as this executable.
//
// This file must contain ONLY the application ID number.
// For this program, it should contain: 1985260
std::cout << "--- Steam Cloud Uploader ---" << std::endl;
std::cout << "INFO: Looking for AppID context via 'steam_appid.txt'." << std::endl;
std::cout << " Please ensure 'steam_appid.txt' exists in this directory" << std::endl;
std::cout << " and contains only the number: " << TARGET_APP_ID << std::endl;
std::cout << "----------------------------" << std::endl << std::endl;
// --- --- --- --- --- --- --- --- --- --- ---
// Attempt to initialize Steam API
// Requires:
// 1. Steam client running and user logged in.
// 2. The logged-in user must own the TARGET_APP_ID.
// 3. 'steam_appid.txt' must be present and correct.
// 4. The necessary Steam API library file (steam_api.dll or steam_api64.dll)
// must be in the same directory or accessible via the system path.
if (!SteamAPI_Init()) {
std::cerr << "[Problem] Could not initialize the Steam API." << std::endl;
std::cerr << "Common Checks:" << std::endl;
std::cerr << " - Is the Steam client running and are you logged in?" << std::endl;
std::cerr << " - Do you own the game/app with ID " << TARGET_APP_ID << " on this Steam account?" << std::endl;
std::cerr << " - Is the 'steam_appid.txt' file present in the same folder as this program" << std::endl;
std::cerr << " and does it contain exactly '" << TARGET_APP_ID << "'?" << std::endl;
std::cerr << " - Is the 'steam_api[64].dll' file present in this folder?" << std::endl;
std::cout << "\nPress Enter to exit." << std::endl;
std::cin.get();
return 1; // Indicate failure
}
std::cout << "Steam API Initialized successfully." << std::endl;
// Get the interface for interacting with Steam Remote Storage (Cloud)
ISteamRemoteStorage* pRemoteStorage = SteamRemoteStorage();
if (!pRemoteStorage) {
// This is less likely if SteamAPI_Init succeeded, but check anyway.
std::cerr << "[Problem] Could not get the Steam Remote Storage interface." << std::endl;
std::cerr << " This might indicate an unexpected issue with the Steam client." << std::endl;
SteamAPI_Shutdown(); // Clean up Steam API
std::cout << "\nPress Enter to exit." << std::endl;
std::cin.get();
return 1; // Indicate failure
}
// --- Load the local file data into memory ---
std::cout << "Attempting to read local file: '" << LOCAL_FILENAME << "'..." << std::endl;
std::ifstream localFile(LOCAL_FILENAME, std::ios::binary | std::ios::ate); // Open at the end to get size
if (!localFile.is_open()) {
std::cerr << "[Problem] Could not open the local file: '" << LOCAL_FILENAME << "'" << std::endl;
std::cerr << " Please ensure this file exists in the same directory as this program." << std::endl;
SteamAPI_Shutdown();
std::cout << "\nPress Enter to exit." << std::endl;
std::cin.get();
return 1; // Indicate failure
}
std::streamsize size = localFile.tellg(); // Get the file size
localFile.seekg(0, std::ios::beg); // Go back to the beginning of the file
std::vector<char> fileBuffer(static_cast<size_t>(size)); // Create a buffer to hold the file data
if (size > 0) {
if (!localFile.read(fileBuffer.data(), size)) {
// This might happen if the disk has an error during read
std::cerr << "[Problem] Could not read data from the file: '" << LOCAL_FILENAME << "'" << std::endl;
localFile.close();
SteamAPI_Shutdown();
std::cout << "\nPress Enter to exit." << std::endl;
std::cin.get();
return 1; // Indicate failure
}
std::cout << "Successfully read " << size << " bytes from '" << LOCAL_FILENAME << "'." << std::endl;
}
else {
// Handle empty file case (uploading an empty file is usually allowed)
std::cout << "Info: The local file '" << LOCAL_FILENAME << "' is empty (0 bytes)." << std::endl;
}
localFile.close(); // Close the file handle
// --- Finished loading file ---
// --- Write the file data to Steam Cloud ---
std::cout << "Attempting to write " << fileBuffer.size() << " bytes to Steam Cloud as: '" << CLOUD_FILENAME << "'..." << std::endl;
// Call the Steam API function to write the file.
// This writes to a local cache first; the Steam client handles the actual upload.
bool bWriteSuccess = pRemoteStorage->FileWrite(CLOUD_FILENAME, fileBuffer.data(), (int32)fileBuffer.size());
if (bWriteSuccess) {
std::cout << "[Success] File data submitted to Steam." << std::endl;
std::cout << " The Steam client will now handle uploading '" << CLOUD_FILENAME << "' to the cloud." << std::endl;
std::cout << " You can monitor the upload progress via the Steam client's interface." << std::endl;
}
else {
std::cerr << "[Error] Steam reported an issue writing the file to the cloud cache." << std::endl;
std::cerr << "Common Checks:" << std::endl;
std::cerr << " - Are you out of Steam Cloud storage space for this game (AppID " << TARGET_APP_ID << ")?" << std::endl;
std::cerr << " - Is Steam Cloud enabled for this game in your Steam settings?" << std::endl;
std::cerr << " - Is Steam Cloud enabled globally in your Steam settings?" << std::endl;
std::cerr << " - Does the filename '" << CLOUD_FILENAME << "' contain invalid characters?" << std::endl;
// Optional: Check quota (can be uncommented for more detail)
// uint64 totalBytes, availableBytes;
// if (pRemoteStorage->GetQuota(&totalBytes, &availableBytes)) {
// std::cerr << " Cloud Quota Info: Available " << availableBytes << " / Total " << totalBytes << " bytes." << std::endl;
// } else {
// std::cerr << " Could not retrieve cloud quota information." << std::endl;
// }
}
// --- Finished writing ---
// Shutdown the Steam API gracefully
SteamAPI_Shutdown();
std::cout << "Steam API Shutdown." << std::endl;
std::cout << "\nOperation finished. Press Enter to exit." << std::endl;
std::cin.get(); // Keep window open until user presses Enter
return bWriteSuccess ? 0 : 1; // Return 0 if the write call succeeded, 1 otherwise
}
|