package test import "core:fmt" 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 make_struct() 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("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 }