0ad/binaries/data/mods/public/simulation/components/tests/setup.js
Dunedan 93ce94655d
Use @stylistic/brace-style for eslint
Up to now `eslint-plugin-brace-rules` was used to enforce a common brace
style for JavaScript code. This plugin was however updated the last time
over 9 years ago and will be incompatible with ESLint v10, as that
[removes `context.getSourceCode()`][1], the plugin relies on.

To keep the eslint config working with ESLint v10, this replaces
`eslint-plugin-brace-rules` with the [`@stylistic/brace-style`][2] rule
from `@stylistic/eslint-plugin`, a package we already use.

While `@stylistic/brace-style` doesn't offer an option to format braces
in exactly the same way as before, the "allman" style seems to be the
one closest to the existing code.

[1]: https://eslint.org/blog/2025/11/eslint-v10.0.0-alpha.0-released/#removed-deprecated-rule-context-members
[2]: https://eslint.style/rules/brace-style
2026-01-12 21:33:52 +01:00

168 lines
3.2 KiB
JavaScript

var g_NewIID = 1000; // some arbitrary not-yet-used number
var g_NewMTID = 1000; // some arbitrary not-yet-used number
var g_ComponentTypes = {};
var g_Components = {};
// Emulate some engine functions:
Engine.RegisterComponentType = function(iid, name, ctor)
{
TS_ASSERT(!g_ComponentTypes[name]);
g_ComponentTypes[name] = { "iid": iid, "ctor": ctor };
};
Engine.RegisterSystemComponentType = function(iid, name, ctor)
{
TS_ASSERT(!g_ComponentTypes[name]);
g_ComponentTypes[name] = { "iid": iid, "ctor": ctor };
};
Engine.RegisterInterface = function(name)
{
global["IID_" + name] = g_NewIID++;
};
Engine.RegisterMessageType = function(name)
{
global["MT_" + name] = g_NewMTID++;
};
Engine.QueryInterface = function(ent, iid)
{
if (g_Components[ent] && g_Components[ent][iid])
return g_Components[ent][iid];
return null;
};
Engine.RegisterGlobal = function(name, value)
{
global[name] = value;
};
Engine.DestroyEntity = function(ent)
{
for (const cid in g_Components[ent])
{
const cmp = g_Components[ent][cid];
if (cmp && cmp.Deinit)
cmp.Deinit();
}
delete g_Components[ent];
// TODO: should send Destroy message
};
Engine.PostMessage = function(ent, iid, message)
{
// TODO: make this send a message if necessary
};
Engine.BroadcastMessage = function(iid, message)
{
// TODO: make this send a message if necessary
};
global.ResetState = function()
{
g_Components = {};
};
global.AddMock = function(ent, iid, mock)
{
if (!g_Components[ent])
g_Components[ent] = {};
g_Components[ent][iid] = mock;
return g_Components[ent][iid];
};
global.DeleteMock = function(ent, iid)
{
if (!g_Components[ent])
g_Components[ent] = {};
delete g_Components[ent][iid];
};
global.ConstructComponent = function(ent, name, template)
{
const cmp = new g_ComponentTypes[name].ctor();
Object.defineProperties(cmp, {
"entity": {
"value": ent,
"configurable": false,
"enumerable": false,
"writable": false
},
"template": {
"value": template && deepfreeze(clone(template)),
"configurable": false,
"enumerable": false,
"writable": false
},
"_cmpName": {
"value": name,
"configurable": false,
"enumerable": false,
"writable": false
}
});
cmp.Init?.();
if (!g_Components[ent])
g_Components[ent] = {};
g_Components[ent][g_ComponentTypes[name].iid] = cmp;
return cmp;
};
/**
* A simple Spy proxy that tracks and forward function calls.
* NB: this immediately replaces obj's func.
*/
global.Spy = function(obj, func)
{
this._called = 0;
this._callargs = [];
const og_func = obj[func];
const spy = (...args) =>
{
++this._called;
this._callargs.push(args);
return og_func.apply(obj, args);
};
obj[func] = spy;
this._reset = () =>
{
this._called = 0;
this._callargs = [];
};
return this;
};
global.SerializationCycle = function(cmp)
{
let data;
if (typeof cmp.Serialize === "function")
data = cmp.Serialize();
else
{
data = {};
for (const att of cmp)
if (Object.hasOwn(cmp, att))
data[att] = cmp[att];
}
const newCmp = ConstructComponent(cmp.entity, cmp._cmpName, cmp.template);
if (typeof newCmp.Deserialize === "function")
newCmp.Deserialize(data);
else
for (const att in data)
newCmp[att] = data[att];
return newCmp;
};