-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (108 loc) · 4.26 KB
/
index.js
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
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var fs = require('fs');
var path = require('path');
var os = require('os');
var extend = require('extend');
var Game = require('./lib/game');
var parsePlayerLogEvent = require('./lib/log-parsers/player.js');
var parseGameResultLogEvent = require('./lib/log-parsers/game-result.js');
var parseGameStateLogEvent = require('./lib/log-parsers/game-state.js');
var parseScreenChangeLogEvent = require('./lib/log-parsers/screen-change.js');
var parseZoneChangeLogEvent = require('./lib/log-parsers/zone-change.js');
var defaultOptions = {
endOfLineChar: os.EOL
};
var debug = require('debug');
// Define some debug logging functions for easy and readable debug messages.
var log = {
main: debug('HLW'),
gameStart: debug('HLW:game-start'),
zoneChange: debug('HLW:zone-change'),
gameOver: debug('HLW:game-over')
};
// Determine the default location of the config and log files.
if (/^win/.test(os.platform())) {
log.main('Windows platform detected.');
var programFiles = 'Program Files';
if (/64/.test(os.arch())) {
programFiles += ' (x86)';
}
defaultOptions.logFile = path.join('C:', programFiles, 'Hearthstone', 'Hearthstone_Data', 'output_log.txt');
defaultOptions.configFile = path.join(process.env.LOCALAPPDATA, 'Blizzard', 'Hearthstone', 'log.config');
} else {
log.main('OS X platform detected.');
defaultOptions.logFile = path.join(process.env.HOME, 'Library', 'Logs', 'Unity', 'Player.log');
defaultOptions.configFile = path.join(process.env.HOME, 'Library', 'Preferences', 'Blizzard', 'Hearthstone', 'log.config');
}
// The watcher is an event emitter so we can emit events based on what we parse in the log.
function LogWatcher(options) {
this.options = extend({}, defaultOptions, options);
log.main('config file path: %s', this.options.configFile);
log.main('log file path: %s', this.options.logFile);
// Copy local config file to the correct location.
// We're just gonna do this every time.
var localConfigFile = path.join(__dirname, 'log.config');
fs.createReadStream(localConfigFile).pipe(fs.createWriteStream(this.options.configFile));
log.main('Copied log.config file to force Hearthstone to write to its log file.');
}
util.inherits(LogWatcher, EventEmitter);
LogWatcher.prototype.start = function () {
var self = this;
var parserState = new ParserState;
log.main('Log watcher started.');
// Begin watching the Hearthstone log file.
var fileSize = fs.statSync(self.options.logFile).size;
fs.watchFile(self.options.logFile, function (current, previous) {
if (current.mtime <= previous.mtime) { return; }
// We're only going to read the portion of the file that we have not read so far.
var newFileSize = fs.statSync(self.options.logFile).size;
var sizeDiff = newFileSize - fileSize;
if (sizeDiff < 0) {
fileSize = 0;
sizeDiff = newFileSize;
}
var buffer = new Buffer(sizeDiff);
var fileDescriptor = fs.openSync(self.options.logFile, 'r');
fs.readSync(fileDescriptor, buffer, 0, sizeDiff, fileSize);
fs.closeSync(fileDescriptor);
fileSize = newFileSize;
self.parseBuffer(buffer, parserState);
});
self.stop = function () {
fs.unwatchFile(self.options.logFile);
delete self.stop;
};
};
LogWatcher.prototype._games = [];
LogWatcher.prototype.stop = function () {};
LogWatcher.prototype.parseBuffer = function (buffer, parserState) {
var self = this;
if (!parserState) {
parserState = new ParserState;
}
// Iterate over each line in the buffer.
buffer.toString().split(this.options.endOfLineChar).forEach(function (line) {
// always have a current game instance in place
if(!self._games.current) {
self._games.current = new Game();
}
if(line.replace(/\s/g, '') !== '')
self.emit('log-line', line, self._games.current);
parseZoneChangeLogEvent(line, self);
parsePlayerLogEvent(line, self);
parseGameResultLogEvent(line, self);
parseGameStateLogEvent(line, self);
parseScreenChangeLogEvent(line, self);
});
};
function ParserState() {
this.reset();
}
ParserState.prototype.reset = function () {
this.players = [];
this.playerCount = 0;
this.gameOverCount = 0;
};
// Set the entire module to our emitter.
module.exports = LogWatcher;