Adding modding folder with a hacked build.

This commit is contained in:
ml
2026-09-26 08:53:08 +02:00
parent cdb960dc71
commit c91080aba7
755 changed files with 50424 additions and 0 deletions
@@ -0,0 +1,8 @@
package voc
type blockType byte
const (
terminator = blockType(0x00)
soundData = blockType(0x01)
)
@@ -0,0 +1,113 @@
package voc
import (
"encoding/binary"
"io"
"github.com/inkyblackness/hacked/ss1"
"github.com/inkyblackness/hacked/ss1/content/audio"
)
const (
errSourceIsNil ss1.StringError = "source is nil"
errNoAudioFound ss1.StringError = "no audio found"
errInvalidVersionValidity ss1.StringError = "version validity check failed"
)
// Load reads from the provided source a Creative Voice Sound and returns the data.
func Load(source io.Reader) (data audio.L8, err error) {
if source == nil {
return data, errSourceIsNil
}
err = readAndVerifyHeader(source)
if err != nil {
return
}
return readSoundData(source)
}
func readAndVerifyHeader(source io.Reader) error {
start := make([]byte, len(FileHeader))
headerSize := uint16(0)
version := uint16(0)
versionValidity := uint16(0)
_, err := source.Read(start)
if err != nil {
return err
}
err = binary.Read(source, binary.LittleEndian, &headerSize)
if err != nil {
return err
}
err = binary.Read(source, binary.LittleEndian, &version)
if err != nil {
return err
}
err = binary.Read(source, binary.LittleEndian, &versionValidity)
if err != nil {
return err
}
calculated := ^version + versionCheckValue
if calculated != versionValidity {
return errInvalidVersionValidity
}
skip := make([]byte, headerSize-standardHeaderSize)
_, err = source.Read(skip)
return err
}
func readSoundData(source io.Reader) (data audio.L8, err error) {
sampleRate := float32(0.0)
var samples []byte
done := false
for !done {
blockStart := make([]byte, 4)
_, err = source.Read(blockStart)
if err != nil {
return
}
switch blockType(blockStart[0]) {
case soundData:
{
meta := make([]byte, 2)
_, err = source.Read(meta)
if err != nil {
return
}
sampleRate = divisorToSampleRate(meta[0])
newCount := lengthFromBlockStart(blockStart) - len(meta)
buf := make([]byte, newCount)
_, err = source.Read(buf)
if err != nil {
return
}
oldCount := len(samples)
newSamples := make([]byte, oldCount+newCount)
copy(newSamples, samples)
copy(newSamples[oldCount:], buf)
samples = newSamples
}
case terminator:
{
done = true
}
}
}
if len(samples) == 0 {
return data, errNoAudioFound
}
return audio.L8{
SampleRate: sampleRate,
Samples: samples,
}, nil
}
@@ -0,0 +1,107 @@
package voc_test
import (
"bytes"
"encoding/binary"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/inkyblackness/hacked/ss1/content/audio/voc"
)
func TestLoadReturnsErrorOnNil(t *testing.T) {
_, err := voc.Load(nil)
assert.Errorf(t, err, "source is nil")
}
func newHeader() *bytes.Buffer {
writer := bytes.NewBufferString(voc.FileHeader)
version := uint16(0x010A)
headerSize := uint16(0x001A)
_ = binary.Write(writer, binary.LittleEndian, headerSize)
_ = binary.Write(writer, binary.LittleEndian, version)
versionValidity := ^version + uint16(0x1234)
_ = binary.Write(writer, binary.LittleEndian, versionValidity)
return writer
}
func TestLoadReturnsErrorOnInvalidVersion(t *testing.T) {
writer := newHeader()
writer.Write([]byte{0x00}) // Terminator
data := writer.Bytes()
data[24] = 0x00
source := bytes.NewReader(data)
_, err := voc.Load(source)
assert.Errorf(t, err, "Version validity failed: 0x1129 != 0x1100")
}
func TestLoadReturnsErrorOnValidButEmptyFile(t *testing.T) {
writer := newHeader()
writer.Write([]byte{0x00}) // Terminator
source := bytes.NewReader(writer.Bytes())
_, err := voc.Load(source)
assert.Errorf(t, err, "No audio found")
}
func TestLoadReturnsSoundDataOnSampleData(t *testing.T) {
writer := newHeader()
writer.Write([]byte{0x01}) // sound data
writer.Write([]byte{0x03, 0x00, 0x00}) // block size
writer.Write([]byte{0x64, 0x00}) // divisor, sound type
writer.Write([]byte{0x80}) // one sample
writer.Write([]byte{0x00}) // Terminator
source := bytes.NewReader(writer.Bytes())
data, err := voc.Load(source)
require.Nil(t, err)
assert.NotNil(t, data)
}
func TestLoadReturnsSoundDataWithSampleRate(t *testing.T) {
writer := newHeader()
writer.Write([]byte{0x01}) // sound data
writer.Write([]byte{0x03, 0x00, 0x00}) // block size
writer.Write([]byte{0x9C, 0x00}) // divisor, sound type
writer.Write([]byte{0x80}) // one sample
writer.Write([]byte{0x00}) // Terminator
source := bytes.NewReader(writer.Bytes())
data, err := voc.Load(source)
require.Nil(t, err)
assert.Equal(t, float32(10000.0), data.SampleRate)
}
func TestLoadReturnsSoundDataWithSamples(t *testing.T) {
writer := newHeader()
samples := []byte{0x80, 0xFF, 0x00, 0xC0, 0x40, 0x7F, 0x81}
writer.Write([]byte{0x01}) // sound data
writer.Write([]byte{0x09, 0x00, 0x00}) // block size
writer.Write([]byte{0x9C, 0x00}) // divisor, sound type
writer.Write(samples) // samples
writer.Write([]byte{0x00}) // Terminator
source := bytes.NewReader(writer.Bytes())
data, err := voc.Load(source)
require.Nil(t, err)
assert.Equal(t, samples, data.Samples)
}
@@ -0,0 +1,64 @@
package voc
import (
"bytes"
"encoding/binary"
"io"
)
// Save encodes the provided samples into the given writer.
func Save(writer io.Writer, sampleRate float32, samples []byte) error {
err := writeHeader(writer)
if err != nil {
return err
}
err = writeBasicSoundData(writer, sampleRate, samples)
if err != nil {
return err
}
err = writeEndOfFile(writer)
return err
}
func writeHeader(writer io.Writer) error {
version := baseVersion
_, err := writer.Write(bytes.NewBufferString(FileHeader).Bytes())
if err != nil {
return err
}
err = binary.Write(writer, binary.LittleEndian, standardHeaderSize)
if err != nil {
return err
}
err = binary.Write(writer, binary.LittleEndian, baseVersion)
if err != nil {
return err
}
return binary.Write(writer, binary.LittleEndian, (^version)+versionCheckValue)
}
func writeBlockHeader(writer io.Writer, block blockType, dataBytes int) error {
_, err := writer.Write([]byte{byte(block), byte(dataBytes), byte(dataBytes >> 8), byte(dataBytes >> 16)})
return err
}
func writeBasicSoundData(writer io.Writer, sampleRate float32, samples []byte) error {
sampleType := byte(0)
err := writeBlockHeader(writer, soundData, len(samples)+2)
if err != nil {
return err
}
_, err = writer.Write([]byte{sampleRateToDivisor(sampleRate), sampleType})
if err != nil {
return err
}
_, err = writer.Write(samples)
return err
}
func writeEndOfFile(writer io.Writer) error {
_, err := writer.Write([]byte{byte(terminator)})
return err
}
@@ -0,0 +1,22 @@
package voc
const (
// FileHeader identifies VOC files.
FileHeader string = "Creative Voice File\u001A"
standardHeaderSize uint16 = 0x1A
baseVersion uint16 = 0x010A
versionCheckValue uint16 = 0x1234
rateBase float32 = 1000000.0
)
func lengthFromBlockStart(blockStart []byte) int {
return int(blockStart[3])<<16 + int(blockStart[2])<<8 + int(blockStart[1])
}
func divisorToSampleRate(divisor byte) float32 {
return rateBase / float32(256-int(divisor))
}
func sampleRateToDivisor(sampleRate float32) byte {
return byte(256 - int(rateBase/sampleRate))
}
@@ -0,0 +1,23 @@
package voc // nolint: testpackage
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFrequencyDivisorFor11111(t *testing.T) {
divisor := byte(0xA6)
sampleRate := divisorToSampleRate(divisor)
result := sampleRateToDivisor(sampleRate)
assert.Equal(t, divisor, result)
}
func TestFrequencyDivisorFor222222(t *testing.T) {
divisor := byte(0x2D)
sampleRate := divisorToSampleRate(divisor)
result := sampleRateToDivisor(sampleRate)
assert.Equal(t, divisor, result)
}