@siredvin/typed-peripheral-base 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/base.lua +244 -0
- package/base.ts +262 -0
- package/lualib_bundle.lua +79 -0
- package/package.json +49 -0
package/base.lua
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
local ____lualib = require("lualib_bundle")
|
|
2
|
+
local __TS__Class = ____lualib.__TS__Class
|
|
3
|
+
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
|
|
4
|
+
local __TS__ArrayFind = ____lualib.__TS__ArrayFind
|
|
5
|
+
local __TS__ArrayFilter = ____lualib.__TS__ArrayFilter
|
|
6
|
+
local __TS__ArrayMap = ____lualib.__TS__ArrayMap
|
|
7
|
+
local __TS__ArrayPushArray = ____lualib.__TS__ArrayPushArray
|
|
8
|
+
local ____exports = {}
|
|
9
|
+
____exports.PeripheralDiscovery = __TS__Class()
|
|
10
|
+
local PeripheralDiscovery = ____exports.PeripheralDiscovery
|
|
11
|
+
PeripheralDiscovery.name = "PeripheralDiscovery"
|
|
12
|
+
function PeripheralDiscovery.prototype.____constructor(self)
|
|
13
|
+
end
|
|
14
|
+
____exports.PeripheralDiscoveryByName = __TS__Class()
|
|
15
|
+
local PeripheralDiscoveryByName = ____exports.PeripheralDiscoveryByName
|
|
16
|
+
PeripheralDiscoveryByName.name = "PeripheralDiscoveryByName"
|
|
17
|
+
__TS__ClassExtends(PeripheralDiscoveryByName, ____exports.PeripheralDiscovery)
|
|
18
|
+
function PeripheralDiscoveryByName.prototype.____constructor(self, provider, name)
|
|
19
|
+
PeripheralDiscoveryByName.____super.prototype.____constructor(self)
|
|
20
|
+
self.provider = provider
|
|
21
|
+
self.name = name
|
|
22
|
+
end
|
|
23
|
+
function PeripheralDiscoveryByName.prototype.discover(self)
|
|
24
|
+
return {self.provider:wrap(self.name)}
|
|
25
|
+
end
|
|
26
|
+
____exports.PeripheralDiscoveryByNames = __TS__Class()
|
|
27
|
+
local PeripheralDiscoveryByNames = ____exports.PeripheralDiscoveryByNames
|
|
28
|
+
PeripheralDiscoveryByNames.name = "PeripheralDiscoveryByNames"
|
|
29
|
+
__TS__ClassExtends(PeripheralDiscoveryByNames, ____exports.PeripheralDiscovery)
|
|
30
|
+
function PeripheralDiscoveryByNames.prototype.____constructor(self, provider, ...)
|
|
31
|
+
local names = {...}
|
|
32
|
+
PeripheralDiscoveryByNames.____super.prototype.____constructor(self)
|
|
33
|
+
self.provider = provider
|
|
34
|
+
self.names = names
|
|
35
|
+
end
|
|
36
|
+
function PeripheralDiscoveryByNames.prototype.discover(self)
|
|
37
|
+
local result = {}
|
|
38
|
+
for ____, name in ipairs(self.names) do
|
|
39
|
+
result[#result + 1] = self.provider:wrap(name)
|
|
40
|
+
end
|
|
41
|
+
return result
|
|
42
|
+
end
|
|
43
|
+
____exports.PeripheralDiscoveryByType = __TS__Class()
|
|
44
|
+
local PeripheralDiscoveryByType = ____exports.PeripheralDiscoveryByType
|
|
45
|
+
PeripheralDiscoveryByType.name = "PeripheralDiscoveryByType"
|
|
46
|
+
__TS__ClassExtends(PeripheralDiscoveryByType, ____exports.PeripheralDiscovery)
|
|
47
|
+
function PeripheralDiscoveryByType.prototype.____constructor(self, provider, ____type)
|
|
48
|
+
PeripheralDiscoveryByType.____super.prototype.____constructor(self)
|
|
49
|
+
self.provider = provider
|
|
50
|
+
self.type = ____type
|
|
51
|
+
end
|
|
52
|
+
function PeripheralDiscoveryByType.prototype.discover(self)
|
|
53
|
+
return self.provider:collect(self.type)
|
|
54
|
+
end
|
|
55
|
+
____exports.PeripheralDiscoveryByTypePattern = __TS__Class()
|
|
56
|
+
local PeripheralDiscoveryByTypePattern = ____exports.PeripheralDiscoveryByTypePattern
|
|
57
|
+
PeripheralDiscoveryByTypePattern.name = "PeripheralDiscoveryByTypePattern"
|
|
58
|
+
__TS__ClassExtends(PeripheralDiscoveryByTypePattern, ____exports.PeripheralDiscovery)
|
|
59
|
+
function PeripheralDiscoveryByTypePattern.prototype.____constructor(self, pattern)
|
|
60
|
+
PeripheralDiscoveryByTypePattern.____super.prototype.____constructor(self)
|
|
61
|
+
self.pattern = pattern
|
|
62
|
+
end
|
|
63
|
+
function PeripheralDiscoveryByTypePattern.prototype.discover(self)
|
|
64
|
+
local peripheralNames = peripheral.getNames()
|
|
65
|
+
local purifiedPattern = ("^" .. (string.gsub((string.gsub(self.pattern, "*", ".*")), "-", "%%-"))) .. "$"
|
|
66
|
+
return __TS__ArrayMap(
|
|
67
|
+
__TS__ArrayFilter(
|
|
68
|
+
peripheralNames,
|
|
69
|
+
function(____, peripheralName)
|
|
70
|
+
local peripheralTypes = {peripheral.getType(peripheralName)}
|
|
71
|
+
local matchedType = __TS__ArrayFind(
|
|
72
|
+
peripheralTypes,
|
|
73
|
+
function(____, value)
|
|
74
|
+
local result = {string.find(value, purifiedPattern)}
|
|
75
|
+
return result[1] ~= nil
|
|
76
|
+
end
|
|
77
|
+
)
|
|
78
|
+
return matchedType ~= nil
|
|
79
|
+
end
|
|
80
|
+
),
|
|
81
|
+
function(____, peripheralName) return peripheral.wrap(peripheralName) end
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
____exports.MultiplePeripheralDiscovery = __TS__Class()
|
|
85
|
+
local MultiplePeripheralDiscovery = ____exports.MultiplePeripheralDiscovery
|
|
86
|
+
MultiplePeripheralDiscovery.name = "MultiplePeripheralDiscovery"
|
|
87
|
+
__TS__ClassExtends(MultiplePeripheralDiscovery, ____exports.PeripheralDiscovery)
|
|
88
|
+
function MultiplePeripheralDiscovery.prototype.____constructor(self, ...)
|
|
89
|
+
local discoveries = {...}
|
|
90
|
+
MultiplePeripheralDiscovery.____super.prototype.____constructor(self)
|
|
91
|
+
self.discoveries = discoveries
|
|
92
|
+
end
|
|
93
|
+
function MultiplePeripheralDiscovery.prototype.discover(self)
|
|
94
|
+
local baseResult = {}
|
|
95
|
+
for ____, discovery in ipairs(self.discoveries) do
|
|
96
|
+
__TS__ArrayPushArray(
|
|
97
|
+
baseResult,
|
|
98
|
+
discovery:discover()
|
|
99
|
+
)
|
|
100
|
+
end
|
|
101
|
+
return baseResult
|
|
102
|
+
end
|
|
103
|
+
____exports.IPeripheralProvider = __TS__Class()
|
|
104
|
+
local IPeripheralProvider = ____exports.IPeripheralProvider
|
|
105
|
+
IPeripheralProvider.name = "IPeripheralProvider"
|
|
106
|
+
function IPeripheralProvider.prototype.____constructor(self, peripheral_type, dummy_constructor)
|
|
107
|
+
if dummy_constructor == nil then
|
|
108
|
+
dummy_constructor = function() return nil end
|
|
109
|
+
end
|
|
110
|
+
self.peripheral_type = peripheral_type
|
|
111
|
+
self.dummy_constructor = dummy_constructor
|
|
112
|
+
end
|
|
113
|
+
function IPeripheralProvider.prototype.find(self, derrefed_type)
|
|
114
|
+
local ____temp_0
|
|
115
|
+
if derrefed_type == nil then
|
|
116
|
+
____temp_0 = self.peripheral_type
|
|
117
|
+
else
|
|
118
|
+
____temp_0 = derrefed_type
|
|
119
|
+
end
|
|
120
|
+
local seached_type = ____temp_0
|
|
121
|
+
local any_peripheral = (peripheral.find(seached_type))
|
|
122
|
+
if any_peripheral ~= nil then
|
|
123
|
+
return any_peripheral
|
|
124
|
+
end
|
|
125
|
+
if settings.get("dark_toolkit.allow_dummy", "True") == "True" then
|
|
126
|
+
return self:dummy_constructor()
|
|
127
|
+
end
|
|
128
|
+
return nil
|
|
129
|
+
end
|
|
130
|
+
function IPeripheralProvider.prototype.searchByType(self, pattern, derrefed_type)
|
|
131
|
+
local targetedPeripherals = self:collect(derrefed_type)
|
|
132
|
+
local purifiedPattern = ("^" .. (string.gsub((string.gsub(pattern, "*", ".*")), "-", "%%-"))) .. "$"
|
|
133
|
+
if #targetedPeripherals == 0 then
|
|
134
|
+
return
|
|
135
|
+
end
|
|
136
|
+
return __TS__ArrayFilter(
|
|
137
|
+
targetedPeripherals,
|
|
138
|
+
function(____, p)
|
|
139
|
+
local peripheralTypes = {peripheral.getType(p)}
|
|
140
|
+
local matchedType = __TS__ArrayFind(
|
|
141
|
+
peripheralTypes,
|
|
142
|
+
function(____, value)
|
|
143
|
+
local result = {string.find(value, purifiedPattern)}
|
|
144
|
+
return result[1] ~= nil
|
|
145
|
+
end
|
|
146
|
+
)
|
|
147
|
+
print(
|
|
148
|
+
"peripheral",
|
|
149
|
+
peripheral.getName(p),
|
|
150
|
+
"is",
|
|
151
|
+
matchedType,
|
|
152
|
+
"for",
|
|
153
|
+
purifiedPattern
|
|
154
|
+
)
|
|
155
|
+
return matchedType ~= nil
|
|
156
|
+
end
|
|
157
|
+
)
|
|
158
|
+
end
|
|
159
|
+
function IPeripheralProvider.prototype.stubbornFindOrThrow(self, attemps, derrefed_type)
|
|
160
|
+
if attemps == nil then
|
|
161
|
+
attemps = 5
|
|
162
|
+
end
|
|
163
|
+
for _ = 1, attemps do
|
|
164
|
+
local per = self:find(derrefed_type)
|
|
165
|
+
if per == nil then
|
|
166
|
+
os.sleep(2)
|
|
167
|
+
else
|
|
168
|
+
return per
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
error("Cannot find peripheral " .. (derrefed_type or self.peripheral_type), 0)
|
|
172
|
+
end
|
|
173
|
+
function IPeripheralProvider.prototype.findOrThrow(self, derrefed_type)
|
|
174
|
+
local per = self:find(derrefed_type)
|
|
175
|
+
if per == nil then
|
|
176
|
+
error("Cannot find peripheral " .. (derrefed_type or self.peripheral_type), 0)
|
|
177
|
+
end
|
|
178
|
+
return per
|
|
179
|
+
end
|
|
180
|
+
function IPeripheralProvider.prototype.searchOneByType(self, pattern, derrefed_type)
|
|
181
|
+
local targetedPeripherals = self:searchByType(pattern, derrefed_type)
|
|
182
|
+
if #targetedPeripherals == 0 then
|
|
183
|
+
error("Cannot find peripheral " .. (derrefed_type or self.peripheral_type), 0)
|
|
184
|
+
end
|
|
185
|
+
if #targetedPeripherals > 1 then
|
|
186
|
+
local names = table.concat(
|
|
187
|
+
__TS__ArrayMap(
|
|
188
|
+
targetedPeripherals,
|
|
189
|
+
function(____, p) return peripheral.getName(p) end
|
|
190
|
+
),
|
|
191
|
+
";"
|
|
192
|
+
)
|
|
193
|
+
error((((("Too many peripherals was found " .. (derrefed_type or self.peripheral_type)) .. " even with pattern ") .. pattern) .. " this peripherals was found: ") .. names, 0)
|
|
194
|
+
end
|
|
195
|
+
return targetedPeripherals[1]
|
|
196
|
+
end
|
|
197
|
+
function IPeripheralProvider.prototype.collect(self, derrefed_type)
|
|
198
|
+
local ____temp_1
|
|
199
|
+
if derrefed_type == nil then
|
|
200
|
+
____temp_1 = self.peripheral_type
|
|
201
|
+
else
|
|
202
|
+
____temp_1 = derrefed_type
|
|
203
|
+
end
|
|
204
|
+
local seached_type = ____temp_1
|
|
205
|
+
local any_peripherals = {peripheral.find(seached_type)}
|
|
206
|
+
if #any_peripherals == 0 then
|
|
207
|
+
return {}
|
|
208
|
+
end
|
|
209
|
+
return __TS__ArrayMap(
|
|
210
|
+
any_peripherals,
|
|
211
|
+
function(____, v) return v end
|
|
212
|
+
)
|
|
213
|
+
end
|
|
214
|
+
function IPeripheralProvider.prototype.wrapValidate(self, name)
|
|
215
|
+
local candidate = peripheral.wrap(name)
|
|
216
|
+
if candidate == nil then
|
|
217
|
+
return nil
|
|
218
|
+
end
|
|
219
|
+
local types = {peripheral.getType(candidate)}
|
|
220
|
+
for ____, ____type in ipairs(types) do
|
|
221
|
+
if ____type == self.peripheral_type then
|
|
222
|
+
return candidate
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
return nil
|
|
226
|
+
end
|
|
227
|
+
function IPeripheralProvider.prototype.wrap(self, name)
|
|
228
|
+
if type(name) == "table" then
|
|
229
|
+
local periphs = {}
|
|
230
|
+
for ____, single_name in ipairs(name) do
|
|
231
|
+
local candidate = peripheral.wrap(single_name)
|
|
232
|
+
if candidate ~= nil then
|
|
233
|
+
periphs[#periphs + 1] = candidate
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
return periphs
|
|
237
|
+
end
|
|
238
|
+
local wrapped = peripheral.wrap(name)
|
|
239
|
+
if wrapped == nil then
|
|
240
|
+
return nil
|
|
241
|
+
end
|
|
242
|
+
return wrapped
|
|
243
|
+
end
|
|
244
|
+
return ____exports
|
package/base.ts
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { side } from "cc.completion";
|
|
2
|
+
import { base } from "tstl";
|
|
3
|
+
import { FluidStorageAPI } from "./apis/fluid_storage";
|
|
4
|
+
|
|
5
|
+
export type NamePredicate = (name: string) => boolean;
|
|
6
|
+
|
|
7
|
+
export type ShortItemDetail = {
|
|
8
|
+
count: number;
|
|
9
|
+
name: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type ExtendedItemDetail = ItemDetail & {
|
|
13
|
+
gtceu?: {
|
|
14
|
+
craftingUses?: number;
|
|
15
|
+
generalUses?: number;
|
|
16
|
+
maxUses?: number;
|
|
17
|
+
circuitConfiguration?: number;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type FluidDetail = {
|
|
22
|
+
amount: number;
|
|
23
|
+
name: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type PreciseFluidDetail = FluidDetail & {
|
|
27
|
+
precise_amount: number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type ElementPredicate<T> = (info: T) => boolean;
|
|
31
|
+
export type ItemPredicate = (info: ItemDetail) => boolean;
|
|
32
|
+
export type FluidPredicate = (info: FluidStorageAPI) => boolean;
|
|
33
|
+
export type CombinedPredicate = (info: ItemDetail | FluidDetail) => boolean;
|
|
34
|
+
export type PeripheralPredicate = (info: IPeripheral) => boolean;
|
|
35
|
+
|
|
36
|
+
export abstract class PeripheralDiscovery<T extends IPeripheral> {
|
|
37
|
+
abstract discover(): T[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class PeripheralDiscoveryByName<
|
|
41
|
+
T extends IPeripheral
|
|
42
|
+
> extends PeripheralDiscovery<T> {
|
|
43
|
+
constructor(
|
|
44
|
+
readonly provider: IPeripheralProvider<T>,
|
|
45
|
+
readonly name: string
|
|
46
|
+
) {
|
|
47
|
+
super();
|
|
48
|
+
}
|
|
49
|
+
discover(): T[] {
|
|
50
|
+
return [this.provider.wrap(this.name)];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class PeripheralDiscoveryByNames<
|
|
55
|
+
T extends IPeripheral
|
|
56
|
+
> extends PeripheralDiscovery<T> {
|
|
57
|
+
private names: string[];
|
|
58
|
+
constructor(readonly provider: IPeripheralProvider<T>, ...names: string[]) {
|
|
59
|
+
super();
|
|
60
|
+
this.names = names;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
discover(): T[] {
|
|
64
|
+
const result = [];
|
|
65
|
+
for (let name of this.names) {
|
|
66
|
+
result.push(this.provider.wrap(name));
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export class PeripheralDiscoveryByType<
|
|
73
|
+
T extends IPeripheral
|
|
74
|
+
> extends PeripheralDiscovery<T> {
|
|
75
|
+
constructor(
|
|
76
|
+
readonly provider: IPeripheralProvider<T>,
|
|
77
|
+
readonly type: string
|
|
78
|
+
) {
|
|
79
|
+
super();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
discover(): T[] {
|
|
83
|
+
return this.provider.collect(this.type);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export class PeripheralDiscoveryByTypePattern<
|
|
88
|
+
T extends IPeripheral
|
|
89
|
+
> extends PeripheralDiscovery<T> {
|
|
90
|
+
constructor(readonly pattern: string) {
|
|
91
|
+
super();
|
|
92
|
+
}
|
|
93
|
+
discover(): T[] {
|
|
94
|
+
const peripheralNames = peripheral.getNames();
|
|
95
|
+
const purifiedPattern =
|
|
96
|
+
"^" +
|
|
97
|
+
string.gsub(
|
|
98
|
+
string.gsub(this.pattern, "*", ".*")[0],
|
|
99
|
+
"-",
|
|
100
|
+
"%%-"
|
|
101
|
+
)[0] +
|
|
102
|
+
"$";
|
|
103
|
+
|
|
104
|
+
return peripheralNames
|
|
105
|
+
.filter((peripheralName) => {
|
|
106
|
+
const peripheralTypes = peripheral.getType(peripheralName);
|
|
107
|
+
const matchedType = peripheralTypes.find((value) => {
|
|
108
|
+
let result = string.find(value, purifiedPattern);
|
|
109
|
+
return result[0] != null;
|
|
110
|
+
});
|
|
111
|
+
return matchedType !== undefined;
|
|
112
|
+
})
|
|
113
|
+
.map((peripheralName) => peripheral.wrap(peripheralName)) as T[];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export class MultiplePeripheralDiscovery<
|
|
118
|
+
T extends IPeripheral
|
|
119
|
+
> extends PeripheralDiscovery<T> {
|
|
120
|
+
private discoveries: PeripheralDiscovery<T>[];
|
|
121
|
+
constructor(...discoveries: PeripheralDiscovery<T>[]) {
|
|
122
|
+
super();
|
|
123
|
+
this.discoveries = discoveries;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
discover(): T[] {
|
|
127
|
+
const baseResult = [];
|
|
128
|
+
for (let discovery of this.discoveries) {
|
|
129
|
+
baseResult.push(...discovery.discover());
|
|
130
|
+
}
|
|
131
|
+
return baseResult;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export class IPeripheralProvider<Type extends IPeripheral> {
|
|
136
|
+
constructor(
|
|
137
|
+
readonly peripheral_type: string,
|
|
138
|
+
readonly dummy_constructor: () => Type | null = () => null
|
|
139
|
+
) {}
|
|
140
|
+
|
|
141
|
+
find(derrefed_type?: string): Type | null {
|
|
142
|
+
let seached_type =
|
|
143
|
+
derrefed_type == null ? this.peripheral_type : derrefed_type;
|
|
144
|
+
const any_peripheral = peripheral.find(seached_type)[0];
|
|
145
|
+
if (any_peripheral != null) {
|
|
146
|
+
return any_peripheral as Type;
|
|
147
|
+
}
|
|
148
|
+
if (settings.get("dark_toolkit.allow_dummy", "True") == "True") {
|
|
149
|
+
return this.dummy_constructor();
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
searchByType(pattern: string, derrefed_type?: string): Array<Type> {
|
|
155
|
+
const targetedPeripherals = this.collect(derrefed_type);
|
|
156
|
+
const purifiedPattern =
|
|
157
|
+
"^" +
|
|
158
|
+
string.gsub(string.gsub(pattern, "*", ".*")[0], "-", "%%-")[0] +
|
|
159
|
+
"$";
|
|
160
|
+
if (targetedPeripherals.length == 0) return;
|
|
161
|
+
return targetedPeripherals.filter((p) => {
|
|
162
|
+
const peripheralTypes = peripheral.getType(p);
|
|
163
|
+
const matchedType = peripheralTypes.find((value) => {
|
|
164
|
+
let result = string.find(value, purifiedPattern);
|
|
165
|
+
return result[0] != null;
|
|
166
|
+
});
|
|
167
|
+
print(
|
|
168
|
+
"peripheral",
|
|
169
|
+
peripheral.getName(p),
|
|
170
|
+
"is",
|
|
171
|
+
matchedType,
|
|
172
|
+
"for",
|
|
173
|
+
purifiedPattern
|
|
174
|
+
);
|
|
175
|
+
return matchedType !== undefined;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
stubbornFindOrThrow(attemps: number = 5, derrefed_type?: string): Type {
|
|
180
|
+
for (let _ of $range(1, attemps)) {
|
|
181
|
+
let per = this.find(derrefed_type);
|
|
182
|
+
if (per == null) {
|
|
183
|
+
os.sleep(2);
|
|
184
|
+
} else {
|
|
185
|
+
return per;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
throw (
|
|
189
|
+
"Cannot find peripheral " + (derrefed_type || this.peripheral_type)
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
findOrThrow(derrefed_type?: string): Type {
|
|
194
|
+
let per = this.find(derrefed_type);
|
|
195
|
+
if (per == null) {
|
|
196
|
+
throw (
|
|
197
|
+
"Cannot find peripheral " +
|
|
198
|
+
(derrefed_type || this.peripheral_type)
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
return per;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
searchOneByType(pattern: string, derrefed_type?: string): Type {
|
|
205
|
+
let targetedPeripherals = this.searchByType(pattern, derrefed_type);
|
|
206
|
+
if (targetedPeripherals.length == 0) {
|
|
207
|
+
throw (
|
|
208
|
+
"Cannot find peripheral " +
|
|
209
|
+
(derrefed_type || this.peripheral_type)
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
if (targetedPeripherals.length > 1) {
|
|
213
|
+
const names = targetedPeripherals
|
|
214
|
+
.map((p) => peripheral.getName(p))
|
|
215
|
+
.join(";");
|
|
216
|
+
throw (
|
|
217
|
+
"Too many peripherals was found " +
|
|
218
|
+
(derrefed_type || this.peripheral_type) +
|
|
219
|
+
" even with pattern " +
|
|
220
|
+
pattern +
|
|
221
|
+
" this peripherals was found: " +
|
|
222
|
+
names
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
return targetedPeripherals[0];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
collect(derrefed_type?: string): Array<Type> {
|
|
229
|
+
let seached_type =
|
|
230
|
+
derrefed_type == null ? this.peripheral_type : derrefed_type;
|
|
231
|
+
const any_peripherals = peripheral.find(seached_type);
|
|
232
|
+
if (any_peripherals.length == 0) return [];
|
|
233
|
+
return any_peripherals.map((v) => v as Type);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
wrapValidate(name: string): Type | null {
|
|
237
|
+
const candidate = peripheral.wrap(name) as Type;
|
|
238
|
+
if (candidate == null) return null;
|
|
239
|
+
const types = peripheral.getType(candidate);
|
|
240
|
+
for (let type of types) {
|
|
241
|
+
if (type == this.peripheral_type) return candidate;
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
wrap(name: string): Type | null;
|
|
247
|
+
wrap(name: string[]): Type[];
|
|
248
|
+
|
|
249
|
+
wrap(name: string | string[]): Type | null | Type[] {
|
|
250
|
+
if (type(name) == "table") {
|
|
251
|
+
const periphs = [];
|
|
252
|
+
for (let single_name of name as string[]) {
|
|
253
|
+
const candidate = peripheral.wrap(single_name) as Type;
|
|
254
|
+
if (candidate != null) periphs.push(candidate);
|
|
255
|
+
}
|
|
256
|
+
return periphs;
|
|
257
|
+
}
|
|
258
|
+
const wrapped = peripheral.wrap(name as string);
|
|
259
|
+
if (wrapped == null) return null;
|
|
260
|
+
return wrapped as Type;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
local function __TS__Class(self)
|
|
2
|
+
local c = {prototype = {}}
|
|
3
|
+
c.prototype.__index = c.prototype
|
|
4
|
+
c.prototype.constructor = c
|
|
5
|
+
return c
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
local function __TS__ClassExtends(target, base)
|
|
9
|
+
target.____super = base
|
|
10
|
+
local staticMetatable = setmetatable({__index = base}, base)
|
|
11
|
+
setmetatable(target, staticMetatable)
|
|
12
|
+
local baseMetatable = getmetatable(base)
|
|
13
|
+
if baseMetatable then
|
|
14
|
+
if type(baseMetatable.__index) == "function" then
|
|
15
|
+
staticMetatable.__index = baseMetatable.__index
|
|
16
|
+
end
|
|
17
|
+
if type(baseMetatable.__newindex) == "function" then
|
|
18
|
+
staticMetatable.__newindex = baseMetatable.__newindex
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
setmetatable(target.prototype, base.prototype)
|
|
22
|
+
if type(base.prototype.__index) == "function" then
|
|
23
|
+
target.prototype.__index = base.prototype.__index
|
|
24
|
+
end
|
|
25
|
+
if type(base.prototype.__newindex) == "function" then
|
|
26
|
+
target.prototype.__newindex = base.prototype.__newindex
|
|
27
|
+
end
|
|
28
|
+
if type(base.prototype.__tostring) == "function" then
|
|
29
|
+
target.prototype.__tostring = base.prototype.__tostring
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
local function __TS__ArrayFind(self, predicate, thisArg)
|
|
34
|
+
for i = 1, #self do
|
|
35
|
+
local elem = self[i]
|
|
36
|
+
if predicate(thisArg, elem, i - 1, self) then
|
|
37
|
+
return elem
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
return nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
local function __TS__ArrayFilter(self, callbackfn, thisArg)
|
|
44
|
+
local result = {}
|
|
45
|
+
local len = 0
|
|
46
|
+
for i = 1, #self do
|
|
47
|
+
if callbackfn(thisArg, self[i], i - 1, self) then
|
|
48
|
+
len = len + 1
|
|
49
|
+
result[len] = self[i]
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
return result
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
local function __TS__ArrayMap(self, callbackfn, thisArg)
|
|
56
|
+
local result = {}
|
|
57
|
+
for i = 1, #self do
|
|
58
|
+
result[i] = callbackfn(thisArg, self[i], i - 1, self)
|
|
59
|
+
end
|
|
60
|
+
return result
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
local function __TS__ArrayPushArray(self, items)
|
|
64
|
+
local len = #self
|
|
65
|
+
for i = 1, #items do
|
|
66
|
+
len = len + 1
|
|
67
|
+
self[len] = items[i]
|
|
68
|
+
end
|
|
69
|
+
return len
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
__TS__Class = __TS__Class,
|
|
74
|
+
__TS__ClassExtends = __TS__ClassExtends,
|
|
75
|
+
__TS__ArrayFind = __TS__ArrayFind,
|
|
76
|
+
__TS__ArrayFilter = __TS__ArrayFilter,
|
|
77
|
+
__TS__ArrayMap = __TS__ArrayMap,
|
|
78
|
+
__TS__ArrayPushArray = __TS__ArrayPushArray
|
|
79
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@siredvin/typed-peripheral-base",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Typed peripheral library for my CC:T related projects",
|
|
5
|
+
"files": [
|
|
6
|
+
"./*.ts",
|
|
7
|
+
"./*.lua"
|
|
8
|
+
],
|
|
9
|
+
"author": "SirEdvin",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
13
|
+
"build": "tstl",
|
|
14
|
+
"clean": "rm -f *.lua",
|
|
15
|
+
"lint": "eslint . --ext .ts,.js",
|
|
16
|
+
"depcheck": "depcheck"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@siredvin/cc-types": "1.1.0",
|
|
20
|
+
"@siredvin/craftos-types": "1.2.0",
|
|
21
|
+
"@siredvin/api-types": "1.1.0",
|
|
22
|
+
"@jackmacwindows/lua-types": "^2.13.1",
|
|
23
|
+
"typescript-to-lua": "*",
|
|
24
|
+
"typescript": "*",
|
|
25
|
+
"@siredvin/cc-events": "0.2.0"
|
|
26
|
+
},
|
|
27
|
+
"nx": {
|
|
28
|
+
"targets": {
|
|
29
|
+
"lint": {
|
|
30
|
+
"executor": "@nx/linter:eslint",
|
|
31
|
+
"outputs": [
|
|
32
|
+
"{options.outputFile}"
|
|
33
|
+
],
|
|
34
|
+
"options": {
|
|
35
|
+
"lintFilePatterns": [
|
|
36
|
+
"packages/typed-peripheral/**/*.{ts,tsx,js,jsx}"
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"depcheck": {
|
|
41
|
+
"executor": "nx:run-commands",
|
|
42
|
+
"options": {
|
|
43
|
+
"command": "depcheck",
|
|
44
|
+
"cwd": "packages/typed-peripheral"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|