Home Reference Source

stdlib/build/stdlib.js

(function(global, exports) {
    /**
 * @license ISC
 * Copyright (c) 2016, Max Maton

 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.

 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
    global = global;
    "use strict";
    Object.defineProperty(exports, "__esModule", {
        value: true
    });
    var _createClass = function() {
        function defineProperties(target, props) {
            for (var i = 0; i < props.length; i++) {
                var descriptor = props[i];
                descriptor.enumerable = descriptor.enumerable || false;
                descriptor.configurable = true;
                if ("value" in descriptor) descriptor.writable = true;
                Object.defineProperty(target, descriptor.key, descriptor);
            }
        }
        return function(Constructor, protoProps, staticProps) {
            if (protoProps) defineProperties(Constructor.prototype, protoProps);
            if (staticProps) defineProperties(Constructor, staticProps);
            return Constructor;
        };
    }();
    function _classCallCheck(instance, Constructor) {
        if (!(instance instanceof Constructor)) {
            throw new TypeError("Cannot call a class as a function");
        }
    }
    /**
 * Represents a color in the Source Engine
 */
    var Color = exports.Color = function() {
        /**
  * Creates an empty color
  */
        function Color() {
            _classCallCheck(this, Color);
            this._r = 0;
            this._g = 0;
            this._b = 0;
            this._a = 255;
            this._schemaKey = undefined;
        }
        /**
  * Parse a Color object from a string
  * @param {string} data - The data to parse
  * @return {Color}
  */
        _createClass(Color, [ {
            key: "toString",
            value: function toString() {
                if (this._schemaKey == undefined) {
                    return this.r + " " + this.g + " " + this.b + " " + this.a;
                }
                return this._schemaKey;
            }
        }, {
            key: "schemaKey",
            /**
   * The name of the lookup key in the schema file
   * @return {string}
   */
            get: function get() {
                return this._schemaKey;
            },
            set: function set(value) {
                this._schemaKey = value;
            }
        }, {
            key: "r",
            get: function get() {
                if (this._schemaKey != undefined) throw new Error("Unable to fetch schema from javascript, unknown");
                return this._r;
            },
            /**
   * Sets the red component
   * @param {number} value
   */
            set: function set(value) {
                this._r = Color.clamp(value);
            }
        }, {
            key: "g",
            get: function get() {
                if (this._schemaKey != undefined) throw new Error("Unable to fetch schema from javascript, unknown");
                return this._g;
            },
            set: function set(value) {
                this._g = Color.clamp(value);
            }
        }, {
            key: "b",
            get: function get() {
                if (this._schemaKey != undefined) throw new Error("Unable to fetch schema from javascript, unknown");
                return this._b;
            },
            set: function set(value) {
                this._b = Color.clamp(value);
            }
        }, {
            key: "a",
            get: function get() {
                if (this._schemaKey != undefined) throw new Error("Unable to fetch schema from javascript, unknown");
                return this._a;
            },
            set: function set(value) {
                this._a = Color.clamp(value);
            }
        } ], [ {
            key: "parse",
            value: function parse(data) {
                if (!data) throw new Error("No data given");
                var result = new Color();
                var split = data.split(" ");
                if (split.length == 4) {
                    result.r = parseInt(split[0]);
                    result.g = parseInt(split[1]);
                    result.b = parseInt(split[2]);
                    result.a = parseInt(split[3]);
                } else {
                    result._schemaKey = data;
                }
                return result;
            }
        }, {
            key: "clamp",
            value: function clamp(i) {
                var result = Math.round(i);
                if (result < 0) result = 0;
                if (result > 255) result = 255;
                return result;
            }
        } ]);
        return Color;
    }();
    "use strict";
    Object.defineProperty(exports, "__esModule", {
        value: true
    });
    var _createClass = function() {
        function defineProperties(target, props) {
            for (var i = 0; i < props.length; i++) {
                var descriptor = props[i];
                descriptor.enumerable = descriptor.enumerable || false;
                descriptor.configurable = true;
                if ("value" in descriptor) descriptor.writable = true;
                Object.defineProperty(target, descriptor.key, descriptor);
            }
        }
        return function(Constructor, protoProps, staticProps) {
            if (protoProps) defineProperties(Constructor.prototype, protoProps);
            if (staticProps) defineProperties(Constructor, staticProps);
            return Constructor;
        };
    }();
    function _classCallCheck(instance, Constructor) {
        if (!(instance instanceof Constructor)) {
            throw new TypeError("Cannot call a class as a function");
        }
    }
    /**
 * Handles output to the client console
 * @class
 */
    var console = exports.console = function() {
        function console() {
            _classCallCheck(this, console);
        }
        _createClass(console, null, [ {
            key: "log",
            /** Logs a message to the dev console
   * @param msg - The message to display
   */
            value: function log(msg) {
                global.__std_ConsoleLog(msg);
            }
        }, {
            key: "info",
            value: function info(msg) {
                global.__std_ConsoleLog(msg);
            }
        }, {
            key: "error",
            value: function error(msg) {
                global.__std_ConsoleError(msg);
            }
        } ]);
        return console;
    }();
    "use strict";
    Object.defineProperty(exports, "__esModule", {
        value: true
    });
    var _createClass = function() {
        function defineProperties(target, props) {
            for (var i = 0; i < props.length; i++) {
                var descriptor = props[i];
                descriptor.enumerable = descriptor.enumerable || false;
                descriptor.configurable = true;
                if ("value" in descriptor) descriptor.writable = true;
                Object.defineProperty(target, descriptor.key, descriptor);
            }
        }
        return function(Constructor, protoProps, staticProps) {
            if (protoProps) defineProperties(Constructor.prototype, protoProps);
            if (staticProps) defineProperties(Constructor, staticProps);
            return Constructor;
        };
    }();
    function _classCallCheck(instance, Constructor) {
        if (!(instance instanceof Constructor)) {
            throw new TypeError("Cannot call a class as a function");
        }
    }
    /**
 * Wrapper class for interop with Source Engine
 * 
 * Usage:<br>
 * ```let a = new KeyValues();```<br>
 * ```a["color"] = new Color();```<br>
 * ```b["userData"] = new KeyValues();```<br>
 * ```b["userData"]["foo"] = "bar";```
 */
    var KeyValues = exports.KeyValues = function() {
        function KeyValues() {
            _classCallCheck(this, KeyValues);
        }
        _createClass(KeyValues, [ {
            key: "_serialize",
            /**
   * Serializes this KeyValues into a format supported by the Source Engine
   * @param {string} name - The key name of this KeyValues
   * @type {string}
   */
            value: function _serialize(name) {
                if (!name) throw new Error("name parameter not set");
                var result = '"' + name + '"\n{\n';
                var keys = Object.keys(this);
                for (var i = 0; i < keys.length; i++) {
                    var key = keys[i];
                    var value = this[key];
                    if (value instanceof KeyValues) {
                        result += value._serialize(key);
                    } else if (value instanceof Color) {
                        result += '"' + key + '" "' + value.r + " " + value.g + " " + value.b + " " + value.a + '"\n';
                    } else {
                        result += '"' + key + '" "' + value + '"\n';
                    }
                }
                result += "}\n";
                return result;
            }
        } ], [ {
            key: "__startContext",
            value: function __startContext() {
                if (KeyValues.__current !== undefined) {
                    throw new Error("Another KeyValues construction is busy, unable to start a second context.");
                }
                KeyValues.__current = [ new KeyValues() ];
            }
        }, {
            key: "__stopContext",
            value: function __stopContext() {
                if (KeyValues.__current === undefined) {
                    throw new Error("No context started, please create a new one using __startContent");
                }
                if (KeyValues.__current.length != 1) {
                    throw new Error("Too many values on the stack, please pop until the root node");
                }
                var result = KeyValues.__current[0];
                KeyValues.__current = undefined;
                return result;
            }
        }, {
            key: "__pushNode",
            value: function __pushNode(name) {
                var node = new KeyValues();
                var cur = KeyValues.__current[KeyValues.__current.length - 1];
                cur[name] = node;
                KeyValues.__current.push(node);
            }
        }, {
            key: "__popNode",
            value: function __popNode() {
                KeyValues.__current.pop();
            }
        }, {
            key: "__setValue",
            value: function __setValue(name, value) {
                var cur = KeyValues.__current[KeyValues.__current.length - 1];
                cur[name] = value;
            }
        }, {
            key: "fromObject",
            value: function fromObject(object) {
                if (!object) throw new Error("Object property not set");
                var result = new KeyValues();
                var keys = Object.keys(object);
                for (var i = 0; i < keys.length; i++) {
                    var k = keys[i];
                    var prop = object[k];
                    if (prop instanceof Object) result[k] = KeyValues.fromObject(k, prop); else result[k] = prop;
                }
                return result;
            }
        } ]);
        return KeyValues;
    }();
    "use strict";
    Object.defineProperty(exports, "__esModule", {
        value: true
    });
    var _createClass = function() {
        function defineProperties(target, props) {
            for (var i = 0; i < props.length; i++) {
                var descriptor = props[i];
                descriptor.enumerable = descriptor.enumerable || false;
                descriptor.configurable = true;
                if ("value" in descriptor) descriptor.writable = true;
                Object.defineProperty(target, descriptor.key, descriptor);
            }
        }
        return function(Constructor, protoProps, staticProps) {
            if (protoProps) defineProperties(Constructor.prototype, protoProps);
            if (staticProps) defineProperties(Constructor, staticProps);
            return Constructor;
        };
    }();
    function _classCallCheck(instance, Constructor) {
        if (!(instance instanceof Constructor)) {
            throw new TypeError("Cannot call a class as a function");
        }
    }
    /**
 * Handles looking up translation strings
 * @class
 */
    var translation = exports.translation = function() {
        function translation() {
            _classCallCheck(this, translation);
        }
        _createClass(translation, null, [ {
            key: "lookup",
            /** Converts a string to its translated equivalent
   * @param msg - The message to convert
   * @param arg1 - The first argument (optional)
   * @param arg2 - The second argument (optional)
   * @param arg3 - The third argument (optional)
   * @param arg4 - The forth argument (optional)
   */
            value: function lookup(msg, arg1, arg2, arg3, arg4) {
                return global.__std_TranslationGet(msg, arg1, arg2, arg3, arg4);
            }
        } ]);
        return translation;
    }();
})(global, exports);