Making leaps to better data serialization
This commit is contained in:
BIN
tests/test
Executable file
BIN
tests/test
Executable file
Binary file not shown.
120
tests/test.odin
120
tests/test.odin
@@ -1,47 +1,91 @@
|
||||
package test
|
||||
|
||||
import game "../game"
|
||||
import "core:fmt"
|
||||
import rl "vendor:raylib"
|
||||
import "core:encoding/endian"
|
||||
import "core:os"
|
||||
import "core:slice"
|
||||
|
||||
my_enum :: enum {
|
||||
val1,val2,
|
||||
}
|
||||
|
||||
my_struct :: struct {
|
||||
me: my_enum,
|
||||
num: i32,
|
||||
v: Vec2i
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
final_val:my_struct
|
||||
|
||||
buf : [128]u8
|
||||
my_int : i32 = -32
|
||||
make_struct()
|
||||
|
||||
endian.put_i32(buf[:], .Little, my_int)
|
||||
data := load_file("test1")
|
||||
if data != nil {
|
||||
if len(data) == size_of(my_struct) {
|
||||
static_bytes: [size_of(my_struct)]u8
|
||||
copy(static_bytes[:], data)
|
||||
final_val = from_bytes(my_struct, static_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.printfln("u8 buffer: %v", buf)
|
||||
|
||||
decoded_int, err := endian.get_i32(buf[:], .Little)
|
||||
|
||||
fmt.printfln("Pre-encoding: %v | Decoded int: %v", my_int, decoded_int)
|
||||
|
||||
// nothing := game.Tile {
|
||||
// type = .NOTHING,
|
||||
// color = rl.RED,
|
||||
// tilemap_pos = {3,3},
|
||||
// resource = .NOTHING,
|
||||
// interaction = .NOTHING,
|
||||
// }
|
||||
|
||||
// ts := game.serialize_tile(nothing)
|
||||
|
||||
// fmt.printfln("Expecting size: %v, Got size: %v", size_of(game.Tile), len(ts))
|
||||
// fmt.printfln("Pre-serialized Tile: %v", nothing)
|
||||
|
||||
// dst := game.deserialize_tile(ts)
|
||||
// fmt.printfln("Post-serialized tile: %v", dst)
|
||||
|
||||
// y : int
|
||||
// chunk := game.Chunk {
|
||||
// position = {0,1}
|
||||
// }
|
||||
|
||||
// for x in 0..<len(chunk.tiles) {
|
||||
// for y in 0..<len(chunk.tiles) {
|
||||
// chunk.tiles[x][y] = nothing
|
||||
// }
|
||||
// }
|
||||
fmt.printfln("Decoded Val: %v", final_val)
|
||||
}
|
||||
|
||||
make_struct :: proc() {
|
||||
ms := my_struct {
|
||||
me = .val1,
|
||||
num = 345,
|
||||
v = {45,75}
|
||||
}
|
||||
|
||||
fmt.printfln("Initial value: %v", ms)
|
||||
|
||||
bytes := to_bytes(ms)
|
||||
|
||||
fmt.printfln("Bytes: %v", bytes)
|
||||
save_file("test1", bytes[:])
|
||||
}
|
||||
|
||||
load_file :: proc(path:string) -> []u8 {
|
||||
if !os.is_file(path) {
|
||||
fmt.printfln("File not found")
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := os.read_entire_file_from_filename_or_err(path)
|
||||
if err != nil {
|
||||
fmt.printfln("File read error: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.printfln("Loading file: %v", path)
|
||||
return data
|
||||
}
|
||||
|
||||
save_file :: proc(path:string, data:[]u8) {
|
||||
err := os.write_entire_file_or_err(path, data)
|
||||
if err != nil {
|
||||
fmt.printfln("File write error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.printfln("Wrote file: %v", path)
|
||||
}
|
||||
|
||||
Vec2i :: struct {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
to_bytes :: proc(v: $T) -> [size_of(T)]u8 {
|
||||
val := v
|
||||
encoded_bytes := (^[size_of(T)]u8)(&val)
|
||||
return encoded_bytes^
|
||||
}
|
||||
|
||||
from_bytes :: proc($T:typeid, data: [size_of(T)]u8) -> T {
|
||||
bytes := data
|
||||
decoded_value := (^T)(&bytes)^
|
||||
return decoded_value
|
||||
}
|
||||
|
||||
|
||||
BIN
tests/test1
Normal file
BIN
tests/test1
Normal file
Binary file not shown.
Reference in New Issue
Block a user