Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.3 kB
5
Indexable
Never
/// @function save_game()
/// @description Saves the specified in (save_data) data, to a file named "Save.dat".
/// The game data is first converted to a JSON string, encoded with Base64, and hashed with SHA-1 for integrity checking. The encoded
/// string and the hash are then saved to the file, separated by a unique delimiter.
function save_game() {


    var save_path = "Save.dat";
    var save_data = {
        inventory: global.Inventory,
equipment: global.EquipmentSlots,
    };



    // Convert the save data to JSON string
    var json_string = json_stringify(save_data);

    // Encode the JSON string
    var encoded_string = base64_encode(json_string);

    // Create a hash of the original JSON string
    var hash = sha1_string_utf8(json_string);

    // Save the encoded string and hash to a file with a unique delimiter
    var delimiter = "|||END_OF_SAVE|||"; // Unique delimiter
    var file = file_text_open_write(save_path);
    if (file != -1) {
        var save_content = encoded_string + delimiter + hash;
        file_text_write_string(file, save_content);
        file_text_close(file);
        show_message("Game saved successfully!");
    } else {
        show_message("Failed to write to save file location.");
    }
}

/// @function load_game()
/// @description Loads the game state from the file "Save.dat". The file content is split into the encoded string and the hash using a unique delimiter.
/// The encoded string is decoded from Base64 back to JSON, and its integrity is verified using the SHA-1 hash. If the integrity check passes, the game data is restored.
function load_game() {
    var save_path = "Save.dat";

    if (file_exists(save_path)) {
        var file = file_text_open_read(save_path);
        if (file != -1) {
            // Read the entire file content
            var file_content = file_text_read_string(file);
            file_text_close(file);

            // Split the content using the unique delimiter
            var delimiter = "|||END_OF_SAVE|||"; // Unique delimiter
            var parts = string_split(file_content, delimiter);

            // Check if parts contain both the encoded string and the hash
            if (array_length(parts) == 2) {
                var saved_encoded_string = parts[0];
                var saved_hash = parts[1];

                // Decode the encoded string
                var json_string = base64_decode(saved_encoded_string);

                // Verify the hash
                var current_hash = sha1_string_utf8(json_string);
                if (current_hash == saved_hash) {
                    // Convert JSON string back to a struct
                    var save_data = json_parse(json_string);

                    // Restore game data from the struct
                    global.Inventory = save_data.inventory;
global.EquipmentSlots = save_data.equipment;



                    show_message("Game loaded successfully!");
                } else {
                    show_message("Save file integrity check failed.");
                }
            } else {
                show_message("Save file format is incorrect.");
            }
        } else {
            show_message("Failed to read the save file.");
        }
    } else {
        show_message("Save file does not exist.");
    }
            
}
Leave a Comment