Jump to content
McKay Development

Custom Storage Engine


DRogue

Recommended Posts

How do I use the on 'save' and on 'read' functions? When I'm doing it like this:

const user = new SteamUser();
user.logOn();

user.storage.on('save', function(filename, contents, callback) {
// filename is the name of the file, as a string
// contents is a Buffer containing the file's contents
// callback is a function which you MUST call on completion or error, with a single error argument

// For example:
console.log(filename, contents, callback);
});

user.storage.on('read', function(filename, callback) {
// filename is the name of the file, as a string
// callback is a function which you MUST call on completion or error, with an error argument and a Buffer argument

// For example:
console.log(filename, callback);
});

I just get the output on start of my electron app:

cellid-86[...].txt 91 (err) {
if(callback) {
callback(err || null); }
}


servers.json [91, 10, 9…] (err) {
if(callback) {
callback(err || null); }
}
 

So first this 'cellid-...txt' file and then the 'server.json'. But when I start an actual game via steam and close it the 'save' or 'read' event functions aren't called.

Edited by DRogue
Link to comment
Share on other sites

I thought that when I start a game locally the on 'read' event gets called and I can determine from where the save game should be loaded. And when I close the game that the on 'save' event gets called and I can save the file to a cloud server or something. With "actual game" I just mean a game I bought and pressing "Play" in the Steam client.

Link to comment
Share on other sites

Sorry, you're a bit confused as to what the function of the storage engine is. It isn't for Steam Cloud data (that hasn't been implemented in steam-user at all). Rather, it's for saving data generated by the module itself (for example, sentry files and server lists). By default that data is saved to a platform-specific application data path, but the storage engine allows you to redirect storage elsewhere.

Link to comment
Share on other sites

My example for mysql:
 

user.storage.on('save', function(filename, contents, callback) {  
    connection.query('REPLACE INTO `jsteam_custom_storage` (`filename`, `content`) VALUES (?, ?);', [filename, contents], function(err, results) {
        callback(err);
    });
});
 
user.storage.on('read', function(filename, callback) {
    connection.query('SELECT `content` FROM `jsteam_custom_storage` WHERE `filename` = ?', [filename], function (err, results, fields) {
        if(err) {
            callback(err);
            return;
        }
        callback(null, results[0].content);
    });
});

Please, tell me, what I need call back, when file does not exist (e. g. first start)?

Link to comment
Share on other sites

Found. Now works fine.

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : reldb_host,
  user     : reldb_user,
  password : reldb_password,
  database : reldb
  // ssl  : { ca : fs.readFileSync(__dirname + '/mysql-ca.crt') }
});

connection.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
    return;
  }
  console.log('connected as id ' + connection.threadId);
});
 

user.storage.on('save', function(filename, contents, callback) {  
    connection.query('REPLACE INTO `jsteam_custom_storage` (`filename`, `content`) VALUES (?, ?);', [filename, contents], function(err, results) {
        callback(err);
    });
});
 
user.storage.on('read', function(filename, callback) {
    connection.query('SELECT `content` FROM `jsteam_custom_storage` WHERE `filename` = ?', [filename], function (err, results, fields) {
        if(err) {
            callback(err);
            return;
        }
        if(results.length)
        {console.log(results);
            callback(null, results[0].content);
        } else {
            callback(new Error("File not found"));
        };
    });
});

I wanted to add init.sql, but cloudflare banned me with that.

upd: changed.

Edited by jazz
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...