forked from sampgo/sampgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampgo.go
94 lines (73 loc) · 1.88 KB
/
sampgo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package sampgo
/*
#cgo windows CFLAGS: -I./lib -I./lib/amx -Wno-attributes -Wno-implicit-function-declaration
#cgo windows CFLAGS: -DHAVE_INTTYPES_H -DHAVE_MALLOC_H -DHAVE_STDINT_H -DWIN32
#cgo windows LDFLAGS: -Wl,--subsystem,windows,--kill-at
#cgo linux CFLAGS: -I./lib -I./lib/amx -Wno-attributes -Wno-implicit-function-declaration
#cgo linux CFLAGS: -DHAVE_INTTYPES_H -DHAVE_MALLOC_H -DHAVE_STDINT_H -DLINUX -D_GNU_SOURCE
#cgo linux LDFLAGS: -ldl
#ifndef GOLANG_APP
#define GOLANG_APP
#include "main.h"
#endif
*/
import "C"
import (
"fmt"
"unsafe"
)
type EventType int
const (
Repeat EventType = iota
OnceOnly
)
type event struct {
Handler interface{}
Type EventType
}
var events = make(map[string]event)
var mainEvent func() = nil
//export onTick
func onTick() {
evt, ok := events["tick"]
if !ok {
return
}
fn, ok := evt.Handler.(func())
if !ok {
return
}
fn()
if evt.Type == OnceOnly {
// If this event was registered only once, then reassign this event to a new blank struct.
// PoC code. Still needs to be implemented completely.
}
return
}
// On registers an event with a handler.
func On(eventName string, handler interface{}) error {
_, ok := events[eventName]
if ok {
return fmt.Errorf("this handler already exists")
}
events[eventName] = event{Handler: handler, Type: Repeat}
_ = Print(fmt.Sprintf("Registered %s event", eventName))
return nil
}
// Once registers an event with a handler one time only.
func Once(eventName string, handler interface{}) error {
_, ok := events[eventName]
if ok {
return fmt.Errorf("this handler already exists")
}
events[eventName] = event{Handler: handler, Type: OnceOnly}
_ = Print(fmt.Sprintf("Registered %s event", eventName))
return nil
}
// Print allows you to print to the SAMP console.
func Print(msg string) error {
cstr := C.CString(msg)
defer C.free(unsafe.Pointer(cstr))
C.goLogprintf(cstr)
return nil
}