<commit_before>var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var fs = require('fs');
var http = require('http');
var os = require('os');
var util = require('./util');

// MacOSX setting
var KRENDER_CMD = __dirname+'/krender_mac';
var LUA_CMD     = __dirname+'/lua_mac';

if (os.platform() === 'linux'){ // Linux setting
    KRENDER_CMD = __dirname+'/krender_linux';
    LUA_CMD = 'lua'; // Use system command
}else if (os.platform().indexOf('win') === 0){ // win setting
    KRENDER_CMD = __dirname+'/krender.exe';
    LUA_CMD = __dirname+'/lua.exe';
}


var SH_CMD = 'sh'


var sesstionTable = {};

function registerEditorEvent(socket)
{
	var def_srcdir = __dirname;// + '/work/'
	console.log('Working Dir='+def_srcdir);

	sesstionTable[socket.id] = {"dir":def_srcdir, "proc":null}
	
	socket.on('disconnect', function() {
		console.log("[DISCONNECT] ID="+socket.id);
		if (sesstionTable[socket.id].proc) {
			KillSpawn(sesstionTable[socket.id].proc);
		}
		delete sesstionTable[socket.id];
	});
	function updateFileList(path)
	{
		var list=[];
		util.getFiles(path, list);
		if (list.length != 0)
			socket.emit('updatefilelist', JSON.stringify(list));
	}
	socket.on('setWorkingPath', function(data){
		var path = data.path.toString();
		if (path.substr(path.length - 1) != "/")
			path += "/";
		sesstionTable[socket.id].dir = path;
		updateFileList(path);
	});
	socket.on('reqFileList', function(data) {
		var srcdir = sesstionTable[socket.id].dir;
		updateFileList(srcdir);
	});
	socket.on('reqFileOpen', function(data) {
		var srcdir = sesstionTable[socket.id].dir;
		var ext = util.getExtention(data);
		var filebuf = fs.readFileSync(srcdir+data);
		if (ext === "lua" || ext === "scn" || ext === "pwl" || ext === "cwl")
			socket.emit('showfile',{str:filebuf.toString(), type:"lua"});
		else if (ext === "sh" || ext === "pwf" || ext === "cwf")
			socket.emit('showfile',{str:filebuf.toString(), type:"sh"});
		else if (ext === "json" || ext === "pif" || ext === "cif")
			socket.emit('showfile',{str:filebuf.toString(), type:"json"});
		else if (ext === "frag")
			socket.emit('showfile',{str:filebuf.toString(), type:"glsl"});
		else if (ext === "jpg" || ext === "tga" || ext === "png"){
			var prefix;
				if      (ext==="jpg") prefix = 'data:image/jpeg;base64,';
				else if (ext==="tga") prefix = 'data:image/tga;base64,';
				else if (ext==="png") prefix = 'data:image/png;base64,';
				var base64 = new Buffer(filebuf, 'binary').toString('base64');
				var imgdata = prefix + base64;
				//socket.send(data);
				socket.emit('showfile_image',imgdata);
		}else
			socket.emit('showfile',{str:filebuf.toString(), type:""});
	});
	socket.on('reqFileSave', function(data) {
		//console.log(data.file);
		var srcdir = sesstionTable[socket.id].dir;
		fs.writeFile(srcdir+data.file, data.data, function (err) {
			if (err) throw err;
			console.log('It\'s saved!');
		});
	});
	function KillSpawn(sp){
		if (os.platform() !== 'darwin') {
			sp.kill();
		} else {
			var pid = sp.pid;
			console.log('processID='+pid);
			console.log('sh killthem.sh '+pid);
			exec('sh killthem.sh '+pid, function(error, stdout, stderr) {
				console.log('killed childs');
				console.log(error, stdout, stderr);
			});
		}
	}
	socket.on('stop', function(data) {
		var processspawn = sesstionTable[socket.id].proc;
		if (!processspawn)<commit_msg>update for linux<commit_after>var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var fs = require('fs');
var http = require('http');
var os = require('os');
var util = require('./util');

// MacOSX setting
var KRENDER_CMD = __dirname+'/krender_mac';
var LUA_CMD     = __dirname+'/lua_mac';

if (os.platform() === 'linux'){ // Linux setting
    KRENDER_CMD = __dirname+'/krender_linux';
    LUA_CMD = 'lua'; // Use system command
}else if (os.platform().indexOf('win') === 0){ // win setting
    KRENDER_CMD = __dirname+'/krender.exe';
    LUA_CMD = __dirname+'/lua.exe';
}


var SH_CMD = 'sh'


var sesstionTable = {};

function registerEditorEvent(socket)
{
	var def_srcdir = __dirname;// + '/work/'
	console.log('Working Dir='+def_srcdir);

	sesstionTable[socket.id] = {"dir":def_srcdir, "proc":null}
	
	socket.on('disconnect', function() {
		console.log("[DISCONNECT] ID="+socket.id);
		if (sesstionTable[socket.id].proc) {
			KillSpawn(sesstionTable[socket.id].proc);
		}
		delete sesstionTable[socket.id];
	});
	function updateFileList(path)
	{
		var list=[];
		util.getFiles(path, list);
		if (list.length != 0)
			socket.emit('updatefilelist', JSON.stringify(list));
	}
	socket.on('setWorkingPath', function(data){
		var path = data.path.toString();
		if (path.substr(path.length - 1) != "/")
			path += "/";
		sesstionTable[socket.id].dir = path;
		updateFileList(path);
	});
	socket.on('reqFileList', function(data) {
		var srcdir = sesstionTable[socket.id].dir;
		updateFileList(srcdir);
	});
	socket.on('reqFileOpen', function(data) {
		var srcdir = sesstionTable[socket.id].dir;
		var ext = util.getExtention(data);
		var filebuf = fs.readFileSync(srcdir+data);
		if (ext === "lua" || ext === "scn" || ext === "pwl" || ext === "cwl")
			socket.emit('showfile',{str:filebuf.toString(), type:"lua"});
		else if (ext === "sh" || ext === "pwf" || ext === "cwf")
			socket.emit('showfile',{str:filebuf.toString(), type:"sh"});
		else if (ext === "json" || ext === "pif" || ext === "cif")
			socket.emit('showfile',{str:filebuf.toString(), type:"json"});
		else if (ext === "frag")
			socket.emit('showfile',{str:filebuf.toString(), type:"glsl"});
		else if (ext === "jpg" || ext === "tga" || ext === "png"){
			var prefix;
				if      (ext==="jpg") prefix = 'data:image/jpeg;base64,';
				else if (ext==="tga") prefix = 'data:image/tga;base64,';
				else if (ext==="png") prefix = 'data:image/png;base64,';
				var base64 = new Buffer(filebuf, 'binary').toString('base64');
				var imgdata = prefix + base64;
				//socket.send(data);
				socket.emit('showfile_image',imgdata);
		}else
			socket.emit('showfile',{str:filebuf.toString(), type:""});
	});
	socket.on('reqFileSave', function(data) {
		//console.log(data.file);
		var srcdir = sesstionTable[socket.id].dir;
		fs.writeFile(srcdir+data.file, data.data, function (err) {
			if (err) throw err;
			console.log('It\'s saved!');
		});
	});
	function KillSpawn(sp){
		if (os.platform() === 'darwin' || os.platform() === 'linux') {
			var pid = sp.pid;
			console.log('processID='+pid);
			console.log('sh killthem.sh '+pid);
			exec('sh killthem.sh '+pid, function(error, stdout, stderr) {
				console.log('killed childs');
				console.log(error, stdout, stderr);
			});
		} else {
			sp.kill();
		}
	}
	socket.on('stop', function(data) {
		var processspawn = sesstionTable[socket.id].proc;
		if (!processspawn)
{'fraction_non_alphanumeric': 0.20996756119138896, 'fraction_numerical': 0.0044234739015039815, 'mean_word_length': 4.3325471698113205, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7205457', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2470, 'n_words': 475}