Skip to content

Commit

Permalink
it's starting to do something
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkoo committed Dec 4, 2024
1 parent 44bd4bf commit e182956
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
27 changes: 22 additions & 5 deletions cmd/wasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"csgo-2d-demo-player/pkg/message"
"csgo-2d-demo-player/pkg/parser"
"fmt"
"io"
"log"
"syscall/js"

Check failure on line 11 in cmd/wasm/wasm.go

View workflow job for this annotation

GitHub Actions / lint

could not import syscall/js (-: build constraints exclude all Go files in /home/runner/go/pkg/mod/golang.org/[email protected]/src/syscall/js) (typecheck)
"time"

"github.com/markus-wa/demoinfocs-golang/v5/pkg/demoinfocs"
"google.golang.org/protobuf/proto"
)

func main() {
Expand All @@ -21,27 +23,42 @@ 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)
}

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))

if err != nil {
fmt.Printf("Failed to parse a demo: %+v \n", err)
}
fmt.Println("demo parsed?")

return js.ValueOf("2")
}
24 changes: 20 additions & 4 deletions web/player/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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))
Expand Down

0 comments on commit e182956

Please sign in to comment.