Skip to content

Commit

Permalink
Moving core files to lib/internal and renamed libs to lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsSpyce committed May 23, 2018
1 parent 8ee8b47 commit 674f49e
Show file tree
Hide file tree
Showing 95 changed files with 2,516 additions and 428 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
config.yml
.vs/
.vscode/
core/
core_/
.vscode/
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 29 additions & 29 deletions libs/helpers.js → lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// Helpers file that can be used across Thiq
Object.extend = function(source, target) {
var properties = Object.getOwnPropertyNames(source);
for (var i = 0; i < properties.length; i++) {
var property = properties[i];
target[property] = source[property];
}
}

Object.printProperties = function(src) {
for (var field in src) {
console.log(field);
}
}

String.prototype.toCamelCase = function() {
var result = '';
for (var i = 0; i < this.length; i++) {
if (this[i] == '_') {
result += ' ';
} else {
if (this[i - 1] == '_') {
result += this[i].toUpperCase();
} else {
result += this[i].toLowerCase();
}
}
}
return result;
// Helpers file that can be used across Thiq
Object.extend = function(source, target) {
var properties = Object.getOwnPropertyNames(source);
for (var i = 0; i < properties.length; i++) {
var property = properties[i];
target[property] = source[property];
}
}

Object.printProperties = function(src) {
for (var field in src) {
console.log(field);
}
}

String.prototype.toCamelCase = function() {
var result = '';
for (var i = 0; i < this.length; i++) {
if (this[i] == '_') {
result += ' ';
} else {
if (this[i - 1] == '_') {
result += this[i].toUpperCase();
} else {
result += this[i].toLowerCase();
}
}
}
return result;
}
22 changes: 22 additions & 0 deletions lib/internal/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2017

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions lib/internal/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function assert(toCheck, error) {
if (!toCheck) {
if (!error) return false;
else throw error;
}
}

module.exports = assert;
global.assert = assert; // because we kinda need this everywhere. Not good to have globally, but fuck it
33 changes: 33 additions & 0 deletions lib/internal/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var Thread = require('@java.lang.Thread');
var Runnable = require('@java.lang.Runnable');
var Bukkit = require('@org.bukkit.Bukkit');

var scheduler = Bukkit.getScheduler();
var plugin = Bukkit.getPluginManager().getPlugin('Thiq');

var runnable = function(fn) {
return new Runnable({
run: fn
});
};

function async(fn, callback) {
return scheduler.runTaskAsynchronously(getPlugin(), runnable(function() {
var result;
var error;
try {
if (typeof(fn) == 'string') {
result = load(fn);
} else {
result = fn();
}
} catch (err) {
error = err;
}
if (callback) {
callback(result, error);
}
}));
}

module.exports = async;
33 changes: 33 additions & 0 deletions lib/internal/bootstrap/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* bootstrap/core.js
* ============
* This file is in charge of loading the internal modules. After the loading of the core
* files is complete, this file is also in charge of removing the internal modules from
* reference.
*/

var fs = require('internal/fs/index');
var path = require('internal/path/index');
var os = require('internal/os/index');
var _process = require('internal/process/core');
var assert = require('internal/assert');

var coreFiles = [
'require',
'lang',
'safety',
'promise',
'events',
'commands',
'permissions',
'tpm',
'tts'
];

function initializeCore() {
console.debug('Initializing core.');
for (var i = 0; i < coreFiles.length; i++) {
require('internal/' + coreFiles[i]);
}
console.debug('Initialized core. Running entry files.');
}
184 changes: 184 additions & 0 deletions lib/internal/bootstrap/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/**
* bootstrap/loader.js
* ===================
* This file is in charge of loading internal modules. This file is called before any
* others in the core in order to setup proper and consistent loading (besides plugin.js).
* After this is loaded, it is passed to bootstrap/core.js. The require given to these
* internal modules cannot load external modules, and the global require cannot load these
* modules.
*/


global = this;

var Bukkit = org.bukkit.Bukkit;
(function () {

var BufferedReader = Java.type('java.io.BufferedReader');
var InputStreamReader = Java.type('java.io.InputStreamReader');
var FileInputStream = Java.type('java.io.FileInputStream');
var BufferedWriter = Java.type('java.io.BufferedWriter');
var OutputStreamWriter = Java.type('java.io.OutputStreamWriter');
var FileOutputStream = Java.type('java.io.FileOutputStream');
var File = Java.type('java.io.File');
var Path = Java.type('java.nio.file.Paths');

var isWin32 = Java.type('java.lang.System').getProperty('os.name').startsWith('Windows');
var sep = isWin32 ? '\\' : '/';

var plugin = Bukkit.getPluginManager().getPlugin('Thiq');
var wrapper = [
'(function (exports, module, require, process, plugin) {',
'\n})'
];

var process = {
config: configureProcess()
};

function NativeModule(id) {
this.filename = 'lib/' + id + '.js';
this.id = id;
this.exports = {};
this.reflect = undefined;
this.exportsKeys = undefined;
this.loaded = false;
this.loading = true;
}

NativeModule._source = {}; // TODO: setup natives
NativeModule._cache = {};

var loaderExports = {
NativeModule
};
var loaderId = 'internal/bootstrap/loader';

NativeModule.require = function (id) {
if (id[0] == '@') return eval(id.substr(1));
if (id === loaderId) return loaderExports;
var cached = NativeModule.getCached(id);
if (cached && (cached.loaded || cached.loading)) return cached.exports;

var nativeModule = new NativeModule(id);
nativeModule.cache();
nativeModule.compile();

return nativeModule.exports;
}

NativeModule.wrap = function (script) {
return wrapper[0] + script + wrapper[1];
}

NativeModule.prototype.cache = function () {
NativeModule._cache[this.id] = this;
}

NativeModule.getCached = function(id) {
return NativeModule._cache[id];
}

NativeModule.prototype.compile = function () {
var script = '';
var resx = hasResource(this.filename);
if (!resx) {
script = fs_read('./plugins/Thiq/' + this.filename);
} else {
var fIn = new BufferedReader(new InputStreamReader(resx));
var line;
var string = '';
while ((line = fIn.readLine()) != null) {
string += line + '\n';
}
fIn.close();
script = string;
}

var wrapped = NativeModule.wrap(script);
var args = [
this.exports,
this,
NativeModule.require,
process,
plugin
];
var compiled = eval(wrapped);
try {
compiled.apply(null, args);
this.loaded = true;
} catch (ex) {
console.log('\xA7cAn error occured when reading ' + this.filename + ': ' + ex.message);
console.log(ex.getStackTrace());
throw ex;
} finally {
this.loading = false;
}
return this.exports;
}

function hasResource(filename) {
return plugin.getResource(filename);
}

function fs_read(location) {
var fIn = new BufferedReader(new InputStreamReader(new FileInputStream(location), "UTF8"));

var line;
var string = "";
while ((line = fIn.readLine()) != null) {
string += line + '\n';
}

fIn.close();
return string;
}

function fs_exists(path) {
return new File(path).exists();
}

function path_absolute(path) {
return Path.get(path).toAbsolutePath().toString();
}

function path_normalize(path) {
return Path.get(path).normalize().toString();
}

function path_dirname(path) {
var result = '';
var subs = path.split(sep);
for (var i = 0; i < subs.length - 1; i++) {
result += (subs[i] + sep);
}
return result;
}

function path_resolve() {
var paths = Array.prototype.slice.call(arguments);
if (paths.length === 0) return '';
var lastPath = Path.get(paths[0]);
for (var i = 1; i < paths.length; i++) {
lastPath = lastPath.resolve(Path.get(paths[i]));
}

return lastPath.toString();
}

function configureProcess() {
var config = fs_read('./plugins/Thiq/package.json');
try {
var json = JSON.parse(config);
return json;
} catch (ex) {
var err = new Error("Error reading package.json: " + ex.message);
throw err;
}
}

NativeModule.require('internal/logger');
NativeModule.require('internal/bootstrap/core');

return loaderExports;
})();

0 comments on commit 674f49e

Please sign in to comment.