Skip to content

Commit

Permalink
Merge pull request #44 from BadgerCode/FixLoadingLocalMultiplayer
Browse files Browse the repository at this point in the history
Splits up HTML file into 65k chunks
  • Loading branch information
BadgerCode authored Feb 3, 2019
2 parents d3deb5c + d69326e commit d4b582f
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 315 deletions.
82 changes: 64 additions & 18 deletions emojichat-html/update-lua.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,100 @@
var fs = require('fs');

function LogInfo(message) { console.log('%s\n', message);}
function LogSuccess(message) { console.log('\x1b[32m%s\x1b[0m\n', message);}
function LogFailure(message) { console.log('\x1b[31m%s\x1b[0m\n', message);}
function LogInfo(message) { console.log('%s\n', message); }
function LogSuccess(message) { console.log('\x1b[32m%s\x1b[0m\n', message); }
function LogFailure(message) { console.log('\x1b[31m%s\x1b[0m\n', message); }

const luaFile = '../emojichat/lua/emojichat/cl_html.lua';
const htmlFile = './dist/index.html';
const LUA_MAX_FILE_SIZE = 65536 - 100;
const LUA_FILES_PATH = '../emojichat/lua/emojichat/';
const LUA_INCLUDES_FILE = 'sh_html_init.lua';
const LUA_INCLUDES_FILE_PATTERN = `
if SERVER then
AddCSLuaFile()
{SERVER_LINES}
elseif CLIENT then
emojichatHTML = { }
{CLIENT_LINES}
end`;

const LUA_HTML_FILES_PATH = LUA_FILES_PATH + 'cl_html/';
const LUA_FILE_NAME_PREFIX = 'html';
const LUA_FILE_NAME_SUFFIX = '.lua';
const HTML_FILE = './dist/index.html';

var args = require('minimist')(process.argv.slice(2));
var watchForChanges = args["watch"] == true;


function UpdateLuaFile()
{
fs.readFile(htmlFile, 'utf8', function(err, contents) {
if(err != null) {
function UpdateLuaFile() {
fs.readFile(HTML_FILE, 'utf8', function (err, htmlFileContents) {
if (err != null) {
LogFailure("LUA: error reading file");
console.log(err);
}

var luaFileContents = `eChat.config.html = [===[\n${contents}\n]===]`;
var numLuaFiles = Math.ceil(htmlFileContents.length / LUA_MAX_FILE_SIZE);
var htmlLuaFileNames = [];

for (let f = 0; f < numLuaFiles; f++) {
let htmlSegement = htmlFileContents.substring(f * LUA_MAX_FILE_SIZE, (f + 1) * LUA_MAX_FILE_SIZE);
let fileContents = `table.insert(emojichatHTML, [===[${htmlSegement}]===])`;
let fileName = LUA_FILE_NAME_PREFIX + f + LUA_FILE_NAME_SUFFIX;

htmlLuaFileNames.push(fileName);
fs.writeFile(LUA_HTML_FILES_PATH + fileName, fileContents, function (err) {
if (err != null) {
LogFailure("LUA: error writing file " + fileName)
console.log(err);
}
else {
LogSuccess("LUA: UPDATED " + fileName);
}
});
}


fs.writeFile(luaFile, luaFileContents, function(err) {
if(err != null) {
LogFailure("LUA: error writing file")
fs.writeFile(LUA_FILES_PATH + LUA_INCLUDES_FILE, BuildLuaIncludesFile(htmlLuaFileNames), function (err) {
if (err != null) {
LogFailure("LUA: error writing includes file")
console.log(err);
}
else {
LogSuccess("LUA: UPDATED cl_html.lua");
LogSuccess("LUA: UPDATED " + LUA_INCLUDES_FILE);
}
});
});
}

if(!fs.existsSync(htmlFile)) {
function BuildLuaIncludesFile(htmlFileNames) {
var serverLines = "";
var clientLines = "";

htmlFileNames.forEach(htmlFileName => {
serverLines += `\n AddCSLuaFile('emojichat/cl_html/${htmlFileName}')`;
clientLines += `\n include('emojichat/cl_html/${htmlFileName}')`;
});

return LUA_INCLUDES_FILE_PATTERN.replace("{SERVER_LINES}", serverLines).replace("{CLIENT_LINES}", clientLines);
}



if (!fs.existsSync(HTML_FILE)) {
LogFailure("LUA-ERROR: HTML file not compiled!")
process.exit();
return;
}


if(watchForChanges) {
var watcher = fs.watch(htmlFile, (event, filename) => {
if (watchForChanges) {
var watcher = fs.watch(HTML_FILE, (event, filename) => {
if (filename) {
LogInfo(`LUA: ${filename} file changed`);
UpdateLuaFile();
}
});

process.on('SIGINT', function() {
process.on('SIGINT', function () {
watcher.close();
process.exit();
});
Expand Down
4 changes: 2 additions & 2 deletions emojichat/lua/autorun/emoji-chat.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
include("emojichat/sh_html_init.lua")

if SERVER then
resource.AddWorkshop(1622781878)

AddCSLuaFile()
AddCSLuaFile("emojichat/cl_echat.lua")
AddCSLuaFile("emojichat/cl_html.lua")

AddCSLuaFile("emojichat/cl_chat-overrides.lua")
AddCSLuaFile("emojichat/cl_hooks.lua")
Expand All @@ -16,7 +17,6 @@ if SERVER then
include("emojichat/sv_chat.lua")
elseif CLIENT then
include("emojichat/cl_echat.lua")
include("emojichat/cl_html.lua")

include("emojichat/cl_util.lua")
include("emojichat/cl_text-component.lua")
Expand Down
2 changes: 1 addition & 1 deletion emojichat/lua/emojichat/cl_echat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ eChat = {
}

eChat.config = {
html = "Loading...",
html = table.concat(emojichatHTML),
timestamps = true,
position = 1,
fadeTime = 12,
Expand Down
Loading

0 comments on commit d4b582f

Please sign in to comment.