Detect-It-Easy/db/_init
Kaens c689e4e17b +bytecodeparsers *soundchips *_init
- db/soundchips is going to be just a database of sorts
 - db/bytecodeparsers created with the explicit purpose of handling bytecode parsing needed for the detections of some tougher stuff.
Has a uniform syntax.
Currently hosts MUAP98 and MXDRV parsers.

 - db/_init now has Number.prototype.clamp() as in C++
2025-05-30 21:36:31 +02:00

192 lines
No EOL
5.3 KiB
Text

includeScript("_debug");
var bDetected,
sType,
sName,
sVersion,
sOptions;
/**
* Initializes the detection process with the given parameters.
*
* @param {string} sType - The type of the item to detect. Defaults to "unknown".
* @param {string} sName - The name of the item to detect. Defaults to "unknown".
* @param {string} [sVersion] - The version of the item to detect. Optional.
* @param {string} [sOptions] - Additional options for detection. Optional.
*/
function define(sTypeInput, sNameInput) {
if (!sTypeInput) _error("No input type.");
sType = sTypeInput;
sName = sNameInput;
sVersion = arguments[2] ? arguments[2] : "";
sOptions = arguments[3] ? arguments[3] : "";
bDetected = false;
}
var init = define;
var visualDieOutputFix = false;
if (visualDieOutputFix) {
var _setResult_original = _setResult;
var _setResult_func = function (sType, sName, sVersion, sOptions) {
_setResult_original(sType, sName + (sVersion || sOptions) ? " " : "", sVersion, sOptions);
};
_setResult = _setResult_func;
}
/**
* Processes the detection result and resets the detection variables.
*
* If a detection has been made (bDetected is true), this function sets the result
* using the _setResult function with the provided type, name, version, and options.
* After setting the result, it resets the detection flag (bDetected) to false.
*
* Regardless of whether a detection was made, it resets the sName, sVersion, and sOptions
* variables to empty strings.
*/
function result() {
if (bDetected) {
_setResult(sType, sName, sVersion, sOptions);
bDetected = false;
}
sName = sVersion = sOptions = '';
var resultValue = bDetected;
bDetected = false;
return resultValue;
}
/**
* Attaches new strings to `this`, separating everything with a comma and no space.
* @param {String...} the open list of strings to append.
*/
String.prototype.append = function () {
var s = this.valueOf(),
sep = ",";
if (arguments.length) {
if (s.length) s += sep;
s += Array.prototype.join.call(arguments, sep);
}
return s;
}
/**
* Append a string with a custom separator.
* @param {String} sString - String to append.
* @param {String} [sSep=","] - Separator string.
* @returns {String} The new string.
* @global
* @example
* sOptions = sOptions.appendS("debug","::");
*/
String.prototype.appendS = function (sString, sSep) {
var s = this.valueOf(),
sep = sSep || ",";
if (sString) {
if (s.length) s += sep;
s += sString;
}
return s;
}
String.prototype.addIfNone = function (substring) {
var s = this.valueOf();
if (substring && s.indexOf(substring) < 0)
s += substring;
return s;
}
// Some ECMA6 functions that should be removed if DIE transitions to it
String.prototype.beginsWith = function (s, min) { //the default case-sensitive version of beginsWith
var m = (typeof min == "number")? Math.min(s.length, min): 0;
if(s.length > this.length-m) return false; else return this.slice(m, s.length) == s
}
String.prototype.beginsWithCI = function (s, min) { //case-insensitive version of beginsWith
return this.toLowerCase().beginsWith(s.toLowerCase(), min)
}
String.prototype.endsWith = function (s, max) { //the default case-sensitive version of endsWith
var m = (typeof max == "number")? Math.min(this.length, max): this.length;
if(s.length > m) return false; else return this.slice(m-s.length, m) == s
}
String.prototype.endsWithCI = function (s, max) { //the case-insensitive version of endsWith
return this.toLowerCase().endsWith(s.toLowerCase(), max)
}
String.prototype.repeat = function (num) {
if(typeof num !== "number") return this;
var s = this; for(var i=1; i < num; i++) s += this; return s
}
/**
* Pad the start of a line with spaces or the character given.
* @param {String or Number} The width desired.
* @param {Number} [nPad=" "] The padding string. Loops.
* @returns {String} The new string.
* @global
* @example
* var a = 12; ...; if("aba12" === a.padStart(5,"ab")) ...
*/
String.prototype.padStart = function (targetLength, padString) {
var s = this.valueOf();
targetLength >>= 0; // truncate if number or convert non-number to 0;
padString = String(padString || ' ');
if (s.length >= targetLength) {
return String(s);
}
targetLength -= s.length;
if (targetLength > padString.length) {
padString += padString.repeat(Math.ceil(targetLength / padString.length));
// append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + String(s);
}
Number.prototype.padStart = function (targetLength, padString) {
var s = this.valueOf().toString();
targetLength >>= 0; // truncate if number or convert non-number to 0;
padString = String(padString || ' ');
if (s.length >= targetLength) return s;
targetLength -= s.length;
if (targetLength > padString.length) {
padString += padString.repeat(Math.ceil(targetLength / padString.length));
// append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + s;
}
Number.prototype.clamp = function(min, max) { return Math.min(Math.max(this, min), max) }
includeScript("language");