-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
207 lines (206 loc) · 8.74 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @noResolution
*/
declare module "binser"
{
/**
* Serialize (almost) any Lua data into a Lua string.
* Numbers, strings, tables, booleans, and nil are all fully supported by default.
* Custom userdata and custom types, both identified by metatables, can also be supported by specifying a custom serialization function.
* Unserializable data should throw an error. Aliased to `binser.s`.
*
* @param data Data to serialize
* @returns Serialized data [string]
*/
function serialize(this: void, ...data: any[]): string;
/**
* (Alias to `binser.serialize`)
*
* Serialize (almost) any Lua data into a Lua string.
* Numbers, strings, tables, booleans, and nil are all fully supported by default.
* Custom userdata and custom types, both identified by metatables, can also be supported by specifying a custom serialization function.
* Unserializable data should throw an error. Aliased to `binser.s`.
*
* @param data Data to serialize
* @returns Serialized data [string]
*/
function s(this: void, ...data: any[]): string;
/**
* Deserialize any string previously serialized by binser.
* Unrecognized data should throw an error.
* Results is a list of length `len`.
* Aliased to `binser.d`.
*
* @param str Data to deserialize
* @param index Deserializing will start from this position. Can be used to drop leading characters. Default is 0.
* @returns Tuple `results, len`. Where `results` are deserialized values and `len` is number of deserialized values.
*/
function deserialize(this: void, str: string, index?: number): LuaMultiReturn<[any[], number]>;
/**
* (Alias to `binser.deserialize`)
*
* Deserialize any string previously serialized by binser.
* Unrecognized data should throw an error.
* Results is a list of length `len`.
* Aliased to `binser.d`.
*
* @param str Data to deserialize
* @param index Deserializing will start from this position. Can be used to drop leading characters. Default is 0.
* @returns Tuple `results, len`. Where `results` are deserialized values and `len` is number of deserialized values.
*/
function d(this: void, str: string, index?: number): LuaMultiReturn<[any[], number]>;
/**
* Deserializes at most `n` values from str.
* The default value for `n` is one, so binser.deserializeN(str) will deserialize exactly one value from string, and ignore the rest of the string.
* Can optionally start at a given index, which is 0 by default. Aliased to `binser.dn`.
*
* @param str
* @param n Number of values to deserialize. Default is 1.
* @param index Deserializing will start from this position. Can be used to drop leading characters. Default is 0.
* @returns Deserialized values
*/
function deserializeN(this: void, str: string, n?: number, index?: number): LuaMultiReturn<any[]>;
/**
* (Alias to `binser.deserializeN`)
*
* Deserializes at most `n` values from `str`.
* The default value for `n` is one, so binser.deserializeN(str) will deserialize exactly one value from string, and ignore the rest of the string.
* Can optionally start at a given index, which is 0 by default. Aliased to `binser.deserializeN`.
*
* @param str
* @param n Number of values to deserialize. Default is 1.
* @param index Deserializing will start from this position. Can be used to drop leading characters. Default is 0.
* @returns Deserialized values
*/
function dn(this: void, str: string, n?: number, index?: number): LuaMultiReturn<any[]>;
type Class = Object;
/**
* Registers a custom type, identified by its metatable, to be serialized.
* Registering types has two main purposes.
* First, it allows custom serialization and deserialization for userdata and tables that contain userdata, which can't otherwise be serialized in a uniform way.
* Second, it allows efficient serialization of small tables with large metatables, as registered metatables are not serialized.
*
* @param metatable Metatable, which identifies the type
* @param name Type name used in serialization. The only requirement for names is that they are unique.
* @param serialize Function, which destructs the instance of the type. Can be omitted, if you're registering usual lua table, in that case, default serializer is used.
* @param deserialize Function, which constructs the instance of the type from arguments, returned by `serialize(object)` function. Can be omitted, if you're registering usual lua table, in that case, default deserializer is used.
*
* @returns `metatable`
*
* Usage:
* ```ts
* class Person
* {
* name: string;
* age: number;
*
* constructor(name: string, age: number)
* {
* this.name = name;
* this.age = age;
* }
* }
*
* ...
*
* import * as binser from "binser";
* binser.register<Person>(Person, "Person",
* (person: Person) => {
* return binser.serialize(person.name,person.age)
* },
* (serialized_data: string) => {
* let [name,age] = binser.deserializeN(serialized_data,2);
* return new Person(name, age);
* }
* )
*
* ```
*/
function register<T,SerializableArgs extends any[]>(this: void, metatable: Class, name: string, serialize?:(this: void, object: T) => LuaMultiReturn<SerializableArgs>, deserialize?: (this: void, ...args: SerializableArgs) => T): Class;
/**
* Explicitly unregister a type.
*
* @param classname Class name (same as string passed to `binser.register`) to unregister
* @returns Metatable, which was registered as `classname`
*/
function unregister(this: void, classname: string): Class;
/**
* Registers a resource.
* Resources are are certain objects that don't need to be serialized at all, like images, audio, or any system resource, binser can mark them as such to only serialize a reference to them.
*
* @param resource Resource to register
* @param name Unique name for a resource
* @returns Registered resource
*/
function registerResource<T>(this: void, resource: T, name: string): T;
/**
* Explicitly unregister a resource.
*
* @param name Resource name (same as string passed to `binser.register`) to unregister
* @returns Resource which was registered as `name`
*/
function unregisterResource(this: void, name: string): any;
/**
* Serializes Lua objects and writes them to a file. Overwrites the previous file.
*
* @param filepath Path to write serialized values.
* @param data Values to serialize.
*/
function writeFile(this: void, filepath: string, ...data: any[]): void;
/**
* Serializes Lua objects and writes them to a file. Doesn't overwrites the previous file.
*
* @param filepath Path to write serialized values.
* @param data Values to serialize.
*/
function appendFile(this: void, filepath: string, ...data: any[]): void;
/**
* Reads and deserialize a file.
*
* @param filepath File to deserialize.
* @returns [`results` - list of deserialized values, `len` - number of deserialized values]
*/
function readFile(this: void, filepath: string): LuaMultiReturn<[any[], number]>;
/**
* (Alias to `binser.writeFile`)
* Serializes Lua objects and writes them to a file. Overwrites the previous file.
*
* @param filepath Path to write serialized values.
* @param data Values to serialize.
*/
function w(this: void, filepath: string, ...data: any[]): void;
/**
* (Alias to `binser.appendFile`)
* Serializes Lua objects and writes them to a file. Doesn't overwrites the previous file.
*
* @param filepath Path to write serialized values.
* @param data Values to serialize.
*/
function a(this: void, filepath: string, ...data: any[]): void;
/**
* (Alias to `binser.readFile`)
* Reads and deserialize a file.
*
* @param filepath File to deserialize.
* @returns [`results` - list of deserialized values, `len` - number of deserialized values]
*/
function r(this: void, filepath: string): LuaMultiReturn<[any[], number]>;
/**
* Registers a class as a custom type
* @param _class class to register
* @param name defaults to `_class.name`
*
* Usage:
* ```ts
* class Vec2
* {
* constructor(public x: number, public y: number)
* {
* }
* }
*
* binser.registerClass(Vec2.prototype,Vec2.name)
* ```
*/
function registerClass(this: void, _class: Class, name?: string): Class;
}