|
| 1 | +package greatwave |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/ebitengine/purego" |
| 6 | + operatingsystem "os" |
| 7 | + "runtime" |
| 8 | + "unsafe" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + Max_Array_Size = 4096 |
| 13 | + Max_Array_Cap = 4096 |
| 14 | + |
| 15 | + FruitBanana Fruit = 1 |
| 16 | + FruitApple Fruit = 2 |
| 17 | + FruitOrange Fruit = 3 |
| 18 | + FruitKiwi Fruit = 4 |
| 19 | + FruitApricot Fruit = 5 |
| 20 | + FruitPeach Fruit = 6 |
| 21 | +) |
| 22 | + |
| 23 | +type My_Size_Type uint64 |
| 24 | +type Window struct { |
| 25 | + Name RunicString |
| 26 | + Width uint32 |
| 27 | + Height uint32 |
| 28 | +} |
| 29 | +type Array struct { |
| 30 | + Len My_Size_Type |
| 31 | + Cap My_Size_Type |
| 32 | + Els *uint32 |
| 33 | +} |
| 34 | +type Fruit uint32 |
| 35 | + |
| 36 | +var ( |
| 37 | + CreateWindow func(NAME string, WIDTH uint32, HEIGHT uint32) *Window |
| 38 | + GetWindowHandle func(WINDOW *Window) *operatingsystem.Handle |
| 39 | + CreateArray func(SIZE My_Size_Type) Array |
| 40 | + ArrayAppend func(ARR *Array, VALUE uint32) Array |
| 41 | +) |
| 42 | + |
| 43 | +var ( |
| 44 | + runicSymbols = [][2]any{ |
| 45 | + {&CreateWindow, "create_window"}, |
| 46 | + {&GetWindowHandle, "get_window_handle"}, |
| 47 | + {&CreateArray, "create_array"}, |
| 48 | + {&ArrayAppend, "array_append"}, |
| 49 | + } |
| 50 | + |
| 51 | + runicAllSymbols = [][][2]any{ |
| 52 | + runicSymbols, |
| 53 | + runicSymbolsLinux, |
| 54 | + runicSymbolsWindows, |
| 55 | + } |
| 56 | + |
| 57 | + runicLibraries = map[[2]string]string{ |
| 58 | + {"linux", "any"}: "libfoo.so", |
| 59 | + {"windows", "any"}: "foo.dll", |
| 60 | + } |
| 61 | +) |
| 62 | + |
| 63 | + |
| 64 | +/**************************************************************/ |
| 65 | +/* Public library functions */ |
| 66 | +/**************************************************************/ |
| 67 | + |
| 68 | +// Load the dynamic library and all symbols |
| 69 | +func LoadForeignLibrary() error { |
| 70 | + for _, runicPlatform := range runicPlatforms { |
| 71 | + runicLibraryName, runicLibraryNameOk := runicLibraries[runicPlatform] |
| 72 | + if !runicLibraryNameOk { |
| 73 | + continue |
| 74 | + } |
| 75 | + runicLibrary, runicLibraryErr := purego.Dlopen(runicLibraryName, purego.RTLD_NOW|purego.RTLD_GLOBAL) |
| 76 | + if runicLibraryErr != nil { |
| 77 | + return runicLibraryErr |
| 78 | + } |
| 79 | + runicForeignLibrary = runicLibrary |
| 80 | + } |
| 81 | + |
| 82 | + if runicForeignLibrary == 0 { |
| 83 | + return ErrNoLibraryForPlatform |
| 84 | + } |
| 85 | + |
| 86 | + for _, runicSomeSymbols := range runicAllSymbols { |
| 87 | + if err := runicRegisterSymbols(runicSomeSymbols); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// Unload the library and clean up all resources |
| 96 | +func UnloadForeignLibrary() error { |
| 97 | + err := purego.Dlclose(runicForeignLibrary) |
| 98 | + runicForeignLibrary = 0 |
| 99 | + return err |
| 100 | +} |
| 101 | + |
| 102 | +// Safe. Creates a copy of a C string and returns a go string |
| 103 | +func GoString(str RunicString) string { |
| 104 | + if str == nil { |
| 105 | + return "" |
| 106 | + } |
| 107 | + |
| 108 | + var strLen int |
| 109 | + ptr := uintptr(unsafe.Pointer(str)) |
| 110 | + for { |
| 111 | + c := *(*uint8)(unsafe.Pointer(ptr)) |
| 112 | + if c == 0 { |
| 113 | + break |
| 114 | + } |
| 115 | + |
| 116 | + strLen += 1 |
| 117 | + ptr += unsafe.Sizeof(*str) |
| 118 | + } |
| 119 | + |
| 120 | + slc := unsafe.Slice(str, strLen) |
| 121 | + return string(slc) |
| 122 | +} |
| 123 | + |
| 124 | +// Unsafe. Only use this function if the underlying C string does not change |
| 125 | +func ConstGoString(str RunicString) string { |
| 126 | + if str == nil { |
| 127 | + return "" |
| 128 | + } |
| 129 | + |
| 130 | + var strLen int |
| 131 | + ptr := uintptr(unsafe.Pointer(str)) |
| 132 | + for { |
| 133 | + c := *(*uint8)(unsafe.Pointer(ptr)) |
| 134 | + if c == 0 { |
| 135 | + break |
| 136 | + } |
| 137 | + |
| 138 | + strLen += 1 |
| 139 | + ptr += unsafe.Sizeof(*str) |
| 140 | + } |
| 141 | + |
| 142 | + return unsafe.String((*byte)(unsafe.Pointer(str)), strLen) |
| 143 | +} |
| 144 | + |
| 145 | +/**************************************************************/ |
| 146 | +/* Internal functions */ |
| 147 | +/**************************************************************/ |
| 148 | + |
| 149 | +type RunicString *uint8 |
| 150 | + |
| 151 | +var ( |
| 152 | + ErrNoLibraryForPlatform = errors.New("Current platform does not have a library") |
| 153 | + |
| 154 | + runicForeignLibrary uintptr |
| 155 | + |
| 156 | + runicPlatforms = [][2]string{ |
| 157 | + {runtime.GOOS, runtime.GOARCH}, |
| 158 | + {runtime.GOOS, "any"}, |
| 159 | + {"any", runtime.GOARCH}, |
| 160 | + {"any", "any"}, |
| 161 | + } |
| 162 | +) |
| 163 | + |
| 164 | +func runicRegisterSymbols(runicSymbolsParam [][2]any) error { |
| 165 | + for _, runicEntry := range runicSymbolsParam { |
| 166 | + runicSym, runicSymErr := purego.Dlsym(runicForeignLibrary, runicEntry[1].(string)) |
| 167 | + if runicSymErr != nil { |
| 168 | + return runicSymErr |
| 169 | + } |
| 170 | + |
| 171 | + if runicUintptr, runicIsUintptr := runicEntry[0].(*uintptr); runicIsUintptr { |
| 172 | + *runicUintptr = runicSym |
| 173 | + } else { |
| 174 | + purego.RegisterFunc(runicEntry[0], runicSym) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return nil |
| 179 | +} |
0 commit comments