diff --git a/cmd/wasm/wasm.go b/cmd/wasm/wasm.go index e845d01..3d99fc2 100644 --- a/cmd/wasm/wasm.go +++ b/cmd/wasm/wasm.go @@ -6,11 +6,13 @@ import ( "csgo-2d-demo-player/pkg/message" "csgo-2d-demo-player/pkg/parser" "fmt" + "io" "log" "syscall/js" "time" "github.com/markus-wa/demoinfocs-golang/v5/pkg/demoinfocs" + "google.golang.org/protobuf/proto" ) func main() { @@ -21,13 +23,21 @@ func main() { } func testt(this js.Value, args []js.Value) interface{} { - fmt.Printf("testt: +%v\n", args[0].Get("size")) - input := make([]byte, args[0].Get("size")) + fmt.Printf("testt: +%v\n", args[0].Get("length")) + input := make([]byte, args[0].Get("length").Int()) js.CopyBytesToGo(input, args[0]) fmt.Println("fer") - gzipReader, streamErr := gzip.NewReader(bytes.NewReader(input)) + parse(bytes.NewReader(input), args[1]) + + return js.ValueOf("2") +} + +func parse(input io.Reader, callback js.Value) { + fmt.Printf("callback? %+v\n", callback) + + gzipReader, streamErr := gzip.NewReader(input) if streamErr != nil { log.Printf("Failed to create gzip reader from demo. %+v", streamErr) } @@ -35,6 +45,15 @@ func testt(this js.Value, args []js.Value) interface{} { startTime := time.Now().Local() err := parser.Parse(gzipReader, func(msg *message.Message, state demoinfocs.GameState) { fmt.Printf("message: %+v \n", msg.MsgType) + + payload, protoErr := proto.Marshal(msg) + if protoErr != nil { + fmt.Printf("failed to marshall the message: %+v %+v\n", msg, protoErr) + } + arrayConstructor := js.Global().Get("Uint8Array") + dataJS := arrayConstructor.New(len(payload)) + js.CopyBytesToJS(dataJS, payload) + callback.Invoke(dataJS) }) fmt.Printf("parsing took: %s\n", time.Since(startTime)) @@ -42,6 +61,4 @@ func testt(this js.Value, args []js.Value) interface{} { fmt.Printf("Failed to parse a demo: %+v \n", err) } fmt.Println("demo parsed?") - - return js.ValueOf("2") } diff --git a/web/player/src/App.js b/web/player/src/App.js index 08c4102..a5744c5 100644 --- a/web/player/src/App.js +++ b/web/player/src/App.js @@ -11,6 +11,7 @@ function App() { const [messageBus] = useState(new MessageBus()) const [player] = useState(new Player(messageBus)) const [serverHost] = useState(window.location.host.includes("localhost") ? "http://localhost:8080" : ""); + const proto = require("./protos/Message_pb"); useEffect(() => { console.log("run run run") @@ -25,13 +26,28 @@ function App() { .then((result) => { go.run(result.instance); - + // window.withDownload("https://corsproxy.io/?" + encodeURIComponent("https://github.com/sparkoo/csgo-2d-demo-viewer/raw/refs/heads/master/test_demos/1-c26b4e22-66ac-4904-87cc-3b2b65a67ddb-1-1.dem.gz")) fetch("https://corsproxy.io/?" + encodeURIComponent("https://github.com/sparkoo/csgo-2d-demo-viewer/raw/refs/heads/master/test_demos/1-c26b4e22-66ac-4904-87cc-3b2b65a67ddb-1-1.dem.gz")) .then((result) => { console.log(result) - result.blob().then(b => { - console.log(b) - window.testt(b) + result.arrayBuffer().then(b => { + const data = new Uint8Array(b) + console.log(data) + window.testt(data, function (data) { + if(data instanceof Uint8Array) { + const msg = proto.Message.deserializeBinary(data).toObject() + messageBus.emit(msg) + } else { + // text frame + // console.log(event.data); + console.log("[message] text data received from server, this is weird. We're using protobufs ?!?!?", data); + messageBus.emit(JSON.parse(data)) + } + + // console.log(`[message] Data received from server: ${event.data}`); + // let msg = JSON.parse(event.data) + // messageBus.emit(msg) + }) }) }) .catch(err => console.log(err))