2020-07-16 15:32:59 +08:00
|
|
|
class Property {
|
2020-10-05 05:44:06 +08:00
|
|
|
constructor(target_class, type = 'boolean', name, options = 0) {
|
|
|
|
if (!target_class.properties) {
|
|
|
|
target_class.properties = {};
|
|
|
|
}
|
|
|
|
target_class.properties[name] = this;
|
2020-07-16 15:32:59 +08:00
|
|
|
|
2020-10-05 05:44:06 +08:00
|
|
|
this.class = target_class;
|
|
|
|
this.name = name;
|
|
|
|
this.type = type;
|
2020-07-16 15:32:59 +08:00
|
|
|
|
2020-10-08 04:00:06 +08:00
|
|
|
if (options.default != undefined) {
|
2020-10-05 05:44:06 +08:00
|
|
|
this.default = options.default;
|
|
|
|
} else {
|
|
|
|
switch (this.type) {
|
|
|
|
case 'string': this.default = ''; break;
|
|
|
|
case 'molang': this.default = '0'; break;
|
|
|
|
case 'number': this.default = 0; break;
|
|
|
|
case 'boolean': this.default = false; break;
|
|
|
|
case 'array': this.default = []; break;
|
2021-09-21 00:45:02 +08:00
|
|
|
case 'instance': this.default = null; break;
|
2020-10-05 05:44:06 +08:00
|
|
|
case 'vector': this.default = [0, 0, 0]; break;
|
|
|
|
case 'vector2': this.default = [0, 0]; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (this.type) {
|
|
|
|
case 'string': this.isString = true; break;
|
|
|
|
case 'molang': this.isMolang = true; break;
|
|
|
|
case 'number': this.isNumber = true; break;
|
|
|
|
case 'boolean': this.isBoolean = true; break;
|
|
|
|
case 'array': this.isArray = true; break;
|
2021-09-21 00:45:02 +08:00
|
|
|
case 'instance': this.isInstance = true; break;
|
2020-10-05 05:44:06 +08:00
|
|
|
case 'vector': this.isVector = true; break;
|
|
|
|
case 'vector2': this.isVector2 = true; break;
|
|
|
|
}
|
2020-07-16 15:32:59 +08:00
|
|
|
|
2020-10-05 05:44:06 +08:00
|
|
|
if (this.isMolang) {
|
|
|
|
Object.defineProperty(target_class.prototype, `${name}_string`, {
|
|
|
|
get() {
|
2020-10-24 05:41:03 +08:00
|
|
|
return typeof this[name] == 'number' ? trimFloatNumber(this[name]) || '0' : this[name];
|
2020-10-05 05:44:06 +08:00
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
this[name] = val;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-10-01 19:07:45 +08:00
|
|
|
|
2020-10-05 05:44:06 +08:00
|
|
|
if (typeof options.merge == 'function') this.merge = options.merge;
|
|
|
|
if (typeof options.reset == 'function') this.reset = options.reset;
|
|
|
|
if (typeof options.merge_validation == 'function') this.merge_validation = options.merge_validation;
|
|
|
|
if (options.condition) this.condition = options.condition;
|
|
|
|
if (options.exposed == false) this.exposed = false;
|
2021-10-16 00:10:54 +08:00
|
|
|
if (options.export == false) this.export = false;
|
2022-05-07 05:43:50 +08:00
|
|
|
if (options.copy_value == false) this.copy_value = false;
|
2020-10-05 05:44:06 +08:00
|
|
|
if (options.label) this.label = options.label;
|
2020-12-31 05:27:12 +08:00
|
|
|
if (options.description) this.description = options.description;
|
2020-10-05 05:44:06 +08:00
|
|
|
if (options.options) this.options = options.options;
|
|
|
|
}
|
2020-10-16 04:23:37 +08:00
|
|
|
delete() {
|
2020-08-30 19:42:03 +08:00
|
|
|
delete this.class.properties[this.name];
|
|
|
|
}
|
2020-10-12 20:40:42 +08:00
|
|
|
getDefault(instance) {
|
|
|
|
if (typeof this.default == 'function') {
|
|
|
|
return this.default(instance);
|
|
|
|
} else {
|
|
|
|
return this.default;
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 05:44:06 +08:00
|
|
|
merge(instance, data) {
|
|
|
|
if (data[this.name] == undefined || !Condition(this.condition, instance)) return;
|
2020-07-16 15:32:59 +08:00
|
|
|
|
2020-10-05 05:44:06 +08:00
|
|
|
if (this.isString) {
|
|
|
|
Merge.string(instance, data, this.name, this.merge_validation)
|
|
|
|
}
|
|
|
|
else if (this.isNumber) {
|
|
|
|
Merge.number(instance, data, this.name)
|
|
|
|
}
|
|
|
|
else if (this.isMolang) {
|
|
|
|
Merge.molang(instance, data, this.name)
|
|
|
|
}
|
|
|
|
else if (this.isBoolean) {
|
|
|
|
Merge.boolean(instance, data, this.name, this.merge_validation)
|
|
|
|
}
|
|
|
|
else if (this.isArray || this.isVector || this.isVector2) {
|
|
|
|
if (data[this.name] instanceof Array) {
|
|
|
|
if (instance[this.name] instanceof Array == false) {
|
|
|
|
instance[this.name] = [];
|
|
|
|
}
|
|
|
|
instance[this.name].replace(data[this.name]);
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 00:45:02 +08:00
|
|
|
else if (this.isInstance) {
|
|
|
|
if (typeof data[this.name] === 'object') {
|
|
|
|
instance[this.name] =data[this.name];
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 05:44:06 +08:00
|
|
|
}
|
|
|
|
copy(instance, target) {
|
|
|
|
if (!Condition(this.condition, instance)) return;
|
2020-07-16 15:32:59 +08:00
|
|
|
|
2020-10-05 05:44:06 +08:00
|
|
|
if (this.isArray || this.isVector || this.isVector2) {
|
|
|
|
if (instance[this.name] instanceof Array) {
|
|
|
|
target[this.name] = instance[this.name].slice();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target[this.name] = instance[this.name];
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 21:07:01 +08:00
|
|
|
reset(instance, force) {
|
|
|
|
if (instance[this.name] == undefined && !Condition(this.condition, instance) && !force) return;
|
2020-10-12 20:40:42 +08:00
|
|
|
var dft = this.getDefault(instance)
|
|
|
|
|
2020-10-05 05:44:06 +08:00
|
|
|
if (this.isArray || this.isVector || this.isVector2) {
|
|
|
|
if (instance[this.name] instanceof Array == false) {
|
|
|
|
instance[this.name] = [];
|
|
|
|
}
|
2021-07-28 23:49:09 +08:00
|
|
|
instance[this.name].replace(dft || []);
|
2020-10-05 05:44:06 +08:00
|
|
|
} else {
|
|
|
|
instance[this.name] = dft;
|
|
|
|
}
|
|
|
|
}
|
2020-07-16 15:32:59 +08:00
|
|
|
}
|
2022-05-07 05:43:50 +08:00
|
|
|
Property.resetUniqueValues = function(type, instance) {
|
|
|
|
for (var key in type.properties) {
|
|
|
|
let property = type.properties[key];
|
|
|
|
if (property.copy_value == false) {
|
|
|
|
property.reset(instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|