Skip to content

Commit

Permalink
Support newer tile configs (#22)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JeanJPNM committed Aug 12, 2023
1 parent eba1372 commit 275872f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 31 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Added support for decoding more types of schematic tile configuration.

## [4.2.2] - 2023-07-18

### Fixed
Expand Down Expand Up @@ -290,6 +296,7 @@ const {

- First release

[Unreleased]: https://github.com/JeanJPNM/mindustry-schematic-parser/compare/v4.2.2...HEAD
[4.2.2]: https://github.com/JeanJPNM/mindustry-schematic-parser/compare/v4.2.1...v4.2.2
[4.2.1]: https://github.com/JeanJPNM/mindustry-schematic-parser/compare/v4.2.0...v4.2.1
[4.2.0]: https://github.com/JeanJPNM/mindustry-schematic-parser/compare/v4.1.7...v4.2.0
Expand Down
1 change: 1 addition & 0 deletions src/arc/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './point2'
export * from './vec2'
6 changes: 6 additions & 0 deletions src/arc/vec2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class Vec2 {
constructor(
public x: number,
public y: number
) {}
}
1 change: 1 addition & 0 deletions src/schematic/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Schematic } from './schematic'
export { SchematicTile } from './tile'
export type { SchematicTileConfig } from './tile'
export type MindustryVersion = 'v5' | 'v6' | 'v7'
67 changes: 49 additions & 18 deletions src/schematic/io.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as Pako from 'pako'
import { Block, Blocks, Liquid, UnitCommand } from '../mindustry'
import { Point2, Vec2 } from '../arc'
import { SchematicTile, SchematicTileConfig } from './tile'
import { StreamedDataReader, StreamedDataWriter } from '../streamed_data'
import { Item } from '../mindustry/item'
import { MindustryVersion } from '.'
import { Point2 } from '../arc'
import { Schematic } from './schematic'
import { SchematicTile } from './tile'

const {
distribution: {
Expand Down Expand Up @@ -87,7 +87,7 @@ function mapConfig(block: Block, value: number, position: number) {
return null
}

function readConfigObject(cData: StreamedDataReader) {
function readConfigObject(cData: StreamedDataReader): SchematicTileConfig {
const type = cData.getInt8()
switch (type) {
case 0:
Expand Down Expand Up @@ -128,11 +128,7 @@ function readConfigObject(cData: StreamedDataReader) {
// return content.getByID(ContentType.all[read.b()], read.s());
case 6: {
const length = cData.getInt16()
const arr = []
for (let i = 0; i < length; i++) {
arr.push(cData.getInt32())
}
return arr
return readArray(length, () => cData.getInt32())
}

// original code
Expand All @@ -141,20 +137,15 @@ function readConfigObject(cData: StreamedDataReader) {
return new Point2(cData.getInt32(), cData.getInt32())
case 8: {
const len = cData.getInt8()
const out = []
for (let i = 0; i < len; i++) {
out.push(Point2.unpack(cData.getInt32()))
}
// byte len = read.b(); Point2[] out = new Point2[len]; for (int i = 0; i < len; i++) out[i] = Point2.unpack(read.i());
return out
return readArray(len, () => Point2.unpack(cData.getInt32()))
}

// TODO: somehow implement java code bellow
case 9:
// by now just ignore the config data
cData.getInt8()
cData.getInt16()
break
return
// return TechTree.getNotNull(content.getByID(ContentType.all[read.b()], read.s()));
case 10:
return cData.getBool()
Expand All @@ -172,19 +163,59 @@ function readConfigObject(cData: StreamedDataReader) {
// return LAccess.all[read.s()];
case 14: {
const blen = cData.getInt32()
const bytes = []
for (let i = 0; i < blen; i++) bytes.push(cData.getInt8())
return bytes
return readArray(blen, () => cData.getInt8())
}
// int blen = read.i(); byte[] bytes = new byte[blen]; read.b(bytes); return bytes;
case 15:
return UnitCommand[cData.getInt8()]
case 16: {
const len = cData.getInt32()
return readArray(len, () => cData.getBool())
}
case 17: {
cData.getInt32()
return
}
case 18: {
const len = cData.getInt16()
return readArray(
len,
() => new Vec2(cData.getFloat32(), cData.getFloat32())
)
}
case 19:
return new Vec2(cData.getFloat32(), cData.getFloat32())
case 20: {
cData.getUint8()
return
}
case 21: {
const len = cData.getInt16()
return readArray(len, () => cData.getInt32())
}
case 22: {
const len = cData.getInt32()
return readArray(len, () => readConfigObject(cData))
}
case 23: {
cData.getUint16()
return
}
default:
throw new Error('Unknown object type: ' + type)
// throw new IllegalArgumentException('Unknown object type: ' + type)
}
}

function readArray<T>(length: number, fn: () => T) {
const result = new Array<T>(length)

for (let i = 0; i < length; i++) {
result[i] = fn()
}
return result
}

function decodeTiles(
cData: StreamedDataReader,
blocks: Block[],
Expand Down
28 changes: 15 additions & 13 deletions src/schematic/tile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { Block, Item, Liquid } from '../mindustry'
import { Point2 } from '../arc'
import { Point2, Vec2 } from '../arc'

export type SchematicTileConfig =
| Liquid
| Item
| Point2
| Vec2
| null
| undefined
| number
| bigint
| string
| boolean
| SchematicTileConfig[]

export class SchematicTile {
constructor(
Expand All @@ -18,18 +31,7 @@ export class SchematicTile {
/**
* The configuration of this tile (varies according to the block), may be `undefined` or `null`
*/
public config:
| string
| number
| bigint
| boolean
| number[]
| Point2
| Point2[]
| Item
| Liquid
| null
| undefined,
public config: SchematicTileConfig,
/**
* The rotation of this tile
*/
Expand Down

0 comments on commit 275872f

Please sign in to comment.