@rbxts/replecs 0.0.1-rc.1 → 0.1.0-alpha2

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.
@@ -1,8 +1,8 @@
1
1
  --!native
2
2
  --!optimize 2
3
3
 
4
- local jecs = require(script.Parent.Parent.jecs)
5
- local common = require(script.Parent.common)
4
+ local jecs = require "../jecs"
5
+ local common = require "./common"
6
6
 
7
7
  type World = jecs.World
8
8
  type Entity<T = any> = jecs.Entity<T>
@@ -38,23 +38,56 @@ function cursor.from(buf: buffer): Cursor
38
38
  }
39
39
  end
40
40
 
41
- function cursor.tryalloc(cursor: Cursor, bytes: number)
42
- local buf = cursor.buffer
43
- local offset = cursor.offset
41
+ function cursor.tryalloc(c: Cursor, bytes: number)
42
+ local buf = c.buffer
43
+ local offset = c.offset
44
44
  local len = buffer.len(buf)
45
45
 
46
46
  if len < offset + bytes then
47
47
  local exponent = math.ceil(math.log((bytes + offset) / len, 1.5))
48
48
  local new = buffer.create(len * 1.5 ^ exponent)
49
49
  buffer.copy(new, 0, buf, 0)
50
- cursor.buffer = new
50
+ c.buffer = new
51
51
  end
52
52
  end
53
53
 
54
- function cursor.write_buffer(c: Cursor, buf: buffer)
55
- cursor.tryalloc(c, buffer.len(buf))
56
- buffer.copy(c.buffer, c.offset, buf, 0, buffer.len(buf))
57
- c.offset += buffer.len(buf)
54
+ function cursor.write_buffer(c: Cursor, buf: buffer, start: number?, count: number?)
55
+ if start == nil and count == nil then
56
+ cursor.tryalloc(c, buffer.len(buf))
57
+ buffer.copy(c.buffer, c.offset, buf, 0, buffer.len(buf))
58
+ c.offset += buffer.len(buf)
59
+ else
60
+ local len = buffer.len(buf)
61
+ local available = len - (start or 0)
62
+ local write_count = count and math.min(available, count) or available
63
+
64
+ cursor.tryalloc(c, write_count)
65
+ buffer.copy(c.buffer, c.offset, buf, start or 0, write_count)
66
+ c.offset += write_count
67
+ end
68
+ end
69
+
70
+ function cursor.print(c: Cursor)
71
+ local bytes = { string.byte(buffer.tostring(c.buffer), 1, -1) }
72
+ local offset = 7
73
+ for i = 1, c.offset do
74
+ local byte = bytes[i]
75
+ local digits = if byte == 0 then 1 else math.ceil(math.log10(1 + byte))
76
+ offset += digits + 1
77
+ end
78
+
79
+ local last_byte = bytes[c.offset + 1]
80
+ if last_byte then
81
+ local lastDigits = if last_byte == 0 then 1 else math.ceil(math.log10(1 + last_byte))
82
+ offset += lastDigits // 2
83
+ end
84
+ bytes[c.offset + 1] = `[{bytes[c.offset + 1] :: number}]` :: any
85
+
86
+ if #bytes == 0 or c.offset == buffer.len(c.buffer) then
87
+ table.insert(bytes, " " :: any)
88
+ end
89
+
90
+ return `Pos: {c.offset} / {buffer.len(c.buffer)}\nBuf: \{ {table.concat(bytes, " ")} \}`
58
91
  end
59
92
 
60
93
  function cursor.writeu8(c: Cursor, value: number)
@@ -81,6 +114,13 @@ function cursor.writeu32(c: Cursor, value: number)
81
114
  c.offset += 4
82
115
  end
83
116
 
117
+ function cursor.writeu40(c: Cursor, value: number)
118
+ cursor.tryalloc(c, 5)
119
+ buffer.writeu8(c.buffer, c.offset, value)
120
+ buffer.writeu32(c.buffer, c.offset + 1, value // 256)
121
+ c.offset += 5
122
+ end
123
+
84
124
  function cursor.writei8(c: Cursor, value: number)
85
125
  cursor.tryalloc(c, 1)
86
126
  buffer.writei8(c.buffer, c.offset, value)
@@ -122,11 +162,19 @@ function cursor.readu24(c: Cursor): number
122
162
  local two = buffer.readu16(c.buffer, c.offset + 1)
123
163
  return one + two * 256
124
164
  end
165
+
125
166
  function cursor.readu32(c: Cursor): number
126
167
  c.offset -= 4
127
168
  return buffer.readu32(c.buffer, c.offset)
128
169
  end
129
170
 
171
+ function cursor.readu40(c: Cursor): number
172
+ c.offset -= 5
173
+ local one = buffer.readu8(c.buffer, c.offset)
174
+ local two = buffer.readu32(c.buffer, c.offset + 1)
175
+ return one + two * 256
176
+ end
177
+
130
178
  function cursor.readi8(c: Cursor): number
131
179
  c.offset -= 1
132
180
  return buffer.readi8(c.buffer, c.offset)
@@ -197,6 +245,11 @@ function cursor.close(c: Cursor): buffer
197
245
  return final
198
246
  end
199
247
 
248
+ function utils.print_buffer(buf: buffer)
249
+ local bytes = { string.byte(buffer.tostring(buf), 1, -1) }
250
+ return `\{ {buffer.len(buf)}\n Buf: \{ {table.concat(bytes, " ")} \}`
251
+ end
252
+
200
253
  utils.cursor = cursor
201
254
 
202
255
  local bitmask = {}
@@ -507,7 +560,9 @@ utils.bitmask = {
507
560
 
508
561
  function utils.create_shared_lookup(world: World, components: common.Components)
509
562
  local shared: common.Shared = {
510
- lookup = {},
563
+ lookup = {
564
+ ["jecs.ChildOf"] = CHILD_OF,
565
+ },
511
566
  serdes = {},
512
567
  bytespan = {},
513
568
  ids = {},
@@ -541,8 +596,6 @@ function utils.create_shared_lookup(world: World, components: common.Components)
541
596
  id_components[id] = component
542
597
  end
543
598
 
544
- component_ids[CHILD_OF] = #keys + 1
545
-
546
599
  shared.ids = component_ids
547
600
  shared.components = id_components
548
601
  return shared
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rbxts/replecs",
3
- "version": "0.0.1-rc.1",
3
+ "version": "0.1.0-alpha2",
4
4
  "description": "Replication library for Jecs",
5
5
  "main": "out/init.lua",
6
6
  "types": "out/index.d.ts",