Skip to content

Commit 275872f

Browse files
authored
Support newer tile configs (#22)
* add the Vec2 class from arc The class does not have any methods because it is only meant to be used as a different type from Point2 via instanceof * update the type of the schematic tile config * support the new schematic tile config values * update the changelog
1 parent eba1372 commit 275872f

File tree

6 files changed

+79
-31
lines changed

6 files changed

+79
-31
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Added
11+
12+
- Added support for decoding more types of schematic tile configuration.
13+
814
## [4.2.2] - 2023-07-18
915

1016
### Fixed
@@ -290,6 +296,7 @@ const {
290296

291297
- First release
292298

299+
[Unreleased]: https://github.com/JeanJPNM/mindustry-schematic-parser/compare/v4.2.2...HEAD
293300
[4.2.2]: https://github.com/JeanJPNM/mindustry-schematic-parser/compare/v4.2.1...v4.2.2
294301
[4.2.1]: https://github.com/JeanJPNM/mindustry-schematic-parser/compare/v4.2.0...v4.2.1
295302
[4.2.0]: https://github.com/JeanJPNM/mindustry-schematic-parser/compare/v4.1.7...v4.2.0

src/arc/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './point2'
2+
export * from './vec2'

src/arc/vec2.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class Vec2 {
2+
constructor(
3+
public x: number,
4+
public y: number
5+
) {}
6+
}

src/schematic/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { Schematic } from './schematic'
22
export { SchematicTile } from './tile'
3+
export type { SchematicTileConfig } from './tile'
34
export type MindustryVersion = 'v5' | 'v6' | 'v7'

src/schematic/io.ts

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as Pako from 'pako'
22
import { Block, Blocks, Liquid, UnitCommand } from '../mindustry'
3+
import { Point2, Vec2 } from '../arc'
4+
import { SchematicTile, SchematicTileConfig } from './tile'
35
import { StreamedDataReader, StreamedDataWriter } from '../streamed_data'
46
import { Item } from '../mindustry/item'
57
import { MindustryVersion } from '.'
6-
import { Point2 } from '../arc'
78
import { Schematic } from './schematic'
8-
import { SchematicTile } from './tile'
99

1010
const {
1111
distribution: {
@@ -87,7 +87,7 @@ function mapConfig(block: Block, value: number, position: number) {
8787
return null
8888
}
8989

90-
function readConfigObject(cData: StreamedDataReader) {
90+
function readConfigObject(cData: StreamedDataReader): SchematicTileConfig {
9191
const type = cData.getInt8()
9292
switch (type) {
9393
case 0:
@@ -128,11 +128,7 @@ function readConfigObject(cData: StreamedDataReader) {
128128
// return content.getByID(ContentType.all[read.b()], read.s());
129129
case 6: {
130130
const length = cData.getInt16()
131-
const arr = []
132-
for (let i = 0; i < length; i++) {
133-
arr.push(cData.getInt32())
134-
}
135-
return arr
131+
return readArray(length, () => cData.getInt32())
136132
}
137133

138134
// original code
@@ -141,20 +137,15 @@ function readConfigObject(cData: StreamedDataReader) {
141137
return new Point2(cData.getInt32(), cData.getInt32())
142138
case 8: {
143139
const len = cData.getInt8()
144-
const out = []
145-
for (let i = 0; i < len; i++) {
146-
out.push(Point2.unpack(cData.getInt32()))
147-
}
148-
// byte len = read.b(); Point2[] out = new Point2[len]; for (int i = 0; i < len; i++) out[i] = Point2.unpack(read.i());
149-
return out
140+
return readArray(len, () => Point2.unpack(cData.getInt32()))
150141
}
151142

152143
// TODO: somehow implement java code bellow
153144
case 9:
154145
// by now just ignore the config data
155146
cData.getInt8()
156147
cData.getInt16()
157-
break
148+
return
158149
// return TechTree.getNotNull(content.getByID(ContentType.all[read.b()], read.s()));
159150
case 10:
160151
return cData.getBool()
@@ -172,19 +163,59 @@ function readConfigObject(cData: StreamedDataReader) {
172163
// return LAccess.all[read.s()];
173164
case 14: {
174165
const blen = cData.getInt32()
175-
const bytes = []
176-
for (let i = 0; i < blen; i++) bytes.push(cData.getInt8())
177-
return bytes
166+
return readArray(blen, () => cData.getInt8())
178167
}
179168
// int blen = read.i(); byte[] bytes = new byte[blen]; read.b(bytes); return bytes;
180169
case 15:
181170
return UnitCommand[cData.getInt8()]
171+
case 16: {
172+
const len = cData.getInt32()
173+
return readArray(len, () => cData.getBool())
174+
}
175+
case 17: {
176+
cData.getInt32()
177+
return
178+
}
179+
case 18: {
180+
const len = cData.getInt16()
181+
return readArray(
182+
len,
183+
() => new Vec2(cData.getFloat32(), cData.getFloat32())
184+
)
185+
}
186+
case 19:
187+
return new Vec2(cData.getFloat32(), cData.getFloat32())
188+
case 20: {
189+
cData.getUint8()
190+
return
191+
}
192+
case 21: {
193+
const len = cData.getInt16()
194+
return readArray(len, () => cData.getInt32())
195+
}
196+
case 22: {
197+
const len = cData.getInt32()
198+
return readArray(len, () => readConfigObject(cData))
199+
}
200+
case 23: {
201+
cData.getUint16()
202+
return
203+
}
182204
default:
183205
throw new Error('Unknown object type: ' + type)
184206
// throw new IllegalArgumentException('Unknown object type: ' + type)
185207
}
186208
}
187209

210+
function readArray<T>(length: number, fn: () => T) {
211+
const result = new Array<T>(length)
212+
213+
for (let i = 0; i < length; i++) {
214+
result[i] = fn()
215+
}
216+
return result
217+
}
218+
188219
function decodeTiles(
189220
cData: StreamedDataReader,
190221
blocks: Block[],

src/schematic/tile.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import { Block, Item, Liquid } from '../mindustry'
2-
import { Point2 } from '../arc'
2+
import { Point2, Vec2 } from '../arc'
3+
4+
export type SchematicTileConfig =
5+
| Liquid
6+
| Item
7+
| Point2
8+
| Vec2
9+
| null
10+
| undefined
11+
| number
12+
| bigint
13+
| string
14+
| boolean
15+
| SchematicTileConfig[]
316

417
export class SchematicTile {
518
constructor(
@@ -18,18 +31,7 @@ export class SchematicTile {
1831
/**
1932
* The configuration of this tile (varies according to the block), may be `undefined` or `null`
2033
*/
21-
public config:
22-
| string
23-
| number
24-
| bigint
25-
| boolean
26-
| number[]
27-
| Point2
28-
| Point2[]
29-
| Item
30-
| Liquid
31-
| null
32-
| undefined,
34+
public config: SchematicTileConfig,
3335
/**
3436
* The rotation of this tile
3537
*/

0 commit comments

Comments
 (0)