-
Notifications
You must be signed in to change notification settings - Fork 43
/
GameInfoLoader.js
245 lines (211 loc) · 7.07 KB
/
GameInfoLoader.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// This file is part of InfiniteSky.
// Copyright (c) InfiniteSky Dev Teams - Licensed under GNU GPL
// For more information, see LICENCE in the main folder
// Game info loader
// When a game info file is loaded it should emit this event
// main.events.emit('gameinfo_loaded',file)
// In the future it would be nice to have a way to load game info from database or from file.
// TODO: Rewrite to use buffer rather than restuct as native code will be faster.
var path = require('path');
var events = require('events');
global.TotalGameInfos = 0;
global.TotalLoadedGameInfo = 0;
global.LoadGameInfoTimeout = null;
global.GameInfos = {};
global.GameInfoEmmiter = null;
global.GameInfosLoaded = false;
function GameInfoLoader(filename, structure, onRecordFunction) {
events.EventEmitter.call(this);
if(!filename || !structure || !onRecordFunction){
global.GameInfoEmmiter = this;
return;
}
global.GameInfos[filename] = {filename: filename, structure: structure, onRecordFunction: onRecordFunction, initialized: false, self: this};
var self = this;
this.Reload = function() {
console.log('Note that this is a soft reload nothing is removed only overwritten.');
self.Load(filename,structure,onRecordFunction);
}
this.Total = 0;
this.Loaded = 0;
this.on('loaded', function(filename){
if(global.GameInfos[filename] && !global.GameInfos[filename].initialized){
global.GameInfos[filename].initialized = true;
global.TotalLoadedGameInfo++;
}
if(global.TotalLoadedGameInfo === global.TotalGameInfos){
if(global.GameInfosLoaded) return;
global.GameInfosLoaded = true;
global.GameInfoEmmiter.emit('load');
}
});
global.TotalGameInfos++;
clearTimeout(global.LoadGameInfoTimeout);
global.LoadGameInfoTimeout = setTimeout(function(){
self.LoadAll();
}, 1000);
}
GameInfoLoader.prototype.__proto__ = events.EventEmitter.prototype;
GameInfoLoader.prototype.LoadAll = function(){
for(var i in global.GameInfos){
global.GameInfos[i].self.Load(global.GameInfos[i].filename,global.GameInfos[i].structure,global.GameInfos[i].onRecordFunction);
}
}
GameInfoLoader.prototype.inspect = safeguard_cli.inspect;
GameInfoLoader.prototype.Load = function(filename, structure, onRecordFunction) {
var self = this;
this.Loaded = false;
if (filename === undefined) {
throw new Error('You must provide a filename');
}
if (structure === undefined) {
throw new Error('You must provide a structure!');
}
if (onRecordFunction === undefined) {
onRecordFunction = function(record){ record; };
}
// Work out the file path
this.infos = [];
this.InfoStruct = structure;
var filepath = path.join(config.data_dir || 'data','infos',filename);
console.log('Loading Game Info: '+filepath);
fs.readFile(filepath,function(err,data) {
if (err) {
throw err;
return;
}
var RecordCount;
if (data.length>4) {
RecordCount = data.readUInt32LE(0);
} else {
throw new Error("Not enouth bytes in "+filepath+' to read RecordCount');
}
if (data.length-4>=self.InfoStruct.size * RecordCount) {
// TODO: Use normal buffer rather than restruct? Or code restruct to use buffer implementation.
var tasks = [];
var queue = async.queue(function (task, callback) {
var info = self.InfoStruct.unpack(task.data);
// Put the element into our function which could transform it.
// We expect to be able to get back ID either from the value in record or some sort of getter.
info = onRecordFunction(info);
if (info !== undefined && info.ID) {
// Assign to self as a key on ID for quick reference.
// Example infos.Item[1] would be Silver
// But we only care if ID is not 0 and that the info was actually valid
self.infos[info.ID] = info; // Put in array too
self[info.ID] = info; // Store in hash
}
async.setImmediate(callback);
},10);
var pos = 4;
for (var i=0;i<RecordCount;i++) {
queue.push({ index: i, data: data.slice(pos,pos+self.InfoStruct.size) });
pos+=self.InfoStruct.size;
}
delete data;
queue.pause();
queue.drain = function(err){
if (err) {
console.error(err);
return;
}
var csvFile = '';
var columns = [];
switch (filename) {
// case '005_00001.IMG':
// csvFile = 'Exp.csv';
// break;
case '005_00002.IMG':
csvFile = 'Items.csv';
columns = ['ID','Name','Description1','Description2', 'Description3'];
break;
case '005_00003.IMG':
csvFile = 'Skills.csv';
columns = ['ID','Name','Description1','Description2', 'Description3'];
break;
case '005_00004.IMG':
csvFile = 'Monsters.csv';
columns = ['ID','Name'];
break;
case '005_00006.IMG':
csvFile = 'Npcs.csv';
columns = ['ID','Name','Chat1','Chat2','Chat3','Chat4','Chat5'];
break;
//case '005_00007.IMG':
//csvFile = 'Quests.csv';
//break;
default:
self.emit('loaded', filename);
self.Loaded = true;
// main.events.emit('gameinfo_loaded',filename);
return;
break;
}
filepath = path.join(config.data_dir || 'data','translation',csvFile);
fs.exists(filepath, function(exists) {
if (!exists) {
console.error('Server cannot load "'+filepath+'" skipping translation csv.');
console.log('All '+filename+' records have been processed.');
self.emit('loaded', filename);
self.Loaded = true;
// main.events.emit('gameinfo_loaded',filename);
return;
}
csv
.fromPath(filepath, {quote: '"', escape: '\\',delimiter: ',', headers: true})
.on("record", function(data){
var record = self[data[columns[0]]];
if (record) {
for (var i=1;i<columns.length;i++) {
record[columns[i]] = data[columns[i]];
}
} else {
record = {};
for (var i=0;i<columns.length;i++) {
record[columns[i]] = data[columns[i]];
}
record = onRecordFunction(record);
if (record !== undefined && record.ID) {
self.infos[record[columns[0]]] = record;
self[record[columns[0]]] = record;
}
}
})
.on("end", function(){
self.Loaded = true;
console.log('All '+filename+' records have been processed.');
// main.events.emit('gameinfo_loaded',filename);
self.emit('loaded', filename);
});
});
};
queue.resume();
} else {
throw new Error("Not enouth bytes in "+filepath+' to read InfoStructure');
}
});
}
GameInfoLoader.prototype.getByName = function(name, limit) {
var found = [];
for (var i=0;i<this.infos.length;i++) {
if (this.infos[i] === undefined) continue;
if (this.infos[i].Name === name) {
found.push(this.infos[i]);
if (limit && found.length === limit) break;
}
}
return found;
}
GameInfoLoader.prototype.getByNameLike = function(name, limit) {
var found = [];
name = name.toLowerCase().replace(/ /g,'');
for (var i=0;i<this.infos.length;i++) {
if (this.infos[i] === undefined) continue;
if (this.infos[i].Name.toLowerCase().replace(/ /g,'').indexOf(name)>-1) {
found.push(this.infos[i]);
if (limit && found.length === limit) break;
}
}
return found;
}
module.exports = GameInfoLoader;