Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nanoui stuff 2: Electric boogalo #184

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions code/modules/client/asset_cache.dm
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ You can set verify to TRUE if you want send() to sleep until the client has the
"nano/images/modular_computers/",
"nano/js/"
)
var/list/uncommon_dirs = list(
var/list/template_dirs = list(
"nano/templates/"
)

Expand All @@ -209,12 +209,19 @@ You can set verify to TRUE if you want send() to sleep until the client has the
if(fexists(path + filename))
common[filename] = fcopy_rsc(path + filename)
register_asset(filename, common[filename])
for (var/path in uncommon_dirs)
// Combine all templates into a single bundle.
var/list/template_data = list()
for(var/path in template_dirs)
var/list/filenames = flist(path)
for(var/filename in filenames)
if(copytext(filename, length(filename)) != "/") // Ignore directories.
if(fexists(path + filename))
register_asset(filename, fcopy_rsc(path + filename))
if(copytext(filename, length(filename) - 4) == ".tmpl") // Ignore directories.
template_data[filename] = file2text(path + filename)
var/template_bundle = "function nanouiTemplateBundle(){return [json_encode(template_data)];}"
var/fname = "data/nano_templates_bundle.js"
fdel(fname)
text2file(template_bundle, fname)
register_asset("nano_templates_bundle.js", fcopy_rsc(fname))
fdel(fname)

var/list/mapnames = list()
for(var/z in GLOB.using_map.map_levels)
Expand All @@ -228,12 +235,8 @@ You can set verify to TRUE if you want send() to sleep until the client has the
common[filename] = fcopy_rsc(file_path)
register_asset(filename, common[filename])

/datum/asset/nanoui/send(client, uncommon)
if(!islist(uncommon))
uncommon = list(uncommon)

send_asset_list(client, uncommon, FALSE)
send_asset_list(client, common, TRUE)
/datum/asset/nanoui/send(client)
send_asset_list(client, common)

/datum/asset/group/goonchat
children = list(
Expand Down
1 change: 1 addition & 0 deletions code/modules/nano/nanoui.dm
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ nanoui is used to open and update nano browser uis
/datum/nanoui/proc/add_common_assets()
add_script("libraries.min.js") // A JS file comprising of jQuery, doT.js and jQuery Timer libraries (compressed together)
add_script("nano_utility.js") // The NanoUtility JS, this is used to store utility functions.
add_script("nano_templates_bundle.js") // Contains all templates, generated by asset cache system at server start.
add_script("nano_template.js") // The NanoTemplate JS, this is used to render templates.
add_script("nano_state_manager.js") // The NanoStateManager JS, it handles updates from the server and passes data to the current state
add_script("nano_state.js") // The NanoState JS, this is the base state which all states must inherit from
Expand Down
6 changes: 0 additions & 6 deletions nano/To BYOND Cache.bat

This file was deleted.

53 changes: 53 additions & 0 deletions nano/Update BYOND Cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/python3
# Setup
import os, json, shutil, glob, platform

if(platform.system() != "Windows"):
print("Error: This script doesn't work on anything but Windows. How are you even planning to develop BYOND off windows?")
exit()

cdir = (os.getcwd())
if(cdir[-4:] != "nano"):
print("This script must be run from the nano directory.")
exit()

def find_cache():
target = os.path.join(os.path.expanduser("~"), "Documents", "BYOND", "cache")
for root, dirs, files in os.walk(target):
if "nano_templates_bundle.js" in files:
return root

cache = find_cache()
if cache == None:
print("Failed to find BYOND Cache.")
exit()

# Send all of the random files to the cache
def send_to_cache(arrayOfFiles):
for file in arrayOfFiles:
target = os.path.join(cache, os.path.split(file)[1])
shutil.copyfile(file, target)

send_to_cache([os.path.join(cdir, "css", f) for f in os.listdir(os.path.join(cdir, "css")) if os.path.isfile(os.path.join(cdir, "css", f))])
send_to_cache([os.path.join(cdir, "images", f) for f in os.listdir(os.path.join(cdir, "images")) if os.path.isfile(os.path.join(cdir, "images", f))])
send_to_cache([os.path.join(cdir, "js", f) for f in os.listdir(os.path.join(cdir, "js")) if os.path.isfile(os.path.join(cdir, "js", f))])

# Handle creating the tmpl bundle
arrOfFiles = glob.glob(os.path.join(cdir, "templates", "*.tmpl"))

tmpl_bundle_header = "function nanouiTemplateBundle(){return "
tmpl_bundle_footer = ";}"

big_json_array = {}

for file in arrOfFiles:
tmpl_name = os.path.split(file)[1]
with open(file, 'r') as tmpl:
big_json_array[tmpl_name] = tmpl.read()

tmpl_bundle = tmpl_bundle_header + json.dumps(big_json_array) + tmpl_bundle_footer

# Send the tmpl bundle to the cache
with open(os.path.join(cache, "nano_templates_bundle.js"), "w") as templateBundleFile:
templateBundleFile.write(tmpl_bundle)
templateBundleFile.close()
85 changes: 34 additions & 51 deletions nano/js/nano_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var NanoTemplate = function () {

var _templateData = {};
var _templateSources = {};

var _templates = {};
var _compiledTemplates = {};
Expand All @@ -15,59 +16,43 @@ var NanoTemplate = function () {
if (_templateData == null)
{
alert('Error: Template data did not load correctly.');
}

loadNextTemplate();
};

var loadNextTemplate = function () {
// we count the number of templates for this ui so that we know when they've all been rendered
var templateCount = Object.size(_templateData);

if (!templateCount)
{
$(document).trigger('templatesLoaded');
return;
}

// load markup for each template and register it
for (var key in _templateData)
{
if (!_templateData.hasOwnProperty(key))
{
continue;
}

$.when($.ajax({
url: _templateData[key],
if (('nanouiTemplateBundle' in window) && (typeof nanouiTemplateBundle === 'function')) {
_templateSources = nanouiTemplateBundle(); // From nanoui_templates.js
}
var templateLoadingPromises = $.map(_templateData, function(filename, key) {
var fetchSourcePromise;
if (_templateSources && _templateSources.hasOwnProperty(filename)) {
// Its in the bundle, just do it
fetchSourcePromise = $.Deferred().resolve(_templateSources[filename]).promise();
} else {
// Otherwise fetch from ze network
fetchSourcePromise = $.ajax({
url: filename,
cache: false,
dataType: 'text'
}))
.done(function(templateMarkup) {

templateMarkup += '<div class="clearBoth"></div>';

try
{
NanoTemplate.addTemplate(key, templateMarkup);
}
catch(error)
{
alert('ERROR: An error occurred while loading the UI: ' + error.message);
return;
}

delete _templateData[key];

loadNextTemplate();
})
.fail(function () {
alert('ERROR: Loading template ' + key + '(' + _templateData[key] + ') failed!');
});
}

return;
}
}
return fetchSourcePromise.done(function(templateMarkup) {
templateMarkup += '<div class="clearBoth"></div>';
try {
NanoTemplate.addTemplate(key, templateMarkup);
} catch(error) {
alert('ERROR: An error occurred while loading the UI: ' + error.message);
return;
}
}).fail(function () {
alert('ERROR: Loading template ' + key + '(' + _templateData[key] + ') failed!');
});
});

// Wait for all of them to be done and then trigger the event.
$.when.apply(this, templateLoadingPromises).done(function() {
$(document).trigger('templatesLoaded');
});
};

var compileTemplates = function () {

Expand Down Expand Up @@ -114,7 +99,7 @@ var NanoTemplate = function () {

_helpers[helperName] = helperFunction;
},
addHelpers: function (helpers) {
addHelpers: function (helpers) {
for (var helperName in helpers) {
if (!helpers.hasOwnProperty(helperName))
{
Expand All @@ -127,9 +112,7 @@ var NanoTemplate = function () {
if (helpers.hasOwnProperty(helperName))
{
delete _helpers[helperName];
}
}
}
}
}();